blob: 39a02d3ee51f4620d27276b7ecce7c1d566cd5ea [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
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080043#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044/*
45 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
46 * but when you free a socket again it will mess up its bitmap and
47 * and use the wrong number the next time it needs a socket ID.
48 * Therefore, we wrap the handle alloc/destroy and add our own pid
49 * accounting.
50 */
51static uint32_t port_bitmap[32] = { 0 };
52
53static struct nl_handle *nl80211_handle_alloc(void *cb)
54{
55 struct nl_handle *handle;
56 uint32_t pid = getpid() & 0x3FFFFF;
57 int i;
58
59 handle = nl_handle_alloc_cb(cb);
60
61 for (i = 0; i < 1024; i++) {
62 if (port_bitmap[i / 32] & (1 << (i % 32)))
63 continue;
64 port_bitmap[i / 32] |= 1 << (i % 32);
65 pid += i << 22;
66 break;
67 }
68
69 nl_socket_set_local_port(handle, pid);
70
71 return handle;
72}
73
74static void nl80211_handle_destroy(struct nl_handle *handle)
75{
76 uint32_t port = nl_socket_get_local_port(handle);
77
78 port >>= 22;
79 port_bitmap[port / 32] &= ~(1 << (port % 32));
80
81 nl_handle_destroy(handle);
82}
83#endif /* CONFIG_LIBNL20 */
84
85
Dmitry Shmidt54605472013-11-08 11:10:19 -080086#ifdef ANDROID
87/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080088#undef nl_socket_set_nonblocking
89#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080090
Dmitry Shmidt54605472013-11-08 11:10:19 -080091#endif /* ANDROID */
92
93
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
95{
96 struct nl_handle *handle;
97
98 handle = nl80211_handle_alloc(cb);
99 if (handle == NULL) {
100 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
101 "callbacks (%s)", dbg);
102 return NULL;
103 }
104
105 if (genl_connect(handle)) {
106 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
107 "netlink (%s)", dbg);
108 nl80211_handle_destroy(handle);
109 return NULL;
110 }
111
112 return handle;
113}
114
115
116static void nl_destroy_handles(struct nl_handle **handle)
117{
118 if (*handle == NULL)
119 return;
120 nl80211_handle_destroy(*handle);
121 *handle = NULL;
122}
123
124
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700125#if __WORDSIZE == 64
126#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
127#else
128#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
129#endif
130
131static void nl80211_register_eloop_read(struct nl_handle **handle,
132 eloop_sock_handler handler,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700133 void *eloop_data, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700134{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800135#ifdef CONFIG_LIBNL20
136 /*
137 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
138 * by default. It is possible to hit that limit in some cases where
139 * operations are blocked, e.g., with a burst of Deauthentication frames
140 * to hostapd and STA entry deletion. Try to increase the buffer to make
141 * this less likely to occur.
142 */
143 if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
144 wpa_printf(MSG_DEBUG,
145 "nl80211: Could not set nl_socket RX buffer size: %s",
146 strerror(errno));
147 /* continue anyway with the default (smaller) buffer */
148 }
149#endif /* CONFIG_LIBNL20 */
150
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700151 nl_socket_set_nonblocking(*handle);
152 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
153 eloop_data, *handle);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700154 if (!persist)
155 *handle = (void *) (((intptr_t) *handle) ^
156 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700157}
158
159
Roshan Pius3a1667e2018-07-03 15:17:14 -0700160static void nl80211_destroy_eloop_handle(struct nl_handle **handle, int persist)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700161{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700162 if (!persist)
163 *handle = (void *) (((intptr_t) *handle) ^
164 ELOOP_SOCKET_INVALID);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700165 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
166 nl_destroy_handles(handle);
167}
168
169
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800171static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800172
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800173static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700174static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
175 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800178wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800179 const u8 *set_addr, int first,
180 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800183 const u8 *buf, size_t buf_len, u64 *cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800184 int no_cck, int no_ack, int offchanok,
185 const u16 *csa_offs, size_t csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800186static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
187 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800189#define IFIDX_ANY -1
190
191static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
192 int ifidx_reason);
193static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
194 int ifidx_reason);
195static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
196 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700197
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700198static int nl80211_set_channel(struct i802_bss *bss,
199 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
201 int ifindex, int disabled);
202
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800203static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
204 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800205
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700206static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800207static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700208#ifdef CONFIG_MESH
209static int nl80211_put_mesh_config(struct nl_msg *msg,
210 struct wpa_driver_mesh_bss_params *params);
211#endif /* CONFIG_MESH */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800212static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
213 int reason);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700214
215
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800216/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800217enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800218{
219 switch (width) {
220 case NL80211_CHAN_WIDTH_20_NOHT:
221 return CHAN_WIDTH_20_NOHT;
222 case NL80211_CHAN_WIDTH_20:
223 return CHAN_WIDTH_20;
224 case NL80211_CHAN_WIDTH_40:
225 return CHAN_WIDTH_40;
226 case NL80211_CHAN_WIDTH_80:
227 return CHAN_WIDTH_80;
228 case NL80211_CHAN_WIDTH_80P80:
229 return CHAN_WIDTH_80P80;
230 case NL80211_CHAN_WIDTH_160:
231 return CHAN_WIDTH_160;
232 }
233 return CHAN_WIDTH_UNKNOWN;
234}
235
236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800237int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700239 return nlmode == NL80211_IFTYPE_AP ||
240 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241}
242
243
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800244int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700246 return nlmode == NL80211_IFTYPE_STATION ||
247 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248}
249
250
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700251static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800252{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700253 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
254 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800255}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256
257
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800258struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
259 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700260{
261 struct i802_bss *bss;
262
263 for (bss = drv->first_bss; bss; bss = bss->next) {
264 if (bss->ifindex == ifindex)
265 return bss;
266 }
267
268 return NULL;
269}
270
271
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800272static int is_mesh_interface(enum nl80211_iftype nlmode)
273{
274 return nlmode == NL80211_IFTYPE_MESH_POINT;
275}
276
277
278void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700279{
280 if (drv->associated)
281 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
282 drv->associated = 0;
283 os_memset(drv->bssid, 0, ETH_ALEN);
284}
285
286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287/* nl80211 code */
288static int ack_handler(struct nl_msg *msg, void *arg)
289{
290 int *err = arg;
291 *err = 0;
292 return NL_STOP;
293}
294
295static int finish_handler(struct nl_msg *msg, void *arg)
296{
297 int *ret = arg;
298 *ret = 0;
299 return NL_SKIP;
300}
301
302static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
303 void *arg)
304{
305 int *ret = arg;
306 *ret = err->error;
307 return NL_SKIP;
308}
309
310
311static int no_seq_check(struct nl_msg *msg, void *arg)
312{
313 return NL_OK;
314}
315
316
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317static void nl80211_nlmsg_clear(struct nl_msg *msg)
318{
319 /*
320 * Clear nlmsg data, e.g., to make sure key material is not left in
321 * heap memory for unnecessarily long time.
322 */
323 if (msg) {
324 struct nlmsghdr *hdr = nlmsg_hdr(msg);
325 void *data = nlmsg_data(hdr);
326 /*
327 * This would use nlmsg_datalen() or the older nlmsg_len() if
328 * only libnl were to maintain a stable API.. Neither will work
329 * with all released versions, so just calculate the length
330 * here.
331 */
332 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
333
334 os_memset(data, 0, len);
335 }
336}
337
338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800339static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 struct nl_handle *nl_handle, struct nl_msg *msg,
341 int (*valid_handler)(struct nl_msg *, void *),
342 void *valid_data)
343{
344 struct nl_cb *cb;
345 int err = -ENOMEM;
346
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800347 if (!msg)
348 return -ENOMEM;
349
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800350 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 if (!cb)
352 goto out;
353
354 err = nl_send_auto_complete(nl_handle, msg);
355 if (err < 0)
356 goto out;
357
358 err = 1;
359
360 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
361 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
362 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
363
364 if (valid_handler)
365 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
366 valid_handler, valid_data);
367
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700368 while (err > 0) {
369 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700370 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700371 wpa_printf(MSG_INFO,
372 "nl80211: %s->nl_recvmsgs failed: %d",
373 __func__, res);
374 }
375 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 out:
377 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800378 if (!valid_handler && valid_data == (void *) -1)
379 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 nlmsg_free(msg);
381 return err;
382}
383
384
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800385int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
386 struct nl_msg *msg,
387 int (*valid_handler)(struct nl_msg *, void *),
388 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800390 return send_and_recv(drv->global, drv->global->nl, msg,
391 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392}
393
394
395struct family_data {
396 const char *group;
397 int id;
398};
399
400
401static int family_handler(struct nl_msg *msg, void *arg)
402{
403 struct family_data *res = arg;
404 struct nlattr *tb[CTRL_ATTR_MAX + 1];
405 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
406 struct nlattr *mcgrp;
407 int i;
408
409 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
410 genlmsg_attrlen(gnlh, 0), NULL);
411 if (!tb[CTRL_ATTR_MCAST_GROUPS])
412 return NL_SKIP;
413
414 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
415 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
416 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
417 nla_len(mcgrp), NULL);
418 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
419 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
420 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
421 res->group,
422 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
423 continue;
424 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
425 break;
426 };
427
428 return NL_SKIP;
429}
430
431
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800432static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 const char *family, const char *group)
434{
435 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800436 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 struct family_data res = { group, -ENOENT };
438
439 msg = nlmsg_alloc();
440 if (!msg)
441 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800442 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
443 0, 0, CTRL_CMD_GETFAMILY, 0) ||
444 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
445 nlmsg_free(msg);
446 return -1;
447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800449 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 if (ret == 0)
451 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 return ret;
453}
454
455
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800456void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
457 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800458{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700459 if (TEST_FAIL())
460 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
462 0, flags, cmd, 0);
463}
464
465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800466static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
467{
468 if (bss->wdev_id_set)
469 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
470 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
471}
472
473
474struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
475{
476 struct nl_msg *msg;
477
478 msg = nlmsg_alloc();
479 if (!msg)
480 return NULL;
481
482 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
483 nl80211_set_iface_id(msg, bss) < 0) {
484 nlmsg_free(msg);
485 return NULL;
486 }
487
488 return msg;
489}
490
491
492static struct nl_msg *
493nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
494 int flags, uint8_t cmd)
495{
496 struct nl_msg *msg;
497
498 msg = nlmsg_alloc();
499 if (!msg)
500 return NULL;
501
502 if (!nl80211_cmd(drv, msg, flags, cmd) ||
503 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
504 nlmsg_free(msg);
505 return NULL;
506 }
507
508 return msg;
509}
510
511
512struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
513 uint8_t cmd)
514{
515 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
516}
517
518
519struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
520{
521 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
522}
523
524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800525struct wiphy_idx_data {
526 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700527 enum nl80211_iftype nlmode;
528 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800529};
530
531
532static int netdev_info_handler(struct nl_msg *msg, void *arg)
533{
534 struct nlattr *tb[NL80211_ATTR_MAX + 1];
535 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
536 struct wiphy_idx_data *info = arg;
537
538 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
539 genlmsg_attrlen(gnlh, 0), NULL);
540
541 if (tb[NL80211_ATTR_WIPHY])
542 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
543
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700544 if (tb[NL80211_ATTR_IFTYPE])
545 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
546
547 if (tb[NL80211_ATTR_MAC] && info->macaddr)
548 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
549 ETH_ALEN);
550
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800551 return NL_SKIP;
552}
553
554
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800555int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800556{
557 struct nl_msg *msg;
558 struct wiphy_idx_data data = {
559 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700560 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800561 };
562
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800563 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
564 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800565
566 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
567 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800568 return -1;
569}
570
571
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700572static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
573{
574 struct nl_msg *msg;
575 struct wiphy_idx_data data = {
576 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
577 .macaddr = NULL,
578 };
579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800580 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
581 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700582
583 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
584 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700585 return NL80211_IFTYPE_UNSPECIFIED;
586}
587
588
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700589static int nl80211_get_macaddr(struct i802_bss *bss)
590{
591 struct nl_msg *msg;
592 struct wiphy_idx_data data = {
593 .macaddr = bss->addr,
594 };
595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800596 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
597 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700598
599 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700600}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700601
602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
604 struct nl80211_wiphy_data *w)
605{
606 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800607 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800608
609 msg = nlmsg_alloc();
610 if (!msg)
611 return -1;
612
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800613 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
614 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
615 nlmsg_free(msg);
616 return -1;
617 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800618
619 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 if (ret) {
621 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
622 "failed: ret=%d (%s)",
623 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800625 return ret;
626}
627
628
629static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
630{
631 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700632 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800633
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800634 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700636 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700637 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700638 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
639 __func__, res);
640 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641}
642
643
644static int process_beacon_event(struct nl_msg *msg, void *arg)
645{
646 struct nl80211_wiphy_data *w = arg;
647 struct wpa_driver_nl80211_data *drv;
648 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
649 struct nlattr *tb[NL80211_ATTR_MAX + 1];
650 union wpa_event_data event;
651
652 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
653 genlmsg_attrlen(gnlh, 0), NULL);
654
655 if (gnlh->cmd != NL80211_CMD_FRAME) {
656 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
657 gnlh->cmd);
658 return NL_SKIP;
659 }
660
661 if (!tb[NL80211_ATTR_FRAME])
662 return NL_SKIP;
663
664 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
665 wiphy_list) {
666 os_memset(&event, 0, sizeof(event));
667 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
668 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
669 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
670 }
671
672 return NL_SKIP;
673}
674
675
676static struct nl80211_wiphy_data *
677nl80211_get_wiphy_data_ap(struct i802_bss *bss)
678{
679 static DEFINE_DL_LIST(nl80211_wiphys);
680 struct nl80211_wiphy_data *w;
681 int wiphy_idx, found = 0;
682 struct i802_bss *tmp_bss;
Paul Stewart092955c2017-02-06 09:13:09 -0800683 u8 channel;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800684
685 if (bss->wiphy_data != NULL)
686 return bss->wiphy_data;
687
688 wiphy_idx = nl80211_get_wiphy_index(bss);
689
690 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
691 if (w->wiphy_idx == wiphy_idx)
692 goto add;
693 }
694
695 /* alloc new one */
696 w = os_zalloc(sizeof(*w));
697 if (w == NULL)
698 return NULL;
699 w->wiphy_idx = wiphy_idx;
700 dl_list_init(&w->bsss);
701 dl_list_init(&w->drvs);
702
Paul Stewart092955c2017-02-06 09:13:09 -0800703 /* Beacon frames not supported in IEEE 802.11ad */
704 if (ieee80211_freq_to_chan(bss->freq, &channel) !=
705 HOSTAPD_MODE_IEEE80211AD) {
706 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
707 if (!w->nl_cb) {
708 os_free(w);
709 return NULL;
710 }
711 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
712 no_seq_check, NULL);
713 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
714 process_beacon_event, w);
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000715
Paul Stewart092955c2017-02-06 09:13:09 -0800716 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
717 "wiphy beacons");
718 if (w->nl_beacons == NULL) {
719 os_free(w);
720 return NULL;
721 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000722
Paul Stewart092955c2017-02-06 09:13:09 -0800723 if (nl80211_register_beacons(bss->drv, w)) {
724 nl_destroy_handles(&w->nl_beacons);
725 os_free(w);
726 return NULL;
727 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000728
Paul Stewart092955c2017-02-06 09:13:09 -0800729 nl80211_register_eloop_read(&w->nl_beacons,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700730 nl80211_recv_beacons, w, 0);
Paul Stewart092955c2017-02-06 09:13:09 -0800731 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732
733 dl_list_add(&nl80211_wiphys, &w->list);
734
735add:
736 /* drv entry for this bss already there? */
737 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
738 if (tmp_bss->drv == bss->drv) {
739 found = 1;
740 break;
741 }
742 }
743 /* if not add it */
744 if (!found)
745 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
746
747 dl_list_add(&w->bsss, &bss->wiphy_list);
748 bss->wiphy_data = w;
749 return w;
750}
751
752
753static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
754{
755 struct nl80211_wiphy_data *w = bss->wiphy_data;
756 struct i802_bss *tmp_bss;
757 int found = 0;
758
759 if (w == NULL)
760 return;
761 bss->wiphy_data = NULL;
762 dl_list_del(&bss->wiphy_list);
763
764 /* still any for this drv present? */
765 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
766 if (tmp_bss->drv == bss->drv) {
767 found = 1;
768 break;
769 }
770 }
771 /* if not remove it */
772 if (!found)
773 dl_list_del(&bss->drv->wiphy_list);
774
775 if (!dl_list_empty(&w->bsss))
776 return;
777
Paul Stewart092955c2017-02-06 09:13:09 -0800778 if (w->nl_beacons)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700779 nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800780
781 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 dl_list_del(&w->list);
783 os_free(w);
784}
785
786
Dmitry Shmidte4663042016-04-04 10:07:49 -0700787static unsigned int nl80211_get_ifindex(void *priv)
788{
789 struct i802_bss *bss = priv;
790 struct wpa_driver_nl80211_data *drv = bss->drv;
791
792 return drv->ifindex;
793}
794
795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
797{
798 struct i802_bss *bss = priv;
799 struct wpa_driver_nl80211_data *drv = bss->drv;
800 if (!drv->associated)
801 return -1;
802 os_memcpy(bssid, drv->bssid, ETH_ALEN);
803 return 0;
804}
805
806
807static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
808{
809 struct i802_bss *bss = priv;
810 struct wpa_driver_nl80211_data *drv = bss->drv;
811 if (!drv->associated)
812 return -1;
813 os_memcpy(ssid, drv->ssid, drv->ssid_len);
814 return drv->ssid_len;
815}
816
817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800818static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700819 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
820 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821{
822 union wpa_event_data event;
823
Dmitry Shmidte4663042016-04-04 10:07:49 -0700824 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800825 if (if_nametoindex(drv->first_bss->ifname) == 0) {
826 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
827 drv->first_bss->ifname);
828 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700829 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800830 if (!drv->if_removed)
831 return;
832 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
833 drv->first_bss->ifname);
834 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 }
836
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800837 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700838 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800839 os_strlcpy(event.interface_status.ifname, ifname,
840 sizeof(event.interface_status.ifname));
841 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700842 if (drv)
843 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
844 else
845 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
846 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800847}
848
849
850static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700851 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
852 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800853{
854 union wpa_event_data event;
855
Dmitry Shmidte4663042016-04-04 10:07:49 -0700856 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800857 if (drv->if_removed) {
858 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
859 ifname);
860 return;
861 }
862 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
863 ifname);
864 drv->if_removed = 1;
865 } else {
866 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
867 ifname);
868 }
869
870 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700871 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800872 os_strlcpy(event.interface_status.ifname, ifname,
873 sizeof(event.interface_status.ifname));
874 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700875 if (drv)
876 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
877 else
878 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
879 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880}
881
882
883static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
884 u8 *buf, size_t len)
885{
886 int attrlen, rta_len;
887 struct rtattr *attr;
888
889 attrlen = len;
890 attr = (struct rtattr *) buf;
891
892 rta_len = RTA_ALIGN(sizeof(struct rtattr));
893 while (RTA_OK(attr, attrlen)) {
894 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800895 if (os_strcmp(((char *) attr) + rta_len,
896 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 return 1;
898 else
899 break;
900 }
901 attr = RTA_NEXT(attr, attrlen);
902 }
903
904 return 0;
905}
906
907
908static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
909 int ifindex, u8 *buf, size_t len)
910{
911 if (drv->ifindex == ifindex)
912 return 1;
913
914 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800915 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
917 "interface");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700918 if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
919 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 return 1;
921 }
922
923 return 0;
924}
925
926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800927static struct wpa_driver_nl80211_data *
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700928nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
929 int *init_failed)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800930{
931 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700932 int res;
933
934 if (init_failed)
935 *init_failed = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936 dl_list_for_each(drv, &global->interfaces,
937 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700938 res = wpa_driver_nl80211_own_ifindex(drv, idx, buf, len);
939 if (res < 0) {
940 wpa_printf(MSG_DEBUG,
941 "nl80211: Found matching own interface, but failed to complete reinitialization");
942 if (init_failed)
943 *init_failed = 1;
944 return drv;
945 }
946 if (res > 0 || have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800947 return drv;
948 }
949 return NULL;
950}
951
952
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700953static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700954 int ifindex, int notify)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700955{
956 struct i802_bss *bss;
957 u8 addr[ETH_ALEN];
958
959 bss = get_bss_ifindex(drv, ifindex);
960 if (bss &&
961 linux_get_ifhwaddr(drv->global->ioctl_sock,
962 bss->ifname, addr) < 0) {
963 wpa_printf(MSG_DEBUG,
964 "nl80211: %s: failed to re-read MAC address",
965 bss->ifname);
966 } else if (bss && os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
967 wpa_printf(MSG_DEBUG,
968 "nl80211: Own MAC address on ifindex %d (%s) changed from "
969 MACSTR " to " MACSTR,
970 ifindex, bss->ifname,
971 MAC2STR(bss->addr), MAC2STR(addr));
972 os_memcpy(bss->addr, addr, ETH_ALEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700973 if (notify)
974 wpa_supplicant_event(drv->ctx,
975 EVENT_INTERFACE_MAC_CHANGED, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700976 }
977}
978
979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
981 struct ifinfomsg *ifi,
982 u8 *buf, size_t len)
983{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800984 struct nl80211_global *global = ctx;
985 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800986 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 struct rtattr *attr;
988 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800990 char ifname[IFNAMSIZ + 1];
991 char extra[100], *pos, *end;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700992 int init_failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800994 extra[0] = '\0';
995 pos = extra;
996 end = pos + sizeof(extra);
997 ifname[0] = '\0';
998
999 attrlen = len;
1000 attr = (struct rtattr *) buf;
1001 while (RTA_OK(attr, attrlen)) {
1002 switch (attr->rta_type) {
1003 case IFLA_IFNAME:
1004 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1005 break;
1006 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1007 ifname[RTA_PAYLOAD(attr)] = '\0';
1008 break;
1009 case IFLA_MASTER:
1010 brid = nla_get_u32((struct nlattr *) attr);
1011 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1012 break;
1013 case IFLA_WIRELESS:
1014 pos += os_snprintf(pos, end - pos, " wext");
1015 break;
1016 case IFLA_OPERSTATE:
1017 pos += os_snprintf(pos, end - pos, " operstate=%u",
1018 nla_get_u32((struct nlattr *) attr));
1019 break;
1020 case IFLA_LINKMODE:
1021 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1022 nla_get_u32((struct nlattr *) attr));
1023 break;
1024 }
1025 attr = RTA_NEXT(attr, attrlen);
1026 }
1027 extra[sizeof(extra) - 1] = '\0';
1028
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001029 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1030 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1031 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1033 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1034 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1035 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1036
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001037 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
Dmitry Shmidte4663042016-04-04 10:07:49 -07001038 if (!drv)
1039 goto event_newlink;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001040 if (init_failed)
1041 return; /* do not update interface state */
Dmitry Shmidte4663042016-04-04 10:07:49 -07001042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001044 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001045 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001046 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1048 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001049 drv->ignore_if_down_event = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001050 /* Re-read MAC address as it may have changed */
1051 nl80211_refresh_mac(drv, ifi->ifi_index, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001052 return;
1053 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001054 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1055 namebuf, ifname);
1056 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1057 wpa_printf(MSG_DEBUG,
1058 "nl80211: Not the main interface (%s) - do not indicate interface down",
1059 drv->first_bss->ifname);
1060 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1062 "event generated by mode change");
1063 drv->ignore_if_down_event = 0;
1064 } else {
1065 drv->if_disabled = 1;
1066 wpa_supplicant_event(drv->ctx,
1067 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001068
1069 /*
1070 * Try to get drv again, since it may be removed as
1071 * part of the EVENT_INTERFACE_DISABLED handling for
1072 * dynamic interfaces
1073 */
1074 drv = nl80211_find_drv(global, ifi->ifi_index,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001075 buf, len, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001076 if (!drv)
1077 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 }
1080
1081 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07001082 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001083 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001084 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1086 "event since interface %s is down",
1087 namebuf);
Hai Shalomc9e41a12018-07-31 14:41:42 -07001088 return;
1089 }
1090 wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
1091 namebuf, ifname);
1092 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1093 wpa_printf(MSG_DEBUG,
1094 "nl80211: Not the main interface (%s) - do not indicate interface up",
1095 drv->first_bss->ifname);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001096 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001097 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1098 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001099 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001100 } else if (drv->if_removed) {
1101 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1102 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001103 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001104 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001105 /* Re-read MAC address as it may have changed */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001106 nl80211_refresh_mac(drv, ifi->ifi_index, 0);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001108 drv->if_disabled = 0;
1109 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1110 NULL);
1111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 }
1113
1114 /*
1115 * Some drivers send the association event before the operup event--in
1116 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1117 * fails. This will hit us when wpa_supplicant does not need to do
1118 * IEEE 802.1X authentication
1119 */
1120 if (drv->operstate == 1 &&
1121 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001122 !(ifi->ifi_flags & IFF_RUNNING)) {
1123 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001124 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 }
1127
Dmitry Shmidte4663042016-04-04 10:07:49 -07001128event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001129 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001130 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1131 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001132
Dmitry Shmidte4663042016-04-04 10:07:49 -07001133 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001134 struct i802_bss *bss;
1135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001137 if (!if_indextoname(brid, namebuf)) {
1138 wpa_printf(MSG_DEBUG,
1139 "nl80211: Could not find bridge ifname for ifindex %u",
1140 brid);
1141 return;
1142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1144 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001145 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001146
1147 for (bss = drv->first_bss; bss; bss = bss->next) {
1148 if (os_strcmp(ifname, bss->ifname) == 0) {
1149 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1150 break;
1151 }
1152 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154}
1155
1156
1157static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1158 struct ifinfomsg *ifi,
1159 u8 *buf, size_t len)
1160{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001161 struct nl80211_global *global = ctx;
1162 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001163 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 struct rtattr *attr;
1165 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001166 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001167 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001169 extra[0] = '\0';
1170 pos = extra;
1171 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001172 ifname[0] = '\0';
1173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 attrlen = len;
1175 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001177 switch (attr->rta_type) {
1178 case IFLA_IFNAME:
1179 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1180 break;
1181 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1182 ifname[RTA_PAYLOAD(attr)] = '\0';
1183 break;
1184 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001186 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1187 break;
1188 case IFLA_OPERSTATE:
1189 pos += os_snprintf(pos, end - pos, " operstate=%u",
1190 nla_get_u32((struct nlattr *) attr));
1191 break;
1192 case IFLA_LINKMODE:
1193 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1194 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001195 break;
1196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 attr = RTA_NEXT(attr, attrlen);
1198 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001199 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001201 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1202 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1203 ifi->ifi_flags,
1204 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1205 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1206 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1207 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1208
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001209 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001210
Dmitry Shmidte4663042016-04-04 10:07:49 -07001211 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 /* device has been removed from bridge */
1213 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001214
1215 if (!if_indextoname(brid, namebuf)) {
1216 wpa_printf(MSG_DEBUG,
1217 "nl80211: Could not find bridge ifname for ifindex %u",
1218 brid);
1219 } else {
1220 wpa_printf(MSG_DEBUG,
1221 "nl80211: Remove ifindex %u for bridge %s",
1222 brid, namebuf);
1223 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001224 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001226
1227 if (ifi->ifi_family != AF_BRIDGE || !brid)
1228 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1229 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230}
1231
1232
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001233struct nl80211_get_assoc_freq_arg {
1234 struct wpa_driver_nl80211_data *drv;
1235 unsigned int assoc_freq;
1236 unsigned int ibss_freq;
1237 u8 assoc_bssid[ETH_ALEN];
1238 u8 assoc_ssid[SSID_MAX_LEN];
1239 u8 assoc_ssid_len;
1240};
1241
1242static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1243{
1244 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1245 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1246 struct nlattr *bss[NL80211_BSS_MAX + 1];
1247 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1248 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1249 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1250 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1251 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1252 };
1253 struct nl80211_get_assoc_freq_arg *ctx = arg;
1254 enum nl80211_bss_status status;
1255
1256 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1257 genlmsg_attrlen(gnlh, 0), NULL);
1258 if (!tb[NL80211_ATTR_BSS] ||
1259 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1260 bss_policy) ||
1261 !bss[NL80211_BSS_STATUS])
1262 return NL_SKIP;
1263
1264 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1265 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1266 bss[NL80211_BSS_FREQUENCY]) {
1267 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1268 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1269 ctx->assoc_freq);
1270 }
1271 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1272 bss[NL80211_BSS_FREQUENCY]) {
1273 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1274 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1275 ctx->ibss_freq);
1276 }
1277 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1278 bss[NL80211_BSS_BSSID]) {
1279 os_memcpy(ctx->assoc_bssid,
1280 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1281 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1282 MACSTR, MAC2STR(ctx->assoc_bssid));
1283 }
1284
1285 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1286 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1287 const u8 *ie, *ssid;
1288 size_t ie_len;
1289
1290 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1291 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1292 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1293 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1294 ctx->assoc_ssid_len = ssid[1];
1295 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1296 }
1297 }
1298
1299 return NL_SKIP;
1300}
1301
1302
1303int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001304{
1305 struct nl_msg *msg;
1306 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001307 struct nl80211_get_assoc_freq_arg arg;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001308
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001309 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001310 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001311 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001312 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1313 &arg);
1314 if (ret == 0) {
1315 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1316 return arg.assoc_ssid_len;
1317 }
1318 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1319 ret, strerror(-ret));
1320 return ret;
1321}
1322
1323
1324unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1325{
1326 struct nl_msg *msg;
1327 int ret;
1328 struct nl80211_get_assoc_freq_arg arg;
1329
1330 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1331 os_memset(&arg, 0, sizeof(arg));
1332 arg.drv = drv;
1333 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1334 &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001335 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001336 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1337 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001338 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001339 "associated BSS from scan results: %u MHz", freq);
1340 if (freq)
1341 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001342 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001343 }
1344 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1345 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001346 return drv->assoc_freq;
1347}
1348
1349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350static int get_link_signal(struct nl_msg *msg, void *arg)
1351{
1352 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1353 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1354 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1355 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1356 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001357 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001358 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 };
1360 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1361 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1362 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1363 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1364 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1365 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1366 };
1367 struct wpa_signal_info *sig_change = arg;
1368
1369 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1370 genlmsg_attrlen(gnlh, 0), NULL);
1371 if (!tb[NL80211_ATTR_STA_INFO] ||
1372 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1373 tb[NL80211_ATTR_STA_INFO], policy))
1374 return NL_SKIP;
1375 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1376 return NL_SKIP;
1377
1378 sig_change->current_signal =
1379 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1380
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001381 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1382 sig_change->avg_signal =
1383 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1384 else
1385 sig_change->avg_signal = 0;
1386
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001387 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1388 sig_change->avg_beacon_signal =
1389 (s8)
1390 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1391 else
1392 sig_change->avg_beacon_signal = 0;
1393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1395 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1396 sinfo[NL80211_STA_INFO_TX_BITRATE],
1397 rate_policy)) {
1398 sig_change->current_txrate = 0;
1399 } else {
1400 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1401 sig_change->current_txrate =
1402 nla_get_u16(rinfo[
1403 NL80211_RATE_INFO_BITRATE]) * 100;
1404 }
1405 }
1406 }
1407
1408 return NL_SKIP;
1409}
1410
1411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001412int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1413 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414{
1415 struct nl_msg *msg;
1416
1417 sig->current_signal = -9999;
1418 sig->current_txrate = 0;
1419
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001420 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1421 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1422 nlmsg_free(msg);
1423 return -ENOBUFS;
1424 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425
1426 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427}
1428
1429
1430static int get_link_noise(struct nl_msg *msg, void *arg)
1431{
1432 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1433 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1434 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1435 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1436 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1437 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1438 };
1439 struct wpa_signal_info *sig_change = arg;
1440
1441 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1442 genlmsg_attrlen(gnlh, 0), NULL);
1443
1444 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1445 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1446 return NL_SKIP;
1447 }
1448
1449 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1450 tb[NL80211_ATTR_SURVEY_INFO],
1451 survey_policy)) {
1452 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1453 "attributes!");
1454 return NL_SKIP;
1455 }
1456
1457 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1458 return NL_SKIP;
1459
1460 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1461 sig_change->frequency)
1462 return NL_SKIP;
1463
1464 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1465 return NL_SKIP;
1466
1467 sig_change->current_noise =
1468 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1469
1470 return NL_SKIP;
1471}
1472
1473
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001474int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1475 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476{
1477 struct nl_msg *msg;
1478
1479 sig_change->current_noise = 9999;
1480 sig_change->frequency = drv->assoc_freq;
1481
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001482 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484}
1485
1486
1487static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1488 void *handle)
1489{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001490 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001491 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001493 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001495 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001496 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001497 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1498 __func__, res);
1499 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500}
1501
1502
1503/**
1504 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1505 * @priv: driver_nl80211 private data
1506 * @alpha2_arg: country to which to switch to
1507 * Returns: 0 on success, -1 on failure
1508 *
1509 * This asks nl80211 to set the regulatory domain for given
1510 * country ISO / IEC alpha2.
1511 */
1512static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1513{
1514 struct i802_bss *bss = priv;
1515 struct wpa_driver_nl80211_data *drv = bss->drv;
1516 char alpha2[3];
1517 struct nl_msg *msg;
1518
1519 msg = nlmsg_alloc();
1520 if (!msg)
1521 return -ENOMEM;
1522
1523 alpha2[0] = alpha2_arg[0];
1524 alpha2[1] = alpha2_arg[1];
1525 alpha2[2] = '\0';
1526
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001527 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1528 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1529 nlmsg_free(msg);
1530 return -EINVAL;
1531 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1533 return -EINVAL;
1534 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535}
1536
1537
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001538static int nl80211_get_country(struct nl_msg *msg, void *arg)
1539{
1540 char *alpha2 = arg;
1541 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1542 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1543
1544 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1545 genlmsg_attrlen(gnlh, 0), NULL);
1546 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1547 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1548 return NL_SKIP;
1549 }
1550 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1551 return NL_SKIP;
1552}
1553
1554
1555static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1556{
1557 struct i802_bss *bss = priv;
1558 struct wpa_driver_nl80211_data *drv = bss->drv;
1559 struct nl_msg *msg;
1560 int ret;
1561
1562 msg = nlmsg_alloc();
1563 if (!msg)
1564 return -ENOMEM;
1565
1566 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1567 alpha2[0] = '\0';
1568 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1569 if (!alpha2[0])
1570 ret = -1;
1571
1572 return ret;
1573}
1574
1575
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001576static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577{
1578 int ret;
1579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001580 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1581 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1583 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 }
1586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001587 global->nl = nl_create_handle(global->nl_cb, "nl");
1588 if (global->nl == NULL)
1589 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001591 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1592 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1594 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001595 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596 }
1597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001598 global->nl_event = nl_create_handle(global->nl_cb, "event");
1599 if (global->nl_event == NULL)
1600 goto err;
1601
1602 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605 if (ret < 0) {
1606 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1607 "membership for scan events: %d (%s)",
1608 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 }
1611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001614 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 if (ret < 0) {
1616 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1617 "membership for mlme events: %d (%s)",
1618 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001619 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 }
1621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001624 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 if (ret < 0) {
1626 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1627 "membership for regulatory events: %d (%s)",
1628 ret, strerror(-ret));
1629 /* Continue without regulatory events */
1630 }
1631
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001632 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1633 if (ret >= 0)
1634 ret = nl_socket_add_membership(global->nl_event, ret);
1635 if (ret < 0) {
1636 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1637 "membership for vendor events: %d (%s)",
1638 ret, strerror(-ret));
1639 /* Continue without vendor events */
1640 }
1641
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001642 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1643 no_seq_check, NULL);
1644 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1645 process_global_event, global);
1646
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001647 nl80211_register_eloop_read(&global->nl_event,
1648 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001649 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650
1651 return 0;
1652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001653err:
1654 nl_destroy_handles(&global->nl_event);
1655 nl_destroy_handles(&global->nl);
1656 nl_cb_put(global->nl_cb);
1657 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 return -1;
1659}
1660
1661
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001662static void nl80211_check_global(struct nl80211_global *global)
1663{
1664 struct nl_handle *handle;
1665 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1666 int ret;
1667 unsigned int i;
1668
1669 /*
1670 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1671 * and all registration having been cleared.
1672 */
1673 handle = (void *) (((intptr_t) global->nl_event) ^
1674 ELOOP_SOCKET_INVALID);
1675
1676 for (i = 0; groups[i]; i++) {
1677 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1678 if (ret >= 0)
1679 ret = nl_socket_add_membership(handle, ret);
1680 if (ret < 0) {
1681 wpa_printf(MSG_INFO,
1682 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1683 groups[i], ret, strerror(-ret));
1684 }
1685 }
1686}
1687
1688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1690{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001691 struct wpa_driver_nl80211_data *drv = ctx;
1692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001696 * rtnetlink ifdown handler will report interfaces other than the P2P
1697 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001699 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1700 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701}
1702
1703
1704static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1705{
1706 struct wpa_driver_nl80211_data *drv = ctx;
1707 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001708 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1710 "after rfkill unblock");
1711 return;
1712 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001713
1714 if (is_p2p_net_interface(drv->nlmode))
1715 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1716
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001717 /*
1718 * rtnetlink ifup handler will report interfaces other than the P2P
1719 * Device interface as enabled.
1720 */
1721 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1722 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723}
1724
1725
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001726static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1727 void *eloop_ctx,
1728 void *handle)
1729{
1730 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1731 u8 data[2048];
1732 struct msghdr msg;
1733 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001734 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001735 struct cmsghdr *cmsg;
1736 int res, found_ee = 0, found_wifi = 0, acked = 0;
1737 union wpa_event_data event;
1738
1739 memset(&msg, 0, sizeof(msg));
1740 msg.msg_iov = &entry;
1741 msg.msg_iovlen = 1;
1742 entry.iov_base = data;
1743 entry.iov_len = sizeof(data);
1744 msg.msg_control = &control;
1745 msg.msg_controllen = sizeof(control);
1746
1747 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1748 /* if error or not fitting 802.3 header, return */
1749 if (res < 14)
1750 return;
1751
1752 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1753 {
1754 if (cmsg->cmsg_level == SOL_SOCKET &&
1755 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1756 int *ack;
1757
1758 found_wifi = 1;
1759 ack = (void *)CMSG_DATA(cmsg);
1760 acked = *ack;
1761 }
1762
1763 if (cmsg->cmsg_level == SOL_PACKET &&
1764 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1765 struct sock_extended_err *err =
1766 (struct sock_extended_err *)CMSG_DATA(cmsg);
1767
1768 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1769 found_ee = 1;
1770 }
1771 }
1772
1773 if (!found_ee || !found_wifi)
1774 return;
1775
1776 memset(&event, 0, sizeof(event));
1777 event.eapol_tx_status.dst = data;
1778 event.eapol_tx_status.data = data + 14;
1779 event.eapol_tx_status.data_len = res - 14;
1780 event.eapol_tx_status.ack = acked;
1781 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1782}
1783
1784
1785static int nl80211_init_bss(struct i802_bss *bss)
1786{
1787 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1788 if (!bss->nl_cb)
1789 return -1;
1790
1791 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1792 no_seq_check, NULL);
1793 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1794 process_bss_event, bss);
1795
1796 return 0;
1797}
1798
1799
1800static void nl80211_destroy_bss(struct i802_bss *bss)
1801{
1802 nl_cb_put(bss->nl_cb);
1803 bss->nl_cb = NULL;
1804}
1805
1806
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001807static void
1808wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1809{
1810 struct rfkill_config *rcfg;
1811
1812 if (drv->rfkill)
1813 return;
1814
1815 rcfg = os_zalloc(sizeof(*rcfg));
1816 if (!rcfg)
1817 return;
1818
1819 rcfg->ctx = drv;
1820
1821 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1822 * not associated with a netdev, so use the name of some other interface
1823 * sharing the same wiphy as the P2P Device interface.
1824 *
1825 * Note: This is valid, as a P2P Device interface is always dynamically
1826 * created and is created only once another wpa_s interface was added.
1827 */
1828 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1829 struct nl80211_global *global = drv->global;
1830 struct wpa_driver_nl80211_data *tmp1;
1831
1832 dl_list_for_each(tmp1, &global->interfaces,
1833 struct wpa_driver_nl80211_data, list) {
1834 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1835 !tmp1->rfkill)
1836 continue;
1837
1838 wpa_printf(MSG_DEBUG,
1839 "nl80211: Use (%s) to initialize P2P Device rfkill",
1840 tmp1->first_bss->ifname);
1841 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1842 sizeof(rcfg->ifname));
1843 break;
1844 }
1845 } else {
1846 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1847 sizeof(rcfg->ifname));
1848 }
1849
1850 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1851 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1852 drv->rfkill = rfkill_init(rcfg);
1853 if (!drv->rfkill) {
1854 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1855 os_free(rcfg);
1856 }
1857}
1858
1859
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001860static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1861 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001862 const u8 *set_addr,
1863 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864{
1865 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 struct i802_bss *bss;
1867
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001868 if (global_priv == NULL)
1869 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 drv = os_zalloc(sizeof(*drv));
1871 if (drv == NULL)
1872 return NULL;
1873 drv->global = global_priv;
1874 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001875 drv->hostapd = !!hostapd;
1876 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001877
1878 /*
1879 * There is no driver capability flag for this, so assume it is
1880 * supported and disable this on first attempt to use if the driver
1881 * rejects the command due to missing support.
1882 */
1883 drv->set_rekey_offload = 1;
1884
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001885 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1886 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001887 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001888
1889 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1890 if (!drv->first_bss) {
1891 os_free(drv);
1892 return NULL;
1893 }
1894 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001896 bss->ctx = ctx;
1897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1899 drv->monitor_ifidx = -1;
1900 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001901 drv->eapol_tx_sock = -1;
1902 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904 if (nl80211_init_bss(bss))
1905 goto failed;
1906
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 goto failed;
1909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1911 if (drv->eapol_tx_sock < 0)
1912 goto failed;
1913
1914 if (drv->data_tx_status) {
1915 int enabled = 1;
1916
1917 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1918 &enabled, sizeof(enabled)) < 0) {
1919 wpa_printf(MSG_DEBUG,
1920 "nl80211: wifi status sockopt failed\n");
1921 drv->data_tx_status = 0;
1922 if (!drv->use_monitor)
1923 drv->capa.flags &=
1924 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1925 } else {
1926 eloop_register_read_sock(drv->eapol_tx_sock,
1927 wpa_driver_nl80211_handle_eapol_tx_status,
1928 drv, NULL);
1929 }
1930 }
1931
1932 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001933 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001935 drv->in_interface_list = 1;
1936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937
1938 return bss;
1939
1940failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001941 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 return NULL;
1943}
1944
1945
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001946/**
1947 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1948 * @ctx: context to be used when calling wpa_supplicant functions,
1949 * e.g., wpa_supplicant_event()
1950 * @ifname: interface name, e.g., wlan0
1951 * @global_priv: private driver global data from global_init()
1952 * Returns: Pointer to private data, %NULL on failure
1953 */
1954static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1955 void *global_priv)
1956{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001957 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1958 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001959}
1960
1961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001962static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963 struct nl_handle *nl_handle,
1964 u16 type, const u8 *match, size_t match_len)
1965{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001966 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001968 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001969 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001971 buf[0] = '\0';
1972 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001973 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1974 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001976 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1977 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1978 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1979 nlmsg_free(msg);
1980 return -1;
1981 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 if (ret) {
1985 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1986 "failed (type=%u): ret=%d (%s)",
1987 type, ret, strerror(-ret));
1988 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1989 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 return ret;
1992}
1993
1994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001995static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1996{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 if (bss->nl_mgmt) {
1998 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1999 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
2000 return -1;
2001 }
2002
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002003 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004 if (bss->nl_mgmt == NULL)
2005 return -1;
2006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002007 return 0;
2008}
2009
2010
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002011static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2012{
2013 nl80211_register_eloop_read(&bss->nl_mgmt,
2014 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002015 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002016}
2017
2018
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002019static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 const u8 *match, size_t match_len)
2021{
2022 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 type, match, match_len);
2025}
2026
2027
Roshan Pius3a1667e2018-07-03 15:17:14 -07002028static int nl80211_init_connect_handle(struct i802_bss *bss)
2029{
2030 if (bss->nl_connect) {
2031 wpa_printf(MSG_DEBUG,
2032 "nl80211: Connect handle already created (nl_connect=%p)",
2033 bss->nl_connect);
2034 return -1;
2035 }
2036
2037 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2038 if (!bss->nl_connect)
2039 return -1;
2040 nl80211_register_eloop_read(&bss->nl_connect,
2041 wpa_driver_nl80211_event_receive,
2042 bss->nl_cb, 1);
2043 return 0;
2044}
2045
2046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002049 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002050 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002051
2052 if (nl80211_alloc_mgmt_handle(bss))
2053 return -1;
2054 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2055 "handle %p", bss->nl_mgmt);
2056
Roshan Pius3a1667e2018-07-03 15:17:14 -07002057 if (drv->nlmode == NL80211_IFTYPE_ADHOC ||
2058 ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2059 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002060 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2061
2062 /* register for any AUTH message */
2063 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
2064 }
2065
Dmitry Shmidt051af732013-10-22 13:52:46 -07002066#ifdef CONFIG_INTERWORKING
2067 /* QoS Map Configure */
2068 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002069 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002070#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002071#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002074 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002076 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002077 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002079 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002080 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002083 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002084 /* Protected GAS Initial Request */
2085 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2086 ret = -1;
2087 /* Protected GAS Initial Response */
2088 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2089 ret = -1;
2090 /* Protected GAS Comeback Request */
2091 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2092 ret = -1;
2093 /* Protected GAS Comeback Response */
2094 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2095 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002096#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002097#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002099 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2101 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002102 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002104 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 (u8 *) "\x7f\x50\x6f\x9a\x09",
2106 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002107 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108#endif /* CONFIG_P2P */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002109#ifdef CONFIG_DPP
2110 /* DPP Public Action */
2111 if (nl80211_register_action_frame(bss,
2112 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2113 6) < 0)
2114 ret = -1;
2115#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116#ifdef CONFIG_IEEE80211W
2117 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002119 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002121#ifdef CONFIG_TDLS
2122 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2123 /* TDLS Discovery Response */
2124 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2125 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002126 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002127 }
2128#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002129#ifdef CONFIG_FST
2130 /* FST Action frames */
2131 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2132 ret = -1;
2133#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134
2135 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002137 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 else
2139 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2140 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2141
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002142 /* WNM - BSS Transition Management Request */
2143 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002144 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002145 /* WNM-Sleep Mode Response */
2146 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002147 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002148
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002149#ifdef CONFIG_HS20
2150 /* WNM-Notification */
2151 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002152 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002153#endif /* CONFIG_HS20 */
2154
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002155 /* WMM-AC ADDTS Response */
2156 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2157 ret = -1;
2158
2159 /* WMM-AC DELTS */
2160 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2161 ret = -1;
2162
2163 /* Radio Measurement - Neighbor Report Response */
2164 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2165 ret = -1;
2166
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002167 /* Radio Measurement - Radio Measurement Request */
2168 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2169 ret = -1;
2170
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002171 /* Radio Measurement - Link Measurement Request */
2172 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2173 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2174 ret = -1;
2175
2176 nl80211_mgmt_handle_register_eloop(bss);
2177
2178 return ret;
2179}
2180
2181
2182static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2183{
2184 int ret = 0;
2185
2186 if (nl80211_alloc_mgmt_handle(bss))
2187 return -1;
2188
2189 wpa_printf(MSG_DEBUG,
2190 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2191 bss->nl_mgmt);
2192
2193 /* Auth frames for mesh SAE */
2194 if (nl80211_register_frame(bss, bss->nl_mgmt,
2195 (WLAN_FC_TYPE_MGMT << 2) |
2196 (WLAN_FC_STYPE_AUTH << 4),
2197 NULL, 0) < 0)
2198 ret = -1;
2199
2200 /* Mesh peering open */
2201 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2202 ret = -1;
2203 /* Mesh peering confirm */
2204 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2205 ret = -1;
2206 /* Mesh peering close */
2207 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2208 ret = -1;
2209
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002210 nl80211_mgmt_handle_register_eloop(bss);
2211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002212 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213}
2214
2215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002216static int nl80211_register_spurious_class3(struct i802_bss *bss)
2217{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002219 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002220
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002221 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2222 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002223 if (ret) {
2224 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2225 "failed: ret=%d (%s)",
2226 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002227 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002228 return ret;
2229}
2230
2231
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002232static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2233{
2234 int ret = 0;
2235
2236 /* Public Action frames */
2237 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2238 ret = -1;
2239 /* RRM Measurement Report */
2240 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2241 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002242 /* RRM Link Measurement Report */
2243 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2244 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002245 /* RRM Neighbor Report Request */
2246 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2247 ret = -1;
2248 /* FT Action frames */
2249 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2250 ret = -1;
2251#ifdef CONFIG_IEEE80211W
2252 /* SA Query */
2253 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2254 ret = -1;
2255#endif /* CONFIG_IEEE80211W */
2256 /* Protected Dual of Public Action */
2257 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2258 ret = -1;
2259 /* WNM */
2260 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2261 ret = -1;
2262 /* WMM */
2263 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2264 ret = -1;
2265#ifdef CONFIG_FST
2266 /* FST Action frames */
2267 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2268 ret = -1;
2269#endif /* CONFIG_FST */
2270 /* Vendor-specific */
2271 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2272 ret = -1;
2273
2274 return ret;
2275}
2276
2277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002278static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2279{
2280 static const int stypes[] = {
2281 WLAN_FC_STYPE_AUTH,
2282 WLAN_FC_STYPE_ASSOC_REQ,
2283 WLAN_FC_STYPE_REASSOC_REQ,
2284 WLAN_FC_STYPE_DISASSOC,
2285 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002286 WLAN_FC_STYPE_PROBE_REQ,
2287/* Beacon doesn't work as mac80211 doesn't currently allow
2288 * it, but it wouldn't really be the right thing anyway as
2289 * it isn't per interface ... maybe just dump the scan
2290 * results periodically for OLBC?
2291 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002292 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002293 };
2294 unsigned int i;
2295
2296 if (nl80211_alloc_mgmt_handle(bss))
2297 return -1;
2298 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2299 "handle %p", bss->nl_mgmt);
2300
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002301 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002302 if (nl80211_register_frame(bss, bss->nl_mgmt,
2303 (WLAN_FC_TYPE_MGMT << 2) |
2304 (stypes[i] << 4),
2305 NULL, 0) < 0) {
2306 goto out_err;
2307 }
2308 }
2309
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002310 if (nl80211_action_subscribe_ap(bss))
2311 goto out_err;
2312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002313 if (nl80211_register_spurious_class3(bss))
2314 goto out_err;
2315
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002316 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002317 return 0;
2318
2319out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002320 nl_destroy_handles(&bss->nl_mgmt);
2321 return -1;
2322}
2323
2324
2325static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2326{
2327 if (nl80211_alloc_mgmt_handle(bss))
2328 return -1;
2329 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2330 "handle %p (device SME)", bss->nl_mgmt);
2331
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002332 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002333 goto out_err;
2334
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002335 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002336 return 0;
2337
2338out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002339 nl_destroy_handles(&bss->nl_mgmt);
2340 return -1;
2341}
2342
2343
2344static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2345{
2346 if (bss->nl_mgmt == NULL)
2347 return;
2348 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2349 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002350 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002351
2352 nl80211_put_wiphy_data_ap(bss);
2353}
2354
2355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2357{
2358 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2359}
2360
2361
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002362static void nl80211_del_p2pdev(struct i802_bss *bss)
2363{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002364 struct nl_msg *msg;
2365 int ret;
2366
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002367 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2368 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002369
2370 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2371 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002372 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002373}
2374
2375
2376static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2377{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002378 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002379 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002380
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002381 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2382 NL80211_CMD_STOP_P2P_DEVICE);
2383 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002384
2385 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2386 start ? "Start" : "Stop",
2387 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002388 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002389 return ret;
2390}
2391
2392
2393static int i802_set_iface_flags(struct i802_bss *bss, int up)
2394{
2395 enum nl80211_iftype nlmode;
2396
2397 nlmode = nl80211_get_ifmode(bss);
2398 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2399 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2400 bss->ifname, up);
2401 }
2402
2403 /* P2P Device has start/stop which is equivalent */
2404 return nl80211_set_p2pdev(bss, up);
2405}
2406
2407
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002408#ifdef CONFIG_TESTING_OPTIONS
2409static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2410{
2411 /* struct wpa_driver_nl80211_data *drv = arg; */
2412 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2413 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2414
2415
2416 wpa_printf(MSG_DEBUG,
2417 "nl80211: QCA vendor test command response received");
2418
2419 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2420 genlmsg_attrlen(gnlh, 0), NULL);
2421 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2422 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2423 return NL_SKIP;
2424 }
2425
2426 wpa_hexdump(MSG_DEBUG,
2427 "nl80211: Received QCA vendor test command response",
2428 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2429 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2430
2431 return NL_SKIP;
2432}
2433#endif /* CONFIG_TESTING_OPTIONS */
2434
2435
2436static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2437{
2438#ifdef CONFIG_TESTING_OPTIONS
2439 struct nl_msg *msg;
2440 struct nlattr *params;
2441 int ret;
2442
2443 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2444 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2445 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2446 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2447 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2448 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2449 nlmsg_free(msg);
2450 return;
2451 }
2452 nla_nest_end(msg, params);
2453
2454 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2455 wpa_printf(MSG_DEBUG,
2456 "nl80211: QCA vendor test command returned %d (%s)",
2457 ret, strerror(-ret));
2458#endif /* CONFIG_TESTING_OPTIONS */
2459}
2460
2461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002463wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002464 const u8 *set_addr, int first,
2465 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002467 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002469 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470
2471 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002472 bss->ifindex = drv->ifindex;
2473 bss->wdev_id = drv->global->if_add_wdevid;
2474 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2475
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002476 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2477 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002478 drv->global->if_add_wdevid_set = 0;
2479
Dmitry Shmidt03658832014-08-13 11:03:49 -07002480 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2481 bss->static_ap = 1;
2482
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002483 if (first &&
2484 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2485 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2486 drv->start_iface_up = 1;
2487
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002488 if (wpa_driver_nl80211_capa(drv))
2489 return -1;
2490
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002491 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2492 return -1;
2493
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002494 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2495 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002497 if (set_addr &&
2498 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2499 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2500 set_addr)))
2501 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002502
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002503 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2504 drv->start_mode_ap = 1;
2505
Dmitry Shmidt03658832014-08-13 11:03:49 -07002506 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002507 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002508 else if (bss->if_dynamic ||
2509 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002510 nlmode = nl80211_get_ifmode(bss);
2511 else
2512 nlmode = NL80211_IFTYPE_STATION;
2513
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002514 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002515 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002516 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002518
Dmitry Shmidt98660862014-03-11 17:26:21 -07002519 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002520 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002522 wpa_driver_nl80211_drv_init_rfkill(drv);
2523
Dmitry Shmidt98660862014-03-11 17:26:21 -07002524 if (!rfkill_is_blocked(drv->rfkill)) {
2525 int ret = i802_set_iface_flags(bss, 1);
2526 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2528 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002529 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002531
2532 if (is_p2p_net_interface(nlmode))
2533 nl80211_disable_11b_rates(bss->drv,
2534 bss->drv->ifindex, 1);
2535
Dmitry Shmidt98660862014-03-11 17:26:21 -07002536 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2537 return ret;
2538 } else {
2539 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2540 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002541 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2542 drv->if_disabled = 1;
2543
Dmitry Shmidt98660862014-03-11 17:26:21 -07002544 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 }
2546
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002547 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002548 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2549 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002551 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2552 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2553 bss->addr))
2554 return -1;
2555 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2556 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 if (send_rfkill_event) {
2559 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2560 drv, drv->ctx);
2561 }
2562
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002563 if (drv->vendor_cmd_test_avail)
2564 qca_vendor_test(drv);
2565
Roshan Pius3a1667e2018-07-03 15:17:14 -07002566 nl80211_init_connect_handle(bss);
2567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 return 0;
2569}
2570
2571
Paul Stewart092955c2017-02-06 09:13:09 -08002572static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573{
2574 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08002575 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002577 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2578 drv->ifindex);
Paul Stewart092955c2017-02-06 09:13:09 -08002579 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002580 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582}
2583
2584
2585/**
2586 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002587 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588 *
2589 * Shut down driver interface and processing of driver events. Free
2590 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2591 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002592static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002595 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002597 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2598 bss->ifname, drv->disabled_11b_rates);
2599
Dmitry Shmidt04949592012-07-19 12:16:46 -07002600 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002601 if (drv->data_tx_status)
2602 eloop_unregister_read_sock(drv->eapol_tx_sock);
2603 if (drv->eapol_tx_sock >= 0)
2604 close(drv->eapol_tx_sock);
2605
2606 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 wpa_driver_nl80211_probe_req_report(bss, 0);
2608 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002609 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2610 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2612 "interface %s from bridge %s: %s",
2613 bss->ifname, bss->brname, strerror(errno));
2614 }
Hai Shalomc9e41a12018-07-31 14:41:42 -07002615
2616 if (drv->rtnl_sk)
2617 nl80211_handle_destroy(drv->rtnl_sk);
2618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002620 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2621 0) < 0)
2622 wpa_printf(MSG_INFO,
2623 "nl80211: Could not set bridge %s down",
2624 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002625 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2627 "bridge %s: %s",
2628 bss->brname, strerror(errno));
2629 }
2630
2631 nl80211_remove_monitor_interface(drv);
2632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002633 if (is_ap_interface(drv->nlmode))
Paul Stewart092955c2017-02-06 09:13:09 -08002634 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 if (drv->eapol_sock >= 0) {
2637 eloop_unregister_read_sock(drv->eapol_sock);
2638 close(drv->eapol_sock);
2639 }
2640
2641 if (drv->if_indices != drv->default_if_indices)
2642 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002644 if (drv->if_indices_reason != drv->default_if_indices_reason)
2645 os_free(drv->if_indices_reason);
2646
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002647 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2649
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002650 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2651 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002652 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 rfkill_deinit(drv->rfkill);
2654
2655 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2656
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002657 if (!drv->start_iface_up)
2658 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002659
2660 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002661 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2662 0) < 0) {
2663 wpa_printf(MSG_DEBUG,
2664 "nl80211: Could not set interface down to restore permanent MAC address");
2665 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002666 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2667 drv->perm_addr) < 0) {
2668 wpa_printf(MSG_DEBUG,
2669 "nl80211: Could not restore permanent MAC address");
2670 }
2671 }
2672
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002673 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002674 if (!drv->hostapd || !drv->start_mode_ap)
2675 wpa_driver_nl80211_set_mode(bss,
2676 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002677 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002678 } else {
2679 nl80211_mgmt_unsubscribe(bss, "deinit");
2680 nl80211_del_p2pdev(bss);
2681 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682
Roshan Pius3a1667e2018-07-03 15:17:14 -07002683 if (bss->nl_connect)
2684 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
2685
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002686 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 os_free(drv->filter_ssids);
2689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002690 os_free(drv->auth_ie);
2691
2692 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693 dl_list_del(&drv->list);
2694
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002695 os_free(drv->extended_capa);
2696 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002697 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2698 os_free(drv->iface_ext_capa[i].ext_capa);
2699 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2700 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002701 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 os_free(drv);
2703}
2704
2705
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002706static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2707{
2708 switch (alg) {
2709 case WPA_ALG_WEP:
2710 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08002711 return RSN_CIPHER_SUITE_WEP40;
2712 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002713 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002714 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002715 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002716 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002717 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002718 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002720 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002721 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002722 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002723 case WPA_ALG_IGTK:
Paul Stewart092955c2017-02-06 09:13:09 -08002724 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002725 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002726 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002727 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002728 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002729 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002730 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002731 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08002732 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002733 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08002734 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002735 case WPA_ALG_NONE:
2736 case WPA_ALG_PMK:
2737 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2738 alg);
2739 return 0;
2740 }
2741
2742 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2743 alg);
2744 return 0;
2745}
2746
2747
2748static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2749{
2750 switch (cipher) {
2751 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002752 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002753 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002754 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002755 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002756 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002757 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002758 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002759 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002760 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002761 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08002762 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002763 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08002764 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002765 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08002766 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002767 }
2768
2769 return 0;
2770}
2771
2772
2773static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2774 int max_suites)
2775{
2776 int num_suites = 0;
2777
2778 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002779 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002780 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002781 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002782 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002783 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002784 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002785 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002786 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08002787 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002788 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08002789 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002790 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08002791 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002792
2793 return num_suites;
2794}
2795
2796
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002797#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002798static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2799 const u8 *key, size_t key_len)
2800{
2801 struct nl_msg *msg;
2802 int ret;
2803
2804 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2805 return 0;
2806
2807 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2808 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2809 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2810 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2811 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2812 nl80211_nlmsg_clear(msg);
2813 nlmsg_free(msg);
2814 return -1;
2815 }
2816 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2817 if (ret) {
2818 wpa_printf(MSG_DEBUG,
2819 "nl80211: Key management set key failed: ret=%d (%s)",
2820 ret, strerror(-ret));
2821 }
2822
2823 return ret;
2824}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002825#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002826
2827
Roshan Pius3a1667e2018-07-03 15:17:14 -07002828static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
2829 const u8 *key, size_t key_len,
2830 const u8 *addr)
2831{
2832 struct nl_msg *msg = NULL;
2833 int ret;
2834
2835 /*
2836 * If the authenticator address is not set, assume it is
2837 * the current BSSID.
2838 */
2839 if (!addr && drv->associated)
2840 addr = drv->bssid;
2841 else if (!addr)
2842 return -1;
2843
2844 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
2845 MAC2STR(addr));
2846 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
2847 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
2848 if (!msg ||
2849 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
2850 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
2851 nl80211_nlmsg_clear(msg);
2852 nlmsg_free(msg);
2853 return -ENOBUFS;
2854 }
2855
2856 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2857 if (ret) {
2858 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
2859 ret, strerror(-ret));
2860 }
2861
2862 return ret;
2863}
2864
2865
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002866static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 enum wpa_alg alg, const u8 *addr,
2868 int key_idx, int set_tx,
2869 const u8 *seq, size_t seq_len,
2870 const u8 *key, size_t key_len)
2871{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002873 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002874 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002876 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002878 /* Ignore for P2P Device */
2879 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2880 return 0;
2881
2882 ifindex = if_nametoindex(ifname);
2883 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002885 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002887#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002888 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002889 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002890 tdls = 1;
2891 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002894#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002895 if (alg == WPA_ALG_PMK &&
2896 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2897 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2898 __func__);
2899 ret = issue_key_mgmt_set_key(drv, key, key_len);
2900 return ret;
2901 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002902#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903
Roshan Pius3a1667e2018-07-03 15:17:14 -07002904 if (alg == WPA_ALG_PMK &&
2905 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2906 return nl80211_set_pmk(drv, key, key_len, addr);
2907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002909 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2910 if (!msg)
2911 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002913 u32 suite;
2914
2915 suite = wpa_alg_to_cipher_suite(alg, key_len);
2916 if (!suite)
2917 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002918 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2919 if (!msg ||
2920 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002921 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002922 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002923 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 }
2925
Dmitry Shmidt98660862014-03-11 17:26:21 -07002926 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002927 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2928 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002929 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
2932 if (addr && !is_broadcast_ether_addr(addr)) {
2933 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002934 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2935 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936
2937 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2938 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002939 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2940 NL80211_KEYTYPE_GROUP))
2941 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942 }
2943 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002944 struct nlattr *types;
2945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002947
2948 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002949 if (!types ||
2950 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2951 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002952 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002954 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2955 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002957 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2959 ret = 0;
2960 if (ret)
2961 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2962 ret, strerror(-ret));
2963
2964 /*
2965 * If we failed or don't need to set the default TX key (below),
2966 * we're done here.
2967 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002968 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002970 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 !is_broadcast_ether_addr(addr))
2972 return ret;
2973
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002974 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2975 if (!msg ||
2976 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002977 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2978 alg == WPA_ALG_BIP_GMAC_128 ||
2979 alg == WPA_ALG_BIP_GMAC_256 ||
2980 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002981 NL80211_ATTR_KEY_DEFAULT_MGMT :
2982 NL80211_ATTR_KEY_DEFAULT))
2983 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002985 struct nlattr *types;
2986
2987 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002988 if (!types ||
2989 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2990 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002991 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002993 struct nlattr *types;
2994
2995 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002996 if (!types ||
2997 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2998 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002999 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 }
3001
3002 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3003 if (ret == -ENOENT)
3004 ret = 0;
3005 if (ret)
3006 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
3007 "err=%d %s)", ret, strerror(-ret));
3008 return ret;
3009
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003010fail:
3011 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003012 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 return -ENOBUFS;
3014}
3015
3016
3017static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3018 int key_idx, int defkey,
3019 const u8 *seq, size_t seq_len,
3020 const u8 *key, size_t key_len)
3021{
3022 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003023 u32 suite;
3024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 if (!key_attr)
3026 return -1;
3027
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003028 suite = wpa_alg_to_cipher_suite(alg, key_len);
3029 if (!suite)
3030 return -1;
3031
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003032 if (defkey && alg == WPA_ALG_IGTK) {
3033 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3034 return -1;
3035 } else if (defkey) {
3036 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3037 return -1;
3038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003040 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003041 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042 (seq && seq_len &&
3043 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3044 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3045 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046
3047 nla_nest_end(msg, key_attr);
3048
3049 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050}
3051
3052
3053static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3054 struct nl_msg *msg)
3055{
3056 int i, privacy = 0;
3057 struct nlattr *nl_keys, *nl_key;
3058
3059 for (i = 0; i < 4; i++) {
3060 if (!params->wep_key[i])
3061 continue;
3062 privacy = 1;
3063 break;
3064 }
3065 if (params->wps == WPS_MODE_PRIVACY)
3066 privacy = 1;
3067 if (params->pairwise_suite &&
3068 params->pairwise_suite != WPA_CIPHER_NONE)
3069 privacy = 1;
3070
3071 if (!privacy)
3072 return 0;
3073
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003074 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3075 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076
3077 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3078 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003079 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080
3081 for (i = 0; i < 4; i++) {
3082 if (!params->wep_key[i])
3083 continue;
3084
3085 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003086 if (!nl_key ||
3087 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3088 params->wep_key[i]) ||
3089 nla_put_u32(msg, NL80211_KEY_CIPHER,
3090 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003091 RSN_CIPHER_SUITE_WEP40 :
3092 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003093 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3094 (i == params->wep_tx_keyidx &&
3095 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3096 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003097
3098 nla_nest_end(msg, nl_key);
3099 }
3100 nla_nest_end(msg, nl_keys);
3101
3102 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103}
3104
3105
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003106int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3107 const u8 *addr, int cmd, u16 reason_code,
3108 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003110 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 struct nl_msg *msg;
3112
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003113 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3114 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3115 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3116 (local_state_change &&
3117 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3118 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003119 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121
3122 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003124 wpa_dbg(drv->ctx, MSG_DEBUG,
3125 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3126 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 return ret;
3129}
3130
3131
3132static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003133 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003135 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003136 int drv_associated = drv->associated;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003137
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003138 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003139 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003140 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003141 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3142 reason_code, 0);
3143 /*
3144 * For locally generated disconnect, supplicant already generates a
3145 * DEAUTH event, so ignore the event from NL80211.
3146 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003147 drv->ignore_next_local_disconnect = drv_associated && (ret == 0);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003148
3149 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150}
3151
3152
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003153static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3154 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003157 int ret;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003158 int drv_associated = drv->associated;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003159
3160 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3161 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003162 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003163 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003165 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3167 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003168 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003169 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3170 reason_code, 0);
3171 /*
3172 * For locally generated deauthenticate, supplicant already generates a
3173 * DEAUTH event, so ignore the event from NL80211.
3174 */
Hai Shalomce48b4a2018-09-05 11:41:35 -07003175 drv->ignore_next_local_deauth = drv_associated && (ret == 0);
3176
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003177 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178}
3179
3180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003181static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3182 struct wpa_driver_auth_params *params)
3183{
3184 int i;
3185
3186 drv->auth_freq = params->freq;
3187 drv->auth_alg = params->auth_alg;
3188 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3189 drv->auth_local_state_change = params->local_state_change;
3190 drv->auth_p2p = params->p2p;
3191
3192 if (params->bssid)
3193 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3194 else
3195 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3196
3197 if (params->ssid) {
3198 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3199 drv->auth_ssid_len = params->ssid_len;
3200 } else
3201 drv->auth_ssid_len = 0;
3202
3203
3204 os_free(drv->auth_ie);
3205 drv->auth_ie = NULL;
3206 drv->auth_ie_len = 0;
3207 if (params->ie) {
3208 drv->auth_ie = os_malloc(params->ie_len);
3209 if (drv->auth_ie) {
3210 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3211 drv->auth_ie_len = params->ie_len;
3212 }
3213 }
3214
3215 for (i = 0; i < 4; i++) {
3216 if (params->wep_key[i] && params->wep_key_len[i] &&
3217 params->wep_key_len[i] <= 16) {
3218 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3219 params->wep_key_len[i]);
3220 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3221 } else
3222 drv->auth_wep_key_len[i] = 0;
3223 }
3224}
3225
3226
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003227static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3228{
3229 struct wpa_driver_nl80211_data *drv = bss->drv;
3230
3231 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3232 return;
3233
3234 /*
3235 * Looks like we failed to unmask 11b rates previously. This could
3236 * happen, e.g., if the interface was down at the point in time when a
3237 * P2P group was terminated.
3238 */
3239 wpa_printf(MSG_DEBUG,
3240 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3241 bss->ifname);
3242 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3243}
3244
3245
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003246static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3247{
3248 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3249 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3250 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3251 return NL80211_AUTHTYPE_SHARED_KEY;
3252 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3253 return NL80211_AUTHTYPE_NETWORK_EAP;
3254 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3255 return NL80211_AUTHTYPE_FT;
3256 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3257 return NL80211_AUTHTYPE_SAE;
3258 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3259 return NL80211_AUTHTYPE_FILS_SK;
3260 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3261 return NL80211_AUTHTYPE_FILS_SK_PFS;
3262
3263 return NL80211_AUTHTYPE_MAX;
3264}
3265
3266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003268 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270 struct wpa_driver_nl80211_data *drv = bss->drv;
3271 int ret = -1, i;
3272 struct nl_msg *msg;
3273 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003274 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003276 int is_retry;
3277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003278 nl80211_unmask_11b_rates(bss);
3279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003280 is_retry = drv->retry_auth;
3281 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003282 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003284 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003286 if (params->bssid)
3287 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3288 else
3289 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003291 nlmode = params->p2p ?
3292 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3293 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003294 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 return -1;
3296
3297retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3299 drv->ifindex);
3300
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003301 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3302 if (!msg)
3303 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304
3305 for (i = 0; i < 4; i++) {
3306 if (!params->wep_key[i])
3307 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003308 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 NULL, i,
3310 i == params->wep_tx_keyidx, NULL, 0,
3311 params->wep_key[i],
3312 params->wep_key_len[i]);
3313 if (params->wep_tx_keyidx != i)
3314 continue;
3315 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003316 params->wep_key[i], params->wep_key_len[i]))
3317 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 }
3319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 if (params->bssid) {
3321 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3322 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003323 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3324 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 }
3326 if (params->freq) {
3327 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003328 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3329 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 }
3331 if (params->ssid) {
3332 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3333 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003334 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3335 params->ssid))
3336 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 }
3338 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003339 if (params->ie &&
3340 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3341 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003342 if (params->auth_data) {
3343 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3344 params->auth_data_len);
3345 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3346 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003347 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003348 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003349 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003351 if (type == NL80211_AUTHTYPE_MAX ||
3352 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003353 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 if (params->local_state_change) {
3355 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003356 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3357 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 }
3359
3360 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3361 msg = NULL;
3362 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003363 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003364 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
3365 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003367 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
3368 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369 /*
3370 * mac80211 does not currently accept new
3371 * authentication if we are already authenticated. As a
3372 * workaround, force deauthentication and try again.
3373 */
3374 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3375 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003376 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377 wpa_driver_nl80211_deauthenticate(
3378 bss, params->bssid,
3379 WLAN_REASON_PREV_AUTH_NOT_VALID);
3380 nlmsg_free(msg);
3381 goto retry;
3382 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003383
3384 if (ret == -ENOENT && params->freq && !is_retry) {
3385 /*
3386 * cfg80211 has likely expired the BSS entry even
3387 * though it was previously available in our internal
3388 * BSS table. To recover quickly, start a single
3389 * channel scan on the specified channel.
3390 */
3391 struct wpa_driver_scan_params scan;
3392 int freqs[2];
3393
3394 os_memset(&scan, 0, sizeof(scan));
3395 scan.num_ssids = 1;
3396 if (params->ssid) {
3397 scan.ssids[0].ssid = params->ssid;
3398 scan.ssids[0].ssid_len = params->ssid_len;
3399 }
3400 freqs[0] = params->freq;
3401 freqs[1] = 0;
3402 scan.freqs = freqs;
3403 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3404 "channel scan to refresh cfg80211 BSS "
3405 "entry");
3406 ret = wpa_driver_nl80211_scan(bss, &scan);
3407 if (ret == 0) {
3408 nl80211_copy_auth_params(drv, params);
3409 drv->scan_for_auth = 1;
3410 }
3411 } else if (is_retry) {
3412 /*
3413 * Need to indicate this with an event since the return
3414 * value from the retry is not delivered to core code.
3415 */
3416 union wpa_event_data event;
3417 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3418 "failed");
3419 os_memset(&event, 0, sizeof(event));
3420 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3421 ETH_ALEN);
3422 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3423 &event);
3424 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003425 } else {
3426 wpa_printf(MSG_DEBUG,
3427 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003430fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 nlmsg_free(msg);
3432 return ret;
3433}
3434
3435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003436int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003437{
3438 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003439 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003440 int i;
3441
3442 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3443
3444 os_memset(&params, 0, sizeof(params));
3445 params.freq = drv->auth_freq;
3446 params.auth_alg = drv->auth_alg;
3447 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3448 params.local_state_change = drv->auth_local_state_change;
3449 params.p2p = drv->auth_p2p;
3450
3451 if (!is_zero_ether_addr(drv->auth_bssid_))
3452 params.bssid = drv->auth_bssid_;
3453
3454 if (drv->auth_ssid_len) {
3455 params.ssid = drv->auth_ssid;
3456 params.ssid_len = drv->auth_ssid_len;
3457 }
3458
3459 params.ie = drv->auth_ie;
3460 params.ie_len = drv->auth_ie_len;
3461
3462 for (i = 0; i < 4; i++) {
3463 if (drv->auth_wep_key_len[i]) {
3464 params.wep_key[i] = drv->auth_wep_key[i];
3465 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3466 }
3467 }
3468
3469 drv->retry_auth = 1;
3470 return wpa_driver_nl80211_authenticate(bss, &params);
3471}
3472
3473
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003474static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3475 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003476 int encrypt, int noack,
3477 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003478 int offchanok, unsigned int wait_time,
3479 const u16 *csa_offs,
3480 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003481{
3482 struct wpa_driver_nl80211_data *drv = bss->drv;
3483 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003484 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003485
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003486 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3487 freq = nl80211_get_assoc_freq(drv);
3488 wpa_printf(MSG_DEBUG,
3489 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3490 freq);
3491 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003492 if (freq == 0) {
3493 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3494 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003495 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003496 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003497
Dmitry Shmidt56052862013-10-04 10:23:25 -07003498 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003499 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003500 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003501 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003502 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003503
Dmitry Shmidt56052862013-10-04 10:23:25 -07003504 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003505 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003506 &cookie, no_cck, noack, offchanok,
3507 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003508 if (res == 0 && !noack) {
3509 const struct ieee80211_mgmt *mgmt;
3510 u16 fc;
3511
3512 mgmt = (const struct ieee80211_mgmt *) data;
3513 fc = le_to_host16(mgmt->frame_control);
3514 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3515 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3516 wpa_printf(MSG_MSGDUMP,
3517 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3518 (long long unsigned int)
3519 drv->send_action_cookie,
3520 (long long unsigned int) cookie);
3521 drv->send_action_cookie = cookie;
3522 }
3523 }
3524
3525 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003526}
3527
3528
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003529static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3530 size_t data_len, int noack,
3531 unsigned int freq, int no_cck,
3532 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003533 unsigned int wait_time,
3534 const u16 *csa_offs,
3535 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537 struct wpa_driver_nl80211_data *drv = bss->drv;
3538 struct ieee80211_mgmt *mgmt;
3539 int encrypt = 1;
3540 u16 fc;
3541
3542 mgmt = (struct ieee80211_mgmt *) data;
3543 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003544 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3545 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3546 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3547 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003548
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003549 if ((is_sta_interface(drv->nlmode) ||
3550 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3552 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3553 /*
3554 * The use of last_mgmt_freq is a bit of a hack,
3555 * but it works due to the single-threaded nature
3556 * of wpa_supplicant.
3557 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003558 if (freq == 0) {
3559 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3560 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003561 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003562 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003563 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003564 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003565 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003567
3568 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003569 if (freq == 0) {
3570 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3571 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003572 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003573 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003574 return nl80211_send_frame_cmd(bss, freq,
3575 (int) freq == bss->freq ? 0 :
3576 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003577 data, data_len,
3578 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003579 no_cck, noack, offchanok,
3580 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003581 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3584 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3585 /*
3586 * Only one of the authentication frame types is encrypted.
3587 * In order for static WEP encryption to work properly (i.e.,
3588 * to not encrypt the frame), we need to tell mac80211 about
3589 * the frames that must not be encrypted.
3590 */
3591 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3592 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3593 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3594 encrypt = 0;
3595 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003596
Dmitry Shmidt56052862013-10-04 10:23:25 -07003597 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003598 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003599 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003600 wait_time, csa_offs,
3601 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003602}
3603
3604
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003605static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3606{
3607 u8 rates[NL80211_MAX_SUPP_RATES];
3608 u8 rates_len = 0;
3609 int i;
3610
3611 if (!basic_rates)
3612 return 0;
3613
3614 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3615 rates[rates_len++] = basic_rates[i] / 5;
3616
3617 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3618}
3619
3620
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003621static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3622 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003623 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003624{
3625 struct wpa_driver_nl80211_data *drv = bss->drv;
3626 struct nl_msg *msg;
3627
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003628 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3629 (cts >= 0 &&
3630 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3631 (preamble >= 0 &&
3632 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3633 (slot >= 0 &&
3634 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3635 (ht_opmode >= 0 &&
3636 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3637 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003638 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3639 nl80211_put_basic_rates(msg, basic_rates)) {
3640 nlmsg_free(msg);
3641 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003642 }
3643
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003644 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003645}
3646
3647
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003648static int wpa_driver_nl80211_set_acl(void *priv,
3649 struct hostapd_acl_params *params)
3650{
3651 struct i802_bss *bss = priv;
3652 struct wpa_driver_nl80211_data *drv = bss->drv;
3653 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003654 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003655 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003656 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003657
3658 if (!(drv->capa.max_acl_mac_addrs))
3659 return -ENOTSUP;
3660
3661 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3662 return -ENOTSUP;
3663
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003664 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3665 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3666
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003667 acl = nlmsg_alloc();
3668 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003669 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003670 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003671 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3672 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003673 return -ENOMEM;
3674 }
3675 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003677 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3678 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3679 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3680 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3681 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3682 nlmsg_free(msg);
3683 nlmsg_free(acl);
3684 return -ENOMEM;
3685 }
3686 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003687
3688 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003689 if (ret) {
3690 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3691 ret, strerror(-ret));
3692 }
3693
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003694 return ret;
3695}
3696
3697
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003698static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3699{
3700 if (beacon_int > 0) {
3701 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3702 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3703 beacon_int);
3704 }
3705
3706 return 0;
3707}
3708
3709
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003710static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3711{
3712 if (dtim_period > 0) {
3713 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3714 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3715 }
3716
3717 return 0;
3718}
3719
3720
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003721#ifdef CONFIG_MESH
3722static int nl80211_set_mesh_config(void *priv,
3723 struct wpa_driver_mesh_bss_params *params)
3724{
3725 struct i802_bss *bss = priv;
3726 struct wpa_driver_nl80211_data *drv = bss->drv;
3727 struct nl_msg *msg;
3728 int ret;
3729
3730 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3731 if (!msg)
3732 return -1;
3733
3734 ret = nl80211_put_mesh_config(msg, params);
3735 if (ret < 0) {
3736 nlmsg_free(msg);
3737 return ret;
3738 }
3739
3740 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3741 if (ret) {
3742 wpa_printf(MSG_ERROR,
3743 "nl80211: Mesh config set failed: %d (%s)",
3744 ret, strerror(-ret));
3745 return ret;
3746 }
3747 return 0;
3748}
3749#endif /* CONFIG_MESH */
3750
3751
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003752static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3753 struct wpa_driver_ap_params *params)
3754{
3755 struct nlattr *bands, *band;
3756 struct nl80211_txrate_vht vht_rate;
3757
3758 if (!params->freq ||
3759 (params->beacon_rate == 0 &&
3760 params->rate_type == BEACON_RATE_LEGACY))
3761 return 0;
3762
3763 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3764 if (!bands)
3765 return -1;
3766
3767 switch (params->freq->mode) {
3768 case HOSTAPD_MODE_IEEE80211B:
3769 case HOSTAPD_MODE_IEEE80211G:
3770 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3771 break;
3772 case HOSTAPD_MODE_IEEE80211A:
3773 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3774 break;
3775 case HOSTAPD_MODE_IEEE80211AD:
3776 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3777 break;
3778 default:
3779 return 0;
3780 }
3781
3782 if (!band)
3783 return -1;
3784
3785 os_memset(&vht_rate, 0, sizeof(vht_rate));
3786
3787 switch (params->rate_type) {
3788 case BEACON_RATE_LEGACY:
3789 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3790 wpa_printf(MSG_INFO,
3791 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3792 return -1;
3793 }
3794
3795 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3796 (u8) params->beacon_rate / 5) ||
3797 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3798 (params->freq->vht_enabled &&
3799 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3800 &vht_rate)))
3801 return -1;
3802
3803 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3804 params->beacon_rate);
3805 break;
3806 case BEACON_RATE_HT:
3807 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3808 wpa_printf(MSG_INFO,
3809 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3810 return -1;
3811 }
3812 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3813 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3814 (params->freq->vht_enabled &&
3815 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3816 &vht_rate)))
3817 return -1;
3818 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3819 params->beacon_rate);
3820 break;
3821 case BEACON_RATE_VHT:
3822 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3823 wpa_printf(MSG_INFO,
3824 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3825 return -1;
3826 }
3827 vht_rate.mcs[0] = BIT(params->beacon_rate);
3828 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3829 return -1;
3830 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3831 return -1;
3832 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3833 &vht_rate))
3834 return -1;
3835 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3836 params->beacon_rate);
3837 break;
3838 }
3839
3840 nla_nest_end(msg, band);
3841 nla_nest_end(msg, bands);
3842
3843 return 0;
3844}
3845
3846
3847static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3848 int multicast_to_unicast)
3849{
3850 struct wpa_driver_nl80211_data *drv = bss->drv;
3851 struct nl_msg *msg;
3852 int ret;
3853
3854 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3855 if (!msg ||
3856 (multicast_to_unicast &&
3857 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3858 wpa_printf(MSG_ERROR,
3859 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3860 bss->ifname);
3861 nlmsg_free(msg);
3862 return -ENOBUFS;
3863 }
3864
3865 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3866
3867 switch (ret) {
3868 case 0:
3869 wpa_printf(MSG_DEBUG,
3870 "nl80211: multicast to unicast %s on interface %s",
3871 multicast_to_unicast ? "enabled" : "disabled",
3872 bss->ifname);
3873 break;
3874 case -EOPNOTSUPP:
3875 if (!multicast_to_unicast)
3876 break;
3877 wpa_printf(MSG_INFO,
3878 "nl80211: multicast to unicast not supported on interface %s",
3879 bss->ifname);
3880 break;
3881 default:
3882 wpa_printf(MSG_ERROR,
3883 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3884 multicast_to_unicast ? "enabling" : "disabling",
3885 ret, strerror(-ret), bss->ifname);
3886 break;
3887 }
3888
3889 return ret;
3890}
3891
3892
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003893static int wpa_driver_nl80211_set_ap(void *priv,
3894 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895{
3896 struct i802_bss *bss = priv;
3897 struct wpa_driver_nl80211_data *drv = bss->drv;
3898 struct nl_msg *msg;
3899 u8 cmd = NL80211_CMD_NEW_BEACON;
3900 int ret;
3901 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003903 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003904 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003905 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003906#ifdef CONFIG_MESH
3907 struct wpa_driver_mesh_bss_params mesh_params;
3908#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003909
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003910 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3913 beacon_set);
3914 if (beacon_set)
3915 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08003916 else if (!drv->device_ap_sme && !drv->use_monitor &&
3917 !nl80211_get_wiphy_data_ap(bss))
3918 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003920 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3921 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003922 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3923 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003924 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003925 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003926 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3927 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003928 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003929 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3930 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003931 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3932 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3933 params->head) ||
3934 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3935 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003936 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003937 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003938 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003939 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3940 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003941 if (params->proberesp && params->proberesp_len) {
3942 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3943 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003944 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3945 params->proberesp))
3946 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003947 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003948 switch (params->hide_ssid) {
3949 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003950 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003951 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3952 NL80211_HIDDEN_SSID_NOT_IN_USE))
3953 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003954 break;
3955 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003956 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003957 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3958 NL80211_HIDDEN_SSID_ZERO_LEN))
3959 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960 break;
3961 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003962 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003963 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3964 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3965 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003966 break;
3967 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003968 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003969 if (params->privacy &&
3970 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3971 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003972 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003973 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3974 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3975 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003976 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3977 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3978 NL80211_AUTHTYPE_SHARED_KEY))
3979 goto fail;
3980 } else {
3981 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3982 NL80211_AUTHTYPE_OPEN_SYSTEM))
3983 goto fail;
3984 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003986 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003987 ver = 0;
3988 if (params->wpa_version & WPA_PROTO_WPA)
3989 ver |= NL80211_WPA_VERSION_1;
3990 if (params->wpa_version & WPA_PROTO_RSN)
3991 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003992 if (ver &&
3993 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3994 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003995
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003996 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3997 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003998 num_suites = 0;
3999 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
Paul Stewart092955c2017-02-06 09:13:09 -08004000 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004001 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
Paul Stewart092955c2017-02-06 09:13:09 -08004002 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004003 if (num_suites &&
4004 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
4005 suites))
4006 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004007
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004008 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004009 (!params->pairwise_ciphers ||
4010 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
4011 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
4012 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004013 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004014
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004015 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4016 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004017 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4018 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004019 if (num_suites &&
4020 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4021 num_suites * sizeof(u32), suites))
4022 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004023
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004024 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4025 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004026 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004027 if (suite &&
4028 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4029 goto fail;
4030
Dmitry Shmidte4663042016-04-04 10:07:49 -07004031 if (params->ht_opmode != -1) {
4032 switch (params->smps_mode) {
4033 case HT_CAP_INFO_SMPS_DYNAMIC:
4034 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
4035 smps_mode = NL80211_SMPS_DYNAMIC;
4036 break;
4037 case HT_CAP_INFO_SMPS_STATIC:
4038 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
4039 smps_mode = NL80211_SMPS_STATIC;
4040 break;
4041 default:
4042 /* invalid - fallback to smps off */
4043 case HT_CAP_INFO_SMPS_DISABLED:
4044 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
4045 smps_mode = NL80211_SMPS_OFF;
4046 break;
4047 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004048 if (nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
Dmitry Shmidte4663042016-04-04 10:07:49 -07004049 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004050 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004051
4052 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004053 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4054 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004055 if (nla_put(msg, NL80211_ATTR_IE,
4056 wpabuf_len(params->beacon_ies),
4057 wpabuf_head(params->beacon_ies)))
4058 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004059 }
4060 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004061 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4062 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004063 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4064 wpabuf_len(params->proberesp_ies),
4065 wpabuf_head(params->proberesp_ies)))
4066 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004067 }
4068 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004069 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4070 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004071 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4072 wpabuf_len(params->assocresp_ies),
4073 wpabuf_head(params->assocresp_ies)))
4074 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076
Dmitry Shmidt04949592012-07-19 12:16:46 -07004077 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004078 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4079 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4081 params->ap_max_inactivity))
4082 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004083 }
4084
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004085#ifdef CONFIG_P2P
4086 if (params->p2p_go_ctwindow > 0) {
4087 if (drv->p2p_go_ctwindow_supported) {
4088 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4089 params->p2p_go_ctwindow);
4090 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4091 params->p2p_go_ctwindow))
4092 goto fail;
4093 } else {
4094 wpa_printf(MSG_INFO,
4095 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4096 }
4097 }
4098#endif /* CONFIG_P2P */
4099
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004100 if (params->pbss) {
4101 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4102 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4103 goto fail;
4104 }
4105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4107 if (ret) {
4108 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4109 ret, strerror(-ret));
4110 } else {
4111 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004112 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4113 params->short_slot_time, params->ht_opmode,
4114 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004115 nl80211_set_multicast_to_unicast(bss,
4116 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004117 if (beacon_set && params->freq &&
4118 params->freq->bandwidth != bss->bandwidth) {
4119 wpa_printf(MSG_DEBUG,
4120 "nl80211: Update BSS %s bandwidth: %d -> %d",
4121 bss->ifname, bss->bandwidth,
4122 params->freq->bandwidth);
4123 ret = nl80211_set_channel(bss, params->freq, 1);
4124 if (ret) {
4125 wpa_printf(MSG_DEBUG,
4126 "nl80211: Frequency set failed: %d (%s)",
4127 ret, strerror(-ret));
4128 } else {
4129 wpa_printf(MSG_DEBUG,
4130 "nl80211: Frequency set succeeded for ht2040 coex");
4131 bss->bandwidth = params->freq->bandwidth;
4132 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004133 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004134 /*
4135 * cfg80211 updates the driver on frequence change in AP
4136 * mode only at the point when beaconing is started, so
4137 * set the initial value here.
4138 */
4139 bss->bandwidth = params->freq->bandwidth;
4140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004142
4143#ifdef CONFIG_MESH
4144 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4145 os_memset(&mesh_params, 0, sizeof(mesh_params));
4146 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4147 mesh_params.ht_opmode = params->ht_opmode;
4148 ret = nl80211_set_mesh_config(priv, &mesh_params);
4149 if (ret < 0)
4150 return ret;
4151 }
4152#endif /* CONFIG_MESH */
4153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004154 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004155fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004156 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 return -ENOBUFS;
4158}
4159
4160
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004161static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004162 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004164 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004165 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4166 return -ENOBUFS;
4167
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004168 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4169 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4170
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004171 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004172 enum nl80211_chan_width cw;
4173
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004174 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004175 switch (freq->bandwidth) {
4176 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004177 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004178 break;
4179 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004180 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004181 break;
4182 case 80:
4183 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004184 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004185 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004186 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004187 break;
4188 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004189 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004190 break;
4191 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004192 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004193 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004194
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004195 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4196 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4197 freq->center_freq1);
4198 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4199 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004200 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4201 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4202 freq->center_freq1) ||
4203 (freq->center_freq2 &&
4204 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4205 freq->center_freq2)))
4206 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004207 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004208 enum nl80211_channel_type ct;
4209
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004210 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4211 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004212 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004214 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 break;
4216 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004217 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 break;
4219 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004220 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004221 break;
4222 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004223
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004224 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004225 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4226 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004227 } else {
4228 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4229 NL80211_CHAN_NO_HT);
4230 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4231 NL80211_CHAN_NO_HT))
4232 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004234 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004235}
4236
4237
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004238static int nl80211_set_channel(struct i802_bss *bss,
4239 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004240{
4241 struct wpa_driver_nl80211_data *drv = bss->drv;
4242 struct nl_msg *msg;
4243 int ret;
4244
4245 wpa_printf(MSG_DEBUG,
4246 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4247 freq->freq, freq->ht_enabled, freq->vht_enabled,
4248 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004249
4250 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4251 NL80211_CMD_SET_WIPHY);
4252 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4253 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004254 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256
4257 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004258 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004259 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004263 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 return -1;
4265}
4266
4267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004268static u32 sta_flags_nl80211(int flags)
4269{
4270 u32 f = 0;
4271
4272 if (flags & WPA_STA_AUTHORIZED)
4273 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4274 if (flags & WPA_STA_WMM)
4275 f |= BIT(NL80211_STA_FLAG_WME);
4276 if (flags & WPA_STA_SHORT_PREAMBLE)
4277 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4278 if (flags & WPA_STA_MFP)
4279 f |= BIT(NL80211_STA_FLAG_MFP);
4280 if (flags & WPA_STA_TDLS_PEER)
4281 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004282 if (flags & WPA_STA_AUTHENTICATED)
4283 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004284 if (flags & WPA_STA_ASSOCIATED)
4285 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004286
4287 return f;
4288}
4289
4290
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004291#ifdef CONFIG_MESH
4292static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4293{
4294 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004295 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004296 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004297 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004298 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004299 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004300 return NL80211_PLINK_OPN_RCVD;
4301 case PLINK_CNF_RCVD:
4302 return NL80211_PLINK_CNF_RCVD;
4303 case PLINK_ESTAB:
4304 return NL80211_PLINK_ESTAB;
4305 case PLINK_HOLDING:
4306 return NL80211_PLINK_HOLDING;
4307 case PLINK_BLOCKED:
4308 return NL80211_PLINK_BLOCKED;
4309 default:
4310 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4311 state);
4312 }
4313 return -1;
4314}
4315#endif /* CONFIG_MESH */
4316
4317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318static int wpa_driver_nl80211_sta_add(void *priv,
4319 struct hostapd_sta_add_params *params)
4320{
4321 struct i802_bss *bss = priv;
4322 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004323 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004324 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 int ret = -ENOBUFS;
4326
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004327 if ((params->flags & WPA_STA_TDLS_PEER) &&
4328 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4329 return -EOPNOTSUPP;
4330
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004331 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4332 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004333 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4334 NL80211_CMD_NEW_STATION);
4335 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4336 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004338 /*
4339 * Set the below properties only in one of the following cases:
4340 * 1. New station is added, already associated.
4341 * 2. Set WPA_STA_TDLS_PEER station.
4342 * 3. Set an already added unassociated station, if driver supports
4343 * full AP client state. (Set these properties after station became
4344 * associated will be rejected by the driver).
4345 */
4346 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4347 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4348 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004349 wpa_hexdump(MSG_DEBUG, " * supported rates",
4350 params->supp_rates, params->supp_rates_len);
4351 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4352 params->capability);
4353 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4354 params->supp_rates_len, params->supp_rates) ||
4355 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4356 params->capability))
4357 goto fail;
4358
4359 if (params->ht_capabilities) {
4360 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4361 (u8 *) params->ht_capabilities,
4362 sizeof(*params->ht_capabilities));
4363 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4364 sizeof(*params->ht_capabilities),
4365 params->ht_capabilities))
4366 goto fail;
4367 }
4368
4369 if (params->vht_capabilities) {
4370 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4371 (u8 *) params->vht_capabilities,
4372 sizeof(*params->vht_capabilities));
4373 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4374 sizeof(*params->vht_capabilities),
4375 params->vht_capabilities))
4376 goto fail;
4377 }
4378
4379 if (params->ext_capab) {
4380 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4381 params->ext_capab, params->ext_capab_len);
4382 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4383 params->ext_capab_len, params->ext_capab))
4384 goto fail;
4385 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004386
4387 if (is_ap_interface(drv->nlmode) &&
4388 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4389 params->support_p2p_ps ?
4390 NL80211_P2P_PS_SUPPORTED :
4391 NL80211_P2P_PS_UNSUPPORTED))
4392 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004393 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004394 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004395 if (params->aid) {
4396 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004397 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4398 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004399 } else {
4400 /*
4401 * cfg80211 validates that AID is non-zero, so we have
4402 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004403 * a dummy STA entry is used for now and for a station
4404 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004405 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004406 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4407 (params->flags & WPA_STA_TDLS_PEER) ?
4408 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004409 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4410 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004411 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004412 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4413 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004414 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4415 params->listen_interval))
4416 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004417 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4418 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004419 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4420 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004421 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4422 (params->flags & WPA_STA_ASSOCIATED)) {
4423 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4424 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4425 params->listen_interval);
4426 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4427 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4428 params->listen_interval))
4429 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004430 }
4431
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004432 if (params->vht_opmode_enabled) {
4433 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004434 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4435 params->vht_opmode))
4436 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004437 }
4438
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004439 if (params->supp_channels) {
4440 wpa_hexdump(MSG_DEBUG, " * supported channels",
4441 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004442 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4443 params->supp_channels_len, params->supp_channels))
4444 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004445 }
4446
4447 if (params->supp_oper_classes) {
4448 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4449 params->supp_oper_classes,
4450 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004451 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4452 params->supp_oper_classes_len,
4453 params->supp_oper_classes))
4454 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004455 }
4456
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004457 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004458 upd.set = sta_flags_nl80211(params->flags);
4459 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004460
4461 /*
4462 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4463 * flags, as nl80211 driver moves a new station, by default, into
4464 * associated state.
4465 *
4466 * On the other hand, if the driver supports that feature and the
4467 * station is added in unauthenticated state, set the
4468 * authenticated/associated bits in the mask to prevent moving this
4469 * station to associated state before it is actually associated.
4470 *
4471 * This is irrelevant for mesh mode where the station is added to the
4472 * driver as authenticated already, and ASSOCIATED isn't part of the
4473 * nl80211 API.
4474 */
4475 if (!is_mesh_interface(drv->nlmode)) {
4476 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4477 wpa_printf(MSG_DEBUG,
4478 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4479 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4480 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4481 } else if (!params->set &&
4482 !(params->flags & WPA_STA_TDLS_PEER)) {
4483 if (!(params->flags & WPA_STA_AUTHENTICATED))
4484 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4485 if (!(params->flags & WPA_STA_ASSOCIATED))
4486 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4487 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004488#ifdef CONFIG_MESH
4489 } else {
4490 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4491 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4492 params->peer_aid);
4493 if (ret)
4494 goto fail;
4495 }
4496#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004497 }
4498
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004499 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4500 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004501 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4502 goto fail;
4503
4504#ifdef CONFIG_MESH
4505 if (params->plink_state &&
4506 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4507 sta_plink_state_nl80211(params->plink_state)))
4508 goto fail;
4509#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004510
4511 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004512 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4513
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004514 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 if (!wme ||
4516 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4517 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4518 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4519 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4520 WMM_QOSINFO_STA_SP_MASK))
4521 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004522 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004523 }
4524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004526 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004527 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004528 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4529 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4530 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 if (ret == -EEXIST)
4532 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004533fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004534 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 return ret;
4536}
4537
4538
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004539static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4540{
4541#ifdef CONFIG_LIBNL3_ROUTE
4542 struct wpa_driver_nl80211_data *drv = bss->drv;
4543 struct rtnl_neigh *rn;
4544 struct nl_addr *nl_addr;
4545 int err;
4546
4547 rn = rtnl_neigh_alloc();
4548 if (!rn)
4549 return;
4550
4551 rtnl_neigh_set_family(rn, AF_BRIDGE);
4552 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4553 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4554 if (!nl_addr) {
4555 rtnl_neigh_put(rn);
4556 return;
4557 }
4558 rtnl_neigh_set_lladdr(rn, nl_addr);
4559
4560 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4561 if (err < 0) {
4562 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4563 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4564 bss->ifindex, nl_geterror(err));
4565 } else {
4566 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4567 MACSTR, MAC2STR(addr));
4568 }
4569
4570 nl_addr_put(nl_addr);
4571 rtnl_neigh_put(rn);
4572#endif /* CONFIG_LIBNL3_ROUTE */
4573}
4574
4575
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004576static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4577 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579 struct wpa_driver_nl80211_data *drv = bss->drv;
4580 struct nl_msg *msg;
4581 int ret;
4582
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004583 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4584 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4585 (deauth == 0 &&
4586 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4587 WLAN_FC_STYPE_DISASSOC)) ||
4588 (deauth == 1 &&
4589 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4590 WLAN_FC_STYPE_DEAUTH)) ||
4591 (reason_code &&
4592 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4593 nlmsg_free(msg);
4594 return -ENOBUFS;
4595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596
4597 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004598 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4599 " --> %d (%s)",
4600 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004601
4602 if (drv->rtnl_sk)
4603 rtnl_neigh_delete_fdb_entry(bss, addr);
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 if (ret == -ENOENT)
4606 return 0;
4607 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608}
4609
4610
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004611void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612{
4613 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004614 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615
4616 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004619 dl_list_for_each(drv2, &drv->global->interfaces,
4620 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004621 {
4622 del_ifidx(drv2, ifidx, IFIDX_ANY);
4623 /* Remove all bridges learned for this iface */
4624 del_ifidx(drv2, IFIDX_ANY, ifidx);
4625 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004627 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4629 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004630 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4631}
4632
4633
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004634const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004635{
4636 switch (mode) {
4637 case NL80211_IFTYPE_ADHOC:
4638 return "ADHOC";
4639 case NL80211_IFTYPE_STATION:
4640 return "STATION";
4641 case NL80211_IFTYPE_AP:
4642 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004643 case NL80211_IFTYPE_AP_VLAN:
4644 return "AP_VLAN";
4645 case NL80211_IFTYPE_WDS:
4646 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004647 case NL80211_IFTYPE_MONITOR:
4648 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004649 case NL80211_IFTYPE_MESH_POINT:
4650 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004651 case NL80211_IFTYPE_P2P_CLIENT:
4652 return "P2P_CLIENT";
4653 case NL80211_IFTYPE_P2P_GO:
4654 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004655 case NL80211_IFTYPE_P2P_DEVICE:
4656 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004657 default:
4658 return "unknown";
4659 }
4660}
4661
4662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4664 const char *ifname,
4665 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004666 const u8 *addr, int wds,
4667 int (*handler)(struct nl_msg *, void *),
4668 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004670 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 int ifidx;
4672 int ret = -ENOBUFS;
4673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004674 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4675 iftype, nl80211_iftype_str(iftype));
4676
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004677 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4678 if (!msg ||
4679 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4680 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4681 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682
4683 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004684 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004686 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004687 if (!flags ||
4688 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4689 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004691 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004693 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4694 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004695 }
4696
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004697 /*
4698 * Tell cfg80211 that the interface belongs to the socket that created
4699 * it, and the interface should be deleted when the socket is closed.
4700 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004701 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4702 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004703
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004704 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004705 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004706 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004707 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004708 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4710 ifname, ret, strerror(-ret));
4711 return ret;
4712 }
4713
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004714 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4715 return 0;
4716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717 ifidx = if_nametoindex(ifname);
4718 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4719 ifname, ifidx);
4720
4721 if (ifidx <= 0)
4722 return -1;
4723
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004724 /*
4725 * Some virtual interfaces need to process EAPOL packets and events on
4726 * the parent interface. This is used mainly with hostapd.
4727 */
4728 if (drv->hostapd ||
4729 iftype == NL80211_IFTYPE_AP_VLAN ||
4730 iftype == NL80211_IFTYPE_WDS ||
4731 iftype == NL80211_IFTYPE_MONITOR) {
4732 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004733 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735
4736 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004737 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 nl80211_remove_iface(drv, ifidx);
4739 return -1;
4740 }
4741
4742 return ifidx;
4743}
4744
4745
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004746int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4747 const char *ifname, enum nl80211_iftype iftype,
4748 const u8 *addr, int wds,
4749 int (*handler)(struct nl_msg *, void *),
4750 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751{
4752 int ret;
4753
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004754 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4755 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004757 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004759 if (use_existing) {
4760 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4761 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004762 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4763 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4764 addr) < 0 &&
4765 (linux_set_iface_flags(drv->global->ioctl_sock,
4766 ifname, 0) < 0 ||
4767 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4768 addr) < 0 ||
4769 linux_set_iface_flags(drv->global->ioctl_sock,
4770 ifname, 1) < 0))
4771 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004772 return -ENFILE;
4773 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4775
4776 /* Try to remove the interface that was already there. */
4777 nl80211_remove_iface(drv, if_nametoindex(ifname));
4778
4779 /* Try to create the interface again */
4780 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004781 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782 }
4783
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004784 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4785 wpa_printf(MSG_DEBUG,
4786 "nl80211: Interface %s created for P2P - disable 11b rates",
4787 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790
4791 return ret;
4792}
4793
4794
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004795static int nl80211_setup_ap(struct i802_bss *bss)
4796{
4797 struct wpa_driver_nl80211_data *drv = bss->drv;
4798
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004799 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4800 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004801
4802 /*
4803 * Disable Probe Request reporting unless we need it in this way for
4804 * devices that include the AP SME, in the other case (unless using
4805 * monitor iface) we'll get it through the nl_mgmt socket instead.
4806 */
4807 if (!drv->device_ap_sme)
4808 wpa_driver_nl80211_probe_req_report(bss, 0);
4809
4810 if (!drv->device_ap_sme && !drv->use_monitor)
4811 if (nl80211_mgmt_subscribe_ap(bss))
4812 return -1;
4813
4814 if (drv->device_ap_sme && !drv->use_monitor)
4815 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004816 wpa_printf(MSG_DEBUG,
4817 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004818
4819 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004820 nl80211_create_monitor_interface(drv) &&
4821 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004822 return -1;
4823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004824 if (drv->device_ap_sme &&
4825 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4826 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4827 "Probe Request frame reporting in AP mode");
4828 /* Try to survive without this */
4829 }
4830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004831 return 0;
4832}
4833
4834
4835static void nl80211_teardown_ap(struct i802_bss *bss)
4836{
4837 struct wpa_driver_nl80211_data *drv = bss->drv;
4838
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004839 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4840 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004841 if (drv->device_ap_sme) {
4842 wpa_driver_nl80211_probe_req_report(bss, 0);
4843 if (!drv->use_monitor)
4844 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4845 } else if (drv->use_monitor)
4846 nl80211_remove_monitor_interface(drv);
4847 else
4848 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4849
Paul Stewart092955c2017-02-06 09:13:09 -08004850 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004851 bss->beacon_set = 0;
4852}
4853
4854
4855static int nl80211_send_eapol_data(struct i802_bss *bss,
4856 const u8 *addr, const u8 *data,
4857 size_t data_len)
4858{
4859 struct sockaddr_ll ll;
4860 int ret;
4861
4862 if (bss->drv->eapol_tx_sock < 0) {
4863 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4864 return -1;
4865 }
4866
4867 os_memset(&ll, 0, sizeof(ll));
4868 ll.sll_family = AF_PACKET;
4869 ll.sll_ifindex = bss->ifindex;
4870 ll.sll_protocol = htons(ETH_P_PAE);
4871 ll.sll_halen = ETH_ALEN;
4872 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4873 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4874 (struct sockaddr *) &ll, sizeof(ll));
4875 if (ret < 0)
4876 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4877 strerror(errno));
4878
4879 return ret;
4880}
4881
4882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4884
4885static int wpa_driver_nl80211_hapd_send_eapol(
4886 void *priv, const u8 *addr, const u8 *data,
4887 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4888{
4889 struct i802_bss *bss = priv;
4890 struct wpa_driver_nl80211_data *drv = bss->drv;
4891 struct ieee80211_hdr *hdr;
4892 size_t len;
4893 u8 *pos;
4894 int res;
4895 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004896
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004897 if (drv->device_ap_sme || !drv->use_monitor)
4898 return nl80211_send_eapol_data(bss, addr, data, data_len);
4899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4901 data_len;
4902 hdr = os_zalloc(len);
4903 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004904 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4905 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906 return -1;
4907 }
4908
4909 hdr->frame_control =
4910 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4911 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4912 if (encrypt)
4913 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4914 if (qos) {
4915 hdr->frame_control |=
4916 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4917 }
4918
4919 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4920 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4921 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4922 pos = (u8 *) (hdr + 1);
4923
4924 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004925 /* Set highest priority in QoS header */
4926 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927 pos[1] = 0;
4928 pos += 2;
4929 }
4930
4931 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4932 pos += sizeof(rfc1042_header);
4933 WPA_PUT_BE16(pos, ETH_P_PAE);
4934 pos += 2;
4935 memcpy(pos, data, data_len);
4936
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004937 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004938 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 if (res < 0) {
4940 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4941 "failed: %d (%s)",
4942 (unsigned long) len, errno, strerror(errno));
4943 }
4944 os_free(hdr);
4945
4946 return res;
4947}
4948
4949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004951 unsigned int total_flags,
4952 unsigned int flags_or,
4953 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954{
4955 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004956 struct nl_msg *msg;
4957 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 struct nl80211_sta_flag_update upd;
4959
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004960 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4961 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4962 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4963 !!(total_flags & WPA_STA_AUTHORIZED));
4964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004965 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4966 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4967 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968
4969 /*
4970 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4971 * can be removed eventually.
4972 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004973 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004974 if (!flags ||
4975 ((total_flags & WPA_STA_AUTHORIZED) &&
4976 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4977 ((total_flags & WPA_STA_WMM) &&
4978 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4979 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4980 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4981 ((total_flags & WPA_STA_MFP) &&
4982 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4983 ((total_flags & WPA_STA_TDLS_PEER) &&
4984 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4985 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004986
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004987 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988
4989 os_memset(&upd, 0, sizeof(upd));
4990 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4991 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004992 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4993 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4996fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004997 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 return -ENOBUFS;
4999}
5000
5001
5002static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5003 struct wpa_driver_associate_params *params)
5004{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005005 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005006
5007 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5009 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005010 nlmode = NL80211_IFTYPE_P2P_GO;
5011 } else
5012 nlmode = NL80211_IFTYPE_AP;
5013
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005014 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005015 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005016 nl80211_remove_monitor_interface(drv);
5017 return -1;
5018 }
5019
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005020 if (params->freq.freq &&
5021 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005022 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005023 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024 nl80211_remove_monitor_interface(drv);
5025 return -1;
5026 }
5027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 return 0;
5029}
5030
5031
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005032static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5033 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034{
5035 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005036 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005038 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040 if (ret) {
5041 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5042 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005043 } else {
5044 wpa_printf(MSG_DEBUG,
5045 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 }
5047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005048 if (reset_mode &&
5049 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005050 NL80211_IFTYPE_STATION)) {
5051 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5052 "station mode");
5053 }
5054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 return ret;
5056}
5057
5058
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005059static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5060 struct wpa_driver_associate_params *params)
5061{
5062 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5063 return -1;
5064
5065 if (params->htcaps && params->htcaps_mask) {
5066 int sz = sizeof(struct ieee80211_ht_capabilities);
5067 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5068 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5069 params->htcaps_mask, sz);
5070 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5071 params->htcaps) ||
5072 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5073 params->htcaps_mask))
5074 return -1;
5075 }
5076
5077#ifdef CONFIG_VHT_OVERRIDES
5078 if (params->disable_vht) {
5079 wpa_printf(MSG_DEBUG, " * VHT disabled");
5080 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5081 return -1;
5082 }
5083
5084 if (params->vhtcaps && params->vhtcaps_mask) {
5085 int sz = sizeof(struct ieee80211_vht_capabilities);
5086 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5087 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5088 params->vhtcaps_mask, sz);
5089 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5090 params->vhtcaps) ||
5091 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5092 params->vhtcaps_mask))
5093 return -1;
5094 }
5095#endif /* CONFIG_VHT_OVERRIDES */
5096
5097 return 0;
5098}
5099
5100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5102 struct wpa_driver_associate_params *params)
5103{
5104 struct nl_msg *msg;
5105 int ret = -1;
5106 int count = 0;
5107
5108 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5109
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005110 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5112 "IBSS mode");
5113 return -1;
5114 }
5115
5116retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005117 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5118 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5119 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120
5121 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5122 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005123 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5124 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5126 drv->ssid_len = params->ssid_len;
5127
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005128 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5129 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005130 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131
5132 ret = nl80211_set_conn_keys(params, msg);
5133 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005134 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005136 if (params->bssid && params->fixed_bssid) {
5137 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5138 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005139 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5140 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005141 }
5142
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005143 if (params->fixed_freq) {
5144 wpa_printf(MSG_DEBUG, " * fixed_freq");
5145 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5146 goto fail;
5147 }
5148
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005149 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5150 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5151 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5152 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005153 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005154 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5155 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005156 }
5157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158 if (params->wpa_ie) {
5159 wpa_hexdump(MSG_DEBUG,
5160 " * Extra IEs for Beacon/Probe Response frames",
5161 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005162 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5163 params->wpa_ie))
5164 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165 }
5166
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005167 ret = nl80211_ht_vht_overrides(msg, params);
5168 if (ret < 0)
5169 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5172 msg = NULL;
5173 if (ret) {
5174 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5175 ret, strerror(-ret));
5176 count++;
5177 if (ret == -EALREADY && count == 1) {
5178 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5179 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181 nlmsg_free(msg);
5182 goto retry;
5183 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005184 } else {
5185 wpa_printf(MSG_DEBUG,
5186 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005189fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 nlmsg_free(msg);
5191 return ret;
5192}
5193
5194
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005195static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5196 struct wpa_driver_associate_params *params,
5197 struct nl_msg *msg)
5198{
5199 if (params->fils_erp_username_len) {
5200 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5201 params->fils_erp_username,
5202 params->fils_erp_username_len);
5203 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5204 params->fils_erp_username_len,
5205 params->fils_erp_username))
5206 return -1;
5207 }
5208
5209 if (params->fils_erp_realm_len) {
5210 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5211 params->fils_erp_realm,
5212 params->fils_erp_realm_len);
5213 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5214 params->fils_erp_realm_len, params->fils_erp_realm))
5215 return -1;
5216 }
5217
5218 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5219 params->fils_erp_next_seq_num);
5220 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5221 params->fils_erp_next_seq_num))
5222 return -1;
5223
5224 if (params->fils_erp_rrk_len) {
5225 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5226 (unsigned long) params->fils_erp_rrk_len);
5227 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5228 params->fils_erp_rrk_len, params->fils_erp_rrk))
5229 return -1;
5230 }
5231
5232 return 0;
5233}
5234
5235
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005236static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5237 struct wpa_driver_associate_params *params,
5238 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239{
Paul Stewart092955c2017-02-06 09:13:09 -08005240 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5241 return -1;
5242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005243 if (params->bssid) {
5244 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5245 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005246 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5247 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005249
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005250 if (params->bssid_hint) {
5251 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5252 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005253 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5254 params->bssid_hint))
5255 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005256 }
5257
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005258 if (params->freq.freq) {
5259 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005260 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5261 params->freq.freq))
5262 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005263 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005264 } else
5265 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005266
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005267 if (params->freq_hint) {
5268 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005269 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5270 params->freq_hint))
5271 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005272 }
5273
Dmitry Shmidt04949592012-07-19 12:16:46 -07005274 if (params->bg_scan_period >= 0) {
5275 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5276 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005277 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5278 params->bg_scan_period))
5279 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005280 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005282 if (params->ssid) {
5283 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5284 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005285 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5286 params->ssid))
5287 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005289 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5291 drv->ssid_len = params->ssid_len;
5292 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005294 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005295 if (params->wpa_ie &&
5296 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5297 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005299 if (params->wpa_proto) {
5300 enum nl80211_wpa_versions ver = 0;
5301
5302 if (params->wpa_proto & WPA_PROTO_WPA)
5303 ver |= NL80211_WPA_VERSION_1;
5304 if (params->wpa_proto & WPA_PROTO_RSN)
5305 ver |= NL80211_WPA_VERSION_2;
5306
5307 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005308 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5309 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005310 }
5311
5312 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5313 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5314 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005315 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5316 cipher))
5317 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005318 }
5319
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005320 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5321 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5322 /*
5323 * This is likely to work even though many drivers do not
5324 * advertise support for operations without GTK.
5325 */
5326 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5327 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005328 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5329 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005330 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5331 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005332 }
5333
5334 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5335 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5336 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5337 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005338 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005339 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5340 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005341 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005342 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005343 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5344 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5345 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5346 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07005347 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5348 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5349 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
Paul Stewart092955c2017-02-06 09:13:09 -08005350 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005351
5352 switch (params->key_mgmt_suite) {
5353 case WPA_KEY_MGMT_CCKM:
Paul Stewart092955c2017-02-06 09:13:09 -08005354 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005355 break;
5356 case WPA_KEY_MGMT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005357 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005358 break;
5359 case WPA_KEY_MGMT_FT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005360 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005361 break;
5362 case WPA_KEY_MGMT_FT_PSK:
Paul Stewart092955c2017-02-06 09:13:09 -08005363 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005364 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005365 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005366 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005367 break;
5368 case WPA_KEY_MGMT_PSK_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005369 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005370 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005371 case WPA_KEY_MGMT_OSEN:
Paul Stewart092955c2017-02-06 09:13:09 -08005372 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005373 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005374 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Paul Stewart092955c2017-02-06 09:13:09 -08005375 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005376 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005377 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Paul Stewart092955c2017-02-06 09:13:09 -08005378 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005379 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005380 case WPA_KEY_MGMT_FILS_SHA256:
5381 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5382 break;
5383 case WPA_KEY_MGMT_FILS_SHA384:
5384 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5385 break;
5386 case WPA_KEY_MGMT_FT_FILS_SHA256:
5387 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5388 break;
5389 case WPA_KEY_MGMT_FT_FILS_SHA384:
5390 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5391 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005392 case WPA_KEY_MGMT_OWE:
5393 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5394 break;
5395 case WPA_KEY_MGMT_DPP:
5396 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5397 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005398 case WPA_KEY_MGMT_PSK:
5399 default:
Paul Stewart092955c2017-02-06 09:13:09 -08005400 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005401 break;
5402 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005403 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005404 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5405 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005406 }
5407
Roshan Pius3a1667e2018-07-03 15:17:14 -07005408 /* Add PSK in case of 4-way handshake offload */
5409 if (params->psk &&
5410 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)) {
5411 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5412 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5413 return -1;
5414 }
5415
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005416 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5417 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005418
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005419 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5420 (params->pairwise_suite == WPA_CIPHER_NONE ||
5421 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5422 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5423 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5424 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5425 return -1;
5426
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005427 if (params->rrm_used) {
5428 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005429 if ((!((drv_rrm_flags &
5430 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5431 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5432 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005433 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5434 return -1;
5435 }
5436
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005437 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005438 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005439
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005440 if (params->p2p)
5441 wpa_printf(MSG_DEBUG, " * P2P group");
5442
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005443 if (params->pbss) {
5444 wpa_printf(MSG_DEBUG, " * PBSS");
5445 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5446 return -1;
5447 }
5448
Dmitry Shmidte4663042016-04-04 10:07:49 -07005449 drv->connect_reassoc = 0;
5450 if (params->prev_bssid) {
5451 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5452 MAC2STR(params->prev_bssid));
5453 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5454 params->prev_bssid))
5455 return -1;
5456 drv->connect_reassoc = 1;
5457 }
5458
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005459 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5460 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5461 return -1;
5462
Roshan Pius3a1667e2018-07-03 15:17:14 -07005463 if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5464 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5465 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5466 return -1;
5467
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005468 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005469}
5470
5471
5472static int wpa_driver_nl80211_try_connect(
5473 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005474 struct wpa_driver_associate_params *params,
5475 struct nl_handle *nl_connect)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005476{
5477 struct nl_msg *msg;
5478 enum nl80211_auth_type type;
5479 int ret;
5480 int algs;
5481
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005482#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005483 if (params->req_key_mgmt_offload && params->psk &&
5484 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5485 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5486 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5487 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5488 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5489 if (ret)
5490 return ret;
5491 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005492#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005493
5494 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5495 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005496 if (!msg)
5497 return -1;
5498
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005499 ret = nl80211_connect_common(drv, params, msg);
5500 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005501 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005502
Roshan Pius3a1667e2018-07-03 15:17:14 -07005503 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5504 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5505 goto fail;
5506
5507 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
5508 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
5509 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
5510 goto fail;
5511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512 algs = 0;
5513 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5514 algs++;
5515 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5516 algs++;
5517 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5518 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005519 if (params->auth_alg & WPA_AUTH_ALG_FILS)
5520 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005521 if (params->auth_alg & WPA_AUTH_ALG_FT)
5522 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523 if (algs > 1) {
5524 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5525 "selection");
5526 goto skip_auth_type;
5527 }
5528
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005529 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005530 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005531 if (type == NL80211_AUTHTYPE_MAX ||
5532 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005533 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534
5535skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536 ret = nl80211_set_conn_keys(params, msg);
5537 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005538 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539
Roshan Pius3a1667e2018-07-03 15:17:14 -07005540 if (nl_connect)
5541 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
5542 else
5543 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545 msg = NULL;
5546 if (ret) {
5547 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5548 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005549 } else {
5550 wpa_printf(MSG_DEBUG,
5551 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005554fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005555 nlmsg_free(msg);
5556 return ret;
5557
5558}
5559
5560
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005561static int wpa_driver_nl80211_connect(
5562 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005563 struct wpa_driver_associate_params *params,
5564 struct nl_handle *nl_connect)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005565{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305566 int ret;
5567
5568 /* Store the connection attempted bssid for future use */
5569 if (params->bssid)
5570 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5571 else
5572 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5573
Roshan Pius3a1667e2018-07-03 15:17:14 -07005574 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005575 if (ret == -EALREADY) {
5576 /*
5577 * cfg80211 does not currently accept new connections if
5578 * we are already connected. As a workaround, force
5579 * disconnection and try again.
5580 */
5581 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5582 "disconnecting before reassociation "
5583 "attempt");
5584 if (wpa_driver_nl80211_disconnect(
5585 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5586 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005587 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005588 }
5589 return ret;
5590}
5591
5592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593static int wpa_driver_nl80211_associate(
5594 void *priv, struct wpa_driver_associate_params *params)
5595{
5596 struct i802_bss *bss = priv;
5597 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005598 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599 struct nl_msg *msg;
5600
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005601 nl80211_unmask_11b_rates(bss);
5602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 if (params->mode == IEEE80211_MODE_AP)
5604 return wpa_driver_nl80211_ap(drv, params);
5605
5606 if (params->mode == IEEE80211_MODE_IBSS)
5607 return wpa_driver_nl80211_ibss(drv, params);
5608
5609 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005610 enum nl80211_iftype nlmode = params->p2p ?
5611 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005612 struct nl_handle *nl_connect = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005613
5614 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005616 if (params->auth_alg & WPA_AUTH_ALG_SAE)
5617 nl_connect = bss->nl_connect;
5618 return wpa_driver_nl80211_connect(drv, params, nl_connect);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 }
5620
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005621 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005623 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5624 drv->ifindex);
5625 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626 if (!msg)
5627 return -1;
5628
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005629 ret = nl80211_connect_common(drv, params, msg);
5630 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005631 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632
Roshan Pius3a1667e2018-07-03 15:17:14 -07005633 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5634 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5635 goto fail;
5636
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005637 if (params->fils_kek) {
5638 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5639 (unsigned int) params->fils_kek_len);
5640 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5641 params->fils_kek))
5642 goto fail;
5643 }
5644 if (params->fils_nonces) {
5645 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5646 params->fils_nonces,
5647 params->fils_nonces_len);
5648 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5649 params->fils_nonces_len, params->fils_nonces))
5650 goto fail;
5651 }
5652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5654 msg = NULL;
5655 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005656 wpa_dbg(drv->ctx, MSG_DEBUG,
5657 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5658 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005660 } else {
5661 wpa_printf(MSG_DEBUG,
5662 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005664
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005665fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666 nlmsg_free(msg);
5667 return ret;
5668}
5669
5670
5671static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005672 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005673{
5674 struct nl_msg *msg;
5675 int ret = -ENOBUFS;
5676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005677 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5678 ifindex, mode, nl80211_iftype_str(mode));
5679
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005680 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5681 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5682 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683
5684 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005685 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 if (!ret)
5687 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005688fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005689 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5691 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5692 return ret;
5693}
5694
5695
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005696static int wpa_driver_nl80211_set_mode_impl(
5697 struct i802_bss *bss,
5698 enum nl80211_iftype nlmode,
5699 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701 struct wpa_driver_nl80211_data *drv = bss->drv;
5702 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005704 int was_ap = is_ap_interface(drv->nlmode);
5705 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005706 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005708 if (TEST_FAIL())
5709 return -1;
5710
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005711 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5712 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5713 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005714
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005715 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 drv->nlmode = nlmode;
5717 ret = 0;
5718 goto done;
5719 }
5720
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005721 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005722 return -1;
5723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 if (nlmode == drv->nlmode) {
5725 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5726 "requested mode - ignore error");
5727 ret = 0;
5728 goto done; /* Already in the requested mode */
5729 }
5730
5731 /* mac80211 doesn't allow mode changes while the device is up, so
5732 * take the device down, try to set the mode again, and bring the
5733 * device back up.
5734 */
5735 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5736 "interface down");
5737 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005738 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005739 if (res == -EACCES || res == -ENODEV)
5740 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005741 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5743 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005744 os_sleep(0, 100000);
5745 continue;
5746 }
5747
5748 /*
5749 * Setting the mode will fail for some drivers if the phy is
5750 * on a frequency that the mode is disallowed in.
5751 */
5752 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005753 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005754 if (res) {
5755 wpa_printf(MSG_DEBUG,
5756 "nl80211: Failed to set frequency on interface");
5757 }
5758 }
5759
5760 /* Try to set the mode again while the interface is down */
5761 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5762 if (mode_switch_res == -EBUSY) {
5763 wpa_printf(MSG_DEBUG,
5764 "nl80211: Delaying mode set while interface going down");
5765 os_sleep(0, 100000);
5766 continue;
5767 }
5768 ret = mode_switch_res;
5769 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 }
5771
5772 if (!ret) {
5773 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5774 "interface is down");
5775 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005776 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005777 }
5778
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005779 /* Bring the interface back up */
5780 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5781 if (res != 0) {
5782 wpa_printf(MSG_DEBUG,
5783 "nl80211: Failed to set interface up after switching mode");
5784 ret = -1;
5785 }
5786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005788 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005789 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5790 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005791 return ret;
5792 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005794 if (is_p2p_net_interface(nlmode)) {
5795 wpa_printf(MSG_DEBUG,
5796 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5797 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005798 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005799 } else if (drv->disabled_11b_rates) {
5800 wpa_printf(MSG_DEBUG,
5801 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5802 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005803 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005804 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005805
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005806 if (is_ap_interface(nlmode)) {
5807 nl80211_mgmt_unsubscribe(bss, "start AP");
5808 /* Setup additional AP mode functionality if needed */
5809 if (nl80211_setup_ap(bss))
5810 return -1;
5811 } else if (was_ap) {
5812 /* Remove additional AP mode functionality */
5813 nl80211_teardown_ap(bss);
5814 } else {
5815 nl80211_mgmt_unsubscribe(bss, "mode change");
5816 }
5817
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005818 if (is_mesh_interface(nlmode) &&
5819 nl80211_mgmt_subscribe_mesh(bss))
5820 return -1;
5821
Dmitry Shmidt04949592012-07-19 12:16:46 -07005822 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005823 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5825 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5826 "frame processing - ignore for now");
5827
5828 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829}
5830
5831
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005832int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5833 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005834{
5835 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5836}
5837
5838
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005839static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5840 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005841{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005842 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005843 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005844}
5845
5846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847static int wpa_driver_nl80211_get_capa(void *priv,
5848 struct wpa_driver_capa *capa)
5849{
5850 struct i802_bss *bss = priv;
5851 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 if (!drv->has_capability)
5854 return -1;
5855 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005856 if (drv->extended_capa && drv->extended_capa_mask) {
5857 capa->extended_capa = drv->extended_capa;
5858 capa->extended_capa_mask = drv->extended_capa_mask;
5859 capa->extended_capa_len = drv->extended_capa_len;
5860 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005861
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005862 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863}
5864
5865
5866static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5867{
5868 struct i802_bss *bss = priv;
5869 struct wpa_driver_nl80211_data *drv = bss->drv;
5870
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005871 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5872 bss->ifname, drv->operstate, state,
5873 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005875 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 state ? IF_OPER_UP : IF_OPER_DORMANT);
5877}
5878
5879
5880static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5881{
5882 struct i802_bss *bss = priv;
5883 struct wpa_driver_nl80211_data *drv = bss->drv;
5884 struct nl_msg *msg;
5885 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005886 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005887
5888 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5889 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5890 return 0;
5891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005893 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5894 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005896 os_memset(&upd, 0, sizeof(upd));
5897 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5898 if (authorized)
5899 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005900
5901 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5902 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5903 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5904 nlmsg_free(msg);
5905 return -ENOBUFS;
5906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005908 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005909 if (!ret)
5910 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005911 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5912 ret, strerror(-ret));
5913 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914}
5915
5916
Jouni Malinen75ecf522011-06-27 15:19:46 -07005917/* Set kernel driver on given frequency (MHz) */
5918static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005920 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005921 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922}
5923
5924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925static inline int min_int(int a, int b)
5926{
5927 if (a < b)
5928 return a;
5929 return b;
5930}
5931
5932
5933static int get_key_handler(struct nl_msg *msg, void *arg)
5934{
5935 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5936 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5937
5938 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5939 genlmsg_attrlen(gnlh, 0), NULL);
5940
5941 /*
5942 * TODO: validate the key index and mac address!
5943 * Otherwise, there's a race condition as soon as
5944 * the kernel starts sending key notifications.
5945 */
5946
5947 if (tb[NL80211_ATTR_KEY_SEQ])
5948 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5949 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5950 return NL_SKIP;
5951}
5952
5953
5954static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5955 int idx, u8 *seq)
5956{
5957 struct i802_bss *bss = priv;
5958 struct wpa_driver_nl80211_data *drv = bss->drv;
5959 struct nl_msg *msg;
5960
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005961 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5962 NL80211_CMD_GET_KEY);
5963 if (!msg ||
5964 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5965 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5966 nlmsg_free(msg);
5967 return -ENOBUFS;
5968 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969
5970 memset(seq, 0, 6);
5971
5972 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005973}
5974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975
5976static int i802_set_rts(void *priv, int rts)
5977{
5978 struct i802_bss *bss = priv;
5979 struct wpa_driver_nl80211_data *drv = bss->drv;
5980 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005981 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 u32 val;
5983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 if (rts >= 2347)
5985 val = (u32) -1;
5986 else
5987 val = rts;
5988
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005989 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5990 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5991 nlmsg_free(msg);
5992 return -ENOBUFS;
5993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994
5995 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5996 if (!ret)
5997 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005998 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5999 "%d (%s)", rts, ret, strerror(-ret));
6000 return ret;
6001}
6002
6003
6004static int i802_set_frag(void *priv, int frag)
6005{
6006 struct i802_bss *bss = priv;
6007 struct wpa_driver_nl80211_data *drv = bss->drv;
6008 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006009 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 u32 val;
6011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012 if (frag >= 2346)
6013 val = (u32) -1;
6014 else
6015 val = frag;
6016
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006017 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6018 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6019 nlmsg_free(msg);
6020 return -ENOBUFS;
6021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022
6023 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6024 if (!ret)
6025 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6027 "%d: %d (%s)", frag, ret, strerror(-ret));
6028 return ret;
6029}
6030
6031
6032static int i802_flush(void *priv)
6033{
6034 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006035 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006036 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006038 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6039 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040
6041 /*
6042 * XXX: FIX! this needs to flush all VLANs too
6043 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006044 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6045 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006046 if (res) {
6047 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6048 "(%s)", res, strerror(-res));
6049 }
6050 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006051}
6052
6053
6054static int get_sta_handler(struct nl_msg *msg, void *arg)
6055{
6056 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6057 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6058 struct hostap_sta_driver_data *data = arg;
6059 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6060 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6061 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6062 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6063 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6064 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6065 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006066 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006067 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6068 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006069 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07006070 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006071 };
6072 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6073 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6074 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6075 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6076 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6077 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6078 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6079 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006080 };
6081
6082 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6083 genlmsg_attrlen(gnlh, 0), NULL);
6084
6085 /*
6086 * TODO: validate the interface and mac address!
6087 * Otherwise, there's a race condition as soon as
6088 * the kernel starts sending station notifications.
6089 */
6090
6091 if (!tb[NL80211_ATTR_STA_INFO]) {
6092 wpa_printf(MSG_DEBUG, "sta stats missing!");
6093 return NL_SKIP;
6094 }
6095 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6096 tb[NL80211_ATTR_STA_INFO],
6097 stats_policy)) {
6098 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6099 return NL_SKIP;
6100 }
6101
6102 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6103 data->inactive_msec =
6104 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006105 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106 if (stats[NL80211_STA_INFO_RX_BYTES])
6107 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6108 if (stats[NL80211_STA_INFO_TX_BYTES])
6109 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006110 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6111 stats[NL80211_STA_INFO_TX_BYTES64]) {
6112 /*
6113 * The driver supports 64-bit counters, so use them to override
6114 * the 32-bit values.
6115 */
6116 data->rx_bytes =
6117 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6118 data->tx_bytes =
6119 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6120 data->bytes_64bit = 1;
6121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122 if (stats[NL80211_STA_INFO_RX_PACKETS])
6123 data->rx_packets =
6124 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6125 if (stats[NL80211_STA_INFO_TX_PACKETS])
6126 data->tx_packets =
6127 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006128 if (stats[NL80211_STA_INFO_TX_FAILED])
6129 data->tx_retry_failed =
6130 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006131 if (stats[NL80211_STA_INFO_SIGNAL])
6132 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006133 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6134 data->last_ack_rssi =
6135 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6136 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6137 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006138
6139 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6140 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6141 stats[NL80211_STA_INFO_TX_BITRATE],
6142 rate_policy) == 0) {
6143 if (rate[NL80211_RATE_INFO_BITRATE32])
6144 data->current_tx_rate =
6145 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6146 else if (rate[NL80211_RATE_INFO_BITRATE])
6147 data->current_tx_rate =
6148 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6149
6150 if (rate[NL80211_RATE_INFO_MCS]) {
6151 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6152 data->flags |= STA_DRV_DATA_TX_MCS;
6153 }
6154 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6155 data->tx_vhtmcs =
6156 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6157 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6158 }
6159 if (rate[NL80211_RATE_INFO_SHORT_GI])
6160 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6161 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6162 data->tx_vht_nss =
6163 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6164 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6165 }
6166 }
6167
6168 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6169 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6170 stats[NL80211_STA_INFO_RX_BITRATE],
6171 rate_policy) == 0) {
6172 if (rate[NL80211_RATE_INFO_BITRATE32])
6173 data->current_rx_rate =
6174 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6175 else if (rate[NL80211_RATE_INFO_BITRATE])
6176 data->current_rx_rate =
6177 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6178
6179 if (rate[NL80211_RATE_INFO_MCS]) {
6180 data->rx_mcs =
6181 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6182 data->flags |= STA_DRV_DATA_RX_MCS;
6183 }
6184 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6185 data->rx_vhtmcs =
6186 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6187 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6188 }
6189 if (rate[NL80211_RATE_INFO_SHORT_GI])
6190 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6191 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6192 data->rx_vht_nss =
6193 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6194 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6195 }
6196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197
6198 return NL_SKIP;
6199}
6200
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006201static int i802_read_sta_data(struct i802_bss *bss,
6202 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203 const u8 *addr)
6204{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205 struct nl_msg *msg;
6206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006207 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6208 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6209 nlmsg_free(msg);
6210 return -ENOBUFS;
6211 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006213 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214}
6215
6216
6217static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6218 int cw_min, int cw_max, int burst_time)
6219{
6220 struct i802_bss *bss = priv;
6221 struct wpa_driver_nl80211_data *drv = bss->drv;
6222 struct nl_msg *msg;
6223 struct nlattr *txq, *params;
6224
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006225 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006226 if (!msg)
6227 return -1;
6228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6230 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006231 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006232
6233 /* We are only sending parameters for a single TXQ at a time */
6234 params = nla_nest_start(msg, 1);
6235 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006236 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237
6238 switch (queue) {
6239 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006240 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6241 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242 break;
6243 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006244 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6245 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246 break;
6247 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006248 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6249 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250 break;
6251 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006252 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6253 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006254 break;
6255 }
6256 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6257 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006258 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6259 (burst_time * 100 + 16) / 32) ||
6260 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6261 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6262 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6263 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006264
6265 nla_nest_end(msg, params);
6266
6267 nla_nest_end(msg, txq);
6268
6269 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6270 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006271 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006272fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006273 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006274 return -1;
6275}
6276
6277
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006278static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279 const char *ifname, int vlan_id)
6280{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281 struct wpa_driver_nl80211_data *drv = bss->drv;
6282 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006283 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006284
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006285 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6286 ", ifname=%s[%d], vlan_id=%d)",
6287 bss->ifname, if_nametoindex(bss->ifname),
6288 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006289 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6290 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6291 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6292 nlmsg_free(msg);
6293 return -ENOBUFS;
6294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006295
6296 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6297 if (ret < 0) {
6298 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6299 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6300 MAC2STR(addr), ifname, vlan_id, ret,
6301 strerror(-ret));
6302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303 return ret;
6304}
6305
6306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006307static int i802_get_inact_sec(void *priv, const u8 *addr)
6308{
6309 struct hostap_sta_driver_data data;
6310 int ret;
6311
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006312 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313 data.inactive_msec = (unsigned long) -1;
6314 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006315 if (ret == -ENOENT)
6316 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317 if (ret || data.inactive_msec == (unsigned long) -1)
6318 return -1;
6319 return data.inactive_msec / 1000;
6320}
6321
6322
6323static int i802_sta_clear_stats(void *priv, const u8 *addr)
6324{
6325#if 0
6326 /* TODO */
6327#endif
6328 return 0;
6329}
6330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006331
6332static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6333 int reason)
6334{
6335 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006336 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006337 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006338 u8 channel;
6339
6340 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6341 HOSTAPD_MODE_IEEE80211AD) {
6342 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6343 * disassociate the STA instead. */
6344 return i802_sta_disassoc(priv, own_addr, addr, reason);
6345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006346
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006347 if (is_mesh_interface(drv->nlmode))
6348 return -1;
6349
Dmitry Shmidt04949592012-07-19 12:16:46 -07006350 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006351 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353 memset(&mgmt, 0, sizeof(mgmt));
6354 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6355 WLAN_FC_STYPE_DEAUTH);
6356 memcpy(mgmt.da, addr, ETH_ALEN);
6357 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6358 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6359 mgmt.u.deauth.reason_code = host_to_le16(reason);
6360 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6361 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006362 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006363 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364}
6365
6366
6367static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6368 int reason)
6369{
6370 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006371 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006372 struct ieee80211_mgmt mgmt;
6373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006374 if (is_mesh_interface(drv->nlmode))
6375 return -1;
6376
Dmitry Shmidt04949592012-07-19 12:16:46 -07006377 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006378 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380 memset(&mgmt, 0, sizeof(mgmt));
6381 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6382 WLAN_FC_STYPE_DISASSOC);
6383 memcpy(mgmt.da, addr, ETH_ALEN);
6384 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6385 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6386 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6387 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6388 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006389 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006390 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006391}
6392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006394static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6395{
6396 char buf[200], *pos, *end;
6397 int i, res;
6398
6399 pos = buf;
6400 end = pos + sizeof(buf);
6401
6402 for (i = 0; i < drv->num_if_indices; i++) {
6403 if (!drv->if_indices[i])
6404 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006405 res = os_snprintf(pos, end - pos, " %d(%d)",
6406 drv->if_indices[i],
6407 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006408 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006409 break;
6410 pos += res;
6411 }
6412 *pos = '\0';
6413
6414 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6415 drv->num_if_indices, buf);
6416}
6417
6418
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006419static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6420 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006421{
6422 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006423 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006424
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006425 wpa_printf(MSG_DEBUG,
6426 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6427 ifidx, ifidx_reason);
6428 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006429 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6430 ifidx);
6431 return;
6432 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006433 for (i = 0; i < drv->num_if_indices; i++) {
6434 if (drv->if_indices[i] == 0) {
6435 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006436 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006437 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006438 return;
6439 }
6440 }
6441
6442 if (drv->if_indices != drv->default_if_indices)
6443 old = drv->if_indices;
6444 else
6445 old = NULL;
6446
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006447 if (drv->if_indices_reason != drv->default_if_indices_reason)
6448 old_reason = drv->if_indices_reason;
6449 else
6450 old_reason = NULL;
6451
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006452 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6453 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006454 drv->if_indices_reason = os_realloc_array(old_reason,
6455 drv->num_if_indices + 1,
6456 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006457 if (!drv->if_indices) {
6458 if (!old)
6459 drv->if_indices = drv->default_if_indices;
6460 else
6461 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006462 }
6463 if (!drv->if_indices_reason) {
6464 if (!old_reason)
6465 drv->if_indices_reason = drv->default_if_indices_reason;
6466 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07006467 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006468 }
6469 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006470 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6471 "interfaces");
6472 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6473 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006474 }
6475 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006476 os_memcpy(drv->if_indices, drv->default_if_indices,
6477 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006478 if (!old_reason)
6479 os_memcpy(drv->if_indices_reason,
6480 drv->default_if_indices_reason,
6481 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006482 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006483 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006484 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006485 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006486}
6487
6488
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006489static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6490 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006491{
6492 int i;
6493
6494 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006495 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6496 (drv->if_indices_reason[i] == ifidx_reason ||
6497 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006498 drv->if_indices[i] = 0;
6499 break;
6500 }
6501 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006502 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006503}
6504
6505
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006506static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6507 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006508{
6509 int i;
6510
6511 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006512 if (drv->if_indices[i] == ifidx &&
6513 (drv->if_indices_reason[i] == ifidx_reason ||
6514 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006515 return 1;
6516
6517 return 0;
6518}
6519
6520
6521static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006522 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006523{
6524 struct i802_bss *bss = priv;
6525 struct wpa_driver_nl80211_data *drv = bss->drv;
6526 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07006527 union wpa_event_data event;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006528
6529 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006530 if (ifname_wds)
6531 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6532
Jouni Malinen75ecf522011-06-27 15:19:46 -07006533 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6534 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6535 if (val) {
6536 if (!if_nametoindex(name)) {
6537 if (nl80211_create_iface(drv, name,
6538 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006539 bss->addr, 1, NULL, NULL, 0) <
6540 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006541 return -1;
6542 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006543 linux_br_add_if(drv->global->ioctl_sock,
6544 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006545 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006546
6547 os_memset(&event, 0, sizeof(event));
6548 event.wds_sta_interface.sta_addr = addr;
6549 event.wds_sta_interface.ifname = name;
6550 event.wds_sta_interface.istatus = INTERFACE_ADDED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07006551 wpa_supplicant_event(bss->ctx,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006552 EVENT_WDS_STA_INTERFACE_STATUS,
6553 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006554 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006555 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6556 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6557 "interface %s up", name);
6558 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006559 return i802_set_sta_vlan(priv, addr, name, 0);
6560 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006561 if (bridge_ifname)
6562 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6563 name);
6564
Jouni Malinen75ecf522011-06-27 15:19:46 -07006565 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006566 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006567 os_memset(&event, 0, sizeof(event));
6568 event.wds_sta_interface.sta_addr = addr;
6569 event.wds_sta_interface.ifname = name;
6570 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
Hai Shalomce48b4a2018-09-05 11:41:35 -07006571 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006572 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006573 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006574 }
6575}
6576
6577
6578static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6579{
6580 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6581 struct sockaddr_ll lladdr;
6582 unsigned char buf[3000];
6583 int len;
6584 socklen_t fromlen = sizeof(lladdr);
6585
6586 len = recvfrom(sock, buf, sizeof(buf), 0,
6587 (struct sockaddr *)&lladdr, &fromlen);
6588 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006589 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6590 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006591 return;
6592 }
6593
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006594 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006595 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6596}
6597
6598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006599static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6600 struct i802_bss *bss,
6601 const char *brname, const char *ifname)
6602{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006603 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604 char in_br[IFNAMSIZ];
6605
6606 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006607 br_ifindex = if_nametoindex(brname);
6608 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006609 /*
6610 * Bridge was configured, but the bridge device does
6611 * not exist. Try to add it now.
6612 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006613 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006614 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6615 "bridge interface %s: %s",
6616 brname, strerror(errno));
6617 return -1;
6618 }
6619 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006620 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006621 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006622 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006623 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006624
6625 if (linux_br_get(in_br, ifname) == 0) {
Hai Shalomc9e41a12018-07-31 14:41:42 -07006626 if (os_strcmp(in_br, brname) == 0) {
6627 bss->already_in_bridge = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628 return 0; /* already in the bridge */
Hai Shalomc9e41a12018-07-31 14:41:42 -07006629 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006630
6631 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6632 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006633 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6634 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6636 "remove interface %s from bridge "
6637 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006638 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006639 return -1;
6640 }
6641 }
6642
6643 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6644 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006645 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006646 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6647 "into bridge %s: %s",
6648 ifname, brname, strerror(errno));
6649 return -1;
6650 }
6651 bss->added_if_into_bridge = 1;
6652
6653 return 0;
6654}
6655
6656
6657static void *i802_init(struct hostapd_data *hapd,
6658 struct wpa_init_params *params)
6659{
6660 struct wpa_driver_nl80211_data *drv;
6661 struct i802_bss *bss;
6662 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006663 char master_ifname[IFNAMSIZ];
6664 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006665 int br_added = 0;
6666
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006667 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6668 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006669 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006670 if (bss == NULL)
6671 return NULL;
6672
6673 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006674
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006675 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006677 params->ifname, master_ifname);
6678 br_ifindex = if_nametoindex(master_ifname);
6679 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6680 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6681 linux_master_get(master_ifname, params->ifname) == 0) {
6682 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6683 params->ifname, master_ifname);
6684 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006685 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006686
6687 /* check if master itself is under bridge */
6688 if (linux_br_get(master_ifname, master_ifname) == 0) {
6689 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6690 master_ifname);
6691 br_ifindex = if_nametoindex(master_ifname);
6692 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006695 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006696 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006697
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006698 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006700 for (i = 0; i < params->num_bridge; i++) {
6701 if (params->bridge[i]) {
6702 ifindex = if_nametoindex(params->bridge[i]);
6703 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006704 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006705 if (ifindex == br_ifindex)
6706 br_added = 1;
6707 }
6708 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006709
6710 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006711 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006712
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006713 if (params->num_bridge && params->bridge[0]) {
6714 if (i802_check_bridge(drv, bss, params->bridge[0],
6715 params->ifname) < 0)
6716 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006717 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006718 br_added = 1;
6719 }
6720
6721 if (!br_added && br_ifindex &&
6722 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006723 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006724
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006725#ifdef CONFIG_LIBNL3_ROUTE
Hai Shalomc9e41a12018-07-31 14:41:42 -07006726 if (bss->added_if_into_bridge || bss->already_in_bridge) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006727 drv->rtnl_sk = nl_socket_alloc();
6728 if (drv->rtnl_sk == NULL) {
6729 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6730 goto failed;
6731 }
6732
6733 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6734 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6735 strerror(errno));
6736 goto failed;
6737 }
6738 }
6739#endif /* CONFIG_LIBNL3_ROUTE */
6740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006741 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6742 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006743 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6744 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006745 goto failed;
6746 }
6747
6748 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6749 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006750 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751 goto failed;
6752 }
6753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006754 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6755 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006757 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006759 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006761 return bss;
6762
6763failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006764 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006765 return NULL;
6766}
6767
6768
6769static void i802_deinit(void *priv)
6770{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006771 struct i802_bss *bss = priv;
6772 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006773}
6774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006775
6776static enum nl80211_iftype wpa_driver_nl80211_if_type(
6777 enum wpa_driver_if_type type)
6778{
6779 switch (type) {
6780 case WPA_IF_STATION:
6781 return NL80211_IFTYPE_STATION;
6782 case WPA_IF_P2P_CLIENT:
6783 case WPA_IF_P2P_GROUP:
6784 return NL80211_IFTYPE_P2P_CLIENT;
6785 case WPA_IF_AP_VLAN:
6786 return NL80211_IFTYPE_AP_VLAN;
6787 case WPA_IF_AP_BSS:
6788 return NL80211_IFTYPE_AP;
6789 case WPA_IF_P2P_GO:
6790 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006791 case WPA_IF_P2P_DEVICE:
6792 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006793 case WPA_IF_MESH:
6794 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006795 default:
6796 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006798}
6799
6800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6802{
6803 struct wpa_driver_nl80211_data *drv;
6804 dl_list_for_each(drv, &global->interfaces,
6805 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006806 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006807 return 1;
6808 }
6809 return 0;
6810}
6811
6812
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006813static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006814{
6815 unsigned int idx;
6816
6817 if (!drv->global)
6818 return -1;
6819
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006820 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006821 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006822 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823 new_addr[0] ^= idx << 2;
6824 if (!nl80211_addr_in_use(drv->global, new_addr))
6825 break;
6826 }
6827 if (idx == 64)
6828 return -1;
6829
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006830 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006831 MACSTR, MAC2STR(new_addr));
6832
6833 return 0;
6834}
6835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006837struct wdev_info {
6838 u64 wdev_id;
6839 int wdev_id_set;
6840 u8 macaddr[ETH_ALEN];
6841};
6842
6843static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6844{
6845 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6846 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6847 struct wdev_info *wi = arg;
6848
6849 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6850 genlmsg_attrlen(gnlh, 0), NULL);
6851 if (tb[NL80211_ATTR_WDEV]) {
6852 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6853 wi->wdev_id_set = 1;
6854 }
6855
6856 if (tb[NL80211_ATTR_MAC])
6857 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6858 ETH_ALEN);
6859
6860 return NL_SKIP;
6861}
6862
6863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006864static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6865 const char *ifname, const u8 *addr,
6866 void *bss_ctx, void **drv_priv,
6867 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006868 const char *bridge, int use_existing,
6869 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006870{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006871 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 struct i802_bss *bss = priv;
6873 struct wpa_driver_nl80211_data *drv = bss->drv;
6874 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006875 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876
6877 if (addr)
6878 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006879 nlmode = wpa_driver_nl80211_if_type(type);
6880 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6881 struct wdev_info p2pdev_info;
6882
6883 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6884 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6885 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006886 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006887 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6888 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6889 ifname);
6890 return -1;
6891 }
6892
6893 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6894 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6895 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6896 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6897 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6898 ifname,
6899 (long long unsigned int) p2pdev_info.wdev_id);
6900 } else {
6901 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006902 0, NULL, NULL, use_existing);
6903 if (use_existing && ifidx == -ENFILE) {
6904 added = 0;
6905 ifidx = if_nametoindex(ifname);
6906 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006907 return -1;
6908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006909 }
6910
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006911 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006912 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006913 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6914 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006915 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006916 if (added)
6917 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006918 return -1;
6919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006920 }
6921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922 if (!addr &&
6923 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006924 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6925 type == WPA_IF_STATION)) {
6926 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006927 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006929 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006930 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006931 if (added)
6932 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933 return -1;
6934 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006935 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006937 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006938 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006939 if (added)
6940 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 return -1;
6942 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006943 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006944 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006945 if (added)
6946 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006947 return -1;
6948 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006950 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006953 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006954 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6955 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006956 if (added)
6957 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006958 return -1;
6959 }
6960
6961 if (bridge &&
6962 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6963 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6964 "interface %s to a bridge %s",
6965 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006966 if (added)
6967 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006968 os_free(new_bss);
6969 return -1;
6970 }
6971
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006972 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6973 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006974 if (added)
6975 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 os_free(new_bss);
6977 return -1;
6978 }
6979 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006980 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 new_bss->ifindex = ifidx;
6982 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006983 new_bss->next = drv->first_bss->next;
6984 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006985 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006986 new_bss->added_if = added;
6987 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988 if (drv_priv)
6989 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006991
6992 /* Subscribe management frames for this WPA_IF_AP_BSS */
6993 if (nl80211_setup_ap(new_bss))
6994 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006996
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006997 if (drv->global)
6998 drv->global->if_add_ifindex = ifidx;
6999
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007000 /*
7001 * Some virtual interfaces need to process EAPOL packets and events on
7002 * the parent interface. This is used mainly with hostapd.
7003 */
7004 if (ifidx > 0 &&
7005 (drv->hostapd ||
7006 nlmode == NL80211_IFTYPE_AP_VLAN ||
7007 nlmode == NL80211_IFTYPE_WDS ||
7008 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007009 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011 return 0;
7012}
7013
7014
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007015static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007016 enum wpa_driver_if_type type,
7017 const char *ifname)
7018{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007019 struct wpa_driver_nl80211_data *drv = bss->drv;
7020 int ifindex = if_nametoindex(ifname);
7021
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007022 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7023 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007024 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07007025 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007026 else if (ifindex > 0 && !bss->added_if) {
7027 struct wpa_driver_nl80211_data *drv2;
7028 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007029 struct wpa_driver_nl80211_data, list) {
7030 del_ifidx(drv2, ifindex, IFIDX_ANY);
7031 del_ifidx(drv2, IFIDX_ANY, ifindex);
7032 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007033 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007034
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007035 if (type != WPA_IF_AP_BSS)
7036 return 0;
7037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007039 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7040 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007041 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7042 "interface %s from bridge %s: %s",
7043 bss->ifname, bss->brname, strerror(errno));
7044 }
7045 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7048 "bridge %s: %s",
7049 bss->brname, strerror(errno));
7050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007051
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007052 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007053 struct i802_bss *tbss;
7054
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007055 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7056 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 if (tbss->next == bss) {
7058 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007059 /* Unsubscribe management frames */
7060 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007061 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007062 if (!bss->added_if)
7063 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007064 os_free(bss);
7065 bss = NULL;
7066 break;
7067 }
7068 }
7069 if (bss)
7070 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7071 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007072 } else {
7073 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7074 nl80211_teardown_ap(bss);
7075 if (!bss->added_if && !drv->first_bss->next)
Paul Stewart092955c2017-02-06 09:13:09 -08007076 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007077 nl80211_destroy_bss(bss);
7078 if (!bss->added_if)
7079 i802_set_iface_flags(bss, 0);
7080 if (drv->first_bss->next) {
7081 drv->first_bss = drv->first_bss->next;
7082 drv->ctx = drv->first_bss->ctx;
7083 os_free(bss);
7084 } else {
7085 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088
7089 return 0;
7090}
7091
7092
7093static int cookie_handler(struct nl_msg *msg, void *arg)
7094{
7095 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7096 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7097 u64 *cookie = arg;
7098 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7099 genlmsg_attrlen(gnlh, 0), NULL);
7100 if (tb[NL80211_ATTR_COOKIE])
7101 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7102 return NL_SKIP;
7103}
7104
7105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 unsigned int freq, unsigned int wait,
7108 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007109 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007110 int offchanok, const u16 *csa_offs,
7111 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007112{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007113 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007114 struct nl_msg *msg;
7115 u64 cookie;
7116 int ret = -1;
7117
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007118 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07007119 "no_ack=%d offchanok=%d",
7120 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007121 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007122
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007123 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7124 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7125 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7126 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7127 drv->test_use_roc_tx) &&
7128 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7129 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7130 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007131 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7132 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007133 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7134 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007135
7136 cookie = 0;
7137 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7138 msg = NULL;
7139 if (ret) {
7140 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007141 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7142 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007143 } else {
7144 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7145 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7146 (long long unsigned int) cookie);
7147
7148 if (cookie_out)
7149 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007150
7151 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7152 wpa_printf(MSG_DEBUG,
7153 "nl80211: Drop oldest pending send action cookie 0x%llx",
7154 (long long unsigned int)
7155 drv->send_action_cookies[0]);
7156 os_memmove(&drv->send_action_cookies[0],
7157 &drv->send_action_cookies[1],
7158 (MAX_SEND_ACTION_COOKIES - 1) *
7159 sizeof(u64));
7160 drv->num_send_action_cookies--;
7161 }
7162 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7163 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007166fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167 nlmsg_free(msg);
7168 return ret;
7169}
7170
7171
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007172static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7173 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007174 unsigned int wait_time,
7175 const u8 *dst, const u8 *src,
7176 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007177 const u8 *data, size_t data_len,
7178 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007179{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007180 struct wpa_driver_nl80211_data *drv = bss->drv;
7181 int ret = -1;
7182 u8 *buf;
7183 struct ieee80211_hdr *hdr;
7184
7185 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007186 "freq=%u MHz wait=%d ms no_cck=%d)",
7187 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188
7189 buf = os_zalloc(24 + data_len);
7190 if (buf == NULL)
7191 return ret;
7192 os_memcpy(buf + 24, data, data_len);
7193 hdr = (struct ieee80211_hdr *) buf;
7194 hdr->frame_control =
7195 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7196 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7197 os_memcpy(hdr->addr2, src, ETH_ALEN);
7198 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7199
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007200 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7201 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7202 MAC2STR(src));
7203 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7204 } else {
7205 os_memset(bss->rand_addr, 0, ETH_ALEN);
7206 }
7207
Dmitry Shmidt56052862013-10-04 10:23:25 -07007208 if (is_ap_interface(drv->nlmode) &&
7209 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7210 (int) freq == bss->freq || drv->device_ap_sme ||
7211 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007212 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7213 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007214 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007215 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007216 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007217 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007218 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007219 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220
7221 os_free(buf);
7222 return ret;
7223}
7224
7225
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007226static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007228 struct wpa_driver_nl80211_data *drv = bss->drv;
7229 struct nl_msg *msg;
7230 int ret;
7231
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08007232 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007233 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007234 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007235 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007236 nlmsg_free(msg);
7237 return;
7238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239
7240 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007241 if (ret)
7242 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7243 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007244}
7245
7246
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007247static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7248{
7249 struct i802_bss *bss = priv;
7250 struct wpa_driver_nl80211_data *drv = bss->drv;
7251 unsigned int i;
7252 u64 cookie;
7253
7254 /* Cancel the last pending TX cookie */
7255 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7256
7257 /*
7258 * Cancel the other pending TX cookies, if any. This is needed since
7259 * the driver may keep a list of all pending offchannel TX operations
7260 * and free up the radio only once they have expired or cancelled.
7261 */
7262 for (i = drv->num_send_action_cookies; i > 0; i--) {
7263 cookie = drv->send_action_cookies[i - 1];
7264 if (cookie != drv->send_action_cookie)
7265 nl80211_frame_wait_cancel(bss, cookie);
7266 }
7267 drv->num_send_action_cookies = 0;
7268}
7269
7270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007271static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7272 unsigned int duration)
7273{
7274 struct i802_bss *bss = priv;
7275 struct wpa_driver_nl80211_data *drv = bss->drv;
7276 struct nl_msg *msg;
7277 int ret;
7278 u64 cookie;
7279
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007280 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7281 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7282 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7283 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007284 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286
7287 cookie = 0;
7288 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7289 if (ret == 0) {
7290 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7291 "0x%llx for freq=%u MHz duration=%u",
7292 (long long unsigned int) cookie, freq, duration);
7293 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007294 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007295 return 0;
7296 }
7297 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7298 "(freq=%d duration=%u): %d (%s)",
7299 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007300 return -1;
7301}
7302
7303
7304static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7305{
7306 struct i802_bss *bss = priv;
7307 struct wpa_driver_nl80211_data *drv = bss->drv;
7308 struct nl_msg *msg;
7309 int ret;
7310
7311 if (!drv->pending_remain_on_chan) {
7312 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7313 "to cancel");
7314 return -1;
7315 }
7316
7317 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7318 "0x%llx",
7319 (long long unsigned int) drv->remain_on_chan_cookie);
7320
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007321 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7322 if (!msg ||
7323 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7324 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007325 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007327
7328 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7329 if (ret == 0)
7330 return 0;
7331 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7332 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007333 return -1;
7334}
7335
7336
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007337static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007338{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007341 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007342 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007343 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7344 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007345 /*
7346 * Do not disable Probe Request reporting that was
7347 * enabled in nl80211_setup_ap().
7348 */
7349 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7350 "Probe Request reporting nl_preq=%p while "
7351 "in AP mode", bss->nl_preq);
7352 } else if (bss->nl_preq) {
7353 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7354 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007355 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356 }
7357 return 0;
7358 }
7359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007360 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007361 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007362 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363 return 0;
7364 }
7365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007366 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7367 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007368 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007369 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7370 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007372 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007373 (WLAN_FC_TYPE_MGMT << 2) |
7374 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007375 NULL, 0) < 0)
7376 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007377
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007378 nl80211_register_eloop_read(&bss->nl_preq,
7379 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007380 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381
7382 return 0;
7383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007384 out_err:
7385 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007386 return -1;
7387}
7388
7389
7390static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7391 int ifindex, int disabled)
7392{
7393 struct nl_msg *msg;
7394 struct nlattr *bands, *band;
7395 int ret;
7396
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007397 wpa_printf(MSG_DEBUG,
7398 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7399 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7400 "no NL80211_TXRATE_LEGACY constraint");
7401
7402 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7403 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007404 if (!msg)
7405 return -1;
7406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7408 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007409 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007410
7411 /*
7412 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7413 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7414 * rates. All 5 GHz rates are left enabled.
7415 */
7416 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007417 if (!band ||
7418 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7419 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7420 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007421 nla_nest_end(msg, band);
7422
7423 nla_nest_end(msg, bands);
7424
7425 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007426 if (ret) {
7427 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7428 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007429 } else
7430 drv->disabled_11b_rates = disabled;
7431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432 return ret;
7433
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007434fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007435 nlmsg_free(msg);
7436 return -1;
7437}
7438
7439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007440static int wpa_driver_nl80211_deinit_ap(void *priv)
7441{
7442 struct i802_bss *bss = priv;
7443 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007444 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007445 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007446 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007447 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007448
7449 /*
7450 * If the P2P GO interface was dynamically added, then it is
7451 * possible that the interface change to station is not possible.
7452 */
7453 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7454 return 0;
7455
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007456 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007457}
7458
7459
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007460static int wpa_driver_nl80211_stop_ap(void *priv)
7461{
7462 struct i802_bss *bss = priv;
7463 struct wpa_driver_nl80211_data *drv = bss->drv;
7464 if (!is_ap_interface(drv->nlmode))
7465 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007466 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007467 bss->beacon_set = 0;
7468 return 0;
7469}
7470
7471
Dmitry Shmidt04949592012-07-19 12:16:46 -07007472static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7473{
7474 struct i802_bss *bss = priv;
7475 struct wpa_driver_nl80211_data *drv = bss->drv;
7476 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7477 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007478
7479 /*
7480 * If the P2P Client interface was dynamically added, then it is
7481 * possible that the interface change to station is not possible.
7482 */
7483 if (bss->if_dynamic)
7484 return 0;
7485
Dmitry Shmidt04949592012-07-19 12:16:46 -07007486 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7487}
7488
7489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007490static void wpa_driver_nl80211_resume(void *priv)
7491{
7492 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007493 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007494
7495 if (i802_set_iface_flags(bss, 1))
7496 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007497
7498 if (is_p2p_net_interface(nlmode))
7499 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007500}
7501
7502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007503static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7504{
7505 struct i802_bss *bss = priv;
7506 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007507 struct nl_msg *msg;
7508 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007509
7510 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7511 "hysteresis=%d", threshold, hysteresis);
7512
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007513 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7514 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7515 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7516 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7517 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007518 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007519 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007520 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007522 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007523}
7524
7525
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007526static int get_channel_width(struct nl_msg *msg, void *arg)
7527{
7528 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7529 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7530 struct wpa_signal_info *sig_change = arg;
7531
7532 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7533 genlmsg_attrlen(gnlh, 0), NULL);
7534
7535 sig_change->center_frq1 = -1;
7536 sig_change->center_frq2 = -1;
7537 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7538
7539 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7540 sig_change->chanwidth = convert2width(
7541 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7542 if (tb[NL80211_ATTR_CENTER_FREQ1])
7543 sig_change->center_frq1 =
7544 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7545 if (tb[NL80211_ATTR_CENTER_FREQ2])
7546 sig_change->center_frq2 =
7547 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7548 }
7549
7550 return NL_SKIP;
7551}
7552
7553
7554static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7555 struct wpa_signal_info *sig)
7556{
7557 struct nl_msg *msg;
7558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007559 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007560 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007561}
7562
7563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7565{
7566 struct i802_bss *bss = priv;
7567 struct wpa_driver_nl80211_data *drv = bss->drv;
7568 int res;
7569
7570 os_memset(si, 0, sizeof(*si));
7571 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007572 if (res) {
7573 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7574 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7575 return res;
7576 si->current_signal = 0;
7577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007578
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007579 res = nl80211_get_channel_width(drv, si);
7580 if (res != 0)
7581 return res;
7582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007583 return nl80211_get_link_noise(drv, si);
7584}
7585
7586
7587static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7588 int encrypt)
7589{
7590 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007591 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007592 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007593}
7594
7595
7596static int nl80211_set_param(void *priv, const char *param)
7597{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007598 struct i802_bss *bss = priv;
7599 struct wpa_driver_nl80211_data *drv = bss->drv;
7600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601 if (param == NULL)
7602 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007603 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007604
7605#ifdef CONFIG_P2P
7606 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7608 "interface");
7609 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7610 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7611 }
7612#endif /* CONFIG_P2P */
7613
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007614 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007615 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007616
7617 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007618 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007619 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007620 }
7621
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007622 if (os_strstr(param, "force_bss_selection=1"))
7623 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7624
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007625 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007626 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7627 drv->test_use_roc_tx = 1;
7628 }
7629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007630 return 0;
7631}
7632
7633
Dmitry Shmidte4663042016-04-04 10:07:49 -07007634static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635{
7636 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007637 struct netlink_config *cfg;
7638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007639 global = os_zalloc(sizeof(*global));
7640 if (global == NULL)
7641 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007642 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007643 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007645 global->if_add_ifindex = -1;
7646
7647 cfg = os_zalloc(sizeof(*cfg));
7648 if (cfg == NULL)
7649 goto err;
7650
7651 cfg->ctx = global;
7652 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7653 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7654 global->netlink = netlink_init(cfg);
7655 if (global->netlink == NULL) {
7656 os_free(cfg);
7657 goto err;
7658 }
7659
7660 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7661 goto err;
7662
7663 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7664 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007665 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7666 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007667 goto err;
7668 }
7669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007671
7672err:
7673 nl80211_global_deinit(global);
7674 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007675}
7676
7677
7678static void nl80211_global_deinit(void *priv)
7679{
7680 struct nl80211_global *global = priv;
7681 if (global == NULL)
7682 return;
7683 if (!dl_list_empty(&global->interfaces)) {
7684 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7685 "nl80211_global_deinit",
7686 dl_list_len(&global->interfaces));
7687 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007688
7689 if (global->netlink)
7690 netlink_deinit(global->netlink);
7691
7692 nl_destroy_handles(&global->nl);
7693
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007694 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007695 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007696
7697 nl_cb_put(global->nl_cb);
7698
7699 if (global->ioctl_sock >= 0)
7700 close(global->ioctl_sock);
7701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007702 os_free(global);
7703}
7704
7705
7706static const char * nl80211_get_radio_name(void *priv)
7707{
7708 struct i802_bss *bss = priv;
7709 struct wpa_driver_nl80211_data *drv = bss->drv;
7710 return drv->phyname;
7711}
7712
7713
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007714static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7715 struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007716{
7717 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007718 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
Jouni Malinen75ecf522011-06-27 15:19:46 -07007719
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007720 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007721 (params->pmkid &&
7722 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7723 (params->bssid &&
7724 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7725 (params->ssid_len &&
7726 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7727 (params->fils_cache_id &&
7728 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7729 params->fils_cache_id)) ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07007730 (params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007731 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007732 nlmsg_free(msg);
7733 return -ENOBUFS;
7734 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007735
7736 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007737}
7738
7739
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007740static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007741{
7742 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007743 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007744
7745 if (params->bssid)
7746 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
7747 MAC2STR(params->bssid));
7748 else if (params->fils_cache_id && params->ssid_len) {
7749 wpa_printf(MSG_DEBUG,
7750 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
7751 params->fils_cache_id[0], params->fils_cache_id[1],
7752 wpa_ssid_txt(params->ssid, params->ssid_len));
7753 }
7754
Roshan Pius3a1667e2018-07-03 15:17:14 -07007755 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
7756 if (ret < 0) {
7757 wpa_printf(MSG_DEBUG,
7758 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7759 ret, strerror(-ret));
7760 }
7761
7762 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007763}
7764
7765
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007766static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007767{
7768 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007769 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007770
7771 if (params->bssid)
7772 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7773 MAC2STR(params->bssid));
7774 else if (params->fils_cache_id && params->ssid_len) {
7775 wpa_printf(MSG_DEBUG,
7776 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7777 params->fils_cache_id[0], params->fils_cache_id[1],
7778 wpa_ssid_txt(params->ssid, params->ssid_len));
7779 }
7780
Roshan Pius3a1667e2018-07-03 15:17:14 -07007781 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7782 if (ret < 0) {
7783 wpa_printf(MSG_DEBUG,
7784 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
7785 ret, strerror(-ret));
7786 }
7787
7788 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007789}
7790
7791
7792static int nl80211_flush_pmkid(void *priv)
7793{
7794 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007795 struct nl_msg *msg;
7796
Jouni Malinen75ecf522011-06-27 15:19:46 -07007797 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007798 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
7799 if (!msg)
7800 return -ENOBUFS;
7801 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007802}
7803
7804
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007805static void clean_survey_results(struct survey_results *survey_results)
7806{
7807 struct freq_survey *survey, *tmp;
7808
7809 if (dl_list_empty(&survey_results->survey_list))
7810 return;
7811
7812 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7813 struct freq_survey, list) {
7814 dl_list_del(&survey->list);
7815 os_free(survey);
7816 }
7817}
7818
7819
7820static void add_survey(struct nlattr **sinfo, u32 ifidx,
7821 struct dl_list *survey_list)
7822{
7823 struct freq_survey *survey;
7824
7825 survey = os_zalloc(sizeof(struct freq_survey));
7826 if (!survey)
7827 return;
7828
7829 survey->ifidx = ifidx;
7830 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7831 survey->filled = 0;
7832
7833 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7834 survey->nf = (int8_t)
7835 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7836 survey->filled |= SURVEY_HAS_NF;
7837 }
7838
7839 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7840 survey->channel_time =
7841 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7842 survey->filled |= SURVEY_HAS_CHAN_TIME;
7843 }
7844
7845 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7846 survey->channel_time_busy =
7847 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7848 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7849 }
7850
7851 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7852 survey->channel_time_rx =
7853 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7854 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7855 }
7856
7857 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7858 survey->channel_time_tx =
7859 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7860 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7861 }
7862
7863 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)",
7864 survey->freq,
7865 survey->nf,
7866 (unsigned long int) survey->channel_time,
7867 (unsigned long int) survey->channel_time_busy,
7868 (unsigned long int) survey->channel_time_tx,
7869 (unsigned long int) survey->channel_time_rx,
7870 survey->filled);
7871
7872 dl_list_add_tail(survey_list, &survey->list);
7873}
7874
7875
7876static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7877 unsigned int freq_filter)
7878{
7879 if (!freq_filter)
7880 return 1;
7881
7882 return freq_filter == surveyed_freq;
7883}
7884
7885
7886static int survey_handler(struct nl_msg *msg, void *arg)
7887{
7888 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7889 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7890 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7891 struct survey_results *survey_results;
7892 u32 surveyed_freq = 0;
7893 u32 ifidx;
7894
7895 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7896 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7897 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7898 };
7899
7900 survey_results = (struct survey_results *) arg;
7901
7902 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7903 genlmsg_attrlen(gnlh, 0), NULL);
7904
Dmitry Shmidt97672262014-02-03 13:02:54 -08007905 if (!tb[NL80211_ATTR_IFINDEX])
7906 return NL_SKIP;
7907
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007908 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7909
7910 if (!tb[NL80211_ATTR_SURVEY_INFO])
7911 return NL_SKIP;
7912
7913 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7914 tb[NL80211_ATTR_SURVEY_INFO],
7915 survey_policy))
7916 return NL_SKIP;
7917
7918 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7919 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7920 return NL_SKIP;
7921 }
7922
7923 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7924
7925 if (!check_survey_ok(sinfo, surveyed_freq,
7926 survey_results->freq_filter))
7927 return NL_SKIP;
7928
7929 if (survey_results->freq_filter &&
7930 survey_results->freq_filter != surveyed_freq) {
7931 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7932 surveyed_freq);
7933 return NL_SKIP;
7934 }
7935
7936 add_survey(sinfo, ifidx, &survey_results->survey_list);
7937
7938 return NL_SKIP;
7939}
7940
7941
7942static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7943{
7944 struct i802_bss *bss = priv;
7945 struct wpa_driver_nl80211_data *drv = bss->drv;
7946 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007947 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007948 union wpa_event_data data;
7949 struct survey_results *survey_results;
7950
7951 os_memset(&data, 0, sizeof(data));
7952 survey_results = &data.survey_results;
7953
7954 dl_list_init(&survey_results->survey_list);
7955
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007956 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007957 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007958 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007959
7960 if (freq)
7961 data.survey_results.freq_filter = freq;
7962
7963 do {
7964 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7965 err = send_and_recv_msgs(drv, msg, survey_handler,
7966 survey_results);
7967 } while (err > 0);
7968
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007969 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007970 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007971 else
7972 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007973
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007974 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007975 return err;
7976}
7977
7978
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007979static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7980 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007981 const u8 *replay_ctr)
7982{
7983 struct i802_bss *bss = priv;
7984 struct wpa_driver_nl80211_data *drv = bss->drv;
7985 struct nlattr *replay_nested;
7986 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007987 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007988
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007989 if (!drv->set_rekey_offload)
7990 return;
7991
7992 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007993 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7994 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007995 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007996 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007997 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7998 replay_ctr)) {
7999 nl80211_nlmsg_clear(msg);
8000 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008001 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008002 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008003
8004 nla_nest_end(msg, replay_nested);
8005
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008006 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
8007 if (ret == -EOPNOTSUPP) {
8008 wpa_printf(MSG_DEBUG,
8009 "nl80211: Driver does not support rekey offload");
8010 drv->set_rekey_offload = 0;
8011 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008012}
8013
8014
8015static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8016 const u8 *addr, int qos)
8017{
8018 /* send data frame to poll STA and check whether
8019 * this frame is ACKed */
8020 struct {
8021 struct ieee80211_hdr hdr;
8022 u16 qos_ctl;
8023 } STRUCT_PACKED nulldata;
8024 size_t size;
8025
8026 /* Send data frame to poll STA and check whether this frame is ACKed */
8027
8028 os_memset(&nulldata, 0, sizeof(nulldata));
8029
8030 if (qos) {
8031 nulldata.hdr.frame_control =
8032 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8033 WLAN_FC_STYPE_QOS_NULL);
8034 size = sizeof(nulldata);
8035 } else {
8036 nulldata.hdr.frame_control =
8037 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8038 WLAN_FC_STYPE_NULLFUNC);
8039 size = sizeof(struct ieee80211_hdr);
8040 }
8041
8042 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8043 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8044 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8045 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8046
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008047 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008048 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008049 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8050 "send poll frame");
8051}
8052
8053static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8054 int qos)
8055{
8056 struct i802_bss *bss = priv;
8057 struct wpa_driver_nl80211_data *drv = bss->drv;
8058 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008059 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008060
8061 if (!drv->poll_command_supported) {
8062 nl80211_send_null_frame(bss, own_addr, addr, qos);
8063 return;
8064 }
8065
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008066 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8067 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8068 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008069 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008070 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008071
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008072 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8073 if (ret < 0) {
8074 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8075 MACSTR " failed: ret=%d (%s)",
8076 MAC2STR(addr), ret, strerror(-ret));
8077 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008078}
8079
8080
8081static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8082{
8083 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008084 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008085
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008086 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8087 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8088 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8089 nlmsg_free(msg);
8090 return -ENOBUFS;
8091 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07008092
8093 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8094 if (ret < 0) {
8095 wpa_printf(MSG_DEBUG,
8096 "nl80211: Setting PS state %s failed: %d (%s)",
8097 enabled ? "enabled" : "disabled",
8098 ret, strerror(-ret));
8099 }
8100 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008101}
8102
8103
8104static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8105 int ctwindow)
8106{
8107 struct i802_bss *bss = priv;
8108
8109 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8110 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8111
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008112 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008113#ifdef ANDROID_P2P
8114 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008115#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008116 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008117#endif /* ANDROID_P2P */
8118 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008119
8120 if (legacy_ps == -1)
8121 return 0;
8122 if (legacy_ps != 0 && legacy_ps != 1)
8123 return -1; /* Not yet supported */
8124
8125 return nl80211_set_power_save(bss, legacy_ps);
8126}
8127
8128
Dmitry Shmidt051af732013-10-22 13:52:46 -07008129static int nl80211_start_radar_detection(void *priv,
8130 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008131{
8132 struct i802_bss *bss = priv;
8133 struct wpa_driver_nl80211_data *drv = bss->drv;
8134 struct nl_msg *msg;
8135 int ret;
8136
Dmitry Shmidt051af732013-10-22 13:52:46 -07008137 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)",
8138 freq->freq, freq->ht_enabled, freq->vht_enabled,
8139 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8140
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008141 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8142 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8143 "detection");
8144 return -1;
8145 }
8146
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008147 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8148 nl80211_put_freq_params(msg, freq) < 0) {
8149 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008150 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008151 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008152
8153 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8154 if (ret == 0)
8155 return 0;
8156 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8157 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008158 return -1;
8159}
8160
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008161#ifdef CONFIG_TDLS
8162
8163static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8164 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008165 u32 peer_capab, int initiator, const u8 *buf,
8166 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008167{
8168 struct i802_bss *bss = priv;
8169 struct wpa_driver_nl80211_data *drv = bss->drv;
8170 struct nl_msg *msg;
8171
8172 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8173 return -EOPNOTSUPP;
8174
8175 if (!dst)
8176 return -EINVAL;
8177
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008178 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8179 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8180 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8181 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8182 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8183 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008184 if (peer_capab) {
8185 /*
8186 * The internal enum tdls_peer_capability definition is
8187 * currently identical with the nl80211 enum
8188 * nl80211_tdls_peer_capability, so no conversion is needed
8189 * here.
8190 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008191 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8192 peer_capab))
8193 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008194 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008195 if ((initiator &&
8196 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8197 nla_put(msg, NL80211_ATTR_IE, len, buf))
8198 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008199
8200 return send_and_recv_msgs(drv, msg, NULL, NULL);
8201
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008202fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008203 nlmsg_free(msg);
8204 return -ENOBUFS;
8205}
8206
8207
8208static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8209{
8210 struct i802_bss *bss = priv;
8211 struct wpa_driver_nl80211_data *drv = bss->drv;
8212 struct nl_msg *msg;
8213 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -08008214 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008215
8216 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8217 return -EOPNOTSUPP;
8218
8219 switch (oper) {
8220 case TDLS_DISCOVERY_REQ:
8221 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8222 break;
8223 case TDLS_SETUP:
8224 nl80211_oper = NL80211_TDLS_SETUP;
8225 break;
8226 case TDLS_TEARDOWN:
8227 nl80211_oper = NL80211_TDLS_TEARDOWN;
8228 break;
8229 case TDLS_ENABLE_LINK:
8230 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8231 break;
8232 case TDLS_DISABLE_LINK:
8233 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8234 break;
8235 case TDLS_ENABLE:
8236 return 0;
8237 case TDLS_DISABLE:
8238 return 0;
8239 default:
8240 return -EINVAL;
8241 }
8242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008243 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8244 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8245 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8246 nlmsg_free(msg);
8247 return -ENOBUFS;
8248 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008249
Paul Stewart092955c2017-02-06 09:13:09 -08008250 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8251 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8252 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8253 strerror(-res));
8254 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008255}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008256
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008257
8258static int
8259nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8260 const struct hostapd_freq_params *params)
8261{
8262 struct i802_bss *bss = priv;
8263 struct wpa_driver_nl80211_data *drv = bss->drv;
8264 struct nl_msg *msg;
8265 int ret = -ENOBUFS;
8266
8267 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8268 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8269 return -EOPNOTSUPP;
8270
8271 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8272 " oper_class=%u freq=%u",
8273 MAC2STR(addr), oper_class, params->freq);
8274 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8275 if (!msg ||
8276 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8277 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8278 (ret = nl80211_put_freq_params(msg, params))) {
8279 nlmsg_free(msg);
8280 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8281 return ret;
8282 }
8283
8284 return send_and_recv_msgs(drv, msg, NULL, NULL);
8285}
8286
8287
8288static int
8289nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8290{
8291 struct i802_bss *bss = priv;
8292 struct wpa_driver_nl80211_data *drv = bss->drv;
8293 struct nl_msg *msg;
8294
8295 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8296 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8297 return -EOPNOTSUPP;
8298
8299 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8300 MAC2STR(addr));
8301 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8302 if (!msg ||
8303 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8304 nlmsg_free(msg);
8305 wpa_printf(MSG_DEBUG,
8306 "nl80211: Could not build TDLS cancel chan switch");
8307 return -ENOBUFS;
8308 }
8309
8310 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008311}
8312
8313#endif /* CONFIG TDLS */
8314
8315
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008316static int driver_nl80211_set_key(const char *ifname, void *priv,
8317 enum wpa_alg alg, const u8 *addr,
8318 int key_idx, int set_tx,
8319 const u8 *seq, size_t seq_len,
8320 const u8 *key, size_t key_len)
8321{
8322 struct i802_bss *bss = priv;
8323 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8324 set_tx, seq, seq_len, key, key_len);
8325}
8326
8327
8328static int driver_nl80211_scan2(void *priv,
8329 struct wpa_driver_scan_params *params)
8330{
8331 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008332#ifdef CONFIG_DRIVER_NL80211_QCA
8333 struct wpa_driver_nl80211_data *drv = bss->drv;
8334
8335 /*
8336 * Do a vendor specific scan if possible. If only_new_results is
8337 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8338 * cannot be achieved through a vendor scan. The below condition may
8339 * need to be modified if new scan flags are added in the future whose
8340 * functionality can only be achieved through a normal scan.
8341 */
8342 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8343 return wpa_driver_nl80211_vendor_scan(bss, params);
8344#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008345 return wpa_driver_nl80211_scan(bss, params);
8346}
8347
8348
8349static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8350 int reason_code)
8351{
8352 struct i802_bss *bss = priv;
8353 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8354}
8355
8356
8357static int driver_nl80211_authenticate(void *priv,
8358 struct wpa_driver_auth_params *params)
8359{
8360 struct i802_bss *bss = priv;
8361 return wpa_driver_nl80211_authenticate(bss, params);
8362}
8363
8364
8365static void driver_nl80211_deinit(void *priv)
8366{
8367 struct i802_bss *bss = priv;
8368 wpa_driver_nl80211_deinit(bss);
8369}
8370
8371
8372static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8373 const char *ifname)
8374{
8375 struct i802_bss *bss = priv;
8376 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8377}
8378
8379
8380static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008381 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008382 unsigned int freq,
8383 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008384{
8385 struct i802_bss *bss = priv;
8386 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008387 freq, 0, 0, 0, csa_offs,
8388 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008389}
8390
8391
8392static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8393{
8394 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008395 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008396}
8397
8398
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008399static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8400 const char *ifname, int vlan_id)
8401{
8402 struct i802_bss *bss = priv;
8403 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8404}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008405
8406
8407static int driver_nl80211_read_sta_data(void *priv,
8408 struct hostap_sta_driver_data *data,
8409 const u8 *addr)
8410{
8411 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008412
8413 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008414 return i802_read_sta_data(bss, data, addr);
8415}
8416
8417
8418static int driver_nl80211_send_action(void *priv, unsigned int freq,
8419 unsigned int wait_time,
8420 const u8 *dst, const u8 *src,
8421 const u8 *bssid,
8422 const u8 *data, size_t data_len,
8423 int no_cck)
8424{
8425 struct i802_bss *bss = priv;
8426 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8427 bssid, data, data_len, no_cck);
8428}
8429
8430
8431static int driver_nl80211_probe_req_report(void *priv, int report)
8432{
8433 struct i802_bss *bss = priv;
8434 return wpa_driver_nl80211_probe_req_report(bss, report);
8435}
8436
8437
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008438static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8439 const u8 *ies, size_t ies_len)
8440{
8441 int ret;
8442 struct nl_msg *msg;
8443 struct i802_bss *bss = priv;
8444 struct wpa_driver_nl80211_data *drv = bss->drv;
8445 u16 mdid = WPA_GET_LE16(md);
8446
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008447 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008448 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8449 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8450 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8451 nlmsg_free(msg);
8452 return -ENOBUFS;
8453 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008454
8455 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8456 if (ret) {
8457 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8458 "err=%d (%s)", ret, strerror(-ret));
8459 }
8460
8461 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008462}
8463
8464
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07008465static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008466{
8467 struct i802_bss *bss = priv;
8468 struct wpa_driver_nl80211_data *drv = bss->drv;
8469
8470 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8471 return NULL;
8472
8473 return bss->addr;
8474}
8475
8476
Dmitry Shmidt56052862013-10-04 10:23:25 -07008477static const char * scan_state_str(enum scan_states scan_state)
8478{
8479 switch (scan_state) {
8480 case NO_SCAN:
8481 return "NO_SCAN";
8482 case SCAN_REQUESTED:
8483 return "SCAN_REQUESTED";
8484 case SCAN_STARTED:
8485 return "SCAN_STARTED";
8486 case SCAN_COMPLETED:
8487 return "SCAN_COMPLETED";
8488 case SCAN_ABORTED:
8489 return "SCAN_ABORTED";
8490 case SCHED_SCAN_STARTED:
8491 return "SCHED_SCAN_STARTED";
8492 case SCHED_SCAN_STOPPED:
8493 return "SCHED_SCAN_STOPPED";
8494 case SCHED_SCAN_RESULTS:
8495 return "SCHED_SCAN_RESULTS";
8496 }
8497
8498 return "??";
8499}
8500
8501
8502static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8503{
8504 struct i802_bss *bss = priv;
8505 struct wpa_driver_nl80211_data *drv = bss->drv;
8506 int res;
8507 char *pos, *end;
8508
8509 pos = buf;
8510 end = buf + buflen;
8511
8512 res = os_snprintf(pos, end - pos,
8513 "ifindex=%d\n"
8514 "ifname=%s\n"
8515 "brname=%s\n"
8516 "addr=" MACSTR "\n"
8517 "freq=%d\n"
Hai Shalomc9e41a12018-07-31 14:41:42 -07008518 "%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008519 bss->ifindex,
8520 bss->ifname,
8521 bss->brname,
8522 MAC2STR(bss->addr),
8523 bss->freq,
8524 bss->beacon_set ? "beacon_set=1\n" : "",
8525 bss->added_if_into_bridge ?
8526 "added_if_into_bridge=1\n" : "",
Hai Shalomc9e41a12018-07-31 14:41:42 -07008527 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008528 bss->added_bridge ? "added_bridge=1\n" : "",
8529 bss->in_deinit ? "in_deinit=1\n" : "",
8530 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008531 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008532 return pos - buf;
8533 pos += res;
8534
8535 if (bss->wdev_id_set) {
8536 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8537 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008538 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008539 return pos - buf;
8540 pos += res;
8541 }
8542
8543 res = os_snprintf(pos, end - pos,
8544 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008545 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008546 "drv_ifindex=%d\n"
8547 "operstate=%d\n"
8548 "scan_state=%s\n"
8549 "auth_bssid=" MACSTR "\n"
8550 "auth_attempt_bssid=" MACSTR "\n"
8551 "bssid=" MACSTR "\n"
8552 "prev_bssid=" MACSTR "\n"
8553 "associated=%d\n"
8554 "assoc_freq=%u\n"
8555 "monitor_sock=%d\n"
8556 "monitor_ifidx=%d\n"
8557 "monitor_refcount=%d\n"
8558 "last_mgmt_freq=%u\n"
8559 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008560 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008561 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008562 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07008563 drv->ifindex,
8564 drv->operstate,
8565 scan_state_str(drv->scan_state),
8566 MAC2STR(drv->auth_bssid),
8567 MAC2STR(drv->auth_attempt_bssid),
8568 MAC2STR(drv->bssid),
8569 MAC2STR(drv->prev_bssid),
8570 drv->associated,
8571 drv->assoc_freq,
8572 drv->monitor_sock,
8573 drv->monitor_ifidx,
8574 drv->monitor_refcount,
8575 drv->last_mgmt_freq,
8576 drv->eapol_tx_sock,
8577 drv->ignore_if_down_event ?
8578 "ignore_if_down_event=1\n" : "",
8579 drv->scan_complete_events ?
8580 "scan_complete_events=1\n" : "",
8581 drv->disabled_11b_rates ?
8582 "disabled_11b_rates=1\n" : "",
8583 drv->pending_remain_on_chan ?
8584 "pending_remain_on_chan=1\n" : "",
8585 drv->in_interface_list ? "in_interface_list=1\n" : "",
8586 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8587 drv->poll_command_supported ?
8588 "poll_command_supported=1\n" : "",
8589 drv->data_tx_status ? "data_tx_status=1\n" : "",
8590 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8591 drv->retry_auth ? "retry_auth=1\n" : "",
8592 drv->use_monitor ? "use_monitor=1\n" : "",
8593 drv->ignore_next_local_disconnect ?
8594 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008595 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008596 "ignore_next_local_deauth=1\n" : "");
8597 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008598 return pos - buf;
8599 pos += res;
8600
8601 if (drv->has_capability) {
8602 res = os_snprintf(pos, end - pos,
8603 "capa.key_mgmt=0x%x\n"
8604 "capa.enc=0x%x\n"
8605 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008606 "capa.flags=0x%llx\n"
8607 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008608 "capa.max_scan_ssids=%d\n"
8609 "capa.max_sched_scan_ssids=%d\n"
8610 "capa.sched_scan_supported=%d\n"
8611 "capa.max_match_sets=%d\n"
8612 "capa.max_remain_on_chan=%u\n"
8613 "capa.max_stations=%u\n"
8614 "capa.probe_resp_offloads=0x%x\n"
8615 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008616 "capa.num_multichan_concurrent=%u\n"
8617 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008618 "capa.mac_addr_rand_scan_supported=%d\n"
8619 "capa.conc_capab=%u\n"
8620 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008621 "capa.max_conc_chan_5_0=%u\n"
8622 "capa.max_sched_scan_plans=%u\n"
8623 "capa.max_sched_scan_plan_interval=%u\n"
8624 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008625 drv->capa.key_mgmt,
8626 drv->capa.enc,
8627 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008628 (unsigned long long) drv->capa.flags,
8629 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008630 drv->capa.max_scan_ssids,
8631 drv->capa.max_sched_scan_ssids,
8632 drv->capa.sched_scan_supported,
8633 drv->capa.max_match_sets,
8634 drv->capa.max_remain_on_chan,
8635 drv->capa.max_stations,
8636 drv->capa.probe_resp_offloads,
8637 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008638 drv->capa.num_multichan_concurrent,
8639 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008640 drv->capa.mac_addr_rand_scan_supported,
8641 drv->capa.conc_capab,
8642 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008643 drv->capa.max_conc_chan_5_0,
8644 drv->capa.max_sched_scan_plans,
8645 drv->capa.max_sched_scan_plan_interval,
8646 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008647 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008648 return pos - buf;
8649 pos += res;
8650 }
8651
8652 return pos - buf;
8653}
8654
8655
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008656static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8657{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008658 if ((settings->head &&
8659 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8660 settings->head_len, settings->head)) ||
8661 (settings->tail &&
8662 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8663 settings->tail_len, settings->tail)) ||
8664 (settings->beacon_ies &&
8665 nla_put(msg, NL80211_ATTR_IE,
8666 settings->beacon_ies_len, settings->beacon_ies)) ||
8667 (settings->proberesp_ies &&
8668 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8669 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8670 (settings->assocresp_ies &&
8671 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8672 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8673 (settings->probe_resp &&
8674 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8675 settings->probe_resp_len, settings->probe_resp)))
8676 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008677
8678 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008679}
8680
8681
8682static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8683{
8684 struct nl_msg *msg;
8685 struct i802_bss *bss = priv;
8686 struct wpa_driver_nl80211_data *drv = bss->drv;
8687 struct nlattr *beacon_csa;
8688 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008689 int csa_off_len = 0;
8690 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008691
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008692 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 -08008693 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008694 settings->freq_params.freq, settings->freq_params.bandwidth,
8695 settings->freq_params.center_freq1,
8696 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008698 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008699 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8700 return -EOPNOTSUPP;
8701 }
8702
Roshan Pius3a1667e2018-07-03 15:17:14 -07008703 if (drv->nlmode != NL80211_IFTYPE_AP &&
8704 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
8705 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008706 return -EOPNOTSUPP;
8707
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008708 /*
8709 * Remove empty counters, assuming Probe Response and Beacon frame
8710 * counters match. This implementation assumes that there are only two
8711 * counters.
8712 */
8713 if (settings->counter_offset_beacon[0] &&
8714 !settings->counter_offset_beacon[1]) {
8715 csa_off_len = 1;
8716 } else if (settings->counter_offset_beacon[1] &&
8717 !settings->counter_offset_beacon[0]) {
8718 csa_off_len = 1;
8719 settings->counter_offset_beacon[0] =
8720 settings->counter_offset_beacon[1];
8721 settings->counter_offset_presp[0] =
8722 settings->counter_offset_presp[1];
8723 } else if (settings->counter_offset_beacon[1] &&
8724 settings->counter_offset_beacon[0]) {
8725 csa_off_len = 2;
8726 } else {
8727 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8728 return -EINVAL;
8729 }
8730
8731 /* Check CSA counters validity */
8732 if (drv->capa.max_csa_counters &&
8733 csa_off_len > drv->capa.max_csa_counters) {
8734 wpa_printf(MSG_ERROR,
8735 "nl80211: Too many CSA counters provided");
8736 return -EINVAL;
8737 }
8738
8739 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008740 return -EINVAL;
8741
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008742 for (i = 0; i < csa_off_len; i++) {
8743 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8744 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8745
8746 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8747 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8748 settings->cs_count))
8749 return -EINVAL;
8750
8751 if (settings->beacon_csa.probe_resp &&
8752 ((settings->beacon_csa.probe_resp_len <=
8753 csa_c_off_presp) ||
8754 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8755 settings->cs_count)))
8756 return -EINVAL;
8757 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008758
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008759 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8760 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8761 settings->cs_count) ||
8762 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8763 (settings->block_tx &&
8764 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008765 goto error;
8766
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008767 /* beacon_after params */
8768 ret = set_beacon_data(msg, &settings->beacon_after);
8769 if (ret)
8770 goto error;
8771
8772 /* beacon_csa params */
8773 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8774 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008775 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008776
8777 ret = set_beacon_data(msg, &settings->beacon_csa);
8778 if (ret)
8779 goto error;
8780
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008781 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8782 csa_off_len * sizeof(u16),
8783 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008784 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008785 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8786 csa_off_len * sizeof(u16),
8787 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008788 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008789
8790 nla_nest_end(msg, beacon_csa);
8791 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8792 if (ret) {
8793 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8794 ret, strerror(-ret));
8795 }
8796 return ret;
8797
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008798fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008799 ret = -ENOBUFS;
8800error:
8801 nlmsg_free(msg);
8802 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8803 return ret;
8804}
8805
8806
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008807static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8808 u8 user_priority, u16 admitted_time)
8809{
8810 struct i802_bss *bss = priv;
8811 struct wpa_driver_nl80211_data *drv = bss->drv;
8812 struct nl_msg *msg;
8813 int ret;
8814
8815 wpa_printf(MSG_DEBUG,
8816 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8817 tsid, admitted_time, user_priority);
8818
8819 if (!is_sta_interface(drv->nlmode))
8820 return -ENOTSUP;
8821
8822 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8823 if (!msg ||
8824 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8825 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8826 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8827 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8828 nlmsg_free(msg);
8829 return -ENOBUFS;
8830 }
8831
8832 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8833 if (ret)
8834 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8835 ret, strerror(-ret));
8836 return ret;
8837}
8838
8839
8840static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8841{
8842 struct i802_bss *bss = priv;
8843 struct wpa_driver_nl80211_data *drv = bss->drv;
8844 struct nl_msg *msg;
8845 int ret;
8846
8847 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8848
8849 if (!is_sta_interface(drv->nlmode))
8850 return -ENOTSUP;
8851
8852 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8853 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8854 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8855 nlmsg_free(msg);
8856 return -ENOBUFS;
8857 }
8858
8859 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8860 if (ret)
8861 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8862 ret, strerror(-ret));
8863 return ret;
8864}
8865
8866
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008867#ifdef CONFIG_TESTING_OPTIONS
8868static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8869{
8870 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8871 struct wpabuf *buf = arg;
8872
8873 if (!buf)
8874 return NL_SKIP;
8875
8876 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8877 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8878 return NL_SKIP;
8879 }
8880
8881 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8882 genlmsg_attrlen(gnlh, 0));
8883
8884 return NL_SKIP;
8885}
8886#endif /* CONFIG_TESTING_OPTIONS */
8887
8888
8889static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8890{
8891 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8892 struct nlattr *nl_vendor_reply, *nl;
8893 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8894 struct wpabuf *buf = arg;
8895 int rem;
8896
8897 if (!buf)
8898 return NL_SKIP;
8899
8900 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8901 genlmsg_attrlen(gnlh, 0), NULL);
8902 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8903
8904 if (!nl_vendor_reply)
8905 return NL_SKIP;
8906
8907 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8908 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8909 return NL_SKIP;
8910 }
8911
8912 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8913 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8914 }
8915
8916 return NL_SKIP;
8917}
8918
8919
8920static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8921 unsigned int subcmd, const u8 *data,
8922 size_t data_len, struct wpabuf *buf)
8923{
8924 struct i802_bss *bss = priv;
8925 struct wpa_driver_nl80211_data *drv = bss->drv;
8926 struct nl_msg *msg;
8927 int ret;
8928
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008929#ifdef CONFIG_TESTING_OPTIONS
8930 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008931 msg = nlmsg_alloc();
8932 if (!msg)
8933 return -ENOMEM;
8934
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008935 nl80211_cmd(drv, msg, 0, subcmd);
8936 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8937 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008938 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008939 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8940 if (ret)
8941 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8942 ret);
8943 return ret;
8944 }
8945#endif /* CONFIG_TESTING_OPTIONS */
8946
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008947 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8948 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8949 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8950 (data &&
8951 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8952 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008953
8954 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8955 if (ret)
8956 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8957 ret);
8958 return ret;
8959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008960fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008961 nlmsg_free(msg);
8962 return -ENOBUFS;
8963}
8964
8965
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008966static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8967 u8 qos_map_set_len)
8968{
8969 struct i802_bss *bss = priv;
8970 struct wpa_driver_nl80211_data *drv = bss->drv;
8971 struct nl_msg *msg;
8972 int ret;
8973
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008974 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8975 qos_map_set, qos_map_set_len);
8976
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008977 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8978 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8979 nlmsg_free(msg);
8980 return -ENOBUFS;
8981 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008982
8983 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8984 if (ret)
8985 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8986
8987 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008988}
8989
8990
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008991static int nl80211_set_wowlan(void *priv,
8992 const struct wowlan_triggers *triggers)
8993{
8994 struct i802_bss *bss = priv;
8995 struct wpa_driver_nl80211_data *drv = bss->drv;
8996 struct nl_msg *msg;
8997 struct nlattr *wowlan_triggers;
8998 int ret;
8999
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009000 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
9001
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009002 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009003 !(wowlan_triggers = nla_nest_start(msg,
9004 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
9005 (triggers->any &&
9006 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9007 (triggers->disconnect &&
9008 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9009 (triggers->magic_pkt &&
9010 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9011 (triggers->gtk_rekey_failure &&
9012 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9013 (triggers->eap_identity_req &&
9014 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9015 (triggers->four_way_handshake &&
9016 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9017 (triggers->rfkill_release &&
9018 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9019 nlmsg_free(msg);
9020 return -ENOBUFS;
9021 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009022
9023 nla_nest_end(msg, wowlan_triggers);
9024
9025 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9026 if (ret)
9027 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9028
9029 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009030}
9031
9032
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009033#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009034static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9035{
9036 struct i802_bss *bss = priv;
9037 struct wpa_driver_nl80211_data *drv = bss->drv;
9038 struct nl_msg *msg;
9039 struct nlattr *params;
9040
9041 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9042
9043 if (!drv->roaming_vendor_cmd_avail) {
9044 wpa_printf(MSG_DEBUG,
9045 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9046 return -1;
9047 }
9048
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009049 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9050 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9051 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9052 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9053 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9054 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9055 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9056 QCA_ROAMING_NOT_ALLOWED) ||
9057 (bssid &&
9058 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9059 nlmsg_free(msg);
9060 return -1;
9061 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009062 nla_nest_end(msg, params);
9063
9064 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009065}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009066
9067
Roshan Pius3a1667e2018-07-03 15:17:14 -07009068static int nl80211_disable_fils(void *priv, int disable)
9069{
9070 struct i802_bss *bss = priv;
9071 struct wpa_driver_nl80211_data *drv = bss->drv;
9072 struct nl_msg *msg;
9073 struct nlattr *params;
9074
9075 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9076
9077 if (!drv->set_wifi_conf_vendor_cmd_avail)
9078 return -1;
9079
9080 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9081 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9082 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9083 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9084 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9085 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9086 disable)) {
9087 nlmsg_free(msg);
9088 return -1;
9089 }
9090 nla_nest_end(msg, params);
9091
9092 return send_and_recv_msgs(drv, msg, NULL, NULL);
9093}
9094
9095
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009096/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9097#define WPA_SUPPLICANT_CLIENT_ID 1
9098
9099static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9100 const u8 *bssid)
9101{
9102 struct i802_bss *bss = priv;
9103 struct wpa_driver_nl80211_data *drv = bss->drv;
9104 struct nl_msg *msg;
9105 struct nlattr *params, *nlbssids, *attr;
9106 unsigned int i;
9107
9108 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9109 num_bssid);
9110
9111 if (!drv->roam_vendor_cmd_avail)
9112 return -1;
9113
9114 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9115 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9116 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9117 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9118 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9119 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9120 QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9121 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9122 WPA_SUPPLICANT_CLIENT_ID) ||
9123 nla_put_u32(msg,
9124 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9125 num_bssid))
9126 goto fail;
9127
9128 nlbssids = nla_nest_start(
9129 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9130 if (!nlbssids)
9131 goto fail;
9132
9133 for (i = 0; i < num_bssid; i++) {
9134 attr = nla_nest_start(msg, i);
9135 if (!attr)
9136 goto fail;
9137 if (nla_put(msg,
9138 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9139 ETH_ALEN, &bssid[i * ETH_ALEN]))
9140 goto fail;
9141 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9142 MAC2STR(&bssid[i * ETH_ALEN]));
9143 nla_nest_end(msg, attr);
9144 }
9145 nla_nest_end(msg, nlbssids);
9146 nla_nest_end(msg, params);
9147
9148 return send_and_recv_msgs(drv, msg, NULL, NULL);
9149
9150fail:
9151 nlmsg_free(msg);
9152 return -1;
9153}
9154
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009155#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009156
9157
9158static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9159{
9160 struct i802_bss *bss = priv;
9161 struct wpa_driver_nl80211_data *drv = bss->drv;
9162 int new_addr = addr != NULL;
9163
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009164 if (TEST_FAIL())
9165 return -1;
9166
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009167 if (!addr)
9168 addr = drv->perm_addr;
9169
9170 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9171 return -1;
9172
9173 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9174 {
9175 wpa_printf(MSG_DEBUG,
9176 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9177 bss->ifname, MAC2STR(addr));
9178 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9179 1) < 0) {
9180 wpa_printf(MSG_DEBUG,
9181 "nl80211: Could not restore interface UP after failed set_mac_addr");
9182 }
9183 return -1;
9184 }
9185
9186 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9187 bss->ifname, MAC2STR(addr));
9188 drv->addr_changed = new_addr;
9189 os_memcpy(bss->addr, addr, ETH_ALEN);
9190
9191 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9192 {
9193 wpa_printf(MSG_DEBUG,
9194 "nl80211: Could not restore interface UP after set_mac_addr");
9195 }
9196
9197 return 0;
9198}
9199
9200
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009201#ifdef CONFIG_MESH
9202
9203static int wpa_driver_nl80211_init_mesh(void *priv)
9204{
9205 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9206 wpa_printf(MSG_INFO,
9207 "nl80211: Failed to set interface into mesh mode");
9208 return -1;
9209 }
9210 return 0;
9211}
9212
9213
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009214static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9215 size_t mesh_id_len)
9216{
9217 if (mesh_id) {
9218 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
9219 mesh_id, mesh_id_len);
9220 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9221 }
9222
9223 return 0;
9224}
9225
9226
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009227static int nl80211_put_mesh_config(struct nl_msg *msg,
9228 struct wpa_driver_mesh_bss_params *params)
9229{
9230 struct nlattr *container;
9231
9232 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9233 if (!container)
9234 return -1;
9235
9236 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009237 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9238 params->auto_plinks)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009239 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9240 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009241 params->max_peer_links)) ||
9242 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9243 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9244 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009245 return -1;
9246
9247 /*
9248 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9249 * the timer could disconnect stations even in that case.
9250 */
9251 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9252 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
9253 params->peer_link_timeout)) {
9254 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9255 return -1;
9256 }
9257
9258 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9259 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9260 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9261 return -1;
9262 }
9263
9264 nla_nest_end(msg, container);
9265
9266 return 0;
9267}
9268
9269
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009270static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009271 struct wpa_driver_mesh_join_params *params)
9272{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009273 struct wpa_driver_nl80211_data *drv = bss->drv;
9274 struct nl_msg *msg;
9275 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08009276 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009277
9278 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9279 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009280 if (!msg ||
9281 nl80211_put_freq_params(msg, &params->freq) ||
9282 nl80211_put_basic_rates(msg, params->basic_rates) ||
9283 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009284 nl80211_put_beacon_int(msg, params->beacon_int) ||
9285 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009286 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009287
9288 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
9289
9290 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9291 if (!container)
9292 goto fail;
9293
9294 if (params->ies) {
9295 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
9296 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9297 params->ies))
9298 goto fail;
9299 }
9300 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9301 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
9302 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9303 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9304 goto fail;
9305 }
9306 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9307 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9308 goto fail;
9309 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9310 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9311 goto fail;
9312 nla_nest_end(msg, container);
9313
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009314 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9315 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9316 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
9317 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009318 goto fail;
9319
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009320 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9321 msg = NULL;
9322 if (ret) {
9323 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9324 ret, strerror(-ret));
9325 goto fail;
9326 }
9327 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009328 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009329 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9330
9331fail:
9332 nlmsg_free(msg);
9333 return ret;
9334}
9335
9336
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009337static int
9338wpa_driver_nl80211_join_mesh(void *priv,
9339 struct wpa_driver_mesh_join_params *params)
9340{
9341 struct i802_bss *bss = priv;
9342 int ret, timeout;
9343
9344 timeout = params->conf.peer_link_timeout;
9345
9346 /* Disable kernel inactivity timer */
9347 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9348 params->conf.peer_link_timeout = 0;
9349
9350 ret = nl80211_join_mesh(bss, params);
9351 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9352 wpa_printf(MSG_DEBUG,
9353 "nl80211: Mesh join retry for peer_link_timeout");
9354 /*
9355 * Old kernel does not support setting
9356 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9357 * into future from peer_link_timeout.
9358 */
9359 params->conf.peer_link_timeout = timeout + 60;
9360 ret = nl80211_join_mesh(priv, params);
9361 }
9362
9363 params->conf.peer_link_timeout = timeout;
9364 return ret;
9365}
9366
9367
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009368static int wpa_driver_nl80211_leave_mesh(void *priv)
9369{
9370 struct i802_bss *bss = priv;
9371 struct wpa_driver_nl80211_data *drv = bss->drv;
9372 struct nl_msg *msg;
9373 int ret;
9374
9375 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9376 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
9377 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9378 if (ret) {
9379 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9380 ret, strerror(-ret));
9381 } else {
9382 wpa_printf(MSG_DEBUG,
9383 "nl80211: mesh leave request send successfully");
9384 }
9385
9386 if (wpa_driver_nl80211_set_mode(drv->first_bss,
9387 NL80211_IFTYPE_STATION)) {
9388 wpa_printf(MSG_INFO,
9389 "nl80211: Failed to set interface into station mode");
9390 }
9391 return ret;
9392}
9393
9394#endif /* CONFIG_MESH */
9395
9396
9397static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9398 const u8 *ipaddr, int prefixlen,
9399 const u8 *addr)
9400{
9401#ifdef CONFIG_LIBNL3_ROUTE
9402 struct i802_bss *bss = priv;
9403 struct wpa_driver_nl80211_data *drv = bss->drv;
9404 struct rtnl_neigh *rn;
9405 struct nl_addr *nl_ipaddr = NULL;
9406 struct nl_addr *nl_lladdr = NULL;
9407 int family, addrsize;
9408 int res;
9409
9410 if (!ipaddr || prefixlen == 0 || !addr)
9411 return -EINVAL;
9412
9413 if (bss->br_ifindex == 0) {
9414 wpa_printf(MSG_DEBUG,
9415 "nl80211: bridge must be set before adding an ip neigh to it");
9416 return -1;
9417 }
9418
9419 if (!drv->rtnl_sk) {
9420 wpa_printf(MSG_DEBUG,
9421 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9422 return -1;
9423 }
9424
9425 if (version == 4) {
9426 family = AF_INET;
9427 addrsize = 4;
9428 } else if (version == 6) {
9429 family = AF_INET6;
9430 addrsize = 16;
9431 } else {
9432 return -EINVAL;
9433 }
9434
9435 rn = rtnl_neigh_alloc();
9436 if (rn == NULL)
9437 return -ENOMEM;
9438
9439 /* set the destination ip address for neigh */
9440 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9441 if (nl_ipaddr == NULL) {
9442 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9443 res = -ENOMEM;
9444 goto errout;
9445 }
9446 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9447 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9448 if (res) {
9449 wpa_printf(MSG_DEBUG,
9450 "nl80211: neigh set destination addr failed");
9451 goto errout;
9452 }
9453
9454 /* set the corresponding lladdr for neigh */
9455 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9456 if (nl_lladdr == NULL) {
9457 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9458 res = -ENOMEM;
9459 goto errout;
9460 }
9461 rtnl_neigh_set_lladdr(rn, nl_lladdr);
9462
9463 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9464 rtnl_neigh_set_state(rn, NUD_PERMANENT);
9465
9466 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9467 if (res) {
9468 wpa_printf(MSG_DEBUG,
9469 "nl80211: Adding bridge ip neigh failed: %s",
9470 strerror(errno));
9471 }
9472errout:
9473 if (nl_lladdr)
9474 nl_addr_put(nl_lladdr);
9475 if (nl_ipaddr)
9476 nl_addr_put(nl_ipaddr);
9477 if (rn)
9478 rtnl_neigh_put(rn);
9479 return res;
9480#else /* CONFIG_LIBNL3_ROUTE */
9481 return -1;
9482#endif /* CONFIG_LIBNL3_ROUTE */
9483}
9484
9485
9486static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9487 const u8 *ipaddr)
9488{
9489#ifdef CONFIG_LIBNL3_ROUTE
9490 struct i802_bss *bss = priv;
9491 struct wpa_driver_nl80211_data *drv = bss->drv;
9492 struct rtnl_neigh *rn;
9493 struct nl_addr *nl_ipaddr;
9494 int family, addrsize;
9495 int res;
9496
9497 if (!ipaddr)
9498 return -EINVAL;
9499
9500 if (version == 4) {
9501 family = AF_INET;
9502 addrsize = 4;
9503 } else if (version == 6) {
9504 family = AF_INET6;
9505 addrsize = 16;
9506 } else {
9507 return -EINVAL;
9508 }
9509
9510 if (bss->br_ifindex == 0) {
9511 wpa_printf(MSG_DEBUG,
9512 "nl80211: bridge must be set to delete an ip neigh");
9513 return -1;
9514 }
9515
9516 if (!drv->rtnl_sk) {
9517 wpa_printf(MSG_DEBUG,
9518 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9519 return -1;
9520 }
9521
9522 rn = rtnl_neigh_alloc();
9523 if (rn == NULL)
9524 return -ENOMEM;
9525
9526 /* set the destination ip address for neigh */
9527 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9528 if (nl_ipaddr == NULL) {
9529 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9530 res = -ENOMEM;
9531 goto errout;
9532 }
9533 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9534 if (res) {
9535 wpa_printf(MSG_DEBUG,
9536 "nl80211: neigh set destination addr failed");
9537 goto errout;
9538 }
9539
9540 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9541
9542 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9543 if (res) {
9544 wpa_printf(MSG_DEBUG,
9545 "nl80211: Deleting bridge ip neigh failed: %s",
9546 strerror(errno));
9547 }
9548errout:
9549 if (nl_ipaddr)
9550 nl_addr_put(nl_ipaddr);
9551 if (rn)
9552 rtnl_neigh_put(rn);
9553 return res;
9554#else /* CONFIG_LIBNL3_ROUTE */
9555 return -1;
9556#endif /* CONFIG_LIBNL3_ROUTE */
9557}
9558
9559
9560static int linux_write_system_file(const char *path, unsigned int val)
9561{
9562 char buf[50];
9563 int fd, len;
9564
9565 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9566 if (os_snprintf_error(sizeof(buf), len))
9567 return -1;
9568
9569 fd = open(path, O_WRONLY);
9570 if (fd < 0)
9571 return -1;
9572
9573 if (write(fd, buf, len) < 0) {
9574 wpa_printf(MSG_DEBUG,
9575 "nl80211: Failed to write Linux system file: %s with the value of %d",
9576 path, val);
9577 close(fd);
9578 return -1;
9579 }
9580 close(fd);
9581
9582 return 0;
9583}
9584
9585
9586static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9587{
9588 switch (attr) {
9589 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07009590 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009591 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9592 return "hairpin_mode";
9593 }
9594
9595 return NULL;
9596}
9597
9598
9599static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9600 unsigned int val)
9601{
9602 struct i802_bss *bss = priv;
9603 char path[128];
9604 const char *attr_txt;
9605
9606 attr_txt = drv_br_port_attr_str(attr);
9607 if (attr_txt == NULL)
9608 return -EINVAL;
9609
9610 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9611 bss->ifname, attr_txt);
9612
9613 if (linux_write_system_file(path, val))
9614 return -1;
9615
9616 return 0;
9617}
9618
9619
9620static const char * drv_br_net_param_str(enum drv_br_net_param param)
9621{
9622 switch (param) {
9623 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9624 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07009625 default:
9626 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009627 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009628}
9629
9630
9631static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9632 unsigned int val)
9633{
9634 struct i802_bss *bss = priv;
9635 char path[128];
9636 const char *param_txt;
9637 int ip_version = 4;
9638
Dmitry Shmidt83474442015-04-15 13:47:09 -07009639 if (param == DRV_BR_MULTICAST_SNOOPING) {
9640 os_snprintf(path, sizeof(path),
9641 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9642 bss->brname);
9643 goto set_val;
9644 }
9645
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009646 param_txt = drv_br_net_param_str(param);
9647 if (param_txt == NULL)
9648 return -EINVAL;
9649
9650 switch (param) {
9651 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9652 ip_version = 4;
9653 break;
9654 default:
9655 return -EINVAL;
9656 }
9657
9658 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9659 ip_version, bss->brname, param_txt);
9660
Dmitry Shmidt83474442015-04-15 13:47:09 -07009661set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009662 if (linux_write_system_file(path, val))
9663 return -1;
9664
9665 return 0;
9666}
9667
9668
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009669#ifdef CONFIG_DRIVER_NL80211_QCA
9670
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009671static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9672{
9673 switch (hw_mode) {
9674 case HOSTAPD_MODE_IEEE80211B:
9675 return QCA_ACS_MODE_IEEE80211B;
9676 case HOSTAPD_MODE_IEEE80211G:
9677 return QCA_ACS_MODE_IEEE80211G;
9678 case HOSTAPD_MODE_IEEE80211A:
9679 return QCA_ACS_MODE_IEEE80211A;
9680 case HOSTAPD_MODE_IEEE80211AD:
9681 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07009682 case HOSTAPD_MODE_IEEE80211ANY:
9683 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009684 default:
9685 return -1;
9686 }
9687}
9688
9689
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009690static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9691{
9692 int i, len, ret;
9693 u32 *freqs;
9694
9695 if (!freq_list)
9696 return 0;
9697 len = int_array_len(freq_list);
9698 freqs = os_malloc(sizeof(u32) * len);
9699 if (!freqs)
9700 return -1;
9701 for (i = 0; i < len; i++)
9702 freqs[i] = freq_list[i];
9703 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9704 sizeof(u32) * len, freqs);
9705 os_free(freqs);
9706 return ret;
9707}
9708
9709
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009710static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9711{
9712 struct i802_bss *bss = priv;
9713 struct wpa_driver_nl80211_data *drv = bss->drv;
9714 struct nl_msg *msg;
9715 struct nlattr *data;
9716 int ret;
9717 int mode;
9718
9719 mode = hw_mode_to_qca_acs(params->hw_mode);
9720 if (mode < 0)
9721 return -1;
9722
9723 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9724 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9725 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9726 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9727 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9728 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9729 (params->ht_enabled &&
9730 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9731 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009732 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9733 (params->vht_enabled &&
9734 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9735 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9736 params->ch_width) ||
9737 (params->ch_list_len &&
9738 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009739 params->ch_list)) ||
9740 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009741 nlmsg_free(msg);
9742 return -ENOBUFS;
9743 }
9744 nla_nest_end(msg, data);
9745
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009746 wpa_printf(MSG_DEBUG,
9747 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9748 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9749 params->vht_enabled, params->ch_width, params->ch_list_len);
9750
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009751 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9752 if (ret) {
9753 wpa_printf(MSG_DEBUG,
9754 "nl80211: Failed to invoke driver ACS function: %s",
9755 strerror(errno));
9756 }
9757 return ret;
9758}
9759
9760
Ravi Joshie6ccb162015-07-16 17:45:41 -07009761static int nl80211_set_band(void *priv, enum set_band band)
9762{
9763 struct i802_bss *bss = priv;
9764 struct wpa_driver_nl80211_data *drv = bss->drv;
9765 struct nl_msg *msg;
9766 struct nlattr *data;
9767 int ret;
9768 enum qca_set_band qca_band;
9769
9770 if (!drv->setband_vendor_cmd_avail)
9771 return -1;
9772
9773 switch (band) {
9774 case WPA_SETBAND_AUTO:
9775 qca_band = QCA_SETBAND_AUTO;
9776 break;
9777 case WPA_SETBAND_5G:
9778 qca_band = QCA_SETBAND_5G;
9779 break;
9780 case WPA_SETBAND_2G:
9781 qca_band = QCA_SETBAND_2G;
9782 break;
9783 default:
9784 return -1;
9785 }
9786
9787 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9788 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9789 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9790 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9791 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9792 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9793 nlmsg_free(msg);
9794 return -ENOBUFS;
9795 }
9796 nla_nest_end(msg, data);
9797
9798 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9799 if (ret) {
9800 wpa_printf(MSG_DEBUG,
9801 "nl80211: Driver setband function failed: %s",
9802 strerror(errno));
9803 }
9804 return ret;
9805}
9806
9807
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009808struct nl80211_pcl {
9809 unsigned int num;
9810 unsigned int *freq_list;
9811};
9812
9813static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9814{
9815 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9816 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9817 struct nl80211_pcl *param = arg;
9818 struct nlattr *nl_vend, *attr;
9819 enum qca_iface_type iface_type;
9820 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9821 unsigned int num, max_num;
9822 u32 *freqs;
9823
9824 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9825 genlmsg_attrlen(gnlh, 0), NULL);
9826
9827 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9828 if (!nl_vend)
9829 return NL_SKIP;
9830
9831 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9832 nla_data(nl_vend), nla_len(nl_vend), NULL);
9833
9834 attr = tb_vendor[
9835 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9836 if (!attr) {
9837 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9838 param->num = 0;
9839 return NL_SKIP;
9840 }
9841
9842 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9843 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9844 iface_type);
9845
9846 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9847 if (!attr) {
9848 wpa_printf(MSG_ERROR,
9849 "nl80211: preferred_freq_list couldn't be found");
9850 param->num = 0;
9851 return NL_SKIP;
9852 }
9853
9854 /*
9855 * param->num has the maximum number of entries for which there
9856 * is room in the freq_list provided by the caller.
9857 */
9858 freqs = nla_data(attr);
9859 max_num = nla_len(attr) / sizeof(u32);
9860 if (max_num > param->num)
9861 max_num = param->num;
9862 for (num = 0; num < max_num; num++)
9863 param->freq_list[num] = freqs[num];
9864 param->num = num;
9865
9866 return NL_SKIP;
9867}
9868
9869
9870static int nl80211_get_pref_freq_list(void *priv,
9871 enum wpa_driver_if_type if_type,
9872 unsigned int *num,
9873 unsigned int *freq_list)
9874{
9875 struct i802_bss *bss = priv;
9876 struct wpa_driver_nl80211_data *drv = bss->drv;
9877 struct nl_msg *msg;
9878 int ret;
9879 unsigned int i;
9880 struct nlattr *params;
9881 struct nl80211_pcl param;
9882 enum qca_iface_type iface_type;
9883
9884 if (!drv->get_pref_freq_list)
9885 return -1;
9886
9887 switch (if_type) {
9888 case WPA_IF_STATION:
9889 iface_type = QCA_IFACE_TYPE_STA;
9890 break;
9891 case WPA_IF_AP_BSS:
9892 iface_type = QCA_IFACE_TYPE_AP;
9893 break;
9894 case WPA_IF_P2P_GO:
9895 iface_type = QCA_IFACE_TYPE_P2P_GO;
9896 break;
9897 case WPA_IF_P2P_CLIENT:
9898 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9899 break;
9900 case WPA_IF_IBSS:
9901 iface_type = QCA_IFACE_TYPE_IBSS;
9902 break;
9903 case WPA_IF_TDLS:
9904 iface_type = QCA_IFACE_TYPE_TDLS;
9905 break;
9906 default:
9907 return -1;
9908 }
9909
9910 param.num = *num;
9911 param.freq_list = freq_list;
9912
9913 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9914 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9915 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9916 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9917 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9918 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9919 nla_put_u32(msg,
9920 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9921 iface_type)) {
9922 wpa_printf(MSG_ERROR,
9923 "%s: err in adding vendor_cmd and vendor_data",
9924 __func__);
9925 nlmsg_free(msg);
9926 return -1;
9927 }
9928 nla_nest_end(msg, params);
9929
9930 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9931 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9932 if (ret) {
9933 wpa_printf(MSG_ERROR,
9934 "%s: err in send_and_recv_msgs", __func__);
9935 return ret;
9936 }
9937
9938 *num = param.num;
9939
9940 for (i = 0; i < *num; i++) {
9941 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9942 i, freq_list[i]);
9943 }
9944
9945 return 0;
9946}
9947
9948
9949static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9950{
9951 struct i802_bss *bss = priv;
9952 struct wpa_driver_nl80211_data *drv = bss->drv;
9953 struct nl_msg *msg;
9954 int ret;
9955 struct nlattr *params;
9956
9957 if (!drv->set_prob_oper_freq)
9958 return -1;
9959
9960 wpa_printf(MSG_DEBUG,
9961 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9962 freq, bss->ifindex);
9963
9964 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9965 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9966 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9967 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9968 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9969 nla_put_u32(msg,
9970 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9971 QCA_IFACE_TYPE_P2P_CLIENT) ||
9972 nla_put_u32(msg,
9973 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9974 freq)) {
9975 wpa_printf(MSG_ERROR,
9976 "%s: err in adding vendor_cmd and vendor_data",
9977 __func__);
9978 nlmsg_free(msg);
9979 return -1;
9980 }
9981 nla_nest_end(msg, params);
9982
9983 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9984 msg = NULL;
9985 if (ret) {
9986 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9987 __func__);
9988 return ret;
9989 }
9990 nlmsg_free(msg);
9991 return 0;
9992}
9993
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009994
9995static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9996 unsigned int period, unsigned int interval,
9997 unsigned int count, const u8 *device_types,
9998 size_t dev_types_len,
9999 const u8 *ies, size_t ies_len)
10000{
10001 struct i802_bss *bss = priv;
10002 struct wpa_driver_nl80211_data *drv = bss->drv;
10003 struct nl_msg *msg;
10004 struct nlattr *container;
10005 int ret;
10006
10007 wpa_printf(MSG_DEBUG,
10008 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
10009 freq, period, interval, count);
10010
10011 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10012 return -1;
10013
10014 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10015 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10016 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10017 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10018 goto fail;
10019
10020 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10021 if (!container)
10022 goto fail;
10023
10024 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10025 freq) ||
10026 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10027 period) ||
10028 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10029 interval) ||
10030 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10031 count) ||
10032 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10033 dev_types_len, device_types) ||
10034 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10035 ies_len, ies))
10036 goto fail;
10037
10038 nla_nest_end(msg, container);
10039 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10040 msg = NULL;
10041 if (ret) {
10042 wpa_printf(MSG_DEBUG,
10043 "nl80211: Failed to send P2P Listen offload vendor command");
10044 goto fail;
10045 }
10046
10047 return 0;
10048
10049fail:
10050 nlmsg_free(msg);
10051 return -1;
10052}
10053
10054
10055static int nl80211_p2p_lo_stop(void *priv)
10056{
10057 struct i802_bss *bss = priv;
10058 struct wpa_driver_nl80211_data *drv = bss->drv;
10059 struct nl_msg *msg;
10060
10061 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10062
10063 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10064 return -1;
10065
10066 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10067 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10068 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10069 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10070 nlmsg_free(msg);
10071 return -1;
10072 }
10073
10074 return send_and_recv_msgs(drv, msg, NULL, NULL);
10075}
10076
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010077
10078static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10079{
10080 struct i802_bss *bss = priv;
10081 struct wpa_driver_nl80211_data *drv = bss->drv;
10082 struct nl_msg *msg;
10083 struct nlattr *params;
10084 int ret;
10085 u32 tdls_mode;
10086
10087 wpa_printf(MSG_DEBUG,
10088 "nl80211: Set TDKS mode: tdls_external_control=%d",
10089 tdls_external_control);
10090
10091 if (tdls_external_control == 1)
10092 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10093 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10094 else
10095 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10096
10097 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10098 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10099 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10100 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10101 goto fail;
10102
10103 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10104 if (!params)
10105 goto fail;
10106
10107 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10108 tdls_mode))
10109 goto fail;
10110
10111 nla_nest_end(msg, params);
10112
10113 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10114 msg = NULL;
10115 if (ret) {
10116 wpa_printf(MSG_ERROR,
10117 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10118 ret, strerror(-ret));
10119 goto fail;
10120 }
10121 return 0;
10122fail:
10123 nlmsg_free(msg);
10124 return -1;
10125}
10126
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010127
10128#ifdef CONFIG_MBO
10129
10130static enum mbo_transition_reject_reason
10131nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10132{
10133 switch (status) {
10134 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10135 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10136 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10137 return MBO_TRANSITION_REJECT_REASON_DELAY;
10138 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10139 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10140 case QCA_STATUS_REJECT_LOW_RSSI:
10141 return MBO_TRANSITION_REJECT_REASON_RSSI;
10142 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10143 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10144 case QCA_STATUS_REJECT_UNKNOWN:
10145 default:
10146 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10147 }
10148}
10149
10150
10151static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10152 struct nlattr *tb[], int num)
10153{
10154 enum qca_wlan_btm_candidate_status status;
10155 char buf[50];
10156
10157 os_memcpy(candidate->bssid,
10158 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10159 ETH_ALEN);
10160
10161 status = nla_get_u32(
10162 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10163 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10164 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10165
10166 if (candidate->is_accept)
10167 os_snprintf(buf, sizeof(buf), "Accepted");
10168 else
10169 os_snprintf(buf, sizeof(buf),
10170 "Rejected, Reject_reason: %d",
10171 candidate->reject_reason);
10172 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10173 num, MAC2STR(candidate->bssid), buf);
10174}
10175
10176
10177static int
10178nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10179{
10180 struct wpa_bss_candidate_info *info = arg;
10181 struct candidate_list *candidate = info->candidates;
10182 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10183 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10184 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10185 static struct nla_policy policy[
10186 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10187 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10188 .minlen = ETH_ALEN
10189 },
10190 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10191 .type = NLA_U32,
10192 },
10193 };
10194 struct nlattr *attr;
10195 int rem;
10196 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10197 u8 num;
10198
10199 num = info->num; /* number of candidates sent to driver */
10200 info->num = 0;
10201 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10202 genlmsg_attrlen(gnlh, 0), NULL);
10203
10204 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10205 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10206 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10207 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10208 return NL_SKIP;
10209
10210 wpa_printf(MSG_DEBUG,
10211 "nl80211: WNM Candidate list received from driver");
10212 nla_for_each_nested(attr,
10213 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10214 rem) {
10215 if (info->num >= num ||
10216 nla_parse_nested(
10217 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10218 attr, policy) ||
10219 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10220 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10221 break;
10222
10223 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10224
10225 candidate++;
10226 info->num++;
10227 }
10228
10229 return NL_SKIP;
10230}
10231
10232
10233static struct wpa_bss_candidate_info *
10234nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10235{
10236 struct i802_bss *bss = priv;
10237 struct wpa_driver_nl80211_data *drv = bss->drv;
10238 struct nl_msg *msg;
10239 struct nlattr *attr, *attr1, *attr2;
10240 struct wpa_bss_candidate_info *info;
10241 u8 i;
10242 int ret;
10243 u8 *pos;
10244
10245 if (!drv->fetch_bss_trans_status)
10246 return NULL;
10247
10248 info = os_zalloc(sizeof(*info));
10249 if (!info)
10250 return NULL;
10251 /* Allocate memory for number of candidates sent to driver */
10252 info->candidates = os_calloc(params->n_candidates,
10253 sizeof(*info->candidates));
10254 if (!info->candidates) {
10255 os_free(info);
10256 return NULL;
10257 }
10258
10259 /* Copy the number of candidates being sent to driver. This is used in
10260 * nl80211_get_bss_transition_status_handler() to limit the number of
10261 * candidates that can be populated in info->candidates and will be
10262 * later overwritten with the actual number of candidates received from
10263 * the driver.
10264 */
10265 info->num = params->n_candidates;
10266
10267 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10268 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10269 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10270 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10271 goto fail;
10272
10273 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10274 if (!attr)
10275 goto fail;
10276
10277 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10278 params->mbo_transition_reason))
10279 goto fail;
10280
10281 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10282 if (!attr1)
10283 goto fail;
10284
10285 wpa_printf(MSG_DEBUG,
10286 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10287 params->mbo_transition_reason, params->n_candidates);
10288 pos = params->bssid;
10289 for (i = 0; i < params->n_candidates; i++) {
10290 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
10291 MAC2STR(pos));
10292 attr2 = nla_nest_start(msg, i);
10293 if (!attr2 ||
10294 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10295 ETH_ALEN, pos))
10296 goto fail;
10297 pos += ETH_ALEN;
10298 nla_nest_end(msg, attr2);
10299 }
10300
10301 nla_nest_end(msg, attr1);
10302 nla_nest_end(msg, attr);
10303
10304 ret = send_and_recv_msgs(drv, msg,
10305 nl80211_get_bss_transition_status_handler,
10306 info);
10307 msg = NULL;
10308 if (ret) {
10309 wpa_printf(MSG_ERROR,
10310 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10311 ret, strerror(-ret));
10312 goto fail;
10313 }
10314 return info;
10315
10316fail:
10317 nlmsg_free(msg);
10318 os_free(info->candidates);
10319 os_free(info);
10320 return NULL;
10321}
10322
10323
10324/**
10325 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10326 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10327 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10328 * Returns: 0 on success, -1 on failure
10329 */
10330static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10331{
10332 struct i802_bss *bss = priv;
10333 struct wpa_driver_nl80211_data *drv = bss->drv;
10334 struct nl_msg *msg;
10335 struct nlattr *attr;
10336 int ret = -1;
10337
10338 if (!drv->set_wifi_conf_vendor_cmd_avail)
10339 return -1;
10340
10341 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10342 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10343 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10344 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10345 goto fail;
10346
10347 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10348 if (!attr)
10349 goto fail;
10350
10351 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10352 ignore_disallow);
10353 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10354 ignore_disallow))
10355 goto fail;
10356
10357 nla_nest_end(msg, attr);
10358
10359 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10360 msg = NULL;
10361 if (ret) {
10362 wpa_printf(MSG_ERROR,
10363 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10364 ret, strerror(-ret));
10365 goto fail;
10366 }
10367
10368fail:
10369 nlmsg_free(msg);
10370 return ret;
10371}
10372
10373#endif /* CONFIG_MBO */
10374
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010375#endif /* CONFIG_DRIVER_NL80211_QCA */
10376
10377
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010378static int nl80211_write_to_file(const char *name, unsigned int val)
10379{
10380 int fd, len;
10381 char tmp[128];
10382
10383 fd = open(name, O_RDWR);
10384 if (fd < 0) {
10385 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10386 name, strerror(errno));
10387 return fd;
10388 }
10389
10390 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10391 len = write(fd, tmp, len);
10392 if (len < 0)
10393 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10394 name, strerror(errno));
10395 close(fd);
10396
10397 return 0;
10398}
10399
10400
10401static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10402{
10403 struct i802_bss *bss = priv;
10404 char path[128];
10405 int ret;
10406
10407 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10408 filter_flags);
10409
10410 /* Configure filtering of unicast frame encrypted using GTK */
10411 ret = os_snprintf(path, sizeof(path),
10412 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10413 bss->ifname);
10414 if (os_snprintf_error(sizeof(path), ret))
10415 return -1;
10416
10417 ret = nl80211_write_to_file(path,
10418 !!(filter_flags &
10419 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10420 if (ret) {
10421 wpa_printf(MSG_ERROR,
10422 "nl80211: Failed to set IPv4 unicast in multicast filter");
10423 return ret;
10424 }
10425
10426 os_snprintf(path, sizeof(path),
10427 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10428 bss->ifname);
10429 ret = nl80211_write_to_file(path,
10430 !!(filter_flags &
10431 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10432
10433 if (ret) {
10434 wpa_printf(MSG_ERROR,
10435 "nl80211: Failed to set IPv6 unicast in multicast filter");
10436 return ret;
10437 }
10438
10439 /* Configure filtering of unicast frame encrypted using GTK */
10440 os_snprintf(path, sizeof(path),
10441 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10442 bss->ifname);
10443 ret = nl80211_write_to_file(path,
10444 !!(filter_flags &
10445 WPA_DATA_FRAME_FILTER_FLAG_ARP));
10446 if (ret) {
10447 wpa_printf(MSG_ERROR,
10448 "nl80211: Failed set gratuitous ARP filter");
10449 return ret;
10450 }
10451
10452 /* Configure filtering of IPv6 NA frames */
10453 os_snprintf(path, sizeof(path),
10454 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10455 bss->ifname);
10456 ret = nl80211_write_to_file(path,
10457 !!(filter_flags &
10458 WPA_DATA_FRAME_FILTER_FLAG_NA));
10459 if (ret) {
10460 wpa_printf(MSG_ERROR,
10461 "nl80211: Failed to set unsolicited NA filter");
10462 return ret;
10463 }
10464
10465 return 0;
10466}
10467
10468
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010469static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10470 const u8 **ext_capa, const u8 **ext_capa_mask,
10471 unsigned int *ext_capa_len)
10472{
10473 struct i802_bss *bss = priv;
10474 struct wpa_driver_nl80211_data *drv = bss->drv;
10475 enum nl80211_iftype nlmode;
10476 unsigned int i;
10477
10478 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10479 return -1;
10480
10481 nlmode = wpa_driver_nl80211_if_type(type);
10482
10483 /* By default, use the per-radio values */
10484 *ext_capa = drv->extended_capa;
10485 *ext_capa_mask = drv->extended_capa_mask;
10486 *ext_capa_len = drv->extended_capa_len;
10487
10488 /* Replace the default value if a per-interface type value exists */
10489 for (i = 0; i < drv->num_iface_ext_capa; i++) {
10490 if (nlmode == drv->iface_ext_capa[i].iftype) {
10491 *ext_capa = drv->iface_ext_capa[i].ext_capa;
10492 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10493 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10494 break;
10495 }
10496 }
10497
10498 return 0;
10499}
10500
10501
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010502static int nl80211_update_connection_params(
10503 void *priv, struct wpa_driver_associate_params *params,
10504 enum wpa_drv_update_connect_params_mask mask)
10505{
10506 struct i802_bss *bss = priv;
10507 struct wpa_driver_nl80211_data *drv = bss->drv;
10508 struct nl_msg *msg;
10509 int ret = -1;
10510 enum nl80211_auth_type type;
10511
10512 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10513 if (!msg)
10514 goto fail;
10515
10516 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10517 drv->ifindex);
10518
10519 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10520 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10521 params->wpa_ie))
10522 goto fail;
10523 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
10524 params->wpa_ie_len);
10525 }
10526
10527 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10528 type = get_nl_auth_type(params->auth_alg);
10529 if (type == NL80211_AUTHTYPE_MAX ||
10530 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10531 goto fail;
10532 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
10533 }
10534
10535 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10536 nl80211_put_fils_connect_params(drv, params, msg))
10537 goto fail;
10538
10539 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10540 msg = NULL;
10541 if (ret)
10542 wpa_dbg(drv->ctx, MSG_DEBUG,
10543 "nl80211: Update connect params command failed: ret=%d (%s)",
10544 ret, strerror(-ret));
10545
10546fail:
10547 nlmsg_free(msg);
10548 return ret;
10549}
10550
10551
Roshan Pius3a1667e2018-07-03 15:17:14 -070010552static int nl80211_send_external_auth_status(void *priv,
10553 struct external_auth *params)
10554{
10555 struct i802_bss *bss = priv;
10556 struct wpa_driver_nl80211_data *drv = bss->drv;
10557 struct nl_msg *msg = NULL;
10558 int ret = -1;
10559
10560 wpa_dbg(drv->ctx, MSG_DEBUG,
10561 "nl80211: External auth status: %u", params->status);
10562
10563 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10564 if (!msg ||
10565 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10566 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10567 params->ssid) ||
10568 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10569 goto fail;
10570 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10571 msg = NULL;
10572 if (ret) {
10573 wpa_printf(MSG_DEBUG,
10574 "nl80211: External Auth status update failed: ret=%d (%s)",
10575 ret, strerror(-ret));
10576 goto fail;
10577 }
10578fail:
10579 nlmsg_free(msg);
10580 return ret;
10581}
10582
10583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010584const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10585 .name = "nl80211",
10586 .desc = "Linux nl80211/cfg80211",
10587 .get_bssid = wpa_driver_nl80211_get_bssid,
10588 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010589 .set_key = driver_nl80211_set_key,
10590 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010591 .sched_scan = wpa_driver_nl80211_sched_scan,
10592 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010593 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080010594 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010595 .deauthenticate = driver_nl80211_deauthenticate,
10596 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010597 .associate = wpa_driver_nl80211_associate,
10598 .global_init = nl80211_global_init,
10599 .global_deinit = nl80211_global_deinit,
10600 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010601 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010602 .get_capa = wpa_driver_nl80211_get_capa,
10603 .set_operstate = wpa_driver_nl80211_set_operstate,
10604 .set_supp_port = wpa_driver_nl80211_set_supp_port,
10605 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010606 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010607 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070010608 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010609 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010610 .if_remove = driver_nl80211_if_remove,
10611 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010612 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010613 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010614 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010615 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
10616 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010617 .hapd_init = i802_init,
10618 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070010619 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010620 .get_seqnum = i802_get_seqnum,
10621 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010622 .get_inact_sec = i802_get_inact_sec,
10623 .sta_clear_stats = i802_sta_clear_stats,
10624 .set_rts = i802_set_rts,
10625 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010626 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010627 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010628 .sta_deauth = i802_sta_deauth,
10629 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010630 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010631 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010632 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010633 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
10634 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10635 .cancel_remain_on_channel =
10636 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010637 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010638 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070010639 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010640 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010641 .signal_monitor = nl80211_signal_monitor,
10642 .signal_poll = nl80211_signal_poll,
10643 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010644 .set_param = nl80211_set_param,
10645 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070010646 .add_pmkid = nl80211_add_pmkid,
10647 .remove_pmkid = nl80211_remove_pmkid,
10648 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010649 .set_rekey_info = nl80211_set_rekey_info,
10650 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010651 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010652 .start_dfs_cac = nl80211_start_radar_detection,
10653 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010654#ifdef CONFIG_TDLS
10655 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10656 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010657 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10658 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010659#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010660 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010661 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010662 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070010663 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080010664 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010665#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010666 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010667 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010668 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010669#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070010670#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070010671#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070010672 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070010673#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010674#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080010675 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010676 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070010677 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010678 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010679#ifdef CONFIG_MESH
10680 .init_mesh = wpa_driver_nl80211_init_mesh,
10681 .join_mesh = wpa_driver_nl80211_join_mesh,
10682 .leave_mesh = wpa_driver_nl80211_leave_mesh,
10683#endif /* CONFIG_MESH */
10684 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10685 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
10686 .br_port_set_attr = wpa_driver_br_port_set_attr,
10687 .br_set_net_param = wpa_driver_br_set_net_param,
10688 .add_tx_ts = nl80211_add_ts,
10689 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070010690 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010691#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010692 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070010693 .disable_fils = nl80211_disable_fils,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010694 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -070010695 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010696 .get_pref_freq_list = nl80211_get_pref_freq_list,
10697 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010698 .p2p_lo_start = nl80211_p2p_lo_start,
10699 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070010700 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010701 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010702#ifdef CONFIG_MBO
10703 .get_bss_transition_status = nl80211_get_bss_transition_status,
10704 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
10705#endif /* CONFIG_MBO */
10706 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010707#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010708 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010709 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010710 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070010711 .send_external_auth_status = nl80211_send_external_auth_status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010712};