blob: 204099b8ed0cd64ebc59ef1b7d12612afa63b559 [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 }
Sunil Ravi90775442020-09-24 11:53:19 -07001203 } else if (ifi->ifi_flags & IFF_UP) {
1204 /* Re-read MAC address as it may have changed */
1205 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
1206 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 }
1208
1209 /*
1210 * Some drivers send the association event before the operup event--in
1211 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1212 * fails. This will hit us when wpa_supplicant does not need to do
1213 * IEEE 802.1X authentication
1214 */
1215 if (drv->operstate == 1 &&
1216 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001217 !(ifi->ifi_flags & IFF_RUNNING)) {
1218 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 }
1222
Dmitry Shmidte4663042016-04-04 10:07:49 -07001223event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001224 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001225 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1226 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001227
Dmitry Shmidte4663042016-04-04 10:07:49 -07001228 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001229 struct i802_bss *bss;
1230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001232 if (!if_indextoname(brid, namebuf)) {
1233 wpa_printf(MSG_DEBUG,
1234 "nl80211: Could not find bridge ifname for ifindex %u",
1235 brid);
1236 return;
1237 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1239 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001240 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241
1242 for (bss = drv->first_bss; bss; bss = bss->next) {
1243 if (os_strcmp(ifname, bss->ifname) == 0) {
1244 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1245 break;
1246 }
1247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249}
1250
1251
1252static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1253 struct ifinfomsg *ifi,
1254 u8 *buf, size_t len)
1255{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001256 struct nl80211_global *global = ctx;
1257 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001258 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 struct rtattr *attr;
1260 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001261 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001262 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001264 extra[0] = '\0';
1265 pos = extra;
1266 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001267 ifname[0] = '\0';
1268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 attrlen = len;
1270 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001272 switch (attr->rta_type) {
1273 case IFLA_IFNAME:
Hai Shalomfdcde762020-04-02 11:19:20 -07001274 if (RTA_PAYLOAD(attr) > IFNAMSIZ)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001275 break;
1276 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1277 ifname[RTA_PAYLOAD(attr)] = '\0';
1278 break;
1279 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001281 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1282 break;
1283 case IFLA_OPERSTATE:
1284 pos += os_snprintf(pos, end - pos, " operstate=%u",
1285 nla_get_u32((struct nlattr *) attr));
1286 break;
1287 case IFLA_LINKMODE:
1288 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1289 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001290 break;
1291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 attr = RTA_NEXT(attr, attrlen);
1293 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001294 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001296 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1297 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1298 ifi->ifi_flags,
1299 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1300 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1301 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1302 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1303
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001304 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001305
Dmitry Shmidte4663042016-04-04 10:07:49 -07001306 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 /* device has been removed from bridge */
1308 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001309
1310 if (!if_indextoname(brid, namebuf)) {
1311 wpa_printf(MSG_DEBUG,
1312 "nl80211: Could not find bridge ifname for ifindex %u",
1313 brid);
1314 } else {
1315 wpa_printf(MSG_DEBUG,
1316 "nl80211: Remove ifindex %u for bridge %s",
1317 brid, namebuf);
1318 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001319 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001321
1322 if (ifi->ifi_family != AF_BRIDGE || !brid)
1323 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1324 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325}
1326
1327
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001328struct nl80211_get_assoc_freq_arg {
1329 struct wpa_driver_nl80211_data *drv;
1330 unsigned int assoc_freq;
1331 unsigned int ibss_freq;
1332 u8 assoc_bssid[ETH_ALEN];
1333 u8 assoc_ssid[SSID_MAX_LEN];
1334 u8 assoc_ssid_len;
1335};
1336
1337static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1338{
1339 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1340 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1341 struct nlattr *bss[NL80211_BSS_MAX + 1];
1342 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1343 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1344 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1345 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1346 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1347 };
1348 struct nl80211_get_assoc_freq_arg *ctx = arg;
1349 enum nl80211_bss_status status;
1350
1351 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1352 genlmsg_attrlen(gnlh, 0), NULL);
1353 if (!tb[NL80211_ATTR_BSS] ||
1354 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1355 bss_policy) ||
1356 !bss[NL80211_BSS_STATUS])
1357 return NL_SKIP;
1358
1359 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1360 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1361 bss[NL80211_BSS_FREQUENCY]) {
1362 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1363 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1364 ctx->assoc_freq);
1365 }
1366 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1367 bss[NL80211_BSS_FREQUENCY]) {
1368 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1369 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1370 ctx->ibss_freq);
1371 }
1372 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1373 bss[NL80211_BSS_BSSID]) {
1374 os_memcpy(ctx->assoc_bssid,
1375 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1376 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1377 MACSTR, MAC2STR(ctx->assoc_bssid));
1378 }
1379
1380 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1381 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1382 const u8 *ie, *ssid;
1383 size_t ie_len;
1384
1385 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1386 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1387 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1388 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1389 ctx->assoc_ssid_len = ssid[1];
1390 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1391 }
1392 }
1393
1394 return NL_SKIP;
1395}
1396
1397
1398int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001399{
1400 struct nl_msg *msg;
1401 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001402 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001403 int count = 0;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001404
Hai Shalomfdcde762020-04-02 11:19:20 -07001405try_again:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001406 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001407 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001408 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001409 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1410 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001411 if (ret == -EAGAIN) {
1412 count++;
1413 if (count >= 10) {
1414 wpa_printf(MSG_INFO,
1415 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid");
1416 } else {
1417 wpa_printf(MSG_DEBUG,
1418 "nl80211: Failed to receive consistent scan result dump for get_assoc_ssid - try again");
1419 goto try_again;
1420 }
1421 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001422 if (ret == 0) {
1423 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1424 return arg.assoc_ssid_len;
1425 }
1426 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1427 ret, strerror(-ret));
1428 return ret;
1429}
1430
1431
1432unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1433{
1434 struct nl_msg *msg;
1435 int ret;
1436 struct nl80211_get_assoc_freq_arg arg;
Hai Shalomfdcde762020-04-02 11:19:20 -07001437 int count = 0;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001438
Hai Shalomfdcde762020-04-02 11:19:20 -07001439try_again:
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001440 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1441 os_memset(&arg, 0, sizeof(arg));
1442 arg.drv = drv;
1443 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1444 &arg);
Hai Shalomfdcde762020-04-02 11:19:20 -07001445 if (ret == -EAGAIN) {
1446 count++;
1447 if (count >= 10) {
1448 wpa_printf(MSG_INFO,
1449 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq");
1450 } else {
1451 wpa_printf(MSG_DEBUG,
1452 "nl80211: Failed to receive consistent scan result dump for get_assoc_freq - try again");
1453 goto try_again;
1454 }
1455 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001456 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001457 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1458 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001459 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001460 "associated BSS from scan results: %u MHz", freq);
1461 if (freq)
1462 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001463 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001464 }
1465 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1466 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001467 return drv->assoc_freq;
1468}
1469
1470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471static int get_link_signal(struct nl_msg *msg, void *arg)
1472{
1473 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1474 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1475 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1476 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1477 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001478 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001479 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 };
1481 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1482 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1483 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1484 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1485 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1486 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1487 };
1488 struct wpa_signal_info *sig_change = arg;
1489
1490 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1491 genlmsg_attrlen(gnlh, 0), NULL);
1492 if (!tb[NL80211_ATTR_STA_INFO] ||
1493 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1494 tb[NL80211_ATTR_STA_INFO], policy))
1495 return NL_SKIP;
1496 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1497 return NL_SKIP;
1498
1499 sig_change->current_signal =
1500 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1501
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001502 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1503 sig_change->avg_signal =
1504 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1505 else
1506 sig_change->avg_signal = 0;
1507
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001508 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1509 sig_change->avg_beacon_signal =
1510 (s8)
1511 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1512 else
1513 sig_change->avg_beacon_signal = 0;
1514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1516 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1517 sinfo[NL80211_STA_INFO_TX_BITRATE],
1518 rate_policy)) {
1519 sig_change->current_txrate = 0;
1520 } else {
1521 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1522 sig_change->current_txrate =
1523 nla_get_u16(rinfo[
1524 NL80211_RATE_INFO_BITRATE]) * 100;
1525 }
1526 }
1527 }
1528
1529 return NL_SKIP;
1530}
1531
1532
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001533int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1534 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535{
1536 struct nl_msg *msg;
1537
Hai Shalom74f70d42019-02-11 14:42:39 -08001538 sig->current_signal = -WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 sig->current_txrate = 0;
1540
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001541 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1542 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1543 nlmsg_free(msg);
1544 return -ENOBUFS;
1545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546
1547 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548}
1549
1550
1551static int get_link_noise(struct nl_msg *msg, void *arg)
1552{
1553 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1554 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1555 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1556 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1557 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1558 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1559 };
1560 struct wpa_signal_info *sig_change = arg;
1561
1562 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1563 genlmsg_attrlen(gnlh, 0), NULL);
1564
1565 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1566 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1567 return NL_SKIP;
1568 }
1569
1570 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1571 tb[NL80211_ATTR_SURVEY_INFO],
1572 survey_policy)) {
1573 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1574 "attributes!");
1575 return NL_SKIP;
1576 }
1577
1578 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1579 return NL_SKIP;
1580
1581 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1582 sig_change->frequency)
1583 return NL_SKIP;
1584
1585 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1586 return NL_SKIP;
1587
1588 sig_change->current_noise =
1589 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1590
1591 return NL_SKIP;
1592}
1593
1594
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001595int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1596 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597{
1598 struct nl_msg *msg;
1599
Hai Shalom74f70d42019-02-11 14:42:39 -08001600 sig_change->current_noise = WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 sig_change->frequency = drv->assoc_freq;
1602
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001603 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605}
1606
1607
Hai Shalom74f70d42019-02-11 14:42:39 -08001608static int get_channel_info(struct nl_msg *msg, void *arg)
1609{
1610 struct nlattr *tb[NL80211_ATTR_MAX + 1] = { 0 };
1611 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1612 struct wpa_channel_info *chan_info = arg;
1613
1614 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1615 genlmsg_attrlen(gnlh, 0), NULL);
1616
1617 os_memset(chan_info, 0, sizeof(struct wpa_channel_info));
1618 chan_info->chanwidth = CHAN_WIDTH_UNKNOWN;
1619
1620 if (tb[NL80211_ATTR_WIPHY_FREQ])
1621 chan_info->frequency =
1622 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1623 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
1624 chan_info->chanwidth = convert2width(
1625 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
1626 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1627 enum nl80211_channel_type ct =
1628 nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1629
1630 switch (ct) {
1631 case NL80211_CHAN_HT40MINUS:
1632 chan_info->sec_channel = -1;
1633 break;
1634 case NL80211_CHAN_HT40PLUS:
1635 chan_info->sec_channel = 1;
1636 break;
1637 default:
1638 chan_info->sec_channel = 0;
1639 break;
1640 }
1641 }
1642 if (tb[NL80211_ATTR_CENTER_FREQ1])
1643 chan_info->center_frq1 =
1644 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1645 if (tb[NL80211_ATTR_CENTER_FREQ2])
1646 chan_info->center_frq2 =
1647 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1648
1649 if (chan_info->center_frq2) {
1650 u8 seg1_idx = 0;
1651
1652 if (ieee80211_freq_to_chan(chan_info->center_frq2, &seg1_idx) !=
1653 NUM_HOSTAPD_MODES)
1654 chan_info->seg1_idx = seg1_idx;
1655 }
1656
1657 return NL_SKIP;
1658}
1659
1660
1661static int nl80211_channel_info(void *priv, struct wpa_channel_info *ci)
1662{
1663 struct i802_bss *bss = priv;
1664 struct wpa_driver_nl80211_data *drv = bss->drv;
1665 struct nl_msg *msg;
1666
1667 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
1668 return send_and_recv_msgs(drv, msg, get_channel_info, ci);
1669}
1670
1671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1673 void *handle)
1674{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001675 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001676 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001678 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001680 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001681 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001682 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1683 __func__, res);
1684 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685}
1686
1687
1688/**
1689 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1690 * @priv: driver_nl80211 private data
1691 * @alpha2_arg: country to which to switch to
1692 * Returns: 0 on success, -1 on failure
1693 *
1694 * This asks nl80211 to set the regulatory domain for given
1695 * country ISO / IEC alpha2.
1696 */
1697static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1698{
1699 struct i802_bss *bss = priv;
1700 struct wpa_driver_nl80211_data *drv = bss->drv;
1701 char alpha2[3];
1702 struct nl_msg *msg;
1703
1704 msg = nlmsg_alloc();
1705 if (!msg)
1706 return -ENOMEM;
1707
1708 alpha2[0] = alpha2_arg[0];
1709 alpha2[1] = alpha2_arg[1];
1710 alpha2[2] = '\0';
1711
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001712 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1713 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1714 nlmsg_free(msg);
1715 return -EINVAL;
1716 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1718 return -EINVAL;
1719 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720}
1721
1722
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001723static int nl80211_get_country(struct nl_msg *msg, void *arg)
1724{
1725 char *alpha2 = arg;
1726 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1727 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1728
1729 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1730 genlmsg_attrlen(gnlh, 0), NULL);
1731 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1732 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1733 return NL_SKIP;
1734 }
1735 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1736 return NL_SKIP;
1737}
1738
1739
1740static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1741{
1742 struct i802_bss *bss = priv;
1743 struct wpa_driver_nl80211_data *drv = bss->drv;
1744 struct nl_msg *msg;
1745 int ret;
1746
1747 msg = nlmsg_alloc();
1748 if (!msg)
1749 return -ENOMEM;
1750
1751 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1752 alpha2[0] = '\0';
1753 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1754 if (!alpha2[0])
1755 ret = -1;
1756
1757 return ret;
1758}
1759
1760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001761static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762{
1763 int ret;
1764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001765 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1766 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1768 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001769 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 }
1771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001772 global->nl = nl_create_handle(global->nl_cb, "nl");
1773 if (global->nl == NULL)
1774 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001776 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1777 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1779 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001780 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 }
1782
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001783 global->nl_event = nl_create_handle(global->nl_cb, "event");
1784 if (global->nl_event == NULL)
1785 goto err;
1786
1787 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001789 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 if (ret < 0) {
1791 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1792 "membership for scan events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001793 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001794 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795 }
1796
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001797 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001799 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 if (ret < 0) {
1801 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1802 "membership for mlme events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001803 ret, nl_geterror(ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001804 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 }
1806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001807 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 if (ret < 0) {
1811 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1812 "membership for regulatory events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001813 ret, nl_geterror(ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 /* Continue without regulatory events */
1815 }
1816
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001817 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1818 if (ret >= 0)
1819 ret = nl_socket_add_membership(global->nl_event, ret);
1820 if (ret < 0) {
1821 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1822 "membership for vendor events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001823 ret, nl_geterror(ret));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001824 /* Continue without vendor events */
1825 }
1826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001827 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1828 no_seq_check, NULL);
1829 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1830 process_global_event, global);
1831
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001832 nl80211_register_eloop_read(&global->nl_event,
1833 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001834 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835
1836 return 0;
1837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838err:
1839 nl_destroy_handles(&global->nl_event);
1840 nl_destroy_handles(&global->nl);
1841 nl_cb_put(global->nl_cb);
1842 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 return -1;
1844}
1845
1846
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001847static void nl80211_check_global(struct nl80211_global *global)
1848{
Hai Shalomfdcde762020-04-02 11:19:20 -07001849 struct nl_sock *handle;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001850 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1851 int ret;
1852 unsigned int i;
1853
1854 /*
1855 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1856 * and all registration having been cleared.
1857 */
1858 handle = (void *) (((intptr_t) global->nl_event) ^
1859 ELOOP_SOCKET_INVALID);
1860
1861 for (i = 0; groups[i]; i++) {
1862 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1863 if (ret >= 0)
1864 ret = nl_socket_add_membership(handle, ret);
1865 if (ret < 0) {
1866 wpa_printf(MSG_INFO,
1867 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
Hai Shalomfdcde762020-04-02 11:19:20 -07001868 groups[i], ret, nl_geterror(ret));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001869 }
1870 }
1871}
1872
1873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1875{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001876 struct wpa_driver_nl80211_data *drv = ctx;
1877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001881 * rtnetlink ifdown handler will report interfaces other than the P2P
1882 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001884 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1885 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886}
1887
1888
1889static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1890{
1891 struct wpa_driver_nl80211_data *drv = ctx;
1892 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001893 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1895 "after rfkill unblock");
1896 return;
1897 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001898
1899 if (is_p2p_net_interface(drv->nlmode))
1900 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1901
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001902 /*
1903 * rtnetlink ifup handler will report interfaces other than the P2P
1904 * Device interface as enabled.
1905 */
1906 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1907 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908}
1909
1910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001911static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1912 void *eloop_ctx,
1913 void *handle)
1914{
1915 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1916 u8 data[2048];
1917 struct msghdr msg;
1918 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001919 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001920 struct cmsghdr *cmsg;
1921 int res, found_ee = 0, found_wifi = 0, acked = 0;
1922 union wpa_event_data event;
1923
1924 memset(&msg, 0, sizeof(msg));
1925 msg.msg_iov = &entry;
1926 msg.msg_iovlen = 1;
1927 entry.iov_base = data;
1928 entry.iov_len = sizeof(data);
1929 msg.msg_control = &control;
1930 msg.msg_controllen = sizeof(control);
1931
1932 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1933 /* if error or not fitting 802.3 header, return */
1934 if (res < 14)
1935 return;
1936
1937 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1938 {
1939 if (cmsg->cmsg_level == SOL_SOCKET &&
1940 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1941 int *ack;
1942
1943 found_wifi = 1;
1944 ack = (void *)CMSG_DATA(cmsg);
1945 acked = *ack;
1946 }
1947
1948 if (cmsg->cmsg_level == SOL_PACKET &&
1949 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1950 struct sock_extended_err *err =
1951 (struct sock_extended_err *)CMSG_DATA(cmsg);
1952
1953 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1954 found_ee = 1;
1955 }
1956 }
1957
1958 if (!found_ee || !found_wifi)
1959 return;
1960
1961 memset(&event, 0, sizeof(event));
1962 event.eapol_tx_status.dst = data;
1963 event.eapol_tx_status.data = data + 14;
1964 event.eapol_tx_status.data_len = res - 14;
1965 event.eapol_tx_status.ack = acked;
1966 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1967}
1968
1969
Hai Shalomb755a2a2020-04-23 21:49:02 -07001970static int nl80211_init_connect_handle(struct i802_bss *bss)
1971{
1972 if (bss->nl_connect) {
1973 wpa_printf(MSG_DEBUG,
1974 "nl80211: Connect handle already created (nl_connect=%p)",
1975 bss->nl_connect);
1976 return -1;
1977 }
1978
1979 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
1980 if (!bss->nl_connect)
1981 return -1;
1982 nl80211_register_eloop_read(&bss->nl_connect,
1983 wpa_driver_nl80211_event_receive,
1984 bss->nl_cb, 1);
1985 return 0;
1986}
1987
1988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989static int nl80211_init_bss(struct i802_bss *bss)
1990{
1991 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1992 if (!bss->nl_cb)
1993 return -1;
1994
1995 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1996 no_seq_check, NULL);
1997 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1998 process_bss_event, bss);
1999
Hai Shalomb755a2a2020-04-23 21:49:02 -07002000 nl80211_init_connect_handle(bss);
2001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002002 return 0;
2003}
2004
2005
2006static void nl80211_destroy_bss(struct i802_bss *bss)
2007{
2008 nl_cb_put(bss->nl_cb);
2009 bss->nl_cb = NULL;
Hai Shalomb755a2a2020-04-23 21:49:02 -07002010
2011 if (bss->nl_connect)
2012 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002013}
2014
2015
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002016static void
2017wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
2018{
2019 struct rfkill_config *rcfg;
2020
2021 if (drv->rfkill)
2022 return;
2023
2024 rcfg = os_zalloc(sizeof(*rcfg));
2025 if (!rcfg)
2026 return;
2027
2028 rcfg->ctx = drv;
2029
2030 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
2031 * not associated with a netdev, so use the name of some other interface
2032 * sharing the same wiphy as the P2P Device interface.
2033 *
2034 * Note: This is valid, as a P2P Device interface is always dynamically
2035 * created and is created only once another wpa_s interface was added.
2036 */
2037 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
2038 struct nl80211_global *global = drv->global;
2039 struct wpa_driver_nl80211_data *tmp1;
2040
2041 dl_list_for_each(tmp1, &global->interfaces,
2042 struct wpa_driver_nl80211_data, list) {
2043 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
2044 !tmp1->rfkill)
2045 continue;
2046
2047 wpa_printf(MSG_DEBUG,
2048 "nl80211: Use (%s) to initialize P2P Device rfkill",
2049 tmp1->first_bss->ifname);
2050 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
2051 sizeof(rcfg->ifname));
2052 break;
2053 }
2054 } else {
2055 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
2056 sizeof(rcfg->ifname));
2057 }
2058
2059 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2060 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2061 drv->rfkill = rfkill_init(rcfg);
2062 if (!drv->rfkill) {
2063 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2064 os_free(rcfg);
2065 }
2066}
2067
2068
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002069static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
2070 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002071 const u8 *set_addr,
2072 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073{
2074 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 struct i802_bss *bss;
2076
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077 if (global_priv == NULL)
2078 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 drv = os_zalloc(sizeof(*drv));
2080 if (drv == NULL)
2081 return NULL;
2082 drv->global = global_priv;
2083 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002084 drv->hostapd = !!hostapd;
2085 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002086
2087 /*
2088 * There is no driver capability flag for this, so assume it is
2089 * supported and disable this on first attempt to use if the driver
2090 * rejects the command due to missing support.
2091 */
2092 drv->set_rekey_offload = 1;
2093
Hai Shalom81f62d82019-07-22 12:10:00 -07002094 drv->num_if_indices = ARRAY_SIZE(drv->default_if_indices);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002095 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002096
2097 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
2098 if (!drv->first_bss) {
2099 os_free(drv);
2100 return NULL;
2101 }
2102 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002104 bss->ctx = ctx;
2105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2107 drv->monitor_ifidx = -1;
2108 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002109 drv->eapol_tx_sock = -1;
2110 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 if (nl80211_init_bss(bss))
2113 goto failed;
2114
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002115 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 goto failed;
2117
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
2119 if (drv->eapol_tx_sock < 0)
2120 goto failed;
2121
2122 if (drv->data_tx_status) {
2123 int enabled = 1;
2124
2125 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2126 &enabled, sizeof(enabled)) < 0) {
2127 wpa_printf(MSG_DEBUG,
2128 "nl80211: wifi status sockopt failed\n");
2129 drv->data_tx_status = 0;
2130 if (!drv->use_monitor)
2131 drv->capa.flags &=
2132 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2133 } else {
2134 eloop_register_read_sock(drv->eapol_tx_sock,
2135 wpa_driver_nl80211_handle_eapol_tx_status,
2136 drv, NULL);
2137 }
2138 }
2139
2140 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002141 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002143 drv->in_interface_list = 1;
2144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145
2146 return bss;
2147
2148failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002149 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 return NULL;
2151}
2152
2153
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002154/**
2155 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2156 * @ctx: context to be used when calling wpa_supplicant functions,
2157 * e.g., wpa_supplicant_event()
2158 * @ifname: interface name, e.g., wlan0
2159 * @global_priv: private driver global data from global_init()
2160 * Returns: Pointer to private data, %NULL on failure
2161 */
2162static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2163 void *global_priv)
2164{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002165 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
2166 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002167}
2168
2169
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002170static int nl80211_register_frame(struct i802_bss *bss,
Hai Shalomfdcde762020-04-02 11:19:20 -07002171 struct nl_sock *nl_handle,
Hai Shalome21d4e82020-04-29 16:34:06 -07002172 u16 type, const u8 *match, size_t match_len,
2173 bool multicast)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002175 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002177 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002178 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002180 buf[0] = '\0';
2181 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Hai Shalome21d4e82020-04-29 16:34:06 -07002182 wpa_printf(MSG_DEBUG,
2183 "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s multicast=%d",
2184 type, fc2str(type), nl_handle, buf, multicast);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185
Hai Shalomfdcde762020-04-02 11:19:20 -07002186 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_FRAME)) ||
Hai Shalome21d4e82020-04-29 16:34:06 -07002187 (multicast && nla_put_flag(msg, NL80211_ATTR_RECEIVE_MULTICAST)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002188 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
2189 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
2190 nlmsg_free(msg);
2191 return -1;
2192 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002194 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 if (ret) {
2196 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2197 "failed (type=%u): ret=%d (%s)",
2198 type, ret, strerror(-ret));
2199 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2200 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 return ret;
2203}
2204
2205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2207{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002208 if (bss->nl_mgmt) {
2209 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
2210 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2211 return -1;
2212 }
2213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002214 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002215 if (bss->nl_mgmt == NULL)
2216 return -1;
2217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218 return 0;
2219}
2220
2221
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002222static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2223{
2224 nl80211_register_eloop_read(&bss->nl_mgmt,
2225 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002226 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002227}
2228
2229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002230static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 const u8 *match, size_t match_len)
2232{
2233 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002234 return nl80211_register_frame(bss, bss->nl_mgmt,
Hai Shalome21d4e82020-04-29 16:34:06 -07002235 type, match, match_len, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236}
2237
2238
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002239static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002241 struct wpa_driver_nl80211_data *drv = bss->drv;
Hai Shalomfdcde762020-04-02 11:19:20 -07002242 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002243 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002244
2245 if (nl80211_alloc_mgmt_handle(bss))
2246 return -1;
2247 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2248 "handle %p", bss->nl_mgmt);
2249
Hai Shalomfdcde762020-04-02 11:19:20 -07002250 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002251 /* register for any AUTH message */
Hai Shalome21d4e82020-04-29 16:34:06 -07002252 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07002253 } else if ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2254 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
2255 /* register for SAE Authentication frames */
2256 nl80211_register_frame(bss, bss->nl_mgmt, type,
Hai Shalome21d4e82020-04-29 16:34:06 -07002257 (u8 *) "\x03\x00", 2, false);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002258 }
2259
Dmitry Shmidt051af732013-10-22 13:52:46 -07002260#ifdef CONFIG_INTERWORKING
2261 /* QoS Map Configure */
2262 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002263 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002264#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002265#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002268 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002271 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002273 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002274 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002276 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002277 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002278 /* Protected GAS Initial Request */
2279 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2280 ret = -1;
2281 /* Protected GAS Initial Response */
2282 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2283 ret = -1;
2284 /* Protected GAS Comeback Request */
2285 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2286 ret = -1;
2287 /* Protected GAS Comeback Response */
2288 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2289 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002290#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002291#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002293 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2295 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002296 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002298 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 (u8 *) "\x7f\x50\x6f\x9a\x09",
2300 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002301 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302#endif /* CONFIG_P2P */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002303#ifdef CONFIG_DPP
2304 /* DPP Public Action */
2305 if (nl80211_register_action_frame(bss,
2306 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2307 6) < 0)
2308 ret = -1;
2309#endif /* CONFIG_DPP */
Hai Shalom74f70d42019-02-11 14:42:39 -08002310#ifdef CONFIG_OCV
2311 /* SA Query Request */
2312 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x00", 2) < 0)
2313 ret = -1;
2314#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002317 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002318#ifdef CONFIG_TDLS
2319 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2320 /* TDLS Discovery Response */
2321 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2322 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002323 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002324 }
2325#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002326#ifdef CONFIG_FST
2327 /* FST Action frames */
2328 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2329 ret = -1;
2330#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331
2332 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002333 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002334 ret = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002335 else if (!drv->has_driver_key_mgmt) {
2336 int i;
2337
2338 /* Update supported AKMs only if the driver doesn't advertize
2339 * any AKM capabilities. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2341 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2342
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002343 /* Update per interface supported AKMs */
2344 for (i = 0; i < WPA_IF_MAX; i++)
2345 drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
2346 }
2347
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002348 /* WNM - BSS Transition Management Request */
2349 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002350 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002351 /* WNM-Sleep Mode Response */
2352 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002353 ret = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002354#ifdef CONFIG_WNM
2355 /* WNM - Collocated Interference Request */
2356 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2357 ret = -1;
2358#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002359
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002360#ifdef CONFIG_HS20
2361 /* WNM-Notification */
2362 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002363 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002364#endif /* CONFIG_HS20 */
2365
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002366 /* WMM-AC ADDTS Response */
2367 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2368 ret = -1;
2369
2370 /* WMM-AC DELTS */
2371 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2372 ret = -1;
2373
2374 /* Radio Measurement - Neighbor Report Response */
2375 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2376 ret = -1;
2377
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002378 /* Radio Measurement - Radio Measurement Request */
2379 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2380 ret = -1;
2381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002382 /* Radio Measurement - Link Measurement Request */
2383 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2384 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2385 ret = -1;
2386
2387 nl80211_mgmt_handle_register_eloop(bss);
2388
2389 return ret;
2390}
2391
2392
2393static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2394{
2395 int ret = 0;
2396
2397 if (nl80211_alloc_mgmt_handle(bss))
2398 return -1;
2399
2400 wpa_printf(MSG_DEBUG,
2401 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2402 bss->nl_mgmt);
2403
2404 /* Auth frames for mesh SAE */
2405 if (nl80211_register_frame(bss, bss->nl_mgmt,
2406 (WLAN_FC_TYPE_MGMT << 2) |
2407 (WLAN_FC_STYPE_AUTH << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002408 NULL, 0, false) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002409 ret = -1;
2410
2411 /* Mesh peering open */
2412 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2413 ret = -1;
2414 /* Mesh peering confirm */
2415 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2416 ret = -1;
2417 /* Mesh peering close */
2418 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2419 ret = -1;
2420
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002421 nl80211_mgmt_handle_register_eloop(bss);
2422
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002423 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424}
2425
2426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427static int nl80211_register_spurious_class3(struct i802_bss *bss)
2428{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002429 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002430 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002431
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002432 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2433 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434 if (ret) {
2435 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2436 "failed: ret=%d (%s)",
2437 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 return ret;
2440}
2441
2442
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002443static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2444{
2445 int ret = 0;
2446
2447 /* Public Action frames */
2448 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2449 ret = -1;
2450 /* RRM Measurement Report */
2451 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2452 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002453 /* RRM Link Measurement Report */
2454 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2455 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002456 /* RRM Neighbor Report Request */
2457 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2458 ret = -1;
2459 /* FT Action frames */
2460 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2461 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002462 /* SA Query */
2463 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2464 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002465 /* Protected Dual of Public Action */
2466 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2467 ret = -1;
2468 /* WNM */
2469 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2470 ret = -1;
2471 /* WMM */
2472 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2473 ret = -1;
2474#ifdef CONFIG_FST
2475 /* FST Action frames */
2476 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2477 ret = -1;
2478#endif /* CONFIG_FST */
2479 /* Vendor-specific */
2480 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2481 ret = -1;
2482
2483 return ret;
2484}
2485
2486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002487static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2488{
2489 static const int stypes[] = {
2490 WLAN_FC_STYPE_AUTH,
2491 WLAN_FC_STYPE_ASSOC_REQ,
2492 WLAN_FC_STYPE_REASSOC_REQ,
2493 WLAN_FC_STYPE_DISASSOC,
2494 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002495 WLAN_FC_STYPE_PROBE_REQ,
2496/* Beacon doesn't work as mac80211 doesn't currently allow
2497 * it, but it wouldn't really be the right thing anyway as
2498 * it isn't per interface ... maybe just dump the scan
2499 * results periodically for OLBC?
2500 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002501 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002502 };
2503 unsigned int i;
2504
2505 if (nl80211_alloc_mgmt_handle(bss))
2506 return -1;
2507 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2508 "handle %p", bss->nl_mgmt);
2509
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002510 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002511 if (nl80211_register_frame(bss, bss->nl_mgmt,
2512 (WLAN_FC_TYPE_MGMT << 2) |
2513 (stypes[i] << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07002514 NULL, 0, false) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002515 goto out_err;
2516 }
2517 }
2518
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002519 if (nl80211_action_subscribe_ap(bss))
2520 goto out_err;
2521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002522 if (nl80211_register_spurious_class3(bss))
2523 goto out_err;
2524
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002525 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002526 return 0;
2527
2528out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002529 nl_destroy_handles(&bss->nl_mgmt);
2530 return -1;
2531}
2532
2533
2534static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2535{
2536 if (nl80211_alloc_mgmt_handle(bss))
2537 return -1;
2538 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2539 "handle %p (device SME)", bss->nl_mgmt);
2540
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002541 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 goto out_err;
2543
Hai Shalom5f92bc92019-04-18 11:54:11 -07002544 if (bss->drv->device_ap_sme) {
2545 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2546
2547 /* Register for all Authentication frames */
Hai Shalome21d4e82020-04-29 16:34:06 -07002548 if (nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0,
2549 false) < 0)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002550 wpa_printf(MSG_DEBUG,
2551 "nl80211: Failed to subscribe to handle Authentication frames - SAE offload may not work");
2552 }
2553
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002554 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 return 0;
2556
2557out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002558 nl_destroy_handles(&bss->nl_mgmt);
2559 return -1;
2560}
2561
2562
2563static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2564{
2565 if (bss->nl_mgmt == NULL)
2566 return;
2567 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2568 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002569 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002570
2571 nl80211_put_wiphy_data_ap(bss);
2572}
2573
2574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2576{
2577 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2578}
2579
2580
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002581static void nl80211_del_p2pdev(struct i802_bss *bss)
2582{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002583 struct nl_msg *msg;
2584 int ret;
2585
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002586 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2587 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002588
2589 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2590 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002591 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002592}
2593
2594
2595static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2596{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002597 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002598 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002599
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002600 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2601 NL80211_CMD_STOP_P2P_DEVICE);
2602 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002603
2604 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2605 start ? "Start" : "Stop",
2606 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002607 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002608 return ret;
2609}
2610
2611
2612static int i802_set_iface_flags(struct i802_bss *bss, int up)
2613{
2614 enum nl80211_iftype nlmode;
2615
2616 nlmode = nl80211_get_ifmode(bss);
2617 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2618 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2619 bss->ifname, up);
2620 }
2621
2622 /* P2P Device has start/stop which is equivalent */
2623 return nl80211_set_p2pdev(bss, up);
2624}
2625
2626
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002627#ifdef CONFIG_TESTING_OPTIONS
2628static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2629{
2630 /* struct wpa_driver_nl80211_data *drv = arg; */
2631 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2632 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2633
2634
2635 wpa_printf(MSG_DEBUG,
2636 "nl80211: QCA vendor test command response received");
2637
2638 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2639 genlmsg_attrlen(gnlh, 0), NULL);
2640 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2641 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2642 return NL_SKIP;
2643 }
2644
2645 wpa_hexdump(MSG_DEBUG,
2646 "nl80211: Received QCA vendor test command response",
2647 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2648 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2649
2650 return NL_SKIP;
2651}
2652#endif /* CONFIG_TESTING_OPTIONS */
2653
2654
2655static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2656{
2657#ifdef CONFIG_TESTING_OPTIONS
2658 struct nl_msg *msg;
2659 struct nlattr *params;
2660 int ret;
2661
2662 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2663 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2664 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2665 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2666 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2667 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2668 nlmsg_free(msg);
2669 return;
2670 }
2671 nla_nest_end(msg, params);
2672
2673 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2674 wpa_printf(MSG_DEBUG,
2675 "nl80211: QCA vendor test command returned %d (%s)",
2676 ret, strerror(-ret));
2677#endif /* CONFIG_TESTING_OPTIONS */
2678}
2679
2680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002682wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002683 const u8 *set_addr, int first,
2684 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002686 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002688 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689
2690 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002691 bss->ifindex = drv->ifindex;
2692 bss->wdev_id = drv->global->if_add_wdevid;
2693 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2694
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002695 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2696 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002697 drv->global->if_add_wdevid_set = 0;
2698
Dmitry Shmidt03658832014-08-13 11:03:49 -07002699 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2700 bss->static_ap = 1;
2701
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002702 if (first &&
2703 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2704 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2705 drv->start_iface_up = 1;
2706
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002707 if (wpa_driver_nl80211_capa(drv))
2708 return -1;
2709
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002710 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2711 return -1;
2712
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002713 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2714 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002716 if (set_addr &&
2717 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2718 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2719 set_addr)))
2720 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002721
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002722 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2723 drv->start_mode_ap = 1;
2724
Dmitry Shmidt03658832014-08-13 11:03:49 -07002725 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002726 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002727 else if (bss->if_dynamic ||
2728 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002729 nlmode = nl80211_get_ifmode(bss);
2730 else
2731 nlmode = NL80211_IFTYPE_STATION;
2732
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002733 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002734 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002737
Dmitry Shmidt98660862014-03-11 17:26:21 -07002738 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002739 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002741 wpa_driver_nl80211_drv_init_rfkill(drv);
2742
Dmitry Shmidt98660862014-03-11 17:26:21 -07002743 if (!rfkill_is_blocked(drv->rfkill)) {
2744 int ret = i802_set_iface_flags(bss, 1);
2745 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2747 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002748 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002750
2751 if (is_p2p_net_interface(nlmode))
2752 nl80211_disable_11b_rates(bss->drv,
2753 bss->drv->ifindex, 1);
2754
Dmitry Shmidt98660862014-03-11 17:26:21 -07002755 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2756 return ret;
2757 } else {
2758 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2759 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002760 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2761 drv->if_disabled = 1;
2762
Dmitry Shmidt98660862014-03-11 17:26:21 -07002763 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 }
2765
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002766 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002767 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2768 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002770 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2771 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2772 bss->addr))
2773 return -1;
2774 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2775 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 if (send_rfkill_event) {
2778 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2779 drv, drv->ctx);
2780 }
2781
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002782 if (drv->vendor_cmd_test_avail)
2783 qca_vendor_test(drv);
2784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 return 0;
2786}
2787
2788
Paul Stewart092955c2017-02-06 09:13:09 -08002789static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790{
2791 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08002792 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002794 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2795 drv->ifindex);
Paul Stewart092955c2017-02-06 09:13:09 -08002796 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002797 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799}
2800
2801
2802/**
2803 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002804 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 *
2806 * Shut down driver interface and processing of driver events. Free
2807 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2808 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002809static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002812 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002814 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2815 bss->ifname, drv->disabled_11b_rates);
2816
Dmitry Shmidt04949592012-07-19 12:16:46 -07002817 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818 if (drv->data_tx_status)
2819 eloop_unregister_read_sock(drv->eapol_tx_sock);
2820 if (drv->eapol_tx_sock >= 0)
2821 close(drv->eapol_tx_sock);
2822
2823 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 wpa_driver_nl80211_probe_req_report(bss, 0);
2825 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2827 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2829 "interface %s from bridge %s: %s",
2830 bss->ifname, bss->brname, strerror(errno));
2831 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07002832
2833 if (drv->rtnl_sk)
Hai Shalomfdcde762020-04-02 11:19:20 -07002834 nl_socket_free(drv->rtnl_sk);
Hai Shalomc9e41a12018-07-31 14:41:42 -07002835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002837 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2838 0) < 0)
2839 wpa_printf(MSG_INFO,
2840 "nl80211: Could not set bridge %s down",
2841 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2844 "bridge %s: %s",
2845 bss->brname, strerror(errno));
2846 }
2847
2848 nl80211_remove_monitor_interface(drv);
2849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002850 if (is_ap_interface(drv->nlmode))
Paul Stewart092955c2017-02-06 09:13:09 -08002851 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 if (drv->eapol_sock >= 0) {
2854 eloop_unregister_read_sock(drv->eapol_sock);
2855 close(drv->eapol_sock);
2856 }
2857
2858 if (drv->if_indices != drv->default_if_indices)
2859 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002861 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002864 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2865 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002866 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 rfkill_deinit(drv->rfkill);
2868
2869 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2870
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002871 if (!drv->start_iface_up)
2872 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002873
2874 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002875 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2876 0) < 0) {
2877 wpa_printf(MSG_DEBUG,
2878 "nl80211: Could not set interface down to restore permanent MAC address");
2879 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002880 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2881 drv->perm_addr) < 0) {
2882 wpa_printf(MSG_DEBUG,
2883 "nl80211: Could not restore permanent MAC address");
2884 }
2885 }
2886
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002887 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002888 if (!drv->hostapd || !drv->start_mode_ap)
2889 wpa_driver_nl80211_set_mode(bss,
2890 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002891 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002892 } else {
2893 nl80211_mgmt_unsubscribe(bss, "deinit");
2894 nl80211_del_p2pdev(bss);
2895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002897 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 os_free(drv->filter_ssids);
2900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002901 os_free(drv->auth_ie);
2902
2903 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 dl_list_del(&drv->list);
2905
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002906 os_free(drv->extended_capa);
2907 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002908 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2909 os_free(drv->iface_ext_capa[i].ext_capa);
2910 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2911 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002912 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 os_free(drv);
2914}
2915
2916
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002917static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2918{
2919 switch (alg) {
2920 case WPA_ALG_WEP:
2921 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08002922 return RSN_CIPHER_SUITE_WEP40;
2923 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002924 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002925 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002926 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002927 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002928 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002929 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002930 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002931 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002932 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002933 return RSN_CIPHER_SUITE_GCMP_256;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002934 case WPA_ALG_BIP_CMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002935 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002936 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002937 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002938 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002939 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002940 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002941 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002942 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08002943 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002944 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08002945 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002946 case WPA_ALG_NONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002947 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2948 alg);
2949 return 0;
2950 }
2951
2952 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2953 alg);
2954 return 0;
2955}
2956
2957
2958static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2959{
2960 switch (cipher) {
2961 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002962 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002963 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002964 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002965 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002966 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002967 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002968 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002969 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002970 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002971 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08002972 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002973 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08002974 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002975 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08002976 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002977 }
2978
2979 return 0;
2980}
2981
2982
2983static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2984 int max_suites)
2985{
2986 int num_suites = 0;
2987
2988 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002989 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002990 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002991 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002992 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002993 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002994 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002995 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002996 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08002997 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002998 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08002999 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003000 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08003001 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003002
3003 return num_suites;
3004}
3005
3006
Hai Shalomfdcde762020-04-02 11:19:20 -07003007static int wpa_key_mgmt_to_suites(unsigned int key_mgmt_suites, u32 suites[],
3008 int max_suites)
3009{
3010 int num_suites = 0;
3011
3012#define __AKM(a, b) \
3013 if (num_suites < max_suites && \
3014 (key_mgmt_suites & (WPA_KEY_MGMT_ ## a))) \
3015 suites[num_suites++] = (RSN_AUTH_KEY_MGMT_ ## b)
3016 __AKM(IEEE8021X, UNSPEC_802_1X);
3017 __AKM(PSK, PSK_OVER_802_1X);
3018 __AKM(FT_IEEE8021X, FT_802_1X);
3019 __AKM(FT_PSK, FT_PSK);
3020 __AKM(IEEE8021X_SHA256, 802_1X_SHA256);
3021 __AKM(PSK_SHA256, PSK_SHA256);
3022 __AKM(SAE, SAE);
3023 __AKM(FT_SAE, FT_SAE);
3024 __AKM(CCKM, CCKM);
3025 __AKM(OSEN, OSEN);
3026 __AKM(IEEE8021X_SUITE_B, 802_1X_SUITE_B);
3027 __AKM(IEEE8021X_SUITE_B_192, 802_1X_SUITE_B_192);
3028 __AKM(FILS_SHA256, FILS_SHA256);
3029 __AKM(FILS_SHA384, FILS_SHA384);
3030 __AKM(FT_FILS_SHA256, FT_FILS_SHA256);
3031 __AKM(FT_FILS_SHA384, FT_FILS_SHA384);
3032 __AKM(OWE, OWE);
3033 __AKM(DPP, DPP);
3034 __AKM(FT_IEEE8021X_SHA384, FT_802_1X_SHA384);
3035#undef __AKM
3036
3037 return num_suites;
3038}
3039
3040
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003041#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
3043 const u8 *key, size_t key_len)
3044{
3045 struct nl_msg *msg;
3046 int ret;
3047
3048 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
3049 return 0;
3050
3051 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
3052 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
3053 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
3054 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
3055 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
3056 nl80211_nlmsg_clear(msg);
3057 nlmsg_free(msg);
3058 return -1;
3059 }
3060 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
3061 if (ret) {
3062 wpa_printf(MSG_DEBUG,
3063 "nl80211: Key management set key failed: ret=%d (%s)",
3064 ret, strerror(-ret));
3065 }
3066
3067 return ret;
3068}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003069#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003070
3071
Roshan Pius3a1667e2018-07-03 15:17:14 -07003072static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
3073 const u8 *key, size_t key_len,
3074 const u8 *addr)
3075{
3076 struct nl_msg *msg = NULL;
3077 int ret;
3078
3079 /*
3080 * If the authenticator address is not set, assume it is
3081 * the current BSSID.
3082 */
3083 if (!addr && drv->associated)
3084 addr = drv->bssid;
3085 else if (!addr)
3086 return -1;
3087
3088 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
3089 MAC2STR(addr));
3090 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
3091 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
3092 if (!msg ||
3093 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3094 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
3095 nl80211_nlmsg_clear(msg);
3096 nlmsg_free(msg);
3097 return -ENOBUFS;
3098 }
3099
3100 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
3101 if (ret) {
3102 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
3103 ret, strerror(-ret));
3104 }
3105
3106 return ret;
3107}
3108
3109
Hai Shalomfdcde762020-04-02 11:19:20 -07003110static int wpa_driver_nl80211_set_key(struct i802_bss *bss,
3111 struct wpa_driver_set_key_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003114 int ifindex;
Hai Shalomc3565922019-10-28 11:58:20 -07003115 struct nl_msg *msg;
3116 struct nl_msg *key_msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003118 int skip_set_key = 1;
3119 const char *ifname = params->ifname;
3120 enum wpa_alg alg = params->alg;
3121 const u8 *addr = params->addr;
3122 int key_idx = params->key_idx;
3123 int set_tx = params->set_tx;
3124 const u8 *seq = params->seq;
3125 size_t seq_len = params->seq_len;
3126 const u8 *key = params->key;
3127 size_t key_len = params->key_len;
3128 int vlan_id = params->vlan_id;
3129 enum key_flag key_flag = params->key_flag;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003131 /* Ignore for P2P Device */
3132 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
3133 return 0;
3134
3135 ifindex = if_nametoindex(ifname);
3136 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Hai Shalomfdcde762020-04-02 11:19:20 -07003137 "set_tx=%d seq_len=%lu key_len=%lu key_flag=0x%x",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003138 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Hai Shalomfdcde762020-04-02 11:19:20 -07003139 (unsigned long) seq_len, (unsigned long) key_len, key_flag);
3140
3141 if (check_key_flag(key_flag)) {
3142 wpa_printf(MSG_DEBUG, "%s: invalid key_flag", __func__);
3143 return -EINVAL;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003146#ifdef CONFIG_DRIVER_NL80211_QCA
Hai Shalomfdcde762020-04-02 11:19:20 -07003147 if ((key_flag & KEY_FLAG_PMK) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003148 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
3149 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
3150 __func__);
3151 ret = issue_key_mgmt_set_key(drv, key, key_len);
3152 return ret;
3153 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003154#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155
Hai Shalomfdcde762020-04-02 11:19:20 -07003156 if (key_flag & KEY_FLAG_PMK) {
3157 if (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)
3158 return nl80211_set_pmk(drv, key, key_len, addr);
3159 /* The driver does not have any offload mechanism for PMK, so
3160 * there is no need to configure this key. */
3161 return 0;
3162 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003163
Hai Shalomfdcde762020-04-02 11:19:20 -07003164 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003165 key_msg = nlmsg_alloc();
3166 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003167 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003168
Hai Shalomfdcde762020-04-02 11:19:20 -07003169 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3170 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3171 wpa_printf(MSG_DEBUG,
3172 "nl80211: SET_KEY (pairwise RX/TX modify)");
3173 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3174 if (!msg)
3175 goto fail2;
3176 } else if (alg == WPA_ALG_NONE && (key_flag & KEY_FLAG_RX_TX)) {
3177 wpa_printf(MSG_DEBUG, "%s: invalid key_flag to delete key",
3178 __func__);
3179 ret = -EINVAL;
3180 goto fail2;
3181 } else if (alg == WPA_ALG_NONE) {
3182 wpa_printf(MSG_DEBUG, "nl80211: DEL_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003183 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
3184 if (!msg)
Hai Shalomc3565922019-10-28 11:58:20 -07003185 goto fail2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003187 u32 suite;
3188
3189 suite = wpa_alg_to_cipher_suite(alg, key_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07003190 if (!suite) {
3191 ret = -EINVAL;
Hai Shalomc3565922019-10-28 11:58:20 -07003192 goto fail2;
Hai Shalomfdcde762020-04-02 11:19:20 -07003193 }
3194 wpa_printf(MSG_DEBUG, "nl80211: NEW_KEY");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003195 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003196 if (!msg)
3197 goto fail2;
3198 if (nla_put(key_msg, NL80211_KEY_DATA, key_len, key) ||
3199 nla_put_u32(key_msg, NL80211_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003200 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003201 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003202
Hai Shalomfdcde762020-04-02 11:19:20 -07003203 if (seq && seq_len) {
3204 if (nla_put(key_msg, NL80211_KEY_SEQ, seq_len, seq))
3205 goto fail;
3206 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ",
3207 seq, seq_len);
3208 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07003209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210
3211 if (addr && !is_broadcast_ether_addr(addr)) {
3212 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003213 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
3214 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215
Hai Shalomfdcde762020-04-02 11:19:20 -07003216 if ((key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3217 KEY_FLAG_PAIRWISE_RX ||
3218 (key_flag & KEY_FLAG_PAIRWISE_MASK) ==
3219 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) {
3220 if (nla_put_u8(key_msg, NL80211_KEY_MODE,
3221 key_flag == KEY_FLAG_PAIRWISE_RX ?
3222 NL80211_KEY_NO_TX : NL80211_KEY_SET_TX))
3223 goto fail;
3224 } else if ((key_flag & KEY_FLAG_GROUP_MASK) ==
3225 KEY_FLAG_GROUP_RX) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Hai Shalomc3565922019-10-28 11:58:20 -07003227 if (nla_put_u32(key_msg, NL80211_KEY_TYPE,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003228 NL80211_KEYTYPE_GROUP))
3229 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003230 } else if (!(key_flag & KEY_FLAG_PAIRWISE)) {
3231 wpa_printf(MSG_DEBUG,
3232 " key_flag missing PAIRWISE when setting a pairwise key");
3233 ret = -EINVAL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003234 goto fail;
Hai Shalomfdcde762020-04-02 11:19:20 -07003235 } else if (alg == WPA_ALG_WEP &&
3236 (key_flag & KEY_FLAG_RX_TX) == KEY_FLAG_RX_TX) {
3237 wpa_printf(MSG_DEBUG, " unicast WEP key");
3238 skip_set_key = 0;
3239 } else {
3240 wpa_printf(MSG_DEBUG, " pairwise key");
3241 }
3242 } else if ((key_flag & KEY_FLAG_PAIRWISE) ||
3243 !(key_flag & KEY_FLAG_GROUP)) {
3244 wpa_printf(MSG_DEBUG,
3245 " invalid key_flag for a broadcast key");
3246 ret = -EINVAL;
3247 goto fail;
3248 } else {
3249 wpa_printf(MSG_DEBUG, " broadcast key");
3250 if (key_flag & KEY_FLAG_DEFAULT)
3251 skip_set_key = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252 }
Hai Shalomc3565922019-10-28 11:58:20 -07003253 if (nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
3254 nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003255 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003256 nl80211_nlmsg_clear(key_msg);
3257 nlmsg_free(key_msg);
3258 key_msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003259
Hai Shalomfdcde762020-04-02 11:19:20 -07003260 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3261 wpa_printf(MSG_DEBUG, "nl80211: VLAN ID %d", vlan_id);
3262 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3263 goto fail;
3264 }
3265
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003266 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3268 ret = 0;
3269 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003270 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 ret, strerror(-ret));
3272
3273 /*
Hai Shalomfdcde762020-04-02 11:19:20 -07003274 * If we failed or don't need to set the key as default (below),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 * we're done here.
3276 */
Hai Shalomfdcde762020-04-02 11:19:20 -07003277 if (ret || skip_set_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 return ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07003279 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_SET_KEY - default key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280
Hai Shalomfdcde762020-04-02 11:19:20 -07003281 ret = -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07003282 key_msg = nlmsg_alloc();
3283 if (!key_msg)
Hai Shalomfdcde762020-04-02 11:19:20 -07003284 return ret;
Hai Shalomc3565922019-10-28 11:58:20 -07003285
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003286 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
Hai Shalomc3565922019-10-28 11:58:20 -07003287 if (!msg)
3288 goto fail2;
3289 if (!key_msg ||
3290 nla_put_u8(key_msg, NL80211_KEY_IDX, key_idx) ||
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003291 nla_put_flag(key_msg, wpa_alg_bip(alg) ?
Hai Shalomfdcde762020-04-02 11:19:20 -07003292 (key_idx == 6 || key_idx == 7 ?
3293 NL80211_KEY_DEFAULT_BEACON :
3294 NL80211_KEY_DEFAULT_MGMT) :
3295 NL80211_KEY_DEFAULT))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003296 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003298 struct nlattr *types;
3299
Hai Shalomc3565922019-10-28 11:58:20 -07003300 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003301 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003302 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003303 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003304 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003306 struct nlattr *types;
3307
Hai Shalomc3565922019-10-28 11:58:20 -07003308 types = nla_nest_start(key_msg, NL80211_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003309 if (!types ||
Hai Shalomc3565922019-10-28 11:58:20 -07003310 nla_put_flag(key_msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003311 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07003312 nla_nest_end(key_msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 }
3314
Hai Shalomc3565922019-10-28 11:58:20 -07003315 if (nla_put_nested(msg, NL80211_ATTR_KEY, key_msg))
3316 goto fail;
3317 nl80211_nlmsg_clear(key_msg);
3318 nlmsg_free(key_msg);
3319 key_msg = NULL;
3320
Hai Shalomfdcde762020-04-02 11:19:20 -07003321 if (vlan_id && (drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
3322 wpa_printf(MSG_DEBUG, "nl80211: set_key default - VLAN ID %d",
3323 vlan_id);
3324 if (nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id))
3325 goto fail;
3326 }
3327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 if (ret)
Hai Shalomfdcde762020-04-02 11:19:20 -07003330 wpa_printf(MSG_DEBUG,
3331 "nl80211: set_key default failed; err=%d %s",
3332 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333 return ret;
3334
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003335fail:
3336 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003337 nlmsg_free(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07003338fail2:
3339 nl80211_nlmsg_clear(key_msg);
3340 nlmsg_free(key_msg);
Hai Shalomfdcde762020-04-02 11:19:20 -07003341 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342}
3343
3344
3345static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3346 int key_idx, int defkey,
3347 const u8 *seq, size_t seq_len,
3348 const u8 *key, size_t key_len)
3349{
3350 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003351 u32 suite;
3352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353 if (!key_attr)
3354 return -1;
3355
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003356 suite = wpa_alg_to_cipher_suite(alg, key_len);
3357 if (!suite)
3358 return -1;
3359
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003360 if (defkey && wpa_alg_bip(alg)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003361 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3362 return -1;
3363 } else if (defkey) {
3364 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3365 return -1;
3366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003368 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003369 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003370 (seq && seq_len &&
3371 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3372 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3373 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374
3375 nla_nest_end(msg, key_attr);
3376
3377 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378}
3379
3380
3381static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3382 struct nl_msg *msg)
3383{
3384 int i, privacy = 0;
3385 struct nlattr *nl_keys, *nl_key;
3386
3387 for (i = 0; i < 4; i++) {
3388 if (!params->wep_key[i])
3389 continue;
3390 privacy = 1;
3391 break;
3392 }
3393 if (params->wps == WPS_MODE_PRIVACY)
3394 privacy = 1;
3395 if (params->pairwise_suite &&
3396 params->pairwise_suite != WPA_CIPHER_NONE)
3397 privacy = 1;
3398
3399 if (!privacy)
3400 return 0;
3401
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003402 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3403 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404
3405 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3406 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003407 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408
3409 for (i = 0; i < 4; i++) {
3410 if (!params->wep_key[i])
3411 continue;
3412
3413 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003414 if (!nl_key ||
3415 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3416 params->wep_key[i]) ||
3417 nla_put_u32(msg, NL80211_KEY_CIPHER,
3418 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003419 RSN_CIPHER_SUITE_WEP40 :
3420 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003421 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3422 (i == params->wep_tx_keyidx &&
3423 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3424 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425
3426 nla_nest_end(msg, nl_key);
3427 }
3428 nla_nest_end(msg, nl_keys);
3429
3430 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431}
3432
3433
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003434int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3435 const u8 *addr, int cmd, u16 reason_code,
Hai Shalom74f70d42019-02-11 14:42:39 -08003436 int local_state_change,
Hai Shalomfdcde762020-04-02 11:19:20 -07003437 struct nl_sock *nl_connect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003439 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 struct nl_msg *msg;
3441
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003442 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3443 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3444 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3445 (local_state_change &&
3446 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3447 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450
Hai Shalom74f70d42019-02-11 14:42:39 -08003451 if (nl_connect)
3452 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
3453 else
3454 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003456 wpa_dbg(drv->ctx, MSG_DEBUG,
3457 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3458 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 return ret;
3461}
3462
3463
3464static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Hai Shalom81f62d82019-07-22 12:10:00 -07003465 u16 reason_code,
Hai Shalomfdcde762020-04-02 11:19:20 -07003466 struct nl_sock *nl_connect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003468 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003469 int drv_associated = drv->associated;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003470
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003471 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003472 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003473 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003474 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Hai Shalom74f70d42019-02-11 14:42:39 -08003475 reason_code, 0, nl_connect);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003476 /*
3477 * For locally generated disconnect, supplicant already generates a
3478 * DEAUTH event, so ignore the event from NL80211.
3479 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003480 drv->ignore_next_local_disconnect = drv_associated && (ret == 0);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003481
3482 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483}
3484
3485
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003486static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
Hai Shalom81f62d82019-07-22 12:10:00 -07003487 const u8 *addr, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003490 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003491 int drv_associated = drv->associated;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003492
3493 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3494 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003495 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003496 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003497 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003498 return wpa_driver_nl80211_disconnect(drv, reason_code,
Hai Shalomb755a2a2020-04-23 21:49:02 -07003499 get_connect_handle(bss));
Hai Shalom74f70d42019-02-11 14:42:39 -08003500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3502 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003503 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003504 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
Hai Shalomb755a2a2020-04-23 21:49:02 -07003505 reason_code, 0, get_connect_handle(bss));
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003506 /*
3507 * For locally generated deauthenticate, supplicant already generates a
3508 * DEAUTH event, so ignore the event from NL80211.
3509 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003510 drv->ignore_next_local_deauth = drv_associated && (ret == 0);
3511
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003512 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003513}
3514
3515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003516static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3517 struct wpa_driver_auth_params *params)
3518{
3519 int i;
3520
3521 drv->auth_freq = params->freq;
3522 drv->auth_alg = params->auth_alg;
3523 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3524 drv->auth_local_state_change = params->local_state_change;
3525 drv->auth_p2p = params->p2p;
3526
3527 if (params->bssid)
3528 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3529 else
3530 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3531
3532 if (params->ssid) {
3533 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3534 drv->auth_ssid_len = params->ssid_len;
3535 } else
3536 drv->auth_ssid_len = 0;
3537
3538
3539 os_free(drv->auth_ie);
3540 drv->auth_ie = NULL;
3541 drv->auth_ie_len = 0;
3542 if (params->ie) {
3543 drv->auth_ie = os_malloc(params->ie_len);
3544 if (drv->auth_ie) {
3545 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3546 drv->auth_ie_len = params->ie_len;
3547 }
3548 }
3549
3550 for (i = 0; i < 4; i++) {
3551 if (params->wep_key[i] && params->wep_key_len[i] &&
3552 params->wep_key_len[i] <= 16) {
3553 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3554 params->wep_key_len[i]);
3555 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3556 } else
3557 drv->auth_wep_key_len[i] = 0;
3558 }
3559}
3560
3561
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003562static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3563{
3564 struct wpa_driver_nl80211_data *drv = bss->drv;
3565
3566 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3567 return;
3568
3569 /*
3570 * Looks like we failed to unmask 11b rates previously. This could
3571 * happen, e.g., if the interface was down at the point in time when a
3572 * P2P group was terminated.
3573 */
3574 wpa_printf(MSG_DEBUG,
3575 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3576 bss->ifname);
3577 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3578}
3579
3580
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003581static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3582{
3583 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3584 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3585 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3586 return NL80211_AUTHTYPE_SHARED_KEY;
3587 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3588 return NL80211_AUTHTYPE_NETWORK_EAP;
3589 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3590 return NL80211_AUTHTYPE_FT;
3591 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3592 return NL80211_AUTHTYPE_SAE;
3593 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3594 return NL80211_AUTHTYPE_FILS_SK;
3595 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3596 return NL80211_AUTHTYPE_FILS_SK_PFS;
3597
3598 return NL80211_AUTHTYPE_MAX;
3599}
3600
3601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003603 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 struct wpa_driver_nl80211_data *drv = bss->drv;
3606 int ret = -1, i;
3607 struct nl_msg *msg;
3608 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003609 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003611 int is_retry;
Hai Shalomfdcde762020-04-02 11:19:20 -07003612 struct wpa_driver_set_key_params p;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003613
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003614 nl80211_unmask_11b_rates(bss);
3615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003616 is_retry = drv->retry_auth;
3617 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003618 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003620 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003622 if (params->bssid)
3623 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3624 else
3625 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003627 nlmode = params->p2p ?
3628 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3629 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003630 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631 return -1;
3632
3633retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3635 drv->ifindex);
3636
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003637 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3638 if (!msg)
3639 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640
Hai Shalomfdcde762020-04-02 11:19:20 -07003641 os_memset(&p, 0, sizeof(p));
3642 p.ifname = bss->ifname;
3643 p.alg = WPA_ALG_WEP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644 for (i = 0; i < 4; i++) {
3645 if (!params->wep_key[i])
3646 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -07003647 p.key_idx = i;
3648 p.set_tx = i == params->wep_tx_keyidx;
3649 p.key = params->wep_key[i];
3650 p.key_len = params->wep_key_len[i];
3651 p.key_flag = i == params->wep_tx_keyidx ?
3652 KEY_FLAG_GROUP_RX_TX_DEFAULT :
3653 KEY_FLAG_GROUP_RX_TX;
3654 wpa_driver_nl80211_set_key(bss, &p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 if (params->wep_tx_keyidx != i)
3656 continue;
3657 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003658 params->wep_key[i], params->wep_key_len[i]))
3659 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660 }
3661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 if (params->bssid) {
3663 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3664 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003665 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3666 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 }
3668 if (params->freq) {
3669 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003670 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3671 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 }
3673 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003674 wpa_printf(MSG_DEBUG, " * SSID=%s",
3675 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003676 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3677 params->ssid))
3678 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 }
3680 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003681 if (params->ie &&
3682 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3683 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003684 if (params->auth_data) {
3685 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3686 params->auth_data_len);
3687 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3688 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003689 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003690 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003691 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003693 if (type == NL80211_AUTHTYPE_MAX ||
3694 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003695 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 if (params->local_state_change) {
3697 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003698 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3699 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 }
3701
3702 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3703 msg = NULL;
3704 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003705 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003706 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
3707 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003709 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
3710 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 /*
3712 * mac80211 does not currently accept new
3713 * authentication if we are already authenticated. As a
3714 * workaround, force deauthentication and try again.
3715 */
3716 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3717 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003718 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 wpa_driver_nl80211_deauthenticate(
3720 bss, params->bssid,
3721 WLAN_REASON_PREV_AUTH_NOT_VALID);
3722 nlmsg_free(msg);
3723 goto retry;
3724 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003725
3726 if (ret == -ENOENT && params->freq && !is_retry) {
3727 /*
3728 * cfg80211 has likely expired the BSS entry even
3729 * though it was previously available in our internal
3730 * BSS table. To recover quickly, start a single
3731 * channel scan on the specified channel.
3732 */
3733 struct wpa_driver_scan_params scan;
3734 int freqs[2];
3735
3736 os_memset(&scan, 0, sizeof(scan));
3737 scan.num_ssids = 1;
3738 if (params->ssid) {
3739 scan.ssids[0].ssid = params->ssid;
3740 scan.ssids[0].ssid_len = params->ssid_len;
3741 }
3742 freqs[0] = params->freq;
3743 freqs[1] = 0;
3744 scan.freqs = freqs;
3745 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3746 "channel scan to refresh cfg80211 BSS "
3747 "entry");
3748 ret = wpa_driver_nl80211_scan(bss, &scan);
3749 if (ret == 0) {
3750 nl80211_copy_auth_params(drv, params);
3751 drv->scan_for_auth = 1;
3752 }
3753 } else if (is_retry) {
3754 /*
3755 * Need to indicate this with an event since the return
3756 * value from the retry is not delivered to core code.
3757 */
3758 union wpa_event_data event;
3759 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3760 "failed");
3761 os_memset(&event, 0, sizeof(event));
3762 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3763 ETH_ALEN);
3764 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3765 &event);
3766 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003767 } else {
3768 wpa_printf(MSG_DEBUG,
3769 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003772fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 nlmsg_free(msg);
3774 return ret;
3775}
3776
3777
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003778int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003779{
3780 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003781 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003782 int i;
3783
3784 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3785
3786 os_memset(&params, 0, sizeof(params));
3787 params.freq = drv->auth_freq;
3788 params.auth_alg = drv->auth_alg;
3789 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3790 params.local_state_change = drv->auth_local_state_change;
3791 params.p2p = drv->auth_p2p;
3792
3793 if (!is_zero_ether_addr(drv->auth_bssid_))
3794 params.bssid = drv->auth_bssid_;
3795
3796 if (drv->auth_ssid_len) {
3797 params.ssid = drv->auth_ssid;
3798 params.ssid_len = drv->auth_ssid_len;
3799 }
3800
3801 params.ie = drv->auth_ie;
3802 params.ie_len = drv->auth_ie_len;
3803
3804 for (i = 0; i < 4; i++) {
3805 if (drv->auth_wep_key_len[i]) {
3806 params.wep_key[i] = drv->auth_wep_key[i];
3807 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3808 }
3809 }
3810
3811 drv->retry_auth = 1;
3812 return wpa_driver_nl80211_authenticate(bss, &params);
3813}
3814
3815
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003816static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3817 size_t data_len, int noack,
3818 unsigned int freq, int no_cck,
3819 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003820 unsigned int wait_time,
3821 const u16 *csa_offs,
Hai Shalomfdcde762020-04-02 11:19:20 -07003822 size_t csa_offs_len, int no_encrypt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 struct wpa_driver_nl80211_data *drv = bss->drv;
3825 struct ieee80211_mgmt *mgmt;
Hai Shalomfdcde762020-04-02 11:19:20 -07003826 int encrypt = !no_encrypt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 u16 fc;
Hai Shalomfdcde762020-04-02 11:19:20 -07003828 int use_cookie = 1;
3829 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830
3831 mgmt = (struct ieee80211_mgmt *) data;
3832 fc = le_to_host16(mgmt->frame_control);
Hai Shalomfdcde762020-04-02 11:19:20 -07003833 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da=" MACSTR
3834 " 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 -07003835 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
Hai Shalomfdcde762020-04-02 11:19:20 -07003836 no_encrypt, fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003837
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003838 if ((is_sta_interface(drv->nlmode) ||
3839 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3841 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3842 /*
3843 * The use of last_mgmt_freq is a bit of a hack,
3844 * but it works due to the single-threaded nature
3845 * of wpa_supplicant.
3846 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003847 if (freq == 0) {
3848 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3849 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003850 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003851 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003852 wait_time = 0;
3853 use_cookie = 0;
3854 no_cck = 1;
3855 offchanok = 1;
3856 goto send_frame_cmd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003858
3859 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003860 if (freq == 0) {
3861 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3862 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003863 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003864 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003865 if ((int) freq == bss->freq)
3866 wait_time = 0;
3867 goto send_frame_cmd;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003868 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3871 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3872 /*
3873 * Only one of the authentication frame types is encrypted.
3874 * In order for static WEP encryption to work properly (i.e.,
3875 * to not encrypt the frame), we need to tell mac80211 about
3876 * the frames that must not be encrypted.
3877 */
3878 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3879 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3880 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3881 encrypt = 0;
3882 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883
Hai Shalomfdcde762020-04-02 11:19:20 -07003884 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_STATION &&
3885 (drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
3886 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
3887 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3888 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3889 freq = nl80211_get_assoc_freq(drv);
3890 wpa_printf(MSG_DEBUG,
3891 "nl80211: send_mlme - Use assoc_freq=%u for external auth",
3892 freq);
3893 }
3894
3895 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3896 freq = nl80211_get_assoc_freq(drv);
3897 wpa_printf(MSG_DEBUG,
3898 "nl80211: send_mlme - Use assoc_freq=%u for IBSS",
3899 freq);
3900 }
3901 if (freq == 0) {
3902 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - Use bss->freq=%u",
3903 bss->freq);
3904 freq = bss->freq;
3905 }
3906
3907 if (drv->use_monitor) {
3908 wpa_printf(MSG_DEBUG,
3909 "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
3910 freq, bss->freq);
3911 return nl80211_send_monitor(drv, data, data_len, encrypt,
3912 noack);
3913 }
3914
3915 if (noack || WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
3916 WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
3917 use_cookie = 0;
3918send_frame_cmd:
3919#ifdef CONFIG_TESTING_OPTIONS
3920 if (no_encrypt && !encrypt && !drv->use_monitor) {
3921 wpa_printf(MSG_DEBUG,
3922 "nl80211: Request to send an unencrypted frame - use a monitor interface for this");
3923 if (nl80211_create_monitor_interface(drv) < 0)
3924 return -1;
3925 res = nl80211_send_monitor(drv, data, data_len, encrypt,
3926 noack);
3927 nl80211_remove_monitor_interface(drv);
3928 return res;
3929 }
3930#endif /* CONFIG_TESTING_OPTIONS */
3931
3932 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame_cmd");
3933 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, data_len,
3934 use_cookie, no_cck, noack, offchanok,
3935 csa_offs, csa_offs_len);
3936
3937 return res;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003938}
3939
3940
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003941static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3942{
3943 u8 rates[NL80211_MAX_SUPP_RATES];
3944 u8 rates_len = 0;
3945 int i;
3946
3947 if (!basic_rates)
3948 return 0;
3949
3950 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3951 rates[rates_len++] = basic_rates[i] / 5;
3952
3953 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3954}
3955
3956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003957static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3958 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003959 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960{
3961 struct wpa_driver_nl80211_data *drv = bss->drv;
3962 struct nl_msg *msg;
3963
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003964 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3965 (cts >= 0 &&
3966 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3967 (preamble >= 0 &&
3968 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3969 (slot >= 0 &&
3970 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3971 (ht_opmode >= 0 &&
3972 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3973 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003974 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3975 nl80211_put_basic_rates(msg, basic_rates)) {
3976 nlmsg_free(msg);
3977 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003978 }
3979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003980 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003981}
3982
3983
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003984static int wpa_driver_nl80211_set_acl(void *priv,
3985 struct hostapd_acl_params *params)
3986{
3987 struct i802_bss *bss = priv;
3988 struct wpa_driver_nl80211_data *drv = bss->drv;
3989 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003990 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003991 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003992 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003993
3994 if (!(drv->capa.max_acl_mac_addrs))
3995 return -ENOTSUP;
3996
3997 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3998 return -ENOTSUP;
3999
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004000 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
4001 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
4002
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004003 acl = nlmsg_alloc();
4004 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004005 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004006 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004007 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
4008 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004009 return -ENOMEM;
4010 }
4011 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004012
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004013 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
4014 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
4015 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
4016 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
4017 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
4018 nlmsg_free(msg);
4019 nlmsg_free(acl);
4020 return -ENOMEM;
4021 }
4022 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004023
4024 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004025 if (ret) {
4026 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
4027 ret, strerror(-ret));
4028 }
4029
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004030 return ret;
4031}
4032
4033
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004034static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
4035{
4036 if (beacon_int > 0) {
4037 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
4038 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
4039 beacon_int);
4040 }
4041
4042 return 0;
4043}
4044
4045
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004046static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
4047{
4048 if (dtim_period > 0) {
4049 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
4050 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
4051 }
4052
4053 return 0;
4054}
4055
4056
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004057#ifdef CONFIG_MESH
4058static int nl80211_set_mesh_config(void *priv,
4059 struct wpa_driver_mesh_bss_params *params)
4060{
4061 struct i802_bss *bss = priv;
4062 struct wpa_driver_nl80211_data *drv = bss->drv;
4063 struct nl_msg *msg;
4064 int ret;
4065
4066 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
4067 if (!msg)
4068 return -1;
4069
4070 ret = nl80211_put_mesh_config(msg, params);
4071 if (ret < 0) {
4072 nlmsg_free(msg);
4073 return ret;
4074 }
4075
4076 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4077 if (ret) {
4078 wpa_printf(MSG_ERROR,
4079 "nl80211: Mesh config set failed: %d (%s)",
4080 ret, strerror(-ret));
4081 return ret;
4082 }
4083 return 0;
4084}
4085#endif /* CONFIG_MESH */
4086
4087
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004088static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
4089 struct wpa_driver_ap_params *params)
4090{
4091 struct nlattr *bands, *band;
4092 struct nl80211_txrate_vht vht_rate;
4093
4094 if (!params->freq ||
4095 (params->beacon_rate == 0 &&
4096 params->rate_type == BEACON_RATE_LEGACY))
4097 return 0;
4098
4099 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
4100 if (!bands)
4101 return -1;
4102
4103 switch (params->freq->mode) {
4104 case HOSTAPD_MODE_IEEE80211B:
4105 case HOSTAPD_MODE_IEEE80211G:
4106 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
4107 break;
4108 case HOSTAPD_MODE_IEEE80211A:
4109 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
4110 break;
4111 case HOSTAPD_MODE_IEEE80211AD:
4112 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
4113 break;
4114 default:
4115 return 0;
4116 }
4117
4118 if (!band)
4119 return -1;
4120
4121 os_memset(&vht_rate, 0, sizeof(vht_rate));
4122
4123 switch (params->rate_type) {
4124 case BEACON_RATE_LEGACY:
4125 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
4126 wpa_printf(MSG_INFO,
4127 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
4128 return -1;
4129 }
4130
4131 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
4132 (u8) params->beacon_rate / 5) ||
4133 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
4134 (params->freq->vht_enabled &&
4135 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4136 &vht_rate)))
4137 return -1;
4138
4139 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
4140 params->beacon_rate);
4141 break;
4142 case BEACON_RATE_HT:
4143 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
4144 wpa_printf(MSG_INFO,
4145 "nl80211: Driver does not support setting Beacon frame rate (HT)");
4146 return -1;
4147 }
4148 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
4149 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
4150 (params->freq->vht_enabled &&
4151 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4152 &vht_rate)))
4153 return -1;
4154 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
4155 params->beacon_rate);
4156 break;
4157 case BEACON_RATE_VHT:
4158 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
4159 wpa_printf(MSG_INFO,
4160 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
4161 return -1;
4162 }
4163 vht_rate.mcs[0] = BIT(params->beacon_rate);
4164 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
4165 return -1;
4166 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
4167 return -1;
4168 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
4169 &vht_rate))
4170 return -1;
4171 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
4172 params->beacon_rate);
4173 break;
4174 }
4175
4176 nla_nest_end(msg, band);
4177 nla_nest_end(msg, bands);
4178
4179 return 0;
4180}
4181
4182
4183static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
4184 int multicast_to_unicast)
4185{
4186 struct wpa_driver_nl80211_data *drv = bss->drv;
4187 struct nl_msg *msg;
4188 int ret;
4189
4190 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
4191 if (!msg ||
4192 (multicast_to_unicast &&
4193 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
4194 wpa_printf(MSG_ERROR,
4195 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
4196 bss->ifname);
4197 nlmsg_free(msg);
4198 return -ENOBUFS;
4199 }
4200
4201 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4202
4203 switch (ret) {
4204 case 0:
4205 wpa_printf(MSG_DEBUG,
4206 "nl80211: multicast to unicast %s on interface %s",
4207 multicast_to_unicast ? "enabled" : "disabled",
4208 bss->ifname);
4209 break;
4210 case -EOPNOTSUPP:
4211 if (!multicast_to_unicast)
4212 break;
4213 wpa_printf(MSG_INFO,
4214 "nl80211: multicast to unicast not supported on interface %s",
4215 bss->ifname);
4216 break;
4217 default:
4218 wpa_printf(MSG_ERROR,
4219 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
4220 multicast_to_unicast ? "enabling" : "disabling",
4221 ret, strerror(-ret), bss->ifname);
4222 break;
4223 }
4224
4225 return ret;
4226}
4227
4228
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004229static int wpa_driver_nl80211_set_ap(void *priv,
4230 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231{
4232 struct i802_bss *bss = priv;
4233 struct wpa_driver_nl80211_data *drv = bss->drv;
4234 struct nl_msg *msg;
4235 u8 cmd = NL80211_CMD_NEW_BEACON;
Hai Shalom74f70d42019-02-11 14:42:39 -08004236 int ret = -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004238 int num_suites;
Hai Shalomfdcde762020-04-02 11:19:20 -07004239 u32 suites[20], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004240 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004241#ifdef CONFIG_MESH
4242 struct wpa_driver_mesh_bss_params mesh_params;
4243#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004244
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004245 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
4248 beacon_set);
4249 if (beacon_set)
4250 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08004251 else if (!drv->device_ap_sme && !drv->use_monitor &&
4252 !nl80211_get_wiphy_data_ap(bss))
4253 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004254
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004255 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
4256 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004257 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
4258 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004259 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004260 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004261 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
4262 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004263 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Hai Shalom74f70d42019-02-11 14:42:39 -08004264 wpa_printf(MSG_DEBUG, "nl80211: ssid=%s",
4265 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004266 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
4267 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
4268 params->head) ||
4269 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
4270 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004271 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004272 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004273 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004274 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4275 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004276 if (params->proberesp && params->proberesp_len) {
4277 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
4278 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004279 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
4280 params->proberesp))
4281 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004282 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 switch (params->hide_ssid) {
4284 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004285 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004286 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4287 NL80211_HIDDEN_SSID_NOT_IN_USE))
4288 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004289 break;
4290 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004291 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004292 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4293 NL80211_HIDDEN_SSID_ZERO_LEN))
4294 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004295 break;
4296 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004297 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004298 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4299 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
4300 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004301 break;
4302 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004303 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004304 if (params->privacy &&
4305 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
4306 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004307 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004308 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
4309 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
4310 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004311 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
4312 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4313 NL80211_AUTHTYPE_SHARED_KEY))
4314 goto fail;
4315 } else {
4316 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4317 NL80211_AUTHTYPE_OPEN_SYSTEM))
4318 goto fail;
4319 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004320
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004321 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 ver = 0;
4323 if (params->wpa_version & WPA_PROTO_WPA)
4324 ver |= NL80211_WPA_VERSION_1;
4325 if (params->wpa_version & WPA_PROTO_RSN)
4326 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004327 if (ver &&
4328 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4329 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004330
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004331 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
4332 params->key_mgmt_suites);
Hai Shalomfdcde762020-04-02 11:19:20 -07004333 num_suites = wpa_key_mgmt_to_suites(params->key_mgmt_suites,
4334 suites, ARRAY_SIZE(suites));
4335 if (num_suites > NL80211_MAX_NR_AKM_SUITES)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004336 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07004337 "nl80211: Not enough room for all AKM suites (num_suites=%d > NL80211_MAX_NR_AKM_SUITES)",
4338 num_suites);
4339 else if (num_suites &&
4340 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
4341 suites))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004342 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004343
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004344 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004345 (!params->pairwise_ciphers ||
4346 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
4347 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
4348 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004349 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004350
Hai Shalom81f62d82019-07-22 12:10:00 -07004351 if (drv->device_ap_sme &&
4352 (params->key_mgmt_suites & WPA_KEY_MGMT_SAE) &&
4353 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
4354 goto fail;
Hai Shalom5f92bc92019-04-18 11:54:11 -07004355
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004356 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4357 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004358 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4359 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004360 if (num_suites &&
4361 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4362 num_suites * sizeof(u32), suites))
4363 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004364
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004365 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4366 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004367 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004368 if (suite &&
4369 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4370 goto fail;
4371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004373 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4374 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004375 if (nla_put(msg, NL80211_ATTR_IE,
4376 wpabuf_len(params->beacon_ies),
4377 wpabuf_head(params->beacon_ies)))
4378 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379 }
4380 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004381 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4382 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004383 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4384 wpabuf_len(params->proberesp_ies),
4385 wpabuf_head(params->proberesp_ies)))
4386 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004387 }
4388 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004389 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4390 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004391 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4392 wpabuf_len(params->assocresp_ies),
4393 wpabuf_head(params->assocresp_ies)))
4394 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396
Dmitry Shmidt04949592012-07-19 12:16:46 -07004397 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004398 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4399 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004400 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4401 params->ap_max_inactivity))
4402 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004403 }
4404
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004405#ifdef CONFIG_P2P
4406 if (params->p2p_go_ctwindow > 0) {
4407 if (drv->p2p_go_ctwindow_supported) {
4408 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4409 params->p2p_go_ctwindow);
4410 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4411 params->p2p_go_ctwindow))
4412 goto fail;
4413 } else {
4414 wpa_printf(MSG_INFO,
4415 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4416 }
4417 }
4418#endif /* CONFIG_P2P */
4419
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004420 if (params->pbss) {
4421 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4422 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4423 goto fail;
4424 }
4425
Hai Shalom74f70d42019-02-11 14:42:39 -08004426 if (params->ftm_responder) {
4427 struct nlattr *ftm;
4428
4429 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
4430 ret = -ENOTSUP;
4431 goto fail;
4432 }
4433
4434 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
4435 if (!ftm ||
4436 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
4437 (params->lci &&
4438 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
4439 wpabuf_len(params->lci),
4440 wpabuf_head(params->lci))) ||
4441 (params->civic &&
4442 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
4443 wpabuf_len(params->civic),
4444 wpabuf_head(params->civic))))
4445 goto fail;
4446 nla_nest_end(msg, ftm);
4447 }
4448
Hai Shalomc3565922019-10-28 11:58:20 -07004449#ifdef CONFIG_IEEE80211AX
4450 if (params->he_spr) {
4451 struct nlattr *spr;
4452
4453 spr = nla_nest_start(msg, NL80211_ATTR_HE_OBSS_PD);
4454 wpa_printf(MSG_DEBUG, "nl80211: he_spr=%d", params->he_spr);
4455
Hai Shalomfdcde762020-04-02 11:19:20 -07004456 if (!spr ||
4457 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET,
Hai Shalomc3565922019-10-28 11:58:20 -07004458 params->he_spr_srg_obss_pd_min_offset) ||
4459 nla_put_u8(msg, NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET,
4460 params->he_spr_srg_obss_pd_max_offset))
4461 goto fail;
4462
4463 nla_nest_end(msg, spr);
4464 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004465
4466 if (params->freq && params->freq->he_enabled) {
4467 struct nlattr *bss_color;
4468
4469 bss_color = nla_nest_start(msg, NL80211_ATTR_HE_BSS_COLOR);
4470 if (!bss_color ||
4471 (params->he_bss_color_disabled &&
4472 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_DISABLED)) ||
4473 (params->he_bss_color_partial &&
4474 nla_put_flag(msg, NL80211_HE_BSS_COLOR_ATTR_PARTIAL)) ||
4475 nla_put_u8(msg, NL80211_HE_BSS_COLOR_ATTR_COLOR,
4476 params->he_bss_color))
4477 goto fail;
4478 nla_nest_end(msg, bss_color);
4479 }
4480
4481 if (params->twt_responder) {
4482 wpa_printf(MSG_DEBUG, "nl80211: twt_responder=%d",
4483 params->twt_responder);
4484 if (nla_put_flag(msg, NL80211_ATTR_TWT_RESPONDER))
4485 goto fail;
4486 }
Hai Shalomc3565922019-10-28 11:58:20 -07004487#endif /* CONFIG_IEEE80211AX */
4488
Hai Shalomb755a2a2020-04-23 21:49:02 -07004489 ret = send_and_recv_msgs_owner(drv, msg, get_connect_handle(bss), 1,
4490 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 if (ret) {
4492 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4493 ret, strerror(-ret));
4494 } else {
4495 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004496 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4497 params->short_slot_time, params->ht_opmode,
4498 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004499 nl80211_set_multicast_to_unicast(bss,
4500 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004501 if (beacon_set && params->freq &&
4502 params->freq->bandwidth != bss->bandwidth) {
4503 wpa_printf(MSG_DEBUG,
4504 "nl80211: Update BSS %s bandwidth: %d -> %d",
4505 bss->ifname, bss->bandwidth,
4506 params->freq->bandwidth);
4507 ret = nl80211_set_channel(bss, params->freq, 1);
4508 if (ret) {
4509 wpa_printf(MSG_DEBUG,
4510 "nl80211: Frequency set failed: %d (%s)",
4511 ret, strerror(-ret));
4512 } else {
4513 wpa_printf(MSG_DEBUG,
4514 "nl80211: Frequency set succeeded for ht2040 coex");
4515 bss->bandwidth = params->freq->bandwidth;
4516 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004517 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004518 /*
4519 * cfg80211 updates the driver on frequence change in AP
4520 * mode only at the point when beaconing is started, so
4521 * set the initial value here.
4522 */
4523 bss->bandwidth = params->freq->bandwidth;
4524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004526
4527#ifdef CONFIG_MESH
4528 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4529 os_memset(&mesh_params, 0, sizeof(mesh_params));
4530 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4531 mesh_params.ht_opmode = params->ht_opmode;
4532 ret = nl80211_set_mesh_config(priv, &mesh_params);
4533 if (ret < 0)
4534 return ret;
4535 }
4536#endif /* CONFIG_MESH */
4537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004539fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004540 nlmsg_free(msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08004541 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542}
4543
4544
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004545static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004546 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547{
Hai Shalomc3565922019-10-28 11:58:20 -07004548 enum hostapd_hw_mode hw_mode;
4549 int is_24ghz;
4550 u8 channel;
4551
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004552 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004553 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4554 return -ENOBUFS;
4555
Hai Shalom81f62d82019-07-22 12:10:00 -07004556 wpa_printf(MSG_DEBUG, " * he_enabled=%d", freq->he_enabled);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004557 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4558 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4559
Hai Shalomc3565922019-10-28 11:58:20 -07004560 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
4561 is_24ghz = hw_mode == HOSTAPD_MODE_IEEE80211G ||
4562 hw_mode == HOSTAPD_MODE_IEEE80211B;
4563
4564 if (freq->vht_enabled || (freq->he_enabled && !is_24ghz)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004565 enum nl80211_chan_width cw;
4566
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004567 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004568 switch (freq->bandwidth) {
4569 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004570 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004571 break;
4572 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004573 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004574 break;
4575 case 80:
4576 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004577 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004578 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004579 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004580 break;
4581 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004582 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004583 break;
4584 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004585 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004586 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004587
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004588 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4589 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4590 freq->center_freq1);
4591 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4592 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004593 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4594 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4595 freq->center_freq1) ||
4596 (freq->center_freq2 &&
4597 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4598 freq->center_freq2)))
4599 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004600 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004601 enum nl80211_channel_type ct;
4602
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004603 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4604 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004605 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004607 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 break;
4609 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004610 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 break;
4612 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004613 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614 break;
4615 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004616
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004617 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004618 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4619 return -ENOBUFS;
Hai Shalomc3565922019-10-28 11:58:20 -07004620 } else if (freq->edmg.channels && freq->edmg.bw_config) {
4621 wpa_printf(MSG_DEBUG,
4622 " * EDMG configuration: channels=0x%x bw_config=%d",
4623 freq->edmg.channels, freq->edmg.bw_config);
4624 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
4625 freq->edmg.channels) ||
4626 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
4627 freq->edmg.bw_config))
4628 return -1;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004629 } else {
4630 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4631 NL80211_CHAN_NO_HT);
4632 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4633 NL80211_CHAN_NO_HT))
4634 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004636 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004637}
4638
4639
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004640static int nl80211_set_channel(struct i802_bss *bss,
4641 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004642{
4643 struct wpa_driver_nl80211_data *drv = bss->drv;
4644 struct nl_msg *msg;
4645 int ret;
4646
4647 wpa_printf(MSG_DEBUG,
Hai Shalom81f62d82019-07-22 12:10:00 -07004648 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, he_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4649 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004650 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004651
4652 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4653 NL80211_CMD_SET_WIPHY);
4654 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4655 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004656 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658
4659 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004660 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004661 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004665 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 return -1;
4667}
4668
4669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004670static u32 sta_flags_nl80211(int flags)
4671{
4672 u32 f = 0;
4673
4674 if (flags & WPA_STA_AUTHORIZED)
4675 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4676 if (flags & WPA_STA_WMM)
4677 f |= BIT(NL80211_STA_FLAG_WME);
4678 if (flags & WPA_STA_SHORT_PREAMBLE)
4679 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4680 if (flags & WPA_STA_MFP)
4681 f |= BIT(NL80211_STA_FLAG_MFP);
4682 if (flags & WPA_STA_TDLS_PEER)
4683 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004684 if (flags & WPA_STA_AUTHENTICATED)
4685 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004686 if (flags & WPA_STA_ASSOCIATED)
4687 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004688
4689 return f;
4690}
4691
4692
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004693#ifdef CONFIG_MESH
4694static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4695{
4696 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004697 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004698 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004699 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004700 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004701 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004702 return NL80211_PLINK_OPN_RCVD;
4703 case PLINK_CNF_RCVD:
4704 return NL80211_PLINK_CNF_RCVD;
4705 case PLINK_ESTAB:
4706 return NL80211_PLINK_ESTAB;
4707 case PLINK_HOLDING:
4708 return NL80211_PLINK_HOLDING;
4709 case PLINK_BLOCKED:
4710 return NL80211_PLINK_BLOCKED;
4711 default:
4712 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4713 state);
4714 }
4715 return -1;
4716}
4717#endif /* CONFIG_MESH */
4718
4719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720static int wpa_driver_nl80211_sta_add(void *priv,
4721 struct hostapd_sta_add_params *params)
4722{
4723 struct i802_bss *bss = priv;
4724 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004725 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004726 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 int ret = -ENOBUFS;
4728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004729 if ((params->flags & WPA_STA_TDLS_PEER) &&
4730 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4731 return -EOPNOTSUPP;
4732
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004733 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4734 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004735 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4736 NL80211_CMD_NEW_STATION);
4737 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4738 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004740 /*
4741 * Set the below properties only in one of the following cases:
4742 * 1. New station is added, already associated.
4743 * 2. Set WPA_STA_TDLS_PEER station.
4744 * 3. Set an already added unassociated station, if driver supports
4745 * full AP client state. (Set these properties after station became
4746 * associated will be rejected by the driver).
4747 */
4748 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4749 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4750 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004751 wpa_hexdump(MSG_DEBUG, " * supported rates",
4752 params->supp_rates, params->supp_rates_len);
4753 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4754 params->capability);
4755 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4756 params->supp_rates_len, params->supp_rates) ||
4757 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4758 params->capability))
4759 goto fail;
4760
4761 if (params->ht_capabilities) {
4762 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4763 (u8 *) params->ht_capabilities,
4764 sizeof(*params->ht_capabilities));
4765 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4766 sizeof(*params->ht_capabilities),
4767 params->ht_capabilities))
4768 goto fail;
4769 }
4770
4771 if (params->vht_capabilities) {
4772 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4773 (u8 *) params->vht_capabilities,
4774 sizeof(*params->vht_capabilities));
4775 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4776 sizeof(*params->vht_capabilities),
4777 params->vht_capabilities))
4778 goto fail;
4779 }
4780
Hai Shalom81f62d82019-07-22 12:10:00 -07004781 if (params->he_capab) {
4782 wpa_hexdump(MSG_DEBUG, " * he_capab",
4783 params->he_capab, params->he_capab_len);
4784 if (nla_put(msg, NL80211_ATTR_HE_CAPABILITY,
4785 params->he_capab_len, params->he_capab))
4786 goto fail;
4787 }
4788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004789 if (params->ext_capab) {
4790 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4791 params->ext_capab, params->ext_capab_len);
4792 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4793 params->ext_capab_len, params->ext_capab))
4794 goto fail;
4795 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004796
4797 if (is_ap_interface(drv->nlmode) &&
4798 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4799 params->support_p2p_ps ?
4800 NL80211_P2P_PS_SUPPORTED :
4801 NL80211_P2P_PS_UNSUPPORTED))
4802 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004803 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004804 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004805 if (params->aid) {
4806 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004807 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4808 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004809 } else {
4810 /*
4811 * cfg80211 validates that AID is non-zero, so we have
4812 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004813 * a dummy STA entry is used for now and for a station
4814 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004815 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004816 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4817 (params->flags & WPA_STA_TDLS_PEER) ?
4818 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004819 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4820 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004821 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004822 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4823 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004824 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4825 params->listen_interval))
4826 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004827 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4828 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004829 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4830 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004831 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4832 (params->flags & WPA_STA_ASSOCIATED)) {
4833 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4834 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4835 params->listen_interval);
4836 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4837 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4838 params->listen_interval))
4839 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004840 }
4841
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004842 if (params->vht_opmode_enabled) {
4843 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004844 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4845 params->vht_opmode))
4846 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004847 }
4848
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004849 if (params->supp_channels) {
4850 wpa_hexdump(MSG_DEBUG, " * supported channels",
4851 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004852 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4853 params->supp_channels_len, params->supp_channels))
4854 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004855 }
4856
4857 if (params->supp_oper_classes) {
4858 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4859 params->supp_oper_classes,
4860 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004861 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4862 params->supp_oper_classes_len,
4863 params->supp_oper_classes))
4864 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004865 }
4866
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004867 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004868 upd.set = sta_flags_nl80211(params->flags);
4869 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004870
4871 /*
4872 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4873 * flags, as nl80211 driver moves a new station, by default, into
4874 * associated state.
4875 *
4876 * On the other hand, if the driver supports that feature and the
4877 * station is added in unauthenticated state, set the
4878 * authenticated/associated bits in the mask to prevent moving this
4879 * station to associated state before it is actually associated.
4880 *
4881 * This is irrelevant for mesh mode where the station is added to the
4882 * driver as authenticated already, and ASSOCIATED isn't part of the
4883 * nl80211 API.
4884 */
4885 if (!is_mesh_interface(drv->nlmode)) {
4886 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4887 wpa_printf(MSG_DEBUG,
4888 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4889 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4890 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4891 } else if (!params->set &&
4892 !(params->flags & WPA_STA_TDLS_PEER)) {
4893 if (!(params->flags & WPA_STA_AUTHENTICATED))
4894 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4895 if (!(params->flags & WPA_STA_ASSOCIATED))
4896 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4897 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004898#ifdef CONFIG_MESH
4899 } else {
4900 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4901 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4902 params->peer_aid);
4903 if (ret)
4904 goto fail;
4905 }
4906#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004907 }
4908
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004909 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4910 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004911 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4912 goto fail;
4913
4914#ifdef CONFIG_MESH
4915 if (params->plink_state &&
4916 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4917 sta_plink_state_nl80211(params->plink_state)))
4918 goto fail;
4919#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004920
Hai Shalomc3565922019-10-28 11:58:20 -07004921 if ((!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4922 FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) &&
4923 (params->flags & WPA_STA_WMM)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004924 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4925
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004926 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004927 if (!wme ||
4928 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4929 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4930 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4931 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4932 WMM_QOSINFO_STA_SP_MASK))
4933 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004934 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 }
4936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004938 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004940 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4941 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4942 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 if (ret == -EEXIST)
4944 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004945fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004946 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 return ret;
4948}
4949
4950
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004951static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4952{
4953#ifdef CONFIG_LIBNL3_ROUTE
4954 struct wpa_driver_nl80211_data *drv = bss->drv;
4955 struct rtnl_neigh *rn;
4956 struct nl_addr *nl_addr;
4957 int err;
4958
4959 rn = rtnl_neigh_alloc();
4960 if (!rn)
4961 return;
4962
4963 rtnl_neigh_set_family(rn, AF_BRIDGE);
4964 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4965 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4966 if (!nl_addr) {
4967 rtnl_neigh_put(rn);
4968 return;
4969 }
4970 rtnl_neigh_set_lladdr(rn, nl_addr);
4971
4972 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4973 if (err < 0) {
4974 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4975 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4976 bss->ifindex, nl_geterror(err));
4977 } else {
4978 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4979 MACSTR, MAC2STR(addr));
4980 }
4981
4982 nl_addr_put(nl_addr);
4983 rtnl_neigh_put(rn);
4984#endif /* CONFIG_LIBNL3_ROUTE */
4985}
4986
4987
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004988static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4989 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 struct wpa_driver_nl80211_data *drv = bss->drv;
4992 struct nl_msg *msg;
4993 int ret;
4994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4996 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4997 (deauth == 0 &&
4998 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4999 WLAN_FC_STYPE_DISASSOC)) ||
5000 (deauth == 1 &&
5001 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
5002 WLAN_FC_STYPE_DEAUTH)) ||
5003 (reason_code &&
5004 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
5005 nlmsg_free(msg);
5006 return -ENOBUFS;
5007 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008
5009 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005010 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
5011 " --> %d (%s)",
5012 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005013
5014 if (drv->rtnl_sk)
5015 rtnl_neigh_delete_fdb_entry(bss, addr);
5016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 if (ret == -ENOENT)
5018 return 0;
5019 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020}
5021
5022
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005023void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024{
5025 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005026 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027
5028 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005031 dl_list_for_each(drv2, &drv->global->interfaces,
5032 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005033 {
5034 del_ifidx(drv2, ifidx, IFIDX_ANY);
5035 /* Remove all bridges learned for this iface */
5036 del_ifidx(drv2, IFIDX_ANY, ifidx);
5037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005039 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5041 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
5043}
5044
5045
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005046const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047{
5048 switch (mode) {
5049 case NL80211_IFTYPE_ADHOC:
5050 return "ADHOC";
5051 case NL80211_IFTYPE_STATION:
5052 return "STATION";
5053 case NL80211_IFTYPE_AP:
5054 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07005055 case NL80211_IFTYPE_AP_VLAN:
5056 return "AP_VLAN";
5057 case NL80211_IFTYPE_WDS:
5058 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059 case NL80211_IFTYPE_MONITOR:
5060 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07005061 case NL80211_IFTYPE_MESH_POINT:
5062 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005063 case NL80211_IFTYPE_P2P_CLIENT:
5064 return "P2P_CLIENT";
5065 case NL80211_IFTYPE_P2P_GO:
5066 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005067 case NL80211_IFTYPE_P2P_DEVICE:
5068 return "P2P_DEVICE";
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005069 case NL80211_IFTYPE_OCB:
5070 return "OCB";
5071 case NL80211_IFTYPE_NAN:
5072 return "NAN";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005073 default:
5074 return "unknown";
5075 }
5076}
5077
5078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
5080 const char *ifname,
5081 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005082 const u8 *addr, int wds,
5083 int (*handler)(struct nl_msg *, void *),
5084 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005086 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087 int ifidx;
5088 int ret = -ENOBUFS;
5089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005090 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
5091 iftype, nl80211_iftype_str(iftype));
5092
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005093 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
5094 if (!msg ||
5095 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
5096 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
5097 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098
5099 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005100 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005102 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005103 if (!flags ||
5104 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
5105 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005107 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005109 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
5110 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111 }
5112
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005113 /*
5114 * Tell cfg80211 that the interface belongs to the socket that created
5115 * it, and the interface should be deleted when the socket is closed.
5116 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005117 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5118 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005119
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005120 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005121 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005123 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005124 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
5126 ifname, ret, strerror(-ret));
5127 return ret;
5128 }
5129
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005130 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
5131 return 0;
5132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133 ifidx = if_nametoindex(ifname);
5134 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
5135 ifname, ifidx);
5136
5137 if (ifidx <= 0)
5138 return -1;
5139
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005140 /*
5141 * Some virtual interfaces need to process EAPOL packets and events on
5142 * the parent interface. This is used mainly with hostapd.
5143 */
5144 if (drv->hostapd ||
5145 iftype == NL80211_IFTYPE_AP_VLAN ||
5146 iftype == NL80211_IFTYPE_WDS ||
5147 iftype == NL80211_IFTYPE_MONITOR) {
5148 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005149 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07005150 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151
5152 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005153 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154 nl80211_remove_iface(drv, ifidx);
5155 return -1;
5156 }
5157
5158 return ifidx;
5159}
5160
5161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005162int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
5163 const char *ifname, enum nl80211_iftype iftype,
5164 const u8 *addr, int wds,
5165 int (*handler)(struct nl_msg *, void *),
5166 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167{
5168 int ret;
5169
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005170 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
5171 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005173 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005175 if (use_existing) {
5176 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
5177 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07005178 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
5179 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
5180 addr) < 0 &&
5181 (linux_set_iface_flags(drv->global->ioctl_sock,
5182 ifname, 0) < 0 ||
5183 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
5184 addr) < 0 ||
5185 linux_set_iface_flags(drv->global->ioctl_sock,
5186 ifname, 1) < 0))
5187 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005188 return -ENFILE;
5189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
5191
5192 /* Try to remove the interface that was already there. */
5193 nl80211_remove_iface(drv, if_nametoindex(ifname));
5194
5195 /* Try to create the interface again */
5196 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005197 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198 }
5199
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005200 if (ret >= 0 && is_p2p_net_interface(iftype)) {
5201 wpa_printf(MSG_DEBUG,
5202 "nl80211: Interface %s created for P2P - disable 11b rates",
5203 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206
5207 return ret;
5208}
5209
5210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005211static int nl80211_setup_ap(struct i802_bss *bss)
5212{
5213 struct wpa_driver_nl80211_data *drv = bss->drv;
5214
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005215 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
5216 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005217
5218 /*
5219 * Disable Probe Request reporting unless we need it in this way for
5220 * devices that include the AP SME, in the other case (unless using
5221 * monitor iface) we'll get it through the nl_mgmt socket instead.
5222 */
5223 if (!drv->device_ap_sme)
5224 wpa_driver_nl80211_probe_req_report(bss, 0);
5225
5226 if (!drv->device_ap_sme && !drv->use_monitor)
5227 if (nl80211_mgmt_subscribe_ap(bss))
5228 return -1;
5229
5230 if (drv->device_ap_sme && !drv->use_monitor)
5231 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005232 wpa_printf(MSG_DEBUG,
5233 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005234
5235 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07005236 nl80211_create_monitor_interface(drv) &&
5237 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005238 return -1;
5239
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005240 if (drv->device_ap_sme &&
5241 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
5242 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
5243 "Probe Request frame reporting in AP mode");
5244 /* Try to survive without this */
5245 }
5246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005247 return 0;
5248}
5249
5250
5251static void nl80211_teardown_ap(struct i802_bss *bss)
5252{
5253 struct wpa_driver_nl80211_data *drv = bss->drv;
5254
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005255 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
5256 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005257 if (drv->device_ap_sme) {
5258 wpa_driver_nl80211_probe_req_report(bss, 0);
5259 if (!drv->use_monitor)
5260 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
5261 } else if (drv->use_monitor)
5262 nl80211_remove_monitor_interface(drv);
5263 else
5264 nl80211_mgmt_unsubscribe(bss, "AP teardown");
5265
Paul Stewart092955c2017-02-06 09:13:09 -08005266 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005267 bss->beacon_set = 0;
5268}
5269
5270
Hai Shalomfdcde762020-04-02 11:19:20 -07005271static int nl80211_tx_control_port(void *priv, const u8 *dest,
5272 u16 proto, const u8 *buf, size_t len,
5273 int no_encrypt)
5274{
5275 struct i802_bss *bss = priv;
5276 struct nl_msg *msg;
5277 int ret;
5278
5279 wpa_printf(MSG_DEBUG,
5280 "nl80211: Send over control port dest=" MACSTR
5281 " proto=0x%04x len=%u no_encrypt=%d",
5282 MAC2STR(dest), proto, (unsigned int) len, no_encrypt);
5283
5284 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CONTROL_PORT_FRAME);
5285 if (!msg ||
5286 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
5287 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dest) ||
5288 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
5289 (no_encrypt &&
5290 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))) {
5291 nlmsg_free(msg);
5292 return -ENOBUFS;
5293 }
5294
5295 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5296 if (ret)
5297 wpa_printf(MSG_DEBUG,
5298 "nl80211: tx_control_port failed: ret=%d (%s)",
5299 ret, strerror(-ret));
5300
5301 return ret;
5302}
5303
5304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005305static int nl80211_send_eapol_data(struct i802_bss *bss,
5306 const u8 *addr, const u8 *data,
5307 size_t data_len)
5308{
5309 struct sockaddr_ll ll;
5310 int ret;
5311
5312 if (bss->drv->eapol_tx_sock < 0) {
5313 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
5314 return -1;
5315 }
5316
5317 os_memset(&ll, 0, sizeof(ll));
5318 ll.sll_family = AF_PACKET;
5319 ll.sll_ifindex = bss->ifindex;
5320 ll.sll_protocol = htons(ETH_P_PAE);
5321 ll.sll_halen = ETH_ALEN;
5322 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
5323 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
5324 (struct sockaddr *) &ll, sizeof(ll));
5325 if (ret < 0)
5326 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
5327 strerror(errno));
5328
5329 return ret;
5330}
5331
5332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
5334
5335static int wpa_driver_nl80211_hapd_send_eapol(
5336 void *priv, const u8 *addr, const u8 *data,
5337 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
5338{
5339 struct i802_bss *bss = priv;
5340 struct wpa_driver_nl80211_data *drv = bss->drv;
5341 struct ieee80211_hdr *hdr;
5342 size_t len;
5343 u8 *pos;
5344 int res;
5345 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08005346
Hai Shalomb755a2a2020-04-23 21:49:02 -07005347 /* For now, disable EAPOL TX over control port in AP mode by default
5348 * since it does not provide TX status notifications. */
5349 if (drv->control_port_ap &&
5350 (drv->capa.flags & WPA_DRIVER_FLAGS_CONTROL_PORT))
Hai Shalomfdcde762020-04-02 11:19:20 -07005351 return nl80211_tx_control_port(bss, addr, ETH_P_EAPOL,
5352 data, data_len, !encrypt);
5353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005354 if (drv->device_ap_sme || !drv->use_monitor)
5355 return nl80211_send_eapol_data(bss, addr, data, data_len);
5356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
5358 data_len;
5359 hdr = os_zalloc(len);
5360 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005361 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
5362 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363 return -1;
5364 }
5365
5366 hdr->frame_control =
5367 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
5368 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
5369 if (encrypt)
5370 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
5371 if (qos) {
5372 hdr->frame_control |=
5373 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
5374 }
5375
5376 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
5377 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
5378 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
5379 pos = (u8 *) (hdr + 1);
5380
5381 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005382 /* Set highest priority in QoS header */
5383 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 pos[1] = 0;
5385 pos += 2;
5386 }
5387
5388 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5389 pos += sizeof(rfc1042_header);
5390 WPA_PUT_BE16(pos, ETH_P_PAE);
5391 pos += 2;
5392 memcpy(pos, data, data_len);
5393
Hai Shalomfdcde762020-04-02 11:19:20 -07005394 res = nl80211_send_monitor(drv, hdr, len, encrypt, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395 if (res < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07005396 wpa_printf(MSG_ERROR,
5397 "hapd_send_eapol - packet len: %lu - failed",
5398 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399 }
5400 os_free(hdr);
5401
5402 return res;
5403}
5404
5405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005407 unsigned int total_flags,
5408 unsigned int flags_or,
5409 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005410{
5411 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005412 struct nl_msg *msg;
5413 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 struct nl80211_sta_flag_update upd;
5415
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005416 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
5417 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
5418 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
5419 !!(total_flags & WPA_STA_AUTHORIZED));
5420
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005421 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5422 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
5423 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424
5425 /*
5426 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5427 * can be removed eventually.
5428 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005429 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005430 if (!flags ||
5431 ((total_flags & WPA_STA_AUTHORIZED) &&
5432 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
5433 ((total_flags & WPA_STA_WMM) &&
5434 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
5435 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
5436 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
5437 ((total_flags & WPA_STA_MFP) &&
5438 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
5439 ((total_flags & WPA_STA_TDLS_PEER) &&
5440 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
5441 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005442
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005443 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444
5445 os_memset(&upd, 0, sizeof(upd));
5446 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5447 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005448 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5449 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005451 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5452fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005453 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454 return -ENOBUFS;
5455}
5456
5457
Hai Shalom81f62d82019-07-22 12:10:00 -07005458static int driver_nl80211_sta_set_airtime_weight(void *priv, const u8 *addr,
5459 unsigned int weight)
5460{
5461 struct i802_bss *bss = priv;
5462 struct nl_msg *msg;
5463
5464 wpa_printf(MSG_DEBUG,
5465 "nl80211: Set STA airtime weight - ifname=%s addr=" MACSTR
5466 " weight=%u", bss->ifname, MAC2STR(addr), weight);
5467
5468 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5469 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5470 nla_put_u16(msg, NL80211_ATTR_AIRTIME_WEIGHT, weight))
5471 goto fail;
5472
5473 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5474fail:
5475 nlmsg_free(msg);
5476 return -ENOBUFS;
5477}
5478
5479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5481 struct wpa_driver_associate_params *params)
5482{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005483 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005484
5485 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5487 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005488 nlmode = NL80211_IFTYPE_P2P_GO;
5489 } else
5490 nlmode = NL80211_IFTYPE_AP;
5491
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005492 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005493 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005494 nl80211_remove_monitor_interface(drv);
5495 return -1;
5496 }
5497
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005498 if (params->freq.freq &&
5499 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005500 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005501 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 nl80211_remove_monitor_interface(drv);
5503 return -1;
5504 }
5505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 return 0;
5507}
5508
5509
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005510static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5511 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512{
5513 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005514 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005516 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Hai Shalomb755a2a2020-04-23 21:49:02 -07005517 ret = send_and_recv_msgs_owner(drv, msg,
5518 get_connect_handle(drv->first_bss), 1,
5519 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005520 if (ret) {
5521 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5522 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005523 } else {
5524 wpa_printf(MSG_DEBUG,
5525 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005526 }
5527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005528 if (reset_mode &&
5529 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005530 NL80211_IFTYPE_STATION)) {
5531 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5532 "station mode");
5533 }
5534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535 return ret;
5536}
5537
5538
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005539static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5540 struct wpa_driver_associate_params *params)
5541{
5542 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5543 return -1;
5544
5545 if (params->htcaps && params->htcaps_mask) {
5546 int sz = sizeof(struct ieee80211_ht_capabilities);
5547 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5548 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5549 params->htcaps_mask, sz);
5550 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5551 params->htcaps) ||
5552 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5553 params->htcaps_mask))
5554 return -1;
5555 }
5556
5557#ifdef CONFIG_VHT_OVERRIDES
5558 if (params->disable_vht) {
5559 wpa_printf(MSG_DEBUG, " * VHT disabled");
5560 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5561 return -1;
5562 }
5563
5564 if (params->vhtcaps && params->vhtcaps_mask) {
5565 int sz = sizeof(struct ieee80211_vht_capabilities);
5566 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5567 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5568 params->vhtcaps_mask, sz);
5569 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5570 params->vhtcaps) ||
5571 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5572 params->vhtcaps_mask))
5573 return -1;
5574 }
5575#endif /* CONFIG_VHT_OVERRIDES */
5576
5577 return 0;
5578}
5579
5580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005581static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5582 struct wpa_driver_associate_params *params)
5583{
5584 struct nl_msg *msg;
5585 int ret = -1;
5586 int count = 0;
5587
5588 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5589
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005590 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5592 "IBSS mode");
5593 return -1;
5594 }
5595
5596retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005597 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5598 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5599 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005600
Hai Shalom74f70d42019-02-11 14:42:39 -08005601 wpa_printf(MSG_DEBUG, " * SSID=%s",
5602 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005603 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5604 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005605 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5606 drv->ssid_len = params->ssid_len;
5607
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005608 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5609 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611
5612 ret = nl80211_set_conn_keys(params, msg);
5613 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005614 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005616 if (params->bssid && params->fixed_bssid) {
5617 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5618 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005619 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5620 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005621 }
5622
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005623 if (params->fixed_freq) {
5624 wpa_printf(MSG_DEBUG, " * fixed_freq");
5625 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5626 goto fail;
5627 }
5628
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005629 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5630 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5631 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5632 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005633 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005634 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5635 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005636 }
5637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 if (params->wpa_ie) {
5639 wpa_hexdump(MSG_DEBUG,
5640 " * Extra IEs for Beacon/Probe Response frames",
5641 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005642 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5643 params->wpa_ie))
5644 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645 }
5646
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005647 ret = nl80211_ht_vht_overrides(msg, params);
5648 if (ret < 0)
5649 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005650
Hai Shalomb755a2a2020-04-23 21:49:02 -07005651 ret = send_and_recv_msgs_owner(drv, msg,
5652 get_connect_handle(drv->first_bss), 1,
5653 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 msg = NULL;
5655 if (ret) {
5656 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5657 ret, strerror(-ret));
5658 count++;
5659 if (ret == -EALREADY && count == 1) {
5660 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5661 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005662 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 nlmsg_free(msg);
5664 goto retry;
5665 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005666 } else {
5667 wpa_printf(MSG_DEBUG,
5668 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005671fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 nlmsg_free(msg);
5673 return ret;
5674}
5675
5676
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005677static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5678 struct wpa_driver_associate_params *params,
5679 struct nl_msg *msg)
5680{
5681 if (params->fils_erp_username_len) {
5682 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5683 params->fils_erp_username,
5684 params->fils_erp_username_len);
5685 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5686 params->fils_erp_username_len,
5687 params->fils_erp_username))
5688 return -1;
5689 }
5690
5691 if (params->fils_erp_realm_len) {
5692 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5693 params->fils_erp_realm,
5694 params->fils_erp_realm_len);
5695 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5696 params->fils_erp_realm_len, params->fils_erp_realm))
5697 return -1;
5698 }
5699
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005700 if (params->fils_erp_rrk_len) {
Vinita S. Maloo3a5b4412020-05-19 17:43:22 +05305701 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5702 params->fils_erp_next_seq_num);
5703 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5704 params->fils_erp_next_seq_num))
5705 return -1;
5706
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005707 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5708 (unsigned long) params->fils_erp_rrk_len);
5709 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5710 params->fils_erp_rrk_len, params->fils_erp_rrk))
5711 return -1;
5712 }
5713
5714 return 0;
5715}
5716
5717
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005718static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5719 struct wpa_driver_associate_params *params,
5720 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721{
Paul Stewart092955c2017-02-06 09:13:09 -08005722 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5723 return -1;
5724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725 if (params->bssid) {
5726 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5727 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005728 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5729 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005731
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005732 if (params->bssid_hint) {
5733 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5734 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005735 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5736 params->bssid_hint))
5737 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005738 }
5739
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005740 if (params->freq.freq) {
5741 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005742 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5743 params->freq.freq))
5744 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005745 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005746 } else
5747 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005748
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005749 if (params->freq_hint) {
5750 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005751 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5752 params->freq_hint))
5753 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005754 }
5755
Hai Shalomc3565922019-10-28 11:58:20 -07005756 if (params->freq.edmg.channels && params->freq.edmg.bw_config) {
5757 wpa_printf(MSG_DEBUG,
5758 " * EDMG configuration: channels=0x%x bw_config=%d",
5759 params->freq.edmg.channels,
5760 params->freq.edmg.bw_config);
5761 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_CHANNELS,
5762 params->freq.edmg.channels) ||
5763 nla_put_u8(msg, NL80211_ATTR_WIPHY_EDMG_BW_CONFIG,
5764 params->freq.edmg.bw_config))
5765 return -1;
5766 }
5767
Dmitry Shmidt04949592012-07-19 12:16:46 -07005768 if (params->bg_scan_period >= 0) {
5769 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5770 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005771 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5772 params->bg_scan_period))
5773 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005774 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08005777 wpa_printf(MSG_DEBUG, " * SSID=%s",
5778 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005779 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5780 params->ssid))
5781 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005782 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005783 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005784 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5785 drv->ssid_len = params->ssid_len;
5786 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005789 if (params->wpa_ie &&
5790 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5791 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005793 if (params->wpa_proto) {
5794 enum nl80211_wpa_versions ver = 0;
5795
5796 if (params->wpa_proto & WPA_PROTO_WPA)
5797 ver |= NL80211_WPA_VERSION_1;
5798 if (params->wpa_proto & WPA_PROTO_RSN)
5799 ver |= NL80211_WPA_VERSION_2;
5800
5801 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005802 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5803 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005804 }
5805
5806 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5807 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5808 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005809 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5810 cipher))
5811 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005812 }
5813
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005814 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5815 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5816 /*
5817 * This is likely to work even though many drivers do not
5818 * advertise support for operations without GTK.
5819 */
5820 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5821 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005822 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5823 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005824 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5825 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005826 }
5827
5828 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5829 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5830 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5831 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005832 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005833 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5834 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005835 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07005836 params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
5837 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005838 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005839 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07005840 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005841 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5842 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5843 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07005844 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5845 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5846 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
Paul Stewart092955c2017-02-06 09:13:09 -08005847 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005848
5849 switch (params->key_mgmt_suite) {
5850 case WPA_KEY_MGMT_CCKM:
Paul Stewart092955c2017-02-06 09:13:09 -08005851 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005852 break;
5853 case WPA_KEY_MGMT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005854 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005855 break;
5856 case WPA_KEY_MGMT_FT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005857 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005858 break;
5859 case WPA_KEY_MGMT_FT_PSK:
Paul Stewart092955c2017-02-06 09:13:09 -08005860 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005861 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005862 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005863 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005864 break;
5865 case WPA_KEY_MGMT_PSK_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005866 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005867 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005868 case WPA_KEY_MGMT_OSEN:
Paul Stewart092955c2017-02-06 09:13:09 -08005869 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005870 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07005871 case WPA_KEY_MGMT_SAE:
5872 mgmt = RSN_AUTH_KEY_MGMT_SAE;
5873 break;
5874 case WPA_KEY_MGMT_FT_SAE:
5875 mgmt = RSN_AUTH_KEY_MGMT_FT_SAE;
5876 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005877 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Paul Stewart092955c2017-02-06 09:13:09 -08005878 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005879 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005880 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Paul Stewart092955c2017-02-06 09:13:09 -08005881 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005882 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07005883 case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
5884 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
5885 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005886 case WPA_KEY_MGMT_FILS_SHA256:
5887 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5888 break;
5889 case WPA_KEY_MGMT_FILS_SHA384:
5890 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5891 break;
5892 case WPA_KEY_MGMT_FT_FILS_SHA256:
5893 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5894 break;
5895 case WPA_KEY_MGMT_FT_FILS_SHA384:
5896 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5897 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005898 case WPA_KEY_MGMT_OWE:
5899 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5900 break;
5901 case WPA_KEY_MGMT_DPP:
5902 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5903 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005904 case WPA_KEY_MGMT_PSK:
5905 default:
Paul Stewart092955c2017-02-06 09:13:09 -08005906 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005907 break;
5908 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005909 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005910 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5911 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005912 }
5913
Hai Shalomc3565922019-10-28 11:58:20 -07005914 if (params->req_handshake_offload &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005915 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)) {
5916 wpa_printf(MSG_DEBUG, " * WANT_1X_4WAY_HS");
5917 if (nla_put_flag(msg, NL80211_ATTR_WANT_1X_4WAY_HS))
5918 return -1;
5919 }
5920
Roshan Pius3a1667e2018-07-03 15:17:14 -07005921 /* Add PSK in case of 4-way handshake offload */
5922 if (params->psk &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005923 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005924 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5925 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5926 return -1;
5927 }
5928
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005929 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5930 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005931
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005932 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5933 (params->pairwise_suite == WPA_CIPHER_NONE ||
5934 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5935 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5936 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5937 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5938 return -1;
5939
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005940 if (params->rrm_used) {
5941 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005942 if ((!((drv_rrm_flags &
5943 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5944 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5945 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005946 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5947 return -1;
5948 }
5949
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005950 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005951 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005952
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005953 if (params->p2p)
5954 wpa_printf(MSG_DEBUG, " * P2P group");
5955
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005956 if (params->pbss) {
5957 wpa_printf(MSG_DEBUG, " * PBSS");
5958 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5959 return -1;
5960 }
5961
Dmitry Shmidte4663042016-04-04 10:07:49 -07005962 drv->connect_reassoc = 0;
5963 if (params->prev_bssid) {
5964 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5965 MAC2STR(params->prev_bssid));
5966 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5967 params->prev_bssid))
5968 return -1;
5969 drv->connect_reassoc = 1;
5970 }
5971
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005972 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5973 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5974 return -1;
5975
Hai Shalomc3565922019-10-28 11:58:20 -07005976 if ((params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
5977 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005978 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5979 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5980 return -1;
5981
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005982 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005983}
5984
5985
5986static int wpa_driver_nl80211_try_connect(
5987 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005988 struct wpa_driver_associate_params *params,
Hai Shalomfdcde762020-04-02 11:19:20 -07005989 struct nl_sock *nl_connect)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005990{
5991 struct nl_msg *msg;
5992 enum nl80211_auth_type type;
5993 int ret;
5994 int algs;
5995
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005996#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005997 if (params->req_key_mgmt_offload && params->psk &&
5998 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5999 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
6000 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
6001 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
6002 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
6003 if (ret)
6004 return ret;
6005 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006006#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006007
6008 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
6009 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006010 if (!msg)
6011 return -1;
6012
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006013 ret = nl80211_connect_common(drv, params, msg);
6014 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006015 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006016
Roshan Pius3a1667e2018-07-03 15:17:14 -07006017 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
6018 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
6019 goto fail;
6020
6021 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
6022 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
6023 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
6024 goto fail;
6025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026 algs = 0;
6027 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6028 algs++;
6029 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6030 algs++;
6031 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6032 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006033 if (params->auth_alg & WPA_AUTH_ALG_FILS)
6034 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006035 if (params->auth_alg & WPA_AUTH_ALG_FT)
6036 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 if (algs > 1) {
6038 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
6039 "selection");
6040 goto skip_auth_type;
6041 }
6042
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006043 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006045 if (type == NL80211_AUTHTYPE_MAX ||
6046 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006047 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006048
6049skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050 ret = nl80211_set_conn_keys(params, msg);
6051 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006052 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053
Hai Shalomb755a2a2020-04-23 21:49:02 -07006054 ret = send_and_recv_msgs_owner(drv, msg, nl_connect, 1, NULL,
6055 (void *) -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 msg = NULL;
6057 if (ret) {
6058 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
6059 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006060 } else {
6061 wpa_printf(MSG_DEBUG,
6062 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006065fail:
Hai Shalom74f70d42019-02-11 14:42:39 -08006066 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 nlmsg_free(msg);
6068 return ret;
6069
6070}
6071
6072
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006073static int wpa_driver_nl80211_connect(
6074 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006075 struct wpa_driver_associate_params *params,
Hai Shalomfdcde762020-04-02 11:19:20 -07006076 struct nl_sock *nl_connect)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006077{
Jithu Jancea7c60b42014-12-03 18:54:40 +05306078 int ret;
6079
6080 /* Store the connection attempted bssid for future use */
6081 if (params->bssid)
6082 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6083 else
6084 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
6085
Roshan Pius3a1667e2018-07-03 15:17:14 -07006086 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006087 if (ret == -EALREADY) {
6088 /*
6089 * cfg80211 does not currently accept new connections if
6090 * we are already connected. As a workaround, force
6091 * disconnection and try again.
6092 */
6093 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
6094 "disconnecting before reassociation "
6095 "attempt");
6096 if (wpa_driver_nl80211_disconnect(
Hai Shalom74f70d42019-02-11 14:42:39 -08006097 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, nl_connect))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006098 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006099 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006100 }
6101 return ret;
6102}
6103
6104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105static int wpa_driver_nl80211_associate(
6106 void *priv, struct wpa_driver_associate_params *params)
6107{
6108 struct i802_bss *bss = priv;
6109 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006110 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111 struct nl_msg *msg;
6112
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006113 nl80211_unmask_11b_rates(bss);
6114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006115 if (params->mode == IEEE80211_MODE_AP)
6116 return wpa_driver_nl80211_ap(drv, params);
6117
6118 if (params->mode == IEEE80211_MODE_IBSS)
6119 return wpa_driver_nl80211_ibss(drv, params);
6120
6121 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006122 enum nl80211_iftype nlmode = params->p2p ?
6123 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6124
6125 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07006127 if (params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
Hai Shalomb755a2a2020-04-23 21:49:02 -07006128 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE)
Hai Shalom74f70d42019-02-11 14:42:39 -08006129 bss->use_nl_connect = 1;
Hai Shalomb755a2a2020-04-23 21:49:02 -07006130 else
Hai Shalom74f70d42019-02-11 14:42:39 -08006131 bss->use_nl_connect = 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08006132
Hai Shalomb755a2a2020-04-23 21:49:02 -07006133 return wpa_driver_nl80211_connect(drv, params,
6134 get_connect_handle(bss));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006135 }
6136
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006137 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006139 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
6140 drv->ifindex);
6141 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142 if (!msg)
6143 return -1;
6144
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006145 ret = nl80211_connect_common(drv, params, msg);
6146 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006147 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006148
Roshan Pius3a1667e2018-07-03 15:17:14 -07006149 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
6150 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
6151 goto fail;
6152
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006153 if (params->fils_kek) {
6154 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
6155 (unsigned int) params->fils_kek_len);
6156 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
6157 params->fils_kek))
6158 goto fail;
6159 }
6160 if (params->fils_nonces) {
6161 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
6162 params->fils_nonces,
6163 params->fils_nonces_len);
6164 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
6165 params->fils_nonces_len, params->fils_nonces))
6166 goto fail;
6167 }
6168
Hai Shalomb755a2a2020-04-23 21:49:02 -07006169 ret = send_and_recv_msgs_owner(drv, msg,
6170 get_connect_handle(drv->first_bss), 1,
6171 NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 msg = NULL;
6173 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174 wpa_dbg(drv->ctx, MSG_DEBUG,
6175 "nl80211: MLME command failed (assoc): ret=%d (%s)",
6176 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006178 } else {
6179 wpa_printf(MSG_DEBUG,
6180 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006182
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006183fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006184 nlmsg_free(msg);
6185 return ret;
6186}
6187
6188
6189static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006190 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191{
6192 struct nl_msg *msg;
6193 int ret = -ENOBUFS;
6194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006195 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
6196 ifindex, mode, nl80211_iftype_str(mode));
6197
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006198 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
6199 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
6200 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201
6202 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006203 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204 if (!ret)
6205 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006206fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006207 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006208 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
6209 " %d (%s)", ifindex, mode, ret, strerror(-ret));
6210 return ret;
6211}
6212
6213
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006214static int wpa_driver_nl80211_set_mode_impl(
6215 struct i802_bss *bss,
6216 enum nl80211_iftype nlmode,
6217 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006219 struct wpa_driver_nl80211_data *drv = bss->drv;
6220 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006222 int was_ap = is_ap_interface(drv->nlmode);
6223 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006224 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07006226 if (TEST_FAIL())
6227 return -1;
6228
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006229 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
6230 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
6231 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006232
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006233 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234 drv->nlmode = nlmode;
6235 ret = 0;
6236 goto done;
6237 }
6238
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006239 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006240 return -1;
6241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242 if (nlmode == drv->nlmode) {
6243 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
6244 "requested mode - ignore error");
6245 ret = 0;
6246 goto done; /* Already in the requested mode */
6247 }
6248
6249 /* mac80211 doesn't allow mode changes while the device is up, so
6250 * take the device down, try to set the mode again, and bring the
6251 * device back up.
6252 */
6253 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
6254 "interface down");
6255 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006256 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006257 if (res == -EACCES || res == -ENODEV)
6258 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006259 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006260 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
6261 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006262 os_sleep(0, 100000);
6263 continue;
6264 }
6265
6266 /*
6267 * Setting the mode will fail for some drivers if the phy is
6268 * on a frequency that the mode is disallowed in.
6269 */
6270 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006271 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006272 if (res) {
6273 wpa_printf(MSG_DEBUG,
6274 "nl80211: Failed to set frequency on interface");
6275 }
6276 }
6277
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006278 if (i == 0 && was_ap && !is_ap_interface(nlmode) &&
6279 bss->brname[0] &&
6280 (bss->added_if_into_bridge || bss->already_in_bridge)) {
6281 wpa_printf(MSG_DEBUG,
6282 "nl80211: Remove AP interface %s temporarily from the bridge %s to allow its mode to be set to STATION",
6283 bss->ifname, bss->brname);
6284 if (linux_br_del_if(drv->global->ioctl_sock,
6285 bss->brname, bss->ifname) < 0)
6286 wpa_printf(MSG_INFO,
6287 "nl80211: Failed to remove interface %s from bridge %s: %s",
6288 bss->ifname, bss->brname,
6289 strerror(errno));
6290 }
6291
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006292 /* Try to set the mode again while the interface is down */
6293 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
6294 if (mode_switch_res == -EBUSY) {
6295 wpa_printf(MSG_DEBUG,
6296 "nl80211: Delaying mode set while interface going down");
6297 os_sleep(0, 100000);
6298 continue;
6299 }
6300 ret = mode_switch_res;
6301 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 }
6303
6304 if (!ret) {
6305 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
6306 "interface is down");
6307 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006308 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309 }
6310
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006311 /* Bring the interface back up */
6312 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
6313 if (res != 0) {
6314 wpa_printf(MSG_DEBUG,
6315 "nl80211: Failed to set interface up after switching mode");
6316 ret = -1;
6317 }
6318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006320 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
6322 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006323 return ret;
6324 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006325
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006326 if (is_p2p_net_interface(nlmode)) {
6327 wpa_printf(MSG_DEBUG,
6328 "nl80211: Interface %s mode change to P2P - disable 11b rates",
6329 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006330 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006331 } else if (drv->disabled_11b_rates) {
6332 wpa_printf(MSG_DEBUG,
6333 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
6334 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006335 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006336 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006338 if (is_ap_interface(nlmode)) {
6339 nl80211_mgmt_unsubscribe(bss, "start AP");
6340 /* Setup additional AP mode functionality if needed */
6341 if (nl80211_setup_ap(bss))
6342 return -1;
6343 } else if (was_ap) {
6344 /* Remove additional AP mode functionality */
6345 nl80211_teardown_ap(bss);
6346 } else {
6347 nl80211_mgmt_unsubscribe(bss, "mode change");
6348 }
6349
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006350 if (is_mesh_interface(nlmode) &&
6351 nl80211_mgmt_subscribe_mesh(bss))
6352 return -1;
6353
Dmitry Shmidt04949592012-07-19 12:16:46 -07006354 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006355 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006356 nl80211_mgmt_subscribe_non_ap(bss) < 0)
6357 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
6358 "frame processing - ignore for now");
6359
6360 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006361}
6362
6363
Hai Shalom4fbc08f2020-05-18 12:37:00 -07006364void nl80211_restore_ap_mode(struct i802_bss *bss)
6365{
6366 struct wpa_driver_nl80211_data *drv = bss->drv;
6367 int was_ap = is_ap_interface(drv->nlmode);
6368
6369 wpa_driver_nl80211_set_mode(bss, drv->ap_scan_as_station);
6370 if (!was_ap && is_ap_interface(drv->ap_scan_as_station) &&
6371 bss->brname[0] &&
6372 (bss->added_if_into_bridge || bss->already_in_bridge)) {
6373 wpa_printf(MSG_DEBUG,
6374 "nl80211: Add AP interface %s back into the bridge %s",
6375 bss->ifname, bss->brname);
6376 if (linux_br_add_if(drv->global->ioctl_sock, bss->brname,
6377 bss->ifname) < 0) {
6378 wpa_printf(MSG_WARNING,
6379 "nl80211: Failed to add interface %s into bridge %s: %s",
6380 bss->ifname, bss->brname, strerror(errno));
6381 }
6382 }
6383 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
6384}
6385
6386
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006387int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
6388 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006389{
6390 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
6391}
6392
6393
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006394static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
6395 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006396{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006397 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006398 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006399}
6400
6401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402static int wpa_driver_nl80211_get_capa(void *priv,
6403 struct wpa_driver_capa *capa)
6404{
6405 struct i802_bss *bss = priv;
6406 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07006407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408 if (!drv->has_capability)
6409 return -1;
6410 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006411 if (drv->extended_capa && drv->extended_capa_mask) {
6412 capa->extended_capa = drv->extended_capa;
6413 capa->extended_capa_mask = drv->extended_capa_mask;
6414 capa->extended_capa_len = drv->extended_capa_len;
6415 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006416
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006417 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006418}
6419
6420
6421static int wpa_driver_nl80211_set_operstate(void *priv, int state)
6422{
6423 struct i802_bss *bss = priv;
6424 struct wpa_driver_nl80211_data *drv = bss->drv;
6425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006426 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
6427 bss->ifname, drv->operstate, state,
6428 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006430 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431 state ? IF_OPER_UP : IF_OPER_DORMANT);
6432}
6433
6434
6435static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
6436{
6437 struct i802_bss *bss = priv;
6438 struct wpa_driver_nl80211_data *drv = bss->drv;
6439 struct nl_msg *msg;
6440 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006441 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006442
6443 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
6444 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
6445 return 0;
6446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006447
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006448 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
6449 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
6450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006451 os_memset(&upd, 0, sizeof(upd));
6452 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
6453 if (authorized)
6454 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006455
6456 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6457 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
6458 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
6459 nlmsg_free(msg);
6460 return -ENOBUFS;
6461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006463 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006464 if (!ret)
6465 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006466 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
6467 ret, strerror(-ret));
6468 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006469}
6470
6471
Jouni Malinen75ecf522011-06-27 15:19:46 -07006472/* Set kernel driver on given frequency (MHz) */
6473static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474{
Jouni Malinen75ecf522011-06-27 15:19:46 -07006475 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006476 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477}
6478
6479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006480static inline int min_int(int a, int b)
6481{
6482 if (a < b)
6483 return a;
6484 return b;
6485}
6486
6487
6488static int get_key_handler(struct nl_msg *msg, void *arg)
6489{
6490 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6491 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6492
6493 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6494 genlmsg_attrlen(gnlh, 0), NULL);
6495
6496 /*
6497 * TODO: validate the key index and mac address!
6498 * Otherwise, there's a race condition as soon as
6499 * the kernel starts sending key notifications.
6500 */
6501
6502 if (tb[NL80211_ATTR_KEY_SEQ])
6503 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
6504 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
Hai Shalom021b0b52019-04-10 11:17:58 -07006505 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006506 return NL_SKIP;
6507}
6508
6509
6510static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
6511 int idx, u8 *seq)
6512{
6513 struct i802_bss *bss = priv;
6514 struct wpa_driver_nl80211_data *drv = bss->drv;
6515 struct nl_msg *msg;
6516
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006517 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
6518 NL80211_CMD_GET_KEY);
6519 if (!msg ||
6520 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
6521 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
6522 nlmsg_free(msg);
6523 return -ENOBUFS;
6524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525
6526 memset(seq, 0, 6);
6527
6528 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529}
6530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006531
6532static int i802_set_rts(void *priv, int rts)
6533{
6534 struct i802_bss *bss = priv;
6535 struct wpa_driver_nl80211_data *drv = bss->drv;
6536 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006537 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006538 u32 val;
6539
Hai Shalom021b0b52019-04-10 11:17:58 -07006540 if (rts >= 2347 || rts == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006541 val = (u32) -1;
6542 else
6543 val = rts;
6544
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006545 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6546 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
6547 nlmsg_free(msg);
6548 return -ENOBUFS;
6549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550
6551 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6552 if (!ret)
6553 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
6555 "%d (%s)", rts, ret, strerror(-ret));
6556 return ret;
6557}
6558
6559
6560static int i802_set_frag(void *priv, int frag)
6561{
6562 struct i802_bss *bss = priv;
6563 struct wpa_driver_nl80211_data *drv = bss->drv;
6564 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006565 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006566 u32 val;
6567
Hai Shalom021b0b52019-04-10 11:17:58 -07006568 if (frag >= 2346 || frag == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006569 val = (u32) -1;
6570 else
6571 val = frag;
6572
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006573 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6574 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6575 nlmsg_free(msg);
6576 return -ENOBUFS;
6577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578
6579 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6580 if (!ret)
6581 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006582 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6583 "%d: %d (%s)", frag, ret, strerror(-ret));
6584 return ret;
6585}
6586
6587
6588static int i802_flush(void *priv)
6589{
6590 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006591 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006592 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006594 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6595 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596
6597 /*
6598 * XXX: FIX! this needs to flush all VLANs too
6599 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006600 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6601 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006602 if (res) {
6603 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6604 "(%s)", res, strerror(-res));
6605 }
6606 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607}
6608
6609
Hai Shalom81f62d82019-07-22 12:10:00 -07006610static void get_sta_tid_stats(struct hostap_sta_driver_data *data,
6611 struct nlattr *attr)
6612{
6613 struct nlattr *tid_stats[NL80211_TID_STATS_MAX + 1], *tidattr;
6614 struct nlattr *txq_stats[NL80211_TXQ_STATS_MAX + 1];
6615 static struct nla_policy txq_stats_policy[NL80211_TXQ_STATS_MAX + 1] = {
6616 [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
6617 [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
6618 };
6619 int rem;
6620
6621 nla_for_each_nested(tidattr, attr, rem) {
6622 if (nla_parse_nested(tid_stats, NL80211_TID_STATS_MAX,
6623 tidattr, NULL) != 0 ||
6624 !tid_stats[NL80211_TID_STATS_TXQ_STATS] ||
6625 nla_parse_nested(txq_stats, NL80211_TXQ_STATS_MAX,
6626 tid_stats[NL80211_TID_STATS_TXQ_STATS],
6627 txq_stats_policy) != 0)
6628 continue;
6629 /* sum the backlogs over all TIDs for station */
6630 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES])
6631 data->backlog_bytes += nla_get_u32(
6632 txq_stats[NL80211_TXQ_STATS_BACKLOG_BYTES]);
6633 if (txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS])
6634 data->backlog_bytes += nla_get_u32(
6635 txq_stats[NL80211_TXQ_STATS_BACKLOG_PACKETS]);
6636 }
6637}
6638
6639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640static int get_sta_handler(struct nl_msg *msg, void *arg)
6641{
6642 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6643 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6644 struct hostap_sta_driver_data *data = arg;
6645 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6646 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6647 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6648 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6649 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6650 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6651 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006652 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006653 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6654 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006655 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07006656 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
Hai Shalom81f62d82019-07-22 12:10:00 -07006657 [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
6658 [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006659 };
6660 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6661 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6662 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6663 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6664 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6665 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6666 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6667 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006668 };
6669
6670 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6671 genlmsg_attrlen(gnlh, 0), NULL);
6672
6673 /*
6674 * TODO: validate the interface and mac address!
6675 * Otherwise, there's a race condition as soon as
6676 * the kernel starts sending station notifications.
6677 */
6678
6679 if (!tb[NL80211_ATTR_STA_INFO]) {
6680 wpa_printf(MSG_DEBUG, "sta stats missing!");
6681 return NL_SKIP;
6682 }
6683 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6684 tb[NL80211_ATTR_STA_INFO],
6685 stats_policy)) {
6686 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6687 return NL_SKIP;
6688 }
6689
6690 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6691 data->inactive_msec =
6692 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006693 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 if (stats[NL80211_STA_INFO_RX_BYTES])
6695 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6696 if (stats[NL80211_STA_INFO_TX_BYTES])
6697 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006698 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6699 stats[NL80211_STA_INFO_TX_BYTES64]) {
6700 /*
6701 * The driver supports 64-bit counters, so use them to override
6702 * the 32-bit values.
6703 */
6704 data->rx_bytes =
6705 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6706 data->tx_bytes =
6707 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6708 data->bytes_64bit = 1;
6709 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006710 if (stats[NL80211_STA_INFO_RX_PACKETS])
6711 data->rx_packets =
6712 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6713 if (stats[NL80211_STA_INFO_TX_PACKETS])
6714 data->tx_packets =
6715 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Hai Shalom81f62d82019-07-22 12:10:00 -07006716 if (stats[NL80211_STA_INFO_RX_DURATION])
6717 data->rx_airtime =
6718 nla_get_u64(stats[NL80211_STA_INFO_RX_DURATION]);
6719 if (stats[NL80211_STA_INFO_TX_DURATION])
6720 data->tx_airtime =
6721 nla_get_u64(stats[NL80211_STA_INFO_TX_DURATION]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006722 if (stats[NL80211_STA_INFO_TX_FAILED])
6723 data->tx_retry_failed =
6724 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006725 if (stats[NL80211_STA_INFO_SIGNAL])
6726 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006727 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6728 data->last_ack_rssi =
6729 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6730 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6731 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006732
6733 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6734 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6735 stats[NL80211_STA_INFO_TX_BITRATE],
6736 rate_policy) == 0) {
6737 if (rate[NL80211_RATE_INFO_BITRATE32])
6738 data->current_tx_rate =
6739 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6740 else if (rate[NL80211_RATE_INFO_BITRATE])
6741 data->current_tx_rate =
6742 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6743
6744 if (rate[NL80211_RATE_INFO_MCS]) {
6745 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6746 data->flags |= STA_DRV_DATA_TX_MCS;
6747 }
6748 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6749 data->tx_vhtmcs =
6750 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6751 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6752 }
6753 if (rate[NL80211_RATE_INFO_SHORT_GI])
6754 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6755 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6756 data->tx_vht_nss =
6757 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6758 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6759 }
6760 }
6761
6762 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6763 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6764 stats[NL80211_STA_INFO_RX_BITRATE],
6765 rate_policy) == 0) {
6766 if (rate[NL80211_RATE_INFO_BITRATE32])
6767 data->current_rx_rate =
6768 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6769 else if (rate[NL80211_RATE_INFO_BITRATE])
6770 data->current_rx_rate =
6771 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6772
6773 if (rate[NL80211_RATE_INFO_MCS]) {
6774 data->rx_mcs =
6775 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6776 data->flags |= STA_DRV_DATA_RX_MCS;
6777 }
6778 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6779 data->rx_vhtmcs =
6780 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6781 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6782 }
6783 if (rate[NL80211_RATE_INFO_SHORT_GI])
6784 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6785 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6786 data->rx_vht_nss =
6787 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6788 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6789 }
6790 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791
Hai Shalom81f62d82019-07-22 12:10:00 -07006792 if (stats[NL80211_STA_INFO_TID_STATS])
6793 get_sta_tid_stats(data, stats[NL80211_STA_INFO_TID_STATS]);
6794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795 return NL_SKIP;
6796}
6797
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006798static int i802_read_sta_data(struct i802_bss *bss,
6799 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006800 const u8 *addr)
6801{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006802 struct nl_msg *msg;
6803
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006804 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6805 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6806 nlmsg_free(msg);
6807 return -ENOBUFS;
6808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006810 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006811}
6812
6813
6814static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6815 int cw_min, int cw_max, int burst_time)
6816{
6817 struct i802_bss *bss = priv;
6818 struct wpa_driver_nl80211_data *drv = bss->drv;
6819 struct nl_msg *msg;
6820 struct nlattr *txq, *params;
Hai Shalom74f70d42019-02-11 14:42:39 -08006821 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006822
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006823 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006824 if (!msg)
6825 return -1;
6826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006827 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6828 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006829 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006830
6831 /* We are only sending parameters for a single TXQ at a time */
6832 params = nla_nest_start(msg, 1);
6833 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006834 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835
6836 switch (queue) {
6837 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006838 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6839 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840 break;
6841 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006842 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6843 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844 break;
6845 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006846 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6847 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006848 break;
6849 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006850 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6851 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006852 break;
6853 }
6854 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6855 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006856 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6857 (burst_time * 100 + 16) / 32) ||
6858 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6859 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6860 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6861 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862
6863 nla_nest_end(msg, params);
6864
6865 nla_nest_end(msg, txq);
6866
Hai Shalom74f70d42019-02-11 14:42:39 -08006867 res = send_and_recv_msgs(drv, msg, NULL, NULL);
6868 wpa_printf(MSG_DEBUG,
6869 "nl80211: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
6870 queue, aifs, cw_min, cw_max, burst_time, res);
6871 if (res == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006873 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006874fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006875 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876 return -1;
6877}
6878
6879
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006880static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881 const char *ifname, int vlan_id)
6882{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006883 struct wpa_driver_nl80211_data *drv = bss->drv;
6884 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006885 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006887 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6888 ", ifname=%s[%d], vlan_id=%d)",
6889 bss->ifname, if_nametoindex(bss->ifname),
6890 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006891 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6892 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
Hai Shalomfdcde762020-04-02 11:19:20 -07006893 ((drv->capa.flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD) &&
6894 nla_put_u16(msg, NL80211_ATTR_VLAN_ID, vlan_id)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006895 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6896 nlmsg_free(msg);
6897 return -ENOBUFS;
6898 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006899
6900 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6901 if (ret < 0) {
6902 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6903 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6904 MAC2STR(addr), ifname, vlan_id, ret,
6905 strerror(-ret));
6906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907 return ret;
6908}
6909
6910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911static int i802_get_inact_sec(void *priv, const u8 *addr)
6912{
6913 struct hostap_sta_driver_data data;
6914 int ret;
6915
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006916 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917 data.inactive_msec = (unsigned long) -1;
6918 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006919 if (ret == -ENOENT)
6920 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006921 if (ret || data.inactive_msec == (unsigned long) -1)
6922 return -1;
6923 return data.inactive_msec / 1000;
6924}
6925
6926
6927static int i802_sta_clear_stats(void *priv, const u8 *addr)
6928{
6929#if 0
6930 /* TODO */
6931#endif
6932 return 0;
6933}
6934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935
6936static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07006937 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938{
6939 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006940 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006942 u8 channel;
6943
6944 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6945 HOSTAPD_MODE_IEEE80211AD) {
6946 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6947 * disassociate the STA instead. */
6948 return i802_sta_disassoc(priv, own_addr, addr, reason);
6949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006950
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006951 if (is_mesh_interface(drv->nlmode))
6952 return -1;
6953
Dmitry Shmidt04949592012-07-19 12:16:46 -07006954 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006955 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006957 memset(&mgmt, 0, sizeof(mgmt));
6958 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6959 WLAN_FC_STYPE_DEAUTH);
6960 memcpy(mgmt.da, addr, ETH_ALEN);
6961 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6962 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6963 mgmt.u.deauth.reason_code = host_to_le16(reason);
6964 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6965 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006966 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -07006967 0, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968}
6969
6970
6971static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07006972 u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006973{
6974 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006975 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 struct ieee80211_mgmt mgmt;
6977
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006978 if (is_mesh_interface(drv->nlmode))
6979 return -1;
6980
Dmitry Shmidt04949592012-07-19 12:16:46 -07006981 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006982 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006984 memset(&mgmt, 0, sizeof(mgmt));
6985 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6986 WLAN_FC_STYPE_DISASSOC);
6987 memcpy(mgmt.da, addr, ETH_ALEN);
6988 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6989 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6990 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6991 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6992 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006993 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -07006994 0, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995}
6996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006997
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006998static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6999{
7000 char buf[200], *pos, *end;
7001 int i, res;
7002
7003 pos = buf;
7004 end = pos + sizeof(buf);
7005
7006 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007007 if (!drv->if_indices[i].ifindex)
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007008 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007009 res = os_snprintf(pos, end - pos, " %d(%d)",
Hai Shalom81f62d82019-07-22 12:10:00 -07007010 drv->if_indices[i].ifindex,
7011 drv->if_indices[i].reason);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007012 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007013 break;
7014 pos += res;
7015 }
7016 *pos = '\0';
7017
7018 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
7019 drv->num_if_indices, buf);
7020}
7021
7022
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007023static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
7024 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007025{
7026 int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07007027 struct drv_nl80211_if_info *old;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007028
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007029 wpa_printf(MSG_DEBUG,
7030 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
7031 ifidx, ifidx_reason);
7032 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007033 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
7034 ifidx);
7035 return;
7036 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007037 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007038 if (drv->if_indices[i].ifindex == 0) {
7039 drv->if_indices[i].ifindex = ifidx;
7040 drv->if_indices[i].reason = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007041 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007042 return;
7043 }
7044 }
7045
7046 if (drv->if_indices != drv->default_if_indices)
7047 old = drv->if_indices;
7048 else
7049 old = NULL;
7050
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007051 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
Hai Shalom81f62d82019-07-22 12:10:00 -07007052 sizeof(*old));
Jouni Malinen75ecf522011-06-27 15:19:46 -07007053 if (!drv->if_indices) {
7054 if (!old)
7055 drv->if_indices = drv->default_if_indices;
7056 else
7057 drv->if_indices = old;
7058 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7059 "interfaces");
7060 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7061 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007062 }
7063 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007064 os_memcpy(drv->if_indices, drv->default_if_indices,
7065 sizeof(drv->default_if_indices));
Hai Shalom81f62d82019-07-22 12:10:00 -07007066 drv->if_indices[drv->num_if_indices].ifindex = ifidx;
7067 drv->if_indices[drv->num_if_indices].reason = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007068 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007069 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007070}
7071
7072
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007073static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
7074 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007075{
7076 int i;
7077
7078 for (i = 0; i < drv->num_if_indices; i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007079 if ((drv->if_indices[i].ifindex == ifidx ||
7080 ifidx == IFIDX_ANY) &&
7081 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007082 ifidx_reason == IFIDX_ANY)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07007083 drv->if_indices[i].ifindex = 0;
7084 drv->if_indices[i].reason = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007085 break;
7086 }
7087 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007088 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007089}
7090
7091
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007092static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
7093 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007094{
7095 int i;
7096
7097 for (i = 0; i < drv->num_if_indices; i++)
Hai Shalom81f62d82019-07-22 12:10:00 -07007098 if (drv->if_indices[i].ifindex == ifidx &&
7099 (drv->if_indices[i].reason == ifidx_reason ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007100 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07007101 return 1;
7102
7103 return 0;
7104}
7105
7106
7107static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007108 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007109{
7110 struct i802_bss *bss = priv;
7111 struct wpa_driver_nl80211_data *drv = bss->drv;
7112 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07007113 union wpa_event_data event;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08007114 int ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007115
Hai Shalom39ba6fc2019-01-22 12:40:38 -08007116 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7117 if (ret >= (int) sizeof(name))
7118 wpa_printf(MSG_WARNING,
7119 "nl80211: WDS interface name was truncated");
7120 else if (ret < 0)
7121 return ret;
7122
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007123 if (ifname_wds)
7124 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
7125
Jouni Malinen75ecf522011-06-27 15:19:46 -07007126 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7127 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7128 if (val) {
7129 if (!if_nametoindex(name)) {
7130 if (nl80211_create_iface(drv, name,
7131 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007132 bss->addr, 1, NULL, NULL, 0) <
7133 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007134 return -1;
7135 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007136 linux_br_add_if(drv->global->ioctl_sock,
7137 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007138 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007139
7140 os_memset(&event, 0, sizeof(event));
7141 event.wds_sta_interface.sta_addr = addr;
7142 event.wds_sta_interface.ifname = name;
7143 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07007144 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007145 EVENT_WDS_STA_INTERFACE_STATUS,
7146 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007147 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007148 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
7149 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
7150 "interface %s up", name);
7151 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007152 return i802_set_sta_vlan(priv, addr, name, 0);
7153 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08007154 if (bridge_ifname &&
7155 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
7156 name) < 0)
7157 wpa_printf(MSG_INFO,
7158 "nl80211: Failed to remove interface %s from bridge %s: %s",
7159 name, bridge_ifname, strerror(errno));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007160
Jouni Malinen75ecf522011-06-27 15:19:46 -07007161 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007162 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07007163 os_memset(&event, 0, sizeof(event));
7164 event.wds_sta_interface.sta_addr = addr;
7165 event.wds_sta_interface.ifname = name;
7166 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07007167 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007168 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08007169 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007170 }
7171}
7172
7173
7174static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7175{
7176 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7177 struct sockaddr_ll lladdr;
7178 unsigned char buf[3000];
7179 int len;
7180 socklen_t fromlen = sizeof(lladdr);
7181
7182 len = recvfrom(sock, buf, sizeof(buf), 0,
7183 (struct sockaddr *)&lladdr, &fromlen);
7184 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007185 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
7186 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07007187 return;
7188 }
7189
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007190 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07007191 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7192}
7193
7194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007195static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
7196 struct i802_bss *bss,
7197 const char *brname, const char *ifname)
7198{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007199 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007200 char in_br[IFNAMSIZ];
7201
7202 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007203 br_ifindex = if_nametoindex(brname);
7204 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007205 /*
7206 * Bridge was configured, but the bridge device does
7207 * not exist. Try to add it now.
7208 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007209 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
7211 "bridge interface %s: %s",
7212 brname, strerror(errno));
7213 return -1;
7214 }
7215 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007216 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007217 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007218 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007219 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220
7221 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07007222 if (os_strcmp(in_br, brname) == 0) {
7223 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07007225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226
7227 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
7228 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007229 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
7230 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231 wpa_printf(MSG_ERROR, "nl80211: Failed to "
7232 "remove interface %s from bridge "
7233 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007234 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007235 return -1;
7236 }
7237 }
7238
7239 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
7240 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007241 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007242 wpa_printf(MSG_WARNING,
7243 "nl80211: Failed to add interface %s into bridge %s: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007244 ifname, brname, strerror(errno));
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007245 /* Try to continue without the interface being in a bridge. This
7246 * may be needed for some cases, e.g., with Open vSwitch, where
7247 * an external component will need to handle bridge
7248 * configuration. */
7249 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007250 }
7251 bss->added_if_into_bridge = 1;
7252
7253 return 0;
7254}
7255
7256
7257static void *i802_init(struct hostapd_data *hapd,
7258 struct wpa_init_params *params)
7259{
7260 struct wpa_driver_nl80211_data *drv;
7261 struct i802_bss *bss;
7262 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007263 char master_ifname[IFNAMSIZ];
7264 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007265 int br_added = 0;
7266
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007267 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
7268 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007269 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007270 if (bss == NULL)
7271 return NULL;
7272
7273 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007275 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007276 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007277 params->ifname, master_ifname);
7278 br_ifindex = if_nametoindex(master_ifname);
7279 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
7280 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
7281 linux_master_get(master_ifname, params->ifname) == 0) {
7282 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
7283 params->ifname, master_ifname);
7284 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007285 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007286
7287 /* check if master itself is under bridge */
7288 if (linux_br_get(master_ifname, master_ifname) == 0) {
7289 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
7290 master_ifname);
7291 br_ifindex = if_nametoindex(master_ifname);
7292 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
7293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007294 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007295 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007296 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007297
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007298 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007300 for (i = 0; i < params->num_bridge; i++) {
7301 if (params->bridge[i]) {
7302 ifindex = if_nametoindex(params->bridge[i]);
7303 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007304 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007305 if (ifindex == br_ifindex)
7306 br_added = 1;
7307 }
7308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007309
7310 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007311 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007313 if (params->num_bridge && params->bridge[0]) {
7314 if (i802_check_bridge(drv, bss, params->bridge[0],
7315 params->ifname) < 0)
7316 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007317 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007318 br_added = 1;
7319 }
7320
7321 if (!br_added && br_ifindex &&
7322 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007323 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007324
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007325#ifdef CONFIG_LIBNL3_ROUTE
Hai Shalomc9e41a12018-07-31 14:41:42 -07007326 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007327 int err;
7328
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007329 drv->rtnl_sk = nl_socket_alloc();
7330 if (drv->rtnl_sk == NULL) {
7331 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
7332 goto failed;
7333 }
7334
Hai Shalomfdcde762020-04-02 11:19:20 -07007335 err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
7336 if (err) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007337 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07007338 nl_geterror(err));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007339 goto failed;
7340 }
7341 }
7342#endif /* CONFIG_LIBNL3_ROUTE */
7343
Hai Shalomb755a2a2020-04-23 21:49:02 -07007344 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) {
7345 wpa_printf(MSG_DEBUG,
7346 "nl80211: Do not open EAPOL RX socket - using control port for RX");
7347 goto skip_eapol_sock;
7348 }
7349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
7351 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007352 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
7353 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007354 goto failed;
7355 }
7356
7357 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
7358 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007359 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 goto failed;
7361 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07007362skip_eapol_sock:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007364 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7365 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007366 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007367 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007369 memcpy(bss->addr, params->own_addr, ETH_ALEN);
7370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 return bss;
7372
7373failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007374 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007375 return NULL;
7376}
7377
7378
7379static void i802_deinit(void *priv)
7380{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007381 struct i802_bss *bss = priv;
7382 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383}
7384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385
7386static enum nl80211_iftype wpa_driver_nl80211_if_type(
7387 enum wpa_driver_if_type type)
7388{
7389 switch (type) {
7390 case WPA_IF_STATION:
7391 return NL80211_IFTYPE_STATION;
7392 case WPA_IF_P2P_CLIENT:
7393 case WPA_IF_P2P_GROUP:
7394 return NL80211_IFTYPE_P2P_CLIENT;
7395 case WPA_IF_AP_VLAN:
7396 return NL80211_IFTYPE_AP_VLAN;
7397 case WPA_IF_AP_BSS:
7398 return NL80211_IFTYPE_AP;
7399 case WPA_IF_P2P_GO:
7400 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007401 case WPA_IF_P2P_DEVICE:
7402 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007403 case WPA_IF_MESH:
7404 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007405 default:
7406 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408}
7409
7410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007411static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7412{
7413 struct wpa_driver_nl80211_data *drv;
7414 dl_list_for_each(drv, &global->interfaces,
7415 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007416 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007417 return 1;
7418 }
7419 return 0;
7420}
7421
7422
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007423static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007424{
7425 unsigned int idx;
7426
7427 if (!drv->global)
7428 return -1;
7429
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007430 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007432 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 new_addr[0] ^= idx << 2;
7434 if (!nl80211_addr_in_use(drv->global, new_addr))
7435 break;
7436 }
7437 if (idx == 64)
7438 return -1;
7439
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007440 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441 MACSTR, MAC2STR(new_addr));
7442
7443 return 0;
7444}
7445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007447struct wdev_info {
7448 u64 wdev_id;
7449 int wdev_id_set;
7450 u8 macaddr[ETH_ALEN];
7451};
7452
7453static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
7454{
7455 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7456 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7457 struct wdev_info *wi = arg;
7458
7459 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7460 genlmsg_attrlen(gnlh, 0), NULL);
7461 if (tb[NL80211_ATTR_WDEV]) {
7462 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
7463 wi->wdev_id_set = 1;
7464 }
7465
7466 if (tb[NL80211_ATTR_MAC])
7467 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
7468 ETH_ALEN);
7469
7470 return NL_SKIP;
7471}
7472
7473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
7475 const char *ifname, const u8 *addr,
7476 void *bss_ctx, void **drv_priv,
7477 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007478 const char *bridge, int use_existing,
7479 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007480{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007481 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007482 struct i802_bss *bss = priv;
7483 struct wpa_driver_nl80211_data *drv = bss->drv;
7484 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007485 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007486
7487 if (addr)
7488 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007489 nlmode = wpa_driver_nl80211_if_type(type);
7490 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
7491 struct wdev_info p2pdev_info;
7492
7493 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
7494 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
7495 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007496 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007497 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
7498 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
7499 ifname);
7500 return -1;
7501 }
7502
7503 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
7504 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
7505 if (!is_zero_ether_addr(p2pdev_info.macaddr))
7506 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
7507 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
7508 ifname,
7509 (long long unsigned int) p2pdev_info.wdev_id);
7510 } else {
7511 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007512 0, NULL, NULL, use_existing);
7513 if (use_existing && ifidx == -ENFILE) {
7514 added = 0;
7515 ifidx = if_nametoindex(ifname);
7516 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007517 return -1;
7518 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007519 }
7520
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007521 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007522 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007523 os_memcpy(if_addr, bss->addr, ETH_ALEN);
7524 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007525 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007526 if (added)
7527 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007528 return -1;
7529 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007530 }
7531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007532 if (!addr &&
7533 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007534 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
7535 type == WPA_IF_STATION)) {
7536 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007537 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007538
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007539 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007540 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007541 if (added)
7542 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543 return -1;
7544 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007545 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007546 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007547 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007548 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007549 if (added)
7550 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551 return -1;
7552 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007553 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007555 if (added)
7556 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 return -1;
7558 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007559 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007560 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007561 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007562
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007563 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007564 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
7565 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007566 if (added)
7567 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007568 return -1;
7569 }
7570
7571 if (bridge &&
7572 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7573 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7574 "interface %s to a bridge %s",
7575 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007576 if (added)
7577 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007578 os_free(new_bss);
7579 return -1;
7580 }
7581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007582 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7583 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007584 if (added)
7585 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007586 os_free(new_bss);
7587 return -1;
7588 }
7589 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007590 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007591 new_bss->ifindex = ifidx;
7592 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007593 new_bss->next = drv->first_bss->next;
7594 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007595 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007596 new_bss->added_if = added;
7597 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007598 if (drv_priv)
7599 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007600 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007601
7602 /* Subscribe management frames for this WPA_IF_AP_BSS */
7603 if (nl80211_setup_ap(new_bss))
7604 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007607 if (drv->global)
7608 drv->global->if_add_ifindex = ifidx;
7609
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007610 /*
7611 * Some virtual interfaces need to process EAPOL packets and events on
7612 * the parent interface. This is used mainly with hostapd.
7613 */
7614 if (ifidx > 0 &&
7615 (drv->hostapd ||
7616 nlmode == NL80211_IFTYPE_AP_VLAN ||
7617 nlmode == NL80211_IFTYPE_WDS ||
7618 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007619 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621 return 0;
7622}
7623
7624
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007625static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007626 enum wpa_driver_if_type type,
7627 const char *ifname)
7628{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629 struct wpa_driver_nl80211_data *drv = bss->drv;
7630 int ifindex = if_nametoindex(ifname);
7631
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007632 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7633 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007634 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07007635 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007636 else if (ifindex > 0 && !bss->added_if) {
7637 struct wpa_driver_nl80211_data *drv2;
7638 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007639 struct wpa_driver_nl80211_data, list) {
7640 del_ifidx(drv2, ifindex, IFIDX_ANY);
7641 del_ifidx(drv2, IFIDX_ANY, ifindex);
7642 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007643 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007644
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007645 if (type != WPA_IF_AP_BSS)
7646 return 0;
7647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007648 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007649 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7650 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007651 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7652 "interface %s from bridge %s: %s",
7653 bss->ifname, bss->brname, strerror(errno));
7654 }
7655 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007656 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7658 "bridge %s: %s",
7659 bss->brname, strerror(errno));
7660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007661
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007662 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663 struct i802_bss *tbss;
7664
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007665 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7666 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667 if (tbss->next == bss) {
7668 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007669 /* Unsubscribe management frames */
7670 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007671 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007672 if (!bss->added_if)
7673 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007674 os_free(bss);
7675 bss = NULL;
7676 break;
7677 }
7678 }
7679 if (bss)
7680 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7681 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007682 } else {
7683 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7684 nl80211_teardown_ap(bss);
7685 if (!bss->added_if && !drv->first_bss->next)
Paul Stewart092955c2017-02-06 09:13:09 -08007686 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007687 nl80211_destroy_bss(bss);
7688 if (!bss->added_if)
7689 i802_set_iface_flags(bss, 0);
7690 if (drv->first_bss->next) {
7691 drv->first_bss = drv->first_bss->next;
7692 drv->ctx = drv->first_bss->ctx;
7693 os_free(bss);
7694 } else {
7695 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7696 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007698
7699 return 0;
7700}
7701
7702
7703static int cookie_handler(struct nl_msg *msg, void *arg)
7704{
7705 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7706 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7707 u64 *cookie = arg;
7708 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7709 genlmsg_attrlen(gnlh, 0), NULL);
7710 if (tb[NL80211_ATTR_COOKIE])
7711 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7712 return NL_SKIP;
7713}
7714
7715
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007716static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007717 unsigned int freq, unsigned int wait,
7718 const u8 *buf, size_t buf_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07007719 int save_cookie, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007720 int offchanok, const u16 *csa_offs,
7721 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007723 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724 struct nl_msg *msg;
7725 u64 cookie;
7726 int ret = -1;
7727
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007728 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07007729 "no_ack=%d offchanok=%d",
7730 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007731 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007733 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7734 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7735 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7736 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7737 drv->test_use_roc_tx) &&
7738 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7739 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7740 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007741 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7742 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007743 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7744 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007745
7746 cookie = 0;
7747 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7748 msg = NULL;
7749 if (ret) {
7750 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007751 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7752 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007753 } else {
7754 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7755 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7756 (long long unsigned int) cookie);
7757
Hai Shalomfdcde762020-04-02 11:19:20 -07007758 if (save_cookie)
7759 drv->send_frame_cookie = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007760
Hai Shalomfdcde762020-04-02 11:19:20 -07007761 if (drv->num_send_frame_cookies == MAX_SEND_FRAME_COOKIES) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007762 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07007763 "nl80211: Drop oldest pending send frame cookie 0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007764 (long long unsigned int)
Hai Shalomfdcde762020-04-02 11:19:20 -07007765 drv->send_frame_cookies[0]);
7766 os_memmove(&drv->send_frame_cookies[0],
7767 &drv->send_frame_cookies[1],
7768 (MAX_SEND_FRAME_COOKIES - 1) *
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007769 sizeof(u64));
Hai Shalomfdcde762020-04-02 11:19:20 -07007770 drv->num_send_frame_cookies--;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007771 }
Hai Shalomfdcde762020-04-02 11:19:20 -07007772 drv->send_frame_cookies[drv->num_send_frame_cookies] = cookie;
7773 drv->num_send_frame_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007774 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007775
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007776fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777 nlmsg_free(msg);
7778 return ret;
7779}
7780
7781
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007782static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7783 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007784 unsigned int wait_time,
7785 const u8 *dst, const u8 *src,
7786 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007787 const u8 *data, size_t data_len,
7788 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007790 struct wpa_driver_nl80211_data *drv = bss->drv;
7791 int ret = -1;
7792 u8 *buf;
7793 struct ieee80211_hdr *hdr;
Hai Shalomfdcde762020-04-02 11:19:20 -07007794 int offchanok = 1;
7795
Hai Shalom4fbc08f2020-05-18 12:37:00 -07007796 if (is_ap_interface(drv->nlmode) && (int) freq == bss->freq &&
7797 bss->beacon_set)
Hai Shalomfdcde762020-04-02 11:19:20 -07007798 offchanok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799
7800 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Hai Shalomfdcde762020-04-02 11:19:20 -07007801 "freq=%u MHz wait=%d ms no_cck=%d offchanok=%d)",
7802 drv->ifindex, freq, wait_time, no_cck, offchanok);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803
7804 buf = os_zalloc(24 + data_len);
7805 if (buf == NULL)
7806 return ret;
7807 os_memcpy(buf + 24, data, data_len);
7808 hdr = (struct ieee80211_hdr *) buf;
7809 hdr->frame_control =
7810 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7811 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7812 os_memcpy(hdr->addr2, src, ETH_ALEN);
7813 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7814
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007815 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7816 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7817 MAC2STR(src));
7818 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7819 } else {
7820 os_memset(bss->rand_addr, 0, ETH_ALEN);
7821 }
7822
Dmitry Shmidt56052862013-10-04 10:23:25 -07007823 if (is_ap_interface(drv->nlmode) &&
7824 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7825 (int) freq == bss->freq || drv->device_ap_sme ||
7826 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007827 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07007828 0, freq, no_cck, offchanok,
7829 wait_time, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007830 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007831 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007832 24 + data_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07007833 1, no_cck, 0, offchanok, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007834
7835 os_free(buf);
7836 return ret;
7837}
7838
7839
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007840static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007841{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007842 struct wpa_driver_nl80211_data *drv = bss->drv;
7843 struct nl_msg *msg;
7844 int ret;
7845
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08007846 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007847 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007848 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007849 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007850 nlmsg_free(msg);
7851 return;
7852 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007853
7854 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007855 if (ret)
7856 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7857 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858}
7859
7860
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007861static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7862{
7863 struct i802_bss *bss = priv;
7864 struct wpa_driver_nl80211_data *drv = bss->drv;
7865 unsigned int i;
7866 u64 cookie;
7867
7868 /* Cancel the last pending TX cookie */
Hai Shalomfdcde762020-04-02 11:19:20 -07007869 nl80211_frame_wait_cancel(bss, drv->send_frame_cookie);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007870
7871 /*
7872 * Cancel the other pending TX cookies, if any. This is needed since
7873 * the driver may keep a list of all pending offchannel TX operations
7874 * and free up the radio only once they have expired or cancelled.
7875 */
Hai Shalomfdcde762020-04-02 11:19:20 -07007876 for (i = drv->num_send_frame_cookies; i > 0; i--) {
7877 cookie = drv->send_frame_cookies[i - 1];
7878 if (cookie != drv->send_frame_cookie)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007879 nl80211_frame_wait_cancel(bss, cookie);
7880 }
Hai Shalomfdcde762020-04-02 11:19:20 -07007881 drv->num_send_frame_cookies = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007882}
7883
7884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007885static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7886 unsigned int duration)
7887{
7888 struct i802_bss *bss = priv;
7889 struct wpa_driver_nl80211_data *drv = bss->drv;
7890 struct nl_msg *msg;
7891 int ret;
7892 u64 cookie;
7893
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007894 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7895 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7896 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7897 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007898 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900
7901 cookie = 0;
7902 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7903 if (ret == 0) {
7904 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7905 "0x%llx for freq=%u MHz duration=%u",
7906 (long long unsigned int) cookie, freq, duration);
7907 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007908 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007909 return 0;
7910 }
7911 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7912 "(freq=%d duration=%u): %d (%s)",
7913 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007914 return -1;
7915}
7916
7917
7918static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7919{
7920 struct i802_bss *bss = priv;
7921 struct wpa_driver_nl80211_data *drv = bss->drv;
7922 struct nl_msg *msg;
7923 int ret;
7924
7925 if (!drv->pending_remain_on_chan) {
7926 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7927 "to cancel");
7928 return -1;
7929 }
7930
7931 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7932 "0x%llx",
7933 (long long unsigned int) drv->remain_on_chan_cookie);
7934
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007935 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7936 if (!msg ||
7937 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7938 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007939 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007941
7942 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7943 if (ret == 0)
7944 return 0;
7945 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7946 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007947 return -1;
7948}
7949
7950
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007951static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007952{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007953 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007955 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007956 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007957 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7958 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007959 /*
7960 * Do not disable Probe Request reporting that was
7961 * enabled in nl80211_setup_ap().
7962 */
7963 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7964 "Probe Request reporting nl_preq=%p while "
7965 "in AP mode", bss->nl_preq);
7966 } else if (bss->nl_preq) {
7967 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7968 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007969 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007970 }
7971 return 0;
7972 }
7973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007974 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007975 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007976 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007977 return 0;
7978 }
7979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007980 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7981 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007982 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007983 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7984 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007987 (WLAN_FC_TYPE_MGMT << 2) |
7988 (WLAN_FC_STYPE_PROBE_REQ << 4),
Hai Shalome21d4e82020-04-29 16:34:06 -07007989 NULL, 0, false) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007990 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007991
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007992 nl80211_register_eloop_read(&bss->nl_preq,
7993 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007994 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007995
7996 return 0;
7997
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007998 out_err:
7999 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008000 return -1;
8001}
8002
8003
8004static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8005 int ifindex, int disabled)
8006{
8007 struct nl_msg *msg;
8008 struct nlattr *bands, *band;
8009 int ret;
8010
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008011 wpa_printf(MSG_DEBUG,
8012 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
8013 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
8014 "no NL80211_TXRATE_LEGACY constraint");
8015
8016 msg = nl80211_ifindex_msg(drv, ifindex, 0,
8017 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008018 if (!msg)
8019 return -1;
8020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008021 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8022 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008023 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008024
8025 /*
8026 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8027 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8028 * rates. All 5 GHz rates are left enabled.
8029 */
8030 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008031 if (!band ||
8032 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
8033 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
8034 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008035 nla_nest_end(msg, band);
8036
8037 nla_nest_end(msg, bands);
8038
8039 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008040 if (ret) {
8041 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8042 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008043 } else
8044 drv->disabled_11b_rates = disabled;
8045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008046 return ret;
8047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008048fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008049 nlmsg_free(msg);
8050 return -1;
8051}
8052
8053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008054static int wpa_driver_nl80211_deinit_ap(void *priv)
8055{
8056 struct i802_bss *bss = priv;
8057 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008058 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08008060 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008061 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008062
8063 /*
8064 * If the P2P GO interface was dynamically added, then it is
8065 * possible that the interface change to station is not possible.
8066 */
8067 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
8068 return 0;
8069
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008070 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008071}
8072
8073
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008074static int wpa_driver_nl80211_stop_ap(void *priv)
8075{
8076 struct i802_bss *bss = priv;
8077 struct wpa_driver_nl80211_data *drv = bss->drv;
8078 if (!is_ap_interface(drv->nlmode))
8079 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08008080 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008081 bss->beacon_set = 0;
8082 return 0;
8083}
8084
8085
Dmitry Shmidt04949592012-07-19 12:16:46 -07008086static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8087{
8088 struct i802_bss *bss = priv;
8089 struct wpa_driver_nl80211_data *drv = bss->drv;
8090 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8091 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008092
8093 /*
8094 * If the P2P Client interface was dynamically added, then it is
8095 * possible that the interface change to station is not possible.
8096 */
8097 if (bss->if_dynamic)
8098 return 0;
8099
Dmitry Shmidt04949592012-07-19 12:16:46 -07008100 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8101}
8102
8103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008104static void wpa_driver_nl80211_resume(void *priv)
8105{
8106 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008107 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008108
8109 if (i802_set_iface_flags(bss, 1))
8110 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008111
8112 if (is_p2p_net_interface(nlmode))
8113 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008114}
8115
8116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008117static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8118{
8119 struct i802_bss *bss = priv;
8120 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008121 struct nl_msg *msg;
8122 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008123
8124 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8125 "hysteresis=%d", threshold, hysteresis);
8126
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008127 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
8128 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
8129 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
8130 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
8131 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008132 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008133 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008134 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008135
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008136 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008137}
8138
8139
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008140static int get_channel_width(struct nl_msg *msg, void *arg)
8141{
8142 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8143 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8144 struct wpa_signal_info *sig_change = arg;
8145
8146 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8147 genlmsg_attrlen(gnlh, 0), NULL);
8148
8149 sig_change->center_frq1 = -1;
8150 sig_change->center_frq2 = -1;
8151 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
8152
8153 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
8154 sig_change->chanwidth = convert2width(
8155 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
8156 if (tb[NL80211_ATTR_CENTER_FREQ1])
8157 sig_change->center_frq1 =
8158 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
8159 if (tb[NL80211_ATTR_CENTER_FREQ2])
8160 sig_change->center_frq2 =
8161 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
8162 }
8163
8164 return NL_SKIP;
8165}
8166
8167
8168static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
8169 struct wpa_signal_info *sig)
8170{
8171 struct nl_msg *msg;
8172
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008173 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008174 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008175}
8176
8177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008178static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8179{
8180 struct i802_bss *bss = priv;
8181 struct wpa_driver_nl80211_data *drv = bss->drv;
8182 int res;
8183
8184 os_memset(si, 0, sizeof(*si));
8185 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008186 if (res) {
8187 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
8188 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
8189 return res;
8190 si->current_signal = 0;
8191 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008192
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008193 res = nl80211_get_channel_width(drv, si);
8194 if (res != 0)
8195 return res;
8196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008197 return nl80211_get_link_noise(drv, si);
8198}
8199
8200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008201static int nl80211_set_param(void *priv, const char *param)
8202{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008203 struct i802_bss *bss = priv;
8204 struct wpa_driver_nl80211_data *drv = bss->drv;
8205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008206 if (param == NULL)
8207 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08008208 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008209
8210#ifdef CONFIG_P2P
8211 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008212 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8213 "interface");
8214 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8215 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8216 }
8217#endif /* CONFIG_P2P */
8218
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008219 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008220 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008221
8222 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008223 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008224 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008225 }
8226
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07008227 if (os_strstr(param, "force_bss_selection=1"))
8228 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
8229
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08008230 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08008231 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
8232 drv->test_use_roc_tx = 1;
8233 }
8234
Hai Shalomb755a2a2020-04-23 21:49:02 -07008235 if (os_strstr(param, "control_port=0")) {
Hai Shalomfdcde762020-04-02 11:19:20 -07008236 drv->capa.flags &= ~WPA_DRIVER_FLAGS_CONTROL_PORT;
Hai Shalomb755a2a2020-04-23 21:49:02 -07008237 drv->capa.flags2 &= ~WPA_DRIVER_FLAGS2_CONTROL_PORT_RX;
8238 }
8239
8240 if (os_strstr(param, "control_port_ap=1"))
8241 drv->control_port_ap = 1;
Hai Shalomfdcde762020-04-02 11:19:20 -07008242
8243 if (os_strstr(param, "full_ap_client_state=0"))
8244 drv->capa.flags &= ~WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
8245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008246 return 0;
8247}
8248
8249
Dmitry Shmidte4663042016-04-04 10:07:49 -07008250static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251{
8252 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008253 struct netlink_config *cfg;
8254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008255 global = os_zalloc(sizeof(*global));
8256 if (global == NULL)
8257 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07008258 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008259 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008260 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008261 global->if_add_ifindex = -1;
8262
8263 cfg = os_zalloc(sizeof(*cfg));
8264 if (cfg == NULL)
8265 goto err;
8266
8267 cfg->ctx = global;
8268 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8269 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8270 global->netlink = netlink_init(cfg);
8271 if (global->netlink == NULL) {
8272 os_free(cfg);
8273 goto err;
8274 }
8275
8276 if (wpa_driver_nl80211_init_nl_global(global) < 0)
8277 goto err;
8278
8279 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8280 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008281 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
8282 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008283 goto err;
8284 }
8285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008286 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008287
8288err:
8289 nl80211_global_deinit(global);
8290 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008291}
8292
8293
8294static void nl80211_global_deinit(void *priv)
8295{
8296 struct nl80211_global *global = priv;
8297 if (global == NULL)
8298 return;
8299 if (!dl_list_empty(&global->interfaces)) {
8300 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8301 "nl80211_global_deinit",
8302 dl_list_len(&global->interfaces));
8303 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008304
8305 if (global->netlink)
8306 netlink_deinit(global->netlink);
8307
8308 nl_destroy_handles(&global->nl);
8309
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008310 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -07008311 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008312
8313 nl_cb_put(global->nl_cb);
8314
8315 if (global->ioctl_sock >= 0)
8316 close(global->ioctl_sock);
8317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008318 os_free(global);
8319}
8320
8321
8322static const char * nl80211_get_radio_name(void *priv)
8323{
8324 struct i802_bss *bss = priv;
8325 struct wpa_driver_nl80211_data *drv = bss->drv;
8326 return drv->phyname;
8327}
8328
8329
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008330static int nl80211_pmkid(struct i802_bss *bss, int cmd,
8331 struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008332{
8333 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008334 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
Jouni Malinen75ecf522011-06-27 15:19:46 -07008335
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008336 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008337 (params->pmkid &&
8338 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
8339 (params->bssid &&
8340 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
8341 (params->ssid_len &&
8342 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
8343 (params->fils_cache_id &&
8344 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
8345 params->fils_cache_id)) ||
Hai Shalomfdcde762020-04-02 11:19:20 -07008346 (params->pmk_lifetime &&
8347 nla_put_u32(msg, NL80211_ATTR_PMK_LIFETIME,
8348 params->pmk_lifetime)) ||
8349 (params->pmk_reauth_threshold &&
8350 nla_put_u8(msg, NL80211_ATTR_PMK_REAUTH_THRESHOLD,
8351 params->pmk_reauth_threshold)) ||
Hai Shalom021b0b52019-04-10 11:17:58 -07008352 (cmd != NL80211_CMD_DEL_PMKSA &&
8353 params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008354 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Hai Shalom74f70d42019-02-11 14:42:39 -08008355 nl80211_nlmsg_clear(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008356 nlmsg_free(msg);
8357 return -ENOBUFS;
8358 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008359
Hai Shalom74f70d42019-02-11 14:42:39 -08008360 return send_and_recv_msgs(bss->drv, msg, NULL, (void *) -1);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008361}
8362
8363
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008364static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008365{
8366 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008367 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008368
8369 if (params->bssid)
8370 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
8371 MAC2STR(params->bssid));
8372 else if (params->fils_cache_id && params->ssid_len) {
8373 wpa_printf(MSG_DEBUG,
8374 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
8375 params->fils_cache_id[0], params->fils_cache_id[1],
8376 wpa_ssid_txt(params->ssid, params->ssid_len));
8377 }
8378
Roshan Pius3a1667e2018-07-03 15:17:14 -07008379 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
8380 if (ret < 0) {
8381 wpa_printf(MSG_DEBUG,
8382 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
8383 ret, strerror(-ret));
8384 }
8385
8386 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008387}
8388
8389
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008390static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008391{
8392 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008393 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008394
8395 if (params->bssid)
8396 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
8397 MAC2STR(params->bssid));
8398 else if (params->fils_cache_id && params->ssid_len) {
8399 wpa_printf(MSG_DEBUG,
8400 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
8401 params->fils_cache_id[0], params->fils_cache_id[1],
8402 wpa_ssid_txt(params->ssid, params->ssid_len));
8403 }
8404
Roshan Pius3a1667e2018-07-03 15:17:14 -07008405 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
8406 if (ret < 0) {
8407 wpa_printf(MSG_DEBUG,
8408 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
8409 ret, strerror(-ret));
8410 }
8411
8412 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008413}
8414
8415
8416static int nl80211_flush_pmkid(void *priv)
8417{
8418 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008419 struct nl_msg *msg;
8420
Jouni Malinen75ecf522011-06-27 15:19:46 -07008421 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008422 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
8423 if (!msg)
8424 return -ENOBUFS;
8425 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008426}
8427
8428
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008429static void clean_survey_results(struct survey_results *survey_results)
8430{
8431 struct freq_survey *survey, *tmp;
8432
8433 if (dl_list_empty(&survey_results->survey_list))
8434 return;
8435
8436 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
8437 struct freq_survey, list) {
8438 dl_list_del(&survey->list);
8439 os_free(survey);
8440 }
8441}
8442
8443
8444static void add_survey(struct nlattr **sinfo, u32 ifidx,
8445 struct dl_list *survey_list)
8446{
8447 struct freq_survey *survey;
8448
8449 survey = os_zalloc(sizeof(struct freq_survey));
8450 if (!survey)
8451 return;
8452
8453 survey->ifidx = ifidx;
8454 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8455 survey->filled = 0;
8456
8457 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
8458 survey->nf = (int8_t)
8459 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
8460 survey->filled |= SURVEY_HAS_NF;
8461 }
8462
8463 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
8464 survey->channel_time =
8465 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
8466 survey->filled |= SURVEY_HAS_CHAN_TIME;
8467 }
8468
8469 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
8470 survey->channel_time_busy =
8471 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
8472 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
8473 }
8474
8475 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
8476 survey->channel_time_rx =
8477 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
8478 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
8479 }
8480
8481 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
8482 survey->channel_time_tx =
8483 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
8484 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
8485 }
8486
8487 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)",
8488 survey->freq,
8489 survey->nf,
8490 (unsigned long int) survey->channel_time,
8491 (unsigned long int) survey->channel_time_busy,
8492 (unsigned long int) survey->channel_time_tx,
8493 (unsigned long int) survey->channel_time_rx,
8494 survey->filled);
8495
8496 dl_list_add_tail(survey_list, &survey->list);
8497}
8498
8499
8500static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
8501 unsigned int freq_filter)
8502{
8503 if (!freq_filter)
8504 return 1;
8505
8506 return freq_filter == surveyed_freq;
8507}
8508
8509
8510static int survey_handler(struct nl_msg *msg, void *arg)
8511{
8512 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8513 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8514 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
8515 struct survey_results *survey_results;
8516 u32 surveyed_freq = 0;
8517 u32 ifidx;
8518
8519 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
8520 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
8521 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
8522 };
8523
8524 survey_results = (struct survey_results *) arg;
8525
8526 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8527 genlmsg_attrlen(gnlh, 0), NULL);
8528
Dmitry Shmidt97672262014-02-03 13:02:54 -08008529 if (!tb[NL80211_ATTR_IFINDEX])
8530 return NL_SKIP;
8531
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008532 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
8533
8534 if (!tb[NL80211_ATTR_SURVEY_INFO])
8535 return NL_SKIP;
8536
8537 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
8538 tb[NL80211_ATTR_SURVEY_INFO],
8539 survey_policy))
8540 return NL_SKIP;
8541
8542 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
8543 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
8544 return NL_SKIP;
8545 }
8546
8547 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8548
8549 if (!check_survey_ok(sinfo, surveyed_freq,
8550 survey_results->freq_filter))
8551 return NL_SKIP;
8552
8553 if (survey_results->freq_filter &&
8554 survey_results->freq_filter != surveyed_freq) {
8555 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
8556 surveyed_freq);
8557 return NL_SKIP;
8558 }
8559
8560 add_survey(sinfo, ifidx, &survey_results->survey_list);
8561
8562 return NL_SKIP;
8563}
8564
8565
8566static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
8567{
8568 struct i802_bss *bss = priv;
8569 struct wpa_driver_nl80211_data *drv = bss->drv;
8570 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008571 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008572 union wpa_event_data data;
8573 struct survey_results *survey_results;
8574
8575 os_memset(&data, 0, sizeof(data));
8576 survey_results = &data.survey_results;
8577
8578 dl_list_init(&survey_results->survey_list);
8579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008580 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008581 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008582 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008583
8584 if (freq)
8585 data.survey_results.freq_filter = freq;
8586
8587 do {
8588 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
8589 err = send_and_recv_msgs(drv, msg, survey_handler,
8590 survey_results);
8591 } while (err > 0);
8592
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008593 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008594 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008595 else
8596 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008597
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008598 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008599 return err;
8600}
8601
8602
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008603static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
8604 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008605 const u8 *replay_ctr)
8606{
8607 struct i802_bss *bss = priv;
8608 struct wpa_driver_nl80211_data *drv = bss->drv;
8609 struct nlattr *replay_nested;
8610 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008611 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008612
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008613 if (!drv->set_rekey_offload)
8614 return;
8615
8616 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008617 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
8618 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008619 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008620 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008621 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8622 replay_ctr)) {
8623 nl80211_nlmsg_clear(msg);
8624 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008625 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008626 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008627
8628 nla_nest_end(msg, replay_nested);
8629
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008630 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
8631 if (ret == -EOPNOTSUPP) {
8632 wpa_printf(MSG_DEBUG,
8633 "nl80211: Driver does not support rekey offload");
8634 drv->set_rekey_offload = 0;
8635 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008636}
8637
8638
8639static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8640 const u8 *addr, int qos)
8641{
8642 /* send data frame to poll STA and check whether
8643 * this frame is ACKed */
8644 struct {
8645 struct ieee80211_hdr hdr;
8646 u16 qos_ctl;
8647 } STRUCT_PACKED nulldata;
8648 size_t size;
8649
8650 /* Send data frame to poll STA and check whether this frame is ACKed */
8651
8652 os_memset(&nulldata, 0, sizeof(nulldata));
8653
8654 if (qos) {
8655 nulldata.hdr.frame_control =
8656 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8657 WLAN_FC_STYPE_QOS_NULL);
8658 size = sizeof(nulldata);
8659 } else {
8660 nulldata.hdr.frame_control =
8661 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8662 WLAN_FC_STYPE_NULLFUNC);
8663 size = sizeof(struct ieee80211_hdr);
8664 }
8665
8666 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8667 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8668 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8669 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8670
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008671 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -07008672 0, 0, NULL, 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008673 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8674 "send poll frame");
8675}
8676
8677static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8678 int qos)
8679{
8680 struct i802_bss *bss = priv;
8681 struct wpa_driver_nl80211_data *drv = bss->drv;
8682 struct nl_msg *msg;
Hai Shalom5f92bc92019-04-18 11:54:11 -07008683 u64 cookie;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008684 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008685
8686 if (!drv->poll_command_supported) {
8687 nl80211_send_null_frame(bss, own_addr, addr, qos);
8688 return;
8689 }
8690
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008691 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8692 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8693 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008694 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008695 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008696
Hai Shalom5f92bc92019-04-18 11:54:11 -07008697 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008698 if (ret < 0) {
8699 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8700 MACSTR " failed: ret=%d (%s)",
8701 MAC2STR(addr), ret, strerror(-ret));
Hai Shalom5f92bc92019-04-18 11:54:11 -07008702 } else {
8703 wpa_printf(MSG_DEBUG,
8704 "nl80211: Client probe request addr=" MACSTR
8705 " cookie=%llu", MAC2STR(addr),
8706 (long long unsigned int) cookie);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008707 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008708}
8709
8710
8711static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8712{
8713 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008714 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008715
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008716 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8717 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8718 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8719 nlmsg_free(msg);
8720 return -ENOBUFS;
8721 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07008722
8723 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8724 if (ret < 0) {
8725 wpa_printf(MSG_DEBUG,
8726 "nl80211: Setting PS state %s failed: %d (%s)",
8727 enabled ? "enabled" : "disabled",
8728 ret, strerror(-ret));
8729 }
8730 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008731}
8732
8733
8734static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8735 int ctwindow)
8736{
8737 struct i802_bss *bss = priv;
8738
8739 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8740 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8741
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008742 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008743#ifdef ANDROID_P2P
8744 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008745#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008746 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008747#endif /* ANDROID_P2P */
8748 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008749
8750 if (legacy_ps == -1)
8751 return 0;
8752 if (legacy_ps != 0 && legacy_ps != 1)
8753 return -1; /* Not yet supported */
8754
8755 return nl80211_set_power_save(bss, legacy_ps);
8756}
8757
8758
Dmitry Shmidt051af732013-10-22 13:52:46 -07008759static int nl80211_start_radar_detection(void *priv,
8760 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008761{
8762 struct i802_bss *bss = priv;
8763 struct wpa_driver_nl80211_data *drv = bss->drv;
8764 struct nl_msg *msg;
8765 int ret;
8766
Hai Shalom81f62d82019-07-22 12:10:00 -07008767 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)",
8768 freq->freq, freq->ht_enabled, freq->vht_enabled, freq->he_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07008769 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8770
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008771 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8772 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8773 "detection");
8774 return -1;
8775 }
8776
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008777 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8778 nl80211_put_freq_params(msg, freq) < 0) {
8779 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008780 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008781 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008782
8783 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8784 if (ret == 0)
8785 return 0;
8786 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8787 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008788 return -1;
8789}
8790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008791#ifdef CONFIG_TDLS
8792
8793static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8794 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008795 u32 peer_capab, int initiator, const u8 *buf,
8796 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008797{
8798 struct i802_bss *bss = priv;
8799 struct wpa_driver_nl80211_data *drv = bss->drv;
8800 struct nl_msg *msg;
8801
8802 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8803 return -EOPNOTSUPP;
8804
8805 if (!dst)
8806 return -EINVAL;
8807
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008808 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8809 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8810 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8811 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8812 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8813 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008814 if (peer_capab) {
8815 /*
8816 * The internal enum tdls_peer_capability definition is
8817 * currently identical with the nl80211 enum
8818 * nl80211_tdls_peer_capability, so no conversion is needed
8819 * here.
8820 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008821 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8822 peer_capab))
8823 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008824 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008825 if ((initiator &&
8826 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8827 nla_put(msg, NL80211_ATTR_IE, len, buf))
8828 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008829
8830 return send_and_recv_msgs(drv, msg, NULL, NULL);
8831
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008832fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008833 nlmsg_free(msg);
8834 return -ENOBUFS;
8835}
8836
8837
8838static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8839{
8840 struct i802_bss *bss = priv;
8841 struct wpa_driver_nl80211_data *drv = bss->drv;
8842 struct nl_msg *msg;
8843 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -08008844 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008845
8846 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8847 return -EOPNOTSUPP;
8848
8849 switch (oper) {
8850 case TDLS_DISCOVERY_REQ:
8851 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8852 break;
8853 case TDLS_SETUP:
8854 nl80211_oper = NL80211_TDLS_SETUP;
8855 break;
8856 case TDLS_TEARDOWN:
8857 nl80211_oper = NL80211_TDLS_TEARDOWN;
8858 break;
8859 case TDLS_ENABLE_LINK:
8860 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8861 break;
8862 case TDLS_DISABLE_LINK:
8863 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8864 break;
8865 case TDLS_ENABLE:
8866 return 0;
8867 case TDLS_DISABLE:
8868 return 0;
8869 default:
8870 return -EINVAL;
8871 }
8872
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008873 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8874 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8875 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8876 nlmsg_free(msg);
8877 return -ENOBUFS;
8878 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008879
Paul Stewart092955c2017-02-06 09:13:09 -08008880 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8881 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8882 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8883 strerror(-res));
8884 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008885}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008886
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008887
8888static int
8889nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8890 const struct hostapd_freq_params *params)
8891{
8892 struct i802_bss *bss = priv;
8893 struct wpa_driver_nl80211_data *drv = bss->drv;
8894 struct nl_msg *msg;
8895 int ret = -ENOBUFS;
8896
8897 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8898 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8899 return -EOPNOTSUPP;
8900
8901 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8902 " oper_class=%u freq=%u",
8903 MAC2STR(addr), oper_class, params->freq);
8904 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8905 if (!msg ||
8906 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8907 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8908 (ret = nl80211_put_freq_params(msg, params))) {
8909 nlmsg_free(msg);
8910 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8911 return ret;
8912 }
8913
8914 return send_and_recv_msgs(drv, msg, NULL, NULL);
8915}
8916
8917
8918static int
8919nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8920{
8921 struct i802_bss *bss = priv;
8922 struct wpa_driver_nl80211_data *drv = bss->drv;
8923 struct nl_msg *msg;
8924
8925 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8926 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8927 return -EOPNOTSUPP;
8928
8929 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8930 MAC2STR(addr));
8931 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8932 if (!msg ||
8933 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8934 nlmsg_free(msg);
8935 wpa_printf(MSG_DEBUG,
8936 "nl80211: Could not build TDLS cancel chan switch");
8937 return -ENOBUFS;
8938 }
8939
8940 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008941}
8942
8943#endif /* CONFIG TDLS */
8944
8945
Hai Shalomfdcde762020-04-02 11:19:20 -07008946static int driver_nl80211_set_key(void *priv,
8947 struct wpa_driver_set_key_params *params)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008948{
8949 struct i802_bss *bss = priv;
Hai Shalomfdcde762020-04-02 11:19:20 -07008950
8951 return wpa_driver_nl80211_set_key(bss, params);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008952}
8953
8954
8955static int driver_nl80211_scan2(void *priv,
8956 struct wpa_driver_scan_params *params)
8957{
8958 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008959#ifdef CONFIG_DRIVER_NL80211_QCA
8960 struct wpa_driver_nl80211_data *drv = bss->drv;
8961
8962 /*
8963 * Do a vendor specific scan if possible. If only_new_results is
8964 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8965 * cannot be achieved through a vendor scan. The below condition may
8966 * need to be modified if new scan flags are added in the future whose
8967 * functionality can only be achieved through a normal scan.
8968 */
8969 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8970 return wpa_driver_nl80211_vendor_scan(bss, params);
8971#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008972 return wpa_driver_nl80211_scan(bss, params);
8973}
8974
8975
8976static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
Hai Shalom81f62d82019-07-22 12:10:00 -07008977 u16 reason_code)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008978{
8979 struct i802_bss *bss = priv;
8980 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8981}
8982
8983
8984static int driver_nl80211_authenticate(void *priv,
8985 struct wpa_driver_auth_params *params)
8986{
8987 struct i802_bss *bss = priv;
8988 return wpa_driver_nl80211_authenticate(bss, params);
8989}
8990
8991
8992static void driver_nl80211_deinit(void *priv)
8993{
8994 struct i802_bss *bss = priv;
8995 wpa_driver_nl80211_deinit(bss);
8996}
8997
8998
8999static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9000 const char *ifname)
9001{
9002 struct i802_bss *bss = priv;
9003 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9004}
9005
9006
9007static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009008 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009009 unsigned int freq,
Hai Shalomfdcde762020-04-02 11:19:20 -07009010 const u16 *csa_offs, size_t csa_offs_len,
9011 int no_encrypt, unsigned int wait)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009012{
9013 struct i802_bss *bss = priv;
9014 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Hai Shalomfdcde762020-04-02 11:19:20 -07009015 freq, 0, 0, wait, csa_offs,
9016 csa_offs_len, no_encrypt);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009017}
9018
9019
9020static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9021{
9022 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009023 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009024}
9025
9026
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009027static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9028 const char *ifname, int vlan_id)
9029{
9030 struct i802_bss *bss = priv;
9031 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9032}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009033
9034
9035static int driver_nl80211_read_sta_data(void *priv,
9036 struct hostap_sta_driver_data *data,
9037 const u8 *addr)
9038{
9039 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08009040
9041 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009042 return i802_read_sta_data(bss, data, addr);
9043}
9044
9045
9046static int driver_nl80211_send_action(void *priv, unsigned int freq,
9047 unsigned int wait_time,
9048 const u8 *dst, const u8 *src,
9049 const u8 *bssid,
9050 const u8 *data, size_t data_len,
9051 int no_cck)
9052{
9053 struct i802_bss *bss = priv;
9054 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9055 bssid, data, data_len, no_cck);
9056}
9057
9058
9059static int driver_nl80211_probe_req_report(void *priv, int report)
9060{
9061 struct i802_bss *bss = priv;
9062 return wpa_driver_nl80211_probe_req_report(bss, report);
9063}
9064
9065
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009066static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9067 const u8 *ies, size_t ies_len)
9068{
9069 int ret;
9070 struct nl_msg *msg;
9071 struct i802_bss *bss = priv;
9072 struct wpa_driver_nl80211_data *drv = bss->drv;
9073 u16 mdid = WPA_GET_LE16(md);
9074
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009075 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009076 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
9077 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
9078 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
9079 nlmsg_free(msg);
9080 return -ENOBUFS;
9081 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009082
9083 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9084 if (ret) {
9085 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9086 "err=%d (%s)", ret, strerror(-ret));
9087 }
9088
9089 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009090}
9091
9092
Hai Shalom81f62d82019-07-22 12:10:00 -07009093static int nl80211_update_dh_ie(void *priv, const u8 *peer_mac,
9094 u16 reason_code, const u8 *ie, size_t ie_len)
9095{
9096 int ret;
9097 struct nl_msg *msg;
9098 struct i802_bss *bss = priv;
9099 struct wpa_driver_nl80211_data *drv = bss->drv;
9100
9101 wpa_printf(MSG_DEBUG, "nl80211: Updating DH IE peer: " MACSTR
9102 " reason %u", MAC2STR(peer_mac), reason_code);
9103 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UPDATE_OWE_INFO)) ||
9104 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer_mac) ||
9105 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, reason_code) ||
9106 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie))) {
9107 nlmsg_free(msg);
9108 return -ENOBUFS;
9109 }
9110
9111 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9112 if (ret) {
9113 wpa_printf(MSG_DEBUG,
9114 "nl80211: update_dh_ie failed err=%d (%s)",
9115 ret, strerror(-ret));
9116 }
9117
9118 return ret;
9119}
9120
9121
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07009122static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009123{
9124 struct i802_bss *bss = priv;
9125 struct wpa_driver_nl80211_data *drv = bss->drv;
9126
9127 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
9128 return NULL;
9129
9130 return bss->addr;
9131}
9132
9133
Dmitry Shmidt56052862013-10-04 10:23:25 -07009134static const char * scan_state_str(enum scan_states scan_state)
9135{
9136 switch (scan_state) {
9137 case NO_SCAN:
9138 return "NO_SCAN";
9139 case SCAN_REQUESTED:
9140 return "SCAN_REQUESTED";
9141 case SCAN_STARTED:
9142 return "SCAN_STARTED";
9143 case SCAN_COMPLETED:
9144 return "SCAN_COMPLETED";
9145 case SCAN_ABORTED:
9146 return "SCAN_ABORTED";
9147 case SCHED_SCAN_STARTED:
9148 return "SCHED_SCAN_STARTED";
9149 case SCHED_SCAN_STOPPED:
9150 return "SCHED_SCAN_STOPPED";
9151 case SCHED_SCAN_RESULTS:
9152 return "SCHED_SCAN_RESULTS";
9153 }
9154
9155 return "??";
9156}
9157
9158
9159static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
9160{
9161 struct i802_bss *bss = priv;
9162 struct wpa_driver_nl80211_data *drv = bss->drv;
9163 int res;
9164 char *pos, *end;
Hai Shalom74f70d42019-02-11 14:42:39 -08009165 struct nl_msg *msg;
9166 char alpha2[3] = { 0, 0, 0 };
Dmitry Shmidt56052862013-10-04 10:23:25 -07009167
9168 pos = buf;
9169 end = buf + buflen;
9170
9171 res = os_snprintf(pos, end - pos,
9172 "ifindex=%d\n"
9173 "ifname=%s\n"
9174 "brname=%s\n"
9175 "addr=" MACSTR "\n"
9176 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -07009177 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009178 bss->ifindex,
9179 bss->ifname,
9180 bss->brname,
9181 MAC2STR(bss->addr),
9182 bss->freq,
9183 bss->beacon_set ? "beacon_set=1\n" : "",
9184 bss->added_if_into_bridge ?
9185 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -07009186 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009187 bss->added_bridge ? "added_bridge=1\n" : "",
9188 bss->in_deinit ? "in_deinit=1\n" : "",
9189 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009190 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009191 return pos - buf;
9192 pos += res;
9193
9194 if (bss->wdev_id_set) {
9195 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
9196 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009197 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009198 return pos - buf;
9199 pos += res;
9200 }
9201
9202 res = os_snprintf(pos, end - pos,
9203 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009204 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07009205 "drv_ifindex=%d\n"
9206 "operstate=%d\n"
9207 "scan_state=%s\n"
9208 "auth_bssid=" MACSTR "\n"
9209 "auth_attempt_bssid=" MACSTR "\n"
9210 "bssid=" MACSTR "\n"
9211 "prev_bssid=" MACSTR "\n"
9212 "associated=%d\n"
9213 "assoc_freq=%u\n"
9214 "monitor_sock=%d\n"
9215 "monitor_ifidx=%d\n"
9216 "monitor_refcount=%d\n"
9217 "last_mgmt_freq=%u\n"
9218 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009219 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009220 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009221 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07009222 drv->ifindex,
9223 drv->operstate,
9224 scan_state_str(drv->scan_state),
9225 MAC2STR(drv->auth_bssid),
9226 MAC2STR(drv->auth_attempt_bssid),
9227 MAC2STR(drv->bssid),
9228 MAC2STR(drv->prev_bssid),
9229 drv->associated,
9230 drv->assoc_freq,
9231 drv->monitor_sock,
9232 drv->monitor_ifidx,
9233 drv->monitor_refcount,
9234 drv->last_mgmt_freq,
9235 drv->eapol_tx_sock,
9236 drv->ignore_if_down_event ?
9237 "ignore_if_down_event=1\n" : "",
9238 drv->scan_complete_events ?
9239 "scan_complete_events=1\n" : "",
9240 drv->disabled_11b_rates ?
9241 "disabled_11b_rates=1\n" : "",
9242 drv->pending_remain_on_chan ?
9243 "pending_remain_on_chan=1\n" : "",
9244 drv->in_interface_list ? "in_interface_list=1\n" : "",
9245 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
9246 drv->poll_command_supported ?
9247 "poll_command_supported=1\n" : "",
9248 drv->data_tx_status ? "data_tx_status=1\n" : "",
9249 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
9250 drv->retry_auth ? "retry_auth=1\n" : "",
9251 drv->use_monitor ? "use_monitor=1\n" : "",
9252 drv->ignore_next_local_disconnect ?
9253 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07009254 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009255 "ignore_next_local_deauth=1\n" : "");
9256 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009257 return pos - buf;
9258 pos += res;
9259
9260 if (drv->has_capability) {
9261 res = os_snprintf(pos, end - pos,
9262 "capa.key_mgmt=0x%x\n"
9263 "capa.enc=0x%x\n"
9264 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009265 "capa.flags=0x%llx\n"
9266 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07009267 "capa.max_scan_ssids=%d\n"
9268 "capa.max_sched_scan_ssids=%d\n"
9269 "capa.sched_scan_supported=%d\n"
9270 "capa.max_match_sets=%d\n"
9271 "capa.max_remain_on_chan=%u\n"
9272 "capa.max_stations=%u\n"
9273 "capa.probe_resp_offloads=0x%x\n"
9274 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009275 "capa.num_multichan_concurrent=%u\n"
9276 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009277 "capa.mac_addr_rand_scan_supported=%d\n"
9278 "capa.conc_capab=%u\n"
9279 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009280 "capa.max_conc_chan_5_0=%u\n"
9281 "capa.max_sched_scan_plans=%u\n"
9282 "capa.max_sched_scan_plan_interval=%u\n"
9283 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07009284 drv->capa.key_mgmt,
9285 drv->capa.enc,
9286 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009287 (unsigned long long) drv->capa.flags,
9288 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009289 drv->capa.max_scan_ssids,
9290 drv->capa.max_sched_scan_ssids,
9291 drv->capa.sched_scan_supported,
9292 drv->capa.max_match_sets,
9293 drv->capa.max_remain_on_chan,
9294 drv->capa.max_stations,
9295 drv->capa.probe_resp_offloads,
9296 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009297 drv->capa.num_multichan_concurrent,
9298 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009299 drv->capa.mac_addr_rand_scan_supported,
9300 drv->capa.conc_capab,
9301 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009302 drv->capa.max_conc_chan_5_0,
9303 drv->capa.max_sched_scan_plans,
9304 drv->capa.max_sched_scan_plan_interval,
9305 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009306 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07009307 return pos - buf;
9308 pos += res;
9309 }
9310
Hai Shalom74f70d42019-02-11 14:42:39 -08009311 msg = nlmsg_alloc();
9312 if (msg &&
9313 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG) &&
9314 nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx) == 0) {
9315 if (send_and_recv_msgs(drv, msg, nl80211_get_country,
9316 alpha2) == 0 &&
9317 alpha2[0]) {
9318 res = os_snprintf(pos, end - pos, "country=%s\n",
9319 alpha2);
9320 if (os_snprintf_error(end - pos, res))
9321 return pos - buf;
9322 pos += res;
9323 }
9324 } else {
9325 nlmsg_free(msg);
9326 }
9327
Dmitry Shmidt56052862013-10-04 10:23:25 -07009328 return pos - buf;
9329}
9330
9331
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009332static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
9333{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009334 if ((settings->head &&
9335 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
9336 settings->head_len, settings->head)) ||
9337 (settings->tail &&
9338 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
9339 settings->tail_len, settings->tail)) ||
9340 (settings->beacon_ies &&
9341 nla_put(msg, NL80211_ATTR_IE,
9342 settings->beacon_ies_len, settings->beacon_ies)) ||
9343 (settings->proberesp_ies &&
9344 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
9345 settings->proberesp_ies_len, settings->proberesp_ies)) ||
9346 (settings->assocresp_ies &&
9347 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
9348 settings->assocresp_ies_len, settings->assocresp_ies)) ||
9349 (settings->probe_resp &&
9350 nla_put(msg, NL80211_ATTR_PROBE_RESP,
9351 settings->probe_resp_len, settings->probe_resp)))
9352 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009353
9354 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009355}
9356
9357
9358static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
9359{
9360 struct nl_msg *msg;
9361 struct i802_bss *bss = priv;
9362 struct wpa_driver_nl80211_data *drv = bss->drv;
9363 struct nlattr *beacon_csa;
9364 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009365 int csa_off_len = 0;
9366 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009367
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009368 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 -08009369 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009370 settings->freq_params.freq, settings->freq_params.bandwidth,
9371 settings->freq_params.center_freq1,
9372 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009373
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009374 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009375 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
9376 return -EOPNOTSUPP;
9377 }
9378
Roshan Pius3a1667e2018-07-03 15:17:14 -07009379 if (drv->nlmode != NL80211_IFTYPE_AP &&
9380 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
9381 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009382 return -EOPNOTSUPP;
9383
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009384 /*
9385 * Remove empty counters, assuming Probe Response and Beacon frame
9386 * counters match. This implementation assumes that there are only two
9387 * counters.
9388 */
9389 if (settings->counter_offset_beacon[0] &&
9390 !settings->counter_offset_beacon[1]) {
9391 csa_off_len = 1;
9392 } else if (settings->counter_offset_beacon[1] &&
9393 !settings->counter_offset_beacon[0]) {
9394 csa_off_len = 1;
9395 settings->counter_offset_beacon[0] =
9396 settings->counter_offset_beacon[1];
9397 settings->counter_offset_presp[0] =
9398 settings->counter_offset_presp[1];
9399 } else if (settings->counter_offset_beacon[1] &&
9400 settings->counter_offset_beacon[0]) {
9401 csa_off_len = 2;
9402 } else {
9403 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
9404 return -EINVAL;
9405 }
9406
9407 /* Check CSA counters validity */
9408 if (drv->capa.max_csa_counters &&
9409 csa_off_len > drv->capa.max_csa_counters) {
9410 wpa_printf(MSG_ERROR,
9411 "nl80211: Too many CSA counters provided");
9412 return -EINVAL;
9413 }
9414
9415 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009416 return -EINVAL;
9417
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009418 for (i = 0; i < csa_off_len; i++) {
9419 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
9420 u16 csa_c_off_presp = settings->counter_offset_presp[i];
9421
9422 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
9423 (settings->beacon_csa.tail[csa_c_off_bcn] !=
9424 settings->cs_count))
9425 return -EINVAL;
9426
9427 if (settings->beacon_csa.probe_resp &&
9428 ((settings->beacon_csa.probe_resp_len <=
9429 csa_c_off_presp) ||
9430 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
9431 settings->cs_count)))
9432 return -EINVAL;
9433 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009434
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009435 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
9436 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
9437 settings->cs_count) ||
9438 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
9439 (settings->block_tx &&
9440 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009441 goto error;
9442
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009443 /* beacon_after params */
9444 ret = set_beacon_data(msg, &settings->beacon_after);
9445 if (ret)
9446 goto error;
9447
9448 /* beacon_csa params */
9449 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
9450 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009451 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009452
9453 ret = set_beacon_data(msg, &settings->beacon_csa);
9454 if (ret)
9455 goto error;
9456
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009457 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
9458 csa_off_len * sizeof(u16),
9459 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009460 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009461 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
9462 csa_off_len * sizeof(u16),
9463 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009464 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009465
9466 nla_nest_end(msg, beacon_csa);
9467 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9468 if (ret) {
9469 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
9470 ret, strerror(-ret));
9471 }
9472 return ret;
9473
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009474fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009475 ret = -ENOBUFS;
9476error:
9477 nlmsg_free(msg);
9478 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
9479 return ret;
9480}
9481
9482
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009483static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
9484 u8 user_priority, u16 admitted_time)
9485{
9486 struct i802_bss *bss = priv;
9487 struct wpa_driver_nl80211_data *drv = bss->drv;
9488 struct nl_msg *msg;
9489 int ret;
9490
9491 wpa_printf(MSG_DEBUG,
9492 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
9493 tsid, admitted_time, user_priority);
9494
9495 if (!is_sta_interface(drv->nlmode))
9496 return -ENOTSUP;
9497
9498 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
9499 if (!msg ||
9500 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9501 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9502 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
9503 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
9504 nlmsg_free(msg);
9505 return -ENOBUFS;
9506 }
9507
9508 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9509 if (ret)
9510 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
9511 ret, strerror(-ret));
9512 return ret;
9513}
9514
9515
9516static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
9517{
9518 struct i802_bss *bss = priv;
9519 struct wpa_driver_nl80211_data *drv = bss->drv;
9520 struct nl_msg *msg;
9521 int ret;
9522
9523 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
9524
9525 if (!is_sta_interface(drv->nlmode))
9526 return -ENOTSUP;
9527
9528 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
9529 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9530 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
9531 nlmsg_free(msg);
9532 return -ENOBUFS;
9533 }
9534
9535 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9536 if (ret)
9537 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
9538 ret, strerror(-ret));
9539 return ret;
9540}
9541
9542
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009543#ifdef CONFIG_TESTING_OPTIONS
9544static int cmd_reply_handler(struct nl_msg *msg, void *arg)
9545{
9546 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9547 struct wpabuf *buf = arg;
9548
9549 if (!buf)
9550 return NL_SKIP;
9551
9552 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
9553 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
9554 return NL_SKIP;
9555 }
9556
9557 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
9558 genlmsg_attrlen(gnlh, 0));
9559
9560 return NL_SKIP;
9561}
9562#endif /* CONFIG_TESTING_OPTIONS */
9563
9564
9565static int vendor_reply_handler(struct nl_msg *msg, void *arg)
9566{
9567 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9568 struct nlattr *nl_vendor_reply, *nl;
9569 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9570 struct wpabuf *buf = arg;
9571 int rem;
9572
9573 if (!buf)
9574 return NL_SKIP;
9575
9576 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9577 genlmsg_attrlen(gnlh, 0), NULL);
9578 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
9579
9580 if (!nl_vendor_reply)
9581 return NL_SKIP;
9582
9583 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
9584 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
9585 return NL_SKIP;
9586 }
9587
9588 nla_for_each_nested(nl, nl_vendor_reply, rem) {
9589 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
9590 }
9591
9592 return NL_SKIP;
9593}
9594
9595
9596static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
9597 unsigned int subcmd, const u8 *data,
9598 size_t data_len, struct wpabuf *buf)
9599{
9600 struct i802_bss *bss = priv;
9601 struct wpa_driver_nl80211_data *drv = bss->drv;
9602 struct nl_msg *msg;
9603 int ret;
9604
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009605#ifdef CONFIG_TESTING_OPTIONS
9606 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009607 msg = nlmsg_alloc();
9608 if (!msg)
9609 return -ENOMEM;
9610
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009611 nl80211_cmd(drv, msg, 0, subcmd);
9612 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
9613 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009614 goto fail;
Hai Shalomb755a2a2020-04-23 21:49:02 -07009615 /* This test vendor_cmd can be used with nl80211 commands that
9616 * need the connect nl_sock, so use the owner-setting variant
9617 * of send_and_recv_msgs(). */
9618 ret = send_and_recv_msgs_owner(drv, msg,
9619 get_connect_handle(bss), 0,
9620 cmd_reply_handler, buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009621 if (ret)
9622 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
9623 ret);
9624 return ret;
9625 }
9626#endif /* CONFIG_TESTING_OPTIONS */
9627
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009628 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
9629 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
9630 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
9631 (data &&
9632 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
9633 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009634
9635 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
9636 if (ret)
9637 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
9638 ret);
9639 return ret;
9640
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009641fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009642 nlmsg_free(msg);
9643 return -ENOBUFS;
9644}
9645
9646
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009647static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
9648 u8 qos_map_set_len)
9649{
9650 struct i802_bss *bss = priv;
9651 struct wpa_driver_nl80211_data *drv = bss->drv;
9652 struct nl_msg *msg;
9653 int ret;
9654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009655 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
9656 qos_map_set, qos_map_set_len);
9657
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009658 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
9659 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
9660 nlmsg_free(msg);
9661 return -ENOBUFS;
9662 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009663
9664 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9665 if (ret)
9666 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
9667
9668 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009669}
9670
9671
Hai Shalomfdcde762020-04-02 11:19:20 -07009672static int get_wowlan_handler(struct nl_msg *msg, void *arg)
9673{
9674 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9675 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9676 int *wowlan_enabled = arg;
9677
9678 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9679 genlmsg_attrlen(gnlh, 0), NULL);
9680
9681 *wowlan_enabled = !!tb[NL80211_ATTR_WOWLAN_TRIGGERS];
9682
9683 return NL_SKIP;
9684}
9685
9686
9687static int nl80211_get_wowlan(void *priv)
9688{
9689 struct i802_bss *bss = priv;
9690 struct wpa_driver_nl80211_data *drv = bss->drv;
9691 struct nl_msg *msg;
9692 int wowlan_enabled;
9693 int ret;
9694
9695 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status");
9696
9697 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_WOWLAN);
9698
9699 ret = send_and_recv_msgs(drv, msg, get_wowlan_handler, &wowlan_enabled);
9700 if (ret) {
9701 wpa_printf(MSG_DEBUG, "nl80211: Getting wowlan status failed");
9702 return 0;
9703 }
9704
9705 wpa_printf(MSG_DEBUG, "nl80211: wowlan is %s",
9706 wowlan_enabled ? "enabled" : "disabled");
9707
9708 return wowlan_enabled;
9709}
9710
9711
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009712static int nl80211_set_wowlan(void *priv,
9713 const struct wowlan_triggers *triggers)
9714{
9715 struct i802_bss *bss = priv;
9716 struct wpa_driver_nl80211_data *drv = bss->drv;
9717 struct nl_msg *msg;
9718 struct nlattr *wowlan_triggers;
9719 int ret;
9720
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009721 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
9722
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009723 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009724 !(wowlan_triggers = nla_nest_start(msg,
9725 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
9726 (triggers->any &&
9727 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9728 (triggers->disconnect &&
9729 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9730 (triggers->magic_pkt &&
9731 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9732 (triggers->gtk_rekey_failure &&
9733 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9734 (triggers->eap_identity_req &&
9735 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9736 (triggers->four_way_handshake &&
9737 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9738 (triggers->rfkill_release &&
9739 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9740 nlmsg_free(msg);
9741 return -ENOBUFS;
9742 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009743
9744 nla_nest_end(msg, wowlan_triggers);
9745
9746 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9747 if (ret)
9748 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9749
9750 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009751}
9752
9753
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009754#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009755static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9756{
9757 struct i802_bss *bss = priv;
9758 struct wpa_driver_nl80211_data *drv = bss->drv;
9759 struct nl_msg *msg;
9760 struct nlattr *params;
9761
9762 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9763
9764 if (!drv->roaming_vendor_cmd_avail) {
9765 wpa_printf(MSG_DEBUG,
9766 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9767 return -1;
9768 }
9769
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009770 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9771 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9772 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9773 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9774 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9775 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9776 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9777 QCA_ROAMING_NOT_ALLOWED) ||
9778 (bssid &&
9779 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9780 nlmsg_free(msg);
9781 return -1;
9782 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009783 nla_nest_end(msg, params);
9784
9785 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009786}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009787
9788
Roshan Pius3a1667e2018-07-03 15:17:14 -07009789static int nl80211_disable_fils(void *priv, int disable)
9790{
9791 struct i802_bss *bss = priv;
9792 struct wpa_driver_nl80211_data *drv = bss->drv;
9793 struct nl_msg *msg;
9794 struct nlattr *params;
9795
9796 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9797
9798 if (!drv->set_wifi_conf_vendor_cmd_avail)
9799 return -1;
9800
9801 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9802 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9803 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9804 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9805 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9806 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9807 disable)) {
9808 nlmsg_free(msg);
9809 return -1;
9810 }
9811 nla_nest_end(msg, params);
9812
9813 return send_and_recv_msgs(drv, msg, NULL, NULL);
9814}
9815
9816
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009817/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9818#define WPA_SUPPLICANT_CLIENT_ID 1
9819
9820static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9821 const u8 *bssid)
9822{
9823 struct i802_bss *bss = priv;
9824 struct wpa_driver_nl80211_data *drv = bss->drv;
9825 struct nl_msg *msg;
9826 struct nlattr *params, *nlbssids, *attr;
9827 unsigned int i;
9828
9829 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9830 num_bssid);
9831
9832 if (!drv->roam_vendor_cmd_avail)
9833 return -1;
9834
9835 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9836 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9837 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9838 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9839 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9840 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
Hai Shalomc3565922019-10-28 11:58:20 -07009841 QCA_WLAN_VENDOR_ROAMING_SUBCMD_SET_BLACKLIST_BSSID) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009842 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9843 WPA_SUPPLICANT_CLIENT_ID) ||
9844 nla_put_u32(msg,
9845 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9846 num_bssid))
9847 goto fail;
9848
9849 nlbssids = nla_nest_start(
9850 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9851 if (!nlbssids)
9852 goto fail;
9853
9854 for (i = 0; i < num_bssid; i++) {
9855 attr = nla_nest_start(msg, i);
9856 if (!attr)
9857 goto fail;
9858 if (nla_put(msg,
9859 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9860 ETH_ALEN, &bssid[i * ETH_ALEN]))
9861 goto fail;
9862 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9863 MAC2STR(&bssid[i * ETH_ALEN]));
9864 nla_nest_end(msg, attr);
9865 }
9866 nla_nest_end(msg, nlbssids);
9867 nla_nest_end(msg, params);
9868
9869 return send_and_recv_msgs(drv, msg, NULL, NULL);
9870
9871fail:
9872 nlmsg_free(msg);
9873 return -1;
9874}
9875
Hai Shalomc3565922019-10-28 11:58:20 -07009876
9877static int nl80211_add_sta_node(void *priv, const u8 *addr, u16 auth_alg)
9878{
9879 struct i802_bss *bss = priv;
9880 struct wpa_driver_nl80211_data *drv = bss->drv;
9881 struct nl_msg *msg;
9882 struct nlattr *params;
9883
9884 if (!drv->add_sta_node_vendor_cmd_avail)
9885 return -EOPNOTSUPP;
9886
9887 wpa_printf(MSG_DEBUG, "nl80211: Add STA node");
9888
9889 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9890 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9891 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9892 QCA_NL80211_VENDOR_SUBCMD_ADD_STA_NODE) ||
9893 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9894 (addr &&
9895 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_MAC_ADDR, ETH_ALEN,
9896 addr)) ||
9897 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ADD_STA_NODE_AUTH_ALGO,
9898 auth_alg)) {
9899 nlmsg_free(msg);
9900 wpa_printf(MSG_ERROR,
9901 "%s: err in adding vendor_cmd and vendor_data",
9902 __func__);
9903 return -1;
9904 }
9905 nla_nest_end(msg, params);
9906
9907 return send_and_recv_msgs(drv, msg, NULL, NULL);
9908}
9909
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009910#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009911
9912
9913static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9914{
9915 struct i802_bss *bss = priv;
9916 struct wpa_driver_nl80211_data *drv = bss->drv;
9917 int new_addr = addr != NULL;
9918
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009919 if (TEST_FAIL())
9920 return -1;
9921
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009922 if (!addr)
9923 addr = drv->perm_addr;
9924
9925 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9926 return -1;
9927
9928 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9929 {
9930 wpa_printf(MSG_DEBUG,
9931 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9932 bss->ifname, MAC2STR(addr));
9933 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9934 1) < 0) {
9935 wpa_printf(MSG_DEBUG,
9936 "nl80211: Could not restore interface UP after failed set_mac_addr");
9937 }
9938 return -1;
9939 }
9940
9941 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9942 bss->ifname, MAC2STR(addr));
9943 drv->addr_changed = new_addr;
9944 os_memcpy(bss->addr, addr, ETH_ALEN);
9945
9946 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9947 {
9948 wpa_printf(MSG_DEBUG,
9949 "nl80211: Could not restore interface UP after set_mac_addr");
9950 }
9951
9952 return 0;
9953}
9954
9955
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009956#ifdef CONFIG_MESH
9957
9958static int wpa_driver_nl80211_init_mesh(void *priv)
9959{
9960 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9961 wpa_printf(MSG_INFO,
9962 "nl80211: Failed to set interface into mesh mode");
9963 return -1;
9964 }
9965 return 0;
9966}
9967
9968
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009969static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9970 size_t mesh_id_len)
9971{
9972 if (mesh_id) {
Hai Shalom74f70d42019-02-11 14:42:39 -08009973 wpa_printf(MSG_DEBUG, " * Mesh ID (SSID)=%s",
9974 wpa_ssid_txt(mesh_id, mesh_id_len));
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009975 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9976 }
9977
9978 return 0;
9979}
9980
9981
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009982static int nl80211_put_mesh_config(struct nl_msg *msg,
9983 struct wpa_driver_mesh_bss_params *params)
9984{
9985 struct nlattr *container;
9986
9987 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9988 if (!container)
9989 return -1;
9990
9991 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009992 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9993 params->auto_plinks)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009994 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9995 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009996 params->max_peer_links)) ||
9997 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9998 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9999 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070010000 return -1;
10001
10002 /*
10003 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
10004 * the timer could disconnect stations even in that case.
10005 */
10006 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
10007 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
10008 params->peer_link_timeout)) {
10009 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
10010 return -1;
10011 }
10012
10013 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
10014 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
10015 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
10016 return -1;
10017 }
10018
10019 nla_nest_end(msg, container);
10020
10021 return 0;
10022}
10023
10024
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010025static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010026 struct wpa_driver_mesh_join_params *params)
10027{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010028 struct wpa_driver_nl80211_data *drv = bss->drv;
10029 struct nl_msg *msg;
10030 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080010031 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010032
10033 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
10034 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -080010035 if (!msg ||
10036 nl80211_put_freq_params(msg, &params->freq) ||
10037 nl80211_put_basic_rates(msg, params->basic_rates) ||
10038 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010039 nl80211_put_beacon_int(msg, params->beacon_int) ||
10040 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010041 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010042
10043 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
10044
10045 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
10046 if (!container)
10047 goto fail;
10048
10049 if (params->ies) {
10050 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
10051 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
10052 params->ies))
10053 goto fail;
10054 }
10055 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
10056 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
10057 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
10058 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
10059 goto fail;
10060 }
10061 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
10062 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
10063 goto fail;
10064 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
10065 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
10066 goto fail;
10067 nla_nest_end(msg, container);
10068
Dmitry Shmidtd13095b2016-08-22 14:02:19 -070010069 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
10070 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
10071 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
10072 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010073 goto fail;
10074
Hai Shalomb755a2a2020-04-23 21:49:02 -070010075 ret = send_and_recv_msgs_owner(drv, msg, get_connect_handle(bss), 1,
10076 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010077 msg = NULL;
10078 if (ret) {
10079 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
10080 ret, strerror(-ret));
10081 goto fail;
10082 }
10083 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010084 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010085 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
10086
10087fail:
10088 nlmsg_free(msg);
10089 return ret;
10090}
10091
10092
Dmitry Shmidt7f656022015-02-25 14:36:37 -080010093static int
10094wpa_driver_nl80211_join_mesh(void *priv,
10095 struct wpa_driver_mesh_join_params *params)
10096{
10097 struct i802_bss *bss = priv;
10098 int ret, timeout;
10099
10100 timeout = params->conf.peer_link_timeout;
10101
10102 /* Disable kernel inactivity timer */
10103 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
10104 params->conf.peer_link_timeout = 0;
10105
10106 ret = nl80211_join_mesh(bss, params);
10107 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
10108 wpa_printf(MSG_DEBUG,
10109 "nl80211: Mesh join retry for peer_link_timeout");
10110 /*
10111 * Old kernel does not support setting
10112 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
10113 * into future from peer_link_timeout.
10114 */
10115 params->conf.peer_link_timeout = timeout + 60;
10116 ret = nl80211_join_mesh(priv, params);
10117 }
10118
10119 params->conf.peer_link_timeout = timeout;
10120 return ret;
10121}
10122
10123
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010124static int wpa_driver_nl80211_leave_mesh(void *priv)
10125{
10126 struct i802_bss *bss = priv;
10127 struct wpa_driver_nl80211_data *drv = bss->drv;
10128 struct nl_msg *msg;
10129 int ret;
10130
10131 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
10132 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
Hai Shalomb755a2a2020-04-23 21:49:02 -070010133 ret = send_and_recv_msgs_owner(drv, msg, get_connect_handle(bss), 0,
10134 NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010135 if (ret) {
10136 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
10137 ret, strerror(-ret));
10138 } else {
10139 wpa_printf(MSG_DEBUG,
10140 "nl80211: mesh leave request send successfully");
10141 }
10142
10143 if (wpa_driver_nl80211_set_mode(drv->first_bss,
10144 NL80211_IFTYPE_STATION)) {
10145 wpa_printf(MSG_INFO,
10146 "nl80211: Failed to set interface into station mode");
10147 }
10148 return ret;
10149}
10150
Hai Shalom81f62d82019-07-22 12:10:00 -070010151
10152static int nl80211_probe_mesh_link(void *priv, const u8 *addr, const u8 *eth,
10153 size_t len)
10154{
10155 struct i802_bss *bss = priv;
10156 struct wpa_driver_nl80211_data *drv = bss->drv;
10157 struct nl_msg *msg;
10158 int ret;
10159
10160 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_PROBE_MESH_LINK);
10161 if (!msg ||
10162 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10163 nla_put(msg, NL80211_ATTR_FRAME, len, eth)) {
10164 nlmsg_free(msg);
10165 return -ENOBUFS;
10166 }
10167
10168 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10169 if (ret) {
10170 wpa_printf(MSG_DEBUG, "nl80211: mesh link probe to " MACSTR
10171 " failed: ret=%d (%s)",
10172 MAC2STR(addr), ret, strerror(-ret));
10173 } else {
10174 wpa_printf(MSG_DEBUG, "nl80211: Mesh link to " MACSTR
10175 " probed successfully", MAC2STR(addr));
10176 }
10177
10178 return ret;
10179}
10180
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010181#endif /* CONFIG_MESH */
10182
10183
10184static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
10185 const u8 *ipaddr, int prefixlen,
10186 const u8 *addr)
10187{
10188#ifdef CONFIG_LIBNL3_ROUTE
10189 struct i802_bss *bss = priv;
10190 struct wpa_driver_nl80211_data *drv = bss->drv;
10191 struct rtnl_neigh *rn;
10192 struct nl_addr *nl_ipaddr = NULL;
10193 struct nl_addr *nl_lladdr = NULL;
10194 int family, addrsize;
10195 int res;
10196
10197 if (!ipaddr || prefixlen == 0 || !addr)
10198 return -EINVAL;
10199
10200 if (bss->br_ifindex == 0) {
10201 wpa_printf(MSG_DEBUG,
10202 "nl80211: bridge must be set before adding an ip neigh to it");
10203 return -1;
10204 }
10205
10206 if (!drv->rtnl_sk) {
10207 wpa_printf(MSG_DEBUG,
10208 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
10209 return -1;
10210 }
10211
10212 if (version == 4) {
10213 family = AF_INET;
10214 addrsize = 4;
10215 } else if (version == 6) {
10216 family = AF_INET6;
10217 addrsize = 16;
10218 } else {
10219 return -EINVAL;
10220 }
10221
10222 rn = rtnl_neigh_alloc();
10223 if (rn == NULL)
10224 return -ENOMEM;
10225
10226 /* set the destination ip address for neigh */
10227 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
10228 if (nl_ipaddr == NULL) {
10229 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
10230 res = -ENOMEM;
10231 goto errout;
10232 }
10233 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
10234 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
10235 if (res) {
10236 wpa_printf(MSG_DEBUG,
10237 "nl80211: neigh set destination addr failed");
10238 goto errout;
10239 }
10240
10241 /* set the corresponding lladdr for neigh */
10242 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
10243 if (nl_lladdr == NULL) {
10244 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
10245 res = -ENOMEM;
10246 goto errout;
10247 }
10248 rtnl_neigh_set_lladdr(rn, nl_lladdr);
10249
10250 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
10251 rtnl_neigh_set_state(rn, NUD_PERMANENT);
10252
10253 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
10254 if (res) {
10255 wpa_printf(MSG_DEBUG,
10256 "nl80211: Adding bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010257 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010258 }
10259errout:
10260 if (nl_lladdr)
10261 nl_addr_put(nl_lladdr);
10262 if (nl_ipaddr)
10263 nl_addr_put(nl_ipaddr);
10264 if (rn)
10265 rtnl_neigh_put(rn);
10266 return res;
10267#else /* CONFIG_LIBNL3_ROUTE */
10268 return -1;
10269#endif /* CONFIG_LIBNL3_ROUTE */
10270}
10271
10272
10273static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
10274 const u8 *ipaddr)
10275{
10276#ifdef CONFIG_LIBNL3_ROUTE
10277 struct i802_bss *bss = priv;
10278 struct wpa_driver_nl80211_data *drv = bss->drv;
10279 struct rtnl_neigh *rn;
10280 struct nl_addr *nl_ipaddr;
10281 int family, addrsize;
10282 int res;
10283
10284 if (!ipaddr)
10285 return -EINVAL;
10286
10287 if (version == 4) {
10288 family = AF_INET;
10289 addrsize = 4;
10290 } else if (version == 6) {
10291 family = AF_INET6;
10292 addrsize = 16;
10293 } else {
10294 return -EINVAL;
10295 }
10296
10297 if (bss->br_ifindex == 0) {
10298 wpa_printf(MSG_DEBUG,
10299 "nl80211: bridge must be set to delete an ip neigh");
10300 return -1;
10301 }
10302
10303 if (!drv->rtnl_sk) {
10304 wpa_printf(MSG_DEBUG,
10305 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
10306 return -1;
10307 }
10308
10309 rn = rtnl_neigh_alloc();
10310 if (rn == NULL)
10311 return -ENOMEM;
10312
10313 /* set the destination ip address for neigh */
10314 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
10315 if (nl_ipaddr == NULL) {
10316 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
10317 res = -ENOMEM;
10318 goto errout;
10319 }
10320 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
10321 if (res) {
10322 wpa_printf(MSG_DEBUG,
10323 "nl80211: neigh set destination addr failed");
10324 goto errout;
10325 }
10326
10327 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
10328
10329 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
10330 if (res) {
10331 wpa_printf(MSG_DEBUG,
10332 "nl80211: Deleting bridge ip neigh failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010333 nl_geterror(res));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010334 }
10335errout:
10336 if (nl_ipaddr)
10337 nl_addr_put(nl_ipaddr);
10338 if (rn)
10339 rtnl_neigh_put(rn);
10340 return res;
10341#else /* CONFIG_LIBNL3_ROUTE */
10342 return -1;
10343#endif /* CONFIG_LIBNL3_ROUTE */
10344}
10345
10346
10347static int linux_write_system_file(const char *path, unsigned int val)
10348{
10349 char buf[50];
10350 int fd, len;
10351
10352 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
10353 if (os_snprintf_error(sizeof(buf), len))
10354 return -1;
10355
10356 fd = open(path, O_WRONLY);
10357 if (fd < 0)
10358 return -1;
10359
10360 if (write(fd, buf, len) < 0) {
10361 wpa_printf(MSG_DEBUG,
10362 "nl80211: Failed to write Linux system file: %s with the value of %d",
10363 path, val);
10364 close(fd);
10365 return -1;
10366 }
10367 close(fd);
10368
10369 return 0;
10370}
10371
10372
10373static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
10374{
10375 switch (attr) {
10376 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -070010377 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010378 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
10379 return "hairpin_mode";
10380 }
10381
10382 return NULL;
10383}
10384
10385
10386static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
10387 unsigned int val)
10388{
10389 struct i802_bss *bss = priv;
10390 char path[128];
10391 const char *attr_txt;
10392
10393 attr_txt = drv_br_port_attr_str(attr);
10394 if (attr_txt == NULL)
10395 return -EINVAL;
10396
10397 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
10398 bss->ifname, attr_txt);
10399
10400 if (linux_write_system_file(path, val))
10401 return -1;
10402
10403 return 0;
10404}
10405
10406
10407static const char * drv_br_net_param_str(enum drv_br_net_param param)
10408{
10409 switch (param) {
10410 case DRV_BR_NET_PARAM_GARP_ACCEPT:
10411 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -070010412 default:
10413 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010414 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010415}
10416
10417
10418static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
10419 unsigned int val)
10420{
10421 struct i802_bss *bss = priv;
10422 char path[128];
10423 const char *param_txt;
10424 int ip_version = 4;
10425
Dmitry Shmidt83474442015-04-15 13:47:09 -070010426 if (param == DRV_BR_MULTICAST_SNOOPING) {
10427 os_snprintf(path, sizeof(path),
10428 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
10429 bss->brname);
10430 goto set_val;
10431 }
10432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010433 param_txt = drv_br_net_param_str(param);
10434 if (param_txt == NULL)
10435 return -EINVAL;
10436
10437 switch (param) {
10438 case DRV_BR_NET_PARAM_GARP_ACCEPT:
10439 ip_version = 4;
10440 break;
10441 default:
10442 return -EINVAL;
10443 }
10444
10445 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
10446 ip_version, bss->brname, param_txt);
10447
Dmitry Shmidt83474442015-04-15 13:47:09 -070010448set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010449 if (linux_write_system_file(path, val))
10450 return -1;
10451
10452 return 0;
10453}
10454
10455
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010456#ifdef CONFIG_DRIVER_NL80211_QCA
10457
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010458static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
10459{
10460 switch (hw_mode) {
10461 case HOSTAPD_MODE_IEEE80211B:
10462 return QCA_ACS_MODE_IEEE80211B;
10463 case HOSTAPD_MODE_IEEE80211G:
10464 return QCA_ACS_MODE_IEEE80211G;
10465 case HOSTAPD_MODE_IEEE80211A:
10466 return QCA_ACS_MODE_IEEE80211A;
10467 case HOSTAPD_MODE_IEEE80211AD:
10468 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -070010469 case HOSTAPD_MODE_IEEE80211ANY:
10470 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010471 default:
10472 return -1;
10473 }
10474}
10475
10476
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010477static int add_acs_ch_list(struct nl_msg *msg, const int *freq_list)
10478{
10479 int num_channels = 0, num_freqs;
10480 u8 *ch_list;
10481 enum hostapd_hw_mode hw_mode;
10482 int ret = 0;
10483 int i;
10484
10485 if (!freq_list)
10486 return 0;
10487
10488 num_freqs = int_array_len(freq_list);
10489 ch_list = os_malloc(sizeof(u8) * num_freqs);
10490 if (!ch_list)
10491 return -1;
10492
10493 for (i = 0; i < num_freqs; i++) {
10494 const int freq = freq_list[i];
10495
10496 if (freq == 0)
10497 break;
10498 /* Send 2.4 GHz and 5 GHz channels with
10499 * QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST to maintain backwards
10500 * compatibility.
10501 */
10502 if (!(freq >= 2412 && freq <= 2484) &&
10503 !(freq >= 5180 && freq <= 5900))
10504 continue;
10505 hw_mode = ieee80211_freq_to_chan(freq, &ch_list[num_channels]);
10506 if (hw_mode != NUM_HOSTAPD_MODES)
10507 num_channels++;
10508 }
10509
10510 if (num_channels)
10511 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST,
10512 num_channels, ch_list);
10513
10514 os_free(ch_list);
10515 return ret;
10516}
10517
10518
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010519static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
10520{
10521 int i, len, ret;
10522 u32 *freqs;
10523
10524 if (!freq_list)
10525 return 0;
10526 len = int_array_len(freq_list);
10527 freqs = os_malloc(sizeof(u32) * len);
10528 if (!freqs)
10529 return -1;
10530 for (i = 0; i < len; i++)
10531 freqs[i] = freq_list[i];
10532 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
10533 sizeof(u32) * len, freqs);
10534 os_free(freqs);
10535 return ret;
10536}
10537
10538
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010539static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
10540{
10541 struct i802_bss *bss = priv;
10542 struct wpa_driver_nl80211_data *drv = bss->drv;
10543 struct nl_msg *msg;
10544 struct nlattr *data;
10545 int ret;
10546 int mode;
10547
10548 mode = hw_mode_to_qca_acs(params->hw_mode);
10549 if (mode < 0)
10550 return -1;
10551
10552 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10553 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10554 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10555 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
10556 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10557 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
10558 (params->ht_enabled &&
10559 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
10560 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010561 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
10562 (params->vht_enabled &&
10563 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
10564 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
10565 params->ch_width) ||
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010566 add_acs_ch_list(msg, params->freq_list) ||
Hai Shalomfdcde762020-04-02 11:19:20 -070010567 add_acs_freq_list(msg, params->freq_list) ||
10568 (params->edmg_enabled &&
10569 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_EDMG_ENABLED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010570 nlmsg_free(msg);
10571 return -ENOBUFS;
10572 }
10573 nla_nest_end(msg, data);
10574
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010575 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -070010576 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d EDMG: %d",
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010577 params->hw_mode, params->ht_enabled, params->ht40_enabled,
Hai Shalomfdcde762020-04-02 11:19:20 -070010578 params->vht_enabled, params->ch_width, params->edmg_enabled);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070010579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010580 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10581 if (ret) {
10582 wpa_printf(MSG_DEBUG,
10583 "nl80211: Failed to invoke driver ACS function: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010584 strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010585 }
10586 return ret;
10587}
10588
10589
Ravi Joshie6ccb162015-07-16 17:45:41 -070010590static int nl80211_set_band(void *priv, enum set_band band)
10591{
10592 struct i802_bss *bss = priv;
10593 struct wpa_driver_nl80211_data *drv = bss->drv;
10594 struct nl_msg *msg;
10595 struct nlattr *data;
10596 int ret;
10597 enum qca_set_band qca_band;
10598
10599 if (!drv->setband_vendor_cmd_avail)
10600 return -1;
10601
10602 switch (band) {
10603 case WPA_SETBAND_AUTO:
10604 qca_band = QCA_SETBAND_AUTO;
10605 break;
10606 case WPA_SETBAND_5G:
10607 qca_band = QCA_SETBAND_5G;
10608 break;
10609 case WPA_SETBAND_2G:
10610 qca_band = QCA_SETBAND_2G;
10611 break;
10612 default:
10613 return -1;
10614 }
10615
10616 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10617 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10618 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10619 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
10620 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10621 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
10622 nlmsg_free(msg);
10623 return -ENOBUFS;
10624 }
10625 nla_nest_end(msg, data);
10626
10627 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10628 if (ret) {
10629 wpa_printf(MSG_DEBUG,
10630 "nl80211: Driver setband function failed: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -070010631 strerror(-ret));
Ravi Joshie6ccb162015-07-16 17:45:41 -070010632 }
10633 return ret;
10634}
10635
10636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010637struct nl80211_pcl {
10638 unsigned int num;
10639 unsigned int *freq_list;
10640};
10641
10642static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
10643{
10644 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10645 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10646 struct nl80211_pcl *param = arg;
10647 struct nlattr *nl_vend, *attr;
10648 enum qca_iface_type iface_type;
10649 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10650 unsigned int num, max_num;
10651 u32 *freqs;
10652
10653 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10654 genlmsg_attrlen(gnlh, 0), NULL);
10655
10656 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
10657 if (!nl_vend)
10658 return NL_SKIP;
10659
10660 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10661 nla_data(nl_vend), nla_len(nl_vend), NULL);
10662
10663 attr = tb_vendor[
10664 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
10665 if (!attr) {
10666 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
10667 param->num = 0;
10668 return NL_SKIP;
10669 }
10670
10671 iface_type = (enum qca_iface_type) nla_get_u32(attr);
10672 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
10673 iface_type);
10674
10675 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
10676 if (!attr) {
10677 wpa_printf(MSG_ERROR,
10678 "nl80211: preferred_freq_list couldn't be found");
10679 param->num = 0;
10680 return NL_SKIP;
10681 }
10682
10683 /*
10684 * param->num has the maximum number of entries for which there
10685 * is room in the freq_list provided by the caller.
10686 */
10687 freqs = nla_data(attr);
10688 max_num = nla_len(attr) / sizeof(u32);
10689 if (max_num > param->num)
10690 max_num = param->num;
10691 for (num = 0; num < max_num; num++)
10692 param->freq_list[num] = freqs[num];
10693 param->num = num;
10694
10695 return NL_SKIP;
10696}
10697
10698
10699static int nl80211_get_pref_freq_list(void *priv,
10700 enum wpa_driver_if_type if_type,
10701 unsigned int *num,
10702 unsigned int *freq_list)
10703{
10704 struct i802_bss *bss = priv;
10705 struct wpa_driver_nl80211_data *drv = bss->drv;
10706 struct nl_msg *msg;
10707 int ret;
10708 unsigned int i;
10709 struct nlattr *params;
10710 struct nl80211_pcl param;
10711 enum qca_iface_type iface_type;
10712
10713 if (!drv->get_pref_freq_list)
10714 return -1;
10715
10716 switch (if_type) {
10717 case WPA_IF_STATION:
10718 iface_type = QCA_IFACE_TYPE_STA;
10719 break;
10720 case WPA_IF_AP_BSS:
10721 iface_type = QCA_IFACE_TYPE_AP;
10722 break;
10723 case WPA_IF_P2P_GO:
10724 iface_type = QCA_IFACE_TYPE_P2P_GO;
10725 break;
10726 case WPA_IF_P2P_CLIENT:
10727 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
10728 break;
10729 case WPA_IF_IBSS:
10730 iface_type = QCA_IFACE_TYPE_IBSS;
10731 break;
10732 case WPA_IF_TDLS:
10733 iface_type = QCA_IFACE_TYPE_TDLS;
10734 break;
10735 default:
10736 return -1;
10737 }
10738
10739 param.num = *num;
10740 param.freq_list = freq_list;
10741
10742 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10743 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10744 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10745 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10746 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
10747 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10748 nla_put_u32(msg,
10749 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
10750 iface_type)) {
10751 wpa_printf(MSG_ERROR,
10752 "%s: err in adding vendor_cmd and vendor_data",
10753 __func__);
10754 nlmsg_free(msg);
10755 return -1;
10756 }
10757 nla_nest_end(msg, params);
10758
10759 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
10760 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
10761 if (ret) {
10762 wpa_printf(MSG_ERROR,
10763 "%s: err in send_and_recv_msgs", __func__);
10764 return ret;
10765 }
10766
10767 *num = param.num;
10768
10769 for (i = 0; i < *num; i++) {
10770 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
10771 i, freq_list[i]);
10772 }
10773
10774 return 0;
10775}
10776
10777
10778static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
10779{
10780 struct i802_bss *bss = priv;
10781 struct wpa_driver_nl80211_data *drv = bss->drv;
10782 struct nl_msg *msg;
10783 int ret;
10784 struct nlattr *params;
10785
10786 if (!drv->set_prob_oper_freq)
10787 return -1;
10788
10789 wpa_printf(MSG_DEBUG,
10790 "nl80211: Set P2P probable operating freq %u for ifindex %d",
10791 freq, bss->ifindex);
10792
10793 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10794 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10795 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10796 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
10797 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10798 nla_put_u32(msg,
10799 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
10800 QCA_IFACE_TYPE_P2P_CLIENT) ||
10801 nla_put_u32(msg,
10802 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
10803 freq)) {
10804 wpa_printf(MSG_ERROR,
10805 "%s: err in adding vendor_cmd and vendor_data",
10806 __func__);
10807 nlmsg_free(msg);
10808 return -1;
10809 }
10810 nla_nest_end(msg, params);
10811
10812 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10813 msg = NULL;
10814 if (ret) {
10815 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
10816 __func__);
10817 return ret;
10818 }
10819 nlmsg_free(msg);
10820 return 0;
10821}
10822
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010823
10824static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
10825 unsigned int period, unsigned int interval,
10826 unsigned int count, const u8 *device_types,
10827 size_t dev_types_len,
10828 const u8 *ies, size_t ies_len)
10829{
10830 struct i802_bss *bss = priv;
10831 struct wpa_driver_nl80211_data *drv = bss->drv;
10832 struct nl_msg *msg;
10833 struct nlattr *container;
10834 int ret;
10835
10836 wpa_printf(MSG_DEBUG,
10837 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
10838 freq, period, interval, count);
10839
10840 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10841 return -1;
10842
10843 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10844 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10845 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10846 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10847 goto fail;
10848
10849 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10850 if (!container)
10851 goto fail;
10852
10853 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10854 freq) ||
10855 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10856 period) ||
10857 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10858 interval) ||
10859 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10860 count) ||
10861 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10862 dev_types_len, device_types) ||
10863 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10864 ies_len, ies))
10865 goto fail;
10866
10867 nla_nest_end(msg, container);
10868 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10869 msg = NULL;
10870 if (ret) {
10871 wpa_printf(MSG_DEBUG,
10872 "nl80211: Failed to send P2P Listen offload vendor command");
10873 goto fail;
10874 }
10875
10876 return 0;
10877
10878fail:
10879 nlmsg_free(msg);
10880 return -1;
10881}
10882
10883
10884static int nl80211_p2p_lo_stop(void *priv)
10885{
10886 struct i802_bss *bss = priv;
10887 struct wpa_driver_nl80211_data *drv = bss->drv;
10888 struct nl_msg *msg;
10889
10890 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10891
10892 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10893 return -1;
10894
10895 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10896 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10897 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10898 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10899 nlmsg_free(msg);
10900 return -1;
10901 }
10902
10903 return send_and_recv_msgs(drv, msg, NULL, NULL);
10904}
10905
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010906
10907static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10908{
10909 struct i802_bss *bss = priv;
10910 struct wpa_driver_nl80211_data *drv = bss->drv;
10911 struct nl_msg *msg;
10912 struct nlattr *params;
10913 int ret;
10914 u32 tdls_mode;
10915
10916 wpa_printf(MSG_DEBUG,
10917 "nl80211: Set TDKS mode: tdls_external_control=%d",
10918 tdls_external_control);
10919
10920 if (tdls_external_control == 1)
10921 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10922 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10923 else
10924 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10925
10926 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10927 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10928 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10929 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10930 goto fail;
10931
10932 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10933 if (!params)
10934 goto fail;
10935
10936 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10937 tdls_mode))
10938 goto fail;
10939
10940 nla_nest_end(msg, params);
10941
10942 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10943 msg = NULL;
10944 if (ret) {
10945 wpa_printf(MSG_ERROR,
10946 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10947 ret, strerror(-ret));
10948 goto fail;
10949 }
10950 return 0;
10951fail:
10952 nlmsg_free(msg);
10953 return -1;
10954}
10955
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010956
10957#ifdef CONFIG_MBO
10958
10959static enum mbo_transition_reject_reason
10960nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10961{
10962 switch (status) {
10963 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10964 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10965 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10966 return MBO_TRANSITION_REJECT_REASON_DELAY;
10967 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10968 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10969 case QCA_STATUS_REJECT_LOW_RSSI:
10970 return MBO_TRANSITION_REJECT_REASON_RSSI;
10971 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10972 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10973 case QCA_STATUS_REJECT_UNKNOWN:
10974 default:
10975 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10976 }
10977}
10978
10979
10980static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10981 struct nlattr *tb[], int num)
10982{
10983 enum qca_wlan_btm_candidate_status status;
10984 char buf[50];
10985
10986 os_memcpy(candidate->bssid,
10987 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10988 ETH_ALEN);
10989
10990 status = nla_get_u32(
10991 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10992 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10993 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10994
10995 if (candidate->is_accept)
10996 os_snprintf(buf, sizeof(buf), "Accepted");
10997 else
10998 os_snprintf(buf, sizeof(buf),
10999 "Rejected, Reject_reason: %d",
11000 candidate->reject_reason);
11001 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
11002 num, MAC2STR(candidate->bssid), buf);
11003}
11004
11005
11006static int
11007nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
11008{
11009 struct wpa_bss_candidate_info *info = arg;
11010 struct candidate_list *candidate = info->candidates;
11011 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
11012 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
11013 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
11014 static struct nla_policy policy[
11015 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
11016 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
11017 .minlen = ETH_ALEN
11018 },
11019 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
11020 .type = NLA_U32,
11021 },
11022 };
11023 struct nlattr *attr;
11024 int rem;
11025 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11026 u8 num;
11027
11028 num = info->num; /* number of candidates sent to driver */
11029 info->num = 0;
11030 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11031 genlmsg_attrlen(gnlh, 0), NULL);
11032
11033 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
11034 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
11035 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
11036 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
11037 return NL_SKIP;
11038
11039 wpa_printf(MSG_DEBUG,
11040 "nl80211: WNM Candidate list received from driver");
11041 nla_for_each_nested(attr,
11042 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
11043 rem) {
11044 if (info->num >= num ||
11045 nla_parse_nested(
11046 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
11047 attr, policy) ||
11048 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
11049 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
11050 break;
11051
11052 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
11053
11054 candidate++;
11055 info->num++;
11056 }
11057
11058 return NL_SKIP;
11059}
11060
11061
11062static struct wpa_bss_candidate_info *
11063nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
11064{
11065 struct i802_bss *bss = priv;
11066 struct wpa_driver_nl80211_data *drv = bss->drv;
11067 struct nl_msg *msg;
11068 struct nlattr *attr, *attr1, *attr2;
11069 struct wpa_bss_candidate_info *info;
11070 u8 i;
11071 int ret;
11072 u8 *pos;
11073
11074 if (!drv->fetch_bss_trans_status)
11075 return NULL;
11076
11077 info = os_zalloc(sizeof(*info));
11078 if (!info)
11079 return NULL;
11080 /* Allocate memory for number of candidates sent to driver */
11081 info->candidates = os_calloc(params->n_candidates,
11082 sizeof(*info->candidates));
11083 if (!info->candidates) {
11084 os_free(info);
11085 return NULL;
11086 }
11087
11088 /* Copy the number of candidates being sent to driver. This is used in
11089 * nl80211_get_bss_transition_status_handler() to limit the number of
11090 * candidates that can be populated in info->candidates and will be
11091 * later overwritten with the actual number of candidates received from
11092 * the driver.
11093 */
11094 info->num = params->n_candidates;
11095
11096 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11097 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11098 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11099 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
11100 goto fail;
11101
11102 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
11103 if (!attr)
11104 goto fail;
11105
11106 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
11107 params->mbo_transition_reason))
11108 goto fail;
11109
11110 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
11111 if (!attr1)
11112 goto fail;
11113
11114 wpa_printf(MSG_DEBUG,
11115 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
11116 params->mbo_transition_reason, params->n_candidates);
11117 pos = params->bssid;
11118 for (i = 0; i < params->n_candidates; i++) {
11119 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
11120 MAC2STR(pos));
11121 attr2 = nla_nest_start(msg, i);
11122 if (!attr2 ||
11123 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
11124 ETH_ALEN, pos))
11125 goto fail;
11126 pos += ETH_ALEN;
11127 nla_nest_end(msg, attr2);
11128 }
11129
11130 nla_nest_end(msg, attr1);
11131 nla_nest_end(msg, attr);
11132
11133 ret = send_and_recv_msgs(drv, msg,
11134 nl80211_get_bss_transition_status_handler,
11135 info);
11136 msg = NULL;
11137 if (ret) {
11138 wpa_printf(MSG_ERROR,
11139 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
11140 ret, strerror(-ret));
11141 goto fail;
11142 }
11143 return info;
11144
11145fail:
11146 nlmsg_free(msg);
11147 os_free(info->candidates);
11148 os_free(info);
11149 return NULL;
11150}
11151
11152
11153/**
11154 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
11155 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
11156 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
11157 * Returns: 0 on success, -1 on failure
11158 */
11159static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
11160{
11161 struct i802_bss *bss = priv;
11162 struct wpa_driver_nl80211_data *drv = bss->drv;
11163 struct nl_msg *msg;
11164 struct nlattr *attr;
11165 int ret = -1;
11166
11167 if (!drv->set_wifi_conf_vendor_cmd_avail)
11168 return -1;
11169
11170 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
11171 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
11172 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
11173 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
11174 goto fail;
11175
11176 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
11177 if (!attr)
11178 goto fail;
11179
11180 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
11181 ignore_disallow);
11182 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
11183 ignore_disallow))
11184 goto fail;
11185
11186 nla_nest_end(msg, attr);
11187
11188 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11189 msg = NULL;
11190 if (ret) {
11191 wpa_printf(MSG_ERROR,
11192 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
11193 ret, strerror(-ret));
11194 goto fail;
11195 }
11196
11197fail:
11198 nlmsg_free(msg);
11199 return ret;
11200}
11201
11202#endif /* CONFIG_MBO */
11203
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011204#endif /* CONFIG_DRIVER_NL80211_QCA */
11205
11206
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011207static int nl80211_write_to_file(const char *name, unsigned int val)
11208{
11209 int fd, len;
11210 char tmp[128];
Hai Shalomc3565922019-10-28 11:58:20 -070011211 int ret = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011212
11213 fd = open(name, O_RDWR);
11214 if (fd < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -070011215 int level;
11216 /*
11217 * Flags may not exist on older kernels, or while we're tearing
11218 * down a disappearing device.
11219 */
11220 if (errno == ENOENT) {
11221 ret = 0;
11222 level = MSG_DEBUG;
11223 } else {
11224 ret = -1;
11225 level = MSG_ERROR;
11226 }
11227 wpa_printf(level, "nl80211: Failed to open %s: %s",
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011228 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070011229 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011230 }
11231
11232 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
11233 len = write(fd, tmp, len);
Hai Shalomc3565922019-10-28 11:58:20 -070011234 if (len < 0) {
11235 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011236 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
11237 name, strerror(errno));
Hai Shalomc3565922019-10-28 11:58:20 -070011238 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011239 close(fd);
11240
Hai Shalomc3565922019-10-28 11:58:20 -070011241 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011242}
11243
11244
11245static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
11246{
11247 struct i802_bss *bss = priv;
11248 char path[128];
11249 int ret;
11250
11251 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
11252 filter_flags);
11253
11254 /* Configure filtering of unicast frame encrypted using GTK */
11255 ret = os_snprintf(path, sizeof(path),
11256 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
11257 bss->ifname);
11258 if (os_snprintf_error(sizeof(path), ret))
11259 return -1;
11260
11261 ret = nl80211_write_to_file(path,
11262 !!(filter_flags &
11263 WPA_DATA_FRAME_FILTER_FLAG_GTK));
11264 if (ret) {
11265 wpa_printf(MSG_ERROR,
11266 "nl80211: Failed to set IPv4 unicast in multicast filter");
11267 return ret;
11268 }
11269
11270 os_snprintf(path, sizeof(path),
11271 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
11272 bss->ifname);
11273 ret = nl80211_write_to_file(path,
11274 !!(filter_flags &
11275 WPA_DATA_FRAME_FILTER_FLAG_GTK));
11276
11277 if (ret) {
11278 wpa_printf(MSG_ERROR,
11279 "nl80211: Failed to set IPv6 unicast in multicast filter");
11280 return ret;
11281 }
11282
11283 /* Configure filtering of unicast frame encrypted using GTK */
11284 os_snprintf(path, sizeof(path),
11285 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
11286 bss->ifname);
11287 ret = nl80211_write_to_file(path,
11288 !!(filter_flags &
11289 WPA_DATA_FRAME_FILTER_FLAG_ARP));
11290 if (ret) {
11291 wpa_printf(MSG_ERROR,
11292 "nl80211: Failed set gratuitous ARP filter");
11293 return ret;
11294 }
11295
11296 /* Configure filtering of IPv6 NA frames */
11297 os_snprintf(path, sizeof(path),
11298 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
11299 bss->ifname);
11300 ret = nl80211_write_to_file(path,
11301 !!(filter_flags &
11302 WPA_DATA_FRAME_FILTER_FLAG_NA));
11303 if (ret) {
11304 wpa_printf(MSG_ERROR,
11305 "nl80211: Failed to set unsolicited NA filter");
11306 return ret;
11307 }
11308
11309 return 0;
11310}
11311
11312
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070011313static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
11314 const u8 **ext_capa, const u8 **ext_capa_mask,
11315 unsigned int *ext_capa_len)
11316{
11317 struct i802_bss *bss = priv;
11318 struct wpa_driver_nl80211_data *drv = bss->drv;
11319 enum nl80211_iftype nlmode;
11320 unsigned int i;
11321
11322 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
11323 return -1;
11324
11325 nlmode = wpa_driver_nl80211_if_type(type);
11326
11327 /* By default, use the per-radio values */
11328 *ext_capa = drv->extended_capa;
11329 *ext_capa_mask = drv->extended_capa_mask;
11330 *ext_capa_len = drv->extended_capa_len;
11331
11332 /* Replace the default value if a per-interface type value exists */
11333 for (i = 0; i < drv->num_iface_ext_capa; i++) {
11334 if (nlmode == drv->iface_ext_capa[i].iftype) {
11335 *ext_capa = drv->iface_ext_capa[i].ext_capa;
11336 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
11337 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
11338 break;
11339 }
11340 }
11341
11342 return 0;
11343}
11344
11345
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011346static int nl80211_update_connection_params(
11347 void *priv, struct wpa_driver_associate_params *params,
11348 enum wpa_drv_update_connect_params_mask mask)
11349{
11350 struct i802_bss *bss = priv;
11351 struct wpa_driver_nl80211_data *drv = bss->drv;
11352 struct nl_msg *msg;
11353 int ret = -1;
11354 enum nl80211_auth_type type;
11355
Hai Shalomc3565922019-10-28 11:58:20 -070011356 /* Update Connection Params is intended for drivers that implement
11357 * internal SME and expect these updated connection params from
11358 * wpa_supplicant. Do not send this request for the drivers using
11359 * SME from wpa_supplicant.
11360 */
11361 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
11362 return 0;
11363
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011364 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
11365 if (!msg)
11366 goto fail;
11367
11368 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
11369 drv->ifindex);
11370
11371 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
11372 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
11373 params->wpa_ie))
11374 goto fail;
11375 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
11376 params->wpa_ie_len);
11377 }
11378
11379 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
11380 type = get_nl_auth_type(params->auth_alg);
11381 if (type == NL80211_AUTHTYPE_MAX ||
11382 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
11383 goto fail;
11384 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
11385 }
11386
11387 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
11388 nl80211_put_fils_connect_params(drv, params, msg))
11389 goto fail;
11390
11391 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11392 msg = NULL;
11393 if (ret)
11394 wpa_dbg(drv->ctx, MSG_DEBUG,
11395 "nl80211: Update connect params command failed: ret=%d (%s)",
11396 ret, strerror(-ret));
11397
11398fail:
11399 nlmsg_free(msg);
11400 return ret;
11401}
11402
11403
Roshan Pius3a1667e2018-07-03 15:17:14 -070011404static int nl80211_send_external_auth_status(void *priv,
11405 struct external_auth *params)
11406{
11407 struct i802_bss *bss = priv;
11408 struct wpa_driver_nl80211_data *drv = bss->drv;
11409 struct nl_msg *msg = NULL;
11410 int ret = -1;
11411
Hai Shalom5f92bc92019-04-18 11:54:11 -070011412 /* External auth command/status is intended for drivers that implement
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080011413 * internal SME but want to offload authentication processing (e.g.,
11414 * SAE) to hostapd/wpa_supplicant. Do not send the status to drivers
Hai Shalom5f92bc92019-04-18 11:54:11 -070011415 * which do not support AP SME or use wpa_supplicant/hostapd SME.
11416 */
Hai Shalom81f62d82019-07-22 12:10:00 -070011417 if ((is_ap_interface(drv->nlmode) && !bss->drv->device_ap_sme) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070011418 (drv->capa.flags & WPA_DRIVER_FLAGS_SME))
11419 return -1;
11420
Roshan Pius3a1667e2018-07-03 15:17:14 -070011421 wpa_dbg(drv->ctx, MSG_DEBUG,
11422 "nl80211: External auth status: %u", params->status);
11423
11424 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
11425 if (!msg ||
11426 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
Hai Shalom5f92bc92019-04-18 11:54:11 -070011427 (params->ssid && params->ssid_len &&
11428 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
11429 (params->pmkid &&
11430 nla_put(msg, NL80211_ATTR_PMKID, PMKID_LEN, params->pmkid)) ||
11431 (params->bssid &&
11432 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid)))
Roshan Pius3a1667e2018-07-03 15:17:14 -070011433 goto fail;
11434 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11435 msg = NULL;
11436 if (ret) {
11437 wpa_printf(MSG_DEBUG,
11438 "nl80211: External Auth status update failed: ret=%d (%s)",
11439 ret, strerror(-ret));
11440 goto fail;
11441 }
11442fail:
11443 nlmsg_free(msg);
11444 return ret;
11445}
11446
11447
Hai Shalom74f70d42019-02-11 14:42:39 -080011448static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
11449 int val)
11450{
11451 struct i802_bss *bss = priv;
11452 struct wpa_driver_nl80211_data *drv = bss->drv;
11453 struct nl_msg *msg;
11454 int ret = -ENOBUFS;
11455
11456 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
11457 val ? "Enable" : "Disable", bridge_ifname);
11458
11459 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
11460 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
11461 goto fail;
11462
11463 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
11464 if (linux_br_del_if(drv->global->ioctl_sock,
11465 bridge_ifname, bss->ifname)) {
11466 wpa_printf(MSG_ERROR,
11467 "nl80211: Failed to remove interface %s from bridge %s",
11468 bss->ifname, bridge_ifname);
11469 return -1;
11470 }
11471 bss->added_if_into_bridge = 0;
11472 }
11473
11474 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11475 msg = NULL;
11476 if (!ret) {
11477 if (bridge_ifname[0] && val &&
11478 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
11479 return -1;
11480 return 0;
11481 }
11482
11483fail:
11484 nlmsg_free(msg);
11485 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
11486
11487 return ret;
11488}
11489
11490
Hai Shalome21d4e82020-04-29 16:34:06 -070011491#ifdef CONFIG_DPP
11492static int nl80211_dpp_listen(void *priv, bool enable)
11493{
11494 struct i802_bss *bss = priv;
11495 struct wpa_driver_nl80211_data *drv = bss->drv;
11496 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
11497 struct nl_sock *handle;
11498
11499 if (!drv->multicast_registrations || !bss->nl_mgmt)
11500 return 0; /* cannot do more than hope broadcast RX works */
11501
11502 wpa_printf(MSG_DEBUG,
11503 "nl80211: Update DPP Public Action frame registration (%s multicast RX)",
11504 enable ? "enable" : "disable");
11505 handle = (void *) (((intptr_t) bss->nl_mgmt) ^ ELOOP_SOCKET_INVALID);
11506 return nl80211_register_frame(bss, handle, type,
11507 (u8 *) "\x04\x09\x50\x6f\x9a\x1a", 6,
11508 enable);
11509}
11510#endif /* CONFIG_DPP */
11511
11512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011513const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11514 .name = "nl80211",
11515 .desc = "Linux nl80211/cfg80211",
11516 .get_bssid = wpa_driver_nl80211_get_bssid,
11517 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011518 .set_key = driver_nl80211_set_key,
11519 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011520 .sched_scan = wpa_driver_nl80211_sched_scan,
11521 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011522 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080011523 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011524 .deauthenticate = driver_nl80211_deauthenticate,
11525 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011526 .associate = wpa_driver_nl80211_associate,
11527 .global_init = nl80211_global_init,
11528 .global_deinit = nl80211_global_deinit,
11529 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011530 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011531 .get_capa = wpa_driver_nl80211_get_capa,
11532 .set_operstate = wpa_driver_nl80211_set_operstate,
11533 .set_supp_port = wpa_driver_nl80211_set_supp_port,
11534 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011535 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011536 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070011537 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011538 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011539 .if_remove = driver_nl80211_if_remove,
11540 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011541 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011542 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011543 .sta_remove = driver_nl80211_sta_remove,
Hai Shalomfdcde762020-04-02 11:19:20 -070011544 .tx_control_port = nl80211_tx_control_port,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011545 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
11546 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Hai Shalom81f62d82019-07-22 12:10:00 -070011547 .sta_set_airtime_weight = driver_nl80211_sta_set_airtime_weight,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011548 .hapd_init = i802_init,
11549 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011550 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011551 .get_seqnum = i802_get_seqnum,
11552 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011553 .get_inact_sec = i802_get_inact_sec,
11554 .sta_clear_stats = i802_sta_clear_stats,
11555 .set_rts = i802_set_rts,
11556 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011557 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011558 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011559 .sta_deauth = i802_sta_deauth,
11560 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011561 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011562 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011563 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011564 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
11565 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11566 .cancel_remain_on_channel =
11567 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011568 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011569 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070011570 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011571 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011572 .signal_monitor = nl80211_signal_monitor,
11573 .signal_poll = nl80211_signal_poll,
Hai Shalom74f70d42019-02-11 14:42:39 -080011574 .channel_info = nl80211_channel_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011575 .set_param = nl80211_set_param,
11576 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070011577 .add_pmkid = nl80211_add_pmkid,
11578 .remove_pmkid = nl80211_remove_pmkid,
11579 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011580 .set_rekey_info = nl80211_set_rekey_info,
11581 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011582 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011583 .start_dfs_cac = nl80211_start_radar_detection,
11584 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011585#ifdef CONFIG_TDLS
11586 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11587 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011588 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
11589 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011590#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011591 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Hai Shalom81f62d82019-07-22 12:10:00 -070011592 .update_dh_ie = nl80211_update_dh_ie,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011593 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011594 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070011595 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011596 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011597#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011598 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011599 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070011600 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011601#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011602#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070011603#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070011604 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070011605#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011606#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080011607 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011608 .set_qos_map = nl80211_set_qos_map,
Hai Shalomfdcde762020-04-02 11:19:20 -070011609 .get_wowlan = nl80211_get_wowlan,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070011610 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011611 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011612#ifdef CONFIG_MESH
11613 .init_mesh = wpa_driver_nl80211_init_mesh,
11614 .join_mesh = wpa_driver_nl80211_join_mesh,
11615 .leave_mesh = wpa_driver_nl80211_leave_mesh,
Hai Shalom81f62d82019-07-22 12:10:00 -070011616 .probe_mesh_link = nl80211_probe_mesh_link,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011617#endif /* CONFIG_MESH */
11618 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
11619 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
11620 .br_port_set_attr = wpa_driver_br_port_set_attr,
11621 .br_set_net_param = wpa_driver_br_set_net_param,
11622 .add_tx_ts = nl80211_add_ts,
11623 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070011624 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011625#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070011626 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070011627 .disable_fils = nl80211_disable_fils,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080011628 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -070011629 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011630 .get_pref_freq_list = nl80211_get_pref_freq_list,
11631 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070011632 .p2p_lo_start = nl80211_p2p_lo_start,
11633 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070011634 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080011635 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011636#ifdef CONFIG_MBO
11637 .get_bss_transition_status = nl80211_get_bss_transition_status,
11638 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
11639#endif /* CONFIG_MBO */
11640 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
Hai Shalomc3565922019-10-28 11:58:20 -070011641 .add_sta_node = nl80211_add_sta_node,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011642#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -070011643 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070011644 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011645 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070011646 .send_external_auth_status = nl80211_send_external_auth_status,
Hai Shalom74f70d42019-02-11 14:42:39 -080011647 .set_4addr_mode = nl80211_set_4addr_mode,
Hai Shalome21d4e82020-04-29 16:34:06 -070011648#ifdef CONFIG_DPP
11649 .dpp_listen = nl80211_dpp_listen,
11650#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011651};