blob: 5cff47fab6da49baca82195917f405172a0d0636 [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)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001083 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1085 "event since interface %s is down",
1086 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001087 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001088 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1089 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001090 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001091 } else if (drv->if_removed) {
1092 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1093 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001094 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001095 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001096 /* Re-read MAC address as it may have changed */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001097 nl80211_refresh_mac(drv, ifi->ifi_index, 0);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001099 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1100 drv->if_disabled = 0;
1101 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1102 NULL);
1103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 }
1105
1106 /*
1107 * Some drivers send the association event before the operup event--in
1108 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1109 * fails. This will hit us when wpa_supplicant does not need to do
1110 * IEEE 802.1X authentication
1111 */
1112 if (drv->operstate == 1 &&
1113 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001114 !(ifi->ifi_flags & IFF_RUNNING)) {
1115 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001116 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 }
1119
Dmitry Shmidte4663042016-04-04 10:07:49 -07001120event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001121 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001122 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1123 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001124
Dmitry Shmidte4663042016-04-04 10:07:49 -07001125 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001126 struct i802_bss *bss;
1127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001129 if (!if_indextoname(brid, namebuf)) {
1130 wpa_printf(MSG_DEBUG,
1131 "nl80211: Could not find bridge ifname for ifindex %u",
1132 brid);
1133 return;
1134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1136 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001137 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001138
1139 for (bss = drv->first_bss; bss; bss = bss->next) {
1140 if (os_strcmp(ifname, bss->ifname) == 0) {
1141 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1142 break;
1143 }
1144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146}
1147
1148
1149static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1150 struct ifinfomsg *ifi,
1151 u8 *buf, size_t len)
1152{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001153 struct nl80211_global *global = ctx;
1154 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001155 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 struct rtattr *attr;
1157 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001158 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001159 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001161 extra[0] = '\0';
1162 pos = extra;
1163 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001164 ifname[0] = '\0';
1165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 attrlen = len;
1167 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001169 switch (attr->rta_type) {
1170 case IFLA_IFNAME:
1171 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1172 break;
1173 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1174 ifname[RTA_PAYLOAD(attr)] = '\0';
1175 break;
1176 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001178 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1179 break;
1180 case IFLA_OPERSTATE:
1181 pos += os_snprintf(pos, end - pos, " operstate=%u",
1182 nla_get_u32((struct nlattr *) attr));
1183 break;
1184 case IFLA_LINKMODE:
1185 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1186 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001187 break;
1188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 attr = RTA_NEXT(attr, attrlen);
1190 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001191 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001193 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1194 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1195 ifi->ifi_flags,
1196 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1197 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1198 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1199 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1200
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001201 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001202
Dmitry Shmidte4663042016-04-04 10:07:49 -07001203 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 /* device has been removed from bridge */
1205 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001206
1207 if (!if_indextoname(brid, namebuf)) {
1208 wpa_printf(MSG_DEBUG,
1209 "nl80211: Could not find bridge ifname for ifindex %u",
1210 brid);
1211 } else {
1212 wpa_printf(MSG_DEBUG,
1213 "nl80211: Remove ifindex %u for bridge %s",
1214 brid, namebuf);
1215 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001216 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001218
1219 if (ifi->ifi_family != AF_BRIDGE || !brid)
1220 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1221 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222}
1223
1224
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001225struct nl80211_get_assoc_freq_arg {
1226 struct wpa_driver_nl80211_data *drv;
1227 unsigned int assoc_freq;
1228 unsigned int ibss_freq;
1229 u8 assoc_bssid[ETH_ALEN];
1230 u8 assoc_ssid[SSID_MAX_LEN];
1231 u8 assoc_ssid_len;
1232};
1233
1234static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1235{
1236 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1237 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1238 struct nlattr *bss[NL80211_BSS_MAX + 1];
1239 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1240 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1241 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1242 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1243 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1244 };
1245 struct nl80211_get_assoc_freq_arg *ctx = arg;
1246 enum nl80211_bss_status status;
1247
1248 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1249 genlmsg_attrlen(gnlh, 0), NULL);
1250 if (!tb[NL80211_ATTR_BSS] ||
1251 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1252 bss_policy) ||
1253 !bss[NL80211_BSS_STATUS])
1254 return NL_SKIP;
1255
1256 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1257 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1258 bss[NL80211_BSS_FREQUENCY]) {
1259 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1260 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1261 ctx->assoc_freq);
1262 }
1263 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1264 bss[NL80211_BSS_FREQUENCY]) {
1265 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1266 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1267 ctx->ibss_freq);
1268 }
1269 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1270 bss[NL80211_BSS_BSSID]) {
1271 os_memcpy(ctx->assoc_bssid,
1272 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1273 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1274 MACSTR, MAC2STR(ctx->assoc_bssid));
1275 }
1276
1277 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1278 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1279 const u8 *ie, *ssid;
1280 size_t ie_len;
1281
1282 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1283 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1284 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1285 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1286 ctx->assoc_ssid_len = ssid[1];
1287 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1288 }
1289 }
1290
1291 return NL_SKIP;
1292}
1293
1294
1295int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001296{
1297 struct nl_msg *msg;
1298 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001299 struct nl80211_get_assoc_freq_arg arg;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001300
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001301 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001302 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001303 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001304 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1305 &arg);
1306 if (ret == 0) {
1307 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1308 return arg.assoc_ssid_len;
1309 }
1310 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1311 ret, strerror(-ret));
1312 return ret;
1313}
1314
1315
1316unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1317{
1318 struct nl_msg *msg;
1319 int ret;
1320 struct nl80211_get_assoc_freq_arg arg;
1321
1322 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1323 os_memset(&arg, 0, sizeof(arg));
1324 arg.drv = drv;
1325 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1326 &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001327 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001328 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1329 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001330 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001331 "associated BSS from scan results: %u MHz", freq);
1332 if (freq)
1333 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001334 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001335 }
1336 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1337 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001338 return drv->assoc_freq;
1339}
1340
1341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342static int get_link_signal(struct nl_msg *msg, void *arg)
1343{
1344 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1345 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1346 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1347 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1348 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001349 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001350 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 };
1352 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1353 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1354 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1355 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1356 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1357 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1358 };
1359 struct wpa_signal_info *sig_change = arg;
1360
1361 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1362 genlmsg_attrlen(gnlh, 0), NULL);
1363 if (!tb[NL80211_ATTR_STA_INFO] ||
1364 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1365 tb[NL80211_ATTR_STA_INFO], policy))
1366 return NL_SKIP;
1367 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1368 return NL_SKIP;
1369
1370 sig_change->current_signal =
1371 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1372
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001373 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1374 sig_change->avg_signal =
1375 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1376 else
1377 sig_change->avg_signal = 0;
1378
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001379 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1380 sig_change->avg_beacon_signal =
1381 (s8)
1382 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1383 else
1384 sig_change->avg_beacon_signal = 0;
1385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1387 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1388 sinfo[NL80211_STA_INFO_TX_BITRATE],
1389 rate_policy)) {
1390 sig_change->current_txrate = 0;
1391 } else {
1392 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1393 sig_change->current_txrate =
1394 nla_get_u16(rinfo[
1395 NL80211_RATE_INFO_BITRATE]) * 100;
1396 }
1397 }
1398 }
1399
1400 return NL_SKIP;
1401}
1402
1403
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001404int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1405 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406{
1407 struct nl_msg *msg;
1408
1409 sig->current_signal = -9999;
1410 sig->current_txrate = 0;
1411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001412 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1413 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1414 nlmsg_free(msg);
1415 return -ENOBUFS;
1416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417
1418 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419}
1420
1421
1422static int get_link_noise(struct nl_msg *msg, void *arg)
1423{
1424 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1425 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1426 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1427 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1428 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1429 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1430 };
1431 struct wpa_signal_info *sig_change = arg;
1432
1433 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1434 genlmsg_attrlen(gnlh, 0), NULL);
1435
1436 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1437 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1438 return NL_SKIP;
1439 }
1440
1441 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1442 tb[NL80211_ATTR_SURVEY_INFO],
1443 survey_policy)) {
1444 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1445 "attributes!");
1446 return NL_SKIP;
1447 }
1448
1449 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1450 return NL_SKIP;
1451
1452 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1453 sig_change->frequency)
1454 return NL_SKIP;
1455
1456 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1457 return NL_SKIP;
1458
1459 sig_change->current_noise =
1460 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1461
1462 return NL_SKIP;
1463}
1464
1465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001466int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1467 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468{
1469 struct nl_msg *msg;
1470
1471 sig_change->current_noise = 9999;
1472 sig_change->frequency = drv->assoc_freq;
1473
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001474 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476}
1477
1478
1479static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1480 void *handle)
1481{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001482 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001483 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001485 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001487 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001488 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001489 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1490 __func__, res);
1491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492}
1493
1494
1495/**
1496 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1497 * @priv: driver_nl80211 private data
1498 * @alpha2_arg: country to which to switch to
1499 * Returns: 0 on success, -1 on failure
1500 *
1501 * This asks nl80211 to set the regulatory domain for given
1502 * country ISO / IEC alpha2.
1503 */
1504static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1505{
1506 struct i802_bss *bss = priv;
1507 struct wpa_driver_nl80211_data *drv = bss->drv;
1508 char alpha2[3];
1509 struct nl_msg *msg;
1510
1511 msg = nlmsg_alloc();
1512 if (!msg)
1513 return -ENOMEM;
1514
1515 alpha2[0] = alpha2_arg[0];
1516 alpha2[1] = alpha2_arg[1];
1517 alpha2[2] = '\0';
1518
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001519 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1520 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1521 nlmsg_free(msg);
1522 return -EINVAL;
1523 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1525 return -EINVAL;
1526 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527}
1528
1529
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001530static int nl80211_get_country(struct nl_msg *msg, void *arg)
1531{
1532 char *alpha2 = arg;
1533 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1534 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1535
1536 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1537 genlmsg_attrlen(gnlh, 0), NULL);
1538 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1539 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1540 return NL_SKIP;
1541 }
1542 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1543 return NL_SKIP;
1544}
1545
1546
1547static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1548{
1549 struct i802_bss *bss = priv;
1550 struct wpa_driver_nl80211_data *drv = bss->drv;
1551 struct nl_msg *msg;
1552 int ret;
1553
1554 msg = nlmsg_alloc();
1555 if (!msg)
1556 return -ENOMEM;
1557
1558 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1559 alpha2[0] = '\0';
1560 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1561 if (!alpha2[0])
1562 ret = -1;
1563
1564 return ret;
1565}
1566
1567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001568static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569{
1570 int ret;
1571
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1573 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1575 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001576 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 }
1578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579 global->nl = nl_create_handle(global->nl_cb, "nl");
1580 if (global->nl == NULL)
1581 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001583 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1584 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1586 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001587 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 }
1589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001590 global->nl_event = nl_create_handle(global->nl_cb, "event");
1591 if (global->nl_event == NULL)
1592 goto err;
1593
1594 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001596 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 if (ret < 0) {
1598 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1599 "membership for scan events: %d (%s)",
1600 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001601 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 }
1603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001606 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 if (ret < 0) {
1608 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1609 "membership for mlme events: %d (%s)",
1610 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612 }
1613
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001614 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001616 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 if (ret < 0) {
1618 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1619 "membership for regulatory events: %d (%s)",
1620 ret, strerror(-ret));
1621 /* Continue without regulatory events */
1622 }
1623
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001624 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1625 if (ret >= 0)
1626 ret = nl_socket_add_membership(global->nl_event, ret);
1627 if (ret < 0) {
1628 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1629 "membership for vendor events: %d (%s)",
1630 ret, strerror(-ret));
1631 /* Continue without vendor events */
1632 }
1633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1635 no_seq_check, NULL);
1636 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1637 process_global_event, global);
1638
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001639 nl80211_register_eloop_read(&global->nl_event,
1640 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001641 global->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642
1643 return 0;
1644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001645err:
1646 nl_destroy_handles(&global->nl_event);
1647 nl_destroy_handles(&global->nl);
1648 nl_cb_put(global->nl_cb);
1649 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650 return -1;
1651}
1652
1653
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001654static void nl80211_check_global(struct nl80211_global *global)
1655{
1656 struct nl_handle *handle;
1657 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1658 int ret;
1659 unsigned int i;
1660
1661 /*
1662 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1663 * and all registration having been cleared.
1664 */
1665 handle = (void *) (((intptr_t) global->nl_event) ^
1666 ELOOP_SOCKET_INVALID);
1667
1668 for (i = 0; groups[i]; i++) {
1669 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1670 if (ret >= 0)
1671 ret = nl_socket_add_membership(handle, ret);
1672 if (ret < 0) {
1673 wpa_printf(MSG_INFO,
1674 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1675 groups[i], ret, strerror(-ret));
1676 }
1677 }
1678}
1679
1680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1682{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001683 struct wpa_driver_nl80211_data *drv = ctx;
1684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001688 * rtnetlink ifdown handler will report interfaces other than the P2P
1689 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001691 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1692 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693}
1694
1695
1696static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1697{
1698 struct wpa_driver_nl80211_data *drv = ctx;
1699 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001700 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1702 "after rfkill unblock");
1703 return;
1704 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001705
1706 if (is_p2p_net_interface(drv->nlmode))
1707 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1708
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001709 /*
1710 * rtnetlink ifup handler will report interfaces other than the P2P
1711 * Device interface as enabled.
1712 */
1713 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1714 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715}
1716
1717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1719 void *eloop_ctx,
1720 void *handle)
1721{
1722 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1723 u8 data[2048];
1724 struct msghdr msg;
1725 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001726 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001727 struct cmsghdr *cmsg;
1728 int res, found_ee = 0, found_wifi = 0, acked = 0;
1729 union wpa_event_data event;
1730
1731 memset(&msg, 0, sizeof(msg));
1732 msg.msg_iov = &entry;
1733 msg.msg_iovlen = 1;
1734 entry.iov_base = data;
1735 entry.iov_len = sizeof(data);
1736 msg.msg_control = &control;
1737 msg.msg_controllen = sizeof(control);
1738
1739 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1740 /* if error or not fitting 802.3 header, return */
1741 if (res < 14)
1742 return;
1743
1744 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1745 {
1746 if (cmsg->cmsg_level == SOL_SOCKET &&
1747 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1748 int *ack;
1749
1750 found_wifi = 1;
1751 ack = (void *)CMSG_DATA(cmsg);
1752 acked = *ack;
1753 }
1754
1755 if (cmsg->cmsg_level == SOL_PACKET &&
1756 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1757 struct sock_extended_err *err =
1758 (struct sock_extended_err *)CMSG_DATA(cmsg);
1759
1760 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1761 found_ee = 1;
1762 }
1763 }
1764
1765 if (!found_ee || !found_wifi)
1766 return;
1767
1768 memset(&event, 0, sizeof(event));
1769 event.eapol_tx_status.dst = data;
1770 event.eapol_tx_status.data = data + 14;
1771 event.eapol_tx_status.data_len = res - 14;
1772 event.eapol_tx_status.ack = acked;
1773 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1774}
1775
1776
1777static int nl80211_init_bss(struct i802_bss *bss)
1778{
1779 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1780 if (!bss->nl_cb)
1781 return -1;
1782
1783 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1784 no_seq_check, NULL);
1785 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1786 process_bss_event, bss);
1787
1788 return 0;
1789}
1790
1791
1792static void nl80211_destroy_bss(struct i802_bss *bss)
1793{
1794 nl_cb_put(bss->nl_cb);
1795 bss->nl_cb = NULL;
1796}
1797
1798
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001799static void
1800wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1801{
1802 struct rfkill_config *rcfg;
1803
1804 if (drv->rfkill)
1805 return;
1806
1807 rcfg = os_zalloc(sizeof(*rcfg));
1808 if (!rcfg)
1809 return;
1810
1811 rcfg->ctx = drv;
1812
1813 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1814 * not associated with a netdev, so use the name of some other interface
1815 * sharing the same wiphy as the P2P Device interface.
1816 *
1817 * Note: This is valid, as a P2P Device interface is always dynamically
1818 * created and is created only once another wpa_s interface was added.
1819 */
1820 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1821 struct nl80211_global *global = drv->global;
1822 struct wpa_driver_nl80211_data *tmp1;
1823
1824 dl_list_for_each(tmp1, &global->interfaces,
1825 struct wpa_driver_nl80211_data, list) {
1826 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1827 !tmp1->rfkill)
1828 continue;
1829
1830 wpa_printf(MSG_DEBUG,
1831 "nl80211: Use (%s) to initialize P2P Device rfkill",
1832 tmp1->first_bss->ifname);
1833 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1834 sizeof(rcfg->ifname));
1835 break;
1836 }
1837 } else {
1838 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1839 sizeof(rcfg->ifname));
1840 }
1841
1842 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1843 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1844 drv->rfkill = rfkill_init(rcfg);
1845 if (!drv->rfkill) {
1846 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1847 os_free(rcfg);
1848 }
1849}
1850
1851
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001852static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1853 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001854 const u8 *set_addr,
1855 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856{
1857 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858 struct i802_bss *bss;
1859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001860 if (global_priv == NULL)
1861 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 drv = os_zalloc(sizeof(*drv));
1863 if (drv == NULL)
1864 return NULL;
1865 drv->global = global_priv;
1866 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001867 drv->hostapd = !!hostapd;
1868 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001869
1870 /*
1871 * There is no driver capability flag for this, so assume it is
1872 * supported and disable this on first attempt to use if the driver
1873 * rejects the command due to missing support.
1874 */
1875 drv->set_rekey_offload = 1;
1876
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001877 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1878 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001879 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001880
1881 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1882 if (!drv->first_bss) {
1883 os_free(drv);
1884 return NULL;
1885 }
1886 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001888 bss->ctx = ctx;
1889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1891 drv->monitor_ifidx = -1;
1892 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001893 drv->eapol_tx_sock = -1;
1894 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896 if (nl80211_init_bss(bss))
1897 goto failed;
1898
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001899 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 goto failed;
1901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001902 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1903 if (drv->eapol_tx_sock < 0)
1904 goto failed;
1905
1906 if (drv->data_tx_status) {
1907 int enabled = 1;
1908
1909 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1910 &enabled, sizeof(enabled)) < 0) {
1911 wpa_printf(MSG_DEBUG,
1912 "nl80211: wifi status sockopt failed\n");
1913 drv->data_tx_status = 0;
1914 if (!drv->use_monitor)
1915 drv->capa.flags &=
1916 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1917 } else {
1918 eloop_register_read_sock(drv->eapol_tx_sock,
1919 wpa_driver_nl80211_handle_eapol_tx_status,
1920 drv, NULL);
1921 }
1922 }
1923
1924 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001925 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001927 drv->in_interface_list = 1;
1928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929
1930 return bss;
1931
1932failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001933 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 return NULL;
1935}
1936
1937
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001938/**
1939 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1940 * @ctx: context to be used when calling wpa_supplicant functions,
1941 * e.g., wpa_supplicant_event()
1942 * @ifname: interface name, e.g., wlan0
1943 * @global_priv: private driver global data from global_init()
1944 * Returns: Pointer to private data, %NULL on failure
1945 */
1946static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1947 void *global_priv)
1948{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001949 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1950 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001951}
1952
1953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 struct nl_handle *nl_handle,
1956 u16 type, const u8 *match, size_t match_len)
1957{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001958 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001960 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001961 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001963 buf[0] = '\0';
1964 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001965 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1966 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001968 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1969 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1970 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1971 nlmsg_free(msg);
1972 return -1;
1973 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001975 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 if (ret) {
1977 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1978 "failed (type=%u): ret=%d (%s)",
1979 type, ret, strerror(-ret));
1980 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1981 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 return ret;
1984}
1985
1986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001987static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1988{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989 if (bss->nl_mgmt) {
1990 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1991 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1992 return -1;
1993 }
1994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001995 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 if (bss->nl_mgmt == NULL)
1997 return -1;
1998
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999 return 0;
2000}
2001
2002
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002003static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2004{
2005 nl80211_register_eloop_read(&bss->nl_mgmt,
2006 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002007 bss->nl_cb, 0);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002008}
2009
2010
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 const u8 *match, size_t match_len)
2013{
2014 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002015 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 type, match, match_len);
2017}
2018
2019
Roshan Pius3a1667e2018-07-03 15:17:14 -07002020static int nl80211_init_connect_handle(struct i802_bss *bss)
2021{
2022 if (bss->nl_connect) {
2023 wpa_printf(MSG_DEBUG,
2024 "nl80211: Connect handle already created (nl_connect=%p)",
2025 bss->nl_connect);
2026 return -1;
2027 }
2028
2029 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2030 if (!bss->nl_connect)
2031 return -1;
2032 nl80211_register_eloop_read(&bss->nl_connect,
2033 wpa_driver_nl80211_event_receive,
2034 bss->nl_cb, 1);
2035 return 0;
2036}
2037
2038
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002039static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002041 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002042 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002043
2044 if (nl80211_alloc_mgmt_handle(bss))
2045 return -1;
2046 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2047 "handle %p", bss->nl_mgmt);
2048
Roshan Pius3a1667e2018-07-03 15:17:14 -07002049 if (drv->nlmode == NL80211_IFTYPE_ADHOC ||
2050 ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2051 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002052 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2053
2054 /* register for any AUTH message */
2055 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
2056 }
2057
Dmitry Shmidt051af732013-10-22 13:52:46 -07002058#ifdef CONFIG_INTERWORKING
2059 /* QoS Map Configure */
2060 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002061 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002062#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002063#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002065 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002066 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002069 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002071 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002072 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002074 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002075 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002076 /* Protected GAS Initial Request */
2077 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2078 ret = -1;
2079 /* Protected GAS Initial Response */
2080 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2081 ret = -1;
2082 /* Protected GAS Comeback Request */
2083 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2084 ret = -1;
2085 /* Protected GAS Comeback Response */
2086 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2087 ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002088#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002089#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002091 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2093 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002094 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097 (u8 *) "\x7f\x50\x6f\x9a\x09",
2098 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002099 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100#endif /* CONFIG_P2P */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002101#ifdef CONFIG_DPP
2102 /* DPP Public Action */
2103 if (nl80211_register_action_frame(bss,
2104 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2105 6) < 0)
2106 ret = -1;
2107#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108#ifdef CONFIG_IEEE80211W
2109 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002111 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002113#ifdef CONFIG_TDLS
2114 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2115 /* TDLS Discovery Response */
2116 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2117 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002118 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002119 }
2120#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002121#ifdef CONFIG_FST
2122 /* FST Action frames */
2123 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2124 ret = -1;
2125#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126
2127 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002128 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002129 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 else
2131 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2132 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2133
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002134 /* WNM - BSS Transition Management Request */
2135 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002136 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002137 /* WNM-Sleep Mode Response */
2138 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002139 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002141#ifdef CONFIG_HS20
2142 /* WNM-Notification */
2143 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002144 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002145#endif /* CONFIG_HS20 */
2146
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002147 /* WMM-AC ADDTS Response */
2148 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2149 ret = -1;
2150
2151 /* WMM-AC DELTS */
2152 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2153 ret = -1;
2154
2155 /* Radio Measurement - Neighbor Report Response */
2156 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2157 ret = -1;
2158
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002159 /* Radio Measurement - Radio Measurement Request */
2160 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2161 ret = -1;
2162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002163 /* Radio Measurement - Link Measurement Request */
2164 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2165 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2166 ret = -1;
2167
2168 nl80211_mgmt_handle_register_eloop(bss);
2169
2170 return ret;
2171}
2172
2173
2174static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2175{
2176 int ret = 0;
2177
2178 if (nl80211_alloc_mgmt_handle(bss))
2179 return -1;
2180
2181 wpa_printf(MSG_DEBUG,
2182 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2183 bss->nl_mgmt);
2184
2185 /* Auth frames for mesh SAE */
2186 if (nl80211_register_frame(bss, bss->nl_mgmt,
2187 (WLAN_FC_TYPE_MGMT << 2) |
2188 (WLAN_FC_STYPE_AUTH << 4),
2189 NULL, 0) < 0)
2190 ret = -1;
2191
2192 /* Mesh peering open */
2193 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2194 ret = -1;
2195 /* Mesh peering confirm */
2196 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2197 ret = -1;
2198 /* Mesh peering close */
2199 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2200 ret = -1;
2201
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002202 nl80211_mgmt_handle_register_eloop(bss);
2203
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002204 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205}
2206
2207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002208static int nl80211_register_spurious_class3(struct i802_bss *bss)
2209{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002210 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002211 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002213 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2214 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002215 if (ret) {
2216 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2217 "failed: ret=%d (%s)",
2218 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002219 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002220 return ret;
2221}
2222
2223
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002224static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2225{
2226 int ret = 0;
2227
2228 /* Public Action frames */
2229 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2230 ret = -1;
2231 /* RRM Measurement Report */
2232 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2233 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002234 /* RRM Link Measurement Report */
2235 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2236 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002237 /* RRM Neighbor Report Request */
2238 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2239 ret = -1;
2240 /* FT Action frames */
2241 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2242 ret = -1;
2243#ifdef CONFIG_IEEE80211W
2244 /* SA Query */
2245 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2246 ret = -1;
2247#endif /* CONFIG_IEEE80211W */
2248 /* Protected Dual of Public Action */
2249 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2250 ret = -1;
2251 /* WNM */
2252 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2253 ret = -1;
2254 /* WMM */
2255 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2256 ret = -1;
2257#ifdef CONFIG_FST
2258 /* FST Action frames */
2259 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2260 ret = -1;
2261#endif /* CONFIG_FST */
2262 /* Vendor-specific */
2263 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2264 ret = -1;
2265
2266 return ret;
2267}
2268
2269
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2271{
2272 static const int stypes[] = {
2273 WLAN_FC_STYPE_AUTH,
2274 WLAN_FC_STYPE_ASSOC_REQ,
2275 WLAN_FC_STYPE_REASSOC_REQ,
2276 WLAN_FC_STYPE_DISASSOC,
2277 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002278 WLAN_FC_STYPE_PROBE_REQ,
2279/* Beacon doesn't work as mac80211 doesn't currently allow
2280 * it, but it wouldn't really be the right thing anyway as
2281 * it isn't per interface ... maybe just dump the scan
2282 * results periodically for OLBC?
2283 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002284 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002285 };
2286 unsigned int i;
2287
2288 if (nl80211_alloc_mgmt_handle(bss))
2289 return -1;
2290 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2291 "handle %p", bss->nl_mgmt);
2292
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002293 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002294 if (nl80211_register_frame(bss, bss->nl_mgmt,
2295 (WLAN_FC_TYPE_MGMT << 2) |
2296 (stypes[i] << 4),
2297 NULL, 0) < 0) {
2298 goto out_err;
2299 }
2300 }
2301
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002302 if (nl80211_action_subscribe_ap(bss))
2303 goto out_err;
2304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002305 if (nl80211_register_spurious_class3(bss))
2306 goto out_err;
2307
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002308 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002309 return 0;
2310
2311out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 nl_destroy_handles(&bss->nl_mgmt);
2313 return -1;
2314}
2315
2316
2317static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2318{
2319 if (nl80211_alloc_mgmt_handle(bss))
2320 return -1;
2321 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2322 "handle %p (device SME)", bss->nl_mgmt);
2323
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002324 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002325 goto out_err;
2326
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002327 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002328 return 0;
2329
2330out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002331 nl_destroy_handles(&bss->nl_mgmt);
2332 return -1;
2333}
2334
2335
2336static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2337{
2338 if (bss->nl_mgmt == NULL)
2339 return;
2340 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2341 "(%s)", bss->nl_mgmt, reason);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002342 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002343
2344 nl80211_put_wiphy_data_ap(bss);
2345}
2346
2347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2349{
2350 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2351}
2352
2353
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002354static void nl80211_del_p2pdev(struct i802_bss *bss)
2355{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002356 struct nl_msg *msg;
2357 int ret;
2358
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002359 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2360 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002361
2362 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2363 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002364 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002365}
2366
2367
2368static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2369{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002370 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002371 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002372
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002373 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2374 NL80211_CMD_STOP_P2P_DEVICE);
2375 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002376
2377 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2378 start ? "Start" : "Stop",
2379 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002380 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002381 return ret;
2382}
2383
2384
2385static int i802_set_iface_flags(struct i802_bss *bss, int up)
2386{
2387 enum nl80211_iftype nlmode;
2388
2389 nlmode = nl80211_get_ifmode(bss);
2390 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2391 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2392 bss->ifname, up);
2393 }
2394
2395 /* P2P Device has start/stop which is equivalent */
2396 return nl80211_set_p2pdev(bss, up);
2397}
2398
2399
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002400#ifdef CONFIG_TESTING_OPTIONS
2401static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2402{
2403 /* struct wpa_driver_nl80211_data *drv = arg; */
2404 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2405 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2406
2407
2408 wpa_printf(MSG_DEBUG,
2409 "nl80211: QCA vendor test command response received");
2410
2411 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2412 genlmsg_attrlen(gnlh, 0), NULL);
2413 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2414 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2415 return NL_SKIP;
2416 }
2417
2418 wpa_hexdump(MSG_DEBUG,
2419 "nl80211: Received QCA vendor test command response",
2420 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2421 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2422
2423 return NL_SKIP;
2424}
2425#endif /* CONFIG_TESTING_OPTIONS */
2426
2427
2428static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2429{
2430#ifdef CONFIG_TESTING_OPTIONS
2431 struct nl_msg *msg;
2432 struct nlattr *params;
2433 int ret;
2434
2435 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2436 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2437 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2438 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2439 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2440 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2441 nlmsg_free(msg);
2442 return;
2443 }
2444 nla_nest_end(msg, params);
2445
2446 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2447 wpa_printf(MSG_DEBUG,
2448 "nl80211: QCA vendor test command returned %d (%s)",
2449 ret, strerror(-ret));
2450#endif /* CONFIG_TESTING_OPTIONS */
2451}
2452
2453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002455wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002456 const u8 *set_addr, int first,
2457 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002459 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002461 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462
2463 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002464 bss->ifindex = drv->ifindex;
2465 bss->wdev_id = drv->global->if_add_wdevid;
2466 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2467
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002468 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2469 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002470 drv->global->if_add_wdevid_set = 0;
2471
Dmitry Shmidt03658832014-08-13 11:03:49 -07002472 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2473 bss->static_ap = 1;
2474
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002475 if (first &&
2476 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2477 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2478 drv->start_iface_up = 1;
2479
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002480 if (wpa_driver_nl80211_capa(drv))
2481 return -1;
2482
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002483 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2484 return -1;
2485
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002486 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2487 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002489 if (set_addr &&
2490 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2491 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2492 set_addr)))
2493 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002494
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002495 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2496 drv->start_mode_ap = 1;
2497
Dmitry Shmidt03658832014-08-13 11:03:49 -07002498 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002499 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002500 else if (bss->if_dynamic ||
2501 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002502 nlmode = nl80211_get_ifmode(bss);
2503 else
2504 nlmode = NL80211_IFTYPE_STATION;
2505
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002506 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002507 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002508 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002510
Dmitry Shmidt98660862014-03-11 17:26:21 -07002511 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002512 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002514 wpa_driver_nl80211_drv_init_rfkill(drv);
2515
Dmitry Shmidt98660862014-03-11 17:26:21 -07002516 if (!rfkill_is_blocked(drv->rfkill)) {
2517 int ret = i802_set_iface_flags(bss, 1);
2518 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2520 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002521 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002523
2524 if (is_p2p_net_interface(nlmode))
2525 nl80211_disable_11b_rates(bss->drv,
2526 bss->drv->ifindex, 1);
2527
Dmitry Shmidt98660862014-03-11 17:26:21 -07002528 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2529 return ret;
2530 } else {
2531 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2532 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002533 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2534 drv->if_disabled = 1;
2535
Dmitry Shmidt98660862014-03-11 17:26:21 -07002536 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 }
2538
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002539 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002540 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2541 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002543 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2544 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2545 bss->addr))
2546 return -1;
2547 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550 if (send_rfkill_event) {
2551 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2552 drv, drv->ctx);
2553 }
2554
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002555 if (drv->vendor_cmd_test_avail)
2556 qca_vendor_test(drv);
2557
Roshan Pius3a1667e2018-07-03 15:17:14 -07002558 nl80211_init_connect_handle(bss);
2559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560 return 0;
2561}
2562
2563
Paul Stewart092955c2017-02-06 09:13:09 -08002564static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565{
2566 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08002567 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002569 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2570 drv->ifindex);
Paul Stewart092955c2017-02-06 09:13:09 -08002571 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002572 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574}
2575
2576
2577/**
2578 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002579 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 *
2581 * Shut down driver interface and processing of driver events. Free
2582 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2583 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002584static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002587 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002589 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2590 bss->ifname, drv->disabled_11b_rates);
2591
Dmitry Shmidt04949592012-07-19 12:16:46 -07002592 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002593 if (drv->data_tx_status)
2594 eloop_unregister_read_sock(drv->eapol_tx_sock);
2595 if (drv->eapol_tx_sock >= 0)
2596 close(drv->eapol_tx_sock);
2597
2598 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599 wpa_driver_nl80211_probe_req_report(bss, 0);
2600 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002601 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2602 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2604 "interface %s from bridge %s: %s",
2605 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002606 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002607 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 }
2609 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002610 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2611 0) < 0)
2612 wpa_printf(MSG_INFO,
2613 "nl80211: Could not set bridge %s down",
2614 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002615 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2617 "bridge %s: %s",
2618 bss->brname, strerror(errno));
2619 }
2620
2621 nl80211_remove_monitor_interface(drv);
2622
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002623 if (is_ap_interface(drv->nlmode))
Paul Stewart092955c2017-02-06 09:13:09 -08002624 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 if (drv->eapol_sock >= 0) {
2627 eloop_unregister_read_sock(drv->eapol_sock);
2628 close(drv->eapol_sock);
2629 }
2630
2631 if (drv->if_indices != drv->default_if_indices)
2632 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002634 if (drv->if_indices_reason != drv->default_if_indices_reason)
2635 os_free(drv->if_indices_reason);
2636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002637 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2639
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2641 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002642 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 rfkill_deinit(drv->rfkill);
2644
2645 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2646
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002647 if (!drv->start_iface_up)
2648 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002649
2650 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002651 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2652 0) < 0) {
2653 wpa_printf(MSG_DEBUG,
2654 "nl80211: Could not set interface down to restore permanent MAC address");
2655 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002656 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2657 drv->perm_addr) < 0) {
2658 wpa_printf(MSG_DEBUG,
2659 "nl80211: Could not restore permanent MAC address");
2660 }
2661 }
2662
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002663 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002664 if (!drv->hostapd || !drv->start_mode_ap)
2665 wpa_driver_nl80211_set_mode(bss,
2666 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002667 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002668 } else {
2669 nl80211_mgmt_unsubscribe(bss, "deinit");
2670 nl80211_del_p2pdev(bss);
2671 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672
Roshan Pius3a1667e2018-07-03 15:17:14 -07002673 if (bss->nl_connect)
2674 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
2675
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002676 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 os_free(drv->filter_ssids);
2679
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002680 os_free(drv->auth_ie);
2681
2682 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 dl_list_del(&drv->list);
2684
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002685 os_free(drv->extended_capa);
2686 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002687 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2688 os_free(drv->iface_ext_capa[i].ext_capa);
2689 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2690 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002691 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 os_free(drv);
2693}
2694
2695
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002696static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2697{
2698 switch (alg) {
2699 case WPA_ALG_WEP:
2700 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08002701 return RSN_CIPHER_SUITE_WEP40;
2702 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002703 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002704 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002705 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002706 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002707 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002708 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002709 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002710 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002711 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002712 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002713 case WPA_ALG_IGTK:
Paul Stewart092955c2017-02-06 09:13:09 -08002714 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002715 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002716 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002717 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002718 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002720 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002721 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08002722 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002723 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08002724 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002725 case WPA_ALG_NONE:
2726 case WPA_ALG_PMK:
2727 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2728 alg);
2729 return 0;
2730 }
2731
2732 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2733 alg);
2734 return 0;
2735}
2736
2737
2738static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2739{
2740 switch (cipher) {
2741 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002742 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002743 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002744 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002745 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002746 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002747 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002748 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002749 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002750 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002751 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08002752 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002753 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08002754 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002755 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08002756 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002757 }
2758
2759 return 0;
2760}
2761
2762
2763static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2764 int max_suites)
2765{
2766 int num_suites = 0;
2767
2768 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002769 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002770 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002771 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002772 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002773 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002774 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002775 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002776 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08002777 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002778 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08002779 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002780 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08002781 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002782
2783 return num_suites;
2784}
2785
2786
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002787#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002788static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2789 const u8 *key, size_t key_len)
2790{
2791 struct nl_msg *msg;
2792 int ret;
2793
2794 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2795 return 0;
2796
2797 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2798 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2799 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2800 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2801 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2802 nl80211_nlmsg_clear(msg);
2803 nlmsg_free(msg);
2804 return -1;
2805 }
2806 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2807 if (ret) {
2808 wpa_printf(MSG_DEBUG,
2809 "nl80211: Key management set key failed: ret=%d (%s)",
2810 ret, strerror(-ret));
2811 }
2812
2813 return ret;
2814}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002815#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002816
2817
Roshan Pius3a1667e2018-07-03 15:17:14 -07002818static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
2819 const u8 *key, size_t key_len,
2820 const u8 *addr)
2821{
2822 struct nl_msg *msg = NULL;
2823 int ret;
2824
2825 /*
2826 * If the authenticator address is not set, assume it is
2827 * the current BSSID.
2828 */
2829 if (!addr && drv->associated)
2830 addr = drv->bssid;
2831 else if (!addr)
2832 return -1;
2833
2834 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
2835 MAC2STR(addr));
2836 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
2837 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
2838 if (!msg ||
2839 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
2840 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
2841 nl80211_nlmsg_clear(msg);
2842 nlmsg_free(msg);
2843 return -ENOBUFS;
2844 }
2845
2846 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2847 if (ret) {
2848 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
2849 ret, strerror(-ret));
2850 }
2851
2852 return ret;
2853}
2854
2855
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002856static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 enum wpa_alg alg, const u8 *addr,
2858 int key_idx, int set_tx,
2859 const u8 *seq, size_t seq_len,
2860 const u8 *key, size_t key_len)
2861{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002863 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002864 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002866 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002868 /* Ignore for P2P Device */
2869 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2870 return 0;
2871
2872 ifindex = if_nametoindex(ifname);
2873 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002875 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002878 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002880 tdls = 1;
2881 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002882#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002884#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002885 if (alg == WPA_ALG_PMK &&
2886 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2887 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2888 __func__);
2889 ret = issue_key_mgmt_set_key(drv, key, key_len);
2890 return ret;
2891 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002892#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893
Roshan Pius3a1667e2018-07-03 15:17:14 -07002894 if (alg == WPA_ALG_PMK &&
2895 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2896 return nl80211_set_pmk(drv, key, key_len, addr);
2897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002899 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2900 if (!msg)
2901 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002903 u32 suite;
2904
2905 suite = wpa_alg_to_cipher_suite(alg, key_len);
2906 if (!suite)
2907 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002908 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2909 if (!msg ||
2910 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002911 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002912 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002913 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 }
2915
Dmitry Shmidt98660862014-03-11 17:26:21 -07002916 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002917 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2918 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002919 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921
2922 if (addr && !is_broadcast_ether_addr(addr)) {
2923 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002924 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2925 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926
2927 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2928 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002929 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2930 NL80211_KEYTYPE_GROUP))
2931 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 }
2933 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002934 struct nlattr *types;
2935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002937
2938 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002939 if (!types ||
2940 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2941 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002942 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002944 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2945 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002947 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2949 ret = 0;
2950 if (ret)
2951 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2952 ret, strerror(-ret));
2953
2954 /*
2955 * If we failed or don't need to set the default TX key (below),
2956 * we're done here.
2957 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002958 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002960 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961 !is_broadcast_ether_addr(addr))
2962 return ret;
2963
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002964 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2965 if (!msg ||
2966 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002967 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2968 alg == WPA_ALG_BIP_GMAC_128 ||
2969 alg == WPA_ALG_BIP_GMAC_256 ||
2970 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002971 NL80211_ATTR_KEY_DEFAULT_MGMT :
2972 NL80211_ATTR_KEY_DEFAULT))
2973 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002975 struct nlattr *types;
2976
2977 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002978 if (!types ||
2979 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2980 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002981 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002983 struct nlattr *types;
2984
2985 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002986 if (!types ||
2987 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2988 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002989 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990 }
2991
2992 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2993 if (ret == -ENOENT)
2994 ret = 0;
2995 if (ret)
2996 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2997 "err=%d %s)", ret, strerror(-ret));
2998 return ret;
2999
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003000fail:
3001 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003002 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 return -ENOBUFS;
3004}
3005
3006
3007static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3008 int key_idx, int defkey,
3009 const u8 *seq, size_t seq_len,
3010 const u8 *key, size_t key_len)
3011{
3012 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003013 u32 suite;
3014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 if (!key_attr)
3016 return -1;
3017
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003018 suite = wpa_alg_to_cipher_suite(alg, key_len);
3019 if (!suite)
3020 return -1;
3021
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003022 if (defkey && alg == WPA_ALG_IGTK) {
3023 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3024 return -1;
3025 } else if (defkey) {
3026 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3027 return -1;
3028 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003031 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003032 (seq && seq_len &&
3033 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3034 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3035 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036
3037 nla_nest_end(msg, key_attr);
3038
3039 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040}
3041
3042
3043static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3044 struct nl_msg *msg)
3045{
3046 int i, privacy = 0;
3047 struct nlattr *nl_keys, *nl_key;
3048
3049 for (i = 0; i < 4; i++) {
3050 if (!params->wep_key[i])
3051 continue;
3052 privacy = 1;
3053 break;
3054 }
3055 if (params->wps == WPS_MODE_PRIVACY)
3056 privacy = 1;
3057 if (params->pairwise_suite &&
3058 params->pairwise_suite != WPA_CIPHER_NONE)
3059 privacy = 1;
3060
3061 if (!privacy)
3062 return 0;
3063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003064 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3065 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066
3067 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3068 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003069 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070
3071 for (i = 0; i < 4; i++) {
3072 if (!params->wep_key[i])
3073 continue;
3074
3075 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003076 if (!nl_key ||
3077 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3078 params->wep_key[i]) ||
3079 nla_put_u32(msg, NL80211_KEY_CIPHER,
3080 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08003081 RSN_CIPHER_SUITE_WEP40 :
3082 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003083 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3084 (i == params->wep_tx_keyidx &&
3085 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3086 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087
3088 nla_nest_end(msg, nl_key);
3089 }
3090 nla_nest_end(msg, nl_keys);
3091
3092 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093}
3094
3095
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003096int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3097 const u8 *addr, int cmd, u16 reason_code,
3098 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003100 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 struct nl_msg *msg;
3102
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003103 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3104 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3105 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3106 (local_state_change &&
3107 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3108 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111
3112 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114 wpa_dbg(drv->ctx, MSG_DEBUG,
3115 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3116 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 return ret;
3119}
3120
3121
3122static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003123 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003125 int ret;
3126
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003127 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003128 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003129 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003130 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3131 reason_code, 0);
3132 /*
3133 * For locally generated disconnect, supplicant already generates a
3134 * DEAUTH event, so ignore the event from NL80211.
3135 */
3136 drv->ignore_next_local_disconnect = ret == 0;
3137
3138 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139}
3140
3141
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003142static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3143 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003146 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003147
3148 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3149 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003150 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003151 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003153 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3155 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003156 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003157 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3158 reason_code, 0);
3159 /*
3160 * For locally generated deauthenticate, supplicant already generates a
3161 * DEAUTH event, so ignore the event from NL80211.
3162 */
3163 drv->ignore_next_local_deauth = ret == 0;
3164 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003165}
3166
3167
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003168static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3169 struct wpa_driver_auth_params *params)
3170{
3171 int i;
3172
3173 drv->auth_freq = params->freq;
3174 drv->auth_alg = params->auth_alg;
3175 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3176 drv->auth_local_state_change = params->local_state_change;
3177 drv->auth_p2p = params->p2p;
3178
3179 if (params->bssid)
3180 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3181 else
3182 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3183
3184 if (params->ssid) {
3185 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3186 drv->auth_ssid_len = params->ssid_len;
3187 } else
3188 drv->auth_ssid_len = 0;
3189
3190
3191 os_free(drv->auth_ie);
3192 drv->auth_ie = NULL;
3193 drv->auth_ie_len = 0;
3194 if (params->ie) {
3195 drv->auth_ie = os_malloc(params->ie_len);
3196 if (drv->auth_ie) {
3197 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3198 drv->auth_ie_len = params->ie_len;
3199 }
3200 }
3201
3202 for (i = 0; i < 4; i++) {
3203 if (params->wep_key[i] && params->wep_key_len[i] &&
3204 params->wep_key_len[i] <= 16) {
3205 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3206 params->wep_key_len[i]);
3207 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3208 } else
3209 drv->auth_wep_key_len[i] = 0;
3210 }
3211}
3212
3213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003214static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3215{
3216 struct wpa_driver_nl80211_data *drv = bss->drv;
3217
3218 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3219 return;
3220
3221 /*
3222 * Looks like we failed to unmask 11b rates previously. This could
3223 * happen, e.g., if the interface was down at the point in time when a
3224 * P2P group was terminated.
3225 */
3226 wpa_printf(MSG_DEBUG,
3227 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3228 bss->ifname);
3229 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3230}
3231
3232
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003233static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3234{
3235 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3236 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3237 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3238 return NL80211_AUTHTYPE_SHARED_KEY;
3239 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3240 return NL80211_AUTHTYPE_NETWORK_EAP;
3241 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3242 return NL80211_AUTHTYPE_FT;
3243 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3244 return NL80211_AUTHTYPE_SAE;
3245 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3246 return NL80211_AUTHTYPE_FILS_SK;
3247 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3248 return NL80211_AUTHTYPE_FILS_SK_PFS;
3249
3250 return NL80211_AUTHTYPE_MAX;
3251}
3252
3253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003255 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 struct wpa_driver_nl80211_data *drv = bss->drv;
3258 int ret = -1, i;
3259 struct nl_msg *msg;
3260 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003261 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003263 int is_retry;
3264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003265 nl80211_unmask_11b_rates(bss);
3266
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003267 is_retry = drv->retry_auth;
3268 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003269 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003271 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003273 if (params->bssid)
3274 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3275 else
3276 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003278 nlmode = params->p2p ?
3279 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3280 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003281 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 return -1;
3283
3284retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3286 drv->ifindex);
3287
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003288 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3289 if (!msg)
3290 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291
3292 for (i = 0; i < 4; i++) {
3293 if (!params->wep_key[i])
3294 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003295 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 NULL, i,
3297 i == params->wep_tx_keyidx, NULL, 0,
3298 params->wep_key[i],
3299 params->wep_key_len[i]);
3300 if (params->wep_tx_keyidx != i)
3301 continue;
3302 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003303 params->wep_key[i], params->wep_key_len[i]))
3304 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305 }
3306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 if (params->bssid) {
3308 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3309 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003310 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3311 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 }
3313 if (params->freq) {
3314 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003315 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3316 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 }
3318 if (params->ssid) {
3319 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3320 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003321 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3322 params->ssid))
3323 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 }
3325 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003326 if (params->ie &&
3327 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3328 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003329 if (params->auth_data) {
3330 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3331 params->auth_data_len);
3332 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3333 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003334 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003335 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003336 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003338 if (type == NL80211_AUTHTYPE_MAX ||
3339 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003340 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003341 if (params->local_state_change) {
3342 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003343 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3344 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 }
3346
3347 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3348 msg = NULL;
3349 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003350 wpa_dbg(drv->ctx, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003351 "nl80211: MLME command failed (auth): count=%d ret=%d (%s)",
3352 count, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353 count++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003354 if ((ret == -EALREADY || ret == -EEXIST) && count == 1 &&
3355 params->bssid && !params->local_state_change) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003356 /*
3357 * mac80211 does not currently accept new
3358 * authentication if we are already authenticated. As a
3359 * workaround, force deauthentication and try again.
3360 */
3361 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3362 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003363 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 wpa_driver_nl80211_deauthenticate(
3365 bss, params->bssid,
3366 WLAN_REASON_PREV_AUTH_NOT_VALID);
3367 nlmsg_free(msg);
3368 goto retry;
3369 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003370
3371 if (ret == -ENOENT && params->freq && !is_retry) {
3372 /*
3373 * cfg80211 has likely expired the BSS entry even
3374 * though it was previously available in our internal
3375 * BSS table. To recover quickly, start a single
3376 * channel scan on the specified channel.
3377 */
3378 struct wpa_driver_scan_params scan;
3379 int freqs[2];
3380
3381 os_memset(&scan, 0, sizeof(scan));
3382 scan.num_ssids = 1;
3383 if (params->ssid) {
3384 scan.ssids[0].ssid = params->ssid;
3385 scan.ssids[0].ssid_len = params->ssid_len;
3386 }
3387 freqs[0] = params->freq;
3388 freqs[1] = 0;
3389 scan.freqs = freqs;
3390 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3391 "channel scan to refresh cfg80211 BSS "
3392 "entry");
3393 ret = wpa_driver_nl80211_scan(bss, &scan);
3394 if (ret == 0) {
3395 nl80211_copy_auth_params(drv, params);
3396 drv->scan_for_auth = 1;
3397 }
3398 } else if (is_retry) {
3399 /*
3400 * Need to indicate this with an event since the return
3401 * value from the retry is not delivered to core code.
3402 */
3403 union wpa_event_data event;
3404 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3405 "failed");
3406 os_memset(&event, 0, sizeof(event));
3407 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3408 ETH_ALEN);
3409 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3410 &event);
3411 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003412 } else {
3413 wpa_printf(MSG_DEBUG,
3414 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003417fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 nlmsg_free(msg);
3419 return ret;
3420}
3421
3422
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003423int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003424{
3425 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003426 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003427 int i;
3428
3429 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3430
3431 os_memset(&params, 0, sizeof(params));
3432 params.freq = drv->auth_freq;
3433 params.auth_alg = drv->auth_alg;
3434 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3435 params.local_state_change = drv->auth_local_state_change;
3436 params.p2p = drv->auth_p2p;
3437
3438 if (!is_zero_ether_addr(drv->auth_bssid_))
3439 params.bssid = drv->auth_bssid_;
3440
3441 if (drv->auth_ssid_len) {
3442 params.ssid = drv->auth_ssid;
3443 params.ssid_len = drv->auth_ssid_len;
3444 }
3445
3446 params.ie = drv->auth_ie;
3447 params.ie_len = drv->auth_ie_len;
3448
3449 for (i = 0; i < 4; i++) {
3450 if (drv->auth_wep_key_len[i]) {
3451 params.wep_key[i] = drv->auth_wep_key[i];
3452 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3453 }
3454 }
3455
3456 drv->retry_auth = 1;
3457 return wpa_driver_nl80211_authenticate(bss, &params);
3458}
3459
3460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003461static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3462 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003463 int encrypt, int noack,
3464 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003465 int offchanok, unsigned int wait_time,
3466 const u16 *csa_offs,
3467 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003468{
3469 struct wpa_driver_nl80211_data *drv = bss->drv;
3470 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003471 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003472
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003473 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3474 freq = nl80211_get_assoc_freq(drv);
3475 wpa_printf(MSG_DEBUG,
3476 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3477 freq);
3478 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003479 if (freq == 0) {
3480 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3481 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003482 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003483 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003484
Dmitry Shmidt56052862013-10-04 10:23:25 -07003485 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003486 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003487 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003488 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003489 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003490
Dmitry Shmidt56052862013-10-04 10:23:25 -07003491 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003492 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003493 &cookie, no_cck, noack, offchanok,
3494 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003495 if (res == 0 && !noack) {
3496 const struct ieee80211_mgmt *mgmt;
3497 u16 fc;
3498
3499 mgmt = (const struct ieee80211_mgmt *) data;
3500 fc = le_to_host16(mgmt->frame_control);
3501 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3502 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3503 wpa_printf(MSG_MSGDUMP,
3504 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3505 (long long unsigned int)
3506 drv->send_action_cookie,
3507 (long long unsigned int) cookie);
3508 drv->send_action_cookie = cookie;
3509 }
3510 }
3511
3512 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003513}
3514
3515
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003516static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3517 size_t data_len, int noack,
3518 unsigned int freq, int no_cck,
3519 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003520 unsigned int wait_time,
3521 const u16 *csa_offs,
3522 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524 struct wpa_driver_nl80211_data *drv = bss->drv;
3525 struct ieee80211_mgmt *mgmt;
3526 int encrypt = 1;
3527 u16 fc;
3528
3529 mgmt = (struct ieee80211_mgmt *) data;
3530 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003531 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3532 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3533 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3534 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003535
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003536 if ((is_sta_interface(drv->nlmode) ||
3537 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3539 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3540 /*
3541 * The use of last_mgmt_freq is a bit of a hack,
3542 * but it works due to the single-threaded nature
3543 * of wpa_supplicant.
3544 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003545 if (freq == 0) {
3546 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3547 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003548 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003549 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003550 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003551 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003552 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003553 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003554
3555 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003556 if (freq == 0) {
3557 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3558 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003559 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003560 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003561 return nl80211_send_frame_cmd(bss, freq,
3562 (int) freq == bss->freq ? 0 :
3563 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003564 data, data_len,
3565 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003566 no_cck, noack, offchanok,
3567 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003568 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003570 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3571 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3572 /*
3573 * Only one of the authentication frame types is encrypted.
3574 * In order for static WEP encryption to work properly (i.e.,
3575 * to not encrypt the frame), we need to tell mac80211 about
3576 * the frames that must not be encrypted.
3577 */
3578 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3579 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3580 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3581 encrypt = 0;
3582 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003583
Dmitry Shmidt56052862013-10-04 10:23:25 -07003584 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003586 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003587 wait_time, csa_offs,
3588 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003589}
3590
3591
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003592static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3593{
3594 u8 rates[NL80211_MAX_SUPP_RATES];
3595 u8 rates_len = 0;
3596 int i;
3597
3598 if (!basic_rates)
3599 return 0;
3600
3601 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3602 rates[rates_len++] = basic_rates[i] / 5;
3603
3604 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3605}
3606
3607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3609 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003610 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003611{
3612 struct wpa_driver_nl80211_data *drv = bss->drv;
3613 struct nl_msg *msg;
3614
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003615 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3616 (cts >= 0 &&
3617 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3618 (preamble >= 0 &&
3619 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3620 (slot >= 0 &&
3621 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3622 (ht_opmode >= 0 &&
3623 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3624 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003625 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3626 nl80211_put_basic_rates(msg, basic_rates)) {
3627 nlmsg_free(msg);
3628 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003629 }
3630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003631 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003632}
3633
3634
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003635static int wpa_driver_nl80211_set_acl(void *priv,
3636 struct hostapd_acl_params *params)
3637{
3638 struct i802_bss *bss = priv;
3639 struct wpa_driver_nl80211_data *drv = bss->drv;
3640 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003641 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003642 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003643 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003644
3645 if (!(drv->capa.max_acl_mac_addrs))
3646 return -ENOTSUP;
3647
3648 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3649 return -ENOTSUP;
3650
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003651 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3652 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3653
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003654 acl = nlmsg_alloc();
3655 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003656 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003657 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003658 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3659 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003660 return -ENOMEM;
3661 }
3662 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003663
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003664 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3665 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3666 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3667 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3668 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3669 nlmsg_free(msg);
3670 nlmsg_free(acl);
3671 return -ENOMEM;
3672 }
3673 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003674
3675 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003676 if (ret) {
3677 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3678 ret, strerror(-ret));
3679 }
3680
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003681 return ret;
3682}
3683
3684
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003685static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3686{
3687 if (beacon_int > 0) {
3688 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3689 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3690 beacon_int);
3691 }
3692
3693 return 0;
3694}
3695
3696
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003697static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3698{
3699 if (dtim_period > 0) {
3700 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3701 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3702 }
3703
3704 return 0;
3705}
3706
3707
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003708#ifdef CONFIG_MESH
3709static int nl80211_set_mesh_config(void *priv,
3710 struct wpa_driver_mesh_bss_params *params)
3711{
3712 struct i802_bss *bss = priv;
3713 struct wpa_driver_nl80211_data *drv = bss->drv;
3714 struct nl_msg *msg;
3715 int ret;
3716
3717 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3718 if (!msg)
3719 return -1;
3720
3721 ret = nl80211_put_mesh_config(msg, params);
3722 if (ret < 0) {
3723 nlmsg_free(msg);
3724 return ret;
3725 }
3726
3727 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3728 if (ret) {
3729 wpa_printf(MSG_ERROR,
3730 "nl80211: Mesh config set failed: %d (%s)",
3731 ret, strerror(-ret));
3732 return ret;
3733 }
3734 return 0;
3735}
3736#endif /* CONFIG_MESH */
3737
3738
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003739static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3740 struct wpa_driver_ap_params *params)
3741{
3742 struct nlattr *bands, *band;
3743 struct nl80211_txrate_vht vht_rate;
3744
3745 if (!params->freq ||
3746 (params->beacon_rate == 0 &&
3747 params->rate_type == BEACON_RATE_LEGACY))
3748 return 0;
3749
3750 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3751 if (!bands)
3752 return -1;
3753
3754 switch (params->freq->mode) {
3755 case HOSTAPD_MODE_IEEE80211B:
3756 case HOSTAPD_MODE_IEEE80211G:
3757 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3758 break;
3759 case HOSTAPD_MODE_IEEE80211A:
3760 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3761 break;
3762 case HOSTAPD_MODE_IEEE80211AD:
3763 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3764 break;
3765 default:
3766 return 0;
3767 }
3768
3769 if (!band)
3770 return -1;
3771
3772 os_memset(&vht_rate, 0, sizeof(vht_rate));
3773
3774 switch (params->rate_type) {
3775 case BEACON_RATE_LEGACY:
3776 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3777 wpa_printf(MSG_INFO,
3778 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3779 return -1;
3780 }
3781
3782 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3783 (u8) params->beacon_rate / 5) ||
3784 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3785 (params->freq->vht_enabled &&
3786 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3787 &vht_rate)))
3788 return -1;
3789
3790 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3791 params->beacon_rate);
3792 break;
3793 case BEACON_RATE_HT:
3794 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3795 wpa_printf(MSG_INFO,
3796 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3797 return -1;
3798 }
3799 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3800 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3801 (params->freq->vht_enabled &&
3802 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3803 &vht_rate)))
3804 return -1;
3805 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3806 params->beacon_rate);
3807 break;
3808 case BEACON_RATE_VHT:
3809 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3810 wpa_printf(MSG_INFO,
3811 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3812 return -1;
3813 }
3814 vht_rate.mcs[0] = BIT(params->beacon_rate);
3815 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3816 return -1;
3817 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3818 return -1;
3819 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3820 &vht_rate))
3821 return -1;
3822 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3823 params->beacon_rate);
3824 break;
3825 }
3826
3827 nla_nest_end(msg, band);
3828 nla_nest_end(msg, bands);
3829
3830 return 0;
3831}
3832
3833
3834static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3835 int multicast_to_unicast)
3836{
3837 struct wpa_driver_nl80211_data *drv = bss->drv;
3838 struct nl_msg *msg;
3839 int ret;
3840
3841 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3842 if (!msg ||
3843 (multicast_to_unicast &&
3844 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3845 wpa_printf(MSG_ERROR,
3846 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3847 bss->ifname);
3848 nlmsg_free(msg);
3849 return -ENOBUFS;
3850 }
3851
3852 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3853
3854 switch (ret) {
3855 case 0:
3856 wpa_printf(MSG_DEBUG,
3857 "nl80211: multicast to unicast %s on interface %s",
3858 multicast_to_unicast ? "enabled" : "disabled",
3859 bss->ifname);
3860 break;
3861 case -EOPNOTSUPP:
3862 if (!multicast_to_unicast)
3863 break;
3864 wpa_printf(MSG_INFO,
3865 "nl80211: multicast to unicast not supported on interface %s",
3866 bss->ifname);
3867 break;
3868 default:
3869 wpa_printf(MSG_ERROR,
3870 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3871 multicast_to_unicast ? "enabling" : "disabling",
3872 ret, strerror(-ret), bss->ifname);
3873 break;
3874 }
3875
3876 return ret;
3877}
3878
3879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003880static int wpa_driver_nl80211_set_ap(void *priv,
3881 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882{
3883 struct i802_bss *bss = priv;
3884 struct wpa_driver_nl80211_data *drv = bss->drv;
3885 struct nl_msg *msg;
3886 u8 cmd = NL80211_CMD_NEW_BEACON;
3887 int ret;
3888 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003889 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003890 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003891 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003892 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003893#ifdef CONFIG_MESH
3894 struct wpa_driver_mesh_bss_params mesh_params;
3895#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003896
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003897 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3900 beacon_set);
3901 if (beacon_set)
3902 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08003903 else if (!drv->device_ap_sme && !drv->use_monitor &&
3904 !nl80211_get_wiphy_data_ap(bss))
3905 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003907 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3908 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003909 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3910 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003911 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003912 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003913 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3914 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003915 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003916 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3917 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003918 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3919 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3920 params->head) ||
3921 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3922 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003923 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003924 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003925 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003926 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3927 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003928 if (params->proberesp && params->proberesp_len) {
3929 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3930 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003931 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3932 params->proberesp))
3933 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003934 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003935 switch (params->hide_ssid) {
3936 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003937 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003938 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3939 NL80211_HIDDEN_SSID_NOT_IN_USE))
3940 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003941 break;
3942 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003943 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003944 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3945 NL80211_HIDDEN_SSID_ZERO_LEN))
3946 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003947 break;
3948 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003949 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003950 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3951 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3952 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003953 break;
3954 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003955 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003956 if (params->privacy &&
3957 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3958 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003959 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3961 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3962 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003963 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3964 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3965 NL80211_AUTHTYPE_SHARED_KEY))
3966 goto fail;
3967 } else {
3968 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3969 NL80211_AUTHTYPE_OPEN_SYSTEM))
3970 goto fail;
3971 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003972
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003973 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003974 ver = 0;
3975 if (params->wpa_version & WPA_PROTO_WPA)
3976 ver |= NL80211_WPA_VERSION_1;
3977 if (params->wpa_version & WPA_PROTO_RSN)
3978 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003979 if (ver &&
3980 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3981 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003982
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003983 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3984 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985 num_suites = 0;
3986 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
Paul Stewart092955c2017-02-06 09:13:09 -08003987 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003988 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
Paul Stewart092955c2017-02-06 09:13:09 -08003989 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003990 if (num_suites &&
3991 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3992 suites))
3993 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003995 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003996 (!params->pairwise_ciphers ||
3997 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
3998 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
3999 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004000 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004001
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004002 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4003 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004004 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4005 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004006 if (num_suites &&
4007 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4008 num_suites * sizeof(u32), suites))
4009 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004010
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004011 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4012 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004013 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004014 if (suite &&
4015 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4016 goto fail;
4017
Dmitry Shmidte4663042016-04-04 10:07:49 -07004018 if (params->ht_opmode != -1) {
4019 switch (params->smps_mode) {
4020 case HT_CAP_INFO_SMPS_DYNAMIC:
4021 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
4022 smps_mode = NL80211_SMPS_DYNAMIC;
4023 break;
4024 case HT_CAP_INFO_SMPS_STATIC:
4025 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
4026 smps_mode = NL80211_SMPS_STATIC;
4027 break;
4028 default:
4029 /* invalid - fallback to smps off */
4030 case HT_CAP_INFO_SMPS_DISABLED:
4031 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
4032 smps_mode = NL80211_SMPS_OFF;
4033 break;
4034 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004035 if (nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
Dmitry Shmidte4663042016-04-04 10:07:49 -07004036 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004037 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004038
4039 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004040 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4041 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004042 if (nla_put(msg, NL80211_ATTR_IE,
4043 wpabuf_len(params->beacon_ies),
4044 wpabuf_head(params->beacon_ies)))
4045 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 }
4047 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004048 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4049 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004050 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4051 wpabuf_len(params->proberesp_ies),
4052 wpabuf_head(params->proberesp_ies)))
4053 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004054 }
4055 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004056 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4057 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004058 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4059 wpabuf_len(params->assocresp_ies),
4060 wpabuf_head(params->assocresp_ies)))
4061 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063
Dmitry Shmidt04949592012-07-19 12:16:46 -07004064 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004065 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4066 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004067 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4068 params->ap_max_inactivity))
4069 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004070 }
4071
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004072#ifdef CONFIG_P2P
4073 if (params->p2p_go_ctwindow > 0) {
4074 if (drv->p2p_go_ctwindow_supported) {
4075 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4076 params->p2p_go_ctwindow);
4077 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4078 params->p2p_go_ctwindow))
4079 goto fail;
4080 } else {
4081 wpa_printf(MSG_INFO,
4082 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4083 }
4084 }
4085#endif /* CONFIG_P2P */
4086
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004087 if (params->pbss) {
4088 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4089 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4090 goto fail;
4091 }
4092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4094 if (ret) {
4095 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4096 ret, strerror(-ret));
4097 } else {
4098 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004099 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4100 params->short_slot_time, params->ht_opmode,
4101 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004102 nl80211_set_multicast_to_unicast(bss,
4103 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004104 if (beacon_set && params->freq &&
4105 params->freq->bandwidth != bss->bandwidth) {
4106 wpa_printf(MSG_DEBUG,
4107 "nl80211: Update BSS %s bandwidth: %d -> %d",
4108 bss->ifname, bss->bandwidth,
4109 params->freq->bandwidth);
4110 ret = nl80211_set_channel(bss, params->freq, 1);
4111 if (ret) {
4112 wpa_printf(MSG_DEBUG,
4113 "nl80211: Frequency set failed: %d (%s)",
4114 ret, strerror(-ret));
4115 } else {
4116 wpa_printf(MSG_DEBUG,
4117 "nl80211: Frequency set succeeded for ht2040 coex");
4118 bss->bandwidth = params->freq->bandwidth;
4119 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004120 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004121 /*
4122 * cfg80211 updates the driver on frequence change in AP
4123 * mode only at the point when beaconing is started, so
4124 * set the initial value here.
4125 */
4126 bss->bandwidth = params->freq->bandwidth;
4127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004129
4130#ifdef CONFIG_MESH
4131 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4132 os_memset(&mesh_params, 0, sizeof(mesh_params));
4133 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4134 mesh_params.ht_opmode = params->ht_opmode;
4135 ret = nl80211_set_mesh_config(priv, &mesh_params);
4136 if (ret < 0)
4137 return ret;
4138 }
4139#endif /* CONFIG_MESH */
4140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004142fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 return -ENOBUFS;
4145}
4146
4147
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004148static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004149 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004151 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004152 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4153 return -ENOBUFS;
4154
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004155 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4156 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4157
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004158 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004159 enum nl80211_chan_width cw;
4160
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004161 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004162 switch (freq->bandwidth) {
4163 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004164 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004165 break;
4166 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004167 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004168 break;
4169 case 80:
4170 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004172 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004173 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004174 break;
4175 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004176 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004177 break;
4178 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004179 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004180 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004181
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004182 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4183 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4184 freq->center_freq1);
4185 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4186 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004187 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4188 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4189 freq->center_freq1) ||
4190 (freq->center_freq2 &&
4191 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4192 freq->center_freq2)))
4193 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004194 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004195 enum nl80211_channel_type ct;
4196
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004197 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4198 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004199 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004201 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 break;
4203 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004204 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205 break;
4206 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004207 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208 break;
4209 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004210
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004211 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004212 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4213 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004214 } else {
4215 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4216 NL80211_CHAN_NO_HT);
4217 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4218 NL80211_CHAN_NO_HT))
4219 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004221 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004222}
4223
4224
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004225static int nl80211_set_channel(struct i802_bss *bss,
4226 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004227{
4228 struct wpa_driver_nl80211_data *drv = bss->drv;
4229 struct nl_msg *msg;
4230 int ret;
4231
4232 wpa_printf(MSG_DEBUG,
4233 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4234 freq->freq, freq->ht_enabled, freq->vht_enabled,
4235 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004236
4237 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4238 NL80211_CMD_SET_WIPHY);
4239 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4240 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004241 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243
4244 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004245 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004246 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004250 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251 return -1;
4252}
4253
4254
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004255static u32 sta_flags_nl80211(int flags)
4256{
4257 u32 f = 0;
4258
4259 if (flags & WPA_STA_AUTHORIZED)
4260 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4261 if (flags & WPA_STA_WMM)
4262 f |= BIT(NL80211_STA_FLAG_WME);
4263 if (flags & WPA_STA_SHORT_PREAMBLE)
4264 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4265 if (flags & WPA_STA_MFP)
4266 f |= BIT(NL80211_STA_FLAG_MFP);
4267 if (flags & WPA_STA_TDLS_PEER)
4268 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004269 if (flags & WPA_STA_AUTHENTICATED)
4270 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004271 if (flags & WPA_STA_ASSOCIATED)
4272 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004273
4274 return f;
4275}
4276
4277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004278#ifdef CONFIG_MESH
4279static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4280{
4281 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004282 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004283 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004284 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004285 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004286 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004287 return NL80211_PLINK_OPN_RCVD;
4288 case PLINK_CNF_RCVD:
4289 return NL80211_PLINK_CNF_RCVD;
4290 case PLINK_ESTAB:
4291 return NL80211_PLINK_ESTAB;
4292 case PLINK_HOLDING:
4293 return NL80211_PLINK_HOLDING;
4294 case PLINK_BLOCKED:
4295 return NL80211_PLINK_BLOCKED;
4296 default:
4297 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4298 state);
4299 }
4300 return -1;
4301}
4302#endif /* CONFIG_MESH */
4303
4304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305static int wpa_driver_nl80211_sta_add(void *priv,
4306 struct hostapd_sta_add_params *params)
4307{
4308 struct i802_bss *bss = priv;
4309 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004310 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004311 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312 int ret = -ENOBUFS;
4313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314 if ((params->flags & WPA_STA_TDLS_PEER) &&
4315 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4316 return -EOPNOTSUPP;
4317
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004318 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4319 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004320 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4321 NL80211_CMD_NEW_STATION);
4322 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4323 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004325 /*
4326 * Set the below properties only in one of the following cases:
4327 * 1. New station is added, already associated.
4328 * 2. Set WPA_STA_TDLS_PEER station.
4329 * 3. Set an already added unassociated station, if driver supports
4330 * full AP client state. (Set these properties after station became
4331 * associated will be rejected by the driver).
4332 */
4333 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4334 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4335 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004336 wpa_hexdump(MSG_DEBUG, " * supported rates",
4337 params->supp_rates, params->supp_rates_len);
4338 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4339 params->capability);
4340 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4341 params->supp_rates_len, params->supp_rates) ||
4342 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4343 params->capability))
4344 goto fail;
4345
4346 if (params->ht_capabilities) {
4347 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4348 (u8 *) params->ht_capabilities,
4349 sizeof(*params->ht_capabilities));
4350 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4351 sizeof(*params->ht_capabilities),
4352 params->ht_capabilities))
4353 goto fail;
4354 }
4355
4356 if (params->vht_capabilities) {
4357 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4358 (u8 *) params->vht_capabilities,
4359 sizeof(*params->vht_capabilities));
4360 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4361 sizeof(*params->vht_capabilities),
4362 params->vht_capabilities))
4363 goto fail;
4364 }
4365
4366 if (params->ext_capab) {
4367 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4368 params->ext_capab, params->ext_capab_len);
4369 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4370 params->ext_capab_len, params->ext_capab))
4371 goto fail;
4372 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004373
4374 if (is_ap_interface(drv->nlmode) &&
4375 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4376 params->support_p2p_ps ?
4377 NL80211_P2P_PS_SUPPORTED :
4378 NL80211_P2P_PS_UNSUPPORTED))
4379 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004380 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004381 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004382 if (params->aid) {
4383 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004384 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4385 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004386 } else {
4387 /*
4388 * cfg80211 validates that AID is non-zero, so we have
4389 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004390 * a dummy STA entry is used for now and for a station
4391 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004392 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004393 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4394 (params->flags & WPA_STA_TDLS_PEER) ?
4395 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004396 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4397 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004398 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004399 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4400 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004401 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4402 params->listen_interval))
4403 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004404 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4405 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004406 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4407 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004408 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4409 (params->flags & WPA_STA_ASSOCIATED)) {
4410 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4411 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4412 params->listen_interval);
4413 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4414 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4415 params->listen_interval))
4416 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004417 }
4418
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004419 if (params->vht_opmode_enabled) {
4420 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004421 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4422 params->vht_opmode))
4423 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004424 }
4425
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004426 if (params->supp_channels) {
4427 wpa_hexdump(MSG_DEBUG, " * supported channels",
4428 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004429 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4430 params->supp_channels_len, params->supp_channels))
4431 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004432 }
4433
4434 if (params->supp_oper_classes) {
4435 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4436 params->supp_oper_classes,
4437 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004438 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4439 params->supp_oper_classes_len,
4440 params->supp_oper_classes))
4441 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004442 }
4443
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004444 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004445 upd.set = sta_flags_nl80211(params->flags);
4446 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004447
4448 /*
4449 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4450 * flags, as nl80211 driver moves a new station, by default, into
4451 * associated state.
4452 *
4453 * On the other hand, if the driver supports that feature and the
4454 * station is added in unauthenticated state, set the
4455 * authenticated/associated bits in the mask to prevent moving this
4456 * station to associated state before it is actually associated.
4457 *
4458 * This is irrelevant for mesh mode where the station is added to the
4459 * driver as authenticated already, and ASSOCIATED isn't part of the
4460 * nl80211 API.
4461 */
4462 if (!is_mesh_interface(drv->nlmode)) {
4463 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4464 wpa_printf(MSG_DEBUG,
4465 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4466 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4467 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4468 } else if (!params->set &&
4469 !(params->flags & WPA_STA_TDLS_PEER)) {
4470 if (!(params->flags & WPA_STA_AUTHENTICATED))
4471 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4472 if (!(params->flags & WPA_STA_ASSOCIATED))
4473 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4474 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004475#ifdef CONFIG_MESH
4476 } else {
4477 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4478 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4479 params->peer_aid);
4480 if (ret)
4481 goto fail;
4482 }
4483#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004484 }
4485
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004486 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4487 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004488 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4489 goto fail;
4490
4491#ifdef CONFIG_MESH
4492 if (params->plink_state &&
4493 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4494 sta_plink_state_nl80211(params->plink_state)))
4495 goto fail;
4496#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497
4498 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004499 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4500
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004501 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004502 if (!wme ||
4503 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4504 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4505 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4506 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4507 WMM_QOSINFO_STA_SP_MASK))
4508 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004509 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004510 }
4511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004513 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004515 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4516 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4517 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 if (ret == -EEXIST)
4519 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004520fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004521 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522 return ret;
4523}
4524
4525
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004526static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4527{
4528#ifdef CONFIG_LIBNL3_ROUTE
4529 struct wpa_driver_nl80211_data *drv = bss->drv;
4530 struct rtnl_neigh *rn;
4531 struct nl_addr *nl_addr;
4532 int err;
4533
4534 rn = rtnl_neigh_alloc();
4535 if (!rn)
4536 return;
4537
4538 rtnl_neigh_set_family(rn, AF_BRIDGE);
4539 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4540 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4541 if (!nl_addr) {
4542 rtnl_neigh_put(rn);
4543 return;
4544 }
4545 rtnl_neigh_set_lladdr(rn, nl_addr);
4546
4547 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4548 if (err < 0) {
4549 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4550 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4551 bss->ifindex, nl_geterror(err));
4552 } else {
4553 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4554 MACSTR, MAC2STR(addr));
4555 }
4556
4557 nl_addr_put(nl_addr);
4558 rtnl_neigh_put(rn);
4559#endif /* CONFIG_LIBNL3_ROUTE */
4560}
4561
4562
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004563static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4564 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 struct wpa_driver_nl80211_data *drv = bss->drv;
4567 struct nl_msg *msg;
4568 int ret;
4569
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004570 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4571 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4572 (deauth == 0 &&
4573 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4574 WLAN_FC_STYPE_DISASSOC)) ||
4575 (deauth == 1 &&
4576 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4577 WLAN_FC_STYPE_DEAUTH)) ||
4578 (reason_code &&
4579 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4580 nlmsg_free(msg);
4581 return -ENOBUFS;
4582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583
4584 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004585 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4586 " --> %d (%s)",
4587 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004588
4589 if (drv->rtnl_sk)
4590 rtnl_neigh_delete_fdb_entry(bss, addr);
4591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592 if (ret == -ENOENT)
4593 return 0;
4594 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595}
4596
4597
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004598void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599{
4600 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004601 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602
4603 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004606 dl_list_for_each(drv2, &drv->global->interfaces,
4607 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004608 {
4609 del_ifidx(drv2, ifidx, IFIDX_ANY);
4610 /* Remove all bridges learned for this iface */
4611 del_ifidx(drv2, IFIDX_ANY, ifidx);
4612 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004614 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4616 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4618}
4619
4620
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004621const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004622{
4623 switch (mode) {
4624 case NL80211_IFTYPE_ADHOC:
4625 return "ADHOC";
4626 case NL80211_IFTYPE_STATION:
4627 return "STATION";
4628 case NL80211_IFTYPE_AP:
4629 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004630 case NL80211_IFTYPE_AP_VLAN:
4631 return "AP_VLAN";
4632 case NL80211_IFTYPE_WDS:
4633 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004634 case NL80211_IFTYPE_MONITOR:
4635 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004636 case NL80211_IFTYPE_MESH_POINT:
4637 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004638 case NL80211_IFTYPE_P2P_CLIENT:
4639 return "P2P_CLIENT";
4640 case NL80211_IFTYPE_P2P_GO:
4641 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004642 case NL80211_IFTYPE_P2P_DEVICE:
4643 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004644 default:
4645 return "unknown";
4646 }
4647}
4648
4649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4651 const char *ifname,
4652 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004653 const u8 *addr, int wds,
4654 int (*handler)(struct nl_msg *, void *),
4655 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004656{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004657 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 int ifidx;
4659 int ret = -ENOBUFS;
4660
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004661 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4662 iftype, nl80211_iftype_str(iftype));
4663
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004664 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4665 if (!msg ||
4666 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4667 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4668 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669
4670 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004671 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004673 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004674 if (!flags ||
4675 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4676 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004678 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004680 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4681 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 }
4683
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004684 /*
4685 * Tell cfg80211 that the interface belongs to the socket that created
4686 * it, and the interface should be deleted when the socket is closed.
4687 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004688 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4689 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004690
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004691 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004692 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004694 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004695 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4697 ifname, ret, strerror(-ret));
4698 return ret;
4699 }
4700
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004701 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4702 return 0;
4703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 ifidx = if_nametoindex(ifname);
4705 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4706 ifname, ifidx);
4707
4708 if (ifidx <= 0)
4709 return -1;
4710
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004711 /*
4712 * Some virtual interfaces need to process EAPOL packets and events on
4713 * the parent interface. This is used mainly with hostapd.
4714 */
4715 if (drv->hostapd ||
4716 iftype == NL80211_IFTYPE_AP_VLAN ||
4717 iftype == NL80211_IFTYPE_WDS ||
4718 iftype == NL80211_IFTYPE_MONITOR) {
4719 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004720 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004721 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722
4723 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004724 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725 nl80211_remove_iface(drv, ifidx);
4726 return -1;
4727 }
4728
4729 return ifidx;
4730}
4731
4732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004733int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4734 const char *ifname, enum nl80211_iftype iftype,
4735 const u8 *addr, int wds,
4736 int (*handler)(struct nl_msg *, void *),
4737 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738{
4739 int ret;
4740
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004741 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4742 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004744 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004746 if (use_existing) {
4747 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4748 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004749 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4750 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4751 addr) < 0 &&
4752 (linux_set_iface_flags(drv->global->ioctl_sock,
4753 ifname, 0) < 0 ||
4754 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4755 addr) < 0 ||
4756 linux_set_iface_flags(drv->global->ioctl_sock,
4757 ifname, 1) < 0))
4758 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004759 return -ENFILE;
4760 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4762
4763 /* Try to remove the interface that was already there. */
4764 nl80211_remove_iface(drv, if_nametoindex(ifname));
4765
4766 /* Try to create the interface again */
4767 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004768 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 }
4770
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004771 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4772 wpa_printf(MSG_DEBUG,
4773 "nl80211: Interface %s created for P2P - disable 11b rates",
4774 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777
4778 return ret;
4779}
4780
4781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004782static int nl80211_setup_ap(struct i802_bss *bss)
4783{
4784 struct wpa_driver_nl80211_data *drv = bss->drv;
4785
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004786 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4787 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004788
4789 /*
4790 * Disable Probe Request reporting unless we need it in this way for
4791 * devices that include the AP SME, in the other case (unless using
4792 * monitor iface) we'll get it through the nl_mgmt socket instead.
4793 */
4794 if (!drv->device_ap_sme)
4795 wpa_driver_nl80211_probe_req_report(bss, 0);
4796
4797 if (!drv->device_ap_sme && !drv->use_monitor)
4798 if (nl80211_mgmt_subscribe_ap(bss))
4799 return -1;
4800
4801 if (drv->device_ap_sme && !drv->use_monitor)
4802 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004803 wpa_printf(MSG_DEBUG,
4804 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004805
4806 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004807 nl80211_create_monitor_interface(drv) &&
4808 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004809 return -1;
4810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004811 if (drv->device_ap_sme &&
4812 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4813 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4814 "Probe Request frame reporting in AP mode");
4815 /* Try to survive without this */
4816 }
4817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004818 return 0;
4819}
4820
4821
4822static void nl80211_teardown_ap(struct i802_bss *bss)
4823{
4824 struct wpa_driver_nl80211_data *drv = bss->drv;
4825
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004826 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4827 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004828 if (drv->device_ap_sme) {
4829 wpa_driver_nl80211_probe_req_report(bss, 0);
4830 if (!drv->use_monitor)
4831 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4832 } else if (drv->use_monitor)
4833 nl80211_remove_monitor_interface(drv);
4834 else
4835 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4836
Paul Stewart092955c2017-02-06 09:13:09 -08004837 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004838 bss->beacon_set = 0;
4839}
4840
4841
4842static int nl80211_send_eapol_data(struct i802_bss *bss,
4843 const u8 *addr, const u8 *data,
4844 size_t data_len)
4845{
4846 struct sockaddr_ll ll;
4847 int ret;
4848
4849 if (bss->drv->eapol_tx_sock < 0) {
4850 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4851 return -1;
4852 }
4853
4854 os_memset(&ll, 0, sizeof(ll));
4855 ll.sll_family = AF_PACKET;
4856 ll.sll_ifindex = bss->ifindex;
4857 ll.sll_protocol = htons(ETH_P_PAE);
4858 ll.sll_halen = ETH_ALEN;
4859 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4860 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4861 (struct sockaddr *) &ll, sizeof(ll));
4862 if (ret < 0)
4863 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4864 strerror(errno));
4865
4866 return ret;
4867}
4868
4869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4871
4872static int wpa_driver_nl80211_hapd_send_eapol(
4873 void *priv, const u8 *addr, const u8 *data,
4874 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4875{
4876 struct i802_bss *bss = priv;
4877 struct wpa_driver_nl80211_data *drv = bss->drv;
4878 struct ieee80211_hdr *hdr;
4879 size_t len;
4880 u8 *pos;
4881 int res;
4882 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004884 if (drv->device_ap_sme || !drv->use_monitor)
4885 return nl80211_send_eapol_data(bss, addr, data, data_len);
4886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4888 data_len;
4889 hdr = os_zalloc(len);
4890 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004891 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4892 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 return -1;
4894 }
4895
4896 hdr->frame_control =
4897 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4898 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4899 if (encrypt)
4900 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4901 if (qos) {
4902 hdr->frame_control |=
4903 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4904 }
4905
4906 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4907 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4908 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4909 pos = (u8 *) (hdr + 1);
4910
4911 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004912 /* Set highest priority in QoS header */
4913 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 pos[1] = 0;
4915 pos += 2;
4916 }
4917
4918 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4919 pos += sizeof(rfc1042_header);
4920 WPA_PUT_BE16(pos, ETH_P_PAE);
4921 pos += 2;
4922 memcpy(pos, data, data_len);
4923
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004924 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004925 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 if (res < 0) {
4927 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4928 "failed: %d (%s)",
4929 (unsigned long) len, errno, strerror(errno));
4930 }
4931 os_free(hdr);
4932
4933 return res;
4934}
4935
4936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004938 unsigned int total_flags,
4939 unsigned int flags_or,
4940 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941{
4942 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004943 struct nl_msg *msg;
4944 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 struct nl80211_sta_flag_update upd;
4946
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004947 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4948 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4949 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4950 !!(total_flags & WPA_STA_AUTHORIZED));
4951
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004952 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4953 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4954 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955
4956 /*
4957 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4958 * can be removed eventually.
4959 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004960 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004961 if (!flags ||
4962 ((total_flags & WPA_STA_AUTHORIZED) &&
4963 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4964 ((total_flags & WPA_STA_WMM) &&
4965 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4966 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4967 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4968 ((total_flags & WPA_STA_MFP) &&
4969 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4970 ((total_flags & WPA_STA_TDLS_PEER) &&
4971 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4972 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004973
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004974 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975
4976 os_memset(&upd, 0, sizeof(upd));
4977 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4978 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004979 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4980 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004982 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4983fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004984 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985 return -ENOBUFS;
4986}
4987
4988
4989static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4990 struct wpa_driver_associate_params *params)
4991{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004992 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004993
4994 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4996 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004997 nlmode = NL80211_IFTYPE_P2P_GO;
4998 } else
4999 nlmode = NL80211_IFTYPE_AP;
5000
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005001 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005002 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005003 nl80211_remove_monitor_interface(drv);
5004 return -1;
5005 }
5006
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005007 if (params->freq.freq &&
5008 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005009 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005010 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 nl80211_remove_monitor_interface(drv);
5012 return -1;
5013 }
5014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015 return 0;
5016}
5017
5018
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005019static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5020 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021{
5022 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005023 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005025 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027 if (ret) {
5028 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5029 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005030 } else {
5031 wpa_printf(MSG_DEBUG,
5032 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033 }
5034
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005035 if (reset_mode &&
5036 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005037 NL80211_IFTYPE_STATION)) {
5038 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5039 "station mode");
5040 }
5041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 return ret;
5043}
5044
5045
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005046static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5047 struct wpa_driver_associate_params *params)
5048{
5049 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5050 return -1;
5051
5052 if (params->htcaps && params->htcaps_mask) {
5053 int sz = sizeof(struct ieee80211_ht_capabilities);
5054 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5055 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5056 params->htcaps_mask, sz);
5057 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5058 params->htcaps) ||
5059 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5060 params->htcaps_mask))
5061 return -1;
5062 }
5063
5064#ifdef CONFIG_VHT_OVERRIDES
5065 if (params->disable_vht) {
5066 wpa_printf(MSG_DEBUG, " * VHT disabled");
5067 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5068 return -1;
5069 }
5070
5071 if (params->vhtcaps && params->vhtcaps_mask) {
5072 int sz = sizeof(struct ieee80211_vht_capabilities);
5073 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5074 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5075 params->vhtcaps_mask, sz);
5076 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5077 params->vhtcaps) ||
5078 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5079 params->vhtcaps_mask))
5080 return -1;
5081 }
5082#endif /* CONFIG_VHT_OVERRIDES */
5083
5084 return 0;
5085}
5086
5087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5089 struct wpa_driver_associate_params *params)
5090{
5091 struct nl_msg *msg;
5092 int ret = -1;
5093 int count = 0;
5094
5095 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5096
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005097 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5099 "IBSS mode");
5100 return -1;
5101 }
5102
5103retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005104 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5105 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5106 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107
5108 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5109 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005110 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5111 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5113 drv->ssid_len = params->ssid_len;
5114
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005115 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5116 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005117 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005118
5119 ret = nl80211_set_conn_keys(params, msg);
5120 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005121 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005123 if (params->bssid && params->fixed_bssid) {
5124 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5125 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005126 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5127 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005128 }
5129
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005130 if (params->fixed_freq) {
5131 wpa_printf(MSG_DEBUG, " * fixed_freq");
5132 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5133 goto fail;
5134 }
5135
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005136 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5137 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5138 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5139 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005140 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005141 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5142 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005143 }
5144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 if (params->wpa_ie) {
5146 wpa_hexdump(MSG_DEBUG,
5147 " * Extra IEs for Beacon/Probe Response frames",
5148 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005149 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5150 params->wpa_ie))
5151 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152 }
5153
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005154 ret = nl80211_ht_vht_overrides(msg, params);
5155 if (ret < 0)
5156 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5159 msg = NULL;
5160 if (ret) {
5161 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5162 ret, strerror(-ret));
5163 count++;
5164 if (ret == -EALREADY && count == 1) {
5165 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5166 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005167 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168 nlmsg_free(msg);
5169 goto retry;
5170 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005171 } else {
5172 wpa_printf(MSG_DEBUG,
5173 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005176fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177 nlmsg_free(msg);
5178 return ret;
5179}
5180
5181
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005182static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5183 struct wpa_driver_associate_params *params,
5184 struct nl_msg *msg)
5185{
5186 if (params->fils_erp_username_len) {
5187 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5188 params->fils_erp_username,
5189 params->fils_erp_username_len);
5190 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5191 params->fils_erp_username_len,
5192 params->fils_erp_username))
5193 return -1;
5194 }
5195
5196 if (params->fils_erp_realm_len) {
5197 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5198 params->fils_erp_realm,
5199 params->fils_erp_realm_len);
5200 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5201 params->fils_erp_realm_len, params->fils_erp_realm))
5202 return -1;
5203 }
5204
5205 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5206 params->fils_erp_next_seq_num);
5207 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5208 params->fils_erp_next_seq_num))
5209 return -1;
5210
5211 if (params->fils_erp_rrk_len) {
5212 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5213 (unsigned long) params->fils_erp_rrk_len);
5214 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5215 params->fils_erp_rrk_len, params->fils_erp_rrk))
5216 return -1;
5217 }
5218
5219 return 0;
5220}
5221
5222
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005223static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5224 struct wpa_driver_associate_params *params,
5225 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226{
Paul Stewart092955c2017-02-06 09:13:09 -08005227 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5228 return -1;
5229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230 if (params->bssid) {
5231 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5232 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005233 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5234 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005236
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005237 if (params->bssid_hint) {
5238 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5239 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005240 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5241 params->bssid_hint))
5242 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005243 }
5244
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005245 if (params->freq.freq) {
5246 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005247 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5248 params->freq.freq))
5249 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005250 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005251 } else
5252 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005253
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005254 if (params->freq_hint) {
5255 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005256 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5257 params->freq_hint))
5258 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005259 }
5260
Dmitry Shmidt04949592012-07-19 12:16:46 -07005261 if (params->bg_scan_period >= 0) {
5262 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5263 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005264 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5265 params->bg_scan_period))
5266 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005267 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005269 if (params->ssid) {
5270 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5271 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005272 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5273 params->ssid))
5274 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005275 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005276 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5278 drv->ssid_len = params->ssid_len;
5279 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005282 if (params->wpa_ie &&
5283 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5284 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005286 if (params->wpa_proto) {
5287 enum nl80211_wpa_versions ver = 0;
5288
5289 if (params->wpa_proto & WPA_PROTO_WPA)
5290 ver |= NL80211_WPA_VERSION_1;
5291 if (params->wpa_proto & WPA_PROTO_RSN)
5292 ver |= NL80211_WPA_VERSION_2;
5293
5294 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005295 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5296 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005297 }
5298
5299 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5300 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5301 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005302 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5303 cipher))
5304 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005305 }
5306
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005307 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5308 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5309 /*
5310 * This is likely to work even though many drivers do not
5311 * advertise support for operations without GTK.
5312 */
5313 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5314 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005315 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5316 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005317 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5318 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005319 }
5320
5321 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5322 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5323 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5324 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005325 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005326 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5327 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005328 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005329 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005330 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5331 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5332 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5333 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07005334 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5335 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5336 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
Paul Stewart092955c2017-02-06 09:13:09 -08005337 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005338
5339 switch (params->key_mgmt_suite) {
5340 case WPA_KEY_MGMT_CCKM:
Paul Stewart092955c2017-02-06 09:13:09 -08005341 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005342 break;
5343 case WPA_KEY_MGMT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005344 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005345 break;
5346 case WPA_KEY_MGMT_FT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005347 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005348 break;
5349 case WPA_KEY_MGMT_FT_PSK:
Paul Stewart092955c2017-02-06 09:13:09 -08005350 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005351 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005352 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005353 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005354 break;
5355 case WPA_KEY_MGMT_PSK_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005356 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005357 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005358 case WPA_KEY_MGMT_OSEN:
Paul Stewart092955c2017-02-06 09:13:09 -08005359 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005360 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005361 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Paul Stewart092955c2017-02-06 09:13:09 -08005362 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005363 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005364 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Paul Stewart092955c2017-02-06 09:13:09 -08005365 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005366 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005367 case WPA_KEY_MGMT_FILS_SHA256:
5368 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5369 break;
5370 case WPA_KEY_MGMT_FILS_SHA384:
5371 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5372 break;
5373 case WPA_KEY_MGMT_FT_FILS_SHA256:
5374 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5375 break;
5376 case WPA_KEY_MGMT_FT_FILS_SHA384:
5377 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5378 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005379 case WPA_KEY_MGMT_OWE:
5380 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5381 break;
5382 case WPA_KEY_MGMT_DPP:
5383 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5384 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005385 case WPA_KEY_MGMT_PSK:
5386 default:
Paul Stewart092955c2017-02-06 09:13:09 -08005387 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005388 break;
5389 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005390 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005391 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5392 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005393 }
5394
Roshan Pius3a1667e2018-07-03 15:17:14 -07005395 /* Add PSK in case of 4-way handshake offload */
5396 if (params->psk &&
5397 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)) {
5398 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5399 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5400 return -1;
5401 }
5402
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005403 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5404 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005405
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005406 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5407 (params->pairwise_suite == WPA_CIPHER_NONE ||
5408 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5409 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5410 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5411 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5412 return -1;
5413
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005414 if (params->rrm_used) {
5415 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005416 if ((!((drv_rrm_flags &
5417 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5418 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5419 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005420 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5421 return -1;
5422 }
5423
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005424 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005425 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005426
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005427 if (params->p2p)
5428 wpa_printf(MSG_DEBUG, " * P2P group");
5429
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005430 if (params->pbss) {
5431 wpa_printf(MSG_DEBUG, " * PBSS");
5432 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5433 return -1;
5434 }
5435
Dmitry Shmidte4663042016-04-04 10:07:49 -07005436 drv->connect_reassoc = 0;
5437 if (params->prev_bssid) {
5438 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5439 MAC2STR(params->prev_bssid));
5440 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5441 params->prev_bssid))
5442 return -1;
5443 drv->connect_reassoc = 1;
5444 }
5445
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005446 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5447 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5448 return -1;
5449
Roshan Pius3a1667e2018-07-03 15:17:14 -07005450 if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5451 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5452 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5453 return -1;
5454
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005455 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005456}
5457
5458
5459static int wpa_driver_nl80211_try_connect(
5460 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005461 struct wpa_driver_associate_params *params,
5462 struct nl_handle *nl_connect)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005463{
5464 struct nl_msg *msg;
5465 enum nl80211_auth_type type;
5466 int ret;
5467 int algs;
5468
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005469#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005470 if (params->req_key_mgmt_offload && params->psk &&
5471 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5472 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5473 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5474 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5475 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5476 if (ret)
5477 return ret;
5478 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005479#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005480
5481 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5482 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005483 if (!msg)
5484 return -1;
5485
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005486 ret = nl80211_connect_common(drv, params, msg);
5487 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005488 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005489
Roshan Pius3a1667e2018-07-03 15:17:14 -07005490 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5491 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5492 goto fail;
5493
5494 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
5495 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
5496 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
5497 goto fail;
5498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 algs = 0;
5500 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5501 algs++;
5502 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5503 algs++;
5504 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5505 algs++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005506 if (params->auth_alg & WPA_AUTH_ALG_FILS)
5507 algs++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005508 if (params->auth_alg & WPA_AUTH_ALG_FT)
5509 algs++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 if (algs > 1) {
5511 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5512 "selection");
5513 goto skip_auth_type;
5514 }
5515
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005516 type = get_nl_auth_type(params->auth_alg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005518 if (type == NL80211_AUTHTYPE_MAX ||
5519 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005520 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521
5522skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523 ret = nl80211_set_conn_keys(params, msg);
5524 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005525 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005526
Roshan Pius3a1667e2018-07-03 15:17:14 -07005527 if (nl_connect)
5528 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
5529 else
5530 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532 msg = NULL;
5533 if (ret) {
5534 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5535 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005536 } else {
5537 wpa_printf(MSG_DEBUG,
5538 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005540
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005541fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 nlmsg_free(msg);
5543 return ret;
5544
5545}
5546
5547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005548static int wpa_driver_nl80211_connect(
5549 struct wpa_driver_nl80211_data *drv,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005550 struct wpa_driver_associate_params *params,
5551 struct nl_handle *nl_connect)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005552{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305553 int ret;
5554
5555 /* Store the connection attempted bssid for future use */
5556 if (params->bssid)
5557 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5558 else
5559 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5560
Roshan Pius3a1667e2018-07-03 15:17:14 -07005561 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005562 if (ret == -EALREADY) {
5563 /*
5564 * cfg80211 does not currently accept new connections if
5565 * we are already connected. As a workaround, force
5566 * disconnection and try again.
5567 */
5568 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5569 "disconnecting before reassociation "
5570 "attempt");
5571 if (wpa_driver_nl80211_disconnect(
5572 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5573 return -1;
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 }
5576 return ret;
5577}
5578
5579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580static int wpa_driver_nl80211_associate(
5581 void *priv, struct wpa_driver_associate_params *params)
5582{
5583 struct i802_bss *bss = priv;
5584 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005585 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005586 struct nl_msg *msg;
5587
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005588 nl80211_unmask_11b_rates(bss);
5589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590 if (params->mode == IEEE80211_MODE_AP)
5591 return wpa_driver_nl80211_ap(drv, params);
5592
5593 if (params->mode == IEEE80211_MODE_IBSS)
5594 return wpa_driver_nl80211_ibss(drv, params);
5595
5596 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005597 enum nl80211_iftype nlmode = params->p2p ?
5598 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005599 struct nl_handle *nl_connect = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005600
5601 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005602 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005603 if (params->auth_alg & WPA_AUTH_ALG_SAE)
5604 nl_connect = bss->nl_connect;
5605 return wpa_driver_nl80211_connect(drv, params, nl_connect);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606 }
5607
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005608 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5611 drv->ifindex);
5612 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 if (!msg)
5614 return -1;
5615
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005616 ret = nl80211_connect_common(drv, params, msg);
5617 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005618 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619
Roshan Pius3a1667e2018-07-03 15:17:14 -07005620 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5621 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5622 goto fail;
5623
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005624 if (params->fils_kek) {
5625 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5626 (unsigned int) params->fils_kek_len);
5627 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5628 params->fils_kek))
5629 goto fail;
5630 }
5631 if (params->fils_nonces) {
5632 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5633 params->fils_nonces,
5634 params->fils_nonces_len);
5635 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5636 params->fils_nonces_len, params->fils_nonces))
5637 goto fail;
5638 }
5639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5641 msg = NULL;
5642 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 wpa_dbg(drv->ctx, MSG_DEBUG,
5644 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5645 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005647 } else {
5648 wpa_printf(MSG_DEBUG,
5649 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005652fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653 nlmsg_free(msg);
5654 return ret;
5655}
5656
5657
5658static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005659 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660{
5661 struct nl_msg *msg;
5662 int ret = -ENOBUFS;
5663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005664 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5665 ifindex, mode, nl80211_iftype_str(mode));
5666
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005667 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5668 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5669 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670
5671 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005672 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005673 if (!ret)
5674 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005675fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005676 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5678 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5679 return ret;
5680}
5681
5682
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005683static int wpa_driver_nl80211_set_mode_impl(
5684 struct i802_bss *bss,
5685 enum nl80211_iftype nlmode,
5686 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005688 struct wpa_driver_nl80211_data *drv = bss->drv;
5689 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005691 int was_ap = is_ap_interface(drv->nlmode);
5692 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005693 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005695 if (TEST_FAIL())
5696 return -1;
5697
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005698 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5699 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5700 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005701
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005702 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 drv->nlmode = nlmode;
5704 ret = 0;
5705 goto done;
5706 }
5707
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005708 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005709 return -1;
5710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 if (nlmode == drv->nlmode) {
5712 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5713 "requested mode - ignore error");
5714 ret = 0;
5715 goto done; /* Already in the requested mode */
5716 }
5717
5718 /* mac80211 doesn't allow mode changes while the device is up, so
5719 * take the device down, try to set the mode again, and bring the
5720 * device back up.
5721 */
5722 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5723 "interface down");
5724 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005725 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005726 if (res == -EACCES || res == -ENODEV)
5727 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005728 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005729 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5730 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005731 os_sleep(0, 100000);
5732 continue;
5733 }
5734
5735 /*
5736 * Setting the mode will fail for some drivers if the phy is
5737 * on a frequency that the mode is disallowed in.
5738 */
5739 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005740 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005741 if (res) {
5742 wpa_printf(MSG_DEBUG,
5743 "nl80211: Failed to set frequency on interface");
5744 }
5745 }
5746
5747 /* Try to set the mode again while the interface is down */
5748 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5749 if (mode_switch_res == -EBUSY) {
5750 wpa_printf(MSG_DEBUG,
5751 "nl80211: Delaying mode set while interface going down");
5752 os_sleep(0, 100000);
5753 continue;
5754 }
5755 ret = mode_switch_res;
5756 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 }
5758
5759 if (!ret) {
5760 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5761 "interface is down");
5762 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005763 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764 }
5765
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005766 /* Bring the interface back up */
5767 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5768 if (res != 0) {
5769 wpa_printf(MSG_DEBUG,
5770 "nl80211: Failed to set interface up after switching mode");
5771 ret = -1;
5772 }
5773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005775 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5777 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005778 return ret;
5779 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005780
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005781 if (is_p2p_net_interface(nlmode)) {
5782 wpa_printf(MSG_DEBUG,
5783 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5784 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005785 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005786 } else if (drv->disabled_11b_rates) {
5787 wpa_printf(MSG_DEBUG,
5788 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5789 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005790 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005791 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005793 if (is_ap_interface(nlmode)) {
5794 nl80211_mgmt_unsubscribe(bss, "start AP");
5795 /* Setup additional AP mode functionality if needed */
5796 if (nl80211_setup_ap(bss))
5797 return -1;
5798 } else if (was_ap) {
5799 /* Remove additional AP mode functionality */
5800 nl80211_teardown_ap(bss);
5801 } else {
5802 nl80211_mgmt_unsubscribe(bss, "mode change");
5803 }
5804
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005805 if (is_mesh_interface(nlmode) &&
5806 nl80211_mgmt_subscribe_mesh(bss))
5807 return -1;
5808
Dmitry Shmidt04949592012-07-19 12:16:46 -07005809 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005810 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005811 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5812 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5813 "frame processing - ignore for now");
5814
5815 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005816}
5817
5818
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005819int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5820 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005821{
5822 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5823}
5824
5825
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005826static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5827 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005828{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005829 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005830 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005831}
5832
5833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834static int wpa_driver_nl80211_get_capa(void *priv,
5835 struct wpa_driver_capa *capa)
5836{
5837 struct i802_bss *bss = priv;
5838 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 if (!drv->has_capability)
5841 return -1;
5842 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005843 if (drv->extended_capa && drv->extended_capa_mask) {
5844 capa->extended_capa = drv->extended_capa;
5845 capa->extended_capa_mask = drv->extended_capa_mask;
5846 capa->extended_capa_len = drv->extended_capa_len;
5847 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005848
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005849 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850}
5851
5852
5853static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5854{
5855 struct i802_bss *bss = priv;
5856 struct wpa_driver_nl80211_data *drv = bss->drv;
5857
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005858 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5859 bss->ifname, drv->operstate, state,
5860 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005862 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863 state ? IF_OPER_UP : IF_OPER_DORMANT);
5864}
5865
5866
5867static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5868{
5869 struct i802_bss *bss = priv;
5870 struct wpa_driver_nl80211_data *drv = bss->drv;
5871 struct nl_msg *msg;
5872 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005873 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005874
5875 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5876 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5877 return 0;
5878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005880 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5881 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 os_memset(&upd, 0, sizeof(upd));
5884 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5885 if (authorized)
5886 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005887
5888 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5889 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5890 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5891 nlmsg_free(msg);
5892 return -ENOBUFS;
5893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005895 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005896 if (!ret)
5897 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005898 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5899 ret, strerror(-ret));
5900 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901}
5902
5903
Jouni Malinen75ecf522011-06-27 15:19:46 -07005904/* Set kernel driver on given frequency (MHz) */
5905static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005907 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005908 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005909}
5910
5911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912static inline int min_int(int a, int b)
5913{
5914 if (a < b)
5915 return a;
5916 return b;
5917}
5918
5919
5920static int get_key_handler(struct nl_msg *msg, void *arg)
5921{
5922 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5923 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5924
5925 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5926 genlmsg_attrlen(gnlh, 0), NULL);
5927
5928 /*
5929 * TODO: validate the key index and mac address!
5930 * Otherwise, there's a race condition as soon as
5931 * the kernel starts sending key notifications.
5932 */
5933
5934 if (tb[NL80211_ATTR_KEY_SEQ])
5935 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5936 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5937 return NL_SKIP;
5938}
5939
5940
5941static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5942 int idx, u8 *seq)
5943{
5944 struct i802_bss *bss = priv;
5945 struct wpa_driver_nl80211_data *drv = bss->drv;
5946 struct nl_msg *msg;
5947
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005948 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5949 NL80211_CMD_GET_KEY);
5950 if (!msg ||
5951 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5952 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5953 nlmsg_free(msg);
5954 return -ENOBUFS;
5955 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956
5957 memset(seq, 0, 6);
5958
5959 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005960}
5961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005962
5963static int i802_set_rts(void *priv, int rts)
5964{
5965 struct i802_bss *bss = priv;
5966 struct wpa_driver_nl80211_data *drv = bss->drv;
5967 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005968 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969 u32 val;
5970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971 if (rts >= 2347)
5972 val = (u32) -1;
5973 else
5974 val = rts;
5975
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005976 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5977 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5978 nlmsg_free(msg);
5979 return -ENOBUFS;
5980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981
5982 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5983 if (!ret)
5984 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5986 "%d (%s)", rts, ret, strerror(-ret));
5987 return ret;
5988}
5989
5990
5991static int i802_set_frag(void *priv, int frag)
5992{
5993 struct i802_bss *bss = priv;
5994 struct wpa_driver_nl80211_data *drv = bss->drv;
5995 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005996 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997 u32 val;
5998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005999 if (frag >= 2346)
6000 val = (u32) -1;
6001 else
6002 val = frag;
6003
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006004 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6005 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6006 nlmsg_free(msg);
6007 return -ENOBUFS;
6008 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009
6010 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6011 if (!ret)
6012 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6014 "%d: %d (%s)", frag, ret, strerror(-ret));
6015 return ret;
6016}
6017
6018
6019static int i802_flush(void *priv)
6020{
6021 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006023 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006024
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006025 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6026 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027
6028 /*
6029 * XXX: FIX! this needs to flush all VLANs too
6030 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006031 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6032 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033 if (res) {
6034 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6035 "(%s)", res, strerror(-res));
6036 }
6037 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038}
6039
6040
6041static int get_sta_handler(struct nl_msg *msg, void *arg)
6042{
6043 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6044 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6045 struct hostap_sta_driver_data *data = arg;
6046 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6047 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6048 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6049 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6050 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6051 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6052 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006053 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006054 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6055 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006056 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07006057 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006058 };
6059 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6060 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6061 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6062 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6063 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6064 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6065 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6066 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 };
6068
6069 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6070 genlmsg_attrlen(gnlh, 0), NULL);
6071
6072 /*
6073 * TODO: validate the interface and mac address!
6074 * Otherwise, there's a race condition as soon as
6075 * the kernel starts sending station notifications.
6076 */
6077
6078 if (!tb[NL80211_ATTR_STA_INFO]) {
6079 wpa_printf(MSG_DEBUG, "sta stats missing!");
6080 return NL_SKIP;
6081 }
6082 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6083 tb[NL80211_ATTR_STA_INFO],
6084 stats_policy)) {
6085 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6086 return NL_SKIP;
6087 }
6088
6089 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6090 data->inactive_msec =
6091 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006092 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093 if (stats[NL80211_STA_INFO_RX_BYTES])
6094 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6095 if (stats[NL80211_STA_INFO_TX_BYTES])
6096 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006097 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6098 stats[NL80211_STA_INFO_TX_BYTES64]) {
6099 /*
6100 * The driver supports 64-bit counters, so use them to override
6101 * the 32-bit values.
6102 */
6103 data->rx_bytes =
6104 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6105 data->tx_bytes =
6106 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6107 data->bytes_64bit = 1;
6108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 if (stats[NL80211_STA_INFO_RX_PACKETS])
6110 data->rx_packets =
6111 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6112 if (stats[NL80211_STA_INFO_TX_PACKETS])
6113 data->tx_packets =
6114 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03006115 if (stats[NL80211_STA_INFO_TX_FAILED])
6116 data->tx_retry_failed =
6117 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006118 if (stats[NL80211_STA_INFO_SIGNAL])
6119 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006120 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6121 data->last_ack_rssi =
6122 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6123 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6124 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006125
6126 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6127 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6128 stats[NL80211_STA_INFO_TX_BITRATE],
6129 rate_policy) == 0) {
6130 if (rate[NL80211_RATE_INFO_BITRATE32])
6131 data->current_tx_rate =
6132 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6133 else if (rate[NL80211_RATE_INFO_BITRATE])
6134 data->current_tx_rate =
6135 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6136
6137 if (rate[NL80211_RATE_INFO_MCS]) {
6138 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6139 data->flags |= STA_DRV_DATA_TX_MCS;
6140 }
6141 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6142 data->tx_vhtmcs =
6143 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6144 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6145 }
6146 if (rate[NL80211_RATE_INFO_SHORT_GI])
6147 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6148 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6149 data->tx_vht_nss =
6150 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6151 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6152 }
6153 }
6154
6155 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6156 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6157 stats[NL80211_STA_INFO_RX_BITRATE],
6158 rate_policy) == 0) {
6159 if (rate[NL80211_RATE_INFO_BITRATE32])
6160 data->current_rx_rate =
6161 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6162 else if (rate[NL80211_RATE_INFO_BITRATE])
6163 data->current_rx_rate =
6164 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6165
6166 if (rate[NL80211_RATE_INFO_MCS]) {
6167 data->rx_mcs =
6168 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6169 data->flags |= STA_DRV_DATA_RX_MCS;
6170 }
6171 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6172 data->rx_vhtmcs =
6173 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6174 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6175 }
6176 if (rate[NL80211_RATE_INFO_SHORT_GI])
6177 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6178 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6179 data->rx_vht_nss =
6180 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6181 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6182 }
6183 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006184
6185 return NL_SKIP;
6186}
6187
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006188static int i802_read_sta_data(struct i802_bss *bss,
6189 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006190 const u8 *addr)
6191{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006192 struct nl_msg *msg;
6193
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006194 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6195 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6196 nlmsg_free(msg);
6197 return -ENOBUFS;
6198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006200 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201}
6202
6203
6204static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6205 int cw_min, int cw_max, int burst_time)
6206{
6207 struct i802_bss *bss = priv;
6208 struct wpa_driver_nl80211_data *drv = bss->drv;
6209 struct nl_msg *msg;
6210 struct nlattr *txq, *params;
6211
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006212 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213 if (!msg)
6214 return -1;
6215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6217 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006218 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006219
6220 /* We are only sending parameters for a single TXQ at a time */
6221 params = nla_nest_start(msg, 1);
6222 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006223 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006224
6225 switch (queue) {
6226 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006227 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6228 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229 break;
6230 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006231 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6232 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006233 break;
6234 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006235 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6236 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237 break;
6238 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006239 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6240 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006241 break;
6242 }
6243 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6244 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006245 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6246 (burst_time * 100 + 16) / 32) ||
6247 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6248 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6249 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6250 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251
6252 nla_nest_end(msg, params);
6253
6254 nla_nest_end(msg, txq);
6255
6256 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6257 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006258 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006259fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006260 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006261 return -1;
6262}
6263
6264
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006265static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 const char *ifname, int vlan_id)
6267{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268 struct wpa_driver_nl80211_data *drv = bss->drv;
6269 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006270 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006271
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006272 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6273 ", ifname=%s[%d], vlan_id=%d)",
6274 bss->ifname, if_nametoindex(bss->ifname),
6275 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006276 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6277 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6278 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6279 nlmsg_free(msg);
6280 return -ENOBUFS;
6281 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282
6283 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6284 if (ret < 0) {
6285 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6286 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6287 MAC2STR(addr), ifname, vlan_id, ret,
6288 strerror(-ret));
6289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006290 return ret;
6291}
6292
6293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294static int i802_get_inact_sec(void *priv, const u8 *addr)
6295{
6296 struct hostap_sta_driver_data data;
6297 int ret;
6298
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006299 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300 data.inactive_msec = (unsigned long) -1;
6301 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006302 if (ret == -ENOENT)
6303 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304 if (ret || data.inactive_msec == (unsigned long) -1)
6305 return -1;
6306 return data.inactive_msec / 1000;
6307}
6308
6309
6310static int i802_sta_clear_stats(void *priv, const u8 *addr)
6311{
6312#if 0
6313 /* TODO */
6314#endif
6315 return 0;
6316}
6317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318
6319static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6320 int reason)
6321{
6322 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006323 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006324 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006325 u8 channel;
6326
6327 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6328 HOSTAPD_MODE_IEEE80211AD) {
6329 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6330 * disassociate the STA instead. */
6331 return i802_sta_disassoc(priv, own_addr, addr, reason);
6332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006333
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006334 if (is_mesh_interface(drv->nlmode))
6335 return -1;
6336
Dmitry Shmidt04949592012-07-19 12:16:46 -07006337 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006338 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006340 memset(&mgmt, 0, sizeof(mgmt));
6341 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6342 WLAN_FC_STYPE_DEAUTH);
6343 memcpy(mgmt.da, addr, ETH_ALEN);
6344 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6345 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6346 mgmt.u.deauth.reason_code = host_to_le16(reason);
6347 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6348 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006349 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006350 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006351}
6352
6353
6354static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6355 int reason)
6356{
6357 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006358 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359 struct ieee80211_mgmt mgmt;
6360
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006361 if (is_mesh_interface(drv->nlmode))
6362 return -1;
6363
Dmitry Shmidt04949592012-07-19 12:16:46 -07006364 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006365 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006367 memset(&mgmt, 0, sizeof(mgmt));
6368 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6369 WLAN_FC_STYPE_DISASSOC);
6370 memcpy(mgmt.da, addr, ETH_ALEN);
6371 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6372 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6373 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6374 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6375 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006376 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006377 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006378}
6379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006381static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6382{
6383 char buf[200], *pos, *end;
6384 int i, res;
6385
6386 pos = buf;
6387 end = pos + sizeof(buf);
6388
6389 for (i = 0; i < drv->num_if_indices; i++) {
6390 if (!drv->if_indices[i])
6391 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006392 res = os_snprintf(pos, end - pos, " %d(%d)",
6393 drv->if_indices[i],
6394 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006395 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006396 break;
6397 pos += res;
6398 }
6399 *pos = '\0';
6400
6401 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6402 drv->num_if_indices, buf);
6403}
6404
6405
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006406static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6407 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006408{
6409 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006410 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006411
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006412 wpa_printf(MSG_DEBUG,
6413 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6414 ifidx, ifidx_reason);
6415 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006416 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6417 ifidx);
6418 return;
6419 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006420 for (i = 0; i < drv->num_if_indices; i++) {
6421 if (drv->if_indices[i] == 0) {
6422 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006423 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006424 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006425 return;
6426 }
6427 }
6428
6429 if (drv->if_indices != drv->default_if_indices)
6430 old = drv->if_indices;
6431 else
6432 old = NULL;
6433
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006434 if (drv->if_indices_reason != drv->default_if_indices_reason)
6435 old_reason = drv->if_indices_reason;
6436 else
6437 old_reason = NULL;
6438
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006439 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6440 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006441 drv->if_indices_reason = os_realloc_array(old_reason,
6442 drv->num_if_indices + 1,
6443 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006444 if (!drv->if_indices) {
6445 if (!old)
6446 drv->if_indices = drv->default_if_indices;
6447 else
6448 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006449 }
6450 if (!drv->if_indices_reason) {
6451 if (!old_reason)
6452 drv->if_indices_reason = drv->default_if_indices_reason;
6453 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07006454 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006455 }
6456 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006457 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6458 "interfaces");
6459 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6460 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006461 }
6462 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006463 os_memcpy(drv->if_indices, drv->default_if_indices,
6464 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006465 if (!old_reason)
6466 os_memcpy(drv->if_indices_reason,
6467 drv->default_if_indices_reason,
6468 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006469 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006470 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006471 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006472 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006473}
6474
6475
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006476static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6477 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006478{
6479 int i;
6480
6481 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006482 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6483 (drv->if_indices_reason[i] == ifidx_reason ||
6484 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006485 drv->if_indices[i] = 0;
6486 break;
6487 }
6488 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006489 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006490}
6491
6492
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006493static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6494 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006495{
6496 int i;
6497
6498 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006499 if (drv->if_indices[i] == ifidx &&
6500 (drv->if_indices_reason[i] == ifidx_reason ||
6501 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006502 return 1;
6503
6504 return 0;
6505}
6506
6507
6508static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006509 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006510{
6511 struct i802_bss *bss = priv;
6512 struct wpa_driver_nl80211_data *drv = bss->drv;
6513 char name[IFNAMSIZ + 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07006514 union wpa_event_data event;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006515
6516 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006517 if (ifname_wds)
6518 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6519
Jouni Malinen75ecf522011-06-27 15:19:46 -07006520 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6521 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6522 if (val) {
6523 if (!if_nametoindex(name)) {
6524 if (nl80211_create_iface(drv, name,
6525 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006526 bss->addr, 1, NULL, NULL, 0) <
6527 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006528 return -1;
6529 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006530 linux_br_add_if(drv->global->ioctl_sock,
6531 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006532 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006533
6534 os_memset(&event, 0, sizeof(event));
6535 event.wds_sta_interface.sta_addr = addr;
6536 event.wds_sta_interface.ifname = name;
6537 event.wds_sta_interface.istatus = INTERFACE_ADDED;
6538 wpa_supplicant_event(drv->ctx,
6539 EVENT_WDS_STA_INTERFACE_STATUS,
6540 &event);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006541 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006542 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6543 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6544 "interface %s up", name);
6545 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006546 return i802_set_sta_vlan(priv, addr, name, 0);
6547 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006548 if (bridge_ifname)
6549 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6550 name);
6551
Jouni Malinen75ecf522011-06-27 15:19:46 -07006552 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006553 nl80211_remove_iface(drv, if_nametoindex(name));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006554 os_memset(&event, 0, sizeof(event));
6555 event.wds_sta_interface.sta_addr = addr;
6556 event.wds_sta_interface.ifname = name;
6557 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
6558 wpa_supplicant_event(drv->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
6559 &event);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006560 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006561 }
6562}
6563
6564
6565static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6566{
6567 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6568 struct sockaddr_ll lladdr;
6569 unsigned char buf[3000];
6570 int len;
6571 socklen_t fromlen = sizeof(lladdr);
6572
6573 len = recvfrom(sock, buf, sizeof(buf), 0,
6574 (struct sockaddr *)&lladdr, &fromlen);
6575 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006576 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6577 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006578 return;
6579 }
6580
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006581 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006582 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6583}
6584
6585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006586static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6587 struct i802_bss *bss,
6588 const char *brname, const char *ifname)
6589{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006590 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006591 char in_br[IFNAMSIZ];
6592
6593 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006594 br_ifindex = if_nametoindex(brname);
6595 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596 /*
6597 * Bridge was configured, but the bridge device does
6598 * not exist. Try to add it now.
6599 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006600 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006601 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6602 "bridge interface %s: %s",
6603 brname, strerror(errno));
6604 return -1;
6605 }
6606 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006607 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006608 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006609 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006610 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611
6612 if (linux_br_get(in_br, ifname) == 0) {
6613 if (os_strcmp(in_br, brname) == 0)
6614 return 0; /* already in the bridge */
6615
6616 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6617 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006618 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6619 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006620 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6621 "remove interface %s from bridge "
6622 "%s: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006623 ifname, in_br, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006624 return -1;
6625 }
6626 }
6627
6628 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6629 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006630 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006631 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6632 "into bridge %s: %s",
6633 ifname, brname, strerror(errno));
6634 return -1;
6635 }
6636 bss->added_if_into_bridge = 1;
6637
6638 return 0;
6639}
6640
6641
6642static void *i802_init(struct hostapd_data *hapd,
6643 struct wpa_init_params *params)
6644{
6645 struct wpa_driver_nl80211_data *drv;
6646 struct i802_bss *bss;
6647 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006648 char master_ifname[IFNAMSIZ];
6649 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006650 int br_added = 0;
6651
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006652 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6653 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006654 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006655 if (bss == NULL)
6656 return NULL;
6657
6658 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006659
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006660 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006661 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006662 params->ifname, master_ifname);
6663 br_ifindex = if_nametoindex(master_ifname);
6664 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6665 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6666 linux_master_get(master_ifname, params->ifname) == 0) {
6667 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6668 params->ifname, master_ifname);
6669 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006670 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006671
6672 /* check if master itself is under bridge */
6673 if (linux_br_get(master_ifname, master_ifname) == 0) {
6674 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6675 master_ifname);
6676 br_ifindex = if_nametoindex(master_ifname);
6677 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6678 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006680 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006682
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006683 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006685 for (i = 0; i < params->num_bridge; i++) {
6686 if (params->bridge[i]) {
6687 ifindex = if_nametoindex(params->bridge[i]);
6688 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006689 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 if (ifindex == br_ifindex)
6691 br_added = 1;
6692 }
6693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694
6695 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006696 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006698 if (params->num_bridge && params->bridge[0]) {
6699 if (i802_check_bridge(drv, bss, params->bridge[0],
6700 params->ifname) < 0)
6701 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006702 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006703 br_added = 1;
6704 }
6705
6706 if (!br_added && br_ifindex &&
6707 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006708 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006709
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006710#ifdef CONFIG_LIBNL3_ROUTE
6711 if (bss->added_if_into_bridge) {
6712 drv->rtnl_sk = nl_socket_alloc();
6713 if (drv->rtnl_sk == NULL) {
6714 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6715 goto failed;
6716 }
6717
6718 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6719 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6720 strerror(errno));
6721 goto failed;
6722 }
6723 }
6724#endif /* CONFIG_LIBNL3_ROUTE */
6725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006726 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6727 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006728 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6729 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006730 goto failed;
6731 }
6732
6733 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6734 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006735 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006736 goto failed;
6737 }
6738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006739 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6740 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006741 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006742 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006744 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006746 return bss;
6747
6748failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006749 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006750 return NULL;
6751}
6752
6753
6754static void i802_deinit(void *priv)
6755{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006756 struct i802_bss *bss = priv;
6757 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758}
6759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006760
6761static enum nl80211_iftype wpa_driver_nl80211_if_type(
6762 enum wpa_driver_if_type type)
6763{
6764 switch (type) {
6765 case WPA_IF_STATION:
6766 return NL80211_IFTYPE_STATION;
6767 case WPA_IF_P2P_CLIENT:
6768 case WPA_IF_P2P_GROUP:
6769 return NL80211_IFTYPE_P2P_CLIENT;
6770 case WPA_IF_AP_VLAN:
6771 return NL80211_IFTYPE_AP_VLAN;
6772 case WPA_IF_AP_BSS:
6773 return NL80211_IFTYPE_AP;
6774 case WPA_IF_P2P_GO:
6775 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006776 case WPA_IF_P2P_DEVICE:
6777 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006778 case WPA_IF_MESH:
6779 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006780 default:
6781 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006783}
6784
6785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006786static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6787{
6788 struct wpa_driver_nl80211_data *drv;
6789 dl_list_for_each(drv, &global->interfaces,
6790 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006791 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006792 return 1;
6793 }
6794 return 0;
6795}
6796
6797
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006798static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799{
6800 unsigned int idx;
6801
6802 if (!drv->global)
6803 return -1;
6804
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006805 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006806 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006807 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006808 new_addr[0] ^= idx << 2;
6809 if (!nl80211_addr_in_use(drv->global, new_addr))
6810 break;
6811 }
6812 if (idx == 64)
6813 return -1;
6814
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006815 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006816 MACSTR, MAC2STR(new_addr));
6817
6818 return 0;
6819}
6820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006821
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006822struct wdev_info {
6823 u64 wdev_id;
6824 int wdev_id_set;
6825 u8 macaddr[ETH_ALEN];
6826};
6827
6828static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6829{
6830 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6831 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6832 struct wdev_info *wi = arg;
6833
6834 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6835 genlmsg_attrlen(gnlh, 0), NULL);
6836 if (tb[NL80211_ATTR_WDEV]) {
6837 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6838 wi->wdev_id_set = 1;
6839 }
6840
6841 if (tb[NL80211_ATTR_MAC])
6842 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6843 ETH_ALEN);
6844
6845 return NL_SKIP;
6846}
6847
6848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6850 const char *ifname, const u8 *addr,
6851 void *bss_ctx, void **drv_priv,
6852 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006853 const char *bridge, int use_existing,
6854 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006856 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857 struct i802_bss *bss = priv;
6858 struct wpa_driver_nl80211_data *drv = bss->drv;
6859 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006860 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006861
6862 if (addr)
6863 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006864 nlmode = wpa_driver_nl80211_if_type(type);
6865 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6866 struct wdev_info p2pdev_info;
6867
6868 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6869 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6870 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006871 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006872 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6873 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6874 ifname);
6875 return -1;
6876 }
6877
6878 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6879 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6880 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6881 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6882 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6883 ifname,
6884 (long long unsigned int) p2pdev_info.wdev_id);
6885 } else {
6886 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006887 0, NULL, NULL, use_existing);
6888 if (use_existing && ifidx == -ENFILE) {
6889 added = 0;
6890 ifidx = if_nametoindex(ifname);
6891 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006892 return -1;
6893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894 }
6895
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006896 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006897 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006898 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6899 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006900 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006901 if (added)
6902 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006903 return -1;
6904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905 }
6906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907 if (!addr &&
6908 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006909 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6910 type == WPA_IF_STATION)) {
6911 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006912 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006913
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006914 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006915 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006916 if (added)
6917 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006918 return -1;
6919 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006920 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006921 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006922 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006923 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006924 if (added)
6925 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926 return -1;
6927 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006928 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006929 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006930 if (added)
6931 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006932 return -1;
6933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006935 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006938 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006939 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6940 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006941 if (added)
6942 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006943 return -1;
6944 }
6945
6946 if (bridge &&
6947 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6948 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6949 "interface %s to a bridge %s",
6950 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006951 if (added)
6952 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006953 os_free(new_bss);
6954 return -1;
6955 }
6956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006957 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6958 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006959 if (added)
6960 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961 os_free(new_bss);
6962 return -1;
6963 }
6964 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006965 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006966 new_bss->ifindex = ifidx;
6967 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006968 new_bss->next = drv->first_bss->next;
6969 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006970 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006971 new_bss->added_if = added;
6972 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006973 if (drv_priv)
6974 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006975 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006976
6977 /* Subscribe management frames for this WPA_IF_AP_BSS */
6978 if (nl80211_setup_ap(new_bss))
6979 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006982 if (drv->global)
6983 drv->global->if_add_ifindex = ifidx;
6984
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006985 /*
6986 * Some virtual interfaces need to process EAPOL packets and events on
6987 * the parent interface. This is used mainly with hostapd.
6988 */
6989 if (ifidx > 0 &&
6990 (drv->hostapd ||
6991 nlmode == NL80211_IFTYPE_AP_VLAN ||
6992 nlmode == NL80211_IFTYPE_WDS ||
6993 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006994 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006996 return 0;
6997}
6998
6999
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007000static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007001 enum wpa_driver_if_type type,
7002 const char *ifname)
7003{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004 struct wpa_driver_nl80211_data *drv = bss->drv;
7005 int ifindex = if_nametoindex(ifname);
7006
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007007 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7008 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007009 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07007010 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007011 else if (ifindex > 0 && !bss->added_if) {
7012 struct wpa_driver_nl80211_data *drv2;
7013 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007014 struct wpa_driver_nl80211_data, list) {
7015 del_ifidx(drv2, ifindex, IFIDX_ANY);
7016 del_ifidx(drv2, IFIDX_ANY, ifindex);
7017 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007018 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007019
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007020 if (type != WPA_IF_AP_BSS)
7021 return 0;
7022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007024 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7025 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7027 "interface %s from bridge %s: %s",
7028 bss->ifname, bss->brname, strerror(errno));
7029 }
7030 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007031 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007032 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7033 "bridge %s: %s",
7034 bss->brname, strerror(errno));
7035 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007037 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 struct i802_bss *tbss;
7039
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007040 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7041 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 if (tbss->next == bss) {
7043 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007044 /* Unsubscribe management frames */
7045 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007047 if (!bss->added_if)
7048 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049 os_free(bss);
7050 bss = NULL;
7051 break;
7052 }
7053 }
7054 if (bss)
7055 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7056 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007057 } else {
7058 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7059 nl80211_teardown_ap(bss);
7060 if (!bss->added_if && !drv->first_bss->next)
Paul Stewart092955c2017-02-06 09:13:09 -08007061 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007062 nl80211_destroy_bss(bss);
7063 if (!bss->added_if)
7064 i802_set_iface_flags(bss, 0);
7065 if (drv->first_bss->next) {
7066 drv->first_bss = drv->first_bss->next;
7067 drv->ctx = drv->first_bss->ctx;
7068 os_free(bss);
7069 } else {
7070 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007073
7074 return 0;
7075}
7076
7077
7078static int cookie_handler(struct nl_msg *msg, void *arg)
7079{
7080 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7081 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7082 u64 *cookie = arg;
7083 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7084 genlmsg_attrlen(gnlh, 0), NULL);
7085 if (tb[NL80211_ATTR_COOKIE])
7086 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7087 return NL_SKIP;
7088}
7089
7090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007091static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007092 unsigned int freq, unsigned int wait,
7093 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007094 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007095 int offchanok, const u16 *csa_offs,
7096 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007097{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007098 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007099 struct nl_msg *msg;
7100 u64 cookie;
7101 int ret = -1;
7102
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007103 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07007104 "no_ack=%d offchanok=%d",
7105 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007106 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007108 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7109 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7110 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7111 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7112 drv->test_use_roc_tx) &&
7113 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7114 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7115 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007116 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7117 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007118 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7119 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120
7121 cookie = 0;
7122 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7123 msg = NULL;
7124 if (ret) {
7125 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007126 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7127 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007128 } else {
7129 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7130 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7131 (long long unsigned int) cookie);
7132
7133 if (cookie_out)
7134 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007135
7136 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7137 wpa_printf(MSG_DEBUG,
7138 "nl80211: Drop oldest pending send action cookie 0x%llx",
7139 (long long unsigned int)
7140 drv->send_action_cookies[0]);
7141 os_memmove(&drv->send_action_cookies[0],
7142 &drv->send_action_cookies[1],
7143 (MAX_SEND_ACTION_COOKIES - 1) *
7144 sizeof(u64));
7145 drv->num_send_action_cookies--;
7146 }
7147 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7148 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007150
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007151fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007152 nlmsg_free(msg);
7153 return ret;
7154}
7155
7156
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007157static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7158 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007159 unsigned int wait_time,
7160 const u8 *dst, const u8 *src,
7161 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007162 const u8 *data, size_t data_len,
7163 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165 struct wpa_driver_nl80211_data *drv = bss->drv;
7166 int ret = -1;
7167 u8 *buf;
7168 struct ieee80211_hdr *hdr;
7169
7170 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007171 "freq=%u MHz wait=%d ms no_cck=%d)",
7172 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173
7174 buf = os_zalloc(24 + data_len);
7175 if (buf == NULL)
7176 return ret;
7177 os_memcpy(buf + 24, data, data_len);
7178 hdr = (struct ieee80211_hdr *) buf;
7179 hdr->frame_control =
7180 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7181 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7182 os_memcpy(hdr->addr2, src, ETH_ALEN);
7183 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7184
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007185 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7186 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7187 MAC2STR(src));
7188 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7189 } else {
7190 os_memset(bss->rand_addr, 0, ETH_ALEN);
7191 }
7192
Dmitry Shmidt56052862013-10-04 10:23:25 -07007193 if (is_ap_interface(drv->nlmode) &&
7194 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7195 (int) freq == bss->freq || drv->device_ap_sme ||
7196 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007197 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7198 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007199 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007200 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007201 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007203 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007204 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007205
7206 os_free(buf);
7207 return ret;
7208}
7209
7210
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007211static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007212{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007213 struct wpa_driver_nl80211_data *drv = bss->drv;
7214 struct nl_msg *msg;
7215 int ret;
7216
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08007217 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007218 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007219 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007220 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007221 nlmsg_free(msg);
7222 return;
7223 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224
7225 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226 if (ret)
7227 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7228 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007229}
7230
7231
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007232static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7233{
7234 struct i802_bss *bss = priv;
7235 struct wpa_driver_nl80211_data *drv = bss->drv;
7236 unsigned int i;
7237 u64 cookie;
7238
7239 /* Cancel the last pending TX cookie */
7240 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7241
7242 /*
7243 * Cancel the other pending TX cookies, if any. This is needed since
7244 * the driver may keep a list of all pending offchannel TX operations
7245 * and free up the radio only once they have expired or cancelled.
7246 */
7247 for (i = drv->num_send_action_cookies; i > 0; i--) {
7248 cookie = drv->send_action_cookies[i - 1];
7249 if (cookie != drv->send_action_cookie)
7250 nl80211_frame_wait_cancel(bss, cookie);
7251 }
7252 drv->num_send_action_cookies = 0;
7253}
7254
7255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007256static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7257 unsigned int duration)
7258{
7259 struct i802_bss *bss = priv;
7260 struct wpa_driver_nl80211_data *drv = bss->drv;
7261 struct nl_msg *msg;
7262 int ret;
7263 u64 cookie;
7264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007265 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7266 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7267 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7268 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007269 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007271
7272 cookie = 0;
7273 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7274 if (ret == 0) {
7275 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7276 "0x%llx for freq=%u MHz duration=%u",
7277 (long long unsigned int) cookie, freq, duration);
7278 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007279 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007280 return 0;
7281 }
7282 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7283 "(freq=%d duration=%u): %d (%s)",
7284 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285 return -1;
7286}
7287
7288
7289static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7290{
7291 struct i802_bss *bss = priv;
7292 struct wpa_driver_nl80211_data *drv = bss->drv;
7293 struct nl_msg *msg;
7294 int ret;
7295
7296 if (!drv->pending_remain_on_chan) {
7297 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7298 "to cancel");
7299 return -1;
7300 }
7301
7302 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7303 "0x%llx",
7304 (long long unsigned int) drv->remain_on_chan_cookie);
7305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007306 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7307 if (!msg ||
7308 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7309 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007310 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312
7313 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7314 if (ret == 0)
7315 return 0;
7316 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7317 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318 return -1;
7319}
7320
7321
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007322static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007324 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007326 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007328 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7329 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007330 /*
7331 * Do not disable Probe Request reporting that was
7332 * enabled in nl80211_setup_ap().
7333 */
7334 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7335 "Probe Request reporting nl_preq=%p while "
7336 "in AP mode", bss->nl_preq);
7337 } else if (bss->nl_preq) {
7338 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7339 "reporting nl_preq=%p", bss->nl_preq);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007340 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007341 }
7342 return 0;
7343 }
7344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007345 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007346 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007347 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007348 return 0;
7349 }
7350
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007351 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7352 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007353 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007354 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7355 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007357 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007358 (WLAN_FC_TYPE_MGMT << 2) |
7359 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007360 NULL, 0) < 0)
7361 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007362
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007363 nl80211_register_eloop_read(&bss->nl_preq,
7364 wpa_driver_nl80211_event_receive,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007365 bss->nl_cb, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007366
7367 return 0;
7368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007369 out_err:
7370 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 return -1;
7372}
7373
7374
7375static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7376 int ifindex, int disabled)
7377{
7378 struct nl_msg *msg;
7379 struct nlattr *bands, *band;
7380 int ret;
7381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007382 wpa_printf(MSG_DEBUG,
7383 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7384 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7385 "no NL80211_TXRATE_LEGACY constraint");
7386
7387 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7388 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389 if (!msg)
7390 return -1;
7391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007392 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7393 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007394 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395
7396 /*
7397 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7398 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7399 * rates. All 5 GHz rates are left enabled.
7400 */
7401 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007402 if (!band ||
7403 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7404 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7405 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406 nla_nest_end(msg, band);
7407
7408 nla_nest_end(msg, bands);
7409
7410 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007411 if (ret) {
7412 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7413 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007414 } else
7415 drv->disabled_11b_rates = disabled;
7416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007417 return ret;
7418
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007419fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420 nlmsg_free(msg);
7421 return -1;
7422}
7423
7424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007425static int wpa_driver_nl80211_deinit_ap(void *priv)
7426{
7427 struct i802_bss *bss = priv;
7428 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007429 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007430 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007431 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007432 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007433
7434 /*
7435 * If the P2P GO interface was dynamically added, then it is
7436 * possible that the interface change to station is not possible.
7437 */
7438 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7439 return 0;
7440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007441 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442}
7443
7444
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007445static int wpa_driver_nl80211_stop_ap(void *priv)
7446{
7447 struct i802_bss *bss = priv;
7448 struct wpa_driver_nl80211_data *drv = bss->drv;
7449 if (!is_ap_interface(drv->nlmode))
7450 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007451 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007452 bss->beacon_set = 0;
7453 return 0;
7454}
7455
7456
Dmitry Shmidt04949592012-07-19 12:16:46 -07007457static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7458{
7459 struct i802_bss *bss = priv;
7460 struct wpa_driver_nl80211_data *drv = bss->drv;
7461 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7462 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007463
7464 /*
7465 * If the P2P Client interface was dynamically added, then it is
7466 * possible that the interface change to station is not possible.
7467 */
7468 if (bss->if_dynamic)
7469 return 0;
7470
Dmitry Shmidt04949592012-07-19 12:16:46 -07007471 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7472}
7473
7474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007475static void wpa_driver_nl80211_resume(void *priv)
7476{
7477 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007478 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007479
7480 if (i802_set_iface_flags(bss, 1))
7481 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007482
7483 if (is_p2p_net_interface(nlmode))
7484 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007485}
7486
7487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007488static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7489{
7490 struct i802_bss *bss = priv;
7491 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007492 struct nl_msg *msg;
7493 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494
7495 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7496 "hysteresis=%d", threshold, hysteresis);
7497
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007498 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7499 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7500 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7501 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7502 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007503 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007504 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007505 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007506
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007507 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508}
7509
7510
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007511static int get_channel_width(struct nl_msg *msg, void *arg)
7512{
7513 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7514 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7515 struct wpa_signal_info *sig_change = arg;
7516
7517 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7518 genlmsg_attrlen(gnlh, 0), NULL);
7519
7520 sig_change->center_frq1 = -1;
7521 sig_change->center_frq2 = -1;
7522 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7523
7524 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7525 sig_change->chanwidth = convert2width(
7526 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7527 if (tb[NL80211_ATTR_CENTER_FREQ1])
7528 sig_change->center_frq1 =
7529 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7530 if (tb[NL80211_ATTR_CENTER_FREQ2])
7531 sig_change->center_frq2 =
7532 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7533 }
7534
7535 return NL_SKIP;
7536}
7537
7538
7539static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7540 struct wpa_signal_info *sig)
7541{
7542 struct nl_msg *msg;
7543
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007544 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007545 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007546}
7547
7548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7550{
7551 struct i802_bss *bss = priv;
7552 struct wpa_driver_nl80211_data *drv = bss->drv;
7553 int res;
7554
7555 os_memset(si, 0, sizeof(*si));
7556 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007557 if (res) {
7558 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7559 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7560 return res;
7561 si->current_signal = 0;
7562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007564 res = nl80211_get_channel_width(drv, si);
7565 if (res != 0)
7566 return res;
7567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007568 return nl80211_get_link_noise(drv, si);
7569}
7570
7571
7572static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7573 int encrypt)
7574{
7575 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007576 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007577 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007578}
7579
7580
7581static int nl80211_set_param(void *priv, const char *param)
7582{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007583 struct i802_bss *bss = priv;
7584 struct wpa_driver_nl80211_data *drv = bss->drv;
7585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007586 if (param == NULL)
7587 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007588 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007589
7590#ifdef CONFIG_P2P
7591 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7593 "interface");
7594 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7595 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7596 }
7597#endif /* CONFIG_P2P */
7598
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007599 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007600 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007601
7602 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007603 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007604 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007605 }
7606
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007607 if (os_strstr(param, "force_bss_selection=1"))
7608 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7609
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007610 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007611 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7612 drv->test_use_roc_tx = 1;
7613 }
7614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007615 return 0;
7616}
7617
7618
Dmitry Shmidte4663042016-04-04 10:07:49 -07007619static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007620{
7621 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007622 struct netlink_config *cfg;
7623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007624 global = os_zalloc(sizeof(*global));
7625 if (global == NULL)
7626 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007627 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007628 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007630 global->if_add_ifindex = -1;
7631
7632 cfg = os_zalloc(sizeof(*cfg));
7633 if (cfg == NULL)
7634 goto err;
7635
7636 cfg->ctx = global;
7637 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7638 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7639 global->netlink = netlink_init(cfg);
7640 if (global->netlink == NULL) {
7641 os_free(cfg);
7642 goto err;
7643 }
7644
7645 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7646 goto err;
7647
7648 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7649 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007650 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7651 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007652 goto err;
7653 }
7654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007655 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007656
7657err:
7658 nl80211_global_deinit(global);
7659 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660}
7661
7662
7663static void nl80211_global_deinit(void *priv)
7664{
7665 struct nl80211_global *global = priv;
7666 if (global == NULL)
7667 return;
7668 if (!dl_list_empty(&global->interfaces)) {
7669 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7670 "nl80211_global_deinit",
7671 dl_list_len(&global->interfaces));
7672 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007673
7674 if (global->netlink)
7675 netlink_deinit(global->netlink);
7676
7677 nl_destroy_handles(&global->nl);
7678
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007679 if (global->nl_event)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007680 nl80211_destroy_eloop_handle(&global->nl_event, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007681
7682 nl_cb_put(global->nl_cb);
7683
7684 if (global->ioctl_sock >= 0)
7685 close(global->ioctl_sock);
7686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007687 os_free(global);
7688}
7689
7690
7691static const char * nl80211_get_radio_name(void *priv)
7692{
7693 struct i802_bss *bss = priv;
7694 struct wpa_driver_nl80211_data *drv = bss->drv;
7695 return drv->phyname;
7696}
7697
7698
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007699static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7700 struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007701{
7702 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007703 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
Jouni Malinen75ecf522011-06-27 15:19:46 -07007704
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007705 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007706 (params->pmkid &&
7707 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7708 (params->bssid &&
7709 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7710 (params->ssid_len &&
7711 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7712 (params->fils_cache_id &&
7713 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7714 params->fils_cache_id)) ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07007715 (params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007716 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007717 nlmsg_free(msg);
7718 return -ENOBUFS;
7719 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007720
7721 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007722}
7723
7724
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007725static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007726{
7727 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007728 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007729
7730 if (params->bssid)
7731 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
7732 MAC2STR(params->bssid));
7733 else if (params->fils_cache_id && params->ssid_len) {
7734 wpa_printf(MSG_DEBUG,
7735 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
7736 params->fils_cache_id[0], params->fils_cache_id[1],
7737 wpa_ssid_txt(params->ssid, params->ssid_len));
7738 }
7739
Roshan Pius3a1667e2018-07-03 15:17:14 -07007740 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
7741 if (ret < 0) {
7742 wpa_printf(MSG_DEBUG,
7743 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7744 ret, strerror(-ret));
7745 }
7746
7747 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007748}
7749
7750
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007751static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007752{
7753 struct i802_bss *bss = priv;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007754 int ret;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007755
7756 if (params->bssid)
7757 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7758 MAC2STR(params->bssid));
7759 else if (params->fils_cache_id && params->ssid_len) {
7760 wpa_printf(MSG_DEBUG,
7761 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7762 params->fils_cache_id[0], params->fils_cache_id[1],
7763 wpa_ssid_txt(params->ssid, params->ssid_len));
7764 }
7765
Roshan Pius3a1667e2018-07-03 15:17:14 -07007766 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7767 if (ret < 0) {
7768 wpa_printf(MSG_DEBUG,
7769 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
7770 ret, strerror(-ret));
7771 }
7772
7773 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007774}
7775
7776
7777static int nl80211_flush_pmkid(void *priv)
7778{
7779 struct i802_bss *bss = priv;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007780 struct nl_msg *msg;
7781
Jouni Malinen75ecf522011-06-27 15:19:46 -07007782 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007783 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
7784 if (!msg)
7785 return -ENOBUFS;
7786 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007787}
7788
7789
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007790static void clean_survey_results(struct survey_results *survey_results)
7791{
7792 struct freq_survey *survey, *tmp;
7793
7794 if (dl_list_empty(&survey_results->survey_list))
7795 return;
7796
7797 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7798 struct freq_survey, list) {
7799 dl_list_del(&survey->list);
7800 os_free(survey);
7801 }
7802}
7803
7804
7805static void add_survey(struct nlattr **sinfo, u32 ifidx,
7806 struct dl_list *survey_list)
7807{
7808 struct freq_survey *survey;
7809
7810 survey = os_zalloc(sizeof(struct freq_survey));
7811 if (!survey)
7812 return;
7813
7814 survey->ifidx = ifidx;
7815 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7816 survey->filled = 0;
7817
7818 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7819 survey->nf = (int8_t)
7820 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7821 survey->filled |= SURVEY_HAS_NF;
7822 }
7823
7824 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7825 survey->channel_time =
7826 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7827 survey->filled |= SURVEY_HAS_CHAN_TIME;
7828 }
7829
7830 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7831 survey->channel_time_busy =
7832 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7833 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7834 }
7835
7836 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7837 survey->channel_time_rx =
7838 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7839 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7840 }
7841
7842 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7843 survey->channel_time_tx =
7844 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7845 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7846 }
7847
7848 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)",
7849 survey->freq,
7850 survey->nf,
7851 (unsigned long int) survey->channel_time,
7852 (unsigned long int) survey->channel_time_busy,
7853 (unsigned long int) survey->channel_time_tx,
7854 (unsigned long int) survey->channel_time_rx,
7855 survey->filled);
7856
7857 dl_list_add_tail(survey_list, &survey->list);
7858}
7859
7860
7861static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7862 unsigned int freq_filter)
7863{
7864 if (!freq_filter)
7865 return 1;
7866
7867 return freq_filter == surveyed_freq;
7868}
7869
7870
7871static int survey_handler(struct nl_msg *msg, void *arg)
7872{
7873 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7874 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7875 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7876 struct survey_results *survey_results;
7877 u32 surveyed_freq = 0;
7878 u32 ifidx;
7879
7880 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7881 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7882 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7883 };
7884
7885 survey_results = (struct survey_results *) arg;
7886
7887 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7888 genlmsg_attrlen(gnlh, 0), NULL);
7889
Dmitry Shmidt97672262014-02-03 13:02:54 -08007890 if (!tb[NL80211_ATTR_IFINDEX])
7891 return NL_SKIP;
7892
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007893 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7894
7895 if (!tb[NL80211_ATTR_SURVEY_INFO])
7896 return NL_SKIP;
7897
7898 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7899 tb[NL80211_ATTR_SURVEY_INFO],
7900 survey_policy))
7901 return NL_SKIP;
7902
7903 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7904 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7905 return NL_SKIP;
7906 }
7907
7908 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7909
7910 if (!check_survey_ok(sinfo, surveyed_freq,
7911 survey_results->freq_filter))
7912 return NL_SKIP;
7913
7914 if (survey_results->freq_filter &&
7915 survey_results->freq_filter != surveyed_freq) {
7916 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7917 surveyed_freq);
7918 return NL_SKIP;
7919 }
7920
7921 add_survey(sinfo, ifidx, &survey_results->survey_list);
7922
7923 return NL_SKIP;
7924}
7925
7926
7927static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7928{
7929 struct i802_bss *bss = priv;
7930 struct wpa_driver_nl80211_data *drv = bss->drv;
7931 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007932 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007933 union wpa_event_data data;
7934 struct survey_results *survey_results;
7935
7936 os_memset(&data, 0, sizeof(data));
7937 survey_results = &data.survey_results;
7938
7939 dl_list_init(&survey_results->survey_list);
7940
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007941 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007942 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007943 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007944
7945 if (freq)
7946 data.survey_results.freq_filter = freq;
7947
7948 do {
7949 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7950 err = send_and_recv_msgs(drv, msg, survey_handler,
7951 survey_results);
7952 } while (err > 0);
7953
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007954 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007955 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007956 else
7957 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007958
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007959 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007960 return err;
7961}
7962
7963
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007964static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7965 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007966 const u8 *replay_ctr)
7967{
7968 struct i802_bss *bss = priv;
7969 struct wpa_driver_nl80211_data *drv = bss->drv;
7970 struct nlattr *replay_nested;
7971 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007972 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007973
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007974 if (!drv->set_rekey_offload)
7975 return;
7976
7977 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007978 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7979 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007980 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007981 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007982 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7983 replay_ctr)) {
7984 nl80211_nlmsg_clear(msg);
7985 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007987 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007988
7989 nla_nest_end(msg, replay_nested);
7990
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007991 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7992 if (ret == -EOPNOTSUPP) {
7993 wpa_printf(MSG_DEBUG,
7994 "nl80211: Driver does not support rekey offload");
7995 drv->set_rekey_offload = 0;
7996 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007997}
7998
7999
8000static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8001 const u8 *addr, int qos)
8002{
8003 /* send data frame to poll STA and check whether
8004 * this frame is ACKed */
8005 struct {
8006 struct ieee80211_hdr hdr;
8007 u16 qos_ctl;
8008 } STRUCT_PACKED nulldata;
8009 size_t size;
8010
8011 /* Send data frame to poll STA and check whether this frame is ACKed */
8012
8013 os_memset(&nulldata, 0, sizeof(nulldata));
8014
8015 if (qos) {
8016 nulldata.hdr.frame_control =
8017 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8018 WLAN_FC_STYPE_QOS_NULL);
8019 size = sizeof(nulldata);
8020 } else {
8021 nulldata.hdr.frame_control =
8022 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8023 WLAN_FC_STYPE_NULLFUNC);
8024 size = sizeof(struct ieee80211_hdr);
8025 }
8026
8027 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8028 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8029 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8030 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8031
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008032 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008033 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008034 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8035 "send poll frame");
8036}
8037
8038static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8039 int qos)
8040{
8041 struct i802_bss *bss = priv;
8042 struct wpa_driver_nl80211_data *drv = bss->drv;
8043 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008044 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008045
8046 if (!drv->poll_command_supported) {
8047 nl80211_send_null_frame(bss, own_addr, addr, qos);
8048 return;
8049 }
8050
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008051 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8052 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8053 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008054 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008055 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008056
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008057 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8058 if (ret < 0) {
8059 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8060 MACSTR " failed: ret=%d (%s)",
8061 MAC2STR(addr), ret, strerror(-ret));
8062 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008063}
8064
8065
8066static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8067{
8068 struct nl_msg *msg;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008069 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008070
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008071 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8072 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8073 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8074 nlmsg_free(msg);
8075 return -ENOBUFS;
8076 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07008077
8078 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8079 if (ret < 0) {
8080 wpa_printf(MSG_DEBUG,
8081 "nl80211: Setting PS state %s failed: %d (%s)",
8082 enabled ? "enabled" : "disabled",
8083 ret, strerror(-ret));
8084 }
8085 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008086}
8087
8088
8089static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8090 int ctwindow)
8091{
8092 struct i802_bss *bss = priv;
8093
8094 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8095 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8096
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008097 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008098#ifdef ANDROID_P2P
8099 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008100#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008101 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08008102#endif /* ANDROID_P2P */
8103 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008104
8105 if (legacy_ps == -1)
8106 return 0;
8107 if (legacy_ps != 0 && legacy_ps != 1)
8108 return -1; /* Not yet supported */
8109
8110 return nl80211_set_power_save(bss, legacy_ps);
8111}
8112
8113
Dmitry Shmidt051af732013-10-22 13:52:46 -07008114static int nl80211_start_radar_detection(void *priv,
8115 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008116{
8117 struct i802_bss *bss = priv;
8118 struct wpa_driver_nl80211_data *drv = bss->drv;
8119 struct nl_msg *msg;
8120 int ret;
8121
Dmitry Shmidt051af732013-10-22 13:52:46 -07008122 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)",
8123 freq->freq, freq->ht_enabled, freq->vht_enabled,
8124 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8125
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008126 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8127 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8128 "detection");
8129 return -1;
8130 }
8131
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008132 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8133 nl80211_put_freq_params(msg, freq) < 0) {
8134 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008135 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008136 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008137
8138 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8139 if (ret == 0)
8140 return 0;
8141 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8142 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008143 return -1;
8144}
8145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008146#ifdef CONFIG_TDLS
8147
8148static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8149 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008150 u32 peer_capab, int initiator, const u8 *buf,
8151 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008152{
8153 struct i802_bss *bss = priv;
8154 struct wpa_driver_nl80211_data *drv = bss->drv;
8155 struct nl_msg *msg;
8156
8157 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8158 return -EOPNOTSUPP;
8159
8160 if (!dst)
8161 return -EINVAL;
8162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008163 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8164 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8165 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8166 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8167 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8168 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008169 if (peer_capab) {
8170 /*
8171 * The internal enum tdls_peer_capability definition is
8172 * currently identical with the nl80211 enum
8173 * nl80211_tdls_peer_capability, so no conversion is needed
8174 * here.
8175 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008176 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8177 peer_capab))
8178 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008179 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008180 if ((initiator &&
8181 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8182 nla_put(msg, NL80211_ATTR_IE, len, buf))
8183 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008184
8185 return send_and_recv_msgs(drv, msg, NULL, NULL);
8186
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008187fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008188 nlmsg_free(msg);
8189 return -ENOBUFS;
8190}
8191
8192
8193static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8194{
8195 struct i802_bss *bss = priv;
8196 struct wpa_driver_nl80211_data *drv = bss->drv;
8197 struct nl_msg *msg;
8198 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -08008199 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008200
8201 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8202 return -EOPNOTSUPP;
8203
8204 switch (oper) {
8205 case TDLS_DISCOVERY_REQ:
8206 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8207 break;
8208 case TDLS_SETUP:
8209 nl80211_oper = NL80211_TDLS_SETUP;
8210 break;
8211 case TDLS_TEARDOWN:
8212 nl80211_oper = NL80211_TDLS_TEARDOWN;
8213 break;
8214 case TDLS_ENABLE_LINK:
8215 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8216 break;
8217 case TDLS_DISABLE_LINK:
8218 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8219 break;
8220 case TDLS_ENABLE:
8221 return 0;
8222 case TDLS_DISABLE:
8223 return 0;
8224 default:
8225 return -EINVAL;
8226 }
8227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008228 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8229 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8230 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8231 nlmsg_free(msg);
8232 return -ENOBUFS;
8233 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008234
Paul Stewart092955c2017-02-06 09:13:09 -08008235 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8236 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8237 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8238 strerror(-res));
8239 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008240}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008241
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008242
8243static int
8244nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8245 const struct hostapd_freq_params *params)
8246{
8247 struct i802_bss *bss = priv;
8248 struct wpa_driver_nl80211_data *drv = bss->drv;
8249 struct nl_msg *msg;
8250 int ret = -ENOBUFS;
8251
8252 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8253 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8254 return -EOPNOTSUPP;
8255
8256 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8257 " oper_class=%u freq=%u",
8258 MAC2STR(addr), oper_class, params->freq);
8259 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8260 if (!msg ||
8261 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8262 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8263 (ret = nl80211_put_freq_params(msg, params))) {
8264 nlmsg_free(msg);
8265 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8266 return ret;
8267 }
8268
8269 return send_and_recv_msgs(drv, msg, NULL, NULL);
8270}
8271
8272
8273static int
8274nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8275{
8276 struct i802_bss *bss = priv;
8277 struct wpa_driver_nl80211_data *drv = bss->drv;
8278 struct nl_msg *msg;
8279
8280 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8281 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8282 return -EOPNOTSUPP;
8283
8284 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8285 MAC2STR(addr));
8286 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8287 if (!msg ||
8288 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8289 nlmsg_free(msg);
8290 wpa_printf(MSG_DEBUG,
8291 "nl80211: Could not build TDLS cancel chan switch");
8292 return -ENOBUFS;
8293 }
8294
8295 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008296}
8297
8298#endif /* CONFIG TDLS */
8299
8300
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008301static int driver_nl80211_set_key(const char *ifname, void *priv,
8302 enum wpa_alg alg, const u8 *addr,
8303 int key_idx, int set_tx,
8304 const u8 *seq, size_t seq_len,
8305 const u8 *key, size_t key_len)
8306{
8307 struct i802_bss *bss = priv;
8308 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8309 set_tx, seq, seq_len, key, key_len);
8310}
8311
8312
8313static int driver_nl80211_scan2(void *priv,
8314 struct wpa_driver_scan_params *params)
8315{
8316 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008317#ifdef CONFIG_DRIVER_NL80211_QCA
8318 struct wpa_driver_nl80211_data *drv = bss->drv;
8319
8320 /*
8321 * Do a vendor specific scan if possible. If only_new_results is
8322 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8323 * cannot be achieved through a vendor scan. The below condition may
8324 * need to be modified if new scan flags are added in the future whose
8325 * functionality can only be achieved through a normal scan.
8326 */
8327 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8328 return wpa_driver_nl80211_vendor_scan(bss, params);
8329#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008330 return wpa_driver_nl80211_scan(bss, params);
8331}
8332
8333
8334static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8335 int reason_code)
8336{
8337 struct i802_bss *bss = priv;
8338 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8339}
8340
8341
8342static int driver_nl80211_authenticate(void *priv,
8343 struct wpa_driver_auth_params *params)
8344{
8345 struct i802_bss *bss = priv;
8346 return wpa_driver_nl80211_authenticate(bss, params);
8347}
8348
8349
8350static void driver_nl80211_deinit(void *priv)
8351{
8352 struct i802_bss *bss = priv;
8353 wpa_driver_nl80211_deinit(bss);
8354}
8355
8356
8357static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8358 const char *ifname)
8359{
8360 struct i802_bss *bss = priv;
8361 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8362}
8363
8364
8365static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008366 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008367 unsigned int freq,
8368 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008369{
8370 struct i802_bss *bss = priv;
8371 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008372 freq, 0, 0, 0, csa_offs,
8373 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008374}
8375
8376
8377static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8378{
8379 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008380 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008381}
8382
8383
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008384static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8385 const char *ifname, int vlan_id)
8386{
8387 struct i802_bss *bss = priv;
8388 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8389}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008390
8391
8392static int driver_nl80211_read_sta_data(void *priv,
8393 struct hostap_sta_driver_data *data,
8394 const u8 *addr)
8395{
8396 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008397
8398 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008399 return i802_read_sta_data(bss, data, addr);
8400}
8401
8402
8403static int driver_nl80211_send_action(void *priv, unsigned int freq,
8404 unsigned int wait_time,
8405 const u8 *dst, const u8 *src,
8406 const u8 *bssid,
8407 const u8 *data, size_t data_len,
8408 int no_cck)
8409{
8410 struct i802_bss *bss = priv;
8411 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8412 bssid, data, data_len, no_cck);
8413}
8414
8415
8416static int driver_nl80211_probe_req_report(void *priv, int report)
8417{
8418 struct i802_bss *bss = priv;
8419 return wpa_driver_nl80211_probe_req_report(bss, report);
8420}
8421
8422
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008423static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8424 const u8 *ies, size_t ies_len)
8425{
8426 int ret;
8427 struct nl_msg *msg;
8428 struct i802_bss *bss = priv;
8429 struct wpa_driver_nl80211_data *drv = bss->drv;
8430 u16 mdid = WPA_GET_LE16(md);
8431
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008432 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008433 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8434 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8435 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8436 nlmsg_free(msg);
8437 return -ENOBUFS;
8438 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008439
8440 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8441 if (ret) {
8442 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8443 "err=%d (%s)", ret, strerror(-ret));
8444 }
8445
8446 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008447}
8448
8449
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07008450static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008451{
8452 struct i802_bss *bss = priv;
8453 struct wpa_driver_nl80211_data *drv = bss->drv;
8454
8455 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8456 return NULL;
8457
8458 return bss->addr;
8459}
8460
8461
Dmitry Shmidt56052862013-10-04 10:23:25 -07008462static const char * scan_state_str(enum scan_states scan_state)
8463{
8464 switch (scan_state) {
8465 case NO_SCAN:
8466 return "NO_SCAN";
8467 case SCAN_REQUESTED:
8468 return "SCAN_REQUESTED";
8469 case SCAN_STARTED:
8470 return "SCAN_STARTED";
8471 case SCAN_COMPLETED:
8472 return "SCAN_COMPLETED";
8473 case SCAN_ABORTED:
8474 return "SCAN_ABORTED";
8475 case SCHED_SCAN_STARTED:
8476 return "SCHED_SCAN_STARTED";
8477 case SCHED_SCAN_STOPPED:
8478 return "SCHED_SCAN_STOPPED";
8479 case SCHED_SCAN_RESULTS:
8480 return "SCHED_SCAN_RESULTS";
8481 }
8482
8483 return "??";
8484}
8485
8486
8487static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8488{
8489 struct i802_bss *bss = priv;
8490 struct wpa_driver_nl80211_data *drv = bss->drv;
8491 int res;
8492 char *pos, *end;
8493
8494 pos = buf;
8495 end = buf + buflen;
8496
8497 res = os_snprintf(pos, end - pos,
8498 "ifindex=%d\n"
8499 "ifname=%s\n"
8500 "brname=%s\n"
8501 "addr=" MACSTR "\n"
8502 "freq=%d\n"
8503 "%s%s%s%s%s",
8504 bss->ifindex,
8505 bss->ifname,
8506 bss->brname,
8507 MAC2STR(bss->addr),
8508 bss->freq,
8509 bss->beacon_set ? "beacon_set=1\n" : "",
8510 bss->added_if_into_bridge ?
8511 "added_if_into_bridge=1\n" : "",
8512 bss->added_bridge ? "added_bridge=1\n" : "",
8513 bss->in_deinit ? "in_deinit=1\n" : "",
8514 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008515 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008516 return pos - buf;
8517 pos += res;
8518
8519 if (bss->wdev_id_set) {
8520 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8521 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008522 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008523 return pos - buf;
8524 pos += res;
8525 }
8526
8527 res = os_snprintf(pos, end - pos,
8528 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008529 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008530 "drv_ifindex=%d\n"
8531 "operstate=%d\n"
8532 "scan_state=%s\n"
8533 "auth_bssid=" MACSTR "\n"
8534 "auth_attempt_bssid=" MACSTR "\n"
8535 "bssid=" MACSTR "\n"
8536 "prev_bssid=" MACSTR "\n"
8537 "associated=%d\n"
8538 "assoc_freq=%u\n"
8539 "monitor_sock=%d\n"
8540 "monitor_ifidx=%d\n"
8541 "monitor_refcount=%d\n"
8542 "last_mgmt_freq=%u\n"
8543 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008544 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008545 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008546 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07008547 drv->ifindex,
8548 drv->operstate,
8549 scan_state_str(drv->scan_state),
8550 MAC2STR(drv->auth_bssid),
8551 MAC2STR(drv->auth_attempt_bssid),
8552 MAC2STR(drv->bssid),
8553 MAC2STR(drv->prev_bssid),
8554 drv->associated,
8555 drv->assoc_freq,
8556 drv->monitor_sock,
8557 drv->monitor_ifidx,
8558 drv->monitor_refcount,
8559 drv->last_mgmt_freq,
8560 drv->eapol_tx_sock,
8561 drv->ignore_if_down_event ?
8562 "ignore_if_down_event=1\n" : "",
8563 drv->scan_complete_events ?
8564 "scan_complete_events=1\n" : "",
8565 drv->disabled_11b_rates ?
8566 "disabled_11b_rates=1\n" : "",
8567 drv->pending_remain_on_chan ?
8568 "pending_remain_on_chan=1\n" : "",
8569 drv->in_interface_list ? "in_interface_list=1\n" : "",
8570 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8571 drv->poll_command_supported ?
8572 "poll_command_supported=1\n" : "",
8573 drv->data_tx_status ? "data_tx_status=1\n" : "",
8574 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8575 drv->retry_auth ? "retry_auth=1\n" : "",
8576 drv->use_monitor ? "use_monitor=1\n" : "",
8577 drv->ignore_next_local_disconnect ?
8578 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008579 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008580 "ignore_next_local_deauth=1\n" : "");
8581 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008582 return pos - buf;
8583 pos += res;
8584
8585 if (drv->has_capability) {
8586 res = os_snprintf(pos, end - pos,
8587 "capa.key_mgmt=0x%x\n"
8588 "capa.enc=0x%x\n"
8589 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008590 "capa.flags=0x%llx\n"
8591 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008592 "capa.max_scan_ssids=%d\n"
8593 "capa.max_sched_scan_ssids=%d\n"
8594 "capa.sched_scan_supported=%d\n"
8595 "capa.max_match_sets=%d\n"
8596 "capa.max_remain_on_chan=%u\n"
8597 "capa.max_stations=%u\n"
8598 "capa.probe_resp_offloads=0x%x\n"
8599 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008600 "capa.num_multichan_concurrent=%u\n"
8601 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008602 "capa.mac_addr_rand_scan_supported=%d\n"
8603 "capa.conc_capab=%u\n"
8604 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008605 "capa.max_conc_chan_5_0=%u\n"
8606 "capa.max_sched_scan_plans=%u\n"
8607 "capa.max_sched_scan_plan_interval=%u\n"
8608 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008609 drv->capa.key_mgmt,
8610 drv->capa.enc,
8611 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008612 (unsigned long long) drv->capa.flags,
8613 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008614 drv->capa.max_scan_ssids,
8615 drv->capa.max_sched_scan_ssids,
8616 drv->capa.sched_scan_supported,
8617 drv->capa.max_match_sets,
8618 drv->capa.max_remain_on_chan,
8619 drv->capa.max_stations,
8620 drv->capa.probe_resp_offloads,
8621 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008622 drv->capa.num_multichan_concurrent,
8623 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008624 drv->capa.mac_addr_rand_scan_supported,
8625 drv->capa.conc_capab,
8626 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008627 drv->capa.max_conc_chan_5_0,
8628 drv->capa.max_sched_scan_plans,
8629 drv->capa.max_sched_scan_plan_interval,
8630 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008631 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008632 return pos - buf;
8633 pos += res;
8634 }
8635
8636 return pos - buf;
8637}
8638
8639
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008640static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8641{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008642 if ((settings->head &&
8643 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8644 settings->head_len, settings->head)) ||
8645 (settings->tail &&
8646 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8647 settings->tail_len, settings->tail)) ||
8648 (settings->beacon_ies &&
8649 nla_put(msg, NL80211_ATTR_IE,
8650 settings->beacon_ies_len, settings->beacon_ies)) ||
8651 (settings->proberesp_ies &&
8652 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8653 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8654 (settings->assocresp_ies &&
8655 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8656 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8657 (settings->probe_resp &&
8658 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8659 settings->probe_resp_len, settings->probe_resp)))
8660 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008661
8662 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008663}
8664
8665
8666static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8667{
8668 struct nl_msg *msg;
8669 struct i802_bss *bss = priv;
8670 struct wpa_driver_nl80211_data *drv = bss->drv;
8671 struct nlattr *beacon_csa;
8672 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008673 int csa_off_len = 0;
8674 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008675
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008676 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 -08008677 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008678 settings->freq_params.freq, settings->freq_params.bandwidth,
8679 settings->freq_params.center_freq1,
8680 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008681
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008682 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008683 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8684 return -EOPNOTSUPP;
8685 }
8686
Roshan Pius3a1667e2018-07-03 15:17:14 -07008687 if (drv->nlmode != NL80211_IFTYPE_AP &&
8688 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
8689 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008690 return -EOPNOTSUPP;
8691
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008692 /*
8693 * Remove empty counters, assuming Probe Response and Beacon frame
8694 * counters match. This implementation assumes that there are only two
8695 * counters.
8696 */
8697 if (settings->counter_offset_beacon[0] &&
8698 !settings->counter_offset_beacon[1]) {
8699 csa_off_len = 1;
8700 } else if (settings->counter_offset_beacon[1] &&
8701 !settings->counter_offset_beacon[0]) {
8702 csa_off_len = 1;
8703 settings->counter_offset_beacon[0] =
8704 settings->counter_offset_beacon[1];
8705 settings->counter_offset_presp[0] =
8706 settings->counter_offset_presp[1];
8707 } else if (settings->counter_offset_beacon[1] &&
8708 settings->counter_offset_beacon[0]) {
8709 csa_off_len = 2;
8710 } else {
8711 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8712 return -EINVAL;
8713 }
8714
8715 /* Check CSA counters validity */
8716 if (drv->capa.max_csa_counters &&
8717 csa_off_len > drv->capa.max_csa_counters) {
8718 wpa_printf(MSG_ERROR,
8719 "nl80211: Too many CSA counters provided");
8720 return -EINVAL;
8721 }
8722
8723 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008724 return -EINVAL;
8725
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008726 for (i = 0; i < csa_off_len; i++) {
8727 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8728 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8729
8730 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8731 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8732 settings->cs_count))
8733 return -EINVAL;
8734
8735 if (settings->beacon_csa.probe_resp &&
8736 ((settings->beacon_csa.probe_resp_len <=
8737 csa_c_off_presp) ||
8738 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8739 settings->cs_count)))
8740 return -EINVAL;
8741 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008742
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008743 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8744 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8745 settings->cs_count) ||
8746 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8747 (settings->block_tx &&
8748 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008749 goto error;
8750
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008751 /* beacon_after params */
8752 ret = set_beacon_data(msg, &settings->beacon_after);
8753 if (ret)
8754 goto error;
8755
8756 /* beacon_csa params */
8757 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8758 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008759 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008760
8761 ret = set_beacon_data(msg, &settings->beacon_csa);
8762 if (ret)
8763 goto error;
8764
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008765 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8766 csa_off_len * sizeof(u16),
8767 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008768 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008769 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8770 csa_off_len * sizeof(u16),
8771 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008772 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008773
8774 nla_nest_end(msg, beacon_csa);
8775 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8776 if (ret) {
8777 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8778 ret, strerror(-ret));
8779 }
8780 return ret;
8781
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008782fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008783 ret = -ENOBUFS;
8784error:
8785 nlmsg_free(msg);
8786 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8787 return ret;
8788}
8789
8790
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008791static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8792 u8 user_priority, u16 admitted_time)
8793{
8794 struct i802_bss *bss = priv;
8795 struct wpa_driver_nl80211_data *drv = bss->drv;
8796 struct nl_msg *msg;
8797 int ret;
8798
8799 wpa_printf(MSG_DEBUG,
8800 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8801 tsid, admitted_time, user_priority);
8802
8803 if (!is_sta_interface(drv->nlmode))
8804 return -ENOTSUP;
8805
8806 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8807 if (!msg ||
8808 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8809 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8810 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8811 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8812 nlmsg_free(msg);
8813 return -ENOBUFS;
8814 }
8815
8816 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8817 if (ret)
8818 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8819 ret, strerror(-ret));
8820 return ret;
8821}
8822
8823
8824static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8825{
8826 struct i802_bss *bss = priv;
8827 struct wpa_driver_nl80211_data *drv = bss->drv;
8828 struct nl_msg *msg;
8829 int ret;
8830
8831 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8832
8833 if (!is_sta_interface(drv->nlmode))
8834 return -ENOTSUP;
8835
8836 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8837 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8838 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8839 nlmsg_free(msg);
8840 return -ENOBUFS;
8841 }
8842
8843 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8844 if (ret)
8845 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8846 ret, strerror(-ret));
8847 return ret;
8848}
8849
8850
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008851#ifdef CONFIG_TESTING_OPTIONS
8852static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8853{
8854 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8855 struct wpabuf *buf = arg;
8856
8857 if (!buf)
8858 return NL_SKIP;
8859
8860 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8861 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8862 return NL_SKIP;
8863 }
8864
8865 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8866 genlmsg_attrlen(gnlh, 0));
8867
8868 return NL_SKIP;
8869}
8870#endif /* CONFIG_TESTING_OPTIONS */
8871
8872
8873static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8874{
8875 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8876 struct nlattr *nl_vendor_reply, *nl;
8877 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8878 struct wpabuf *buf = arg;
8879 int rem;
8880
8881 if (!buf)
8882 return NL_SKIP;
8883
8884 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8885 genlmsg_attrlen(gnlh, 0), NULL);
8886 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8887
8888 if (!nl_vendor_reply)
8889 return NL_SKIP;
8890
8891 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8892 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8893 return NL_SKIP;
8894 }
8895
8896 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8897 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8898 }
8899
8900 return NL_SKIP;
8901}
8902
8903
8904static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8905 unsigned int subcmd, const u8 *data,
8906 size_t data_len, struct wpabuf *buf)
8907{
8908 struct i802_bss *bss = priv;
8909 struct wpa_driver_nl80211_data *drv = bss->drv;
8910 struct nl_msg *msg;
8911 int ret;
8912
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008913#ifdef CONFIG_TESTING_OPTIONS
8914 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008915 msg = nlmsg_alloc();
8916 if (!msg)
8917 return -ENOMEM;
8918
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008919 nl80211_cmd(drv, msg, 0, subcmd);
8920 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8921 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008922 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008923 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8924 if (ret)
8925 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8926 ret);
8927 return ret;
8928 }
8929#endif /* CONFIG_TESTING_OPTIONS */
8930
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008931 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8932 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8933 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8934 (data &&
8935 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8936 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008937
8938 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8939 if (ret)
8940 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8941 ret);
8942 return ret;
8943
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008944fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008945 nlmsg_free(msg);
8946 return -ENOBUFS;
8947}
8948
8949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008950static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8951 u8 qos_map_set_len)
8952{
8953 struct i802_bss *bss = priv;
8954 struct wpa_driver_nl80211_data *drv = bss->drv;
8955 struct nl_msg *msg;
8956 int ret;
8957
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008958 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8959 qos_map_set, qos_map_set_len);
8960
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008961 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8962 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8963 nlmsg_free(msg);
8964 return -ENOBUFS;
8965 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008966
8967 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8968 if (ret)
8969 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8970
8971 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008972}
8973
8974
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008975static int nl80211_set_wowlan(void *priv,
8976 const struct wowlan_triggers *triggers)
8977{
8978 struct i802_bss *bss = priv;
8979 struct wpa_driver_nl80211_data *drv = bss->drv;
8980 struct nl_msg *msg;
8981 struct nlattr *wowlan_triggers;
8982 int ret;
8983
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008984 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8985
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008986 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008987 !(wowlan_triggers = nla_nest_start(msg,
8988 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8989 (triggers->any &&
8990 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8991 (triggers->disconnect &&
8992 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8993 (triggers->magic_pkt &&
8994 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8995 (triggers->gtk_rekey_failure &&
8996 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8997 (triggers->eap_identity_req &&
8998 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8999 (triggers->four_way_handshake &&
9000 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9001 (triggers->rfkill_release &&
9002 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9003 nlmsg_free(msg);
9004 return -ENOBUFS;
9005 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009006
9007 nla_nest_end(msg, wowlan_triggers);
9008
9009 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9010 if (ret)
9011 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9012
9013 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009014}
9015
9016
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009017#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009018static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9019{
9020 struct i802_bss *bss = priv;
9021 struct wpa_driver_nl80211_data *drv = bss->drv;
9022 struct nl_msg *msg;
9023 struct nlattr *params;
9024
9025 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9026
9027 if (!drv->roaming_vendor_cmd_avail) {
9028 wpa_printf(MSG_DEBUG,
9029 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9030 return -1;
9031 }
9032
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009033 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9034 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9035 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9036 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9037 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9038 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9039 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9040 QCA_ROAMING_NOT_ALLOWED) ||
9041 (bssid &&
9042 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9043 nlmsg_free(msg);
9044 return -1;
9045 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009046 nla_nest_end(msg, params);
9047
9048 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009049}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009050
9051
Roshan Pius3a1667e2018-07-03 15:17:14 -07009052static int nl80211_disable_fils(void *priv, int disable)
9053{
9054 struct i802_bss *bss = priv;
9055 struct wpa_driver_nl80211_data *drv = bss->drv;
9056 struct nl_msg *msg;
9057 struct nlattr *params;
9058
9059 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9060
9061 if (!drv->set_wifi_conf_vendor_cmd_avail)
9062 return -1;
9063
9064 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9065 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9066 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9067 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9068 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9069 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9070 disable)) {
9071 nlmsg_free(msg);
9072 return -1;
9073 }
9074 nla_nest_end(msg, params);
9075
9076 return send_and_recv_msgs(drv, msg, NULL, NULL);
9077}
9078
9079
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009080/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9081#define WPA_SUPPLICANT_CLIENT_ID 1
9082
9083static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9084 const u8 *bssid)
9085{
9086 struct i802_bss *bss = priv;
9087 struct wpa_driver_nl80211_data *drv = bss->drv;
9088 struct nl_msg *msg;
9089 struct nlattr *params, *nlbssids, *attr;
9090 unsigned int i;
9091
9092 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9093 num_bssid);
9094
9095 if (!drv->roam_vendor_cmd_avail)
9096 return -1;
9097
9098 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9099 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9100 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9101 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9102 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9103 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9104 QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9105 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9106 WPA_SUPPLICANT_CLIENT_ID) ||
9107 nla_put_u32(msg,
9108 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9109 num_bssid))
9110 goto fail;
9111
9112 nlbssids = nla_nest_start(
9113 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9114 if (!nlbssids)
9115 goto fail;
9116
9117 for (i = 0; i < num_bssid; i++) {
9118 attr = nla_nest_start(msg, i);
9119 if (!attr)
9120 goto fail;
9121 if (nla_put(msg,
9122 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9123 ETH_ALEN, &bssid[i * ETH_ALEN]))
9124 goto fail;
9125 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9126 MAC2STR(&bssid[i * ETH_ALEN]));
9127 nla_nest_end(msg, attr);
9128 }
9129 nla_nest_end(msg, nlbssids);
9130 nla_nest_end(msg, params);
9131
9132 return send_and_recv_msgs(drv, msg, NULL, NULL);
9133
9134fail:
9135 nlmsg_free(msg);
9136 return -1;
9137}
9138
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009139#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009140
9141
9142static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9143{
9144 struct i802_bss *bss = priv;
9145 struct wpa_driver_nl80211_data *drv = bss->drv;
9146 int new_addr = addr != NULL;
9147
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009148 if (TEST_FAIL())
9149 return -1;
9150
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009151 if (!addr)
9152 addr = drv->perm_addr;
9153
9154 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9155 return -1;
9156
9157 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9158 {
9159 wpa_printf(MSG_DEBUG,
9160 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9161 bss->ifname, MAC2STR(addr));
9162 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9163 1) < 0) {
9164 wpa_printf(MSG_DEBUG,
9165 "nl80211: Could not restore interface UP after failed set_mac_addr");
9166 }
9167 return -1;
9168 }
9169
9170 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9171 bss->ifname, MAC2STR(addr));
9172 drv->addr_changed = new_addr;
9173 os_memcpy(bss->addr, addr, ETH_ALEN);
9174
9175 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9176 {
9177 wpa_printf(MSG_DEBUG,
9178 "nl80211: Could not restore interface UP after set_mac_addr");
9179 }
9180
9181 return 0;
9182}
9183
9184
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009185#ifdef CONFIG_MESH
9186
9187static int wpa_driver_nl80211_init_mesh(void *priv)
9188{
9189 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9190 wpa_printf(MSG_INFO,
9191 "nl80211: Failed to set interface into mesh mode");
9192 return -1;
9193 }
9194 return 0;
9195}
9196
9197
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009198static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9199 size_t mesh_id_len)
9200{
9201 if (mesh_id) {
9202 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
9203 mesh_id, mesh_id_len);
9204 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9205 }
9206
9207 return 0;
9208}
9209
9210
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009211static int nl80211_put_mesh_config(struct nl_msg *msg,
9212 struct wpa_driver_mesh_bss_params *params)
9213{
9214 struct nlattr *container;
9215
9216 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9217 if (!container)
9218 return -1;
9219
9220 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009221 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9222 params->auto_plinks)) ||
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009223 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9224 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009225 params->max_peer_links)) ||
9226 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9227 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9228 params->rssi_threshold)))
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009229 return -1;
9230
9231 /*
9232 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9233 * the timer could disconnect stations even in that case.
9234 */
9235 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9236 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
9237 params->peer_link_timeout)) {
9238 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9239 return -1;
9240 }
9241
9242 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9243 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9244 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9245 return -1;
9246 }
9247
9248 nla_nest_end(msg, container);
9249
9250 return 0;
9251}
9252
9253
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009254static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009255 struct wpa_driver_mesh_join_params *params)
9256{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009257 struct wpa_driver_nl80211_data *drv = bss->drv;
9258 struct nl_msg *msg;
9259 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08009260 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009261
9262 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9263 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08009264 if (!msg ||
9265 nl80211_put_freq_params(msg, &params->freq) ||
9266 nl80211_put_basic_rates(msg, params->basic_rates) ||
9267 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009268 nl80211_put_beacon_int(msg, params->beacon_int) ||
9269 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009270 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009271
9272 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
9273
9274 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9275 if (!container)
9276 goto fail;
9277
9278 if (params->ies) {
9279 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
9280 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9281 params->ies))
9282 goto fail;
9283 }
9284 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9285 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
9286 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9287 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9288 goto fail;
9289 }
9290 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9291 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9292 goto fail;
9293 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9294 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9295 goto fail;
9296 nla_nest_end(msg, container);
9297
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07009298 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9299 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9300 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
9301 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009302 goto fail;
9303
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009304 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9305 msg = NULL;
9306 if (ret) {
9307 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9308 ret, strerror(-ret));
9309 goto fail;
9310 }
9311 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009312 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009313 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9314
9315fail:
9316 nlmsg_free(msg);
9317 return ret;
9318}
9319
9320
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009321static int
9322wpa_driver_nl80211_join_mesh(void *priv,
9323 struct wpa_driver_mesh_join_params *params)
9324{
9325 struct i802_bss *bss = priv;
9326 int ret, timeout;
9327
9328 timeout = params->conf.peer_link_timeout;
9329
9330 /* Disable kernel inactivity timer */
9331 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9332 params->conf.peer_link_timeout = 0;
9333
9334 ret = nl80211_join_mesh(bss, params);
9335 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9336 wpa_printf(MSG_DEBUG,
9337 "nl80211: Mesh join retry for peer_link_timeout");
9338 /*
9339 * Old kernel does not support setting
9340 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9341 * into future from peer_link_timeout.
9342 */
9343 params->conf.peer_link_timeout = timeout + 60;
9344 ret = nl80211_join_mesh(priv, params);
9345 }
9346
9347 params->conf.peer_link_timeout = timeout;
9348 return ret;
9349}
9350
9351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009352static int wpa_driver_nl80211_leave_mesh(void *priv)
9353{
9354 struct i802_bss *bss = priv;
9355 struct wpa_driver_nl80211_data *drv = bss->drv;
9356 struct nl_msg *msg;
9357 int ret;
9358
9359 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9360 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
9361 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9362 if (ret) {
9363 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9364 ret, strerror(-ret));
9365 } else {
9366 wpa_printf(MSG_DEBUG,
9367 "nl80211: mesh leave request send successfully");
9368 }
9369
9370 if (wpa_driver_nl80211_set_mode(drv->first_bss,
9371 NL80211_IFTYPE_STATION)) {
9372 wpa_printf(MSG_INFO,
9373 "nl80211: Failed to set interface into station mode");
9374 }
9375 return ret;
9376}
9377
9378#endif /* CONFIG_MESH */
9379
9380
9381static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9382 const u8 *ipaddr, int prefixlen,
9383 const u8 *addr)
9384{
9385#ifdef CONFIG_LIBNL3_ROUTE
9386 struct i802_bss *bss = priv;
9387 struct wpa_driver_nl80211_data *drv = bss->drv;
9388 struct rtnl_neigh *rn;
9389 struct nl_addr *nl_ipaddr = NULL;
9390 struct nl_addr *nl_lladdr = NULL;
9391 int family, addrsize;
9392 int res;
9393
9394 if (!ipaddr || prefixlen == 0 || !addr)
9395 return -EINVAL;
9396
9397 if (bss->br_ifindex == 0) {
9398 wpa_printf(MSG_DEBUG,
9399 "nl80211: bridge must be set before adding an ip neigh to it");
9400 return -1;
9401 }
9402
9403 if (!drv->rtnl_sk) {
9404 wpa_printf(MSG_DEBUG,
9405 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9406 return -1;
9407 }
9408
9409 if (version == 4) {
9410 family = AF_INET;
9411 addrsize = 4;
9412 } else if (version == 6) {
9413 family = AF_INET6;
9414 addrsize = 16;
9415 } else {
9416 return -EINVAL;
9417 }
9418
9419 rn = rtnl_neigh_alloc();
9420 if (rn == NULL)
9421 return -ENOMEM;
9422
9423 /* set the destination ip address for neigh */
9424 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9425 if (nl_ipaddr == NULL) {
9426 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9427 res = -ENOMEM;
9428 goto errout;
9429 }
9430 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9431 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9432 if (res) {
9433 wpa_printf(MSG_DEBUG,
9434 "nl80211: neigh set destination addr failed");
9435 goto errout;
9436 }
9437
9438 /* set the corresponding lladdr for neigh */
9439 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9440 if (nl_lladdr == NULL) {
9441 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9442 res = -ENOMEM;
9443 goto errout;
9444 }
9445 rtnl_neigh_set_lladdr(rn, nl_lladdr);
9446
9447 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9448 rtnl_neigh_set_state(rn, NUD_PERMANENT);
9449
9450 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9451 if (res) {
9452 wpa_printf(MSG_DEBUG,
9453 "nl80211: Adding bridge ip neigh failed: %s",
9454 strerror(errno));
9455 }
9456errout:
9457 if (nl_lladdr)
9458 nl_addr_put(nl_lladdr);
9459 if (nl_ipaddr)
9460 nl_addr_put(nl_ipaddr);
9461 if (rn)
9462 rtnl_neigh_put(rn);
9463 return res;
9464#else /* CONFIG_LIBNL3_ROUTE */
9465 return -1;
9466#endif /* CONFIG_LIBNL3_ROUTE */
9467}
9468
9469
9470static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9471 const u8 *ipaddr)
9472{
9473#ifdef CONFIG_LIBNL3_ROUTE
9474 struct i802_bss *bss = priv;
9475 struct wpa_driver_nl80211_data *drv = bss->drv;
9476 struct rtnl_neigh *rn;
9477 struct nl_addr *nl_ipaddr;
9478 int family, addrsize;
9479 int res;
9480
9481 if (!ipaddr)
9482 return -EINVAL;
9483
9484 if (version == 4) {
9485 family = AF_INET;
9486 addrsize = 4;
9487 } else if (version == 6) {
9488 family = AF_INET6;
9489 addrsize = 16;
9490 } else {
9491 return -EINVAL;
9492 }
9493
9494 if (bss->br_ifindex == 0) {
9495 wpa_printf(MSG_DEBUG,
9496 "nl80211: bridge must be set to delete an ip neigh");
9497 return -1;
9498 }
9499
9500 if (!drv->rtnl_sk) {
9501 wpa_printf(MSG_DEBUG,
9502 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9503 return -1;
9504 }
9505
9506 rn = rtnl_neigh_alloc();
9507 if (rn == NULL)
9508 return -ENOMEM;
9509
9510 /* set the destination ip address for neigh */
9511 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9512 if (nl_ipaddr == NULL) {
9513 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9514 res = -ENOMEM;
9515 goto errout;
9516 }
9517 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9518 if (res) {
9519 wpa_printf(MSG_DEBUG,
9520 "nl80211: neigh set destination addr failed");
9521 goto errout;
9522 }
9523
9524 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9525
9526 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9527 if (res) {
9528 wpa_printf(MSG_DEBUG,
9529 "nl80211: Deleting bridge ip neigh failed: %s",
9530 strerror(errno));
9531 }
9532errout:
9533 if (nl_ipaddr)
9534 nl_addr_put(nl_ipaddr);
9535 if (rn)
9536 rtnl_neigh_put(rn);
9537 return res;
9538#else /* CONFIG_LIBNL3_ROUTE */
9539 return -1;
9540#endif /* CONFIG_LIBNL3_ROUTE */
9541}
9542
9543
9544static int linux_write_system_file(const char *path, unsigned int val)
9545{
9546 char buf[50];
9547 int fd, len;
9548
9549 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9550 if (os_snprintf_error(sizeof(buf), len))
9551 return -1;
9552
9553 fd = open(path, O_WRONLY);
9554 if (fd < 0)
9555 return -1;
9556
9557 if (write(fd, buf, len) < 0) {
9558 wpa_printf(MSG_DEBUG,
9559 "nl80211: Failed to write Linux system file: %s with the value of %d",
9560 path, val);
9561 close(fd);
9562 return -1;
9563 }
9564 close(fd);
9565
9566 return 0;
9567}
9568
9569
9570static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9571{
9572 switch (attr) {
9573 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07009574 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009575 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9576 return "hairpin_mode";
9577 }
9578
9579 return NULL;
9580}
9581
9582
9583static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9584 unsigned int val)
9585{
9586 struct i802_bss *bss = priv;
9587 char path[128];
9588 const char *attr_txt;
9589
9590 attr_txt = drv_br_port_attr_str(attr);
9591 if (attr_txt == NULL)
9592 return -EINVAL;
9593
9594 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9595 bss->ifname, attr_txt);
9596
9597 if (linux_write_system_file(path, val))
9598 return -1;
9599
9600 return 0;
9601}
9602
9603
9604static const char * drv_br_net_param_str(enum drv_br_net_param param)
9605{
9606 switch (param) {
9607 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9608 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07009609 default:
9610 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009611 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009612}
9613
9614
9615static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9616 unsigned int val)
9617{
9618 struct i802_bss *bss = priv;
9619 char path[128];
9620 const char *param_txt;
9621 int ip_version = 4;
9622
Dmitry Shmidt83474442015-04-15 13:47:09 -07009623 if (param == DRV_BR_MULTICAST_SNOOPING) {
9624 os_snprintf(path, sizeof(path),
9625 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9626 bss->brname);
9627 goto set_val;
9628 }
9629
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009630 param_txt = drv_br_net_param_str(param);
9631 if (param_txt == NULL)
9632 return -EINVAL;
9633
9634 switch (param) {
9635 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9636 ip_version = 4;
9637 break;
9638 default:
9639 return -EINVAL;
9640 }
9641
9642 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9643 ip_version, bss->brname, param_txt);
9644
Dmitry Shmidt83474442015-04-15 13:47:09 -07009645set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009646 if (linux_write_system_file(path, val))
9647 return -1;
9648
9649 return 0;
9650}
9651
9652
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009653#ifdef CONFIG_DRIVER_NL80211_QCA
9654
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009655static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9656{
9657 switch (hw_mode) {
9658 case HOSTAPD_MODE_IEEE80211B:
9659 return QCA_ACS_MODE_IEEE80211B;
9660 case HOSTAPD_MODE_IEEE80211G:
9661 return QCA_ACS_MODE_IEEE80211G;
9662 case HOSTAPD_MODE_IEEE80211A:
9663 return QCA_ACS_MODE_IEEE80211A;
9664 case HOSTAPD_MODE_IEEE80211AD:
9665 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07009666 case HOSTAPD_MODE_IEEE80211ANY:
9667 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009668 default:
9669 return -1;
9670 }
9671}
9672
9673
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009674static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9675{
9676 int i, len, ret;
9677 u32 *freqs;
9678
9679 if (!freq_list)
9680 return 0;
9681 len = int_array_len(freq_list);
9682 freqs = os_malloc(sizeof(u32) * len);
9683 if (!freqs)
9684 return -1;
9685 for (i = 0; i < len; i++)
9686 freqs[i] = freq_list[i];
9687 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9688 sizeof(u32) * len, freqs);
9689 os_free(freqs);
9690 return ret;
9691}
9692
9693
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009694static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9695{
9696 struct i802_bss *bss = priv;
9697 struct wpa_driver_nl80211_data *drv = bss->drv;
9698 struct nl_msg *msg;
9699 struct nlattr *data;
9700 int ret;
9701 int mode;
9702
9703 mode = hw_mode_to_qca_acs(params->hw_mode);
9704 if (mode < 0)
9705 return -1;
9706
9707 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9708 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9709 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9710 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9711 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9712 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9713 (params->ht_enabled &&
9714 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9715 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009716 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9717 (params->vht_enabled &&
9718 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9719 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9720 params->ch_width) ||
9721 (params->ch_list_len &&
9722 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009723 params->ch_list)) ||
9724 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009725 nlmsg_free(msg);
9726 return -ENOBUFS;
9727 }
9728 nla_nest_end(msg, data);
9729
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009730 wpa_printf(MSG_DEBUG,
9731 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9732 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9733 params->vht_enabled, params->ch_width, params->ch_list_len);
9734
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009735 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9736 if (ret) {
9737 wpa_printf(MSG_DEBUG,
9738 "nl80211: Failed to invoke driver ACS function: %s",
9739 strerror(errno));
9740 }
9741 return ret;
9742}
9743
9744
Ravi Joshie6ccb162015-07-16 17:45:41 -07009745static int nl80211_set_band(void *priv, enum set_band band)
9746{
9747 struct i802_bss *bss = priv;
9748 struct wpa_driver_nl80211_data *drv = bss->drv;
9749 struct nl_msg *msg;
9750 struct nlattr *data;
9751 int ret;
9752 enum qca_set_band qca_band;
9753
9754 if (!drv->setband_vendor_cmd_avail)
9755 return -1;
9756
9757 switch (band) {
9758 case WPA_SETBAND_AUTO:
9759 qca_band = QCA_SETBAND_AUTO;
9760 break;
9761 case WPA_SETBAND_5G:
9762 qca_band = QCA_SETBAND_5G;
9763 break;
9764 case WPA_SETBAND_2G:
9765 qca_band = QCA_SETBAND_2G;
9766 break;
9767 default:
9768 return -1;
9769 }
9770
9771 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9772 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9773 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9774 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9775 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9776 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9777 nlmsg_free(msg);
9778 return -ENOBUFS;
9779 }
9780 nla_nest_end(msg, data);
9781
9782 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9783 if (ret) {
9784 wpa_printf(MSG_DEBUG,
9785 "nl80211: Driver setband function failed: %s",
9786 strerror(errno));
9787 }
9788 return ret;
9789}
9790
9791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009792struct nl80211_pcl {
9793 unsigned int num;
9794 unsigned int *freq_list;
9795};
9796
9797static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9798{
9799 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9800 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9801 struct nl80211_pcl *param = arg;
9802 struct nlattr *nl_vend, *attr;
9803 enum qca_iface_type iface_type;
9804 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9805 unsigned int num, max_num;
9806 u32 *freqs;
9807
9808 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9809 genlmsg_attrlen(gnlh, 0), NULL);
9810
9811 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9812 if (!nl_vend)
9813 return NL_SKIP;
9814
9815 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9816 nla_data(nl_vend), nla_len(nl_vend), NULL);
9817
9818 attr = tb_vendor[
9819 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9820 if (!attr) {
9821 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9822 param->num = 0;
9823 return NL_SKIP;
9824 }
9825
9826 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9827 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9828 iface_type);
9829
9830 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9831 if (!attr) {
9832 wpa_printf(MSG_ERROR,
9833 "nl80211: preferred_freq_list couldn't be found");
9834 param->num = 0;
9835 return NL_SKIP;
9836 }
9837
9838 /*
9839 * param->num has the maximum number of entries for which there
9840 * is room in the freq_list provided by the caller.
9841 */
9842 freqs = nla_data(attr);
9843 max_num = nla_len(attr) / sizeof(u32);
9844 if (max_num > param->num)
9845 max_num = param->num;
9846 for (num = 0; num < max_num; num++)
9847 param->freq_list[num] = freqs[num];
9848 param->num = num;
9849
9850 return NL_SKIP;
9851}
9852
9853
9854static int nl80211_get_pref_freq_list(void *priv,
9855 enum wpa_driver_if_type if_type,
9856 unsigned int *num,
9857 unsigned int *freq_list)
9858{
9859 struct i802_bss *bss = priv;
9860 struct wpa_driver_nl80211_data *drv = bss->drv;
9861 struct nl_msg *msg;
9862 int ret;
9863 unsigned int i;
9864 struct nlattr *params;
9865 struct nl80211_pcl param;
9866 enum qca_iface_type iface_type;
9867
9868 if (!drv->get_pref_freq_list)
9869 return -1;
9870
9871 switch (if_type) {
9872 case WPA_IF_STATION:
9873 iface_type = QCA_IFACE_TYPE_STA;
9874 break;
9875 case WPA_IF_AP_BSS:
9876 iface_type = QCA_IFACE_TYPE_AP;
9877 break;
9878 case WPA_IF_P2P_GO:
9879 iface_type = QCA_IFACE_TYPE_P2P_GO;
9880 break;
9881 case WPA_IF_P2P_CLIENT:
9882 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9883 break;
9884 case WPA_IF_IBSS:
9885 iface_type = QCA_IFACE_TYPE_IBSS;
9886 break;
9887 case WPA_IF_TDLS:
9888 iface_type = QCA_IFACE_TYPE_TDLS;
9889 break;
9890 default:
9891 return -1;
9892 }
9893
9894 param.num = *num;
9895 param.freq_list = freq_list;
9896
9897 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9898 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9899 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9900 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9901 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9902 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9903 nla_put_u32(msg,
9904 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9905 iface_type)) {
9906 wpa_printf(MSG_ERROR,
9907 "%s: err in adding vendor_cmd and vendor_data",
9908 __func__);
9909 nlmsg_free(msg);
9910 return -1;
9911 }
9912 nla_nest_end(msg, params);
9913
9914 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9915 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9916 if (ret) {
9917 wpa_printf(MSG_ERROR,
9918 "%s: err in send_and_recv_msgs", __func__);
9919 return ret;
9920 }
9921
9922 *num = param.num;
9923
9924 for (i = 0; i < *num; i++) {
9925 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9926 i, freq_list[i]);
9927 }
9928
9929 return 0;
9930}
9931
9932
9933static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9934{
9935 struct i802_bss *bss = priv;
9936 struct wpa_driver_nl80211_data *drv = bss->drv;
9937 struct nl_msg *msg;
9938 int ret;
9939 struct nlattr *params;
9940
9941 if (!drv->set_prob_oper_freq)
9942 return -1;
9943
9944 wpa_printf(MSG_DEBUG,
9945 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9946 freq, bss->ifindex);
9947
9948 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9949 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9950 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9951 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9952 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9953 nla_put_u32(msg,
9954 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9955 QCA_IFACE_TYPE_P2P_CLIENT) ||
9956 nla_put_u32(msg,
9957 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9958 freq)) {
9959 wpa_printf(MSG_ERROR,
9960 "%s: err in adding vendor_cmd and vendor_data",
9961 __func__);
9962 nlmsg_free(msg);
9963 return -1;
9964 }
9965 nla_nest_end(msg, params);
9966
9967 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9968 msg = NULL;
9969 if (ret) {
9970 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9971 __func__);
9972 return ret;
9973 }
9974 nlmsg_free(msg);
9975 return 0;
9976}
9977
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009978
9979static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9980 unsigned int period, unsigned int interval,
9981 unsigned int count, const u8 *device_types,
9982 size_t dev_types_len,
9983 const u8 *ies, size_t ies_len)
9984{
9985 struct i802_bss *bss = priv;
9986 struct wpa_driver_nl80211_data *drv = bss->drv;
9987 struct nl_msg *msg;
9988 struct nlattr *container;
9989 int ret;
9990
9991 wpa_printf(MSG_DEBUG,
9992 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9993 freq, period, interval, count);
9994
9995 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9996 return -1;
9997
9998 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9999 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10000 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10001 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10002 goto fail;
10003
10004 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10005 if (!container)
10006 goto fail;
10007
10008 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10009 freq) ||
10010 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10011 period) ||
10012 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10013 interval) ||
10014 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10015 count) ||
10016 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10017 dev_types_len, device_types) ||
10018 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10019 ies_len, ies))
10020 goto fail;
10021
10022 nla_nest_end(msg, container);
10023 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10024 msg = NULL;
10025 if (ret) {
10026 wpa_printf(MSG_DEBUG,
10027 "nl80211: Failed to send P2P Listen offload vendor command");
10028 goto fail;
10029 }
10030
10031 return 0;
10032
10033fail:
10034 nlmsg_free(msg);
10035 return -1;
10036}
10037
10038
10039static int nl80211_p2p_lo_stop(void *priv)
10040{
10041 struct i802_bss *bss = priv;
10042 struct wpa_driver_nl80211_data *drv = bss->drv;
10043 struct nl_msg *msg;
10044
10045 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10046
10047 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10048 return -1;
10049
10050 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10051 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10052 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10053 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10054 nlmsg_free(msg);
10055 return -1;
10056 }
10057
10058 return send_and_recv_msgs(drv, msg, NULL, NULL);
10059}
10060
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010061
10062static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10063{
10064 struct i802_bss *bss = priv;
10065 struct wpa_driver_nl80211_data *drv = bss->drv;
10066 struct nl_msg *msg;
10067 struct nlattr *params;
10068 int ret;
10069 u32 tdls_mode;
10070
10071 wpa_printf(MSG_DEBUG,
10072 "nl80211: Set TDKS mode: tdls_external_control=%d",
10073 tdls_external_control);
10074
10075 if (tdls_external_control == 1)
10076 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10077 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10078 else
10079 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10080
10081 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10082 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10083 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10084 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10085 goto fail;
10086
10087 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10088 if (!params)
10089 goto fail;
10090
10091 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10092 tdls_mode))
10093 goto fail;
10094
10095 nla_nest_end(msg, params);
10096
10097 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10098 msg = NULL;
10099 if (ret) {
10100 wpa_printf(MSG_ERROR,
10101 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10102 ret, strerror(-ret));
10103 goto fail;
10104 }
10105 return 0;
10106fail:
10107 nlmsg_free(msg);
10108 return -1;
10109}
10110
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010111
10112#ifdef CONFIG_MBO
10113
10114static enum mbo_transition_reject_reason
10115nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10116{
10117 switch (status) {
10118 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10119 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10120 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10121 return MBO_TRANSITION_REJECT_REASON_DELAY;
10122 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10123 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10124 case QCA_STATUS_REJECT_LOW_RSSI:
10125 return MBO_TRANSITION_REJECT_REASON_RSSI;
10126 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10127 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10128 case QCA_STATUS_REJECT_UNKNOWN:
10129 default:
10130 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10131 }
10132}
10133
10134
10135static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10136 struct nlattr *tb[], int num)
10137{
10138 enum qca_wlan_btm_candidate_status status;
10139 char buf[50];
10140
10141 os_memcpy(candidate->bssid,
10142 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10143 ETH_ALEN);
10144
10145 status = nla_get_u32(
10146 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10147 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10148 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10149
10150 if (candidate->is_accept)
10151 os_snprintf(buf, sizeof(buf), "Accepted");
10152 else
10153 os_snprintf(buf, sizeof(buf),
10154 "Rejected, Reject_reason: %d",
10155 candidate->reject_reason);
10156 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10157 num, MAC2STR(candidate->bssid), buf);
10158}
10159
10160
10161static int
10162nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10163{
10164 struct wpa_bss_candidate_info *info = arg;
10165 struct candidate_list *candidate = info->candidates;
10166 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10167 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10168 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10169 static struct nla_policy policy[
10170 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10171 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10172 .minlen = ETH_ALEN
10173 },
10174 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10175 .type = NLA_U32,
10176 },
10177 };
10178 struct nlattr *attr;
10179 int rem;
10180 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10181 u8 num;
10182
10183 num = info->num; /* number of candidates sent to driver */
10184 info->num = 0;
10185 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10186 genlmsg_attrlen(gnlh, 0), NULL);
10187
10188 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10189 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10190 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10191 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10192 return NL_SKIP;
10193
10194 wpa_printf(MSG_DEBUG,
10195 "nl80211: WNM Candidate list received from driver");
10196 nla_for_each_nested(attr,
10197 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10198 rem) {
10199 if (info->num >= num ||
10200 nla_parse_nested(
10201 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10202 attr, policy) ||
10203 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10204 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10205 break;
10206
10207 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10208
10209 candidate++;
10210 info->num++;
10211 }
10212
10213 return NL_SKIP;
10214}
10215
10216
10217static struct wpa_bss_candidate_info *
10218nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10219{
10220 struct i802_bss *bss = priv;
10221 struct wpa_driver_nl80211_data *drv = bss->drv;
10222 struct nl_msg *msg;
10223 struct nlattr *attr, *attr1, *attr2;
10224 struct wpa_bss_candidate_info *info;
10225 u8 i;
10226 int ret;
10227 u8 *pos;
10228
10229 if (!drv->fetch_bss_trans_status)
10230 return NULL;
10231
10232 info = os_zalloc(sizeof(*info));
10233 if (!info)
10234 return NULL;
10235 /* Allocate memory for number of candidates sent to driver */
10236 info->candidates = os_calloc(params->n_candidates,
10237 sizeof(*info->candidates));
10238 if (!info->candidates) {
10239 os_free(info);
10240 return NULL;
10241 }
10242
10243 /* Copy the number of candidates being sent to driver. This is used in
10244 * nl80211_get_bss_transition_status_handler() to limit the number of
10245 * candidates that can be populated in info->candidates and will be
10246 * later overwritten with the actual number of candidates received from
10247 * the driver.
10248 */
10249 info->num = params->n_candidates;
10250
10251 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10252 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10253 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10254 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10255 goto fail;
10256
10257 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10258 if (!attr)
10259 goto fail;
10260
10261 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10262 params->mbo_transition_reason))
10263 goto fail;
10264
10265 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10266 if (!attr1)
10267 goto fail;
10268
10269 wpa_printf(MSG_DEBUG,
10270 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10271 params->mbo_transition_reason, params->n_candidates);
10272 pos = params->bssid;
10273 for (i = 0; i < params->n_candidates; i++) {
10274 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
10275 MAC2STR(pos));
10276 attr2 = nla_nest_start(msg, i);
10277 if (!attr2 ||
10278 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10279 ETH_ALEN, pos))
10280 goto fail;
10281 pos += ETH_ALEN;
10282 nla_nest_end(msg, attr2);
10283 }
10284
10285 nla_nest_end(msg, attr1);
10286 nla_nest_end(msg, attr);
10287
10288 ret = send_and_recv_msgs(drv, msg,
10289 nl80211_get_bss_transition_status_handler,
10290 info);
10291 msg = NULL;
10292 if (ret) {
10293 wpa_printf(MSG_ERROR,
10294 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10295 ret, strerror(-ret));
10296 goto fail;
10297 }
10298 return info;
10299
10300fail:
10301 nlmsg_free(msg);
10302 os_free(info->candidates);
10303 os_free(info);
10304 return NULL;
10305}
10306
10307
10308/**
10309 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10310 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10311 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10312 * Returns: 0 on success, -1 on failure
10313 */
10314static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10315{
10316 struct i802_bss *bss = priv;
10317 struct wpa_driver_nl80211_data *drv = bss->drv;
10318 struct nl_msg *msg;
10319 struct nlattr *attr;
10320 int ret = -1;
10321
10322 if (!drv->set_wifi_conf_vendor_cmd_avail)
10323 return -1;
10324
10325 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10326 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10327 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10328 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10329 goto fail;
10330
10331 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10332 if (!attr)
10333 goto fail;
10334
10335 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10336 ignore_disallow);
10337 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10338 ignore_disallow))
10339 goto fail;
10340
10341 nla_nest_end(msg, attr);
10342
10343 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10344 msg = NULL;
10345 if (ret) {
10346 wpa_printf(MSG_ERROR,
10347 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10348 ret, strerror(-ret));
10349 goto fail;
10350 }
10351
10352fail:
10353 nlmsg_free(msg);
10354 return ret;
10355}
10356
10357#endif /* CONFIG_MBO */
10358
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010359#endif /* CONFIG_DRIVER_NL80211_QCA */
10360
10361
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010362static int nl80211_write_to_file(const char *name, unsigned int val)
10363{
10364 int fd, len;
10365 char tmp[128];
10366
10367 fd = open(name, O_RDWR);
10368 if (fd < 0) {
10369 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10370 name, strerror(errno));
10371 return fd;
10372 }
10373
10374 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10375 len = write(fd, tmp, len);
10376 if (len < 0)
10377 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10378 name, strerror(errno));
10379 close(fd);
10380
10381 return 0;
10382}
10383
10384
10385static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10386{
10387 struct i802_bss *bss = priv;
10388 char path[128];
10389 int ret;
10390
10391 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10392 filter_flags);
10393
10394 /* Configure filtering of unicast frame encrypted using GTK */
10395 ret = os_snprintf(path, sizeof(path),
10396 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10397 bss->ifname);
10398 if (os_snprintf_error(sizeof(path), ret))
10399 return -1;
10400
10401 ret = nl80211_write_to_file(path,
10402 !!(filter_flags &
10403 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10404 if (ret) {
10405 wpa_printf(MSG_ERROR,
10406 "nl80211: Failed to set IPv4 unicast in multicast filter");
10407 return ret;
10408 }
10409
10410 os_snprintf(path, sizeof(path),
10411 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10412 bss->ifname);
10413 ret = nl80211_write_to_file(path,
10414 !!(filter_flags &
10415 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10416
10417 if (ret) {
10418 wpa_printf(MSG_ERROR,
10419 "nl80211: Failed to set IPv6 unicast in multicast filter");
10420 return ret;
10421 }
10422
10423 /* Configure filtering of unicast frame encrypted using GTK */
10424 os_snprintf(path, sizeof(path),
10425 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10426 bss->ifname);
10427 ret = nl80211_write_to_file(path,
10428 !!(filter_flags &
10429 WPA_DATA_FRAME_FILTER_FLAG_ARP));
10430 if (ret) {
10431 wpa_printf(MSG_ERROR,
10432 "nl80211: Failed set gratuitous ARP filter");
10433 return ret;
10434 }
10435
10436 /* Configure filtering of IPv6 NA frames */
10437 os_snprintf(path, sizeof(path),
10438 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10439 bss->ifname);
10440 ret = nl80211_write_to_file(path,
10441 !!(filter_flags &
10442 WPA_DATA_FRAME_FILTER_FLAG_NA));
10443 if (ret) {
10444 wpa_printf(MSG_ERROR,
10445 "nl80211: Failed to set unsolicited NA filter");
10446 return ret;
10447 }
10448
10449 return 0;
10450}
10451
10452
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010453static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10454 const u8 **ext_capa, const u8 **ext_capa_mask,
10455 unsigned int *ext_capa_len)
10456{
10457 struct i802_bss *bss = priv;
10458 struct wpa_driver_nl80211_data *drv = bss->drv;
10459 enum nl80211_iftype nlmode;
10460 unsigned int i;
10461
10462 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10463 return -1;
10464
10465 nlmode = wpa_driver_nl80211_if_type(type);
10466
10467 /* By default, use the per-radio values */
10468 *ext_capa = drv->extended_capa;
10469 *ext_capa_mask = drv->extended_capa_mask;
10470 *ext_capa_len = drv->extended_capa_len;
10471
10472 /* Replace the default value if a per-interface type value exists */
10473 for (i = 0; i < drv->num_iface_ext_capa; i++) {
10474 if (nlmode == drv->iface_ext_capa[i].iftype) {
10475 *ext_capa = drv->iface_ext_capa[i].ext_capa;
10476 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10477 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10478 break;
10479 }
10480 }
10481
10482 return 0;
10483}
10484
10485
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010486static int nl80211_update_connection_params(
10487 void *priv, struct wpa_driver_associate_params *params,
10488 enum wpa_drv_update_connect_params_mask mask)
10489{
10490 struct i802_bss *bss = priv;
10491 struct wpa_driver_nl80211_data *drv = bss->drv;
10492 struct nl_msg *msg;
10493 int ret = -1;
10494 enum nl80211_auth_type type;
10495
10496 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10497 if (!msg)
10498 goto fail;
10499
10500 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10501 drv->ifindex);
10502
10503 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10504 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10505 params->wpa_ie))
10506 goto fail;
10507 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
10508 params->wpa_ie_len);
10509 }
10510
10511 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10512 type = get_nl_auth_type(params->auth_alg);
10513 if (type == NL80211_AUTHTYPE_MAX ||
10514 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10515 goto fail;
10516 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
10517 }
10518
10519 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10520 nl80211_put_fils_connect_params(drv, params, msg))
10521 goto fail;
10522
10523 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10524 msg = NULL;
10525 if (ret)
10526 wpa_dbg(drv->ctx, MSG_DEBUG,
10527 "nl80211: Update connect params command failed: ret=%d (%s)",
10528 ret, strerror(-ret));
10529
10530fail:
10531 nlmsg_free(msg);
10532 return ret;
10533}
10534
10535
Roshan Pius3a1667e2018-07-03 15:17:14 -070010536static int nl80211_send_external_auth_status(void *priv,
10537 struct external_auth *params)
10538{
10539 struct i802_bss *bss = priv;
10540 struct wpa_driver_nl80211_data *drv = bss->drv;
10541 struct nl_msg *msg = NULL;
10542 int ret = -1;
10543
10544 wpa_dbg(drv->ctx, MSG_DEBUG,
10545 "nl80211: External auth status: %u", params->status);
10546
10547 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10548 if (!msg ||
10549 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10550 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10551 params->ssid) ||
10552 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10553 goto fail;
10554 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10555 msg = NULL;
10556 if (ret) {
10557 wpa_printf(MSG_DEBUG,
10558 "nl80211: External Auth status update failed: ret=%d (%s)",
10559 ret, strerror(-ret));
10560 goto fail;
10561 }
10562fail:
10563 nlmsg_free(msg);
10564 return ret;
10565}
10566
10567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010568const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10569 .name = "nl80211",
10570 .desc = "Linux nl80211/cfg80211",
10571 .get_bssid = wpa_driver_nl80211_get_bssid,
10572 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010573 .set_key = driver_nl80211_set_key,
10574 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010575 .sched_scan = wpa_driver_nl80211_sched_scan,
10576 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010577 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080010578 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010579 .deauthenticate = driver_nl80211_deauthenticate,
10580 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010581 .associate = wpa_driver_nl80211_associate,
10582 .global_init = nl80211_global_init,
10583 .global_deinit = nl80211_global_deinit,
10584 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010585 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010586 .get_capa = wpa_driver_nl80211_get_capa,
10587 .set_operstate = wpa_driver_nl80211_set_operstate,
10588 .set_supp_port = wpa_driver_nl80211_set_supp_port,
10589 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010590 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010591 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070010592 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010593 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010594 .if_remove = driver_nl80211_if_remove,
10595 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010596 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010597 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010598 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010599 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
10600 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010601 .hapd_init = i802_init,
10602 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070010603 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010604 .get_seqnum = i802_get_seqnum,
10605 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010606 .get_inact_sec = i802_get_inact_sec,
10607 .sta_clear_stats = i802_sta_clear_stats,
10608 .set_rts = i802_set_rts,
10609 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010610 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010611 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010612 .sta_deauth = i802_sta_deauth,
10613 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010614 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010615 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010616 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010617 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
10618 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10619 .cancel_remain_on_channel =
10620 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010621 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010622 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070010623 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010624 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010625 .signal_monitor = nl80211_signal_monitor,
10626 .signal_poll = nl80211_signal_poll,
10627 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010628 .set_param = nl80211_set_param,
10629 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070010630 .add_pmkid = nl80211_add_pmkid,
10631 .remove_pmkid = nl80211_remove_pmkid,
10632 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010633 .set_rekey_info = nl80211_set_rekey_info,
10634 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010635 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010636 .start_dfs_cac = nl80211_start_radar_detection,
10637 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010638#ifdef CONFIG_TDLS
10639 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10640 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010641 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10642 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010643#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070010644 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010645 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010646 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070010647 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080010648 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010649#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010650 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010651 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010652 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010653#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070010654#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -070010655#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070010656 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -070010657#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080010658#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080010659 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010660 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070010661 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010662 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010663#ifdef CONFIG_MESH
10664 .init_mesh = wpa_driver_nl80211_init_mesh,
10665 .join_mesh = wpa_driver_nl80211_join_mesh,
10666 .leave_mesh = wpa_driver_nl80211_leave_mesh,
10667#endif /* CONFIG_MESH */
10668 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10669 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
10670 .br_port_set_attr = wpa_driver_br_port_set_attr,
10671 .br_set_net_param = wpa_driver_br_set_net_param,
10672 .add_tx_ts = nl80211_add_ts,
10673 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -070010674 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010675#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010676 .roaming = nl80211_roaming,
Roshan Pius3a1667e2018-07-03 15:17:14 -070010677 .disable_fils = nl80211_disable_fils,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010678 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -070010679 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010680 .get_pref_freq_list = nl80211_get_pref_freq_list,
10681 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010682 .p2p_lo_start = nl80211_p2p_lo_start,
10683 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070010684 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080010685 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010686#ifdef CONFIG_MBO
10687 .get_bss_transition_status = nl80211_get_bss_transition_status,
10688 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
10689#endif /* CONFIG_MBO */
10690 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010691#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010692 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070010693 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010694 .update_connect_params = nl80211_update_connection_params,
Roshan Pius3a1667e2018-07-03 15:17:14 -070010695 .send_external_auth_status = nl80211_send_external_auth_status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010696};