blob: 661e34e668aece2583e255b0e513a4f7fde50c9e [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 Shmidt1f69aa52012-01-24 16:10:04 -080033#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080035#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "linux_ioctl.h"
37#include "radiotap.h"
38#include "radiotap_iter.h"
39#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080040#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042
Hai Shalom74f70d42019-02-11 14:42:39 -080043#ifndef NETLINK_CAP_ACK
44#define NETLINK_CAP_ACK 10
45#endif /* NETLINK_CAP_ACK */
Hai Shalom39ba6fc2019-01-22 12:40:38 -080046/* support for extack if compilation headers are too old */
47#ifndef NETLINK_EXT_ACK
48#define NETLINK_EXT_ACK 11
49enum nlmsgerr_attrs {
50 NLMSGERR_ATTR_UNUSED,
51 NLMSGERR_ATTR_MSG,
52 NLMSGERR_ATTR_OFFS,
53 NLMSGERR_ATTR_COOKIE,
54
55 __NLMSGERR_ATTR_MAX,
56 NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
57};
58#endif
59#ifndef NLM_F_CAPPED
60#define NLM_F_CAPPED 0x100
61#endif
62#ifndef NLM_F_ACK_TLVS
63#define NLM_F_ACK_TLVS 0x200
64#endif
65#ifndef SOL_NETLINK
66#define SOL_NETLINK 270
67#endif
68
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080069#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070/*
71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72 * but when you free a socket again it will mess up its bitmap and
73 * and use the wrong number the next time it needs a socket ID.
74 * Therefore, we wrap the handle alloc/destroy and add our own pid
75 * accounting.
76 */
77static uint32_t port_bitmap[32] = { 0 };
78
79static struct nl_handle *nl80211_handle_alloc(void *cb)
80{
81 struct nl_handle *handle;
82 uint32_t pid = getpid() & 0x3FFFFF;
83 int i;
84
85 handle = nl_handle_alloc_cb(cb);
86
87 for (i = 0; i < 1024; i++) {
88 if (port_bitmap[i / 32] & (1 << (i % 32)))
89 continue;
90 port_bitmap[i / 32] |= 1 << (i % 32);
91 pid += i << 22;
92 break;
93 }
94
95 nl_socket_set_local_port(handle, pid);
96
97 return handle;
98}
99
100static void nl80211_handle_destroy(struct nl_handle *handle)
101{
102 uint32_t port = nl_socket_get_local_port(handle);
103
104 port >>= 22;
105 port_bitmap[port / 32] &= ~(1 << (port % 32));
106
107 nl_handle_destroy(handle);
108}
109#endif /* CONFIG_LIBNL20 */
110
111
Dmitry Shmidt54605472013-11-08 11:10:19 -0800112#ifdef ANDROID
113/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -0800114#undef nl_socket_set_nonblocking
115#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800116
Dmitry Shmidt54605472013-11-08 11:10:19 -0800117#endif /* ANDROID */
118
119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800120static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
121{
122 struct nl_handle *handle;
123
124 handle = nl80211_handle_alloc(cb);
125 if (handle == NULL) {
126 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
127 "callbacks (%s)", dbg);
128 return NULL;
129 }
130
131 if (genl_connect(handle)) {
132 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
133 "netlink (%s)", dbg);
134 nl80211_handle_destroy(handle);
135 return NULL;
136 }
137
138 return handle;
139}
140
141
142static void nl_destroy_handles(struct nl_handle **handle)
143{
144 if (*handle == NULL)
145 return;
146 nl80211_handle_destroy(*handle);
147 *handle = NULL;
148}
149
150
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700151#if __WORDSIZE == 64
152#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
153#else
154#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
155#endif
156
157static void nl80211_register_eloop_read(struct nl_handle **handle,
158 eloop_sock_handler handler,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700159 void *eloop_data, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700160{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800161#ifdef CONFIG_LIBNL20
162 /*
163 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
164 * by default. It is possible to hit that limit in some cases where
165 * operations are blocked, e.g., with a burst of Deauthentication frames
166 * to hostapd and STA entry deletion. Try to increase the buffer to make
167 * this less likely to occur.
168 */
169 if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
170 wpa_printf(MSG_DEBUG,
171 "nl80211: Could not set nl_socket RX buffer size: %s",
172 strerror(errno));
173 /* continue anyway with the default (smaller) buffer */
174 }
175#endif /* CONFIG_LIBNL20 */
176
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700177 nl_socket_set_nonblocking(*handle);
178 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
179 eloop_data, *handle);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700180 if (!persist)
181 *handle = (void *) (((intptr_t) *handle) ^
182 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700183}
184
185
Roshan Pius3a1667e2018-07-03 15:17:14 -0700186static void nl80211_destroy_eloop_handle(struct nl_handle **handle, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700187{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700188 if (!persist)
189 *handle = (void *) (((intptr_t) *handle) ^
190 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700191 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
192 nl_destroy_handles(handle);
193}
194
195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800196static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800197static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800199static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700200static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
201 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800204wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800205 const u8 *set_addr, int first,
206 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800207static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209 const u8 *buf, size_t buf_len, u64 *cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800210 int no_cck, int no_ack, int offchanok,
211 const u16 *csa_offs, size_t csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800212static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
213 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800215#define IFIDX_ANY -1
216
217static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
218 int ifidx_reason);
219static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
220 int ifidx_reason);
221static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
222 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700223
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700224static int nl80211_set_channel(struct i802_bss *bss,
225 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
227 int ifindex, int disabled);
228
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800229static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
230 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800231
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700232static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800233static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700234#ifdef CONFIG_MESH
235static int nl80211_put_mesh_config(struct nl_msg *msg,
236 struct wpa_driver_mesh_bss_params *params);
237#endif /* CONFIG_MESH */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800238static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
239 int reason);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700240
241
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800242/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800243enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800244{
245 switch (width) {
246 case NL80211_CHAN_WIDTH_20_NOHT:
247 return CHAN_WIDTH_20_NOHT;
248 case NL80211_CHAN_WIDTH_20:
249 return CHAN_WIDTH_20;
250 case NL80211_CHAN_WIDTH_40:
251 return CHAN_WIDTH_40;
252 case NL80211_CHAN_WIDTH_80:
253 return CHAN_WIDTH_80;
254 case NL80211_CHAN_WIDTH_80P80:
255 return CHAN_WIDTH_80P80;
256 case NL80211_CHAN_WIDTH_160:
257 return CHAN_WIDTH_160;
258 }
259 return CHAN_WIDTH_UNKNOWN;
260}
261
262
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800263int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700265 return nlmode == NL80211_IFTYPE_AP ||
266 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800267}
268
269
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800270int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800271{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700272 return nlmode == NL80211_IFTYPE_STATION ||
273 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800274}
275
276
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700277static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700279 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
280 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800281}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282
283
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800284struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
285 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700286{
287 struct i802_bss *bss;
288
289 for (bss = drv->first_bss; bss; bss = bss->next) {
290 if (bss->ifindex == ifindex)
291 return bss;
292 }
293
294 return NULL;
295}
296
297
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800298static int is_mesh_interface(enum nl80211_iftype nlmode)
299{
300 return nlmode == NL80211_IFTYPE_MESH_POINT;
301}
302
303
304void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700305{
306 if (drv->associated)
307 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
308 drv->associated = 0;
309 os_memset(drv->bssid, 0, ETH_ALEN);
310}
311
312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313/* nl80211 code */
314static int ack_handler(struct nl_msg *msg, void *arg)
315{
316 int *err = arg;
317 *err = 0;
318 return NL_STOP;
319}
320
321static int finish_handler(struct nl_msg *msg, void *arg)
322{
323 int *ret = arg;
324 *ret = 0;
325 return NL_SKIP;
326}
327
328static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
329 void *arg)
330{
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800331 struct nlmsghdr *nlh = (struct nlmsghdr *) err - 1;
332 int len = nlh->nlmsg_len;
333 struct nlattr *attrs;
334 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 int *ret = arg;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800336 int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 *ret = err->error;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800339
340 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
341 return NL_SKIP;
342
343 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
344 ack_len += err->msg.nlmsg_len - sizeof(*nlh);
345
346 if (len <= ack_len)
347 return NL_STOP;
348
349 attrs = (void *) ((unsigned char *) nlh + ack_len);
350 len -= ack_len;
351
352 nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
353 if (tb[NLMSGERR_ATTR_MSG]) {
354 len = strnlen((char *) nla_data(tb[NLMSGERR_ATTR_MSG]),
355 nla_len(tb[NLMSGERR_ATTR_MSG]));
356 wpa_printf(MSG_ERROR, "nl80211: kernel reports: %*s",
357 len, (char *) nla_data(tb[NLMSGERR_ATTR_MSG]));
358 }
359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 return NL_SKIP;
361}
362
363
364static int no_seq_check(struct nl_msg *msg, void *arg)
365{
366 return NL_OK;
367}
368
369
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800370static void nl80211_nlmsg_clear(struct nl_msg *msg)
371{
372 /*
373 * Clear nlmsg data, e.g., to make sure key material is not left in
374 * heap memory for unnecessarily long time.
375 */
376 if (msg) {
377 struct nlmsghdr *hdr = nlmsg_hdr(msg);
378 void *data = nlmsg_data(hdr);
379 /*
380 * This would use nlmsg_datalen() or the older nlmsg_len() if
381 * only libnl were to maintain a stable API.. Neither will work
382 * with all released versions, so just calculate the length
383 * here.
384 */
385 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
386
387 os_memset(data, 0, len);
388 }
389}
390
391
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393 struct nl_handle *nl_handle, struct nl_msg *msg,
394 int (*valid_handler)(struct nl_msg *, void *),
395 void *valid_data)
396{
397 struct nl_cb *cb;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800398 int err = -ENOMEM, opt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800400 if (!msg)
401 return -ENOMEM;
402
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800403 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 if (!cb)
405 goto out;
406
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800407 /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
408 opt = 1;
409 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
410 NETLINK_EXT_ACK, &opt, sizeof(opt));
411
Hai Shalom74f70d42019-02-11 14:42:39 -0800412 /* try to set NETLINK_CAP_ACK to 1, ignoring errors */
413 opt = 1;
414 setsockopt(nl_socket_get_fd(nl_handle), SOL_NETLINK,
415 NETLINK_CAP_ACK, &opt, sizeof(opt));
416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 err = nl_send_auto_complete(nl_handle, msg);
418 if (err < 0)
419 goto out;
420
421 err = 1;
422
423 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
424 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
425 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
426
427 if (valid_handler)
428 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
429 valid_handler, valid_data);
430
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700431 while (err > 0) {
432 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700433 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700434 wpa_printf(MSG_INFO,
435 "nl80211: %s->nl_recvmsgs failed: %d",
436 __func__, res);
437 }
438 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 out:
440 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800441 if (!valid_handler && valid_data == (void *) -1)
442 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 nlmsg_free(msg);
444 return err;
445}
446
447
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800448int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
449 struct nl_msg *msg,
450 int (*valid_handler)(struct nl_msg *, void *),
451 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800453 return send_and_recv(drv->global, drv->global->nl, msg,
454 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455}
456
457
458struct family_data {
459 const char *group;
460 int id;
461};
462
463
464static int family_handler(struct nl_msg *msg, void *arg)
465{
466 struct family_data *res = arg;
467 struct nlattr *tb[CTRL_ATTR_MAX + 1];
468 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
469 struct nlattr *mcgrp;
470 int i;
471
472 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
473 genlmsg_attrlen(gnlh, 0), NULL);
474 if (!tb[CTRL_ATTR_MCAST_GROUPS])
475 return NL_SKIP;
476
477 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
478 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
479 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
480 nla_len(mcgrp), NULL);
481 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
482 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
483 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
484 res->group,
485 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
486 continue;
487 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
488 break;
489 };
490
491 return NL_SKIP;
492}
493
494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800495static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 const char *family, const char *group)
497{
498 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800499 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 struct family_data res = { group, -ENOENT };
501
502 msg = nlmsg_alloc();
503 if (!msg)
504 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800505 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
506 0, 0, CTRL_CMD_GETFAMILY, 0) ||
507 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
508 nlmsg_free(msg);
509 return -1;
510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800512 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 if (ret == 0)
514 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 return ret;
516}
517
518
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800519void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
520 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800521{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700522 if (TEST_FAIL())
523 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800524 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
525 0, flags, cmd, 0);
526}
527
528
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800529static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
530{
531 if (bss->wdev_id_set)
532 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
533 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
534}
535
536
537struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
538{
539 struct nl_msg *msg;
540
541 msg = nlmsg_alloc();
542 if (!msg)
543 return NULL;
544
545 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
546 nl80211_set_iface_id(msg, bss) < 0) {
547 nlmsg_free(msg);
548 return NULL;
549 }
550
551 return msg;
552}
553
554
555static struct nl_msg *
556nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
557 int flags, uint8_t cmd)
558{
559 struct nl_msg *msg;
560
561 msg = nlmsg_alloc();
562 if (!msg)
563 return NULL;
564
565 if (!nl80211_cmd(drv, msg, flags, cmd) ||
566 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
567 nlmsg_free(msg);
568 return NULL;
569 }
570
571 return msg;
572}
573
574
575struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
576 uint8_t cmd)
577{
578 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
579}
580
581
582struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
583{
584 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
585}
586
587
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800588struct wiphy_idx_data {
589 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700590 enum nl80211_iftype nlmode;
591 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800592};
593
594
595static int netdev_info_handler(struct nl_msg *msg, void *arg)
596{
597 struct nlattr *tb[NL80211_ATTR_MAX + 1];
598 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
599 struct wiphy_idx_data *info = arg;
600
601 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
602 genlmsg_attrlen(gnlh, 0), NULL);
603
604 if (tb[NL80211_ATTR_WIPHY])
605 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
606
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700607 if (tb[NL80211_ATTR_IFTYPE])
608 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
609
610 if (tb[NL80211_ATTR_MAC] && info->macaddr)
611 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
612 ETH_ALEN);
613
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 return NL_SKIP;
615}
616
617
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800618int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800619{
620 struct nl_msg *msg;
621 struct wiphy_idx_data data = {
622 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700623 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624 };
625
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800626 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
627 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628
629 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
630 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800631 return -1;
632}
633
634
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700635static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
636{
637 struct nl_msg *msg;
638 struct wiphy_idx_data data = {
639 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
640 .macaddr = NULL,
641 };
642
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800643 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
644 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700645
646 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
647 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700648 return NL80211_IFTYPE_UNSPECIFIED;
649}
650
651
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700652static int nl80211_get_macaddr(struct i802_bss *bss)
653{
654 struct nl_msg *msg;
655 struct wiphy_idx_data data = {
656 .macaddr = bss->addr,
657 };
658
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800659 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
660 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700661
662 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700663}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700664
665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800666static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
667 struct nl80211_wiphy_data *w)
668{
669 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800670 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800671
672 msg = nlmsg_alloc();
673 if (!msg)
674 return -1;
675
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800676 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
677 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
678 nlmsg_free(msg);
679 return -1;
680 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800681
682 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800683 if (ret) {
684 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
685 "failed: ret=%d (%s)",
686 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800687 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800688 return ret;
689}
690
691
692static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
693{
694 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700695 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800696
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800697 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800698
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700699 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700700 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700701 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
702 __func__, res);
703 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800704}
705
706
707static int process_beacon_event(struct nl_msg *msg, void *arg)
708{
709 struct nl80211_wiphy_data *w = arg;
710 struct wpa_driver_nl80211_data *drv;
711 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
712 struct nlattr *tb[NL80211_ATTR_MAX + 1];
713 union wpa_event_data event;
714
715 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
716 genlmsg_attrlen(gnlh, 0), NULL);
717
718 if (gnlh->cmd != NL80211_CMD_FRAME) {
719 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
720 gnlh->cmd);
721 return NL_SKIP;
722 }
723
724 if (!tb[NL80211_ATTR_FRAME])
725 return NL_SKIP;
726
727 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
728 wiphy_list) {
729 os_memset(&event, 0, sizeof(event));
730 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
731 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
732 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
733 }
734
735 return NL_SKIP;
736}
737
738
739static struct nl80211_wiphy_data *
740nl80211_get_wiphy_data_ap(struct i802_bss *bss)
741{
742 static DEFINE_DL_LIST(nl80211_wiphys);
743 struct nl80211_wiphy_data *w;
744 int wiphy_idx, found = 0;
745 struct i802_bss *tmp_bss;
Paul Stewart092955c2017-02-06 09:13:09 -0800746 u8 channel;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747
748 if (bss->wiphy_data != NULL)
749 return bss->wiphy_data;
750
751 wiphy_idx = nl80211_get_wiphy_index(bss);
752
753 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
754 if (w->wiphy_idx == wiphy_idx)
755 goto add;
756 }
757
758 /* alloc new one */
759 w = os_zalloc(sizeof(*w));
760 if (w == NULL)
761 return NULL;
762 w->wiphy_idx = wiphy_idx;
763 dl_list_init(&w->bsss);
764 dl_list_init(&w->drvs);
765
Paul Stewart092955c2017-02-06 09:13:09 -0800766 /* Beacon frames not supported in IEEE 802.11ad */
767 if (ieee80211_freq_to_chan(bss->freq, &channel) !=
768 HOSTAPD_MODE_IEEE80211AD) {
769 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
770 if (!w->nl_cb) {
771 os_free(w);
772 return NULL;
773 }
774 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
775 no_seq_check, NULL);
776 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
777 process_beacon_event, w);
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000778
Paul Stewart092955c2017-02-06 09:13:09 -0800779 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
780 "wiphy beacons");
781 if (w->nl_beacons == NULL) {
782 os_free(w);
783 return NULL;
784 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000785
Paul Stewart092955c2017-02-06 09:13:09 -0800786 if (nl80211_register_beacons(bss->drv, w)) {
787 nl_destroy_handles(&w->nl_beacons);
788 os_free(w);
789 return NULL;
790 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000791
Paul Stewart092955c2017-02-06 09:13:09 -0800792 nl80211_register_eloop_read(&w->nl_beacons,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700793 nl80211_recv_beacons, w, 0);
Paul Stewart092955c2017-02-06 09:13:09 -0800794 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795
796 dl_list_add(&nl80211_wiphys, &w->list);
797
798add:
799 /* drv entry for this bss already there? */
800 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
801 if (tmp_bss->drv == bss->drv) {
802 found = 1;
803 break;
804 }
805 }
806 /* if not add it */
807 if (!found)
808 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
809
810 dl_list_add(&w->bsss, &bss->wiphy_list);
811 bss->wiphy_data = w;
812 return w;
813}
814
815
816static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
817{
818 struct nl80211_wiphy_data *w = bss->wiphy_data;
819 struct i802_bss *tmp_bss;
820 int found = 0;
821
822 if (w == NULL)
823 return;
824 bss->wiphy_data = NULL;
825 dl_list_del(&bss->wiphy_list);
826
827 /* still any for this drv present? */
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 remove it */
835 if (!found)
836 dl_list_del(&bss->drv->wiphy_list);
837
838 if (!dl_list_empty(&w->bsss))
839 return;
840
Paul Stewart092955c2017-02-06 09:13:09 -0800841 if (w->nl_beacons)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700842 nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800843
844 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845 dl_list_del(&w->list);
846 os_free(w);
847}
848
849
Dmitry Shmidte4663042016-04-04 10:07:49 -0700850static unsigned int nl80211_get_ifindex(void *priv)
851{
852 struct i802_bss *bss = priv;
853 struct wpa_driver_nl80211_data *drv = bss->drv;
854
855 return drv->ifindex;
856}
857
858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
860{
861 struct i802_bss *bss = priv;
862 struct wpa_driver_nl80211_data *drv = bss->drv;
863 if (!drv->associated)
864 return -1;
865 os_memcpy(bssid, drv->bssid, ETH_ALEN);
866 return 0;
867}
868
869
870static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
871{
872 struct i802_bss *bss = priv;
873 struct wpa_driver_nl80211_data *drv = bss->drv;
874 if (!drv->associated)
875 return -1;
876 os_memcpy(ssid, drv->ssid, drv->ssid_len);
877 return drv->ssid_len;
878}
879
880
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800881static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700882 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
883 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884{
885 union wpa_event_data event;
886
Dmitry Shmidte4663042016-04-04 10:07:49 -0700887 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800888 if (if_nametoindex(drv->first_bss->ifname) == 0) {
889 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
890 drv->first_bss->ifname);
891 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700892 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800893 if (!drv->if_removed)
894 return;
895 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
896 drv->first_bss->ifname);
897 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 }
899
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800900 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700901 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800902 os_strlcpy(event.interface_status.ifname, ifname,
903 sizeof(event.interface_status.ifname));
904 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700905 if (drv)
906 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
907 else
908 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
909 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800910}
911
912
913static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700914 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
915 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800916{
917 union wpa_event_data event;
918
Dmitry Shmidte4663042016-04-04 10:07:49 -0700919 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800920 if (drv->if_removed) {
921 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
922 ifname);
923 return;
924 }
925 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
926 ifname);
927 drv->if_removed = 1;
928 } else {
929 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
930 ifname);
931 }
932
933 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700934 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800935 os_strlcpy(event.interface_status.ifname, ifname,
936 sizeof(event.interface_status.ifname));
937 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700938 if (drv)
939 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
940 else
941 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
942 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943}
944
945
946static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
947 u8 *buf, size_t len)
948{
949 int attrlen, rta_len;
950 struct rtattr *attr;
951
952 attrlen = len;
953 attr = (struct rtattr *) buf;
954
955 rta_len = RTA_ALIGN(sizeof(struct rtattr));
956 while (RTA_OK(attr, attrlen)) {
957 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800958 if (os_strcmp(((char *) attr) + rta_len,
959 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 return 1;
961 else
962 break;
963 }
964 attr = RTA_NEXT(attr, attrlen);
965 }
966
967 return 0;
968}
969
970
971static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
972 int ifindex, u8 *buf, size_t len)
973{
974 if (drv->ifindex == ifindex)
975 return 1;
976
977 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800978 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
980 "interface");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700981 if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
982 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 return 1;
984 }
985
986 return 0;
987}
988
989
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800990static struct wpa_driver_nl80211_data *
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700991nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
992 int *init_failed)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993{
994 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700995 int res;
996
997 if (init_failed)
998 *init_failed = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800999 dl_list_for_each(drv, &global->interfaces,
1000 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001001 res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
1002 if (res < 0) {
1003 wpa_printf(MSG_DEBUG,
1004 "nl80211: Found matching own interface, but failed to complete reinitialization");
1005 if (init_failed)
1006 *init_failed = 1;
1007 return drv;
1008 }
1009 if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001010 return drv;
1011 }
1012 return NULL;
1013}
1014
1015
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001016static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001017 int ifindex, int notify)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001018{
1019 struct i802_bss *bss;
1020 u8 addr[ETH_ALEN];
1021
1022 bss = get_bss_ifindex(drv, ifindex);
1023 if (bss &&
1024 linux_get_ifhwaddr(drv->global->ioctl_sock,
1025 bss->ifname, addr) < 0) {
1026 wpa_printf(MSG_DEBUG,
1027 "nl80211: %s: failed to re-read MAC address",
1028 bss->ifname);
1029 } else if (bss && os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1030 wpa_printf(MSG_DEBUG,
1031 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1032 MACSTR " to " MACSTR,
1033 ifindex, bss->ifname,
1034 MAC2STR(bss->addr), MAC2STR(addr));
1035 os_memcpy(bss->addr, addr, ETH_ALEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001036 if (notify)
1037 wpa_supplicant_event(drv->ctx,
1038 EVENT_INTERFACE_MAC_CHANGED, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001039 }
1040}
1041
1042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1044 struct ifinfomsg *ifi,
1045 u8 *buf, size_t len)
1046{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047 struct nl80211_global *global = ctx;
1048 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001049 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 struct rtattr *attr;
1051 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001053 char ifname[IFNAMSIZ + 1];
1054 char extra[100], *pos, *end;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001055 int init_failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001057 extra[0] = '\0';
1058 pos = extra;
1059 end = pos + sizeof(extra);
1060 ifname[0] = '\0';
1061
1062 attrlen = len;
1063 attr = (struct rtattr *) buf;
1064 while (RTA_OK(attr, attrlen)) {
1065 switch (attr->rta_type) {
1066 case IFLA_IFNAME:
1067 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1068 break;
1069 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1070 ifname[RTA_PAYLOAD(attr)] = '\0';
1071 break;
1072 case IFLA_MASTER:
1073 brid = nla_get_u32((struct nlattr *) attr);
1074 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1075 break;
1076 case IFLA_WIRELESS:
1077 pos += os_snprintf(pos, end - pos, " wext");
1078 break;
1079 case IFLA_OPERSTATE:
1080 pos += os_snprintf(pos, end - pos, " operstate=%u",
1081 nla_get_u32((struct nlattr *) attr));
1082 break;
1083 case IFLA_LINKMODE:
1084 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1085 nla_get_u32((struct nlattr *) attr));
1086 break;
1087 }
1088 attr = RTA_NEXT(attr, attrlen);
1089 }
1090 extra[sizeof(extra) - 1] = '\0';
1091
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001092 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1093 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1094 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1096 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1097 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1098 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1099
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001100 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
Dmitry Shmidte4663042016-04-04 10:07:49 -07001101 if (!drv)
1102 goto event_newlink;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001103 if (init_failed)
1104 return; /* do not update interface state */
Dmitry Shmidte4663042016-04-04 10:07:49 -07001105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001107 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001108 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001109 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001110 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1111 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001112 drv->ignore_if_down_event = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001113 /* Re-read MAC address as it may have changed */
1114 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001115 return;
1116 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001117 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1118 namebuf, ifname);
1119 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1120 wpa_printf(MSG_DEBUG,
1121 "nl80211: Not the main interface (%s) - do not indicate interface down",
1122 drv->first_bss->ifname);
1123 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001124 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1125 "event generated by mode change");
1126 drv->ignore_if_down_event = 0;
1127 } else {
1128 drv->if_disabled = 1;
1129 wpa_supplicant_event(drv->ctx,
1130 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001131
1132 /*
1133 * Try to get drv again, since it may be removed as
1134 * part of the EVENT_INTERFACE_DISABLED handling for
1135 * dynamic interfaces
1136 */
1137 drv = nl80211_find_drv(global, ifi->ifi_index,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001138 buf, len, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001139 if (!drv)
1140 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001141 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 }
1143
1144 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07001145 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001146 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001147 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001148 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1149 "event since interface %s is down",
1150 namebuf);
Hai Shalomc9e41a12018-07-31 14:41:42 -07001151 return;
1152 }
1153 wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
1154 namebuf, ifname);
1155 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1156 wpa_printf(MSG_DEBUG,
1157 "nl80211: Not the main interface (%s) - do not indicate interface up",
1158 drv->first_bss->ifname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001159 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001160 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1161 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001162 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001163 } else if (drv->if_removed) {
1164 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1165 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001166 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001167 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001168 /* Re-read MAC address as it may have changed */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001169 nl80211_refresh_mac(drv, ifi->ifi_index, 0);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171 drv->if_disabled = 0;
1172 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1173 NULL);
1174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 }
1176
1177 /*
1178 * Some drivers send the association event before the operup event--in
1179 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1180 * fails. This will hit us when wpa_supplicant does not need to do
1181 * IEEE 802.1X authentication
1182 */
1183 if (drv->operstate == 1 &&
1184 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001185 !(ifi->ifi_flags & IFF_RUNNING)) {
1186 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001187 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 }
1190
Dmitry Shmidte4663042016-04-04 10:07:49 -07001191event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001192 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001193 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1194 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001195
Dmitry Shmidte4663042016-04-04 10:07:49 -07001196 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001197 struct i802_bss *bss;
1198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001200 if (!if_indextoname(brid, namebuf)) {
1201 wpa_printf(MSG_DEBUG,
1202 "nl80211: Could not find bridge ifname for ifindex %u",
1203 brid);
1204 return;
1205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1207 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001208 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001209
1210 for (bss = drv->first_bss; bss; bss = bss->next) {
1211 if (os_strcmp(ifname, bss->ifname) == 0) {
1212 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1213 break;
1214 }
1215 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217}
1218
1219
1220static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1221 struct ifinfomsg *ifi,
1222 u8 *buf, size_t len)
1223{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001224 struct nl80211_global *global = ctx;
1225 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001226 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 struct rtattr *attr;
1228 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001229 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001230 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001232 extra[0] = '\0';
1233 pos = extra;
1234 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001235 ifname[0] = '\0';
1236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 attrlen = len;
1238 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001240 switch (attr->rta_type) {
1241 case IFLA_IFNAME:
1242 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1243 break;
1244 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1245 ifname[RTA_PAYLOAD(attr)] = '\0';
1246 break;
1247 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001249 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1250 break;
1251 case IFLA_OPERSTATE:
1252 pos += os_snprintf(pos, end - pos, " operstate=%u",
1253 nla_get_u32((struct nlattr *) attr));
1254 break;
1255 case IFLA_LINKMODE:
1256 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1257 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001258 break;
1259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 attr = RTA_NEXT(attr, attrlen);
1261 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001262 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001264 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1265 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1266 ifi->ifi_flags,
1267 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1268 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1269 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1270 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1271
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001272 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001273
Dmitry Shmidte4663042016-04-04 10:07:49 -07001274 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 /* device has been removed from bridge */
1276 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001277
1278 if (!if_indextoname(brid, namebuf)) {
1279 wpa_printf(MSG_DEBUG,
1280 "nl80211: Could not find bridge ifname for ifindex %u",
1281 brid);
1282 } else {
1283 wpa_printf(MSG_DEBUG,
1284 "nl80211: Remove ifindex %u for bridge %s",
1285 brid, namebuf);
1286 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001287 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001289
1290 if (ifi->ifi_family != AF_BRIDGE || !brid)
1291 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1292 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293}
1294
1295
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001296struct nl80211_get_assoc_freq_arg {
1297 struct wpa_driver_nl80211_data *drv;
1298 unsigned int assoc_freq;
1299 unsigned int ibss_freq;
1300 u8 assoc_bssid[ETH_ALEN];
1301 u8 assoc_ssid[SSID_MAX_LEN];
1302 u8 assoc_ssid_len;
1303};
1304
1305static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1306{
1307 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1308 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1309 struct nlattr *bss[NL80211_BSS_MAX + 1];
1310 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1311 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1312 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1313 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1314 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1315 };
1316 struct nl80211_get_assoc_freq_arg *ctx = arg;
1317 enum nl80211_bss_status status;
1318
1319 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1320 genlmsg_attrlen(gnlh, 0), NULL);
1321 if (!tb[NL80211_ATTR_BSS] ||
1322 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1323 bss_policy) ||
1324 !bss[NL80211_BSS_STATUS])
1325 return NL_SKIP;
1326
1327 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1328 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1329 bss[NL80211_BSS_FREQUENCY]) {
1330 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1331 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1332 ctx->assoc_freq);
1333 }
1334 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1335 bss[NL80211_BSS_FREQUENCY]) {
1336 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1337 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1338 ctx->ibss_freq);
1339 }
1340 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1341 bss[NL80211_BSS_BSSID]) {
1342 os_memcpy(ctx->assoc_bssid,
1343 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1344 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1345 MACSTR, MAC2STR(ctx->assoc_bssid));
1346 }
1347
1348 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1349 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1350 const u8 *ie, *ssid;
1351 size_t ie_len;
1352
1353 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1354 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1355 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1356 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1357 ctx->assoc_ssid_len = ssid[1];
1358 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1359 }
1360 }
1361
1362 return NL_SKIP;
1363}
1364
1365
1366int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001367{
1368 struct nl_msg *msg;
1369 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001370 struct nl80211_get_assoc_freq_arg arg;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001371
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001372 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001373 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001374 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001375 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1376 &arg);
1377 if (ret == 0) {
1378 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1379 return arg.assoc_ssid_len;
1380 }
1381 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1382 ret, strerror(-ret));
1383 return ret;
1384}
1385
1386
1387unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1388{
1389 struct nl_msg *msg;
1390 int ret;
1391 struct nl80211_get_assoc_freq_arg arg;
1392
1393 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1394 os_memset(&arg, 0, sizeof(arg));
1395 arg.drv = drv;
1396 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1397 &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001398 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001399 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1400 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001401 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001402 "associated BSS from scan results: %u MHz", freq);
1403 if (freq)
1404 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001405 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001406 }
1407 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1408 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001409 return drv->assoc_freq;
1410}
1411
1412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413static int get_link_signal(struct nl_msg *msg, void *arg)
1414{
1415 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1416 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1417 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1418 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1419 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001420 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001421 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 };
1423 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1424 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1425 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1426 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1427 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1428 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1429 };
1430 struct wpa_signal_info *sig_change = arg;
1431
1432 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1433 genlmsg_attrlen(gnlh, 0), NULL);
1434 if (!tb[NL80211_ATTR_STA_INFO] ||
1435 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1436 tb[NL80211_ATTR_STA_INFO], policy))
1437 return NL_SKIP;
1438 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1439 return NL_SKIP;
1440
1441 sig_change->current_signal =
1442 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1443
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001444 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1445 sig_change->avg_signal =
1446 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1447 else
1448 sig_change->avg_signal = 0;
1449
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001450 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1451 sig_change->avg_beacon_signal =
1452 (s8)
1453 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1454 else
1455 sig_change->avg_beacon_signal = 0;
1456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1458 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1459 sinfo[NL80211_STA_INFO_TX_BITRATE],
1460 rate_policy)) {
1461 sig_change->current_txrate = 0;
1462 } else {
1463 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1464 sig_change->current_txrate =
1465 nla_get_u16(rinfo[
1466 NL80211_RATE_INFO_BITRATE]) * 100;
1467 }
1468 }
1469 }
1470
1471 return NL_SKIP;
1472}
1473
1474
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001475int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1476 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477{
1478 struct nl_msg *msg;
1479
Hai Shalom74f70d42019-02-11 14:42:39 -08001480 sig->current_signal = -WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481 sig->current_txrate = 0;
1482
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001483 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1484 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1485 nlmsg_free(msg);
1486 return -ENOBUFS;
1487 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488
1489 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490}
1491
1492
1493static int get_link_noise(struct nl_msg *msg, void *arg)
1494{
1495 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1496 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1497 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1498 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1499 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1500 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1501 };
1502 struct wpa_signal_info *sig_change = arg;
1503
1504 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1505 genlmsg_attrlen(gnlh, 0), NULL);
1506
1507 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1508 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1509 return NL_SKIP;
1510 }
1511
1512 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1513 tb[NL80211_ATTR_SURVEY_INFO],
1514 survey_policy)) {
1515 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1516 "attributes!");
1517 return NL_SKIP;
1518 }
1519
1520 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1521 return NL_SKIP;
1522
1523 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1524 sig_change->frequency)
1525 return NL_SKIP;
1526
1527 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1528 return NL_SKIP;
1529
1530 sig_change->current_noise =
1531 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1532
1533 return NL_SKIP;
1534}
1535
1536
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001537int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1538 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539{
1540 struct nl_msg *msg;
1541
Hai Shalom74f70d42019-02-11 14:42:39 -08001542 sig_change->current_noise = WPA_INVALID_NOISE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543 sig_change->frequency = drv->assoc_freq;
1544
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001545 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547}
1548
1549
Hai Shalom74f70d42019-02-11 14:42:39 -08001550static int get_channel_info(struct nl_msg *msg, void *arg)
1551{
1552 struct nlattr *tb[NL80211_ATTR_MAX + 1] = { 0 };
1553 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1554 struct wpa_channel_info *chan_info = arg;
1555
1556 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1557 genlmsg_attrlen(gnlh, 0), NULL);
1558
1559 os_memset(chan_info, 0, sizeof(struct wpa_channel_info));
1560 chan_info->chanwidth = CHAN_WIDTH_UNKNOWN;
1561
1562 if (tb[NL80211_ATTR_WIPHY_FREQ])
1563 chan_info->frequency =
1564 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1565 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
1566 chan_info->chanwidth = convert2width(
1567 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
1568 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1569 enum nl80211_channel_type ct =
1570 nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1571
1572 switch (ct) {
1573 case NL80211_CHAN_HT40MINUS:
1574 chan_info->sec_channel = -1;
1575 break;
1576 case NL80211_CHAN_HT40PLUS:
1577 chan_info->sec_channel = 1;
1578 break;
1579 default:
1580 chan_info->sec_channel = 0;
1581 break;
1582 }
1583 }
1584 if (tb[NL80211_ATTR_CENTER_FREQ1])
1585 chan_info->center_frq1 =
1586 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1587 if (tb[NL80211_ATTR_CENTER_FREQ2])
1588 chan_info->center_frq2 =
1589 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1590
1591 if (chan_info->center_frq2) {
1592 u8 seg1_idx = 0;
1593
1594 if (ieee80211_freq_to_chan(chan_info->center_frq2, &seg1_idx) !=
1595 NUM_HOSTAPD_MODES)
1596 chan_info->seg1_idx = seg1_idx;
1597 }
1598
1599 return NL_SKIP;
1600}
1601
1602
1603static int nl80211_channel_info(void *priv, struct wpa_channel_info *ci)
1604{
1605 struct i802_bss *bss = priv;
1606 struct wpa_driver_nl80211_data *drv = bss->drv;
1607 struct nl_msg *msg;
1608
1609 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
1610 return send_and_recv_msgs(drv, msg, get_channel_info, ci);
1611}
1612
1613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1615 void *handle)
1616{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001617 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001618 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001620 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001622 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001623 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001624 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1625 __func__, res);
1626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627}
1628
1629
1630/**
1631 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1632 * @priv: driver_nl80211 private data
1633 * @alpha2_arg: country to which to switch to
1634 * Returns: 0 on success, -1 on failure
1635 *
1636 * This asks nl80211 to set the regulatory domain for given
1637 * country ISO / IEC alpha2.
1638 */
1639static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1640{
1641 struct i802_bss *bss = priv;
1642 struct wpa_driver_nl80211_data *drv = bss->drv;
1643 char alpha2[3];
1644 struct nl_msg *msg;
1645
1646 msg = nlmsg_alloc();
1647 if (!msg)
1648 return -ENOMEM;
1649
1650 alpha2[0] = alpha2_arg[0];
1651 alpha2[1] = alpha2_arg[1];
1652 alpha2[2] = '\0';
1653
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001654 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1655 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1656 nlmsg_free(msg);
1657 return -EINVAL;
1658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1660 return -EINVAL;
1661 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662}
1663
1664
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001665static int nl80211_get_country(struct nl_msg *msg, void *arg)
1666{
1667 char *alpha2 = arg;
1668 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1669 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1670
1671 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1672 genlmsg_attrlen(gnlh, 0), NULL);
1673 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1674 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1675 return NL_SKIP;
1676 }
1677 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1678 return NL_SKIP;
1679}
1680
1681
1682static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1683{
1684 struct i802_bss *bss = priv;
1685 struct wpa_driver_nl80211_data *drv = bss->drv;
1686 struct nl_msg *msg;
1687 int ret;
1688
1689 msg = nlmsg_alloc();
1690 if (!msg)
1691 return -ENOMEM;
1692
1693 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1694 alpha2[0] = '\0';
1695 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1696 if (!alpha2[0])
1697 ret = -1;
1698
1699 return ret;
1700}
1701
1702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704{
1705 int ret;
1706
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001707 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1708 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1710 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 }
1713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001714 global->nl = nl_create_handle(global->nl_cb, "nl");
1715 if (global->nl == NULL)
1716 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1719 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1721 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001722 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 }
1724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001725 global->nl_event = nl_create_handle(global->nl_cb, "event");
1726 if (global->nl_event == NULL)
1727 goto err;
1728
1729 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001731 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 if (ret < 0) {
1733 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1734 "membership for scan events: %d (%s)",
1735 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001736 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 }
1738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001741 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742 if (ret < 0) {
1743 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1744 "membership for mlme events: %d (%s)",
1745 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 }
1748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001749 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001751 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752 if (ret < 0) {
1753 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1754 "membership for regulatory events: %d (%s)",
1755 ret, strerror(-ret));
1756 /* Continue without regulatory events */
1757 }
1758
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001759 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1760 if (ret >= 0)
1761 ret = nl_socket_add_membership(global->nl_event, ret);
1762 if (ret < 0) {
1763 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1764 "membership for vendor events: %d (%s)",
1765 ret, strerror(-ret));
1766 /* Continue without vendor events */
1767 }
1768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001769 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1770 no_seq_check, NULL);
1771 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1772 process_global_event, global);
1773
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001774 nl80211_register_eloop_read(&global->nl_event,
1775 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001776 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777
1778 return 0;
1779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001780err:
1781 nl_destroy_handles(&global->nl_event);
1782 nl_destroy_handles(&global->nl);
1783 nl_cb_put(global->nl_cb);
1784 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785 return -1;
1786}
1787
1788
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001789static void nl80211_check_global(struct nl80211_global *global)
1790{
1791 struct nl_handle *handle;
1792 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1793 int ret;
1794 unsigned int i;
1795
1796 /*
1797 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1798 * and all registration having been cleared.
1799 */
1800 handle = (void *) (((intptr_t) global->nl_event) ^
1801 ELOOP_SOCKET_INVALID);
1802
1803 for (i = 0; groups[i]; i++) {
1804 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1805 if (ret >= 0)
1806 ret = nl_socket_add_membership(handle, ret);
1807 if (ret < 0) {
1808 wpa_printf(MSG_INFO,
1809 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1810 groups[i], ret, strerror(-ret));
1811 }
1812 }
1813}
1814
1815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1817{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001818 struct wpa_driver_nl80211_data *drv = ctx;
1819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001823 * rtnetlink ifdown handler will report interfaces other than the P2P
1824 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001826 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1827 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828}
1829
1830
1831static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1832{
1833 struct wpa_driver_nl80211_data *drv = ctx;
1834 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001835 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1837 "after rfkill unblock");
1838 return;
1839 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001840
1841 if (is_p2p_net_interface(drv->nlmode))
1842 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1843
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001844 /*
1845 * rtnetlink ifup handler will report interfaces other than the P2P
1846 * Device interface as enabled.
1847 */
1848 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1849 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850}
1851
1852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1854 void *eloop_ctx,
1855 void *handle)
1856{
1857 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1858 u8 data[2048];
1859 struct msghdr msg;
1860 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001861 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001862 struct cmsghdr *cmsg;
1863 int res, found_ee = 0, found_wifi = 0, acked = 0;
1864 union wpa_event_data event;
1865
1866 memset(&msg, 0, sizeof(msg));
1867 msg.msg_iov = &entry;
1868 msg.msg_iovlen = 1;
1869 entry.iov_base = data;
1870 entry.iov_len = sizeof(data);
1871 msg.msg_control = &control;
1872 msg.msg_controllen = sizeof(control);
1873
1874 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1875 /* if error or not fitting 802.3 header, return */
1876 if (res < 14)
1877 return;
1878
1879 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1880 {
1881 if (cmsg->cmsg_level == SOL_SOCKET &&
1882 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1883 int *ack;
1884
1885 found_wifi = 1;
1886 ack = (void *)CMSG_DATA(cmsg);
1887 acked = *ack;
1888 }
1889
1890 if (cmsg->cmsg_level == SOL_PACKET &&
1891 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1892 struct sock_extended_err *err =
1893 (struct sock_extended_err *)CMSG_DATA(cmsg);
1894
1895 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1896 found_ee = 1;
1897 }
1898 }
1899
1900 if (!found_ee || !found_wifi)
1901 return;
1902
1903 memset(&event, 0, sizeof(event));
1904 event.eapol_tx_status.dst = data;
1905 event.eapol_tx_status.data = data + 14;
1906 event.eapol_tx_status.data_len = res - 14;
1907 event.eapol_tx_status.ack = acked;
1908 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1909}
1910
1911
1912static int nl80211_init_bss(struct i802_bss *bss)
1913{
1914 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1915 if (!bss->nl_cb)
1916 return -1;
1917
1918 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1919 no_seq_check, NULL);
1920 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1921 process_bss_event, bss);
1922
1923 return 0;
1924}
1925
1926
1927static void nl80211_destroy_bss(struct i802_bss *bss)
1928{
1929 nl_cb_put(bss->nl_cb);
1930 bss->nl_cb = NULL;
1931}
1932
1933
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001934static void
1935wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1936{
1937 struct rfkill_config *rcfg;
1938
1939 if (drv->rfkill)
1940 return;
1941
1942 rcfg = os_zalloc(sizeof(*rcfg));
1943 if (!rcfg)
1944 return;
1945
1946 rcfg->ctx = drv;
1947
1948 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1949 * not associated with a netdev, so use the name of some other interface
1950 * sharing the same wiphy as the P2P Device interface.
1951 *
1952 * Note: This is valid, as a P2P Device interface is always dynamically
1953 * created and is created only once another wpa_s interface was added.
1954 */
1955 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1956 struct nl80211_global *global = drv->global;
1957 struct wpa_driver_nl80211_data *tmp1;
1958
1959 dl_list_for_each(tmp1, &global->interfaces,
1960 struct wpa_driver_nl80211_data, list) {
1961 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1962 !tmp1->rfkill)
1963 continue;
1964
1965 wpa_printf(MSG_DEBUG,
1966 "nl80211: Use (%s) to initialize P2P Device rfkill",
1967 tmp1->first_bss->ifname);
1968 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1969 sizeof(rcfg->ifname));
1970 break;
1971 }
1972 } else {
1973 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1974 sizeof(rcfg->ifname));
1975 }
1976
1977 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1978 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1979 drv->rfkill = rfkill_init(rcfg);
1980 if (!drv->rfkill) {
1981 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1982 os_free(rcfg);
1983 }
1984}
1985
1986
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001987static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1988 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001989 const u8 *set_addr,
1990 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991{
1992 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 struct i802_bss *bss;
1994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001995 if (global_priv == NULL)
1996 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 drv = os_zalloc(sizeof(*drv));
1998 if (drv == NULL)
1999 return NULL;
2000 drv->global = global_priv;
2001 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002002 drv->hostapd = !!hostapd;
2003 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002004
2005 /*
2006 * There is no driver capability flag for this, so assume it is
2007 * supported and disable this on first attempt to use if the driver
2008 * rejects the command due to missing support.
2009 */
2010 drv->set_rekey_offload = 1;
2011
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002012 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2013 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002014 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002015
2016 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
2017 if (!drv->first_bss) {
2018 os_free(drv);
2019 return NULL;
2020 }
2021 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002023 bss->ctx = ctx;
2024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2026 drv->monitor_ifidx = -1;
2027 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002028 drv->eapol_tx_sock = -1;
2029 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002031 if (nl80211_init_bss(bss))
2032 goto failed;
2033
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002034 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 goto failed;
2036
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002037 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
2038 if (drv->eapol_tx_sock < 0)
2039 goto failed;
2040
2041 if (drv->data_tx_status) {
2042 int enabled = 1;
2043
2044 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2045 &enabled, sizeof(enabled)) < 0) {
2046 wpa_printf(MSG_DEBUG,
2047 "nl80211: wifi status sockopt failed\n");
2048 drv->data_tx_status = 0;
2049 if (!drv->use_monitor)
2050 drv->capa.flags &=
2051 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2052 } else {
2053 eloop_register_read_sock(drv->eapol_tx_sock,
2054 wpa_driver_nl80211_handle_eapol_tx_status,
2055 drv, NULL);
2056 }
2057 }
2058
2059 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002060 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062 drv->in_interface_list = 1;
2063 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064
2065 return bss;
2066
2067failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 return NULL;
2070}
2071
2072
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002073/**
2074 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2075 * @ctx: context to be used when calling wpa_supplicant functions,
2076 * e.g., wpa_supplicant_event()
2077 * @ifname: interface name, e.g., wlan0
2078 * @global_priv: private driver global data from global_init()
2079 * Returns: Pointer to private data, %NULL on failure
2080 */
2081static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2082 void *global_priv)
2083{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002084 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
2085 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002086}
2087
2088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002089static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 struct nl_handle *nl_handle,
2091 u16 type, const u8 *match, size_t match_len)
2092{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002095 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002096 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002098 buf[0] = '\0';
2099 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002100 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
2101 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002103 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
2104 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
2105 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
2106 nlmsg_free(msg);
2107 return -1;
2108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 if (ret) {
2112 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2113 "failed (type=%u): ret=%d (%s)",
2114 type, ret, strerror(-ret));
2115 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2116 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 return ret;
2119}
2120
2121
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002122static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
2123{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002124 if (bss->nl_mgmt) {
2125 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
2126 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2127 return -1;
2128 }
2129
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002130 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131 if (bss->nl_mgmt == NULL)
2132 return -1;
2133
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002134 return 0;
2135}
2136
2137
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002138static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2139{
2140 nl80211_register_eloop_read(&bss->nl_mgmt,
2141 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002142 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002143}
2144
2145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002146static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 const u8 *match, size_t match_len)
2148{
2149 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002150 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 type, match, match_len);
2152}
2153
2154
Roshan Pius3a1667e2018-07-03 15:17:14 -07002155static int nl80211_init_connect_handle(struct i802_bss *bss)
2156{
2157 if (bss->nl_connect) {
2158 wpa_printf(MSG_DEBUG,
2159 "nl80211: Connect handle already created (nl_connect=%p)",
2160 bss->nl_connect);
2161 return -1;
2162 }
2163
2164 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2165 if (!bss->nl_connect)
2166 return -1;
2167 nl80211_register_eloop_read(&bss->nl_connect,
2168 wpa_driver_nl80211_event_receive,
2169 bss->nl_cb, 1);
2170 return 0;
2171}
2172
2173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002174static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002176 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002177 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002178
2179 if (nl80211_alloc_mgmt_handle(bss))
2180 return -1;
2181 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2182 "handle %p", bss->nl_mgmt);
2183
Roshan Pius3a1667e2018-07-03 15:17:14 -07002184 if (drv->nlmode == NL80211_IFTYPE_ADHOC ||
2185 ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2186 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002187 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2188
2189 /* register for any AUTH message */
2190 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
2191 }
2192
Dmitry Shmidt051af732013-10-22 13:52:46 -07002193#ifdef CONFIG_INTERWORKING
2194 /* QoS Map Configure */
2195 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002196 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002197#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002198#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002200 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002201 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002203 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002204 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002207 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002209 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002210 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002211 /* Protected GAS Initial Request */
2212 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2213 ret = -1;
2214 /* Protected GAS Initial Response */
2215 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2216 ret = -1;
2217 /* Protected GAS Comeback Request */
2218 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2219 ret = -1;
2220 /* Protected GAS Comeback Response */
2221 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2222 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002223#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002224#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002226 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2228 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002229 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 (u8 *) "\x7f\x50\x6f\x9a\x09",
2233 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002234 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235#endif /* CONFIG_P2P */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002236#ifdef CONFIG_DPP
2237 /* DPP Public Action */
2238 if (nl80211_register_action_frame(bss,
2239 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2240 6) < 0)
2241 ret = -1;
2242#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243#ifdef CONFIG_IEEE80211W
Hai Shalom74f70d42019-02-11 14:42:39 -08002244#ifdef CONFIG_OCV
2245 /* SA Query Request */
2246 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x00", 2) < 0)
2247 ret = -1;
2248#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002251 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002253#ifdef CONFIG_TDLS
2254 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2255 /* TDLS Discovery Response */
2256 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2257 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002258 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002259 }
2260#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002261#ifdef CONFIG_FST
2262 /* FST Action frames */
2263 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2264 ret = -1;
2265#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
2267 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002268 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002269 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 else
2271 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2272 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002274 /* WNM - BSS Transition Management Request */
2275 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002276 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002277 /* WNM-Sleep Mode Response */
2278 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002279 ret = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002280#ifdef CONFIG_WNM
2281 /* WNM - Collocated Interference Request */
2282 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x0b", 2) < 0)
2283 ret = -1;
2284#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002285
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002286#ifdef CONFIG_HS20
2287 /* WNM-Notification */
2288 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002289 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002290#endif /* CONFIG_HS20 */
2291
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002292 /* WMM-AC ADDTS Response */
2293 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2294 ret = -1;
2295
2296 /* WMM-AC DELTS */
2297 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2298 ret = -1;
2299
2300 /* Radio Measurement - Neighbor Report Response */
2301 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2302 ret = -1;
2303
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002304 /* Radio Measurement - Radio Measurement Request */
2305 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2306 ret = -1;
2307
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002308 /* Radio Measurement - Link Measurement Request */
2309 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2310 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2311 ret = -1;
2312
2313 nl80211_mgmt_handle_register_eloop(bss);
2314
2315 return ret;
2316}
2317
2318
2319static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2320{
2321 int ret = 0;
2322
2323 if (nl80211_alloc_mgmt_handle(bss))
2324 return -1;
2325
2326 wpa_printf(MSG_DEBUG,
2327 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2328 bss->nl_mgmt);
2329
2330 /* Auth frames for mesh SAE */
2331 if (nl80211_register_frame(bss, bss->nl_mgmt,
2332 (WLAN_FC_TYPE_MGMT << 2) |
2333 (WLAN_FC_STYPE_AUTH << 4),
2334 NULL, 0) < 0)
2335 ret = -1;
2336
2337 /* Mesh peering open */
2338 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2339 ret = -1;
2340 /* Mesh peering confirm */
2341 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2342 ret = -1;
2343 /* Mesh peering close */
2344 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2345 ret = -1;
2346
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002347 nl80211_mgmt_handle_register_eloop(bss);
2348
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002349 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350}
2351
2352
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002353static int nl80211_register_spurious_class3(struct i802_bss *bss)
2354{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002356 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002357
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002358 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2359 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360 if (ret) {
2361 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2362 "failed: ret=%d (%s)",
2363 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002364 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002365 return ret;
2366}
2367
2368
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002369static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2370{
2371 int ret = 0;
2372
2373 /* Public Action frames */
2374 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2375 ret = -1;
2376 /* RRM Measurement Report */
2377 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2378 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002379 /* RRM Link Measurement Report */
2380 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2381 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002382 /* RRM Neighbor Report Request */
2383 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2384 ret = -1;
2385 /* FT Action frames */
2386 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2387 ret = -1;
2388#ifdef CONFIG_IEEE80211W
2389 /* SA Query */
2390 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2391 ret = -1;
2392#endif /* CONFIG_IEEE80211W */
2393 /* Protected Dual of Public Action */
2394 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2395 ret = -1;
2396 /* WNM */
2397 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2398 ret = -1;
2399 /* WMM */
2400 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2401 ret = -1;
2402#ifdef CONFIG_FST
2403 /* FST Action frames */
2404 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2405 ret = -1;
2406#endif /* CONFIG_FST */
2407 /* Vendor-specific */
2408 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2409 ret = -1;
2410
2411 return ret;
2412}
2413
2414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002415static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2416{
2417 static const int stypes[] = {
2418 WLAN_FC_STYPE_AUTH,
2419 WLAN_FC_STYPE_ASSOC_REQ,
2420 WLAN_FC_STYPE_REASSOC_REQ,
2421 WLAN_FC_STYPE_DISASSOC,
2422 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002423 WLAN_FC_STYPE_PROBE_REQ,
2424/* Beacon doesn't work as mac80211 doesn't currently allow
2425 * it, but it wouldn't really be the right thing anyway as
2426 * it isn't per interface ... maybe just dump the scan
2427 * results periodically for OLBC?
2428 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002429 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002430 };
2431 unsigned int i;
2432
2433 if (nl80211_alloc_mgmt_handle(bss))
2434 return -1;
2435 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2436 "handle %p", bss->nl_mgmt);
2437
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002438 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 if (nl80211_register_frame(bss, bss->nl_mgmt,
2440 (WLAN_FC_TYPE_MGMT << 2) |
2441 (stypes[i] << 4),
2442 NULL, 0) < 0) {
2443 goto out_err;
2444 }
2445 }
2446
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002447 if (nl80211_action_subscribe_ap(bss))
2448 goto out_err;
2449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002450 if (nl80211_register_spurious_class3(bss))
2451 goto out_err;
2452
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002453 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002454 return 0;
2455
2456out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 nl_destroy_handles(&bss->nl_mgmt);
2458 return -1;
2459}
2460
2461
2462static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2463{
2464 if (nl80211_alloc_mgmt_handle(bss))
2465 return -1;
2466 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2467 "handle %p (device SME)", bss->nl_mgmt);
2468
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002469 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002470 goto out_err;
2471
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002472 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002473 return 0;
2474
2475out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002476 nl_destroy_handles(&bss->nl_mgmt);
2477 return -1;
2478}
2479
2480
2481static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2482{
2483 if (bss->nl_mgmt == NULL)
2484 return;
2485 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2486 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002487 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002488
2489 nl80211_put_wiphy_data_ap(bss);
2490}
2491
2492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2494{
2495 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2496}
2497
2498
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002499static void nl80211_del_p2pdev(struct i802_bss *bss)
2500{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002501 struct nl_msg *msg;
2502 int ret;
2503
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002504 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2505 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002506
2507 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2508 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002509 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002510}
2511
2512
2513static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2514{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002515 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002516 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002518 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2519 NL80211_CMD_STOP_P2P_DEVICE);
2520 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002521
2522 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2523 start ? "Start" : "Stop",
2524 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002525 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002526 return ret;
2527}
2528
2529
2530static int i802_set_iface_flags(struct i802_bss *bss, int up)
2531{
2532 enum nl80211_iftype nlmode;
2533
2534 nlmode = nl80211_get_ifmode(bss);
2535 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2536 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2537 bss->ifname, up);
2538 }
2539
2540 /* P2P Device has start/stop which is equivalent */
2541 return nl80211_set_p2pdev(bss, up);
2542}
2543
2544
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002545#ifdef CONFIG_TESTING_OPTIONS
2546static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2547{
2548 /* struct wpa_driver_nl80211_data *drv = arg; */
2549 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2550 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2551
2552
2553 wpa_printf(MSG_DEBUG,
2554 "nl80211: QCA vendor test command response received");
2555
2556 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2557 genlmsg_attrlen(gnlh, 0), NULL);
2558 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2559 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2560 return NL_SKIP;
2561 }
2562
2563 wpa_hexdump(MSG_DEBUG,
2564 "nl80211: Received QCA vendor test command response",
2565 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2566 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2567
2568 return NL_SKIP;
2569}
2570#endif /* CONFIG_TESTING_OPTIONS */
2571
2572
2573static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2574{
2575#ifdef CONFIG_TESTING_OPTIONS
2576 struct nl_msg *msg;
2577 struct nlattr *params;
2578 int ret;
2579
2580 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2581 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2582 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2583 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2584 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2585 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2586 nlmsg_free(msg);
2587 return;
2588 }
2589 nla_nest_end(msg, params);
2590
2591 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2592 wpa_printf(MSG_DEBUG,
2593 "nl80211: QCA vendor test command returned %d (%s)",
2594 ret, strerror(-ret));
2595#endif /* CONFIG_TESTING_OPTIONS */
2596}
2597
2598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002600wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002601 const u8 *set_addr, int first,
2602 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002604 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002606 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607
2608 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002609 bss->ifindex = drv->ifindex;
2610 bss->wdev_id = drv->global->if_add_wdevid;
2611 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2612
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002613 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2614 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002615 drv->global->if_add_wdevid_set = 0;
2616
Dmitry Shmidt03658832014-08-13 11:03:49 -07002617 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2618 bss->static_ap = 1;
2619
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002620 if (first &&
2621 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2622 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2623 drv->start_iface_up = 1;
2624
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002625 if (wpa_driver_nl80211_capa(drv))
2626 return -1;
2627
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002628 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2629 return -1;
2630
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002631 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2632 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002634 if (set_addr &&
2635 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2636 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2637 set_addr)))
2638 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002639
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002640 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2641 drv->start_mode_ap = 1;
2642
Dmitry Shmidt03658832014-08-13 11:03:49 -07002643 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002644 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002645 else if (bss->if_dynamic ||
2646 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002647 nlmode = nl80211_get_ifmode(bss);
2648 else
2649 nlmode = NL80211_IFTYPE_STATION;
2650
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002651 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002652 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002653 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002655
Dmitry Shmidt98660862014-03-11 17:26:21 -07002656 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002657 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002659 wpa_driver_nl80211_drv_init_rfkill(drv);
2660
Dmitry Shmidt98660862014-03-11 17:26:21 -07002661 if (!rfkill_is_blocked(drv->rfkill)) {
2662 int ret = i802_set_iface_flags(bss, 1);
2663 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2665 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002666 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002668
2669 if (is_p2p_net_interface(nlmode))
2670 nl80211_disable_11b_rates(bss->drv,
2671 bss->drv->ifindex, 1);
2672
Dmitry Shmidt98660862014-03-11 17:26:21 -07002673 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2674 return ret;
2675 } else {
2676 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2677 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002678 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2679 drv->if_disabled = 1;
2680
Dmitry Shmidt98660862014-03-11 17:26:21 -07002681 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 }
2683
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002684 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002685 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2686 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002688 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2689 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2690 bss->addr))
2691 return -1;
2692 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695 if (send_rfkill_event) {
2696 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2697 drv, drv->ctx);
2698 }
2699
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002700 if (drv->vendor_cmd_test_avail)
2701 qca_vendor_test(drv);
2702
Roshan Pius3a1667e2018-07-03 15:17:14 -07002703 nl80211_init_connect_handle(bss);
2704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 return 0;
2706}
2707
2708
Paul Stewart092955c2017-02-06 09:13:09 -08002709static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710{
2711 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08002712 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002714 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2715 drv->ifindex);
Paul Stewart092955c2017-02-06 09:13:09 -08002716 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002717 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719}
2720
2721
2722/**
2723 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002724 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 *
2726 * Shut down driver interface and processing of driver events. Free
2727 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2728 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002729static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002732 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002734 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2735 bss->ifname, drv->disabled_11b_rates);
2736
Dmitry Shmidt04949592012-07-19 12:16:46 -07002737 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002738 if (drv->data_tx_status)
2739 eloop_unregister_read_sock(drv->eapol_tx_sock);
2740 if (drv->eapol_tx_sock >= 0)
2741 close(drv->eapol_tx_sock);
2742
2743 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 wpa_driver_nl80211_probe_req_report(bss, 0);
2745 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002746 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2747 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2749 "interface %s from bridge %s: %s",
2750 bss->ifname, bss->brname, strerror(errno));
2751 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07002752
2753 if (drv->rtnl_sk)
2754 nl80211_handle_destroy(drv->rtnl_sk);
2755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002757 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2758 0) < 0)
2759 wpa_printf(MSG_INFO,
2760 "nl80211: Could not set bridge %s down",
2761 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002762 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2764 "bridge %s: %s",
2765 bss->brname, strerror(errno));
2766 }
2767
2768 nl80211_remove_monitor_interface(drv);
2769
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002770 if (is_ap_interface(drv->nlmode))
Paul Stewart092955c2017-02-06 09:13:09 -08002771 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 if (drv->eapol_sock >= 0) {
2774 eloop_unregister_read_sock(drv->eapol_sock);
2775 close(drv->eapol_sock);
2776 }
2777
2778 if (drv->if_indices != drv->default_if_indices)
2779 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002781 if (drv->if_indices_reason != drv->default_if_indices_reason)
2782 os_free(drv->if_indices_reason);
2783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002784 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2786
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002787 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2788 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002789 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 rfkill_deinit(drv->rfkill);
2791
2792 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2793
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002794 if (!drv->start_iface_up)
2795 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002796
2797 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002798 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2799 0) < 0) {
2800 wpa_printf(MSG_DEBUG,
2801 "nl80211: Could not set interface down to restore permanent MAC address");
2802 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002803 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2804 drv->perm_addr) < 0) {
2805 wpa_printf(MSG_DEBUG,
2806 "nl80211: Could not restore permanent MAC address");
2807 }
2808 }
2809
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002810 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002811 if (!drv->hostapd || !drv->start_mode_ap)
2812 wpa_driver_nl80211_set_mode(bss,
2813 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002814 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002815 } else {
2816 nl80211_mgmt_unsubscribe(bss, "deinit");
2817 nl80211_del_p2pdev(bss);
2818 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002819
Roshan Pius3a1667e2018-07-03 15:17:14 -07002820 if (bss->nl_connect)
2821 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
2822
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002823 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 os_free(drv->filter_ssids);
2826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002827 os_free(drv->auth_ie);
2828
2829 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 dl_list_del(&drv->list);
2831
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002832 os_free(drv->extended_capa);
2833 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002834 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2835 os_free(drv->iface_ext_capa[i].ext_capa);
2836 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2837 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002838 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839 os_free(drv);
2840}
2841
2842
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002843static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2844{
2845 switch (alg) {
2846 case WPA_ALG_WEP:
2847 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08002848 return RSN_CIPHER_SUITE_WEP40;
2849 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002850 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002851 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002852 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002853 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002854 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002855 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002856 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002857 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002858 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002859 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002860 case WPA_ALG_IGTK:
Paul Stewart092955c2017-02-06 09:13:09 -08002861 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002862 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002863 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002864 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002865 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002866 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002867 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002868 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08002869 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002870 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08002871 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002872 case WPA_ALG_NONE:
2873 case WPA_ALG_PMK:
2874 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2875 alg);
2876 return 0;
2877 }
2878
2879 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2880 alg);
2881 return 0;
2882}
2883
2884
2885static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2886{
2887 switch (cipher) {
2888 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002889 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002890 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002891 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002892 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002893 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002894 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002895 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002896 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002897 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002898 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08002899 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002900 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08002901 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002902 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08002903 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002904 }
2905
2906 return 0;
2907}
2908
2909
2910static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2911 int max_suites)
2912{
2913 int num_suites = 0;
2914
2915 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002916 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002917 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002918 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002919 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002920 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002921 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002922 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002923 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08002924 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002925 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08002926 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002927 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08002928 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002929
2930 return num_suites;
2931}
2932
2933
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002934#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002935static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2936 const u8 *key, size_t key_len)
2937{
2938 struct nl_msg *msg;
2939 int ret;
2940
2941 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2942 return 0;
2943
2944 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2945 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2946 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2947 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2948 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2949 nl80211_nlmsg_clear(msg);
2950 nlmsg_free(msg);
2951 return -1;
2952 }
2953 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2954 if (ret) {
2955 wpa_printf(MSG_DEBUG,
2956 "nl80211: Key management set key failed: ret=%d (%s)",
2957 ret, strerror(-ret));
2958 }
2959
2960 return ret;
2961}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002962#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002963
2964
Roshan Pius3a1667e2018-07-03 15:17:14 -07002965static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
2966 const u8 *key, size_t key_len,
2967 const u8 *addr)
2968{
2969 struct nl_msg *msg = NULL;
2970 int ret;
2971
2972 /*
2973 * If the authenticator address is not set, assume it is
2974 * the current BSSID.
2975 */
2976 if (!addr && drv->associated)
2977 addr = drv->bssid;
2978 else if (!addr)
2979 return -1;
2980
2981 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
2982 MAC2STR(addr));
2983 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
2984 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
2985 if (!msg ||
2986 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
2987 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
2988 nl80211_nlmsg_clear(msg);
2989 nlmsg_free(msg);
2990 return -ENOBUFS;
2991 }
2992
2993 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2994 if (ret) {
2995 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
2996 ret, strerror(-ret));
2997 }
2998
2999 return ret;
3000}
3001
3002
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003003static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003004 enum wpa_alg alg, const u8 *addr,
3005 int key_idx, int set_tx,
3006 const u8 *seq, size_t seq_len,
3007 const u8 *key, size_t key_len)
3008{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003009 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003010 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003011 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003013 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003015 /* Ignore for P2P Device */
3016 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
3017 return 0;
3018
3019 ifindex = if_nametoindex(ifname);
3020 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003022 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003024#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003025 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003026 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003027 tdls = 1;
3028 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003029#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003031#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003032 if (alg == WPA_ALG_PMK &&
3033 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
3034 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
3035 __func__);
3036 ret = issue_key_mgmt_set_key(drv, key, key_len);
3037 return ret;
3038 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003039#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040
Roshan Pius3a1667e2018-07-03 15:17:14 -07003041 if (alg == WPA_ALG_PMK &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003042 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003043 return nl80211_set_pmk(drv, key, key_len, addr);
3044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003046 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
3047 if (!msg)
3048 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003050 u32 suite;
3051
3052 suite = wpa_alg_to_cipher_suite(alg, key_len);
3053 if (!suite)
3054 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003055 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
3056 if (!msg ||
3057 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003058 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003059 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003060 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 }
3062
Dmitry Shmidt98660862014-03-11 17:26:21 -07003063 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003064 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
3065 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003066 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
3067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
3069 if (addr && !is_broadcast_ether_addr(addr)) {
3070 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003071 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
3072 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073
3074 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
3075 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003076 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
3077 NL80211_KEYTYPE_GROUP))
3078 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 }
3080 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003081 struct nlattr *types;
3082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003084
3085 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003086 if (!types ||
3087 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
3088 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003089 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003091 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3092 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003094 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3096 ret = 0;
3097 if (ret)
3098 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
3099 ret, strerror(-ret));
3100
3101 /*
3102 * If we failed or don't need to set the default TX key (below),
3103 * we're done here.
3104 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07003105 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003107 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108 !is_broadcast_ether_addr(addr))
3109 return ret;
3110
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003111 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
3112 if (!msg ||
3113 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003114 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
3115 alg == WPA_ALG_BIP_GMAC_128 ||
3116 alg == WPA_ALG_BIP_GMAC_256 ||
3117 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003118 NL80211_ATTR_KEY_DEFAULT_MGMT :
3119 NL80211_ATTR_KEY_DEFAULT))
3120 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003122 struct nlattr *types;
3123
3124 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003125 if (!types ||
3126 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
3127 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003128 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003130 struct nlattr *types;
3131
3132 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003133 if (!types ||
3134 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
3135 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003136 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 }
3138
3139 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3140 if (ret == -ENOENT)
3141 ret = 0;
3142 if (ret)
3143 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
3144 "err=%d %s)", ret, strerror(-ret));
3145 return ret;
3146
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003147fail:
3148 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003149 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 return -ENOBUFS;
3151}
3152
3153
3154static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3155 int key_idx, int defkey,
3156 const u8 *seq, size_t seq_len,
3157 const u8 *key, size_t key_len)
3158{
3159 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003160 u32 suite;
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 if (!key_attr)
3163 return -1;
3164
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003165 suite = wpa_alg_to_cipher_suite(alg, key_len);
3166 if (!suite)
3167 return -1;
3168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003169 if (defkey && alg == WPA_ALG_IGTK) {
3170 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3171 return -1;
3172 } else if (defkey) {
3173 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3174 return -1;
3175 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003176
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003177 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003178 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003179 (seq && seq_len &&
3180 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3181 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3182 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183
3184 nla_nest_end(msg, key_attr);
3185
3186 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187}
3188
3189
3190static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3191 struct nl_msg *msg)
3192{
3193 int i, privacy = 0;
3194 struct nlattr *nl_keys, *nl_key;
3195
3196 for (i = 0; i < 4; i++) {
3197 if (!params->wep_key[i])
3198 continue;
3199 privacy = 1;
3200 break;
3201 }
3202 if (params->wps == WPS_MODE_PRIVACY)
3203 privacy = 1;
3204 if (params->pairwise_suite &&
3205 params->pairwise_suite != WPA_CIPHER_NONE)
3206 privacy = 1;
3207
3208 if (!privacy)
3209 return 0;
3210
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003211 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3212 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213
3214 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3215 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003216 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217
3218 for (i = 0; i < 4; i++) {
3219 if (!params->wep_key[i])
3220 continue;
3221
3222 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003223 if (!nl_key ||
3224 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3225 params->wep_key[i]) ||
3226 nla_put_u32(msg, NL80211_KEY_CIPHER,
3227 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003228 RSN_CIPHER_SUITE_WEP40 :
3229 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003230 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3231 (i == params->wep_tx_keyidx &&
3232 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3233 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234
3235 nla_nest_end(msg, nl_key);
3236 }
3237 nla_nest_end(msg, nl_keys);
3238
3239 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240}
3241
3242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003243int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3244 const u8 *addr, int cmd, u16 reason_code,
Hai Shalom74f70d42019-02-11 14:42:39 -08003245 int local_state_change,
3246 struct nl_handle *nl_connect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003248 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 struct nl_msg *msg;
3250
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003251 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3252 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3253 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3254 (local_state_change &&
3255 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3256 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003259
Hai Shalom74f70d42019-02-11 14:42:39 -08003260 if (nl_connect)
3261 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
3262 else
3263 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003265 wpa_dbg(drv->ctx, MSG_DEBUG,
3266 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3267 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 return ret;
3270}
3271
3272
3273static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Hai Shalom74f70d42019-02-11 14:42:39 -08003274 int reason_code,
3275 struct nl_handle *nl_connect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003277 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003278 int drv_associated = drv->associated;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003279
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003280 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003281 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003282 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003283 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Hai Shalom74f70d42019-02-11 14:42:39 -08003284 reason_code, 0, nl_connect);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003285 /*
3286 * For locally generated disconnect, supplicant already generates a
3287 * DEAUTH event, so ignore the event from NL80211.
3288 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003289 drv->ignore_next_local_disconnect = drv_associated && (ret == 0);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003290
3291 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292}
3293
3294
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003295static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3296 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003299 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003300 int drv_associated = drv->associated;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003301
3302 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3303 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003304 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003305 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003306 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
3307 struct nl_handle *nl_connect = NULL;
3308
3309 if (bss->use_nl_connect)
3310 nl_connect = bss->nl_connect;
3311 return wpa_driver_nl80211_disconnect(drv, reason_code,
3312 nl_connect);
3313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3315 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003316 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003317 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
Hai Shalom74f70d42019-02-11 14:42:39 -08003318 reason_code, 0, NULL);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003319 /*
3320 * For locally generated deauthenticate, supplicant already generates a
3321 * DEAUTH event, so ignore the event from NL80211.
3322 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003323 drv->ignore_next_local_deauth = drv_associated && (ret == 0);
3324
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003325 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326}
3327
3328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003329static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3330 struct wpa_driver_auth_params *params)
3331{
3332 int i;
3333
3334 drv->auth_freq = params->freq;
3335 drv->auth_alg = params->auth_alg;
3336 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3337 drv->auth_local_state_change = params->local_state_change;
3338 drv->auth_p2p = params->p2p;
3339
3340 if (params->bssid)
3341 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3342 else
3343 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3344
3345 if (params->ssid) {
3346 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3347 drv->auth_ssid_len = params->ssid_len;
3348 } else
3349 drv->auth_ssid_len = 0;
3350
3351
3352 os_free(drv->auth_ie);
3353 drv->auth_ie = NULL;
3354 drv->auth_ie_len = 0;
3355 if (params->ie) {
3356 drv->auth_ie = os_malloc(params->ie_len);
3357 if (drv->auth_ie) {
3358 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3359 drv->auth_ie_len = params->ie_len;
3360 }
3361 }
3362
3363 for (i = 0; i < 4; i++) {
3364 if (params->wep_key[i] && params->wep_key_len[i] &&
3365 params->wep_key_len[i] <= 16) {
3366 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3367 params->wep_key_len[i]);
3368 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3369 } else
3370 drv->auth_wep_key_len[i] = 0;
3371 }
3372}
3373
3374
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003375static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3376{
3377 struct wpa_driver_nl80211_data *drv = bss->drv;
3378
3379 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3380 return;
3381
3382 /*
3383 * Looks like we failed to unmask 11b rates previously. This could
3384 * happen, e.g., if the interface was down at the point in time when a
3385 * P2P group was terminated.
3386 */
3387 wpa_printf(MSG_DEBUG,
3388 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3389 bss->ifname);
3390 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3391}
3392
3393
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003394static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3395{
3396 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3397 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3398 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3399 return NL80211_AUTHTYPE_SHARED_KEY;
3400 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3401 return NL80211_AUTHTYPE_NETWORK_EAP;
3402 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3403 return NL80211_AUTHTYPE_FT;
3404 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3405 return NL80211_AUTHTYPE_SAE;
3406 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3407 return NL80211_AUTHTYPE_FILS_SK;
3408 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3409 return NL80211_AUTHTYPE_FILS_SK_PFS;
3410
3411 return NL80211_AUTHTYPE_MAX;
3412}
3413
3414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003416 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 struct wpa_driver_nl80211_data *drv = bss->drv;
3419 int ret = -1, i;
3420 struct nl_msg *msg;
3421 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003422 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003424 int is_retry;
3425
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003426 nl80211_unmask_11b_rates(bss);
3427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003428 is_retry = drv->retry_auth;
3429 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003430 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003432 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003433 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003434 if (params->bssid)
3435 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3436 else
3437 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003439 nlmode = params->p2p ?
3440 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3441 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003442 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 return -1;
3444
3445retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3447 drv->ifindex);
3448
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003449 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3450 if (!msg)
3451 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452
3453 for (i = 0; i < 4; i++) {
3454 if (!params->wep_key[i])
3455 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003456 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 NULL, i,
3458 i == params->wep_tx_keyidx, NULL, 0,
3459 params->wep_key[i],
3460 params->wep_key_len[i]);
3461 if (params->wep_tx_keyidx != i)
3462 continue;
3463 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003464 params->wep_key[i], params->wep_key_len[i]))
3465 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003466 }
3467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468 if (params->bssid) {
3469 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3470 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003471 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3472 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473 }
3474 if (params->freq) {
3475 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003476 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3477 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478 }
3479 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003480 wpa_printf(MSG_DEBUG, " * SSID=%s",
3481 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003482 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3483 params->ssid))
3484 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 }
3486 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003487 if (params->ie &&
3488 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3489 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003490 if (params->auth_data) {
3491 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3492 params->auth_data_len);
3493 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3494 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003495 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003496 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003497 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003499 if (type == NL80211_AUTHTYPE_MAX ||
3500 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003501 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 if (params->local_state_change) {
3503 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003504 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3505 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 }
3507
3508 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3509 msg = NULL;
3510 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003511 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003512 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
3513 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003515 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
3516 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517 /*
3518 * mac80211 does not currently accept new
3519 * authentication if we are already authenticated. As a
3520 * workaround, force deauthentication and try again.
3521 */
3522 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3523 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003524 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 wpa_driver_nl80211_deauthenticate(
3526 bss, params->bssid,
3527 WLAN_REASON_PREV_AUTH_NOT_VALID);
3528 nlmsg_free(msg);
3529 goto retry;
3530 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003531
3532 if (ret == -ENOENT && params->freq && !is_retry) {
3533 /*
3534 * cfg80211 has likely expired the BSS entry even
3535 * though it was previously available in our internal
3536 * BSS table. To recover quickly, start a single
3537 * channel scan on the specified channel.
3538 */
3539 struct wpa_driver_scan_params scan;
3540 int freqs[2];
3541
3542 os_memset(&scan, 0, sizeof(scan));
3543 scan.num_ssids = 1;
3544 if (params->ssid) {
3545 scan.ssids[0].ssid = params->ssid;
3546 scan.ssids[0].ssid_len = params->ssid_len;
3547 }
3548 freqs[0] = params->freq;
3549 freqs[1] = 0;
3550 scan.freqs = freqs;
3551 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3552 "channel scan to refresh cfg80211 BSS "
3553 "entry");
3554 ret = wpa_driver_nl80211_scan(bss, &scan);
3555 if (ret == 0) {
3556 nl80211_copy_auth_params(drv, params);
3557 drv->scan_for_auth = 1;
3558 }
3559 } else if (is_retry) {
3560 /*
3561 * Need to indicate this with an event since the return
3562 * value from the retry is not delivered to core code.
3563 */
3564 union wpa_event_data event;
3565 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3566 "failed");
3567 os_memset(&event, 0, sizeof(event));
3568 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3569 ETH_ALEN);
3570 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3571 &event);
3572 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003573 } else {
3574 wpa_printf(MSG_DEBUG,
3575 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003578fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 nlmsg_free(msg);
3580 return ret;
3581}
3582
3583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003584int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585{
3586 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003587 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003588 int i;
3589
3590 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3591
3592 os_memset(&params, 0, sizeof(params));
3593 params.freq = drv->auth_freq;
3594 params.auth_alg = drv->auth_alg;
3595 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3596 params.local_state_change = drv->auth_local_state_change;
3597 params.p2p = drv->auth_p2p;
3598
3599 if (!is_zero_ether_addr(drv->auth_bssid_))
3600 params.bssid = drv->auth_bssid_;
3601
3602 if (drv->auth_ssid_len) {
3603 params.ssid = drv->auth_ssid;
3604 params.ssid_len = drv->auth_ssid_len;
3605 }
3606
3607 params.ie = drv->auth_ie;
3608 params.ie_len = drv->auth_ie_len;
3609
3610 for (i = 0; i < 4; i++) {
3611 if (drv->auth_wep_key_len[i]) {
3612 params.wep_key[i] = drv->auth_wep_key[i];
3613 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3614 }
3615 }
3616
3617 drv->retry_auth = 1;
3618 return wpa_driver_nl80211_authenticate(bss, &params);
3619}
3620
3621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003622static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3623 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003624 int encrypt, int noack,
3625 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003626 int offchanok, unsigned int wait_time,
3627 const u16 *csa_offs,
3628 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003629{
3630 struct wpa_driver_nl80211_data *drv = bss->drv;
3631 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003632 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003633
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003634 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3635 freq = nl80211_get_assoc_freq(drv);
3636 wpa_printf(MSG_DEBUG,
3637 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3638 freq);
3639 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003640 if (freq == 0) {
3641 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3642 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003643 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003644 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003645
Dmitry Shmidt56052862013-10-04 10:23:25 -07003646 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003647 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003648 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003649 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003650 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003651
Dmitry Shmidt56052862013-10-04 10:23:25 -07003652 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003653 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003654 &cookie, no_cck, noack, offchanok,
3655 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003656 if (res == 0 && !noack) {
3657 const struct ieee80211_mgmt *mgmt;
3658 u16 fc;
3659
3660 mgmt = (const struct ieee80211_mgmt *) data;
3661 fc = le_to_host16(mgmt->frame_control);
3662 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3663 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3664 wpa_printf(MSG_MSGDUMP,
3665 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3666 (long long unsigned int)
3667 drv->send_action_cookie,
3668 (long long unsigned int) cookie);
3669 drv->send_action_cookie = cookie;
3670 }
3671 }
3672
3673 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003674}
3675
3676
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003677static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3678 size_t data_len, int noack,
3679 unsigned int freq, int no_cck,
3680 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003681 unsigned int wait_time,
3682 const u16 *csa_offs,
3683 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 struct wpa_driver_nl80211_data *drv = bss->drv;
3686 struct ieee80211_mgmt *mgmt;
3687 int encrypt = 1;
3688 u16 fc;
3689
3690 mgmt = (struct ieee80211_mgmt *) data;
3691 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003692 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3693 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3694 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3695 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003696
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003697 if ((is_sta_interface(drv->nlmode) ||
3698 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3700 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3701 /*
3702 * The use of last_mgmt_freq is a bit of a hack,
3703 * but it works due to the single-threaded nature
3704 * of wpa_supplicant.
3705 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003706 if (freq == 0) {
3707 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3708 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003709 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003710 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003711 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003712 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003713 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003715
3716 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003717 if (freq == 0) {
3718 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3719 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003720 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003721 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003722 return nl80211_send_frame_cmd(bss, freq,
3723 (int) freq == bss->freq ? 0 :
3724 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003725 data, data_len,
3726 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003727 no_cck, noack, offchanok,
3728 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003729 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3732 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3733 /*
3734 * Only one of the authentication frame types is encrypted.
3735 * In order for static WEP encryption to work properly (i.e.,
3736 * to not encrypt the frame), we need to tell mac80211 about
3737 * the frames that must not be encrypted.
3738 */
3739 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3740 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3741 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3742 encrypt = 0;
3743 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003744
Dmitry Shmidt56052862013-10-04 10:23:25 -07003745 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003746 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003747 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003748 wait_time, csa_offs,
3749 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003750}
3751
3752
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003753static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3754{
3755 u8 rates[NL80211_MAX_SUPP_RATES];
3756 u8 rates_len = 0;
3757 int i;
3758
3759 if (!basic_rates)
3760 return 0;
3761
3762 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3763 rates[rates_len++] = basic_rates[i] / 5;
3764
3765 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3766}
3767
3768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003769static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3770 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003771 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003772{
3773 struct wpa_driver_nl80211_data *drv = bss->drv;
3774 struct nl_msg *msg;
3775
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003776 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3777 (cts >= 0 &&
3778 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3779 (preamble >= 0 &&
3780 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3781 (slot >= 0 &&
3782 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3783 (ht_opmode >= 0 &&
3784 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3785 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003786 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3787 nl80211_put_basic_rates(msg, basic_rates)) {
3788 nlmsg_free(msg);
3789 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003790 }
3791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003792 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003793}
3794
3795
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003796static int wpa_driver_nl80211_set_acl(void *priv,
3797 struct hostapd_acl_params *params)
3798{
3799 struct i802_bss *bss = priv;
3800 struct wpa_driver_nl80211_data *drv = bss->drv;
3801 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003802 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003803 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003804 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003805
3806 if (!(drv->capa.max_acl_mac_addrs))
3807 return -ENOTSUP;
3808
3809 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3810 return -ENOTSUP;
3811
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003812 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3813 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3814
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003815 acl = nlmsg_alloc();
3816 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003817 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003818 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003819 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3820 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003821 return -ENOMEM;
3822 }
3823 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003824
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003825 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3826 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3827 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3828 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3829 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3830 nlmsg_free(msg);
3831 nlmsg_free(acl);
3832 return -ENOMEM;
3833 }
3834 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003835
3836 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003837 if (ret) {
3838 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3839 ret, strerror(-ret));
3840 }
3841
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003842 return ret;
3843}
3844
3845
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003846static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3847{
3848 if (beacon_int > 0) {
3849 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3850 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3851 beacon_int);
3852 }
3853
3854 return 0;
3855}
3856
3857
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003858static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3859{
3860 if (dtim_period > 0) {
3861 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3862 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3863 }
3864
3865 return 0;
3866}
3867
3868
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003869#ifdef CONFIG_MESH
3870static int nl80211_set_mesh_config(void *priv,
3871 struct wpa_driver_mesh_bss_params *params)
3872{
3873 struct i802_bss *bss = priv;
3874 struct wpa_driver_nl80211_data *drv = bss->drv;
3875 struct nl_msg *msg;
3876 int ret;
3877
3878 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3879 if (!msg)
3880 return -1;
3881
3882 ret = nl80211_put_mesh_config(msg, params);
3883 if (ret < 0) {
3884 nlmsg_free(msg);
3885 return ret;
3886 }
3887
3888 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3889 if (ret) {
3890 wpa_printf(MSG_ERROR,
3891 "nl80211: Mesh config set failed: %d (%s)",
3892 ret, strerror(-ret));
3893 return ret;
3894 }
3895 return 0;
3896}
3897#endif /* CONFIG_MESH */
3898
3899
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003900static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3901 struct wpa_driver_ap_params *params)
3902{
3903 struct nlattr *bands, *band;
3904 struct nl80211_txrate_vht vht_rate;
3905
3906 if (!params->freq ||
3907 (params->beacon_rate == 0 &&
3908 params->rate_type == BEACON_RATE_LEGACY))
3909 return 0;
3910
3911 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3912 if (!bands)
3913 return -1;
3914
3915 switch (params->freq->mode) {
3916 case HOSTAPD_MODE_IEEE80211B:
3917 case HOSTAPD_MODE_IEEE80211G:
3918 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3919 break;
3920 case HOSTAPD_MODE_IEEE80211A:
3921 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3922 break;
3923 case HOSTAPD_MODE_IEEE80211AD:
3924 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3925 break;
3926 default:
3927 return 0;
3928 }
3929
3930 if (!band)
3931 return -1;
3932
3933 os_memset(&vht_rate, 0, sizeof(vht_rate));
3934
3935 switch (params->rate_type) {
3936 case BEACON_RATE_LEGACY:
3937 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3938 wpa_printf(MSG_INFO,
3939 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3940 return -1;
3941 }
3942
3943 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3944 (u8) params->beacon_rate / 5) ||
3945 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3946 (params->freq->vht_enabled &&
3947 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3948 &vht_rate)))
3949 return -1;
3950
3951 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3952 params->beacon_rate);
3953 break;
3954 case BEACON_RATE_HT:
3955 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3956 wpa_printf(MSG_INFO,
3957 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3958 return -1;
3959 }
3960 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3961 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3962 (params->freq->vht_enabled &&
3963 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3964 &vht_rate)))
3965 return -1;
3966 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3967 params->beacon_rate);
3968 break;
3969 case BEACON_RATE_VHT:
3970 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3971 wpa_printf(MSG_INFO,
3972 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3973 return -1;
3974 }
3975 vht_rate.mcs[0] = BIT(params->beacon_rate);
3976 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3977 return -1;
3978 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3979 return -1;
3980 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3981 &vht_rate))
3982 return -1;
3983 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3984 params->beacon_rate);
3985 break;
3986 }
3987
3988 nla_nest_end(msg, band);
3989 nla_nest_end(msg, bands);
3990
3991 return 0;
3992}
3993
3994
3995static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3996 int multicast_to_unicast)
3997{
3998 struct wpa_driver_nl80211_data *drv = bss->drv;
3999 struct nl_msg *msg;
4000 int ret;
4001
4002 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
4003 if (!msg ||
4004 (multicast_to_unicast &&
4005 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
4006 wpa_printf(MSG_ERROR,
4007 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
4008 bss->ifname);
4009 nlmsg_free(msg);
4010 return -ENOBUFS;
4011 }
4012
4013 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4014
4015 switch (ret) {
4016 case 0:
4017 wpa_printf(MSG_DEBUG,
4018 "nl80211: multicast to unicast %s on interface %s",
4019 multicast_to_unicast ? "enabled" : "disabled",
4020 bss->ifname);
4021 break;
4022 case -EOPNOTSUPP:
4023 if (!multicast_to_unicast)
4024 break;
4025 wpa_printf(MSG_INFO,
4026 "nl80211: multicast to unicast not supported on interface %s",
4027 bss->ifname);
4028 break;
4029 default:
4030 wpa_printf(MSG_ERROR,
4031 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
4032 multicast_to_unicast ? "enabling" : "disabling",
4033 ret, strerror(-ret), bss->ifname);
4034 break;
4035 }
4036
4037 return ret;
4038}
4039
4040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004041static int wpa_driver_nl80211_set_ap(void *priv,
4042 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043{
4044 struct i802_bss *bss = priv;
4045 struct wpa_driver_nl80211_data *drv = bss->drv;
4046 struct nl_msg *msg;
4047 u8 cmd = NL80211_CMD_NEW_BEACON;
Hai Shalom74f70d42019-02-11 14:42:39 -08004048 int ret = -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004050 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004051 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004052 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004053 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004054#ifdef CONFIG_MESH
4055 struct wpa_driver_mesh_bss_params mesh_params;
4056#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004058 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
4061 beacon_set);
4062 if (beacon_set)
4063 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08004064 else if (!drv->device_ap_sme && !drv->use_monitor &&
4065 !nl80211_get_wiphy_data_ap(bss))
4066 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004068 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
4069 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004070 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
4071 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004072 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004073 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004074 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
4075 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004076 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Hai Shalom74f70d42019-02-11 14:42:39 -08004077 wpa_printf(MSG_DEBUG, "nl80211: ssid=%s",
4078 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004079 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
4080 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
4081 params->head) ||
4082 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
4083 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004084 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004085 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004086 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004087 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4088 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004089 if (params->proberesp && params->proberesp_len) {
4090 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
4091 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004092 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
4093 params->proberesp))
4094 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004095 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004096 switch (params->hide_ssid) {
4097 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004098 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004099 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4100 NL80211_HIDDEN_SSID_NOT_IN_USE))
4101 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004102 break;
4103 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004104 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004105 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4106 NL80211_HIDDEN_SSID_ZERO_LEN))
4107 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004108 break;
4109 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004110 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004111 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
4112 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
4113 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004114 break;
4115 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004116 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004117 if (params->privacy &&
4118 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
4119 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004120 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004121 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
4122 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
4123 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004124 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
4125 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4126 NL80211_AUTHTYPE_SHARED_KEY))
4127 goto fail;
4128 } else {
4129 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
4130 NL80211_AUTHTYPE_OPEN_SYSTEM))
4131 goto fail;
4132 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004133
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004134 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004135 ver = 0;
4136 if (params->wpa_version & WPA_PROTO_WPA)
4137 ver |= NL80211_WPA_VERSION_1;
4138 if (params->wpa_version & WPA_PROTO_RSN)
4139 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004140 if (ver &&
4141 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4142 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004144 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
4145 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004146 num_suites = 0;
4147 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
Paul Stewart092955c2017-02-06 09:13:09 -08004148 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004149 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
Paul Stewart092955c2017-02-06 09:13:09 -08004150 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004151 if (num_suites &&
4152 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
4153 suites))
4154 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004155
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004156 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004157 (!params->pairwise_ciphers ||
4158 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
4159 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
4160 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004161 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004162
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004163 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4164 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004165 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4166 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004167 if (num_suites &&
4168 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4169 num_suites * sizeof(u32), suites))
4170 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004171
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004172 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4173 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004174 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004175 if (suite &&
4176 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4177 goto fail;
4178
Dmitry Shmidte4663042016-04-04 10:07:49 -07004179 if (params->ht_opmode != -1) {
4180 switch (params->smps_mode) {
4181 case HT_CAP_INFO_SMPS_DYNAMIC:
4182 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
4183 smps_mode = NL80211_SMPS_DYNAMIC;
4184 break;
4185 case HT_CAP_INFO_SMPS_STATIC:
4186 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
4187 smps_mode = NL80211_SMPS_STATIC;
4188 break;
4189 default:
4190 /* invalid - fallback to smps off */
4191 case HT_CAP_INFO_SMPS_DISABLED:
4192 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
4193 smps_mode = NL80211_SMPS_OFF;
4194 break;
4195 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004196 if (nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
Dmitry Shmidte4663042016-04-04 10:07:49 -07004197 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004198 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004199
4200 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004201 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4202 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004203 if (nla_put(msg, NL80211_ATTR_IE,
4204 wpabuf_len(params->beacon_ies),
4205 wpabuf_head(params->beacon_ies)))
4206 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004207 }
4208 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004209 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4210 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004211 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4212 wpabuf_len(params->proberesp_ies),
4213 wpabuf_head(params->proberesp_ies)))
4214 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004215 }
4216 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004217 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4218 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004219 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4220 wpabuf_len(params->assocresp_ies),
4221 wpabuf_head(params->assocresp_ies)))
4222 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004223 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224
Dmitry Shmidt04949592012-07-19 12:16:46 -07004225 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004226 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4227 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004228 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4229 params->ap_max_inactivity))
4230 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004231 }
4232
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004233#ifdef CONFIG_P2P
4234 if (params->p2p_go_ctwindow > 0) {
4235 if (drv->p2p_go_ctwindow_supported) {
4236 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4237 params->p2p_go_ctwindow);
4238 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4239 params->p2p_go_ctwindow))
4240 goto fail;
4241 } else {
4242 wpa_printf(MSG_INFO,
4243 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4244 }
4245 }
4246#endif /* CONFIG_P2P */
4247
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004248 if (params->pbss) {
4249 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4250 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4251 goto fail;
4252 }
4253
Hai Shalom74f70d42019-02-11 14:42:39 -08004254 if (params->ftm_responder) {
4255 struct nlattr *ftm;
4256
4257 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
4258 ret = -ENOTSUP;
4259 goto fail;
4260 }
4261
4262 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
4263 if (!ftm ||
4264 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
4265 (params->lci &&
4266 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
4267 wpabuf_len(params->lci),
4268 wpabuf_head(params->lci))) ||
4269 (params->civic &&
4270 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
4271 wpabuf_len(params->civic),
4272 wpabuf_head(params->civic))))
4273 goto fail;
4274 nla_nest_end(msg, ftm);
4275 }
4276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4278 if (ret) {
4279 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4280 ret, strerror(-ret));
4281 } else {
4282 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4284 params->short_slot_time, params->ht_opmode,
4285 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004286 nl80211_set_multicast_to_unicast(bss,
4287 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004288 if (beacon_set && params->freq &&
4289 params->freq->bandwidth != bss->bandwidth) {
4290 wpa_printf(MSG_DEBUG,
4291 "nl80211: Update BSS %s bandwidth: %d -> %d",
4292 bss->ifname, bss->bandwidth,
4293 params->freq->bandwidth);
4294 ret = nl80211_set_channel(bss, params->freq, 1);
4295 if (ret) {
4296 wpa_printf(MSG_DEBUG,
4297 "nl80211: Frequency set failed: %d (%s)",
4298 ret, strerror(-ret));
4299 } else {
4300 wpa_printf(MSG_DEBUG,
4301 "nl80211: Frequency set succeeded for ht2040 coex");
4302 bss->bandwidth = params->freq->bandwidth;
4303 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004304 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004305 /*
4306 * cfg80211 updates the driver on frequence change in AP
4307 * mode only at the point when beaconing is started, so
4308 * set the initial value here.
4309 */
4310 bss->bandwidth = params->freq->bandwidth;
4311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004313
4314#ifdef CONFIG_MESH
4315 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4316 os_memset(&mesh_params, 0, sizeof(mesh_params));
4317 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4318 mesh_params.ht_opmode = params->ht_opmode;
4319 ret = nl80211_set_mesh_config(priv, &mesh_params);
4320 if (ret < 0)
4321 return ret;
4322 }
4323#endif /* CONFIG_MESH */
4324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004326fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004327 nlmsg_free(msg);
Hai Shalom74f70d42019-02-11 14:42:39 -08004328 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329}
4330
4331
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004332static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004333 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004335 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004336 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4337 return -ENOBUFS;
4338
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004339 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4340 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4341
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004342 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004343 enum nl80211_chan_width cw;
4344
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004345 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004346 switch (freq->bandwidth) {
4347 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004348 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004349 break;
4350 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004351 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004352 break;
4353 case 80:
4354 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004355 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004356 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004357 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004358 break;
4359 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004360 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004361 break;
4362 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004363 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004364 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004365
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004366 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4367 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4368 freq->center_freq1);
4369 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4370 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004371 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4372 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4373 freq->center_freq1) ||
4374 (freq->center_freq2 &&
4375 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4376 freq->center_freq2)))
4377 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004378 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004379 enum nl80211_channel_type ct;
4380
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004381 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4382 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004383 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004385 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 break;
4387 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004388 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 break;
4390 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004391 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392 break;
4393 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004394
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004395 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004396 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4397 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004398 } else {
4399 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4400 NL80211_CHAN_NO_HT);
4401 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4402 NL80211_CHAN_NO_HT))
4403 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004405 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004406}
4407
4408
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004409static int nl80211_set_channel(struct i802_bss *bss,
4410 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004411{
4412 struct wpa_driver_nl80211_data *drv = bss->drv;
4413 struct nl_msg *msg;
4414 int ret;
4415
4416 wpa_printf(MSG_DEBUG,
4417 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4418 freq->freq, freq->ht_enabled, freq->vht_enabled,
4419 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004420
4421 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4422 NL80211_CMD_SET_WIPHY);
4423 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4424 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004425 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004426 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427
4428 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004429 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004430 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004434 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 return -1;
4436}
4437
4438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004439static u32 sta_flags_nl80211(int flags)
4440{
4441 u32 f = 0;
4442
4443 if (flags & WPA_STA_AUTHORIZED)
4444 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4445 if (flags & WPA_STA_WMM)
4446 f |= BIT(NL80211_STA_FLAG_WME);
4447 if (flags & WPA_STA_SHORT_PREAMBLE)
4448 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4449 if (flags & WPA_STA_MFP)
4450 f |= BIT(NL80211_STA_FLAG_MFP);
4451 if (flags & WPA_STA_TDLS_PEER)
4452 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004453 if (flags & WPA_STA_AUTHENTICATED)
4454 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004455 if (flags & WPA_STA_ASSOCIATED)
4456 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004457
4458 return f;
4459}
4460
4461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004462#ifdef CONFIG_MESH
4463static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4464{
4465 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004466 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004467 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004468 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004469 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004470 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004471 return NL80211_PLINK_OPN_RCVD;
4472 case PLINK_CNF_RCVD:
4473 return NL80211_PLINK_CNF_RCVD;
4474 case PLINK_ESTAB:
4475 return NL80211_PLINK_ESTAB;
4476 case PLINK_HOLDING:
4477 return NL80211_PLINK_HOLDING;
4478 case PLINK_BLOCKED:
4479 return NL80211_PLINK_BLOCKED;
4480 default:
4481 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4482 state);
4483 }
4484 return -1;
4485}
4486#endif /* CONFIG_MESH */
4487
4488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489static int wpa_driver_nl80211_sta_add(void *priv,
4490 struct hostapd_sta_add_params *params)
4491{
4492 struct i802_bss *bss = priv;
4493 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004494 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004495 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496 int ret = -ENOBUFS;
4497
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004498 if ((params->flags & WPA_STA_TDLS_PEER) &&
4499 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4500 return -EOPNOTSUPP;
4501
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004502 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4503 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004504 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4505 NL80211_CMD_NEW_STATION);
4506 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4507 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004509 /*
4510 * Set the below properties only in one of the following cases:
4511 * 1. New station is added, already associated.
4512 * 2. Set WPA_STA_TDLS_PEER station.
4513 * 3. Set an already added unassociated station, if driver supports
4514 * full AP client state. (Set these properties after station became
4515 * associated will be rejected by the driver).
4516 */
4517 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4518 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4519 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004520 wpa_hexdump(MSG_DEBUG, " * supported rates",
4521 params->supp_rates, params->supp_rates_len);
4522 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4523 params->capability);
4524 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4525 params->supp_rates_len, params->supp_rates) ||
4526 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4527 params->capability))
4528 goto fail;
4529
4530 if (params->ht_capabilities) {
4531 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4532 (u8 *) params->ht_capabilities,
4533 sizeof(*params->ht_capabilities));
4534 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4535 sizeof(*params->ht_capabilities),
4536 params->ht_capabilities))
4537 goto fail;
4538 }
4539
4540 if (params->vht_capabilities) {
4541 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4542 (u8 *) params->vht_capabilities,
4543 sizeof(*params->vht_capabilities));
4544 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4545 sizeof(*params->vht_capabilities),
4546 params->vht_capabilities))
4547 goto fail;
4548 }
4549
4550 if (params->ext_capab) {
4551 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4552 params->ext_capab, params->ext_capab_len);
4553 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4554 params->ext_capab_len, params->ext_capab))
4555 goto fail;
4556 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004557
4558 if (is_ap_interface(drv->nlmode) &&
4559 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4560 params->support_p2p_ps ?
4561 NL80211_P2P_PS_SUPPORTED :
4562 NL80211_P2P_PS_UNSUPPORTED))
4563 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004564 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004565 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004566 if (params->aid) {
4567 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004568 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4569 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004570 } else {
4571 /*
4572 * cfg80211 validates that AID is non-zero, so we have
4573 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004574 * a dummy STA entry is used for now and for a station
4575 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004576 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004577 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4578 (params->flags & WPA_STA_TDLS_PEER) ?
4579 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004580 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4581 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004582 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004583 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4584 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004585 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4586 params->listen_interval))
4587 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004588 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4589 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004590 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4591 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004592 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4593 (params->flags & WPA_STA_ASSOCIATED)) {
4594 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4595 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4596 params->listen_interval);
4597 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4598 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4599 params->listen_interval))
4600 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004601 }
4602
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004603 if (params->vht_opmode_enabled) {
4604 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004605 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4606 params->vht_opmode))
4607 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004608 }
4609
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004610 if (params->supp_channels) {
4611 wpa_hexdump(MSG_DEBUG, " * supported channels",
4612 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004613 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4614 params->supp_channels_len, params->supp_channels))
4615 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004616 }
4617
4618 if (params->supp_oper_classes) {
4619 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4620 params->supp_oper_classes,
4621 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004622 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4623 params->supp_oper_classes_len,
4624 params->supp_oper_classes))
4625 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004626 }
4627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004628 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004629 upd.set = sta_flags_nl80211(params->flags);
4630 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004631
4632 /*
4633 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4634 * flags, as nl80211 driver moves a new station, by default, into
4635 * associated state.
4636 *
4637 * On the other hand, if the driver supports that feature and the
4638 * station is added in unauthenticated state, set the
4639 * authenticated/associated bits in the mask to prevent moving this
4640 * station to associated state before it is actually associated.
4641 *
4642 * This is irrelevant for mesh mode where the station is added to the
4643 * driver as authenticated already, and ASSOCIATED isn't part of the
4644 * nl80211 API.
4645 */
4646 if (!is_mesh_interface(drv->nlmode)) {
4647 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4648 wpa_printf(MSG_DEBUG,
4649 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4650 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4651 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4652 } else if (!params->set &&
4653 !(params->flags & WPA_STA_TDLS_PEER)) {
4654 if (!(params->flags & WPA_STA_AUTHENTICATED))
4655 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4656 if (!(params->flags & WPA_STA_ASSOCIATED))
4657 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4658 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004659#ifdef CONFIG_MESH
4660 } else {
4661 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4662 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4663 params->peer_aid);
4664 if (ret)
4665 goto fail;
4666 }
4667#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004668 }
4669
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004670 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4671 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004672 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4673 goto fail;
4674
4675#ifdef CONFIG_MESH
4676 if (params->plink_state &&
4677 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4678 sta_plink_state_nl80211(params->plink_state)))
4679 goto fail;
4680#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004681
Hai Shalom021b0b52019-04-10 11:17:58 -07004682 if ((!params->set || FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) &&
4683 (params->flags & WPA_STA_WMM)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004684 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4685
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004686 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004687 if (!wme ||
4688 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4689 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4690 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4691 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4692 WMM_QOSINFO_STA_SP_MASK))
4693 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004694 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004695 }
4696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004698 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004700 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4701 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4702 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 if (ret == -EEXIST)
4704 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004705fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004706 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 return ret;
4708}
4709
4710
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004711static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4712{
4713#ifdef CONFIG_LIBNL3_ROUTE
4714 struct wpa_driver_nl80211_data *drv = bss->drv;
4715 struct rtnl_neigh *rn;
4716 struct nl_addr *nl_addr;
4717 int err;
4718
4719 rn = rtnl_neigh_alloc();
4720 if (!rn)
4721 return;
4722
4723 rtnl_neigh_set_family(rn, AF_BRIDGE);
4724 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4725 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4726 if (!nl_addr) {
4727 rtnl_neigh_put(rn);
4728 return;
4729 }
4730 rtnl_neigh_set_lladdr(rn, nl_addr);
4731
4732 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4733 if (err < 0) {
4734 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4735 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4736 bss->ifindex, nl_geterror(err));
4737 } else {
4738 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4739 MACSTR, MAC2STR(addr));
4740 }
4741
4742 nl_addr_put(nl_addr);
4743 rtnl_neigh_put(rn);
4744#endif /* CONFIG_LIBNL3_ROUTE */
4745}
4746
4747
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004748static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4749 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751 struct wpa_driver_nl80211_data *drv = bss->drv;
4752 struct nl_msg *msg;
4753 int ret;
4754
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004755 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4756 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4757 (deauth == 0 &&
4758 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4759 WLAN_FC_STYPE_DISASSOC)) ||
4760 (deauth == 1 &&
4761 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4762 WLAN_FC_STYPE_DEAUTH)) ||
4763 (reason_code &&
4764 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4765 nlmsg_free(msg);
4766 return -ENOBUFS;
4767 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768
4769 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004770 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4771 " --> %d (%s)",
4772 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004773
4774 if (drv->rtnl_sk)
4775 rtnl_neigh_delete_fdb_entry(bss, addr);
4776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777 if (ret == -ENOENT)
4778 return 0;
4779 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780}
4781
4782
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004783void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784{
4785 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004786 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787
4788 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004791 dl_list_for_each(drv2, &drv->global->interfaces,
4792 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004793 {
4794 del_ifidx(drv2, ifidx, IFIDX_ANY);
4795 /* Remove all bridges learned for this iface */
4796 del_ifidx(drv2, IFIDX_ANY, ifidx);
4797 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004799 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4801 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4803}
4804
4805
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004806const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004807{
4808 switch (mode) {
4809 case NL80211_IFTYPE_ADHOC:
4810 return "ADHOC";
4811 case NL80211_IFTYPE_STATION:
4812 return "STATION";
4813 case NL80211_IFTYPE_AP:
4814 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004815 case NL80211_IFTYPE_AP_VLAN:
4816 return "AP_VLAN";
4817 case NL80211_IFTYPE_WDS:
4818 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004819 case NL80211_IFTYPE_MONITOR:
4820 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004821 case NL80211_IFTYPE_MESH_POINT:
4822 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004823 case NL80211_IFTYPE_P2P_CLIENT:
4824 return "P2P_CLIENT";
4825 case NL80211_IFTYPE_P2P_GO:
4826 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004827 case NL80211_IFTYPE_P2P_DEVICE:
4828 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004829 default:
4830 return "unknown";
4831 }
4832}
4833
4834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4836 const char *ifname,
4837 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004838 const u8 *addr, int wds,
4839 int (*handler)(struct nl_msg *, void *),
4840 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004842 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 int ifidx;
4844 int ret = -ENOBUFS;
4845
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004846 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4847 iftype, nl80211_iftype_str(iftype));
4848
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004849 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4850 if (!msg ||
4851 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4852 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4853 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854
4855 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004856 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004858 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004859 if (!flags ||
4860 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4861 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004863 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004865 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4866 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 }
4868
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004869 /*
4870 * Tell cfg80211 that the interface belongs to the socket that created
4871 * it, and the interface should be deleted when the socket is closed.
4872 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004873 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4874 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004875
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004876 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004877 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004879 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004880 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4882 ifname, ret, strerror(-ret));
4883 return ret;
4884 }
4885
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004886 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4887 return 0;
4888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 ifidx = if_nametoindex(ifname);
4890 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4891 ifname, ifidx);
4892
4893 if (ifidx <= 0)
4894 return -1;
4895
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004896 /*
4897 * Some virtual interfaces need to process EAPOL packets and events on
4898 * the parent interface. This is used mainly with hostapd.
4899 */
4900 if (drv->hostapd ||
4901 iftype == NL80211_IFTYPE_AP_VLAN ||
4902 iftype == NL80211_IFTYPE_WDS ||
4903 iftype == NL80211_IFTYPE_MONITOR) {
4904 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004905 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907
4908 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004909 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 nl80211_remove_iface(drv, ifidx);
4911 return -1;
4912 }
4913
4914 return ifidx;
4915}
4916
4917
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004918int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4919 const char *ifname, enum nl80211_iftype iftype,
4920 const u8 *addr, int wds,
4921 int (*handler)(struct nl_msg *, void *),
4922 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923{
4924 int ret;
4925
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004926 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4927 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004929 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004931 if (use_existing) {
4932 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4933 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004934 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4935 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4936 addr) < 0 &&
4937 (linux_set_iface_flags(drv->global->ioctl_sock,
4938 ifname, 0) < 0 ||
4939 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4940 addr) < 0 ||
4941 linux_set_iface_flags(drv->global->ioctl_sock,
4942 ifname, 1) < 0))
4943 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004944 return -ENFILE;
4945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4947
4948 /* Try to remove the interface that was already there. */
4949 nl80211_remove_iface(drv, if_nametoindex(ifname));
4950
4951 /* Try to create the interface again */
4952 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004953 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954 }
4955
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004956 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4957 wpa_printf(MSG_DEBUG,
4958 "nl80211: Interface %s created for P2P - disable 11b rates",
4959 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004961 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962
4963 return ret;
4964}
4965
4966
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004967static int nl80211_setup_ap(struct i802_bss *bss)
4968{
4969 struct wpa_driver_nl80211_data *drv = bss->drv;
4970
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004971 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4972 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004973
4974 /*
4975 * Disable Probe Request reporting unless we need it in this way for
4976 * devices that include the AP SME, in the other case (unless using
4977 * monitor iface) we'll get it through the nl_mgmt socket instead.
4978 */
4979 if (!drv->device_ap_sme)
4980 wpa_driver_nl80211_probe_req_report(bss, 0);
4981
4982 if (!drv->device_ap_sme && !drv->use_monitor)
4983 if (nl80211_mgmt_subscribe_ap(bss))
4984 return -1;
4985
4986 if (drv->device_ap_sme && !drv->use_monitor)
4987 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004988 wpa_printf(MSG_DEBUG,
4989 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004990
4991 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004992 nl80211_create_monitor_interface(drv) &&
4993 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004994 return -1;
4995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004996 if (drv->device_ap_sme &&
4997 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4998 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4999 "Probe Request frame reporting in AP mode");
5000 /* Try to survive without this */
5001 }
5002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005003 return 0;
5004}
5005
5006
5007static void nl80211_teardown_ap(struct i802_bss *bss)
5008{
5009 struct wpa_driver_nl80211_data *drv = bss->drv;
5010
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005011 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
5012 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005013 if (drv->device_ap_sme) {
5014 wpa_driver_nl80211_probe_req_report(bss, 0);
5015 if (!drv->use_monitor)
5016 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
5017 } else if (drv->use_monitor)
5018 nl80211_remove_monitor_interface(drv);
5019 else
5020 nl80211_mgmt_unsubscribe(bss, "AP teardown");
5021
Paul Stewart092955c2017-02-06 09:13:09 -08005022 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005023 bss->beacon_set = 0;
5024}
5025
5026
5027static int nl80211_send_eapol_data(struct i802_bss *bss,
5028 const u8 *addr, const u8 *data,
5029 size_t data_len)
5030{
5031 struct sockaddr_ll ll;
5032 int ret;
5033
5034 if (bss->drv->eapol_tx_sock < 0) {
5035 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
5036 return -1;
5037 }
5038
5039 os_memset(&ll, 0, sizeof(ll));
5040 ll.sll_family = AF_PACKET;
5041 ll.sll_ifindex = bss->ifindex;
5042 ll.sll_protocol = htons(ETH_P_PAE);
5043 ll.sll_halen = ETH_ALEN;
5044 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
5045 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
5046 (struct sockaddr *) &ll, sizeof(ll));
5047 if (ret < 0)
5048 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
5049 strerror(errno));
5050
5051 return ret;
5052}
5053
5054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
5056
5057static int wpa_driver_nl80211_hapd_send_eapol(
5058 void *priv, const u8 *addr, const u8 *data,
5059 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
5060{
5061 struct i802_bss *bss = priv;
5062 struct wpa_driver_nl80211_data *drv = bss->drv;
5063 struct ieee80211_hdr *hdr;
5064 size_t len;
5065 u8 *pos;
5066 int res;
5067 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08005068
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 if (drv->device_ap_sme || !drv->use_monitor)
5070 return nl80211_send_eapol_data(bss, addr, data, data_len);
5071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
5073 data_len;
5074 hdr = os_zalloc(len);
5075 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005076 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
5077 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 return -1;
5079 }
5080
5081 hdr->frame_control =
5082 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
5083 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
5084 if (encrypt)
5085 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
5086 if (qos) {
5087 hdr->frame_control |=
5088 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
5089 }
5090
5091 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
5092 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
5093 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
5094 pos = (u8 *) (hdr + 1);
5095
5096 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005097 /* Set highest priority in QoS header */
5098 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005099 pos[1] = 0;
5100 pos += 2;
5101 }
5102
5103 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5104 pos += sizeof(rfc1042_header);
5105 WPA_PUT_BE16(pos, ETH_P_PAE);
5106 pos += 2;
5107 memcpy(pos, data, data_len);
5108
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005109 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005110 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111 if (res < 0) {
5112 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
5113 "failed: %d (%s)",
5114 (unsigned long) len, errno, strerror(errno));
5115 }
5116 os_free(hdr);
5117
5118 return res;
5119}
5120
5121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005123 unsigned int total_flags,
5124 unsigned int flags_or,
5125 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126{
5127 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005128 struct nl_msg *msg;
5129 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 struct nl80211_sta_flag_update upd;
5131
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005132 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
5133 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
5134 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
5135 !!(total_flags & WPA_STA_AUTHORIZED));
5136
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005137 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5138 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
5139 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140
5141 /*
5142 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5143 * can be removed eventually.
5144 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005145 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005146 if (!flags ||
5147 ((total_flags & WPA_STA_AUTHORIZED) &&
5148 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
5149 ((total_flags & WPA_STA_WMM) &&
5150 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
5151 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
5152 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
5153 ((total_flags & WPA_STA_MFP) &&
5154 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
5155 ((total_flags & WPA_STA_TDLS_PEER) &&
5156 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
5157 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005158
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005159 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160
5161 os_memset(&upd, 0, sizeof(upd));
5162 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5163 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005164 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5165 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005167 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
5168fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005169 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 return -ENOBUFS;
5171}
5172
5173
5174static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5175 struct wpa_driver_associate_params *params)
5176{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005177 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005178
5179 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5181 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005182 nlmode = NL80211_IFTYPE_P2P_GO;
5183 } else
5184 nlmode = NL80211_IFTYPE_AP;
5185
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005186 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005187 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005188 nl80211_remove_monitor_interface(drv);
5189 return -1;
5190 }
5191
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005192 if (params->freq.freq &&
5193 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005194 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005195 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196 nl80211_remove_monitor_interface(drv);
5197 return -1;
5198 }
5199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 return 0;
5201}
5202
5203
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005204static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5205 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206{
5207 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005208 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005210 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005212 if (ret) {
5213 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5214 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005215 } else {
5216 wpa_printf(MSG_DEBUG,
5217 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005218 }
5219
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005220 if (reset_mode &&
5221 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005222 NL80211_IFTYPE_STATION)) {
5223 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5224 "station mode");
5225 }
5226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005227 return ret;
5228}
5229
5230
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005231static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5232 struct wpa_driver_associate_params *params)
5233{
5234 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5235 return -1;
5236
5237 if (params->htcaps && params->htcaps_mask) {
5238 int sz = sizeof(struct ieee80211_ht_capabilities);
5239 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5240 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5241 params->htcaps_mask, sz);
5242 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5243 params->htcaps) ||
5244 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5245 params->htcaps_mask))
5246 return -1;
5247 }
5248
5249#ifdef CONFIG_VHT_OVERRIDES
5250 if (params->disable_vht) {
5251 wpa_printf(MSG_DEBUG, " * VHT disabled");
5252 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5253 return -1;
5254 }
5255
5256 if (params->vhtcaps && params->vhtcaps_mask) {
5257 int sz = sizeof(struct ieee80211_vht_capabilities);
5258 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5259 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5260 params->vhtcaps_mask, sz);
5261 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5262 params->vhtcaps) ||
5263 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5264 params->vhtcaps_mask))
5265 return -1;
5266 }
5267#endif /* CONFIG_VHT_OVERRIDES */
5268
5269 return 0;
5270}
5271
5272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5274 struct wpa_driver_associate_params *params)
5275{
5276 struct nl_msg *msg;
5277 int ret = -1;
5278 int count = 0;
5279
5280 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5281
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005282 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005283 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5284 "IBSS mode");
5285 return -1;
5286 }
5287
5288retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005289 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5290 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5291 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292
Hai Shalom74f70d42019-02-11 14:42:39 -08005293 wpa_printf(MSG_DEBUG, " * SSID=%s",
5294 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005295 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5296 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5298 drv->ssid_len = params->ssid_len;
5299
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005300 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5301 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005302 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303
5304 ret = nl80211_set_conn_keys(params, msg);
5305 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005306 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005308 if (params->bssid && params->fixed_bssid) {
5309 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5310 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005311 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5312 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005313 }
5314
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005315 if (params->fixed_freq) {
5316 wpa_printf(MSG_DEBUG, " * fixed_freq");
5317 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5318 goto fail;
5319 }
5320
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005321 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5322 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5323 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5324 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005325 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005326 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5327 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005328 }
5329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330 if (params->wpa_ie) {
5331 wpa_hexdump(MSG_DEBUG,
5332 " * Extra IEs for Beacon/Probe Response frames",
5333 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005334 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5335 params->wpa_ie))
5336 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337 }
5338
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005339 ret = nl80211_ht_vht_overrides(msg, params);
5340 if (ret < 0)
5341 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5344 msg = NULL;
5345 if (ret) {
5346 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5347 ret, strerror(-ret));
5348 count++;
5349 if (ret == -EALREADY && count == 1) {
5350 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5351 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005352 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353 nlmsg_free(msg);
5354 goto retry;
5355 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005356 } else {
5357 wpa_printf(MSG_DEBUG,
5358 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005360
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005361fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362 nlmsg_free(msg);
5363 return ret;
5364}
5365
5366
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005367static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5368 struct wpa_driver_associate_params *params,
5369 struct nl_msg *msg)
5370{
5371 if (params->fils_erp_username_len) {
5372 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5373 params->fils_erp_username,
5374 params->fils_erp_username_len);
5375 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5376 params->fils_erp_username_len,
5377 params->fils_erp_username))
5378 return -1;
5379 }
5380
5381 if (params->fils_erp_realm_len) {
5382 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5383 params->fils_erp_realm,
5384 params->fils_erp_realm_len);
5385 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5386 params->fils_erp_realm_len, params->fils_erp_realm))
5387 return -1;
5388 }
5389
5390 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5391 params->fils_erp_next_seq_num);
5392 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5393 params->fils_erp_next_seq_num))
5394 return -1;
5395
5396 if (params->fils_erp_rrk_len) {
5397 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5398 (unsigned long) params->fils_erp_rrk_len);
5399 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5400 params->fils_erp_rrk_len, params->fils_erp_rrk))
5401 return -1;
5402 }
5403
5404 return 0;
5405}
5406
5407
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005408static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5409 struct wpa_driver_associate_params *params,
5410 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411{
Paul Stewart092955c2017-02-06 09:13:09 -08005412 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5413 return -1;
5414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005415 if (params->bssid) {
5416 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5417 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005418 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5419 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005421
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005422 if (params->bssid_hint) {
5423 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5424 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005425 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5426 params->bssid_hint))
5427 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005428 }
5429
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005430 if (params->freq.freq) {
5431 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005432 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5433 params->freq.freq))
5434 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005435 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005436 } else
5437 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005438
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005439 if (params->freq_hint) {
5440 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005441 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5442 params->freq_hint))
5443 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005444 }
5445
Dmitry Shmidt04949592012-07-19 12:16:46 -07005446 if (params->bg_scan_period >= 0) {
5447 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5448 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005449 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5450 params->bg_scan_period))
5451 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005452 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454 if (params->ssid) {
Hai Shalom74f70d42019-02-11 14:42:39 -08005455 wpa_printf(MSG_DEBUG, " * SSID=%s",
5456 wpa_ssid_txt(params->ssid, params->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005457 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5458 params->ssid))
5459 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005461 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5463 drv->ssid_len = params->ssid_len;
5464 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005467 if (params->wpa_ie &&
5468 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5469 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005470
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005471 if (params->wpa_proto) {
5472 enum nl80211_wpa_versions ver = 0;
5473
5474 if (params->wpa_proto & WPA_PROTO_WPA)
5475 ver |= NL80211_WPA_VERSION_1;
5476 if (params->wpa_proto & WPA_PROTO_RSN)
5477 ver |= NL80211_WPA_VERSION_2;
5478
5479 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005480 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5481 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005482 }
5483
5484 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5485 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5486 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005487 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5488 cipher))
5489 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005490 }
5491
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005492 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5493 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5494 /*
5495 * This is likely to work even though many drivers do not
5496 * advertise support for operations without GTK.
5497 */
5498 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5499 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005500 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5501 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005502 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5503 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005504 }
5505
5506 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5507 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5508 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5509 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005510 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005511 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5512 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005513 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07005514 params->key_mgmt_suite == WPA_KEY_MGMT_SAE ||
5515 params->key_mgmt_suite == WPA_KEY_MGMT_FT_SAE ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005516 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005517 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
Hai Shalom021b0b52019-04-10 11:17:58 -07005518 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005519 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5520 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5521 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07005522 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5523 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5524 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
Paul Stewart092955c2017-02-06 09:13:09 -08005525 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005526
5527 switch (params->key_mgmt_suite) {
5528 case WPA_KEY_MGMT_CCKM:
Paul Stewart092955c2017-02-06 09:13:09 -08005529 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005530 break;
5531 case WPA_KEY_MGMT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005532 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005533 break;
5534 case WPA_KEY_MGMT_FT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005535 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005536 break;
5537 case WPA_KEY_MGMT_FT_PSK:
Paul Stewart092955c2017-02-06 09:13:09 -08005538 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005539 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005540 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005541 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005542 break;
5543 case WPA_KEY_MGMT_PSK_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005544 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005545 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005546 case WPA_KEY_MGMT_OSEN:
Paul Stewart092955c2017-02-06 09:13:09 -08005547 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005548 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07005549 case WPA_KEY_MGMT_SAE:
5550 mgmt = RSN_AUTH_KEY_MGMT_SAE;
5551 break;
5552 case WPA_KEY_MGMT_FT_SAE:
5553 mgmt = RSN_AUTH_KEY_MGMT_FT_SAE;
5554 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005555 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Paul Stewart092955c2017-02-06 09:13:09 -08005556 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005557 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005558 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Paul Stewart092955c2017-02-06 09:13:09 -08005559 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005560 break;
Hai Shalom021b0b52019-04-10 11:17:58 -07005561 case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
5562 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
5563 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005564 case WPA_KEY_MGMT_FILS_SHA256:
5565 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5566 break;
5567 case WPA_KEY_MGMT_FILS_SHA384:
5568 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5569 break;
5570 case WPA_KEY_MGMT_FT_FILS_SHA256:
5571 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5572 break;
5573 case WPA_KEY_MGMT_FT_FILS_SHA384:
5574 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5575 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005576 case WPA_KEY_MGMT_OWE:
5577 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5578 break;
5579 case WPA_KEY_MGMT_DPP:
5580 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5581 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005582 case WPA_KEY_MGMT_PSK:
5583 default:
Paul Stewart092955c2017-02-06 09:13:09 -08005584 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005585 break;
5586 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005587 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005588 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5589 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005590 }
5591
Hai Shalom74f70d42019-02-11 14:42:39 -08005592 if (params->req_key_mgmt_offload &&
5593 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X)) {
5594 wpa_printf(MSG_DEBUG, " * WANT_1X_4WAY_HS");
5595 if (nla_put_flag(msg, NL80211_ATTR_WANT_1X_4WAY_HS))
5596 return -1;
5597 }
5598
Roshan Pius3a1667e2018-07-03 15:17:14 -07005599 /* Add PSK in case of 4-way handshake offload */
5600 if (params->psk &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005601 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005602 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5603 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5604 return -1;
5605 }
5606
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005607 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5608 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005609
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005610 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5611 (params->pairwise_suite == WPA_CIPHER_NONE ||
5612 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5613 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5614 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5615 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5616 return -1;
5617
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005618 if (params->rrm_used) {
5619 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005620 if ((!((drv_rrm_flags &
5621 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5622 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5623 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005624 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5625 return -1;
5626 }
5627
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005628 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005629 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005630
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005631 if (params->p2p)
5632 wpa_printf(MSG_DEBUG, " * P2P group");
5633
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005634 if (params->pbss) {
5635 wpa_printf(MSG_DEBUG, " * PBSS");
5636 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5637 return -1;
5638 }
5639
Dmitry Shmidte4663042016-04-04 10:07:49 -07005640 drv->connect_reassoc = 0;
5641 if (params->prev_bssid) {
5642 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5643 MAC2STR(params->prev_bssid));
5644 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5645 params->prev_bssid))
5646 return -1;
5647 drv->connect_reassoc = 1;
5648 }
5649
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005650 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5651 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5652 return -1;
5653
Roshan Pius3a1667e2018-07-03 15:17:14 -07005654 if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5655 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5656 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5657 return -1;
5658
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005659 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005660}
5661
5662
5663static int wpa_driver_nl80211_try_connect(
5664 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005665 struct wpa_driver_associate_params *params,
5666 struct nl_handle *nl_connect)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005667{
5668 struct nl_msg *msg;
5669 enum nl80211_auth_type type;
5670 int ret;
5671 int algs;
5672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005673#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005674 if (params->req_key_mgmt_offload && params->psk &&
5675 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5676 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5677 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5678 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5679 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5680 if (ret)
5681 return ret;
5682 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005683#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005684
5685 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5686 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005687 if (!msg)
5688 return -1;
5689
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005690 ret = nl80211_connect_common(drv, params, msg);
5691 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005692 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005693
Roshan Pius3a1667e2018-07-03 15:17:14 -07005694 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5695 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5696 goto fail;
5697
5698 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
5699 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
5700 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
5701 goto fail;
5702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 algs = 0;
5704 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5705 algs++;
5706 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5707 algs++;
5708 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5709 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005710 if (params->auth_alg & WPA_AUTH_ALG_FILS)
5711 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005712 if (params->auth_alg & WPA_AUTH_ALG_FT)
5713 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714 if (algs > 1) {
5715 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5716 "selection");
5717 goto skip_auth_type;
5718 }
5719
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005720 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005722 if (type == NL80211_AUTHTYPE_MAX ||
5723 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005724 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725
5726skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727 ret = nl80211_set_conn_keys(params, msg);
5728 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005729 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730
Roshan Pius3a1667e2018-07-03 15:17:14 -07005731 if (nl_connect)
Hai Shalom74f70d42019-02-11 14:42:39 -08005732 ret = send_and_recv(drv->global, nl_connect, msg,
5733 NULL, (void *) -1);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005734 else
Hai Shalom74f70d42019-02-11 14:42:39 -08005735 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737 msg = NULL;
5738 if (ret) {
5739 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5740 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005741 } else {
5742 wpa_printf(MSG_DEBUG,
5743 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005744 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005746fail:
Hai Shalom74f70d42019-02-11 14:42:39 -08005747 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748 nlmsg_free(msg);
5749 return ret;
5750
5751}
5752
5753
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005754static int wpa_driver_nl80211_connect(
5755 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005756 struct wpa_driver_associate_params *params,
5757 struct nl_handle *nl_connect)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005758{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305759 int ret;
5760
5761 /* Store the connection attempted bssid for future use */
5762 if (params->bssid)
5763 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5764 else
5765 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5766
Roshan Pius3a1667e2018-07-03 15:17:14 -07005767 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005768 if (ret == -EALREADY) {
5769 /*
5770 * cfg80211 does not currently accept new connections if
5771 * we are already connected. As a workaround, force
5772 * disconnection and try again.
5773 */
5774 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5775 "disconnecting before reassociation "
5776 "attempt");
5777 if (wpa_driver_nl80211_disconnect(
Hai Shalom74f70d42019-02-11 14:42:39 -08005778 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, nl_connect))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005779 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005780 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005781 }
5782 return ret;
5783}
5784
5785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005786static int wpa_driver_nl80211_associate(
5787 void *priv, struct wpa_driver_associate_params *params)
5788{
5789 struct i802_bss *bss = priv;
5790 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005791 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792 struct nl_msg *msg;
5793
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005794 nl80211_unmask_11b_rates(bss);
5795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005796 if (params->mode == IEEE80211_MODE_AP)
5797 return wpa_driver_nl80211_ap(drv, params);
5798
5799 if (params->mode == IEEE80211_MODE_IBSS)
5800 return wpa_driver_nl80211_ibss(drv, params);
5801
5802 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005803 enum nl80211_iftype nlmode = params->p2p ?
5804 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005805 struct nl_handle *nl_connect = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005806
5807 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808 return -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08005809 if (params->auth_alg & WPA_AUTH_ALG_SAE) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005810 nl_connect = bss->nl_connect;
Hai Shalom74f70d42019-02-11 14:42:39 -08005811 bss->use_nl_connect = 1;
5812 } else {
5813 bss->use_nl_connect = 0;
5814 }
5815
Roshan Pius3a1667e2018-07-03 15:17:14 -07005816 return wpa_driver_nl80211_connect(drv, params, nl_connect);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817 }
5818
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005819 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005821 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5822 drv->ifindex);
5823 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005824 if (!msg)
5825 return -1;
5826
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005827 ret = nl80211_connect_common(drv, params, msg);
5828 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005829 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830
Roshan Pius3a1667e2018-07-03 15:17:14 -07005831 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5832 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5833 goto fail;
5834
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005835 if (params->fils_kek) {
5836 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5837 (unsigned int) params->fils_kek_len);
5838 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5839 params->fils_kek))
5840 goto fail;
5841 }
5842 if (params->fils_nonces) {
5843 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5844 params->fils_nonces,
5845 params->fils_nonces_len);
5846 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5847 params->fils_nonces_len, params->fils_nonces))
5848 goto fail;
5849 }
5850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5852 msg = NULL;
5853 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005854 wpa_dbg(drv->ctx, MSG_DEBUG,
5855 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5856 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005858 } else {
5859 wpa_printf(MSG_DEBUG,
5860 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005863fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 nlmsg_free(msg);
5865 return ret;
5866}
5867
5868
5869static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005870 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871{
5872 struct nl_msg *msg;
5873 int ret = -ENOBUFS;
5874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005875 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5876 ifindex, mode, nl80211_iftype_str(mode));
5877
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005878 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5879 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5880 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005881
5882 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005883 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005884 if (!ret)
5885 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005886fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005887 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5889 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5890 return ret;
5891}
5892
5893
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005894static int wpa_driver_nl80211_set_mode_impl(
5895 struct i802_bss *bss,
5896 enum nl80211_iftype nlmode,
5897 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899 struct wpa_driver_nl80211_data *drv = bss->drv;
5900 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005902 int was_ap = is_ap_interface(drv->nlmode);
5903 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005904 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005906 if (TEST_FAIL())
5907 return -1;
5908
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005909 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5910 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5911 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005912
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005913 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 drv->nlmode = nlmode;
5915 ret = 0;
5916 goto done;
5917 }
5918
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005919 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005920 return -1;
5921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922 if (nlmode == drv->nlmode) {
5923 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5924 "requested mode - ignore error");
5925 ret = 0;
5926 goto done; /* Already in the requested mode */
5927 }
5928
5929 /* mac80211 doesn't allow mode changes while the device is up, so
5930 * take the device down, try to set the mode again, and bring the
5931 * device back up.
5932 */
5933 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5934 "interface down");
5935 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005936 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005937 if (res == -EACCES || res == -ENODEV)
5938 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005939 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005940 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5941 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005942 os_sleep(0, 100000);
5943 continue;
5944 }
5945
5946 /*
5947 * Setting the mode will fail for some drivers if the phy is
5948 * on a frequency that the mode is disallowed in.
5949 */
5950 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005951 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005952 if (res) {
5953 wpa_printf(MSG_DEBUG,
5954 "nl80211: Failed to set frequency on interface");
5955 }
5956 }
5957
5958 /* Try to set the mode again while the interface is down */
5959 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5960 if (mode_switch_res == -EBUSY) {
5961 wpa_printf(MSG_DEBUG,
5962 "nl80211: Delaying mode set while interface going down");
5963 os_sleep(0, 100000);
5964 continue;
5965 }
5966 ret = mode_switch_res;
5967 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005968 }
5969
5970 if (!ret) {
5971 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5972 "interface is down");
5973 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005974 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975 }
5976
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005977 /* Bring the interface back up */
5978 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5979 if (res != 0) {
5980 wpa_printf(MSG_DEBUG,
5981 "nl80211: Failed to set interface up after switching mode");
5982 ret = -1;
5983 }
5984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005986 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5988 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005989 return ret;
5990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005992 if (is_p2p_net_interface(nlmode)) {
5993 wpa_printf(MSG_DEBUG,
5994 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5995 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005996 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005997 } else if (drv->disabled_11b_rates) {
5998 wpa_printf(MSG_DEBUG,
5999 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
6000 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006001 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006002 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006003
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006004 if (is_ap_interface(nlmode)) {
6005 nl80211_mgmt_unsubscribe(bss, "start AP");
6006 /* Setup additional AP mode functionality if needed */
6007 if (nl80211_setup_ap(bss))
6008 return -1;
6009 } else if (was_ap) {
6010 /* Remove additional AP mode functionality */
6011 nl80211_teardown_ap(bss);
6012 } else {
6013 nl80211_mgmt_unsubscribe(bss, "mode change");
6014 }
6015
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006016 if (is_mesh_interface(nlmode) &&
6017 nl80211_mgmt_subscribe_mesh(bss))
6018 return -1;
6019
Dmitry Shmidt04949592012-07-19 12:16:46 -07006020 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006021 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006022 nl80211_mgmt_subscribe_non_ap(bss) < 0)
6023 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
6024 "frame processing - ignore for now");
6025
6026 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027}
6028
6029
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006030int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
6031 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006032{
6033 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
6034}
6035
6036
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006037static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
6038 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006039{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006040 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07006041 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07006042}
6043
6044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045static int wpa_driver_nl80211_get_capa(void *priv,
6046 struct wpa_driver_capa *capa)
6047{
6048 struct i802_bss *bss = priv;
6049 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07006050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006051 if (!drv->has_capability)
6052 return -1;
6053 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006054 if (drv->extended_capa && drv->extended_capa_mask) {
6055 capa->extended_capa = drv->extended_capa;
6056 capa->extended_capa_mask = drv->extended_capa_mask;
6057 capa->extended_capa_len = drv->extended_capa_len;
6058 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006059
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006060 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061}
6062
6063
6064static int wpa_driver_nl80211_set_operstate(void *priv, int state)
6065{
6066 struct i802_bss *bss = priv;
6067 struct wpa_driver_nl80211_data *drv = bss->drv;
6068
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006069 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
6070 bss->ifname, drv->operstate, state,
6071 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006072 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006073 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074 state ? IF_OPER_UP : IF_OPER_DORMANT);
6075}
6076
6077
6078static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
6079{
6080 struct i802_bss *bss = priv;
6081 struct wpa_driver_nl80211_data *drv = bss->drv;
6082 struct nl_msg *msg;
6083 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006084 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006085
6086 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
6087 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
6088 return 0;
6089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006091 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
6092 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
6093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094 os_memset(&upd, 0, sizeof(upd));
6095 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
6096 if (authorized)
6097 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006098
6099 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6100 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
6101 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
6102 nlmsg_free(msg);
6103 return -ENOBUFS;
6104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006106 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006107 if (!ret)
6108 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006109 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
6110 ret, strerror(-ret));
6111 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006112}
6113
6114
Jouni Malinen75ecf522011-06-27 15:19:46 -07006115/* Set kernel driver on given frequency (MHz) */
6116static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117{
Jouni Malinen75ecf522011-06-27 15:19:46 -07006118 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006119 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120}
6121
6122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006123static inline int min_int(int a, int b)
6124{
6125 if (a < b)
6126 return a;
6127 return b;
6128}
6129
6130
6131static int get_key_handler(struct nl_msg *msg, void *arg)
6132{
6133 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6134 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6135
6136 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6137 genlmsg_attrlen(gnlh, 0), NULL);
6138
6139 /*
6140 * TODO: validate the key index and mac address!
6141 * Otherwise, there's a race condition as soon as
6142 * the kernel starts sending key notifications.
6143 */
6144
6145 if (tb[NL80211_ATTR_KEY_SEQ])
6146 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
6147 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
Hai Shalom021b0b52019-04-10 11:17:58 -07006148 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006149 return NL_SKIP;
6150}
6151
6152
6153static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
6154 int idx, u8 *seq)
6155{
6156 struct i802_bss *bss = priv;
6157 struct wpa_driver_nl80211_data *drv = bss->drv;
6158 struct nl_msg *msg;
6159
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006160 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
6161 NL80211_CMD_GET_KEY);
6162 if (!msg ||
6163 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
6164 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
6165 nlmsg_free(msg);
6166 return -ENOBUFS;
6167 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168
6169 memset(seq, 0, 6);
6170
6171 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172}
6173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174
6175static int i802_set_rts(void *priv, int rts)
6176{
6177 struct i802_bss *bss = priv;
6178 struct wpa_driver_nl80211_data *drv = bss->drv;
6179 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006180 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181 u32 val;
6182
Hai Shalom021b0b52019-04-10 11:17:58 -07006183 if (rts >= 2347 || rts == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006184 val = (u32) -1;
6185 else
6186 val = rts;
6187
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006188 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6189 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
6190 nlmsg_free(msg);
6191 return -ENOBUFS;
6192 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193
6194 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6195 if (!ret)
6196 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
6198 "%d (%s)", rts, ret, strerror(-ret));
6199 return ret;
6200}
6201
6202
6203static int i802_set_frag(void *priv, int frag)
6204{
6205 struct i802_bss *bss = priv;
6206 struct wpa_driver_nl80211_data *drv = bss->drv;
6207 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006208 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 u32 val;
6210
Hai Shalom021b0b52019-04-10 11:17:58 -07006211 if (frag >= 2346 || frag == -1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 val = (u32) -1;
6213 else
6214 val = frag;
6215
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006216 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6217 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6218 nlmsg_free(msg);
6219 return -ENOBUFS;
6220 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221
6222 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6223 if (!ret)
6224 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6226 "%d: %d (%s)", frag, ret, strerror(-ret));
6227 return ret;
6228}
6229
6230
6231static int i802_flush(void *priv)
6232{
6233 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006235 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006237 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6238 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006239
6240 /*
6241 * XXX: FIX! this needs to flush all VLANs too
6242 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006243 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6244 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006245 if (res) {
6246 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6247 "(%s)", res, strerror(-res));
6248 }
6249 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250}
6251
6252
6253static int get_sta_handler(struct nl_msg *msg, void *arg)
6254{
6255 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6256 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6257 struct hostap_sta_driver_data *data = arg;
6258 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6259 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6260 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6261 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6262 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6263 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6264 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006265 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006266 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6267 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006268 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07006269 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006270 };
6271 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6272 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6273 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6274 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6275 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6276 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6277 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6278 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279 };
6280
6281 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6282 genlmsg_attrlen(gnlh, 0), NULL);
6283
6284 /*
6285 * TODO: validate the interface and mac address!
6286 * Otherwise, there's a race condition as soon as
6287 * the kernel starts sending station notifications.
6288 */
6289
6290 if (!tb[NL80211_ATTR_STA_INFO]) {
6291 wpa_printf(MSG_DEBUG, "sta stats missing!");
6292 return NL_SKIP;
6293 }
6294 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6295 tb[NL80211_ATTR_STA_INFO],
6296 stats_policy)) {
6297 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6298 return NL_SKIP;
6299 }
6300
6301 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6302 data->inactive_msec =
6303 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006304 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006305 if (stats[NL80211_STA_INFO_RX_BYTES])
6306 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6307 if (stats[NL80211_STA_INFO_TX_BYTES])
6308 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006309 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6310 stats[NL80211_STA_INFO_TX_BYTES64]) {
6311 /*
6312 * The driver supports 64-bit counters, so use them to override
6313 * the 32-bit values.
6314 */
6315 data->rx_bytes =
6316 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6317 data->tx_bytes =
6318 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6319 data->bytes_64bit = 1;
6320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321 if (stats[NL80211_STA_INFO_RX_PACKETS])
6322 data->rx_packets =
6323 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6324 if (stats[NL80211_STA_INFO_TX_PACKETS])
6325 data->tx_packets =
6326 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006327 if (stats[NL80211_STA_INFO_TX_FAILED])
6328 data->tx_retry_failed =
6329 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006330 if (stats[NL80211_STA_INFO_SIGNAL])
6331 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006332 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6333 data->last_ack_rssi =
6334 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6335 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6336 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006337
6338 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6339 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6340 stats[NL80211_STA_INFO_TX_BITRATE],
6341 rate_policy) == 0) {
6342 if (rate[NL80211_RATE_INFO_BITRATE32])
6343 data->current_tx_rate =
6344 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6345 else if (rate[NL80211_RATE_INFO_BITRATE])
6346 data->current_tx_rate =
6347 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6348
6349 if (rate[NL80211_RATE_INFO_MCS]) {
6350 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6351 data->flags |= STA_DRV_DATA_TX_MCS;
6352 }
6353 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6354 data->tx_vhtmcs =
6355 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6356 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6357 }
6358 if (rate[NL80211_RATE_INFO_SHORT_GI])
6359 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6360 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6361 data->tx_vht_nss =
6362 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6363 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6364 }
6365 }
6366
6367 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6368 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6369 stats[NL80211_STA_INFO_RX_BITRATE],
6370 rate_policy) == 0) {
6371 if (rate[NL80211_RATE_INFO_BITRATE32])
6372 data->current_rx_rate =
6373 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6374 else if (rate[NL80211_RATE_INFO_BITRATE])
6375 data->current_rx_rate =
6376 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6377
6378 if (rate[NL80211_RATE_INFO_MCS]) {
6379 data->rx_mcs =
6380 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6381 data->flags |= STA_DRV_DATA_RX_MCS;
6382 }
6383 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6384 data->rx_vhtmcs =
6385 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6386 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6387 }
6388 if (rate[NL80211_RATE_INFO_SHORT_GI])
6389 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6390 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6391 data->rx_vht_nss =
6392 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6393 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6394 }
6395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006396
6397 return NL_SKIP;
6398}
6399
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006400static int i802_read_sta_data(struct i802_bss *bss,
6401 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402 const u8 *addr)
6403{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404 struct nl_msg *msg;
6405
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006406 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6407 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6408 nlmsg_free(msg);
6409 return -ENOBUFS;
6410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006412 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006413}
6414
6415
6416static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6417 int cw_min, int cw_max, int burst_time)
6418{
6419 struct i802_bss *bss = priv;
6420 struct wpa_driver_nl80211_data *drv = bss->drv;
6421 struct nl_msg *msg;
6422 struct nlattr *txq, *params;
Hai Shalom74f70d42019-02-11 14:42:39 -08006423 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006425 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006426 if (!msg)
6427 return -1;
6428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6430 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006431 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432
6433 /* We are only sending parameters for a single TXQ at a time */
6434 params = nla_nest_start(msg, 1);
6435 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006436 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437
6438 switch (queue) {
6439 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006440 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6441 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442 break;
6443 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006444 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6445 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006446 break;
6447 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006448 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6449 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006450 break;
6451 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006452 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6453 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454 break;
6455 }
6456 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6457 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006458 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6459 (burst_time * 100 + 16) / 32) ||
6460 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6461 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6462 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6463 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464
6465 nla_nest_end(msg, params);
6466
6467 nla_nest_end(msg, txq);
6468
Hai Shalom74f70d42019-02-11 14:42:39 -08006469 res = send_and_recv_msgs(drv, msg, NULL, NULL);
6470 wpa_printf(MSG_DEBUG,
6471 "nl80211: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
6472 queue, aifs, cw_min, cw_max, burst_time, res);
6473 if (res == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006475 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006476fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006477 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478 return -1;
6479}
6480
6481
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006482static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483 const char *ifname, int vlan_id)
6484{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006485 struct wpa_driver_nl80211_data *drv = bss->drv;
6486 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006487 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006488
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006489 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6490 ", ifname=%s[%d], vlan_id=%d)",
6491 bss->ifname, if_nametoindex(bss->ifname),
6492 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006493 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6494 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6495 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6496 nlmsg_free(msg);
6497 return -ENOBUFS;
6498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499
6500 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6501 if (ret < 0) {
6502 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6503 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6504 MAC2STR(addr), ifname, vlan_id, ret,
6505 strerror(-ret));
6506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 return ret;
6508}
6509
6510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006511static int i802_get_inact_sec(void *priv, const u8 *addr)
6512{
6513 struct hostap_sta_driver_data data;
6514 int ret;
6515
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006516 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 data.inactive_msec = (unsigned long) -1;
6518 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006519 if (ret == -ENOENT)
6520 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521 if (ret || data.inactive_msec == (unsigned long) -1)
6522 return -1;
6523 return data.inactive_msec / 1000;
6524}
6525
6526
6527static int i802_sta_clear_stats(void *priv, const u8 *addr)
6528{
6529#if 0
6530 /* TODO */
6531#endif
6532 return 0;
6533}
6534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535
6536static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6537 int reason)
6538{
6539 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006540 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006541 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006542 u8 channel;
6543
6544 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6545 HOSTAPD_MODE_IEEE80211AD) {
6546 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6547 * disassociate the STA instead. */
6548 return i802_sta_disassoc(priv, own_addr, addr, reason);
6549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006551 if (is_mesh_interface(drv->nlmode))
6552 return -1;
6553
Dmitry Shmidt04949592012-07-19 12:16:46 -07006554 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006555 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006557 memset(&mgmt, 0, sizeof(mgmt));
6558 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6559 WLAN_FC_STYPE_DEAUTH);
6560 memcpy(mgmt.da, addr, ETH_ALEN);
6561 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6562 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6563 mgmt.u.deauth.reason_code = host_to_le16(reason);
6564 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6565 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006566 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006567 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568}
6569
6570
6571static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6572 int reason)
6573{
6574 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006575 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006576 struct ieee80211_mgmt mgmt;
6577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006578 if (is_mesh_interface(drv->nlmode))
6579 return -1;
6580
Dmitry Shmidt04949592012-07-19 12:16:46 -07006581 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006582 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584 memset(&mgmt, 0, sizeof(mgmt));
6585 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6586 WLAN_FC_STYPE_DISASSOC);
6587 memcpy(mgmt.da, addr, ETH_ALEN);
6588 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6589 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6590 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6591 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6592 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006593 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006594 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006595}
6596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006597
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006598static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6599{
6600 char buf[200], *pos, *end;
6601 int i, res;
6602
6603 pos = buf;
6604 end = pos + sizeof(buf);
6605
6606 for (i = 0; i < drv->num_if_indices; i++) {
6607 if (!drv->if_indices[i])
6608 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006609 res = os_snprintf(pos, end - pos, " %d(%d)",
6610 drv->if_indices[i],
6611 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006612 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006613 break;
6614 pos += res;
6615 }
6616 *pos = '\0';
6617
6618 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6619 drv->num_if_indices, buf);
6620}
6621
6622
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006623static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6624 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006625{
6626 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006627 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006628
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006629 wpa_printf(MSG_DEBUG,
6630 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6631 ifidx, ifidx_reason);
6632 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006633 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6634 ifidx);
6635 return;
6636 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006637 for (i = 0; i < drv->num_if_indices; i++) {
6638 if (drv->if_indices[i] == 0) {
6639 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006640 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006641 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006642 return;
6643 }
6644 }
6645
6646 if (drv->if_indices != drv->default_if_indices)
6647 old = drv->if_indices;
6648 else
6649 old = NULL;
6650
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006651 if (drv->if_indices_reason != drv->default_if_indices_reason)
6652 old_reason = drv->if_indices_reason;
6653 else
6654 old_reason = NULL;
6655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006656 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6657 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006658 drv->if_indices_reason = os_realloc_array(old_reason,
6659 drv->num_if_indices + 1,
6660 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006661 if (!drv->if_indices) {
6662 if (!old)
6663 drv->if_indices = drv->default_if_indices;
6664 else
6665 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006666 }
6667 if (!drv->if_indices_reason) {
6668 if (!old_reason)
6669 drv->if_indices_reason = drv->default_if_indices_reason;
6670 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07006671 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006672 }
6673 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006674 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6675 "interfaces");
6676 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6677 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006678 }
6679 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006680 os_memcpy(drv->if_indices, drv->default_if_indices,
6681 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006682 if (!old_reason)
6683 os_memcpy(drv->if_indices_reason,
6684 drv->default_if_indices_reason,
6685 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006686 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006687 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006688 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006689 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006690}
6691
6692
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006693static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6694 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006695{
6696 int i;
6697
6698 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006699 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6700 (drv->if_indices_reason[i] == ifidx_reason ||
6701 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006702 drv->if_indices[i] = 0;
6703 break;
6704 }
6705 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006706 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006707}
6708
6709
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006710static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6711 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006712{
6713 int i;
6714
6715 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006716 if (drv->if_indices[i] == ifidx &&
6717 (drv->if_indices_reason[i] == ifidx_reason ||
6718 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006719 return 1;
6720
6721 return 0;
6722}
6723
6724
6725static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006726 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006727{
6728 struct i802_bss *bss = priv;
6729 struct wpa_driver_nl80211_data *drv = bss->drv;
6730 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07006731 union wpa_event_data event;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08006732 int ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006733
Hai Shalom39ba6fc2019-01-22 12:40:38 -08006734 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
6735 if (ret >= (int) sizeof(name))
6736 wpa_printf(MSG_WARNING,
6737 "nl80211: WDS interface name was truncated");
6738 else if (ret < 0)
6739 return ret;
6740
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006741 if (ifname_wds)
6742 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6743
Jouni Malinen75ecf522011-06-27 15:19:46 -07006744 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6745 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6746 if (val) {
6747 if (!if_nametoindex(name)) {
6748 if (nl80211_create_iface(drv, name,
6749 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006750 bss->addr, 1, NULL, NULL, 0) <
6751 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006752 return -1;
6753 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006754 linux_br_add_if(drv->global->ioctl_sock,
6755 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006756 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006757
6758 os_memset(&event, 0, sizeof(event));
6759 event.wds_sta_interface.sta_addr = addr;
6760 event.wds_sta_interface.ifname = name;
6761 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07006762 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006763 EVENT_WDS_STA_INTERFACE_STATUS,
6764 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006765 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006766 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6767 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6768 "interface %s up", name);
6769 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006770 return i802_set_sta_vlan(priv, addr, name, 0);
6771 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08006772 if (bridge_ifname &&
6773 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6774 name) < 0)
6775 wpa_printf(MSG_INFO,
6776 "nl80211: Failed to remove interface %s from bridge %s: %s",
6777 name, bridge_ifname, strerror(errno));
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006778
Jouni Malinen75ecf522011-06-27 15:19:46 -07006779 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006780 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006781 os_memset(&event, 0, sizeof(event));
6782 event.wds_sta_interface.sta_addr = addr;
6783 event.wds_sta_interface.ifname = name;
6784 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07006785 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006786 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006787 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006788 }
6789}
6790
6791
6792static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6793{
6794 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6795 struct sockaddr_ll lladdr;
6796 unsigned char buf[3000];
6797 int len;
6798 socklen_t fromlen = sizeof(lladdr);
6799
6800 len = recvfrom(sock, buf, sizeof(buf), 0,
6801 (struct sockaddr *)&lladdr, &fromlen);
6802 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006803 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6804 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006805 return;
6806 }
6807
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006808 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006809 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6810}
6811
6812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006813static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6814 struct i802_bss *bss,
6815 const char *brname, const char *ifname)
6816{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006817 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006818 char in_br[IFNAMSIZ];
6819
6820 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006821 br_ifindex = if_nametoindex(brname);
6822 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823 /*
6824 * Bridge was configured, but the bridge device does
6825 * not exist. Try to add it now.
6826 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006827 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006828 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6829 "bridge interface %s: %s",
6830 brname, strerror(errno));
6831 return -1;
6832 }
6833 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006834 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006835 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006837 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006838
6839 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07006840 if (os_strcmp(in_br, brname) == 0) {
6841 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006842 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07006843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844
6845 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6846 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006847 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6848 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6850 "remove interface %s from bridge "
6851 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006852 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853 return -1;
6854 }
6855 }
6856
6857 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6858 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006859 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006860 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6861 "into bridge %s: %s",
6862 ifname, brname, strerror(errno));
6863 return -1;
6864 }
6865 bss->added_if_into_bridge = 1;
6866
6867 return 0;
6868}
6869
6870
6871static void *i802_init(struct hostapd_data *hapd,
6872 struct wpa_init_params *params)
6873{
6874 struct wpa_driver_nl80211_data *drv;
6875 struct i802_bss *bss;
6876 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006877 char master_ifname[IFNAMSIZ];
6878 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006879 int br_added = 0;
6880
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006881 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6882 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006883 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006884 if (bss == NULL)
6885 return NULL;
6886
6887 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006888
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006889 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006890 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006891 params->ifname, master_ifname);
6892 br_ifindex = if_nametoindex(master_ifname);
6893 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6894 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6895 linux_master_get(master_ifname, params->ifname) == 0) {
6896 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6897 params->ifname, master_ifname);
6898 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006899 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006900
6901 /* check if master itself is under bridge */
6902 if (linux_br_get(master_ifname, master_ifname) == 0) {
6903 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6904 master_ifname);
6905 br_ifindex = if_nametoindex(master_ifname);
6906 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006908 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006909 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006910 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006911
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006912 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914 for (i = 0; i < params->num_bridge; i++) {
6915 if (params->bridge[i]) {
6916 ifindex = if_nametoindex(params->bridge[i]);
6917 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006918 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919 if (ifindex == br_ifindex)
6920 br_added = 1;
6921 }
6922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006923
6924 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006925 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006927 if (params->num_bridge && params->bridge[0]) {
6928 if (i802_check_bridge(drv, bss, params->bridge[0],
6929 params->ifname) < 0)
6930 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006931 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006932 br_added = 1;
6933 }
6934
6935 if (!br_added && br_ifindex &&
6936 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006937 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006939#ifdef CONFIG_LIBNL3_ROUTE
Hai Shalomc9e41a12018-07-31 14:41:42 -07006940 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006941 drv->rtnl_sk = nl_socket_alloc();
6942 if (drv->rtnl_sk == NULL) {
6943 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6944 goto failed;
6945 }
6946
6947 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6948 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6949 strerror(errno));
6950 goto failed;
6951 }
6952 }
6953#endif /* CONFIG_LIBNL3_ROUTE */
6954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6956 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006957 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6958 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006959 goto failed;
6960 }
6961
6962 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6963 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006964 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965 goto failed;
6966 }
6967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006968 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6969 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006971 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006973 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975 return bss;
6976
6977failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006978 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979 return NULL;
6980}
6981
6982
6983static void i802_deinit(void *priv)
6984{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006985 struct i802_bss *bss = priv;
6986 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987}
6988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006989
6990static enum nl80211_iftype wpa_driver_nl80211_if_type(
6991 enum wpa_driver_if_type type)
6992{
6993 switch (type) {
6994 case WPA_IF_STATION:
6995 return NL80211_IFTYPE_STATION;
6996 case WPA_IF_P2P_CLIENT:
6997 case WPA_IF_P2P_GROUP:
6998 return NL80211_IFTYPE_P2P_CLIENT;
6999 case WPA_IF_AP_VLAN:
7000 return NL80211_IFTYPE_AP_VLAN;
7001 case WPA_IF_AP_BSS:
7002 return NL80211_IFTYPE_AP;
7003 case WPA_IF_P2P_GO:
7004 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007005 case WPA_IF_P2P_DEVICE:
7006 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007007 case WPA_IF_MESH:
7008 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007009 default:
7010 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012}
7013
7014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007015static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7016{
7017 struct wpa_driver_nl80211_data *drv;
7018 dl_list_for_each(drv, &global->interfaces,
7019 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007020 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021 return 1;
7022 }
7023 return 0;
7024}
7025
7026
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007027static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028{
7029 unsigned int idx;
7030
7031 if (!drv->global)
7032 return -1;
7033
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007034 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007035 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007036 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037 new_addr[0] ^= idx << 2;
7038 if (!nl80211_addr_in_use(drv->global, new_addr))
7039 break;
7040 }
7041 if (idx == 64)
7042 return -1;
7043
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007044 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007045 MACSTR, MAC2STR(new_addr));
7046
7047 return 0;
7048}
7049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007051struct wdev_info {
7052 u64 wdev_id;
7053 int wdev_id_set;
7054 u8 macaddr[ETH_ALEN];
7055};
7056
7057static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
7058{
7059 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7060 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7061 struct wdev_info *wi = arg;
7062
7063 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7064 genlmsg_attrlen(gnlh, 0), NULL);
7065 if (tb[NL80211_ATTR_WDEV]) {
7066 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
7067 wi->wdev_id_set = 1;
7068 }
7069
7070 if (tb[NL80211_ATTR_MAC])
7071 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
7072 ETH_ALEN);
7073
7074 return NL_SKIP;
7075}
7076
7077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007078static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
7079 const char *ifname, const u8 *addr,
7080 void *bss_ctx, void **drv_priv,
7081 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007082 const char *bridge, int use_existing,
7083 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007085 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007086 struct i802_bss *bss = priv;
7087 struct wpa_driver_nl80211_data *drv = bss->drv;
7088 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007089 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007090
7091 if (addr)
7092 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007093 nlmode = wpa_driver_nl80211_if_type(type);
7094 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
7095 struct wdev_info p2pdev_info;
7096
7097 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
7098 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
7099 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007100 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007101 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
7102 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
7103 ifname);
7104 return -1;
7105 }
7106
7107 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
7108 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
7109 if (!is_zero_ether_addr(p2pdev_info.macaddr))
7110 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
7111 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
7112 ifname,
7113 (long long unsigned int) p2pdev_info.wdev_id);
7114 } else {
7115 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007116 0, NULL, NULL, use_existing);
7117 if (use_existing && ifidx == -ENFILE) {
7118 added = 0;
7119 ifidx = if_nametoindex(ifname);
7120 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007121 return -1;
7122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007123 }
7124
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007125 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007126 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007127 os_memcpy(if_addr, bss->addr, ETH_ALEN);
7128 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007129 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007130 if (added)
7131 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007132 return -1;
7133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007134 }
7135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007136 if (!addr &&
7137 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007138 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
7139 type == WPA_IF_STATION)) {
7140 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007141 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007142
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007143 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007144 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007145 if (added)
7146 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147 return -1;
7148 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007149 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007150 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007151 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007152 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007153 if (added)
7154 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007155 return -1;
7156 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007157 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007159 if (added)
7160 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007161 return -1;
7162 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007163 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007164 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007166
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007167 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007168 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
7169 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007170 if (added)
7171 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007172 return -1;
7173 }
7174
7175 if (bridge &&
7176 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7177 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7178 "interface %s to a bridge %s",
7179 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007180 if (added)
7181 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007182 os_free(new_bss);
7183 return -1;
7184 }
7185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007186 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7187 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07007188 if (added)
7189 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007190 os_free(new_bss);
7191 return -1;
7192 }
7193 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007194 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007195 new_bss->ifindex = ifidx;
7196 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007197 new_bss->next = drv->first_bss->next;
7198 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007199 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007200 new_bss->added_if = added;
7201 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202 if (drv_priv)
7203 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007204 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007205
7206 /* Subscribe management frames for this WPA_IF_AP_BSS */
7207 if (nl80211_setup_ap(new_bss))
7208 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007211 if (drv->global)
7212 drv->global->if_add_ifindex = ifidx;
7213
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007214 /*
7215 * Some virtual interfaces need to process EAPOL packets and events on
7216 * the parent interface. This is used mainly with hostapd.
7217 */
7218 if (ifidx > 0 &&
7219 (drv->hostapd ||
7220 nlmode == NL80211_IFTYPE_AP_VLAN ||
7221 nlmode == NL80211_IFTYPE_WDS ||
7222 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007223 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007225 return 0;
7226}
7227
7228
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007229static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007230 enum wpa_driver_if_type type,
7231 const char *ifname)
7232{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233 struct wpa_driver_nl80211_data *drv = bss->drv;
7234 int ifindex = if_nametoindex(ifname);
7235
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007236 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7237 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007238 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07007239 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007240 else if (ifindex > 0 && !bss->added_if) {
7241 struct wpa_driver_nl80211_data *drv2;
7242 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007243 struct wpa_driver_nl80211_data, list) {
7244 del_ifidx(drv2, ifindex, IFIDX_ANY);
7245 del_ifidx(drv2, IFIDX_ANY, ifindex);
7246 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007247 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007248
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007249 if (type != WPA_IF_AP_BSS)
7250 return 0;
7251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007253 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7254 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007255 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7256 "interface %s from bridge %s: %s",
7257 bss->ifname, bss->brname, strerror(errno));
7258 }
7259 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007260 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007261 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7262 "bridge %s: %s",
7263 bss->brname, strerror(errno));
7264 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007265
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007266 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007267 struct i802_bss *tbss;
7268
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007269 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7270 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007271 if (tbss->next == bss) {
7272 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007273 /* Unsubscribe management frames */
7274 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007275 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007276 if (!bss->added_if)
7277 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007278 os_free(bss);
7279 bss = NULL;
7280 break;
7281 }
7282 }
7283 if (bss)
7284 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7285 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007286 } else {
7287 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7288 nl80211_teardown_ap(bss);
7289 if (!bss->added_if && !drv->first_bss->next)
Paul Stewart092955c2017-02-06 09:13:09 -08007290 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007291 nl80211_destroy_bss(bss);
7292 if (!bss->added_if)
7293 i802_set_iface_flags(bss, 0);
7294 if (drv->first_bss->next) {
7295 drv->first_bss = drv->first_bss->next;
7296 drv->ctx = drv->first_bss->ctx;
7297 os_free(bss);
7298 } else {
7299 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7300 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302
7303 return 0;
7304}
7305
7306
7307static int cookie_handler(struct nl_msg *msg, void *arg)
7308{
7309 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7310 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7311 u64 *cookie = arg;
7312 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7313 genlmsg_attrlen(gnlh, 0), NULL);
7314 if (tb[NL80211_ATTR_COOKIE])
7315 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7316 return NL_SKIP;
7317}
7318
7319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007320static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007321 unsigned int freq, unsigned int wait,
7322 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007323 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007324 int offchanok, const u16 *csa_offs,
7325 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007326{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328 struct nl_msg *msg;
7329 u64 cookie;
7330 int ret = -1;
7331
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007332 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07007333 "no_ack=%d offchanok=%d",
7334 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007335 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007336
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007337 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7338 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7339 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7340 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7341 drv->test_use_roc_tx) &&
7342 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7343 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7344 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007345 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7346 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007347 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7348 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007349
7350 cookie = 0;
7351 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7352 msg = NULL;
7353 if (ret) {
7354 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007355 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7356 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007357 } else {
7358 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7359 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7360 (long long unsigned int) cookie);
7361
7362 if (cookie_out)
7363 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007364
7365 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7366 wpa_printf(MSG_DEBUG,
7367 "nl80211: Drop oldest pending send action cookie 0x%llx",
7368 (long long unsigned int)
7369 drv->send_action_cookies[0]);
7370 os_memmove(&drv->send_action_cookies[0],
7371 &drv->send_action_cookies[1],
7372 (MAX_SEND_ACTION_COOKIES - 1) *
7373 sizeof(u64));
7374 drv->num_send_action_cookies--;
7375 }
7376 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7377 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007378 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007380fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381 nlmsg_free(msg);
7382 return ret;
7383}
7384
7385
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007386static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7387 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007388 unsigned int wait_time,
7389 const u8 *dst, const u8 *src,
7390 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007391 const u8 *data, size_t data_len,
7392 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007393{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394 struct wpa_driver_nl80211_data *drv = bss->drv;
7395 int ret = -1;
7396 u8 *buf;
7397 struct ieee80211_hdr *hdr;
7398
7399 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007400 "freq=%u MHz wait=%d ms no_cck=%d)",
7401 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007402
7403 buf = os_zalloc(24 + data_len);
7404 if (buf == NULL)
7405 return ret;
7406 os_memcpy(buf + 24, data, data_len);
7407 hdr = (struct ieee80211_hdr *) buf;
7408 hdr->frame_control =
7409 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7410 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7411 os_memcpy(hdr->addr2, src, ETH_ALEN);
7412 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7413
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007414 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7415 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7416 MAC2STR(src));
7417 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7418 } else {
7419 os_memset(bss->rand_addr, 0, ETH_ALEN);
7420 }
7421
Dmitry Shmidt56052862013-10-04 10:23:25 -07007422 if (is_ap_interface(drv->nlmode) &&
7423 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7424 (int) freq == bss->freq || drv->device_ap_sme ||
7425 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007426 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7427 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007428 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007429 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007430 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007432 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007433 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434
7435 os_free(buf);
7436 return ret;
7437}
7438
7439
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007440static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442 struct wpa_driver_nl80211_data *drv = bss->drv;
7443 struct nl_msg *msg;
7444 int ret;
7445
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08007446 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007447 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007448 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007449 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007450 nlmsg_free(msg);
7451 return;
7452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007453
7454 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007455 if (ret)
7456 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7457 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458}
7459
7460
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007461static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7462{
7463 struct i802_bss *bss = priv;
7464 struct wpa_driver_nl80211_data *drv = bss->drv;
7465 unsigned int i;
7466 u64 cookie;
7467
7468 /* Cancel the last pending TX cookie */
7469 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7470
7471 /*
7472 * Cancel the other pending TX cookies, if any. This is needed since
7473 * the driver may keep a list of all pending offchannel TX operations
7474 * and free up the radio only once they have expired or cancelled.
7475 */
7476 for (i = drv->num_send_action_cookies; i > 0; i--) {
7477 cookie = drv->send_action_cookies[i - 1];
7478 if (cookie != drv->send_action_cookie)
7479 nl80211_frame_wait_cancel(bss, cookie);
7480 }
7481 drv->num_send_action_cookies = 0;
7482}
7483
7484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007485static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7486 unsigned int duration)
7487{
7488 struct i802_bss *bss = priv;
7489 struct wpa_driver_nl80211_data *drv = bss->drv;
7490 struct nl_msg *msg;
7491 int ret;
7492 u64 cookie;
7493
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007494 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7495 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7496 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7497 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007498 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007499 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007500
7501 cookie = 0;
7502 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7503 if (ret == 0) {
7504 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7505 "0x%llx for freq=%u MHz duration=%u",
7506 (long long unsigned int) cookie, freq, duration);
7507 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007508 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007509 return 0;
7510 }
7511 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7512 "(freq=%d duration=%u): %d (%s)",
7513 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007514 return -1;
7515}
7516
7517
7518static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7519{
7520 struct i802_bss *bss = priv;
7521 struct wpa_driver_nl80211_data *drv = bss->drv;
7522 struct nl_msg *msg;
7523 int ret;
7524
7525 if (!drv->pending_remain_on_chan) {
7526 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7527 "to cancel");
7528 return -1;
7529 }
7530
7531 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7532 "0x%llx",
7533 (long long unsigned int) drv->remain_on_chan_cookie);
7534
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007535 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7536 if (!msg ||
7537 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7538 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007539 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007540 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007541
7542 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7543 if (ret == 0)
7544 return 0;
7545 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7546 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547 return -1;
7548}
7549
7550
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007551static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007555 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007556 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007557 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7558 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007559 /*
7560 * Do not disable Probe Request reporting that was
7561 * enabled in nl80211_setup_ap().
7562 */
7563 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7564 "Probe Request reporting nl_preq=%p while "
7565 "in AP mode", bss->nl_preq);
7566 } else if (bss->nl_preq) {
7567 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7568 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007569 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007570 }
7571 return 0;
7572 }
7573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007574 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007576 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577 return 0;
7578 }
7579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007580 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7581 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007583 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7584 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007585
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007586 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587 (WLAN_FC_TYPE_MGMT << 2) |
7588 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007589 NULL, 0) < 0)
7590 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007591
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007592 nl80211_register_eloop_read(&bss->nl_preq,
7593 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007594 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595
7596 return 0;
7597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007598 out_err:
7599 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007600 return -1;
7601}
7602
7603
7604static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7605 int ifindex, int disabled)
7606{
7607 struct nl_msg *msg;
7608 struct nlattr *bands, *band;
7609 int ret;
7610
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007611 wpa_printf(MSG_DEBUG,
7612 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7613 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7614 "no NL80211_TXRATE_LEGACY constraint");
7615
7616 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7617 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007618 if (!msg)
7619 return -1;
7620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7622 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007623 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007624
7625 /*
7626 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7627 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7628 * rates. All 5 GHz rates are left enabled.
7629 */
7630 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007631 if (!band ||
7632 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7633 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7634 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635 nla_nest_end(msg, band);
7636
7637 nla_nest_end(msg, bands);
7638
7639 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007640 if (ret) {
7641 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7642 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007643 } else
7644 drv->disabled_11b_rates = disabled;
7645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007646 return ret;
7647
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007648fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007649 nlmsg_free(msg);
7650 return -1;
7651}
7652
7653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007654static int wpa_driver_nl80211_deinit_ap(void *priv)
7655{
7656 struct i802_bss *bss = priv;
7657 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007658 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007659 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007660 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007661 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007662
7663 /*
7664 * If the P2P GO interface was dynamically added, then it is
7665 * possible that the interface change to station is not possible.
7666 */
7667 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7668 return 0;
7669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007670 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671}
7672
7673
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007674static int wpa_driver_nl80211_stop_ap(void *priv)
7675{
7676 struct i802_bss *bss = priv;
7677 struct wpa_driver_nl80211_data *drv = bss->drv;
7678 if (!is_ap_interface(drv->nlmode))
7679 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007680 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007681 bss->beacon_set = 0;
7682 return 0;
7683}
7684
7685
Dmitry Shmidt04949592012-07-19 12:16:46 -07007686static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7687{
7688 struct i802_bss *bss = priv;
7689 struct wpa_driver_nl80211_data *drv = bss->drv;
7690 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7691 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007692
7693 /*
7694 * If the P2P Client interface was dynamically added, then it is
7695 * possible that the interface change to station is not possible.
7696 */
7697 if (bss->if_dynamic)
7698 return 0;
7699
Dmitry Shmidt04949592012-07-19 12:16:46 -07007700 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7701}
7702
7703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007704static void wpa_driver_nl80211_resume(void *priv)
7705{
7706 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007707 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007708
7709 if (i802_set_iface_flags(bss, 1))
7710 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007711
7712 if (is_p2p_net_interface(nlmode))
7713 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007714}
7715
7716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007717static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7718{
7719 struct i802_bss *bss = priv;
7720 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007721 struct nl_msg *msg;
7722 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723
7724 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7725 "hysteresis=%d", threshold, hysteresis);
7726
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007727 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7728 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7729 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7730 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7731 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007732 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007733 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007734 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007735
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007736 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737}
7738
7739
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007740static int get_channel_width(struct nl_msg *msg, void *arg)
7741{
7742 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7743 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7744 struct wpa_signal_info *sig_change = arg;
7745
7746 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7747 genlmsg_attrlen(gnlh, 0), NULL);
7748
7749 sig_change->center_frq1 = -1;
7750 sig_change->center_frq2 = -1;
7751 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7752
7753 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7754 sig_change->chanwidth = convert2width(
7755 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7756 if (tb[NL80211_ATTR_CENTER_FREQ1])
7757 sig_change->center_frq1 =
7758 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7759 if (tb[NL80211_ATTR_CENTER_FREQ2])
7760 sig_change->center_frq2 =
7761 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7762 }
7763
7764 return NL_SKIP;
7765}
7766
7767
7768static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7769 struct wpa_signal_info *sig)
7770{
7771 struct nl_msg *msg;
7772
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007773 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007774 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007775}
7776
7777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007778static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7779{
7780 struct i802_bss *bss = priv;
7781 struct wpa_driver_nl80211_data *drv = bss->drv;
7782 int res;
7783
7784 os_memset(si, 0, sizeof(*si));
7785 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007786 if (res) {
7787 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7788 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7789 return res;
7790 si->current_signal = 0;
7791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007793 res = nl80211_get_channel_width(drv, si);
7794 if (res != 0)
7795 return res;
7796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007797 return nl80211_get_link_noise(drv, si);
7798}
7799
7800
7801static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7802 int encrypt)
7803{
7804 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007805 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007806 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007807}
7808
7809
7810static int nl80211_set_param(void *priv, const char *param)
7811{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007812 struct i802_bss *bss = priv;
7813 struct wpa_driver_nl80211_data *drv = bss->drv;
7814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007815 if (param == NULL)
7816 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007817 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007818
7819#ifdef CONFIG_P2P
7820 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7822 "interface");
7823 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7824 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7825 }
7826#endif /* CONFIG_P2P */
7827
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007828 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007829 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007830
7831 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007832 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007833 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007834 }
7835
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007836 if (os_strstr(param, "force_bss_selection=1"))
7837 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7838
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007839 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007840 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7841 drv->test_use_roc_tx = 1;
7842 }
7843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007844 return 0;
7845}
7846
7847
Dmitry Shmidte4663042016-04-04 10:07:49 -07007848static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007849{
7850 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007851 struct netlink_config *cfg;
7852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007853 global = os_zalloc(sizeof(*global));
7854 if (global == NULL)
7855 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007856 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007857 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007859 global->if_add_ifindex = -1;
7860
7861 cfg = os_zalloc(sizeof(*cfg));
7862 if (cfg == NULL)
7863 goto err;
7864
7865 cfg->ctx = global;
7866 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7867 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7868 global->netlink = netlink_init(cfg);
7869 if (global->netlink == NULL) {
7870 os_free(cfg);
7871 goto err;
7872 }
7873
7874 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7875 goto err;
7876
7877 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7878 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007879 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7880 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007881 goto err;
7882 }
7883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007884 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007885
7886err:
7887 nl80211_global_deinit(global);
7888 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889}
7890
7891
7892static void nl80211_global_deinit(void *priv)
7893{
7894 struct nl80211_global *global = priv;
7895 if (global == NULL)
7896 return;
7897 if (!dl_list_empty(&global->interfaces)) {
7898 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7899 "nl80211_global_deinit",
7900 dl_list_len(&global->interfaces));
7901 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007902
7903 if (global->netlink)
7904 netlink_deinit(global->netlink);
7905
7906 nl_destroy_handles(&global->nl);
7907
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007908 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007909 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007910
7911 nl_cb_put(global->nl_cb);
7912
7913 if (global->ioctl_sock >= 0)
7914 close(global->ioctl_sock);
7915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916 os_free(global);
7917}
7918
7919
7920static const char * nl80211_get_radio_name(void *priv)
7921{
7922 struct i802_bss *bss = priv;
7923 struct wpa_driver_nl80211_data *drv = bss->drv;
7924 return drv->phyname;
7925}
7926
7927
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007928static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7929 struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007930{
7931 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007932 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
Jouni Malinen75ecf522011-06-27 15:19:46 -07007933
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007934 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007935 (params->pmkid &&
7936 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7937 (params->bssid &&
7938 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7939 (params->ssid_len &&
7940 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7941 (params->fils_cache_id &&
7942 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7943 params->fils_cache_id)) ||
Hai Shalom021b0b52019-04-10 11:17:58 -07007944 (cmd != NL80211_CMD_DEL_PMKSA &&
7945 params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007946 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Hai Shalom74f70d42019-02-11 14:42:39 -08007947 nl80211_nlmsg_clear(msg);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007948 nlmsg_free(msg);
7949 return -ENOBUFS;
7950 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007951
Hai Shalom74f70d42019-02-11 14:42:39 -08007952 return send_and_recv_msgs(bss->drv, msg, NULL, (void *) -1);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007953}
7954
7955
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007956static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007957{
7958 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007959 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007960
7961 if (params->bssid)
7962 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
7963 MAC2STR(params->bssid));
7964 else if (params->fils_cache_id && params->ssid_len) {
7965 wpa_printf(MSG_DEBUG,
7966 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
7967 params->fils_cache_id[0], params->fils_cache_id[1],
7968 wpa_ssid_txt(params->ssid, params->ssid_len));
7969 }
7970
Roshan Pius3a1667e2018-07-03 15:17:14 -07007971 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
7972 if (ret < 0) {
7973 wpa_printf(MSG_DEBUG,
7974 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7975 ret, strerror(-ret));
7976 }
7977
7978 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007979}
7980
7981
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007982static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007983{
7984 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007985 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007986
7987 if (params->bssid)
7988 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7989 MAC2STR(params->bssid));
7990 else if (params->fils_cache_id && params->ssid_len) {
7991 wpa_printf(MSG_DEBUG,
7992 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7993 params->fils_cache_id[0], params->fils_cache_id[1],
7994 wpa_ssid_txt(params->ssid, params->ssid_len));
7995 }
7996
Roshan Pius3a1667e2018-07-03 15:17:14 -07007997 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7998 if (ret < 0) {
7999 wpa_printf(MSG_DEBUG,
8000 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
8001 ret, strerror(-ret));
8002 }
8003
8004 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008005}
8006
8007
8008static int nl80211_flush_pmkid(void *priv)
8009{
8010 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008011 struct nl_msg *msg;
8012
Jouni Malinen75ecf522011-06-27 15:19:46 -07008013 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008014 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
8015 if (!msg)
8016 return -ENOBUFS;
8017 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008018}
8019
8020
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008021static void clean_survey_results(struct survey_results *survey_results)
8022{
8023 struct freq_survey *survey, *tmp;
8024
8025 if (dl_list_empty(&survey_results->survey_list))
8026 return;
8027
8028 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
8029 struct freq_survey, list) {
8030 dl_list_del(&survey->list);
8031 os_free(survey);
8032 }
8033}
8034
8035
8036static void add_survey(struct nlattr **sinfo, u32 ifidx,
8037 struct dl_list *survey_list)
8038{
8039 struct freq_survey *survey;
8040
8041 survey = os_zalloc(sizeof(struct freq_survey));
8042 if (!survey)
8043 return;
8044
8045 survey->ifidx = ifidx;
8046 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8047 survey->filled = 0;
8048
8049 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
8050 survey->nf = (int8_t)
8051 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
8052 survey->filled |= SURVEY_HAS_NF;
8053 }
8054
8055 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
8056 survey->channel_time =
8057 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
8058 survey->filled |= SURVEY_HAS_CHAN_TIME;
8059 }
8060
8061 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
8062 survey->channel_time_busy =
8063 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
8064 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
8065 }
8066
8067 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
8068 survey->channel_time_rx =
8069 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
8070 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
8071 }
8072
8073 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
8074 survey->channel_time_tx =
8075 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
8076 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
8077 }
8078
8079 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)",
8080 survey->freq,
8081 survey->nf,
8082 (unsigned long int) survey->channel_time,
8083 (unsigned long int) survey->channel_time_busy,
8084 (unsigned long int) survey->channel_time_tx,
8085 (unsigned long int) survey->channel_time_rx,
8086 survey->filled);
8087
8088 dl_list_add_tail(survey_list, &survey->list);
8089}
8090
8091
8092static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
8093 unsigned int freq_filter)
8094{
8095 if (!freq_filter)
8096 return 1;
8097
8098 return freq_filter == surveyed_freq;
8099}
8100
8101
8102static int survey_handler(struct nl_msg *msg, void *arg)
8103{
8104 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8105 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8106 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
8107 struct survey_results *survey_results;
8108 u32 surveyed_freq = 0;
8109 u32 ifidx;
8110
8111 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
8112 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
8113 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
8114 };
8115
8116 survey_results = (struct survey_results *) arg;
8117
8118 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8119 genlmsg_attrlen(gnlh, 0), NULL);
8120
Dmitry Shmidt97672262014-02-03 13:02:54 -08008121 if (!tb[NL80211_ATTR_IFINDEX])
8122 return NL_SKIP;
8123
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008124 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
8125
8126 if (!tb[NL80211_ATTR_SURVEY_INFO])
8127 return NL_SKIP;
8128
8129 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
8130 tb[NL80211_ATTR_SURVEY_INFO],
8131 survey_policy))
8132 return NL_SKIP;
8133
8134 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
8135 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
8136 return NL_SKIP;
8137 }
8138
8139 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8140
8141 if (!check_survey_ok(sinfo, surveyed_freq,
8142 survey_results->freq_filter))
8143 return NL_SKIP;
8144
8145 if (survey_results->freq_filter &&
8146 survey_results->freq_filter != surveyed_freq) {
8147 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
8148 surveyed_freq);
8149 return NL_SKIP;
8150 }
8151
8152 add_survey(sinfo, ifidx, &survey_results->survey_list);
8153
8154 return NL_SKIP;
8155}
8156
8157
8158static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
8159{
8160 struct i802_bss *bss = priv;
8161 struct wpa_driver_nl80211_data *drv = bss->drv;
8162 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008163 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008164 union wpa_event_data data;
8165 struct survey_results *survey_results;
8166
8167 os_memset(&data, 0, sizeof(data));
8168 survey_results = &data.survey_results;
8169
8170 dl_list_init(&survey_results->survey_list);
8171
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008172 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008173 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008174 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008175
8176 if (freq)
8177 data.survey_results.freq_filter = freq;
8178
8179 do {
8180 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
8181 err = send_and_recv_msgs(drv, msg, survey_handler,
8182 survey_results);
8183 } while (err > 0);
8184
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008185 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008186 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008187 else
8188 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008189
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008190 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008191 return err;
8192}
8193
8194
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008195static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
8196 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008197 const u8 *replay_ctr)
8198{
8199 struct i802_bss *bss = priv;
8200 struct wpa_driver_nl80211_data *drv = bss->drv;
8201 struct nlattr *replay_nested;
8202 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008203 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008204
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008205 if (!drv->set_rekey_offload)
8206 return;
8207
8208 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008209 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
8210 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008211 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008212 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008213 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8214 replay_ctr)) {
8215 nl80211_nlmsg_clear(msg);
8216 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008217 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008218 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008219
8220 nla_nest_end(msg, replay_nested);
8221
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008222 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
8223 if (ret == -EOPNOTSUPP) {
8224 wpa_printf(MSG_DEBUG,
8225 "nl80211: Driver does not support rekey offload");
8226 drv->set_rekey_offload = 0;
8227 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008228}
8229
8230
8231static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8232 const u8 *addr, int qos)
8233{
8234 /* send data frame to poll STA and check whether
8235 * this frame is ACKed */
8236 struct {
8237 struct ieee80211_hdr hdr;
8238 u16 qos_ctl;
8239 } STRUCT_PACKED nulldata;
8240 size_t size;
8241
8242 /* Send data frame to poll STA and check whether this frame is ACKed */
8243
8244 os_memset(&nulldata, 0, sizeof(nulldata));
8245
8246 if (qos) {
8247 nulldata.hdr.frame_control =
8248 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8249 WLAN_FC_STYPE_QOS_NULL);
8250 size = sizeof(nulldata);
8251 } else {
8252 nulldata.hdr.frame_control =
8253 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8254 WLAN_FC_STYPE_NULLFUNC);
8255 size = sizeof(struct ieee80211_hdr);
8256 }
8257
8258 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8259 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8260 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8261 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8262
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008263 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008264 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008265 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8266 "send poll frame");
8267}
8268
8269static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8270 int qos)
8271{
8272 struct i802_bss *bss = priv;
8273 struct wpa_driver_nl80211_data *drv = bss->drv;
8274 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008275 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008276
8277 if (!drv->poll_command_supported) {
8278 nl80211_send_null_frame(bss, own_addr, addr, qos);
8279 return;
8280 }
8281
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008282 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8283 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8284 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008285 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008286 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008287
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008288 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8289 if (ret < 0) {
8290 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8291 MACSTR " failed: ret=%d (%s)",
8292 MAC2STR(addr), ret, strerror(-ret));
8293 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008294}
8295
8296
8297static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8298{
8299 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008300 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008301
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008302 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8303 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8304 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8305 nlmsg_free(msg);
8306 return -ENOBUFS;
8307 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07008308
8309 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8310 if (ret < 0) {
8311 wpa_printf(MSG_DEBUG,
8312 "nl80211: Setting PS state %s failed: %d (%s)",
8313 enabled ? "enabled" : "disabled",
8314 ret, strerror(-ret));
8315 }
8316 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008317}
8318
8319
8320static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8321 int ctwindow)
8322{
8323 struct i802_bss *bss = priv;
8324
8325 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8326 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8327
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008328 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008329#ifdef ANDROID_P2P
8330 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008331#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008332 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008333#endif /* ANDROID_P2P */
8334 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008335
8336 if (legacy_ps == -1)
8337 return 0;
8338 if (legacy_ps != 0 && legacy_ps != 1)
8339 return -1; /* Not yet supported */
8340
8341 return nl80211_set_power_save(bss, legacy_ps);
8342}
8343
8344
Dmitry Shmidt051af732013-10-22 13:52:46 -07008345static int nl80211_start_radar_detection(void *priv,
8346 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008347{
8348 struct i802_bss *bss = priv;
8349 struct wpa_driver_nl80211_data *drv = bss->drv;
8350 struct nl_msg *msg;
8351 int ret;
8352
Dmitry Shmidt051af732013-10-22 13:52:46 -07008353 wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
8354 freq->freq, freq->ht_enabled, freq->vht_enabled,
8355 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8356
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008357 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8358 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8359 "detection");
8360 return -1;
8361 }
8362
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008363 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8364 nl80211_put_freq_params(msg, freq) < 0) {
8365 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008366 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008367 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008368
8369 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8370 if (ret == 0)
8371 return 0;
8372 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8373 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008374 return -1;
8375}
8376
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008377#ifdef CONFIG_TDLS
8378
8379static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8380 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008381 u32 peer_capab, int initiator, const u8 *buf,
8382 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008383{
8384 struct i802_bss *bss = priv;
8385 struct wpa_driver_nl80211_data *drv = bss->drv;
8386 struct nl_msg *msg;
8387
8388 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8389 return -EOPNOTSUPP;
8390
8391 if (!dst)
8392 return -EINVAL;
8393
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008394 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8395 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8396 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8397 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8398 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8399 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008400 if (peer_capab) {
8401 /*
8402 * The internal enum tdls_peer_capability definition is
8403 * currently identical with the nl80211 enum
8404 * nl80211_tdls_peer_capability, so no conversion is needed
8405 * here.
8406 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008407 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8408 peer_capab))
8409 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008410 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008411 if ((initiator &&
8412 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8413 nla_put(msg, NL80211_ATTR_IE, len, buf))
8414 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008415
8416 return send_and_recv_msgs(drv, msg, NULL, NULL);
8417
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008418fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008419 nlmsg_free(msg);
8420 return -ENOBUFS;
8421}
8422
8423
8424static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8425{
8426 struct i802_bss *bss = priv;
8427 struct wpa_driver_nl80211_data *drv = bss->drv;
8428 struct nl_msg *msg;
8429 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -08008430 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008431
8432 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8433 return -EOPNOTSUPP;
8434
8435 switch (oper) {
8436 case TDLS_DISCOVERY_REQ:
8437 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8438 break;
8439 case TDLS_SETUP:
8440 nl80211_oper = NL80211_TDLS_SETUP;
8441 break;
8442 case TDLS_TEARDOWN:
8443 nl80211_oper = NL80211_TDLS_TEARDOWN;
8444 break;
8445 case TDLS_ENABLE_LINK:
8446 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8447 break;
8448 case TDLS_DISABLE_LINK:
8449 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8450 break;
8451 case TDLS_ENABLE:
8452 return 0;
8453 case TDLS_DISABLE:
8454 return 0;
8455 default:
8456 return -EINVAL;
8457 }
8458
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008459 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8460 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8461 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8462 nlmsg_free(msg);
8463 return -ENOBUFS;
8464 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008465
Paul Stewart092955c2017-02-06 09:13:09 -08008466 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8467 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8468 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8469 strerror(-res));
8470 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008471}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008472
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008473
8474static int
8475nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8476 const struct hostapd_freq_params *params)
8477{
8478 struct i802_bss *bss = priv;
8479 struct wpa_driver_nl80211_data *drv = bss->drv;
8480 struct nl_msg *msg;
8481 int ret = -ENOBUFS;
8482
8483 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8484 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8485 return -EOPNOTSUPP;
8486
8487 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8488 " oper_class=%u freq=%u",
8489 MAC2STR(addr), oper_class, params->freq);
8490 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8491 if (!msg ||
8492 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8493 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8494 (ret = nl80211_put_freq_params(msg, params))) {
8495 nlmsg_free(msg);
8496 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8497 return ret;
8498 }
8499
8500 return send_and_recv_msgs(drv, msg, NULL, NULL);
8501}
8502
8503
8504static int
8505nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8506{
8507 struct i802_bss *bss = priv;
8508 struct wpa_driver_nl80211_data *drv = bss->drv;
8509 struct nl_msg *msg;
8510
8511 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8512 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8513 return -EOPNOTSUPP;
8514
8515 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8516 MAC2STR(addr));
8517 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8518 if (!msg ||
8519 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8520 nlmsg_free(msg);
8521 wpa_printf(MSG_DEBUG,
8522 "nl80211: Could not build TDLS cancel chan switch");
8523 return -ENOBUFS;
8524 }
8525
8526 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008527}
8528
8529#endif /* CONFIG TDLS */
8530
8531
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008532static int driver_nl80211_set_key(const char *ifname, void *priv,
8533 enum wpa_alg alg, const u8 *addr,
8534 int key_idx, int set_tx,
8535 const u8 *seq, size_t seq_len,
8536 const u8 *key, size_t key_len)
8537{
8538 struct i802_bss *bss = priv;
8539 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8540 set_tx, seq, seq_len, key, key_len);
8541}
8542
8543
8544static int driver_nl80211_scan2(void *priv,
8545 struct wpa_driver_scan_params *params)
8546{
8547 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008548#ifdef CONFIG_DRIVER_NL80211_QCA
8549 struct wpa_driver_nl80211_data *drv = bss->drv;
8550
8551 /*
8552 * Do a vendor specific scan if possible. If only_new_results is
8553 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8554 * cannot be achieved through a vendor scan. The below condition may
8555 * need to be modified if new scan flags are added in the future whose
8556 * functionality can only be achieved through a normal scan.
8557 */
8558 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8559 return wpa_driver_nl80211_vendor_scan(bss, params);
8560#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008561 return wpa_driver_nl80211_scan(bss, params);
8562}
8563
8564
8565static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8566 int reason_code)
8567{
8568 struct i802_bss *bss = priv;
8569 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8570}
8571
8572
8573static int driver_nl80211_authenticate(void *priv,
8574 struct wpa_driver_auth_params *params)
8575{
8576 struct i802_bss *bss = priv;
8577 return wpa_driver_nl80211_authenticate(bss, params);
8578}
8579
8580
8581static void driver_nl80211_deinit(void *priv)
8582{
8583 struct i802_bss *bss = priv;
8584 wpa_driver_nl80211_deinit(bss);
8585}
8586
8587
8588static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8589 const char *ifname)
8590{
8591 struct i802_bss *bss = priv;
8592 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8593}
8594
8595
8596static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008597 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008598 unsigned int freq,
8599 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008600{
8601 struct i802_bss *bss = priv;
8602 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008603 freq, 0, 0, 0, csa_offs,
8604 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008605}
8606
8607
8608static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8609{
8610 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008611 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008612}
8613
8614
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008615static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8616 const char *ifname, int vlan_id)
8617{
8618 struct i802_bss *bss = priv;
8619 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8620}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008621
8622
8623static int driver_nl80211_read_sta_data(void *priv,
8624 struct hostap_sta_driver_data *data,
8625 const u8 *addr)
8626{
8627 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008628
8629 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008630 return i802_read_sta_data(bss, data, addr);
8631}
8632
8633
8634static int driver_nl80211_send_action(void *priv, unsigned int freq,
8635 unsigned int wait_time,
8636 const u8 *dst, const u8 *src,
8637 const u8 *bssid,
8638 const u8 *data, size_t data_len,
8639 int no_cck)
8640{
8641 struct i802_bss *bss = priv;
8642 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8643 bssid, data, data_len, no_cck);
8644}
8645
8646
8647static int driver_nl80211_probe_req_report(void *priv, int report)
8648{
8649 struct i802_bss *bss = priv;
8650 return wpa_driver_nl80211_probe_req_report(bss, report);
8651}
8652
8653
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008654static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8655 const u8 *ies, size_t ies_len)
8656{
8657 int ret;
8658 struct nl_msg *msg;
8659 struct i802_bss *bss = priv;
8660 struct wpa_driver_nl80211_data *drv = bss->drv;
8661 u16 mdid = WPA_GET_LE16(md);
8662
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008663 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008664 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8665 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8666 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8667 nlmsg_free(msg);
8668 return -ENOBUFS;
8669 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008670
8671 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8672 if (ret) {
8673 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8674 "err=%d (%s)", ret, strerror(-ret));
8675 }
8676
8677 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008678}
8679
8680
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07008681static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008682{
8683 struct i802_bss *bss = priv;
8684 struct wpa_driver_nl80211_data *drv = bss->drv;
8685
8686 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8687 return NULL;
8688
8689 return bss->addr;
8690}
8691
8692
Dmitry Shmidt56052862013-10-04 10:23:25 -07008693static const char * scan_state_str(enum scan_states scan_state)
8694{
8695 switch (scan_state) {
8696 case NO_SCAN:
8697 return "NO_SCAN";
8698 case SCAN_REQUESTED:
8699 return "SCAN_REQUESTED";
8700 case SCAN_STARTED:
8701 return "SCAN_STARTED";
8702 case SCAN_COMPLETED:
8703 return "SCAN_COMPLETED";
8704 case SCAN_ABORTED:
8705 return "SCAN_ABORTED";
8706 case SCHED_SCAN_STARTED:
8707 return "SCHED_SCAN_STARTED";
8708 case SCHED_SCAN_STOPPED:
8709 return "SCHED_SCAN_STOPPED";
8710 case SCHED_SCAN_RESULTS:
8711 return "SCHED_SCAN_RESULTS";
8712 }
8713
8714 return "??";
8715}
8716
8717
8718static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8719{
8720 struct i802_bss *bss = priv;
8721 struct wpa_driver_nl80211_data *drv = bss->drv;
8722 int res;
8723 char *pos, *end;
Hai Shalom74f70d42019-02-11 14:42:39 -08008724 struct nl_msg *msg;
8725 char alpha2[3] = { 0, 0, 0 };
Dmitry Shmidt56052862013-10-04 10:23:25 -07008726
8727 pos = buf;
8728 end = buf + buflen;
8729
8730 res = os_snprintf(pos, end - pos,
8731 "ifindex=%d\n"
8732 "ifname=%s\n"
8733 "brname=%s\n"
8734 "addr=" MACSTR "\n"
8735 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -07008736 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008737 bss->ifindex,
8738 bss->ifname,
8739 bss->brname,
8740 MAC2STR(bss->addr),
8741 bss->freq,
8742 bss->beacon_set ? "beacon_set=1\n" : "",
8743 bss->added_if_into_bridge ?
8744 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -07008745 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008746 bss->added_bridge ? "added_bridge=1\n" : "",
8747 bss->in_deinit ? "in_deinit=1\n" : "",
8748 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008749 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008750 return pos - buf;
8751 pos += res;
8752
8753 if (bss->wdev_id_set) {
8754 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8755 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008756 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008757 return pos - buf;
8758 pos += res;
8759 }
8760
8761 res = os_snprintf(pos, end - pos,
8762 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008763 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008764 "drv_ifindex=%d\n"
8765 "operstate=%d\n"
8766 "scan_state=%s\n"
8767 "auth_bssid=" MACSTR "\n"
8768 "auth_attempt_bssid=" MACSTR "\n"
8769 "bssid=" MACSTR "\n"
8770 "prev_bssid=" MACSTR "\n"
8771 "associated=%d\n"
8772 "assoc_freq=%u\n"
8773 "monitor_sock=%d\n"
8774 "monitor_ifidx=%d\n"
8775 "monitor_refcount=%d\n"
8776 "last_mgmt_freq=%u\n"
8777 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008778 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008779 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008780 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07008781 drv->ifindex,
8782 drv->operstate,
8783 scan_state_str(drv->scan_state),
8784 MAC2STR(drv->auth_bssid),
8785 MAC2STR(drv->auth_attempt_bssid),
8786 MAC2STR(drv->bssid),
8787 MAC2STR(drv->prev_bssid),
8788 drv->associated,
8789 drv->assoc_freq,
8790 drv->monitor_sock,
8791 drv->monitor_ifidx,
8792 drv->monitor_refcount,
8793 drv->last_mgmt_freq,
8794 drv->eapol_tx_sock,
8795 drv->ignore_if_down_event ?
8796 "ignore_if_down_event=1\n" : "",
8797 drv->scan_complete_events ?
8798 "scan_complete_events=1\n" : "",
8799 drv->disabled_11b_rates ?
8800 "disabled_11b_rates=1\n" : "",
8801 drv->pending_remain_on_chan ?
8802 "pending_remain_on_chan=1\n" : "",
8803 drv->in_interface_list ? "in_interface_list=1\n" : "",
8804 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8805 drv->poll_command_supported ?
8806 "poll_command_supported=1\n" : "",
8807 drv->data_tx_status ? "data_tx_status=1\n" : "",
8808 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8809 drv->retry_auth ? "retry_auth=1\n" : "",
8810 drv->use_monitor ? "use_monitor=1\n" : "",
8811 drv->ignore_next_local_disconnect ?
8812 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008813 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008814 "ignore_next_local_deauth=1\n" : "");
8815 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008816 return pos - buf;
8817 pos += res;
8818
8819 if (drv->has_capability) {
8820 res = os_snprintf(pos, end - pos,
8821 "capa.key_mgmt=0x%x\n"
8822 "capa.enc=0x%x\n"
8823 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008824 "capa.flags=0x%llx\n"
8825 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008826 "capa.max_scan_ssids=%d\n"
8827 "capa.max_sched_scan_ssids=%d\n"
8828 "capa.sched_scan_supported=%d\n"
8829 "capa.max_match_sets=%d\n"
8830 "capa.max_remain_on_chan=%u\n"
8831 "capa.max_stations=%u\n"
8832 "capa.probe_resp_offloads=0x%x\n"
8833 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008834 "capa.num_multichan_concurrent=%u\n"
8835 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008836 "capa.mac_addr_rand_scan_supported=%d\n"
8837 "capa.conc_capab=%u\n"
8838 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008839 "capa.max_conc_chan_5_0=%u\n"
8840 "capa.max_sched_scan_plans=%u\n"
8841 "capa.max_sched_scan_plan_interval=%u\n"
8842 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008843 drv->capa.key_mgmt,
8844 drv->capa.enc,
8845 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008846 (unsigned long long) drv->capa.flags,
8847 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008848 drv->capa.max_scan_ssids,
8849 drv->capa.max_sched_scan_ssids,
8850 drv->capa.sched_scan_supported,
8851 drv->capa.max_match_sets,
8852 drv->capa.max_remain_on_chan,
8853 drv->capa.max_stations,
8854 drv->capa.probe_resp_offloads,
8855 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008856 drv->capa.num_multichan_concurrent,
8857 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008858 drv->capa.mac_addr_rand_scan_supported,
8859 drv->capa.conc_capab,
8860 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008861 drv->capa.max_conc_chan_5_0,
8862 drv->capa.max_sched_scan_plans,
8863 drv->capa.max_sched_scan_plan_interval,
8864 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008865 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008866 return pos - buf;
8867 pos += res;
8868 }
8869
Hai Shalom74f70d42019-02-11 14:42:39 -08008870 msg = nlmsg_alloc();
8871 if (msg &&
8872 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG) &&
8873 nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx) == 0) {
8874 if (send_and_recv_msgs(drv, msg, nl80211_get_country,
8875 alpha2) == 0 &&
8876 alpha2[0]) {
8877 res = os_snprintf(pos, end - pos, "country=%s\n",
8878 alpha2);
8879 if (os_snprintf_error(end - pos, res))
8880 return pos - buf;
8881 pos += res;
8882 }
8883 } else {
8884 nlmsg_free(msg);
8885 }
8886
Dmitry Shmidt56052862013-10-04 10:23:25 -07008887 return pos - buf;
8888}
8889
8890
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008891static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8892{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008893 if ((settings->head &&
8894 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8895 settings->head_len, settings->head)) ||
8896 (settings->tail &&
8897 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8898 settings->tail_len, settings->tail)) ||
8899 (settings->beacon_ies &&
8900 nla_put(msg, NL80211_ATTR_IE,
8901 settings->beacon_ies_len, settings->beacon_ies)) ||
8902 (settings->proberesp_ies &&
8903 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8904 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8905 (settings->assocresp_ies &&
8906 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8907 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8908 (settings->probe_resp &&
8909 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8910 settings->probe_resp_len, settings->probe_resp)))
8911 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008912
8913 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008914}
8915
8916
8917static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8918{
8919 struct nl_msg *msg;
8920 struct i802_bss *bss = priv;
8921 struct wpa_driver_nl80211_data *drv = bss->drv;
8922 struct nlattr *beacon_csa;
8923 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008924 int csa_off_len = 0;
8925 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008926
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008927 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 -08008928 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008929 settings->freq_params.freq, settings->freq_params.bandwidth,
8930 settings->freq_params.center_freq1,
8931 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008932
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008933 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008934 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8935 return -EOPNOTSUPP;
8936 }
8937
Roshan Pius3a1667e2018-07-03 15:17:14 -07008938 if (drv->nlmode != NL80211_IFTYPE_AP &&
8939 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
8940 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008941 return -EOPNOTSUPP;
8942
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008943 /*
8944 * Remove empty counters, assuming Probe Response and Beacon frame
8945 * counters match. This implementation assumes that there are only two
8946 * counters.
8947 */
8948 if (settings->counter_offset_beacon[0] &&
8949 !settings->counter_offset_beacon[1]) {
8950 csa_off_len = 1;
8951 } else if (settings->counter_offset_beacon[1] &&
8952 !settings->counter_offset_beacon[0]) {
8953 csa_off_len = 1;
8954 settings->counter_offset_beacon[0] =
8955 settings->counter_offset_beacon[1];
8956 settings->counter_offset_presp[0] =
8957 settings->counter_offset_presp[1];
8958 } else if (settings->counter_offset_beacon[1] &&
8959 settings->counter_offset_beacon[0]) {
8960 csa_off_len = 2;
8961 } else {
8962 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8963 return -EINVAL;
8964 }
8965
8966 /* Check CSA counters validity */
8967 if (drv->capa.max_csa_counters &&
8968 csa_off_len > drv->capa.max_csa_counters) {
8969 wpa_printf(MSG_ERROR,
8970 "nl80211: Too many CSA counters provided");
8971 return -EINVAL;
8972 }
8973
8974 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008975 return -EINVAL;
8976
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008977 for (i = 0; i < csa_off_len; i++) {
8978 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8979 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8980
8981 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8982 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8983 settings->cs_count))
8984 return -EINVAL;
8985
8986 if (settings->beacon_csa.probe_resp &&
8987 ((settings->beacon_csa.probe_resp_len <=
8988 csa_c_off_presp) ||
8989 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8990 settings->cs_count)))
8991 return -EINVAL;
8992 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008993
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008994 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8995 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8996 settings->cs_count) ||
8997 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8998 (settings->block_tx &&
8999 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009000 goto error;
9001
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009002 /* beacon_after params */
9003 ret = set_beacon_data(msg, &settings->beacon_after);
9004 if (ret)
9005 goto error;
9006
9007 /* beacon_csa params */
9008 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
9009 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009010 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009011
9012 ret = set_beacon_data(msg, &settings->beacon_csa);
9013 if (ret)
9014 goto error;
9015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009016 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
9017 csa_off_len * sizeof(u16),
9018 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009019 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009020 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
9021 csa_off_len * sizeof(u16),
9022 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009023 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009024
9025 nla_nest_end(msg, beacon_csa);
9026 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9027 if (ret) {
9028 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
9029 ret, strerror(-ret));
9030 }
9031 return ret;
9032
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009033fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009034 ret = -ENOBUFS;
9035error:
9036 nlmsg_free(msg);
9037 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
9038 return ret;
9039}
9040
9041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009042static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
9043 u8 user_priority, u16 admitted_time)
9044{
9045 struct i802_bss *bss = priv;
9046 struct wpa_driver_nl80211_data *drv = bss->drv;
9047 struct nl_msg *msg;
9048 int ret;
9049
9050 wpa_printf(MSG_DEBUG,
9051 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
9052 tsid, admitted_time, user_priority);
9053
9054 if (!is_sta_interface(drv->nlmode))
9055 return -ENOTSUP;
9056
9057 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
9058 if (!msg ||
9059 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9060 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9061 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
9062 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
9063 nlmsg_free(msg);
9064 return -ENOBUFS;
9065 }
9066
9067 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9068 if (ret)
9069 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
9070 ret, strerror(-ret));
9071 return ret;
9072}
9073
9074
9075static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
9076{
9077 struct i802_bss *bss = priv;
9078 struct wpa_driver_nl80211_data *drv = bss->drv;
9079 struct nl_msg *msg;
9080 int ret;
9081
9082 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
9083
9084 if (!is_sta_interface(drv->nlmode))
9085 return -ENOTSUP;
9086
9087 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
9088 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9089 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
9090 nlmsg_free(msg);
9091 return -ENOBUFS;
9092 }
9093
9094 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9095 if (ret)
9096 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
9097 ret, strerror(-ret));
9098 return ret;
9099}
9100
9101
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009102#ifdef CONFIG_TESTING_OPTIONS
9103static int cmd_reply_handler(struct nl_msg *msg, void *arg)
9104{
9105 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9106 struct wpabuf *buf = arg;
9107
9108 if (!buf)
9109 return NL_SKIP;
9110
9111 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
9112 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
9113 return NL_SKIP;
9114 }
9115
9116 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
9117 genlmsg_attrlen(gnlh, 0));
9118
9119 return NL_SKIP;
9120}
9121#endif /* CONFIG_TESTING_OPTIONS */
9122
9123
9124static int vendor_reply_handler(struct nl_msg *msg, void *arg)
9125{
9126 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9127 struct nlattr *nl_vendor_reply, *nl;
9128 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9129 struct wpabuf *buf = arg;
9130 int rem;
9131
9132 if (!buf)
9133 return NL_SKIP;
9134
9135 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9136 genlmsg_attrlen(gnlh, 0), NULL);
9137 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
9138
9139 if (!nl_vendor_reply)
9140 return NL_SKIP;
9141
9142 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
9143 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
9144 return NL_SKIP;
9145 }
9146
9147 nla_for_each_nested(nl, nl_vendor_reply, rem) {
9148 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
9149 }
9150
9151 return NL_SKIP;
9152}
9153
9154
9155static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
9156 unsigned int subcmd, const u8 *data,
9157 size_t data_len, struct wpabuf *buf)
9158{
9159 struct i802_bss *bss = priv;
9160 struct wpa_driver_nl80211_data *drv = bss->drv;
9161 struct nl_msg *msg;
9162 int ret;
9163
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009164#ifdef CONFIG_TESTING_OPTIONS
9165 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009166 msg = nlmsg_alloc();
9167 if (!msg)
9168 return -ENOMEM;
9169
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009170 nl80211_cmd(drv, msg, 0, subcmd);
9171 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
9172 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009173 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009174 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
9175 if (ret)
9176 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
9177 ret);
9178 return ret;
9179 }
9180#endif /* CONFIG_TESTING_OPTIONS */
9181
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009182 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
9183 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
9184 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
9185 (data &&
9186 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
9187 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009188
9189 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
9190 if (ret)
9191 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
9192 ret);
9193 return ret;
9194
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009195fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009196 nlmsg_free(msg);
9197 return -ENOBUFS;
9198}
9199
9200
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009201static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
9202 u8 qos_map_set_len)
9203{
9204 struct i802_bss *bss = priv;
9205 struct wpa_driver_nl80211_data *drv = bss->drv;
9206 struct nl_msg *msg;
9207 int ret;
9208
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009209 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
9210 qos_map_set, qos_map_set_len);
9211
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009212 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
9213 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
9214 nlmsg_free(msg);
9215 return -ENOBUFS;
9216 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009217
9218 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9219 if (ret)
9220 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
9221
9222 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009223}
9224
9225
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009226static int nl80211_set_wowlan(void *priv,
9227 const struct wowlan_triggers *triggers)
9228{
9229 struct i802_bss *bss = priv;
9230 struct wpa_driver_nl80211_data *drv = bss->drv;
9231 struct nl_msg *msg;
9232 struct nlattr *wowlan_triggers;
9233 int ret;
9234
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009235 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
9236
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009237 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009238 !(wowlan_triggers = nla_nest_start(msg,
9239 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
9240 (triggers->any &&
9241 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9242 (triggers->disconnect &&
9243 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9244 (triggers->magic_pkt &&
9245 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9246 (triggers->gtk_rekey_failure &&
9247 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9248 (triggers->eap_identity_req &&
9249 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9250 (triggers->four_way_handshake &&
9251 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9252 (triggers->rfkill_release &&
9253 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9254 nlmsg_free(msg);
9255 return -ENOBUFS;
9256 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009257
9258 nla_nest_end(msg, wowlan_triggers);
9259
9260 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9261 if (ret)
9262 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9263
9264 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009265}
9266
9267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009268#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009269static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9270{
9271 struct i802_bss *bss = priv;
9272 struct wpa_driver_nl80211_data *drv = bss->drv;
9273 struct nl_msg *msg;
9274 struct nlattr *params;
9275
9276 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9277
9278 if (!drv->roaming_vendor_cmd_avail) {
9279 wpa_printf(MSG_DEBUG,
9280 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9281 return -1;
9282 }
9283
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009284 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9285 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9286 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9287 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9288 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9289 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9290 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9291 QCA_ROAMING_NOT_ALLOWED) ||
9292 (bssid &&
9293 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9294 nlmsg_free(msg);
9295 return -1;
9296 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009297 nla_nest_end(msg, params);
9298
9299 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009300}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009301
9302
Roshan Pius3a1667e2018-07-03 15:17:14 -07009303static int nl80211_disable_fils(void *priv, int disable)
9304{
9305 struct i802_bss *bss = priv;
9306 struct wpa_driver_nl80211_data *drv = bss->drv;
9307 struct nl_msg *msg;
9308 struct nlattr *params;
9309
9310 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9311
9312 if (!drv->set_wifi_conf_vendor_cmd_avail)
9313 return -1;
9314
9315 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9316 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9317 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9318 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9319 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9320 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9321 disable)) {
9322 nlmsg_free(msg);
9323 return -1;
9324 }
9325 nla_nest_end(msg, params);
9326
9327 return send_and_recv_msgs(drv, msg, NULL, NULL);
9328}
9329
9330
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009331/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9332#define WPA_SUPPLICANT_CLIENT_ID 1
9333
9334static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9335 const u8 *bssid)
9336{
9337 struct i802_bss *bss = priv;
9338 struct wpa_driver_nl80211_data *drv = bss->drv;
9339 struct nl_msg *msg;
9340 struct nlattr *params, *nlbssids, *attr;
9341 unsigned int i;
9342
9343 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9344 num_bssid);
9345
9346 if (!drv->roam_vendor_cmd_avail)
9347 return -1;
9348
9349 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9350 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9351 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9352 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9353 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9354 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9355 QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9356 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9357 WPA_SUPPLICANT_CLIENT_ID) ||
9358 nla_put_u32(msg,
9359 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9360 num_bssid))
9361 goto fail;
9362
9363 nlbssids = nla_nest_start(
9364 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9365 if (!nlbssids)
9366 goto fail;
9367
9368 for (i = 0; i < num_bssid; i++) {
9369 attr = nla_nest_start(msg, i);
9370 if (!attr)
9371 goto fail;
9372 if (nla_put(msg,
9373 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9374 ETH_ALEN, &bssid[i * ETH_ALEN]))
9375 goto fail;
9376 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9377 MAC2STR(&bssid[i * ETH_ALEN]));
9378 nla_nest_end(msg, attr);
9379 }
9380 nla_nest_end(msg, nlbssids);
9381 nla_nest_end(msg, params);
9382
9383 return send_and_recv_msgs(drv, msg, NULL, NULL);
9384
9385fail:
9386 nlmsg_free(msg);
9387 return -1;
9388}
9389
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009390#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009391
9392
9393static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9394{
9395 struct i802_bss *bss = priv;
9396 struct wpa_driver_nl80211_data *drv = bss->drv;
9397 int new_addr = addr != NULL;
9398
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009399 if (TEST_FAIL())
9400 return -1;
9401
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009402 if (!addr)
9403 addr = drv->perm_addr;
9404
9405 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9406 return -1;
9407
9408 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9409 {
9410 wpa_printf(MSG_DEBUG,
9411 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9412 bss->ifname, MAC2STR(addr));
9413 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9414 1) < 0) {
9415 wpa_printf(MSG_DEBUG,
9416 "nl80211: Could not restore interface UP after failed set_mac_addr");
9417 }
9418 return -1;
9419 }
9420
9421 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9422 bss->ifname, MAC2STR(addr));
9423 drv->addr_changed = new_addr;
9424 os_memcpy(bss->addr, addr, ETH_ALEN);
9425
9426 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9427 {
9428 wpa_printf(MSG_DEBUG,
9429 "nl80211: Could not restore interface UP after set_mac_addr");
9430 }
9431
9432 return 0;
9433}
9434
9435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009436#ifdef CONFIG_MESH
9437
9438static int wpa_driver_nl80211_init_mesh(void *priv)
9439{
9440 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9441 wpa_printf(MSG_INFO,
9442 "nl80211: Failed to set interface into mesh mode");
9443 return -1;
9444 }
9445 return 0;
9446}
9447
9448
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009449static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9450 size_t mesh_id_len)
9451{
9452 if (mesh_id) {
Hai Shalom74f70d42019-02-11 14:42:39 -08009453 wpa_printf(MSG_DEBUG, " * Mesh ID (SSID)=%s",
9454 wpa_ssid_txt(mesh_id, mesh_id_len));
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009455 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9456 }
9457
9458 return 0;
9459}
9460
9461
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009462static int nl80211_put_mesh_config(struct nl_msg *msg,
9463 struct wpa_driver_mesh_bss_params *params)
9464{
9465 struct nlattr *container;
9466
9467 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9468 if (!container)
9469 return -1;
9470
9471 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009472 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9473 params->auto_plinks)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009474 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9475 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009476 params->max_peer_links)) ||
9477 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9478 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9479 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009480 return -1;
9481
9482 /*
9483 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9484 * the timer could disconnect stations even in that case.
9485 */
9486 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9487 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
9488 params->peer_link_timeout)) {
9489 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9490 return -1;
9491 }
9492
9493 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9494 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9495 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9496 return -1;
9497 }
9498
9499 nla_nest_end(msg, container);
9500
9501 return 0;
9502}
9503
9504
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009505static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009506 struct wpa_driver_mesh_join_params *params)
9507{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009508 struct wpa_driver_nl80211_data *drv = bss->drv;
9509 struct nl_msg *msg;
9510 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08009511 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009512
9513 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9514 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009515 if (!msg ||
9516 nl80211_put_freq_params(msg, &params->freq) ||
9517 nl80211_put_basic_rates(msg, params->basic_rates) ||
9518 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009519 nl80211_put_beacon_int(msg, params->beacon_int) ||
9520 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009521 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009522
9523 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
9524
9525 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9526 if (!container)
9527 goto fail;
9528
9529 if (params->ies) {
9530 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
9531 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9532 params->ies))
9533 goto fail;
9534 }
9535 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9536 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
9537 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9538 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9539 goto fail;
9540 }
9541 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9542 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9543 goto fail;
9544 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9545 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9546 goto fail;
9547 nla_nest_end(msg, container);
9548
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009549 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9550 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9551 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
9552 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009553 goto fail;
9554
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009555 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9556 msg = NULL;
9557 if (ret) {
9558 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9559 ret, strerror(-ret));
9560 goto fail;
9561 }
9562 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009563 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009564 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9565
9566fail:
9567 nlmsg_free(msg);
9568 return ret;
9569}
9570
9571
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009572static int
9573wpa_driver_nl80211_join_mesh(void *priv,
9574 struct wpa_driver_mesh_join_params *params)
9575{
9576 struct i802_bss *bss = priv;
9577 int ret, timeout;
9578
9579 timeout = params->conf.peer_link_timeout;
9580
9581 /* Disable kernel inactivity timer */
9582 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9583 params->conf.peer_link_timeout = 0;
9584
9585 ret = nl80211_join_mesh(bss, params);
9586 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9587 wpa_printf(MSG_DEBUG,
9588 "nl80211: Mesh join retry for peer_link_timeout");
9589 /*
9590 * Old kernel does not support setting
9591 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9592 * into future from peer_link_timeout.
9593 */
9594 params->conf.peer_link_timeout = timeout + 60;
9595 ret = nl80211_join_mesh(priv, params);
9596 }
9597
9598 params->conf.peer_link_timeout = timeout;
9599 return ret;
9600}
9601
9602
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009603static int wpa_driver_nl80211_leave_mesh(void *priv)
9604{
9605 struct i802_bss *bss = priv;
9606 struct wpa_driver_nl80211_data *drv = bss->drv;
9607 struct nl_msg *msg;
9608 int ret;
9609
9610 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9611 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
9612 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9613 if (ret) {
9614 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9615 ret, strerror(-ret));
9616 } else {
9617 wpa_printf(MSG_DEBUG,
9618 "nl80211: mesh leave request send successfully");
9619 }
9620
9621 if (wpa_driver_nl80211_set_mode(drv->first_bss,
9622 NL80211_IFTYPE_STATION)) {
9623 wpa_printf(MSG_INFO,
9624 "nl80211: Failed to set interface into station mode");
9625 }
9626 return ret;
9627}
9628
9629#endif /* CONFIG_MESH */
9630
9631
9632static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9633 const u8 *ipaddr, int prefixlen,
9634 const u8 *addr)
9635{
9636#ifdef CONFIG_LIBNL3_ROUTE
9637 struct i802_bss *bss = priv;
9638 struct wpa_driver_nl80211_data *drv = bss->drv;
9639 struct rtnl_neigh *rn;
9640 struct nl_addr *nl_ipaddr = NULL;
9641 struct nl_addr *nl_lladdr = NULL;
9642 int family, addrsize;
9643 int res;
9644
9645 if (!ipaddr || prefixlen == 0 || !addr)
9646 return -EINVAL;
9647
9648 if (bss->br_ifindex == 0) {
9649 wpa_printf(MSG_DEBUG,
9650 "nl80211: bridge must be set before adding an ip neigh to it");
9651 return -1;
9652 }
9653
9654 if (!drv->rtnl_sk) {
9655 wpa_printf(MSG_DEBUG,
9656 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9657 return -1;
9658 }
9659
9660 if (version == 4) {
9661 family = AF_INET;
9662 addrsize = 4;
9663 } else if (version == 6) {
9664 family = AF_INET6;
9665 addrsize = 16;
9666 } else {
9667 return -EINVAL;
9668 }
9669
9670 rn = rtnl_neigh_alloc();
9671 if (rn == NULL)
9672 return -ENOMEM;
9673
9674 /* set the destination ip address for neigh */
9675 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9676 if (nl_ipaddr == NULL) {
9677 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9678 res = -ENOMEM;
9679 goto errout;
9680 }
9681 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9682 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9683 if (res) {
9684 wpa_printf(MSG_DEBUG,
9685 "nl80211: neigh set destination addr failed");
9686 goto errout;
9687 }
9688
9689 /* set the corresponding lladdr for neigh */
9690 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9691 if (nl_lladdr == NULL) {
9692 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9693 res = -ENOMEM;
9694 goto errout;
9695 }
9696 rtnl_neigh_set_lladdr(rn, nl_lladdr);
9697
9698 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9699 rtnl_neigh_set_state(rn, NUD_PERMANENT);
9700
9701 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9702 if (res) {
9703 wpa_printf(MSG_DEBUG,
9704 "nl80211: Adding bridge ip neigh failed: %s",
9705 strerror(errno));
9706 }
9707errout:
9708 if (nl_lladdr)
9709 nl_addr_put(nl_lladdr);
9710 if (nl_ipaddr)
9711 nl_addr_put(nl_ipaddr);
9712 if (rn)
9713 rtnl_neigh_put(rn);
9714 return res;
9715#else /* CONFIG_LIBNL3_ROUTE */
9716 return -1;
9717#endif /* CONFIG_LIBNL3_ROUTE */
9718}
9719
9720
9721static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9722 const u8 *ipaddr)
9723{
9724#ifdef CONFIG_LIBNL3_ROUTE
9725 struct i802_bss *bss = priv;
9726 struct wpa_driver_nl80211_data *drv = bss->drv;
9727 struct rtnl_neigh *rn;
9728 struct nl_addr *nl_ipaddr;
9729 int family, addrsize;
9730 int res;
9731
9732 if (!ipaddr)
9733 return -EINVAL;
9734
9735 if (version == 4) {
9736 family = AF_INET;
9737 addrsize = 4;
9738 } else if (version == 6) {
9739 family = AF_INET6;
9740 addrsize = 16;
9741 } else {
9742 return -EINVAL;
9743 }
9744
9745 if (bss->br_ifindex == 0) {
9746 wpa_printf(MSG_DEBUG,
9747 "nl80211: bridge must be set to delete an ip neigh");
9748 return -1;
9749 }
9750
9751 if (!drv->rtnl_sk) {
9752 wpa_printf(MSG_DEBUG,
9753 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9754 return -1;
9755 }
9756
9757 rn = rtnl_neigh_alloc();
9758 if (rn == NULL)
9759 return -ENOMEM;
9760
9761 /* set the destination ip address for neigh */
9762 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9763 if (nl_ipaddr == NULL) {
9764 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9765 res = -ENOMEM;
9766 goto errout;
9767 }
9768 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9769 if (res) {
9770 wpa_printf(MSG_DEBUG,
9771 "nl80211: neigh set destination addr failed");
9772 goto errout;
9773 }
9774
9775 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9776
9777 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9778 if (res) {
9779 wpa_printf(MSG_DEBUG,
9780 "nl80211: Deleting bridge ip neigh failed: %s",
9781 strerror(errno));
9782 }
9783errout:
9784 if (nl_ipaddr)
9785 nl_addr_put(nl_ipaddr);
9786 if (rn)
9787 rtnl_neigh_put(rn);
9788 return res;
9789#else /* CONFIG_LIBNL3_ROUTE */
9790 return -1;
9791#endif /* CONFIG_LIBNL3_ROUTE */
9792}
9793
9794
9795static int linux_write_system_file(const char *path, unsigned int val)
9796{
9797 char buf[50];
9798 int fd, len;
9799
9800 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9801 if (os_snprintf_error(sizeof(buf), len))
9802 return -1;
9803
9804 fd = open(path, O_WRONLY);
9805 if (fd < 0)
9806 return -1;
9807
9808 if (write(fd, buf, len) < 0) {
9809 wpa_printf(MSG_DEBUG,
9810 "nl80211: Failed to write Linux system file: %s with the value of %d",
9811 path, val);
9812 close(fd);
9813 return -1;
9814 }
9815 close(fd);
9816
9817 return 0;
9818}
9819
9820
9821static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9822{
9823 switch (attr) {
9824 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07009825 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009826 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9827 return "hairpin_mode";
9828 }
9829
9830 return NULL;
9831}
9832
9833
9834static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9835 unsigned int val)
9836{
9837 struct i802_bss *bss = priv;
9838 char path[128];
9839 const char *attr_txt;
9840
9841 attr_txt = drv_br_port_attr_str(attr);
9842 if (attr_txt == NULL)
9843 return -EINVAL;
9844
9845 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9846 bss->ifname, attr_txt);
9847
9848 if (linux_write_system_file(path, val))
9849 return -1;
9850
9851 return 0;
9852}
9853
9854
9855static const char * drv_br_net_param_str(enum drv_br_net_param param)
9856{
9857 switch (param) {
9858 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9859 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07009860 default:
9861 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009862 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009863}
9864
9865
9866static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9867 unsigned int val)
9868{
9869 struct i802_bss *bss = priv;
9870 char path[128];
9871 const char *param_txt;
9872 int ip_version = 4;
9873
Dmitry Shmidt83474442015-04-15 13:47:09 -07009874 if (param == DRV_BR_MULTICAST_SNOOPING) {
9875 os_snprintf(path, sizeof(path),
9876 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9877 bss->brname);
9878 goto set_val;
9879 }
9880
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009881 param_txt = drv_br_net_param_str(param);
9882 if (param_txt == NULL)
9883 return -EINVAL;
9884
9885 switch (param) {
9886 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9887 ip_version = 4;
9888 break;
9889 default:
9890 return -EINVAL;
9891 }
9892
9893 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9894 ip_version, bss->brname, param_txt);
9895
Dmitry Shmidt83474442015-04-15 13:47:09 -07009896set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009897 if (linux_write_system_file(path, val))
9898 return -1;
9899
9900 return 0;
9901}
9902
9903
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009904#ifdef CONFIG_DRIVER_NL80211_QCA
9905
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009906static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9907{
9908 switch (hw_mode) {
9909 case HOSTAPD_MODE_IEEE80211B:
9910 return QCA_ACS_MODE_IEEE80211B;
9911 case HOSTAPD_MODE_IEEE80211G:
9912 return QCA_ACS_MODE_IEEE80211G;
9913 case HOSTAPD_MODE_IEEE80211A:
9914 return QCA_ACS_MODE_IEEE80211A;
9915 case HOSTAPD_MODE_IEEE80211AD:
9916 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07009917 case HOSTAPD_MODE_IEEE80211ANY:
9918 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009919 default:
9920 return -1;
9921 }
9922}
9923
9924
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009925static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9926{
9927 int i, len, ret;
9928 u32 *freqs;
9929
9930 if (!freq_list)
9931 return 0;
9932 len = int_array_len(freq_list);
9933 freqs = os_malloc(sizeof(u32) * len);
9934 if (!freqs)
9935 return -1;
9936 for (i = 0; i < len; i++)
9937 freqs[i] = freq_list[i];
9938 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9939 sizeof(u32) * len, freqs);
9940 os_free(freqs);
9941 return ret;
9942}
9943
9944
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009945static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9946{
9947 struct i802_bss *bss = priv;
9948 struct wpa_driver_nl80211_data *drv = bss->drv;
9949 struct nl_msg *msg;
9950 struct nlattr *data;
9951 int ret;
9952 int mode;
9953
9954 mode = hw_mode_to_qca_acs(params->hw_mode);
9955 if (mode < 0)
9956 return -1;
9957
9958 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9959 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9960 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9961 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9962 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9963 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9964 (params->ht_enabled &&
9965 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9966 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009967 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9968 (params->vht_enabled &&
9969 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9970 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9971 params->ch_width) ||
9972 (params->ch_list_len &&
9973 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009974 params->ch_list)) ||
9975 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009976 nlmsg_free(msg);
9977 return -ENOBUFS;
9978 }
9979 nla_nest_end(msg, data);
9980
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009981 wpa_printf(MSG_DEBUG,
9982 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9983 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9984 params->vht_enabled, params->ch_width, params->ch_list_len);
9985
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009986 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9987 if (ret) {
9988 wpa_printf(MSG_DEBUG,
9989 "nl80211: Failed to invoke driver ACS function: %s",
9990 strerror(errno));
9991 }
9992 return ret;
9993}
9994
9995
Ravi Joshie6ccb162015-07-16 17:45:41 -07009996static int nl80211_set_band(void *priv, enum set_band band)
9997{
9998 struct i802_bss *bss = priv;
9999 struct wpa_driver_nl80211_data *drv = bss->drv;
10000 struct nl_msg *msg;
10001 struct nlattr *data;
10002 int ret;
10003 enum qca_set_band qca_band;
10004
10005 if (!drv->setband_vendor_cmd_avail)
10006 return -1;
10007
10008 switch (band) {
10009 case WPA_SETBAND_AUTO:
10010 qca_band = QCA_SETBAND_AUTO;
10011 break;
10012 case WPA_SETBAND_5G:
10013 qca_band = QCA_SETBAND_5G;
10014 break;
10015 case WPA_SETBAND_2G:
10016 qca_band = QCA_SETBAND_2G;
10017 break;
10018 default:
10019 return -1;
10020 }
10021
10022 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10023 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10024 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10025 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
10026 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10027 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
10028 nlmsg_free(msg);
10029 return -ENOBUFS;
10030 }
10031 nla_nest_end(msg, data);
10032
10033 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10034 if (ret) {
10035 wpa_printf(MSG_DEBUG,
10036 "nl80211: Driver setband function failed: %s",
10037 strerror(errno));
10038 }
10039 return ret;
10040}
10041
10042
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010043struct nl80211_pcl {
10044 unsigned int num;
10045 unsigned int *freq_list;
10046};
10047
10048static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
10049{
10050 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10051 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10052 struct nl80211_pcl *param = arg;
10053 struct nlattr *nl_vend, *attr;
10054 enum qca_iface_type iface_type;
10055 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10056 unsigned int num, max_num;
10057 u32 *freqs;
10058
10059 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10060 genlmsg_attrlen(gnlh, 0), NULL);
10061
10062 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
10063 if (!nl_vend)
10064 return NL_SKIP;
10065
10066 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10067 nla_data(nl_vend), nla_len(nl_vend), NULL);
10068
10069 attr = tb_vendor[
10070 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
10071 if (!attr) {
10072 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
10073 param->num = 0;
10074 return NL_SKIP;
10075 }
10076
10077 iface_type = (enum qca_iface_type) nla_get_u32(attr);
10078 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
10079 iface_type);
10080
10081 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
10082 if (!attr) {
10083 wpa_printf(MSG_ERROR,
10084 "nl80211: preferred_freq_list couldn't be found");
10085 param->num = 0;
10086 return NL_SKIP;
10087 }
10088
10089 /*
10090 * param->num has the maximum number of entries for which there
10091 * is room in the freq_list provided by the caller.
10092 */
10093 freqs = nla_data(attr);
10094 max_num = nla_len(attr) / sizeof(u32);
10095 if (max_num > param->num)
10096 max_num = param->num;
10097 for (num = 0; num < max_num; num++)
10098 param->freq_list[num] = freqs[num];
10099 param->num = num;
10100
10101 return NL_SKIP;
10102}
10103
10104
10105static int nl80211_get_pref_freq_list(void *priv,
10106 enum wpa_driver_if_type if_type,
10107 unsigned int *num,
10108 unsigned int *freq_list)
10109{
10110 struct i802_bss *bss = priv;
10111 struct wpa_driver_nl80211_data *drv = bss->drv;
10112 struct nl_msg *msg;
10113 int ret;
10114 unsigned int i;
10115 struct nlattr *params;
10116 struct nl80211_pcl param;
10117 enum qca_iface_type iface_type;
10118
10119 if (!drv->get_pref_freq_list)
10120 return -1;
10121
10122 switch (if_type) {
10123 case WPA_IF_STATION:
10124 iface_type = QCA_IFACE_TYPE_STA;
10125 break;
10126 case WPA_IF_AP_BSS:
10127 iface_type = QCA_IFACE_TYPE_AP;
10128 break;
10129 case WPA_IF_P2P_GO:
10130 iface_type = QCA_IFACE_TYPE_P2P_GO;
10131 break;
10132 case WPA_IF_P2P_CLIENT:
10133 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
10134 break;
10135 case WPA_IF_IBSS:
10136 iface_type = QCA_IFACE_TYPE_IBSS;
10137 break;
10138 case WPA_IF_TDLS:
10139 iface_type = QCA_IFACE_TYPE_TDLS;
10140 break;
10141 default:
10142 return -1;
10143 }
10144
10145 param.num = *num;
10146 param.freq_list = freq_list;
10147
10148 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10149 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10150 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10151 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10152 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
10153 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10154 nla_put_u32(msg,
10155 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
10156 iface_type)) {
10157 wpa_printf(MSG_ERROR,
10158 "%s: err in adding vendor_cmd and vendor_data",
10159 __func__);
10160 nlmsg_free(msg);
10161 return -1;
10162 }
10163 nla_nest_end(msg, params);
10164
10165 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
10166 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
10167 if (ret) {
10168 wpa_printf(MSG_ERROR,
10169 "%s: err in send_and_recv_msgs", __func__);
10170 return ret;
10171 }
10172
10173 *num = param.num;
10174
10175 for (i = 0; i < *num; i++) {
10176 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
10177 i, freq_list[i]);
10178 }
10179
10180 return 0;
10181}
10182
10183
10184static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
10185{
10186 struct i802_bss *bss = priv;
10187 struct wpa_driver_nl80211_data *drv = bss->drv;
10188 struct nl_msg *msg;
10189 int ret;
10190 struct nlattr *params;
10191
10192 if (!drv->set_prob_oper_freq)
10193 return -1;
10194
10195 wpa_printf(MSG_DEBUG,
10196 "nl80211: Set P2P probable operating freq %u for ifindex %d",
10197 freq, bss->ifindex);
10198
10199 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10200 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10201 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10202 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
10203 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10204 nla_put_u32(msg,
10205 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
10206 QCA_IFACE_TYPE_P2P_CLIENT) ||
10207 nla_put_u32(msg,
10208 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
10209 freq)) {
10210 wpa_printf(MSG_ERROR,
10211 "%s: err in adding vendor_cmd and vendor_data",
10212 __func__);
10213 nlmsg_free(msg);
10214 return -1;
10215 }
10216 nla_nest_end(msg, params);
10217
10218 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10219 msg = NULL;
10220 if (ret) {
10221 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
10222 __func__);
10223 return ret;
10224 }
10225 nlmsg_free(msg);
10226 return 0;
10227}
10228
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010229
10230static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
10231 unsigned int period, unsigned int interval,
10232 unsigned int count, const u8 *device_types,
10233 size_t dev_types_len,
10234 const u8 *ies, size_t ies_len)
10235{
10236 struct i802_bss *bss = priv;
10237 struct wpa_driver_nl80211_data *drv = bss->drv;
10238 struct nl_msg *msg;
10239 struct nlattr *container;
10240 int ret;
10241
10242 wpa_printf(MSG_DEBUG,
10243 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
10244 freq, period, interval, count);
10245
10246 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10247 return -1;
10248
10249 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10250 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10251 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10252 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10253 goto fail;
10254
10255 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10256 if (!container)
10257 goto fail;
10258
10259 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10260 freq) ||
10261 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10262 period) ||
10263 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10264 interval) ||
10265 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10266 count) ||
10267 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10268 dev_types_len, device_types) ||
10269 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10270 ies_len, ies))
10271 goto fail;
10272
10273 nla_nest_end(msg, container);
10274 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10275 msg = NULL;
10276 if (ret) {
10277 wpa_printf(MSG_DEBUG,
10278 "nl80211: Failed to send P2P Listen offload vendor command");
10279 goto fail;
10280 }
10281
10282 return 0;
10283
10284fail:
10285 nlmsg_free(msg);
10286 return -1;
10287}
10288
10289
10290static int nl80211_p2p_lo_stop(void *priv)
10291{
10292 struct i802_bss *bss = priv;
10293 struct wpa_driver_nl80211_data *drv = bss->drv;
10294 struct nl_msg *msg;
10295
10296 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10297
10298 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10299 return -1;
10300
10301 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10302 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10303 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10304 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10305 nlmsg_free(msg);
10306 return -1;
10307 }
10308
10309 return send_and_recv_msgs(drv, msg, NULL, NULL);
10310}
10311
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010312
10313static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10314{
10315 struct i802_bss *bss = priv;
10316 struct wpa_driver_nl80211_data *drv = bss->drv;
10317 struct nl_msg *msg;
10318 struct nlattr *params;
10319 int ret;
10320 u32 tdls_mode;
10321
10322 wpa_printf(MSG_DEBUG,
10323 "nl80211: Set TDKS mode: tdls_external_control=%d",
10324 tdls_external_control);
10325
10326 if (tdls_external_control == 1)
10327 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10328 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10329 else
10330 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10331
10332 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10333 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10334 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10335 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10336 goto fail;
10337
10338 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10339 if (!params)
10340 goto fail;
10341
10342 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10343 tdls_mode))
10344 goto fail;
10345
10346 nla_nest_end(msg, params);
10347
10348 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10349 msg = NULL;
10350 if (ret) {
10351 wpa_printf(MSG_ERROR,
10352 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10353 ret, strerror(-ret));
10354 goto fail;
10355 }
10356 return 0;
10357fail:
10358 nlmsg_free(msg);
10359 return -1;
10360}
10361
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010362
10363#ifdef CONFIG_MBO
10364
10365static enum mbo_transition_reject_reason
10366nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10367{
10368 switch (status) {
10369 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10370 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10371 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10372 return MBO_TRANSITION_REJECT_REASON_DELAY;
10373 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10374 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10375 case QCA_STATUS_REJECT_LOW_RSSI:
10376 return MBO_TRANSITION_REJECT_REASON_RSSI;
10377 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10378 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10379 case QCA_STATUS_REJECT_UNKNOWN:
10380 default:
10381 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10382 }
10383}
10384
10385
10386static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10387 struct nlattr *tb[], int num)
10388{
10389 enum qca_wlan_btm_candidate_status status;
10390 char buf[50];
10391
10392 os_memcpy(candidate->bssid,
10393 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10394 ETH_ALEN);
10395
10396 status = nla_get_u32(
10397 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10398 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10399 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10400
10401 if (candidate->is_accept)
10402 os_snprintf(buf, sizeof(buf), "Accepted");
10403 else
10404 os_snprintf(buf, sizeof(buf),
10405 "Rejected, Reject_reason: %d",
10406 candidate->reject_reason);
10407 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10408 num, MAC2STR(candidate->bssid), buf);
10409}
10410
10411
10412static int
10413nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10414{
10415 struct wpa_bss_candidate_info *info = arg;
10416 struct candidate_list *candidate = info->candidates;
10417 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10418 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10419 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10420 static struct nla_policy policy[
10421 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10422 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10423 .minlen = ETH_ALEN
10424 },
10425 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10426 .type = NLA_U32,
10427 },
10428 };
10429 struct nlattr *attr;
10430 int rem;
10431 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10432 u8 num;
10433
10434 num = info->num; /* number of candidates sent to driver */
10435 info->num = 0;
10436 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10437 genlmsg_attrlen(gnlh, 0), NULL);
10438
10439 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10440 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10441 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10442 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10443 return NL_SKIP;
10444
10445 wpa_printf(MSG_DEBUG,
10446 "nl80211: WNM Candidate list received from driver");
10447 nla_for_each_nested(attr,
10448 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10449 rem) {
10450 if (info->num >= num ||
10451 nla_parse_nested(
10452 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10453 attr, policy) ||
10454 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10455 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10456 break;
10457
10458 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10459
10460 candidate++;
10461 info->num++;
10462 }
10463
10464 return NL_SKIP;
10465}
10466
10467
10468static struct wpa_bss_candidate_info *
10469nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10470{
10471 struct i802_bss *bss = priv;
10472 struct wpa_driver_nl80211_data *drv = bss->drv;
10473 struct nl_msg *msg;
10474 struct nlattr *attr, *attr1, *attr2;
10475 struct wpa_bss_candidate_info *info;
10476 u8 i;
10477 int ret;
10478 u8 *pos;
10479
10480 if (!drv->fetch_bss_trans_status)
10481 return NULL;
10482
10483 info = os_zalloc(sizeof(*info));
10484 if (!info)
10485 return NULL;
10486 /* Allocate memory for number of candidates sent to driver */
10487 info->candidates = os_calloc(params->n_candidates,
10488 sizeof(*info->candidates));
10489 if (!info->candidates) {
10490 os_free(info);
10491 return NULL;
10492 }
10493
10494 /* Copy the number of candidates being sent to driver. This is used in
10495 * nl80211_get_bss_transition_status_handler() to limit the number of
10496 * candidates that can be populated in info->candidates and will be
10497 * later overwritten with the actual number of candidates received from
10498 * the driver.
10499 */
10500 info->num = params->n_candidates;
10501
10502 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10503 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10504 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10505 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10506 goto fail;
10507
10508 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10509 if (!attr)
10510 goto fail;
10511
10512 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10513 params->mbo_transition_reason))
10514 goto fail;
10515
10516 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10517 if (!attr1)
10518 goto fail;
10519
10520 wpa_printf(MSG_DEBUG,
10521 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10522 params->mbo_transition_reason, params->n_candidates);
10523 pos = params->bssid;
10524 for (i = 0; i < params->n_candidates; i++) {
10525 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
10526 MAC2STR(pos));
10527 attr2 = nla_nest_start(msg, i);
10528 if (!attr2 ||
10529 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10530 ETH_ALEN, pos))
10531 goto fail;
10532 pos += ETH_ALEN;
10533 nla_nest_end(msg, attr2);
10534 }
10535
10536 nla_nest_end(msg, attr1);
10537 nla_nest_end(msg, attr);
10538
10539 ret = send_and_recv_msgs(drv, msg,
10540 nl80211_get_bss_transition_status_handler,
10541 info);
10542 msg = NULL;
10543 if (ret) {
10544 wpa_printf(MSG_ERROR,
10545 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10546 ret, strerror(-ret));
10547 goto fail;
10548 }
10549 return info;
10550
10551fail:
10552 nlmsg_free(msg);
10553 os_free(info->candidates);
10554 os_free(info);
10555 return NULL;
10556}
10557
10558
10559/**
10560 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10561 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10562 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10563 * Returns: 0 on success, -1 on failure
10564 */
10565static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10566{
10567 struct i802_bss *bss = priv;
10568 struct wpa_driver_nl80211_data *drv = bss->drv;
10569 struct nl_msg *msg;
10570 struct nlattr *attr;
10571 int ret = -1;
10572
10573 if (!drv->set_wifi_conf_vendor_cmd_avail)
10574 return -1;
10575
10576 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10577 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10578 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10579 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10580 goto fail;
10581
10582 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10583 if (!attr)
10584 goto fail;
10585
10586 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10587 ignore_disallow);
10588 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10589 ignore_disallow))
10590 goto fail;
10591
10592 nla_nest_end(msg, attr);
10593
10594 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10595 msg = NULL;
10596 if (ret) {
10597 wpa_printf(MSG_ERROR,
10598 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10599 ret, strerror(-ret));
10600 goto fail;
10601 }
10602
10603fail:
10604 nlmsg_free(msg);
10605 return ret;
10606}
10607
10608#endif /* CONFIG_MBO */
10609
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010610#endif /* CONFIG_DRIVER_NL80211_QCA */
10611
10612
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010613static int nl80211_write_to_file(const char *name, unsigned int val)
10614{
10615 int fd, len;
10616 char tmp[128];
10617
10618 fd = open(name, O_RDWR);
10619 if (fd < 0) {
10620 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10621 name, strerror(errno));
10622 return fd;
10623 }
10624
10625 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10626 len = write(fd, tmp, len);
10627 if (len < 0)
10628 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10629 name, strerror(errno));
10630 close(fd);
10631
10632 return 0;
10633}
10634
10635
10636static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10637{
10638 struct i802_bss *bss = priv;
10639 char path[128];
10640 int ret;
10641
10642 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10643 filter_flags);
10644
10645 /* Configure filtering of unicast frame encrypted using GTK */
10646 ret = os_snprintf(path, sizeof(path),
10647 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10648 bss->ifname);
10649 if (os_snprintf_error(sizeof(path), ret))
10650 return -1;
10651
10652 ret = nl80211_write_to_file(path,
10653 !!(filter_flags &
10654 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10655 if (ret) {
10656 wpa_printf(MSG_ERROR,
10657 "nl80211: Failed to set IPv4 unicast in multicast filter");
10658 return ret;
10659 }
10660
10661 os_snprintf(path, sizeof(path),
10662 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10663 bss->ifname);
10664 ret = nl80211_write_to_file(path,
10665 !!(filter_flags &
10666 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10667
10668 if (ret) {
10669 wpa_printf(MSG_ERROR,
10670 "nl80211: Failed to set IPv6 unicast in multicast filter");
10671 return ret;
10672 }
10673
10674 /* Configure filtering of unicast frame encrypted using GTK */
10675 os_snprintf(path, sizeof(path),
10676 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10677 bss->ifname);
10678 ret = nl80211_write_to_file(path,
10679 !!(filter_flags &
10680 WPA_DATA_FRAME_FILTER_FLAG_ARP));
10681 if (ret) {
10682 wpa_printf(MSG_ERROR,
10683 "nl80211: Failed set gratuitous ARP filter");
10684 return ret;
10685 }
10686
10687 /* Configure filtering of IPv6 NA frames */
10688 os_snprintf(path, sizeof(path),
10689 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10690 bss->ifname);
10691 ret = nl80211_write_to_file(path,
10692 !!(filter_flags &
10693 WPA_DATA_FRAME_FILTER_FLAG_NA));
10694 if (ret) {
10695 wpa_printf(MSG_ERROR,
10696 "nl80211: Failed to set unsolicited NA filter");
10697 return ret;
10698 }
10699
10700 return 0;
10701}
10702
10703
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010704static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10705 const u8 **ext_capa, const u8 **ext_capa_mask,
10706 unsigned int *ext_capa_len)
10707{
10708 struct i802_bss *bss = priv;
10709 struct wpa_driver_nl80211_data *drv = bss->drv;
10710 enum nl80211_iftype nlmode;
10711 unsigned int i;
10712
10713 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10714 return -1;
10715
10716 nlmode = wpa_driver_nl80211_if_type(type);
10717
10718 /* By default, use the per-radio values */
10719 *ext_capa = drv->extended_capa;
10720 *ext_capa_mask = drv->extended_capa_mask;
10721 *ext_capa_len = drv->extended_capa_len;
10722
10723 /* Replace the default value if a per-interface type value exists */
10724 for (i = 0; i < drv->num_iface_ext_capa; i++) {
10725 if (nlmode == drv->iface_ext_capa[i].iftype) {
10726 *ext_capa = drv->iface_ext_capa[i].ext_capa;
10727 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10728 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10729 break;
10730 }
10731 }
10732
10733 return 0;
10734}
10735
10736
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010737static int nl80211_update_connection_params(
10738 void *priv, struct wpa_driver_associate_params *params,
10739 enum wpa_drv_update_connect_params_mask mask)
10740{
10741 struct i802_bss *bss = priv;
10742 struct wpa_driver_nl80211_data *drv = bss->drv;
10743 struct nl_msg *msg;
10744 int ret = -1;
10745 enum nl80211_auth_type type;
10746
10747 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10748 if (!msg)
10749 goto fail;
10750
10751 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10752 drv->ifindex);
10753
10754 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10755 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10756 params->wpa_ie))
10757 goto fail;
10758 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
10759 params->wpa_ie_len);
10760 }
10761
10762 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10763 type = get_nl_auth_type(params->auth_alg);
10764 if (type == NL80211_AUTHTYPE_MAX ||
10765 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10766 goto fail;
10767 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
10768 }
10769
10770 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10771 nl80211_put_fils_connect_params(drv, params, msg))
10772 goto fail;
10773
10774 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10775 msg = NULL;
10776 if (ret)
10777 wpa_dbg(drv->ctx, MSG_DEBUG,
10778 "nl80211: Update connect params command failed: ret=%d (%s)",
10779 ret, strerror(-ret));
10780
10781fail:
10782 nlmsg_free(msg);
10783 return ret;
10784}
10785
10786
Roshan Pius3a1667e2018-07-03 15:17:14 -070010787static int nl80211_send_external_auth_status(void *priv,
10788 struct external_auth *params)
10789{
10790 struct i802_bss *bss = priv;
10791 struct wpa_driver_nl80211_data *drv = bss->drv;
10792 struct nl_msg *msg = NULL;
10793 int ret = -1;
10794
10795 wpa_dbg(drv->ctx, MSG_DEBUG,
10796 "nl80211: External auth status: %u", params->status);
10797
10798 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10799 if (!msg ||
10800 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10801 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10802 params->ssid) ||
10803 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10804 goto fail;
10805 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10806 msg = NULL;
10807 if (ret) {
10808 wpa_printf(MSG_DEBUG,
10809 "nl80211: External Auth status update failed: ret=%d (%s)",
10810 ret, strerror(-ret));
10811 goto fail;
10812 }
10813fail:
10814 nlmsg_free(msg);
10815 return ret;
10816}
10817
10818
Hai Shalom74f70d42019-02-11 14:42:39 -080010819static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
10820 int val)
10821{
10822 struct i802_bss *bss = priv;
10823 struct wpa_driver_nl80211_data *drv = bss->drv;
10824 struct nl_msg *msg;
10825 int ret = -ENOBUFS;
10826
10827 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
10828 val ? "Enable" : "Disable", bridge_ifname);
10829
10830 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
10831 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
10832 goto fail;
10833
10834 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
10835 if (linux_br_del_if(drv->global->ioctl_sock,
10836 bridge_ifname, bss->ifname)) {
10837 wpa_printf(MSG_ERROR,
10838 "nl80211: Failed to remove interface %s from bridge %s",
10839 bss->ifname, bridge_ifname);
10840 return -1;
10841 }
10842 bss->added_if_into_bridge = 0;
10843 }
10844
10845 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10846 msg = NULL;
10847 if (!ret) {
10848 if (bridge_ifname[0] && val &&
10849 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
10850 return -1;
10851 return 0;
10852 }
10853
10854fail:
10855 nlmsg_free(msg);
10856 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
10857
10858 return ret;
10859}
10860
10861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010862const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10863 .name = "nl80211",
10864 .desc = "Linux nl80211/cfg80211",
10865 .get_bssid = wpa_driver_nl80211_get_bssid,
10866 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010867 .set_key = driver_nl80211_set_key,
10868 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010869 .sched_scan = wpa_driver_nl80211_sched_scan,
10870 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010871 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080010872 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010873 .deauthenticate = driver_nl80211_deauthenticate,
10874 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010875 .associate = wpa_driver_nl80211_associate,
10876 .global_init = nl80211_global_init,
10877 .global_deinit = nl80211_global_deinit,
10878 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010879 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010880 .get_capa = wpa_driver_nl80211_get_capa,
10881 .set_operstate = wpa_driver_nl80211_set_operstate,
10882 .set_supp_port = wpa_driver_nl80211_set_supp_port,
10883 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010884 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010885 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070010886 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010887 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010888 .if_remove = driver_nl80211_if_remove,
10889 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010890 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010891 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010892 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010893 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
10894 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010895 .hapd_init = i802_init,
10896 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070010897 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010898 .get_seqnum = i802_get_seqnum,
10899 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010900 .get_inact_sec = i802_get_inact_sec,
10901 .sta_clear_stats = i802_sta_clear_stats,
10902 .set_rts = i802_set_rts,
10903 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010904 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010905 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010906 .sta_deauth = i802_sta_deauth,
10907 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010908 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010909 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010910 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010911 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
10912 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10913 .cancel_remain_on_channel =
10914 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010915 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010916 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070010917 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010918 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010919 .signal_monitor = nl80211_signal_monitor,
10920 .signal_poll = nl80211_signal_poll,
Hai Shalom74f70d42019-02-11 14:42:39 -080010921 .channel_info = nl80211_channel_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010922 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010923 .set_param = nl80211_set_param,
10924 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070010925 .add_pmkid = nl80211_add_pmkid,
10926 .remove_pmkid = nl80211_remove_pmkid,
10927 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010928 .set_rekey_info = nl80211_set_rekey_info,
10929 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010930 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010931 .start_dfs_cac = nl80211_start_radar_detection,
10932 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010933#ifdef CONFIG_TDLS
10934 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10935 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010936 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10937 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010938#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010939 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010940 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010941 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070010942 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080010943 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010944#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010945 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010946 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010947 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010948#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070010949#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070010950#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070010951 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070010952#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010953#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080010954 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010955 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070010956 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010957 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010958#ifdef CONFIG_MESH
10959 .init_mesh = wpa_driver_nl80211_init_mesh,
10960 .join_mesh = wpa_driver_nl80211_join_mesh,
10961 .leave_mesh = wpa_driver_nl80211_leave_mesh,
10962#endif /* CONFIG_MESH */
10963 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10964 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
10965 .br_port_set_attr = wpa_driver_br_port_set_attr,
10966 .br_set_net_param = wpa_driver_br_set_net_param,
10967 .add_tx_ts = nl80211_add_ts,
10968 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070010969 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010970#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010971 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070010972 .disable_fils = nl80211_disable_fils,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010973 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -070010974 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010975 .get_pref_freq_list = nl80211_get_pref_freq_list,
10976 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010977 .p2p_lo_start = nl80211_p2p_lo_start,
10978 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070010979 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010980 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010981#ifdef CONFIG_MBO
10982 .get_bss_transition_status = nl80211_get_bss_transition_status,
10983 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
10984#endif /* CONFIG_MBO */
10985 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010986#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010987 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010988 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010989 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070010990 .send_external_auth_status = nl80211_send_external_auth_status,
Hai Shalom74f70d42019-02-11 14:42:39 -080010991 .set_4addr_mode = nl80211_set_4addr_mode,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010992};