blob: 51674f0f2e87070300c1d7fcd362d7033b7c9aed [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>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include <fcntl.h>
16#include <net/if.h>
17#include <netlink/genl/genl.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include <netlink/genl/ctrl.h>
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070019#ifdef CONFIG_LIBNL3_ROUTE
20#include <netlink/route/neighbour.h>
21#endif /* CONFIG_LIBNL3_ROUTE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "eloop.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080028#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070029#include "common/qca-vendor-attr.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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080034#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080039#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041
Hai Shalom74f70d42019-02-11 14:42:39 -080042#ifndef NETLINK_CAP_ACK
43#define NETLINK_CAP_ACK 10
44#endif /* NETLINK_CAP_ACK */
Hai Shalom39ba6fc2019-01-22 12:40:38 -080045/* support for extack if compilation headers are too old */
46#ifndef NETLINK_EXT_ACK
47#define NETLINK_EXT_ACK 11
48enum nlmsgerr_attrs {
49 NLMSGERR_ATTR_UNUSED,
50 NLMSGERR_ATTR_MSG,
51 NLMSGERR_ATTR_OFFS,
52 NLMSGERR_ATTR_COOKIE,
53
54 __NLMSGERR_ATTR_MAX,
55 NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
56};
57#endif
58#ifndef NLM_F_CAPPED
59#define NLM_F_CAPPED 0x100
60#endif
61#ifndef NLM_F_ACK_TLVS
62#define NLM_F_ACK_TLVS 0x200
63#endif
64#ifndef SOL_NETLINK
65#define SOL_NETLINK 270
66#endif
67
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068
Dmitry Shmidt54605472013-11-08 11:10:19 -080069#ifdef ANDROID
70/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080071#undef nl_socket_set_nonblocking
72#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080073
Dmitry Shmidt54605472013-11-08 11:10:19 -080074#endif /* ANDROID */
75
76
Hai Shalomfdcde762020-04-02 11:19:20 -070077static struct nl_sock * nl_create_handle(struct nl_cb *cb, const char *dbg)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080078{
Hai Shalomfdcde762020-04-02 11:19:20 -070079 struct nl_sock *handle;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080080
Hai Shalomfdcde762020-04-02 11:19:20 -070081 handle = nl_socket_alloc_cb(cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080082 if (handle == NULL) {
83 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
84 "callbacks (%s)", dbg);
85 return NULL;
86 }
87
88 if (genl_connect(handle)) {
89 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
90 "netlink (%s)", dbg);
Hai Shalomfdcde762020-04-02 11:19:20 -070091 nl_socket_free(handle);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092 return NULL;
93 }
94
95 return handle;
96}
97
98
Hai Shalomfdcde762020-04-02 11:19:20 -070099static void nl_destroy_handles(struct nl_sock **handle)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800100{
101 if (*handle == NULL)
102 return;
Hai Shalomfdcde762020-04-02 11:19:20 -0700103 nl_socket_free(*handle);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800104 *handle = NULL;
105}
106
107
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700108#if __WORDSIZE == 64
109#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
110#else
111#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
112#endif
113
Hai Shalomfdcde762020-04-02 11:19:20 -0700114static void nl80211_register_eloop_read(struct nl_sock **handle,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700115 eloop_sock_handler handler,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700116 void *eloop_data, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700117{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800118 /*
119 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
120 * by default. It is possible to hit that limit in some cases where
121 * operations are blocked, e.g., with a burst of Deauthentication frames
122 * to hostapd and STA entry deletion. Try to increase the buffer to make
123 * this less likely to occur.
124 */
Hai Shalomfdcde762020-04-02 11:19:20 -0700125 int err;
126
127 err = nl_socket_set_buffer_size(*handle, 262144, 0);
128 if (err < 0) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800129 wpa_printf(MSG_DEBUG,
130 "nl80211: Could not set nl_socket RX buffer size: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -0700131 nl_geterror(err));
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800132 /* continue anyway with the default (smaller) buffer */
133 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800134
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700135 nl_socket_set_nonblocking(*handle);
136 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
137 eloop_data, *handle);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700138 if (!persist)
139 *handle = (void *) (((intptr_t) *handle) ^
140 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700141}
142
143
Hai Shalomfdcde762020-04-02 11:19:20 -0700144static void nl80211_destroy_eloop_handle(struct nl_sock **handle, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700145{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700146 if (!persist)
147 *handle = (void *) (((intptr_t) *handle) ^
148 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700149 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
150 nl_destroy_handles(handle);
151}
152
153
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800155static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800156
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800157static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700158static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
159 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800162wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800163 const u8 *set_addr, int first,
164 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166 unsigned int freq, unsigned int wait,
Hai Shalomfdcde762020-04-02 11:19:20 -0700167 const u8 *buf, size_t buf_len,
168 int save_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800169 int no_cck, int no_ack, int offchanok,
170 const u16 *csa_offs, size_t csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800171static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
172 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800174#define IFIDX_ANY -1
175
176static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
177 int ifidx_reason);
178static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
179 int ifidx_reason);
180static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
181 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700182
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700183static int nl80211_set_channel(struct i802_bss *bss,
184 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
186 int ifindex, int disabled);
187
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800188static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
189 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800190
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700191static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800192static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700193#ifdef CONFIG_MESH
194static int nl80211_put_mesh_config(struct nl_msg *msg,
195 struct wpa_driver_mesh_bss_params *params);
196#endif /* CONFIG_MESH */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800197static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -0700198 u16 reason);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700199
200
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800201/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800202enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800203{
204 switch (width) {
205 case NL80211_CHAN_WIDTH_20_NOHT:
206 return CHAN_WIDTH_20_NOHT;
207 case NL80211_CHAN_WIDTH_20:
208 return CHAN_WIDTH_20;
209 case NL80211_CHAN_WIDTH_40:
210 return CHAN_WIDTH_40;
211 case NL80211_CHAN_WIDTH_80:
212 return CHAN_WIDTH_80;
213 case NL80211_CHAN_WIDTH_80P80:
214 return CHAN_WIDTH_80P80;
215 case NL80211_CHAN_WIDTH_160:
216 return CHAN_WIDTH_160;
217 }
218 return CHAN_WIDTH_UNKNOWN;
219}
220
221
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800222int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800223{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700224 return nlmode == NL80211_IFTYPE_AP ||
225 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800226}
227
228
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800229int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700231 return nlmode == NL80211_IFTYPE_STATION ||
232 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233}
234
235
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700236static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700238 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
239 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241
242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800243struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
244 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700245{
246 struct i802_bss *bss;
247
248 for (bss = drv->first_bss; bss; bss = bss->next) {
249 if (bss->ifindex == ifindex)
250 return bss;
251 }
252
253 return NULL;
254}
255
256
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800257static int is_mesh_interface(enum nl80211_iftype nlmode)
258{
259 return nlmode == NL80211_IFTYPE_MESH_POINT;
260}
261
262
263void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700264{
265 if (drv->associated)
266 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
267 drv->associated = 0;
268 os_memset(drv->bssid, 0, ETH_ALEN);
Hai Shalom5f92bc92019-04-18 11:54:11 -0700269 drv->first_bss->freq = 0;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700270}
271
272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273/* nl80211 code */
274static int ack_handler(struct nl_msg *msg, void *arg)
275{
276 int *err = arg;
277 *err = 0;
278 return NL_STOP;
279}
280
281static int finish_handler(struct nl_msg *msg, void *arg)
282{
283 int *ret = arg;
284 *ret = 0;
285 return NL_SKIP;
286}
287
288static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
289 void *arg)
290{
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800291 struct nlmsghdr *nlh = (struct nlmsghdr *) err - 1;
292 int len = nlh->nlmsg_len;
293 struct nlattr *attrs;
294 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 int *ret = arg;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800296 int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 *ret = err->error;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800299
300 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
301 return NL_SKIP;
302
303 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
304 ack_len += err->msg.nlmsg_len - sizeof(*nlh);
305
306 if (len <= ack_len)
307 return NL_STOP;
308
309 attrs = (void *) ((unsigned char *) nlh + ack_len);
310 len -= ack_len;
311
312 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
313 if (tb[NLMSGERR_ATTR_MSG]) {
314 len = strnlen((char *) nla_data(tb[NLMSGERR_ATTR_MSG]),
315 nla_len(tb[NLMSGERR_ATTR_MSG]));
316 wpa_printf(MSG_ERROR, "nl80211: kernel reports: %*s",
317 len, (char *) nla_data(tb[NLMSGERR_ATTR_MSG]));
318 }
319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 return NL_SKIP;
321}
322
323
324static int no_seq_check(struct nl_msg *msg, void *arg)
325{
326 return NL_OK;
327}
328
329
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800330static void nl80211_nlmsg_clear(struct nl_msg *msg)
331{
332 /*
333 * Clear nlmsg data, e.g., to make sure key material is not left in
334 * heap memory for unnecessarily long time.
335 */
336 if (msg) {
337 struct nlmsghdr *hdr = nlmsg_hdr(msg);
338 void *data = nlmsg_data(hdr);
339 /*
340 * This would use nlmsg_datalen() or the older nlmsg_len() if
341 * only libnl were to maintain a stable API.. Neither will work
342 * with all released versions, so just calculate the length
343 * here.
344 */
345 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
346
347 os_memset(data, 0, len);
348 }
349}
350
351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800352static int send_and_recv(struct nl80211_global *global,
Hai Shalomfdcde762020-04-02 11:19:20 -0700353 struct nl_sock *nl_handle, struct nl_msg *msg,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 int (*valid_handler)(struct nl_msg *, void *),
355 void *valid_data)
356{
357 struct nl_cb *cb;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800358 int err = -ENOMEM, opt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800360 if (!msg)
361 return -ENOMEM;
362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800363 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 if (!cb)
365 goto out;
366
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800367 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
368 opt = 1;
369 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
370 NETLINK_EXT_ACK, &opt, sizeof(opt));
371
Hai Shalom74f70d42019-02-11 14:42:39 -0800372 /* try to set NETLINK_CAP_ACK to 1, ignoring errors */
373 opt = 1;
374 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
375 NETLINK_CAP_ACK, &opt, sizeof(opt));
376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 err = nl_send_auto_complete(nl_handle, msg);
Hai Shalomfdcde762020-04-02 11:19:20 -0700378 if (err < 0) {
379 wpa_printf(MSG_INFO,
380 "nl80211: nl_send_auto_complete() failed: %s",
381 nl_geterror(err));
382 /* Need to convert libnl error code to an errno value. For now,
383 * just hardcode this to EBADF; the real error reason is shown
384 * in that error print above. */
385 err = -EBADF;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 goto out;
Hai Shalomfdcde762020-04-02 11:19:20 -0700387 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388
389 err = 1;
390
391 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
392 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
393 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
394
395 if (valid_handler)
396 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
397 valid_handler, valid_data);
398
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700399 while (err > 0) {
400 int res = nl_recvmsgs(nl_handle, cb);
Hai Shalomfdcde762020-04-02 11:19:20 -0700401
402 if (res == -NLE_DUMP_INTR) {
403 /* Most likely one of the nl80211 dump routines hit a
404 * case where internal results changed while the dump
405 * was being sent. The most common known case for this
406 * is scan results fetching while associated were every
407 * received Beacon frame from the AP may end up
408 * incrementing bss_generation. This
409 * NL80211_CMD_GET_SCAN case tries again in the caller;
410 * other cases (of which there are no known common ones)
411 * will stop and return an error. */
412 wpa_printf(MSG_DEBUG, "nl80211: %s; convert to -EAGAIN",
413 nl_geterror(res));
414 err = -EAGAIN;
415 } else if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700416 wpa_printf(MSG_INFO,
Hai Shalomfdcde762020-04-02 11:19:20 -0700417 "nl80211: %s->nl_recvmsgs failed: %d (%s)",
418 __func__, res, nl_geterror(res));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700419 }
420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 out:
422 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800423 if (!valid_handler && valid_data == (void *) -1)
424 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 nlmsg_free(msg);
426 return err;
427}
428
429
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800430int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
431 struct nl_msg *msg,
432 int (*valid_handler)(struct nl_msg *, void *),
433 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800435 return send_and_recv(drv->global, drv->global->nl, msg,
436 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437}
438
439
Hai Shalomb755a2a2020-04-23 21:49:02 -0700440/* Use this method to mark that it is necessary to own the connection/interface
441 * for this operation.
442 * handle may be set to NULL, to get the same behavior as send_and_recv_msgs().
443 * set_owner can be used to mark this socket for receiving control port frames.
444 */
445static int send_and_recv_msgs_owner(struct wpa_driver_nl80211_data *drv,
446 struct nl_msg *msg,
447 struct nl_sock *handle, int set_owner,
448 int (*valid_handler)(struct nl_msg *,
449 void *),
450 void *valid_data)
451{
452 /* Control port over nl80211 needs the flags and attributes below.
453 *
454 * The Linux kernel has initial checks for them (in nl80211.c) like:
455 * validate_pae_over_nl80211(...)
456 * or final checks like:
457 * dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid
458 *
459 * Final operations (e.g., disassociate) don't need to set these
460 * attributes, but they have to be performed on the socket, which has
461 * the connection owner property set in the kernel.
462 */
463 if ((drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) &&
464 handle && set_owner &&
465 (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_OVER_NL80211) ||
466 nla_put_flag(msg, NL80211_ATTR_SOCKET_OWNER) ||
467 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
468 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_PREAUTH)))
469 return -1;
470
471 return send_and_recv(drv->global, handle ? handle : drv->global->nl,
472 msg, valid_handler, valid_data);
473}
474
475
476struct nl_sock * get_connect_handle(struct i802_bss *bss)
477{
478 if ((bss->drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) ||
479 bss->use_nl_connect)
480 return bss->nl_connect;
481
482 return NULL;
483}
484
485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486struct family_data {
487 const char *group;
488 int id;
489};
490
491
492static int family_handler(struct nl_msg *msg, void *arg)
493{
494 struct family_data *res = arg;
495 struct nlattr *tb[CTRL_ATTR_MAX + 1];
496 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
497 struct nlattr *mcgrp;
498 int i;
499
500 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
501 genlmsg_attrlen(gnlh, 0), NULL);
502 if (!tb[CTRL_ATTR_MCAST_GROUPS])
503 return NL_SKIP;
504
505 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
506 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
507 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
508 nla_len(mcgrp), NULL);
509 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
510 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
511 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
512 res->group,
513 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
514 continue;
515 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
516 break;
517 };
518
519 return NL_SKIP;
520}
521
522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800523static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 const char *family, const char *group)
525{
526 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800527 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 struct family_data res = { group, -ENOENT };
529
530 msg = nlmsg_alloc();
531 if (!msg)
532 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800533 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
534 0, 0, CTRL_CMD_GETFAMILY, 0) ||
535 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
536 nlmsg_free(msg);
537 return -1;
538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800540 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 if (ret == 0)
542 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 return ret;
544}
545
546
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800547void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
548 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700550 if (TEST_FAIL())
551 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
553 0, flags, cmd, 0);
554}
555
556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800557static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
558{
559 if (bss->wdev_id_set)
560 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
561 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
562}
563
564
565struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
566{
567 struct nl_msg *msg;
568
569 msg = nlmsg_alloc();
570 if (!msg)
571 return NULL;
572
573 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
574 nl80211_set_iface_id(msg, bss) < 0) {
575 nlmsg_free(msg);
576 return NULL;
577 }
578
579 return msg;
580}
581
582
583static struct nl_msg *
584nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
585 int flags, uint8_t cmd)
586{
587 struct nl_msg *msg;
588
589 msg = nlmsg_alloc();
590 if (!msg)
591 return NULL;
592
593 if (!nl80211_cmd(drv, msg, flags, cmd) ||
594 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
595 nlmsg_free(msg);
596 return NULL;
597 }
598
599 return msg;
600}
601
602
603struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
604 uint8_t cmd)
605{
606 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
607}
608
609
610struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
611{
612 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
613}
614
615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616struct wiphy_idx_data {
617 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700618 enum nl80211_iftype nlmode;
619 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620};
621
622
623static int netdev_info_handler(struct nl_msg *msg, void *arg)
624{
625 struct nlattr *tb[NL80211_ATTR_MAX + 1];
626 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
627 struct wiphy_idx_data *info = arg;
628
629 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
630 genlmsg_attrlen(gnlh, 0), NULL);
631
632 if (tb[NL80211_ATTR_WIPHY])
633 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
634
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700635 if (tb[NL80211_ATTR_IFTYPE])
636 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
637
638 if (tb[NL80211_ATTR_MAC] && info->macaddr)
639 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
640 ETH_ALEN);
641
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800642 return NL_SKIP;
643}
644
645
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800646int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800647{
648 struct nl_msg *msg;
649 struct wiphy_idx_data data = {
650 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700651 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 };
653
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800654 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
655 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800656
657 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
658 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800659 return -1;
660}
661
662
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700663static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
664{
665 struct nl_msg *msg;
666 struct wiphy_idx_data data = {
667 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
668 .macaddr = NULL,
669 };
670
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800671 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
672 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700673
674 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
675 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700676 return NL80211_IFTYPE_UNSPECIFIED;
677}
678
679
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700680static int nl80211_get_macaddr(struct i802_bss *bss)
681{
682 struct nl_msg *msg;
683 struct wiphy_idx_data data = {
684 .macaddr = bss->addr,
685 };
686
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800687 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
688 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700689
690 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700691}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700692
693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800694static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
695 struct nl80211_wiphy_data *w)
696{
697 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800698 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800699
700 msg = nlmsg_alloc();
701 if (!msg)
702 return -1;
703
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800704 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
705 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
706 nlmsg_free(msg);
707 return -1;
708 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709
710 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800711 if (ret) {
712 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
713 "failed: ret=%d (%s)",
714 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716 return ret;
717}
718
719
720static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
721{
722 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700723 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800725 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800726
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700727 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700728 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700729 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
730 __func__, res);
731 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732}
733
734
735static int process_beacon_event(struct nl_msg *msg, void *arg)
736{
737 struct nl80211_wiphy_data *w = arg;
738 struct wpa_driver_nl80211_data *drv;
739 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
740 struct nlattr *tb[NL80211_ATTR_MAX + 1];
741 union wpa_event_data event;
742
743 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
744 genlmsg_attrlen(gnlh, 0), NULL);
745
746 if (gnlh->cmd != NL80211_CMD_FRAME) {
747 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
748 gnlh->cmd);
749 return NL_SKIP;
750 }
751
752 if (!tb[NL80211_ATTR_FRAME])
753 return NL_SKIP;
754
755 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
756 wiphy_list) {
757 os_memset(&event, 0, sizeof(event));
758 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
759 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
760 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
761 }
762
763 return NL_SKIP;
764}
765
766
767static struct nl80211_wiphy_data *
768nl80211_get_wiphy_data_ap(struct i802_bss *bss)
769{
770 static DEFINE_DL_LIST(nl80211_wiphys);
771 struct nl80211_wiphy_data *w;
772 int wiphy_idx, found = 0;
773 struct i802_bss *tmp_bss;
Paul Stewart092955c2017-02-06 09:13:09 -0800774 u8 channel;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800775
776 if (bss->wiphy_data != NULL)
777 return bss->wiphy_data;
778
779 wiphy_idx = nl80211_get_wiphy_index(bss);
780
781 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
782 if (w->wiphy_idx == wiphy_idx)
783 goto add;
784 }
785
786 /* alloc new one */
787 w = os_zalloc(sizeof(*w));
788 if (w == NULL)
789 return NULL;
790 w->wiphy_idx = wiphy_idx;
791 dl_list_init(&w->bsss);
792 dl_list_init(&w->drvs);
793
Paul Stewart092955c2017-02-06 09:13:09 -0800794 /* Beacon frames not supported in IEEE 802.11ad */
795 if (ieee80211_freq_to_chan(bss->freq, &channel) !=
796 HOSTAPD_MODE_IEEE80211AD) {
797 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
798 if (!w->nl_cb) {
799 os_free(w);
800 return NULL;
801 }
802 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
803 no_seq_check, NULL);
804 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
805 process_beacon_event, w);
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000806
Paul Stewart092955c2017-02-06 09:13:09 -0800807 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
808 "wiphy beacons");
809 if (w->nl_beacons == NULL) {
810 os_free(w);
811 return NULL;
812 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000813
Paul Stewart092955c2017-02-06 09:13:09 -0800814 if (nl80211_register_beacons(bss->drv, w)) {
815 nl_destroy_handles(&w->nl_beacons);
816 os_free(w);
817 return NULL;
818 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000819
Paul Stewart092955c2017-02-06 09:13:09 -0800820 nl80211_register_eloop_read(&w->nl_beacons,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700821 nl80211_recv_beacons, w, 0);
Paul Stewart092955c2017-02-06 09:13:09 -0800822 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823
824 dl_list_add(&nl80211_wiphys, &w->list);
825
826add:
827 /* drv entry for this bss already there? */
828 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
829 if (tmp_bss->drv == bss->drv) {
830 found = 1;
831 break;
832 }
833 }
834 /* if not add it */
835 if (!found)
836 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
837
838 dl_list_add(&w->bsss, &bss->wiphy_list);
839 bss->wiphy_data = w;
840 return w;
841}
842
843
844static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
845{
846 struct nl80211_wiphy_data *w = bss->wiphy_data;
847 struct i802_bss *tmp_bss;
848 int found = 0;
849
850 if (w == NULL)
851 return;
852 bss->wiphy_data = NULL;
853 dl_list_del(&bss->wiphy_list);
854
855 /* still any for this drv present? */
856 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
857 if (tmp_bss->drv == bss->drv) {
858 found = 1;
859 break;
860 }
861 }
862 /* if not remove it */
863 if (!found)
864 dl_list_del(&bss->drv->wiphy_list);
865
866 if (!dl_list_empty(&w->bsss))
867 return;
868
Paul Stewart092955c2017-02-06 09:13:09 -0800869 if (w->nl_beacons)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700870 nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871
872 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800873 dl_list_del(&w->list);
874 os_free(w);
875}
876
877
Dmitry Shmidte4663042016-04-04 10:07:49 -0700878static unsigned int nl80211_get_ifindex(void *priv)
879{
880 struct i802_bss *bss = priv;
881 struct wpa_driver_nl80211_data *drv = bss->drv;
882
883 return drv->ifindex;
884}
885
886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
888{
889 struct i802_bss *bss = priv;
890 struct wpa_driver_nl80211_data *drv = bss->drv;
891 if (!drv->associated)
892 return -1;
893 os_memcpy(bssid, drv->bssid, ETH_ALEN);
894 return 0;
895}
896
897
898static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
899{
900 struct i802_bss *bss = priv;
901 struct wpa_driver_nl80211_data *drv = bss->drv;
902 if (!drv->associated)
903 return -1;
904 os_memcpy(ssid, drv->ssid, drv->ssid_len);
905 return drv->ssid_len;
906}
907
908
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800909static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700910 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
911 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912{
913 union wpa_event_data event;
914
Dmitry Shmidte4663042016-04-04 10:07:49 -0700915 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800916 if (if_nametoindex(drv->first_bss->ifname) == 0) {
917 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
918 drv->first_bss->ifname);
919 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700920 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800921 if (!drv->if_removed)
922 return;
923 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
924 drv->first_bss->ifname);
925 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 }
927
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800928 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700929 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800930 os_strlcpy(event.interface_status.ifname, ifname,
931 sizeof(event.interface_status.ifname));
932 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700933 if (drv)
934 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
935 else
936 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
937 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800938}
939
940
941static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700942 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
943 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800944{
945 union wpa_event_data event;
946
Dmitry Shmidte4663042016-04-04 10:07:49 -0700947 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800948 if (drv->if_removed) {
949 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
950 ifname);
951 return;
952 }
953 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
954 ifname);
955 drv->if_removed = 1;
956 } else {
957 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
958 ifname);
959 }
960
961 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700962 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800963 os_strlcpy(event.interface_status.ifname, ifname,
964 sizeof(event.interface_status.ifname));
965 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700966 if (drv)
967 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
968 else
969 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
970 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971}
972
973
974static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
975 u8 *buf, size_t len)
976{
977 int attrlen, rta_len;
978 struct rtattr *attr;
979
980 attrlen = len;
981 attr = (struct rtattr *) buf;
982
983 rta_len = RTA_ALIGN(sizeof(struct rtattr));
984 while (RTA_OK(attr, attrlen)) {
985 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800986 if (os_strcmp(((char *) attr) + rta_len,
987 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 return 1;
989 else
990 break;
991 }
992 attr = RTA_NEXT(attr, attrlen);
993 }
994
995 return 0;
996}
997
998
999static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1000 int ifindex, u8 *buf, size_t len)
1001{
1002 if (drv->ifindex == ifindex)
1003 return 1;
1004
1005 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001006 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1008 "interface");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001009 if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
1010 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 return 1;
1012 }
1013
1014 return 0;
1015}
1016
1017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018static struct wpa_driver_nl80211_data *
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001019nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
1020 int *init_failed)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001021{
1022 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001023 int res;
1024
1025 if (init_failed)
1026 *init_failed = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001027 dl_list_for_each(drv, &global->interfaces,
1028 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001029 res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
1030 if (res < 0) {
1031 wpa_printf(MSG_DEBUG,
1032 "nl80211: Found matching own interface, but failed to complete reinitialization");
1033 if (init_failed)
1034 *init_failed = 1;
1035 return drv;
1036 }
1037 if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038 return drv;
1039 }
1040 return NULL;
1041}
1042
1043
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001044static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001045 int ifindex, int notify)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001046{
1047 struct i802_bss *bss;
1048 u8 addr[ETH_ALEN];
1049
1050 bss = get_bss_ifindex(drv, ifindex);
1051 if (bss &&
1052 linux_get_ifhwaddr(drv->global->ioctl_sock,
1053 bss->ifname, addr) < 0) {
1054 wpa_printf(MSG_DEBUG,
1055 "nl80211: %s: failed to re-read MAC address",
1056 bss->ifname);
1057 } else if (bss && os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1058 wpa_printf(MSG_DEBUG,
1059 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1060 MACSTR " to " MACSTR,
1061 ifindex, bss->ifname,
1062 MAC2STR(bss->addr), MAC2STR(addr));
1063 os_memcpy(bss->addr, addr, ETH_ALEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001064 if (notify)
1065 wpa_supplicant_event(drv->ctx,
1066 EVENT_INTERFACE_MAC_CHANGED, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001067 }
1068}
1069
1070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1072 struct ifinfomsg *ifi,
1073 u8 *buf, size_t len)
1074{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001075 struct nl80211_global *global = ctx;
1076 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001077 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 struct rtattr *attr;
1079 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001081 char ifname[IFNAMSIZ + 1];
1082 char extra[100], *pos, *end;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001083 int init_failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001085 extra[0] = '\0';
1086 pos = extra;
1087 end = pos + sizeof(extra);
1088 ifname[0] = '\0';
1089
1090 attrlen = len;
1091 attr = (struct rtattr *) buf;
1092 while (RTA_OK(attr, attrlen)) {
1093 switch (attr->rta_type) {
1094 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001095 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001096 break;
1097 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1098 ifname[RTA_PAYLOAD(attr)] = '\0';
1099 break;
1100 case IFLA_MASTER:
1101 brid = nla_get_u32((struct nlattr *) attr);
1102 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1103 break;
1104 case IFLA_WIRELESS:
1105 pos += os_snprintf(pos, end - pos, " wext");
1106 break;
1107 case IFLA_OPERSTATE:
1108 pos += os_snprintf(pos, end - pos, " operstate=%u",
1109 nla_get_u32((struct nlattr *) attr));
1110 break;
1111 case IFLA_LINKMODE:
1112 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1113 nla_get_u32((struct nlattr *) attr));
1114 break;
1115 }
1116 attr = RTA_NEXT(attr, attrlen);
1117 }
1118 extra[sizeof(extra) - 1] = '\0';
1119
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001120 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1121 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1122 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1124 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1125 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1126 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1127
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001128 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
Dmitry Shmidte4663042016-04-04 10:07:49 -07001129 if (!drv)
1130 goto event_newlink;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001131 if (init_failed)
1132 return; /* do not update interface state */
Dmitry Shmidte4663042016-04-04 10:07:49 -07001133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001135 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001136 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001137 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001138 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1139 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001140 drv->ignore_if_down_event = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001141 /* Re-read MAC address as it may have changed */
1142 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001143 return;
1144 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001145 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1146 namebuf, ifname);
1147 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1148 wpa_printf(MSG_DEBUG,
1149 "nl80211: Not the main interface (%s) - do not indicate interface down",
1150 drv->first_bss->ifname);
1151 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001152 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1153 "event generated by mode change");
1154 drv->ignore_if_down_event = 0;
1155 } else {
1156 drv->if_disabled = 1;
1157 wpa_supplicant_event(drv->ctx,
1158 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001159
1160 /*
1161 * Try to get drv again, since it may be removed as
1162 * part of the EVENT_INTERFACE_DISABLED handling for
1163 * dynamic interfaces
1164 */
1165 drv = nl80211_find_drv(global, ifi->ifi_index,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001166 buf, len, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001167 if (!drv)
1168 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001169 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 }
1171
1172 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07001173 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001174 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001175 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1177 "event since interface %s is down",
1178 namebuf);
Hai Shalomc9e41a12018-07-31 14:41:42 -07001179 return;
1180 }
1181 wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
1182 namebuf, ifname);
1183 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1184 wpa_printf(MSG_DEBUG,
1185 "nl80211: Not the main interface (%s) - do not indicate interface up",
1186 drv->first_bss->ifname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001187 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001188 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1189 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001190 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001191 } else if (drv->if_removed) {
1192 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1193 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001194 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001195 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001196 /* Re-read MAC address as it may have changed */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001197 nl80211_refresh_mac(drv, ifi->ifi_index, 0);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001199 drv->if_disabled = 0;
1200 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1201 NULL);
1202 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 }
1204
1205 /*
1206 * Some drivers send the association event before the operup event--in
1207 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1208 * fails. This will hit us when wpa_supplicant does not need to do
1209 * IEEE 802.1X authentication
1210 */
1211 if (drv->operstate == 1 &&
1212 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001213 !(ifi->ifi_flags & IFF_RUNNING)) {
1214 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001215 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 }
1218
Dmitry Shmidte4663042016-04-04 10:07:49 -07001219event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001220 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001221 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1222 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001223
Dmitry Shmidte4663042016-04-04 10:07:49 -07001224 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001225 struct i802_bss *bss;
1226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001228 if (!if_indextoname(brid, namebuf)) {
1229 wpa_printf(MSG_DEBUG,
1230 "nl80211: Could not find bridge ifname for ifindex %u",
1231 brid);
1232 return;
1233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1235 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001236 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001237
1238 for (bss = drv->first_bss; bss; bss = bss->next) {
1239 if (os_strcmp(ifname, bss->ifname) == 0) {
1240 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1241 break;
1242 }
1243 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245}
1246
1247
1248static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1249 struct ifinfomsg *ifi,
1250 u8 *buf, size_t len)
1251{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252 struct nl80211_global *global = ctx;
1253 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001254 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 struct rtattr *attr;
1256 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001257 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001258 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001260 extra[0] = '\0';
1261 pos = extra;
1262 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001263 ifname[0] = '\0';
1264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 attrlen = len;
1266 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001268 switch (attr->rta_type) {
1269 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001270 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001271 break;
1272 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1273 ifname[RTA_PAYLOAD(attr)] = '\0';
1274 break;
1275 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001277 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1278 break;
1279 case IFLA_OPERSTATE:
1280 pos += os_snprintf(pos, end - pos, " operstate=%u",
1281 nla_get_u32((struct nlattr *) attr));
1282 break;
1283 case IFLA_LINKMODE:
1284 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1285 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001286 break;
1287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 attr = RTA_NEXT(attr, attrlen);
1289 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001290 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001292 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1293 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1294 ifi->ifi_flags,
1295 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1296 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1297 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1298 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1299
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001300 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001301
Dmitry Shmidte4663042016-04-04 10:07:49 -07001302 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 /* device has been removed from bridge */
1304 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001305
1306 if (!if_indextoname(brid, namebuf)) {
1307 wpa_printf(MSG_DEBUG,
1308 "nl80211: Could not find bridge ifname for ifindex %u",
1309 brid);
1310 } else {
1311 wpa_printf(MSG_DEBUG,
1312 "nl80211: Remove ifindex %u for bridge %s",
1313 brid, namebuf);
1314 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001315 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001317
1318 if (ifi->ifi_family != AF_BRIDGE || !brid)
1319 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1320 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321}
1322
1323
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001324struct nl80211_get_assoc_freq_arg {
1325 struct wpa_driver_nl80211_data *drv;
1326 unsigned int assoc_freq;
1327 unsigned int ibss_freq;
1328 u8 assoc_bssid[ETH_ALEN];
1329 u8 assoc_ssid[SSID_MAX_LEN];
1330 u8 assoc_ssid_len;
1331};
1332
1333static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1334{
1335 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1336 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1337 struct nlattr *bss[NL80211_BSS_MAX + 1];
1338 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1339 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1340 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1341 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1342 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1343 };
1344 struct nl80211_get_assoc_freq_arg *ctx = arg;
1345 enum nl80211_bss_status status;
1346
1347 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1348 genlmsg_attrlen(gnlh, 0), NULL);
1349 if (!tb[NL80211_ATTR_BSS] ||
1350 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1351 bss_policy) ||
1352 !bss[NL80211_BSS_STATUS])
1353 return NL_SKIP;
1354
1355 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1356 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1357 bss[NL80211_BSS_FREQUENCY]) {
1358 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1359 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1360 ctx->assoc_freq);
1361 }
1362 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1363 bss[NL80211_BSS_FREQUENCY]) {
1364 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1365 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1366 ctx->ibss_freq);
1367 }
1368 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1369 bss[NL80211_BSS_BSSID]) {
1370 os_memcpy(ctx->assoc_bssid,
1371 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1372 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1373 MACSTR, MAC2STR(ctx->assoc_bssid));
1374 }
1375
1376 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1377 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1378 const u8 *ie, *ssid;
1379 size_t ie_len;
1380
1381 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1382 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1383 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1384 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1385 ctx->assoc_ssid_len = ssid[1];
1386 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1387 }
1388 }
1389
1390 return NL_SKIP;
1391}
1392
1393
1394int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001395{
1396 struct nl_msg *msg;
1397 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001398 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001399 int count = 0;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001400
Hai Shalomfdcde762020-04-02 11:19:20 -07001401try_again:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001402 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001403 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001404 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001405 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1406 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001407 if (ret == -EAGAIN) {
1408 count++;
1409 if (count >= 10) {
1410 wpa_printf(MSG_INFO,
1411 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid");
1412 } else {
1413 wpa_printf(MSG_DEBUG,
1414 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid - try again");
1415 goto try_again;
1416 }
1417 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001418 if (ret == 0) {
1419 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1420 return arg.assoc_ssid_len;
1421 }
1422 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1423 ret, strerror(-ret));
1424 return ret;
1425}
1426
1427
1428unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1429{
1430 struct nl_msg *msg;
1431 int ret;
1432 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001433 int count = 0;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001434
Hai Shalomfdcde762020-04-02 11:19:20 -07001435try_again:
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001436 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1437 os_memset(&arg, 0, sizeof(arg));
1438 arg.drv = drv;
1439 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1440 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001441 if (ret == -EAGAIN) {
1442 count++;
1443 if (count >= 10) {
1444 wpa_printf(MSG_INFO,
1445 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq");
1446 } else {
1447 wpa_printf(MSG_DEBUG,
1448 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq - try again");
1449 goto try_again;
1450 }
1451 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001452 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001453 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1454 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001455 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001456 "associated BSS from scan results: %u MHz", freq);
1457 if (freq)
1458 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001459 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001460 }
1461 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1462 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001463 return drv->assoc_freq;
1464}
1465
1466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467static int get_link_signal(struct nl_msg *msg, void *arg)
1468{
1469 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1470 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1471 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1472 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1473 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001474 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001475 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 };
1477 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1478 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1479 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1480 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1481 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1482 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1483 };
1484 struct wpa_signal_info *sig_change = arg;
1485
1486 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1487 genlmsg_attrlen(gnlh, 0), NULL);
1488 if (!tb[NL80211_ATTR_STA_INFO] ||
1489 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1490 tb[NL80211_ATTR_STA_INFO], policy))
1491 return NL_SKIP;
1492 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1493 return NL_SKIP;
1494
1495 sig_change->current_signal =
1496 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1497
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001498 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1499 sig_change->avg_signal =
1500 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1501 else
1502 sig_change->avg_signal = 0;
1503
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001504 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1505 sig_change->avg_beacon_signal =
1506 (s8)
1507 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1508 else
1509 sig_change->avg_beacon_signal = 0;
1510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1512 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1513 sinfo[NL80211_STA_INFO_TX_BITRATE],
1514 rate_policy)) {
1515 sig_change->current_txrate = 0;
1516 } else {
1517 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1518 sig_change->current_txrate =
1519 nla_get_u16(rinfo[
1520 NL80211_RATE_INFO_BITRATE]) * 100;
1521 }
1522 }
1523 }
1524
1525 return NL_SKIP;
1526}
1527
1528
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001529int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1530 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531{
1532 struct nl_msg *msg;
1533
Hai Shalom74f70d42019-02-11 14:42:39 -08001534 sig->current_signal = -WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 sig->current_txrate = 0;
1536
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001537 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1538 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1539 nlmsg_free(msg);
1540 return -ENOBUFS;
1541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542
1543 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544}
1545
1546
1547static int get_link_noise(struct nl_msg *msg, void *arg)
1548{
1549 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1550 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1551 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1552 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1553 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1554 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1555 };
1556 struct wpa_signal_info *sig_change = arg;
1557
1558 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1559 genlmsg_attrlen(gnlh, 0), NULL);
1560
1561 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1562 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1563 return NL_SKIP;
1564 }
1565
1566 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1567 tb[NL80211_ATTR_SURVEY_INFO],
1568 survey_policy)) {
1569 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1570 "attributes!");
1571 return NL_SKIP;
1572 }
1573
1574 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1575 return NL_SKIP;
1576
1577 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1578 sig_change->frequency)
1579 return NL_SKIP;
1580
1581 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1582 return NL_SKIP;
1583
1584 sig_change->current_noise =
1585 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1586
1587 return NL_SKIP;
1588}
1589
1590
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001591int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1592 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593{
1594 struct nl_msg *msg;
1595
Hai Shalom74f70d42019-02-11 14:42:39 -08001596 sig_change->current_noise = WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 sig_change->frequency = drv->assoc_freq;
1598
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001599 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601}
1602
1603
Hai Shalom74f70d42019-02-11 14:42:39 -08001604static int get_channel_info(struct nl_msg *msg, void *arg)
1605{
1606 struct nlattr *tb[NL80211_ATTR_MAX + 1] = { 0 };
1607 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1608 struct wpa_channel_info *chan_info = arg;
1609
1610 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1611 genlmsg_attrlen(gnlh, 0), NULL);
1612
1613 os_memset(chan_info, 0, sizeof(struct wpa_channel_info));
1614 chan_info->chanwidth = CHAN_WIDTH_UNKNOWN;
1615
1616 if (tb[NL80211_ATTR_WIPHY_FREQ])
1617 chan_info->frequency =
1618 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1619 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
1620 chan_info->chanwidth = convert2width(
1621 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
1622 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1623 enum nl80211_channel_type ct =
1624 nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1625
1626 switch (ct) {
1627 case NL80211_CHAN_HT40MINUS:
1628 chan_info->sec_channel = -1;
1629 break;
1630 case NL80211_CHAN_HT40PLUS:
1631 chan_info->sec_channel = 1;
1632 break;
1633 default:
1634 chan_info->sec_channel = 0;
1635 break;
1636 }
1637 }
1638 if (tb[NL80211_ATTR_CENTER_FREQ1])
1639 chan_info->center_frq1 =
1640 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1641 if (tb[NL80211_ATTR_CENTER_FREQ2])
1642 chan_info->center_frq2 =
1643 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1644
1645 if (chan_info->center_frq2) {
1646 u8 seg1_idx = 0;
1647
1648 if (ieee80211_freq_to_chan(chan_info->center_frq2, &seg1_idx) !=
1649 NUM_HOSTAPD_MODES)
1650 chan_info->seg1_idx = seg1_idx;
1651 }
1652
1653 return NL_SKIP;
1654}
1655
1656
1657static int nl80211_channel_info(void *priv, struct wpa_channel_info *ci)
1658{
1659 struct i802_bss *bss = priv;
1660 struct wpa_driver_nl80211_data *drv = bss->drv;
1661 struct nl_msg *msg;
1662
1663 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
1664 return send_and_recv_msgs(drv, msg, get_channel_info, ci);
1665}
1666
1667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1669 void *handle)
1670{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001671 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001672 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001674 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001676 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001677 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001678 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1679 __func__, res);
1680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681}
1682
1683
1684/**
1685 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1686 * @priv: driver_nl80211 private data
1687 * @alpha2_arg: country to which to switch to
1688 * Returns: 0 on success, -1 on failure
1689 *
1690 * This asks nl80211 to set the regulatory domain for given
1691 * country ISO / IEC alpha2.
1692 */
1693static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1694{
1695 struct i802_bss *bss = priv;
1696 struct wpa_driver_nl80211_data *drv = bss->drv;
1697 char alpha2[3];
1698 struct nl_msg *msg;
1699
1700 msg = nlmsg_alloc();
1701 if (!msg)
1702 return -ENOMEM;
1703
1704 alpha2[0] = alpha2_arg[0];
1705 alpha2[1] = alpha2_arg[1];
1706 alpha2[2] = '\0';
1707
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001708 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1709 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1710 nlmsg_free(msg);
1711 return -EINVAL;
1712 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1714 return -EINVAL;
1715 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716}
1717
1718
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001719static int nl80211_get_country(struct nl_msg *msg, void *arg)
1720{
1721 char *alpha2 = arg;
1722 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1723 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1724
1725 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1726 genlmsg_attrlen(gnlh, 0), NULL);
1727 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1728 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1729 return NL_SKIP;
1730 }
1731 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1732 return NL_SKIP;
1733}
1734
1735
1736static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1737{
1738 struct i802_bss *bss = priv;
1739 struct wpa_driver_nl80211_data *drv = bss->drv;
1740 struct nl_msg *msg;
1741 int ret;
1742
1743 msg = nlmsg_alloc();
1744 if (!msg)
1745 return -ENOMEM;
1746
1747 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1748 alpha2[0] = '\0';
1749 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1750 if (!alpha2[0])
1751 ret = -1;
1752
1753 return ret;
1754}
1755
1756
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001757static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758{
1759 int ret;
1760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001761 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1762 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1764 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001765 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 }
1767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001768 global->nl = nl_create_handle(global->nl_cb, "nl");
1769 if (global->nl == NULL)
1770 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1773 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1775 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001776 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 }
1778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001779 global->nl_event = nl_create_handle(global->nl_cb, "event");
1780 if (global->nl_event == NULL)
1781 goto err;
1782
1783 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001785 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 if (ret < 0) {
1787 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1788 "membership for scan events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001789 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001790 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 }
1792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001795 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796 if (ret < 0) {
1797 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1798 "membership for mlme events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001799 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001800 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 }
1802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001805 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 if (ret < 0) {
1807 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1808 "membership for regulatory events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001809 ret, nl_geterror(ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 /* Continue without regulatory events */
1811 }
1812
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001813 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1814 if (ret >= 0)
1815 ret = nl_socket_add_membership(global->nl_event, ret);
1816 if (ret < 0) {
1817 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1818 "membership for vendor events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001819 ret, nl_geterror(ret));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001820 /* Continue without vendor events */
1821 }
1822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001823 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1824 no_seq_check, NULL);
1825 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1826 process_global_event, global);
1827
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001828 nl80211_register_eloop_read(&global->nl_event,
1829 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001830 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831
1832 return 0;
1833
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001834err:
1835 nl_destroy_handles(&global->nl_event);
1836 nl_destroy_handles(&global->nl);
1837 nl_cb_put(global->nl_cb);
1838 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 return -1;
1840}
1841
1842
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001843static void nl80211_check_global(struct nl80211_global *global)
1844{
Hai Shalomfdcde762020-04-02 11:19:20 -07001845 struct nl_sock *handle;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001846 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1847 int ret;
1848 unsigned int i;
1849
1850 /*
1851 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1852 * and all registration having been cleared.
1853 */
1854 handle = (void *) (((intptr_t) global->nl_event) ^
1855 ELOOP_SOCKET_INVALID);
1856
1857 for (i = 0; groups[i]; i++) {
1858 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1859 if (ret >= 0)
1860 ret = nl_socket_add_membership(handle, ret);
1861 if (ret < 0) {
1862 wpa_printf(MSG_INFO,
1863 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001864 groups[i], ret, nl_geterror(ret));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001865 }
1866 }
1867}
1868
1869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1871{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001872 struct wpa_driver_nl80211_data *drv = ctx;
1873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001877 * rtnetlink ifdown handler will report interfaces other than the P2P
1878 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001880 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1881 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882}
1883
1884
1885static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1886{
1887 struct wpa_driver_nl80211_data *drv = ctx;
1888 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001889 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1891 "after rfkill unblock");
1892 return;
1893 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001894
1895 if (is_p2p_net_interface(drv->nlmode))
1896 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1897
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001898 /*
1899 * rtnetlink ifup handler will report interfaces other than the P2P
1900 * Device interface as enabled.
1901 */
1902 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1903 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904}
1905
1906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1908 void *eloop_ctx,
1909 void *handle)
1910{
1911 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1912 u8 data[2048];
1913 struct msghdr msg;
1914 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001915 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001916 struct cmsghdr *cmsg;
1917 int res, found_ee = 0, found_wifi = 0, acked = 0;
1918 union wpa_event_data event;
1919
1920 memset(&msg, 0, sizeof(msg));
1921 msg.msg_iov = &entry;
1922 msg.msg_iovlen = 1;
1923 entry.iov_base = data;
1924 entry.iov_len = sizeof(data);
1925 msg.msg_control = &control;
1926 msg.msg_controllen = sizeof(control);
1927
1928 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1929 /* if error or not fitting 802.3 header, return */
1930 if (res < 14)
1931 return;
1932
1933 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1934 {
1935 if (cmsg->cmsg_level == SOL_SOCKET &&
1936 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1937 int *ack;
1938
1939 found_wifi = 1;
1940 ack = (void *)CMSG_DATA(cmsg);
1941 acked = *ack;
1942 }
1943
1944 if (cmsg->cmsg_level == SOL_PACKET &&
1945 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1946 struct sock_extended_err *err =
1947 (struct sock_extended_err *)CMSG_DATA(cmsg);
1948
1949 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1950 found_ee = 1;
1951 }
1952 }
1953
1954 if (!found_ee || !found_wifi)
1955 return;
1956
1957 memset(&event, 0, sizeof(event));
1958 event.eapol_tx_status.dst = data;
1959 event.eapol_tx_status.data = data + 14;
1960 event.eapol_tx_status.data_len = res - 14;
1961 event.eapol_tx_status.ack = acked;
1962 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1963}
1964
1965
Hai Shalomb755a2a2020-04-23 21:49:02 -07001966static int nl80211_init_connect_handle(struct i802_bss *bss)
1967{
1968 if (bss->nl_connect) {
1969 wpa_printf(MSG_DEBUG,
1970 "nl80211: Connect handle already created (nl_connect=%p)",
1971 bss->nl_connect);
1972 return -1;
1973 }
1974
1975 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
1976 if (!bss->nl_connect)
1977 return -1;
1978 nl80211_register_eloop_read(&bss->nl_connect,
1979 wpa_driver_nl80211_event_receive,
1980 bss->nl_cb, 1);
1981 return 0;
1982}
1983
1984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985static int nl80211_init_bss(struct i802_bss *bss)
1986{
1987 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1988 if (!bss->nl_cb)
1989 return -1;
1990
1991 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1992 no_seq_check, NULL);
1993 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1994 process_bss_event, bss);
1995
Hai Shalomb755a2a2020-04-23 21:49:02 -07001996 nl80211_init_connect_handle(bss);
1997
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 return 0;
1999}
2000
2001
2002static void nl80211_destroy_bss(struct i802_bss *bss)
2003{
2004 nl_cb_put(bss->nl_cb);
2005 bss->nl_cb = NULL;
Hai Shalomb755a2a2020-04-23 21:49:02 -07002006
2007 if (bss->nl_connect)
2008 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009}
2010
2011
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002012static void
2013wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
2014{
2015 struct rfkill_config *rcfg;
2016
2017 if (drv->rfkill)
2018 return;
2019
2020 rcfg = os_zalloc(sizeof(*rcfg));
2021 if (!rcfg)
2022 return;
2023
2024 rcfg->ctx = drv;
2025
2026 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
2027 * not associated with a netdev, so use the name of some other interface
2028 * sharing the same wiphy as the P2P Device interface.
2029 *
2030 * Note: This is valid, as a P2P Device interface is always dynamically
2031 * created and is created only once another wpa_s interface was added.
2032 */
2033 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
2034 struct nl80211_global *global = drv->global;
2035 struct wpa_driver_nl80211_data *tmp1;
2036
2037 dl_list_for_each(tmp1, &global->interfaces,
2038 struct wpa_driver_nl80211_data, list) {
2039 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
2040 !tmp1->rfkill)
2041 continue;
2042
2043 wpa_printf(MSG_DEBUG,
2044 "nl80211: Use (%s) to initialize P2P Device rfkill",
2045 tmp1->first_bss->ifname);
2046 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
2047 sizeof(rcfg->ifname));
2048 break;
2049 }
2050 } else {
2051 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
2052 sizeof(rcfg->ifname));
2053 }
2054
2055 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2056 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2057 drv->rfkill = rfkill_init(rcfg);
2058 if (!drv->rfkill) {
2059 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2060 os_free(rcfg);
2061 }
2062}
2063
2064
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002065static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
2066 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002067 const u8 *set_addr,
2068 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069{
2070 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 struct i802_bss *bss;
2072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 if (global_priv == NULL)
2074 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 drv = os_zalloc(sizeof(*drv));
2076 if (drv == NULL)
2077 return NULL;
2078 drv->global = global_priv;
2079 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002080 drv->hostapd = !!hostapd;
2081 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002082
2083 /*
2084 * There is no driver capability flag for this, so assume it is
2085 * supported and disable this on first attempt to use if the driver
2086 * rejects the command due to missing support.
2087 */
2088 drv->set_rekey_offload = 1;
2089
Hai Shalom81f62d82019-07-22 12:10:00 -07002090 drv->num_if_indices = ARRAY_SIZE(drv->default_if_indices);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002091 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002092
2093 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
2094 if (!drv->first_bss) {
2095 os_free(drv);
2096 return NULL;
2097 }
2098 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002100 bss->ctx = ctx;
2101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2103 drv->monitor_ifidx = -1;
2104 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002105 drv->eapol_tx_sock = -1;
2106 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002108 if (nl80211_init_bss(bss))
2109 goto failed;
2110
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002111 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 goto failed;
2113
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002114 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
2115 if (drv->eapol_tx_sock < 0)
2116 goto failed;
2117
2118 if (drv->data_tx_status) {
2119 int enabled = 1;
2120
2121 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2122 &enabled, sizeof(enabled)) < 0) {
2123 wpa_printf(MSG_DEBUG,
2124 "nl80211: wifi status sockopt failed\n");
2125 drv->data_tx_status = 0;
2126 if (!drv->use_monitor)
2127 drv->capa.flags &=
2128 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2129 } else {
2130 eloop_register_read_sock(drv->eapol_tx_sock,
2131 wpa_driver_nl80211_handle_eapol_tx_status,
2132 drv, NULL);
2133 }
2134 }
2135
2136 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002137 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002139 drv->in_interface_list = 1;
2140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141
2142 return bss;
2143
2144failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002145 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 return NULL;
2147}
2148
2149
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002150/**
2151 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2152 * @ctx: context to be used when calling wpa_supplicant functions,
2153 * e.g., wpa_supplicant_event()
2154 * @ifname: interface name, e.g., wlan0
2155 * @global_priv: private driver global data from global_init()
2156 * Returns: Pointer to private data, %NULL on failure
2157 */
2158static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2159 void *global_priv)
2160{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002161 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
2162 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002163}
2164
2165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002166static int nl80211_register_frame(struct i802_bss *bss,
Hai Shalomfdcde762020-04-02 11:19:20 -07002167 struct nl_sock *nl_handle,
Hai Shalome21d4e82020-04-29 16:34:06 -07002168 u16 type, const u8 *match, size_t match_len,
2169 bool multicast)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002171 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002173 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002174 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002176 buf[0] = '\0';
2177 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Hai Shalome21d4e82020-04-29 16:34:06 -07002178 wpa_printf(MSG_DEBUG,
2179 "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s multicast=%d",
2180 type, fc2str(type), nl_handle, buf, multicast);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181
Hai Shalomfdcde762020-04-02 11:19:20 -07002182 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_FRAME)) ||
Hai Shalome21d4e82020-04-29 16:34:06 -07002183 (multicast && nla_put_flag(msg, NL80211_ATTR_RECEIVE_MULTICAST)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002184 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
2185 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
2186 nlmsg_free(msg);
2187 return -1;
2188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002190 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 if (ret) {
2192 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2193 "failed (type=%u): ret=%d (%s)",
2194 type, ret, strerror(-ret));
2195 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2196 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 return ret;
2199}
2200
2201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002202static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2203{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002204 if (bss->nl_mgmt) {
2205 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
2206 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2207 return -1;
2208 }
2209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002211 if (bss->nl_mgmt == NULL)
2212 return -1;
2213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002214 return 0;
2215}
2216
2217
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002218static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2219{
2220 nl80211_register_eloop_read(&bss->nl_mgmt,
2221 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002222 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002223}
2224
2225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002226static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 const u8 *match, size_t match_len)
2228{
2229 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002230 return nl80211_register_frame(bss, bss->nl_mgmt,
Hai Shalome21d4e82020-04-29 16:34:06 -07002231 type, match, match_len, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232}
2233
2234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002237 struct wpa_driver_nl80211_data *drv = bss->drv;
Hai Shalomfdcde762020-04-02 11:19:20 -07002238 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002239 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002240
2241 if (nl80211_alloc_mgmt_handle(bss))
2242 return -1;
2243 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2244 "handle %p", bss->nl_mgmt);
2245
Hai Shalomfdcde762020-04-02 11:19:20 -07002246 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002247 /* register for any AUTH message */
Hai Shalome21d4e82020-04-29 16:34:06 -07002248 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07002249 } else if ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2250 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
2251 /* register for SAE Authentication frames */
2252 nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalome21d4e82020-04-29 16:34:06 -07002253 (u8 *) "\x03\x00", 2, false);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002254 }
2255
Dmitry Shmidt051af732013-10-22 13:52:46 -07002256#ifdef CONFIG_INTERWORKING
2257 /* QoS Map Configure */
2258 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002259 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002260#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002261#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002263 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002264 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002266 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002267 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002269 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002270 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002272 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002273 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002274 /* Protected GAS Initial Request */
2275 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2276 ret = -1;
2277 /* Protected GAS Initial Response */
2278 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2279 ret = -1;
2280 /* Protected GAS Comeback Request */
2281 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2282 ret = -1;
2283 /* Protected GAS Comeback Response */
2284 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2285 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002286#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002287#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002289 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2291 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002292 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002294 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 (u8 *) "\x7f\x50\x6f\x9a\x09",
2296 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002297 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298#endif /* CONFIG_P2P */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002299#ifdef CONFIG_DPP
2300 /* DPP Public Action */
2301 if (nl80211_register_action_frame(bss,
2302 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2303 6) < 0)
2304 ret = -1;
2305#endif /* CONFIG_DPP */
Hai Shalom74f70d42019-02-11 14:42:39 -08002306#ifdef CONFIG_OCV
2307 /* SA Query Request */
2308 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x00", 2) < 0)
2309 ret = -1;
2310#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002313 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002314#ifdef CONFIG_TDLS
2315 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2316 /* TDLS Discovery Response */
2317 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2318 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002319 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002320 }
2321#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002322#ifdef CONFIG_FST
2323 /* FST Action frames */
2324 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2325 ret = -1;
2326#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327
2328 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002329 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002330 ret = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002331 else if (!drv->has_driver_key_mgmt) {
2332 int i;
2333
2334 /* Update supported AKMs only if the driver doesn't advertize
2335 * any AKM capabilities. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2337 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2338
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002339 /* Update per interface supported AKMs */
2340 for (i = 0; i < WPA_IF_MAX; i++)
2341 drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
2342 }
2343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344 /* WNM - BSS Transition Management Request */
2345 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002346 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002347 /* WNM-Sleep Mode Response */
2348 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002349 ret = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002350#ifdef CONFIG_WNM
2351 /* WNM - Collocated Interference Request */
2352 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2353 ret = -1;
2354#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002356#ifdef CONFIG_HS20
2357 /* WNM-Notification */
2358 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002359 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002360#endif /* CONFIG_HS20 */
2361
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002362 /* WMM-AC ADDTS Response */
2363 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2364 ret = -1;
2365
2366 /* WMM-AC DELTS */
2367 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2368 ret = -1;
2369
2370 /* Radio Measurement - Neighbor Report Response */
2371 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2372 ret = -1;
2373
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002374 /* Radio Measurement - Radio Measurement Request */
2375 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2376 ret = -1;
2377
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002378 /* Radio Measurement - Link Measurement Request */
2379 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2380 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2381 ret = -1;
2382
2383 nl80211_mgmt_handle_register_eloop(bss);
2384
2385 return ret;
2386}
2387
2388
2389static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2390{
2391 int ret = 0;
2392
2393 if (nl80211_alloc_mgmt_handle(bss))
2394 return -1;
2395
2396 wpa_printf(MSG_DEBUG,
2397 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2398 bss->nl_mgmt);
2399
2400 /* Auth frames for mesh SAE */
2401 if (nl80211_register_frame(bss, bss->nl_mgmt,
2402 (WLAN_FC_TYPE_MGMT << 2) |
2403 (WLAN_FC_STYPE_AUTH << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002404 NULL, 0, false) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002405 ret = -1;
2406
2407 /* Mesh peering open */
2408 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2409 ret = -1;
2410 /* Mesh peering confirm */
2411 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2412 ret = -1;
2413 /* Mesh peering close */
2414 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2415 ret = -1;
2416
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002417 nl80211_mgmt_handle_register_eloop(bss);
2418
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002419 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420}
2421
2422
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002423static int nl80211_register_spurious_class3(struct i802_bss *bss)
2424{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002425 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002426 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002428 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2429 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002430 if (ret) {
2431 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2432 "failed: ret=%d (%s)",
2433 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002435 return ret;
2436}
2437
2438
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002439static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2440{
2441 int ret = 0;
2442
2443 /* Public Action frames */
2444 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2445 ret = -1;
2446 /* RRM Measurement Report */
2447 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2448 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002449 /* RRM Link Measurement Report */
2450 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2451 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002452 /* RRM Neighbor Report Request */
2453 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2454 ret = -1;
2455 /* FT Action frames */
2456 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2457 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002458 /* SA Query */
2459 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2460 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002461 /* Protected Dual of Public Action */
2462 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2463 ret = -1;
2464 /* WNM */
2465 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2466 ret = -1;
2467 /* WMM */
2468 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2469 ret = -1;
2470#ifdef CONFIG_FST
2471 /* FST Action frames */
2472 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2473 ret = -1;
2474#endif /* CONFIG_FST */
2475 /* Vendor-specific */
2476 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2477 ret = -1;
2478
2479 return ret;
2480}
2481
2482
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002483static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2484{
2485 static const int stypes[] = {
2486 WLAN_FC_STYPE_AUTH,
2487 WLAN_FC_STYPE_ASSOC_REQ,
2488 WLAN_FC_STYPE_REASSOC_REQ,
2489 WLAN_FC_STYPE_DISASSOC,
2490 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002491 WLAN_FC_STYPE_PROBE_REQ,
2492/* Beacon doesn't work as mac80211 doesn't currently allow
2493 * it, but it wouldn't really be the right thing anyway as
2494 * it isn't per interface ... maybe just dump the scan
2495 * results periodically for OLBC?
2496 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002497 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 };
2499 unsigned int i;
2500
2501 if (nl80211_alloc_mgmt_handle(bss))
2502 return -1;
2503 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2504 "handle %p", bss->nl_mgmt);
2505
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002506 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002507 if (nl80211_register_frame(bss, bss->nl_mgmt,
2508 (WLAN_FC_TYPE_MGMT << 2) |
2509 (stypes[i] << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002510 NULL, 0, false) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002511 goto out_err;
2512 }
2513 }
2514
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002515 if (nl80211_action_subscribe_ap(bss))
2516 goto out_err;
2517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002518 if (nl80211_register_spurious_class3(bss))
2519 goto out_err;
2520
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002521 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002522 return 0;
2523
2524out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002525 nl_destroy_handles(&bss->nl_mgmt);
2526 return -1;
2527}
2528
2529
2530static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2531{
2532 if (nl80211_alloc_mgmt_handle(bss))
2533 return -1;
2534 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2535 "handle %p (device SME)", bss->nl_mgmt);
2536
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002537 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002538 goto out_err;
2539
Hai Shalom5f92bc92019-04-18 11:54:11 -07002540 if (bss->drv->device_ap_sme) {
2541 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2542
2543 /* Register for all Authentication frames */
Hai Shalome21d4e82020-04-29 16:34:06 -07002544 if (nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0,
2545 false) < 0)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002546 wpa_printf(MSG_DEBUG,
2547 "nl80211: Failed to subscribe to handle Authentication frames - SAE offload may not work");
2548 }
2549
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002550 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002551 return 0;
2552
2553out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002554 nl_destroy_handles(&bss->nl_mgmt);
2555 return -1;
2556}
2557
2558
2559static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2560{
2561 if (bss->nl_mgmt == NULL)
2562 return;
2563 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2564 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002565 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002566
2567 nl80211_put_wiphy_data_ap(bss);
2568}
2569
2570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2572{
2573 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2574}
2575
2576
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002577static void nl80211_del_p2pdev(struct i802_bss *bss)
2578{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002579 struct nl_msg *msg;
2580 int ret;
2581
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002582 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2583 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002584
2585 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2586 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002587 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002588}
2589
2590
2591static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2592{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002593 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002594 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002596 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2597 NL80211_CMD_STOP_P2P_DEVICE);
2598 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002599
2600 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2601 start ? "Start" : "Stop",
2602 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002603 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002604 return ret;
2605}
2606
2607
2608static int i802_set_iface_flags(struct i802_bss *bss, int up)
2609{
2610 enum nl80211_iftype nlmode;
2611
2612 nlmode = nl80211_get_ifmode(bss);
2613 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2614 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2615 bss->ifname, up);
2616 }
2617
2618 /* P2P Device has start/stop which is equivalent */
2619 return nl80211_set_p2pdev(bss, up);
2620}
2621
2622
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002623#ifdef CONFIG_TESTING_OPTIONS
2624static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2625{
2626 /* struct wpa_driver_nl80211_data *drv = arg; */
2627 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2628 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2629
2630
2631 wpa_printf(MSG_DEBUG,
2632 "nl80211: QCA vendor test command response received");
2633
2634 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2635 genlmsg_attrlen(gnlh, 0), NULL);
2636 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2637 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2638 return NL_SKIP;
2639 }
2640
2641 wpa_hexdump(MSG_DEBUG,
2642 "nl80211: Received QCA vendor test command response",
2643 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2644 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2645
2646 return NL_SKIP;
2647}
2648#endif /* CONFIG_TESTING_OPTIONS */
2649
2650
2651static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2652{
2653#ifdef CONFIG_TESTING_OPTIONS
2654 struct nl_msg *msg;
2655 struct nlattr *params;
2656 int ret;
2657
2658 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2659 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2660 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2661 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2662 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2663 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2664 nlmsg_free(msg);
2665 return;
2666 }
2667 nla_nest_end(msg, params);
2668
2669 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2670 wpa_printf(MSG_DEBUG,
2671 "nl80211: QCA vendor test command returned %d (%s)",
2672 ret, strerror(-ret));
2673#endif /* CONFIG_TESTING_OPTIONS */
2674}
2675
2676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002678wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002679 const u8 *set_addr, int first,
2680 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002682 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002684 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685
2686 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002687 bss->ifindex = drv->ifindex;
2688 bss->wdev_id = drv->global->if_add_wdevid;
2689 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2690
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002691 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2692 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002693 drv->global->if_add_wdevid_set = 0;
2694
Dmitry Shmidt03658832014-08-13 11:03:49 -07002695 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2696 bss->static_ap = 1;
2697
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002698 if (first &&
2699 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2700 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2701 drv->start_iface_up = 1;
2702
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002703 if (wpa_driver_nl80211_capa(drv))
2704 return -1;
2705
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002706 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2707 return -1;
2708
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002709 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2710 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002712 if (set_addr &&
2713 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2714 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2715 set_addr)))
2716 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002717
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002718 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2719 drv->start_mode_ap = 1;
2720
Dmitry Shmidt03658832014-08-13 11:03:49 -07002721 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002722 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002723 else if (bss->if_dynamic ||
2724 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002725 nlmode = nl80211_get_ifmode(bss);
2726 else
2727 nlmode = NL80211_IFTYPE_STATION;
2728
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002729 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002730 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002731 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002733
Dmitry Shmidt98660862014-03-11 17:26:21 -07002734 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002735 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002737 wpa_driver_nl80211_drv_init_rfkill(drv);
2738
Dmitry Shmidt98660862014-03-11 17:26:21 -07002739 if (!rfkill_is_blocked(drv->rfkill)) {
2740 int ret = i802_set_iface_flags(bss, 1);
2741 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2743 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002744 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002746
2747 if (is_p2p_net_interface(nlmode))
2748 nl80211_disable_11b_rates(bss->drv,
2749 bss->drv->ifindex, 1);
2750
Dmitry Shmidt98660862014-03-11 17:26:21 -07002751 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2752 return ret;
2753 } else {
2754 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2755 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002756 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2757 drv->if_disabled = 1;
2758
Dmitry Shmidt98660862014-03-11 17:26:21 -07002759 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 }
2761
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002762 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002763 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2764 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002766 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2767 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2768 bss->addr))
2769 return -1;
2770 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 if (send_rfkill_event) {
2774 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2775 drv, drv->ctx);
2776 }
2777
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002778 if (drv->vendor_cmd_test_avail)
2779 qca_vendor_test(drv);
2780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 return 0;
2782}
2783
2784
Paul Stewart092955c2017-02-06 09:13:09 -08002785static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786{
2787 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08002788 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002790 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2791 drv->ifindex);
Paul Stewart092955c2017-02-06 09:13:09 -08002792 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002793 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795}
2796
2797
2798/**
2799 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002800 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 *
2802 * Shut down driver interface and processing of driver events. Free
2803 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2804 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002805static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002808 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002810 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2811 bss->ifname, drv->disabled_11b_rates);
2812
Dmitry Shmidt04949592012-07-19 12:16:46 -07002813 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002814 if (drv->data_tx_status)
2815 eloop_unregister_read_sock(drv->eapol_tx_sock);
2816 if (drv->eapol_tx_sock >= 0)
2817 close(drv->eapol_tx_sock);
2818
2819 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 wpa_driver_nl80211_probe_req_report(bss, 0);
2821 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002822 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2823 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2825 "interface %s from bridge %s: %s",
2826 bss->ifname, bss->brname, strerror(errno));
2827 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07002828
2829 if (drv->rtnl_sk)
Hai Shalomfdcde762020-04-02 11:19:20 -07002830 nl_socket_free(drv->rtnl_sk);
Hai Shalomc9e41a12018-07-31 14:41:42 -07002831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002833 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2834 0) < 0)
2835 wpa_printf(MSG_INFO,
2836 "nl80211: Could not set bridge %s down",
2837 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2840 "bridge %s: %s",
2841 bss->brname, strerror(errno));
2842 }
2843
2844 nl80211_remove_monitor_interface(drv);
2845
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 if (is_ap_interface(drv->nlmode))
Paul Stewart092955c2017-02-06 09:13:09 -08002847 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 if (drv->eapol_sock >= 0) {
2850 eloop_unregister_read_sock(drv->eapol_sock);
2851 close(drv->eapol_sock);
2852 }
2853
2854 if (drv->if_indices != drv->default_if_indices)
2855 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002857 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002860 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2861 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002862 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 rfkill_deinit(drv->rfkill);
2864
2865 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2866
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002867 if (!drv->start_iface_up)
2868 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002869
2870 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002871 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2872 0) < 0) {
2873 wpa_printf(MSG_DEBUG,
2874 "nl80211: Could not set interface down to restore permanent MAC address");
2875 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002876 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2877 drv->perm_addr) < 0) {
2878 wpa_printf(MSG_DEBUG,
2879 "nl80211: Could not restore permanent MAC address");
2880 }
2881 }
2882
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002883 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002884 if (!drv->hostapd || !drv->start_mode_ap)
2885 wpa_driver_nl80211_set_mode(bss,
2886 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002887 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002888 } else {
2889 nl80211_mgmt_unsubscribe(bss, "deinit");
2890 nl80211_del_p2pdev(bss);
2891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002893 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 os_free(drv->filter_ssids);
2896
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002897 os_free(drv->auth_ie);
2898
2899 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 dl_list_del(&drv->list);
2901
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002902 os_free(drv->extended_capa);
2903 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002904 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2905 os_free(drv->iface_ext_capa[i].ext_capa);
2906 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2907 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002908 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 os_free(drv);
2910}
2911
2912
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002913static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2914{
2915 switch (alg) {
2916 case WPA_ALG_WEP:
2917 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08002918 return RSN_CIPHER_SUITE_WEP40;
2919 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002920 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002921 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002922 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002923 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002924 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002925 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002926 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002927 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002928 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002929 return RSN_CIPHER_SUITE_GCMP_256;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002930 case WPA_ALG_BIP_CMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002931 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002932 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002933 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002934 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002935 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002936 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002937 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002938 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08002939 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002940 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08002941 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002942 case WPA_ALG_NONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002943 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2944 alg);
2945 return 0;
2946 }
2947
2948 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2949 alg);
2950 return 0;
2951}
2952
2953
2954static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2955{
2956 switch (cipher) {
2957 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002958 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002959 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002960 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002961 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002962 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002963 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002964 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002965 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002966 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002967 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08002968 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002969 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08002970 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002971 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08002972 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002973 }
2974
2975 return 0;
2976}
2977
2978
2979static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2980 int max_suites)
2981{
2982 int num_suites = 0;
2983
2984 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002985 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002986 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002987 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002988 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002989 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002990 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002991 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002992 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08002993 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002994 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08002995 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002996 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08002997 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002998
2999 return num_suites;
3000}
3001
3002
Hai Shalomfdcde762020-04-02 11:19:20 -07003003static int wpa_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3004 int max_suites)
3005{
3006 int num_suites = 0;
3007
3008#define __AKM(a, b) \
3009 if (num_suites < max_suites && \
3010 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3011 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3012 __AKM(IEEE8021X, UNSPEC_802_1X);
3013 __AKM(PSK, PSK_OVER_802_1X);
3014 __AKM(FT_IEEE8021X, FT_802_1X);
3015 __AKM(FT_PSK, FT_PSK);
3016 __AKM(IEEE8021X_SHA256, 802_1X_SHA256);
3017 __AKM(PSK_SHA256, PSK_SHA256);
3018 __AKM(SAE, SAE);
3019 __AKM(FT_SAE, FT_SAE);
3020 __AKM(CCKM, CCKM);
3021 __AKM(OSEN, OSEN);
3022 __AKM(IEEE8021X_SUITE_B, 802_1X_SUITE_B);
3023 __AKM(IEEE8021X_SUITE_B_192, 802_1X_SUITE_B_192);
3024 __AKM(FILS_SHA256, FILS_SHA256);
3025 __AKM(FILS_SHA384, FILS_SHA384);
3026 __AKM(FT_FILS_SHA256, FT_FILS_SHA256);
3027 __AKM(FT_FILS_SHA384, FT_FILS_SHA384);
3028 __AKM(OWE, OWE);
3029 __AKM(DPP, DPP);
3030 __AKM(FT_IEEE8021X_SHA384, FT_802_1X_SHA384);
3031#undef __AKM
3032
3033 return num_suites;
3034}
3035
3036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003037#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003038static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3039 const u8 *key, size_t key_len)
3040{
3041 struct nl_msg *msg;
3042 int ret;
3043
3044 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
3045 return 0;
3046
3047 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3048 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3049 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3050 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
3051 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
3052 nl80211_nlmsg_clear(msg);
3053 nlmsg_free(msg);
3054 return -1;
3055 }
3056 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
3057 if (ret) {
3058 wpa_printf(MSG_DEBUG,
3059 "nl80211: Key management set key failed: ret=%d (%s)",
3060 ret, strerror(-ret));
3061 }
3062
3063 return ret;
3064}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003065#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003066
3067
Roshan Pius3a1667e2018-07-03 15:17:14 -07003068static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
3069 const u8 *key, size_t key_len,
3070 const u8 *addr)
3071{
3072 struct nl_msg *msg = NULL;
3073 int ret;
3074
3075 /*
3076 * If the authenticator address is not set, assume it is
3077 * the current BSSID.
3078 */
3079 if (!addr && drv->associated)
3080 addr = drv->bssid;
3081 else if (!addr)
3082 return -1;
3083
3084 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
3085 MAC2STR(addr));
3086 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
3087 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
3088 if (!msg ||
3089 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3090 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
3091 nl80211_nlmsg_clear(msg);
3092 nlmsg_free(msg);
3093 return -ENOBUFS;
3094 }
3095
3096 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
3097 if (ret) {
3098 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
3099 ret, strerror(-ret));
3100 }
3101
3102 return ret;
3103}
3104
3105
Hai Shalomfdcde762020-04-02 11:19:20 -07003106static int wpa_driver_nl80211_set_key(struct i802_bss *bss,
3107 struct wpa_driver_set_key_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003110 int ifindex;
Hai Shalomc3565922019-10-28 11:58:20 -07003111 struct nl_msg *msg;
3112 struct nl_msg *key_msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003114 int skip_set_key = 1;
3115 const char *ifname = params->ifname;
3116 enum wpa_alg alg = params->alg;
3117 const u8 *addr = params->addr;
3118 int key_idx = params->key_idx;
3119 int set_tx = params->set_tx;
3120 const u8 *seq = params->seq;
3121 size_t seq_len = params->seq_len;
3122 const u8 *key = params->key;
3123 size_t key_len = params->key_len;
3124 int vlan_id = params->vlan_id;
3125 enum key_flag key_flag = params->key_flag;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003127 /* Ignore for P2P Device */
3128 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
3129 return 0;
3130
3131 ifindex = if_nametoindex(ifname);
3132 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Hai Shalomfdcde762020-04-02 11:19:20 -07003133 "set_tx=%d seq_len=%lu key_len=%lu key_flag=0x%x",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003134 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Hai Shalomfdcde762020-04-02 11:19:20 -07003135 (unsigned long) seq_len, (unsigned long) key_len, key_flag);
3136
3137 if (check_key_flag(key_flag)) {
3138 wpa_printf(MSG_DEBUG, "%s: invalid key_flag", __func__);
3139 return -EINVAL;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003142#ifdef CONFIG_DRIVER_NL80211_QCA
Hai Shalomfdcde762020-04-02 11:19:20 -07003143 if ((key_flag & KEY_FLAG_PMK) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003144 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
3145 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
3146 __func__);
3147 ret = issue_key_mgmt_set_key(drv, key, key_len);
3148 return ret;
3149 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003150#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151
Hai Shalomfdcde762020-04-02 11:19:20 -07003152 if (key_flag & KEY_FLAG_PMK) {
3153 if (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)
3154 return nl80211_set_pmk(drv, key, key_len, addr);
3155 /* The driver does not have any offload mechanism for PMK, so
3156 * there is no need to configure this key. */
3157 return 0;
3158 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003159
Hai Shalomfdcde762020-04-02 11:19:20 -07003160 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003161 key_msg = nlmsg_alloc();
3162 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003163 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003164
Hai Shalomfdcde762020-04-02 11:19:20 -07003165 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3166 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3167 wpa_printf(MSG_DEBUG,
3168 "nl80211: SET_KEY (pairwise RX/TX modify)");
3169 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3170 if (!msg)
3171 goto fail2;
3172 } else if (alg == WPA_ALG_NONE && (key_flag & KEY_FLAG_RX_TX)) {
3173 wpa_printf(MSG_DEBUG, "%s: invalid key_flag to delete key",
3174 __func__);
3175 ret = -EINVAL;
3176 goto fail2;
3177 } else if (alg == WPA_ALG_NONE) {
3178 wpa_printf(MSG_DEBUG, "nl80211: DEL_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003179 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
3180 if (!msg)
Hai Shalomc3565922019-10-28 11:58:20 -07003181 goto fail2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003183 u32 suite;
3184
3185 suite = wpa_alg_to_cipher_suite(alg, key_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07003186 if (!suite) {
3187 ret = -EINVAL;
Hai Shalomc3565922019-10-28 11:58:20 -07003188 goto fail2;
Hai Shalomfdcde762020-04-02 11:19:20 -07003189 }
3190 wpa_printf(MSG_DEBUG, "nl80211: NEW_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003191 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003192 if (!msg)
3193 goto fail2;
3194 if (nla_put(key_msg, NL80211_KEY_DATA, key_len, key) ||
3195 nla_put_u32(key_msg, NL80211_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003196 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003197 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198
Hai Shalomfdcde762020-04-02 11:19:20 -07003199 if (seq && seq_len) {
3200 if (nla_put(key_msg, NL80211_KEY_SEQ, seq_len, seq))
3201 goto fail;
3202 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ",
3203 seq, seq_len);
3204 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206
3207 if (addr && !is_broadcast_ether_addr(addr)) {
3208 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003209 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
3210 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211
Hai Shalomfdcde762020-04-02 11:19:20 -07003212 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3213 KEY_FLAG_PAIRWISE_RX ||
3214 (key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3215 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3216 if (nla_put_u8(key_msg, NL80211_KEY_MODE,
3217 key_flag == KEY_FLAG_PAIRWISE_RX ?
3218 NL80211_KEY_NO_TX : NL80211_KEY_SET_TX))
3219 goto fail;
3220 } else if ((key_flag & KEY_FLAG_GROUP_MASK) ==
3221 KEY_FLAG_GROUP_RX) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Hai Shalomc3565922019-10-28 11:58:20 -07003223 if (nla_put_u32(key_msg, NL80211_KEY_TYPE,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003224 NL80211_KEYTYPE_GROUP))
3225 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003226 } else if (!(key_flag & KEY_FLAG_PAIRWISE)) {
3227 wpa_printf(MSG_DEBUG,
3228 " key_flag missing PAIRWISE when setting a pairwise key");
3229 ret = -EINVAL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003230 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003231 } else if (alg == WPA_ALG_WEP &&
3232 (key_flag & KEY_FLAG_RX_TX) == KEY_FLAG_RX_TX) {
3233 wpa_printf(MSG_DEBUG, " unicast WEP key");
3234 skip_set_key = 0;
3235 } else {
3236 wpa_printf(MSG_DEBUG, " pairwise key");
3237 }
3238 } else if ((key_flag & KEY_FLAG_PAIRWISE) ||
3239 !(key_flag & KEY_FLAG_GROUP)) {
3240 wpa_printf(MSG_DEBUG,
3241 " invalid key_flag for a broadcast key");
3242 ret = -EINVAL;
3243 goto fail;
3244 } else {
3245 wpa_printf(MSG_DEBUG, " broadcast key");
3246 if (key_flag & KEY_FLAG_DEFAULT)
3247 skip_set_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 }
Hai Shalomc3565922019-10-28 11:58:20 -07003249 if (nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
3250 nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003251 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003252 nl80211_nlmsg_clear(key_msg);
3253 nlmsg_free(key_msg);
3254 key_msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255
Hai Shalomfdcde762020-04-02 11:19:20 -07003256 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3257 wpa_printf(MSG_DEBUG, "nl80211: VLAN ID %d", vlan_id);
3258 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3259 goto fail;
3260 }
3261
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003262 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3264 ret = 0;
3265 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003266 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 ret, strerror(-ret));
3268
3269 /*
Hai Shalomfdcde762020-04-02 11:19:20 -07003270 * If we failed or don't need to set the key as default (below),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 * we're done here.
3272 */
Hai Shalomfdcde762020-04-02 11:19:20 -07003273 if (ret || skip_set_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 return ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003275 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_SET_KEY - default key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276
Hai Shalomfdcde762020-04-02 11:19:20 -07003277 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003278 key_msg = nlmsg_alloc();
3279 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003280 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003281
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003282 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003283 if (!msg)
3284 goto fail2;
3285 if (!key_msg ||
3286 nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003287 nla_put_flag(key_msg, wpa_alg_bip(alg) ?
Hai Shalomfdcde762020-04-02 11:19:20 -07003288 (key_idx == 6 || key_idx == 7 ?
3289 NL80211_KEY_DEFAULT_BEACON :
3290 NL80211_KEY_DEFAULT_MGMT) :
3291 NL80211_KEY_DEFAULT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003292 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003294 struct nlattr *types;
3295
Hai Shalomc3565922019-10-28 11:58:20 -07003296 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003297 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003298 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003299 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003300 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003302 struct nlattr *types;
3303
Hai Shalomc3565922019-10-28 11:58:20 -07003304 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003305 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003306 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003307 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003308 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 }
3310
Hai Shalomc3565922019-10-28 11:58:20 -07003311 if (nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
3312 goto fail;
3313 nl80211_nlmsg_clear(key_msg);
3314 nlmsg_free(key_msg);
3315 key_msg = NULL;
3316
Hai Shalomfdcde762020-04-02 11:19:20 -07003317 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3318 wpa_printf(MSG_DEBUG, "nl80211: set_key default - VLAN ID %d",
3319 vlan_id);
3320 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3321 goto fail;
3322 }
3323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003326 wpa_printf(MSG_DEBUG,
3327 "nl80211: set_key default failed; err=%d %s",
3328 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 return ret;
3330
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003331fail:
3332 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003333 nlmsg_free(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07003334fail2:
3335 nl80211_nlmsg_clear(key_msg);
3336 nlmsg_free(key_msg);
Hai Shalomfdcde762020-04-02 11:19:20 -07003337 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338}
3339
3340
3341static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3342 int key_idx, int defkey,
3343 const u8 *seq, size_t seq_len,
3344 const u8 *key, size_t key_len)
3345{
3346 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003347 u32 suite;
3348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 if (!key_attr)
3350 return -1;
3351
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003352 suite = wpa_alg_to_cipher_suite(alg, key_len);
3353 if (!suite)
3354 return -1;
3355
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003356 if (defkey && wpa_alg_bip(alg)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003357 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3358 return -1;
3359 } else if (defkey) {
3360 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3361 return -1;
3362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003364 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003365 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003366 (seq && seq_len &&
3367 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3368 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3369 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370
3371 nla_nest_end(msg, key_attr);
3372
3373 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374}
3375
3376
3377static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3378 struct nl_msg *msg)
3379{
3380 int i, privacy = 0;
3381 struct nlattr *nl_keys, *nl_key;
3382
3383 for (i = 0; i < 4; i++) {
3384 if (!params->wep_key[i])
3385 continue;
3386 privacy = 1;
3387 break;
3388 }
3389 if (params->wps == WPS_MODE_PRIVACY)
3390 privacy = 1;
3391 if (params->pairwise_suite &&
3392 params->pairwise_suite != WPA_CIPHER_NONE)
3393 privacy = 1;
3394
3395 if (!privacy)
3396 return 0;
3397
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003398 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3399 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400
3401 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3402 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003403 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404
3405 for (i = 0; i < 4; i++) {
3406 if (!params->wep_key[i])
3407 continue;
3408
3409 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003410 if (!nl_key ||
3411 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3412 params->wep_key[i]) ||
3413 nla_put_u32(msg, NL80211_KEY_CIPHER,
3414 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003415 RSN_CIPHER_SUITE_WEP40 :
3416 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003417 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3418 (i == params->wep_tx_keyidx &&
3419 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3420 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003421
3422 nla_nest_end(msg, nl_key);
3423 }
3424 nla_nest_end(msg, nl_keys);
3425
3426 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427}
3428
3429
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003430int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3431 const u8 *addr, int cmd, u16 reason_code,
Hai Shalom74f70d42019-02-11 14:42:39 -08003432 int local_state_change,
Hai Shalomfdcde762020-04-02 11:19:20 -07003433 struct nl_sock *nl_connect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003435 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 struct nl_msg *msg;
3437
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003438 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3439 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3440 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3441 (local_state_change &&
3442 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3443 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446
Hai Shalom74f70d42019-02-11 14:42:39 -08003447 if (nl_connect)
3448 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
3449 else
3450 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003452 wpa_dbg(drv->ctx, MSG_DEBUG,
3453 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3454 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 return ret;
3457}
3458
3459
3460static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Hai Shalom81f62d82019-07-22 12:10:00 -07003461 u16 reason_code,
Hai Shalomfdcde762020-04-02 11:19:20 -07003462 struct nl_sock *nl_connect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003464 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003465 int drv_associated = drv->associated;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003466
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003467 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003468 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003469 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003470 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Hai Shalom74f70d42019-02-11 14:42:39 -08003471 reason_code, 0, nl_connect);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003472 /*
3473 * For locally generated disconnect, supplicant already generates a
3474 * DEAUTH event, so ignore the event from NL80211.
3475 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003476 drv->ignore_next_local_disconnect = drv_associated && (ret == 0);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003477
3478 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479}
3480
3481
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003482static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
Hai Shalom81f62d82019-07-22 12:10:00 -07003483 const u8 *addr, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003486 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003487 int drv_associated = drv->associated;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003488
3489 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3490 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003491 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003492 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003493 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003494 return wpa_driver_nl80211_disconnect(drv, reason_code,
Hai Shalomb755a2a2020-04-23 21:49:02 -07003495 get_connect_handle(bss));
Hai Shalom74f70d42019-02-11 14:42:39 -08003496 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003497 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3498 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003499 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003500 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
Hai Shalomb755a2a2020-04-23 21:49:02 -07003501 reason_code, 0, get_connect_handle(bss));
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003502 /*
3503 * For locally generated deauthenticate, supplicant already generates a
3504 * DEAUTH event, so ignore the event from NL80211.
3505 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003506 drv->ignore_next_local_deauth = drv_associated && (ret == 0);
3507
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003508 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509}
3510
3511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003512static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3513 struct wpa_driver_auth_params *params)
3514{
3515 int i;
3516
3517 drv->auth_freq = params->freq;
3518 drv->auth_alg = params->auth_alg;
3519 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3520 drv->auth_local_state_change = params->local_state_change;
3521 drv->auth_p2p = params->p2p;
3522
3523 if (params->bssid)
3524 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3525 else
3526 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3527
3528 if (params->ssid) {
3529 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3530 drv->auth_ssid_len = params->ssid_len;
3531 } else
3532 drv->auth_ssid_len = 0;
3533
3534
3535 os_free(drv->auth_ie);
3536 drv->auth_ie = NULL;
3537 drv->auth_ie_len = 0;
3538 if (params->ie) {
3539 drv->auth_ie = os_malloc(params->ie_len);
3540 if (drv->auth_ie) {
3541 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3542 drv->auth_ie_len = params->ie_len;
3543 }
3544 }
3545
3546 for (i = 0; i < 4; i++) {
3547 if (params->wep_key[i] && params->wep_key_len[i] &&
3548 params->wep_key_len[i] <= 16) {
3549 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3550 params->wep_key_len[i]);
3551 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3552 } else
3553 drv->auth_wep_key_len[i] = 0;
3554 }
3555}
3556
3557
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003558static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3559{
3560 struct wpa_driver_nl80211_data *drv = bss->drv;
3561
3562 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3563 return;
3564
3565 /*
3566 * Looks like we failed to unmask 11b rates previously. This could
3567 * happen, e.g., if the interface was down at the point in time when a
3568 * P2P group was terminated.
3569 */
3570 wpa_printf(MSG_DEBUG,
3571 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3572 bss->ifname);
3573 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3574}
3575
3576
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003577static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3578{
3579 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3580 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3581 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3582 return NL80211_AUTHTYPE_SHARED_KEY;
3583 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3584 return NL80211_AUTHTYPE_NETWORK_EAP;
3585 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3586 return NL80211_AUTHTYPE_FT;
3587 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3588 return NL80211_AUTHTYPE_SAE;
3589 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3590 return NL80211_AUTHTYPE_FILS_SK;
3591 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3592 return NL80211_AUTHTYPE_FILS_SK_PFS;
3593
3594 return NL80211_AUTHTYPE_MAX;
3595}
3596
3597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003599 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 struct wpa_driver_nl80211_data *drv = bss->drv;
3602 int ret = -1, i;
3603 struct nl_msg *msg;
3604 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003607 int is_retry;
Hai Shalomfdcde762020-04-02 11:19:20 -07003608 struct wpa_driver_set_key_params p;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003610 nl80211_unmask_11b_rates(bss);
3611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003612 is_retry = drv->retry_auth;
3613 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003614 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003616 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003618 if (params->bssid)
3619 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3620 else
3621 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003623 nlmode = params->p2p ?
3624 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3625 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003626 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 return -1;
3628
3629retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3631 drv->ifindex);
3632
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003633 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3634 if (!msg)
3635 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636
Hai Shalomfdcde762020-04-02 11:19:20 -07003637 os_memset(&p, 0, sizeof(p));
3638 p.ifname = bss->ifname;
3639 p.alg = WPA_ALG_WEP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 for (i = 0; i < 4; i++) {
3641 if (!params->wep_key[i])
3642 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07003643 p.key_idx = i;
3644 p.set_tx = i == params->wep_tx_keyidx;
3645 p.key = params->wep_key[i];
3646 p.key_len = params->wep_key_len[i];
3647 p.key_flag = i == params->wep_tx_keyidx ?
3648 KEY_FLAG_GROUP_RX_TX_DEFAULT :
3649 KEY_FLAG_GROUP_RX_TX;
3650 wpa_driver_nl80211_set_key(bss, &p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 if (params->wep_tx_keyidx != i)
3652 continue;
3653 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003654 params->wep_key[i], params->wep_key_len[i]))
3655 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656 }
3657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658 if (params->bssid) {
3659 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3660 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003661 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3662 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663 }
3664 if (params->freq) {
3665 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003666 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3667 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003668 }
3669 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003670 wpa_printf(MSG_DEBUG, " * SSID=%s",
3671 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003672 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3673 params->ssid))
3674 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 }
3676 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003677 if (params->ie &&
3678 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3679 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003680 if (params->auth_data) {
3681 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3682 params->auth_data_len);
3683 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3684 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003685 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003686 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003687 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003689 if (type == NL80211_AUTHTYPE_MAX ||
3690 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003691 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 if (params->local_state_change) {
3693 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003694 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3695 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 }
3697
3698 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3699 msg = NULL;
3700 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003701 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003702 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
3703 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003705 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
3706 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003707 /*
3708 * mac80211 does not currently accept new
3709 * authentication if we are already authenticated. As a
3710 * workaround, force deauthentication and try again.
3711 */
3712 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3713 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003714 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715 wpa_driver_nl80211_deauthenticate(
3716 bss, params->bssid,
3717 WLAN_REASON_PREV_AUTH_NOT_VALID);
3718 nlmsg_free(msg);
3719 goto retry;
3720 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003721
3722 if (ret == -ENOENT && params->freq && !is_retry) {
3723 /*
3724 * cfg80211 has likely expired the BSS entry even
3725 * though it was previously available in our internal
3726 * BSS table. To recover quickly, start a single
3727 * channel scan on the specified channel.
3728 */
3729 struct wpa_driver_scan_params scan;
3730 int freqs[2];
3731
3732 os_memset(&scan, 0, sizeof(scan));
3733 scan.num_ssids = 1;
3734 if (params->ssid) {
3735 scan.ssids[0].ssid = params->ssid;
3736 scan.ssids[0].ssid_len = params->ssid_len;
3737 }
3738 freqs[0] = params->freq;
3739 freqs[1] = 0;
3740 scan.freqs = freqs;
3741 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3742 "channel scan to refresh cfg80211 BSS "
3743 "entry");
3744 ret = wpa_driver_nl80211_scan(bss, &scan);
3745 if (ret == 0) {
3746 nl80211_copy_auth_params(drv, params);
3747 drv->scan_for_auth = 1;
3748 }
3749 } else if (is_retry) {
3750 /*
3751 * Need to indicate this with an event since the return
3752 * value from the retry is not delivered to core code.
3753 */
3754 union wpa_event_data event;
3755 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3756 "failed");
3757 os_memset(&event, 0, sizeof(event));
3758 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3759 ETH_ALEN);
3760 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3761 &event);
3762 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003763 } else {
3764 wpa_printf(MSG_DEBUG,
3765 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003768fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 nlmsg_free(msg);
3770 return ret;
3771}
3772
3773
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003774int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003775{
3776 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003777 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003778 int i;
3779
3780 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3781
3782 os_memset(&params, 0, sizeof(params));
3783 params.freq = drv->auth_freq;
3784 params.auth_alg = drv->auth_alg;
3785 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3786 params.local_state_change = drv->auth_local_state_change;
3787 params.p2p = drv->auth_p2p;
3788
3789 if (!is_zero_ether_addr(drv->auth_bssid_))
3790 params.bssid = drv->auth_bssid_;
3791
3792 if (drv->auth_ssid_len) {
3793 params.ssid = drv->auth_ssid;
3794 params.ssid_len = drv->auth_ssid_len;
3795 }
3796
3797 params.ie = drv->auth_ie;
3798 params.ie_len = drv->auth_ie_len;
3799
3800 for (i = 0; i < 4; i++) {
3801 if (drv->auth_wep_key_len[i]) {
3802 params.wep_key[i] = drv->auth_wep_key[i];
3803 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3804 }
3805 }
3806
3807 drv->retry_auth = 1;
3808 return wpa_driver_nl80211_authenticate(bss, &params);
3809}
3810
3811
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003812static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3813 size_t data_len, int noack,
3814 unsigned int freq, int no_cck,
3815 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003816 unsigned int wait_time,
3817 const u16 *csa_offs,
Hai Shalomfdcde762020-04-02 11:19:20 -07003818 size_t csa_offs_len, int no_encrypt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 struct wpa_driver_nl80211_data *drv = bss->drv;
3821 struct ieee80211_mgmt *mgmt;
Hai Shalomfdcde762020-04-02 11:19:20 -07003822 int encrypt = !no_encrypt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823 u16 fc;
Hai Shalomfdcde762020-04-02 11:19:20 -07003824 int use_cookie = 1;
3825 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826
3827 mgmt = (struct ieee80211_mgmt *) data;
3828 fc = le_to_host16(mgmt->frame_control);
Hai Shalomfdcde762020-04-02 11:19:20 -07003829 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da=" MACSTR
3830 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u no_encrypt=%d fc=0x%x (%s) nlmode=%d",
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003831 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
Hai Shalomfdcde762020-04-02 11:19:20 -07003832 no_encrypt, fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003833
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003834 if ((is_sta_interface(drv->nlmode) ||
3835 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3837 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3838 /*
3839 * The use of last_mgmt_freq is a bit of a hack,
3840 * but it works due to the single-threaded nature
3841 * of wpa_supplicant.
3842 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003843 if (freq == 0) {
3844 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3845 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003846 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003847 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003848 wait_time = 0;
3849 use_cookie = 0;
3850 no_cck = 1;
3851 offchanok = 1;
3852 goto send_frame_cmd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003854
3855 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003856 if (freq == 0) {
3857 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3858 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003859 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003860 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003861 if ((int) freq == bss->freq)
3862 wait_time = 0;
3863 goto send_frame_cmd;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003864 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3867 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3868 /*
3869 * Only one of the authentication frame types is encrypted.
3870 * In order for static WEP encryption to work properly (i.e.,
3871 * to not encrypt the frame), we need to tell mac80211 about
3872 * the frames that must not be encrypted.
3873 */
3874 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3875 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3876 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3877 encrypt = 0;
3878 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003879
Hai Shalomfdcde762020-04-02 11:19:20 -07003880 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_STATION &&
3881 (drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
3882 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
3883 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3884 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3885 freq = nl80211_get_assoc_freq(drv);
3886 wpa_printf(MSG_DEBUG,
3887 "nl80211: send_mlme - Use assoc_freq=%u for external auth",
3888 freq);
3889 }
3890
3891 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3892 freq = nl80211_get_assoc_freq(drv);
3893 wpa_printf(MSG_DEBUG,
3894 "nl80211: send_mlme - Use assoc_freq=%u for IBSS",
3895 freq);
3896 }
3897 if (freq == 0) {
3898 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - Use bss->freq=%u",
3899 bss->freq);
3900 freq = bss->freq;
3901 }
3902
3903 if (drv->use_monitor) {
3904 wpa_printf(MSG_DEBUG,
3905 "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
3906 freq, bss->freq);
3907 return nl80211_send_monitor(drv, data, data_len, encrypt,
3908 noack);
3909 }
3910
3911 if (noack || WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
3912 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
3913 use_cookie = 0;
3914send_frame_cmd:
3915#ifdef CONFIG_TESTING_OPTIONS
3916 if (no_encrypt && !encrypt && !drv->use_monitor) {
3917 wpa_printf(MSG_DEBUG,
3918 "nl80211: Request to send an unencrypted frame - use a monitor interface for this");
3919 if (nl80211_create_monitor_interface(drv) < 0)
3920 return -1;
3921 res = nl80211_send_monitor(drv, data, data_len, encrypt,
3922 noack);
3923 nl80211_remove_monitor_interface(drv);
3924 return res;
3925 }
3926#endif /* CONFIG_TESTING_OPTIONS */
3927
3928 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame_cmd");
3929 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, data_len,
3930 use_cookie, no_cck, noack, offchanok,
3931 csa_offs, csa_offs_len);
3932
3933 return res;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003934}
3935
3936
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003937static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3938{
3939 u8 rates[NL80211_MAX_SUPP_RATES];
3940 u8 rates_len = 0;
3941 int i;
3942
3943 if (!basic_rates)
3944 return 0;
3945
3946 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3947 rates[rates_len++] = basic_rates[i] / 5;
3948
3949 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3950}
3951
3952
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003953static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3954 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003955 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003956{
3957 struct wpa_driver_nl80211_data *drv = bss->drv;
3958 struct nl_msg *msg;
3959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003960 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3961 (cts >= 0 &&
3962 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3963 (preamble >= 0 &&
3964 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3965 (slot >= 0 &&
3966 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3967 (ht_opmode >= 0 &&
3968 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3969 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003970 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3971 nl80211_put_basic_rates(msg, basic_rates)) {
3972 nlmsg_free(msg);
3973 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003974 }
3975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003977}
3978
3979
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003980static int wpa_driver_nl80211_set_acl(void *priv,
3981 struct hostapd_acl_params *params)
3982{
3983 struct i802_bss *bss = priv;
3984 struct wpa_driver_nl80211_data *drv = bss->drv;
3985 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003986 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003987 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003988 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003989
3990 if (!(drv->capa.max_acl_mac_addrs))
3991 return -ENOTSUP;
3992
3993 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3994 return -ENOTSUP;
3995
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003996 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3997 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3998
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003999 acl = nlmsg_alloc();
4000 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004001 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004002 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004003 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
4004 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004005 return -ENOMEM;
4006 }
4007 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004008
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004009 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
4010 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
4011 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
4012 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
4013 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
4014 nlmsg_free(msg);
4015 nlmsg_free(acl);
4016 return -ENOMEM;
4017 }
4018 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004019
4020 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004021 if (ret) {
4022 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
4023 ret, strerror(-ret));
4024 }
4025
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004026 return ret;
4027}
4028
4029
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004030static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
4031{
4032 if (beacon_int > 0) {
4033 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
4034 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
4035 beacon_int);
4036 }
4037
4038 return 0;
4039}
4040
4041
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004042static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
4043{
4044 if (dtim_period > 0) {
4045 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
4046 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
4047 }
4048
4049 return 0;
4050}
4051
4052
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004053#ifdef CONFIG_MESH
4054static int nl80211_set_mesh_config(void *priv,
4055 struct wpa_driver_mesh_bss_params *params)
4056{
4057 struct i802_bss *bss = priv;
4058 struct wpa_driver_nl80211_data *drv = bss->drv;
4059 struct nl_msg *msg;
4060 int ret;
4061
4062 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
4063 if (!msg)
4064 return -1;
4065
4066 ret = nl80211_put_mesh_config(msg, params);
4067 if (ret < 0) {
4068 nlmsg_free(msg);
4069 return ret;
4070 }
4071
4072 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4073 if (ret) {
4074 wpa_printf(MSG_ERROR,
4075 "nl80211: Mesh config set failed: %d (%s)",
4076 ret, strerror(-ret));
4077 return ret;
4078 }
4079 return 0;
4080}
4081#endif /* CONFIG_MESH */
4082
4083
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004084static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
4085 struct wpa_driver_ap_params *params)
4086{
4087 struct nlattr *bands, *band;
4088 struct nl80211_txrate_vht vht_rate;
4089
4090 if (!params->freq ||
4091 (params->beacon_rate == 0 &&
4092 params->rate_type == BEACON_RATE_LEGACY))
4093 return 0;
4094
4095 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
4096 if (!bands)
4097 return -1;
4098
4099 switch (params->freq->mode) {
4100 case HOSTAPD_MODE_IEEE80211B:
4101 case HOSTAPD_MODE_IEEE80211G:
4102 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
4103 break;
4104 case HOSTAPD_MODE_IEEE80211A:
4105 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
4106 break;
4107 case HOSTAPD_MODE_IEEE80211AD:
4108 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
4109 break;
4110 default:
4111 return 0;
4112 }
4113
4114 if (!band)
4115 return -1;
4116
4117 os_memset(&vht_rate, 0, sizeof(vht_rate));
4118
4119 switch (params->rate_type) {
4120 case BEACON_RATE_LEGACY:
4121 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
4122 wpa_printf(MSG_INFO,
4123 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
4124 return -1;
4125 }
4126
4127 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
4128 (u8) params->beacon_rate / 5) ||
4129 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4130 (params->freq->vht_enabled &&
4131 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4132 &vht_rate)))
4133 return -1;
4134
4135 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
4136 params->beacon_rate);
4137 break;
4138 case BEACON_RATE_HT:
4139 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
4140 wpa_printf(MSG_INFO,
4141 "nl80211: Driver does not support setting Beacon frame rate (HT)");
4142 return -1;
4143 }
4144 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4145 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
4146 (params->freq->vht_enabled &&
4147 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4148 &vht_rate)))
4149 return -1;
4150 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
4151 params->beacon_rate);
4152 break;
4153 case BEACON_RATE_VHT:
4154 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
4155 wpa_printf(MSG_INFO,
4156 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
4157 return -1;
4158 }
4159 vht_rate.mcs[0] = BIT(params->beacon_rate);
4160 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
4161 return -1;
4162 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
4163 return -1;
4164 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4165 &vht_rate))
4166 return -1;
4167 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
4168 params->beacon_rate);
4169 break;
4170 }
4171
4172 nla_nest_end(msg, band);
4173 nla_nest_end(msg, bands);
4174
4175 return 0;
4176}
4177
4178
4179static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
4180 int multicast_to_unicast)
4181{
4182 struct wpa_driver_nl80211_data *drv = bss->drv;
4183 struct nl_msg *msg;
4184 int ret;
4185
4186 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
4187 if (!msg ||
4188 (multicast_to_unicast &&
4189 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
4190 wpa_printf(MSG_ERROR,
4191 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
4192 bss->ifname);
4193 nlmsg_free(msg);
4194 return -ENOBUFS;
4195 }
4196
4197 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4198
4199 switch (ret) {
4200 case 0:
4201 wpa_printf(MSG_DEBUG,
4202 "nl80211: multicast to unicast %s on interface %s",
4203 multicast_to_unicast ? "enabled" : "disabled",
4204 bss->ifname);
4205 break;
4206 case -EOPNOTSUPP:
4207 if (!multicast_to_unicast)
4208 break;
4209 wpa_printf(MSG_INFO,
4210 "nl80211: multicast to unicast not supported on interface %s",
4211 bss->ifname);
4212 break;
4213 default:
4214 wpa_printf(MSG_ERROR,
4215 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
4216 multicast_to_unicast ? "enabling" : "disabling",
4217 ret, strerror(-ret), bss->ifname);
4218 break;
4219 }
4220
4221 return ret;
4222}
4223
4224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225static int wpa_driver_nl80211_set_ap(void *priv,
4226 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227{
4228 struct i802_bss *bss = priv;
4229 struct wpa_driver_nl80211_data *drv = bss->drv;
4230 struct nl_msg *msg;
4231 u8 cmd = NL80211_CMD_NEW_BEACON;
Hai Shalom74f70d42019-02-11 14:42:39 -08004232 int ret = -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004234 int num_suites;
Hai Shalomfdcde762020-04-02 11:19:20 -07004235 u32 suites[20], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004236 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004237#ifdef CONFIG_MESH
4238 struct wpa_driver_mesh_bss_params mesh_params;
4239#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004240
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004241 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
4244 beacon_set);
4245 if (beacon_set)
4246 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08004247 else if (!drv->device_ap_sme && !drv->use_monitor &&
4248 !nl80211_get_wiphy_data_ap(bss))
4249 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004251 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
4252 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004253 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
4254 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004255 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004256 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004257 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
4258 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004259 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Hai Shalom74f70d42019-02-11 14:42:39 -08004260 wpa_printf(MSG_DEBUG, "nl80211: ssid=%s",
4261 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004262 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
4263 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
4264 params->head) ||
4265 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
4266 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004267 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004268 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004269 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004270 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4271 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004272 if (params->proberesp && params->proberesp_len) {
4273 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
4274 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004275 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
4276 params->proberesp))
4277 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004278 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004279 switch (params->hide_ssid) {
4280 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004281 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004282 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4283 NL80211_HIDDEN_SSID_NOT_IN_USE))
4284 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004285 break;
4286 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004287 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004288 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4289 NL80211_HIDDEN_SSID_ZERO_LEN))
4290 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004291 break;
4292 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004293 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004294 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4295 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
4296 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004297 break;
4298 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004299 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004300 if (params->privacy &&
4301 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
4302 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004303 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004304 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
4305 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
4306 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004307 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
4308 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4309 NL80211_AUTHTYPE_SHARED_KEY))
4310 goto fail;
4311 } else {
4312 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4313 NL80211_AUTHTYPE_OPEN_SYSTEM))
4314 goto fail;
4315 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004316
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004317 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004318 ver = 0;
4319 if (params->wpa_version & WPA_PROTO_WPA)
4320 ver |= NL80211_WPA_VERSION_1;
4321 if (params->wpa_version & WPA_PROTO_RSN)
4322 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004323 if (ver &&
4324 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4325 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004326
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004327 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
4328 params->key_mgmt_suites);
Hai Shalomfdcde762020-04-02 11:19:20 -07004329 num_suites = wpa_key_mgmt_to_suites(params->key_mgmt_suites,
4330 suites, ARRAY_SIZE(suites));
4331 if (num_suites > NL80211_MAX_NR_AKM_SUITES)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004332 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07004333 "nl80211: Not enough room for all AKM suites (num_suites=%d > NL80211_MAX_NR_AKM_SUITES)",
4334 num_suites);
4335 else if (num_suites &&
4336 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
4337 suites))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004338 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004340 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004341 (!params->pairwise_ciphers ||
4342 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
4343 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
4344 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004345 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004346
Hai Shalom81f62d82019-07-22 12:10:00 -07004347 if (drv->device_ap_sme &&
4348 (params->key_mgmt_suites & WPA_KEY_MGMT_SAE) &&
4349 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
4350 goto fail;
Hai Shalom5f92bc92019-04-18 11:54:11 -07004351
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004352 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4353 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004354 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4355 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004356 if (num_suites &&
4357 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4358 num_suites * sizeof(u32), suites))
4359 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004361 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4362 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004363 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004364 if (suite &&
4365 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4366 goto fail;
4367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004368 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004369 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4370 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004371 if (nla_put(msg, NL80211_ATTR_IE,
4372 wpabuf_len(params->beacon_ies),
4373 wpabuf_head(params->beacon_ies)))
4374 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004375 }
4376 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004377 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4378 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004379 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4380 wpabuf_len(params->proberesp_ies),
4381 wpabuf_head(params->proberesp_ies)))
4382 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 }
4384 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004385 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4386 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004387 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4388 wpabuf_len(params->assocresp_ies),
4389 wpabuf_head(params->assocresp_ies)))
4390 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392
Dmitry Shmidt04949592012-07-19 12:16:46 -07004393 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004394 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4395 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004396 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4397 params->ap_max_inactivity))
4398 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004399 }
4400
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004401#ifdef CONFIG_P2P
4402 if (params->p2p_go_ctwindow > 0) {
4403 if (drv->p2p_go_ctwindow_supported) {
4404 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4405 params->p2p_go_ctwindow);
4406 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4407 params->p2p_go_ctwindow))
4408 goto fail;
4409 } else {
4410 wpa_printf(MSG_INFO,
4411 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4412 }
4413 }
4414#endif /* CONFIG_P2P */
4415
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004416 if (params->pbss) {
4417 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4418 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4419 goto fail;
4420 }
4421
Hai Shalom74f70d42019-02-11 14:42:39 -08004422 if (params->ftm_responder) {
4423 struct nlattr *ftm;
4424
4425 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
4426 ret = -ENOTSUP;
4427 goto fail;
4428 }
4429
4430 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
4431 if (!ftm ||
4432 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
4433 (params->lci &&
4434 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
4435 wpabuf_len(params->lci),
4436 wpabuf_head(params->lci))) ||
4437 (params->civic &&
4438 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
4439 wpabuf_len(params->civic),
4440 wpabuf_head(params->civic))))
4441 goto fail;
4442 nla_nest_end(msg, ftm);
4443 }
4444
Hai Shalomc3565922019-10-28 11:58:20 -07004445#ifdef CONFIG_IEEE80211AX
4446 if (params->he_spr) {
4447 struct nlattr *spr;
4448
4449 spr = nla_nest_start(msg, NL80211_ATTR_HE_OBSS_PD);
4450 wpa_printf(MSG_DEBUG, "nl80211: he_spr=%d", params->he_spr);
4451
Hai Shalomfdcde762020-04-02 11:19:20 -07004452 if (!spr ||
4453 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET,
Hai Shalomc3565922019-10-28 11:58:20 -07004454 params->he_spr_srg_obss_pd_min_offset) ||
4455 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET,
4456 params->he_spr_srg_obss_pd_max_offset))
4457 goto fail;
4458
4459 nla_nest_end(msg, spr);
4460 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004461
4462 if (params->freq && params->freq->he_enabled) {
4463 struct nlattr *bss_color;
4464
4465 bss_color = nla_nest_start(msg, NL80211_ATTR_HE_BSS_COLOR);
4466 if (!bss_color ||
4467 (params->he_bss_color_disabled &&
4468 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_DISABLED)) ||
4469 (params->he_bss_color_partial &&
4470 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_PARTIAL)) ||
4471 nla_put_u8(msg, NL80211_HE_BSS_COLOR_ATTR_COLOR,
4472 params->he_bss_color))
4473 goto fail;
4474 nla_nest_end(msg, bss_color);
4475 }
4476
4477 if (params->twt_responder) {
4478 wpa_printf(MSG_DEBUG, "nl80211: twt_responder=%d",
4479 params->twt_responder);
4480 if (nla_put_flag(msg, NL80211_ATTR_TWT_RESPONDER))
4481 goto fail;
4482 }
Hai Shalomc3565922019-10-28 11:58:20 -07004483#endif /* CONFIG_IEEE80211AX */
4484
Hai Shalomb755a2a2020-04-23 21:49:02 -07004485 ret = send_and_recv_msgs_owner(drv, msg, get_connect_handle(bss), 1,
4486 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487 if (ret) {
4488 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4489 ret, strerror(-ret));
4490 } else {
4491 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004492 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4493 params->short_slot_time, params->ht_opmode,
4494 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004495 nl80211_set_multicast_to_unicast(bss,
4496 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004497 if (beacon_set && params->freq &&
4498 params->freq->bandwidth != bss->bandwidth) {
4499 wpa_printf(MSG_DEBUG,
4500 "nl80211: Update BSS %s bandwidth: %d -> %d",
4501 bss->ifname, bss->bandwidth,
4502 params->freq->bandwidth);
4503 ret = nl80211_set_channel(bss, params->freq, 1);
4504 if (ret) {
4505 wpa_printf(MSG_DEBUG,
4506 "nl80211: Frequency set failed: %d (%s)",
4507 ret, strerror(-ret));
4508 } else {
4509 wpa_printf(MSG_DEBUG,
4510 "nl80211: Frequency set succeeded for ht2040 coex");
4511 bss->bandwidth = params->freq->bandwidth;
4512 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004513 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004514 /*
4515 * cfg80211 updates the driver on frequence change in AP
4516 * mode only at the point when beaconing is started, so
4517 * set the initial value here.
4518 */
4519 bss->bandwidth = params->freq->bandwidth;
4520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004522
4523#ifdef CONFIG_MESH
4524 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4525 os_memset(&mesh_params, 0, sizeof(mesh_params));
4526 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4527 mesh_params.ht_opmode = params->ht_opmode;
4528 ret = nl80211_set_mesh_config(priv, &mesh_params);
4529 if (ret < 0)
4530 return ret;
4531 }
4532#endif /* CONFIG_MESH */
4533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004535fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004536 nlmsg_free(msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08004537 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538}
4539
4540
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004541static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004542 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543{
Hai Shalomc3565922019-10-28 11:58:20 -07004544 enum hostapd_hw_mode hw_mode;
4545 int is_24ghz;
4546 u8 channel;
4547
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004548 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004549 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4550 return -ENOBUFS;
4551
Hai Shalom81f62d82019-07-22 12:10:00 -07004552 wpa_printf(MSG_DEBUG, " * he_enabled=%d", freq->he_enabled);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004553 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4554 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4555
Hai Shalomc3565922019-10-28 11:58:20 -07004556 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
4557 is_24ghz = hw_mode == HOSTAPD_MODE_IEEE80211G ||
4558 hw_mode == HOSTAPD_MODE_IEEE80211B;
4559
4560 if (freq->vht_enabled || (freq->he_enabled && !is_24ghz)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004561 enum nl80211_chan_width cw;
4562
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004563 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004564 switch (freq->bandwidth) {
4565 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004566 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004567 break;
4568 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004569 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004570 break;
4571 case 80:
4572 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004573 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004574 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004575 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004576 break;
4577 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004578 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004579 break;
4580 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004581 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004582 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004583
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004584 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4585 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4586 freq->center_freq1);
4587 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4588 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004589 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4590 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4591 freq->center_freq1) ||
4592 (freq->center_freq2 &&
4593 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4594 freq->center_freq2)))
4595 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004596 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004597 enum nl80211_channel_type ct;
4598
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004599 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4600 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004601 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004603 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 break;
4605 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004606 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 break;
4608 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004609 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610 break;
4611 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004612
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004613 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004614 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4615 return -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07004616 } else if (freq->edmg.channels && freq->edmg.bw_config) {
4617 wpa_printf(MSG_DEBUG,
4618 " * EDMG configuration: channels=0x%x bw_config=%d",
4619 freq->edmg.channels, freq->edmg.bw_config);
4620 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
4621 freq->edmg.channels) ||
4622 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
4623 freq->edmg.bw_config))
4624 return -1;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004625 } else {
4626 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4627 NL80211_CHAN_NO_HT);
4628 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4629 NL80211_CHAN_NO_HT))
4630 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004632 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004633}
4634
4635
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004636static int nl80211_set_channel(struct i802_bss *bss,
4637 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004638{
4639 struct wpa_driver_nl80211_data *drv = bss->drv;
4640 struct nl_msg *msg;
4641 int ret;
4642
4643 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07004644 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, he_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4645 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004646 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004647
4648 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4649 NL80211_CMD_SET_WIPHY);
4650 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4651 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004652 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654
4655 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004656 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004657 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004661 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 return -1;
4663}
4664
4665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004666static u32 sta_flags_nl80211(int flags)
4667{
4668 u32 f = 0;
4669
4670 if (flags & WPA_STA_AUTHORIZED)
4671 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4672 if (flags & WPA_STA_WMM)
4673 f |= BIT(NL80211_STA_FLAG_WME);
4674 if (flags & WPA_STA_SHORT_PREAMBLE)
4675 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4676 if (flags & WPA_STA_MFP)
4677 f |= BIT(NL80211_STA_FLAG_MFP);
4678 if (flags & WPA_STA_TDLS_PEER)
4679 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004680 if (flags & WPA_STA_AUTHENTICATED)
4681 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004682 if (flags & WPA_STA_ASSOCIATED)
4683 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004684
4685 return f;
4686}
4687
4688
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004689#ifdef CONFIG_MESH
4690static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4691{
4692 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004693 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004694 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004695 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004696 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004697 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004698 return NL80211_PLINK_OPN_RCVD;
4699 case PLINK_CNF_RCVD:
4700 return NL80211_PLINK_CNF_RCVD;
4701 case PLINK_ESTAB:
4702 return NL80211_PLINK_ESTAB;
4703 case PLINK_HOLDING:
4704 return NL80211_PLINK_HOLDING;
4705 case PLINK_BLOCKED:
4706 return NL80211_PLINK_BLOCKED;
4707 default:
4708 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4709 state);
4710 }
4711 return -1;
4712}
4713#endif /* CONFIG_MESH */
4714
4715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716static int wpa_driver_nl80211_sta_add(void *priv,
4717 struct hostapd_sta_add_params *params)
4718{
4719 struct i802_bss *bss = priv;
4720 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004721 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004722 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 int ret = -ENOBUFS;
4724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004725 if ((params->flags & WPA_STA_TDLS_PEER) &&
4726 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4727 return -EOPNOTSUPP;
4728
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004729 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4730 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004731 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4732 NL80211_CMD_NEW_STATION);
4733 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4734 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004736 /*
4737 * Set the below properties only in one of the following cases:
4738 * 1. New station is added, already associated.
4739 * 2. Set WPA_STA_TDLS_PEER station.
4740 * 3. Set an already added unassociated station, if driver supports
4741 * full AP client state. (Set these properties after station became
4742 * associated will be rejected by the driver).
4743 */
4744 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4745 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4746 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004747 wpa_hexdump(MSG_DEBUG, " * supported rates",
4748 params->supp_rates, params->supp_rates_len);
4749 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4750 params->capability);
4751 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4752 params->supp_rates_len, params->supp_rates) ||
4753 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4754 params->capability))
4755 goto fail;
4756
4757 if (params->ht_capabilities) {
4758 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4759 (u8 *) params->ht_capabilities,
4760 sizeof(*params->ht_capabilities));
4761 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4762 sizeof(*params->ht_capabilities),
4763 params->ht_capabilities))
4764 goto fail;
4765 }
4766
4767 if (params->vht_capabilities) {
4768 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4769 (u8 *) params->vht_capabilities,
4770 sizeof(*params->vht_capabilities));
4771 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4772 sizeof(*params->vht_capabilities),
4773 params->vht_capabilities))
4774 goto fail;
4775 }
4776
Hai Shalom81f62d82019-07-22 12:10:00 -07004777 if (params->he_capab) {
4778 wpa_hexdump(MSG_DEBUG, " * he_capab",
4779 params->he_capab, params->he_capab_len);
4780 if (nla_put(msg, NL80211_ATTR_HE_CAPABILITY,
4781 params->he_capab_len, params->he_capab))
4782 goto fail;
4783 }
4784
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004785 if (params->ext_capab) {
4786 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4787 params->ext_capab, params->ext_capab_len);
4788 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4789 params->ext_capab_len, params->ext_capab))
4790 goto fail;
4791 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004792
4793 if (is_ap_interface(drv->nlmode) &&
4794 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4795 params->support_p2p_ps ?
4796 NL80211_P2P_PS_SUPPORTED :
4797 NL80211_P2P_PS_UNSUPPORTED))
4798 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004799 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004800 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004801 if (params->aid) {
4802 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004803 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4804 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004805 } else {
4806 /*
4807 * cfg80211 validates that AID is non-zero, so we have
4808 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004809 * a dummy STA entry is used for now and for a station
4810 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004811 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004812 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4813 (params->flags & WPA_STA_TDLS_PEER) ?
4814 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004815 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4816 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004817 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004818 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4819 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004820 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4821 params->listen_interval))
4822 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004823 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4824 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004825 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4826 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004827 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4828 (params->flags & WPA_STA_ASSOCIATED)) {
4829 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4830 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4831 params->listen_interval);
4832 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4833 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4834 params->listen_interval))
4835 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004836 }
4837
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004838 if (params->vht_opmode_enabled) {
4839 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004840 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4841 params->vht_opmode))
4842 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004843 }
4844
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004845 if (params->supp_channels) {
4846 wpa_hexdump(MSG_DEBUG, " * supported channels",
4847 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004848 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4849 params->supp_channels_len, params->supp_channels))
4850 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004851 }
4852
4853 if (params->supp_oper_classes) {
4854 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4855 params->supp_oper_classes,
4856 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004857 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4858 params->supp_oper_classes_len,
4859 params->supp_oper_classes))
4860 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004861 }
4862
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004863 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004864 upd.set = sta_flags_nl80211(params->flags);
4865 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004866
4867 /*
4868 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4869 * flags, as nl80211 driver moves a new station, by default, into
4870 * associated state.
4871 *
4872 * On the other hand, if the driver supports that feature and the
4873 * station is added in unauthenticated state, set the
4874 * authenticated/associated bits in the mask to prevent moving this
4875 * station to associated state before it is actually associated.
4876 *
4877 * This is irrelevant for mesh mode where the station is added to the
4878 * driver as authenticated already, and ASSOCIATED isn't part of the
4879 * nl80211 API.
4880 */
4881 if (!is_mesh_interface(drv->nlmode)) {
4882 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4883 wpa_printf(MSG_DEBUG,
4884 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4885 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4886 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4887 } else if (!params->set &&
4888 !(params->flags & WPA_STA_TDLS_PEER)) {
4889 if (!(params->flags & WPA_STA_AUTHENTICATED))
4890 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4891 if (!(params->flags & WPA_STA_ASSOCIATED))
4892 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4893 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004894#ifdef CONFIG_MESH
4895 } else {
4896 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4897 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4898 params->peer_aid);
4899 if (ret)
4900 goto fail;
4901 }
4902#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004903 }
4904
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004905 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4906 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004907 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4908 goto fail;
4909
4910#ifdef CONFIG_MESH
4911 if (params->plink_state &&
4912 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4913 sta_plink_state_nl80211(params->plink_state)))
4914 goto fail;
4915#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004916
Hai Shalomc3565922019-10-28 11:58:20 -07004917 if ((!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4918 FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) &&
4919 (params->flags & WPA_STA_WMM)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004920 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4921
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004922 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004923 if (!wme ||
4924 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4925 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4926 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4927 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4928 WMM_QOSINFO_STA_SP_MASK))
4929 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004930 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004931 }
4932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004934 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004936 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4937 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4938 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 if (ret == -EEXIST)
4940 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004941fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004942 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 return ret;
4944}
4945
4946
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004947static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4948{
4949#ifdef CONFIG_LIBNL3_ROUTE
4950 struct wpa_driver_nl80211_data *drv = bss->drv;
4951 struct rtnl_neigh *rn;
4952 struct nl_addr *nl_addr;
4953 int err;
4954
4955 rn = rtnl_neigh_alloc();
4956 if (!rn)
4957 return;
4958
4959 rtnl_neigh_set_family(rn, AF_BRIDGE);
4960 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4961 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4962 if (!nl_addr) {
4963 rtnl_neigh_put(rn);
4964 return;
4965 }
4966 rtnl_neigh_set_lladdr(rn, nl_addr);
4967
4968 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4969 if (err < 0) {
4970 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4971 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4972 bss->ifindex, nl_geterror(err));
4973 } else {
4974 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4975 MACSTR, MAC2STR(addr));
4976 }
4977
4978 nl_addr_put(nl_addr);
4979 rtnl_neigh_put(rn);
4980#endif /* CONFIG_LIBNL3_ROUTE */
4981}
4982
4983
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004984static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4985 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 struct wpa_driver_nl80211_data *drv = bss->drv;
4988 struct nl_msg *msg;
4989 int ret;
4990
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004991 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4992 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4993 (deauth == 0 &&
4994 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4995 WLAN_FC_STYPE_DISASSOC)) ||
4996 (deauth == 1 &&
4997 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4998 WLAN_FC_STYPE_DEAUTH)) ||
4999 (reason_code &&
5000 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
5001 nlmsg_free(msg);
5002 return -ENOBUFS;
5003 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004
5005 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005006 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
5007 " --> %d (%s)",
5008 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005009
5010 if (drv->rtnl_sk)
5011 rtnl_neigh_delete_fdb_entry(bss, addr);
5012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005013 if (ret == -ENOENT)
5014 return 0;
5015 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016}
5017
5018
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005019void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020{
5021 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005022 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005023
5024 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005027 dl_list_for_each(drv2, &drv->global->interfaces,
5028 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005029 {
5030 del_ifidx(drv2, ifidx, IFIDX_ANY);
5031 /* Remove all bridges learned for this iface */
5032 del_ifidx(drv2, IFIDX_ANY, ifidx);
5033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005035 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5037 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
5039}
5040
5041
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005042const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005043{
5044 switch (mode) {
5045 case NL80211_IFTYPE_ADHOC:
5046 return "ADHOC";
5047 case NL80211_IFTYPE_STATION:
5048 return "STATION";
5049 case NL80211_IFTYPE_AP:
5050 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07005051 case NL80211_IFTYPE_AP_VLAN:
5052 return "AP_VLAN";
5053 case NL80211_IFTYPE_WDS:
5054 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005055 case NL80211_IFTYPE_MONITOR:
5056 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07005057 case NL80211_IFTYPE_MESH_POINT:
5058 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059 case NL80211_IFTYPE_P2P_CLIENT:
5060 return "P2P_CLIENT";
5061 case NL80211_IFTYPE_P2P_GO:
5062 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005063 case NL80211_IFTYPE_P2P_DEVICE:
5064 return "P2P_DEVICE";
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005065 case NL80211_IFTYPE_OCB:
5066 return "OCB";
5067 case NL80211_IFTYPE_NAN:
5068 return "NAN";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 default:
5070 return "unknown";
5071 }
5072}
5073
5074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
5076 const char *ifname,
5077 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005078 const u8 *addr, int wds,
5079 int (*handler)(struct nl_msg *, void *),
5080 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005082 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083 int ifidx;
5084 int ret = -ENOBUFS;
5085
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005086 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
5087 iftype, nl80211_iftype_str(iftype));
5088
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005089 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
5090 if (!msg ||
5091 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
5092 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
5093 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094
5095 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005096 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005098 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005099 if (!flags ||
5100 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
5101 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005103 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005105 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
5106 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107 }
5108
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005109 /*
5110 * Tell cfg80211 that the interface belongs to the socket that created
5111 * it, and the interface should be deleted when the socket is closed.
5112 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005113 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5114 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005115
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005116 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005117 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005118 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005119 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005120 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
5122 ifname, ret, strerror(-ret));
5123 return ret;
5124 }
5125
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005126 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
5127 return 0;
5128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 ifidx = if_nametoindex(ifname);
5130 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
5131 ifname, ifidx);
5132
5133 if (ifidx <= 0)
5134 return -1;
5135
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005136 /*
5137 * Some virtual interfaces need to process EAPOL packets and events on
5138 * the parent interface. This is used mainly with hostapd.
5139 */
5140 if (drv->hostapd ||
5141 iftype == NL80211_IFTYPE_AP_VLAN ||
5142 iftype == NL80211_IFTYPE_WDS ||
5143 iftype == NL80211_IFTYPE_MONITOR) {
5144 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005145 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147
5148 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005149 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150 nl80211_remove_iface(drv, ifidx);
5151 return -1;
5152 }
5153
5154 return ifidx;
5155}
5156
5157
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005158int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
5159 const char *ifname, enum nl80211_iftype iftype,
5160 const u8 *addr, int wds,
5161 int (*handler)(struct nl_msg *, void *),
5162 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163{
5164 int ret;
5165
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005166 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
5167 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005169 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005171 if (use_existing) {
5172 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
5173 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005174 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
5175 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
5176 addr) < 0 &&
5177 (linux_set_iface_flags(drv->global->ioctl_sock,
5178 ifname, 0) < 0 ||
5179 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
5180 addr) < 0 ||
5181 linux_set_iface_flags(drv->global->ioctl_sock,
5182 ifname, 1) < 0))
5183 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005184 return -ENFILE;
5185 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
5187
5188 /* Try to remove the interface that was already there. */
5189 nl80211_remove_iface(drv, if_nametoindex(ifname));
5190
5191 /* Try to create the interface again */
5192 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005193 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005194 }
5195
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005196 if (ret >= 0 && is_p2p_net_interface(iftype)) {
5197 wpa_printf(MSG_DEBUG,
5198 "nl80211: Interface %s created for P2P - disable 11b rates",
5199 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202
5203 return ret;
5204}
5205
5206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005207static int nl80211_setup_ap(struct i802_bss *bss)
5208{
5209 struct wpa_driver_nl80211_data *drv = bss->drv;
5210
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005211 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
5212 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005213
5214 /*
5215 * Disable Probe Request reporting unless we need it in this way for
5216 * devices that include the AP SME, in the other case (unless using
5217 * monitor iface) we'll get it through the nl_mgmt socket instead.
5218 */
5219 if (!drv->device_ap_sme)
5220 wpa_driver_nl80211_probe_req_report(bss, 0);
5221
5222 if (!drv->device_ap_sme && !drv->use_monitor)
5223 if (nl80211_mgmt_subscribe_ap(bss))
5224 return -1;
5225
5226 if (drv->device_ap_sme && !drv->use_monitor)
5227 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005228 wpa_printf(MSG_DEBUG,
5229 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005230
5231 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07005232 nl80211_create_monitor_interface(drv) &&
5233 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005234 return -1;
5235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005236 if (drv->device_ap_sme &&
5237 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
5238 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
5239 "Probe Request frame reporting in AP mode");
5240 /* Try to survive without this */
5241 }
5242
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005243 return 0;
5244}
5245
5246
5247static void nl80211_teardown_ap(struct i802_bss *bss)
5248{
5249 struct wpa_driver_nl80211_data *drv = bss->drv;
5250
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005251 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
5252 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005253 if (drv->device_ap_sme) {
5254 wpa_driver_nl80211_probe_req_report(bss, 0);
5255 if (!drv->use_monitor)
5256 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
5257 } else if (drv->use_monitor)
5258 nl80211_remove_monitor_interface(drv);
5259 else
5260 nl80211_mgmt_unsubscribe(bss, "AP teardown");
5261
Paul Stewart092955c2017-02-06 09:13:09 -08005262 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 bss->beacon_set = 0;
5264}
5265
5266
Hai Shalomfdcde762020-04-02 11:19:20 -07005267static int nl80211_tx_control_port(void *priv, const u8 *dest,
5268 u16 proto, const u8 *buf, size_t len,
5269 int no_encrypt)
5270{
5271 struct i802_bss *bss = priv;
5272 struct nl_msg *msg;
5273 int ret;
5274
5275 wpa_printf(MSG_DEBUG,
5276 "nl80211: Send over control port dest=" MACSTR
5277 " proto=0x%04x len=%u no_encrypt=%d",
5278 MAC2STR(dest), proto, (unsigned int) len, no_encrypt);
5279
5280 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CONTROL_PORT_FRAME);
5281 if (!msg ||
5282 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
5283 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dest) ||
5284 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
5285 (no_encrypt &&
5286 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))) {
5287 nlmsg_free(msg);
5288 return -ENOBUFS;
5289 }
5290
5291 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5292 if (ret)
5293 wpa_printf(MSG_DEBUG,
5294 "nl80211: tx_control_port failed: ret=%d (%s)",
5295 ret, strerror(-ret));
5296
5297 return ret;
5298}
5299
5300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005301static int nl80211_send_eapol_data(struct i802_bss *bss,
5302 const u8 *addr, const u8 *data,
5303 size_t data_len)
5304{
5305 struct sockaddr_ll ll;
5306 int ret;
5307
5308 if (bss->drv->eapol_tx_sock < 0) {
5309 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
5310 return -1;
5311 }
5312
5313 os_memset(&ll, 0, sizeof(ll));
5314 ll.sll_family = AF_PACKET;
5315 ll.sll_ifindex = bss->ifindex;
5316 ll.sll_protocol = htons(ETH_P_PAE);
5317 ll.sll_halen = ETH_ALEN;
5318 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
5319 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
5320 (struct sockaddr *) &ll, sizeof(ll));
5321 if (ret < 0)
5322 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
5323 strerror(errno));
5324
5325 return ret;
5326}
5327
5328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
5330
5331static int wpa_driver_nl80211_hapd_send_eapol(
5332 void *priv, const u8 *addr, const u8 *data,
5333 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
5334{
5335 struct i802_bss *bss = priv;
5336 struct wpa_driver_nl80211_data *drv = bss->drv;
5337 struct ieee80211_hdr *hdr;
5338 size_t len;
5339 u8 *pos;
5340 int res;
5341 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08005342
Hai Shalomb755a2a2020-04-23 21:49:02 -07005343 /* For now, disable EAPOL TX over control port in AP mode by default
5344 * since it does not provide TX status notifications. */
5345 if (drv->control_port_ap &&
5346 (drv->capa.flags & WPA_DRIVER_FLAGS_CONTROL_PORT))
Hai Shalomfdcde762020-04-02 11:19:20 -07005347 return nl80211_tx_control_port(bss, addr, ETH_P_EAPOL,
5348 data, data_len, !encrypt);
5349
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005350 if (drv->device_ap_sme || !drv->use_monitor)
5351 return nl80211_send_eapol_data(bss, addr, data, data_len);
5352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
5354 data_len;
5355 hdr = os_zalloc(len);
5356 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005357 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
5358 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 return -1;
5360 }
5361
5362 hdr->frame_control =
5363 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
5364 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
5365 if (encrypt)
5366 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
5367 if (qos) {
5368 hdr->frame_control |=
5369 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
5370 }
5371
5372 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
5373 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
5374 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
5375 pos = (u8 *) (hdr + 1);
5376
5377 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005378 /* Set highest priority in QoS header */
5379 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380 pos[1] = 0;
5381 pos += 2;
5382 }
5383
5384 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5385 pos += sizeof(rfc1042_header);
5386 WPA_PUT_BE16(pos, ETH_P_PAE);
5387 pos += 2;
5388 memcpy(pos, data, data_len);
5389
Hai Shalomfdcde762020-04-02 11:19:20 -07005390 res = nl80211_send_monitor(drv, hdr, len, encrypt, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391 if (res < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005392 wpa_printf(MSG_ERROR,
5393 "hapd_send_eapol - packet len: %lu - failed",
5394 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395 }
5396 os_free(hdr);
5397
5398 return res;
5399}
5400
5401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005402static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005403 unsigned int total_flags,
5404 unsigned int flags_or,
5405 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406{
5407 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005408 struct nl_msg *msg;
5409 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005410 struct nl80211_sta_flag_update upd;
5411
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005412 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
5413 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
5414 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
5415 !!(total_flags & WPA_STA_AUTHORIZED));
5416
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005417 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5418 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
5419 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420
5421 /*
5422 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5423 * can be removed eventually.
5424 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005425 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005426 if (!flags ||
5427 ((total_flags & WPA_STA_AUTHORIZED) &&
5428 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
5429 ((total_flags & WPA_STA_WMM) &&
5430 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
5431 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
5432 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
5433 ((total_flags & WPA_STA_MFP) &&
5434 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
5435 ((total_flags & WPA_STA_TDLS_PEER) &&
5436 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
5437 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005438
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005439 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440
5441 os_memset(&upd, 0, sizeof(upd));
5442 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5443 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005444 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5445 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005447 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5448fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005449 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 return -ENOBUFS;
5451}
5452
5453
Hai Shalom81f62d82019-07-22 12:10:00 -07005454static int driver_nl80211_sta_set_airtime_weight(void *priv, const u8 *addr,
5455 unsigned int weight)
5456{
5457 struct i802_bss *bss = priv;
5458 struct nl_msg *msg;
5459
5460 wpa_printf(MSG_DEBUG,
5461 "nl80211: Set STA airtime weight - ifname=%s addr=" MACSTR
5462 " weight=%u", bss->ifname, MAC2STR(addr), weight);
5463
5464 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5465 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5466 nla_put_u16(msg, NL80211_ATTR_AIRTIME_WEIGHT, weight))
5467 goto fail;
5468
5469 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5470fail:
5471 nlmsg_free(msg);
5472 return -ENOBUFS;
5473}
5474
5475
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005476static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5477 struct wpa_driver_associate_params *params)
5478{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005479 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005480
5481 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5483 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005484 nlmode = NL80211_IFTYPE_P2P_GO;
5485 } else
5486 nlmode = NL80211_IFTYPE_AP;
5487
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005488 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005489 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005490 nl80211_remove_monitor_interface(drv);
5491 return -1;
5492 }
5493
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005494 if (params->freq.freq &&
5495 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005496 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005497 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005498 nl80211_remove_monitor_interface(drv);
5499 return -1;
5500 }
5501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 return 0;
5503}
5504
5505
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005506static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5507 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508{
5509 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005510 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005512 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Hai Shalomb755a2a2020-04-23 21:49:02 -07005513 ret = send_and_recv_msgs_owner(drv, msg,
5514 get_connect_handle(drv->first_bss), 1,
5515 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516 if (ret) {
5517 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5518 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005519 } else {
5520 wpa_printf(MSG_DEBUG,
5521 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005522 }
5523
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005524 if (reset_mode &&
5525 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005526 NL80211_IFTYPE_STATION)) {
5527 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5528 "station mode");
5529 }
5530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005531 return ret;
5532}
5533
5534
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005535static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5536 struct wpa_driver_associate_params *params)
5537{
5538 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5539 return -1;
5540
5541 if (params->htcaps && params->htcaps_mask) {
5542 int sz = sizeof(struct ieee80211_ht_capabilities);
5543 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5544 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5545 params->htcaps_mask, sz);
5546 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5547 params->htcaps) ||
5548 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5549 params->htcaps_mask))
5550 return -1;
5551 }
5552
5553#ifdef CONFIG_VHT_OVERRIDES
5554 if (params->disable_vht) {
5555 wpa_printf(MSG_DEBUG, " * VHT disabled");
5556 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5557 return -1;
5558 }
5559
5560 if (params->vhtcaps && params->vhtcaps_mask) {
5561 int sz = sizeof(struct ieee80211_vht_capabilities);
5562 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5563 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5564 params->vhtcaps_mask, sz);
5565 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5566 params->vhtcaps) ||
5567 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5568 params->vhtcaps_mask))
5569 return -1;
5570 }
5571#endif /* CONFIG_VHT_OVERRIDES */
5572
5573 return 0;
5574}
5575
5576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5578 struct wpa_driver_associate_params *params)
5579{
5580 struct nl_msg *msg;
5581 int ret = -1;
5582 int count = 0;
5583
5584 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5585
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005586 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5588 "IBSS mode");
5589 return -1;
5590 }
5591
5592retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005593 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5594 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5595 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596
Hai Shalom74f70d42019-02-11 14:42:39 -08005597 wpa_printf(MSG_DEBUG, " * SSID=%s",
5598 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005599 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5600 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5602 drv->ssid_len = params->ssid_len;
5603
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005604 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5605 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005606 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607
5608 ret = nl80211_set_conn_keys(params, msg);
5609 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005612 if (params->bssid && params->fixed_bssid) {
5613 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5614 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005615 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5616 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005617 }
5618
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005619 if (params->fixed_freq) {
5620 wpa_printf(MSG_DEBUG, " * fixed_freq");
5621 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5622 goto fail;
5623 }
5624
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005625 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5626 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5627 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5628 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005629 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005630 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5631 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005632 }
5633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 if (params->wpa_ie) {
5635 wpa_hexdump(MSG_DEBUG,
5636 " * Extra IEs for Beacon/Probe Response frames",
5637 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005638 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5639 params->wpa_ie))
5640 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 }
5642
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005643 ret = nl80211_ht_vht_overrides(msg, params);
5644 if (ret < 0)
5645 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005646
Hai Shalomb755a2a2020-04-23 21:49:02 -07005647 ret = send_and_recv_msgs_owner(drv, msg,
5648 get_connect_handle(drv->first_bss), 1,
5649 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 msg = NULL;
5651 if (ret) {
5652 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5653 ret, strerror(-ret));
5654 count++;
5655 if (ret == -EALREADY && count == 1) {
5656 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5657 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005658 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 nlmsg_free(msg);
5660 goto retry;
5661 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005662 } else {
5663 wpa_printf(MSG_DEBUG,
5664 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005667fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005668 nlmsg_free(msg);
5669 return ret;
5670}
5671
5672
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005673static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5674 struct wpa_driver_associate_params *params,
5675 struct nl_msg *msg)
5676{
5677 if (params->fils_erp_username_len) {
5678 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5679 params->fils_erp_username,
5680 params->fils_erp_username_len);
5681 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5682 params->fils_erp_username_len,
5683 params->fils_erp_username))
5684 return -1;
5685 }
5686
5687 if (params->fils_erp_realm_len) {
5688 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5689 params->fils_erp_realm,
5690 params->fils_erp_realm_len);
5691 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5692 params->fils_erp_realm_len, params->fils_erp_realm))
5693 return -1;
5694 }
5695
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005696 if (params->fils_erp_rrk_len) {
Vinita S. Maloo3a5b4412020-05-19 17:43:22 +05305697 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5698 params->fils_erp_next_seq_num);
5699 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5700 params->fils_erp_next_seq_num))
5701 return -1;
5702
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005703 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5704 (unsigned long) params->fils_erp_rrk_len);
5705 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5706 params->fils_erp_rrk_len, params->fils_erp_rrk))
5707 return -1;
5708 }
5709
5710 return 0;
5711}
5712
5713
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005714static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5715 struct wpa_driver_associate_params *params,
5716 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717{
Paul Stewart092955c2017-02-06 09:13:09 -08005718 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5719 return -1;
5720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721 if (params->bssid) {
5722 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5723 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005724 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5725 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005727
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005728 if (params->bssid_hint) {
5729 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5730 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005731 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5732 params->bssid_hint))
5733 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005734 }
5735
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005736 if (params->freq.freq) {
5737 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005738 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5739 params->freq.freq))
5740 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005741 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005742 } else
5743 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005744
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005745 if (params->freq_hint) {
5746 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005747 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5748 params->freq_hint))
5749 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005750 }
5751
Hai Shalomc3565922019-10-28 11:58:20 -07005752 if (params->freq.edmg.channels && params->freq.edmg.bw_config) {
5753 wpa_printf(MSG_DEBUG,
5754 " * EDMG configuration: channels=0x%x bw_config=%d",
5755 params->freq.edmg.channels,
5756 params->freq.edmg.bw_config);
5757 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
5758 params->freq.edmg.channels) ||
5759 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
5760 params->freq.edmg.bw_config))
5761 return -1;
5762 }
5763
Dmitry Shmidt04949592012-07-19 12:16:46 -07005764 if (params->bg_scan_period >= 0) {
5765 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5766 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005767 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5768 params->bg_scan_period))
5769 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005770 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08005773 wpa_printf(MSG_DEBUG, " * SSID=%s",
5774 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005775 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5776 params->ssid))
5777 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005779 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005780 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5781 drv->ssid_len = params->ssid_len;
5782 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005784 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005785 if (params->wpa_ie &&
5786 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5787 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005789 if (params->wpa_proto) {
5790 enum nl80211_wpa_versions ver = 0;
5791
5792 if (params->wpa_proto & WPA_PROTO_WPA)
5793 ver |= NL80211_WPA_VERSION_1;
5794 if (params->wpa_proto & WPA_PROTO_RSN)
5795 ver |= NL80211_WPA_VERSION_2;
5796
5797 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005798 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5799 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005800 }
5801
5802 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5803 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5804 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005805 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5806 cipher))
5807 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005808 }
5809
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005810 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5811 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5812 /*
5813 * This is likely to work even though many drivers do not
5814 * advertise support for operations without GTK.
5815 */
5816 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5817 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005818 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5819 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005820 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5821 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005822 }
5823
5824 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5825 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5826 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5827 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005828 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005829 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5830 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005831 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07005832 params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
5833 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005834 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005835 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07005836 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005837 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5838 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5839 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07005840 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5841 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5842 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
Paul Stewart092955c2017-02-06 09:13:09 -08005843 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005844
5845 switch (params->key_mgmt_suite) {
5846 case WPA_KEY_MGMT_CCKM:
Paul Stewart092955c2017-02-06 09:13:09 -08005847 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005848 break;
5849 case WPA_KEY_MGMT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005850 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005851 break;
5852 case WPA_KEY_MGMT_FT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005853 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005854 break;
5855 case WPA_KEY_MGMT_FT_PSK:
Paul Stewart092955c2017-02-06 09:13:09 -08005856 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005857 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005858 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005859 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005860 break;
5861 case WPA_KEY_MGMT_PSK_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005862 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005863 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005864 case WPA_KEY_MGMT_OSEN:
Paul Stewart092955c2017-02-06 09:13:09 -08005865 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005866 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07005867 case WPA_KEY_MGMT_SAE:
5868 mgmt = RSN_AUTH_KEY_MGMT_SAE;
5869 break;
5870 case WPA_KEY_MGMT_FT_SAE:
5871 mgmt = RSN_AUTH_KEY_MGMT_FT_SAE;
5872 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005873 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Paul Stewart092955c2017-02-06 09:13:09 -08005874 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005875 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005876 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Paul Stewart092955c2017-02-06 09:13:09 -08005877 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005878 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07005879 case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
5880 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
5881 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005882 case WPA_KEY_MGMT_FILS_SHA256:
5883 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5884 break;
5885 case WPA_KEY_MGMT_FILS_SHA384:
5886 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5887 break;
5888 case WPA_KEY_MGMT_FT_FILS_SHA256:
5889 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5890 break;
5891 case WPA_KEY_MGMT_FT_FILS_SHA384:
5892 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5893 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005894 case WPA_KEY_MGMT_OWE:
5895 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5896 break;
5897 case WPA_KEY_MGMT_DPP:
5898 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5899 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005900 case WPA_KEY_MGMT_PSK:
5901 default:
Paul Stewart092955c2017-02-06 09:13:09 -08005902 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005903 break;
5904 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005905 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005906 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5907 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005908 }
5909
Hai Shalomc3565922019-10-28 11:58:20 -07005910 if (params->req_handshake_offload &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005911 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)) {
5912 wpa_printf(MSG_DEBUG, " * WANT_1X_4WAY_HS");
5913 if (nla_put_flag(msg, NL80211_ATTR_WANT_1X_4WAY_HS))
5914 return -1;
5915 }
5916
Roshan Pius3a1667e2018-07-03 15:17:14 -07005917 /* Add PSK in case of 4-way handshake offload */
5918 if (params->psk &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005919 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005920 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5921 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5922 return -1;
5923 }
5924
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005925 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5926 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005927
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005928 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5929 (params->pairwise_suite == WPA_CIPHER_NONE ||
5930 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5931 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5932 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5933 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5934 return -1;
5935
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005936 if (params->rrm_used) {
5937 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005938 if ((!((drv_rrm_flags &
5939 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5940 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5941 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005942 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5943 return -1;
5944 }
5945
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005946 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005947 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005948
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005949 if (params->p2p)
5950 wpa_printf(MSG_DEBUG, " * P2P group");
5951
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005952 if (params->pbss) {
5953 wpa_printf(MSG_DEBUG, " * PBSS");
5954 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5955 return -1;
5956 }
5957
Dmitry Shmidte4663042016-04-04 10:07:49 -07005958 drv->connect_reassoc = 0;
5959 if (params->prev_bssid) {
5960 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5961 MAC2STR(params->prev_bssid));
5962 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5963 params->prev_bssid))
5964 return -1;
5965 drv->connect_reassoc = 1;
5966 }
5967
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005968 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5969 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5970 return -1;
5971
Hai Shalomc3565922019-10-28 11:58:20 -07005972 if ((params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
5973 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005974 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5975 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5976 return -1;
5977
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005978 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005979}
5980
5981
5982static int wpa_driver_nl80211_try_connect(
5983 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005984 struct wpa_driver_associate_params *params,
Hai Shalomfdcde762020-04-02 11:19:20 -07005985 struct nl_sock *nl_connect)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005986{
5987 struct nl_msg *msg;
5988 enum nl80211_auth_type type;
5989 int ret;
5990 int algs;
5991
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005992#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005993 if (params->req_key_mgmt_offload && params->psk &&
5994 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5995 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5996 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5997 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5998 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5999 if (ret)
6000 return ret;
6001 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006002#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006003
6004 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
6005 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006006 if (!msg)
6007 return -1;
6008
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006009 ret = nl80211_connect_common(drv, params, msg);
6010 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006011 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006012
Roshan Pius3a1667e2018-07-03 15:17:14 -07006013 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
6014 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
6015 goto fail;
6016
6017 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
6018 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
6019 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
6020 goto fail;
6021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 algs = 0;
6023 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6024 algs++;
6025 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6026 algs++;
6027 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6028 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006029 if (params->auth_alg & WPA_AUTH_ALG_FILS)
6030 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006031 if (params->auth_alg & WPA_AUTH_ALG_FT)
6032 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033 if (algs > 1) {
6034 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
6035 "selection");
6036 goto skip_auth_type;
6037 }
6038
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006039 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006041 if (type == NL80211_AUTHTYPE_MAX ||
6042 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006043 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044
6045skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 ret = nl80211_set_conn_keys(params, msg);
6047 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006048 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006049
Hai Shalomb755a2a2020-04-23 21:49:02 -07006050 ret = send_and_recv_msgs_owner(drv, msg, nl_connect, 1, NULL,
6051 (void *) -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006052 msg = NULL;
6053 if (ret) {
6054 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
6055 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006056 } else {
6057 wpa_printf(MSG_DEBUG,
6058 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006061fail:
Hai Shalom74f70d42019-02-11 14:42:39 -08006062 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063 nlmsg_free(msg);
6064 return ret;
6065
6066}
6067
6068
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006069static int wpa_driver_nl80211_connect(
6070 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006071 struct wpa_driver_associate_params *params,
Hai Shalomfdcde762020-04-02 11:19:20 -07006072 struct nl_sock *nl_connect)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006073{
Jithu Jancea7c60b42014-12-03 18:54:40 +05306074 int ret;
6075
6076 /* Store the connection attempted bssid for future use */
6077 if (params->bssid)
6078 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6079 else
6080 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
6081
Roshan Pius3a1667e2018-07-03 15:17:14 -07006082 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006083 if (ret == -EALREADY) {
6084 /*
6085 * cfg80211 does not currently accept new connections if
6086 * we are already connected. As a workaround, force
6087 * disconnection and try again.
6088 */
6089 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
6090 "disconnecting before reassociation "
6091 "attempt");
6092 if (wpa_driver_nl80211_disconnect(
Hai Shalom74f70d42019-02-11 14:42:39 -08006093 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, nl_connect))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006094 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006095 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006096 }
6097 return ret;
6098}
6099
6100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006101static int wpa_driver_nl80211_associate(
6102 void *priv, struct wpa_driver_associate_params *params)
6103{
6104 struct i802_bss *bss = priv;
6105 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006106 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107 struct nl_msg *msg;
6108
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006109 nl80211_unmask_11b_rates(bss);
6110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111 if (params->mode == IEEE80211_MODE_AP)
6112 return wpa_driver_nl80211_ap(drv, params);
6113
6114 if (params->mode == IEEE80211_MODE_IBSS)
6115 return wpa_driver_nl80211_ibss(drv, params);
6116
6117 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006118 enum nl80211_iftype nlmode = params->p2p ?
6119 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6120
6121 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07006123 if (params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
Hai Shalomb755a2a2020-04-23 21:49:02 -07006124 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE)
Hai Shalom74f70d42019-02-11 14:42:39 -08006125 bss->use_nl_connect = 1;
Hai Shalomb755a2a2020-04-23 21:49:02 -07006126 else
Hai Shalom74f70d42019-02-11 14:42:39 -08006127 bss->use_nl_connect = 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08006128
Hai Shalomb755a2a2020-04-23 21:49:02 -07006129 return wpa_driver_nl80211_connect(drv, params,
6130 get_connect_handle(bss));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006131 }
6132
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006133 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006134
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006135 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
6136 drv->ifindex);
6137 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138 if (!msg)
6139 return -1;
6140
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006141 ret = nl80211_connect_common(drv, params, msg);
6142 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006143 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006144
Roshan Pius3a1667e2018-07-03 15:17:14 -07006145 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
6146 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
6147 goto fail;
6148
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006149 if (params->fils_kek) {
6150 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
6151 (unsigned int) params->fils_kek_len);
6152 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
6153 params->fils_kek))
6154 goto fail;
6155 }
6156 if (params->fils_nonces) {
6157 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
6158 params->fils_nonces,
6159 params->fils_nonces_len);
6160 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
6161 params->fils_nonces_len, params->fils_nonces))
6162 goto fail;
6163 }
6164
Hai Shalomb755a2a2020-04-23 21:49:02 -07006165 ret = send_and_recv_msgs_owner(drv, msg,
6166 get_connect_handle(drv->first_bss), 1,
6167 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 msg = NULL;
6169 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006170 wpa_dbg(drv->ctx, MSG_DEBUG,
6171 "nl80211: MLME command failed (assoc): ret=%d (%s)",
6172 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006174 } else {
6175 wpa_printf(MSG_DEBUG,
6176 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006179fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180 nlmsg_free(msg);
6181 return ret;
6182}
6183
6184
6185static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006186 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187{
6188 struct nl_msg *msg;
6189 int ret = -ENOBUFS;
6190
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
6192 ifindex, mode, nl80211_iftype_str(mode));
6193
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006194 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
6195 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
6196 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197
6198 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006199 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 if (!ret)
6201 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006202fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006203 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
6205 " %d (%s)", ifindex, mode, ret, strerror(-ret));
6206 return ret;
6207}
6208
6209
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006210static int wpa_driver_nl80211_set_mode_impl(
6211 struct i802_bss *bss,
6212 enum nl80211_iftype nlmode,
6213 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215 struct wpa_driver_nl80211_data *drv = bss->drv;
6216 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218 int was_ap = is_ap_interface(drv->nlmode);
6219 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006220 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07006222 if (TEST_FAIL())
6223 return -1;
6224
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006225 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
6226 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
6227 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006228
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006229 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 drv->nlmode = nlmode;
6231 ret = 0;
6232 goto done;
6233 }
6234
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006235 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006236 return -1;
6237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238 if (nlmode == drv->nlmode) {
6239 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
6240 "requested mode - ignore error");
6241 ret = 0;
6242 goto done; /* Already in the requested mode */
6243 }
6244
6245 /* mac80211 doesn't allow mode changes while the device is up, so
6246 * take the device down, try to set the mode again, and bring the
6247 * device back up.
6248 */
6249 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
6250 "interface down");
6251 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006252 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006253 if (res == -EACCES || res == -ENODEV)
6254 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006255 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
6257 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006258 os_sleep(0, 100000);
6259 continue;
6260 }
6261
6262 /*
6263 * Setting the mode will fail for some drivers if the phy is
6264 * on a frequency that the mode is disallowed in.
6265 */
6266 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006267 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006268 if (res) {
6269 wpa_printf(MSG_DEBUG,
6270 "nl80211: Failed to set frequency on interface");
6271 }
6272 }
6273
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006274 if (i == 0 && was_ap && !is_ap_interface(nlmode) &&
6275 bss->brname[0] &&
6276 (bss->added_if_into_bridge || bss->already_in_bridge)) {
6277 wpa_printf(MSG_DEBUG,
6278 "nl80211: Remove AP interface %s temporarily from the bridge %s to allow its mode to be set to STATION",
6279 bss->ifname, bss->brname);
6280 if (linux_br_del_if(drv->global->ioctl_sock,
6281 bss->brname, bss->ifname) < 0)
6282 wpa_printf(MSG_INFO,
6283 "nl80211: Failed to remove interface %s from bridge %s: %s",
6284 bss->ifname, bss->brname,
6285 strerror(errno));
6286 }
6287
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006288 /* Try to set the mode again while the interface is down */
6289 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
6290 if (mode_switch_res == -EBUSY) {
6291 wpa_printf(MSG_DEBUG,
6292 "nl80211: Delaying mode set while interface going down");
6293 os_sleep(0, 100000);
6294 continue;
6295 }
6296 ret = mode_switch_res;
6297 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298 }
6299
6300 if (!ret) {
6301 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
6302 "interface is down");
6303 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006304 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006305 }
6306
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006307 /* Bring the interface back up */
6308 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
6309 if (res != 0) {
6310 wpa_printf(MSG_DEBUG,
6311 "nl80211: Failed to set interface up after switching mode");
6312 ret = -1;
6313 }
6314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006315done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006316 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
6318 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006319 return ret;
6320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006322 if (is_p2p_net_interface(nlmode)) {
6323 wpa_printf(MSG_DEBUG,
6324 "nl80211: Interface %s mode change to P2P - disable 11b rates",
6325 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006326 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006327 } else if (drv->disabled_11b_rates) {
6328 wpa_printf(MSG_DEBUG,
6329 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
6330 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006331 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006332 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006334 if (is_ap_interface(nlmode)) {
6335 nl80211_mgmt_unsubscribe(bss, "start AP");
6336 /* Setup additional AP mode functionality if needed */
6337 if (nl80211_setup_ap(bss))
6338 return -1;
6339 } else if (was_ap) {
6340 /* Remove additional AP mode functionality */
6341 nl80211_teardown_ap(bss);
6342 } else {
6343 nl80211_mgmt_unsubscribe(bss, "mode change");
6344 }
6345
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006346 if (is_mesh_interface(nlmode) &&
6347 nl80211_mgmt_subscribe_mesh(bss))
6348 return -1;
6349
Dmitry Shmidt04949592012-07-19 12:16:46 -07006350 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006351 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006352 nl80211_mgmt_subscribe_non_ap(bss) < 0)
6353 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
6354 "frame processing - ignore for now");
6355
6356 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357}
6358
6359
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006360void nl80211_restore_ap_mode(struct i802_bss *bss)
6361{
6362 struct wpa_driver_nl80211_data *drv = bss->drv;
6363 int was_ap = is_ap_interface(drv->nlmode);
6364
6365 wpa_driver_nl80211_set_mode(bss, drv->ap_scan_as_station);
6366 if (!was_ap && is_ap_interface(drv->ap_scan_as_station) &&
6367 bss->brname[0] &&
6368 (bss->added_if_into_bridge || bss->already_in_bridge)) {
6369 wpa_printf(MSG_DEBUG,
6370 "nl80211: Add AP interface %s back into the bridge %s",
6371 bss->ifname, bss->brname);
6372 if (linux_br_add_if(drv->global->ioctl_sock, bss->brname,
6373 bss->ifname) < 0) {
6374 wpa_printf(MSG_WARNING,
6375 "nl80211: Failed to add interface %s into bridge %s: %s",
6376 bss->ifname, bss->brname, strerror(errno));
6377 }
6378 }
6379 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
6380}
6381
6382
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006383int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
6384 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006385{
6386 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
6387}
6388
6389
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006390static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
6391 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006392{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006393 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006394 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006395}
6396
6397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006398static int wpa_driver_nl80211_get_capa(void *priv,
6399 struct wpa_driver_capa *capa)
6400{
6401 struct i802_bss *bss = priv;
6402 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07006403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404 if (!drv->has_capability)
6405 return -1;
6406 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006407 if (drv->extended_capa && drv->extended_capa_mask) {
6408 capa->extended_capa = drv->extended_capa;
6409 capa->extended_capa_mask = drv->extended_capa_mask;
6410 capa->extended_capa_len = drv->extended_capa_len;
6411 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006412
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006413 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006414}
6415
6416
6417static int wpa_driver_nl80211_set_operstate(void *priv, int state)
6418{
6419 struct i802_bss *bss = priv;
6420 struct wpa_driver_nl80211_data *drv = bss->drv;
6421
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006422 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
6423 bss->ifname, drv->operstate, state,
6424 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006425 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006426 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006427 state ? IF_OPER_UP : IF_OPER_DORMANT);
6428}
6429
6430
6431static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
6432{
6433 struct i802_bss *bss = priv;
6434 struct wpa_driver_nl80211_data *drv = bss->drv;
6435 struct nl_msg *msg;
6436 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006437 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006438
6439 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
6440 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
6441 return 0;
6442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006443
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006444 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
6445 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
6446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006447 os_memset(&upd, 0, sizeof(upd));
6448 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
6449 if (authorized)
6450 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006451
6452 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6453 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
6454 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
6455 nlmsg_free(msg);
6456 return -ENOBUFS;
6457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006458
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006459 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006460 if (!ret)
6461 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006462 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
6463 ret, strerror(-ret));
6464 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006465}
6466
6467
Jouni Malinen75ecf522011-06-27 15:19:46 -07006468/* Set kernel driver on given frequency (MHz) */
6469static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006470{
Jouni Malinen75ecf522011-06-27 15:19:46 -07006471 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006472 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006473}
6474
6475
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006476static inline int min_int(int a, int b)
6477{
6478 if (a < b)
6479 return a;
6480 return b;
6481}
6482
6483
6484static int get_key_handler(struct nl_msg *msg, void *arg)
6485{
6486 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6487 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6488
6489 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6490 genlmsg_attrlen(gnlh, 0), NULL);
6491
6492 /*
6493 * TODO: validate the key index and mac address!
6494 * Otherwise, there's a race condition as soon as
6495 * the kernel starts sending key notifications.
6496 */
6497
6498 if (tb[NL80211_ATTR_KEY_SEQ])
6499 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
6500 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
Hai Shalom021b0b52019-04-10 11:17:58 -07006501 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006502 return NL_SKIP;
6503}
6504
6505
6506static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
6507 int idx, u8 *seq)
6508{
6509 struct i802_bss *bss = priv;
6510 struct wpa_driver_nl80211_data *drv = bss->drv;
6511 struct nl_msg *msg;
6512
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006513 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
6514 NL80211_CMD_GET_KEY);
6515 if (!msg ||
6516 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
6517 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
6518 nlmsg_free(msg);
6519 return -ENOBUFS;
6520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521
6522 memset(seq, 0, 6);
6523
6524 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525}
6526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006527
6528static int i802_set_rts(void *priv, int rts)
6529{
6530 struct i802_bss *bss = priv;
6531 struct wpa_driver_nl80211_data *drv = bss->drv;
6532 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006533 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006534 u32 val;
6535
Hai Shalom021b0b52019-04-10 11:17:58 -07006536 if (rts >= 2347 || rts == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006537 val = (u32) -1;
6538 else
6539 val = rts;
6540
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006541 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6542 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
6543 nlmsg_free(msg);
6544 return -ENOBUFS;
6545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006546
6547 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6548 if (!ret)
6549 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
6551 "%d (%s)", rts, ret, strerror(-ret));
6552 return ret;
6553}
6554
6555
6556static int i802_set_frag(void *priv, int frag)
6557{
6558 struct i802_bss *bss = priv;
6559 struct wpa_driver_nl80211_data *drv = bss->drv;
6560 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006561 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006562 u32 val;
6563
Hai Shalom021b0b52019-04-10 11:17:58 -07006564 if (frag >= 2346 || frag == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006565 val = (u32) -1;
6566 else
6567 val = frag;
6568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006569 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6570 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6571 nlmsg_free(msg);
6572 return -ENOBUFS;
6573 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574
6575 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6576 if (!ret)
6577 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6579 "%d: %d (%s)", frag, ret, strerror(-ret));
6580 return ret;
6581}
6582
6583
6584static int i802_flush(void *priv)
6585{
6586 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006588 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006589
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006590 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6591 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592
6593 /*
6594 * XXX: FIX! this needs to flush all VLANs too
6595 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006596 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6597 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006598 if (res) {
6599 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6600 "(%s)", res, strerror(-res));
6601 }
6602 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006603}
6604
6605
Hai Shalom81f62d82019-07-22 12:10:00 -07006606static void get_sta_tid_stats(struct hostap_sta_driver_data *data,
6607 struct nlattr *attr)
6608{
6609 struct nlattr *tid_stats[NL80211_TID_STATS_MAX + 1], *tidattr;
6610 struct nlattr *txq_stats[NL80211_TXQ_STATS_MAX + 1];
6611 static struct nla_policy txq_stats_policy[NL80211_TXQ_STATS_MAX + 1] = {
6612 [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
6613 [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
6614 };
6615 int rem;
6616
6617 nla_for_each_nested(tidattr, attr, rem) {
6618 if (nla_parse_nested(tid_stats, NL80211_TID_STATS_MAX,
6619 tidattr, NULL) != 0 ||
6620 !tid_stats[NL80211_TID_STATS_TXQ_STATS] ||
6621 nla_parse_nested(txq_stats, NL80211_TXQ_STATS_MAX,
6622 tid_stats[NL80211_TID_STATS_TXQ_STATS],
6623 txq_stats_policy) != 0)
6624 continue;
6625 /* sum the backlogs over all TIDs for station */
6626 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES])
6627 data->backlog_bytes += nla_get_u32(
6628 txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES]);
6629 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS])
6630 data->backlog_bytes += nla_get_u32(
6631 txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS]);
6632 }
6633}
6634
6635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006636static int get_sta_handler(struct nl_msg *msg, void *arg)
6637{
6638 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6639 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6640 struct hostap_sta_driver_data *data = arg;
6641 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6642 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6643 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6644 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6645 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6646 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6647 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006648 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006649 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6650 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006651 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07006652 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
Hai Shalom81f62d82019-07-22 12:10:00 -07006653 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
6654 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006655 };
6656 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6657 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6658 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6659 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6660 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6661 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6662 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6663 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664 };
6665
6666 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6667 genlmsg_attrlen(gnlh, 0), NULL);
6668
6669 /*
6670 * TODO: validate the interface and mac address!
6671 * Otherwise, there's a race condition as soon as
6672 * the kernel starts sending station notifications.
6673 */
6674
6675 if (!tb[NL80211_ATTR_STA_INFO]) {
6676 wpa_printf(MSG_DEBUG, "sta stats missing!");
6677 return NL_SKIP;
6678 }
6679 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6680 tb[NL80211_ATTR_STA_INFO],
6681 stats_policy)) {
6682 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6683 return NL_SKIP;
6684 }
6685
6686 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6687 data->inactive_msec =
6688 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006689 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 if (stats[NL80211_STA_INFO_RX_BYTES])
6691 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6692 if (stats[NL80211_STA_INFO_TX_BYTES])
6693 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006694 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6695 stats[NL80211_STA_INFO_TX_BYTES64]) {
6696 /*
6697 * The driver supports 64-bit counters, so use them to override
6698 * the 32-bit values.
6699 */
6700 data->rx_bytes =
6701 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6702 data->tx_bytes =
6703 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6704 data->bytes_64bit = 1;
6705 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006706 if (stats[NL80211_STA_INFO_RX_PACKETS])
6707 data->rx_packets =
6708 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6709 if (stats[NL80211_STA_INFO_TX_PACKETS])
6710 data->tx_packets =
6711 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Hai Shalom81f62d82019-07-22 12:10:00 -07006712 if (stats[NL80211_STA_INFO_RX_DURATION])
6713 data->rx_airtime =
6714 nla_get_u64(stats[NL80211_STA_INFO_RX_DURATION]);
6715 if (stats[NL80211_STA_INFO_TX_DURATION])
6716 data->tx_airtime =
6717 nla_get_u64(stats[NL80211_STA_INFO_TX_DURATION]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006718 if (stats[NL80211_STA_INFO_TX_FAILED])
6719 data->tx_retry_failed =
6720 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006721 if (stats[NL80211_STA_INFO_SIGNAL])
6722 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006723 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6724 data->last_ack_rssi =
6725 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6726 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6727 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006728
6729 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6730 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6731 stats[NL80211_STA_INFO_TX_BITRATE],
6732 rate_policy) == 0) {
6733 if (rate[NL80211_RATE_INFO_BITRATE32])
6734 data->current_tx_rate =
6735 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6736 else if (rate[NL80211_RATE_INFO_BITRATE])
6737 data->current_tx_rate =
6738 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6739
6740 if (rate[NL80211_RATE_INFO_MCS]) {
6741 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6742 data->flags |= STA_DRV_DATA_TX_MCS;
6743 }
6744 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6745 data->tx_vhtmcs =
6746 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6747 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6748 }
6749 if (rate[NL80211_RATE_INFO_SHORT_GI])
6750 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6751 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6752 data->tx_vht_nss =
6753 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6754 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6755 }
6756 }
6757
6758 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6759 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6760 stats[NL80211_STA_INFO_RX_BITRATE],
6761 rate_policy) == 0) {
6762 if (rate[NL80211_RATE_INFO_BITRATE32])
6763 data->current_rx_rate =
6764 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6765 else if (rate[NL80211_RATE_INFO_BITRATE])
6766 data->current_rx_rate =
6767 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6768
6769 if (rate[NL80211_RATE_INFO_MCS]) {
6770 data->rx_mcs =
6771 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6772 data->flags |= STA_DRV_DATA_RX_MCS;
6773 }
6774 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6775 data->rx_vhtmcs =
6776 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6777 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6778 }
6779 if (rate[NL80211_RATE_INFO_SHORT_GI])
6780 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6781 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6782 data->rx_vht_nss =
6783 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6784 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6785 }
6786 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006787
Hai Shalom81f62d82019-07-22 12:10:00 -07006788 if (stats[NL80211_STA_INFO_TID_STATS])
6789 get_sta_tid_stats(data, stats[NL80211_STA_INFO_TID_STATS]);
6790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791 return NL_SKIP;
6792}
6793
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006794static int i802_read_sta_data(struct i802_bss *bss,
6795 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006796 const u8 *addr)
6797{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006798 struct nl_msg *msg;
6799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006800 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6801 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6802 nlmsg_free(msg);
6803 return -ENOBUFS;
6804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006805
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006806 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006807}
6808
6809
6810static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6811 int cw_min, int cw_max, int burst_time)
6812{
6813 struct i802_bss *bss = priv;
6814 struct wpa_driver_nl80211_data *drv = bss->drv;
6815 struct nl_msg *msg;
6816 struct nlattr *txq, *params;
Hai Shalom74f70d42019-02-11 14:42:39 -08006817 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006818
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006819 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006820 if (!msg)
6821 return -1;
6822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6824 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006825 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826
6827 /* We are only sending parameters for a single TXQ at a time */
6828 params = nla_nest_start(msg, 1);
6829 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006830 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006831
6832 switch (queue) {
6833 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006834 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6835 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836 break;
6837 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006838 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6839 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840 break;
6841 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006842 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6843 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844 break;
6845 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006846 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6847 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006848 break;
6849 }
6850 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6851 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006852 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6853 (burst_time * 100 + 16) / 32) ||
6854 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6855 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6856 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6857 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006858
6859 nla_nest_end(msg, params);
6860
6861 nla_nest_end(msg, txq);
6862
Hai Shalom74f70d42019-02-11 14:42:39 -08006863 res = send_and_recv_msgs(drv, msg, NULL, NULL);
6864 wpa_printf(MSG_DEBUG,
6865 "nl80211: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
6866 queue, aifs, cw_min, cw_max, burst_time, res);
6867 if (res == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006869 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006870fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006871 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 return -1;
6873}
6874
6875
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006876static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006877 const char *ifname, int vlan_id)
6878{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006879 struct wpa_driver_nl80211_data *drv = bss->drv;
6880 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006881 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006882
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006883 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6884 ", ifname=%s[%d], vlan_id=%d)",
6885 bss->ifname, if_nametoindex(bss->ifname),
6886 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006887 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6888 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
Hai Shalomfdcde762020-04-02 11:19:20 -07006889 ((drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD) &&
6890 nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006891 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6892 nlmsg_free(msg);
6893 return -ENOBUFS;
6894 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006895
6896 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6897 if (ret < 0) {
6898 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6899 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6900 MAC2STR(addr), ifname, vlan_id, ret,
6901 strerror(-ret));
6902 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006903 return ret;
6904}
6905
6906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907static int i802_get_inact_sec(void *priv, const u8 *addr)
6908{
6909 struct hostap_sta_driver_data data;
6910 int ret;
6911
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006912 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006913 data.inactive_msec = (unsigned long) -1;
6914 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006915 if (ret == -ENOENT)
6916 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917 if (ret || data.inactive_msec == (unsigned long) -1)
6918 return -1;
6919 return data.inactive_msec / 1000;
6920}
6921
6922
6923static int i802_sta_clear_stats(void *priv, const u8 *addr)
6924{
6925#if 0
6926 /* TODO */
6927#endif
6928 return 0;
6929}
6930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931
6932static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07006933 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934{
6935 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006936 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006938 u8 channel;
6939
6940 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6941 HOSTAPD_MODE_IEEE80211AD) {
6942 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6943 * disassociate the STA instead. */
6944 return i802_sta_disassoc(priv, own_addr, addr, reason);
6945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006947 if (is_mesh_interface(drv->nlmode))
6948 return -1;
6949
Dmitry Shmidt04949592012-07-19 12:16:46 -07006950 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006951 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006953 memset(&mgmt, 0, sizeof(mgmt));
6954 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6955 WLAN_FC_STYPE_DEAUTH);
6956 memcpy(mgmt.da, addr, ETH_ALEN);
6957 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6958 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6959 mgmt.u.deauth.reason_code = host_to_le16(reason);
6960 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6961 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006962 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -07006963 0, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006964}
6965
6966
6967static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07006968 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006969{
6970 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006971 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 struct ieee80211_mgmt mgmt;
6973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006974 if (is_mesh_interface(drv->nlmode))
6975 return -1;
6976
Dmitry Shmidt04949592012-07-19 12:16:46 -07006977 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006978 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006980 memset(&mgmt, 0, sizeof(mgmt));
6981 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6982 WLAN_FC_STYPE_DISASSOC);
6983 memcpy(mgmt.da, addr, ETH_ALEN);
6984 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6985 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6986 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6987 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6988 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006989 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -07006990 0, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991}
6992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006993
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006994static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6995{
6996 char buf[200], *pos, *end;
6997 int i, res;
6998
6999 pos = buf;
7000 end = pos + sizeof(buf);
7001
7002 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007003 if (!drv->if_indices[i].ifindex)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007004 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007005 res = os_snprintf(pos, end - pos, " %d(%d)",
Hai Shalom81f62d82019-07-22 12:10:00 -07007006 drv->if_indices[i].ifindex,
7007 drv->if_indices[i].reason);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007008 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007009 break;
7010 pos += res;
7011 }
7012 *pos = '\0';
7013
7014 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
7015 drv->num_if_indices, buf);
7016}
7017
7018
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007019static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
7020 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007021{
7022 int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07007023 struct drv_nl80211_if_info *old;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007024
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007025 wpa_printf(MSG_DEBUG,
7026 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
7027 ifidx, ifidx_reason);
7028 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007029 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
7030 ifidx);
7031 return;
7032 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007033 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007034 if (drv->if_indices[i].ifindex == 0) {
7035 drv->if_indices[i].ifindex = ifidx;
7036 drv->if_indices[i].reason = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007037 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007038 return;
7039 }
7040 }
7041
7042 if (drv->if_indices != drv->default_if_indices)
7043 old = drv->if_indices;
7044 else
7045 old = NULL;
7046
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007047 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
Hai Shalom81f62d82019-07-22 12:10:00 -07007048 sizeof(*old));
Jouni Malinen75ecf522011-06-27 15:19:46 -07007049 if (!drv->if_indices) {
7050 if (!old)
7051 drv->if_indices = drv->default_if_indices;
7052 else
7053 drv->if_indices = old;
7054 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7055 "interfaces");
7056 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7057 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007058 }
7059 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007060 os_memcpy(drv->if_indices, drv->default_if_indices,
7061 sizeof(drv->default_if_indices));
Hai Shalom81f62d82019-07-22 12:10:00 -07007062 drv->if_indices[drv->num_if_indices].ifindex = ifidx;
7063 drv->if_indices[drv->num_if_indices].reason = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007064 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007065 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007066}
7067
7068
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007069static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
7070 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007071{
7072 int i;
7073
7074 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007075 if ((drv->if_indices[i].ifindex == ifidx ||
7076 ifidx == IFIDX_ANY) &&
7077 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007078 ifidx_reason == IFIDX_ANY)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007079 drv->if_indices[i].ifindex = 0;
7080 drv->if_indices[i].reason = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007081 break;
7082 }
7083 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007084 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007085}
7086
7087
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007088static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
7089 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007090{
7091 int i;
7092
7093 for (i = 0; i < drv->num_if_indices; i++)
Hai Shalom81f62d82019-07-22 12:10:00 -07007094 if (drv->if_indices[i].ifindex == ifidx &&
7095 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007096 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07007097 return 1;
7098
7099 return 0;
7100}
7101
7102
7103static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007104 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007105{
7106 struct i802_bss *bss = priv;
7107 struct wpa_driver_nl80211_data *drv = bss->drv;
7108 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07007109 union wpa_event_data event;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08007110 int ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007111
Hai Shalom39ba6fc2019-01-22 12:40:38 -08007112 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7113 if (ret >= (int) sizeof(name))
7114 wpa_printf(MSG_WARNING,
7115 "nl80211: WDS interface name was truncated");
7116 else if (ret < 0)
7117 return ret;
7118
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007119 if (ifname_wds)
7120 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
7121
Jouni Malinen75ecf522011-06-27 15:19:46 -07007122 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7123 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7124 if (val) {
7125 if (!if_nametoindex(name)) {
7126 if (nl80211_create_iface(drv, name,
7127 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007128 bss->addr, 1, NULL, NULL, 0) <
7129 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007130 return -1;
7131 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007132 linux_br_add_if(drv->global->ioctl_sock,
7133 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007134 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007135
7136 os_memset(&event, 0, sizeof(event));
7137 event.wds_sta_interface.sta_addr = addr;
7138 event.wds_sta_interface.ifname = name;
7139 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07007140 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007141 EVENT_WDS_STA_INTERFACE_STATUS,
7142 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007143 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007144 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
7145 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
7146 "interface %s up", name);
7147 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007148 return i802_set_sta_vlan(priv, addr, name, 0);
7149 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08007150 if (bridge_ifname &&
7151 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
7152 name) < 0)
7153 wpa_printf(MSG_INFO,
7154 "nl80211: Failed to remove interface %s from bridge %s: %s",
7155 name, bridge_ifname, strerror(errno));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007156
Jouni Malinen75ecf522011-06-27 15:19:46 -07007157 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007158 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07007159 os_memset(&event, 0, sizeof(event));
7160 event.wds_sta_interface.sta_addr = addr;
7161 event.wds_sta_interface.ifname = name;
7162 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07007163 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007164 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007165 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007166 }
7167}
7168
7169
7170static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7171{
7172 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7173 struct sockaddr_ll lladdr;
7174 unsigned char buf[3000];
7175 int len;
7176 socklen_t fromlen = sizeof(lladdr);
7177
7178 len = recvfrom(sock, buf, sizeof(buf), 0,
7179 (struct sockaddr *)&lladdr, &fromlen);
7180 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007181 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
7182 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07007183 return;
7184 }
7185
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007186 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07007187 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7188}
7189
7190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007191static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
7192 struct i802_bss *bss,
7193 const char *brname, const char *ifname)
7194{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007195 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196 char in_br[IFNAMSIZ];
7197
7198 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007199 br_ifindex = if_nametoindex(brname);
7200 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007201 /*
7202 * Bridge was configured, but the bridge device does
7203 * not exist. Try to add it now.
7204 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007205 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007206 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
7207 "bridge interface %s: %s",
7208 brname, strerror(errno));
7209 return -1;
7210 }
7211 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007212 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007213 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007214 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007215 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007216
7217 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07007218 if (os_strcmp(in_br, brname) == 0) {
7219 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07007221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007222
7223 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
7224 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007225 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
7226 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227 wpa_printf(MSG_ERROR, "nl80211: Failed to "
7228 "remove interface %s from bridge "
7229 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007230 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231 return -1;
7232 }
7233 }
7234
7235 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
7236 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007237 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007238 wpa_printf(MSG_WARNING,
7239 "nl80211: Failed to add interface %s into bridge %s: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007240 ifname, brname, strerror(errno));
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007241 /* Try to continue without the interface being in a bridge. This
7242 * may be needed for some cases, e.g., with Open vSwitch, where
7243 * an external component will need to handle bridge
7244 * configuration. */
7245 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007246 }
7247 bss->added_if_into_bridge = 1;
7248
7249 return 0;
7250}
7251
7252
7253static void *i802_init(struct hostapd_data *hapd,
7254 struct wpa_init_params *params)
7255{
7256 struct wpa_driver_nl80211_data *drv;
7257 struct i802_bss *bss;
7258 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007259 char master_ifname[IFNAMSIZ];
7260 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007261 int br_added = 0;
7262
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007263 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
7264 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007265 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007266 if (bss == NULL)
7267 return NULL;
7268
7269 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007270
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007271 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007272 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007273 params->ifname, master_ifname);
7274 br_ifindex = if_nametoindex(master_ifname);
7275 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
7276 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
7277 linux_master_get(master_ifname, params->ifname) == 0) {
7278 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
7279 params->ifname, master_ifname);
7280 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007281 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007282
7283 /* check if master itself is under bridge */
7284 if (linux_br_get(master_ifname, master_ifname) == 0) {
7285 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
7286 master_ifname);
7287 br_ifindex = if_nametoindex(master_ifname);
7288 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
7289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007291 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007292 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007293
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007294 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007296 for (i = 0; i < params->num_bridge; i++) {
7297 if (params->bridge[i]) {
7298 ifindex = if_nametoindex(params->bridge[i]);
7299 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007300 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007301 if (ifindex == br_ifindex)
7302 br_added = 1;
7303 }
7304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007305
7306 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007307 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007308
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007309 if (params->num_bridge && params->bridge[0]) {
7310 if (i802_check_bridge(drv, bss, params->bridge[0],
7311 params->ifname) < 0)
7312 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007313 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007314 br_added = 1;
7315 }
7316
7317 if (!br_added && br_ifindex &&
7318 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007319 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007321#ifdef CONFIG_LIBNL3_ROUTE
Hai Shalomc9e41a12018-07-31 14:41:42 -07007322 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007323 int err;
7324
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007325 drv->rtnl_sk = nl_socket_alloc();
7326 if (drv->rtnl_sk == NULL) {
7327 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
7328 goto failed;
7329 }
7330
Hai Shalomfdcde762020-04-02 11:19:20 -07007331 err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
7332 if (err) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007333 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07007334 nl_geterror(err));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007335 goto failed;
7336 }
7337 }
7338#endif /* CONFIG_LIBNL3_ROUTE */
7339
Hai Shalomb755a2a2020-04-23 21:49:02 -07007340 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) {
7341 wpa_printf(MSG_DEBUG,
7342 "nl80211: Do not open EAPOL RX socket - using control port for RX");
7343 goto skip_eapol_sock;
7344 }
7345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007346 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
7347 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007348 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
7349 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 goto failed;
7351 }
7352
7353 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
7354 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007355 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356 goto failed;
7357 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07007358skip_eapol_sock:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007360 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7361 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007362 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007363 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007365 memcpy(bss->addr, params->own_addr, ETH_ALEN);
7366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007367 return bss;
7368
7369failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007370 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 return NULL;
7372}
7373
7374
7375static void i802_deinit(void *priv)
7376{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007377 struct i802_bss *bss = priv;
7378 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379}
7380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381
7382static enum nl80211_iftype wpa_driver_nl80211_if_type(
7383 enum wpa_driver_if_type type)
7384{
7385 switch (type) {
7386 case WPA_IF_STATION:
7387 return NL80211_IFTYPE_STATION;
7388 case WPA_IF_P2P_CLIENT:
7389 case WPA_IF_P2P_GROUP:
7390 return NL80211_IFTYPE_P2P_CLIENT;
7391 case WPA_IF_AP_VLAN:
7392 return NL80211_IFTYPE_AP_VLAN;
7393 case WPA_IF_AP_BSS:
7394 return NL80211_IFTYPE_AP;
7395 case WPA_IF_P2P_GO:
7396 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007397 case WPA_IF_P2P_DEVICE:
7398 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007399 case WPA_IF_MESH:
7400 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007401 default:
7402 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007403 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007404}
7405
7406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7408{
7409 struct wpa_driver_nl80211_data *drv;
7410 dl_list_for_each(drv, &global->interfaces,
7411 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007412 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007413 return 1;
7414 }
7415 return 0;
7416}
7417
7418
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007419static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420{
7421 unsigned int idx;
7422
7423 if (!drv->global)
7424 return -1;
7425
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007426 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007427 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007428 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007429 new_addr[0] ^= idx << 2;
7430 if (!nl80211_addr_in_use(drv->global, new_addr))
7431 break;
7432 }
7433 if (idx == 64)
7434 return -1;
7435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007436 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007437 MACSTR, MAC2STR(new_addr));
7438
7439 return 0;
7440}
7441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007443struct wdev_info {
7444 u64 wdev_id;
7445 int wdev_id_set;
7446 u8 macaddr[ETH_ALEN];
7447};
7448
7449static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
7450{
7451 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7452 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7453 struct wdev_info *wi = arg;
7454
7455 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7456 genlmsg_attrlen(gnlh, 0), NULL);
7457 if (tb[NL80211_ATTR_WDEV]) {
7458 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
7459 wi->wdev_id_set = 1;
7460 }
7461
7462 if (tb[NL80211_ATTR_MAC])
7463 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
7464 ETH_ALEN);
7465
7466 return NL_SKIP;
7467}
7468
7469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007470static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
7471 const char *ifname, const u8 *addr,
7472 void *bss_ctx, void **drv_priv,
7473 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007474 const char *bridge, int use_existing,
7475 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007476{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007477 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478 struct i802_bss *bss = priv;
7479 struct wpa_driver_nl80211_data *drv = bss->drv;
7480 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007481 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007482
7483 if (addr)
7484 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007485 nlmode = wpa_driver_nl80211_if_type(type);
7486 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
7487 struct wdev_info p2pdev_info;
7488
7489 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
7490 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
7491 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007492 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007493 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
7494 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
7495 ifname);
7496 return -1;
7497 }
7498
7499 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
7500 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
7501 if (!is_zero_ether_addr(p2pdev_info.macaddr))
7502 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
7503 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
7504 ifname,
7505 (long long unsigned int) p2pdev_info.wdev_id);
7506 } else {
7507 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007508 0, NULL, NULL, use_existing);
7509 if (use_existing && ifidx == -ENFILE) {
7510 added = 0;
7511 ifidx = if_nametoindex(ifname);
7512 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007513 return -1;
7514 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007515 }
7516
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007517 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007518 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007519 os_memcpy(if_addr, bss->addr, ETH_ALEN);
7520 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007521 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007522 if (added)
7523 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007524 return -1;
7525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526 }
7527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007528 if (!addr &&
7529 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007530 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
7531 type == WPA_IF_STATION)) {
7532 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007533 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007535 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007536 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007537 if (added)
7538 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007539 return -1;
7540 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007541 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007542 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007543 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007544 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007545 if (added)
7546 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547 return -1;
7548 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007549 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007551 if (added)
7552 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 return -1;
7554 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007555 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007556 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007559 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007560 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
7561 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007562 if (added)
7563 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007564 return -1;
7565 }
7566
7567 if (bridge &&
7568 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7569 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7570 "interface %s to a bridge %s",
7571 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007572 if (added)
7573 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007574 os_free(new_bss);
7575 return -1;
7576 }
7577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007578 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7579 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007580 if (added)
7581 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582 os_free(new_bss);
7583 return -1;
7584 }
7585 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007586 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587 new_bss->ifindex = ifidx;
7588 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007589 new_bss->next = drv->first_bss->next;
7590 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007591 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007592 new_bss->added_if = added;
7593 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007594 if (drv_priv)
7595 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007596 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007597
7598 /* Subscribe management frames for this WPA_IF_AP_BSS */
7599 if (nl80211_setup_ap(new_bss))
7600 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007603 if (drv->global)
7604 drv->global->if_add_ifindex = ifidx;
7605
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007606 /*
7607 * Some virtual interfaces need to process EAPOL packets and events on
7608 * the parent interface. This is used mainly with hostapd.
7609 */
7610 if (ifidx > 0 &&
7611 (drv->hostapd ||
7612 nlmode == NL80211_IFTYPE_AP_VLAN ||
7613 nlmode == NL80211_IFTYPE_WDS ||
7614 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007615 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617 return 0;
7618}
7619
7620
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007621static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622 enum wpa_driver_if_type type,
7623 const char *ifname)
7624{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007625 struct wpa_driver_nl80211_data *drv = bss->drv;
7626 int ifindex = if_nametoindex(ifname);
7627
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007628 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7629 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007630 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07007631 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007632 else if (ifindex > 0 && !bss->added_if) {
7633 struct wpa_driver_nl80211_data *drv2;
7634 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007635 struct wpa_driver_nl80211_data, list) {
7636 del_ifidx(drv2, ifindex, IFIDX_ANY);
7637 del_ifidx(drv2, IFIDX_ANY, ifindex);
7638 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007639 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007640
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007641 if (type != WPA_IF_AP_BSS)
7642 return 0;
7643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007645 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7646 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7648 "interface %s from bridge %s: %s",
7649 bss->ifname, bss->brname, strerror(errno));
7650 }
7651 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007652 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007653 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7654 "bridge %s: %s",
7655 bss->brname, strerror(errno));
7656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007658 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007659 struct i802_bss *tbss;
7660
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007661 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7662 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663 if (tbss->next == bss) {
7664 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007665 /* Unsubscribe management frames */
7666 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007667 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007668 if (!bss->added_if)
7669 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670 os_free(bss);
7671 bss = NULL;
7672 break;
7673 }
7674 }
7675 if (bss)
7676 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7677 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007678 } else {
7679 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7680 nl80211_teardown_ap(bss);
7681 if (!bss->added_if && !drv->first_bss->next)
Paul Stewart092955c2017-02-06 09:13:09 -08007682 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007683 nl80211_destroy_bss(bss);
7684 if (!bss->added_if)
7685 i802_set_iface_flags(bss, 0);
7686 if (drv->first_bss->next) {
7687 drv->first_bss = drv->first_bss->next;
7688 drv->ctx = drv->first_bss->ctx;
7689 os_free(bss);
7690 } else {
7691 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7692 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007694
7695 return 0;
7696}
7697
7698
7699static int cookie_handler(struct nl_msg *msg, void *arg)
7700{
7701 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7702 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7703 u64 *cookie = arg;
7704 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7705 genlmsg_attrlen(gnlh, 0), NULL);
7706 if (tb[NL80211_ATTR_COOKIE])
7707 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7708 return NL_SKIP;
7709}
7710
7711
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007712static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007713 unsigned int freq, unsigned int wait,
7714 const u8 *buf, size_t buf_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07007715 int save_cookie, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007716 int offchanok, const u16 *csa_offs,
7717 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007718{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007719 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007720 struct nl_msg *msg;
7721 u64 cookie;
7722 int ret = -1;
7723
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007724 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07007725 "no_ack=%d offchanok=%d",
7726 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007727 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007728
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007729 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7730 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7731 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7732 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7733 drv->test_use_roc_tx) &&
7734 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7735 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7736 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007737 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7738 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007739 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7740 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007741
7742 cookie = 0;
7743 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7744 msg = NULL;
7745 if (ret) {
7746 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007747 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7748 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007749 } else {
7750 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7751 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7752 (long long unsigned int) cookie);
7753
Hai Shalomfdcde762020-04-02 11:19:20 -07007754 if (save_cookie)
7755 drv->send_frame_cookie = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007756
Hai Shalomfdcde762020-04-02 11:19:20 -07007757 if (drv->num_send_frame_cookies == MAX_SEND_FRAME_COOKIES) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007758 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07007759 "nl80211: Drop oldest pending send frame cookie 0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007760 (long long unsigned int)
Hai Shalomfdcde762020-04-02 11:19:20 -07007761 drv->send_frame_cookies[0]);
7762 os_memmove(&drv->send_frame_cookies[0],
7763 &drv->send_frame_cookies[1],
7764 (MAX_SEND_FRAME_COOKIES - 1) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007765 sizeof(u64));
Hai Shalomfdcde762020-04-02 11:19:20 -07007766 drv->num_send_frame_cookies--;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007767 }
Hai Shalomfdcde762020-04-02 11:19:20 -07007768 drv->send_frame_cookies[drv->num_send_frame_cookies] = cookie;
7769 drv->num_send_frame_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007772fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007773 nlmsg_free(msg);
7774 return ret;
7775}
7776
7777
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007778static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7779 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 unsigned int wait_time,
7781 const u8 *dst, const u8 *src,
7782 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007783 const u8 *data, size_t data_len,
7784 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007786 struct wpa_driver_nl80211_data *drv = bss->drv;
7787 int ret = -1;
7788 u8 *buf;
7789 struct ieee80211_hdr *hdr;
Hai Shalomfdcde762020-04-02 11:19:20 -07007790 int offchanok = 1;
7791
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007792 if (is_ap_interface(drv->nlmode) && (int) freq == bss->freq &&
7793 bss->beacon_set)
Hai Shalomfdcde762020-04-02 11:19:20 -07007794 offchanok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007795
7796 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Hai Shalomfdcde762020-04-02 11:19:20 -07007797 "freq=%u MHz wait=%d ms no_cck=%d offchanok=%d)",
7798 drv->ifindex, freq, wait_time, no_cck, offchanok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799
7800 buf = os_zalloc(24 + data_len);
7801 if (buf == NULL)
7802 return ret;
7803 os_memcpy(buf + 24, data, data_len);
7804 hdr = (struct ieee80211_hdr *) buf;
7805 hdr->frame_control =
7806 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7807 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7808 os_memcpy(hdr->addr2, src, ETH_ALEN);
7809 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7810
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007811 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7812 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7813 MAC2STR(src));
7814 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7815 } else {
7816 os_memset(bss->rand_addr, 0, ETH_ALEN);
7817 }
7818
Dmitry Shmidt56052862013-10-04 10:23:25 -07007819 if (is_ap_interface(drv->nlmode) &&
7820 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7821 (int) freq == bss->freq || drv->device_ap_sme ||
7822 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007823 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07007824 0, freq, no_cck, offchanok,
7825 wait_time, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007826 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007827 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007828 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07007829 1, no_cck, 0, offchanok, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007830
7831 os_free(buf);
7832 return ret;
7833}
7834
7835
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007836static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007837{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007838 struct wpa_driver_nl80211_data *drv = bss->drv;
7839 struct nl_msg *msg;
7840 int ret;
7841
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08007842 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007843 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007844 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007845 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007846 nlmsg_free(msg);
7847 return;
7848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007849
7850 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007851 if (ret)
7852 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7853 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007854}
7855
7856
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007857static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7858{
7859 struct i802_bss *bss = priv;
7860 struct wpa_driver_nl80211_data *drv = bss->drv;
7861 unsigned int i;
7862 u64 cookie;
7863
7864 /* Cancel the last pending TX cookie */
Hai Shalomfdcde762020-04-02 11:19:20 -07007865 nl80211_frame_wait_cancel(bss, drv->send_frame_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007866
7867 /*
7868 * Cancel the other pending TX cookies, if any. This is needed since
7869 * the driver may keep a list of all pending offchannel TX operations
7870 * and free up the radio only once they have expired or cancelled.
7871 */
Hai Shalomfdcde762020-04-02 11:19:20 -07007872 for (i = drv->num_send_frame_cookies; i > 0; i--) {
7873 cookie = drv->send_frame_cookies[i - 1];
7874 if (cookie != drv->send_frame_cookie)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007875 nl80211_frame_wait_cancel(bss, cookie);
7876 }
Hai Shalomfdcde762020-04-02 11:19:20 -07007877 drv->num_send_frame_cookies = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007878}
7879
7880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007881static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7882 unsigned int duration)
7883{
7884 struct i802_bss *bss = priv;
7885 struct wpa_driver_nl80211_data *drv = bss->drv;
7886 struct nl_msg *msg;
7887 int ret;
7888 u64 cookie;
7889
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007890 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7891 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7892 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7893 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007896
7897 cookie = 0;
7898 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7899 if (ret == 0) {
7900 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7901 "0x%llx for freq=%u MHz duration=%u",
7902 (long long unsigned int) cookie, freq, duration);
7903 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007904 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905 return 0;
7906 }
7907 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7908 "(freq=%d duration=%u): %d (%s)",
7909 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007910 return -1;
7911}
7912
7913
7914static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7915{
7916 struct i802_bss *bss = priv;
7917 struct wpa_driver_nl80211_data *drv = bss->drv;
7918 struct nl_msg *msg;
7919 int ret;
7920
7921 if (!drv->pending_remain_on_chan) {
7922 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7923 "to cancel");
7924 return -1;
7925 }
7926
7927 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7928 "0x%llx",
7929 (long long unsigned int) drv->remain_on_chan_cookie);
7930
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007931 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7932 if (!msg ||
7933 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7934 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007935 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007937
7938 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7939 if (ret == 0)
7940 return 0;
7941 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7942 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007943 return -1;
7944}
7945
7946
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007947static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007948{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007949 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007951 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007952 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007953 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7954 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007955 /*
7956 * Do not disable Probe Request reporting that was
7957 * enabled in nl80211_setup_ap().
7958 */
7959 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7960 "Probe Request reporting nl_preq=%p while "
7961 "in AP mode", bss->nl_preq);
7962 } else if (bss->nl_preq) {
7963 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7964 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007965 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007966 }
7967 return 0;
7968 }
7969
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007970 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007971 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007972 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007973 return 0;
7974 }
7975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007976 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7977 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007978 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007979 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7980 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007982 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007983 (WLAN_FC_TYPE_MGMT << 2) |
7984 (WLAN_FC_STYPE_PROBE_REQ << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07007985 NULL, 0, false) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007987
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007988 nl80211_register_eloop_read(&bss->nl_preq,
7989 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007990 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007991
7992 return 0;
7993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007994 out_err:
7995 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007996 return -1;
7997}
7998
7999
8000static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8001 int ifindex, int disabled)
8002{
8003 struct nl_msg *msg;
8004 struct nlattr *bands, *band;
8005 int ret;
8006
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008007 wpa_printf(MSG_DEBUG,
8008 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
8009 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
8010 "no NL80211_TXRATE_LEGACY constraint");
8011
8012 msg = nl80211_ifindex_msg(drv, ifindex, 0,
8013 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014 if (!msg)
8015 return -1;
8016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008017 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8018 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008019 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008020
8021 /*
8022 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8023 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8024 * rates. All 5 GHz rates are left enabled.
8025 */
8026 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008027 if (!band ||
8028 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
8029 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
8030 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031 nla_nest_end(msg, band);
8032
8033 nla_nest_end(msg, bands);
8034
8035 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008036 if (ret) {
8037 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8038 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008039 } else
8040 drv->disabled_11b_rates = disabled;
8041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008042 return ret;
8043
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008044fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045 nlmsg_free(msg);
8046 return -1;
8047}
8048
8049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008050static int wpa_driver_nl80211_deinit_ap(void *priv)
8051{
8052 struct i802_bss *bss = priv;
8053 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008054 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008055 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08008056 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008057 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008058
8059 /*
8060 * If the P2P GO interface was dynamically added, then it is
8061 * possible that the interface change to station is not possible.
8062 */
8063 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
8064 return 0;
8065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008066 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008067}
8068
8069
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008070static int wpa_driver_nl80211_stop_ap(void *priv)
8071{
8072 struct i802_bss *bss = priv;
8073 struct wpa_driver_nl80211_data *drv = bss->drv;
8074 if (!is_ap_interface(drv->nlmode))
8075 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08008076 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008077 bss->beacon_set = 0;
8078 return 0;
8079}
8080
8081
Dmitry Shmidt04949592012-07-19 12:16:46 -07008082static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8083{
8084 struct i802_bss *bss = priv;
8085 struct wpa_driver_nl80211_data *drv = bss->drv;
8086 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8087 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008088
8089 /*
8090 * If the P2P Client interface was dynamically added, then it is
8091 * possible that the interface change to station is not possible.
8092 */
8093 if (bss->if_dynamic)
8094 return 0;
8095
Dmitry Shmidt04949592012-07-19 12:16:46 -07008096 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8097}
8098
8099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008100static void wpa_driver_nl80211_resume(void *priv)
8101{
8102 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008103 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008104
8105 if (i802_set_iface_flags(bss, 1))
8106 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008107
8108 if (is_p2p_net_interface(nlmode))
8109 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008110}
8111
8112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008113static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8114{
8115 struct i802_bss *bss = priv;
8116 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008117 struct nl_msg *msg;
8118 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008119
8120 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8121 "hysteresis=%d", threshold, hysteresis);
8122
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008123 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
8124 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
8125 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
8126 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
8127 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008128 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008129 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008130 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008131
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008132 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008133}
8134
8135
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008136static int get_channel_width(struct nl_msg *msg, void *arg)
8137{
8138 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8139 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8140 struct wpa_signal_info *sig_change = arg;
8141
8142 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8143 genlmsg_attrlen(gnlh, 0), NULL);
8144
8145 sig_change->center_frq1 = -1;
8146 sig_change->center_frq2 = -1;
8147 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
8148
8149 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
8150 sig_change->chanwidth = convert2width(
8151 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
8152 if (tb[NL80211_ATTR_CENTER_FREQ1])
8153 sig_change->center_frq1 =
8154 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
8155 if (tb[NL80211_ATTR_CENTER_FREQ2])
8156 sig_change->center_frq2 =
8157 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
8158 }
8159
8160 return NL_SKIP;
8161}
8162
8163
8164static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
8165 struct wpa_signal_info *sig)
8166{
8167 struct nl_msg *msg;
8168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008169 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008170 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008171}
8172
8173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008174static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8175{
8176 struct i802_bss *bss = priv;
8177 struct wpa_driver_nl80211_data *drv = bss->drv;
8178 int res;
8179
8180 os_memset(si, 0, sizeof(*si));
8181 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008182 if (res) {
8183 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
8184 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
8185 return res;
8186 si->current_signal = 0;
8187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008188
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008189 res = nl80211_get_channel_width(drv, si);
8190 if (res != 0)
8191 return res;
8192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008193 return nl80211_get_link_noise(drv, si);
8194}
8195
8196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008197static int nl80211_set_param(void *priv, const char *param)
8198{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008199 struct i802_bss *bss = priv;
8200 struct wpa_driver_nl80211_data *drv = bss->drv;
8201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008202 if (param == NULL)
8203 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08008204 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008205
8206#ifdef CONFIG_P2P
8207 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008208 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8209 "interface");
8210 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8211 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8212 }
8213#endif /* CONFIG_P2P */
8214
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008215 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008216 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008217
8218 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008219 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008220 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008221 }
8222
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008223 if (os_strstr(param, "force_bss_selection=1"))
8224 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
8225
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08008226 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08008227 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
8228 drv->test_use_roc_tx = 1;
8229 }
8230
Hai Shalomb755a2a2020-04-23 21:49:02 -07008231 if (os_strstr(param, "control_port=0")) {
Hai Shalomfdcde762020-04-02 11:19:20 -07008232 drv->capa.flags &= ~WPA_DRIVER_FLAGS_CONTROL_PORT;
Hai Shalomb755a2a2020-04-23 21:49:02 -07008233 drv->capa.flags2 &= ~WPA_DRIVER_FLAGS2_CONTROL_PORT_RX;
8234 }
8235
8236 if (os_strstr(param, "control_port_ap=1"))
8237 drv->control_port_ap = 1;
Hai Shalomfdcde762020-04-02 11:19:20 -07008238
8239 if (os_strstr(param, "full_ap_client_state=0"))
8240 drv->capa.flags &= ~WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
8241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008242 return 0;
8243}
8244
8245
Dmitry Shmidte4663042016-04-04 10:07:49 -07008246static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008247{
8248 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008249 struct netlink_config *cfg;
8250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 global = os_zalloc(sizeof(*global));
8252 if (global == NULL)
8253 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07008254 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008255 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008256 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008257 global->if_add_ifindex = -1;
8258
8259 cfg = os_zalloc(sizeof(*cfg));
8260 if (cfg == NULL)
8261 goto err;
8262
8263 cfg->ctx = global;
8264 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8265 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8266 global->netlink = netlink_init(cfg);
8267 if (global->netlink == NULL) {
8268 os_free(cfg);
8269 goto err;
8270 }
8271
8272 if (wpa_driver_nl80211_init_nl_global(global) < 0)
8273 goto err;
8274
8275 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8276 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008277 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
8278 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008279 goto err;
8280 }
8281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008282 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008283
8284err:
8285 nl80211_global_deinit(global);
8286 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008287}
8288
8289
8290static void nl80211_global_deinit(void *priv)
8291{
8292 struct nl80211_global *global = priv;
8293 if (global == NULL)
8294 return;
8295 if (!dl_list_empty(&global->interfaces)) {
8296 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8297 "nl80211_global_deinit",
8298 dl_list_len(&global->interfaces));
8299 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008300
8301 if (global->netlink)
8302 netlink_deinit(global->netlink);
8303
8304 nl_destroy_handles(&global->nl);
8305
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008306 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -07008307 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008308
8309 nl_cb_put(global->nl_cb);
8310
8311 if (global->ioctl_sock >= 0)
8312 close(global->ioctl_sock);
8313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008314 os_free(global);
8315}
8316
8317
8318static const char * nl80211_get_radio_name(void *priv)
8319{
8320 struct i802_bss *bss = priv;
8321 struct wpa_driver_nl80211_data *drv = bss->drv;
8322 return drv->phyname;
8323}
8324
8325
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008326static int nl80211_pmkid(struct i802_bss *bss, int cmd,
8327 struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008328{
8329 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008330 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
Jouni Malinen75ecf522011-06-27 15:19:46 -07008331
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008332 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008333 (params->pmkid &&
8334 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
8335 (params->bssid &&
8336 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
8337 (params->ssid_len &&
8338 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
8339 (params->fils_cache_id &&
8340 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
8341 params->fils_cache_id)) ||
Hai Shalomfdcde762020-04-02 11:19:20 -07008342 (params->pmk_lifetime &&
8343 nla_put_u32(msg, NL80211_ATTR_PMK_LIFETIME,
8344 params->pmk_lifetime)) ||
8345 (params->pmk_reauth_threshold &&
8346 nla_put_u8(msg, NL80211_ATTR_PMK_REAUTH_THRESHOLD,
8347 params->pmk_reauth_threshold)) ||
Hai Shalom021b0b52019-04-10 11:17:58 -07008348 (cmd != NL80211_CMD_DEL_PMKSA &&
8349 params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008350 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Hai Shalom74f70d42019-02-11 14:42:39 -08008351 nl80211_nlmsg_clear(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008352 nlmsg_free(msg);
8353 return -ENOBUFS;
8354 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008355
Hai Shalom74f70d42019-02-11 14:42:39 -08008356 return send_and_recv_msgs(bss->drv, msg, NULL, (void *) -1);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008357}
8358
8359
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008360static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008361{
8362 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008363 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008364
8365 if (params->bssid)
8366 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
8367 MAC2STR(params->bssid));
8368 else if (params->fils_cache_id && params->ssid_len) {
8369 wpa_printf(MSG_DEBUG,
8370 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
8371 params->fils_cache_id[0], params->fils_cache_id[1],
8372 wpa_ssid_txt(params->ssid, params->ssid_len));
8373 }
8374
Roshan Pius3a1667e2018-07-03 15:17:14 -07008375 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
8376 if (ret < 0) {
8377 wpa_printf(MSG_DEBUG,
8378 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
8379 ret, strerror(-ret));
8380 }
8381
8382 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008383}
8384
8385
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008386static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008387{
8388 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008389 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008390
8391 if (params->bssid)
8392 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
8393 MAC2STR(params->bssid));
8394 else if (params->fils_cache_id && params->ssid_len) {
8395 wpa_printf(MSG_DEBUG,
8396 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
8397 params->fils_cache_id[0], params->fils_cache_id[1],
8398 wpa_ssid_txt(params->ssid, params->ssid_len));
8399 }
8400
Roshan Pius3a1667e2018-07-03 15:17:14 -07008401 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
8402 if (ret < 0) {
8403 wpa_printf(MSG_DEBUG,
8404 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
8405 ret, strerror(-ret));
8406 }
8407
8408 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008409}
8410
8411
8412static int nl80211_flush_pmkid(void *priv)
8413{
8414 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008415 struct nl_msg *msg;
8416
Jouni Malinen75ecf522011-06-27 15:19:46 -07008417 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008418 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
8419 if (!msg)
8420 return -ENOBUFS;
8421 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008422}
8423
8424
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008425static void clean_survey_results(struct survey_results *survey_results)
8426{
8427 struct freq_survey *survey, *tmp;
8428
8429 if (dl_list_empty(&survey_results->survey_list))
8430 return;
8431
8432 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
8433 struct freq_survey, list) {
8434 dl_list_del(&survey->list);
8435 os_free(survey);
8436 }
8437}
8438
8439
8440static void add_survey(struct nlattr **sinfo, u32 ifidx,
8441 struct dl_list *survey_list)
8442{
8443 struct freq_survey *survey;
8444
8445 survey = os_zalloc(sizeof(struct freq_survey));
8446 if (!survey)
8447 return;
8448
8449 survey->ifidx = ifidx;
8450 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8451 survey->filled = 0;
8452
8453 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
8454 survey->nf = (int8_t)
8455 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
8456 survey->filled |= SURVEY_HAS_NF;
8457 }
8458
8459 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
8460 survey->channel_time =
8461 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
8462 survey->filled |= SURVEY_HAS_CHAN_TIME;
8463 }
8464
8465 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
8466 survey->channel_time_busy =
8467 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
8468 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
8469 }
8470
8471 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
8472 survey->channel_time_rx =
8473 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
8474 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
8475 }
8476
8477 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
8478 survey->channel_time_tx =
8479 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
8480 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
8481 }
8482
8483 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)",
8484 survey->freq,
8485 survey->nf,
8486 (unsigned long int) survey->channel_time,
8487 (unsigned long int) survey->channel_time_busy,
8488 (unsigned long int) survey->channel_time_tx,
8489 (unsigned long int) survey->channel_time_rx,
8490 survey->filled);
8491
8492 dl_list_add_tail(survey_list, &survey->list);
8493}
8494
8495
8496static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
8497 unsigned int freq_filter)
8498{
8499 if (!freq_filter)
8500 return 1;
8501
8502 return freq_filter == surveyed_freq;
8503}
8504
8505
8506static int survey_handler(struct nl_msg *msg, void *arg)
8507{
8508 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8509 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8510 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
8511 struct survey_results *survey_results;
8512 u32 surveyed_freq = 0;
8513 u32 ifidx;
8514
8515 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
8516 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
8517 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
8518 };
8519
8520 survey_results = (struct survey_results *) arg;
8521
8522 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8523 genlmsg_attrlen(gnlh, 0), NULL);
8524
Dmitry Shmidt97672262014-02-03 13:02:54 -08008525 if (!tb[NL80211_ATTR_IFINDEX])
8526 return NL_SKIP;
8527
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008528 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
8529
8530 if (!tb[NL80211_ATTR_SURVEY_INFO])
8531 return NL_SKIP;
8532
8533 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
8534 tb[NL80211_ATTR_SURVEY_INFO],
8535 survey_policy))
8536 return NL_SKIP;
8537
8538 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
8539 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
8540 return NL_SKIP;
8541 }
8542
8543 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8544
8545 if (!check_survey_ok(sinfo, surveyed_freq,
8546 survey_results->freq_filter))
8547 return NL_SKIP;
8548
8549 if (survey_results->freq_filter &&
8550 survey_results->freq_filter != surveyed_freq) {
8551 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
8552 surveyed_freq);
8553 return NL_SKIP;
8554 }
8555
8556 add_survey(sinfo, ifidx, &survey_results->survey_list);
8557
8558 return NL_SKIP;
8559}
8560
8561
8562static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
8563{
8564 struct i802_bss *bss = priv;
8565 struct wpa_driver_nl80211_data *drv = bss->drv;
8566 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008567 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008568 union wpa_event_data data;
8569 struct survey_results *survey_results;
8570
8571 os_memset(&data, 0, sizeof(data));
8572 survey_results = &data.survey_results;
8573
8574 dl_list_init(&survey_results->survey_list);
8575
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008576 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008577 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008578 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008579
8580 if (freq)
8581 data.survey_results.freq_filter = freq;
8582
8583 do {
8584 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
8585 err = send_and_recv_msgs(drv, msg, survey_handler,
8586 survey_results);
8587 } while (err > 0);
8588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008589 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008590 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008591 else
8592 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008593
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008594 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008595 return err;
8596}
8597
8598
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008599static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
8600 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008601 const u8 *replay_ctr)
8602{
8603 struct i802_bss *bss = priv;
8604 struct wpa_driver_nl80211_data *drv = bss->drv;
8605 struct nlattr *replay_nested;
8606 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008607 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008608
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008609 if (!drv->set_rekey_offload)
8610 return;
8611
8612 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008613 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
8614 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008615 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008616 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008617 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8618 replay_ctr)) {
8619 nl80211_nlmsg_clear(msg);
8620 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008621 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008622 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008623
8624 nla_nest_end(msg, replay_nested);
8625
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008626 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
8627 if (ret == -EOPNOTSUPP) {
8628 wpa_printf(MSG_DEBUG,
8629 "nl80211: Driver does not support rekey offload");
8630 drv->set_rekey_offload = 0;
8631 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008632}
8633
8634
8635static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8636 const u8 *addr, int qos)
8637{
8638 /* send data frame to poll STA and check whether
8639 * this frame is ACKed */
8640 struct {
8641 struct ieee80211_hdr hdr;
8642 u16 qos_ctl;
8643 } STRUCT_PACKED nulldata;
8644 size_t size;
8645
8646 /* Send data frame to poll STA and check whether this frame is ACKed */
8647
8648 os_memset(&nulldata, 0, sizeof(nulldata));
8649
8650 if (qos) {
8651 nulldata.hdr.frame_control =
8652 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8653 WLAN_FC_STYPE_QOS_NULL);
8654 size = sizeof(nulldata);
8655 } else {
8656 nulldata.hdr.frame_control =
8657 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8658 WLAN_FC_STYPE_NULLFUNC);
8659 size = sizeof(struct ieee80211_hdr);
8660 }
8661
8662 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8663 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8664 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8665 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8666
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008667 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -07008668 0, 0, NULL, 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008669 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8670 "send poll frame");
8671}
8672
8673static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8674 int qos)
8675{
8676 struct i802_bss *bss = priv;
8677 struct wpa_driver_nl80211_data *drv = bss->drv;
8678 struct nl_msg *msg;
Hai Shalom5f92bc92019-04-18 11:54:11 -07008679 u64 cookie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008680 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008681
8682 if (!drv->poll_command_supported) {
8683 nl80211_send_null_frame(bss, own_addr, addr, qos);
8684 return;
8685 }
8686
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008687 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8688 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8689 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008690 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008691 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008692
Hai Shalom5f92bc92019-04-18 11:54:11 -07008693 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008694 if (ret < 0) {
8695 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8696 MACSTR " failed: ret=%d (%s)",
8697 MAC2STR(addr), ret, strerror(-ret));
Hai Shalom5f92bc92019-04-18 11:54:11 -07008698 } else {
8699 wpa_printf(MSG_DEBUG,
8700 "nl80211: Client probe request addr=" MACSTR
8701 " cookie=%llu", MAC2STR(addr),
8702 (long long unsigned int) cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008703 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008704}
8705
8706
8707static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8708{
8709 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008710 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008711
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008712 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8713 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8714 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8715 nlmsg_free(msg);
8716 return -ENOBUFS;
8717 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07008718
8719 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8720 if (ret < 0) {
8721 wpa_printf(MSG_DEBUG,
8722 "nl80211: Setting PS state %s failed: %d (%s)",
8723 enabled ? "enabled" : "disabled",
8724 ret, strerror(-ret));
8725 }
8726 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008727}
8728
8729
8730static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8731 int ctwindow)
8732{
8733 struct i802_bss *bss = priv;
8734
8735 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8736 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8737
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008738 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008739#ifdef ANDROID_P2P
8740 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008741#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008742 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008743#endif /* ANDROID_P2P */
8744 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008745
8746 if (legacy_ps == -1)
8747 return 0;
8748 if (legacy_ps != 0 && legacy_ps != 1)
8749 return -1; /* Not yet supported */
8750
8751 return nl80211_set_power_save(bss, legacy_ps);
8752}
8753
8754
Dmitry Shmidt051af732013-10-22 13:52:46 -07008755static int nl80211_start_radar_detection(void *priv,
8756 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008757{
8758 struct i802_bss *bss = priv;
8759 struct wpa_driver_nl80211_data *drv = bss->drv;
8760 struct nl_msg *msg;
8761 int ret;
8762
Hai Shalom81f62d82019-07-22 12:10:00 -07008763 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)",
8764 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07008765 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8766
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008767 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8768 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8769 "detection");
8770 return -1;
8771 }
8772
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008773 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8774 nl80211_put_freq_params(msg, freq) < 0) {
8775 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008776 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008777 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008778
8779 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8780 if (ret == 0)
8781 return 0;
8782 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8783 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008784 return -1;
8785}
8786
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008787#ifdef CONFIG_TDLS
8788
8789static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8790 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008791 u32 peer_capab, int initiator, const u8 *buf,
8792 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008793{
8794 struct i802_bss *bss = priv;
8795 struct wpa_driver_nl80211_data *drv = bss->drv;
8796 struct nl_msg *msg;
8797
8798 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8799 return -EOPNOTSUPP;
8800
8801 if (!dst)
8802 return -EINVAL;
8803
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008804 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8805 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8806 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8807 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8808 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8809 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008810 if (peer_capab) {
8811 /*
8812 * The internal enum tdls_peer_capability definition is
8813 * currently identical with the nl80211 enum
8814 * nl80211_tdls_peer_capability, so no conversion is needed
8815 * here.
8816 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008817 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8818 peer_capab))
8819 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008820 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008821 if ((initiator &&
8822 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8823 nla_put(msg, NL80211_ATTR_IE, len, buf))
8824 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008825
8826 return send_and_recv_msgs(drv, msg, NULL, NULL);
8827
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008828fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008829 nlmsg_free(msg);
8830 return -ENOBUFS;
8831}
8832
8833
8834static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8835{
8836 struct i802_bss *bss = priv;
8837 struct wpa_driver_nl80211_data *drv = bss->drv;
8838 struct nl_msg *msg;
8839 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -08008840 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008841
8842 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8843 return -EOPNOTSUPP;
8844
8845 switch (oper) {
8846 case TDLS_DISCOVERY_REQ:
8847 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8848 break;
8849 case TDLS_SETUP:
8850 nl80211_oper = NL80211_TDLS_SETUP;
8851 break;
8852 case TDLS_TEARDOWN:
8853 nl80211_oper = NL80211_TDLS_TEARDOWN;
8854 break;
8855 case TDLS_ENABLE_LINK:
8856 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8857 break;
8858 case TDLS_DISABLE_LINK:
8859 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8860 break;
8861 case TDLS_ENABLE:
8862 return 0;
8863 case TDLS_DISABLE:
8864 return 0;
8865 default:
8866 return -EINVAL;
8867 }
8868
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008869 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8870 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8871 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8872 nlmsg_free(msg);
8873 return -ENOBUFS;
8874 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008875
Paul Stewart092955c2017-02-06 09:13:09 -08008876 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8877 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8878 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8879 strerror(-res));
8880 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008881}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008882
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008883
8884static int
8885nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8886 const struct hostapd_freq_params *params)
8887{
8888 struct i802_bss *bss = priv;
8889 struct wpa_driver_nl80211_data *drv = bss->drv;
8890 struct nl_msg *msg;
8891 int ret = -ENOBUFS;
8892
8893 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8894 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8895 return -EOPNOTSUPP;
8896
8897 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8898 " oper_class=%u freq=%u",
8899 MAC2STR(addr), oper_class, params->freq);
8900 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8901 if (!msg ||
8902 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8903 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8904 (ret = nl80211_put_freq_params(msg, params))) {
8905 nlmsg_free(msg);
8906 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8907 return ret;
8908 }
8909
8910 return send_and_recv_msgs(drv, msg, NULL, NULL);
8911}
8912
8913
8914static int
8915nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8916{
8917 struct i802_bss *bss = priv;
8918 struct wpa_driver_nl80211_data *drv = bss->drv;
8919 struct nl_msg *msg;
8920
8921 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8922 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8923 return -EOPNOTSUPP;
8924
8925 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8926 MAC2STR(addr));
8927 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8928 if (!msg ||
8929 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8930 nlmsg_free(msg);
8931 wpa_printf(MSG_DEBUG,
8932 "nl80211: Could not build TDLS cancel chan switch");
8933 return -ENOBUFS;
8934 }
8935
8936 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008937}
8938
8939#endif /* CONFIG TDLS */
8940
8941
Hai Shalomfdcde762020-04-02 11:19:20 -07008942static int driver_nl80211_set_key(void *priv,
8943 struct wpa_driver_set_key_params *params)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008944{
8945 struct i802_bss *bss = priv;
Hai Shalomfdcde762020-04-02 11:19:20 -07008946
8947 return wpa_driver_nl80211_set_key(bss, params);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008948}
8949
8950
8951static int driver_nl80211_scan2(void *priv,
8952 struct wpa_driver_scan_params *params)
8953{
8954 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008955#ifdef CONFIG_DRIVER_NL80211_QCA
8956 struct wpa_driver_nl80211_data *drv = bss->drv;
8957
8958 /*
8959 * Do a vendor specific scan if possible. If only_new_results is
8960 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8961 * cannot be achieved through a vendor scan. The below condition may
8962 * need to be modified if new scan flags are added in the future whose
8963 * functionality can only be achieved through a normal scan.
8964 */
8965 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8966 return wpa_driver_nl80211_vendor_scan(bss, params);
8967#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008968 return wpa_driver_nl80211_scan(bss, params);
8969}
8970
8971
8972static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07008973 u16 reason_code)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008974{
8975 struct i802_bss *bss = priv;
8976 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8977}
8978
8979
8980static int driver_nl80211_authenticate(void *priv,
8981 struct wpa_driver_auth_params *params)
8982{
8983 struct i802_bss *bss = priv;
8984 return wpa_driver_nl80211_authenticate(bss, params);
8985}
8986
8987
8988static void driver_nl80211_deinit(void *priv)
8989{
8990 struct i802_bss *bss = priv;
8991 wpa_driver_nl80211_deinit(bss);
8992}
8993
8994
8995static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8996 const char *ifname)
8997{
8998 struct i802_bss *bss = priv;
8999 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9000}
9001
9002
9003static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009004 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009005 unsigned int freq,
Hai Shalomfdcde762020-04-02 11:19:20 -07009006 const u16 *csa_offs, size_t csa_offs_len,
9007 int no_encrypt, unsigned int wait)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009008{
9009 struct i802_bss *bss = priv;
9010 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Hai Shalomfdcde762020-04-02 11:19:20 -07009011 freq, 0, 0, wait, csa_offs,
9012 csa_offs_len, no_encrypt);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009013}
9014
9015
9016static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9017{
9018 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009019 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009020}
9021
9022
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009023static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9024 const char *ifname, int vlan_id)
9025{
9026 struct i802_bss *bss = priv;
9027 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9028}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009029
9030
9031static int driver_nl80211_read_sta_data(void *priv,
9032 struct hostap_sta_driver_data *data,
9033 const u8 *addr)
9034{
9035 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08009036
9037 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009038 return i802_read_sta_data(bss, data, addr);
9039}
9040
9041
9042static int driver_nl80211_send_action(void *priv, unsigned int freq,
9043 unsigned int wait_time,
9044 const u8 *dst, const u8 *src,
9045 const u8 *bssid,
9046 const u8 *data, size_t data_len,
9047 int no_cck)
9048{
9049 struct i802_bss *bss = priv;
9050 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9051 bssid, data, data_len, no_cck);
9052}
9053
9054
9055static int driver_nl80211_probe_req_report(void *priv, int report)
9056{
9057 struct i802_bss *bss = priv;
9058 return wpa_driver_nl80211_probe_req_report(bss, report);
9059}
9060
9061
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009062static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9063 const u8 *ies, size_t ies_len)
9064{
9065 int ret;
9066 struct nl_msg *msg;
9067 struct i802_bss *bss = priv;
9068 struct wpa_driver_nl80211_data *drv = bss->drv;
9069 u16 mdid = WPA_GET_LE16(md);
9070
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009071 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009072 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
9073 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
9074 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
9075 nlmsg_free(msg);
9076 return -ENOBUFS;
9077 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009078
9079 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9080 if (ret) {
9081 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9082 "err=%d (%s)", ret, strerror(-ret));
9083 }
9084
9085 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009086}
9087
9088
Hai Shalom81f62d82019-07-22 12:10:00 -07009089static int nl80211_update_dh_ie(void *priv, const u8 *peer_mac,
9090 u16 reason_code, const u8 *ie, size_t ie_len)
9091{
9092 int ret;
9093 struct nl_msg *msg;
9094 struct i802_bss *bss = priv;
9095 struct wpa_driver_nl80211_data *drv = bss->drv;
9096
9097 wpa_printf(MSG_DEBUG, "nl80211: Updating DH IE peer: " MACSTR
9098 " reason %u", MAC2STR(peer_mac), reason_code);
9099 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_OWE_INFO)) ||
9100 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_mac) ||
9101 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, reason_code) ||
9102 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie))) {
9103 nlmsg_free(msg);
9104 return -ENOBUFS;
9105 }
9106
9107 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9108 if (ret) {
9109 wpa_printf(MSG_DEBUG,
9110 "nl80211: update_dh_ie failed err=%d (%s)",
9111 ret, strerror(-ret));
9112 }
9113
9114 return ret;
9115}
9116
9117
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07009118static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009119{
9120 struct i802_bss *bss = priv;
9121 struct wpa_driver_nl80211_data *drv = bss->drv;
9122
9123 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
9124 return NULL;
9125
9126 return bss->addr;
9127}
9128
9129
Dmitry Shmidt56052862013-10-04 10:23:25 -07009130static const char * scan_state_str(enum scan_states scan_state)
9131{
9132 switch (scan_state) {
9133 case NO_SCAN:
9134 return "NO_SCAN";
9135 case SCAN_REQUESTED:
9136 return "SCAN_REQUESTED";
9137 case SCAN_STARTED:
9138 return "SCAN_STARTED";
9139 case SCAN_COMPLETED:
9140 return "SCAN_COMPLETED";
9141 case SCAN_ABORTED:
9142 return "SCAN_ABORTED";
9143 case SCHED_SCAN_STARTED:
9144 return "SCHED_SCAN_STARTED";
9145 case SCHED_SCAN_STOPPED:
9146 return "SCHED_SCAN_STOPPED";
9147 case SCHED_SCAN_RESULTS:
9148 return "SCHED_SCAN_RESULTS";
9149 }
9150
9151 return "??";
9152}
9153
9154
9155static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
9156{
9157 struct i802_bss *bss = priv;
9158 struct wpa_driver_nl80211_data *drv = bss->drv;
9159 int res;
9160 char *pos, *end;
Hai Shalom74f70d42019-02-11 14:42:39 -08009161 struct nl_msg *msg;
9162 char alpha2[3] = { 0, 0, 0 };
Dmitry Shmidt56052862013-10-04 10:23:25 -07009163
9164 pos = buf;
9165 end = buf + buflen;
9166
9167 res = os_snprintf(pos, end - pos,
9168 "ifindex=%d\n"
9169 "ifname=%s\n"
9170 "brname=%s\n"
9171 "addr=" MACSTR "\n"
9172 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -07009173 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009174 bss->ifindex,
9175 bss->ifname,
9176 bss->brname,
9177 MAC2STR(bss->addr),
9178 bss->freq,
9179 bss->beacon_set ? "beacon_set=1\n" : "",
9180 bss->added_if_into_bridge ?
9181 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -07009182 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009183 bss->added_bridge ? "added_bridge=1\n" : "",
9184 bss->in_deinit ? "in_deinit=1\n" : "",
9185 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009186 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009187 return pos - buf;
9188 pos += res;
9189
9190 if (bss->wdev_id_set) {
9191 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
9192 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009193 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009194 return pos - buf;
9195 pos += res;
9196 }
9197
9198 res = os_snprintf(pos, end - pos,
9199 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009200 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07009201 "drv_ifindex=%d\n"
9202 "operstate=%d\n"
9203 "scan_state=%s\n"
9204 "auth_bssid=" MACSTR "\n"
9205 "auth_attempt_bssid=" MACSTR "\n"
9206 "bssid=" MACSTR "\n"
9207 "prev_bssid=" MACSTR "\n"
9208 "associated=%d\n"
9209 "assoc_freq=%u\n"
9210 "monitor_sock=%d\n"
9211 "monitor_ifidx=%d\n"
9212 "monitor_refcount=%d\n"
9213 "last_mgmt_freq=%u\n"
9214 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009215 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009216 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009217 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07009218 drv->ifindex,
9219 drv->operstate,
9220 scan_state_str(drv->scan_state),
9221 MAC2STR(drv->auth_bssid),
9222 MAC2STR(drv->auth_attempt_bssid),
9223 MAC2STR(drv->bssid),
9224 MAC2STR(drv->prev_bssid),
9225 drv->associated,
9226 drv->assoc_freq,
9227 drv->monitor_sock,
9228 drv->monitor_ifidx,
9229 drv->monitor_refcount,
9230 drv->last_mgmt_freq,
9231 drv->eapol_tx_sock,
9232 drv->ignore_if_down_event ?
9233 "ignore_if_down_event=1\n" : "",
9234 drv->scan_complete_events ?
9235 "scan_complete_events=1\n" : "",
9236 drv->disabled_11b_rates ?
9237 "disabled_11b_rates=1\n" : "",
9238 drv->pending_remain_on_chan ?
9239 "pending_remain_on_chan=1\n" : "",
9240 drv->in_interface_list ? "in_interface_list=1\n" : "",
9241 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
9242 drv->poll_command_supported ?
9243 "poll_command_supported=1\n" : "",
9244 drv->data_tx_status ? "data_tx_status=1\n" : "",
9245 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
9246 drv->retry_auth ? "retry_auth=1\n" : "",
9247 drv->use_monitor ? "use_monitor=1\n" : "",
9248 drv->ignore_next_local_disconnect ?
9249 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07009250 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009251 "ignore_next_local_deauth=1\n" : "");
9252 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009253 return pos - buf;
9254 pos += res;
9255
9256 if (drv->has_capability) {
9257 res = os_snprintf(pos, end - pos,
9258 "capa.key_mgmt=0x%x\n"
9259 "capa.enc=0x%x\n"
9260 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009261 "capa.flags=0x%llx\n"
9262 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07009263 "capa.max_scan_ssids=%d\n"
9264 "capa.max_sched_scan_ssids=%d\n"
9265 "capa.sched_scan_supported=%d\n"
9266 "capa.max_match_sets=%d\n"
9267 "capa.max_remain_on_chan=%u\n"
9268 "capa.max_stations=%u\n"
9269 "capa.probe_resp_offloads=0x%x\n"
9270 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009271 "capa.num_multichan_concurrent=%u\n"
9272 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009273 "capa.mac_addr_rand_scan_supported=%d\n"
9274 "capa.conc_capab=%u\n"
9275 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009276 "capa.max_conc_chan_5_0=%u\n"
9277 "capa.max_sched_scan_plans=%u\n"
9278 "capa.max_sched_scan_plan_interval=%u\n"
9279 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009280 drv->capa.key_mgmt,
9281 drv->capa.enc,
9282 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009283 (unsigned long long) drv->capa.flags,
9284 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009285 drv->capa.max_scan_ssids,
9286 drv->capa.max_sched_scan_ssids,
9287 drv->capa.sched_scan_supported,
9288 drv->capa.max_match_sets,
9289 drv->capa.max_remain_on_chan,
9290 drv->capa.max_stations,
9291 drv->capa.probe_resp_offloads,
9292 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009293 drv->capa.num_multichan_concurrent,
9294 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009295 drv->capa.mac_addr_rand_scan_supported,
9296 drv->capa.conc_capab,
9297 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009298 drv->capa.max_conc_chan_5_0,
9299 drv->capa.max_sched_scan_plans,
9300 drv->capa.max_sched_scan_plan_interval,
9301 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009302 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009303 return pos - buf;
9304 pos += res;
9305 }
9306
Hai Shalom74f70d42019-02-11 14:42:39 -08009307 msg = nlmsg_alloc();
9308 if (msg &&
9309 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG) &&
9310 nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx) == 0) {
9311 if (send_and_recv_msgs(drv, msg, nl80211_get_country,
9312 alpha2) == 0 &&
9313 alpha2[0]) {
9314 res = os_snprintf(pos, end - pos, "country=%s\n",
9315 alpha2);
9316 if (os_snprintf_error(end - pos, res))
9317 return pos - buf;
9318 pos += res;
9319 }
9320 } else {
9321 nlmsg_free(msg);
9322 }
9323
Dmitry Shmidt56052862013-10-04 10:23:25 -07009324 return pos - buf;
9325}
9326
9327
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009328static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
9329{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009330 if ((settings->head &&
9331 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
9332 settings->head_len, settings->head)) ||
9333 (settings->tail &&
9334 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
9335 settings->tail_len, settings->tail)) ||
9336 (settings->beacon_ies &&
9337 nla_put(msg, NL80211_ATTR_IE,
9338 settings->beacon_ies_len, settings->beacon_ies)) ||
9339 (settings->proberesp_ies &&
9340 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
9341 settings->proberesp_ies_len, settings->proberesp_ies)) ||
9342 (settings->assocresp_ies &&
9343 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
9344 settings->assocresp_ies_len, settings->assocresp_ies)) ||
9345 (settings->probe_resp &&
9346 nla_put(msg, NL80211_ATTR_PROBE_RESP,
9347 settings->probe_resp_len, settings->probe_resp)))
9348 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009349
9350 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009351}
9352
9353
9354static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
9355{
9356 struct nl_msg *msg;
9357 struct i802_bss *bss = priv;
9358 struct wpa_driver_nl80211_data *drv = bss->drv;
9359 struct nlattr *beacon_csa;
9360 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009361 int csa_off_len = 0;
9362 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009363
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009364 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009365 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009366 settings->freq_params.freq, settings->freq_params.bandwidth,
9367 settings->freq_params.center_freq1,
9368 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009369
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009370 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009371 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
9372 return -EOPNOTSUPP;
9373 }
9374
Roshan Pius3a1667e2018-07-03 15:17:14 -07009375 if (drv->nlmode != NL80211_IFTYPE_AP &&
9376 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
9377 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009378 return -EOPNOTSUPP;
9379
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009380 /*
9381 * Remove empty counters, assuming Probe Response and Beacon frame
9382 * counters match. This implementation assumes that there are only two
9383 * counters.
9384 */
9385 if (settings->counter_offset_beacon[0] &&
9386 !settings->counter_offset_beacon[1]) {
9387 csa_off_len = 1;
9388 } else if (settings->counter_offset_beacon[1] &&
9389 !settings->counter_offset_beacon[0]) {
9390 csa_off_len = 1;
9391 settings->counter_offset_beacon[0] =
9392 settings->counter_offset_beacon[1];
9393 settings->counter_offset_presp[0] =
9394 settings->counter_offset_presp[1];
9395 } else if (settings->counter_offset_beacon[1] &&
9396 settings->counter_offset_beacon[0]) {
9397 csa_off_len = 2;
9398 } else {
9399 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
9400 return -EINVAL;
9401 }
9402
9403 /* Check CSA counters validity */
9404 if (drv->capa.max_csa_counters &&
9405 csa_off_len > drv->capa.max_csa_counters) {
9406 wpa_printf(MSG_ERROR,
9407 "nl80211: Too many CSA counters provided");
9408 return -EINVAL;
9409 }
9410
9411 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009412 return -EINVAL;
9413
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009414 for (i = 0; i < csa_off_len; i++) {
9415 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
9416 u16 csa_c_off_presp = settings->counter_offset_presp[i];
9417
9418 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
9419 (settings->beacon_csa.tail[csa_c_off_bcn] !=
9420 settings->cs_count))
9421 return -EINVAL;
9422
9423 if (settings->beacon_csa.probe_resp &&
9424 ((settings->beacon_csa.probe_resp_len <=
9425 csa_c_off_presp) ||
9426 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
9427 settings->cs_count)))
9428 return -EINVAL;
9429 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009430
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009431 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
9432 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
9433 settings->cs_count) ||
9434 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
9435 (settings->block_tx &&
9436 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009437 goto error;
9438
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009439 /* beacon_after params */
9440 ret = set_beacon_data(msg, &settings->beacon_after);
9441 if (ret)
9442 goto error;
9443
9444 /* beacon_csa params */
9445 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
9446 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009447 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009448
9449 ret = set_beacon_data(msg, &settings->beacon_csa);
9450 if (ret)
9451 goto error;
9452
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009453 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
9454 csa_off_len * sizeof(u16),
9455 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009456 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009457 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
9458 csa_off_len * sizeof(u16),
9459 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009460 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009461
9462 nla_nest_end(msg, beacon_csa);
9463 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9464 if (ret) {
9465 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
9466 ret, strerror(-ret));
9467 }
9468 return ret;
9469
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009470fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009471 ret = -ENOBUFS;
9472error:
9473 nlmsg_free(msg);
9474 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
9475 return ret;
9476}
9477
9478
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009479static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
9480 u8 user_priority, u16 admitted_time)
9481{
9482 struct i802_bss *bss = priv;
9483 struct wpa_driver_nl80211_data *drv = bss->drv;
9484 struct nl_msg *msg;
9485 int ret;
9486
9487 wpa_printf(MSG_DEBUG,
9488 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
9489 tsid, admitted_time, user_priority);
9490
9491 if (!is_sta_interface(drv->nlmode))
9492 return -ENOTSUP;
9493
9494 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
9495 if (!msg ||
9496 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9497 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9498 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
9499 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
9500 nlmsg_free(msg);
9501 return -ENOBUFS;
9502 }
9503
9504 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9505 if (ret)
9506 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
9507 ret, strerror(-ret));
9508 return ret;
9509}
9510
9511
9512static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
9513{
9514 struct i802_bss *bss = priv;
9515 struct wpa_driver_nl80211_data *drv = bss->drv;
9516 struct nl_msg *msg;
9517 int ret;
9518
9519 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
9520
9521 if (!is_sta_interface(drv->nlmode))
9522 return -ENOTSUP;
9523
9524 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
9525 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9526 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
9527 nlmsg_free(msg);
9528 return -ENOBUFS;
9529 }
9530
9531 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9532 if (ret)
9533 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
9534 ret, strerror(-ret));
9535 return ret;
9536}
9537
9538
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009539#ifdef CONFIG_TESTING_OPTIONS
9540static int cmd_reply_handler(struct nl_msg *msg, void *arg)
9541{
9542 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9543 struct wpabuf *buf = arg;
9544
9545 if (!buf)
9546 return NL_SKIP;
9547
9548 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
9549 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
9550 return NL_SKIP;
9551 }
9552
9553 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
9554 genlmsg_attrlen(gnlh, 0));
9555
9556 return NL_SKIP;
9557}
9558#endif /* CONFIG_TESTING_OPTIONS */
9559
9560
9561static int vendor_reply_handler(struct nl_msg *msg, void *arg)
9562{
9563 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9564 struct nlattr *nl_vendor_reply, *nl;
9565 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9566 struct wpabuf *buf = arg;
9567 int rem;
9568
9569 if (!buf)
9570 return NL_SKIP;
9571
9572 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9573 genlmsg_attrlen(gnlh, 0), NULL);
9574 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
9575
9576 if (!nl_vendor_reply)
9577 return NL_SKIP;
9578
9579 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
9580 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
9581 return NL_SKIP;
9582 }
9583
9584 nla_for_each_nested(nl, nl_vendor_reply, rem) {
9585 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
9586 }
9587
9588 return NL_SKIP;
9589}
9590
9591
9592static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
9593 unsigned int subcmd, const u8 *data,
9594 size_t data_len, struct wpabuf *buf)
9595{
9596 struct i802_bss *bss = priv;
9597 struct wpa_driver_nl80211_data *drv = bss->drv;
9598 struct nl_msg *msg;
9599 int ret;
9600
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009601#ifdef CONFIG_TESTING_OPTIONS
9602 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009603 msg = nlmsg_alloc();
9604 if (!msg)
9605 return -ENOMEM;
9606
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009607 nl80211_cmd(drv, msg, 0, subcmd);
9608 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
9609 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009610 goto fail;
Hai Shalomb755a2a2020-04-23 21:49:02 -07009611 /* This test vendor_cmd can be used with nl80211 commands that
9612 * need the connect nl_sock, so use the owner-setting variant
9613 * of send_and_recv_msgs(). */
9614 ret = send_and_recv_msgs_owner(drv, msg,
9615 get_connect_handle(bss), 0,
9616 cmd_reply_handler, buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009617 if (ret)
9618 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
9619 ret);
9620 return ret;
9621 }
9622#endif /* CONFIG_TESTING_OPTIONS */
9623
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009624 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
9625 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
9626 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
9627 (data &&
9628 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
9629 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009630
9631 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
9632 if (ret)
9633 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
9634 ret);
9635 return ret;
9636
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009637fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009638 nlmsg_free(msg);
9639 return -ENOBUFS;
9640}
9641
9642
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009643static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
9644 u8 qos_map_set_len)
9645{
9646 struct i802_bss *bss = priv;
9647 struct wpa_driver_nl80211_data *drv = bss->drv;
9648 struct nl_msg *msg;
9649 int ret;
9650
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009651 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
9652 qos_map_set, qos_map_set_len);
9653
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009654 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
9655 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
9656 nlmsg_free(msg);
9657 return -ENOBUFS;
9658 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009659
9660 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9661 if (ret)
9662 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
9663
9664 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009665}
9666
9667
Hai Shalomfdcde762020-04-02 11:19:20 -07009668static int get_wowlan_handler(struct nl_msg *msg, void *arg)
9669{
9670 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9671 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9672 int *wowlan_enabled = arg;
9673
9674 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9675 genlmsg_attrlen(gnlh, 0), NULL);
9676
9677 *wowlan_enabled = !!tb[NL80211_ATTR_WOWLAN_TRIGGERS];
9678
9679 return NL_SKIP;
9680}
9681
9682
9683static int nl80211_get_wowlan(void *priv)
9684{
9685 struct i802_bss *bss = priv;
9686 struct wpa_driver_nl80211_data *drv = bss->drv;
9687 struct nl_msg *msg;
9688 int wowlan_enabled;
9689 int ret;
9690
9691 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status");
9692
9693 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_WOWLAN);
9694
9695 ret = send_and_recv_msgs(drv, msg, get_wowlan_handler, &wowlan_enabled);
9696 if (ret) {
9697 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status failed");
9698 return 0;
9699 }
9700
9701 wpa_printf(MSG_DEBUG, "nl80211: wowlan is %s",
9702 wowlan_enabled ? "enabled" : "disabled");
9703
9704 return wowlan_enabled;
9705}
9706
9707
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009708static int nl80211_set_wowlan(void *priv,
9709 const struct wowlan_triggers *triggers)
9710{
9711 struct i802_bss *bss = priv;
9712 struct wpa_driver_nl80211_data *drv = bss->drv;
9713 struct nl_msg *msg;
9714 struct nlattr *wowlan_triggers;
9715 int ret;
9716
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009717 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
9718
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009719 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009720 !(wowlan_triggers = nla_nest_start(msg,
9721 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
9722 (triggers->any &&
9723 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9724 (triggers->disconnect &&
9725 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9726 (triggers->magic_pkt &&
9727 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9728 (triggers->gtk_rekey_failure &&
9729 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9730 (triggers->eap_identity_req &&
9731 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9732 (triggers->four_way_handshake &&
9733 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9734 (triggers->rfkill_release &&
9735 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9736 nlmsg_free(msg);
9737 return -ENOBUFS;
9738 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009739
9740 nla_nest_end(msg, wowlan_triggers);
9741
9742 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9743 if (ret)
9744 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9745
9746 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009747}
9748
9749
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009750#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009751static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9752{
9753 struct i802_bss *bss = priv;
9754 struct wpa_driver_nl80211_data *drv = bss->drv;
9755 struct nl_msg *msg;
9756 struct nlattr *params;
9757
9758 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9759
9760 if (!drv->roaming_vendor_cmd_avail) {
9761 wpa_printf(MSG_DEBUG,
9762 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9763 return -1;
9764 }
9765
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009766 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9767 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9768 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9769 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9770 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9771 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9772 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9773 QCA_ROAMING_NOT_ALLOWED) ||
9774 (bssid &&
9775 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9776 nlmsg_free(msg);
9777 return -1;
9778 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009779 nla_nest_end(msg, params);
9780
9781 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009782}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009783
9784
Roshan Pius3a1667e2018-07-03 15:17:14 -07009785static int nl80211_disable_fils(void *priv, int disable)
9786{
9787 struct i802_bss *bss = priv;
9788 struct wpa_driver_nl80211_data *drv = bss->drv;
9789 struct nl_msg *msg;
9790 struct nlattr *params;
9791
9792 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9793
9794 if (!drv->set_wifi_conf_vendor_cmd_avail)
9795 return -1;
9796
9797 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9798 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9799 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9800 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9801 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9802 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9803 disable)) {
9804 nlmsg_free(msg);
9805 return -1;
9806 }
9807 nla_nest_end(msg, params);
9808
9809 return send_and_recv_msgs(drv, msg, NULL, NULL);
9810}
9811
9812
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009813/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9814#define WPA_SUPPLICANT_CLIENT_ID 1
9815
9816static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9817 const u8 *bssid)
9818{
9819 struct i802_bss *bss = priv;
9820 struct wpa_driver_nl80211_data *drv = bss->drv;
9821 struct nl_msg *msg;
9822 struct nlattr *params, *nlbssids, *attr;
9823 unsigned int i;
9824
9825 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9826 num_bssid);
9827
9828 if (!drv->roam_vendor_cmd_avail)
9829 return -1;
9830
9831 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9832 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9833 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9834 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9835 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9836 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
Hai Shalomc3565922019-10-28 11:58:20 -07009837 QCA_WLAN_VENDOR_ROAMING_SUBCMD_SET_BLACKLIST_BSSID) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009838 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9839 WPA_SUPPLICANT_CLIENT_ID) ||
9840 nla_put_u32(msg,
9841 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9842 num_bssid))
9843 goto fail;
9844
9845 nlbssids = nla_nest_start(
9846 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9847 if (!nlbssids)
9848 goto fail;
9849
9850 for (i = 0; i < num_bssid; i++) {
9851 attr = nla_nest_start(msg, i);
9852 if (!attr)
9853 goto fail;
9854 if (nla_put(msg,
9855 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9856 ETH_ALEN, &bssid[i * ETH_ALEN]))
9857 goto fail;
9858 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9859 MAC2STR(&bssid[i * ETH_ALEN]));
9860 nla_nest_end(msg, attr);
9861 }
9862 nla_nest_end(msg, nlbssids);
9863 nla_nest_end(msg, params);
9864
9865 return send_and_recv_msgs(drv, msg, NULL, NULL);
9866
9867fail:
9868 nlmsg_free(msg);
9869 return -1;
9870}
9871
Hai Shalomc3565922019-10-28 11:58:20 -07009872
9873static int nl80211_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
9874{
9875 struct i802_bss *bss = priv;
9876 struct wpa_driver_nl80211_data *drv = bss->drv;
9877 struct nl_msg *msg;
9878 struct nlattr *params;
9879
9880 if (!drv->add_sta_node_vendor_cmd_avail)
9881 return -EOPNOTSUPP;
9882
9883 wpa_printf(MSG_DEBUG, "nl80211: Add STA node");
9884
9885 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9886 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9887 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9888 QCA_NL80211_VENDOR_SUBCMD_ADD_STA_NODE) ||
9889 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9890 (addr &&
9891 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_MAC_ADDR, ETH_ALEN,
9892 addr)) ||
9893 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_AUTH_ALGO,
9894 auth_alg)) {
9895 nlmsg_free(msg);
9896 wpa_printf(MSG_ERROR,
9897 "%s: err in adding vendor_cmd and vendor_data",
9898 __func__);
9899 return -1;
9900 }
9901 nla_nest_end(msg, params);
9902
9903 return send_and_recv_msgs(drv, msg, NULL, NULL);
9904}
9905
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009906#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009907
9908
9909static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9910{
9911 struct i802_bss *bss = priv;
9912 struct wpa_driver_nl80211_data *drv = bss->drv;
9913 int new_addr = addr != NULL;
9914
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009915 if (TEST_FAIL())
9916 return -1;
9917
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009918 if (!addr)
9919 addr = drv->perm_addr;
9920
9921 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9922 return -1;
9923
9924 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9925 {
9926 wpa_printf(MSG_DEBUG,
9927 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9928 bss->ifname, MAC2STR(addr));
9929 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9930 1) < 0) {
9931 wpa_printf(MSG_DEBUG,
9932 "nl80211: Could not restore interface UP after failed set_mac_addr");
9933 }
9934 return -1;
9935 }
9936
9937 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9938 bss->ifname, MAC2STR(addr));
9939 drv->addr_changed = new_addr;
9940 os_memcpy(bss->addr, addr, ETH_ALEN);
9941
9942 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9943 {
9944 wpa_printf(MSG_DEBUG,
9945 "nl80211: Could not restore interface UP after set_mac_addr");
9946 }
9947
9948 return 0;
9949}
9950
9951
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009952#ifdef CONFIG_MESH
9953
9954static int wpa_driver_nl80211_init_mesh(void *priv)
9955{
9956 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9957 wpa_printf(MSG_INFO,
9958 "nl80211: Failed to set interface into mesh mode");
9959 return -1;
9960 }
9961 return 0;
9962}
9963
9964
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009965static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9966 size_t mesh_id_len)
9967{
9968 if (mesh_id) {
Hai Shalom74f70d42019-02-11 14:42:39 -08009969 wpa_printf(MSG_DEBUG, " * Mesh ID (SSID)=%s",
9970 wpa_ssid_txt(mesh_id, mesh_id_len));
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009971 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9972 }
9973
9974 return 0;
9975}
9976
9977
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009978static int nl80211_put_mesh_config(struct nl_msg *msg,
9979 struct wpa_driver_mesh_bss_params *params)
9980{
9981 struct nlattr *container;
9982
9983 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9984 if (!container)
9985 return -1;
9986
9987 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009988 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9989 params->auto_plinks)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009990 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9991 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009992 params->max_peer_links)) ||
9993 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9994 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9995 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009996 return -1;
9997
9998 /*
9999 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
10000 * the timer could disconnect stations even in that case.
10001 */
10002 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
10003 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
10004 params->peer_link_timeout)) {
10005 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
10006 return -1;
10007 }
10008
10009 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
10010 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
10011 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
10012 return -1;
10013 }
10014
10015 nla_nest_end(msg, container);
10016
10017 return 0;
10018}
10019
10020
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010021static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010022 struct wpa_driver_mesh_join_params *params)
10023{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010024 struct wpa_driver_nl80211_data *drv = bss->drv;
10025 struct nl_msg *msg;
10026 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080010027 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010028
10029 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
10030 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010031 if (!msg ||
10032 nl80211_put_freq_params(msg, &params->freq) ||
10033 nl80211_put_basic_rates(msg, params->basic_rates) ||
10034 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010035 nl80211_put_beacon_int(msg, params->beacon_int) ||
10036 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010037 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010038
10039 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
10040
10041 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
10042 if (!container)
10043 goto fail;
10044
10045 if (params->ies) {
10046 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
10047 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
10048 params->ies))
10049 goto fail;
10050 }
10051 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
10052 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
10053 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
10054 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
10055 goto fail;
10056 }
10057 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
10058 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
10059 goto fail;
10060 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
10061 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
10062 goto fail;
10063 nla_nest_end(msg, container);
10064
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070010065 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
10066 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
10067 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
10068 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010069 goto fail;
10070
Hai Shalomb755a2a2020-04-23 21:49:02 -070010071 ret = send_and_recv_msgs_owner(drv, msg, get_connect_handle(bss), 1,
10072 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010073 msg = NULL;
10074 if (ret) {
10075 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
10076 ret, strerror(-ret));
10077 goto fail;
10078 }
10079 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010080 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010081 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
10082
10083fail:
10084 nlmsg_free(msg);
10085 return ret;
10086}
10087
10088
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010089static int
10090wpa_driver_nl80211_join_mesh(void *priv,
10091 struct wpa_driver_mesh_join_params *params)
10092{
10093 struct i802_bss *bss = priv;
10094 int ret, timeout;
10095
10096 timeout = params->conf.peer_link_timeout;
10097
10098 /* Disable kernel inactivity timer */
10099 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
10100 params->conf.peer_link_timeout = 0;
10101
10102 ret = nl80211_join_mesh(bss, params);
10103 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
10104 wpa_printf(MSG_DEBUG,
10105 "nl80211: Mesh join retry for peer_link_timeout");
10106 /*
10107 * Old kernel does not support setting
10108 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
10109 * into future from peer_link_timeout.
10110 */
10111 params->conf.peer_link_timeout = timeout + 60;
10112 ret = nl80211_join_mesh(priv, params);
10113 }
10114
10115 params->conf.peer_link_timeout = timeout;
10116 return ret;
10117}
10118
10119
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010120static int wpa_driver_nl80211_leave_mesh(void *priv)
10121{
10122 struct i802_bss *bss = priv;
10123 struct wpa_driver_nl80211_data *drv = bss->drv;
10124 struct nl_msg *msg;
10125 int ret;
10126
10127 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
10128 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
Hai Shalomb755a2a2020-04-23 21:49:02 -070010129 ret = send_and_recv_msgs_owner(drv, msg, get_connect_handle(bss), 0,
10130 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010131 if (ret) {
10132 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
10133 ret, strerror(-ret));
10134 } else {
10135 wpa_printf(MSG_DEBUG,
10136 "nl80211: mesh leave request send successfully");
10137 }
10138
10139 if (wpa_driver_nl80211_set_mode(drv->first_bss,
10140 NL80211_IFTYPE_STATION)) {
10141 wpa_printf(MSG_INFO,
10142 "nl80211: Failed to set interface into station mode");
10143 }
10144 return ret;
10145}
10146
Hai Shalom81f62d82019-07-22 12:10:00 -070010147
10148static int nl80211_probe_mesh_link(void *priv, const u8 *addr, const u8 *eth,
10149 size_t len)
10150{
10151 struct i802_bss *bss = priv;
10152 struct wpa_driver_nl80211_data *drv = bss->drv;
10153 struct nl_msg *msg;
10154 int ret;
10155
10156 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_PROBE_MESH_LINK);
10157 if (!msg ||
10158 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10159 nla_put(msg, NL80211_ATTR_FRAME, len, eth)) {
10160 nlmsg_free(msg);
10161 return -ENOBUFS;
10162 }
10163
10164 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10165 if (ret) {
10166 wpa_printf(MSG_DEBUG, "nl80211: mesh link probe to " MACSTR
10167 " failed: ret=%d (%s)",
10168 MAC2STR(addr), ret, strerror(-ret));
10169 } else {
10170 wpa_printf(MSG_DEBUG, "nl80211: Mesh link to " MACSTR
10171 " probed successfully", MAC2STR(addr));
10172 }
10173
10174 return ret;
10175}
10176
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010177#endif /* CONFIG_MESH */
10178
10179
10180static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
10181 const u8 *ipaddr, int prefixlen,
10182 const u8 *addr)
10183{
10184#ifdef CONFIG_LIBNL3_ROUTE
10185 struct i802_bss *bss = priv;
10186 struct wpa_driver_nl80211_data *drv = bss->drv;
10187 struct rtnl_neigh *rn;
10188 struct nl_addr *nl_ipaddr = NULL;
10189 struct nl_addr *nl_lladdr = NULL;
10190 int family, addrsize;
10191 int res;
10192
10193 if (!ipaddr || prefixlen == 0 || !addr)
10194 return -EINVAL;
10195
10196 if (bss->br_ifindex == 0) {
10197 wpa_printf(MSG_DEBUG,
10198 "nl80211: bridge must be set before adding an ip neigh to it");
10199 return -1;
10200 }
10201
10202 if (!drv->rtnl_sk) {
10203 wpa_printf(MSG_DEBUG,
10204 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
10205 return -1;
10206 }
10207
10208 if (version == 4) {
10209 family = AF_INET;
10210 addrsize = 4;
10211 } else if (version == 6) {
10212 family = AF_INET6;
10213 addrsize = 16;
10214 } else {
10215 return -EINVAL;
10216 }
10217
10218 rn = rtnl_neigh_alloc();
10219 if (rn == NULL)
10220 return -ENOMEM;
10221
10222 /* set the destination ip address for neigh */
10223 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
10224 if (nl_ipaddr == NULL) {
10225 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
10226 res = -ENOMEM;
10227 goto errout;
10228 }
10229 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
10230 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
10231 if (res) {
10232 wpa_printf(MSG_DEBUG,
10233 "nl80211: neigh set destination addr failed");
10234 goto errout;
10235 }
10236
10237 /* set the corresponding lladdr for neigh */
10238 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
10239 if (nl_lladdr == NULL) {
10240 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
10241 res = -ENOMEM;
10242 goto errout;
10243 }
10244 rtnl_neigh_set_lladdr(rn, nl_lladdr);
10245
10246 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
10247 rtnl_neigh_set_state(rn, NUD_PERMANENT);
10248
10249 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
10250 if (res) {
10251 wpa_printf(MSG_DEBUG,
10252 "nl80211: Adding bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010253 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010254 }
10255errout:
10256 if (nl_lladdr)
10257 nl_addr_put(nl_lladdr);
10258 if (nl_ipaddr)
10259 nl_addr_put(nl_ipaddr);
10260 if (rn)
10261 rtnl_neigh_put(rn);
10262 return res;
10263#else /* CONFIG_LIBNL3_ROUTE */
10264 return -1;
10265#endif /* CONFIG_LIBNL3_ROUTE */
10266}
10267
10268
10269static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
10270 const u8 *ipaddr)
10271{
10272#ifdef CONFIG_LIBNL3_ROUTE
10273 struct i802_bss *bss = priv;
10274 struct wpa_driver_nl80211_data *drv = bss->drv;
10275 struct rtnl_neigh *rn;
10276 struct nl_addr *nl_ipaddr;
10277 int family, addrsize;
10278 int res;
10279
10280 if (!ipaddr)
10281 return -EINVAL;
10282
10283 if (version == 4) {
10284 family = AF_INET;
10285 addrsize = 4;
10286 } else if (version == 6) {
10287 family = AF_INET6;
10288 addrsize = 16;
10289 } else {
10290 return -EINVAL;
10291 }
10292
10293 if (bss->br_ifindex == 0) {
10294 wpa_printf(MSG_DEBUG,
10295 "nl80211: bridge must be set to delete an ip neigh");
10296 return -1;
10297 }
10298
10299 if (!drv->rtnl_sk) {
10300 wpa_printf(MSG_DEBUG,
10301 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
10302 return -1;
10303 }
10304
10305 rn = rtnl_neigh_alloc();
10306 if (rn == NULL)
10307 return -ENOMEM;
10308
10309 /* set the destination ip address for neigh */
10310 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
10311 if (nl_ipaddr == NULL) {
10312 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
10313 res = -ENOMEM;
10314 goto errout;
10315 }
10316 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
10317 if (res) {
10318 wpa_printf(MSG_DEBUG,
10319 "nl80211: neigh set destination addr failed");
10320 goto errout;
10321 }
10322
10323 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
10324
10325 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
10326 if (res) {
10327 wpa_printf(MSG_DEBUG,
10328 "nl80211: Deleting bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010329 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010330 }
10331errout:
10332 if (nl_ipaddr)
10333 nl_addr_put(nl_ipaddr);
10334 if (rn)
10335 rtnl_neigh_put(rn);
10336 return res;
10337#else /* CONFIG_LIBNL3_ROUTE */
10338 return -1;
10339#endif /* CONFIG_LIBNL3_ROUTE */
10340}
10341
10342
10343static int linux_write_system_file(const char *path, unsigned int val)
10344{
10345 char buf[50];
10346 int fd, len;
10347
10348 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
10349 if (os_snprintf_error(sizeof(buf), len))
10350 return -1;
10351
10352 fd = open(path, O_WRONLY);
10353 if (fd < 0)
10354 return -1;
10355
10356 if (write(fd, buf, len) < 0) {
10357 wpa_printf(MSG_DEBUG,
10358 "nl80211: Failed to write Linux system file: %s with the value of %d",
10359 path, val);
10360 close(fd);
10361 return -1;
10362 }
10363 close(fd);
10364
10365 return 0;
10366}
10367
10368
10369static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
10370{
10371 switch (attr) {
10372 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -070010373 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010374 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
10375 return "hairpin_mode";
10376 }
10377
10378 return NULL;
10379}
10380
10381
10382static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
10383 unsigned int val)
10384{
10385 struct i802_bss *bss = priv;
10386 char path[128];
10387 const char *attr_txt;
10388
10389 attr_txt = drv_br_port_attr_str(attr);
10390 if (attr_txt == NULL)
10391 return -EINVAL;
10392
10393 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
10394 bss->ifname, attr_txt);
10395
10396 if (linux_write_system_file(path, val))
10397 return -1;
10398
10399 return 0;
10400}
10401
10402
10403static const char * drv_br_net_param_str(enum drv_br_net_param param)
10404{
10405 switch (param) {
10406 case DRV_BR_NET_PARAM_GARP_ACCEPT:
10407 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -070010408 default:
10409 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010410 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010411}
10412
10413
10414static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
10415 unsigned int val)
10416{
10417 struct i802_bss *bss = priv;
10418 char path[128];
10419 const char *param_txt;
10420 int ip_version = 4;
10421
Dmitry Shmidt83474442015-04-15 13:47:09 -070010422 if (param == DRV_BR_MULTICAST_SNOOPING) {
10423 os_snprintf(path, sizeof(path),
10424 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
10425 bss->brname);
10426 goto set_val;
10427 }
10428
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010429 param_txt = drv_br_net_param_str(param);
10430 if (param_txt == NULL)
10431 return -EINVAL;
10432
10433 switch (param) {
10434 case DRV_BR_NET_PARAM_GARP_ACCEPT:
10435 ip_version = 4;
10436 break;
10437 default:
10438 return -EINVAL;
10439 }
10440
10441 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
10442 ip_version, bss->brname, param_txt);
10443
Dmitry Shmidt83474442015-04-15 13:47:09 -070010444set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010445 if (linux_write_system_file(path, val))
10446 return -1;
10447
10448 return 0;
10449}
10450
10451
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010452#ifdef CONFIG_DRIVER_NL80211_QCA
10453
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010454static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
10455{
10456 switch (hw_mode) {
10457 case HOSTAPD_MODE_IEEE80211B:
10458 return QCA_ACS_MODE_IEEE80211B;
10459 case HOSTAPD_MODE_IEEE80211G:
10460 return QCA_ACS_MODE_IEEE80211G;
10461 case HOSTAPD_MODE_IEEE80211A:
10462 return QCA_ACS_MODE_IEEE80211A;
10463 case HOSTAPD_MODE_IEEE80211AD:
10464 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -070010465 case HOSTAPD_MODE_IEEE80211ANY:
10466 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010467 default:
10468 return -1;
10469 }
10470}
10471
10472
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010473static int add_acs_ch_list(struct nl_msg *msg, const int *freq_list)
10474{
10475 int num_channels = 0, num_freqs;
10476 u8 *ch_list;
10477 enum hostapd_hw_mode hw_mode;
10478 int ret = 0;
10479 int i;
10480
10481 if (!freq_list)
10482 return 0;
10483
10484 num_freqs = int_array_len(freq_list);
10485 ch_list = os_malloc(sizeof(u8) * num_freqs);
10486 if (!ch_list)
10487 return -1;
10488
10489 for (i = 0; i < num_freqs; i++) {
10490 const int freq = freq_list[i];
10491
10492 if (freq == 0)
10493 break;
10494 /* Send 2.4 GHz and 5 GHz channels with
10495 * QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST to maintain backwards
10496 * compatibility.
10497 */
10498 if (!(freq >= 2412 && freq <= 2484) &&
10499 !(freq >= 5180 && freq <= 5900))
10500 continue;
10501 hw_mode = ieee80211_freq_to_chan(freq, &ch_list[num_channels]);
10502 if (hw_mode != NUM_HOSTAPD_MODES)
10503 num_channels++;
10504 }
10505
10506 if (num_channels)
10507 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST,
10508 num_channels, ch_list);
10509
10510 os_free(ch_list);
10511 return ret;
10512}
10513
10514
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010515static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
10516{
10517 int i, len, ret;
10518 u32 *freqs;
10519
10520 if (!freq_list)
10521 return 0;
10522 len = int_array_len(freq_list);
10523 freqs = os_malloc(sizeof(u32) * len);
10524 if (!freqs)
10525 return -1;
10526 for (i = 0; i < len; i++)
10527 freqs[i] = freq_list[i];
10528 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
10529 sizeof(u32) * len, freqs);
10530 os_free(freqs);
10531 return ret;
10532}
10533
10534
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010535static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
10536{
10537 struct i802_bss *bss = priv;
10538 struct wpa_driver_nl80211_data *drv = bss->drv;
10539 struct nl_msg *msg;
10540 struct nlattr *data;
10541 int ret;
10542 int mode;
10543
10544 mode = hw_mode_to_qca_acs(params->hw_mode);
10545 if (mode < 0)
10546 return -1;
10547
10548 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10549 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10550 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10551 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
10552 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10553 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
10554 (params->ht_enabled &&
10555 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
10556 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010557 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
10558 (params->vht_enabled &&
10559 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
10560 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
10561 params->ch_width) ||
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010562 add_acs_ch_list(msg, params->freq_list) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070010563 add_acs_freq_list(msg, params->freq_list) ||
10564 (params->edmg_enabled &&
10565 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EDMG_ENABLED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010566 nlmsg_free(msg);
10567 return -ENOBUFS;
10568 }
10569 nla_nest_end(msg, data);
10570
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010571 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -070010572 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d EDMG: %d",
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010573 params->hw_mode, params->ht_enabled, params->ht40_enabled,
Hai Shalomfdcde762020-04-02 11:19:20 -070010574 params->vht_enabled, params->ch_width, params->edmg_enabled);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010575
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010576 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10577 if (ret) {
10578 wpa_printf(MSG_DEBUG,
10579 "nl80211: Failed to invoke driver ACS function: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010580 strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010581 }
10582 return ret;
10583}
10584
10585
Ravi Joshie6ccb162015-07-16 17:45:41 -070010586static int nl80211_set_band(void *priv, enum set_band band)
10587{
10588 struct i802_bss *bss = priv;
10589 struct wpa_driver_nl80211_data *drv = bss->drv;
10590 struct nl_msg *msg;
10591 struct nlattr *data;
10592 int ret;
10593 enum qca_set_band qca_band;
10594
10595 if (!drv->setband_vendor_cmd_avail)
10596 return -1;
10597
10598 switch (band) {
10599 case WPA_SETBAND_AUTO:
10600 qca_band = QCA_SETBAND_AUTO;
10601 break;
10602 case WPA_SETBAND_5G:
10603 qca_band = QCA_SETBAND_5G;
10604 break;
10605 case WPA_SETBAND_2G:
10606 qca_band = QCA_SETBAND_2G;
10607 break;
10608 default:
10609 return -1;
10610 }
10611
10612 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10613 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10614 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10615 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
10616 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10617 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
10618 nlmsg_free(msg);
10619 return -ENOBUFS;
10620 }
10621 nla_nest_end(msg, data);
10622
10623 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10624 if (ret) {
10625 wpa_printf(MSG_DEBUG,
10626 "nl80211: Driver setband function failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010627 strerror(-ret));
Ravi Joshie6ccb162015-07-16 17:45:41 -070010628 }
10629 return ret;
10630}
10631
10632
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010633struct nl80211_pcl {
10634 unsigned int num;
10635 unsigned int *freq_list;
10636};
10637
10638static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
10639{
10640 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10641 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10642 struct nl80211_pcl *param = arg;
10643 struct nlattr *nl_vend, *attr;
10644 enum qca_iface_type iface_type;
10645 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10646 unsigned int num, max_num;
10647 u32 *freqs;
10648
10649 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10650 genlmsg_attrlen(gnlh, 0), NULL);
10651
10652 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
10653 if (!nl_vend)
10654 return NL_SKIP;
10655
10656 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10657 nla_data(nl_vend), nla_len(nl_vend), NULL);
10658
10659 attr = tb_vendor[
10660 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
10661 if (!attr) {
10662 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
10663 param->num = 0;
10664 return NL_SKIP;
10665 }
10666
10667 iface_type = (enum qca_iface_type) nla_get_u32(attr);
10668 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
10669 iface_type);
10670
10671 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
10672 if (!attr) {
10673 wpa_printf(MSG_ERROR,
10674 "nl80211: preferred_freq_list couldn't be found");
10675 param->num = 0;
10676 return NL_SKIP;
10677 }
10678
10679 /*
10680 * param->num has the maximum number of entries for which there
10681 * is room in the freq_list provided by the caller.
10682 */
10683 freqs = nla_data(attr);
10684 max_num = nla_len(attr) / sizeof(u32);
10685 if (max_num > param->num)
10686 max_num = param->num;
10687 for (num = 0; num < max_num; num++)
10688 param->freq_list[num] = freqs[num];
10689 param->num = num;
10690
10691 return NL_SKIP;
10692}
10693
10694
10695static int nl80211_get_pref_freq_list(void *priv,
10696 enum wpa_driver_if_type if_type,
10697 unsigned int *num,
10698 unsigned int *freq_list)
10699{
10700 struct i802_bss *bss = priv;
10701 struct wpa_driver_nl80211_data *drv = bss->drv;
10702 struct nl_msg *msg;
10703 int ret;
10704 unsigned int i;
10705 struct nlattr *params;
10706 struct nl80211_pcl param;
10707 enum qca_iface_type iface_type;
10708
10709 if (!drv->get_pref_freq_list)
10710 return -1;
10711
10712 switch (if_type) {
10713 case WPA_IF_STATION:
10714 iface_type = QCA_IFACE_TYPE_STA;
10715 break;
10716 case WPA_IF_AP_BSS:
10717 iface_type = QCA_IFACE_TYPE_AP;
10718 break;
10719 case WPA_IF_P2P_GO:
10720 iface_type = QCA_IFACE_TYPE_P2P_GO;
10721 break;
10722 case WPA_IF_P2P_CLIENT:
10723 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
10724 break;
10725 case WPA_IF_IBSS:
10726 iface_type = QCA_IFACE_TYPE_IBSS;
10727 break;
10728 case WPA_IF_TDLS:
10729 iface_type = QCA_IFACE_TYPE_TDLS;
10730 break;
10731 default:
10732 return -1;
10733 }
10734
10735 param.num = *num;
10736 param.freq_list = freq_list;
10737
10738 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10739 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10740 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10741 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10742 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
10743 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10744 nla_put_u32(msg,
10745 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
10746 iface_type)) {
10747 wpa_printf(MSG_ERROR,
10748 "%s: err in adding vendor_cmd and vendor_data",
10749 __func__);
10750 nlmsg_free(msg);
10751 return -1;
10752 }
10753 nla_nest_end(msg, params);
10754
10755 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
10756 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
10757 if (ret) {
10758 wpa_printf(MSG_ERROR,
10759 "%s: err in send_and_recv_msgs", __func__);
10760 return ret;
10761 }
10762
10763 *num = param.num;
10764
10765 for (i = 0; i < *num; i++) {
10766 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
10767 i, freq_list[i]);
10768 }
10769
10770 return 0;
10771}
10772
10773
10774static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
10775{
10776 struct i802_bss *bss = priv;
10777 struct wpa_driver_nl80211_data *drv = bss->drv;
10778 struct nl_msg *msg;
10779 int ret;
10780 struct nlattr *params;
10781
10782 if (!drv->set_prob_oper_freq)
10783 return -1;
10784
10785 wpa_printf(MSG_DEBUG,
10786 "nl80211: Set P2P probable operating freq %u for ifindex %d",
10787 freq, bss->ifindex);
10788
10789 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10790 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10791 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10792 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
10793 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10794 nla_put_u32(msg,
10795 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
10796 QCA_IFACE_TYPE_P2P_CLIENT) ||
10797 nla_put_u32(msg,
10798 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
10799 freq)) {
10800 wpa_printf(MSG_ERROR,
10801 "%s: err in adding vendor_cmd and vendor_data",
10802 __func__);
10803 nlmsg_free(msg);
10804 return -1;
10805 }
10806 nla_nest_end(msg, params);
10807
10808 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10809 msg = NULL;
10810 if (ret) {
10811 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
10812 __func__);
10813 return ret;
10814 }
10815 nlmsg_free(msg);
10816 return 0;
10817}
10818
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010819
10820static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
10821 unsigned int period, unsigned int interval,
10822 unsigned int count, const u8 *device_types,
10823 size_t dev_types_len,
10824 const u8 *ies, size_t ies_len)
10825{
10826 struct i802_bss *bss = priv;
10827 struct wpa_driver_nl80211_data *drv = bss->drv;
10828 struct nl_msg *msg;
10829 struct nlattr *container;
10830 int ret;
10831
10832 wpa_printf(MSG_DEBUG,
10833 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
10834 freq, period, interval, count);
10835
10836 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10837 return -1;
10838
10839 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10840 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10841 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10842 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10843 goto fail;
10844
10845 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10846 if (!container)
10847 goto fail;
10848
10849 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10850 freq) ||
10851 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10852 period) ||
10853 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10854 interval) ||
10855 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10856 count) ||
10857 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10858 dev_types_len, device_types) ||
10859 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10860 ies_len, ies))
10861 goto fail;
10862
10863 nla_nest_end(msg, container);
10864 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10865 msg = NULL;
10866 if (ret) {
10867 wpa_printf(MSG_DEBUG,
10868 "nl80211: Failed to send P2P Listen offload vendor command");
10869 goto fail;
10870 }
10871
10872 return 0;
10873
10874fail:
10875 nlmsg_free(msg);
10876 return -1;
10877}
10878
10879
10880static int nl80211_p2p_lo_stop(void *priv)
10881{
10882 struct i802_bss *bss = priv;
10883 struct wpa_driver_nl80211_data *drv = bss->drv;
10884 struct nl_msg *msg;
10885
10886 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10887
10888 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10889 return -1;
10890
10891 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10892 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10893 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10894 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10895 nlmsg_free(msg);
10896 return -1;
10897 }
10898
10899 return send_and_recv_msgs(drv, msg, NULL, NULL);
10900}
10901
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010902
10903static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10904{
10905 struct i802_bss *bss = priv;
10906 struct wpa_driver_nl80211_data *drv = bss->drv;
10907 struct nl_msg *msg;
10908 struct nlattr *params;
10909 int ret;
10910 u32 tdls_mode;
10911
10912 wpa_printf(MSG_DEBUG,
10913 "nl80211: Set TDKS mode: tdls_external_control=%d",
10914 tdls_external_control);
10915
10916 if (tdls_external_control == 1)
10917 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10918 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10919 else
10920 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10921
10922 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10923 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10924 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10925 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10926 goto fail;
10927
10928 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10929 if (!params)
10930 goto fail;
10931
10932 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10933 tdls_mode))
10934 goto fail;
10935
10936 nla_nest_end(msg, params);
10937
10938 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10939 msg = NULL;
10940 if (ret) {
10941 wpa_printf(MSG_ERROR,
10942 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10943 ret, strerror(-ret));
10944 goto fail;
10945 }
10946 return 0;
10947fail:
10948 nlmsg_free(msg);
10949 return -1;
10950}
10951
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010952
10953#ifdef CONFIG_MBO
10954
10955static enum mbo_transition_reject_reason
10956nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10957{
10958 switch (status) {
10959 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10960 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10961 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10962 return MBO_TRANSITION_REJECT_REASON_DELAY;
10963 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10964 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10965 case QCA_STATUS_REJECT_LOW_RSSI:
10966 return MBO_TRANSITION_REJECT_REASON_RSSI;
10967 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10968 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10969 case QCA_STATUS_REJECT_UNKNOWN:
10970 default:
10971 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10972 }
10973}
10974
10975
10976static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10977 struct nlattr *tb[], int num)
10978{
10979 enum qca_wlan_btm_candidate_status status;
10980 char buf[50];
10981
10982 os_memcpy(candidate->bssid,
10983 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10984 ETH_ALEN);
10985
10986 status = nla_get_u32(
10987 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10988 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10989 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10990
10991 if (candidate->is_accept)
10992 os_snprintf(buf, sizeof(buf), "Accepted");
10993 else
10994 os_snprintf(buf, sizeof(buf),
10995 "Rejected, Reject_reason: %d",
10996 candidate->reject_reason);
10997 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10998 num, MAC2STR(candidate->bssid), buf);
10999}
11000
11001
11002static int
11003nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
11004{
11005 struct wpa_bss_candidate_info *info = arg;
11006 struct candidate_list *candidate = info->candidates;
11007 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
11008 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
11009 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
11010 static struct nla_policy policy[
11011 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
11012 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
11013 .minlen = ETH_ALEN
11014 },
11015 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
11016 .type = NLA_U32,
11017 },
11018 };
11019 struct nlattr *attr;
11020 int rem;
11021 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11022 u8 num;
11023
11024 num = info->num; /* number of candidates sent to driver */
11025 info->num = 0;
11026 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11027 genlmsg_attrlen(gnlh, 0), NULL);
11028
11029 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
11030 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
11031 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
11032 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
11033 return NL_SKIP;
11034
11035 wpa_printf(MSG_DEBUG,
11036 "nl80211: WNM Candidate list received from driver");
11037 nla_for_each_nested(attr,
11038 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
11039 rem) {
11040 if (info->num >= num ||
11041 nla_parse_nested(
11042 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
11043 attr, policy) ||
11044 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
11045 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
11046 break;
11047
11048 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
11049
11050 candidate++;
11051 info->num++;
11052 }
11053
11054 return NL_SKIP;
11055}
11056
11057
11058static struct wpa_bss_candidate_info *
11059nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
11060{
11061 struct i802_bss *bss = priv;
11062 struct wpa_driver_nl80211_data *drv = bss->drv;
11063 struct nl_msg *msg;
11064 struct nlattr *attr, *attr1, *attr2;
11065 struct wpa_bss_candidate_info *info;
11066 u8 i;
11067 int ret;
11068 u8 *pos;
11069
11070 if (!drv->fetch_bss_trans_status)
11071 return NULL;
11072
11073 info = os_zalloc(sizeof(*info));
11074 if (!info)
11075 return NULL;
11076 /* Allocate memory for number of candidates sent to driver */
11077 info->candidates = os_calloc(params->n_candidates,
11078 sizeof(*info->candidates));
11079 if (!info->candidates) {
11080 os_free(info);
11081 return NULL;
11082 }
11083
11084 /* Copy the number of candidates being sent to driver. This is used in
11085 * nl80211_get_bss_transition_status_handler() to limit the number of
11086 * candidates that can be populated in info->candidates and will be
11087 * later overwritten with the actual number of candidates received from
11088 * the driver.
11089 */
11090 info->num = params->n_candidates;
11091
11092 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11093 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11094 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11095 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
11096 goto fail;
11097
11098 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
11099 if (!attr)
11100 goto fail;
11101
11102 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
11103 params->mbo_transition_reason))
11104 goto fail;
11105
11106 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
11107 if (!attr1)
11108 goto fail;
11109
11110 wpa_printf(MSG_DEBUG,
11111 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
11112 params->mbo_transition_reason, params->n_candidates);
11113 pos = params->bssid;
11114 for (i = 0; i < params->n_candidates; i++) {
11115 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
11116 MAC2STR(pos));
11117 attr2 = nla_nest_start(msg, i);
11118 if (!attr2 ||
11119 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
11120 ETH_ALEN, pos))
11121 goto fail;
11122 pos += ETH_ALEN;
11123 nla_nest_end(msg, attr2);
11124 }
11125
11126 nla_nest_end(msg, attr1);
11127 nla_nest_end(msg, attr);
11128
11129 ret = send_and_recv_msgs(drv, msg,
11130 nl80211_get_bss_transition_status_handler,
11131 info);
11132 msg = NULL;
11133 if (ret) {
11134 wpa_printf(MSG_ERROR,
11135 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
11136 ret, strerror(-ret));
11137 goto fail;
11138 }
11139 return info;
11140
11141fail:
11142 nlmsg_free(msg);
11143 os_free(info->candidates);
11144 os_free(info);
11145 return NULL;
11146}
11147
11148
11149/**
11150 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
11151 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
11152 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
11153 * Returns: 0 on success, -1 on failure
11154 */
11155static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
11156{
11157 struct i802_bss *bss = priv;
11158 struct wpa_driver_nl80211_data *drv = bss->drv;
11159 struct nl_msg *msg;
11160 struct nlattr *attr;
11161 int ret = -1;
11162
11163 if (!drv->set_wifi_conf_vendor_cmd_avail)
11164 return -1;
11165
11166 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11167 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11168 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11169 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
11170 goto fail;
11171
11172 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
11173 if (!attr)
11174 goto fail;
11175
11176 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
11177 ignore_disallow);
11178 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
11179 ignore_disallow))
11180 goto fail;
11181
11182 nla_nest_end(msg, attr);
11183
11184 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11185 msg = NULL;
11186 if (ret) {
11187 wpa_printf(MSG_ERROR,
11188 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
11189 ret, strerror(-ret));
11190 goto fail;
11191 }
11192
11193fail:
11194 nlmsg_free(msg);
11195 return ret;
11196}
11197
11198#endif /* CONFIG_MBO */
11199
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011200#endif /* CONFIG_DRIVER_NL80211_QCA */
11201
11202
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011203static int nl80211_write_to_file(const char *name, unsigned int val)
11204{
11205 int fd, len;
11206 char tmp[128];
Hai Shalomc3565922019-10-28 11:58:20 -070011207 int ret = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011208
11209 fd = open(name, O_RDWR);
11210 if (fd < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -070011211 int level;
11212 /*
11213 * Flags may not exist on older kernels, or while we're tearing
11214 * down a disappearing device.
11215 */
11216 if (errno == ENOENT) {
11217 ret = 0;
11218 level = MSG_DEBUG;
11219 } else {
11220 ret = -1;
11221 level = MSG_ERROR;
11222 }
11223 wpa_printf(level, "nl80211: Failed to open %s: %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011224 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070011225 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011226 }
11227
11228 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
11229 len = write(fd, tmp, len);
Hai Shalomc3565922019-10-28 11:58:20 -070011230 if (len < 0) {
11231 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011232 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
11233 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070011234 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011235 close(fd);
11236
Hai Shalomc3565922019-10-28 11:58:20 -070011237 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011238}
11239
11240
11241static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
11242{
11243 struct i802_bss *bss = priv;
11244 char path[128];
11245 int ret;
11246
11247 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
11248 filter_flags);
11249
11250 /* Configure filtering of unicast frame encrypted using GTK */
11251 ret = os_snprintf(path, sizeof(path),
11252 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
11253 bss->ifname);
11254 if (os_snprintf_error(sizeof(path), ret))
11255 return -1;
11256
11257 ret = nl80211_write_to_file(path,
11258 !!(filter_flags &
11259 WPA_DATA_FRAME_FILTER_FLAG_GTK));
11260 if (ret) {
11261 wpa_printf(MSG_ERROR,
11262 "nl80211: Failed to set IPv4 unicast in multicast filter");
11263 return ret;
11264 }
11265
11266 os_snprintf(path, sizeof(path),
11267 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
11268 bss->ifname);
11269 ret = nl80211_write_to_file(path,
11270 !!(filter_flags &
11271 WPA_DATA_FRAME_FILTER_FLAG_GTK));
11272
11273 if (ret) {
11274 wpa_printf(MSG_ERROR,
11275 "nl80211: Failed to set IPv6 unicast in multicast filter");
11276 return ret;
11277 }
11278
11279 /* Configure filtering of unicast frame encrypted using GTK */
11280 os_snprintf(path, sizeof(path),
11281 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
11282 bss->ifname);
11283 ret = nl80211_write_to_file(path,
11284 !!(filter_flags &
11285 WPA_DATA_FRAME_FILTER_FLAG_ARP));
11286 if (ret) {
11287 wpa_printf(MSG_ERROR,
11288 "nl80211: Failed set gratuitous ARP filter");
11289 return ret;
11290 }
11291
11292 /* Configure filtering of IPv6 NA frames */
11293 os_snprintf(path, sizeof(path),
11294 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
11295 bss->ifname);
11296 ret = nl80211_write_to_file(path,
11297 !!(filter_flags &
11298 WPA_DATA_FRAME_FILTER_FLAG_NA));
11299 if (ret) {
11300 wpa_printf(MSG_ERROR,
11301 "nl80211: Failed to set unsolicited NA filter");
11302 return ret;
11303 }
11304
11305 return 0;
11306}
11307
11308
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070011309static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
11310 const u8 **ext_capa, const u8 **ext_capa_mask,
11311 unsigned int *ext_capa_len)
11312{
11313 struct i802_bss *bss = priv;
11314 struct wpa_driver_nl80211_data *drv = bss->drv;
11315 enum nl80211_iftype nlmode;
11316 unsigned int i;
11317
11318 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
11319 return -1;
11320
11321 nlmode = wpa_driver_nl80211_if_type(type);
11322
11323 /* By default, use the per-radio values */
11324 *ext_capa = drv->extended_capa;
11325 *ext_capa_mask = drv->extended_capa_mask;
11326 *ext_capa_len = drv->extended_capa_len;
11327
11328 /* Replace the default value if a per-interface type value exists */
11329 for (i = 0; i < drv->num_iface_ext_capa; i++) {
11330 if (nlmode == drv->iface_ext_capa[i].iftype) {
11331 *ext_capa = drv->iface_ext_capa[i].ext_capa;
11332 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
11333 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
11334 break;
11335 }
11336 }
11337
11338 return 0;
11339}
11340
11341
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011342static int nl80211_update_connection_params(
11343 void *priv, struct wpa_driver_associate_params *params,
11344 enum wpa_drv_update_connect_params_mask mask)
11345{
11346 struct i802_bss *bss = priv;
11347 struct wpa_driver_nl80211_data *drv = bss->drv;
11348 struct nl_msg *msg;
11349 int ret = -1;
11350 enum nl80211_auth_type type;
11351
Hai Shalomc3565922019-10-28 11:58:20 -070011352 /* Update Connection Params is intended for drivers that implement
11353 * internal SME and expect these updated connection params from
11354 * wpa_supplicant. Do not send this request for the drivers using
11355 * SME from wpa_supplicant.
11356 */
11357 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
11358 return 0;
11359
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011360 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
11361 if (!msg)
11362 goto fail;
11363
11364 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
11365 drv->ifindex);
11366
11367 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
11368 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
11369 params->wpa_ie))
11370 goto fail;
11371 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
11372 params->wpa_ie_len);
11373 }
11374
11375 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
11376 type = get_nl_auth_type(params->auth_alg);
11377 if (type == NL80211_AUTHTYPE_MAX ||
11378 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
11379 goto fail;
11380 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
11381 }
11382
11383 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
11384 nl80211_put_fils_connect_params(drv, params, msg))
11385 goto fail;
11386
11387 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11388 msg = NULL;
11389 if (ret)
11390 wpa_dbg(drv->ctx, MSG_DEBUG,
11391 "nl80211: Update connect params command failed: ret=%d (%s)",
11392 ret, strerror(-ret));
11393
11394fail:
11395 nlmsg_free(msg);
11396 return ret;
11397}
11398
11399
Roshan Pius3a1667e2018-07-03 15:17:14 -070011400static int nl80211_send_external_auth_status(void *priv,
11401 struct external_auth *params)
11402{
11403 struct i802_bss *bss = priv;
11404 struct wpa_driver_nl80211_data *drv = bss->drv;
11405 struct nl_msg *msg = NULL;
11406 int ret = -1;
11407
Hai Shalom5f92bc92019-04-18 11:54:11 -070011408 /* External auth command/status is intended for drivers that implement
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080011409 * internal SME but want to offload authentication processing (e.g.,
11410 * SAE) to hostapd/wpa_supplicant. Do not send the status to drivers
Hai Shalom5f92bc92019-04-18 11:54:11 -070011411 * which do not support AP SME or use wpa_supplicant/hostapd SME.
11412 */
Hai Shalom81f62d82019-07-22 12:10:00 -070011413 if ((is_ap_interface(drv->nlmode) && !bss->drv->device_ap_sme) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070011414 (drv->capa.flags & WPA_DRIVER_FLAGS_SME))
11415 return -1;
11416
Roshan Pius3a1667e2018-07-03 15:17:14 -070011417 wpa_dbg(drv->ctx, MSG_DEBUG,
11418 "nl80211: External auth status: %u", params->status);
11419
11420 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
11421 if (!msg ||
11422 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070011423 (params->ssid && params->ssid_len &&
11424 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
11425 (params->pmkid &&
11426 nla_put(msg, NL80211_ATTR_PMKID, PMKID_LEN, params->pmkid)) ||
11427 (params->bssid &&
11428 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid)))
Roshan Pius3a1667e2018-07-03 15:17:14 -070011429 goto fail;
11430 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11431 msg = NULL;
11432 if (ret) {
11433 wpa_printf(MSG_DEBUG,
11434 "nl80211: External Auth status update failed: ret=%d (%s)",
11435 ret, strerror(-ret));
11436 goto fail;
11437 }
11438fail:
11439 nlmsg_free(msg);
11440 return ret;
11441}
11442
11443
Hai Shalom74f70d42019-02-11 14:42:39 -080011444static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
11445 int val)
11446{
11447 struct i802_bss *bss = priv;
11448 struct wpa_driver_nl80211_data *drv = bss->drv;
11449 struct nl_msg *msg;
11450 int ret = -ENOBUFS;
11451
11452 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
11453 val ? "Enable" : "Disable", bridge_ifname);
11454
11455 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
11456 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
11457 goto fail;
11458
11459 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
11460 if (linux_br_del_if(drv->global->ioctl_sock,
11461 bridge_ifname, bss->ifname)) {
11462 wpa_printf(MSG_ERROR,
11463 "nl80211: Failed to remove interface %s from bridge %s",
11464 bss->ifname, bridge_ifname);
11465 return -1;
11466 }
11467 bss->added_if_into_bridge = 0;
11468 }
11469
11470 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11471 msg = NULL;
11472 if (!ret) {
11473 if (bridge_ifname[0] && val &&
11474 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
11475 return -1;
11476 return 0;
11477 }
11478
11479fail:
11480 nlmsg_free(msg);
11481 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
11482
11483 return ret;
11484}
11485
11486
Hai Shalome21d4e82020-04-29 16:34:06 -070011487#ifdef CONFIG_DPP
11488static int nl80211_dpp_listen(void *priv, bool enable)
11489{
11490 struct i802_bss *bss = priv;
11491 struct wpa_driver_nl80211_data *drv = bss->drv;
11492 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
11493 struct nl_sock *handle;
11494
11495 if (!drv->multicast_registrations || !bss->nl_mgmt)
11496 return 0; /* cannot do more than hope broadcast RX works */
11497
11498 wpa_printf(MSG_DEBUG,
11499 "nl80211: Update DPP Public Action frame registration (%s multicast RX)",
11500 enable ? "enable" : "disable");
11501 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
11502 return nl80211_register_frame(bss, handle, type,
11503 (u8 *) "\x04\x09\x50\x6f\x9a\x1a", 6,
11504 enable);
11505}
11506#endif /* CONFIG_DPP */
11507
11508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011509const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11510 .name = "nl80211",
11511 .desc = "Linux nl80211/cfg80211",
11512 .get_bssid = wpa_driver_nl80211_get_bssid,
11513 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011514 .set_key = driver_nl80211_set_key,
11515 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011516 .sched_scan = wpa_driver_nl80211_sched_scan,
11517 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011518 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011519 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011520 .deauthenticate = driver_nl80211_deauthenticate,
11521 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011522 .associate = wpa_driver_nl80211_associate,
11523 .global_init = nl80211_global_init,
11524 .global_deinit = nl80211_global_deinit,
11525 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011526 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011527 .get_capa = wpa_driver_nl80211_get_capa,
11528 .set_operstate = wpa_driver_nl80211_set_operstate,
11529 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11530 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011531 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011532 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011533 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011534 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011535 .if_remove = driver_nl80211_if_remove,
11536 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011537 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011538 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011539 .sta_remove = driver_nl80211_sta_remove,
Hai Shalomfdcde762020-04-02 11:19:20 -070011540 .tx_control_port = nl80211_tx_control_port,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011541 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11542 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Hai Shalom81f62d82019-07-22 12:10:00 -070011543 .sta_set_airtime_weight = driver_nl80211_sta_set_airtime_weight,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011544 .hapd_init = i802_init,
11545 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011546 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011547 .get_seqnum = i802_get_seqnum,
11548 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011549 .get_inact_sec = i802_get_inact_sec,
11550 .sta_clear_stats = i802_sta_clear_stats,
11551 .set_rts = i802_set_rts,
11552 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011553 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011554 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011555 .sta_deauth = i802_sta_deauth,
11556 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011557 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011558 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011559 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011560 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11561 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11562 .cancel_remain_on_channel =
11563 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011564 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011565 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011566 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011567 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011568 .signal_monitor = nl80211_signal_monitor,
11569 .signal_poll = nl80211_signal_poll,
Hai Shalom74f70d42019-02-11 14:42:39 -080011570 .channel_info = nl80211_channel_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011571 .set_param = nl80211_set_param,
11572 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011573 .add_pmkid = nl80211_add_pmkid,
11574 .remove_pmkid = nl80211_remove_pmkid,
11575 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011576 .set_rekey_info = nl80211_set_rekey_info,
11577 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011578 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011579 .start_dfs_cac = nl80211_start_radar_detection,
11580 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011581#ifdef CONFIG_TDLS
11582 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11583 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011584 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
11585 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011586#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011587 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Hai Shalom81f62d82019-07-22 12:10:00 -070011588 .update_dh_ie = nl80211_update_dh_ie,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011589 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011590 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011591 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011592 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011593#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011594 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011595 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011596 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011597#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011598#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070011599#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011600 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070011601#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011602#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011603 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011604 .set_qos_map = nl80211_set_qos_map,
Hai Shalomfdcde762020-04-02 11:19:20 -070011605 .get_wowlan = nl80211_get_wowlan,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011606 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011607 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011608#ifdef CONFIG_MESH
11609 .init_mesh = wpa_driver_nl80211_init_mesh,
11610 .join_mesh = wpa_driver_nl80211_join_mesh,
11611 .leave_mesh = wpa_driver_nl80211_leave_mesh,
Hai Shalom81f62d82019-07-22 12:10:00 -070011612 .probe_mesh_link = nl80211_probe_mesh_link,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011613#endif /* CONFIG_MESH */
11614 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
11615 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
11616 .br_port_set_attr = wpa_driver_br_port_set_attr,
11617 .br_set_net_param = wpa_driver_br_set_net_param,
11618 .add_tx_ts = nl80211_add_ts,
11619 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070011620 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011621#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070011622 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070011623 .disable_fils = nl80211_disable_fils,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011624 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -070011625 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011626 .get_pref_freq_list = nl80211_get_pref_freq_list,
11627 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070011628 .p2p_lo_start = nl80211_p2p_lo_start,
11629 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070011630 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080011631 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011632#ifdef CONFIG_MBO
11633 .get_bss_transition_status = nl80211_get_bss_transition_status,
11634 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
11635#endif /* CONFIG_MBO */
11636 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
Hai Shalomc3565922019-10-28 11:58:20 -070011637 .add_sta_node = nl80211_add_sta_node,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011638#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011639 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070011640 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011641 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070011642 .send_external_auth_status = nl80211_send_external_auth_status,
Hai Shalom74f70d42019-02-11 14:42:39 -080011643 .set_4addr_mode = nl80211_set_4addr_mode,
Hai Shalome21d4e82020-04-29 16:34:06 -070011644#ifdef CONFIG_DPP
11645 .dpp_listen = nl80211_dpp_listen,
11646#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011647};