blob: 194a5cd43cad4ce3eed1650851b1330032b5bb5f [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
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002485static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 const u8 *match, size_t match_len)
2487{
2488 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002489 return nl80211_register_frame(bss, bss->nl_mgmt,
Hai Shalome21d4e82020-04-29 16:34:06 -07002490 type, match, match_len, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491}
2492
2493
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002496 struct wpa_driver_nl80211_data *drv = bss->drv;
Hai Shalomfdcde762020-04-02 11:19:20 -07002497 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002498 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002499
2500 if (nl80211_alloc_mgmt_handle(bss))
2501 return -1;
2502 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2503 "handle %p", bss->nl_mgmt);
2504
Hai Shalomfdcde762020-04-02 11:19:20 -07002505 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002506 /* register for any AUTH message */
Hai Shalome21d4e82020-04-29 16:34:06 -07002507 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07002508 } else if ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2509 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
2510 /* register for SAE Authentication frames */
2511 nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalome21d4e82020-04-29 16:34:06 -07002512 (u8 *) "\x03\x00", 2, false);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002513 }
2514
Hai Shalom60840252021-02-19 19:02:11 -08002515#ifdef CONFIG_PASN
2516 /* register for PASN Authentication frames */
Sunil Ravi89eba102022-09-13 21:04:37 -07002517 if (nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalom60840252021-02-19 19:02:11 -08002518 (u8 *) "\x07\x00", 2, false))
2519 ret = -1;
2520#endif /* CONFIG_PASN */
2521
Dmitry Shmidt051af732013-10-22 13:52:46 -07002522#ifdef CONFIG_INTERWORKING
2523 /* QoS Map Configure */
2524 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002525 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002526#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002527#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002529 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002530 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002532 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002533 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002535 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002536 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002538 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002539 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002540 /* Protected GAS Initial Request */
2541 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2542 ret = -1;
2543 /* Protected GAS Initial Response */
2544 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2545 ret = -1;
2546 /* Protected GAS Comeback Request */
2547 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2548 ret = -1;
2549 /* Protected GAS Comeback Response */
2550 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2551 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002552#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002553#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2557 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002558 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002560 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 (u8 *) "\x7f\x50\x6f\x9a\x09",
2562 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002563 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564#endif /* CONFIG_P2P */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002565#ifdef CONFIG_NAN_USD
2566 /* NAN SDF Public Action */
2567 if (nl80211_register_action_frame(bss,
2568 (u8 *) "\x04\x09\x50\x6f\x9a\x13",
2569 6) < 0)
2570 ret = -1;
2571#endif /* CONFIG_NAN_USD */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002572#ifdef CONFIG_DPP
2573 /* DPP Public Action */
2574 if (nl80211_register_action_frame(bss,
2575 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2576 6) < 0)
2577 ret = -1;
2578#endif /* CONFIG_DPP */
Hai Shalom74f70d42019-02-11 14:42:39 -08002579#ifdef CONFIG_OCV
2580 /* SA Query Request */
2581 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x00", 2) < 0)
2582 ret = -1;
2583#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002585 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002586 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002587#ifdef CONFIG_TDLS
2588 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2589 /* TDLS Discovery Response */
2590 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2591 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002592 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002593 }
2594#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002595#ifdef CONFIG_FST
2596 /* FST Action frames */
2597 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2598 ret = -1;
2599#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600
2601 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002602 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002603 ret = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002604 else if (!drv->has_driver_key_mgmt) {
2605 int i;
2606
2607 /* Update supported AKMs only if the driver doesn't advertize
2608 * any AKM capabilities. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2610 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2611
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002612 /* Update per interface supported AKMs */
2613 for (i = 0; i < WPA_IF_MAX; i++)
2614 drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
2615 }
2616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002617 /* WNM - BSS Transition Management Request */
2618 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002619 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002620 /* WNM-Sleep Mode Response */
2621 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002622 ret = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002623#ifdef CONFIG_WNM
2624 /* WNM - Collocated Interference Request */
2625 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2626 ret = -1;
2627#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002628
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002629#ifdef CONFIG_HS20
2630 /* WNM-Notification */
2631 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002632 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002633#endif /* CONFIG_HS20 */
2634
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002635 /* WMM-AC ADDTS Response */
2636 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2637 ret = -1;
2638
2639 /* WMM-AC DELTS */
2640 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2641 ret = -1;
2642
2643 /* Radio Measurement - Neighbor Report Response */
2644 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2645 ret = -1;
2646
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002647 /* Radio Measurement - Radio Measurement Request */
Hai Shalom899fcc72020-10-19 14:38:18 -07002648 if (!drv->no_rrm &&
2649 nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002650 ret = -1;
2651
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002652 /* Radio Measurement - Link Measurement Request */
2653 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2654 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2655 ret = -1;
2656
Hai Shalomc1a21442022-02-04 13:43:00 -08002657 /* Robust AV SCS Response */
2658 if (nl80211_register_action_frame(bss, (u8 *) "\x13\x01", 2) < 0)
2659 ret = -1;
2660
Hai Shalom899fcc72020-10-19 14:38:18 -07002661 /* Robust AV MSCS Response */
2662 if (nl80211_register_action_frame(bss, (u8 *) "\x13\x05", 2) < 0)
2663 ret = -1;
2664
Hai Shalomc1a21442022-02-04 13:43:00 -08002665 /* Protected QoS Management Action frame */
2666 if (nl80211_register_action_frame(bss, (u8 *) "\x7e\x50\x6f\x9a\x1a",
2667 5) < 0)
2668 ret = -1;
2669
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002670 nl80211_mgmt_handle_register_eloop(bss);
2671
2672 return ret;
2673}
2674
2675
2676static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2677{
2678 int ret = 0;
2679
2680 if (nl80211_alloc_mgmt_handle(bss))
2681 return -1;
2682
2683 wpa_printf(MSG_DEBUG,
2684 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2685 bss->nl_mgmt);
2686
2687 /* Auth frames for mesh SAE */
2688 if (nl80211_register_frame(bss, bss->nl_mgmt,
2689 (WLAN_FC_TYPE_MGMT << 2) |
2690 (WLAN_FC_STYPE_AUTH << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002691 NULL, 0, false) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002692 ret = -1;
2693
2694 /* Mesh peering open */
2695 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2696 ret = -1;
2697 /* Mesh peering confirm */
2698 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2699 ret = -1;
2700 /* Mesh peering close */
2701 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2702 ret = -1;
2703
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002704 nl80211_mgmt_handle_register_eloop(bss);
2705
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002706 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707}
2708
2709
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002710static int nl80211_register_spurious_class3(struct i802_bss *bss)
2711{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002712 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002713 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002714
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002715 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
Hai Shalom899fcc72020-10-19 14:38:18 -07002716 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002717 NULL, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002718 if (ret) {
2719 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2720 "failed: ret=%d (%s)",
2721 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002722 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002723 return ret;
2724}
2725
2726
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002727static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2728{
2729 int ret = 0;
2730
2731 /* Public Action frames */
2732 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2733 ret = -1;
2734 /* RRM Measurement Report */
2735 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2736 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002737 /* RRM Link Measurement Report */
2738 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2739 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002740 /* RRM Neighbor Report Request */
2741 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2742 ret = -1;
2743 /* FT Action frames */
2744 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2745 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002746 /* SA Query */
2747 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2748 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002749 /* Protected Dual of Public Action */
2750 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2751 ret = -1;
2752 /* WNM */
2753 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2754 ret = -1;
2755 /* WMM */
2756 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2757 ret = -1;
2758#ifdef CONFIG_FST
2759 /* FST Action frames */
2760 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2761 ret = -1;
2762#endif /* CONFIG_FST */
2763 /* Vendor-specific */
2764 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2765 ret = -1;
2766
2767 return ret;
2768}
2769
2770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002771static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2772{
2773 static const int stypes[] = {
2774 WLAN_FC_STYPE_AUTH,
2775 WLAN_FC_STYPE_ASSOC_REQ,
2776 WLAN_FC_STYPE_REASSOC_REQ,
2777 WLAN_FC_STYPE_DISASSOC,
2778 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002779 WLAN_FC_STYPE_PROBE_REQ,
2780/* Beacon doesn't work as mac80211 doesn't currently allow
2781 * it, but it wouldn't really be the right thing anyway as
2782 * it isn't per interface ... maybe just dump the scan
2783 * results periodically for OLBC?
2784 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002785 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002786 };
2787 unsigned int i;
2788
2789 if (nl80211_alloc_mgmt_handle(bss))
2790 return -1;
2791 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2792 "handle %p", bss->nl_mgmt);
2793
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002794 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002795 if (nl80211_register_frame(bss, bss->nl_mgmt,
2796 (WLAN_FC_TYPE_MGMT << 2) |
2797 (stypes[i] << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002798 NULL, 0, false) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002799 goto out_err;
2800 }
2801 }
2802
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002803 if (nl80211_action_subscribe_ap(bss))
2804 goto out_err;
2805
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002806 if (nl80211_register_spurious_class3(bss))
2807 goto out_err;
2808
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002809 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002810 return 0;
2811
2812out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002813 nl_destroy_handles(&bss->nl_mgmt);
2814 return -1;
2815}
2816
2817
2818static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2819{
2820 if (nl80211_alloc_mgmt_handle(bss))
2821 return -1;
2822 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2823 "handle %p (device SME)", bss->nl_mgmt);
2824
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002825 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826 goto out_err;
2827
Hai Shalom5f92bc92019-04-18 11:54:11 -07002828 if (bss->drv->device_ap_sme) {
2829 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2830
2831 /* Register for all Authentication frames */
Hai Shalome21d4e82020-04-29 16:34:06 -07002832 if (nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0,
2833 false) < 0)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002834 wpa_printf(MSG_DEBUG,
2835 "nl80211: Failed to subscribe to handle Authentication frames - SAE offload may not work");
2836 }
2837
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002838 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002839 return 0;
2840
2841out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 nl_destroy_handles(&bss->nl_mgmt);
2843 return -1;
2844}
2845
2846
2847static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2848{
2849 if (bss->nl_mgmt == NULL)
2850 return;
2851 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2852 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002853 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002854
2855 nl80211_put_wiphy_data_ap(bss);
2856}
2857
2858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2860{
2861 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2862}
2863
2864
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002865static void nl80211_del_p2pdev(struct i802_bss *bss)
2866{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002867 struct nl_msg *msg;
2868 int ret;
2869
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002870 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002871 ret = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002872
2873 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2874 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002875 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002876}
2877
2878
2879static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2880{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002881 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002882 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002883
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002884 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2885 NL80211_CMD_STOP_P2P_DEVICE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002886 ret = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002887
2888 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2889 start ? "Start" : "Stop",
2890 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002891 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002892 return ret;
2893}
2894
2895
2896static int i802_set_iface_flags(struct i802_bss *bss, int up)
2897{
2898 enum nl80211_iftype nlmode;
2899
2900 nlmode = nl80211_get_ifmode(bss);
2901 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2902 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2903 bss->ifname, up);
2904 }
2905
2906 /* P2P Device has start/stop which is equivalent */
2907 return nl80211_set_p2pdev(bss, up);
2908}
2909
2910
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002911#ifdef CONFIG_TESTING_OPTIONS
2912static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2913{
2914 /* struct wpa_driver_nl80211_data *drv = arg; */
2915 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2916 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2917
2918
2919 wpa_printf(MSG_DEBUG,
2920 "nl80211: QCA vendor test command response received");
2921
2922 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2923 genlmsg_attrlen(gnlh, 0), NULL);
2924 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2925 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2926 return NL_SKIP;
2927 }
2928
2929 wpa_hexdump(MSG_DEBUG,
2930 "nl80211: Received QCA vendor test command response",
2931 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2932 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2933
2934 return NL_SKIP;
2935}
2936#endif /* CONFIG_TESTING_OPTIONS */
2937
2938
2939static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2940{
2941#ifdef CONFIG_TESTING_OPTIONS
2942 struct nl_msg *msg;
2943 struct nlattr *params;
2944 int ret;
2945
2946 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2947 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2948 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2949 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2950 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2951 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2952 nlmsg_free(msg);
2953 return;
2954 }
2955 nla_nest_end(msg, params);
2956
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002957 ret = send_and_recv_resp(drv, msg, qca_vendor_test_cmd_handler, drv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002958 wpa_printf(MSG_DEBUG,
2959 "nl80211: QCA vendor test command returned %d (%s)",
2960 ret, strerror(-ret));
2961#endif /* CONFIG_TESTING_OPTIONS */
2962}
2963
2964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002966wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002967 const u8 *set_addr, int first,
2968 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002970 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002972 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973
2974 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002975 bss->ifindex = drv->ifindex;
2976 bss->wdev_id = drv->global->if_add_wdevid;
2977 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2978
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002979 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2980 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002981 drv->global->if_add_wdevid_set = 0;
2982
Dmitry Shmidt03658832014-08-13 11:03:49 -07002983 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2984 bss->static_ap = 1;
2985
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002986 if (first &&
2987 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2988 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2989 drv->start_iface_up = 1;
2990
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002991 if (wpa_driver_nl80211_capa(drv))
2992 return -1;
2993
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002994 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2995 return -1;
2996
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002997 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2998 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003000 if (set_addr &&
3001 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
3002 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3003 set_addr)))
3004 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003005
Hai Shalomc1a21442022-02-04 13:43:00 -08003006 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_STATION)
3007 drv->start_mode_sta = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003008
Dmitry Shmidt03658832014-08-13 11:03:49 -07003009 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003010 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07003011 else if (bss->if_dynamic ||
3012 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003013 nlmode = nl80211_get_ifmode(bss);
3014 else
3015 nlmode = NL80211_IFTYPE_STATION;
3016
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003017 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003018 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003019 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003021
Dmitry Shmidt98660862014-03-11 17:26:21 -07003022 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003023 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003024
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003025 wpa_driver_nl80211_drv_init_rfkill(drv);
3026
Dmitry Shmidt98660862014-03-11 17:26:21 -07003027 if (!rfkill_is_blocked(drv->rfkill)) {
3028 int ret = i802_set_iface_flags(bss, 1);
3029 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3031 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07003032 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003034
3035 if (is_p2p_net_interface(nlmode))
3036 nl80211_disable_11b_rates(bss->drv,
3037 bss->drv->ifindex, 1);
3038
Dmitry Shmidt98660862014-03-11 17:26:21 -07003039 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
3040 return ret;
3041 } else {
3042 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3043 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003044 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
3045 drv->if_disabled = 1;
3046
Dmitry Shmidt98660862014-03-11 17:26:21 -07003047 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 }
3049
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003050 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003051 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
3052 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003054 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
3055 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3056 bss->addr))
3057 return -1;
3058 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
3059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 if (send_rfkill_event) {
3062 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3063 drv, drv->ctx);
3064 }
3065
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003066 if (drv->vendor_cmd_test_avail)
3067 qca_vendor_test(drv);
3068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 return 0;
3070}
3071
3072
Sunil Ravi036cec52023-03-29 11:35:17 -07003073static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss,
Sunil Ravi99c035e2024-07-12 01:42:03 +00003074 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075{
3076 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08003077 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00003078 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079
Sunil Ravi036cec52023-03-29 11:35:17 -07003080 if (!link->beacon_set)
3081 return 0;
3082
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003083 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
Sunil Ravi99c035e2024-07-12 01:42:03 +00003084 bss->ifindex);
Sunil Ravi036cec52023-03-29 11:35:17 -07003085 link->beacon_set = 0;
3086 link->freq = 0;
3087
Paul Stewart092955c2017-02-06 09:13:09 -08003088 nl80211_put_wiphy_data_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00003089 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_BEACON);
Sunil Ravi036cec52023-03-29 11:35:17 -07003090 if (!msg)
3091 return -ENOBUFS;
3092
Sunil Ravi99c035e2024-07-12 01:42:03 +00003093 if (link_id != NL80211_DRV_LINK_ID_NA) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003094 wpa_printf(MSG_DEBUG,
3095 "nl80211: MLD: stop beaconing on link=%u",
Sunil Ravi99c035e2024-07-12 01:42:03 +00003096 link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -07003097
Sunil Ravi99c035e2024-07-12 01:42:03 +00003098 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003099 nlmsg_free(msg);
3100 return -ENOBUFS;
3101 }
3102 }
3103
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003104 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105}
3106
3107
Sunil Ravi036cec52023-03-29 11:35:17 -07003108static void wpa_driver_nl80211_del_beacon_all(struct i802_bss *bss)
3109{
Sunil Ravi99c035e2024-07-12 01:42:03 +00003110 int link_id;
Sunil Ravi036cec52023-03-29 11:35:17 -07003111
Sunil Ravi99c035e2024-07-12 01:42:03 +00003112 for_each_link_default(bss->valid_links, link_id, NL80211_DRV_LINK_ID_NA)
3113 wpa_driver_nl80211_del_beacon(bss, link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -07003114}
3115
3116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117/**
3118 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003119 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 *
3121 * Shut down driver interface and processing of driver events. Free
3122 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3123 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003124static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003127 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003129 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
3130 bss->ifname, drv->disabled_11b_rates);
3131
Dmitry Shmidt04949592012-07-19 12:16:46 -07003132 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003133 if (drv->data_tx_status)
3134 eloop_unregister_read_sock(drv->eapol_tx_sock);
3135 if (drv->eapol_tx_sock >= 0)
3136 close(drv->eapol_tx_sock);
3137
3138 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 wpa_driver_nl80211_probe_req_report(bss, 0);
3140 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003141 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3142 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3144 "interface %s from bridge %s: %s",
3145 bss->ifname, bss->brname, strerror(errno));
3146 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07003147
3148 if (drv->rtnl_sk)
Hai Shalomfdcde762020-04-02 11:19:20 -07003149 nl_socket_free(drv->rtnl_sk);
Hai Shalomc9e41a12018-07-31 14:41:42 -07003150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003152 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
3153 0) < 0)
3154 wpa_printf(MSG_INFO,
3155 "nl80211: Could not set bridge %s down",
3156 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003157 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3159 "bridge %s: %s",
3160 bss->brname, strerror(errno));
3161 }
3162
3163 nl80211_remove_monitor_interface(drv);
3164
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003165 if (is_ap_interface(drv->nlmode)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003166 wpa_driver_nl80211_del_beacon_all(bss);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003167 nl80211_remove_links(bss);
3168 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170 if (drv->eapol_sock >= 0) {
3171 eloop_unregister_read_sock(drv->eapol_sock);
3172 close(drv->eapol_sock);
3173 }
3174
3175 if (drv->if_indices != drv->default_if_indices)
3176 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003178 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003181 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3182 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003183 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 rfkill_deinit(drv->rfkill);
3185
3186 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3187
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003188 if (!drv->start_iface_up)
3189 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003190
3191 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003192 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
3193 0) < 0) {
3194 wpa_printf(MSG_DEBUG,
3195 "nl80211: Could not set interface down to restore permanent MAC address");
3196 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003197 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3198 drv->perm_addr) < 0) {
3199 wpa_printf(MSG_DEBUG,
3200 "nl80211: Could not restore permanent MAC address");
3201 }
3202 }
3203
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003204 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003205 if (drv->start_mode_sta)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003206 wpa_driver_nl80211_set_mode(bss,
3207 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003208 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003209 } else {
3210 nl80211_mgmt_unsubscribe(bss, "deinit");
3211 nl80211_del_p2pdev(bss);
3212 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003214 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 os_free(drv->filter_ssids);
3217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003218 os_free(drv->auth_ie);
Hai Shalom60840252021-02-19 19:02:11 -08003219 os_free(drv->auth_data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003220
3221 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 dl_list_del(&drv->list);
3223
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003224 os_free(drv->extended_capa);
3225 os_free(drv->extended_capa_mask);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003226 for (i = 0; i < drv->num_iface_capa; i++) {
3227 os_free(drv->iface_capa[i].ext_capa);
3228 os_free(drv->iface_capa[i].ext_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003229 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003230 os_free(drv->first_bss);
Hai Shalom60840252021-02-19 19:02:11 -08003231#ifdef CONFIG_DRIVER_NL80211_QCA
3232 os_free(drv->pending_roam_data);
3233#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 os_free(drv);
3235}
3236
3237
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003238static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
3239{
3240 switch (alg) {
3241 case WPA_ALG_WEP:
3242 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08003243 return RSN_CIPHER_SUITE_WEP40;
3244 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003245 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08003246 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003247 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003248 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003249 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003250 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003251 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003252 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003253 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003254 return RSN_CIPHER_SUITE_GCMP_256;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003255 case WPA_ALG_BIP_CMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08003256 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003257 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08003258 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003259 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003260 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003261 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003262 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003263 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08003264 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003265 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08003266 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003267 case WPA_ALG_NONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003268 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
3269 alg);
3270 return 0;
3271 }
3272
3273 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
3274 alg);
3275 return 0;
3276}
3277
3278
3279static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
3280{
3281 switch (cipher) {
3282 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003283 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003284 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08003285 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003286 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003287 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003288 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08003289 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003290 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08003291 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003292 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08003293 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003294 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08003295 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003296 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08003297 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003298 default:
3299 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003300 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003301}
3302
3303
3304static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
3305 int max_suites)
3306{
3307 int num_suites = 0;
3308
3309 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08003310 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003311 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08003312 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003313 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08003314 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003315 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08003316 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003317 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08003318 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003319 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08003320 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003321 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08003322 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003323
3324 return num_suites;
3325}
3326
3327
Hai Shalomfdcde762020-04-02 11:19:20 -07003328static int wpa_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3329 int max_suites)
3330{
3331 int num_suites = 0;
3332
3333#define __AKM(a, b) \
3334 if (num_suites < max_suites && \
3335 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3336 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3337 __AKM(IEEE8021X, UNSPEC_802_1X);
3338 __AKM(PSK, PSK_OVER_802_1X);
3339 __AKM(FT_IEEE8021X, FT_802_1X);
3340 __AKM(FT_PSK, FT_PSK);
3341 __AKM(IEEE8021X_SHA256, 802_1X_SHA256);
3342 __AKM(PSK_SHA256, PSK_SHA256);
3343 __AKM(SAE, SAE);
Sunil Ravi89eba102022-09-13 21:04:37 -07003344 __AKM(SAE_EXT_KEY, SAE_EXT_KEY);
Hai Shalomfdcde762020-04-02 11:19:20 -07003345 __AKM(FT_SAE, FT_SAE);
Sunil Ravi89eba102022-09-13 21:04:37 -07003346 __AKM(FT_SAE_EXT_KEY, FT_SAE_EXT_KEY);
Hai Shalomfdcde762020-04-02 11:19:20 -07003347 __AKM(CCKM, CCKM);
3348 __AKM(OSEN, OSEN);
3349 __AKM(IEEE8021X_SUITE_B, 802_1X_SUITE_B);
3350 __AKM(IEEE8021X_SUITE_B_192, 802_1X_SUITE_B_192);
3351 __AKM(FILS_SHA256, FILS_SHA256);
3352 __AKM(FILS_SHA384, FILS_SHA384);
3353 __AKM(FT_FILS_SHA256, FT_FILS_SHA256);
3354 __AKM(FT_FILS_SHA384, FT_FILS_SHA384);
3355 __AKM(OWE, OWE);
3356 __AKM(DPP, DPP);
3357 __AKM(FT_IEEE8021X_SHA384, FT_802_1X_SHA384);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003358 __AKM(IEEE8021X_SHA384, 802_1X_SHA384);
Hai Shalomfdcde762020-04-02 11:19:20 -07003359#undef __AKM
3360
3361 return num_suites;
3362}
3363
3364
Isaac Chiou6ce580d2024-04-24 17:07:24 +08003365#if (defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(WIFI_BRCM_OPEN_SOURCE_MULTI_AKM)) || \
3366 defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05303367static int wpa_cross_akm_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3368 int max_suites)
3369{
3370 int num_suites = 0;
3371
3372#define __AKM_TO_SUITES_ARRAY(a, b) \
3373 if (num_suites < max_suites && \
3374 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3375 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3376 __AKM_TO_SUITES_ARRAY(PSK, PSK_OVER_802_1X);
3377 __AKM_TO_SUITES_ARRAY(SAE, SAE);
3378#undef __AKM_TO_SUITES_ARRAY
3379
3380 return num_suites;
3381}
Isaac Chiou6ce580d2024-04-24 17:07:24 +08003382#endif /* (CONFIG_DRIVER_NL80211_BRCM && !WIFI_BRCM_OPEN_SOURCE_MULTI_AKM) ||
3383 * CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawad14709082022-03-17 14:25:11 +05303384
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003385
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003386#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003387static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3388 const u8 *key, size_t key_len)
3389{
3390 struct nl_msg *msg;
3391 int ret;
3392
3393 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
3394 return 0;
3395
3396 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3397 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3398 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3399 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
3400 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
3401 nl80211_nlmsg_clear(msg);
3402 nlmsg_free(msg);
3403 return -1;
3404 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003405 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003406 if (ret) {
3407 wpa_printf(MSG_DEBUG,
3408 "nl80211: Key management set key failed: ret=%d (%s)",
3409 ret, strerror(-ret));
3410 }
3411
3412 return ret;
3413}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003414#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003415
3416
Andy Kuoaba17c12022-04-14 16:05:31 +08003417#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303418static int key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3419 const u8 *key, size_t key_len)
3420{
3421 struct nl_msg *msg;
3422 int ret;
3423 struct nlattr *params;
3424
3425 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3426 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
3427 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
Hai Shalomc1a21442022-02-04 13:43:00 -08003428 BRCM_VENDOR_SCMD_SET_PMK) ||
Mir Ali677e7482020-11-12 19:49:02 +05303429 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
3430 nla_put(msg, BRCM_ATTR_DRIVER_KEY_PMK, key_len, key)) {
3431 nl80211_nlmsg_clear(msg);
3432 nlmsg_free(msg);
3433 return -ENOBUFS;
3434 }
3435 nla_nest_end(msg, params);
3436
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003437 ret = send_and_recv_cmd(drv, msg);
Mir Ali677e7482020-11-12 19:49:02 +05303438 if (ret) {
3439 wpa_printf(MSG_DEBUG, "nl80211: Key mgmt set key failed: ret=%d (%s)",
3440 ret, strerror(-ret));
3441 }
3442
3443 return ret;
3444}
Andy Kuoaba17c12022-04-14 16:05:31 +08003445#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3446
Mir Ali677e7482020-11-12 19:49:02 +05303447
Roshan Pius3a1667e2018-07-03 15:17:14 -07003448static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
3449 const u8 *key, size_t key_len,
3450 const u8 *addr)
3451{
3452 struct nl_msg *msg = NULL;
3453 int ret;
3454
3455 /*
3456 * If the authenticator address is not set, assume it is
3457 * the current BSSID.
3458 */
3459 if (!addr && drv->associated)
3460 addr = drv->bssid;
3461 else if (!addr)
3462 return -1;
3463
3464 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
3465 MAC2STR(addr));
3466 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
3467 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
3468 if (!msg ||
3469 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3470 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
3471 nl80211_nlmsg_clear(msg);
3472 nlmsg_free(msg);
3473 return -ENOBUFS;
3474 }
3475
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003476 ret = send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003477 if (ret) {
3478 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
3479 ret, strerror(-ret));
3480 }
3481
3482 return ret;
3483}
3484
3485
Hai Shalomfdcde762020-04-02 11:19:20 -07003486static int wpa_driver_nl80211_set_key(struct i802_bss *bss,
3487 struct wpa_driver_set_key_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003490 int ifindex;
Hai Shalomc3565922019-10-28 11:58:20 -07003491 struct nl_msg *msg;
3492 struct nl_msg *key_msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003494 int skip_set_key = 1;
3495 const char *ifname = params->ifname;
3496 enum wpa_alg alg = params->alg;
3497 const u8 *addr = params->addr;
3498 int key_idx = params->key_idx;
3499 int set_tx = params->set_tx;
3500 const u8 *seq = params->seq;
3501 size_t seq_len = params->seq_len;
3502 const u8 *key = params->key;
3503 size_t key_len = params->key_len;
3504 int vlan_id = params->vlan_id;
3505 enum key_flag key_flag = params->key_flag;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003506 int link_id = params->link_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003508 /* Ignore for P2P Device */
3509 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
3510 return 0;
3511
3512 ifindex = if_nametoindex(ifname);
3513 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Sunil Ravi77d572f2023-01-17 23:58:31 +00003514 "set_tx=%d seq_len=%lu key_len=%lu key_flag=0x%x link_id=%d",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003515 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003516 (unsigned long) seq_len, (unsigned long) key_len, key_flag,
3517 link_id);
Hai Shalomfdcde762020-04-02 11:19:20 -07003518
3519 if (check_key_flag(key_flag)) {
3520 wpa_printf(MSG_DEBUG, "%s: invalid key_flag", __func__);
3521 return -EINVAL;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003524#ifdef CONFIG_DRIVER_NL80211_QCA
Hai Shalomfdcde762020-04-02 11:19:20 -07003525 if ((key_flag & KEY_FLAG_PMK) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003526 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
3527 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
3528 __func__);
3529 ret = issue_key_mgmt_set_key(drv, key, key_len);
3530 return ret;
3531 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003532#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533
Hai Shalomfdcde762020-04-02 11:19:20 -07003534 if (key_flag & KEY_FLAG_PMK) {
3535 if (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)
3536 return nl80211_set_pmk(drv, key, key_len, addr);
Andy Kuoaba17c12022-04-14 16:05:31 +08003537#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303538 if (drv->vendor_set_pmk) {
Jay Patel731adae2021-02-17 14:55:58 -08003539 wpa_printf(MSG_INFO, "nl80211: key_mgmt_set_key with key_len %lu", (unsigned long) key_len);
Mir Ali677e7482020-11-12 19:49:02 +05303540 return key_mgmt_set_key(drv, key, key_len);
3541 }
Andy Kuoaba17c12022-04-14 16:05:31 +08003542#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3543
Hai Shalomfdcde762020-04-02 11:19:20 -07003544 /* The driver does not have any offload mechanism for PMK, so
3545 * there is no need to configure this key. */
3546 return 0;
3547 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003548
Hai Shalomfdcde762020-04-02 11:19:20 -07003549 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003550 key_msg = nlmsg_alloc();
3551 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003552 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003553
Hai Shalomfdcde762020-04-02 11:19:20 -07003554 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3555 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3556 wpa_printf(MSG_DEBUG,
3557 "nl80211: SET_KEY (pairwise RX/TX modify)");
3558 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3559 if (!msg)
3560 goto fail2;
3561 } else if (alg == WPA_ALG_NONE && (key_flag & KEY_FLAG_RX_TX)) {
3562 wpa_printf(MSG_DEBUG, "%s: invalid key_flag to delete key",
3563 __func__);
3564 ret = -EINVAL;
3565 goto fail2;
3566 } else if (alg == WPA_ALG_NONE) {
3567 wpa_printf(MSG_DEBUG, "nl80211: DEL_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003568 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
3569 if (!msg)
Hai Shalomc3565922019-10-28 11:58:20 -07003570 goto fail2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003572 u32 suite;
3573
3574 suite = wpa_alg_to_cipher_suite(alg, key_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07003575 if (!suite) {
3576 ret = -EINVAL;
Hai Shalomc3565922019-10-28 11:58:20 -07003577 goto fail2;
Hai Shalomfdcde762020-04-02 11:19:20 -07003578 }
3579 wpa_printf(MSG_DEBUG, "nl80211: NEW_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003580 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003581 if (!msg)
3582 goto fail2;
3583 if (nla_put(key_msg, NL80211_KEY_DATA, key_len, key) ||
3584 nla_put_u32(key_msg, NL80211_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003585 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003586 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587
Hai Shalomfdcde762020-04-02 11:19:20 -07003588 if (seq && seq_len) {
3589 if (nla_put(key_msg, NL80211_KEY_SEQ, seq_len, seq))
3590 goto fail;
3591 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ",
3592 seq, seq_len);
3593 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595
3596 if (addr && !is_broadcast_ether_addr(addr)) {
3597 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003598 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
3599 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600
Hai Shalomfdcde762020-04-02 11:19:20 -07003601 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3602 KEY_FLAG_PAIRWISE_RX ||
3603 (key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3604 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3605 if (nla_put_u8(key_msg, NL80211_KEY_MODE,
3606 key_flag == KEY_FLAG_PAIRWISE_RX ?
3607 NL80211_KEY_NO_TX : NL80211_KEY_SET_TX))
3608 goto fail;
3609 } else if ((key_flag & KEY_FLAG_GROUP_MASK) ==
3610 KEY_FLAG_GROUP_RX) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Hai Shalomc3565922019-10-28 11:58:20 -07003612 if (nla_put_u32(key_msg, NL80211_KEY_TYPE,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003613 NL80211_KEYTYPE_GROUP))
3614 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003615 } else if (!(key_flag & KEY_FLAG_PAIRWISE)) {
3616 wpa_printf(MSG_DEBUG,
3617 " key_flag missing PAIRWISE when setting a pairwise key");
3618 ret = -EINVAL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003619 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003620 } else if (alg == WPA_ALG_WEP &&
3621 (key_flag & KEY_FLAG_RX_TX) == KEY_FLAG_RX_TX) {
3622 wpa_printf(MSG_DEBUG, " unicast WEP key");
3623 skip_set_key = 0;
3624 } else {
3625 wpa_printf(MSG_DEBUG, " pairwise key");
3626 }
3627 } else if ((key_flag & KEY_FLAG_PAIRWISE) ||
3628 !(key_flag & KEY_FLAG_GROUP)) {
3629 wpa_printf(MSG_DEBUG,
3630 " invalid key_flag for a broadcast key");
3631 ret = -EINVAL;
3632 goto fail;
3633 } else {
3634 wpa_printf(MSG_DEBUG, " broadcast key");
3635 if (key_flag & KEY_FLAG_DEFAULT)
3636 skip_set_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 }
Hai Shalomc3565922019-10-28 11:58:20 -07003638 if (nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
3639 nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003640 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003641 nl80211_nlmsg_clear(key_msg);
3642 nlmsg_free(key_msg);
3643 key_msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644
Hai Shalomfdcde762020-04-02 11:19:20 -07003645 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3646 wpa_printf(MSG_DEBUG, "nl80211: VLAN ID %d", vlan_id);
3647 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3648 goto fail;
3649 }
3650
Sunil Ravi77d572f2023-01-17 23:58:31 +00003651 if (link_id != -1) {
3652 wpa_printf(MSG_DEBUG, "nl80211: Link ID %d", link_id);
3653 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3654 goto fail;
3655 }
3656
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003657 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3659 ret = 0;
3660 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003661 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 ret, strerror(-ret));
3663
3664 /*
Hai Shalomfdcde762020-04-02 11:19:20 -07003665 * If we failed or don't need to set the key as default (below),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 * we're done here.
3667 */
Hai Shalomfdcde762020-04-02 11:19:20 -07003668 if (ret || skip_set_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 return ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003670 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_SET_KEY - default key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671
Hai Shalomfdcde762020-04-02 11:19:20 -07003672 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003673 key_msg = nlmsg_alloc();
3674 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003675 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003676
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003677 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003678 if (!msg)
3679 goto fail2;
3680 if (!key_msg ||
3681 nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003682 nla_put_flag(key_msg, wpa_alg_bip(alg) ?
Hai Shalomfdcde762020-04-02 11:19:20 -07003683 (key_idx == 6 || key_idx == 7 ?
3684 NL80211_KEY_DEFAULT_BEACON :
3685 NL80211_KEY_DEFAULT_MGMT) :
3686 NL80211_KEY_DEFAULT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003687 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003689 struct nlattr *types;
3690
Hai Shalomc3565922019-10-28 11:58:20 -07003691 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003692 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003693 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003694 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003695 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003697 struct nlattr *types;
3698
Hai Shalomc3565922019-10-28 11:58:20 -07003699 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003700 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003701 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003702 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003703 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 }
3705
Hai Shalomc3565922019-10-28 11:58:20 -07003706 if (nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
3707 goto fail;
3708 nl80211_nlmsg_clear(key_msg);
3709 nlmsg_free(key_msg);
3710 key_msg = NULL;
3711
Hai Shalomfdcde762020-04-02 11:19:20 -07003712 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3713 wpa_printf(MSG_DEBUG, "nl80211: set_key default - VLAN ID %d",
3714 vlan_id);
3715 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3716 goto fail;
3717 }
3718
Sunil Ravi77d572f2023-01-17 23:58:31 +00003719 if (link_id != -1) {
3720 wpa_printf(MSG_DEBUG, "nl80211: set_key default - Link ID %d",
3721 link_id);
3722 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3723 goto fail;
3724 }
3725
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003726 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003728 wpa_printf(MSG_DEBUG,
3729 "nl80211: set_key default failed; err=%d %s",
3730 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731 return ret;
3732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003733fail:
3734 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003735 nlmsg_free(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07003736fail2:
3737 nl80211_nlmsg_clear(key_msg);
3738 nlmsg_free(key_msg);
Hai Shalomfdcde762020-04-02 11:19:20 -07003739 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740}
3741
3742
3743static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3744 int key_idx, int defkey,
3745 const u8 *seq, size_t seq_len,
3746 const u8 *key, size_t key_len)
3747{
3748 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003749 u32 suite;
3750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 if (!key_attr)
3752 return -1;
3753
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003754 suite = wpa_alg_to_cipher_suite(alg, key_len);
3755 if (!suite)
3756 return -1;
3757
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003758 if (defkey && wpa_alg_bip(alg)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003759 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3760 return -1;
3761 } else if (defkey) {
3762 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3763 return -1;
3764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003766 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003767 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003768 (seq && seq_len &&
3769 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3770 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3771 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772
3773 nla_nest_end(msg, key_attr);
3774
3775 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003776}
3777
3778
3779static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3780 struct nl_msg *msg)
3781{
3782 int i, privacy = 0;
3783 struct nlattr *nl_keys, *nl_key;
3784
3785 for (i = 0; i < 4; i++) {
3786 if (!params->wep_key[i])
3787 continue;
3788 privacy = 1;
3789 break;
3790 }
3791 if (params->wps == WPS_MODE_PRIVACY)
3792 privacy = 1;
3793 if (params->pairwise_suite &&
3794 params->pairwise_suite != WPA_CIPHER_NONE)
3795 privacy = 1;
3796
3797 if (!privacy)
3798 return 0;
3799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003800 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3801 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802
3803 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3804 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003805 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806
3807 for (i = 0; i < 4; i++) {
3808 if (!params->wep_key[i])
3809 continue;
3810
3811 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003812 if (!nl_key ||
3813 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3814 params->wep_key[i]) ||
3815 nla_put_u32(msg, NL80211_KEY_CIPHER,
3816 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003817 RSN_CIPHER_SUITE_WEP40 :
3818 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003819 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3820 (i == params->wep_tx_keyidx &&
3821 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3822 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823
3824 nla_nest_end(msg, nl_key);
3825 }
3826 nla_nest_end(msg, nl_keys);
3827
3828 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829}
3830
3831
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003832int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3833 const u8 *addr, int cmd, u16 reason_code,
Hai Shalom74f70d42019-02-11 14:42:39 -08003834 int local_state_change,
Hai Shalomc1a21442022-02-04 13:43:00 -08003835 struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003837 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 struct nl_msg *msg;
3839
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003840 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3841 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3842 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3843 (local_state_change &&
3844 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3845 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003849 ret = send_and_recv(drv->global, bss->nl_connect, msg,
3850 NULL, NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003852 wpa_dbg(drv->ctx, MSG_DEBUG,
3853 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3854 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 return ret;
3857}
3858
3859
3860static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Hai Shalom81f62d82019-07-22 12:10:00 -07003861 u16 reason_code,
Hai Shalomc1a21442022-02-04 13:43:00 -08003862 struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003864 int ret;
3865
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003866 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003867 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003868 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003869 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Hai Shalomc1a21442022-02-04 13:43:00 -08003870 reason_code, 0, bss);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003871 /*
3872 * For locally generated disconnect, supplicant already generates a
3873 * DEAUTH event, so ignore the event from NL80211.
3874 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003875 if (ret == 0)
3876 drv->ignore_next_local_disconnect = send_event_marker(drv);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003877
3878 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879}
3880
3881
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003882static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
Hai Shalom81f62d82019-07-22 12:10:00 -07003883 const u8 *addr, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003886 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003887
3888 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3889 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003890 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003891 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003892 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003893 return wpa_driver_nl80211_disconnect(drv, reason_code, bss);
Hai Shalom74f70d42019-02-11 14:42:39 -08003894 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3896 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003897 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003898 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
Hai Shalomc1a21442022-02-04 13:43:00 -08003899 reason_code, 0, bss);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003900 /*
3901 * For locally generated deauthenticate, supplicant already generates a
3902 * DEAUTH event, so ignore the event from NL80211.
3903 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003904 if (ret == 0)
3905 drv->ignore_next_local_deauth = send_event_marker(drv);
Hai Shalomce48b4a2018-09-05 11:41:35 -07003906
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003907 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908}
3909
3910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003911static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3912 struct wpa_driver_auth_params *params)
3913{
3914 int i;
3915
3916 drv->auth_freq = params->freq;
3917 drv->auth_alg = params->auth_alg;
3918 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3919 drv->auth_local_state_change = params->local_state_change;
3920 drv->auth_p2p = params->p2p;
3921
3922 if (params->bssid)
3923 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3924 else
3925 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3926
3927 if (params->ssid) {
3928 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3929 drv->auth_ssid_len = params->ssid_len;
3930 } else
3931 drv->auth_ssid_len = 0;
3932
3933
3934 os_free(drv->auth_ie);
3935 drv->auth_ie = NULL;
3936 drv->auth_ie_len = 0;
3937 if (params->ie) {
3938 drv->auth_ie = os_malloc(params->ie_len);
3939 if (drv->auth_ie) {
3940 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3941 drv->auth_ie_len = params->ie_len;
3942 }
3943 }
3944
Sunil Ravi77d572f2023-01-17 23:58:31 +00003945 if (params->mld && params->ap_mld_addr) {
3946 drv->auth_mld = params->mld;
3947 drv->auth_mld_link_id = params->mld_link_id;
3948 os_memcpy(drv->auth_ap_mld_addr, params->ap_mld_addr, ETH_ALEN);
3949 } else {
3950 drv->auth_mld = false;
3951 drv->auth_mld_link_id = -1;
3952 }
3953
Hai Shalom60840252021-02-19 19:02:11 -08003954 os_free(drv->auth_data);
3955 drv->auth_data = NULL;
3956 drv->auth_data_len = 0;
3957 if (params->auth_data) {
3958 drv->auth_data = os_memdup(params->auth_data,
3959 params->auth_data_len);
3960 if (drv->auth_data)
3961 drv->auth_data_len = params->auth_data_len;
3962 }
3963
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003964 for (i = 0; i < 4; i++) {
3965 if (params->wep_key[i] && params->wep_key_len[i] &&
3966 params->wep_key_len[i] <= 16) {
3967 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3968 params->wep_key_len[i]);
3969 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3970 } else
3971 drv->auth_wep_key_len[i] = 0;
3972 }
3973}
3974
3975
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003976static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3977{
3978 struct wpa_driver_nl80211_data *drv = bss->drv;
3979
3980 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3981 return;
3982
3983 /*
3984 * Looks like we failed to unmask 11b rates previously. This could
3985 * happen, e.g., if the interface was down at the point in time when a
3986 * P2P group was terminated.
3987 */
3988 wpa_printf(MSG_DEBUG,
3989 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3990 bss->ifname);
3991 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3992}
3993
3994
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003995static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3996{
3997 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3998 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3999 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
4000 return NL80211_AUTHTYPE_SHARED_KEY;
4001 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
4002 return NL80211_AUTHTYPE_NETWORK_EAP;
4003 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
4004 return NL80211_AUTHTYPE_FT;
4005 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
4006 return NL80211_AUTHTYPE_SAE;
4007 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
4008 return NL80211_AUTHTYPE_FILS_SK;
4009 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
4010 return NL80211_AUTHTYPE_FILS_SK_PFS;
4011
4012 return NL80211_AUTHTYPE_MAX;
4013}
4014
4015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004017 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 struct wpa_driver_nl80211_data *drv = bss->drv;
4020 int ret = -1, i;
4021 struct nl_msg *msg;
4022 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004023 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004025 int is_retry;
Hai Shalomfdcde762020-04-02 11:19:20 -07004026 struct wpa_driver_set_key_params p;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004027
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004028 nl80211_unmask_11b_rates(bss);
4029
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030 is_retry = drv->retry_auth;
4031 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07004032 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004034 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004036 if (params->bssid)
4037 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4038 else
4039 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004041 nlmode = params->p2p ?
4042 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4043 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004044 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 return -1;
4046
4047retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4049 drv->ifindex);
4050
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004051 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
4052 if (!msg)
4053 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054
Hai Shalomfdcde762020-04-02 11:19:20 -07004055 os_memset(&p, 0, sizeof(p));
4056 p.ifname = bss->ifname;
4057 p.alg = WPA_ALG_WEP;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004058 p.link_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 for (i = 0; i < 4; i++) {
4060 if (!params->wep_key[i])
4061 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07004062 p.key_idx = i;
4063 p.set_tx = i == params->wep_tx_keyidx;
4064 p.key = params->wep_key[i];
4065 p.key_len = params->wep_key_len[i];
4066 p.key_flag = i == params->wep_tx_keyidx ?
4067 KEY_FLAG_GROUP_RX_TX_DEFAULT :
4068 KEY_FLAG_GROUP_RX_TX;
4069 wpa_driver_nl80211_set_key(bss, &p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 if (params->wep_tx_keyidx != i)
4071 continue;
4072 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004073 params->wep_key[i], params->wep_key_len[i]))
4074 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 }
4076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 if (params->bssid) {
4078 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4079 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4081 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 }
4083 if (params->freq) {
4084 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004085 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
4086 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 }
4088 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004089 wpa_printf(MSG_DEBUG, " * SSID=%s",
4090 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004091 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4092 params->ssid))
4093 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 }
4095 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096 if (params->ie &&
4097 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
4098 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004099 if (params->auth_data) {
4100 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
4101 params->auth_data_len);
4102 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
4103 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004104 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004105 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004106 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004108 if (type == NL80211_AUTHTYPE_MAX ||
4109 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004110 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 if (params->local_state_change) {
4112 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004113 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
4114 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 }
4116
Sunil Ravi77d572f2023-01-17 23:58:31 +00004117 if (params->mld && params->ap_mld_addr) {
4118 wpa_printf(MSG_DEBUG, " * MLD: link_id=%u, MLD addr=" MACSTR,
4119 params->mld_link_id, MAC2STR(params->ap_mld_addr));
4120
4121 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
4122 params->mld_link_id) ||
4123 nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
4124 params->ap_mld_addr))
4125 goto fail;
4126 }
4127
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004128 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 msg = NULL;
4130 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004131 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004132 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
4133 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004135 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
4136 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137 /*
4138 * mac80211 does not currently accept new
4139 * authentication if we are already authenticated. As a
4140 * workaround, force deauthentication and try again.
4141 */
4142 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4143 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07004144 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 wpa_driver_nl80211_deauthenticate(
4146 bss, params->bssid,
4147 WLAN_REASON_PREV_AUTH_NOT_VALID);
4148 nlmsg_free(msg);
4149 goto retry;
4150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004151
4152 if (ret == -ENOENT && params->freq && !is_retry) {
4153 /*
4154 * cfg80211 has likely expired the BSS entry even
4155 * though it was previously available in our internal
4156 * BSS table. To recover quickly, start a single
4157 * channel scan on the specified channel.
4158 */
4159 struct wpa_driver_scan_params scan;
4160 int freqs[2];
4161
4162 os_memset(&scan, 0, sizeof(scan));
4163 scan.num_ssids = 1;
4164 if (params->ssid) {
4165 scan.ssids[0].ssid = params->ssid;
4166 scan.ssids[0].ssid_len = params->ssid_len;
4167 }
4168 freqs[0] = params->freq;
4169 freqs[1] = 0;
4170 scan.freqs = freqs;
4171 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4172 "channel scan to refresh cfg80211 BSS "
4173 "entry");
4174 ret = wpa_driver_nl80211_scan(bss, &scan);
4175 if (ret == 0) {
4176 nl80211_copy_auth_params(drv, params);
4177 drv->scan_for_auth = 1;
4178 }
4179 } else if (is_retry) {
4180 /*
4181 * Need to indicate this with an event since the return
4182 * value from the retry is not delivered to core code.
4183 */
4184 union wpa_event_data event;
4185 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4186 "failed");
4187 os_memset(&event, 0, sizeof(event));
4188 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4189 ETH_ALEN);
4190 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4191 &event);
4192 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004193 } else {
4194 wpa_printf(MSG_DEBUG,
4195 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004198fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 nlmsg_free(msg);
4200 return ret;
4201}
4202
4203
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004204int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004205{
4206 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004207 struct i802_bss *bss = drv->first_bss;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004208 u8 ap_mld_addr[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004209 int i;
4210
4211 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4212
4213 os_memset(&params, 0, sizeof(params));
4214 params.freq = drv->auth_freq;
4215 params.auth_alg = drv->auth_alg;
4216 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4217 params.local_state_change = drv->auth_local_state_change;
4218 params.p2p = drv->auth_p2p;
4219
4220 if (!is_zero_ether_addr(drv->auth_bssid_))
4221 params.bssid = drv->auth_bssid_;
4222
4223 if (drv->auth_ssid_len) {
4224 params.ssid = drv->auth_ssid;
4225 params.ssid_len = drv->auth_ssid_len;
4226 }
4227
4228 params.ie = drv->auth_ie;
4229 params.ie_len = drv->auth_ie_len;
Hai Shalom60840252021-02-19 19:02:11 -08004230 params.auth_data = drv->auth_data;
4231 params.auth_data_len = drv->auth_data_len;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004232 params.mld = drv->auth_mld;
4233 params.mld_link_id = drv->auth_mld_link_id;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004234 if (drv->auth_mld) {
4235 os_memcpy(ap_mld_addr, drv->auth_ap_mld_addr, ETH_ALEN);
4236 params.ap_mld_addr = ap_mld_addr;
4237 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004238
4239 for (i = 0; i < 4; i++) {
4240 if (drv->auth_wep_key_len[i]) {
4241 params.wep_key[i] = drv->auth_wep_key[i];
4242 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4243 }
4244 }
4245
4246 drv->retry_auth = 1;
4247 return wpa_driver_nl80211_authenticate(bss, &params);
4248}
4249
4250
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004251struct i802_link * nl80211_get_link(struct i802_bss *bss, s8 link_id)
4252{
Sunil Ravi99c035e2024-07-12 01:42:03 +00004253 if (link_id < 0 || link_id >= MAX_NUM_MLD_LINKS)
4254 return bss->flink;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004255
Sunil Ravi99c035e2024-07-12 01:42:03 +00004256 if (BIT(link_id) & bss->valid_links)
4257 return &bss->links[link_id];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004258
4259 return bss->flink;
4260}
4261
4262
4263static void nl80211_link_set_freq(struct i802_bss *bss, s8 link_id, int freq)
4264{
4265 struct i802_link *link = nl80211_get_link(bss, link_id);
4266
4267 link->freq = freq;
4268}
4269
4270
4271static int nl80211_get_link_freq(struct i802_bss *bss, const u8 *addr,
4272 bool bss_freq_debug)
4273{
Sunil Ravi99c035e2024-07-12 01:42:03 +00004274 u8 i;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004275
Sunil Ravi99c035e2024-07-12 01:42:03 +00004276 for_each_link(bss->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004277 if (ether_addr_equal(bss->links[i].addr, addr)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004278 wpa_printf(MSG_DEBUG,
4279 "nl80211: Use link freq=%d for address "
4280 MACSTR,
4281 bss->links[i].freq, MAC2STR(addr));
4282 return bss->links[i].freq;
4283 }
4284 }
4285
4286 if (bss_freq_debug)
4287 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
4288 bss->flink->freq);
4289
4290 return bss->flink->freq;
4291}
4292
4293
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004294static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
4295 size_t data_len, int noack,
4296 unsigned int freq, int no_cck,
4297 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004298 unsigned int wait_time,
4299 const u16 *csa_offs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004300 size_t csa_offs_len, int no_encrypt,
4301 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 struct wpa_driver_nl80211_data *drv = bss->drv;
4304 struct ieee80211_mgmt *mgmt;
Hai Shalomfdcde762020-04-02 11:19:20 -07004305 int encrypt = !no_encrypt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306 u16 fc;
Hai Shalomfdcde762020-04-02 11:19:20 -07004307 int use_cookie = 1;
4308 int res;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004309 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310
4311 mgmt = (struct ieee80211_mgmt *) data;
4312 fc = le_to_host16(mgmt->frame_control);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004313 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da=" MACSTR " sa=" MACSTR
4314 " bssid=" MACSTR
Hai Shalomfdcde762020-04-02 11:19:20 -07004315 " 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 +00004316 MAC2STR(mgmt->da), MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
4317 noack, freq, no_cck, offchanok, wait_time,
Hai Shalomfdcde762020-04-02 11:19:20 -07004318 no_encrypt, fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004319
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004320 if ((is_sta_interface(drv->nlmode) ||
4321 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4323 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
4324 /*
4325 * The use of last_mgmt_freq is a bit of a hack,
4326 * but it works due to the single-threaded nature
4327 * of wpa_supplicant.
4328 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004329 if (freq == 0) {
4330 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
4331 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004332 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004333 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004334 wait_time = 0;
4335 use_cookie = 0;
4336 no_cck = 1;
4337 offchanok = 1;
4338 goto send_frame_cmd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004340
4341 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004342 unsigned int link_freq = nl80211_get_link_freq(bss, mgmt->sa,
4343 !freq);
4344
4345 if (!freq)
4346 freq = link_freq;
4347
4348 if (freq == link_freq)
Hai Shalomfdcde762020-04-02 11:19:20 -07004349 wait_time = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004350
Hai Shalomfdcde762020-04-02 11:19:20 -07004351 goto send_frame_cmd;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07004352 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07004353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4355 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
4356 /*
4357 * Only one of the authentication frame types is encrypted.
4358 * In order for static WEP encryption to work properly (i.e.,
4359 * to not encrypt the frame), we need to tell mac80211 about
4360 * the frames that must not be encrypted.
4361 */
4362 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4363 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
4364 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
4365 encrypt = 0;
4366 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004367
Hai Shalom60840252021-02-19 19:02:11 -08004368 if (is_sta_interface(drv->nlmode) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07004369 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4370 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
Hai Shalom60840252021-02-19 19:02:11 -08004371 if (freq == 0 &&
4372 (drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
4373 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
4374 freq = nl80211_get_assoc_freq(drv);
4375 wpa_printf(MSG_DEBUG,
4376 "nl80211: send_mlme - Use assoc_freq=%u for external auth",
4377 freq);
4378 }
4379
4380 /* Allow off channel for PASN authentication */
4381 if (data_len >= IEEE80211_HDRLEN + 2 &&
4382 WPA_GET_LE16(data + IEEE80211_HDRLEN) == WLAN_AUTH_PASN &&
4383 !offchanok) {
4384 wpa_printf(MSG_DEBUG,
4385 "nl80211: send_mlme: allow off channel for PASN");
4386 offchanok = 1;
4387 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004388 }
4389
Hai Shalomc1a21442022-02-04 13:43:00 -08004390#ifdef CONFIG_PASN
4391 if (is_sta_interface(drv->nlmode) &&
4392 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4393 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_DEAUTH) {
4394 wpa_printf(MSG_DEBUG,
4395 "nl80211: send_mlme: allow Deauthentication frame for PASN");
4396
4397 use_cookie = 0;
4398 offchanok = 1;
4399 goto send_frame_cmd;
4400 }
4401#endif /* CONFIG_PASN */
4402
Hai Shalomfdcde762020-04-02 11:19:20 -07004403 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
4404 freq = nl80211_get_assoc_freq(drv);
4405 wpa_printf(MSG_DEBUG,
4406 "nl80211: send_mlme - Use assoc_freq=%u for IBSS",
4407 freq);
4408 }
4409 if (freq == 0) {
4410 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - Use bss->freq=%u",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004411 link->freq);
4412 freq = link->freq;
Hai Shalomfdcde762020-04-02 11:19:20 -07004413 }
4414
Hai Shalomc1a21442022-02-04 13:43:00 -08004415 if (drv->use_monitor && is_ap_interface(drv->nlmode)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004416 wpa_printf(MSG_DEBUG,
4417 "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004418 freq, link->freq);
Hai Shalomfdcde762020-04-02 11:19:20 -07004419 return nl80211_send_monitor(drv, data, data_len, encrypt,
4420 noack);
4421 }
4422
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004423 if ((noack || WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
4424 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION) &&
4425 link_id == NL80211_DRV_LINK_ID_NA)
Hai Shalomfdcde762020-04-02 11:19:20 -07004426 use_cookie = 0;
4427send_frame_cmd:
4428#ifdef CONFIG_TESTING_OPTIONS
4429 if (no_encrypt && !encrypt && !drv->use_monitor) {
4430 wpa_printf(MSG_DEBUG,
4431 "nl80211: Request to send an unencrypted frame - use a monitor interface for this");
4432 if (nl80211_create_monitor_interface(drv) < 0)
4433 return -1;
4434 res = nl80211_send_monitor(drv, data, data_len, encrypt,
4435 noack);
4436 nl80211_remove_monitor_interface(drv);
4437 return res;
4438 }
4439#endif /* CONFIG_TESTING_OPTIONS */
4440
4441 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame_cmd");
4442 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, data_len,
4443 use_cookie, no_cck, noack, offchanok,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004444 csa_offs, csa_offs_len, link_id);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004445 if (!res)
4446 drv->send_frame_link_id = link_id;
Hai Shalomfdcde762020-04-02 11:19:20 -07004447
4448 return res;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004449}
4450
4451
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004452static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
4453{
4454 u8 rates[NL80211_MAX_SUPP_RATES];
4455 u8 rates_len = 0;
4456 int i;
4457
4458 if (!basic_rates)
4459 return 0;
4460
4461 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
4462 rates[rates_len++] = basic_rates[i] / 5;
4463
4464 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
4465}
4466
4467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004468static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
4469 int slot, int ht_opmode, int ap_isolate,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004470 const int *basic_rates, int link_id)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004471{
4472 struct wpa_driver_nl80211_data *drv = bss->drv;
4473 struct nl_msg *msg;
4474
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004475 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
4476 (cts >= 0 &&
4477 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
4478 (preamble >= 0 &&
4479 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
4480 (slot >= 0 &&
4481 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
4482 (ht_opmode >= 0 &&
4483 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
4484 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004485 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004486 nl80211_put_basic_rates(msg, basic_rates) ||
4487 (link_id != NL80211_DRV_LINK_ID_NA &&
4488 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004489 nlmsg_free(msg);
4490 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004491 }
4492
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004493 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004494}
4495
4496
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004497static int wpa_driver_nl80211_set_acl(void *priv,
4498 struct hostapd_acl_params *params)
4499{
4500 struct i802_bss *bss = priv;
4501 struct wpa_driver_nl80211_data *drv = bss->drv;
4502 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004503 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004504 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004505 int ret;
Hai Shalomc1a21442022-02-04 13:43:00 -08004506 size_t acl_nla_sz, acl_nlmsg_sz, nla_sz, nlmsg_sz;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004507
4508 if (!(drv->capa.max_acl_mac_addrs))
4509 return -ENOTSUP;
4510
4511 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
4512 return -ENOTSUP;
4513
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004514 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
4515 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
4516
Hai Shalomc1a21442022-02-04 13:43:00 -08004517 acl_nla_sz = nla_total_size(ETH_ALEN) * params->num_mac_acl;
4518 acl_nlmsg_sz = nlmsg_total_size(acl_nla_sz);
4519 acl = nlmsg_alloc_size(acl_nlmsg_sz);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004520 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004521 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004522 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004523 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
4524 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004525 return -ENOMEM;
4526 }
4527 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004528
Hai Shalomc1a21442022-02-04 13:43:00 -08004529 /*
4530 * genetlink message header (Length of user header is 0) +
4531 * u32 attr: NL80211_ATTR_IFINDEX +
4532 * u32 attr: NL80211_ATTR_ACL_POLICY +
4533 * nested acl attr
4534 */
4535 nla_sz = GENL_HDRLEN +
4536 nla_total_size(4) * 2 +
4537 nla_total_size(acl_nla_sz);
4538 nlmsg_sz = nlmsg_total_size(nla_sz);
4539 if (!(msg = nl80211_ifindex_msg_build(drv, nlmsg_alloc_size(nlmsg_sz),
4540 drv->ifindex, 0,
4541 NL80211_CMD_SET_MAC_ACL)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004542 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
4543 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
4544 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
4545 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
4546 nlmsg_free(msg);
4547 nlmsg_free(acl);
4548 return -ENOMEM;
4549 }
4550 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004551
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004552 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004553 if (ret) {
4554 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
4555 ret, strerror(-ret));
4556 }
4557
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004558 return ret;
4559}
4560
4561
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004562static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
4563{
4564 if (beacon_int > 0) {
4565 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
4566 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
4567 beacon_int);
4568 }
4569
4570 return 0;
4571}
4572
4573
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004574static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
4575{
4576 if (dtim_period > 0) {
4577 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
4578 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
4579 }
4580
4581 return 0;
4582}
4583
4584
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004585#ifdef CONFIG_MESH
4586static int nl80211_set_mesh_config(void *priv,
4587 struct wpa_driver_mesh_bss_params *params)
4588{
4589 struct i802_bss *bss = priv;
4590 struct wpa_driver_nl80211_data *drv = bss->drv;
4591 struct nl_msg *msg;
4592 int ret;
4593
4594 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
4595 if (!msg)
4596 return -1;
4597
4598 ret = nl80211_put_mesh_config(msg, params);
4599 if (ret < 0) {
4600 nlmsg_free(msg);
4601 return ret;
4602 }
4603
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004604 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004605 if (ret) {
4606 wpa_printf(MSG_ERROR,
4607 "nl80211: Mesh config set failed: %d (%s)",
4608 ret, strerror(-ret));
4609 return ret;
4610 }
4611 return 0;
4612}
4613#endif /* CONFIG_MESH */
4614
4615
Hai Shalom60840252021-02-19 19:02:11 -08004616static int nl80211_put_beacon_rate(struct nl_msg *msg, u64 flags, u64 flags2,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004617 struct wpa_driver_ap_params *params)
4618{
4619 struct nlattr *bands, *band;
4620 struct nl80211_txrate_vht vht_rate;
Hai Shalom60840252021-02-19 19:02:11 -08004621 struct nl80211_txrate_he he_rate;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004622
4623 if (!params->freq ||
4624 (params->beacon_rate == 0 &&
4625 params->rate_type == BEACON_RATE_LEGACY))
4626 return 0;
4627
4628 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
4629 if (!bands)
4630 return -1;
4631
4632 switch (params->freq->mode) {
4633 case HOSTAPD_MODE_IEEE80211B:
4634 case HOSTAPD_MODE_IEEE80211G:
4635 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
4636 break;
4637 case HOSTAPD_MODE_IEEE80211A:
Hai Shalom60840252021-02-19 19:02:11 -08004638 if (is_6ghz_freq(params->freq->freq))
4639 band = nla_nest_start(msg, NL80211_BAND_6GHZ);
4640 else
4641 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004642 break;
4643 case HOSTAPD_MODE_IEEE80211AD:
4644 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
4645 break;
4646 default:
4647 return 0;
4648 }
4649
4650 if (!band)
4651 return -1;
4652
4653 os_memset(&vht_rate, 0, sizeof(vht_rate));
Hai Shalom60840252021-02-19 19:02:11 -08004654 os_memset(&he_rate, 0, sizeof(he_rate));
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004655
4656 switch (params->rate_type) {
4657 case BEACON_RATE_LEGACY:
4658 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
4659 wpa_printf(MSG_INFO,
4660 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
4661 return -1;
4662 }
4663
4664 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004665 (u8) (params->beacon_rate / 5)) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004666 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4667 (params->freq->vht_enabled &&
4668 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4669 &vht_rate)))
4670 return -1;
4671
4672 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
4673 params->beacon_rate);
4674 break;
4675 case BEACON_RATE_HT:
4676 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
4677 wpa_printf(MSG_INFO,
4678 "nl80211: Driver does not support setting Beacon frame rate (HT)");
4679 return -1;
4680 }
4681 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4682 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
4683 (params->freq->vht_enabled &&
4684 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4685 &vht_rate)))
4686 return -1;
4687 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
4688 params->beacon_rate);
4689 break;
4690 case BEACON_RATE_VHT:
4691 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
4692 wpa_printf(MSG_INFO,
4693 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
4694 return -1;
4695 }
4696 vht_rate.mcs[0] = BIT(params->beacon_rate);
4697 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
4698 return -1;
4699 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
4700 return -1;
4701 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4702 &vht_rate))
4703 return -1;
4704 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
4705 params->beacon_rate);
4706 break;
Hai Shalom60840252021-02-19 19:02:11 -08004707 case BEACON_RATE_HE:
4708 if (!(flags2 & WPA_DRIVER_FLAGS2_BEACON_RATE_HE)) {
4709 wpa_printf(MSG_INFO,
4710 "nl80211: Driver does not support setting Beacon frame rate (HE)");
4711 return -1;
4712 }
4713 he_rate.mcs[0] = BIT(params->beacon_rate);
4714 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4715 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4716 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4717 &vht_rate) ||
4718 nla_put(msg, NL80211_TXRATE_HE, sizeof(he_rate), &he_rate))
4719 return -1;
4720 wpa_printf(MSG_DEBUG, " * beacon_rate = HE-MCS %u",
4721 params->beacon_rate);
4722 break;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004723 }
4724
4725 nla_nest_end(msg, band);
4726 nla_nest_end(msg, bands);
4727
4728 return 0;
4729}
4730
4731
4732static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
4733 int multicast_to_unicast)
4734{
4735 struct wpa_driver_nl80211_data *drv = bss->drv;
4736 struct nl_msg *msg;
4737 int ret;
4738
4739 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
4740 if (!msg ||
4741 (multicast_to_unicast &&
4742 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
4743 wpa_printf(MSG_ERROR,
4744 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
4745 bss->ifname);
4746 nlmsg_free(msg);
4747 return -ENOBUFS;
4748 }
4749
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004750 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004751
4752 switch (ret) {
4753 case 0:
4754 wpa_printf(MSG_DEBUG,
4755 "nl80211: multicast to unicast %s on interface %s",
4756 multicast_to_unicast ? "enabled" : "disabled",
4757 bss->ifname);
4758 break;
4759 case -EOPNOTSUPP:
4760 if (!multicast_to_unicast)
4761 break;
4762 wpa_printf(MSG_INFO,
4763 "nl80211: multicast to unicast not supported on interface %s",
4764 bss->ifname);
4765 break;
4766 default:
4767 wpa_printf(MSG_ERROR,
4768 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
4769 multicast_to_unicast ? "enabling" : "disabling",
4770 ret, strerror(-ret), bss->ifname);
4771 break;
4772 }
4773
4774 return ret;
4775}
4776
4777
Hai Shalom60840252021-02-19 19:02:11 -08004778#ifdef CONFIG_SAE
Sunil Ravi77d572f2023-01-17 23:58:31 +00004779static int nl80211_put_sae_pwe(struct nl_msg *msg, enum sae_pwe pwe)
Hai Shalom60840252021-02-19 19:02:11 -08004780{
4781 u8 sae_pwe;
4782
4783 wpa_printf(MSG_DEBUG, "nl802111: sae_pwe=%d", pwe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004784 if (pwe == SAE_PWE_HUNT_AND_PECK)
Hai Shalom60840252021-02-19 19:02:11 -08004785 sae_pwe = NL80211_SAE_PWE_HUNT_AND_PECK;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004786 else if (pwe == SAE_PWE_HASH_TO_ELEMENT)
Hai Shalom60840252021-02-19 19:02:11 -08004787 sae_pwe = NL80211_SAE_PWE_HASH_TO_ELEMENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004788 else if (pwe == SAE_PWE_BOTH)
Hai Shalom60840252021-02-19 19:02:11 -08004789 sae_pwe = NL80211_SAE_PWE_BOTH;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004790 else if (pwe == SAE_PWE_FORCE_HUNT_AND_PECK)
Hai Shalom60840252021-02-19 19:02:11 -08004791 return 0; /* special test mode */
4792 else
4793 return -1;
4794 if (nla_put_u8(msg, NL80211_ATTR_SAE_PWE, sae_pwe))
4795 return -1;
4796
4797 return 0;
4798}
4799#endif /* CONFIG_SAE */
4800
4801
4802#ifdef CONFIG_FILS
4803static int nl80211_fils_discovery(struct i802_bss *bss, struct nl_msg *msg,
4804 struct wpa_driver_ap_params *params)
4805{
4806 struct nlattr *attr;
4807
4808 if (!bss->drv->fils_discovery) {
4809 wpa_printf(MSG_ERROR,
4810 "nl80211: Driver does not support FILS Discovery frame transmission for %s",
4811 bss->ifname);
4812 return -1;
4813 }
4814
4815 attr = nla_nest_start(msg, NL80211_ATTR_FILS_DISCOVERY);
4816 if (!attr ||
4817 nla_put_u32(msg, NL80211_FILS_DISCOVERY_ATTR_INT_MIN,
4818 params->fd_min_int) ||
4819 nla_put_u32(msg, NL80211_FILS_DISCOVERY_ATTR_INT_MAX,
4820 params->fd_max_int) ||
4821 (params->fd_frame_tmpl &&
4822 nla_put(msg, NL80211_FILS_DISCOVERY_ATTR_TMPL,
4823 params->fd_frame_tmpl_len, params->fd_frame_tmpl)))
4824 return -1;
4825
4826 nla_nest_end(msg, attr);
4827 return 0;
4828}
4829#endif /* CONFIG_FILS */
4830
4831
4832#ifdef CONFIG_IEEE80211AX
Sunil Ravi77d572f2023-01-17 23:58:31 +00004833
Hai Shalom60840252021-02-19 19:02:11 -08004834static int nl80211_unsol_bcast_probe_resp(struct i802_bss *bss,
4835 struct nl_msg *msg,
4836 struct wpa_driver_ap_params *params)
4837{
4838 struct nlattr *attr;
4839
4840 if (!bss->drv->unsol_bcast_probe_resp) {
4841 wpa_printf(MSG_ERROR,
4842 "nl80211: Driver does not support unsolicited broadcast Probe Response frame transmission for %s",
4843 bss->ifname);
4844 return -1;
4845 }
4846
4847 wpa_printf(MSG_DEBUG,
4848 "nl80211: Unsolicited broadcast Probe Response frame interval: %u",
4849 params->unsol_bcast_probe_resp_interval);
4850 attr = nla_nest_start(msg, NL80211_ATTR_UNSOL_BCAST_PROBE_RESP);
4851 if (!attr ||
4852 nla_put_u32(msg, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT,
4853 params->unsol_bcast_probe_resp_interval) ||
4854 (params->unsol_bcast_probe_resp_tmpl &&
4855 nla_put(msg, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL,
4856 params->unsol_bcast_probe_resp_tmpl_len,
4857 params->unsol_bcast_probe_resp_tmpl)))
4858 return -1;
4859
4860 nla_nest_end(msg, attr);
4861 return 0;
4862}
Sunil Ravi77d572f2023-01-17 23:58:31 +00004863
4864
4865static int nl80211_mbssid(struct nl_msg *msg,
4866 struct wpa_driver_ap_params *params)
4867{
4868 struct nlattr *config, *elems;
4869 int ifidx;
4870
4871 if (!params->mbssid_tx_iface)
4872 return 0;
4873
4874 config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
4875 if (!config ||
4876 nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_INDEX,
4877 params->mbssid_index))
4878 return -1;
4879
4880 if (params->mbssid_tx_iface) {
4881 ifidx = if_nametoindex(params->mbssid_tx_iface);
4882 if (ifidx <= 0 ||
4883 nla_put_u32(msg, NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX,
4884 ifidx))
4885 return -1;
4886 }
4887
4888 if (params->ema && nla_put_flag(msg, NL80211_MBSSID_CONFIG_ATTR_EMA))
4889 return -1;
4890
4891 nla_nest_end(msg, config);
4892
4893 if (params->mbssid_elem_count && params->mbssid_elem_len &&
4894 params->mbssid_elem_offset && *params->mbssid_elem_offset) {
4895 u8 i, **offs = params->mbssid_elem_offset;
4896
4897 elems = nla_nest_start(msg, NL80211_ATTR_MBSSID_ELEMS);
4898 if (!elems)
4899 return -1;
4900
4901 for (i = 0; i < params->mbssid_elem_count - 1; i++) {
4902 if (nla_put(msg, i + 1, offs[i + 1] - offs[i], offs[i]))
4903 return -1;
4904 }
4905
4906 if (nla_put(msg, i + 1,
4907 *offs + params->mbssid_elem_len - offs[i],
4908 offs[i]))
4909 return -1;
4910
4911 nla_nest_end(msg, elems);
4912 }
4913
Sunil Ravi640215c2023-06-28 23:08:09 +00004914 if (!params->ema)
4915 return 0;
4916
4917 if (params->rnr_elem_count && params->rnr_elem_len &&
4918 params->rnr_elem_offset && *params->rnr_elem_offset) {
4919 u8 i, **offs = params->rnr_elem_offset;
4920
4921 elems = nla_nest_start(msg, NL80211_ATTR_EMA_RNR_ELEMS);
4922 if (!elems)
4923 return -1;
4924
4925 for (i = 0; i < params->rnr_elem_count - 1; i++) {
4926 if (nla_put(msg, i + 1, offs[i + 1] - offs[i], offs[i]))
4927 return -1;
4928 }
4929
4930 if (nla_put(msg, i + 1, *offs + params->rnr_elem_len - offs[i],
4931 offs[i]))
4932 return -1;
4933 nla_nest_end(msg, elems);
4934 }
4935
Sunil Ravi77d572f2023-01-17 23:58:31 +00004936 return 0;
4937}
4938
Hai Shalom60840252021-02-19 19:02:11 -08004939#endif /* CONFIG_IEEE80211AX */
4940
4941
Sunil Ravi640215c2023-06-28 23:08:09 +00004942#ifdef CONFIG_DRIVER_NL80211_QCA
4943static void qca_set_allowed_ap_freqs(struct wpa_driver_nl80211_data *drv,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004944 const int *freqs, int num_freqs,
4945 int link_id)
Sunil Ravi640215c2023-06-28 23:08:09 +00004946{
4947 struct nl_msg *msg;
4948 struct nlattr *params, *freqs_list;
4949 int i, ret;
4950
4951 if (!drv->set_wifi_conf_vendor_cmd_avail || !drv->qca_ap_allowed_freqs)
4952 return;
4953
4954 wpa_printf(MSG_DEBUG, "nl80211: Set AP allowed frequency list");
4955
4956 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
4957 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
4958 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
4959 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
4960 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)))
4961 goto err;
4962
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004963 if (link_id != NL80211_DRV_LINK_ID_NA &&
4964 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_MLO_LINK_ID, link_id))
4965 goto err;
4966
Sunil Ravi640215c2023-06-28 23:08:09 +00004967 freqs_list = nla_nest_start(
4968 msg, QCA_WLAN_VENDOR_ATTR_CONFIG_AP_ALLOWED_FREQ_LIST);
4969 if (!freqs_list)
4970 goto err;
4971
4972 for (i = 0; i < num_freqs; i++) {
4973 if (nla_put_u32(msg, i, freqs[i]))
4974 goto err;
4975 }
4976
4977 nla_nest_end(msg, freqs_list);
4978 nla_nest_end(msg, params);
4979
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004980 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi640215c2023-06-28 23:08:09 +00004981 if (ret)
4982 wpa_printf(MSG_ERROR,
4983 "nl80211: Failed set AP alllowed frequency list: %d (%s)",
4984 ret, strerror(-ret));
4985
4986 return;
4987err:
4988 nlmsg_free(msg);
4989}
4990#endif /* CONFIG_DRIVER_NL80211_QCA */
4991
4992
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004993static int nl80211_put_freq_params(struct nl_msg *msg,
4994 const struct hostapd_freq_params *freq)
4995{
4996 enum hostapd_hw_mode hw_mode;
4997 int is_24ghz;
4998 u8 channel;
4999
5000 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
5001 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
5002 return -ENOBUFS;
5003
5004 wpa_printf(MSG_DEBUG, " * eht_enabled=%d", freq->eht_enabled);
5005 wpa_printf(MSG_DEBUG, " * he_enabled=%d", freq->he_enabled);
5006 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
5007 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
5008 wpa_printf(MSG_DEBUG, " * radar_background=%d",
5009 freq->radar_background);
5010
5011 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
5012 is_24ghz = hw_mode == HOSTAPD_MODE_IEEE80211G ||
5013 hw_mode == HOSTAPD_MODE_IEEE80211B;
5014
5015 if (freq->vht_enabled ||
5016 ((freq->he_enabled || freq->eht_enabled) && !is_24ghz)) {
5017 enum nl80211_chan_width cw;
5018
5019 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
5020 switch (freq->bandwidth) {
5021 case 20:
5022 cw = NL80211_CHAN_WIDTH_20;
5023 break;
5024 case 40:
5025 cw = NL80211_CHAN_WIDTH_40;
5026 break;
5027 case 80:
5028 if (freq->center_freq2)
5029 cw = NL80211_CHAN_WIDTH_80P80;
5030 else
5031 cw = NL80211_CHAN_WIDTH_80;
5032 break;
5033 case 160:
5034 cw = NL80211_CHAN_WIDTH_160;
5035 break;
5036 case 320:
5037 cw = NL80211_CHAN_WIDTH_320;
5038 break;
5039 default:
5040 return -EINVAL;
5041 }
5042
5043 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
5044 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
5045 freq->center_freq1);
5046 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
5047 freq->center_freq2);
5048 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
5049 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
5050 freq->center_freq1) ||
5051 (freq->center_freq2 &&
5052 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
5053 freq->center_freq2)))
5054 return -ENOBUFS;
5055 } else if (freq->ht_enabled) {
5056 enum nl80211_channel_type ct;
5057
5058 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
5059 freq->sec_channel_offset);
5060 switch (freq->sec_channel_offset) {
5061 case -1:
5062 ct = NL80211_CHAN_HT40MINUS;
5063 break;
5064 case 1:
5065 ct = NL80211_CHAN_HT40PLUS;
5066 break;
5067 default:
5068 ct = NL80211_CHAN_HT20;
5069 break;
5070 }
5071
5072 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
5073 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
5074 return -ENOBUFS;
5075 } else if (freq->edmg.channels && freq->edmg.bw_config) {
5076 wpa_printf(MSG_DEBUG,
5077 " * EDMG configuration: channels=0x%x bw_config=%d",
5078 freq->edmg.channels, freq->edmg.bw_config);
5079 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
5080 freq->edmg.channels) ||
5081 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
5082 freq->edmg.bw_config))
5083 return -1;
5084 } else {
5085 wpa_printf(MSG_DEBUG, " * channel_type=%d",
5086 NL80211_CHAN_NO_HT);
5087 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5088 NL80211_CHAN_NO_HT))
5089 return -ENOBUFS;
5090 }
5091 if (freq->radar_background &&
5092 nla_put_flag(msg, NL80211_ATTR_RADAR_BACKGROUND))
5093 return -ENOBUFS;
5094
5095 return 0;
5096}
5097
5098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005099static int wpa_driver_nl80211_set_ap(void *priv,
5100 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101{
5102 struct i802_bss *bss = priv;
5103 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005104 struct i802_link *link = bss->flink;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105 struct nl_msg *msg;
5106 u8 cmd = NL80211_CMD_NEW_BEACON;
Hai Shalom74f70d42019-02-11 14:42:39 -08005107 int ret = -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005109 int num_suites;
Hai Shalomfdcde762020-04-02 11:19:20 -07005110 u32 suites[20], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005111 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005112#ifdef CONFIG_MESH
5113 struct wpa_driver_mesh_bss_params mesh_params;
5114#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005115
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005116 if (params->mld_ap) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00005117 if (!nl80211_link_valid(bss->valid_links,
5118 params->mld_link_id)) {
5119 wpa_printf(MSG_DEBUG,
5120 "nl80211: Link ID=%u invalid (valid: 0x%04x)",
5121 params->mld_link_id, bss->valid_links);
Sunil Ravi88611412024-06-28 17:34:56 +00005122 return -EINVAL;
5123 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00005124
5125 link = nl80211_get_link(bss, params->mld_link_id);
5126 } else if (bss->valid_links) {
5127 wpa_printf(MSG_DEBUG, "nl80211: MLD configuration expected");
5128 return -EINVAL;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005129 }
5130
5131 beacon_set = params->reenable ? 0 : link->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5134 beacon_set);
5135 if (beacon_set)
5136 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08005137 else if (!drv->device_ap_sme && !drv->use_monitor &&
5138 !nl80211_get_wiphy_data_ap(bss))
5139 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005141 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5142 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005143 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5144 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005145 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005146 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005147 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
5148 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005149 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Hai Shalom74f70d42019-02-11 14:42:39 -08005150 wpa_printf(MSG_DEBUG, "nl80211: ssid=%s",
5151 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005152 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
5153 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
5154 params->head) ||
5155 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
5156 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005157 nl80211_put_beacon_int(msg, params->beacon_int) ||
Hai Shalom60840252021-02-19 19:02:11 -08005158 nl80211_put_beacon_rate(msg, drv->capa.flags, drv->capa.flags2,
5159 params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005160 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005161 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5162 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005163
5164 if (params->mld_ap) {
5165 wpa_printf(MSG_DEBUG, "nl80211: link_id=%u",
5166 params->mld_link_id);
5167
5168 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
Sunil Ravi99c035e2024-07-12 01:42:03 +00005169 params->mld_link_id))
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005170 goto fail;
5171
Sunil Ravi99c035e2024-07-12 01:42:03 +00005172 if (params->freq)
5173 nl80211_link_set_freq(bss, params->mld_link_id,
5174 params->freq->freq);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005175 }
5176
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005177 if (params->proberesp && params->proberesp_len) {
5178 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5179 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5181 params->proberesp))
5182 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005183 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005184 switch (params->hide_ssid) {
5185 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005186 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005187 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5188 NL80211_HIDDEN_SSID_NOT_IN_USE))
5189 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005190 break;
5191 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005192 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005193 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5194 NL80211_HIDDEN_SSID_ZERO_LEN))
5195 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005196 break;
5197 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005198 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005199 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
5200 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
5201 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005202 break;
5203 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005204 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005205 if (params->privacy &&
5206 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
5207 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005208 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005209 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5210 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5211 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005212 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
5213 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
5214 NL80211_AUTHTYPE_SHARED_KEY))
5215 goto fail;
5216 } else {
5217 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
5218 NL80211_AUTHTYPE_OPEN_SYSTEM))
5219 goto fail;
5220 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005221
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005222 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005223 ver = 0;
5224 if (params->wpa_version & WPA_PROTO_WPA)
5225 ver |= NL80211_WPA_VERSION_1;
5226 if (params->wpa_version & WPA_PROTO_RSN)
5227 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005228 if (ver &&
5229 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5230 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005231
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005232 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5233 params->key_mgmt_suites);
Hai Shalomfdcde762020-04-02 11:19:20 -07005234 num_suites = wpa_key_mgmt_to_suites(params->key_mgmt_suites,
5235 suites, ARRAY_SIZE(suites));
5236 if (num_suites > NL80211_MAX_NR_AKM_SUITES)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005237 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07005238 "nl80211: Not enough room for all AKM suites (num_suites=%d > NL80211_MAX_NR_AKM_SUITES)",
5239 num_suites);
5240 else if (num_suites &&
5241 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
5242 suites))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005243 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005244
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005245 if (wpa_key_mgmt_wpa_psk_no_sae(params->key_mgmt_suites) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005246 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005247 params->psk_len &&
5248 nla_put(msg, NL80211_ATTR_PMK, params->psk_len, params->psk))
5249 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005250
5251 if (wpa_key_mgmt_sae(params->key_mgmt_suites) &&
5252 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_AP) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005253 params->sae_password &&
5254 nla_put(msg, NL80211_ATTR_SAE_PASSWORD,
5255 os_strlen(params->sae_password), params->sae_password))
5256 goto fail;
5257
5258 if (nl80211_put_control_port(drv, msg) < 0)
5259 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005260
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005261 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005262 (!params->pairwise_ciphers ||
5263 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005264 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005265 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005266
Sunil Ravia04bd252022-05-02 22:54:18 -07005267 if (drv->device_ap_sme) {
5268 u32 flags = 0;
5269
Sunil Ravi89eba102022-09-13 21:04:37 -07005270 if (params->key_mgmt_suites & (WPA_KEY_MGMT_SAE |
5271 WPA_KEY_MGMT_SAE_EXT_KEY)) {
Sunil Ravia04bd252022-05-02 22:54:18 -07005272 /* Add the previously used flag attribute to support
5273 * older kernel versions and the newer flag bit for
5274 * newer kernels. */
5275 if (nla_put_flag(msg,
5276 NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5277 goto fail;
5278 flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
5279 }
5280
5281 flags |= NL80211_AP_SETTINGS_SA_QUERY_OFFLOAD_SUPPORT;
5282
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005283 if (nl80211_attr_supported(drv,
5284 NL80211_ATTR_AP_SETTINGS_FLAGS) &&
5285 nla_put_u32(msg, NL80211_ATTR_AP_SETTINGS_FLAGS, flags))
Sunil Ravia04bd252022-05-02 22:54:18 -07005286 goto fail;
5287 }
Hai Shalom5f92bc92019-04-18 11:54:11 -07005288
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005289 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5290 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005291 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
5292 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005293 if (num_suites &&
5294 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5295 num_suites * sizeof(u32), suites))
5296 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005297
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005298 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5299 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005300 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005301 if (suite &&
5302 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
5303 goto fail;
5304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005305 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005306 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5307 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005308 if (nla_put(msg, NL80211_ATTR_IE,
5309 wpabuf_len(params->beacon_ies),
5310 wpabuf_head(params->beacon_ies)))
5311 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005312 }
5313 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005314 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5315 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005316 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
5317 wpabuf_len(params->proberesp_ies),
5318 wpabuf_head(params->proberesp_ies)))
5319 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005320 }
5321 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005322 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5323 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005324 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
5325 wpabuf_len(params->assocresp_ies),
5326 wpabuf_head(params->assocresp_ies)))
5327 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005328 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329
Dmitry Shmidt04949592012-07-19 12:16:46 -07005330 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005331 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5332 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005333 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5334 params->ap_max_inactivity))
5335 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005336 }
5337
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005338#ifdef CONFIG_P2P
5339 if (params->p2p_go_ctwindow > 0) {
5340 if (drv->p2p_go_ctwindow_supported) {
5341 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
5342 params->p2p_go_ctwindow);
5343 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
5344 params->p2p_go_ctwindow))
5345 goto fail;
5346 } else {
5347 wpa_printf(MSG_INFO,
5348 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
5349 }
5350 }
5351#endif /* CONFIG_P2P */
5352
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005353 if (params->pbss) {
5354 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
5355 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5356 goto fail;
5357 }
5358
Hai Shalom74f70d42019-02-11 14:42:39 -08005359 if (params->ftm_responder) {
5360 struct nlattr *ftm;
5361
5362 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
5363 ret = -ENOTSUP;
5364 goto fail;
5365 }
5366
5367 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
5368 if (!ftm ||
5369 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
5370 (params->lci &&
5371 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
5372 wpabuf_len(params->lci),
5373 wpabuf_head(params->lci))) ||
5374 (params->civic &&
5375 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
5376 wpabuf_len(params->civic),
5377 wpabuf_head(params->civic))))
5378 goto fail;
5379 nla_nest_end(msg, ftm);
5380 }
5381
Sunil Ravi99c035e2024-07-12 01:42:03 +00005382 if (params->freq && nl80211_put_freq_params(msg, params->freq) < 0)
5383 goto fail;
5384
Hai Shalomc3565922019-10-28 11:58:20 -07005385#ifdef CONFIG_IEEE80211AX
Hai Shalom60840252021-02-19 19:02:11 -08005386 if (params->he_spr_ctrl) {
Hai Shalomc3565922019-10-28 11:58:20 -07005387 struct nlattr *spr;
5388
5389 spr = nla_nest_start(msg, NL80211_ATTR_HE_OBSS_PD);
Hai Shalom60840252021-02-19 19:02:11 -08005390 wpa_printf(MSG_DEBUG, "nl80211: he_spr_ctrl=0x%x",
5391 params->he_spr_ctrl);
Hai Shalomc3565922019-10-28 11:58:20 -07005392
Hai Shalomfdcde762020-04-02 11:19:20 -07005393 if (!spr ||
Hai Shalom60840252021-02-19 19:02:11 -08005394 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_SR_CTRL,
5395 params->he_spr_ctrl) ||
5396 ((params->he_spr_ctrl &
5397 SPATIAL_REUSE_NON_SRG_OFFSET_PRESENT) &&
5398 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET,
5399 params->he_spr_non_srg_obss_pd_max_offset)))
5400 goto fail;
5401
5402 if ((params->he_spr_ctrl &
5403 SPATIAL_REUSE_SRG_INFORMATION_PRESENT) &&
5404 (nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET,
5405 params->he_spr_srg_obss_pd_min_offset) ||
5406 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET,
5407 params->he_spr_srg_obss_pd_max_offset) ||
5408 nla_put(msg, NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP,
5409 sizeof(params->he_spr_bss_color_bitmap),
5410 params->he_spr_bss_color_bitmap) ||
5411 nla_put(msg, NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP,
5412 sizeof(params->he_spr_partial_bssid_bitmap),
5413 params->he_spr_partial_bssid_bitmap)))
Hai Shalomc3565922019-10-28 11:58:20 -07005414 goto fail;
5415
5416 nla_nest_end(msg, spr);
5417 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005418
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005419 if (params->freq && params->freq->he_enabled &&
5420 nl80211_attr_supported(drv, NL80211_ATTR_HE_BSS_COLOR)) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005421 struct nlattr *bss_color;
5422
5423 bss_color = nla_nest_start(msg, NL80211_ATTR_HE_BSS_COLOR);
5424 if (!bss_color ||
5425 (params->he_bss_color_disabled &&
5426 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_DISABLED)) ||
5427 (params->he_bss_color_partial &&
5428 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_PARTIAL)) ||
5429 nla_put_u8(msg, NL80211_HE_BSS_COLOR_ATTR_COLOR,
5430 params->he_bss_color))
5431 goto fail;
5432 nla_nest_end(msg, bss_color);
5433 }
5434
5435 if (params->twt_responder) {
5436 wpa_printf(MSG_DEBUG, "nl80211: twt_responder=%d",
5437 params->twt_responder);
5438 if (nla_put_flag(msg, NL80211_ATTR_TWT_RESPONDER))
5439 goto fail;
5440 }
Hai Shalom60840252021-02-19 19:02:11 -08005441
5442 if (params->unsol_bcast_probe_resp_interval &&
5443 nl80211_unsol_bcast_probe_resp(bss, msg, params) < 0)
5444 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005445
5446 if (nl80211_mbssid(msg, params) < 0)
5447 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07005448#endif /* CONFIG_IEEE80211AX */
5449
Hai Shalom60840252021-02-19 19:02:11 -08005450#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07005451 if (wpa_key_mgmt_sae(params->key_mgmt_suites) &&
Hai Shalom60840252021-02-19 19:02:11 -08005452 nl80211_put_sae_pwe(msg, params->sae_pwe) < 0)
5453 goto fail;
5454#endif /* CONFIG_SAE */
5455
5456#ifdef CONFIG_FILS
5457 if (params->fd_max_int && nl80211_fils_discovery(bss, msg, params) < 0)
5458 goto fail;
5459#endif /* CONFIG_FILS */
5460
Sunil Ravi036cec52023-03-29 11:35:17 -07005461 if (params->punct_bitmap) {
5462 wpa_printf(MSG_DEBUG, "nl80211: Puncturing bitmap=0x%04x",
5463 params->punct_bitmap);
5464 if (nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP,
5465 params->punct_bitmap))
5466 goto fail;
5467 }
5468
Sunil Ravi640215c2023-06-28 23:08:09 +00005469#ifdef CONFIG_DRIVER_NL80211_QCA
5470 if (cmd == NL80211_CMD_NEW_BEACON && params->allowed_freqs)
5471 qca_set_allowed_ap_freqs(drv, params->allowed_freqs,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005472 int_array_len(params->allowed_freqs),
5473 params->mld_ap ? params->mld_link_id :
5474 NL80211_DRV_LINK_ID_NA);
Sunil Ravi640215c2023-06-28 23:08:09 +00005475#endif /* CONFIG_DRIVER_NL80211_QCA */
5476
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005477 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
5478 goto fail;
5479 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
5480 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 if (ret) {
5482 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5483 ret, strerror(-ret));
5484 } else {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005485 link->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005486 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5487 params->short_slot_time, params->ht_opmode,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005488 params->isolate, params->basic_rates,
5489 params->mld_ap ? params->mld_link_id :
5490 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005491 nl80211_set_multicast_to_unicast(bss,
5492 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005493 if (beacon_set && params->freq &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005494 params->freq->bandwidth != link->bandwidth) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005495 wpa_printf(MSG_DEBUG,
5496 "nl80211: Update BSS %s bandwidth: %d -> %d",
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005497 bss->ifname, link->bandwidth,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005498 params->freq->bandwidth);
5499 ret = nl80211_set_channel(bss, params->freq, 1);
5500 if (ret) {
5501 wpa_printf(MSG_DEBUG,
5502 "nl80211: Frequency set failed: %d (%s)",
5503 ret, strerror(-ret));
5504 } else {
5505 wpa_printf(MSG_DEBUG,
5506 "nl80211: Frequency set succeeded for ht2040 coex");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005507 link->bandwidth = params->freq->bandwidth;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005508 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005509 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005510 /*
5511 * cfg80211 updates the driver on frequence change in AP
5512 * mode only at the point when beaconing is started, so
5513 * set the initial value here.
5514 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005515 link->bandwidth = params->freq->bandwidth;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005518
5519#ifdef CONFIG_MESH
5520 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
5521 os_memset(&mesh_params, 0, sizeof(mesh_params));
5522 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
5523 mesh_params.ht_opmode = params->ht_opmode;
5524 ret = nl80211_set_mesh_config(priv, &mesh_params);
5525 if (ret < 0)
5526 return ret;
5527 }
5528#endif /* CONFIG_MESH */
5529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005530 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005531fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005532 nlmsg_free(msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08005533 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534}
5535
5536
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005537static int nl80211_set_channel(struct i802_bss *bss,
5538 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005539{
5540 struct wpa_driver_nl80211_data *drv = bss->drv;
5541 struct nl_msg *msg;
5542 int ret;
5543
5544 wpa_printf(MSG_DEBUG,
Sunil Ravia04bd252022-05-02 22:54:18 -07005545 "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)",
5546 freq->freq, freq->ht_enabled, freq->vht_enabled,
5547 freq->he_enabled, freq->eht_enabled, freq->bandwidth,
5548 freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005549
5550 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
5551 NL80211_CMD_SET_WIPHY);
5552 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
5553 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005554 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005555 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556
Sunil Ravi99c035e2024-07-12 01:42:03 +00005557 if (nl80211_link_valid(bss->valid_links, freq->link_id)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005558 wpa_printf(MSG_DEBUG, "nl80211: Set link_id=%u for freq",
5559 freq->link_id);
5560
5561 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, freq->link_id)) {
5562 nlmsg_free(msg);
5563 return -ENOBUFS;
5564 }
5565 }
5566
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005567 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005568 if (ret == 0) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005569 nl80211_link_set_freq(bss, freq->link_id, freq->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005573 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005574 return -1;
5575}
5576
5577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005578static u32 sta_flags_nl80211(int flags)
5579{
5580 u32 f = 0;
5581
5582 if (flags & WPA_STA_AUTHORIZED)
5583 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5584 if (flags & WPA_STA_WMM)
5585 f |= BIT(NL80211_STA_FLAG_WME);
5586 if (flags & WPA_STA_SHORT_PREAMBLE)
5587 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5588 if (flags & WPA_STA_MFP)
5589 f |= BIT(NL80211_STA_FLAG_MFP);
5590 if (flags & WPA_STA_TDLS_PEER)
5591 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005592 if (flags & WPA_STA_AUTHENTICATED)
5593 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005594 if (flags & WPA_STA_ASSOCIATED)
5595 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005596
5597 return f;
5598}
5599
5600
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005601#ifdef CONFIG_MESH
5602static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
5603{
5604 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005605 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005606 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005607 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005608 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005609 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610 return NL80211_PLINK_OPN_RCVD;
5611 case PLINK_CNF_RCVD:
5612 return NL80211_PLINK_CNF_RCVD;
5613 case PLINK_ESTAB:
5614 return NL80211_PLINK_ESTAB;
5615 case PLINK_HOLDING:
5616 return NL80211_PLINK_HOLDING;
5617 case PLINK_BLOCKED:
5618 return NL80211_PLINK_BLOCKED;
5619 default:
5620 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
5621 state);
5622 }
5623 return -1;
5624}
5625#endif /* CONFIG_MESH */
5626
5627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628static int wpa_driver_nl80211_sta_add(void *priv,
5629 struct hostapd_sta_add_params *params)
5630{
5631 struct i802_bss *bss = priv;
5632 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005633 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005634 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 int ret = -ENOBUFS;
Sunil Ravi036cec52023-03-29 11:35:17 -07005636 u8 cmd;
5637 const char *cmd_string;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005639 if ((params->flags & WPA_STA_TDLS_PEER) &&
5640 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5641 return -EOPNOTSUPP;
5642
Sunil Ravi036cec52023-03-29 11:35:17 -07005643 if (params->mld_link_sta) {
5644 cmd = params->set ? NL80211_CMD_MODIFY_LINK_STA :
5645 NL80211_CMD_ADD_LINK_STA;
5646 cmd_string = params->set ? "NL80211_CMD_MODIFY_LINK_STA" :
5647 "NL80211_CMD_ADD_LINK_STA";
5648 } else {
5649 cmd = params->set ? NL80211_CMD_SET_STATION :
5650 NL80211_CMD_NEW_STATION;
5651 cmd_string = params->set ? "NL80211_CMD_SET_STATION" :
5652 "NL80211_CMD_NEW_STATION";
5653 }
5654
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005655 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
Sunil Ravi036cec52023-03-29 11:35:17 -07005656 cmd_string, MAC2STR(params->addr));
5657 msg = nl80211_bss_msg(bss, 0, cmd);
5658 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005659 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005661 /*
5662 * Set the below properties only in one of the following cases:
5663 * 1. New station is added, already associated.
5664 * 2. Set WPA_STA_TDLS_PEER station.
5665 * 3. Set an already added unassociated station, if driver supports
5666 * full AP client state. (Set these properties after station became
5667 * associated will be rejected by the driver).
5668 */
5669 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
5670 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
5671 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005672 wpa_hexdump(MSG_DEBUG, " * supported rates",
5673 params->supp_rates, params->supp_rates_len);
5674 wpa_printf(MSG_DEBUG, " * capability=0x%x",
5675 params->capability);
5676 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
5677 params->supp_rates_len, params->supp_rates) ||
5678 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
5679 params->capability))
5680 goto fail;
5681
5682 if (params->ht_capabilities) {
5683 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
5684 (u8 *) params->ht_capabilities,
5685 sizeof(*params->ht_capabilities));
5686 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
5687 sizeof(*params->ht_capabilities),
5688 params->ht_capabilities))
5689 goto fail;
5690 }
5691
5692 if (params->vht_capabilities) {
5693 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
5694 (u8 *) params->vht_capabilities,
5695 sizeof(*params->vht_capabilities));
5696 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
5697 sizeof(*params->vht_capabilities),
5698 params->vht_capabilities))
5699 goto fail;
5700 }
5701
Hai Shalom81f62d82019-07-22 12:10:00 -07005702 if (params->he_capab) {
5703 wpa_hexdump(MSG_DEBUG, " * he_capab",
5704 params->he_capab, params->he_capab_len);
5705 if (nla_put(msg, NL80211_ATTR_HE_CAPABILITY,
5706 params->he_capab_len, params->he_capab))
5707 goto fail;
5708 }
5709
Hai Shalom60840252021-02-19 19:02:11 -08005710 if (params->he_6ghz_capab) {
5711 wpa_hexdump(MSG_DEBUG, " * he_6ghz_capab",
5712 params->he_6ghz_capab,
5713 sizeof(*params->he_6ghz_capab));
5714 if (nla_put(msg, NL80211_ATTR_HE_6GHZ_CAPABILITY,
5715 sizeof(*params->he_6ghz_capab),
5716 params->he_6ghz_capab))
5717 goto fail;
5718 }
5719
Sunil Ravia04bd252022-05-02 22:54:18 -07005720 if (params->eht_capab) {
5721 wpa_hexdump(MSG_DEBUG, " * eht_capab",
5722 params->eht_capab, params->eht_capab_len);
5723 if (nla_put(msg, NL80211_ATTR_EHT_CAPABILITY,
5724 params->eht_capab_len, params->eht_capab))
5725 goto fail;
5726 }
5727
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005728 if (params->ext_capab) {
5729 wpa_hexdump(MSG_DEBUG, " * ext_capab",
5730 params->ext_capab, params->ext_capab_len);
5731 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
5732 params->ext_capab_len, params->ext_capab))
5733 goto fail;
5734 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005735
5736 if (is_ap_interface(drv->nlmode) &&
5737 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
5738 params->support_p2p_ps ?
5739 NL80211_P2P_PS_SUPPORTED :
5740 NL80211_P2P_PS_UNSUPPORTED))
5741 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005742 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005743 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005744 if (params->aid) {
5745 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005746 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
5747 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005748 } else {
5749 /*
5750 * cfg80211 validates that AID is non-zero, so we have
5751 * to make this a non-zero value for the TDLS case where
Hai Shalomc1a21442022-02-04 13:43:00 -08005752 * a stub STA entry is used for now and for a station
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005753 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005754 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005755 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
5756 (params->flags & WPA_STA_TDLS_PEER) ?
5757 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005758 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
5759 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005760 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005761 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5762 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005763 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5764 params->listen_interval))
5765 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005766 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
5767 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005768 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
5769 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005770 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
5771 (params->flags & WPA_STA_ASSOCIATED)) {
5772 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
5773 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5774 params->listen_interval);
5775 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
5776 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5777 params->listen_interval))
5778 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005779 }
5780
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005781 if (params->vht_opmode_enabled) {
5782 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005783 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
5784 params->vht_opmode))
5785 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005786 }
5787
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005788 if (params->supp_channels) {
5789 wpa_hexdump(MSG_DEBUG, " * supported channels",
5790 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005791 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
5792 params->supp_channels_len, params->supp_channels))
5793 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005794 }
5795
5796 if (params->supp_oper_classes) {
5797 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
5798 params->supp_oper_classes,
5799 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005800 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
5801 params->supp_oper_classes_len,
5802 params->supp_oper_classes))
5803 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005804 }
5805
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005806 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005807 upd.set = sta_flags_nl80211(params->flags);
5808 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005809
5810 /*
5811 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
5812 * flags, as nl80211 driver moves a new station, by default, into
5813 * associated state.
5814 *
5815 * On the other hand, if the driver supports that feature and the
5816 * station is added in unauthenticated state, set the
5817 * authenticated/associated bits in the mask to prevent moving this
5818 * station to associated state before it is actually associated.
5819 *
5820 * This is irrelevant for mesh mode where the station is added to the
5821 * driver as authenticated already, and ASSOCIATED isn't part of the
5822 * nl80211 API.
5823 */
5824 if (!is_mesh_interface(drv->nlmode)) {
5825 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
5826 wpa_printf(MSG_DEBUG,
5827 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
5828 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
5829 BIT(NL80211_STA_FLAG_AUTHENTICATED));
5830 } else if (!params->set &&
5831 !(params->flags & WPA_STA_TDLS_PEER)) {
5832 if (!(params->flags & WPA_STA_AUTHENTICATED))
5833 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
5834 if (!(params->flags & WPA_STA_ASSOCIATED))
5835 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
5836 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07005837#ifdef CONFIG_MESH
5838 } else {
5839 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
5840 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
5841 params->peer_aid);
5842 if (ret)
5843 goto fail;
5844 }
5845#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005846 }
5847
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005848 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
5849 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005850 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5851 goto fail;
5852
5853#ifdef CONFIG_MESH
5854 if (params->plink_state &&
5855 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
5856 sta_plink_state_nl80211(params->plink_state)))
5857 goto fail;
5858#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005859
Hai Shalomc3565922019-10-28 11:58:20 -07005860 if ((!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
5861 FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) &&
5862 (params->flags & WPA_STA_WMM)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005863 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
5864
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005865 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005866 if (!wme ||
5867 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
5868 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
5869 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
5870 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5871 WMM_QOSINFO_STA_SP_MASK))
5872 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005873 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005874 }
5875
Sunil Ravi036cec52023-03-29 11:35:17 -07005876 /* In case we are an AP MLD need to always specify the link ID */
5877 if (params->mld_link_id >= 0) {
5878 wpa_printf(MSG_DEBUG, " * mld_link_id=%d",
5879 params->mld_link_id);
5880 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
5881 params->mld_link_id))
5882 goto fail;
5883
5884 /*
5885 * If the link address is specified the station is a non-AP MLD
5886 * and thus need to provide the MLD address as the station
5887 * address, and the non-AP MLD link address as the link address.
5888 */
5889 if (params->mld_link_addr) {
5890 wpa_printf(MSG_DEBUG, " * mld_link_addr=" MACSTR,
5891 MAC2STR(params->mld_link_addr));
5892
5893 if (nla_put(msg, NL80211_ATTR_MLD_ADDR,
5894 ETH_ALEN, params->addr) ||
5895 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
5896 params->mld_link_addr))
5897 goto fail;
5898 } else {
5899 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
5900 params->addr))
5901 goto fail;
5902 }
5903 } else {
5904 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
5905 goto fail;
5906 }
5907
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005908 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005909 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910 if (ret)
Sunil Ravi036cec52023-03-29 11:35:17 -07005911 wpa_printf(MSG_DEBUG, "nl80211: %s result: %d (%s)",
5912 cmd_string, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913 if (ret == -EEXIST)
5914 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005915fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005916 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917 return ret;
5918}
5919
5920
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005921static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
5922{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005923 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00005924 struct ndmsg nhdr = {
5925 .ndm_state = NUD_PERMANENT,
5926 .ndm_ifindex = bss->ifindex,
5927 .ndm_family = AF_BRIDGE,
5928 };
5929 struct nl_msg *msg;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005930 int err;
5931
Sunil Ravi99c035e2024-07-12 01:42:03 +00005932 msg = nlmsg_alloc_simple(RTM_DELNEIGH, NLM_F_CREATE);
5933 if (!msg)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005934 return;
5935
Sunil Ravi99c035e2024-07-12 01:42:03 +00005936 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
5937 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr) ||
5938 nl_send_auto_complete(drv->rtnl_sk, msg) < 0)
5939 goto errout;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005940
Sunil Ravi99c035e2024-07-12 01:42:03 +00005941 err = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005942 if (err < 0) {
5943 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
5944 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
5945 bss->ifindex, nl_geterror(err));
5946 } else {
5947 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
5948 MACSTR, MAC2STR(addr));
5949 }
5950
Sunil Ravi99c035e2024-07-12 01:42:03 +00005951errout:
5952 nlmsg_free(msg);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005953}
5954
5955
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005956static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
5957 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959 struct wpa_driver_nl80211_data *drv = bss->drv;
5960 struct nl_msg *msg;
5961 int ret;
5962
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005963 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
5964 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5965 (deauth == 0 &&
5966 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
5967 WLAN_FC_STYPE_DISASSOC)) ||
5968 (deauth == 1 &&
5969 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
5970 WLAN_FC_STYPE_DEAUTH)) ||
5971 (reason_code &&
5972 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
5973 nlmsg_free(msg);
5974 return -ENOBUFS;
5975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005977 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005978 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
5979 " --> %d (%s)",
5980 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005981
5982 if (drv->rtnl_sk)
5983 rtnl_neigh_delete_fdb_entry(bss, addr);
5984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985 if (ret == -ENOENT)
5986 return 0;
5987 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988}
5989
5990
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005991void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992{
5993 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005994 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005995
5996 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005998 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005999 dl_list_for_each(drv2, &drv->global->interfaces,
6000 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006001 {
6002 del_ifidx(drv2, ifidx, IFIDX_ANY);
6003 /* Remove all bridges learned for this iface */
6004 del_ifidx(drv2, IFIDX_ANY, ifidx);
6005 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006006
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006007 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006008 if (send_and_recv_cmd(drv, msg) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6011}
6012
6013
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07006014const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006015{
6016 switch (mode) {
6017 case NL80211_IFTYPE_ADHOC:
6018 return "ADHOC";
6019 case NL80211_IFTYPE_STATION:
6020 return "STATION";
6021 case NL80211_IFTYPE_AP:
6022 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07006023 case NL80211_IFTYPE_AP_VLAN:
6024 return "AP_VLAN";
6025 case NL80211_IFTYPE_WDS:
6026 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006027 case NL80211_IFTYPE_MONITOR:
6028 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07006029 case NL80211_IFTYPE_MESH_POINT:
6030 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006031 case NL80211_IFTYPE_P2P_CLIENT:
6032 return "P2P_CLIENT";
6033 case NL80211_IFTYPE_P2P_GO:
6034 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006035 case NL80211_IFTYPE_P2P_DEVICE:
6036 return "P2P_DEVICE";
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006037 case NL80211_IFTYPE_OCB:
6038 return "OCB";
6039 case NL80211_IFTYPE_NAN:
6040 return "NAN";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006041 default:
6042 return "unknown";
6043 }
6044}
6045
6046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006047static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6048 const char *ifname,
6049 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006050 const u8 *addr, int wds,
6051 int (*handler)(struct nl_msg *, void *),
6052 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006054 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006055 int ifidx;
6056 int ret = -ENOBUFS;
6057
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006058 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6059 iftype, nl80211_iftype_str(iftype));
6060
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006061 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
6062 if (!msg ||
6063 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
6064 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
6065 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066
6067 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006068 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006070 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006071 if (!flags ||
6072 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
6073 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006075 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006077 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
6078 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 }
6080
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006081 /*
6082 * Tell cfg80211 that the interface belongs to the socket that created
6083 * it, and the interface should be deleted when the socket is closed.
6084 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006085 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
6086 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006087
Hai Shalom60840252021-02-19 19:02:11 -08006088 if ((addr && iftype == NL80211_IFTYPE_P2P_DEVICE) &&
6089 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
6090 goto fail;
6091
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006092 ret = send_and_recv_resp(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006093 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006095 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6098 ifname, ret, strerror(-ret));
6099 return ret;
6100 }
6101
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006102 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6103 return 0;
6104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105 ifidx = if_nametoindex(ifname);
6106 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6107 ifname, ifidx);
6108
6109 if (ifidx <= 0)
6110 return -1;
6111
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006112 /*
6113 * Some virtual interfaces need to process EAPOL packets and events on
6114 * the parent interface. This is used mainly with hostapd.
6115 */
6116 if (drv->hostapd ||
6117 iftype == NL80211_IFTYPE_AP_VLAN ||
6118 iftype == NL80211_IFTYPE_WDS ||
6119 iftype == NL80211_IFTYPE_MONITOR) {
6120 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006121 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006123
6124 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006125 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126 nl80211_remove_iface(drv, ifidx);
6127 return -1;
6128 }
6129
6130 return ifidx;
6131}
6132
6133
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006134int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6135 const char *ifname, enum nl80211_iftype iftype,
6136 const u8 *addr, int wds,
6137 int (*handler)(struct nl_msg *, void *),
6138 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139{
6140 int ret;
6141
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006142 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
6143 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006145 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006146 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006147 if (use_existing) {
6148 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
6149 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006150 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
6151 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6152 addr) < 0 &&
6153 (linux_set_iface_flags(drv->global->ioctl_sock,
6154 ifname, 0) < 0 ||
6155 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6156 addr) < 0 ||
6157 linux_set_iface_flags(drv->global->ioctl_sock,
6158 ifname, 1) < 0))
6159 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006160 return -ENFILE;
6161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6163
6164 /* Try to remove the interface that was already there. */
6165 nl80211_remove_iface(drv, if_nametoindex(ifname));
6166
6167 /* Try to create the interface again */
6168 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006169 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 }
6171
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006172 if (ret >= 0 && is_p2p_net_interface(iftype)) {
6173 wpa_printf(MSG_DEBUG,
6174 "nl80211: Interface %s created for P2P - disable 11b rates",
6175 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178
6179 return ret;
6180}
6181
6182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006183static int nl80211_setup_ap(struct i802_bss *bss)
6184{
6185 struct wpa_driver_nl80211_data *drv = bss->drv;
6186
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006187 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
6188 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006189
6190 /*
6191 * Disable Probe Request reporting unless we need it in this way for
6192 * devices that include the AP SME, in the other case (unless using
6193 * monitor iface) we'll get it through the nl_mgmt socket instead.
6194 */
6195 if (!drv->device_ap_sme)
6196 wpa_driver_nl80211_probe_req_report(bss, 0);
6197
6198 if (!drv->device_ap_sme && !drv->use_monitor)
6199 if (nl80211_mgmt_subscribe_ap(bss))
6200 return -1;
6201
6202 if (drv->device_ap_sme && !drv->use_monitor)
6203 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006204 wpa_printf(MSG_DEBUG,
6205 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006206
6207 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07006208 nl80211_create_monitor_interface(drv) &&
6209 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006210 return -1;
6211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006212 if (drv->device_ap_sme &&
6213 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6214 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6215 "Probe Request frame reporting in AP mode");
6216 /* Try to survive without this */
6217 }
6218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006219 return 0;
6220}
6221
6222
6223static void nl80211_teardown_ap(struct i802_bss *bss)
6224{
6225 struct wpa_driver_nl80211_data *drv = bss->drv;
6226
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006227 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
6228 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006229 if (drv->device_ap_sme) {
6230 wpa_driver_nl80211_probe_req_report(bss, 0);
6231 if (!drv->use_monitor)
6232 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6233 } else if (drv->use_monitor)
6234 nl80211_remove_monitor_interface(drv);
6235 else
6236 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6237
Paul Stewart092955c2017-02-06 09:13:09 -08006238 nl80211_put_wiphy_data_ap(bss);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006239 if (bss->flink)
6240 bss->flink->beacon_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006241}
6242
6243
Hai Shalomfdcde762020-04-02 11:19:20 -07006244static int nl80211_tx_control_port(void *priv, const u8 *dest,
6245 u16 proto, const u8 *buf, size_t len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006246 int no_encrypt, int link_id)
Hai Shalomfdcde762020-04-02 11:19:20 -07006247{
Hai Shalom899fcc72020-10-19 14:38:18 -07006248 struct nl80211_ack_ext_arg ext_arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07006249 struct i802_bss *bss = priv;
6250 struct nl_msg *msg;
Hai Shalom899fcc72020-10-19 14:38:18 -07006251 u64 cookie = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07006252 int ret;
6253
6254 wpa_printf(MSG_DEBUG,
6255 "nl80211: Send over control port dest=" MACSTR
6256 " proto=0x%04x len=%u no_encrypt=%d",
6257 MAC2STR(dest), proto, (unsigned int) len, no_encrypt);
6258
6259 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CONTROL_PORT_FRAME);
6260 if (!msg ||
6261 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
6262 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dest) ||
6263 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
6264 (no_encrypt &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006265 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)) ||
6266 (link_id != NL80211_DRV_LINK_ID_NA &&
6267 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006268 nlmsg_free(msg);
6269 return -ENOBUFS;
6270 }
6271
Hai Shalom899fcc72020-10-19 14:38:18 -07006272 os_memset(&ext_arg, 0, sizeof(struct nl80211_ack_ext_arg));
6273 ext_arg.ext_data = &cookie;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006274 ret = send_and_recv(bss->drv->global, bss->drv->global->nl, msg,
6275 NULL, NULL, ack_handler_cookie, &ext_arg, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07006276 if (ret) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006277 wpa_printf(MSG_DEBUG,
6278 "nl80211: tx_control_port failed: ret=%d (%s)",
6279 ret, strerror(-ret));
Hai Shalom899fcc72020-10-19 14:38:18 -07006280 } else {
6281 struct wpa_driver_nl80211_data *drv = bss->drv;
6282
6283 wpa_printf(MSG_DEBUG,
6284 "nl80211: tx_control_port cookie=0x%llx",
6285 (long long unsigned int) cookie);
6286 drv->eapol_tx_cookie = cookie;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006287 drv->eapol_tx_link_id = link_id;
Hai Shalom899fcc72020-10-19 14:38:18 -07006288 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006289
6290 return ret;
6291}
6292
6293
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006294static int nl80211_send_eapol_data(struct i802_bss *bss,
6295 const u8 *addr, const u8 *data,
6296 size_t data_len)
6297{
6298 struct sockaddr_ll ll;
6299 int ret;
6300
6301 if (bss->drv->eapol_tx_sock < 0) {
6302 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6303 return -1;
6304 }
6305
6306 os_memset(&ll, 0, sizeof(ll));
6307 ll.sll_family = AF_PACKET;
6308 ll.sll_ifindex = bss->ifindex;
6309 ll.sll_protocol = htons(ETH_P_PAE);
6310 ll.sll_halen = ETH_ALEN;
6311 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6312 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6313 (struct sockaddr *) &ll, sizeof(ll));
6314 if (ret < 0)
6315 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6316 strerror(errno));
6317
6318 return ret;
6319}
6320
6321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6323
6324static int wpa_driver_nl80211_hapd_send_eapol(
6325 void *priv, const u8 *addr, const u8 *data,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006326 size_t data_len, int encrypt, const u8 *own_addr, u32 flags,
6327 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006328{
6329 struct i802_bss *bss = priv;
6330 struct wpa_driver_nl80211_data *drv = bss->drv;
6331 struct ieee80211_hdr *hdr;
6332 size_t len;
6333 u8 *pos;
6334 int res;
6335 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08006336
Hai Shalomb755a2a2020-04-23 21:49:02 -07006337 /* For now, disable EAPOL TX over control port in AP mode by default
6338 * since it does not provide TX status notifications. */
6339 if (drv->control_port_ap &&
6340 (drv->capa.flags & WPA_DRIVER_FLAGS_CONTROL_PORT))
Hai Shalomfdcde762020-04-02 11:19:20 -07006341 return nl80211_tx_control_port(bss, addr, ETH_P_EAPOL,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006342 data, data_len, !encrypt,
6343 link_id);
Hai Shalomfdcde762020-04-02 11:19:20 -07006344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006345 if (drv->device_ap_sme || !drv->use_monitor)
6346 return nl80211_send_eapol_data(bss, addr, data, data_len);
6347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006348 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6349 data_len;
6350 hdr = os_zalloc(len);
6351 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006352 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
6353 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006354 return -1;
6355 }
6356
6357 hdr->frame_control =
6358 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6359 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6360 if (encrypt)
6361 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6362 if (qos) {
6363 hdr->frame_control |=
6364 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6365 }
6366
6367 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6368 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6369 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6370 pos = (u8 *) (hdr + 1);
6371
6372 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006373 /* Set highest priority in QoS header */
6374 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006375 pos[1] = 0;
6376 pos += 2;
6377 }
6378
6379 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6380 pos += sizeof(rfc1042_header);
6381 WPA_PUT_BE16(pos, ETH_P_PAE);
6382 pos += 2;
6383 memcpy(pos, data, data_len);
6384
Hai Shalomfdcde762020-04-02 11:19:20 -07006385 res = nl80211_send_monitor(drv, hdr, len, encrypt, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006386 if (res < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07006387 wpa_printf(MSG_ERROR,
6388 "hapd_send_eapol - packet len: %lu - failed",
6389 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390 }
6391 os_free(hdr);
6392
6393 return res;
6394}
6395
6396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006397static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006398 unsigned int total_flags,
6399 unsigned int flags_or,
6400 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006401{
6402 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006403 struct nl_msg *msg;
6404 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405 struct nl80211_sta_flag_update upd;
6406
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006407 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
6408 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
6409 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
6410 !!(total_flags & WPA_STA_AUTHORIZED));
6411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006412 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6413 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
6414 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006415
6416 /*
6417 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6418 * can be removed eventually.
6419 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006420 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006421 if (!flags ||
6422 ((total_flags & WPA_STA_AUTHORIZED) &&
6423 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
6424 ((total_flags & WPA_STA_WMM) &&
6425 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
6426 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
6427 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
6428 ((total_flags & WPA_STA_MFP) &&
6429 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
6430 ((total_flags & WPA_STA_TDLS_PEER) &&
6431 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
6432 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006433
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006434 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006435
6436 os_memset(&upd, 0, sizeof(upd));
6437 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6438 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006439 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
6440 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006442 return send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006443fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006444 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006445 return -ENOBUFS;
6446}
6447
6448
Hai Shalom81f62d82019-07-22 12:10:00 -07006449static int driver_nl80211_sta_set_airtime_weight(void *priv, const u8 *addr,
6450 unsigned int weight)
6451{
6452 struct i802_bss *bss = priv;
6453 struct nl_msg *msg;
Hai Shalomc1a21442022-02-04 13:43:00 -08006454 int ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07006455
6456 wpa_printf(MSG_DEBUG,
6457 "nl80211: Set STA airtime weight - ifname=%s addr=" MACSTR
6458 " weight=%u", bss->ifname, MAC2STR(addr), weight);
6459
6460 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6461 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6462 nla_put_u16(msg, NL80211_ATTR_AIRTIME_WEIGHT, weight))
6463 goto fail;
6464
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006465 ret = send_and_recv_cmd(bss->drv, msg);
Hai Shalomc1a21442022-02-04 13:43:00 -08006466 if (ret) {
6467 wpa_printf(MSG_DEBUG,
6468 "nl80211: SET_STATION[AIRTIME_WEIGHT] failed: ret=%d (%s)",
6469 ret, strerror(-ret));
6470 }
6471 return ret;
Hai Shalom81f62d82019-07-22 12:10:00 -07006472fail:
6473 nlmsg_free(msg);
6474 return -ENOBUFS;
6475}
6476
6477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6479 struct wpa_driver_associate_params *params)
6480{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006481 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006482
6483 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006484 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6485 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006486 nlmode = NL80211_IFTYPE_P2P_GO;
6487 } else
6488 nlmode = NL80211_IFTYPE_AP;
6489
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006490 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006491 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006492 nl80211_remove_monitor_interface(drv);
6493 return -1;
6494 }
6495
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006496 if (params->freq.freq &&
6497 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006498 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006499 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500 nl80211_remove_monitor_interface(drv);
6501 return -1;
6502 }
6503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006504 return 0;
6505}
6506
6507
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006508static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
6509 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510{
6511 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006512 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006514 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006515 ret = send_and_recv(drv->global, drv->first_bss->nl_connect, msg, NULL,
6516 NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 if (ret) {
6518 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6519 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006520 } else {
6521 wpa_printf(MSG_DEBUG,
6522 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 }
6524
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006525 if (reset_mode &&
6526 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07006527 NL80211_IFTYPE_STATION)) {
6528 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6529 "station mode");
6530 }
6531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532 return ret;
6533}
6534
6535
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006536static int nl80211_ht_vht_overrides(struct nl_msg *msg,
6537 struct wpa_driver_associate_params *params)
6538{
6539 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
6540 return -1;
6541
6542 if (params->htcaps && params->htcaps_mask) {
6543 int sz = sizeof(struct ieee80211_ht_capabilities);
6544 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
6545 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
6546 params->htcaps_mask, sz);
6547 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
6548 params->htcaps) ||
6549 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6550 params->htcaps_mask))
6551 return -1;
6552 }
6553
6554#ifdef CONFIG_VHT_OVERRIDES
6555 if (params->disable_vht) {
6556 wpa_printf(MSG_DEBUG, " * VHT disabled");
6557 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
6558 return -1;
6559 }
6560
6561 if (params->vhtcaps && params->vhtcaps_mask) {
6562 int sz = sizeof(struct ieee80211_vht_capabilities);
6563 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
6564 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
6565 params->vhtcaps_mask, sz);
6566 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
6567 params->vhtcaps) ||
6568 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
6569 params->vhtcaps_mask))
6570 return -1;
6571 }
6572#endif /* CONFIG_VHT_OVERRIDES */
6573
Hai Shalomc1a21442022-02-04 13:43:00 -08006574#ifdef CONFIG_HE_OVERRIDES
6575 if (params->disable_he) {
6576 wpa_printf(MSG_DEBUG, " * HE disabled");
6577 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_HE))
6578 return -1;
6579 }
6580#endif /* CONFIG_HE_OVERRIDES */
6581
Sunil Ravi77d572f2023-01-17 23:58:31 +00006582 if (params->disable_eht) {
6583 wpa_printf(MSG_DEBUG, " * EHT disabled");
6584 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_EHT))
6585 return -1;
6586 }
6587
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006588 return 0;
6589}
6590
6591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6593 struct wpa_driver_associate_params *params)
6594{
6595 struct nl_msg *msg;
6596 int ret = -1;
6597 int count = 0;
6598
6599 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6600
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006601 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6603 "IBSS mode");
6604 return -1;
6605 }
6606
6607retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006608 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
6609 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6610 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611
Hai Shalom74f70d42019-02-11 14:42:39 -08006612 wpa_printf(MSG_DEBUG, " * SSID=%s",
6613 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006614 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
6615 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006616 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6617 drv->ssid_len = params->ssid_len;
6618
Dmitry Shmidtff787d52015-01-12 13:01:47 -08006619 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
6620 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006621 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006622
6623 ret = nl80211_set_conn_keys(params, msg);
6624 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006625 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006626
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006627 if (params->bssid && params->fixed_bssid) {
6628 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6629 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006630 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
6631 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006632 }
6633
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006634 if (params->fixed_freq) {
6635 wpa_printf(MSG_DEBUG, " * fixed_freq");
6636 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
6637 goto fail;
6638 }
6639
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006640 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
6641 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
6642 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006643 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
6644 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006645 wpa_printf(MSG_DEBUG, " * control port");
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006646 if (nl80211_put_control_port(drv, msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006647 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006648 }
6649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006650 if (params->wpa_ie) {
6651 wpa_hexdump(MSG_DEBUG,
6652 " * Extra IEs for Beacon/Probe Response frames",
6653 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006654 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6655 params->wpa_ie))
6656 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006657 }
6658
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08006659 ret = nl80211_ht_vht_overrides(msg, params);
6660 if (ret < 0)
6661 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006662
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006663 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
6664 goto fail;
6665 ret = send_and_recv(drv->global, drv->first_bss->nl_connect, msg, NULL,
6666 NULL, NULL, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006667 msg = NULL;
6668 if (ret) {
6669 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6670 ret, strerror(-ret));
6671 count++;
6672 if (ret == -EALREADY && count == 1) {
6673 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6674 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006675 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676 nlmsg_free(msg);
6677 goto retry;
6678 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006679 } else {
6680 wpa_printf(MSG_DEBUG,
6681 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006683
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006684fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006685 nlmsg_free(msg);
6686 return ret;
6687}
6688
6689
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006690static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
6691 struct wpa_driver_associate_params *params,
6692 struct nl_msg *msg)
6693{
6694 if (params->fils_erp_username_len) {
6695 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
6696 params->fils_erp_username,
6697 params->fils_erp_username_len);
6698 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
6699 params->fils_erp_username_len,
6700 params->fils_erp_username))
6701 return -1;
6702 }
6703
6704 if (params->fils_erp_realm_len) {
6705 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
6706 params->fils_erp_realm,
6707 params->fils_erp_realm_len);
6708 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
6709 params->fils_erp_realm_len, params->fils_erp_realm))
6710 return -1;
6711 }
6712
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006713 if (params->fils_erp_rrk_len) {
Vinita S. Maloo3a5b4412020-05-19 17:43:22 +05306714 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
6715 params->fils_erp_next_seq_num);
6716 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
6717 params->fils_erp_next_seq_num))
6718 return -1;
6719
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006720 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
6721 (unsigned long) params->fils_erp_rrk_len);
6722 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
6723 params->fils_erp_rrk_len, params->fils_erp_rrk))
6724 return -1;
6725 }
6726
6727 return 0;
6728}
6729
6730
Sunil Ravi89eba102022-09-13 21:04:37 -07006731static unsigned int num_bits_set(u32 val)
6732{
6733 unsigned int c;
6734
6735 for (c = 0; val; c++)
6736 val &= val - 1;
6737
6738 return c;
6739}
6740
6741
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006742static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
6743 struct wpa_driver_associate_params *params,
6744 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006745{
Sunil Ravi77d572f2023-01-17 23:58:31 +00006746 if (params->mld_params.mld_addr && params->mld_params.valid_links > 0) {
6747 struct wpa_driver_mld_params *mld_params = &params->mld_params;
6748 struct nlattr *links, *attr;
Sunil Ravi77d572f2023-01-17 23:58:31 +00006749 u8 link_id;
6750
6751 wpa_printf(MSG_DEBUG, " * MLD: MLD addr=" MACSTR,
6752 MAC2STR(mld_params->mld_addr));
6753
6754 if (nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN,
6755 mld_params->mld_addr) ||
6756 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6757 mld_params->assoc_link_id))
6758 return -1;
6759
6760 links = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
6761 if (!links)
6762 return -1;
6763
Sunil Ravi99c035e2024-07-12 01:42:03 +00006764 for_each_link(mld_params->valid_links, link_id) {
6765 attr = nla_nest_start(msg, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006766 if (!attr)
6767 return -1;
6768
6769 if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID,
6770 link_id) ||
6771 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
6772 mld_params->mld_links[link_id].bssid) ||
6773 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
6774 mld_params->mld_links[link_id].freq) ||
Sunil Ravi99c035e2024-07-12 01:42:03 +00006775 (mld_params->mld_links[link_id].disabled &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006776 nla_put_flag(msg,
6777 NL80211_ATTR_MLO_LINK_DISABLED)) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00006778 (mld_params->mld_links[link_id].ies &&
Sunil Ravi99c035e2024-07-12 01:42:03 +00006779 mld_params->mld_links[link_id].ies_len &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00006780 nla_put(msg, NL80211_ATTR_IE,
6781 mld_params->mld_links[link_id].ies_len,
6782 mld_params->mld_links[link_id].ies)))
6783 return -1;
6784
6785 os_memcpy(drv->sta_mlo_info.links[link_id].bssid,
6786 mld_params->mld_links[link_id].bssid,
6787 ETH_ALEN);
6788 nla_nest_end(msg, attr);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006789 }
6790
6791 nla_nest_end(msg, links);
6792
6793 os_memcpy(drv->sta_mlo_info.ap_mld_addr,
6794 params->mld_params.mld_addr, ETH_ALEN);
6795 drv->sta_mlo_info.assoc_link_id = mld_params->assoc_link_id;
6796 drv->sta_mlo_info.req_links = mld_params->valid_links;
6797 }
6798
Paul Stewart092955c2017-02-06 09:13:09 -08006799 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
6800 return -1;
6801
Sunil Ravi77d572f2023-01-17 23:58:31 +00006802 if (params->bssid && !params->mld_params.mld_addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006803 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6804 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006805 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
6806 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006807 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006808
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006809 if (params->bssid_hint) {
6810 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
6811 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006812 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
6813 params->bssid_hint))
6814 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006815 }
6816
Sunil Ravi77d572f2023-01-17 23:58:31 +00006817 if (params->freq.freq && !params->mld_params.mld_addr) {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006818 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006819 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
6820 params->freq.freq))
6821 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006822 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006823 } else
6824 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006825
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006826 if (params->freq_hint) {
6827 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006828 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
6829 params->freq_hint))
6830 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006831 }
6832
Hai Shalomc3565922019-10-28 11:58:20 -07006833 if (params->freq.edmg.channels && params->freq.edmg.bw_config) {
6834 wpa_printf(MSG_DEBUG,
6835 " * EDMG configuration: channels=0x%x bw_config=%d",
6836 params->freq.edmg.channels,
6837 params->freq.edmg.bw_config);
6838 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
6839 params->freq.edmg.channels) ||
6840 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
6841 params->freq.edmg.bw_config))
6842 return -1;
6843 }
6844
Dmitry Shmidt04949592012-07-19 12:16:46 -07006845 if (params->bg_scan_period >= 0) {
6846 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
6847 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006848 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
6849 params->bg_scan_period))
6850 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006851 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08006854 wpa_printf(MSG_DEBUG, " * SSID=%s",
6855 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006856 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
6857 params->ssid))
6858 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006860 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006861 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6862 drv->ssid_len = params->ssid_len;
6863 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006865 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006866 if (params->wpa_ie &&
6867 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
6868 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006869
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006870 if (params->wpa_proto) {
6871 enum nl80211_wpa_versions ver = 0;
6872
6873 if (params->wpa_proto & WPA_PROTO_WPA)
6874 ver |= NL80211_WPA_VERSION_1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006875 if (params->wpa_proto & WPA_PROTO_RSN) {
6876#if !defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(CONFIG_DRIVER_NL80211_SYNA)
6877 /*
6878 * NL80211_ATTR_SAE_PASSWORD is related and was added
6879 * at the same time as NL80211_WPA_VERSION_3.
6880 */
6881 if (nl80211_attr_supported(drv,
6882 NL80211_ATTR_SAE_PASSWORD) &&
6883 wpa_key_mgmt_sae(params->key_mgmt_suite))
6884 ver |= NL80211_WPA_VERSION_3;
6885 else
6886#endif
6887 ver |= NL80211_WPA_VERSION_2;
6888 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006889
6890 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006891 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
6892 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006893 }
6894
6895 if (params->pairwise_suite != WPA_CIPHER_NONE) {
6896 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
6897 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006898 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6899 cipher))
6900 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006901 }
6902
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006903 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
6904 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
6905 /*
6906 * This is likely to work even though many drivers do not
6907 * advertise support for operations without GTK.
6908 */
6909 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
6910 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006911 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
6912 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006913 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
6914 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006915 }
6916
6917 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
6918 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
6919 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
6920 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07006921 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006922 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
6923 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006924 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07006925 params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
Sunil Ravi89eba102022-09-13 21:04:37 -07006926 params->key_mgmt_suite == WPA_KEY_MGMT_SAE_EXT_KEY ||
Hai Shalom021b0b52019-04-10 11:17:58 -07006927 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE ||
Sunil Ravi89eba102022-09-13 21:04:37 -07006928 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE_EXT_KEY ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006929 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006930 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07006931 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006932 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
6933 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
6934 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07006935 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
6936 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006937 params->key_mgmt_suite == WPA_KEY_MGMT_DPP ||
6938 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384) {
Sunil Ravi89eba102022-09-13 21:04:37 -07006939 u32 *mgmt;
6940 unsigned int akm_count = 1, i;
6941
6942 /*
6943 * Make sure the driver has capability to handle default AKM in
6944 * key_mgmt_suite plus allowed AKMs in allowed_key_mgmts.
6945 */
6946 if (drv->capa.max_num_akms <=
6947 num_bits_set(params->allowed_key_mgmts)) {
6948 wpa_printf(MSG_INFO,
6949 "nl80211: Not enough support for the allowed AKMs (max_num_akms=%u <= num_bits_set=%u)",
6950 drv->capa.max_num_akms,
6951 num_bits_set(params->allowed_key_mgmts));
6952 return -1;
6953 }
6954
6955 mgmt = os_malloc(sizeof(u32) * drv->capa.max_num_akms);
6956 if (!mgmt)
6957 return -1;
6958
6959 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006960
6961 switch (params->key_mgmt_suite) {
6962 case WPA_KEY_MGMT_CCKM:
Sunil Ravi89eba102022-09-13 21:04:37 -07006963 mgmt[0] = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006964 break;
6965 case WPA_KEY_MGMT_IEEE8021X:
Sunil Ravi89eba102022-09-13 21:04:37 -07006966 mgmt[0] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006967 break;
6968 case WPA_KEY_MGMT_FT_IEEE8021X:
Sunil Ravi89eba102022-09-13 21:04:37 -07006969 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006970 break;
6971 case WPA_KEY_MGMT_FT_PSK:
Sunil Ravi89eba102022-09-13 21:04:37 -07006972 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006973 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006974 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07006975 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006976 break;
6977 case WPA_KEY_MGMT_PSK_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07006978 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07006979 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006980 case WPA_KEY_MGMT_OSEN:
Sunil Ravi89eba102022-09-13 21:04:37 -07006981 mgmt[0] = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006982 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07006983 case WPA_KEY_MGMT_SAE:
Sunil Ravi89eba102022-09-13 21:04:37 -07006984 mgmt[0] = RSN_AUTH_KEY_MGMT_SAE;
6985 break;
6986 case WPA_KEY_MGMT_SAE_EXT_KEY:
6987 mgmt[0] = RSN_AUTH_KEY_MGMT_SAE_EXT_KEY;
Hai Shalom021b0b52019-04-10 11:17:58 -07006988 break;
6989 case WPA_KEY_MGMT_FT_SAE:
Sunil Ravi89eba102022-09-13 21:04:37 -07006990 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_SAE;
6991 break;
6992 case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
6993 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY;
Hai Shalom021b0b52019-04-10 11:17:58 -07006994 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006995 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Sunil Ravi89eba102022-09-13 21:04:37 -07006996 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006997 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08006998 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Sunil Ravi89eba102022-09-13 21:04:37 -07006999 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007000 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07007001 case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007002 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
Hai Shalom021b0b52019-04-10 11:17:58 -07007003 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007004 case WPA_KEY_MGMT_FILS_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007005 mgmt[0] = RSN_AUTH_KEY_MGMT_FILS_SHA256;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007006 break;
7007 case WPA_KEY_MGMT_FILS_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007008 mgmt[0] = RSN_AUTH_KEY_MGMT_FILS_SHA384;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007009 break;
7010 case WPA_KEY_MGMT_FT_FILS_SHA256:
Sunil Ravi89eba102022-09-13 21:04:37 -07007011 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007012 break;
7013 case WPA_KEY_MGMT_FT_FILS_SHA384:
Sunil Ravi89eba102022-09-13 21:04:37 -07007014 mgmt[0] = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007015 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007016 case WPA_KEY_MGMT_OWE:
Sunil Ravi89eba102022-09-13 21:04:37 -07007017 mgmt[0] = RSN_AUTH_KEY_MGMT_OWE;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007018 break;
7019 case WPA_KEY_MGMT_DPP:
Sunil Ravi89eba102022-09-13 21:04:37 -07007020 mgmt[0] = RSN_AUTH_KEY_MGMT_DPP;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007021 break;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007022 case WPA_KEY_MGMT_IEEE8021X_SHA384:
7023 mgmt[0] = RSN_AUTH_KEY_MGMT_802_1X_SHA384;
7024 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007025 case WPA_KEY_MGMT_PSK:
7026 default:
Sunil Ravi89eba102022-09-13 21:04:37 -07007027 mgmt[0] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007028 break;
7029 }
Sunil Ravi89eba102022-09-13 21:04:37 -07007030
7031 if (drv->capa.max_num_akms > 1) {
7032 akm_count += wpa_key_mgmt_to_suites(
7033 params->allowed_key_mgmts, &mgmt[1],
7034 drv->capa.max_num_akms - 1);
7035 }
7036
7037 for (i = 0; i < akm_count; i++)
7038 wpa_printf(MSG_DEBUG, " * akm[%d]=0x%x", i, mgmt[i]);
7039
7040 if (nla_put(msg, NL80211_ATTR_AKM_SUITES,
7041 akm_count * sizeof(u32), mgmt)) {
7042 os_free(mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007043 return -1;
Sunil Ravi89eba102022-09-13 21:04:37 -07007044 }
7045
7046 os_free(mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007047 }
7048
Isaac Chiou6ce580d2024-04-24 17:07:24 +08007049#if (defined(CONFIG_DRIVER_NL80211_BRCM) && !defined(WIFI_BRCM_OPEN_SOURCE_MULTI_AKM)) || \
7050 defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05307051 if (IS_CROSS_AKM_ROAM_KEY_MGMT(params->key_mgmt_suite)) {
7052 int num_suites;
7053 u32 suites[NL80211_MAX_NR_AKM_SUITES];
7054
7055 wpa_printf(MSG_INFO, "nl80211: key_mgmt_suites=0x%x",
7056 params->key_mgmt_suite);
7057 num_suites = wpa_cross_akm_key_mgmt_to_suites(params->key_mgmt_suite,
7058 suites, ARRAY_SIZE(suites));
7059 if (num_suites &&
7060 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32), suites)) {
7061 wpa_printf(MSG_ERROR, "Updating multi akm_suite failed");
7062 return -1;
7063 }
7064 }
Isaac Chiou6ce580d2024-04-24 17:07:24 +08007065#endif /* (CONFIG_DRIVER_NL80211_BRCM && !WIFI_BRCM_OPEN_SOURCE_MULTI_AKM) ||
7066 * CONFIG_DRIVER_NL80211_SYNA */
Hai Shalomc3565922019-10-28 11:58:20 -07007067 if (params->req_handshake_offload &&
Hai Shalom74f70d42019-02-11 14:42:39 -08007068 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)) {
7069 wpa_printf(MSG_DEBUG, " * WANT_1X_4WAY_HS");
7070 if (nla_put_flag(msg, NL80211_ATTR_WANT_1X_4WAY_HS))
7071 return -1;
7072 }
7073
Roshan Pius3a1667e2018-07-03 15:17:14 -07007074 /* Add PSK in case of 4-way handshake offload */
7075 if (params->psk &&
Hai Shalom74f70d42019-02-11 14:42:39 -08007076 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07007077 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
7078 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
7079 return -1;
7080 }
7081
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007082 if (nl80211_put_control_port(drv, msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007083 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007084
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07007085 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
7086 (params->pairwise_suite == WPA_CIPHER_NONE ||
7087 params->pairwise_suite == WPA_CIPHER_WEP104 ||
7088 params->pairwise_suite == WPA_CIPHER_WEP40) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007089 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07007090 return -1;
7091
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007092 if (params->rrm_used) {
7093 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007094 if ((!((drv_rrm_flags &
7095 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
7096 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
7097 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007098 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
7099 return -1;
7100 }
7101
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007102 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007103 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007104
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007105 if (params->p2p)
7106 wpa_printf(MSG_DEBUG, " * P2P group");
7107
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007108 if (params->pbss) {
7109 wpa_printf(MSG_DEBUG, " * PBSS");
7110 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
7111 return -1;
7112 }
7113
Dmitry Shmidte4663042016-04-04 10:07:49 -07007114 drv->connect_reassoc = 0;
7115 if (params->prev_bssid) {
7116 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7117 MAC2STR(params->prev_bssid));
7118 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7119 params->prev_bssid))
7120 return -1;
7121 drv->connect_reassoc = 1;
7122 }
7123
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007124 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
7125 nl80211_put_fils_connect_params(drv, params, msg) != 0)
7126 return -1;
7127
Sunil Ravi89eba102022-09-13 21:04:37 -07007128 if ((wpa_key_mgmt_sae(params->key_mgmt_suite) ||
7129 wpa_key_mgmt_sae(params->allowed_key_mgmts)) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07007130 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
7131 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
7132 return -1;
7133
Sunil Ravi036cec52023-03-29 11:35:17 -07007134 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
7135 nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT))
7136 return -1;
7137
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007138 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007139}
7140
7141
7142static int wpa_driver_nl80211_try_connect(
7143 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007144 struct wpa_driver_associate_params *params,
Hai Shalomc1a21442022-02-04 13:43:00 -08007145 struct i802_bss *bss)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007146{
7147 struct nl_msg *msg;
7148 enum nl80211_auth_type type;
7149 int ret;
7150 int algs;
7151
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007152#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007153 if (params->req_key_mgmt_offload && params->psk &&
Sunil Ravi89eba102022-09-13 21:04:37 -07007154 (wpa_key_mgmt_wpa_psk_no_sae(params->key_mgmt_suite) ||
7155 wpa_key_mgmt_wpa_psk_no_sae(params->allowed_key_mgmts))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007156 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
7157 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
7158 if (ret)
7159 return ret;
7160 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007161#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007162
7163 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
7164 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007165 if (!msg)
7166 return -1;
7167
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007168 ret = nl80211_connect_common(drv, params, msg);
7169 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007170 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007171
Roshan Pius3a1667e2018-07-03 15:17:14 -07007172 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
7173 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
7174 goto fail;
7175
7176 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
7177 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
7178 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
7179 goto fail;
7180
Hai Shalom60840252021-02-19 19:02:11 -08007181#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07007182 if ((wpa_key_mgmt_sae(params->key_mgmt_suite) ||
7183 wpa_key_mgmt_sae(params->allowed_key_mgmts)) &&
Hai Shalom60840252021-02-19 19:02:11 -08007184 nl80211_put_sae_pwe(msg, params->sae_pwe) < 0)
7185 goto fail;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007186
7187 /* Add SAE password in case of SAE authentication offload */
7188 if ((params->sae_password || params->passphrase) &&
7189 (drv->capa.flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA)) {
7190 const char *password;
7191 size_t pwd_len;
7192
7193 if (params->sae_password && params->sae_password_id) {
7194 wpa_printf(MSG_INFO,
7195 "nl80211: Use of SAE password identifiers not supported with driver-based SAE");
7196 goto fail;
7197 }
7198
7199 password = params->sae_password;
7200 if (!password)
7201 password = params->passphrase;
7202 pwd_len = os_strlen(password);
7203 wpa_printf(MSG_DEBUG, " * SAE password");
7204 if (nla_put(msg, NL80211_ATTR_SAE_PASSWORD, pwd_len, password))
7205 goto fail;
7206 }
Hai Shalom60840252021-02-19 19:02:11 -08007207#endif /* CONFIG_SAE */
7208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209 algs = 0;
7210 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7211 algs++;
7212 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7213 algs++;
7214 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7215 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007216 if (params->auth_alg & WPA_AUTH_ALG_FILS)
7217 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007218 if (params->auth_alg & WPA_AUTH_ALG_FT)
7219 algs++;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007220 if (params->auth_alg & WPA_AUTH_ALG_SAE)
7221 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007222 if (algs > 1) {
7223 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7224 "selection");
7225 goto skip_auth_type;
7226 }
7227
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007228 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007229 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007230 if (type == NL80211_AUTHTYPE_MAX ||
7231 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007232 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233
7234skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007235 ret = nl80211_set_conn_keys(params, msg);
7236 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007237 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007238
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007239 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
7240 goto fail;
7241 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
7242 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007243 msg = NULL;
7244 if (ret) {
7245 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7246 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007247 } else {
Hai Shalom60840252021-02-19 19:02:11 -08007248#ifdef CONFIG_DRIVER_NL80211_QCA
7249 drv->roam_indication_done = false;
7250#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007251 wpa_printf(MSG_DEBUG,
7252 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007254
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007255fail:
Hai Shalom74f70d42019-02-11 14:42:39 -08007256 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007257 nlmsg_free(msg);
7258 return ret;
7259
7260}
7261
7262
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007263static int wpa_driver_nl80211_connect(
7264 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007265 struct wpa_driver_associate_params *params,
Hai Shalomc1a21442022-02-04 13:43:00 -08007266 struct i802_bss *bss)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007267{
Jithu Jancea7c60b42014-12-03 18:54:40 +05307268 int ret;
7269
7270 /* Store the connection attempted bssid for future use */
7271 if (params->bssid)
7272 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
7273 else
7274 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
7275
Hai Shalomc1a21442022-02-04 13:43:00 -08007276 ret = wpa_driver_nl80211_try_connect(drv, params, bss);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007277 if (ret == -EALREADY) {
7278 /*
7279 * cfg80211 does not currently accept new connections if
7280 * we are already connected. As a workaround, force
7281 * disconnection and try again.
7282 */
7283 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7284 "disconnecting before reassociation "
7285 "attempt");
7286 if (wpa_driver_nl80211_disconnect(
Hai Shalomc1a21442022-02-04 13:43:00 -08007287 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, bss))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007288 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007289 ret = wpa_driver_nl80211_try_connect(drv, params, bss);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007290 }
7291 return ret;
7292}
7293
7294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007295static int wpa_driver_nl80211_associate(
7296 void *priv, struct wpa_driver_associate_params *params)
7297{
7298 struct i802_bss *bss = priv;
7299 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007300 struct nl80211_err_info err_info;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007301 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302 struct nl_msg *msg;
7303
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007304 nl80211_unmask_11b_rates(bss);
7305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007306 if (params->mode == IEEE80211_MODE_AP)
7307 return wpa_driver_nl80211_ap(drv, params);
7308
7309 if (params->mode == IEEE80211_MODE_IBSS)
7310 return wpa_driver_nl80211_ibss(drv, params);
7311
7312 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007313 enum nl80211_iftype nlmode = params->p2p ?
7314 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7315
7316 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007317 return -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08007318
Hai Shalomc1a21442022-02-04 13:43:00 -08007319 return wpa_driver_nl80211_connect(drv, params, bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320 }
7321
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007322 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007324 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7325 drv->ifindex);
7326 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007327 if (!msg)
7328 return -1;
7329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007330 ret = nl80211_connect_common(drv, params, msg);
7331 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007332 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007333
Roshan Pius3a1667e2018-07-03 15:17:14 -07007334 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
7335 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
7336 goto fail;
7337
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08007338 if (params->fils_kek) {
7339 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
7340 (unsigned int) params->fils_kek_len);
7341 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
7342 params->fils_kek))
7343 goto fail;
7344 }
7345 if (params->fils_nonces) {
7346 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
7347 params->fils_nonces,
7348 params->fils_nonces_len);
7349 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
7350 params->fils_nonces_len, params->fils_nonces))
7351 goto fail;
7352 }
7353
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007354 if (!TEST_FAIL_TAG("assoc")) {
7355 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
7356 goto fail;
7357 ret = send_and_recv(drv->global, drv->first_bss->nl_connect,
7358 msg, NULL, NULL, NULL, NULL, &err_info);
7359 msg = NULL;
7360 } else {
7361 int i;
7362
7363 /* Error and force TEST_FAIL checking for each link */
7364 ret = -EINVAL;
Sunil Ravi99c035e2024-07-12 01:42:03 +00007365 for_each_link(params->mld_params.valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007366 if (TEST_FAIL_TAG("link"))
7367 err_info.link_id = i;
7368 }
7369 }
7370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007372 wpa_dbg(drv->ctx, MSG_DEBUG,
7373 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7374 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007375 nl80211_dump_scan(drv);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007376
7377 /* Mark failed link within params */
7378 if (err_info.link_id >= 0) {
7379 if (err_info.link_id >= MAX_NUM_MLD_LINKS ||
7380 !(params->mld_params.valid_links &
7381 BIT(err_info.link_id))) {
7382 wpa_printf(MSG_DEBUG,
7383 "nl80211: Invalid errorred link_id %d",
7384 err_info.link_id);
7385 goto fail;
7386 }
7387 params->mld_params.mld_links[err_info.link_id].error =
7388 ret;
7389 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007390 } else {
7391 wpa_printf(MSG_DEBUG,
7392 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007393 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007395fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007396 nlmsg_free(msg);
7397 return ret;
7398}
7399
7400
7401static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007402 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007403{
7404 struct nl_msg *msg;
7405 int ret = -ENOBUFS;
7406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007407 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7408 ifindex, mode, nl80211_iftype_str(mode));
7409
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007410 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
7411 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
7412 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007413
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007414 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007415 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007416 if (!ret)
7417 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007418fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007419 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7421 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7422 return ret;
7423}
7424
7425
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007426static int wpa_driver_nl80211_set_mode_impl(
7427 struct i802_bss *bss,
7428 enum nl80211_iftype nlmode,
7429 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007430{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 struct wpa_driver_nl80211_data *drv = bss->drv;
7432 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007434 int was_ap = is_ap_interface(drv->nlmode);
7435 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007436 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007437
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007438 if (TEST_FAIL())
7439 return -1;
7440
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007441 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7442 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
7443 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007444
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007445 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446 drv->nlmode = nlmode;
7447 ret = 0;
7448 goto done;
7449 }
7450
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007451 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007452 return -1;
7453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007454 if (nlmode == drv->nlmode) {
7455 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7456 "requested mode - ignore error");
7457 ret = 0;
7458 goto done; /* Already in the requested mode */
7459 }
7460
7461 /* mac80211 doesn't allow mode changes while the device is up, so
7462 * take the device down, try to set the mode again, and bring the
7463 * device back up.
7464 */
7465 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7466 "interface down");
7467 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007468 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007469 if (res == -EACCES || res == -ENODEV)
7470 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007471 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007472 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7473 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007474 os_sleep(0, 100000);
7475 continue;
7476 }
7477
7478 /*
7479 * Setting the mode will fail for some drivers if the phy is
7480 * on a frequency that the mode is disallowed in.
7481 */
7482 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007483 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007484 if (res) {
7485 wpa_printf(MSG_DEBUG,
7486 "nl80211: Failed to set frequency on interface");
7487 }
7488 }
7489
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007490 if (i == 0 && was_ap && !is_ap_interface(nlmode) &&
7491 bss->brname[0] &&
7492 (bss->added_if_into_bridge || bss->already_in_bridge)) {
7493 wpa_printf(MSG_DEBUG,
7494 "nl80211: Remove AP interface %s temporarily from the bridge %s to allow its mode to be set to STATION",
7495 bss->ifname, bss->brname);
7496 if (linux_br_del_if(drv->global->ioctl_sock,
7497 bss->brname, bss->ifname) < 0)
7498 wpa_printf(MSG_INFO,
7499 "nl80211: Failed to remove interface %s from bridge %s: %s",
7500 bss->ifname, bss->brname,
7501 strerror(errno));
7502 }
7503
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007504 /* Try to set the mode again while the interface is down */
7505 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7506 if (mode_switch_res == -EBUSY) {
7507 wpa_printf(MSG_DEBUG,
7508 "nl80211: Delaying mode set while interface going down");
7509 os_sleep(0, 100000);
7510 continue;
7511 }
7512 ret = mode_switch_res;
7513 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007514 }
7515
7516 if (!ret) {
7517 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7518 "interface is down");
7519 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007520 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521 }
7522
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007523 /* Bring the interface back up */
7524 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
7525 if (res != 0) {
7526 wpa_printf(MSG_DEBUG,
7527 "nl80211: Failed to set interface up after switching mode");
7528 ret = -1;
7529 }
7530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007532 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007533 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7534 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007535 return ret;
7536 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007538 if (is_p2p_net_interface(nlmode)) {
7539 wpa_printf(MSG_DEBUG,
7540 "nl80211: Interface %s mode change to P2P - disable 11b rates",
7541 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007542 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007543 } else if (drv->disabled_11b_rates) {
7544 wpa_printf(MSG_DEBUG,
7545 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
7546 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007547 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007548 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007550 if (is_ap_interface(nlmode)) {
7551 nl80211_mgmt_unsubscribe(bss, "start AP");
7552 /* Setup additional AP mode functionality if needed */
7553 if (nl80211_setup_ap(bss))
7554 return -1;
7555 } else if (was_ap) {
7556 /* Remove additional AP mode functionality */
7557 nl80211_teardown_ap(bss);
7558 } else {
7559 nl80211_mgmt_unsubscribe(bss, "mode change");
7560 }
7561
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007562 if (is_mesh_interface(nlmode) &&
7563 nl80211_mgmt_subscribe_mesh(bss))
7564 return -1;
7565
Dmitry Shmidt04949592012-07-19 12:16:46 -07007566 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007567 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007568 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7569 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7570 "frame processing - ignore for now");
7571
7572 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007573}
7574
7575
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007576void nl80211_restore_ap_mode(struct i802_bss *bss)
7577{
7578 struct wpa_driver_nl80211_data *drv = bss->drv;
7579 int was_ap = is_ap_interface(drv->nlmode);
leslc3979c32021-03-29 22:34:02 +08007580 int br_ifindex;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007581
7582 wpa_driver_nl80211_set_mode(bss, drv->ap_scan_as_station);
7583 if (!was_ap && is_ap_interface(drv->ap_scan_as_station) &&
7584 bss->brname[0] &&
7585 (bss->added_if_into_bridge || bss->already_in_bridge)) {
7586 wpa_printf(MSG_DEBUG,
7587 "nl80211: Add AP interface %s back into the bridge %s",
7588 bss->ifname, bss->brname);
7589 if (linux_br_add_if(drv->global->ioctl_sock, bss->brname,
7590 bss->ifname) < 0) {
7591 wpa_printf(MSG_WARNING,
7592 "nl80211: Failed to add interface %s into bridge %s: %s",
7593 bss->ifname, bss->brname, strerror(errno));
7594 }
leslc3979c32021-03-29 22:34:02 +08007595 br_ifindex = if_nametoindex(bss->brname);
7596 add_ifidx(drv, br_ifindex, drv->ifindex);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007597 }
7598 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
7599}
7600
7601
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007602int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7603 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007604{
7605 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
7606}
7607
7608
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007609static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
7610 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007611{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007612 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007613 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07007614}
7615
7616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617static int wpa_driver_nl80211_get_capa(void *priv,
7618 struct wpa_driver_capa *capa)
7619{
7620 struct i802_bss *bss = priv;
7621 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07007622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007623 if (!drv->has_capability)
7624 return -1;
7625 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007626 if (drv->extended_capa && drv->extended_capa_mask) {
7627 capa->extended_capa = drv->extended_capa;
7628 capa->extended_capa_mask = drv->extended_capa_mask;
7629 capa->extended_capa_len = drv->extended_capa_len;
7630 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007631
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007632 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633}
7634
7635
7636static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7637{
7638 struct i802_bss *bss = priv;
7639 struct wpa_driver_nl80211_data *drv = bss->drv;
7640
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007641 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
7642 bss->ifname, drv->operstate, state,
7643 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007645 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007646 state ? IF_OPER_UP : IF_OPER_DORMANT);
7647}
7648
7649
7650static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7651{
7652 struct i802_bss *bss = priv;
7653 struct wpa_driver_nl80211_data *drv = bss->drv;
7654 struct nl_msg *msg;
7655 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007656 int ret;
Sunil Ravi89eba102022-09-13 21:04:37 -07007657 const u8 *connected_addr = drv->sta_mlo_info.valid_links ?
7658 drv->sta_mlo_info.ap_mld_addr : drv->bssid;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007659
Sunil Ravi89eba102022-09-13 21:04:37 -07007660 if (!drv->associated && is_zero_ether_addr(connected_addr) &&
7661 !authorized) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007662 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
7663 return 0;
7664 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007665
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007666 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
Sunil Ravi89eba102022-09-13 21:04:37 -07007667 MACSTR, authorized ? "" : "un", MAC2STR(connected_addr));
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007669 os_memset(&upd, 0, sizeof(upd));
7670 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7671 if (authorized)
7672 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007673
7674 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
Sunil Ravi89eba102022-09-13 21:04:37 -07007675 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007676 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
7677 nlmsg_free(msg);
7678 return -ENOBUFS;
7679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007680
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007681 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007682 if (!ret)
7683 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007684 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
7685 ret, strerror(-ret));
7686 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007687}
7688
7689
Jouni Malinen75ecf522011-06-27 15:19:46 -07007690/* Set kernel driver on given frequency (MHz) */
7691static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007692{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007693 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007694 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007695}
7696
7697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007698static inline int min_int(int a, int b)
7699{
7700 if (a < b)
7701 return a;
7702 return b;
7703}
7704
7705
7706static int get_key_handler(struct nl_msg *msg, void *arg)
7707{
7708 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7709 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7710
7711 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7712 genlmsg_attrlen(gnlh, 0), NULL);
7713
7714 /*
7715 * TODO: validate the key index and mac address!
7716 * Otherwise, there's a race condition as soon as
7717 * the kernel starts sending key notifications.
7718 */
7719
7720 if (tb[NL80211_ATTR_KEY_SEQ])
7721 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7722 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
Hai Shalom021b0b52019-04-10 11:17:58 -07007723 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724 return NL_SKIP;
7725}
7726
7727
7728static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007729 int idx, int link_id, u8 *seq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007730{
7731 struct i802_bss *bss = priv;
7732 struct wpa_driver_nl80211_data *drv = bss->drv;
7733 struct nl_msg *msg;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007734 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007735
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007736 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
7737 NL80211_CMD_GET_KEY);
7738 if (!msg ||
7739 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007740 (link_id != NL80211_DRV_LINK_ID_NA &&
7741 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007742 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
7743 nlmsg_free(msg);
7744 return -ENOBUFS;
7745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007746
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007747 os_memset(seq, 0, 6);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007748 res = send_and_recv_resp(drv, msg, get_key_handler, seq);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007749 if (res) {
7750 wpa_printf(MSG_DEBUG,
7751 "nl80211: Failed to get current TX sequence for a key (link_id=%d idx=%d): %d (%s)",
7752 link_id, idx, res, strerror(-res));
7753 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007754
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007755 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007756}
7757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758
7759static int i802_set_rts(void *priv, int rts)
7760{
7761 struct i802_bss *bss = priv;
7762 struct wpa_driver_nl80211_data *drv = bss->drv;
7763 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007764 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007765 u32 val;
7766
Hai Shalom021b0b52019-04-10 11:17:58 -07007767 if (rts >= 2347 || rts == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768 val = (u32) -1;
7769 else
7770 val = rts;
7771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007772 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
7773 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
7774 nlmsg_free(msg);
7775 return -ENOBUFS;
7776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007778 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007779 if (!ret)
7780 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007781 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7782 "%d (%s)", rts, ret, strerror(-ret));
7783 return ret;
7784}
7785
7786
7787static int i802_set_frag(void *priv, int frag)
7788{
7789 struct i802_bss *bss = priv;
7790 struct wpa_driver_nl80211_data *drv = bss->drv;
7791 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007792 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793 u32 val;
7794
Hai Shalom021b0b52019-04-10 11:17:58 -07007795 if (frag >= 2346 || frag == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007796 val = (u32) -1;
7797 else
7798 val = frag;
7799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007800 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
7801 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
7802 nlmsg_free(msg);
7803 return -ENOBUFS;
7804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007805
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007806 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007807 if (!ret)
7808 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007809 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7810 "%d: %d (%s)", frag, ret, strerror(-ret));
7811 return ret;
7812}
7813
7814
7815static int i802_flush(void *priv)
7816{
7817 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007818 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007819 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007820
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007821 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
7822 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007823
7824 /*
7825 * XXX: FIX! this needs to flush all VLANs too
7826 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007827 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007828 res = send_and_recv_cmd(bss->drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007829 if (res) {
7830 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7831 "(%s)", res, strerror(-res));
7832 }
7833 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007834}
7835
7836
Hai Shalom81f62d82019-07-22 12:10:00 -07007837static void get_sta_tid_stats(struct hostap_sta_driver_data *data,
7838 struct nlattr *attr)
7839{
7840 struct nlattr *tid_stats[NL80211_TID_STATS_MAX + 1], *tidattr;
7841 struct nlattr *txq_stats[NL80211_TXQ_STATS_MAX + 1];
7842 static struct nla_policy txq_stats_policy[NL80211_TXQ_STATS_MAX + 1] = {
7843 [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
7844 [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
7845 };
7846 int rem;
7847
7848 nla_for_each_nested(tidattr, attr, rem) {
7849 if (nla_parse_nested(tid_stats, NL80211_TID_STATS_MAX,
7850 tidattr, NULL) != 0 ||
7851 !tid_stats[NL80211_TID_STATS_TXQ_STATS] ||
7852 nla_parse_nested(txq_stats, NL80211_TXQ_STATS_MAX,
7853 tid_stats[NL80211_TID_STATS_TXQ_STATS],
7854 txq_stats_policy) != 0)
7855 continue;
7856 /* sum the backlogs over all TIDs for station */
7857 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES])
7858 data->backlog_bytes += nla_get_u32(
7859 txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES]);
7860 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS])
7861 data->backlog_bytes += nla_get_u32(
7862 txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS]);
7863 }
7864}
7865
7866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007867static int get_sta_handler(struct nl_msg *msg, void *arg)
7868{
7869 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7870 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7871 struct hostap_sta_driver_data *data = arg;
7872 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7873 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7874 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7875 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7876 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007877 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007878 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7879 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007880 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007881 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007882 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
7883 [NL80211_STA_INFO_CONNECTED_TIME] = { .type = NLA_U32 },
7884 [NL80211_STA_INFO_BEACON_LOSS] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007885 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
7886 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007887 [NL80211_STA_INFO_EXPECTED_THROUGHPUT] = { .type = NLA_U32 },
7888 [NL80211_STA_INFO_RX_DROP_MISC] = { .type = NLA_U64 },
7889 [NL80211_STA_INFO_BEACON_RX] = { .type = NLA_U64 },
7890 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8},
Hai Shalom81f62d82019-07-22 12:10:00 -07007891 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007892 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
7893 [NL80211_STA_INFO_ACK_SIGNAL_AVG] = { .type = NLA_S8 },
7894 [NL80211_STA_INFO_RX_MPDUS] = { .type = NLA_U32 },
7895 [NL80211_STA_INFO_FCS_ERROR_COUNT] = { .type = NLA_U32 },
Hai Shalom81f62d82019-07-22 12:10:00 -07007896 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007897 };
7898 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
7899 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
7900 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
7901 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
7902 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
7903 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
7904 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
7905 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00007906 [NL80211_RATE_INFO_HE_MCS] = { .type = NLA_U8 },
7907 [NL80211_RATE_INFO_HE_NSS] = { .type = NLA_U8 },
Sunil Ravi036cec52023-03-29 11:35:17 -07007908 [NL80211_RATE_INFO_HE_GI] = { .type = NLA_U8 },
7909 [NL80211_RATE_INFO_HE_DCM] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007910 };
7911
7912 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7913 genlmsg_attrlen(gnlh, 0), NULL);
7914
7915 /*
7916 * TODO: validate the interface and mac address!
7917 * Otherwise, there's a race condition as soon as
7918 * the kernel starts sending station notifications.
7919 */
7920
7921 if (!tb[NL80211_ATTR_STA_INFO]) {
7922 wpa_printf(MSG_DEBUG, "sta stats missing!");
7923 return NL_SKIP;
7924 }
7925 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7926 tb[NL80211_ATTR_STA_INFO],
7927 stats_policy)) {
7928 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7929 return NL_SKIP;
7930 }
7931
7932 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7933 data->inactive_msec =
7934 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007935 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936 if (stats[NL80211_STA_INFO_RX_BYTES])
7937 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7938 if (stats[NL80211_STA_INFO_TX_BYTES])
7939 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007940 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
7941 stats[NL80211_STA_INFO_TX_BYTES64]) {
7942 /*
7943 * The driver supports 64-bit counters, so use them to override
7944 * the 32-bit values.
7945 */
7946 data->rx_bytes =
7947 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
7948 data->tx_bytes =
7949 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
7950 data->bytes_64bit = 1;
7951 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00007952 if (stats[NL80211_STA_INFO_SIGNAL])
7953 data->signal = (s8) nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007954 if (stats[NL80211_STA_INFO_RX_PACKETS])
7955 data->rx_packets =
7956 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7957 if (stats[NL80211_STA_INFO_TX_PACKETS])
7958 data->tx_packets =
7959 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007960 if (stats[NL80211_STA_INFO_TX_RETRIES])
7961 data->tx_retry_count =
7962 nla_get_u32(stats[NL80211_STA_INFO_TX_RETRIES]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007963 if (stats[NL80211_STA_INFO_TX_FAILED])
7964 data->tx_retry_failed =
7965 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007966 if (stats[NL80211_STA_INFO_SIGNAL_AVG])
7967 data->avg_signal =
7968 (s8) nla_get_u8(stats[NL80211_STA_INFO_SIGNAL_AVG]);
Hai Shalomc1a21442022-02-04 13:43:00 -08007969 if (stats[NL80211_STA_INFO_CONNECTED_TIME]) {
7970 data->connected_sec =
7971 nla_get_u32(stats[NL80211_STA_INFO_CONNECTED_TIME]);
7972 data->flags |= STA_DRV_DATA_CONN_TIME;
7973 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00007974 if (stats[NL80211_STA_INFO_BEACON_LOSS])
7975 data->beacon_loss_count =
7976 nla_get_u32(stats[NL80211_STA_INFO_BEACON_LOSS]);
7977 if (stats[NL80211_STA_INFO_EXPECTED_THROUGHPUT])
7978 data->expected_throughput =
7979 nla_get_u32(stats[NL80211_STA_INFO_EXPECTED_THROUGHPUT]);
7980 if (stats[NL80211_STA_INFO_RX_DROP_MISC])
7981 data->rx_drop_misc =
7982 nla_get_u64(stats[NL80211_STA_INFO_RX_DROP_MISC]);
7983 if (stats[NL80211_STA_INFO_BEACON_RX])
7984 data->beacons_count =
7985 nla_get_u64(stats[NL80211_STA_INFO_BEACON_RX]);
7986 if (stats[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
7987 data->avg_beacon_signal =
7988 (s8) nla_get_u8(stats[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
7989 if (stats[NL80211_STA_INFO_RX_DURATION])
7990 data->rx_airtime =
7991 nla_get_u64(stats[NL80211_STA_INFO_RX_DURATION]);
7992 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
7993 data->last_ack_rssi =
7994 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
7995 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
7996 }
7997 if (stats[NL80211_STA_INFO_ACK_SIGNAL_AVG])
7998 data->avg_ack_signal =
7999 nla_get_s8(stats[NL80211_STA_INFO_ACK_SIGNAL_AVG]);
8000 if (stats[NL80211_STA_INFO_RX_MPDUS])
8001 data->rx_mpdus = nla_get_u32(stats[NL80211_STA_INFO_RX_MPDUS]);
8002 if (stats[NL80211_STA_INFO_FCS_ERROR_COUNT])
8003 data->fcs_error_count =
8004 nla_get_u32(stats[NL80211_STA_INFO_FCS_ERROR_COUNT]);
8005 if (stats[NL80211_STA_INFO_TX_DURATION])
8006 data->tx_airtime =
8007 nla_get_u64(stats[NL80211_STA_INFO_TX_DURATION]);
Hai Shalomc1a21442022-02-04 13:43:00 -08008008
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008009 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
8010 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
8011 stats[NL80211_STA_INFO_TX_BITRATE],
8012 rate_policy) == 0) {
8013 if (rate[NL80211_RATE_INFO_BITRATE32])
8014 data->current_tx_rate =
8015 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
8016 else if (rate[NL80211_RATE_INFO_BITRATE])
8017 data->current_tx_rate =
8018 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
8019
Sunil Ravi77d572f2023-01-17 23:58:31 +00008020 /* Convert from 100 kbps to kbps; it's a more convenient unit.
8021 * It's also safe up until ~1Tbps. */
8022 data->current_tx_rate = data->current_tx_rate * 100;
8023
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008024 if (rate[NL80211_RATE_INFO_MCS]) {
8025 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
8026 data->flags |= STA_DRV_DATA_TX_MCS;
8027 }
8028 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
8029 data->tx_vhtmcs =
8030 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
8031 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
8032 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008033 if (rate[NL80211_RATE_INFO_SHORT_GI]) {
8034 data->tx_guard_interval = GUARD_INTERVAL_0_4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008035 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
Sunil Ravi036cec52023-03-29 11:35:17 -07008036 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008037 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
8038 data->tx_vht_nss =
8039 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
8040 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
8041 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008042 if (rate[NL80211_RATE_INFO_HE_MCS]) {
8043 data->tx_hemcs =
8044 nla_get_u8(rate[NL80211_RATE_INFO_HE_MCS]);
8045 data->flags |= STA_DRV_DATA_TX_HE_MCS;
8046 }
8047 if (rate[NL80211_RATE_INFO_HE_NSS]) {
8048 data->tx_he_nss =
8049 nla_get_u8(rate[NL80211_RATE_INFO_HE_NSS]);
8050 data->flags |= STA_DRV_DATA_TX_HE_NSS;
8051 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008052 if (rate[NL80211_RATE_INFO_HE_GI]) {
8053 switch (nla_get_u8(rate[NL80211_RATE_INFO_HE_GI])) {
8054 case NL80211_RATE_INFO_HE_GI_0_8:
8055 data->tx_guard_interval = GUARD_INTERVAL_0_8;
8056 break;
8057 case NL80211_RATE_INFO_HE_GI_1_6:
8058 data->tx_guard_interval = GUARD_INTERVAL_1_6;
8059 break;
8060 case NL80211_RATE_INFO_HE_GI_3_2:
8061 data->tx_guard_interval = GUARD_INTERVAL_3_2;
8062 break;
8063 }
8064 data->flags |= STA_DRV_DATA_TX_HE_GI;
8065 }
8066 if (rate[NL80211_RATE_INFO_HE_DCM]) {
8067 data->tx_dcm =
8068 nla_get_u8(rate[NL80211_RATE_INFO_HE_DCM]);
8069 data->flags |= STA_DRV_DATA_TX_HE_DCM;
8070 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008071 }
8072
8073 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
8074 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
8075 stats[NL80211_STA_INFO_RX_BITRATE],
8076 rate_policy) == 0) {
8077 if (rate[NL80211_RATE_INFO_BITRATE32])
8078 data->current_rx_rate =
8079 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
8080 else if (rate[NL80211_RATE_INFO_BITRATE])
8081 data->current_rx_rate =
8082 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
8083
Sunil Ravi77d572f2023-01-17 23:58:31 +00008084 /* Convert from 100 kbps to kbps; it's a more convenient unit.
8085 * It's also safe up until ~1Tbps. */
8086 data->current_rx_rate = data->current_rx_rate * 100;
8087
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008088 if (rate[NL80211_RATE_INFO_MCS]) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00008089 data->rx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008090 data->flags |= STA_DRV_DATA_RX_MCS;
8091 }
8092 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
8093 data->rx_vhtmcs =
8094 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
8095 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
8096 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008097 if (rate[NL80211_RATE_INFO_SHORT_GI]) {
8098 data->rx_guard_interval = GUARD_INTERVAL_0_4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008099 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
Sunil Ravi036cec52023-03-29 11:35:17 -07008100 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008101 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
8102 data->rx_vht_nss =
8103 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
8104 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
8105 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00008106 if (rate[NL80211_RATE_INFO_HE_MCS]) {
8107 data->rx_hemcs =
8108 nla_get_u8(rate[NL80211_RATE_INFO_HE_MCS]);
8109 data->flags |= STA_DRV_DATA_RX_HE_MCS;
8110 }
8111 if (rate[NL80211_RATE_INFO_HE_NSS]) {
8112 data->rx_he_nss =
8113 nla_get_u8(rate[NL80211_RATE_INFO_HE_NSS]);
8114 data->flags |= STA_DRV_DATA_RX_HE_NSS;
8115 }
Sunil Ravi036cec52023-03-29 11:35:17 -07008116 if (rate[NL80211_RATE_INFO_HE_GI]) {
8117 switch (nla_get_u8(rate[NL80211_RATE_INFO_HE_GI])) {
8118 case NL80211_RATE_INFO_HE_GI_0_8:
8119 data->rx_guard_interval = GUARD_INTERVAL_0_8;
8120 break;
8121 case NL80211_RATE_INFO_HE_GI_1_6:
8122 data->rx_guard_interval = GUARD_INTERVAL_1_6;
8123 break;
8124 case NL80211_RATE_INFO_HE_GI_3_2:
8125 data->rx_guard_interval = GUARD_INTERVAL_3_2;
8126 break;
8127 }
8128 data->flags |= STA_DRV_DATA_RX_HE_GI;
8129 }
8130 if (rate[NL80211_RATE_INFO_HE_DCM]) {
8131 data->rx_dcm =
8132 nla_get_u8(rate[NL80211_RATE_INFO_HE_DCM]);
8133 data->flags |= STA_DRV_DATA_RX_HE_DCM;
8134 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008135 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008136
Hai Shalom81f62d82019-07-22 12:10:00 -07008137 if (stats[NL80211_STA_INFO_TID_STATS])
8138 get_sta_tid_stats(data, stats[NL80211_STA_INFO_TID_STATS]);
8139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008140 return NL_SKIP;
8141}
8142
Sunil Ravi77d572f2023-01-17 23:58:31 +00008143
8144int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
8145 const u8 *bssid,
8146 struct hostap_sta_driver_data *data)
8147{
8148 struct nl_msg *msg;
8149
8150 data->signal = -WPA_INVALID_NOISE;
8151 data->current_tx_rate = 0;
8152
8153 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
8154 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) {
8155 nlmsg_free(msg);
8156 return -ENOBUFS;
8157 }
8158
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008159 return send_and_recv_resp(drv, msg, get_sta_handler, data);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008160}
8161
8162
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008163static int i802_read_sta_data(struct i802_bss *bss,
8164 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008165 const u8 *addr)
8166{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008167 struct nl_msg *msg;
8168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008169 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
8170 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8171 nlmsg_free(msg);
8172 return -ENOBUFS;
8173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008174
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008175 return send_and_recv_resp(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008176}
8177
8178
8179static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008180 int cw_min, int cw_max, int burst_time,
8181 int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008182{
8183 struct i802_bss *bss = priv;
8184 struct wpa_driver_nl80211_data *drv = bss->drv;
8185 struct nl_msg *msg;
8186 struct nlattr *txq, *params;
Hai Shalom74f70d42019-02-11 14:42:39 -08008187 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008188
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008189 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008190 if (!msg)
8191 return -1;
8192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008193 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8194 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008195 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008196
8197 /* We are only sending parameters for a single TXQ at a time */
8198 params = nla_nest_start(msg, 1);
8199 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008200 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008201
8202 switch (queue) {
8203 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008204 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
8205 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008206 break;
8207 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008208 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
8209 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008210 break;
8211 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008212 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
8213 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008214 break;
8215 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008216 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
8217 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008218 break;
8219 }
8220 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8221 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008222 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
8223 (burst_time * 100 + 16) / 32) ||
8224 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
8225 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
8226 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
8227 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008228
8229 nla_nest_end(msg, params);
8230
8231 nla_nest_end(msg, txq);
8232
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008233 if (link_id != NL80211_DRV_LINK_ID_NA &&
8234 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
8235 goto fail;
8236
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008237 res = send_and_recv_cmd(drv, msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08008238 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008239 "nl80211: link=%d: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
8240 link_id, queue, aifs, cw_min, cw_max, burst_time, res);
Hai Shalom74f70d42019-02-11 14:42:39 -08008241 if (res == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008242 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008243 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008244fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008245 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008246 return -1;
8247}
8248
8249
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008250static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008251 const char *ifname, int vlan_id, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008252{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008253 struct wpa_driver_nl80211_data *drv = bss->drv;
8254 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008255 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008256
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008257 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
8258 ", ifname=%s[%d], vlan_id=%d)",
8259 bss->ifname, if_nametoindex(bss->ifname),
8260 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008261 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
8262 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07008263 (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD) &&
Hai Shalomfdcde762020-04-02 11:19:20 -07008264 nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id)) ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008265 (link_id != NL80211_DRV_LINK_ID_NA &&
8266 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008267 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
8268 nlmsg_free(msg);
8269 return -ENOBUFS;
8270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008271
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008272 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008273 if (ret < 0) {
8274 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8275 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8276 MAC2STR(addr), ifname, vlan_id, ret,
8277 strerror(-ret));
8278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008279 return ret;
8280}
8281
8282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008283static int i802_get_inact_sec(void *priv, const u8 *addr)
8284{
8285 struct hostap_sta_driver_data data;
8286 int ret;
8287
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008288 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008289 data.inactive_msec = (unsigned long) -1;
8290 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008291 if (ret == -ENOENT)
8292 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008293 if (ret || data.inactive_msec == (unsigned long) -1)
8294 return -1;
8295 return data.inactive_msec / 1000;
8296}
8297
8298
8299static int i802_sta_clear_stats(void *priv, const u8 *addr)
8300{
8301#if 0
8302 /* TODO */
8303#endif
8304 return 0;
8305}
8306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008307
8308static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008309 u16 reason, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008310{
8311 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008312 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008313 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08008314 u8 channel;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008315 struct i802_link *link = nl80211_get_link(bss, link_id);
Dmitry Shmidt29333592017-01-09 12:27:11 -08008316
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008317 if (ieee80211_freq_to_chan(link->freq, &channel) ==
Dmitry Shmidt29333592017-01-09 12:27:11 -08008318 HOSTAPD_MODE_IEEE80211AD) {
8319 /* Deauthentication is not used in DMG/IEEE 802.11ad;
8320 * disassociate the STA instead. */
8321 return i802_sta_disassoc(priv, own_addr, addr, reason);
8322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008323
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008324 if (is_mesh_interface(drv->nlmode))
8325 return -1;
8326
Dmitry Shmidt04949592012-07-19 12:16:46 -07008327 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008328 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008330 memset(&mgmt, 0, sizeof(mgmt));
8331 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8332 WLAN_FC_STYPE_DEAUTH);
8333 memcpy(mgmt.da, addr, ETH_ALEN);
8334 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8335 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8336 mgmt.u.deauth.reason_code = host_to_le16(reason);
8337 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8338 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008339 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008340 0, NULL, 0, 0, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008341}
8342
8343
8344static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07008345 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008346{
8347 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008348 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008349 struct ieee80211_mgmt mgmt;
8350
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008351 if (is_mesh_interface(drv->nlmode))
8352 return -1;
8353
Dmitry Shmidt04949592012-07-19 12:16:46 -07008354 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008355 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008357 memset(&mgmt, 0, sizeof(mgmt));
8358 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8359 WLAN_FC_STYPE_DISASSOC);
8360 memcpy(mgmt.da, addr, ETH_ALEN);
8361 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8362 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8363 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8364 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8365 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008366 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008367 0, NULL, 0, 0, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008368}
8369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008370
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008371static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
8372{
8373 char buf[200], *pos, *end;
8374 int i, res;
8375
8376 pos = buf;
8377 end = pos + sizeof(buf);
8378
8379 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008380 if (!drv->if_indices[i].ifindex)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008381 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008382 res = os_snprintf(pos, end - pos, " %d(%d)",
Hai Shalom81f62d82019-07-22 12:10:00 -07008383 drv->if_indices[i].ifindex,
8384 drv->if_indices[i].reason);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008385 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008386 break;
8387 pos += res;
8388 }
8389 *pos = '\0';
8390
8391 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
8392 drv->num_if_indices, buf);
8393}
8394
8395
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008396static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8397 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008398{
8399 int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07008400 struct drv_nl80211_if_info *old;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008401
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008402 wpa_printf(MSG_DEBUG,
8403 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
8404 ifidx, ifidx_reason);
8405 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008406 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
8407 ifidx);
8408 return;
8409 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008410 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008411 if (drv->if_indices[i].ifindex == 0) {
8412 drv->if_indices[i].ifindex = ifidx;
8413 drv->if_indices[i].reason = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008414 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008415 return;
8416 }
8417 }
8418
8419 if (drv->if_indices != drv->default_if_indices)
8420 old = drv->if_indices;
8421 else
8422 old = NULL;
8423
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008424 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
Hai Shalom81f62d82019-07-22 12:10:00 -07008425 sizeof(*old));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008426 if (!drv->if_indices) {
8427 if (!old)
8428 drv->if_indices = drv->default_if_indices;
8429 else
8430 drv->if_indices = old;
8431 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8432 "interfaces");
8433 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8434 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008435 }
8436 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008437 os_memcpy(drv->if_indices, drv->default_if_indices,
8438 sizeof(drv->default_if_indices));
Hai Shalom81f62d82019-07-22 12:10:00 -07008439 drv->if_indices[drv->num_if_indices].ifindex = ifidx;
8440 drv->if_indices[drv->num_if_indices].reason = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008441 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008442 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008443}
8444
8445
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008446static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8447 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008448{
8449 int i;
8450
8451 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008452 if ((drv->if_indices[i].ifindex == ifidx ||
8453 ifidx == IFIDX_ANY) &&
8454 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008455 ifidx_reason == IFIDX_ANY)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07008456 drv->if_indices[i].ifindex = 0;
8457 drv->if_indices[i].reason = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008458 break;
8459 }
8460 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008461 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008462}
8463
8464
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008465static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
8466 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008467{
8468 int i;
8469
8470 for (i = 0; i < drv->num_if_indices; i++)
Hai Shalom81f62d82019-07-22 12:10:00 -07008471 if (drv->if_indices[i].ifindex == ifidx &&
8472 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008473 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07008474 return 1;
8475
8476 return 0;
8477}
8478
8479
8480static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008481 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008482{
8483 struct i802_bss *bss = priv;
8484 struct wpa_driver_nl80211_data *drv = bss->drv;
8485 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07008486 union wpa_event_data event;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08008487 int ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008488
Hai Shalom39ba6fc2019-01-22 12:40:38 -08008489 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8490 if (ret >= (int) sizeof(name))
8491 wpa_printf(MSG_WARNING,
8492 "nl80211: WDS interface name was truncated");
8493 else if (ret < 0)
8494 return ret;
8495
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008496 if (ifname_wds)
8497 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
8498
Jouni Malinen75ecf522011-06-27 15:19:46 -07008499 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8500 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8501 if (val) {
8502 if (!if_nametoindex(name)) {
8503 if (nl80211_create_iface(drv, name,
8504 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008505 bss->addr, 1, NULL, NULL, 0) <
8506 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008507 return -1;
8508 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008509 linux_br_add_if(drv->global->ioctl_sock,
8510 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008511 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008512
8513 os_memset(&event, 0, sizeof(event));
8514 event.wds_sta_interface.sta_addr = addr;
8515 event.wds_sta_interface.ifname = name;
8516 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07008517 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07008518 EVENT_WDS_STA_INTERFACE_STATUS,
8519 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008520 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008521 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8522 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8523 "interface %s up", name);
8524 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008525 return i802_set_sta_vlan(priv, addr, name, 0,
8526 NL80211_DRV_LINK_ID_NA);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008527 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08008528 if (bridge_ifname &&
8529 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8530 name) < 0)
8531 wpa_printf(MSG_INFO,
8532 "nl80211: Failed to remove interface %s from bridge %s: %s",
8533 name, bridge_ifname, strerror(errno));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008534
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008535 i802_set_sta_vlan(priv, addr, bss->ifname, 0,
8536 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008537 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07008538 os_memset(&event, 0, sizeof(event));
8539 event.wds_sta_interface.sta_addr = addr;
8540 event.wds_sta_interface.ifname = name;
8541 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07008542 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07008543 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008544 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008545 }
8546}
8547
8548
8549static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8550{
8551 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8552 struct sockaddr_ll lladdr;
8553 unsigned char buf[3000];
8554 int len;
8555 socklen_t fromlen = sizeof(lladdr);
8556
8557 len = recvfrom(sock, buf, sizeof(buf), 0,
8558 (struct sockaddr *)&lladdr, &fromlen);
8559 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008560 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
8561 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008562 return;
8563 }
8564
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008565 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07008566 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8567}
8568
8569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008570static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8571 struct i802_bss *bss,
8572 const char *brname, const char *ifname)
8573{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008574 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008575 char in_br[IFNAMSIZ];
8576
8577 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008578 br_ifindex = if_nametoindex(brname);
8579 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008580 /*
8581 * Bridge was configured, but the bridge device does
8582 * not exist. Try to add it now.
8583 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008584 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008585 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8586 "bridge interface %s: %s",
8587 brname, strerror(errno));
8588 return -1;
8589 }
8590 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008591 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008592 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008593 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008594 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008595
8596 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07008597 if (os_strcmp(in_br, brname) == 0) {
8598 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008599 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07008600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008601
8602 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8603 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008604 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8605 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008606 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8607 "remove interface %s from bridge "
8608 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008609 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008610 return -1;
8611 }
8612 }
8613
8614 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8615 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008616 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008617 wpa_printf(MSG_WARNING,
8618 "nl80211: Failed to add interface %s into bridge %s: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008619 ifname, brname, strerror(errno));
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008620 /* Try to continue without the interface being in a bridge. This
8621 * may be needed for some cases, e.g., with Open vSwitch, where
8622 * an external component will need to handle bridge
8623 * configuration. */
8624 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008625 }
8626 bss->added_if_into_bridge = 1;
8627
8628 return 0;
8629}
8630
8631
8632static void *i802_init(struct hostapd_data *hapd,
8633 struct wpa_init_params *params)
8634{
8635 struct wpa_driver_nl80211_data *drv;
8636 struct i802_bss *bss;
8637 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008638 char master_ifname[IFNAMSIZ];
8639 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008640 int br_added = 0;
8641
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008642 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
8643 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008644 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008645 if (bss == NULL)
8646 return NULL;
8647
8648 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008649
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008650 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008651 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008652 params->ifname, master_ifname);
8653 br_ifindex = if_nametoindex(master_ifname);
8654 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
8655 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
8656 linux_master_get(master_ifname, params->ifname) == 0) {
8657 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
8658 params->ifname, master_ifname);
8659 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008660 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08008661
8662 /* check if master itself is under bridge */
8663 if (linux_br_get(master_ifname, master_ifname) == 0) {
8664 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
8665 master_ifname);
8666 br_ifindex = if_nametoindex(master_ifname);
8667 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
8668 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008669 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008670 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008671 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008672
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008673 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675 for (i = 0; i < params->num_bridge; i++) {
8676 if (params->bridge[i]) {
8677 ifindex = if_nametoindex(params->bridge[i]);
8678 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008679 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008680 if (ifindex == br_ifindex)
8681 br_added = 1;
8682 }
8683 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008684
8685 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008686 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008687
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008688 if (params->num_bridge && params->bridge[0]) {
8689 if (i802_check_bridge(drv, bss, params->bridge[0],
8690 params->ifname) < 0)
8691 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008692 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008693 br_added = 1;
8694 }
8695
8696 if (!br_added && br_ifindex &&
8697 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008698 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008699
Hai Shalomc9e41a12018-07-31 14:41:42 -07008700 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Hai Shalomfdcde762020-04-02 11:19:20 -07008701 int err;
8702
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008703 drv->rtnl_sk = nl_socket_alloc();
8704 if (drv->rtnl_sk == NULL) {
8705 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
8706 goto failed;
8707 }
8708
Hai Shalomfdcde762020-04-02 11:19:20 -07008709 err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
8710 if (err) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008711 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07008712 nl_geterror(err));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008713 goto failed;
8714 }
8715 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008716
Hai Shalomb755a2a2020-04-23 21:49:02 -07008717 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) {
8718 wpa_printf(MSG_DEBUG,
8719 "nl80211: Do not open EAPOL RX socket - using control port for RX");
8720 goto skip_eapol_sock;
8721 }
8722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008723 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8724 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008725 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
8726 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008727 goto failed;
8728 }
8729
8730 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8731 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008732 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008733 goto failed;
8734 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07008735skip_eapol_sock:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008737 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8738 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008739 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008740 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008742 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008744 return bss;
8745
8746failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008747 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008748 return NULL;
8749}
8750
8751
8752static void i802_deinit(void *priv)
8753{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008754 struct i802_bss *bss = priv;
8755 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008756}
8757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008758
8759static enum nl80211_iftype wpa_driver_nl80211_if_type(
8760 enum wpa_driver_if_type type)
8761{
8762 switch (type) {
8763 case WPA_IF_STATION:
8764 return NL80211_IFTYPE_STATION;
8765 case WPA_IF_P2P_CLIENT:
8766 case WPA_IF_P2P_GROUP:
8767 return NL80211_IFTYPE_P2P_CLIENT;
8768 case WPA_IF_AP_VLAN:
8769 return NL80211_IFTYPE_AP_VLAN;
8770 case WPA_IF_AP_BSS:
8771 return NL80211_IFTYPE_AP;
8772 case WPA_IF_P2P_GO:
8773 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008774 case WPA_IF_P2P_DEVICE:
8775 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008776 case WPA_IF_MESH:
8777 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008778 default:
8779 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008780 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008781}
8782
8783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008784static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8785{
8786 struct wpa_driver_nl80211_data *drv;
8787 dl_list_for_each(drv, &global->interfaces,
8788 struct wpa_driver_nl80211_data, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008789 if (ether_addr_equal(addr, drv->first_bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008790 return 1;
8791 }
8792 return 0;
8793}
8794
8795
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008796static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008797{
8798 unsigned int idx;
8799
8800 if (!drv->global)
8801 return -1;
8802
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008803 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008804 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008805 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008806 new_addr[0] ^= idx << 2;
8807 if (!nl80211_addr_in_use(drv->global, new_addr))
8808 break;
8809 }
8810 if (idx == 64)
8811 return -1;
8812
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008813 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008814 MACSTR, MAC2STR(new_addr));
8815
8816 return 0;
8817}
8818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008819
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008820struct wdev_info {
8821 u64 wdev_id;
8822 int wdev_id_set;
8823 u8 macaddr[ETH_ALEN];
8824};
8825
8826static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
8827{
8828 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8829 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8830 struct wdev_info *wi = arg;
8831
8832 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8833 genlmsg_attrlen(gnlh, 0), NULL);
8834 if (tb[NL80211_ATTR_WDEV]) {
8835 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
8836 wi->wdev_id_set = 1;
8837 }
8838
8839 if (tb[NL80211_ATTR_MAC])
8840 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
8841 ETH_ALEN);
8842
8843 return NL_SKIP;
8844}
8845
8846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008847static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8848 const char *ifname, const u8 *addr,
8849 void *bss_ctx, void **drv_priv,
8850 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008851 const char *bridge, int use_existing,
8852 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008853{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008854 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008855 struct i802_bss *bss = priv;
8856 struct wpa_driver_nl80211_data *drv = bss->drv;
8857 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008858 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008859
8860 if (addr)
8861 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008862 nlmode = wpa_driver_nl80211_if_type(type);
8863 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
8864 struct wdev_info p2pdev_info;
8865
8866 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
8867 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
8868 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008869 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008870 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
8871 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
8872 ifname);
8873 return -1;
8874 }
8875
8876 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
8877 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
Mir Ali8a8f1002020-10-06 22:41:40 +05308878 if (!is_zero_ether_addr(p2pdev_info.macaddr)) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008879 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
Mir Ali8a8f1002020-10-06 22:41:40 +05308880 os_memcpy(drv->global->p2p_perm_addr, p2pdev_info.macaddr, ETH_ALEN);
8881 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008882 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
8883 ifname,
8884 (long long unsigned int) p2pdev_info.wdev_id);
8885 } else {
8886 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008887 0, NULL, NULL, use_existing);
8888 if (use_existing && ifidx == -ENFILE) {
8889 added = 0;
8890 ifidx = if_nametoindex(ifname);
8891 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008892 return -1;
8893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008894 }
8895
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008896 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008897 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008898 os_memcpy(if_addr, bss->addr, ETH_ALEN);
8899 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008900 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008901 if (added)
8902 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008903 return -1;
8904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008905 }
8906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008907 if (!addr &&
8908 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008909 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00008910 type == WPA_IF_STATION || type == WPA_IF_AP_BSS)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008911 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008912 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008913
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008914 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008915 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008916 if (added)
8917 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008918 return -1;
8919 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008920 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008921 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07008922 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008923 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008924 if (added)
8925 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008926 return -1;
8927 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008928 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008929 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008930 if (added)
8931 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008932 return -1;
8933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008934 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008935 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008937
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008938 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008939 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
Sunil Ravi036cec52023-03-29 11:35:17 -07008940
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008941 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008942 if (added)
8943 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008944 return -1;
8945 }
8946
8947 if (bridge &&
8948 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8949 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8950 "interface %s to a bridge %s",
8951 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008952 if (added)
8953 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008954 os_free(new_bss);
8955 return -1;
8956 }
8957
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008958 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8959 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07008960 if (added)
8961 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008962 os_free(new_bss);
8963 return -1;
8964 }
8965 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008966 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008967 new_bss->ifindex = ifidx;
8968 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008969 new_bss->next = drv->first_bss->next;
Sunil Ravi036cec52023-03-29 11:35:17 -07008970 new_bss->flink = &new_bss->links[0];
Sunil Ravi99c035e2024-07-12 01:42:03 +00008971 new_bss->valid_links = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -07008972 os_memcpy(new_bss->flink->addr, new_bss->addr, ETH_ALEN);
8973
8974 new_bss->flink->freq = drv->first_bss->flink->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008975 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008976 new_bss->added_if = added;
8977 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008978 if (drv_priv)
8979 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008980 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008981
8982 /* Subscribe management frames for this WPA_IF_AP_BSS */
8983 if (nl80211_setup_ap(new_bss))
8984 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008985 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008987 if (drv->global)
8988 drv->global->if_add_ifindex = ifidx;
8989
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008990 /*
8991 * Some virtual interfaces need to process EAPOL packets and events on
8992 * the parent interface. This is used mainly with hostapd.
8993 */
8994 if (ifidx > 0 &&
8995 (drv->hostapd ||
8996 nlmode == NL80211_IFTYPE_AP_VLAN ||
8997 nlmode == NL80211_IFTYPE_WDS ||
8998 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008999 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009001 return 0;
9002}
9003
9004
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009005static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009006 enum wpa_driver_if_type type,
9007 const char *ifname)
9008{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009009 struct wpa_driver_nl80211_data *drv = bss->drv;
9010 int ifindex = if_nametoindex(ifname);
9011
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009012 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9013 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009014 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07009015 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07009016 else if (ifindex > 0 && !bss->added_if) {
9017 struct wpa_driver_nl80211_data *drv2;
9018 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009019 struct wpa_driver_nl80211_data, list) {
9020 del_ifidx(drv2, ifindex, IFIDX_ANY);
9021 del_ifidx(drv2, IFIDX_ANY, ifindex);
9022 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07009023 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009024
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009025 if (type != WPA_IF_AP_BSS)
9026 return 0;
9027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009028 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009029 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9030 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009031 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9032 "interface %s from bridge %s: %s",
9033 bss->ifname, bss->brname, strerror(errno));
9034 }
9035 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009036 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009037 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9038 "bridge %s: %s",
9039 bss->brname, strerror(errno));
9040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009041
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009042 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009043 struct i802_bss *tbss;
9044
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009045 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
9046 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009047 if (tbss->next == bss) {
9048 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009049 /* Unsubscribe management frames */
9050 nl80211_teardown_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009051 nl80211_remove_links(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009052 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009053 if (!bss->added_if)
9054 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009055 os_free(bss);
9056 bss = NULL;
9057 break;
9058 }
9059 }
9060 if (bss)
9061 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9062 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009063 } else {
9064 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
9065 nl80211_teardown_ap(bss);
Sunil Ravi99c035e2024-07-12 01:42:03 +00009066 nl80211_remove_links(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009067 if (!bss->added_if && !drv->first_bss->next)
Sunil Ravi036cec52023-03-29 11:35:17 -07009068 wpa_driver_nl80211_del_beacon_all(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009069 nl80211_destroy_bss(bss);
9070 if (!bss->added_if)
9071 i802_set_iface_flags(bss, 0);
9072 if (drv->first_bss->next) {
9073 drv->first_bss = drv->first_bss->next;
9074 drv->ctx = drv->first_bss->ctx;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009075 drv->ifindex = drv->first_bss->ifindex;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009076 os_free(bss);
9077 } else {
9078 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009080 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009081
9082 return 0;
9083}
9084
9085
9086static int cookie_handler(struct nl_msg *msg, void *arg)
9087{
9088 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9089 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9090 u64 *cookie = arg;
9091 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9092 genlmsg_attrlen(gnlh, 0), NULL);
9093 if (tb[NL80211_ATTR_COOKIE])
9094 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9095 return NL_SKIP;
9096}
9097
9098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009099static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009100 unsigned int freq, unsigned int wait,
9101 const u8 *buf, size_t buf_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07009102 int save_cookie, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009103 int offchanok, const u16 *csa_offs,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009104 size_t csa_offs_len, int link_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009105{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009106 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009107 struct nl_msg *msg;
9108 u64 cookie;
9109 int ret = -1;
9110
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009111 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07009112 "no_ack=%d offchanok=%d",
9113 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009114 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009115
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009116 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009117 ((link_id != NL80211_DRV_LINK_ID_NA) &&
9118 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009119 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9120 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
9121 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9122 drv->test_use_roc_tx) &&
9123 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
9124 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
9125 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009126 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
9127 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009128 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
9129 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009130
9131 cookie = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009132 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009133 msg = NULL;
9134 if (ret) {
9135 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009136 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9137 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009138 } else {
9139 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
9140 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9141 (long long unsigned int) cookie);
9142
Hai Shalomfdcde762020-04-02 11:19:20 -07009143 if (save_cookie)
9144 drv->send_frame_cookie = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009145
Sunil Ravia04bd252022-05-02 22:54:18 -07009146 if (!wait) {
9147 /* There is no need to store this cookie since there
9148 * is no wait that could be canceled later. */
9149 goto fail;
9150 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009151 if (drv->num_send_frame_cookies == MAX_SEND_FRAME_COOKIES) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009152 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07009153 "nl80211: Drop oldest pending send frame cookie 0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009154 (long long unsigned int)
Hai Shalomfdcde762020-04-02 11:19:20 -07009155 drv->send_frame_cookies[0]);
9156 os_memmove(&drv->send_frame_cookies[0],
9157 &drv->send_frame_cookies[1],
9158 (MAX_SEND_FRAME_COOKIES - 1) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009159 sizeof(u64));
Hai Shalomfdcde762020-04-02 11:19:20 -07009160 drv->num_send_frame_cookies--;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009161 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009162 drv->send_frame_cookies[drv->num_send_frame_cookies] = cookie;
9163 drv->num_send_frame_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009165
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009166fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009167 nlmsg_free(msg);
9168 return ret;
9169}
9170
9171
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009172static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9173 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009174 unsigned int wait_time,
9175 const u8 *dst, const u8 *src,
9176 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009177 const u8 *data, size_t data_len,
9178 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009179{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009180 struct wpa_driver_nl80211_data *drv = bss->drv;
9181 int ret = -1;
9182 u8 *buf;
9183 struct ieee80211_hdr *hdr;
Hai Shalomfdcde762020-04-02 11:19:20 -07009184 int offchanok = 1;
9185
Sunil Ravi036cec52023-03-29 11:35:17 -07009186 if (is_ap_interface(drv->nlmode) && (int) freq == bss->flink->freq &&
9187 bss->flink->beacon_set)
Hai Shalomfdcde762020-04-02 11:19:20 -07009188 offchanok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009189
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009190 if (!freq && is_sta_interface(drv->nlmode))
9191 offchanok = 0;
9192
9193 wpa_printf(MSG_DEBUG,
9194 "nl80211: Send Action frame (ifindex=%d, freq=%u MHz wait=%d ms no_cck=%d offchanok=%d dst="
9195 MACSTR " src=" MACSTR " bssid=" MACSTR ")",
9196 drv->ifindex, freq, wait_time, no_cck, offchanok,
9197 MAC2STR(dst), MAC2STR(src), MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009198
9199 buf = os_zalloc(24 + data_len);
9200 if (buf == NULL)
9201 return ret;
9202 os_memcpy(buf + 24, data, data_len);
9203 hdr = (struct ieee80211_hdr *) buf;
9204 hdr->frame_control =
9205 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9206 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9207 os_memcpy(hdr->addr2, src, ETH_ALEN);
9208 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9209
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009210 if (!ether_addr_equal(bss->addr, src)) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08009211 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
9212 MAC2STR(src));
9213 os_memcpy(bss->rand_addr, src, ETH_ALEN);
9214 } else {
9215 os_memset(bss->rand_addr, 0, ETH_ALEN);
9216 }
9217
Hai Shalom60840252021-02-19 19:02:11 -08009218#ifdef CONFIG_MESH
9219 if (is_mesh_interface(drv->nlmode)) {
9220 struct hostapd_hw_modes *modes;
9221 u16 num_modes, flags;
9222 u8 dfs_domain;
9223 int i;
9224
9225 modes = nl80211_get_hw_feature_data(bss, &num_modes,
9226 &flags, &dfs_domain);
9227 if (dfs_domain != HOSTAPD_DFS_REGION_ETSI &&
Sunil Ravi036cec52023-03-29 11:35:17 -07009228 ieee80211_is_dfs(bss->flink->freq, modes, num_modes))
Hai Shalom60840252021-02-19 19:02:11 -08009229 offchanok = 0;
9230 if (modes) {
9231 for (i = 0; i < num_modes; i++) {
9232 os_free(modes[i].channels);
9233 os_free(modes[i].rates);
9234 }
9235 os_free(modes);
9236 }
9237 }
9238#endif /* CONFIG_MESH */
9239
Dmitry Shmidt56052862013-10-04 10:23:25 -07009240 if (is_ap_interface(drv->nlmode) &&
9241 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
Sunil Ravi036cec52023-03-29 11:35:17 -07009242 (int) freq == bss->flink->freq || drv->device_ap_sme ||
Dmitry Shmidt56052862013-10-04 10:23:25 -07009243 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009244 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07009245 0, freq, no_cck, offchanok,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00009246 wait_time, NULL, 0, 0, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009247 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009248 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009249 24 + data_len, 1, no_cck, 0,
9250 offchanok, NULL, 0,
9251 NL80211_DRV_LINK_ID_NA);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009252
9253 os_free(buf);
9254 return ret;
9255}
9256
9257
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009258static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009259{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009260 struct wpa_driver_nl80211_data *drv = bss->drv;
9261 struct nl_msg *msg;
9262 int ret;
9263
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08009264 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009265 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009266 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009267 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009268 nlmsg_free(msg);
9269 return;
9270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009271
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009272 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009273 if (ret)
9274 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9275 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009276}
9277
9278
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009279static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9280{
9281 struct i802_bss *bss = priv;
9282 struct wpa_driver_nl80211_data *drv = bss->drv;
9283 unsigned int i;
9284 u64 cookie;
9285
9286 /* Cancel the last pending TX cookie */
Sunil Ravia04bd252022-05-02 22:54:18 -07009287 if (drv->send_frame_cookie != (u64) -1)
9288 nl80211_frame_wait_cancel(bss, drv->send_frame_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009289
9290 /*
9291 * Cancel the other pending TX cookies, if any. This is needed since
9292 * the driver may keep a list of all pending offchannel TX operations
9293 * and free up the radio only once they have expired or cancelled.
9294 */
Hai Shalomfdcde762020-04-02 11:19:20 -07009295 for (i = drv->num_send_frame_cookies; i > 0; i--) {
9296 cookie = drv->send_frame_cookies[i - 1];
9297 if (cookie != drv->send_frame_cookie)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009298 nl80211_frame_wait_cancel(bss, cookie);
9299 }
Hai Shalomfdcde762020-04-02 11:19:20 -07009300 drv->num_send_frame_cookies = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009301}
9302
9303
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009304static int nl80211_put_any_link_id(struct nl_msg *msg,
9305 struct driver_sta_mlo_info *mlo,
9306 int freq)
9307{
9308 int i;
9309 int link_id = -1;
9310 int any_valid_link_id = -1;
9311
9312 if (!mlo->valid_links)
9313 return 0;
9314
9315 /* First try to pick a link that uses the same band */
Sunil Ravi99c035e2024-07-12 01:42:03 +00009316 for_each_link(mlo->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009317 if (any_valid_link_id == -1)
9318 any_valid_link_id = i;
9319
9320 if (is_same_band(freq, mlo->links[i].freq)) {
9321 link_id = i;
9322 break;
9323 }
9324 }
9325
9326 /* Use any valid link ID if no band match was found */
9327 if (link_id == -1)
9328 link_id = any_valid_link_id;
9329
9330 if (link_id == -1) {
9331 wpa_printf(MSG_INFO,
9332 "nl80211: No valid Link ID found for freq %u", freq);
9333 return 0;
9334 }
9335
9336 wpa_printf(MSG_DEBUG, "nl80211: Add Link ID %d", link_id);
9337 return nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id);
9338}
9339
9340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009341static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9342 unsigned int duration)
9343{
9344 struct i802_bss *bss = priv;
9345 struct wpa_driver_nl80211_data *drv = bss->drv;
9346 struct nl_msg *msg;
9347 int ret;
9348 u64 cookie;
9349
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009350 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
9351 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009352 nla_put_u32(msg, NL80211_ATTR_DURATION, duration) ||
9353 nl80211_put_any_link_id(msg, &drv->sta_mlo_info, freq)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009354 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009355 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009356 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009357
9358 cookie = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009359 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009360 if (ret == 0) {
9361 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9362 "0x%llx for freq=%u MHz duration=%u",
9363 (long long unsigned int) cookie, freq, duration);
9364 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009365 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009366 return 0;
9367 }
9368 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9369 "(freq=%d duration=%u): %d (%s)",
9370 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009371 return -1;
9372}
9373
9374
9375static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9376{
9377 struct i802_bss *bss = priv;
9378 struct wpa_driver_nl80211_data *drv = bss->drv;
9379 struct nl_msg *msg;
9380 int ret;
9381
9382 if (!drv->pending_remain_on_chan) {
9383 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9384 "to cancel");
9385 return -1;
9386 }
9387
9388 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9389 "0x%llx",
9390 (long long unsigned int) drv->remain_on_chan_cookie);
9391
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009392 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
9393 if (!msg ||
9394 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
9395 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009396 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009397 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009398
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009399 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009400 if (ret == 0)
9401 return 0;
9402 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9403 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009404 return -1;
9405}
9406
9407
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009408static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009409{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009410 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009412 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009413 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07009414 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
9415 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009416 /*
9417 * Do not disable Probe Request reporting that was
9418 * enabled in nl80211_setup_ap().
9419 */
9420 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9421 "Probe Request reporting nl_preq=%p while "
9422 "in AP mode", bss->nl_preq);
9423 } else if (bss->nl_preq) {
9424 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9425 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07009426 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009427 }
9428 return 0;
9429 }
9430
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009431 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009432 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009433 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009434 return 0;
9435 }
9436
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009437 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9438 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009439 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009440 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9441 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009443 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009444 (WLAN_FC_TYPE_MGMT << 2) |
9445 (WLAN_FC_STYPE_PROBE_REQ << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07009446 NULL, 0, false) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009447 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07009448
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009449 nl80211_register_eloop_read(&bss->nl_preq,
9450 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07009451 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009452
9453 return 0;
9454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009455 out_err:
9456 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009457 return -1;
9458}
9459
9460
9461static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9462 int ifindex, int disabled)
9463{
9464 struct nl_msg *msg;
9465 struct nlattr *bands, *band;
9466 int ret;
9467
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009468 wpa_printf(MSG_DEBUG,
9469 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
9470 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
9471 "no NL80211_TXRATE_LEGACY constraint");
9472
9473 msg = nl80211_ifindex_msg(drv, ifindex, 0,
9474 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009475 if (!msg)
9476 return -1;
9477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009478 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9479 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009480 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009481
9482 /*
9483 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9484 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9485 * rates. All 5 GHz rates are left enabled.
9486 */
9487 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009488 if (!band ||
9489 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
9490 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
9491 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009492 nla_nest_end(msg, band);
9493
9494 nla_nest_end(msg, bands);
9495
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009496 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009497 if (ret) {
9498 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9499 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009500 } else
9501 drv->disabled_11b_rates = disabled;
9502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009503 return ret;
9504
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009505fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009506 nlmsg_free(msg);
9507 return -1;
9508}
9509
9510
Sunil Ravi99c035e2024-07-12 01:42:03 +00009511static int nl80211_remove_link(struct i802_bss *bss, int link_id)
Sunil Raviaf399a82024-05-05 20:56:55 +00009512{
Sunil Ravi88611412024-06-28 17:34:56 +00009513 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009514 struct i802_link *link;
Sunil Ravi88611412024-06-28 17:34:56 +00009515 struct nl_msg *msg;
Sunil Ravi99c035e2024-07-12 01:42:03 +00009516 size_t i;
9517 int ret;
9518
9519 wpa_printf(MSG_DEBUG, "nl80211: Remove link (ifindex=%d link_id=%u)",
9520 bss->ifindex, link_id);
9521
9522 if (!(bss->valid_links & BIT(link_id))) {
9523 wpa_printf(MSG_DEBUG,
9524 "nl80211: MLD: remove link: Link not found");
9525 return -1;
9526 }
9527
9528 link = &bss->links[link_id];
9529
9530 wpa_driver_nl80211_del_beacon(bss, link_id);
9531
9532 /* First remove the link locally */
9533 bss->valid_links &= ~BIT(link_id);
9534 os_memset(link->addr, 0, ETH_ALEN);
9535
9536 /* Choose new deflink if we are removing that link */
9537 if (bss->flink == link) {
9538 for_each_link_default(bss->valid_links, i, 0) {
9539 bss->flink = &bss->links[i];
9540 break;
9541 }
9542 }
9543
9544 /* If this was the last link, reset default link */
9545 if (!bss->valid_links) {
9546 /* TODO: Does keeping freq/bandwidth make sense? */
9547 if (bss->flink != link)
9548 os_memcpy(bss->flink, link, sizeof(*link));
9549
9550 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
9551 }
9552
9553 /* Remove the link from the kernel */
9554 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_REMOVE_LINK);
9555 if (!msg ||
9556 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
9557 nlmsg_free(msg);
9558 wpa_printf(MSG_ERROR,
9559 "nl80211: remove link (%d) failed", link_id);
9560 return -1;
9561 }
9562
9563 ret = send_and_recv_cmd(drv, msg);
9564 if (ret)
9565 wpa_printf(MSG_ERROR,
9566 "nl80211: remove link (%d) failed. ret=%d (%s)",
9567 link_id, ret, strerror(-ret));
9568
9569 return ret;
9570}
9571
9572
9573static void nl80211_remove_links(struct i802_bss *bss)
9574{
Sunil Ravi036cec52023-03-29 11:35:17 -07009575 int ret;
9576 u8 link_id;
9577
Sunil Ravi99c035e2024-07-12 01:42:03 +00009578 for_each_link(bss->valid_links, link_id) {
9579 ret = nl80211_remove_link(bss, link_id);
9580 if (ret)
9581 break;
Sunil Ravi88611412024-06-28 17:34:56 +00009582 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00009583
9584 if (bss->flink)
9585 os_memcpy(bss->flink->addr, bss->addr, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -07009586}
9587
9588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009589static int wpa_driver_nl80211_deinit_ap(void *priv)
9590{
9591 struct i802_bss *bss = priv;
9592 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi036cec52023-03-29 11:35:17 -07009593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009594 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009595 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07009596
9597 /* Stop beaconing */
Sunil Ravi99c035e2024-07-12 01:42:03 +00009598 wpa_driver_nl80211_del_beacon(bss, NL80211_DRV_LINK_ID_NA);
Sunil Ravi036cec52023-03-29 11:35:17 -07009599
9600 nl80211_remove_links(bss);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009601
9602 /*
9603 * If the P2P GO interface was dynamically added, then it is
9604 * possible that the interface change to station is not possible.
9605 */
9606 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
9607 return 0;
9608
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009609 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009610}
9611
9612
Sunil Ravi99c035e2024-07-12 01:42:03 +00009613static int wpa_driver_nl80211_stop_ap(void *priv, int link_id)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009614{
9615 struct i802_bss *bss = priv;
9616 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi036cec52023-03-29 11:35:17 -07009617
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009618 if (!is_ap_interface(drv->nlmode))
9619 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07009620
Sunil Ravi99c035e2024-07-12 01:42:03 +00009621 if (link_id == -1) {
9622 wpa_driver_nl80211_del_beacon_all(bss);
9623 return 0;
9624 }
Sunil Ravi036cec52023-03-29 11:35:17 -07009625
Sunil Ravi99c035e2024-07-12 01:42:03 +00009626 if (nl80211_link_valid(bss->valid_links, link_id)) {
9627 wpa_driver_nl80211_del_beacon(bss, link_id);
9628 return 0;
9629 }
9630
9631 return -1;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009632}
9633
9634
Dmitry Shmidt04949592012-07-19 12:16:46 -07009635static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9636{
9637 struct i802_bss *bss = priv;
9638 struct wpa_driver_nl80211_data *drv = bss->drv;
9639 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9640 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009641
9642 /*
9643 * If the P2P Client interface was dynamically added, then it is
9644 * possible that the interface change to station is not possible.
9645 */
9646 if (bss->if_dynamic)
9647 return 0;
9648
Dmitry Shmidt04949592012-07-19 12:16:46 -07009649 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9650}
9651
9652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009653static void wpa_driver_nl80211_resume(void *priv)
9654{
9655 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009656 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009657
9658 if (i802_set_iface_flags(bss, 1))
9659 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009660
9661 if (is_p2p_net_interface(nlmode))
9662 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009663}
9664
9665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009666static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9667{
9668 struct i802_bss *bss = priv;
9669 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009670 struct nl_msg *msg;
9671 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009672
9673 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9674 "hysteresis=%d", threshold, hysteresis);
9675
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009676 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
9677 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
9678 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
9679 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
9680 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009681 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009682 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009683 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009684
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009685 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009686}
9687
9688
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009689static int get_channel_width(struct nl_msg *msg, void *arg)
9690{
9691 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9692 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9693 struct wpa_signal_info *sig_change = arg;
9694
9695 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9696 genlmsg_attrlen(gnlh, 0), NULL);
9697
9698 sig_change->center_frq1 = -1;
9699 sig_change->center_frq2 = -1;
9700 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
9701
9702 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
9703 sig_change->chanwidth = convert2width(
9704 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
9705 if (tb[NL80211_ATTR_CENTER_FREQ1])
9706 sig_change->center_frq1 =
9707 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
9708 if (tb[NL80211_ATTR_CENTER_FREQ2])
9709 sig_change->center_frq2 =
9710 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
9711 }
9712
9713 return NL_SKIP;
9714}
9715
9716
9717static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
9718 struct wpa_signal_info *sig)
9719{
9720 struct nl_msg *msg;
9721
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009722 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009723 return send_and_recv_resp(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009724}
9725
9726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009727static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9728{
9729 struct i802_bss *bss = priv;
9730 struct wpa_driver_nl80211_data *drv = bss->drv;
9731 int res;
9732
9733 os_memset(si, 0, sizeof(*si));
Sunil Ravi77d572f2023-01-17 23:58:31 +00009734 res = nl80211_get_link_signal(drv, drv->bssid, &si->data);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009735 if (res) {
9736 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
9737 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
9738 return res;
Sunil Ravi77d572f2023-01-17 23:58:31 +00009739 si->data.signal = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009741
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009742 res = nl80211_get_channel_width(drv, si);
9743 if (res != 0)
9744 return res;
9745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009746 return nl80211_get_link_noise(drv, si);
9747}
9748
9749
Sunil Ravi89eba102022-09-13 21:04:37 -07009750static int get_links_noise(struct nl_msg *msg, void *arg)
9751{
9752 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9753 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9754 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
9755 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
9756 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
9757 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
9758 };
9759 struct wpa_mlo_signal_info *mlo_sig = arg;
9760 int i;
9761
9762 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9763 genlmsg_attrlen(gnlh, 0), NULL);
9764
9765 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
9766 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
9767 return NL_SKIP;
9768 }
9769
9770 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
9771 tb[NL80211_ATTR_SURVEY_INFO],
9772 survey_policy)) {
9773 wpa_printf(MSG_DEBUG,
9774 "nl80211: Failed to parse nested attributes");
9775 return NL_SKIP;
9776 }
9777
9778 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
9779 return NL_SKIP;
9780
9781 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
9782 return NL_SKIP;
9783
Sunil Ravi99c035e2024-07-12 01:42:03 +00009784 for_each_link(mlo_sig->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -07009785 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
9786 mlo_sig->links[i].frequency)
9787 continue;
9788
9789 mlo_sig->links[i].current_noise =
9790 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
9791 break;
9792 }
9793
9794 return NL_SKIP;
9795}
9796
9797
9798static int nl80211_get_links_noise(struct wpa_driver_nl80211_data *drv,
9799 struct wpa_mlo_signal_info *mlo_sig)
9800{
9801 struct nl_msg *msg;
9802
9803 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009804 return send_and_recv_resp(drv, msg, get_links_noise, mlo_sig);
Sunil Ravi89eba102022-09-13 21:04:37 -07009805}
9806
9807
9808static int get_links_channel_width(struct nl_msg *msg, void *arg)
9809{
9810 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9811 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9812 struct wpa_mlo_signal_info *mlo_sig = arg;
9813 struct nlattr *link;
9814 int rem_links;
9815
9816 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9817 genlmsg_attrlen(gnlh, 0), NULL);
9818
9819 if (!tb[NL80211_ATTR_MLO_LINKS])
9820 return NL_SKIP;
9821
9822 nla_for_each_nested(link, tb[NL80211_ATTR_MLO_LINKS], rem_links) {
9823 struct nlattr *tb2[NL80211_ATTR_MAX + 1];
9824 int link_id;
9825
9826 nla_parse(tb2, NL80211_ATTR_MAX, nla_data(link), nla_len(link),
9827 NULL);
9828
9829 if (!tb2[NL80211_ATTR_MLO_LINK_ID])
9830 continue;
9831
9832 link_id = nla_get_u8(tb2[NL80211_ATTR_MLO_LINK_ID]);
9833 if (link_id >= MAX_NUM_MLD_LINKS)
9834 continue;
9835
9836 if (!tb2[NL80211_ATTR_CHANNEL_WIDTH])
9837 continue;
9838 mlo_sig->links[link_id].chanwidth = convert2width(
9839 nla_get_u32(tb2[NL80211_ATTR_CHANNEL_WIDTH]));
9840 if (tb2[NL80211_ATTR_CENTER_FREQ1])
9841 mlo_sig->links[link_id].center_frq1 =
9842 nla_get_u32(tb2[NL80211_ATTR_CENTER_FREQ1]);
9843 if (tb2[NL80211_ATTR_CENTER_FREQ2])
9844 mlo_sig->links[link_id].center_frq2 =
9845 nla_get_u32(tb2[NL80211_ATTR_CENTER_FREQ2]);
9846 }
9847
9848 return NL_SKIP;
9849}
9850
9851
9852static int nl80211_get_links_channel_width(struct wpa_driver_nl80211_data *drv,
9853 struct wpa_mlo_signal_info *mlo_sig)
9854{
9855 struct nl_msg *msg;
9856
9857 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009858 return send_and_recv_resp(drv, msg, get_links_channel_width, mlo_sig);
Sunil Ravi89eba102022-09-13 21:04:37 -07009859}
9860
9861
9862static int nl80211_mlo_signal_poll(void *priv,
9863 struct wpa_mlo_signal_info *mlo_si)
9864{
9865 struct i802_bss *bss = priv;
9866 struct wpa_driver_nl80211_data *drv = bss->drv;
9867 int res;
9868 int i;
9869
9870 if (drv->nlmode != NL80211_IFTYPE_STATION ||
9871 !drv->sta_mlo_info.valid_links)
9872 return -1;
9873
9874 os_memset(mlo_si, 0, sizeof(*mlo_si));
9875 mlo_si->valid_links = drv->sta_mlo_info.valid_links;
9876
Sunil Ravi99c035e2024-07-12 01:42:03 +00009877 for_each_link(mlo_si->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -07009878 res = nl80211_get_link_signal(drv,
9879 drv->sta_mlo_info.links[i].bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00009880 &mlo_si->links[i].data);
Sunil Ravi89eba102022-09-13 21:04:37 -07009881 if (res != 0)
9882 return res;
9883
9884 mlo_si->links[i].center_frq1 = -1;
9885 mlo_si->links[i].center_frq2 = -1;
9886 mlo_si->links[i].chanwidth = CHAN_WIDTH_UNKNOWN;
9887 mlo_si->links[i].current_noise = WPA_INVALID_NOISE;
9888 mlo_si->links[i].frequency = drv->sta_mlo_info.links[i].freq;
9889 }
9890
9891 res = nl80211_get_links_channel_width(drv, mlo_si);
9892 if (res != 0)
9893 return res;
9894
9895 return nl80211_get_links_noise(drv, mlo_si);
9896}
9897
9898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009899static int nl80211_set_param(void *priv, const char *param)
9900{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07009901 struct i802_bss *bss = priv;
9902 struct wpa_driver_nl80211_data *drv = bss->drv;
9903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009904 if (param == NULL)
9905 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009906 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009907
9908#ifdef CONFIG_P2P
9909 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009910 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9911 "interface");
9912 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9913 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9914 }
9915#endif /* CONFIG_P2P */
9916
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07009917 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009918 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009919
9920 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009921 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009922 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009923 }
9924
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07009925 if (os_strstr(param, "force_bss_selection=1"))
9926 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
9927
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08009928 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08009929 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
9930 drv->test_use_roc_tx = 1;
9931 }
9932
Hai Shalomb755a2a2020-04-23 21:49:02 -07009933 if (os_strstr(param, "control_port=0")) {
Hai Shalomfdcde762020-04-02 11:19:20 -07009934 drv->capa.flags &= ~WPA_DRIVER_FLAGS_CONTROL_PORT;
Hai Shalom899fcc72020-10-19 14:38:18 -07009935 drv->capa.flags2 &= ~(WPA_DRIVER_FLAGS2_CONTROL_PORT_RX |
9936 WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS);
9937 drv->control_port_ap = 0;
Hai Shalomb755a2a2020-04-23 21:49:02 -07009938 }
9939
9940 if (os_strstr(param, "control_port_ap=1"))
9941 drv->control_port_ap = 1;
Hai Shalomfdcde762020-04-02 11:19:20 -07009942
Hai Shalom899fcc72020-10-19 14:38:18 -07009943 if (os_strstr(param, "control_port_ap=0")) {
9944 drv->capa.flags2 &= ~WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS;
9945 drv->control_port_ap = 0;
9946 }
9947
Hai Shalomfdcde762020-04-02 11:19:20 -07009948 if (os_strstr(param, "full_ap_client_state=0"))
9949 drv->capa.flags &= ~WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
9950
Hai Shalom899fcc72020-10-19 14:38:18 -07009951 if (os_strstr(param, "no_rrm=1")) {
9952 drv->no_rrm = 1;
9953
9954 if (!bss->in_deinit && !is_ap_interface(drv->nlmode) &&
9955 !is_mesh_interface(drv->nlmode)) {
9956 nl80211_mgmt_unsubscribe(bss, "no_rrm=1");
9957 if (nl80211_mgmt_subscribe_non_ap(bss) < 0)
9958 wpa_printf(MSG_DEBUG,
9959 "nl80211: Failed to re-register Action frame processing - ignore for now");
9960 }
9961 }
9962
Sunil Ravi640215c2023-06-28 23:08:09 +00009963 if (os_strstr(param, "secure_ltf=1")) {
9964 drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_STA |
9965 WPA_DRIVER_FLAGS2_SEC_LTF_AP;
9966 }
9967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009968 return 0;
9969}
9970
9971
Dmitry Shmidte4663042016-04-04 10:07:49 -07009972static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009973{
9974 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009975 struct netlink_config *cfg;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009976 struct utsname name;
9977
9978 if (uname(&name) == 0) {
9979 wpa_printf(MSG_DEBUG, "nl80211: Kernel version: %s %s (%s; %s)",
9980 name.sysname, name.release,
9981 name.version, name.machine);
9982 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009984 global = os_zalloc(sizeof(*global));
9985 if (global == NULL)
9986 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07009987 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009988 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009989 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009990 global->if_add_ifindex = -1;
9991
9992 cfg = os_zalloc(sizeof(*cfg));
9993 if (cfg == NULL)
9994 goto err;
9995
9996 cfg->ctx = global;
9997 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9998 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9999 global->netlink = netlink_init(cfg);
10000 if (global->netlink == NULL) {
10001 os_free(cfg);
10002 goto err;
10003 }
10004
10005 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10006 goto err;
10007
10008 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10009 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010010 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10011 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010012 goto err;
10013 }
10014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010015 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010016
10017err:
10018 nl80211_global_deinit(global);
10019 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010020}
10021
10022
10023static void nl80211_global_deinit(void *priv)
10024{
10025 struct nl80211_global *global = priv;
10026 if (global == NULL)
10027 return;
10028 if (!dl_list_empty(&global->interfaces)) {
10029 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10030 "nl80211_global_deinit",
10031 dl_list_len(&global->interfaces));
10032 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010033
10034 if (global->netlink)
10035 netlink_deinit(global->netlink);
10036
10037 nl_destroy_handles(&global->nl);
10038
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010039 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -070010040 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010041
10042 nl_cb_put(global->nl_cb);
10043
10044 if (global->ioctl_sock >= 0)
10045 close(global->ioctl_sock);
10046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010047 os_free(global);
10048}
10049
10050
10051static const char * nl80211_get_radio_name(void *priv)
10052{
10053 struct i802_bss *bss = priv;
10054 struct wpa_driver_nl80211_data *drv = bss->drv;
10055 return drv->phyname;
10056}
10057
10058
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010059static int nl80211_pmkid(struct i802_bss *bss, int cmd,
Sunil Ravi77d572f2023-01-17 23:58:31 +000010060 struct wpa_pmkid_params *params, bool skip_pmk)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010061{
10062 struct nl_msg *msg;
Sunil Ravi77d572f2023-01-17 23:58:31 +000010063
10064 if (cmd == NL80211_CMD_SET_PMKSA)
10065 wpa_printf(MSG_DEBUG,
10066 "nl80211: NL80211_CMD_SET_PMKSA with skip_pmk=%s pmk_len=%zu",
10067 skip_pmk ? "true" : "false", params->pmk_len);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010068
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010069 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010070 (params->pmkid &&
10071 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
10072 (params->bssid &&
10073 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
10074 (params->ssid_len &&
10075 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
10076 (params->fils_cache_id &&
10077 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
10078 params->fils_cache_id)) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070010079 (params->pmk_lifetime &&
10080 nla_put_u32(msg, NL80211_ATTR_PMK_LIFETIME,
10081 params->pmk_lifetime)) ||
10082 (params->pmk_reauth_threshold &&
10083 nla_put_u8(msg, NL80211_ATTR_PMK_REAUTH_THRESHOLD,
10084 params->pmk_reauth_threshold)) ||
Hai Shalom021b0b52019-04-10 11:17:58 -070010085 (cmd != NL80211_CMD_DEL_PMKSA &&
Sunil Ravi77d572f2023-01-17 23:58:31 +000010086 params->pmk_len && !skip_pmk &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010087 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Hai Shalom74f70d42019-02-11 14:42:39 -080010088 nl80211_nlmsg_clear(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010089 nlmsg_free(msg);
10090 return -ENOBUFS;
10091 }
Jouni Malinen75ecf522011-06-27 15:19:46 -070010092
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010093 return send_and_recv_cmd(bss->drv, msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010094}
10095
10096
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010097static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010098{
10099 struct i802_bss *bss = priv;
Sunil Ravi77d572f2023-01-17 23:58:31 +000010100 const size_t PMK_MAX_LEN = 64; /* current cfg80211 limit */
10101 const size_t LEGACY_PMK_MAX_LEN = 48; /* old cfg80211 limit */
10102 bool skip_pmk = params->pmk_len > PMK_MAX_LEN;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010103 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010104
10105 if (params->bssid)
10106 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
10107 MAC2STR(params->bssid));
10108 else if (params->fils_cache_id && params->ssid_len) {
10109 wpa_printf(MSG_DEBUG,
10110 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
10111 params->fils_cache_id[0], params->fils_cache_id[1],
10112 wpa_ssid_txt(params->ssid, params->ssid_len));
10113 }
10114
Sunil Ravi77d572f2023-01-17 23:58:31 +000010115 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params, skip_pmk);
10116 /*
10117 * Try again by skipping PMK if the first attempt failed with ERANGE
10118 * error, PMK was not skipped, and PMK length is greater than the
10119 * legacy kernel maximum allowed limit.
10120 */
10121 if (ret == -ERANGE && !skip_pmk &&
10122 params->pmk_len > LEGACY_PMK_MAX_LEN)
10123 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params, true);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010124 if (ret < 0) {
10125 wpa_printf(MSG_DEBUG,
10126 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
10127 ret, strerror(-ret));
10128 }
10129
10130 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010131}
10132
10133
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010134static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010135{
10136 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010137 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010138
10139 if (params->bssid)
10140 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10141 MAC2STR(params->bssid));
10142 else if (params->fils_cache_id && params->ssid_len) {
10143 wpa_printf(MSG_DEBUG,
10144 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
10145 params->fils_cache_id[0], params->fils_cache_id[1],
10146 wpa_ssid_txt(params->ssid, params->ssid_len));
10147 }
10148
Sunil Ravi77d572f2023-01-17 23:58:31 +000010149 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params, true);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010150 if (ret < 0) {
10151 wpa_printf(MSG_DEBUG,
10152 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
10153 ret, strerror(-ret));
10154 }
10155
10156 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010157}
10158
10159
10160static int nl80211_flush_pmkid(void *priv)
10161{
10162 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010163 struct nl_msg *msg;
10164
Jouni Malinen75ecf522011-06-27 15:19:46 -070010165 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010166 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
10167 if (!msg)
10168 return -ENOBUFS;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010169 return send_and_recv_cmd(bss->drv, msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010170}
10171
10172
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010173static void clean_survey_results(struct survey_results *survey_results)
10174{
10175 struct freq_survey *survey, *tmp;
10176
10177 if (dl_list_empty(&survey_results->survey_list))
10178 return;
10179
10180 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10181 struct freq_survey, list) {
10182 dl_list_del(&survey->list);
10183 os_free(survey);
10184 }
10185}
10186
10187
10188static void add_survey(struct nlattr **sinfo, u32 ifidx,
10189 struct dl_list *survey_list)
10190{
10191 struct freq_survey *survey;
10192
10193 survey = os_zalloc(sizeof(struct freq_survey));
10194 if (!survey)
10195 return;
10196
10197 survey->ifidx = ifidx;
10198 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10199 survey->filled = 0;
10200
10201 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10202 survey->nf = (int8_t)
10203 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10204 survey->filled |= SURVEY_HAS_NF;
10205 }
10206
10207 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10208 survey->channel_time =
10209 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10210 survey->filled |= SURVEY_HAS_CHAN_TIME;
10211 }
10212
10213 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10214 survey->channel_time_busy =
10215 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10216 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10217 }
10218
10219 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10220 survey->channel_time_rx =
10221 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10222 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10223 }
10224
10225 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10226 survey->channel_time_tx =
10227 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10228 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10229 }
10230
10231 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)",
10232 survey->freq,
10233 survey->nf,
10234 (unsigned long int) survey->channel_time,
10235 (unsigned long int) survey->channel_time_busy,
10236 (unsigned long int) survey->channel_time_tx,
10237 (unsigned long int) survey->channel_time_rx,
10238 survey->filled);
10239
10240 dl_list_add_tail(survey_list, &survey->list);
10241}
10242
10243
10244static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10245 unsigned int freq_filter)
10246{
10247 if (!freq_filter)
10248 return 1;
10249
10250 return freq_filter == surveyed_freq;
10251}
10252
10253
10254static int survey_handler(struct nl_msg *msg, void *arg)
10255{
10256 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10257 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10258 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10259 struct survey_results *survey_results;
10260 u32 surveyed_freq = 0;
10261 u32 ifidx;
10262
10263 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10264 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10265 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10266 };
10267
10268 survey_results = (struct survey_results *) arg;
10269
10270 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10271 genlmsg_attrlen(gnlh, 0), NULL);
10272
Dmitry Shmidt97672262014-02-03 13:02:54 -080010273 if (!tb[NL80211_ATTR_IFINDEX])
10274 return NL_SKIP;
10275
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010276 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10277
10278 if (!tb[NL80211_ATTR_SURVEY_INFO])
10279 return NL_SKIP;
10280
10281 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10282 tb[NL80211_ATTR_SURVEY_INFO],
10283 survey_policy))
10284 return NL_SKIP;
10285
10286 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10287 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10288 return NL_SKIP;
10289 }
10290
10291 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10292
10293 if (!check_survey_ok(sinfo, surveyed_freq,
10294 survey_results->freq_filter))
10295 return NL_SKIP;
10296
10297 if (survey_results->freq_filter &&
10298 survey_results->freq_filter != surveyed_freq) {
10299 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10300 surveyed_freq);
10301 return NL_SKIP;
10302 }
10303
10304 add_survey(sinfo, ifidx, &survey_results->survey_list);
10305
10306 return NL_SKIP;
10307}
10308
10309
10310static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10311{
10312 struct i802_bss *bss = priv;
10313 struct wpa_driver_nl80211_data *drv = bss->drv;
10314 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010315 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010316 union wpa_event_data data;
10317 struct survey_results *survey_results;
10318
10319 os_memset(&data, 0, sizeof(data));
10320 survey_results = &data.survey_results;
10321
10322 dl_list_init(&survey_results->survey_list);
10323
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010324 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010325 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010326 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010327
10328 if (freq)
10329 data.survey_results.freq_filter = freq;
10330
10331 do {
10332 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010333 err = send_and_recv_resp(drv, msg, survey_handler,
10334 survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010335 } while (err > 0);
10336
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010337 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010338 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010339 else
10340 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010341
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010342 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010343 return err;
10344}
10345
10346
Dmitry Shmidt807291d2015-01-27 13:40:23 -080010347static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
10348 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010349 const u8 *replay_ctr)
10350{
10351 struct i802_bss *bss = priv;
10352 struct wpa_driver_nl80211_data *drv = bss->drv;
10353 struct nlattr *replay_nested;
10354 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010355 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010356
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010357 if (!drv->set_rekey_offload)
10358 return;
10359
10360 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010361 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
10362 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -080010363 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010364 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010365 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10366 replay_ctr)) {
10367 nl80211_nlmsg_clear(msg);
10368 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010369 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010370 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010371
10372 nla_nest_end(msg, replay_nested);
10373
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010374 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010375 if (ret == -EOPNOTSUPP) {
10376 wpa_printf(MSG_DEBUG,
10377 "nl80211: Driver does not support rekey offload");
10378 drv->set_rekey_offload = 0;
10379 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010380}
10381
10382
10383static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10384 const u8 *addr, int qos)
10385{
10386 /* send data frame to poll STA and check whether
10387 * this frame is ACKed */
10388 struct {
10389 struct ieee80211_hdr hdr;
10390 u16 qos_ctl;
10391 } STRUCT_PACKED nulldata;
10392 size_t size;
10393
10394 /* Send data frame to poll STA and check whether this frame is ACKed */
10395
10396 os_memset(&nulldata, 0, sizeof(nulldata));
10397
10398 if (qos) {
10399 nulldata.hdr.frame_control =
10400 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10401 WLAN_FC_STYPE_QOS_NULL);
10402 size = sizeof(nulldata);
10403 } else {
10404 nulldata.hdr.frame_control =
10405 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10406 WLAN_FC_STYPE_NULLFUNC);
10407 size = sizeof(struct ieee80211_hdr);
10408 }
10409
10410 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10411 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10412 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10413 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10414
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010415 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010416 0, 0, NULL, 0, 0, -1) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010417 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10418 "send poll frame");
10419}
10420
10421static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10422 int qos)
10423{
10424 struct i802_bss *bss = priv;
10425 struct wpa_driver_nl80211_data *drv = bss->drv;
10426 struct nl_msg *msg;
Hai Shalom5f92bc92019-04-18 11:54:11 -070010427 u64 cookie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010428 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010429
10430 if (!drv->poll_command_supported) {
10431 nl80211_send_null_frame(bss, own_addr, addr, qos);
10432 return;
10433 }
10434
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010435 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
10436 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
10437 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010438 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010439 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010440
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010441 ret = send_and_recv_resp(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010442 if (ret < 0) {
10443 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
10444 MACSTR " failed: ret=%d (%s)",
10445 MAC2STR(addr), ret, strerror(-ret));
Hai Shalom5f92bc92019-04-18 11:54:11 -070010446 } else {
10447 wpa_printf(MSG_DEBUG,
10448 "nl80211: Client probe request addr=" MACSTR
10449 " cookie=%llu", MAC2STR(addr),
10450 (long long unsigned int) cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010451 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010452}
10453
10454
10455static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10456{
10457 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -070010458 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010459
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010460 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
10461 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
10462 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
10463 nlmsg_free(msg);
10464 return -ENOBUFS;
10465 }
Roshan Pius3a1667e2018-07-03 15:17:14 -070010466
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010467 ret = send_and_recv_cmd(bss->drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070010468 if (ret < 0) {
10469 wpa_printf(MSG_DEBUG,
10470 "nl80211: Setting PS state %s failed: %d (%s)",
10471 enabled ? "enabled" : "disabled",
10472 ret, strerror(-ret));
10473 }
10474 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010475}
10476
10477
10478static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10479 int ctwindow)
10480{
10481 struct i802_bss *bss = priv;
10482
10483 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10484 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10485
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010486 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010487#ifdef ANDROID_P2P
10488 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010489#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010490 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010491#endif /* ANDROID_P2P */
10492 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010493
10494 if (legacy_ps == -1)
10495 return 0;
10496 if (legacy_ps != 0 && legacy_ps != 1)
10497 return -1; /* Not yet supported */
10498
10499 return nl80211_set_power_save(bss, legacy_ps);
10500}
10501
10502
Dmitry Shmidt051af732013-10-22 13:52:46 -070010503static int nl80211_start_radar_detection(void *priv,
10504 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010505{
10506 struct i802_bss *bss = priv;
10507 struct wpa_driver_nl80211_data *drv = bss->drv;
10508 struct nl_msg *msg;
10509 int ret;
10510
Hai Shalom81f62d82019-07-22 12:10:00 -070010511 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)",
10512 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -070010513 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10514
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010515 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10516 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10517 "detection");
10518 return -1;
10519 }
10520
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010521 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
10522 nl80211_put_freq_params(msg, freq) < 0) {
10523 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010524 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010525 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010526
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010527 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010528 if (ret == 0)
10529 return 0;
10530 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10531 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010532 return -1;
10533}
10534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010535#ifdef CONFIG_TDLS
10536
Hai Shalomc1a21442022-02-04 13:43:00 -080010537static int nl80211_add_peer_capab(struct nl_msg *msg,
10538 enum tdls_peer_capability capa)
10539{
10540 u32 peer_capab = 0;
10541
10542 if (!capa)
10543 return 0;
10544
10545 if (capa & TDLS_PEER_HT)
10546 peer_capab |= NL80211_TDLS_PEER_HT;
10547 if (capa & TDLS_PEER_VHT)
10548 peer_capab |= NL80211_TDLS_PEER_VHT;
10549 if (capa & TDLS_PEER_WMM)
10550 peer_capab |= NL80211_TDLS_PEER_WMM;
10551 if (capa & TDLS_PEER_HE)
10552 peer_capab |= NL80211_TDLS_PEER_HE;
10553
10554 return nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
10555 peer_capab);
10556}
10557
10558
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010559static int
10560nl80211_tdls_set_discovery_resp_link(struct wpa_driver_nl80211_data *drv,
10561 int link_id)
10562{
10563#ifdef CONFIG_DRIVER_NL80211_QCA
10564 struct nl_msg *msg;
10565 struct nlattr *params;
10566
10567 wpa_printf(MSG_DEBUG, "nl80211: TDLS Discovery Response Tx link ID %u",
10568 link_id);
10569
10570 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10571 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10572 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10573 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10574 QCA_NL80211_VENDOR_SUBCMD_TDLS_DISC_RSP_EXT) ||
10575 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10576 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_TDLS_DISC_RSP_EXT_TX_LINK,
10577 link_id)) {
10578 wpa_printf(MSG_ERROR,
10579 "%s: err in adding vendor_cmd and vendor_data",
10580 __func__);
10581 nlmsg_free(msg);
10582 return -1;
10583 }
10584 nla_nest_end(msg, params);
10585
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010586 return send_and_recv_cmd(drv, msg);
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010587#else /* CONFIG_DRIVER_NL80211_QCA */
10588 wpa_printf(MSG_ERROR,
10589 "nl80211: Setting TX link for TDLS Discovery Response not supported");
10590 return -1;
10591#endif /* CONFIG_DRIVER_NL80211_QCA */
10592}
10593
10594
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010595static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10596 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070010597 u32 peer_capab, int initiator, const u8 *buf,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010598 size_t len, int link_id)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010599{
10600 struct i802_bss *bss = priv;
10601 struct wpa_driver_nl80211_data *drv = bss->drv;
10602 struct nl_msg *msg;
10603
10604 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10605 return -EOPNOTSUPP;
10606
10607 if (!dst)
10608 return -EINVAL;
10609
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010610 if (link_id >= 0 &&
10611 nl80211_tdls_set_discovery_resp_link(drv, link_id) < 0)
10612 return -EOPNOTSUPP;
10613
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010614 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
10615 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
10616 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
10617 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080010618 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code) ||
10619 nl80211_add_peer_capab(msg, peer_capab) ||
10620 (initiator && nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010621 nla_put(msg, NL80211_ATTR_IE, len, buf))
10622 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010623
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010624 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010625
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010626fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010627 nlmsg_free(msg);
10628 return -ENOBUFS;
10629}
10630
10631
10632static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10633{
10634 struct i802_bss *bss = priv;
10635 struct wpa_driver_nl80211_data *drv = bss->drv;
10636 struct nl_msg *msg;
10637 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -080010638 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010639
10640 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10641 return -EOPNOTSUPP;
10642
10643 switch (oper) {
10644 case TDLS_DISCOVERY_REQ:
10645 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10646 break;
10647 case TDLS_SETUP:
10648 nl80211_oper = NL80211_TDLS_SETUP;
10649 break;
10650 case TDLS_TEARDOWN:
10651 nl80211_oper = NL80211_TDLS_TEARDOWN;
10652 break;
10653 case TDLS_ENABLE_LINK:
10654 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10655 break;
10656 case TDLS_DISABLE_LINK:
10657 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10658 break;
10659 case TDLS_ENABLE:
10660 return 0;
10661 case TDLS_DISABLE:
10662 return 0;
10663 default:
10664 return -EINVAL;
10665 }
10666
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010667 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
10668 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
10669 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
10670 nlmsg_free(msg);
10671 return -ENOBUFS;
10672 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010673
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010674 res = send_and_recv_cmd(drv, msg);
Paul Stewart092955c2017-02-06 09:13:09 -080010675 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
10676 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
10677 strerror(-res));
10678 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010679}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010680
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010681
10682static int
10683nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
10684 const struct hostapd_freq_params *params)
10685{
10686 struct i802_bss *bss = priv;
10687 struct wpa_driver_nl80211_data *drv = bss->drv;
10688 struct nl_msg *msg;
10689 int ret = -ENOBUFS;
10690
10691 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
10692 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
10693 return -EOPNOTSUPP;
10694
10695 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
10696 " oper_class=%u freq=%u",
10697 MAC2STR(addr), oper_class, params->freq);
10698 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
10699 if (!msg ||
10700 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10701 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
10702 (ret = nl80211_put_freq_params(msg, params))) {
10703 nlmsg_free(msg);
10704 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
10705 return ret;
10706 }
10707
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010708 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010709}
10710
10711
10712static int
10713nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
10714{
10715 struct i802_bss *bss = priv;
10716 struct wpa_driver_nl80211_data *drv = bss->drv;
10717 struct nl_msg *msg;
10718
10719 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
10720 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
10721 return -EOPNOTSUPP;
10722
10723 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
10724 MAC2STR(addr));
10725 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
10726 if (!msg ||
10727 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
10728 nlmsg_free(msg);
10729 wpa_printf(MSG_DEBUG,
10730 "nl80211: Could not build TDLS cancel chan switch");
10731 return -ENOBUFS;
10732 }
10733
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010734 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010735}
10736
10737#endif /* CONFIG TDLS */
10738
10739
Hai Shalomfdcde762020-04-02 11:19:20 -070010740static int driver_nl80211_set_key(void *priv,
10741 struct wpa_driver_set_key_params *params)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010742{
10743 struct i802_bss *bss = priv;
Hai Shalomfdcde762020-04-02 11:19:20 -070010744
10745 return wpa_driver_nl80211_set_key(bss, params);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010746}
10747
10748
10749static int driver_nl80211_scan2(void *priv,
10750 struct wpa_driver_scan_params *params)
10751{
10752 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010753#ifdef CONFIG_DRIVER_NL80211_QCA
10754 struct wpa_driver_nl80211_data *drv = bss->drv;
10755
10756 /*
10757 * Do a vendor specific scan if possible. If only_new_results is
10758 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
10759 * cannot be achieved through a vendor scan. The below condition may
10760 * need to be modified if new scan flags are added in the future whose
10761 * functionality can only be achieved through a normal scan.
10762 */
10763 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
10764 return wpa_driver_nl80211_vendor_scan(bss, params);
10765#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010766 return wpa_driver_nl80211_scan(bss, params);
10767}
10768
10769
10770static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -070010771 u16 reason_code)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010772{
10773 struct i802_bss *bss = priv;
10774 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
10775}
10776
10777
10778static int driver_nl80211_authenticate(void *priv,
10779 struct wpa_driver_auth_params *params)
10780{
10781 struct i802_bss *bss = priv;
10782 return wpa_driver_nl80211_authenticate(bss, params);
10783}
10784
10785
10786static void driver_nl80211_deinit(void *priv)
10787{
10788 struct i802_bss *bss = priv;
10789 wpa_driver_nl80211_deinit(bss);
10790}
10791
10792
10793static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
10794 const char *ifname)
10795{
10796 struct i802_bss *bss = priv;
10797 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10798}
10799
10800
Sunil Ravi99c035e2024-07-12 01:42:03 +000010801#ifdef CONFIG_IEEE80211BE
10802
10803static int driver_nl80211_link_remove(void *priv, enum wpa_driver_if_type type,
10804 const char *ifname, u8 link_id)
10805{
10806 struct i802_bss *bss = priv;
10807 struct wpa_driver_nl80211_data *drv = bss->drv;
10808
10809 if (type != WPA_IF_AP_BSS ||
10810 !nl80211_link_valid(bss->valid_links, link_id))
10811 return -1;
10812
10813 wpa_printf(MSG_DEBUG,
10814 "nl80211: Teardown AP(%s) link %d (type=%d ifname=%s links=0x%x)",
10815 bss->ifname, link_id, type, ifname, bss->valid_links);
10816
10817 nl80211_remove_link(bss, link_id);
10818
10819 bss->ctx = bss->flink->ctx;
10820
10821 if (drv->first_bss == bss && !bss->valid_links)
10822 drv->ctx = bss->ctx;
10823
10824 if (!bss->valid_links) {
10825 wpa_printf(MSG_DEBUG,
10826 "nl80211: No more links remaining, so remove interface");
10827 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10828 }
10829
10830 return 0;
10831}
10832
10833
10834static bool nl80211_is_drv_shared(void *priv, void *bss_ctx)
10835{
10836 struct i802_bss *bss = priv;
10837 struct wpa_driver_nl80211_data *drv = bss->drv;
10838 unsigned int num_bss = 0;
10839
10840 /* If any other BSS exist, someone else is using this since at this
10841 * time, we would have removed all BSSs created by this driver and only
10842 * this BSS should be remaining if the driver is not shared by anyone.
10843 */
10844 for (bss = drv->first_bss; bss; bss = bss->next) {
10845 num_bss++;
10846 if (num_bss > 1)
10847 return true;
10848 }
10849
10850 /* This is the only BSS present */
10851 bss = priv;
10852
10853 /* If only one/no link is there no one is sharing */
10854 if (bss->valid_links <= 1)
10855 return false;
10856
10857 /* More than one link means someone is still using. To check if
10858 * only 1 bit is set, power of 2 condition can be checked. */
10859 if (!(bss->valid_links & (bss->valid_links - 1)))
10860 return false;
10861
10862 return true;
10863}
10864
10865#endif /* CONFIG_IEEE80211BE */
10866
10867
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010868static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -070010869 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010870 unsigned int freq,
Hai Shalomfdcde762020-04-02 11:19:20 -070010871 const u16 *csa_offs, size_t csa_offs_len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010872 int no_encrypt, unsigned int wait,
10873 int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010874{
10875 struct i802_bss *bss = priv;
10876 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Hai Shalomfdcde762020-04-02 11:19:20 -070010877 freq, 0, 0, wait, csa_offs,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010878 csa_offs_len, no_encrypt, link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010879}
10880
10881
10882static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
10883{
10884 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010885 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010886}
10887
10888
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010889static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010890 const char *ifname, int vlan_id,
10891 int link_id)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010892{
10893 struct i802_bss *bss = priv;
Sunil Ravi2a14cf12023-11-21 00:54:38 +000010894 return i802_set_sta_vlan(bss, addr, ifname, vlan_id, link_id);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010895}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010896
10897
10898static int driver_nl80211_read_sta_data(void *priv,
10899 struct hostap_sta_driver_data *data,
10900 const u8 *addr)
10901{
10902 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -080010903
10904 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010905 return i802_read_sta_data(bss, data, addr);
10906}
10907
10908
10909static int driver_nl80211_send_action(void *priv, unsigned int freq,
10910 unsigned int wait_time,
10911 const u8 *dst, const u8 *src,
10912 const u8 *bssid,
10913 const u8 *data, size_t data_len,
10914 int no_cck)
10915{
10916 struct i802_bss *bss = priv;
10917 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
10918 bssid, data, data_len, no_cck);
10919}
10920
10921
10922static int driver_nl80211_probe_req_report(void *priv, int report)
10923{
10924 struct i802_bss *bss = priv;
10925 return wpa_driver_nl80211_probe_req_report(bss, report);
10926}
10927
10928
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010929static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
10930 const u8 *ies, size_t ies_len)
10931{
10932 int ret;
10933 struct nl_msg *msg;
10934 struct i802_bss *bss = priv;
10935 struct wpa_driver_nl80211_data *drv = bss->drv;
10936 u16 mdid = WPA_GET_LE16(md);
10937
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010938 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010939 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
10940 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
10941 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
10942 nlmsg_free(msg);
10943 return -ENOBUFS;
10944 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010945
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010946 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010947 if (ret) {
10948 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
10949 "err=%d (%s)", ret, strerror(-ret));
10950 }
10951
10952 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010953}
10954
10955
Hai Shalom81f62d82019-07-22 12:10:00 -070010956static int nl80211_update_dh_ie(void *priv, const u8 *peer_mac,
10957 u16 reason_code, const u8 *ie, size_t ie_len)
10958{
10959 int ret;
10960 struct nl_msg *msg;
10961 struct i802_bss *bss = priv;
10962 struct wpa_driver_nl80211_data *drv = bss->drv;
10963
10964 wpa_printf(MSG_DEBUG, "nl80211: Updating DH IE peer: " MACSTR
10965 " reason %u", MAC2STR(peer_mac), reason_code);
10966 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_OWE_INFO)) ||
10967 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_mac) ||
10968 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, reason_code) ||
10969 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie))) {
10970 nlmsg_free(msg);
10971 return -ENOBUFS;
10972 }
10973
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010974 ret = send_and_recv_cmd(drv, msg);
Hai Shalom81f62d82019-07-22 12:10:00 -070010975 if (ret) {
10976 wpa_printf(MSG_DEBUG,
10977 "nl80211: update_dh_ie failed err=%d (%s)",
10978 ret, strerror(-ret));
10979 }
10980
10981 return ret;
10982}
10983
10984
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070010985static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010986{
10987 struct i802_bss *bss = priv;
10988 struct wpa_driver_nl80211_data *drv = bss->drv;
10989
10990 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
10991 return NULL;
10992
10993 return bss->addr;
10994}
10995
10996
Dmitry Shmidt56052862013-10-04 10:23:25 -070010997static const char * scan_state_str(enum scan_states scan_state)
10998{
10999 switch (scan_state) {
11000 case NO_SCAN:
11001 return "NO_SCAN";
11002 case SCAN_REQUESTED:
11003 return "SCAN_REQUESTED";
11004 case SCAN_STARTED:
11005 return "SCAN_STARTED";
11006 case SCAN_COMPLETED:
11007 return "SCAN_COMPLETED";
11008 case SCAN_ABORTED:
11009 return "SCAN_ABORTED";
11010 case SCHED_SCAN_STARTED:
11011 return "SCHED_SCAN_STARTED";
11012 case SCHED_SCAN_STOPPED:
11013 return "SCHED_SCAN_STOPPED";
11014 case SCHED_SCAN_RESULTS:
11015 return "SCHED_SCAN_RESULTS";
11016 }
11017
11018 return "??";
11019}
11020
11021
11022static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11023{
11024 struct i802_bss *bss = priv;
11025 struct wpa_driver_nl80211_data *drv = bss->drv;
11026 int res;
11027 char *pos, *end;
Hai Shalom74f70d42019-02-11 14:42:39 -080011028 struct nl_msg *msg;
11029 char alpha2[3] = { 0, 0, 0 };
Dmitry Shmidt56052862013-10-04 10:23:25 -070011030
11031 pos = buf;
11032 end = buf + buflen;
11033
11034 res = os_snprintf(pos, end - pos,
11035 "ifindex=%d\n"
11036 "ifname=%s\n"
11037 "brname=%s\n"
11038 "addr=" MACSTR "\n"
11039 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -070011040 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011041 bss->ifindex,
11042 bss->ifname,
11043 bss->brname,
11044 MAC2STR(bss->addr),
Sunil Ravi036cec52023-03-29 11:35:17 -070011045 bss->flink->freq,
11046 bss->flink->beacon_set ? "beacon_set=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011047 bss->added_if_into_bridge ?
11048 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -070011049 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011050 bss->added_bridge ? "added_bridge=1\n" : "",
11051 bss->in_deinit ? "in_deinit=1\n" : "",
11052 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011053 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011054 return pos - buf;
11055 pos += res;
11056
11057 if (bss->wdev_id_set) {
11058 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11059 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011060 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011061 return pos - buf;
11062 pos += res;
11063 }
11064
11065 res = os_snprintf(pos, end - pos,
11066 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011067 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070011068 "drv_ifindex=%d\n"
11069 "operstate=%d\n"
11070 "scan_state=%s\n"
11071 "auth_bssid=" MACSTR "\n"
11072 "auth_attempt_bssid=" MACSTR "\n"
11073 "bssid=" MACSTR "\n"
11074 "prev_bssid=" MACSTR "\n"
11075 "associated=%d\n"
11076 "assoc_freq=%u\n"
11077 "monitor_sock=%d\n"
11078 "monitor_ifidx=%d\n"
11079 "monitor_refcount=%d\n"
11080 "last_mgmt_freq=%u\n"
11081 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011082 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011083 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011084 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -070011085 drv->ifindex,
11086 drv->operstate,
11087 scan_state_str(drv->scan_state),
11088 MAC2STR(drv->auth_bssid),
11089 MAC2STR(drv->auth_attempt_bssid),
11090 MAC2STR(drv->bssid),
11091 MAC2STR(drv->prev_bssid),
11092 drv->associated,
11093 drv->assoc_freq,
11094 drv->monitor_sock,
11095 drv->monitor_ifidx,
11096 drv->monitor_refcount,
11097 drv->last_mgmt_freq,
11098 drv->eapol_tx_sock,
11099 drv->ignore_if_down_event ?
11100 "ignore_if_down_event=1\n" : "",
11101 drv->scan_complete_events ?
11102 "scan_complete_events=1\n" : "",
11103 drv->disabled_11b_rates ?
11104 "disabled_11b_rates=1\n" : "",
11105 drv->pending_remain_on_chan ?
11106 "pending_remain_on_chan=1\n" : "",
11107 drv->in_interface_list ? "in_interface_list=1\n" : "",
11108 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11109 drv->poll_command_supported ?
11110 "poll_command_supported=1\n" : "",
11111 drv->data_tx_status ? "data_tx_status=1\n" : "",
11112 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11113 drv->retry_auth ? "retry_auth=1\n" : "",
11114 drv->use_monitor ? "use_monitor=1\n" : "",
11115 drv->ignore_next_local_disconnect ?
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011116 "ignore_next_local_disconnect\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011117 drv->ignore_next_local_deauth ?
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011118 "ignore_next_local_deauth\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011119 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011120 return pos - buf;
11121 pos += res;
11122
Sunil Ravi89eba102022-09-13 21:04:37 -070011123 if (drv->sta_mlo_info.valid_links) {
11124 int i;
11125 struct driver_sta_mlo_info *mlo = &drv->sta_mlo_info;
11126
11127 res = os_snprintf(pos, end - pos,
Sunil Ravi640215c2023-06-28 23:08:09 +000011128 "ap_mld_addr=" MACSTR "\n"
11129 "default_map=%d\n",
11130 MAC2STR(mlo->ap_mld_addr),
11131 mlo->default_map);
Sunil Ravi89eba102022-09-13 21:04:37 -070011132 if (os_snprintf_error(end - pos, res))
11133 return pos - buf;
11134 pos += res;
11135
Sunil Ravi99c035e2024-07-12 01:42:03 +000011136 for_each_link(mlo->valid_links, i) {
Sunil Ravi89eba102022-09-13 21:04:37 -070011137 res = os_snprintf(pos, end - pos,
11138 "link_addr[%u]=" MACSTR "\n"
11139 "link_bssid[%u]=" MACSTR "\n"
11140 "link_freq[%u]=%u\n",
11141 i, MAC2STR(mlo->links[i].addr),
11142 i, MAC2STR(mlo->links[i].bssid),
11143 i, mlo->links[i].freq);
11144 if (os_snprintf_error(end - pos, res))
11145 return pos - buf;
11146 pos += res;
Sunil Ravi640215c2023-06-28 23:08:09 +000011147
11148 if (!mlo->default_map) {
11149 res = os_snprintf(
11150 pos, end - pos,
11151 "uplink_map[%u]=%x\n"
11152 "downlink_map[%u]=%x\n",
11153 i, mlo->links[i].t2lmap.uplink,
11154 i, mlo->links[i].t2lmap.downlink);
11155 if (os_snprintf_error(end - pos, res))
11156 return pos - buf;
11157 pos += res;
11158 }
Sunil Ravi89eba102022-09-13 21:04:37 -070011159 }
11160 }
11161
Dmitry Shmidt56052862013-10-04 10:23:25 -070011162 if (drv->has_capability) {
11163 res = os_snprintf(pos, end - pos,
11164 "capa.key_mgmt=0x%x\n"
11165 "capa.enc=0x%x\n"
11166 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011167 "capa.flags=0x%llx\n"
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011168 "capa.flags2=0x%llx\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011169 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070011170 "capa.max_scan_ssids=%d\n"
11171 "capa.max_sched_scan_ssids=%d\n"
11172 "capa.sched_scan_supported=%d\n"
11173 "capa.max_match_sets=%d\n"
11174 "capa.max_remain_on_chan=%u\n"
11175 "capa.max_stations=%u\n"
11176 "capa.probe_resp_offloads=0x%x\n"
11177 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011178 "capa.num_multichan_concurrent=%u\n"
11179 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011180 "capa.mac_addr_rand_scan_supported=%d\n"
11181 "capa.conc_capab=%u\n"
11182 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011183 "capa.max_conc_chan_5_0=%u\n"
11184 "capa.max_sched_scan_plans=%u\n"
11185 "capa.max_sched_scan_plan_interval=%u\n"
Sunil Ravi77d572f2023-01-17 23:58:31 +000011186 "capa.max_sched_scan_plan_iterations=%u\n"
11187 "capa.mbssid_max_interfaces=%u\n"
11188 "capa.ema_max_periodicity=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011189 drv->capa.key_mgmt,
11190 drv->capa.enc,
11191 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011192 (unsigned long long) drv->capa.flags,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011193 (unsigned long long) drv->capa.flags2,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011194 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011195 drv->capa.max_scan_ssids,
11196 drv->capa.max_sched_scan_ssids,
11197 drv->capa.sched_scan_supported,
11198 drv->capa.max_match_sets,
11199 drv->capa.max_remain_on_chan,
11200 drv->capa.max_stations,
11201 drv->capa.probe_resp_offloads,
11202 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011203 drv->capa.num_multichan_concurrent,
11204 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011205 drv->capa.mac_addr_rand_scan_supported,
11206 drv->capa.conc_capab,
11207 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011208 drv->capa.max_conc_chan_5_0,
11209 drv->capa.max_sched_scan_plans,
11210 drv->capa.max_sched_scan_plan_interval,
Sunil Ravi77d572f2023-01-17 23:58:31 +000011211 drv->capa.max_sched_scan_plan_iterations,
11212 drv->capa.mbssid_max_interfaces,
11213 drv->capa.ema_max_periodicity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011214 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -070011215 return pos - buf;
11216 pos += res;
11217 }
11218
Hai Shalom74f70d42019-02-11 14:42:39 -080011219 msg = nlmsg_alloc();
11220 if (msg &&
11221 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG) &&
11222 nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011223 if (send_and_recv_resp(drv, msg, nl80211_get_country,
11224 alpha2) == 0 &&
Hai Shalom74f70d42019-02-11 14:42:39 -080011225 alpha2[0]) {
11226 res = os_snprintf(pos, end - pos, "country=%s\n",
11227 alpha2);
11228 if (os_snprintf_error(end - pos, res))
11229 return pos - buf;
11230 pos += res;
11231 }
11232 } else {
11233 nlmsg_free(msg);
11234 }
11235
Dmitry Shmidt56052862013-10-04 10:23:25 -070011236 return pos - buf;
11237}
11238
11239
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011240static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11241{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011242 if ((settings->head &&
11243 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
11244 settings->head_len, settings->head)) ||
11245 (settings->tail &&
11246 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
11247 settings->tail_len, settings->tail)) ||
11248 (settings->beacon_ies &&
11249 nla_put(msg, NL80211_ATTR_IE,
11250 settings->beacon_ies_len, settings->beacon_ies)) ||
11251 (settings->proberesp_ies &&
11252 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
11253 settings->proberesp_ies_len, settings->proberesp_ies)) ||
11254 (settings->assocresp_ies &&
11255 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
11256 settings->assocresp_ies_len, settings->assocresp_ies)) ||
11257 (settings->probe_resp &&
11258 nla_put(msg, NL80211_ATTR_PROBE_RESP,
11259 settings->probe_resp_len, settings->probe_resp)))
11260 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011261
11262 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011263}
11264
11265
11266static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11267{
11268 struct nl_msg *msg;
11269 struct i802_bss *bss = priv;
11270 struct wpa_driver_nl80211_data *drv = bss->drv;
11271 struct nlattr *beacon_csa;
11272 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011273 int csa_off_len = 0;
11274 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011275
Hai Shalomc1a21442022-02-04 13:43:00 -080011276 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011277 "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 -080011278 settings->cs_count, settings->block_tx,
Hai Shalomc1a21442022-02-04 13:43:00 -080011279 settings->freq_params.freq,
11280 settings->freq_params.channel,
11281 settings->freq_params.sec_channel_offset,
11282 settings->freq_params.bandwidth,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011283 settings->freq_params.center_freq1,
Hai Shalomc1a21442022-02-04 13:43:00 -080011284 settings->freq_params.center_freq2,
Sunil Ravi036cec52023-03-29 11:35:17 -070011285 settings->punct_bitmap,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011286 settings->link_id,
Hai Shalomc1a21442022-02-04 13:43:00 -080011287 settings->freq_params.ht_enabled ? " ht" : "",
11288 settings->freq_params.vht_enabled ? " vht" : "",
11289 settings->freq_params.he_enabled ? " he" : "");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011290
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011291 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011292 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11293 return -EOPNOTSUPP;
11294 }
11295
Roshan Pius3a1667e2018-07-03 15:17:14 -070011296 if (drv->nlmode != NL80211_IFTYPE_AP &&
11297 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
11298 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011299 return -EOPNOTSUPP;
11300
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011301 /*
11302 * Remove empty counters, assuming Probe Response and Beacon frame
11303 * counters match. This implementation assumes that there are only two
11304 * counters.
11305 */
11306 if (settings->counter_offset_beacon[0] &&
11307 !settings->counter_offset_beacon[1]) {
11308 csa_off_len = 1;
11309 } else if (settings->counter_offset_beacon[1] &&
11310 !settings->counter_offset_beacon[0]) {
11311 csa_off_len = 1;
11312 settings->counter_offset_beacon[0] =
11313 settings->counter_offset_beacon[1];
11314 settings->counter_offset_presp[0] =
11315 settings->counter_offset_presp[1];
11316 } else if (settings->counter_offset_beacon[1] &&
11317 settings->counter_offset_beacon[0]) {
11318 csa_off_len = 2;
11319 } else {
11320 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
11321 return -EINVAL;
11322 }
11323
11324 /* Check CSA counters validity */
11325 if (drv->capa.max_csa_counters &&
11326 csa_off_len > drv->capa.max_csa_counters) {
11327 wpa_printf(MSG_ERROR,
11328 "nl80211: Too many CSA counters provided");
11329 return -EINVAL;
11330 }
11331
11332 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011333 return -EINVAL;
11334
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011335 for (i = 0; i < csa_off_len; i++) {
11336 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
11337 u16 csa_c_off_presp = settings->counter_offset_presp[i];
11338
11339 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
11340 (settings->beacon_csa.tail[csa_c_off_bcn] !=
11341 settings->cs_count))
11342 return -EINVAL;
11343
11344 if (settings->beacon_csa.probe_resp &&
11345 ((settings->beacon_csa.probe_resp_len <=
11346 csa_c_off_presp) ||
11347 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
11348 settings->cs_count)))
11349 return -EINVAL;
11350 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011352 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
11353 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
11354 settings->cs_count) ||
11355 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
11356 (settings->block_tx &&
Sunil Ravi036cec52023-03-29 11:35:17 -070011357 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)) ||
11358 (settings->punct_bitmap &&
11359 nla_put_u32(msg, NL80211_ATTR_PUNCT_BITMAP,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011360 settings->punct_bitmap)) ||
11361 (settings->link_id != NL80211_DRV_LINK_ID_NA &&
11362 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, settings->link_id)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011363 goto error;
11364
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011365 /* beacon_after params */
11366 ret = set_beacon_data(msg, &settings->beacon_after);
11367 if (ret)
11368 goto error;
11369
11370 /* beacon_csa params */
11371 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11372 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011373 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011374
11375 ret = set_beacon_data(msg, &settings->beacon_csa);
11376 if (ret)
11377 goto error;
11378
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011379 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11380 csa_off_len * sizeof(u16),
11381 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011382 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011383 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11384 csa_off_len * sizeof(u16),
11385 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011386 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011387
11388 nla_nest_end(msg, beacon_csa);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011389 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011390 if (ret) {
11391 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11392 ret, strerror(-ret));
11393 }
11394 return ret;
11395
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011396fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011397 ret = -ENOBUFS;
11398error:
11399 nlmsg_free(msg);
11400 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11401 return ret;
11402}
11403
11404
Sunil Ravia04bd252022-05-02 22:54:18 -070011405#ifdef CONFIG_IEEE80211AX
11406static int nl80211_switch_color(void *priv, struct cca_settings *settings)
11407{
11408 struct i802_bss *bss = priv;
11409 struct wpa_driver_nl80211_data *drv = bss->drv;
11410 struct nlattr *beacon_cca;
11411 struct nl_msg *msg;
11412 int ret = -ENOBUFS;
11413
11414 wpa_printf(MSG_DEBUG,
11415 "nl80211: Color change request (cca_count=%u color=%d)",
11416 settings->cca_count, settings->cca_color);
11417
11418 if (drv->nlmode != NL80211_IFTYPE_AP)
11419 return -EOPNOTSUPP;
11420
11421 if (!settings->beacon_cca.tail)
11422 return -EINVAL;
11423
11424 if (settings->beacon_cca.tail_len <= settings->counter_offset_beacon ||
11425 settings->beacon_cca.tail[settings->counter_offset_beacon] !=
11426 settings->cca_count)
11427 return -EINVAL;
11428
11429 if (settings->beacon_cca.probe_resp &&
11430 (settings->beacon_cca.probe_resp_len <=
11431 settings->counter_offset_presp ||
11432 settings->beacon_cca.probe_resp[settings->counter_offset_presp] !=
11433 settings->cca_count))
11434 return -EINVAL;
11435
11436 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_COLOR_CHANGE_REQUEST);
11437 if (!msg ||
11438 nla_put_u8(msg, NL80211_ATTR_COLOR_CHANGE_COUNT,
11439 settings->cca_count) ||
11440 nla_put_u8(msg, NL80211_ATTR_COLOR_CHANGE_COLOR,
11441 settings->cca_color))
11442 goto error;
11443
11444 /* beacon_after params */
11445 ret = set_beacon_data(msg, &settings->beacon_after);
11446 if (ret)
11447 goto error;
11448
11449 /* beacon_csa params */
11450 beacon_cca = nla_nest_start(msg, NL80211_ATTR_COLOR_CHANGE_ELEMS);
11451 if (!beacon_cca) {
11452 ret = -ENOBUFS;
11453 goto error;
11454 }
11455
11456 ret = set_beacon_data(msg, &settings->beacon_cca);
11457 if (ret)
11458 goto error;
11459
11460 if (nla_put_u16(msg, NL80211_ATTR_CNTDWN_OFFS_BEACON,
11461 settings->counter_offset_beacon) ||
11462 (settings->beacon_cca.probe_resp &&
11463 nla_put_u16(msg, NL80211_ATTR_CNTDWN_OFFS_PRESP,
11464 settings->counter_offset_presp))) {
11465 ret = -ENOBUFS;
11466 goto error;
11467 }
11468
11469 nla_nest_end(msg, beacon_cca);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011470 ret = send_and_recv_cmd(drv, msg);
Sunil Ravia04bd252022-05-02 22:54:18 -070011471 if (ret) {
11472 wpa_printf(MSG_DEBUG,
11473 "nl80211: switch_color failed err=%d (%s)",
11474 ret, strerror(-ret));
11475 }
11476 return ret;
11477
11478error:
11479 nlmsg_free(msg);
11480 wpa_printf(MSG_DEBUG, "nl80211: Could not build color switch request");
11481 return ret;
11482}
11483#endif /* CONFIG_IEEE80211AX */
11484
11485
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011486static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
11487 u8 user_priority, u16 admitted_time)
11488{
11489 struct i802_bss *bss = priv;
11490 struct wpa_driver_nl80211_data *drv = bss->drv;
11491 struct nl_msg *msg;
11492 int ret;
11493
11494 wpa_printf(MSG_DEBUG,
11495 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
11496 tsid, admitted_time, user_priority);
11497
11498 if (!is_sta_interface(drv->nlmode))
11499 return -ENOTSUP;
11500
11501 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
11502 if (!msg ||
11503 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
11504 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11505 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
11506 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
11507 nlmsg_free(msg);
11508 return -ENOBUFS;
11509 }
11510
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011511 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011512 if (ret)
11513 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
11514 ret, strerror(-ret));
11515 return ret;
11516}
11517
11518
11519static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
11520{
11521 struct i802_bss *bss = priv;
11522 struct wpa_driver_nl80211_data *drv = bss->drv;
11523 struct nl_msg *msg;
11524 int ret;
11525
11526 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
11527
11528 if (!is_sta_interface(drv->nlmode))
11529 return -ENOTSUP;
11530
11531 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
11532 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
11533 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
11534 nlmsg_free(msg);
11535 return -ENOBUFS;
11536 }
11537
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011538 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011539 if (ret)
11540 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
11541 ret, strerror(-ret));
11542 return ret;
11543}
11544
11545
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011546#ifdef CONFIG_TESTING_OPTIONS
11547static int cmd_reply_handler(struct nl_msg *msg, void *arg)
11548{
11549 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11550 struct wpabuf *buf = arg;
11551
11552 if (!buf)
11553 return NL_SKIP;
11554
11555 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
11556 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
11557 return NL_SKIP;
11558 }
11559
11560 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
11561 genlmsg_attrlen(gnlh, 0));
11562
11563 return NL_SKIP;
11564}
11565#endif /* CONFIG_TESTING_OPTIONS */
11566
11567
11568static int vendor_reply_handler(struct nl_msg *msg, void *arg)
11569{
11570 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11571 struct nlattr *nl_vendor_reply, *nl;
11572 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11573 struct wpabuf *buf = arg;
11574 int rem;
11575
11576 if (!buf)
11577 return NL_SKIP;
11578
11579 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11580 genlmsg_attrlen(gnlh, 0), NULL);
11581 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
11582
11583 if (!nl_vendor_reply)
11584 return NL_SKIP;
11585
11586 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
11587 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
11588 return NL_SKIP;
11589 }
11590
11591 nla_for_each_nested(nl, nl_vendor_reply, rem) {
11592 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
11593 }
11594
11595 return NL_SKIP;
11596}
11597
11598
Hai Shalom60840252021-02-19 19:02:11 -080011599static bool is_cmd_with_nested_attrs(unsigned int vendor_id,
11600 unsigned int subcmd)
11601{
11602 if (vendor_id != OUI_QCA)
11603 return true;
11604
11605 switch (subcmd) {
11606 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
11607 case QCA_NL80211_VENDOR_SUBCMD_STATS_EXT:
11608 case QCA_NL80211_VENDOR_SUBCMD_SCANNING_MAC_OUI:
11609 case QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY:
11610 case QCA_NL80211_VENDOR_SUBCMD_SPECTRAL_SCAN_GET_STATUS:
11611 case QCA_NL80211_VENDOR_SUBCMD_NAN:
11612 return false;
11613 default:
11614 return true;
11615 }
11616}
11617
11618
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011619static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
11620 unsigned int subcmd, const u8 *data,
Hai Shalom60840252021-02-19 19:02:11 -080011621 size_t data_len, enum nested_attr nested_attr,
11622 struct wpabuf *buf)
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011623{
11624 struct i802_bss *bss = priv;
11625 struct wpa_driver_nl80211_data *drv = bss->drv;
11626 struct nl_msg *msg;
Hai Shalom60840252021-02-19 19:02:11 -080011627 int ret, nla_flag;
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011628
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011629#ifdef CONFIG_TESTING_OPTIONS
11630 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011631 msg = nlmsg_alloc();
11632 if (!msg)
11633 return -ENOMEM;
11634
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011635 nl80211_cmd(drv, msg, 0, subcmd);
11636 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
11637 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011638 goto fail;
Hai Shalomb755a2a2020-04-23 21:49:02 -070011639 /* This test vendor_cmd can be used with nl80211 commands that
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011640 * need the connect nl_sock, so use the variant that takes in
11641 * bss->nl_connect as the handle. */
11642 ret = send_and_recv(drv->global, bss->nl_connect, msg,
11643 cmd_reply_handler, buf, NULL, NULL, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011644 if (ret)
11645 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
11646 ret);
11647 return ret;
11648 }
11649#endif /* CONFIG_TESTING_OPTIONS */
11650
Hai Shalom60840252021-02-19 19:02:11 -080011651 if (nested_attr == NESTED_ATTR_USED)
11652 nla_flag = NLA_F_NESTED;
11653 else if (nested_attr == NESTED_ATTR_UNSPECIFIED &&
11654 is_cmd_with_nested_attrs(vendor_id, subcmd))
11655 nla_flag = NLA_F_NESTED;
11656 else
11657 nla_flag = 0;
11658
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011659 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
11660 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
11661 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
11662 (data &&
Hai Shalom60840252021-02-19 19:02:11 -080011663 nla_put(msg, nla_flag | NL80211_ATTR_VENDOR_DATA,
11664 data_len, data)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011665 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011666
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011667 ret = send_and_recv_resp(drv, msg, vendor_reply_handler, buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011668 if (ret)
11669 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
11670 ret);
11671 return ret;
11672
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011673fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011674 nlmsg_free(msg);
11675 return -ENOBUFS;
11676}
11677
11678
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011679static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11680 u8 qos_map_set_len)
11681{
11682 struct i802_bss *bss = priv;
11683 struct wpa_driver_nl80211_data *drv = bss->drv;
11684 struct nl_msg *msg;
11685 int ret;
11686
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011687 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11688 qos_map_set, qos_map_set_len);
11689
Sunil Ravi2a14cf12023-11-21 00:54:38 +000011690 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_QOS_MAP)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011691 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
11692 nlmsg_free(msg);
11693 return -ENOBUFS;
11694 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011695
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011696 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011697 if (ret)
11698 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11699
11700 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011701}
11702
11703
Hai Shalomfdcde762020-04-02 11:19:20 -070011704static int get_wowlan_handler(struct nl_msg *msg, void *arg)
11705{
11706 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11707 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11708 int *wowlan_enabled = arg;
11709
11710 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11711 genlmsg_attrlen(gnlh, 0), NULL);
11712
11713 *wowlan_enabled = !!tb[NL80211_ATTR_WOWLAN_TRIGGERS];
11714
11715 return NL_SKIP;
11716}
11717
11718
11719static int nl80211_get_wowlan(void *priv)
11720{
11721 struct i802_bss *bss = priv;
11722 struct wpa_driver_nl80211_data *drv = bss->drv;
11723 struct nl_msg *msg;
11724 int wowlan_enabled;
11725 int ret;
11726
11727 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status");
11728
11729 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_WOWLAN);
11730
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011731 ret = send_and_recv_resp(drv, msg, get_wowlan_handler, &wowlan_enabled);
Hai Shalomfdcde762020-04-02 11:19:20 -070011732 if (ret) {
11733 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status failed");
11734 return 0;
11735 }
11736
11737 wpa_printf(MSG_DEBUG, "nl80211: wowlan is %s",
11738 wowlan_enabled ? "enabled" : "disabled");
11739
11740 return wowlan_enabled;
11741}
11742
11743
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011744static int nl80211_set_wowlan(void *priv,
11745 const struct wowlan_triggers *triggers)
11746{
11747 struct i802_bss *bss = priv;
11748 struct wpa_driver_nl80211_data *drv = bss->drv;
11749 struct nl_msg *msg;
11750 struct nlattr *wowlan_triggers;
11751 int ret;
11752
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011753 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
11754
Dmitry Shmidta3dc3092015-06-23 11:21:28 -070011755 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011756 !(wowlan_triggers = nla_nest_start(msg,
11757 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
11758 (triggers->any &&
11759 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11760 (triggers->disconnect &&
11761 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11762 (triggers->magic_pkt &&
11763 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11764 (triggers->gtk_rekey_failure &&
11765 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11766 (triggers->eap_identity_req &&
11767 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11768 (triggers->four_way_handshake &&
11769 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11770 (triggers->rfkill_release &&
11771 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
11772 nlmsg_free(msg);
11773 return -ENOBUFS;
11774 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011775
11776 nla_nest_end(msg, wowlan_triggers);
11777
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011778 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011779 if (ret)
11780 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
11781
11782 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011783}
11784
11785
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011786#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011787static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
11788{
11789 struct i802_bss *bss = priv;
11790 struct wpa_driver_nl80211_data *drv = bss->drv;
11791 struct nl_msg *msg;
11792 struct nlattr *params;
11793
11794 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
11795
11796 if (!drv->roaming_vendor_cmd_avail) {
11797 wpa_printf(MSG_DEBUG,
11798 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
11799 return -1;
11800 }
11801
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011802 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11803 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11804 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11805 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
11806 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11807 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
11808 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
11809 QCA_ROAMING_NOT_ALLOWED) ||
11810 (bssid &&
11811 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
11812 nlmsg_free(msg);
11813 return -1;
11814 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011815 nla_nest_end(msg, params);
11816
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011817 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011818}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011819
11820
Roshan Pius3a1667e2018-07-03 15:17:14 -070011821static int nl80211_disable_fils(void *priv, int disable)
11822{
11823 struct i802_bss *bss = priv;
11824 struct wpa_driver_nl80211_data *drv = bss->drv;
11825 struct nl_msg *msg;
11826 struct nlattr *params;
11827
11828 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
11829
11830 if (!drv->set_wifi_conf_vendor_cmd_avail)
11831 return -1;
11832
11833 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11834 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11835 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11836 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
11837 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11838 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
11839 disable)) {
11840 nlmsg_free(msg);
11841 return -1;
11842 }
11843 nla_nest_end(msg, params);
11844
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011845 return send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070011846}
11847
11848
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011849/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
11850#define WPA_SUPPLICANT_CLIENT_ID 1
11851
Hai Shalom899fcc72020-10-19 14:38:18 -070011852static int nl80211_set_bssid_tmp_disallow(void *priv, unsigned int num_bssid,
11853 const u8 *bssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011854{
11855 struct i802_bss *bss = priv;
11856 struct wpa_driver_nl80211_data *drv = bss->drv;
11857 struct nl_msg *msg;
11858 struct nlattr *params, *nlbssids, *attr;
11859 unsigned int i;
11860
Hai Shalom899fcc72020-10-19 14:38:18 -070011861 wpa_printf(MSG_DEBUG,
11862 "nl80211: Set temporarily disallowed BSSIDs (num=%u)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011863 num_bssid);
11864
11865 if (!drv->roam_vendor_cmd_avail)
11866 return -1;
11867
11868 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11869 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11870 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11871 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
11872 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11873 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
Hai Shalomc3565922019-10-28 11:58:20 -070011874 QCA_WLAN_VENDOR_ROAMING_SUBCMD_SET_BLACKLIST_BSSID) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011875 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
11876 WPA_SUPPLICANT_CLIENT_ID) ||
11877 nla_put_u32(msg,
11878 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
11879 num_bssid))
11880 goto fail;
11881
11882 nlbssids = nla_nest_start(
11883 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
11884 if (!nlbssids)
11885 goto fail;
11886
11887 for (i = 0; i < num_bssid; i++) {
11888 attr = nla_nest_start(msg, i);
11889 if (!attr)
11890 goto fail;
11891 if (nla_put(msg,
11892 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
11893 ETH_ALEN, &bssid[i * ETH_ALEN]))
11894 goto fail;
11895 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
11896 MAC2STR(&bssid[i * ETH_ALEN]));
11897 nla_nest_end(msg, attr);
11898 }
11899 nla_nest_end(msg, nlbssids);
11900 nla_nest_end(msg, params);
11901
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011902 return send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011903
11904fail:
11905 nlmsg_free(msg);
11906 return -1;
11907}
11908
Hai Shalomc3565922019-10-28 11:58:20 -070011909
11910static int nl80211_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
11911{
11912 struct i802_bss *bss = priv;
11913 struct wpa_driver_nl80211_data *drv = bss->drv;
11914 struct nl_msg *msg;
11915 struct nlattr *params;
11916
11917 if (!drv->add_sta_node_vendor_cmd_avail)
11918 return -EOPNOTSUPP;
11919
11920 wpa_printf(MSG_DEBUG, "nl80211: Add STA node");
11921
11922 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11923 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11924 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11925 QCA_NL80211_VENDOR_SUBCMD_ADD_STA_NODE) ||
11926 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11927 (addr &&
11928 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_MAC_ADDR, ETH_ALEN,
11929 addr)) ||
11930 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_AUTH_ALGO,
11931 auth_alg)) {
11932 nlmsg_free(msg);
11933 wpa_printf(MSG_ERROR,
11934 "%s: err in adding vendor_cmd and vendor_data",
11935 __func__);
11936 return -1;
11937 }
11938 nla_nest_end(msg, params);
11939
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011940 return send_and_recv_cmd(drv, msg);
Hai Shalomc3565922019-10-28 11:58:20 -070011941}
11942
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011943#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011944
11945
11946static int nl80211_set_mac_addr(void *priv, const u8 *addr)
11947{
11948 struct i802_bss *bss = priv;
11949 struct wpa_driver_nl80211_data *drv = bss->drv;
11950 int new_addr = addr != NULL;
Andy Kuoaba17c12022-04-14 16:05:31 +080011951#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali8a8f1002020-10-06 22:41:40 +053011952 struct nl_msg *msg;
11953 struct nlattr *params;
11954 int ret;
Andy Kuoaba17c12022-04-14 16:05:31 +080011955#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Mir Ali8a8f1002020-10-06 22:41:40 +053011956 wpa_printf(MSG_DEBUG, "Enter: %s", __FUNCTION__);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011957
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011958 if (TEST_FAIL())
11959 return -1;
Mir Ali8a8f1002020-10-06 22:41:40 +053011960 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
Andy Kuoaba17c12022-04-14 16:05:31 +080011961#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali8a8f1002020-10-06 22:41:40 +053011962 if (!addr ) {
11963 addr = drv->global->p2p_perm_addr;
11964 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011965
Mir Ali8a8f1002020-10-06 22:41:40 +053011966 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
11967 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
11968 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
Hai Shalomc1a21442022-02-04 13:43:00 -080011969 BRCM_VENDOR_SCMD_SET_MAC) ||
Mir Ali8a8f1002020-10-06 22:41:40 +053011970 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
11971 nla_put(msg, BRCM_ATTR_DRIVER_MAC_ADDR, ETH_ALEN, addr)) {
11972 wpa_printf(MSG_ERROR, "failed to put p2p randmac");
11973 nl80211_nlmsg_clear(msg);
11974 nlmsg_free(msg);
11975 return -ENOBUFS;
11976 }
11977 nla_nest_end(msg, params);
11978
Sunil Ravib0ac25f2024-07-12 01:42:03 +000011979 ret = send_and_recv_cmd(drv, msg);
Mir Ali8a8f1002020-10-06 22:41:40 +053011980 if (ret) {
11981 wpa_printf(MSG_ERROR, "nl80211: p2p set macaddr failed: ret=%d (%s)",
11982 ret, strerror(-ret));
11983 }
11984 memcpy(bss->addr, addr, ETH_ALEN);
11985 return ret;
11986#else
11987 return -ENOTSUP;
Andy Kuoaba17c12022-04-14 16:05:31 +080011988#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Mir Ali8a8f1002020-10-06 22:41:40 +053011989 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011990 if (!addr)
11991 addr = drv->perm_addr;
11992
11993 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
11994 return -1;
11995
11996 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
11997 {
11998 wpa_printf(MSG_DEBUG,
Mir Ali8a8f1002020-10-06 22:41:40 +053011999 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12000 bss->ifname, MAC2STR(addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012001 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
Mir Ali8a8f1002020-10-06 22:41:40 +053012002 1) < 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012003 wpa_printf(MSG_DEBUG,
Mir Ali8a8f1002020-10-06 22:41:40 +053012004 "nl80211: Could not restore interface UP after failed set_mac_addr");
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012005 }
12006 return -1;
12007 }
12008
12009 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000012010 bss->ifname, MAC2STR(addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012011 drv->addr_changed = new_addr;
Sunil Ravi77d572f2023-01-17 23:58:31 +000012012 os_memcpy(bss->prev_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012013 os_memcpy(bss->addr, addr, ETH_ALEN);
12014
Hsiu-Chang Chen289286d2024-01-24 18:00:43 +080012015 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12016 {
12017 wpa_printf(MSG_DEBUG,
12018 "nl80211: Could not restore interface UP after set_mac_addr");
12019 }
12020
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012021 return 0;
12022}
12023
12024
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012025#ifdef CONFIG_MESH
12026
12027static int wpa_driver_nl80211_init_mesh(void *priv)
12028{
12029 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
12030 wpa_printf(MSG_INFO,
12031 "nl80211: Failed to set interface into mesh mode");
12032 return -1;
12033 }
12034 return 0;
12035}
12036
12037
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012038static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
12039 size_t mesh_id_len)
12040{
12041 if (mesh_id) {
Hai Shalom74f70d42019-02-11 14:42:39 -080012042 wpa_printf(MSG_DEBUG, " * Mesh ID (SSID)=%s",
12043 wpa_ssid_txt(mesh_id, mesh_id_len));
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012044 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
12045 }
12046
12047 return 0;
12048}
12049
12050
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012051static int nl80211_put_mesh_config(struct nl_msg *msg,
12052 struct wpa_driver_mesh_bss_params *params)
12053{
12054 struct nlattr *container;
12055
12056 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
12057 if (!container)
12058 return -1;
12059
12060 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070012061 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
12062 params->auto_plinks)) ||
Hai Shalomc1a21442022-02-04 13:43:00 -080012063 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_FORWARDING) &&
12064 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
12065 params->forwarding)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012066 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
12067 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070012068 params->max_peer_links)) ||
12069 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
12070 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
12071 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012072 return -1;
12073
12074 /*
12075 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
12076 * the timer could disconnect stations even in that case.
12077 */
12078 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
12079 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
12080 params->peer_link_timeout)) {
12081 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
12082 return -1;
12083 }
12084
12085 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
12086 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
12087 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
12088 return -1;
12089 }
12090
12091 nla_nest_end(msg, container);
12092
12093 return 0;
12094}
12095
12096
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012097static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012098 struct wpa_driver_mesh_join_params *params)
12099{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012100 struct wpa_driver_nl80211_data *drv = bss->drv;
12101 struct nl_msg *msg;
12102 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080012103 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012104
12105 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
12106 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080012107 if (!msg ||
12108 nl80211_put_freq_params(msg, &params->freq) ||
12109 nl80211_put_basic_rates(msg, params->basic_rates) ||
12110 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070012111 nl80211_put_beacon_int(msg, params->beacon_int) ||
12112 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012113 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012114
12115 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
12116
Hai Shalom60840252021-02-19 19:02:11 -080012117 if (params->handle_dfs && nla_put_flag(msg, NL80211_ATTR_HANDLE_DFS))
12118 goto fail;
12119
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012120 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
12121 if (!container)
12122 goto fail;
12123
12124 if (params->ies) {
12125 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
12126 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
12127 params->ies))
12128 goto fail;
12129 }
12130 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
12131 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
12132 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
12133 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
12134 goto fail;
12135 }
12136 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
12137 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
12138 goto fail;
12139 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
12140 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
12141 goto fail;
12142 nla_nest_end(msg, container);
12143
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070012144 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
12145 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
12146 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
12147 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012148 goto fail;
12149
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012150 if (nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER))
12151 return -1;
12152 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
12153 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012154 msg = NULL;
12155 if (ret) {
12156 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
12157 ret, strerror(-ret));
12158 goto fail;
12159 }
12160 ret = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -070012161 drv->assoc_freq = bss->flink->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012162 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
12163
12164fail:
12165 nlmsg_free(msg);
12166 return ret;
12167}
12168
12169
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012170static int
12171wpa_driver_nl80211_join_mesh(void *priv,
12172 struct wpa_driver_mesh_join_params *params)
12173{
12174 struct i802_bss *bss = priv;
12175 int ret, timeout;
12176
12177 timeout = params->conf.peer_link_timeout;
12178
12179 /* Disable kernel inactivity timer */
12180 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
12181 params->conf.peer_link_timeout = 0;
12182
12183 ret = nl80211_join_mesh(bss, params);
12184 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
12185 wpa_printf(MSG_DEBUG,
12186 "nl80211: Mesh join retry for peer_link_timeout");
12187 /*
12188 * Old kernel does not support setting
12189 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
12190 * into future from peer_link_timeout.
12191 */
12192 params->conf.peer_link_timeout = timeout + 60;
12193 ret = nl80211_join_mesh(priv, params);
12194 }
12195
12196 params->conf.peer_link_timeout = timeout;
12197 return ret;
12198}
12199
12200
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012201static int wpa_driver_nl80211_leave_mesh(void *priv)
12202{
12203 struct i802_bss *bss = priv;
12204 struct wpa_driver_nl80211_data *drv = bss->drv;
12205 struct nl_msg *msg;
12206 int ret;
12207
12208 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
12209 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012210 ret = send_and_recv(drv->global, bss->nl_connect, msg, NULL, NULL, NULL,
12211 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012212 if (ret) {
12213 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
12214 ret, strerror(-ret));
12215 } else {
12216 wpa_printf(MSG_DEBUG,
12217 "nl80211: mesh leave request send successfully");
Sunil Ravi036cec52023-03-29 11:35:17 -070012218 drv->first_bss->flink->freq = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012219 }
12220
Hai Shalomc1a21442022-02-04 13:43:00 -080012221 if (drv->start_mode_sta &&
12222 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012223 NL80211_IFTYPE_STATION)) {
12224 wpa_printf(MSG_INFO,
12225 "nl80211: Failed to set interface into station mode");
12226 }
12227 return ret;
12228}
12229
Hai Shalom81f62d82019-07-22 12:10:00 -070012230
12231static int nl80211_probe_mesh_link(void *priv, const u8 *addr, const u8 *eth,
12232 size_t len)
12233{
12234 struct i802_bss *bss = priv;
12235 struct wpa_driver_nl80211_data *drv = bss->drv;
12236 struct nl_msg *msg;
12237 int ret;
12238
12239 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_PROBE_MESH_LINK);
12240 if (!msg ||
12241 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12242 nla_put(msg, NL80211_ATTR_FRAME, len, eth)) {
12243 nlmsg_free(msg);
12244 return -ENOBUFS;
12245 }
12246
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012247 ret = send_and_recv_cmd(drv, msg);
Hai Shalom81f62d82019-07-22 12:10:00 -070012248 if (ret) {
12249 wpa_printf(MSG_DEBUG, "nl80211: mesh link probe to " MACSTR
12250 " failed: ret=%d (%s)",
12251 MAC2STR(addr), ret, strerror(-ret));
12252 } else {
12253 wpa_printf(MSG_DEBUG, "nl80211: Mesh link to " MACSTR
12254 " probed successfully", MAC2STR(addr));
12255 }
12256
12257 return ret;
12258}
12259
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012260#endif /* CONFIG_MESH */
12261
12262
12263static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
12264 const u8 *ipaddr, int prefixlen,
12265 const u8 *addr)
12266{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012267 struct i802_bss *bss = priv;
12268 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012269 struct ndmsg nhdr = {
12270 .ndm_state = NUD_PERMANENT,
12271 .ndm_ifindex = bss->br_ifindex,
12272 };
12273 struct nl_msg *msg;
12274 int addrsize;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012275 int res;
12276
12277 if (!ipaddr || prefixlen == 0 || !addr)
12278 return -EINVAL;
12279
12280 if (bss->br_ifindex == 0) {
12281 wpa_printf(MSG_DEBUG,
12282 "nl80211: bridge must be set before adding an ip neigh to it");
12283 return -1;
12284 }
12285
12286 if (!drv->rtnl_sk) {
12287 wpa_printf(MSG_DEBUG,
12288 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
12289 return -1;
12290 }
12291
12292 if (version == 4) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012293 nhdr.ndm_family = AF_INET;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012294 addrsize = 4;
12295 } else if (version == 6) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012296 nhdr.ndm_family = AF_INET6;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012297 addrsize = 16;
12298 } else {
12299 return -EINVAL;
12300 }
12301
Sunil Ravi99c035e2024-07-12 01:42:03 +000012302 msg = nlmsg_alloc_simple(RTM_NEWNEIGH, NLM_F_CREATE);
12303 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012304 return -ENOMEM;
12305
Sunil Ravi99c035e2024-07-12 01:42:03 +000012306 res = -ENOMEM;
12307 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
12308 nla_put(msg, NDA_DST, addrsize, (void *) ipaddr) ||
12309 nla_put(msg, NDA_LLADDR, ETH_ALEN, (void *) addr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012310 goto errout;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012311
12312 res = nl_send_auto_complete(drv->rtnl_sk, msg);
12313 if (res < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012314 goto errout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012315
Sunil Ravi99c035e2024-07-12 01:42:03 +000012316 res = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012317 if (res) {
12318 wpa_printf(MSG_DEBUG,
12319 "nl80211: Adding bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012320 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012321 }
12322errout:
Sunil Ravi99c035e2024-07-12 01:42:03 +000012323 nlmsg_free(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012324 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012325}
12326
12327
12328static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
12329 const u8 *ipaddr)
12330{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012331 struct i802_bss *bss = priv;
12332 struct wpa_driver_nl80211_data *drv = bss->drv;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012333 struct ndmsg nhdr = {
12334 .ndm_state = NUD_PERMANENT,
12335 .ndm_ifindex = bss->br_ifindex,
12336 };
12337 struct nl_msg *msg;
12338 int addrsize;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012339 int res;
12340
12341 if (!ipaddr)
12342 return -EINVAL;
12343
12344 if (version == 4) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012345 nhdr.ndm_family = AF_INET;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012346 addrsize = 4;
12347 } else if (version == 6) {
Sunil Ravi99c035e2024-07-12 01:42:03 +000012348 nhdr.ndm_family = AF_INET6;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012349 addrsize = 16;
12350 } else {
12351 return -EINVAL;
12352 }
12353
12354 if (bss->br_ifindex == 0) {
12355 wpa_printf(MSG_DEBUG,
12356 "nl80211: bridge must be set to delete an ip neigh");
12357 return -1;
12358 }
12359
12360 if (!drv->rtnl_sk) {
12361 wpa_printf(MSG_DEBUG,
12362 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
12363 return -1;
12364 }
12365
Sunil Ravi99c035e2024-07-12 01:42:03 +000012366 msg = nlmsg_alloc_simple(RTM_DELNEIGH, NLM_F_CREATE);
12367 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012368 return -ENOMEM;
12369
Sunil Ravi99c035e2024-07-12 01:42:03 +000012370 res = -ENOMEM;
12371 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0 ||
12372 nla_put(msg, NDA_DST, addrsize, (void *) ipaddr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012373 goto errout;
Sunil Ravi99c035e2024-07-12 01:42:03 +000012374
12375 res = nl_send_auto_complete(drv->rtnl_sk, msg);
12376 if (res < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012377 goto errout;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012378
Sunil Ravi99c035e2024-07-12 01:42:03 +000012379 res = nl_wait_for_ack(drv->rtnl_sk);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012380 if (res) {
12381 wpa_printf(MSG_DEBUG,
12382 "nl80211: Deleting bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012383 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012384 }
12385errout:
Sunil Ravi99c035e2024-07-12 01:42:03 +000012386 nlmsg_free(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012387 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012388}
12389
12390
12391static int linux_write_system_file(const char *path, unsigned int val)
12392{
12393 char buf[50];
12394 int fd, len;
12395
12396 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
12397 if (os_snprintf_error(sizeof(buf), len))
12398 return -1;
12399
12400 fd = open(path, O_WRONLY);
12401 if (fd < 0)
12402 return -1;
12403
12404 if (write(fd, buf, len) < 0) {
12405 wpa_printf(MSG_DEBUG,
12406 "nl80211: Failed to write Linux system file: %s with the value of %d",
12407 path, val);
12408 close(fd);
12409 return -1;
12410 }
12411 close(fd);
12412
12413 return 0;
12414}
12415
12416
12417static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
12418{
12419 switch (attr) {
12420 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -070012421 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012422 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
12423 return "hairpin_mode";
Sunil Ravi036cec52023-03-29 11:35:17 -070012424 case DRV_BR_PORT_ATTR_MCAST2UCAST:
12425 return "multicast_to_unicast";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012426 }
12427
12428 return NULL;
12429}
12430
12431
12432static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
12433 unsigned int val)
12434{
12435 struct i802_bss *bss = priv;
12436 char path[128];
12437 const char *attr_txt;
12438
12439 attr_txt = drv_br_port_attr_str(attr);
12440 if (attr_txt == NULL)
12441 return -EINVAL;
12442
12443 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
12444 bss->ifname, attr_txt);
12445
12446 if (linux_write_system_file(path, val))
12447 return -1;
12448
12449 return 0;
12450}
12451
12452
12453static const char * drv_br_net_param_str(enum drv_br_net_param param)
12454{
12455 switch (param) {
12456 case DRV_BR_NET_PARAM_GARP_ACCEPT:
12457 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -070012458 default:
12459 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012460 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012461}
12462
12463
12464static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
12465 unsigned int val)
12466{
12467 struct i802_bss *bss = priv;
12468 char path[128];
12469 const char *param_txt;
12470 int ip_version = 4;
12471
Dmitry Shmidt83474442015-04-15 13:47:09 -070012472 if (param == DRV_BR_MULTICAST_SNOOPING) {
12473 os_snprintf(path, sizeof(path),
12474 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
12475 bss->brname);
12476 goto set_val;
12477 }
12478
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012479 param_txt = drv_br_net_param_str(param);
12480 if (param_txt == NULL)
12481 return -EINVAL;
12482
12483 switch (param) {
12484 case DRV_BR_NET_PARAM_GARP_ACCEPT:
12485 ip_version = 4;
12486 break;
12487 default:
12488 return -EINVAL;
12489 }
12490
12491 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
12492 ip_version, bss->brname, param_txt);
12493
Dmitry Shmidt83474442015-04-15 13:47:09 -070012494set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012495 if (linux_write_system_file(path, val))
12496 return -1;
12497
12498 return 0;
12499}
12500
12501
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012502#ifdef CONFIG_DRIVER_NL80211_QCA
12503
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012504static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
12505{
12506 switch (hw_mode) {
12507 case HOSTAPD_MODE_IEEE80211B:
12508 return QCA_ACS_MODE_IEEE80211B;
12509 case HOSTAPD_MODE_IEEE80211G:
12510 return QCA_ACS_MODE_IEEE80211G;
12511 case HOSTAPD_MODE_IEEE80211A:
12512 return QCA_ACS_MODE_IEEE80211A;
12513 case HOSTAPD_MODE_IEEE80211AD:
12514 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -070012515 case HOSTAPD_MODE_IEEE80211ANY:
12516 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012517 default:
12518 return -1;
12519 }
12520}
12521
12522
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012523static int add_acs_ch_list(struct nl_msg *msg, const int *freq_list)
12524{
12525 int num_channels = 0, num_freqs;
12526 u8 *ch_list;
12527 enum hostapd_hw_mode hw_mode;
12528 int ret = 0;
12529 int i;
12530
12531 if (!freq_list)
12532 return 0;
12533
12534 num_freqs = int_array_len(freq_list);
12535 ch_list = os_malloc(sizeof(u8) * num_freqs);
12536 if (!ch_list)
12537 return -1;
12538
12539 for (i = 0; i < num_freqs; i++) {
12540 const int freq = freq_list[i];
12541
12542 if (freq == 0)
12543 break;
12544 /* Send 2.4 GHz and 5 GHz channels with
12545 * QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST to maintain backwards
12546 * compatibility.
12547 */
12548 if (!(freq >= 2412 && freq <= 2484) &&
Hai Shalomc1a21442022-02-04 13:43:00 -080012549 !(freq >= 5180 && freq <= 5900) &&
12550 !(freq >= 5945 && freq <= 7115))
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012551 continue;
12552 hw_mode = ieee80211_freq_to_chan(freq, &ch_list[num_channels]);
12553 if (hw_mode != NUM_HOSTAPD_MODES)
12554 num_channels++;
12555 }
12556
12557 if (num_channels)
12558 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST,
12559 num_channels, ch_list);
12560
12561 os_free(ch_list);
12562 return ret;
12563}
12564
12565
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012566static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
12567{
12568 int i, len, ret;
12569 u32 *freqs;
12570
12571 if (!freq_list)
12572 return 0;
12573 len = int_array_len(freq_list);
12574 freqs = os_malloc(sizeof(u32) * len);
12575 if (!freqs)
12576 return -1;
12577 for (i = 0; i < len; i++)
12578 freqs[i] = freq_list[i];
12579 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
12580 sizeof(u32) * len, freqs);
12581 os_free(freqs);
12582 return ret;
12583}
12584
12585
Hai Shalomc1a21442022-02-04 13:43:00 -080012586static int nl80211_qca_do_acs(struct wpa_driver_nl80211_data *drv,
12587 struct drv_acs_params *params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012588{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012589 struct nl_msg *msg;
12590 struct nlattr *data;
12591 int ret;
12592 int mode;
12593
12594 mode = hw_mode_to_qca_acs(params->hw_mode);
12595 if (mode < 0)
12596 return -1;
12597
12598 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12599 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12600 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12601 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
12602 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12603 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
12604 (params->ht_enabled &&
12605 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
12606 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012607 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
12608 (params->vht_enabled &&
12609 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
Sunil Ravia04bd252022-05-02 22:54:18 -070012610 (params->eht_enabled &&
12611 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EHT_ENABLED)) ||
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012612 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
12613 params->ch_width) ||
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080012614 add_acs_ch_list(msg, params->freq_list) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070012615 add_acs_freq_list(msg, params->freq_list) ||
12616 (params->edmg_enabled &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012617 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EDMG_ENABLED)) ||
12618 (params->link_id != NL80211_DRV_LINK_ID_NA &&
12619 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_LINK_ID,
12620 params->link_id))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012621 nlmsg_free(msg);
12622 return -ENOBUFS;
12623 }
12624 nla_nest_end(msg, data);
12625
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012626 wpa_printf(MSG_DEBUG,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012627 "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 -070012628 params->hw_mode, params->ht_enabled, params->ht40_enabled,
Sunil Ravia04bd252022-05-02 22:54:18 -070012629 params->vht_enabled, params->eht_enabled, params->ch_width,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012630 params->edmg_enabled, params->link_id);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070012631
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012632 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012633 if (ret) {
12634 wpa_printf(MSG_DEBUG,
12635 "nl80211: Failed to invoke driver ACS function: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012636 strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080012637 }
12638 return ret;
12639}
12640
12641
Hai Shalom60840252021-02-19 19:02:11 -080012642static int nl80211_set_band(void *priv, u32 band_mask)
Ravi Joshie6ccb162015-07-16 17:45:41 -070012643{
12644 struct i802_bss *bss = priv;
12645 struct wpa_driver_nl80211_data *drv = bss->drv;
12646 struct nl_msg *msg;
12647 struct nlattr *data;
12648 int ret;
Hai Shalom60840252021-02-19 19:02:11 -080012649 enum qca_set_band qca_band_value;
12650 u32 qca_band_mask = QCA_SETBAND_AUTO;
Ravi Joshie6ccb162015-07-16 17:45:41 -070012651
Hai Shalom60840252021-02-19 19:02:11 -080012652 if (!drv->setband_vendor_cmd_avail ||
12653 (band_mask > (WPA_SETBAND_2G | WPA_SETBAND_5G | WPA_SETBAND_6G)))
Ravi Joshie6ccb162015-07-16 17:45:41 -070012654 return -1;
12655
Hai Shalom60840252021-02-19 19:02:11 -080012656 if (band_mask & WPA_SETBAND_5G)
12657 qca_band_mask |= QCA_SETBAND_5G;
12658 if (band_mask & WPA_SETBAND_2G)
12659 qca_band_mask |= QCA_SETBAND_2G;
12660 if (band_mask & WPA_SETBAND_6G)
12661 qca_band_mask |= QCA_SETBAND_6G;
12662
12663 /*
12664 * QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE is a legacy interface hence make
12665 * it suite to its values (AUTO/5G/2G) for backwards compatibility.
12666 */
12667 qca_band_value = ((qca_band_mask & QCA_SETBAND_5G) &&
12668 (qca_band_mask & QCA_SETBAND_2G)) ?
12669 QCA_SETBAND_AUTO :
12670 qca_band_mask & ~QCA_SETBAND_6G;
12671
12672 wpa_printf(MSG_DEBUG,
12673 "nl80211: QCA_BAND_MASK = 0x%x, QCA_BAND_VALUE = %d",
12674 qca_band_mask, qca_band_value);
Ravi Joshie6ccb162015-07-16 17:45:41 -070012675
12676 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12677 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12678 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12679 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
12680 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
Hai Shalom60840252021-02-19 19:02:11 -080012681 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE,
12682 qca_band_value) ||
12683 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_MASK,
12684 qca_band_mask)) {
Ravi Joshie6ccb162015-07-16 17:45:41 -070012685 nlmsg_free(msg);
12686 return -ENOBUFS;
12687 }
12688 nla_nest_end(msg, data);
12689
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012690 ret = send_and_recv_cmd(drv, msg);
Ravi Joshie6ccb162015-07-16 17:45:41 -070012691 if (ret) {
12692 wpa_printf(MSG_DEBUG,
12693 "nl80211: Driver setband function failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070012694 strerror(-ret));
Ravi Joshie6ccb162015-07-16 17:45:41 -070012695 }
12696 return ret;
12697}
12698
12699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012700struct nl80211_pcl {
12701 unsigned int num;
Sunil8cd6f4d2022-06-28 18:40:46 +000012702 struct weighted_pcl *freq_list;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012703};
12704
Sunil8cd6f4d2022-06-28 18:40:46 +000012705static void get_pcl_attr_values(struct weighted_pcl *wpcl, struct nlattr *nl[])
12706{
12707 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_FREQ])
12708 wpcl->freq = nla_get_u32(nl[QCA_WLAN_VENDOR_ATTR_PCL_FREQ]);
12709 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_WEIGHT])
12710 wpcl->weight = nla_get_u8(nl[QCA_WLAN_VENDOR_ATTR_PCL_WEIGHT]);
12711 if (nl[QCA_WLAN_VENDOR_ATTR_PCL_FLAG]) {
12712 u32 flags = nla_get_u32(nl[QCA_WLAN_VENDOR_ATTR_PCL_FLAG]);
12713
12714 wpcl->flag = 0;
12715 if (flags & BIT(0))
12716 wpcl->flag |= WEIGHTED_PCL_GO;
12717 if (flags & BIT(1))
12718 wpcl->flag |= WEIGHTED_PCL_CLI;
12719 if (flags & BIT(2))
12720 wpcl->flag |= WEIGHTED_PCL_MUST_CONSIDER;
12721 if (flags & BIT(3))
12722 wpcl->flag |= WEIGHTED_PCL_EXCLUDE;
12723 } else {
12724 wpcl->flag = WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
12725 }
12726}
12727
12728
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012729static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
12730{
12731 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12732 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12733 struct nl80211_pcl *param = arg;
12734 struct nlattr *nl_vend, *attr;
12735 enum qca_iface_type iface_type;
12736 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
Sunil8cd6f4d2022-06-28 18:40:46 +000012737 struct nlattr *nl_pcl[QCA_WLAN_VENDOR_ATTR_PCL_MAX + 1];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012738 unsigned int num, max_num;
12739 u32 *freqs;
12740
12741 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12742 genlmsg_attrlen(gnlh, 0), NULL);
12743
12744 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
12745 if (!nl_vend)
12746 return NL_SKIP;
12747
12748 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
12749 nla_data(nl_vend), nla_len(nl_vend), NULL);
12750
12751 attr = tb_vendor[
12752 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
12753 if (!attr) {
12754 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
12755 param->num = 0;
12756 return NL_SKIP;
12757 }
12758
12759 iface_type = (enum qca_iface_type) nla_get_u32(attr);
12760 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
12761 iface_type);
12762
Sunil8cd6f4d2022-06-28 18:40:46 +000012763 attr = tb_vendor[
12764 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_WEIGHED_PCL];
12765 if (attr) {
12766 int rem;
12767 struct nlattr *wpcl = attr;
12768 unsigned int i;
12769
12770 num = 0;
12771 nla_for_each_nested(attr, wpcl, rem) {
12772 if (num == param->num)
12773 break; /* not enough room for all entries */
12774 if (nla_parse(nl_pcl, QCA_WLAN_VENDOR_ATTR_PCL_MAX,
12775 nla_data(attr), nla_len(attr), NULL)) {
12776 wpa_printf(MSG_ERROR,
12777 "nl80211: Failed to parse PCL info");
12778 param->num = 0;
12779 return NL_SKIP;
12780 }
12781 get_pcl_attr_values(&param->freq_list[num], nl_pcl);
12782 num++;
12783 }
12784 param->num = num;
12785
12786 /* Sort frequencies based on their weight */
12787 for (i = 0; i < num; i++) {
12788 unsigned int j;
12789
12790 for (j = i + 1; j < num; j++) {
12791 if (param->freq_list[i].weight <
12792 param->freq_list[j].weight) {
12793 struct weighted_pcl tmp;
12794
12795 tmp = param->freq_list[i];
12796 param->freq_list[i] =
12797 param->freq_list[j];
12798 param->freq_list[j] = tmp;
12799 }
12800 }
12801 }
12802 } else if (tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST]) {
12803 wpa_printf(MSG_DEBUG,
12804 "nl80211: Driver does not provide weighted PCL; use the non-weighted variant");
12805 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
12806 /*
12807 * param->num has the maximum number of entries for which there
12808 * is room in the freq_list provided by the caller.
12809 */
12810 freqs = nla_data(attr);
12811 max_num = nla_len(attr) / sizeof(u32);
12812 if (max_num > param->num)
12813 max_num = param->num;
12814 for (num = 0; num < max_num; num++) {
12815 param->freq_list[num].freq = freqs[num];
12816 param->freq_list[num].flag =
12817 WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
12818 }
12819 param->num = num;
12820 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012821 wpa_printf(MSG_ERROR,
12822 "nl80211: preferred_freq_list couldn't be found");
12823 param->num = 0;
12824 return NL_SKIP;
12825 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012826 return NL_SKIP;
12827}
12828
12829
12830static int nl80211_get_pref_freq_list(void *priv,
12831 enum wpa_driver_if_type if_type,
12832 unsigned int *num,
Sunil8cd6f4d2022-06-28 18:40:46 +000012833 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012834{
12835 struct i802_bss *bss = priv;
12836 struct wpa_driver_nl80211_data *drv = bss->drv;
12837 struct nl_msg *msg;
12838 int ret;
12839 unsigned int i;
12840 struct nlattr *params;
12841 struct nl80211_pcl param;
12842 enum qca_iface_type iface_type;
12843
12844 if (!drv->get_pref_freq_list)
12845 return -1;
12846
12847 switch (if_type) {
12848 case WPA_IF_STATION:
12849 iface_type = QCA_IFACE_TYPE_STA;
12850 break;
12851 case WPA_IF_AP_BSS:
12852 iface_type = QCA_IFACE_TYPE_AP;
12853 break;
12854 case WPA_IF_P2P_GO:
12855 iface_type = QCA_IFACE_TYPE_P2P_GO;
12856 break;
12857 case WPA_IF_P2P_CLIENT:
12858 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
12859 break;
12860 case WPA_IF_IBSS:
12861 iface_type = QCA_IFACE_TYPE_IBSS;
12862 break;
12863 case WPA_IF_TDLS:
12864 iface_type = QCA_IFACE_TYPE_TDLS;
12865 break;
12866 default:
12867 return -1;
12868 }
12869
12870 param.num = *num;
12871 param.freq_list = freq_list;
12872
12873 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12874 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
12875 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12876 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12877 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
12878 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12879 nla_put_u32(msg,
12880 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
12881 iface_type)) {
12882 wpa_printf(MSG_ERROR,
12883 "%s: err in adding vendor_cmd and vendor_data",
12884 __func__);
12885 nlmsg_free(msg);
12886 return -1;
12887 }
12888 nla_nest_end(msg, params);
12889
Sunil8cd6f4d2022-06-28 18:40:46 +000012890 if (freq_list)
12891 os_memset(freq_list, 0, *num * sizeof(struct weighted_pcl));
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012892 ret = send_and_recv_resp(drv, msg, preferred_freq_info_handler, &param);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012893 if (ret) {
12894 wpa_printf(MSG_ERROR,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012895 "%s: err in send_and_recv_resp", __func__);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012896 return ret;
12897 }
12898
12899 *num = param.num;
12900
12901 for (i = 0; i < *num; i++) {
Sunil8cd6f4d2022-06-28 18:40:46 +000012902 wpa_printf(MSG_DEBUG,
12903 "nl80211: preferred_channel_list[%d]=%d[%d]:0x%x",
12904 i, freq_list[i].freq, freq_list[i].weight,
12905 freq_list[i].flag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012906 }
12907
12908 return 0;
12909}
12910
12911
12912static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
12913{
12914 struct i802_bss *bss = priv;
12915 struct wpa_driver_nl80211_data *drv = bss->drv;
12916 struct nl_msg *msg;
12917 int ret;
12918 struct nlattr *params;
12919
12920 if (!drv->set_prob_oper_freq)
12921 return -1;
12922
12923 wpa_printf(MSG_DEBUG,
12924 "nl80211: Set P2P probable operating freq %u for ifindex %d",
12925 freq, bss->ifindex);
12926
12927 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12928 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12929 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12930 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
12931 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
12932 nla_put_u32(msg,
12933 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
12934 QCA_IFACE_TYPE_P2P_CLIENT) ||
12935 nla_put_u32(msg,
12936 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
12937 freq)) {
12938 wpa_printf(MSG_ERROR,
12939 "%s: err in adding vendor_cmd and vendor_data",
12940 __func__);
12941 nlmsg_free(msg);
12942 return -1;
12943 }
12944 nla_nest_end(msg, params);
12945
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012946 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012947 msg = NULL;
12948 if (ret) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012949 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_cmd",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080012950 __func__);
12951 return ret;
12952 }
12953 nlmsg_free(msg);
12954 return 0;
12955}
12956
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070012957
12958static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
12959 unsigned int period, unsigned int interval,
12960 unsigned int count, const u8 *device_types,
12961 size_t dev_types_len,
12962 const u8 *ies, size_t ies_len)
12963{
12964 struct i802_bss *bss = priv;
12965 struct wpa_driver_nl80211_data *drv = bss->drv;
12966 struct nl_msg *msg;
12967 struct nlattr *container;
12968 int ret;
12969
12970 wpa_printf(MSG_DEBUG,
12971 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
12972 freq, period, interval, count);
12973
12974 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
12975 return -1;
12976
12977 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
12978 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
12979 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12980 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
12981 goto fail;
12982
12983 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
12984 if (!container)
12985 goto fail;
12986
12987 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
12988 freq) ||
12989 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
12990 period) ||
12991 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
12992 interval) ||
12993 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
12994 count) ||
12995 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
12996 dev_types_len, device_types) ||
12997 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
12998 ies_len, ies))
12999 goto fail;
13000
13001 nla_nest_end(msg, container);
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013002 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013003 msg = NULL;
13004 if (ret) {
13005 wpa_printf(MSG_DEBUG,
13006 "nl80211: Failed to send P2P Listen offload vendor command");
13007 goto fail;
13008 }
13009
13010 return 0;
13011
13012fail:
13013 nlmsg_free(msg);
13014 return -1;
13015}
13016
13017
13018static int nl80211_p2p_lo_stop(void *priv)
13019{
13020 struct i802_bss *bss = priv;
13021 struct wpa_driver_nl80211_data *drv = bss->drv;
13022 struct nl_msg *msg;
13023
13024 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
13025
13026 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
13027 return -1;
13028
13029 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13030 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13031 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13032 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
13033 nlmsg_free(msg);
13034 return -1;
13035 }
13036
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013037 return send_and_recv_cmd(drv, msg);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070013038}
13039
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013040
13041static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
13042{
13043 struct i802_bss *bss = priv;
13044 struct wpa_driver_nl80211_data *drv = bss->drv;
13045 struct nl_msg *msg;
13046 struct nlattr *params;
13047 int ret;
13048 u32 tdls_mode;
13049
13050 wpa_printf(MSG_DEBUG,
13051 "nl80211: Set TDKS mode: tdls_external_control=%d",
13052 tdls_external_control);
13053
13054 if (tdls_external_control == 1)
13055 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
13056 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
13057 else
13058 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
13059
13060 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13061 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13062 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13063 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
13064 goto fail;
13065
13066 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13067 if (!params)
13068 goto fail;
13069
13070 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
13071 tdls_mode))
13072 goto fail;
13073
13074 nla_nest_end(msg, params);
13075
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013076 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013077 msg = NULL;
13078 if (ret) {
13079 wpa_printf(MSG_ERROR,
13080 "nl80211: Set TDLS mode failed: ret=%d (%s)",
13081 ret, strerror(-ret));
13082 goto fail;
13083 }
13084 return 0;
13085fail:
13086 nlmsg_free(msg);
13087 return -1;
13088}
13089
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013090
13091#ifdef CONFIG_MBO
13092
13093static enum mbo_transition_reject_reason
13094nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
13095{
13096 switch (status) {
13097 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
13098 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
13099 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
13100 return MBO_TRANSITION_REJECT_REASON_DELAY;
13101 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
13102 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
13103 case QCA_STATUS_REJECT_LOW_RSSI:
13104 return MBO_TRANSITION_REJECT_REASON_RSSI;
13105 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
13106 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
13107 case QCA_STATUS_REJECT_UNKNOWN:
13108 default:
13109 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
13110 }
13111}
13112
13113
13114static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
13115 struct nlattr *tb[], int num)
13116{
13117 enum qca_wlan_btm_candidate_status status;
13118 char buf[50];
13119
13120 os_memcpy(candidate->bssid,
13121 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
13122 ETH_ALEN);
13123
13124 status = nla_get_u32(
13125 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
13126 candidate->is_accept = status == QCA_STATUS_ACCEPT;
13127 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
13128
13129 if (candidate->is_accept)
13130 os_snprintf(buf, sizeof(buf), "Accepted");
13131 else
13132 os_snprintf(buf, sizeof(buf),
13133 "Rejected, Reject_reason: %d",
13134 candidate->reject_reason);
13135 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
13136 num, MAC2STR(candidate->bssid), buf);
13137}
13138
13139
13140static int
13141nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
13142{
13143 struct wpa_bss_candidate_info *info = arg;
13144 struct candidate_list *candidate = info->candidates;
13145 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
13146 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
13147 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
13148 static struct nla_policy policy[
13149 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
13150 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
13151 .minlen = ETH_ALEN
13152 },
13153 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
13154 .type = NLA_U32,
13155 },
13156 };
13157 struct nlattr *attr;
13158 int rem;
13159 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
13160 u8 num;
13161
13162 num = info->num; /* number of candidates sent to driver */
13163 info->num = 0;
13164 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
13165 genlmsg_attrlen(gnlh, 0), NULL);
13166
13167 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
13168 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
13169 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
13170 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
13171 return NL_SKIP;
13172
13173 wpa_printf(MSG_DEBUG,
13174 "nl80211: WNM Candidate list received from driver");
13175 nla_for_each_nested(attr,
13176 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
13177 rem) {
13178 if (info->num >= num ||
13179 nla_parse_nested(
13180 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
13181 attr, policy) ||
13182 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
13183 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
13184 break;
13185
13186 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
13187
13188 candidate++;
13189 info->num++;
13190 }
13191
13192 return NL_SKIP;
13193}
13194
13195
13196static struct wpa_bss_candidate_info *
13197nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
13198{
13199 struct i802_bss *bss = priv;
13200 struct wpa_driver_nl80211_data *drv = bss->drv;
13201 struct nl_msg *msg;
13202 struct nlattr *attr, *attr1, *attr2;
13203 struct wpa_bss_candidate_info *info;
13204 u8 i;
13205 int ret;
13206 u8 *pos;
13207
13208 if (!drv->fetch_bss_trans_status)
13209 return NULL;
13210
13211 info = os_zalloc(sizeof(*info));
13212 if (!info)
13213 return NULL;
13214 /* Allocate memory for number of candidates sent to driver */
13215 info->candidates = os_calloc(params->n_candidates,
13216 sizeof(*info->candidates));
13217 if (!info->candidates) {
13218 os_free(info);
13219 return NULL;
13220 }
13221
13222 /* Copy the number of candidates being sent to driver. This is used in
13223 * nl80211_get_bss_transition_status_handler() to limit the number of
13224 * candidates that can be populated in info->candidates and will be
13225 * later overwritten with the actual number of candidates received from
13226 * the driver.
13227 */
13228 info->num = params->n_candidates;
13229
13230 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13231 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13232 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13233 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
13234 goto fail;
13235
13236 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13237 if (!attr)
13238 goto fail;
13239
13240 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
13241 params->mbo_transition_reason))
13242 goto fail;
13243
13244 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
13245 if (!attr1)
13246 goto fail;
13247
13248 wpa_printf(MSG_DEBUG,
13249 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
13250 params->mbo_transition_reason, params->n_candidates);
13251 pos = params->bssid;
13252 for (i = 0; i < params->n_candidates; i++) {
13253 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
13254 MAC2STR(pos));
13255 attr2 = nla_nest_start(msg, i);
13256 if (!attr2 ||
13257 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
13258 ETH_ALEN, pos))
13259 goto fail;
13260 pos += ETH_ALEN;
13261 nla_nest_end(msg, attr2);
13262 }
13263
13264 nla_nest_end(msg, attr1);
13265 nla_nest_end(msg, attr);
13266
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013267 ret = send_and_recv_resp(drv, msg,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013268 nl80211_get_bss_transition_status_handler,
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013269 info);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013270 msg = NULL;
13271 if (ret) {
13272 wpa_printf(MSG_ERROR,
13273 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
13274 ret, strerror(-ret));
13275 goto fail;
13276 }
13277 return info;
13278
13279fail:
13280 nlmsg_free(msg);
13281 os_free(info->candidates);
13282 os_free(info);
13283 return NULL;
13284}
13285
13286
13287/**
13288 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
13289 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
13290 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
13291 * Returns: 0 on success, -1 on failure
13292 */
13293static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
13294{
13295 struct i802_bss *bss = priv;
13296 struct wpa_driver_nl80211_data *drv = bss->drv;
13297 struct nl_msg *msg;
13298 struct nlattr *attr;
13299 int ret = -1;
13300
13301 if (!drv->set_wifi_conf_vendor_cmd_avail)
13302 return -1;
13303
13304 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13305 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13306 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13307 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
13308 goto fail;
13309
13310 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13311 if (!attr)
13312 goto fail;
13313
13314 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
13315 ignore_disallow);
13316 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
13317 ignore_disallow))
13318 goto fail;
13319
13320 nla_nest_end(msg, attr);
13321
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013322 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013323 msg = NULL;
13324 if (ret) {
13325 wpa_printf(MSG_ERROR,
13326 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
13327 ret, strerror(-ret));
13328 goto fail;
13329 }
13330
13331fail:
13332 nlmsg_free(msg);
13333 return ret;
13334}
13335
13336#endif /* CONFIG_MBO */
13337
Sunil Ravi89eba102022-09-13 21:04:37 -070013338
13339#ifdef CONFIG_PASN
13340
13341static int nl80211_send_pasn_resp(void *priv, struct pasn_auth *params)
13342{
13343 unsigned int i;
13344 struct i802_bss *bss = priv;
13345 struct nl_msg *msg = NULL;
13346 struct nlattr *nlpeers, *attr, *attr1;
13347 struct wpa_driver_nl80211_data *drv = bss->drv;
13348
13349 wpa_dbg(drv->ctx, MSG_DEBUG,
13350 "nl80211: PASN authentication response for %d entries",
13351 params->num_peers);
13352 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
13353 if (!msg ||
13354 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13355 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13356 QCA_NL80211_VENDOR_SUBCMD_PASN))
13357 goto fail;
13358
13359 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13360 if (!attr)
13361 goto fail;
13362
13363 nlpeers = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEERS);
13364 if (!nlpeers)
13365 goto fail;
13366
13367 for (i = 0; i < params->num_peers; i++) {
13368 attr1 = nla_nest_start(msg, i);
13369 if (!attr1 ||
13370 nla_put(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEER_SRC_ADDR,
13371 ETH_ALEN, params->peer[i].own_addr) ||
13372 nla_put(msg, QCA_WLAN_VENDOR_ATTR_PASN_PEER_MAC_ADDR,
13373 ETH_ALEN, params->peer[i].peer_addr))
13374 goto fail;
13375
Sunil Ravi77d572f2023-01-17 23:58:31 +000013376 if (params->peer[i].status == 0 &&
13377 nla_put_flag(msg,
13378 QCA_WLAN_VENDOR_ATTR_PASN_PEER_STATUS_SUCCESS))
13379 goto fail;
Sunil Ravi89eba102022-09-13 21:04:37 -070013380
13381 wpa_printf(MSG_DEBUG,
13382 "nl80211: Own address[%u]: " MACSTR
13383 " Peer address[%u]: " MACSTR " Status: %s",
13384 i, MAC2STR(params->peer[i].own_addr), i,
13385 MAC2STR(params->peer[i].peer_addr),
13386 params->peer[i].status ? "Fail" : "Success");
13387 nla_nest_end(msg, attr1);
13388 }
13389
13390 nla_nest_end(msg, nlpeers);
13391 nla_nest_end(msg, attr);
13392
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013393 return send_and_recv_cmd(drv, msg);
Sunil Ravi89eba102022-09-13 21:04:37 -070013394
13395fail:
13396 nlmsg_free(msg);
13397 return -1;
13398}
13399
13400
13401static u32 wpa_ltf_keyseed_len_to_sha_type(size_t len)
13402{
13403 if (len == SHA384_MAC_LEN)
13404 return QCA_WLAN_VENDOR_SHA_384;
13405 if (len == SHA256_MAC_LEN)
13406 return QCA_WLAN_VENDOR_SHA_256;
13407
13408 wpa_printf(MSG_ERROR, "nl80211: Unexpected LTF keyseed len %zu", len);
13409 return (u32) -1;
13410}
13411
13412
13413static int nl80211_set_secure_ranging_ctx(void *priv,
13414 struct secure_ranging_params *params)
13415{
13416 int ret;
13417 u32 suite;
13418 struct nlattr *attr;
13419 struct nl_msg *msg = NULL;
13420 struct i802_bss *bss = priv;
13421 struct wpa_driver_nl80211_data *drv = bss->drv;
13422
13423 /* Configure secure ranging context only to the drivers that support it.
13424 */
13425 if (!drv->secure_ranging_ctx_vendor_cmd_avail)
13426 return 0;
13427
13428 if (!params->peer_addr || !params->own_addr)
13429 return -1;
13430
13431 wpa_dbg(drv->ctx, MSG_DEBUG,
13432 "nl80211: Secure ranging context for " MACSTR,
13433 MAC2STR(params->peer_addr));
13434
13435 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
13436 if (!msg ||
13437 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
13438 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13439 QCA_NL80211_VENDOR_SUBCMD_SECURE_RANGING_CONTEXT))
13440 goto fail;
13441
13442 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
13443 if (!attr)
13444 goto fail;
13445
13446 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_PEER_MAC_ADDR,
13447 ETH_ALEN, params->peer_addr) ||
13448 nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_SRC_ADDR,
13449 ETH_ALEN, params->own_addr) ||
13450 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_ACTION,
13451 params->action))
13452 goto fail;
13453
13454 if (params->cipher) {
13455 suite = wpa_cipher_to_cipher_suite(params->cipher);
13456 if (!suite ||
13457 nla_put_u32(msg,
13458 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_CIPHER,
13459 suite))
13460 goto fail;
13461 }
13462
13463 if (params->tk_len && params->tk) {
13464 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_TK,
13465 params->tk_len, params->tk))
13466 goto fail;
13467 wpa_hexdump_key(MSG_DEBUG, "nl80211: TK",
13468 params->tk, params->tk_len);
13469 }
13470
13471 if (params->ltf_keyseed_len && params->ltf_keyseed) {
13472 u32 sha_type = wpa_ltf_keyseed_len_to_sha_type(
13473 params->ltf_keyseed_len);
13474
13475 if (sha_type == (u32) -1 ||
13476 nla_put_u32(
13477 msg,
13478 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_SHA_TYPE,
13479 sha_type) ||
13480 nla_put(msg,
13481 QCA_WLAN_VENDOR_ATTR_SECURE_RANGING_CTX_LTF_KEYSEED,
13482 params->ltf_keyseed_len, params->ltf_keyseed))
13483 goto fail;
13484 wpa_hexdump_key(MSG_DEBUG, "nl80211: LTF keyseed",
13485 params->ltf_keyseed, params->ltf_keyseed_len);
13486 }
13487 nla_nest_end(msg, attr);
13488
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013489 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi89eba102022-09-13 21:04:37 -070013490 if (ret)
13491 wpa_printf(MSG_DEBUG,
13492 "nl80211: Set secure ranging context failed: ret=%d (%s)",
13493 ret, strerror(-ret));
13494 return ret;
13495fail:
13496 nlmsg_free(msg);
13497 return -1;
13498}
13499
13500#endif /* CONFIG_PASN */
13501
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013502#endif /* CONFIG_DRIVER_NL80211_QCA */
13503
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013504
13505#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
13506static int wpa_driver_do_broadcom_acs(struct wpa_driver_nl80211_data *drv,
13507 struct drv_acs_params *params)
13508{
13509 struct nl_msg *msg;
13510 struct nlattr *data;
13511 int freq_list_len;
13512 int ret = -1;
13513
13514 freq_list_len = int_array_len(params->freq_list);
13515 wpa_printf(MSG_DEBUG, "%s: freq_list_len=%d",
13516 __func__, freq_list_len);
13517
13518 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
13519 if (!msg ||
13520 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
13521 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
13522 BRCM_VENDOR_SCMD_ACS) ||
13523 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
13524 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HW_MODE, params->hw_mode) ||
13525 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HT_ENABLED,
13526 params->ht_enabled) ||
13527 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_HT40_ENABLED,
13528 params->ht40_enabled) ||
13529 nla_put_u8(msg, BRCM_VENDOR_ATTR_ACS_VHT_ENABLED,
13530 params->vht_enabled) ||
13531 nla_put_u16(msg, BRCM_VENDOR_ATTR_ACS_CHWIDTH, params->ch_width) ||
13532 (freq_list_len > 0 &&
13533 nla_put(msg, BRCM_VENDOR_ATTR_ACS_FREQ_LIST,
13534 sizeof(int) * freq_list_len, params->freq_list)))
13535 goto fail;
13536 nla_nest_end(msg, data);
13537
13538 wpa_printf(MSG_DEBUG,
13539 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d",
13540 params->hw_mode, params->ht_enabled, params->ht40_enabled,
13541 params->vht_enabled, params->ch_width);
13542
13543 ret = send_and_recv_cmd(drv, msg);
13544 if (ret) {
13545 wpa_printf(MSG_ERROR,
13546 "nl80211: BRCM Failed to invoke driver ACS function: %s",
13547 strerror(errno));
13548 }
13549
13550 msg = NULL;
13551fail:
13552 nlmsg_free(msg);
13553 return ret;
13554}
13555#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
13556
13557
Hai Shalomc1a21442022-02-04 13:43:00 -080013558static int nl80211_do_acs(void *priv, struct drv_acs_params *params)
13559{
Andy Kuoaba17c12022-04-14 16:05:31 +080013560#if defined(CONFIG_DRIVER_NL80211_QCA) || defined(CONFIG_DRIVER_NL80211_BRCM) \
13561 || defined(CONFIG_DRIVER_NL80211_SYNA)
Hai Shalomc1a21442022-02-04 13:43:00 -080013562 struct i802_bss *bss = priv;
13563 struct wpa_driver_nl80211_data *drv = bss->drv;
Andy Kuoaba17c12022-04-14 16:05:31 +080013564#endif /* CONFIG_DRIVER_NL80211_QCA || CONFIG_DRIVER_NL80211_BRCM \
13565 || defined(CONFIG_DRIVER_NL80211_SYNA) */
Hai Shalomc1a21442022-02-04 13:43:00 -080013566
13567#ifdef CONFIG_DRIVER_NL80211_QCA
13568 if (drv->qca_do_acs)
13569 return nl80211_qca_do_acs(drv, params);
13570#endif /* CONFIG_DRIVER_NL80211_QCA */
13571
Andy Kuoaba17c12022-04-14 16:05:31 +080013572#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Hai Shalomc1a21442022-02-04 13:43:00 -080013573 if (drv->brcm_do_acs)
13574 return wpa_driver_do_broadcom_acs(drv, params);
Andy Kuoaba17c12022-04-14 16:05:31 +080013575#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Hai Shalomc1a21442022-02-04 13:43:00 -080013576
13577 return -1;
13578}
13579
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080013580
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013581static int nl80211_write_to_file(const char *name, unsigned int val)
13582{
13583 int fd, len;
13584 char tmp[128];
Hai Shalomc3565922019-10-28 11:58:20 -070013585 int ret = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013586
13587 fd = open(name, O_RDWR);
13588 if (fd < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -070013589 int level;
13590 /*
13591 * Flags may not exist on older kernels, or while we're tearing
13592 * down a disappearing device.
13593 */
13594 if (errno == ENOENT) {
13595 ret = 0;
13596 level = MSG_DEBUG;
13597 } else {
13598 ret = -1;
13599 level = MSG_ERROR;
13600 }
13601 wpa_printf(level, "nl80211: Failed to open %s: %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013602 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070013603 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013604 }
13605
13606 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
13607 len = write(fd, tmp, len);
Hai Shalomc3565922019-10-28 11:58:20 -070013608 if (len < 0) {
13609 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013610 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
13611 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070013612 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013613 close(fd);
13614
Hai Shalomc3565922019-10-28 11:58:20 -070013615 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013616}
13617
13618
13619static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
13620{
13621 struct i802_bss *bss = priv;
13622 char path[128];
13623 int ret;
13624
Hai Shalom60840252021-02-19 19:02:11 -080013625 /* P2P-Device has no netdev that can (or should) be configured here */
13626 if (nl80211_get_ifmode(bss) == NL80211_IFTYPE_P2P_DEVICE)
13627 return 0;
13628
Dmitry Shmidt849734c2016-05-27 09:59:01 -070013629 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
13630 filter_flags);
13631
13632 /* Configure filtering of unicast frame encrypted using GTK */
13633 ret = os_snprintf(path, sizeof(path),
13634 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
13635 bss->ifname);
13636 if (os_snprintf_error(sizeof(path), ret))
13637 return -1;
13638
13639 ret = nl80211_write_to_file(path,
13640 !!(filter_flags &
13641 WPA_DATA_FRAME_FILTER_FLAG_GTK));
13642 if (ret) {
13643 wpa_printf(MSG_ERROR,
13644 "nl80211: Failed to set IPv4 unicast in multicast filter");
13645 return ret;
13646 }
13647
13648 os_snprintf(path, sizeof(path),
13649 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
13650 bss->ifname);
13651 ret = nl80211_write_to_file(path,
13652 !!(filter_flags &
13653 WPA_DATA_FRAME_FILTER_FLAG_GTK));
13654
13655 if (ret) {
13656 wpa_printf(MSG_ERROR,
13657 "nl80211: Failed to set IPv6 unicast in multicast filter");
13658 return ret;
13659 }
13660
13661 /* Configure filtering of unicast frame encrypted using GTK */
13662 os_snprintf(path, sizeof(path),
13663 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
13664 bss->ifname);
13665 ret = nl80211_write_to_file(path,
13666 !!(filter_flags &
13667 WPA_DATA_FRAME_FILTER_FLAG_ARP));
13668 if (ret) {
13669 wpa_printf(MSG_ERROR,
13670 "nl80211: Failed set gratuitous ARP filter");
13671 return ret;
13672 }
13673
13674 /* Configure filtering of IPv6 NA frames */
13675 os_snprintf(path, sizeof(path),
13676 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
13677 bss->ifname);
13678 ret = nl80211_write_to_file(path,
13679 !!(filter_flags &
13680 WPA_DATA_FRAME_FILTER_FLAG_NA));
13681 if (ret) {
13682 wpa_printf(MSG_ERROR,
13683 "nl80211: Failed to set unsolicited NA filter");
13684 return ret;
13685 }
13686
13687 return 0;
13688}
13689
13690
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070013691static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
13692 const u8 **ext_capa, const u8 **ext_capa_mask,
13693 unsigned int *ext_capa_len)
13694{
13695 struct i802_bss *bss = priv;
13696 struct wpa_driver_nl80211_data *drv = bss->drv;
13697 enum nl80211_iftype nlmode;
13698 unsigned int i;
13699
13700 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
13701 return -1;
13702
13703 nlmode = wpa_driver_nl80211_if_type(type);
13704
13705 /* By default, use the per-radio values */
13706 *ext_capa = drv->extended_capa;
13707 *ext_capa_mask = drv->extended_capa_mask;
13708 *ext_capa_len = drv->extended_capa_len;
13709
13710 /* Replace the default value if a per-interface type value exists */
Sunil Ravi2a14cf12023-11-21 00:54:38 +000013711 for (i = 0; i < drv->num_iface_capa; i++) {
13712 if (nlmode == drv->iface_capa[i].iftype) {
13713 *ext_capa = drv->iface_capa[i].ext_capa;
13714 *ext_capa_mask = drv->iface_capa[i].ext_capa_mask;
13715 *ext_capa_len = drv->iface_capa[i].ext_capa_len;
13716 break;
13717 }
13718 }
13719
13720 return 0;
13721}
13722
13723
13724static int nl80211_get_mld_capab(void *priv, enum wpa_driver_if_type type,
13725 u16 *eml_capa, u16 *mld_capa_and_ops)
13726{
13727 struct i802_bss *bss = priv;
13728 struct wpa_driver_nl80211_data *drv = bss->drv;
13729 enum nl80211_iftype nlmode;
13730 unsigned int i;
13731
13732 if (!eml_capa || !mld_capa_and_ops)
13733 return -1;
13734
13735 nlmode = wpa_driver_nl80211_if_type(type);
13736
13737 /* By default, set to zero */
13738 *eml_capa = 0;
13739 *mld_capa_and_ops = 0;
13740
13741 /* Replace the default value if a per-interface type value exists */
13742 for (i = 0; i < drv->num_iface_capa; i++) {
13743 if (nlmode == drv->iface_capa[i].iftype) {
13744 *eml_capa = drv->iface_capa[i].eml_capa;
13745 *mld_capa_and_ops =
13746 drv->iface_capa[i].mld_capa_and_ops;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070013747 break;
13748 }
13749 }
13750
13751 return 0;
13752}
13753
13754
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013755static int nl80211_update_connection_params(
13756 void *priv, struct wpa_driver_associate_params *params,
13757 enum wpa_drv_update_connect_params_mask mask)
13758{
13759 struct i802_bss *bss = priv;
13760 struct wpa_driver_nl80211_data *drv = bss->drv;
13761 struct nl_msg *msg;
13762 int ret = -1;
13763 enum nl80211_auth_type type;
13764
Hai Shalomc3565922019-10-28 11:58:20 -070013765 /* Update Connection Params is intended for drivers that implement
13766 * internal SME and expect these updated connection params from
13767 * wpa_supplicant. Do not send this request for the drivers using
13768 * SME from wpa_supplicant.
13769 */
13770 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
13771 return 0;
13772
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013773 /* Handle any connection param update here which might receive kernel handling in future */
Winnie Chen4138eec2022-11-10 16:32:53 +080013774#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013775 if (mask & WPA_DRV_UPDATE_TD_POLICY) {
13776 ret = nl80211_set_td_policy(priv, params->td_policy);
13777 if (ret) {
13778 wpa_dbg(drv->ctx, MSG_DEBUG,
13779 "nl80211: Update connect params command failed: ret=%d (%s)",
13780 ret, strerror(-ret));
13781 }
13782 return ret;
13783 }
Winnie Chen4138eec2022-11-10 16:32:53 +080013784#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013785
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013786 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
13787 if (!msg)
13788 goto fail;
13789
13790 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
13791 drv->ifindex);
13792
13793 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
13794 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
13795 params->wpa_ie))
13796 goto fail;
13797 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
13798 params->wpa_ie_len);
13799 }
13800
13801 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
13802 type = get_nl_auth_type(params->auth_alg);
13803 if (type == NL80211_AUTHTYPE_MAX ||
13804 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
13805 goto fail;
13806 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
13807 }
13808
13809 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
13810 nl80211_put_fils_connect_params(drv, params, msg))
13811 goto fail;
13812
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013813 ret = send_and_recv_cmd(drv, msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070013814 msg = NULL;
13815 if (ret)
13816 wpa_dbg(drv->ctx, MSG_DEBUG,
13817 "nl80211: Update connect params command failed: ret=%d (%s)",
13818 ret, strerror(-ret));
13819
13820fail:
13821 nlmsg_free(msg);
13822 return ret;
13823}
13824
13825
Roshan Pius3a1667e2018-07-03 15:17:14 -070013826static int nl80211_send_external_auth_status(void *priv,
13827 struct external_auth *params)
13828{
13829 struct i802_bss *bss = priv;
13830 struct wpa_driver_nl80211_data *drv = bss->drv;
13831 struct nl_msg *msg = NULL;
13832 int ret = -1;
13833
Hai Shalom5f92bc92019-04-18 11:54:11 -070013834 /* External auth command/status is intended for drivers that implement
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080013835 * internal SME but want to offload authentication processing (e.g.,
13836 * SAE) to hostapd/wpa_supplicant. Do not send the status to drivers
Hai Shalom5f92bc92019-04-18 11:54:11 -070013837 * which do not support AP SME or use wpa_supplicant/hostapd SME.
13838 */
Hai Shalom81f62d82019-07-22 12:10:00 -070013839 if ((is_ap_interface(drv->nlmode) && !bss->drv->device_ap_sme) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070013840 (drv->capa.flags & WPA_DRIVER_FLAGS_SME))
13841 return -1;
13842
Roshan Pius3a1667e2018-07-03 15:17:14 -070013843 wpa_dbg(drv->ctx, MSG_DEBUG,
13844 "nl80211: External auth status: %u", params->status);
13845
13846 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
13847 if (!msg ||
13848 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070013849 (params->ssid && params->ssid_len &&
13850 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
13851 (params->pmkid &&
13852 nla_put(msg, NL80211_ATTR_PMKID, PMKID_LEN, params->pmkid)) ||
13853 (params->bssid &&
13854 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid)))
Roshan Pius3a1667e2018-07-03 15:17:14 -070013855 goto fail;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013856 ret = send_and_recv_cmd(drv, msg);
Roshan Pius3a1667e2018-07-03 15:17:14 -070013857 msg = NULL;
13858 if (ret) {
13859 wpa_printf(MSG_DEBUG,
13860 "nl80211: External Auth status update failed: ret=%d (%s)",
13861 ret, strerror(-ret));
13862 goto fail;
13863 }
13864fail:
13865 nlmsg_free(msg);
13866 return ret;
13867}
13868
13869
Hai Shalom74f70d42019-02-11 14:42:39 -080013870static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
13871 int val)
13872{
13873 struct i802_bss *bss = priv;
13874 struct wpa_driver_nl80211_data *drv = bss->drv;
13875 struct nl_msg *msg;
13876 int ret = -ENOBUFS;
13877
13878 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
13879 val ? "Enable" : "Disable", bridge_ifname);
13880
13881 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
13882 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
13883 goto fail;
13884
13885 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
13886 if (linux_br_del_if(drv->global->ioctl_sock,
13887 bridge_ifname, bss->ifname)) {
13888 wpa_printf(MSG_ERROR,
13889 "nl80211: Failed to remove interface %s from bridge %s",
13890 bss->ifname, bridge_ifname);
13891 return -1;
13892 }
13893 bss->added_if_into_bridge = 0;
13894 }
13895
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013896 ret = send_and_recv_cmd(drv, msg);
Hai Shalom74f70d42019-02-11 14:42:39 -080013897 msg = NULL;
Hai Shalom60840252021-02-19 19:02:11 -080013898 if (ret && val && nl80211_get_4addr(bss) == 1) {
13899 wpa_printf(MSG_DEBUG,
13900 "nl80211: 4addr mode was already enabled");
13901 ret = 0;
13902 }
Hai Shalom74f70d42019-02-11 14:42:39 -080013903 if (!ret) {
13904 if (bridge_ifname[0] && val &&
13905 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
13906 return -1;
13907 return 0;
13908 }
13909
13910fail:
13911 nlmsg_free(msg);
13912 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
13913
13914 return ret;
13915}
13916
13917
Hai Shalome21d4e82020-04-29 16:34:06 -070013918#ifdef CONFIG_DPP
13919static int nl80211_dpp_listen(void *priv, bool enable)
13920{
13921 struct i802_bss *bss = priv;
13922 struct wpa_driver_nl80211_data *drv = bss->drv;
13923 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
13924 struct nl_sock *handle;
13925
13926 if (!drv->multicast_registrations || !bss->nl_mgmt)
13927 return 0; /* cannot do more than hope broadcast RX works */
13928
13929 wpa_printf(MSG_DEBUG,
13930 "nl80211: Update DPP Public Action frame registration (%s multicast RX)",
13931 enable ? "enable" : "disable");
13932 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
13933 return nl80211_register_frame(bss, handle, type,
13934 (u8 *) "\x04\x09\x50\x6f\x9a\x1a", 6,
13935 enable);
13936}
13937#endif /* CONFIG_DPP */
13938
Winnie Chen4138eec2022-11-10 16:32:53 +080013939#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013940static int nl80211_set_td_policy(void *priv, u32 td_policy)
13941{
13942 struct i802_bss *bss = priv;
13943 struct wpa_driver_nl80211_data *drv = bss->drv;
13944 struct nl_msg *msg;
13945 int ret;
13946 struct nlattr *params;
13947
13948 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
13949 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_BRCM) ||
13950 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, BRCM_VENDOR_SCMD_SET_TD_POLICY) ||
13951 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
13952 (nla_put_u32(msg, BRCM_ATTR_DRIVER_TD_POLICY, td_policy))) {
13953 nl80211_nlmsg_clear(msg);
13954 nlmsg_free(msg);
13955 return -ENOBUFS;
13956 }
13957 nla_nest_end(msg, params);
13958 wpa_printf(MSG_DEBUG, "nl80211: Transition Disable Policy %d\n", td_policy);
13959
Sunil Ravib0ac25f2024-07-12 01:42:03 +000013960 ret = send_and_recv_cmd(drv, msg);
Vinayak Yadawade62409f2022-01-20 12:32:07 +053013961 if (ret) {
13962 wpa_printf(MSG_DEBUG, "nl80211: Transition Disable setting failed: ret=%d (%s)",
13963 ret, strerror(-ret));
13964 }
13965
13966 return ret;
13967}
Winnie Chen4138eec2022-11-10 16:32:53 +080013968#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Hai Shalome21d4e82020-04-29 16:34:06 -070013969
Sunil Ravi99c035e2024-07-12 01:42:03 +000013970static int nl80211_link_add(void *priv, u8 link_id, const u8 *addr,
13971 void *bss_ctx)
Sunil Ravi036cec52023-03-29 11:35:17 -070013972{
13973 struct i802_bss *bss = priv;
13974 struct wpa_driver_nl80211_data *drv = bss->drv;
13975 struct nl_msg *msg;
Sunil Ravi036cec52023-03-29 11:35:17 -070013976 int ret;
13977
13978 wpa_printf(MSG_DEBUG, "nl80211: MLD: add link_id=%u, addr=" MACSTR,
13979 link_id, MAC2STR(addr));
13980
13981 if (drv->nlmode != NL80211_IFTYPE_AP) {
13982 wpa_printf(MSG_DEBUG,
13983 "nl80211: MLD: cannot add link to iftype=%u",
13984 drv->nlmode);
13985 return -EINVAL;
13986 }
13987
Sunil Ravi99c035e2024-07-12 01:42:03 +000013988 if (link_id >= MAX_NUM_MLD_LINKS) {
13989 wpa_printf(MSG_DEBUG,
13990 "nl80211: invalid link_id=%u", link_id);
Sunil Ravi036cec52023-03-29 11:35:17 -070013991 return -EINVAL;
13992 }
13993
Sunil Ravi99c035e2024-07-12 01:42:03 +000013994 if (bss->valid_links & BIT(link_id)) {
13995 wpa_printf(MSG_DEBUG,
13996 "nl80211: MLD: Link %u already set", link_id);
13997 return -EINVAL;
Sunil Ravi036cec52023-03-29 11:35:17 -070013998 }
13999
Sunil Ravi99c035e2024-07-12 01:42:03 +000014000 if (!bss->valid_links) {
14001 /* Becoming MLD, verify we were not beaconing */
Sunil Ravi036cec52023-03-29 11:35:17 -070014002 if (bss->flink->beacon_set) {
14003 wpa_printf(MSG_DEBUG, "nl80211: BSS already beaconing");
14004 return -EINVAL;
14005 }
Sunil Ravi036cec52023-03-29 11:35:17 -070014006 }
14007
14008 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ADD_LINK);
14009 if (!msg ||
14010 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id) ||
14011 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
14012 nlmsg_free(msg);
14013 return -ENOBUFS;
14014 }
14015
Sunil Ravib0ac25f2024-07-12 01:42:03 +000014016 ret = send_and_recv_cmd(drv, msg);
Sunil Ravi036cec52023-03-29 11:35:17 -070014017 if (ret) {
14018 wpa_printf(MSG_DEBUG, "nl80211: add link failed. ret=%d (%s)",
14019 ret, strerror(-ret));
14020 return ret;
14021 }
14022
Sunil Ravi99c035e2024-07-12 01:42:03 +000014023 os_memcpy(bss->links[link_id].addr, addr, ETH_ALEN);
Sunil Ravi036cec52023-03-29 11:35:17 -070014024
Sunil Ravi99c035e2024-07-12 01:42:03 +000014025 /* The new link is the first one, make it the default */
14026 if (!bss->valid_links)
14027 bss->flink = &bss->links[link_id];
Sunil Ravi036cec52023-03-29 11:35:17 -070014028
Sunil Ravi99c035e2024-07-12 01:42:03 +000014029 bss->valid_links |= BIT(link_id);
14030 bss->links[link_id].ctx = bss_ctx;
14031
14032 wpa_printf(MSG_DEBUG, "nl80211: MLD: valid_links=0x%04x",
14033 bss->valid_links);
Sunil Ravi036cec52023-03-29 11:35:17 -070014034 return 0;
14035}
14036
14037
Sunil Ravi99c035e2024-07-12 01:42:03 +000014038#ifdef CONFIG_IEEE80211BE
14039static int wpa_driver_nl80211_link_sta_remove(void *priv, u8 link_id,
14040 const u8 *addr)
14041{
14042 struct i802_bss *bss = priv;
14043 struct wpa_driver_nl80211_data *drv = bss->drv;
14044 struct nl_msg *msg;
14045 int ret;
14046
14047 if (!(bss->valid_links & BIT(link_id)))
14048 return -ENOLINK;
14049
14050 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_REMOVE_LINK_STA)) ||
14051 nla_put(msg, NL80211_ATTR_MLD_ADDR, ETH_ALEN, addr) ||
14052 nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id)) {
14053 nlmsg_free(msg);
14054 return -ENOBUFS;
14055 }
14056
14057 ret = send_and_recv_cmd(drv, msg);
14058 wpa_printf(MSG_DEBUG,
14059 "nl80211: link_sta_remove -> REMOVE_LINK_STA on link_id %u from MLD STA "
14060 MACSTR ", from %s --> %d (%s)",
14061 link_id, MAC2STR(addr), bss->ifname, ret, strerror(-ret));
14062
14063 return ret;
14064}
14065#endif /* CONFIG_IEEE80211BE */
14066
14067
Hai Shalomc1a21442022-02-04 13:43:00 -080014068#ifdef CONFIG_TESTING_OPTIONS
14069
14070static int testing_nl80211_register_frame(void *priv, u16 type,
14071 const u8 *match, size_t match_len,
14072 bool multicast)
14073{
14074 struct i802_bss *bss = priv;
14075 struct nl_sock *handle;
14076
14077 if (!bss->nl_mgmt)
14078 return -1;
14079 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
14080 return nl80211_register_frame(bss, handle, type, match, match_len,
14081 multicast);
14082}
14083
14084
14085static int testing_nl80211_radio_disable(void *priv, int disabled)
14086{
14087 struct i802_bss *bss = priv;
14088 struct wpa_driver_nl80211_data *drv = bss->drv;
14089
14090 /* For now, this is supported only partially in station mode with
14091 * SME-in-wpa_supplicant case where the NL80211_ATTR_LOCAL_STATE_CHANGE
14092 * attribute can be used to avoid sending out the Deauthentication frame
14093 * to the currently associated AP. */
14094
14095 if (!disabled)
14096 return 0;
14097
14098 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
14099 return -1;
14100
14101 if (!drv->associated)
14102 return 0;
14103
14104 return wpa_driver_nl80211_mlme(drv, drv->bssid,
14105 NL80211_CMD_DEAUTHENTICATE,
14106 WLAN_REASON_PREV_AUTH_NOT_VALID, 1,
14107 drv->first_bss);
14108}
14109
14110#endif /* CONFIG_TESTING_OPTIONS */
14111
14112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014113const struct wpa_driver_ops wpa_driver_nl80211_ops = {
14114 .name = "nl80211",
14115 .desc = "Linux nl80211/cfg80211",
14116 .get_bssid = wpa_driver_nl80211_get_bssid,
14117 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014118 .set_key = driver_nl80211_set_key,
14119 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014120 .sched_scan = wpa_driver_nl80211_sched_scan,
14121 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Sunil Ravi99c035e2024-07-12 01:42:03 +000014122 .get_scan_results = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080014123 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014124 .deauthenticate = driver_nl80211_deauthenticate,
14125 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014126 .associate = wpa_driver_nl80211_associate,
14127 .global_init = nl80211_global_init,
14128 .global_deinit = nl80211_global_deinit,
14129 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014130 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014131 .get_capa = wpa_driver_nl80211_get_capa,
14132 .set_operstate = wpa_driver_nl80211_set_operstate,
14133 .set_supp_port = wpa_driver_nl80211_set_supp_port,
14134 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080014135 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014136 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070014137 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014138 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014139 .if_remove = driver_nl80211_if_remove,
14140 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014141 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014142 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014143 .sta_remove = driver_nl80211_sta_remove,
Hai Shalomfdcde762020-04-02 11:19:20 -070014144 .tx_control_port = nl80211_tx_control_port,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014145 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
14146 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Hai Shalom81f62d82019-07-22 12:10:00 -070014147 .sta_set_airtime_weight = driver_nl80211_sta_set_airtime_weight,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014148 .hapd_init = i802_init,
14149 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070014150 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014151 .get_seqnum = i802_get_seqnum,
14152 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014153 .get_inact_sec = i802_get_inact_sec,
14154 .sta_clear_stats = i802_sta_clear_stats,
14155 .set_rts = i802_set_rts,
14156 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014157 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014158 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014159 .sta_deauth = i802_sta_deauth,
14160 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014161 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014162 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014163 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014164 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
14165 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
14166 .cancel_remain_on_channel =
14167 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080014168 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014169 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070014170 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014171 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014172 .signal_monitor = nl80211_signal_monitor,
14173 .signal_poll = nl80211_signal_poll,
Sunil Ravi89eba102022-09-13 21:04:37 -070014174 .mlo_signal_poll = nl80211_mlo_signal_poll,
Hai Shalom74f70d42019-02-11 14:42:39 -080014175 .channel_info = nl80211_channel_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014176 .set_param = nl80211_set_param,
14177 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070014178 .add_pmkid = nl80211_add_pmkid,
14179 .remove_pmkid = nl80211_remove_pmkid,
14180 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014181 .set_rekey_info = nl80211_set_rekey_info,
14182 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014183 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070014184 .start_dfs_cac = nl80211_start_radar_detection,
14185 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014186#ifdef CONFIG_TDLS
14187 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
14188 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014189 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
14190 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014191#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070014192 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Hai Shalom81f62d82019-07-22 12:10:00 -070014193 .update_dh_ie = nl80211_update_dh_ie,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070014194 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070014195 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070014196 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080014197 .switch_channel = nl80211_switch_channel,
Sunil Ravia04bd252022-05-02 22:54:18 -070014198#ifdef CONFIG_IEEE80211AX
14199 .switch_color = nl80211_switch_color,
14200#endif /* CONFIG_IEEE80211AX */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014201#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070014202 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014203 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070014204 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080014205#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070014206#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070014207#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070014208 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070014209#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080014210#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080014211 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080014212 .set_qos_map = nl80211_set_qos_map,
Hai Shalomfdcde762020-04-02 11:19:20 -070014213 .get_wowlan = nl80211_get_wowlan,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070014214 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070014215 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014216#ifdef CONFIG_MESH
14217 .init_mesh = wpa_driver_nl80211_init_mesh,
14218 .join_mesh = wpa_driver_nl80211_join_mesh,
14219 .leave_mesh = wpa_driver_nl80211_leave_mesh,
Hai Shalom81f62d82019-07-22 12:10:00 -070014220 .probe_mesh_link = nl80211_probe_mesh_link,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080014221#endif /* CONFIG_MESH */
14222 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
14223 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
14224 .br_port_set_attr = wpa_driver_br_port_set_attr,
14225 .br_set_net_param = wpa_driver_br_set_net_param,
14226 .add_tx_ts = nl80211_add_ts,
14227 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070014228 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014229#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070014230 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070014231 .disable_fils = nl80211_disable_fils,
Ravi Joshie6ccb162015-07-16 17:45:41 -070014232 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014233 .get_pref_freq_list = nl80211_get_pref_freq_list,
14234 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070014235 .p2p_lo_start = nl80211_p2p_lo_start,
14236 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070014237 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080014238 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014239#ifdef CONFIG_MBO
14240 .get_bss_transition_status = nl80211_get_bss_transition_status,
14241 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
14242#endif /* CONFIG_MBO */
Hai Shalom899fcc72020-10-19 14:38:18 -070014243 .set_bssid_tmp_disallow = nl80211_set_bssid_tmp_disallow,
Hai Shalomc3565922019-10-28 11:58:20 -070014244 .add_sta_node = nl80211_add_sta_node,
Sunil Ravi89eba102022-09-13 21:04:37 -070014245#ifdef CONFIG_PASN
14246 .send_pasn_resp = nl80211_send_pasn_resp,
14247 .set_secure_ranging_ctx = nl80211_set_secure_ranging_ctx,
14248#endif /* CONFIG_PASN */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014249#endif /* CONFIG_DRIVER_NL80211_QCA */
Hai Shalomc1a21442022-02-04 13:43:00 -080014250 .do_acs = nl80211_do_acs,
Dmitry Shmidt849734c2016-05-27 09:59:01 -070014251 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070014252 .get_ext_capab = nl80211_get_ext_capab,
Sunil Ravi2a14cf12023-11-21 00:54:38 +000014253 .get_mld_capab = nl80211_get_mld_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070014254 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070014255 .send_external_auth_status = nl80211_send_external_auth_status,
Hai Shalom74f70d42019-02-11 14:42:39 -080014256 .set_4addr_mode = nl80211_set_4addr_mode,
Hai Shalome21d4e82020-04-29 16:34:06 -070014257#ifdef CONFIG_DPP
14258 .dpp_listen = nl80211_dpp_listen,
14259#endif /* CONFIG_DPP */
Sunil Ravi89eba102022-09-13 21:04:37 -070014260 .get_sta_mlo_info = nl80211_get_sta_mlo_info,
Sunil Ravi036cec52023-03-29 11:35:17 -070014261 .link_add = nl80211_link_add,
Sunil Ravi99c035e2024-07-12 01:42:03 +000014262#ifdef CONFIG_IEEE80211BE
14263 .link_remove = driver_nl80211_link_remove,
14264 .is_drv_shared = nl80211_is_drv_shared,
14265 .link_sta_remove = wpa_driver_nl80211_link_sta_remove,
14266#endif /* CONFIG_IEEE80211BE */
Hai Shalomc1a21442022-02-04 13:43:00 -080014267#ifdef CONFIG_TESTING_OPTIONS
14268 .register_frame = testing_nl80211_register_frame,
14269 .radio_disable = testing_nl80211_radio_disable,
14270#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014271};