]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
wpa_supplicant: Handle port authorized event
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
CommitLineData
3f5285e8 1/*
c5121837 2 * Driver interaction with Linux nl80211/cfg80211
98cd3d1c 3 * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
072ad14c
JM
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
58f6fbe0 7 * Copyright (c) 2009-2010, Atheros Communications
3f5285e8 8 *
0f3d578e
JM
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
3f5285e8
JM
11 */
12
13#include "includes.h"
6859f1cb 14#include <sys/types.h>
73d2294f 15#include <fcntl.h>
37b7d082 16#include <net/if.h>
3f5285e8 17#include <netlink/genl/genl.h>
3f5285e8 18#include <netlink/genl/ctrl.h>
97ed9a06
KP
19#ifdef CONFIG_LIBNL3_ROUTE
20#include <netlink/route/neighbour.h>
21#endif /* CONFIG_LIBNL3_ROUTE */
8602b0f2 22#include <linux/rtnetlink.h>
1b648c7e 23#include <netpacket/packet.h>
32ab4855 24#include <linux/errqueue.h>
625f587b 25
3f5285e8 26#include "common.h"
1b648c7e 27#include "eloop.h"
1682c623 28#include "common/qca-vendor.h"
a26582cb 29#include "common/qca-vendor-attr.h"
1b648c7e 30#include "common/ieee802_11_defs.h"
9b90955e 31#include "common/ieee802_11_common.h"
3aa24db9 32#include "common/wpa_common.h"
f10bfc9a 33#include "l2_packet/l2_packet.h"
e2d02c29 34#include "netlink.h"
1474e6e9 35#include "linux_defines.h"
34f2f814 36#include "linux_ioctl.h"
e2d02c29
JM
37#include "radiotap.h"
38#include "radiotap_iter.h"
8401a6b0 39#include "rfkill.h"
1474e6e9 40#include "driver_nl80211.h"
32ab4855 41
1474e6e9
JM
42
43#ifndef CONFIG_LIBNL20
a65a9aed
JB
44/*
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}
c5121837
JM
83#endif /* CONFIG_LIBNL20 */
84
c5121837 85
1c6edec6
JM
86#ifdef ANDROID
87/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
1c6edec6
JM
88#undef nl_socket_set_nonblocking
89#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
70a96c81 90
1c6edec6
JM
91#endif /* ANDROID */
92
93
481234cf 94static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
a92dfde8 95{
481234cf 96 struct nl_handle *handle;
a92dfde8 97
481234cf
JM
98 handle = nl80211_handle_alloc(cb);
99 if (handle == NULL) {
a92dfde8
JB
100 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
101 "callbacks (%s)", dbg);
481234cf 102 return NULL;
a92dfde8
JB
103 }
104
481234cf 105 if (genl_connect(handle)) {
a92dfde8
JB
106 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
107 "netlink (%s)", dbg);
481234cf
JM
108 nl80211_handle_destroy(handle);
109 return NULL;
a92dfde8
JB
110 }
111
481234cf 112 return handle;
a92dfde8
JB
113}
114
115
481234cf 116static void nl_destroy_handles(struct nl_handle **handle)
a92dfde8 117{
481234cf 118 if (*handle == NULL)
a92dfde8 119 return;
481234cf
JM
120 nl80211_handle_destroy(*handle);
121 *handle = NULL;
a92dfde8
JB
122}
123
124
10b85921
JB
125#if __WORDSIZE == 64
126#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
127#else
128#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
129#endif
130
5f65e9f7
JB
131static void nl80211_register_eloop_read(struct nl_handle **handle,
132 eloop_sock_handler handler,
40a68f33 133 void *eloop_data, int persist)
5f65e9f7 134{
fa38860c 135#ifdef CONFIG_LIBNL20
630b3230
JM
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 }
fa38860c 149#endif /* CONFIG_LIBNL20 */
630b3230 150
10b85921 151 nl_socket_set_nonblocking(*handle);
5f65e9f7
JB
152 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
153 eloop_data, *handle);
40a68f33
SD
154 if (!persist)
155 *handle = (void *) (((intptr_t) *handle) ^
156 ELOOP_SOCKET_INVALID);
5f65e9f7
JB
157}
158
159
40a68f33 160static void nl80211_destroy_eloop_handle(struct nl_handle **handle, int persist)
5f65e9f7 161{
40a68f33
SD
162 if (!persist)
163 *handle = (void *) (((intptr_t) *handle) ^
164 ELOOP_SOCKET_INVALID);
5f65e9f7
JB
165 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
166 nl_destroy_handles(handle);
167}
168
169
36d84860 170static void nl80211_global_deinit(void *priv);
f51f54a0 171static void nl80211_check_global(struct nl80211_global *global);
36d84860 172
9ebce9c5 173static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
4ec68377
JD
174static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
175 struct hostapd_freq_params *freq);
3c5d34e3 176
362f781e 177static int
0d547d5f 178wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
0ecff8d7
JM
179 const u8 *set_addr, int first,
180 const char *driver_params);
88df0ef7 181static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f 182 unsigned int freq, unsigned int wait,
b106173a 183 const u8 *buf, size_t buf_len, u64 *cookie,
2d3943ce
AO
184 int no_cck, int no_ack, int offchanok,
185 const u16 *csa_offs, size_t csa_offs_len);
9ebce9c5
JM
186static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
187 int report);
0915d02c 188
732b1d20
MB
189#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);
072ad14c 197
e87ef751
PX
198static int nl80211_set_channel(struct i802_bss *bss,
199 struct hostapd_freq_params *freq, int set_chan);
4e5cb1a3
JM
200static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
201 int ifindex, int disabled);
504e905c 202
666e508c
JM
203static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
204 int reset_mode);
21bdbe38 205
bf83eab5 206static int i802_set_iface_flags(struct i802_bss *bss, int up);
0ecff8d7 207static int nl80211_set_param(void *priv, const char *param);
052b8d38
MH
208#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 */
05e5e615
DL
212static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
213 int reason);
bf83eab5 214
3f5285e8 215
8d1fdde7 216/* Converts nl80211_chan_width to a common format */
f3407c66 217enum chan_width convert2width(int width)
8d1fdde7
JD
218{
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
f3407c66 237int is_ap_interface(enum nl80211_iftype nlmode)
b1f625e0 238{
0e80ea2c
JM
239 return nlmode == NL80211_IFTYPE_AP ||
240 nlmode == NL80211_IFTYPE_P2P_GO;
b1f625e0
EP
241}
242
243
477af8f8 244int is_sta_interface(enum nl80211_iftype nlmode)
b1f625e0 245{
0e80ea2c
JM
246 return nlmode == NL80211_IFTYPE_STATION ||
247 nlmode == NL80211_IFTYPE_P2P_CLIENT;
b1f625e0
EP
248}
249
250
6a71413e 251static int is_p2p_net_interface(enum nl80211_iftype nlmode)
b3af99d2 252{
0e80ea2c
JM
253 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
254 nlmode == NL80211_IFTYPE_P2P_GO;
b3af99d2
JM
255}
256
257
f3407c66
JM
258struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
259 int ifindex)
5dfbd725
JM
260{
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
afb0550a
BC
272static int is_mesh_interface(enum nl80211_iftype nlmode)
273{
274 return nlmode == NL80211_IFTYPE_MESH_POINT;
275}
276
277
f3407c66 278void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
add9b7a4
JM
279{
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
6241fcb1
JM
287/* 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{
8e8df255
JM
297 int *ret = arg;
298 *ret = 0;
6241fcb1
JM
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
5b7b85f6
JM
310
311static int no_seq_check(struct nl_msg *msg, void *arg)
312{
313 return NL_OK;
314}
315
316
bbd89bfc
JM
317static 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);
d58ade21
JM
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;
bbd89bfc
JM
333
334 os_memset(data, 0, len);
335 }
336}
337
338
d6c9aab8 339static int send_and_recv(struct nl80211_global *global,
58f6fbe0
JM
340 struct nl_handle *nl_handle, struct nl_msg *msg,
341 int (*valid_handler)(struct nl_msg *, void *),
342 void *valid_data)
6241fcb1
JM
343{
344 struct nl_cb *cb;
345 int err = -ENOMEM;
346
9725b784
JM
347 if (!msg)
348 return -ENOMEM;
349
d6c9aab8 350 cb = nl_cb_clone(global->nl_cb);
6241fcb1
JM
351 if (!cb)
352 goto out;
353
58f6fbe0 354 err = nl_send_auto_complete(nl_handle, msg);
6241fcb1
JM
355 if (err < 0)
356 goto out;
357
358 err = 1;
359
360 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
8e8df255 361 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6241fcb1
JM
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
34068ac3
JM
368 while (err > 0) {
369 int res = nl_recvmsgs(nl_handle, cb);
d3d04831 370 if (res < 0) {
34068ac3
JM
371 wpa_printf(MSG_INFO,
372 "nl80211: %s->nl_recvmsgs failed: %d",
373 __func__, res);
374 }
375 }
6241fcb1
JM
376 out:
377 nl_cb_put(cb);
bbd89bfc
JM
378 if (!valid_handler && valid_data == (void *) -1)
379 nl80211_nlmsg_clear(msg);
6241fcb1
JM
380 nlmsg_free(msg);
381 return err;
382}
383
384
f3407c66
JM
385int 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)
58f6fbe0 389{
481234cf 390 return send_and_recv(drv->global, drv->global->nl, msg,
d6c9aab8 391 valid_handler, valid_data);
58f6fbe0
JM
392}
393
394
97865538
JM
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
d6c9aab8 432static int nl_get_multicast_id(struct nl80211_global *global,
97865538
JM
433 const char *family, const char *group)
434{
435 struct nl_msg *msg;
a862e4a3 436 int ret;
97865538
JM
437 struct family_data res = { group, -ENOENT };
438
439 msg = nlmsg_alloc();
440 if (!msg)
441 return -ENOMEM;
a862e4a3
JM
442 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 }
97865538 448
1033315f 449 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
97865538
JM
450 if (ret == 0)
451 ret = res.id;
97865538
JM
452 return ret;
453}
454
455
f3407c66
JM
456void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
457 struct nl_msg *msg, int flags, uint8_t cmd)
9fb04070 458{
a5a187b0
JM
459 if (TEST_FAIL())
460 return NULL;
335d42b1 461 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
276e2d67 462 0, flags, cmd, 0);
9fb04070
JM
463}
464
465
350acc35
JM
466static 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
07c7757c
JM
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
e32ad281
JB
525struct wiphy_idx_data {
526 int wiphy_idx;
01517c8b 527 enum nl80211_iftype nlmode;
597b94f5 528 u8 *macaddr;
e32ad281
JB
529};
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
01517c8b
JB
544 if (tb[NL80211_ATTR_IFTYPE])
545 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
546
597b94f5
AS
547 if (tb[NL80211_ATTR_MAC] && info->macaddr)
548 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
549 ETH_ALEN);
550
e32ad281
JB
551 return NL_SKIP;
552}
553
554
f3407c66 555int nl80211_get_wiphy_index(struct i802_bss *bss)
e32ad281
JB
556{
557 struct nl_msg *msg;
558 struct wiphy_idx_data data = {
559 .wiphy_idx = -1,
597b94f5 560 .macaddr = NULL,
e32ad281
JB
561 };
562
56f77852 563 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
a862e4a3 564 return -1;
e32ad281
JB
565
566 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
567 return data.wiphy_idx;
e32ad281
JB
568 return -1;
569}
570
571
01517c8b
JB
572static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
573{
574 struct nl_msg *msg;
575 struct wiphy_idx_data data = {
597b94f5
AS
576 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
577 .macaddr = NULL,
01517c8b
JB
578 };
579
56f77852 580 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
a862e4a3 581 return NL80211_IFTYPE_UNSPECIFIED;
01517c8b
JB
582
583 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
584 return data.nlmode;
01517c8b
JB
585 return NL80211_IFTYPE_UNSPECIFIED;
586}
01517c8b
JB
587
588
597b94f5
AS
589static 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
56f77852 596 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
a862e4a3 597 return -1;
597b94f5 598
597b94f5 599 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
597b94f5 600}
597b94f5
AS
601
602
e32ad281
JB
603static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
604 struct nl80211_wiphy_data *w)
605{
606 struct nl_msg *msg;
a862e4a3 607 int ret;
e32ad281
JB
608
609 msg = nlmsg_alloc();
610 if (!msg)
611 return -1;
612
a862e4a3
JM
613 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 }
e32ad281 618
481234cf 619 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
e32ad281
JB
620 if (ret) {
621 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
622 "failed: ret=%d (%s)",
623 ret, strerror(-ret));
e32ad281 624 }
e32ad281
JB
625 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;
34068ac3 632 int res;
e32ad281 633
ee9fc67a 634 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
e32ad281 635
34068ac3 636 res = nl_recvmsgs(handle, w->nl_cb);
d3d04831 637 if (res < 0) {
34068ac3
JM
638 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
639 __func__, res);
640 }
e32ad281
JB
641}
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;
a70cd0db 683 u8 channel;
e32ad281
JB
684
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
a70cd0db
DL
703 /* 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);
715
716 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 }
e32ad281 722
a70cd0db
DL
723 if (nl80211_register_beacons(bss->drv, w)) {
724 nl_destroy_handles(&w->nl_beacons);
725 os_free(w);
726 return NULL;
727 }
e32ad281 728
a70cd0db 729 nl80211_register_eloop_read(&w->nl_beacons,
40a68f33 730 nl80211_recv_beacons, w, 0);
e32ad281
JB
731 }
732
e32ad281
JB
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
a70cd0db 778 if (w->nl_beacons)
40a68f33 779 nl80211_destroy_eloop_handle(&w->nl_beacons, 0);
e32ad281
JB
780
781 nl_cb_put(w->nl_cb);
e32ad281
JB
782 dl_list_del(&w->list);
783 os_free(w);
784}
785
786
45e3fc72
RM
787static 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
3f5285e8
JM
796static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
797{
a2e40bb6
FF
798 struct i802_bss *bss = priv;
799 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
800 if (!drv->associated)
801 return -1;
802 os_memcpy(bssid, drv->bssid, ETH_ALEN);
803 return 0;
3f5285e8
JM
804}
805
806
3f5285e8
JM
807static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
808{
a2e40bb6
FF
809 struct i802_bss *bss = priv;
810 struct wpa_driver_nl80211_data *drv = bss->drv;
fd05d64e
JM
811 if (!drv->associated)
812 return -1;
813 os_memcpy(ssid, drv->ssid, drv->ssid_len);
814 return drv->ssid_len;
3f5285e8
JM
815}
816
817
90a545cc 818static void wpa_driver_nl80211_event_newlink(
45e3fc72
RM
819 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
820 int ifindex, const char *ifname)
3f5285e8
JM
821{
822 union wpa_event_data event;
823
45e3fc72 824 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
90a545cc
JM
825 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;
829 }
830 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;
835 }
836
3f5285e8 837 os_memset(&event, 0, sizeof(event));
45e3fc72 838 event.interface_status.ifindex = ifindex;
90a545cc
JM
839 os_strlcpy(event.interface_status.ifname, ifname,
840 sizeof(event.interface_status.ifname));
841 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
45e3fc72
RM
842 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);
90a545cc
JM
847}
848
849
850static void wpa_driver_nl80211_event_dellink(
45e3fc72
RM
851 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
852 int ifindex, const char *ifname)
90a545cc
JM
853{
854 union wpa_event_data event;
855
45e3fc72 856 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
90a545cc
JM
857 if (drv->if_removed) {
858 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
859 ifname);
860 return;
d1f4942b 861 }
90a545cc
JM
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);
7524cfb1
JM
868 }
869
90a545cc 870 os_memset(&event, 0, sizeof(event));
45e3fc72 871 event.interface_status.ifindex = ifindex;
90a545cc
JM
872 os_strlcpy(event.interface_status.ifname, ifname,
873 sizeof(event.interface_status.ifname));
874 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
45e3fc72
RM
875 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);
3f5285e8
JM
880}
881
882
7524cfb1 883static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
62d680c3 884 u8 *buf, size_t len)
7524cfb1 885{
62d680c3 886 int attrlen, rta_len;
7524cfb1
JM
887 struct rtattr *attr;
888
62d680c3
JM
889 attrlen = len;
890 attr = (struct rtattr *) buf;
7524cfb1
JM
891
892 rta_len = RTA_ALIGN(sizeof(struct rtattr));
893 while (RTA_OK(attr, attrlen)) {
894 if (attr->rta_type == IFLA_IFNAME) {
834ee56f
KP
895 if (os_strcmp(((char *) attr) + rta_len,
896 drv->first_bss->ifname) == 0)
7524cfb1
JM
897 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,
62d680c3 909 int ifindex, u8 *buf, size_t len)
7524cfb1
JM
910{
911 if (drv->ifindex == ifindex)
912 return 1;
913
62d680c3 914 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
f51f54a0 915 nl80211_check_global(drv->global);
7524cfb1
JM
916 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
917 "interface");
5e0c20ff
JM
918 if (wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL) < 0)
919 return -1;
7524cfb1
JM
920 return 1;
921 }
922
923 return 0;
924}
925
926
36d84860 927static struct wpa_driver_nl80211_data *
5e0c20ff
JM
928nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len,
929 int *init_failed)
36d84860
BG
930{
931 struct wpa_driver_nl80211_data *drv;
5e0c20ff
JM
932 int res;
933
934 if (init_failed)
935 *init_failed = 0;
36d84860
BG
936 dl_list_for_each(drv, &global->interfaces,
937 struct wpa_driver_nl80211_data, list) {
5e0c20ff
JM
938 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))
36d84860
BG
947 return drv;
948 }
949 return NULL;
950}
951
952
290834df
BG
953static void nl80211_refresh_mac(struct wpa_driver_nl80211_data *drv,
954 int ifindex)
955{
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);
973 }
974}
975
976
62d680c3
JM
977static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
978 struct ifinfomsg *ifi,
979 u8 *buf, size_t len)
3f5285e8 980{
36d84860
BG
981 struct nl80211_global *global = ctx;
982 struct wpa_driver_nl80211_data *drv;
90a545cc 983 int attrlen;
62d680c3 984 struct rtattr *attr;
97cfcf64 985 u32 brid = 0;
aef85ba2 986 char namebuf[IFNAMSIZ];
90a545cc
JM
987 char ifname[IFNAMSIZ + 1];
988 char extra[100], *pos, *end;
5e0c20ff 989 int init_failed;
3f5285e8 990
90a545cc
JM
991 extra[0] = '\0';
992 pos = extra;
993 end = pos + sizeof(extra);
994 ifname[0] = '\0';
995
996 attrlen = len;
997 attr = (struct rtattr *) buf;
998 while (RTA_OK(attr, attrlen)) {
999 switch (attr->rta_type) {
1000 case IFLA_IFNAME:
1001 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1002 break;
1003 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1004 ifname[RTA_PAYLOAD(attr)] = '\0';
1005 break;
1006 case IFLA_MASTER:
1007 brid = nla_get_u32((struct nlattr *) attr);
1008 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1009 break;
1010 case IFLA_WIRELESS:
1011 pos += os_snprintf(pos, end - pos, " wext");
1012 break;
1013 case IFLA_OPERSTATE:
1014 pos += os_snprintf(pos, end - pos, " operstate=%u",
1015 nla_get_u32((struct nlattr *) attr));
1016 break;
1017 case IFLA_LINKMODE:
1018 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1019 nla_get_u32((struct nlattr *) attr));
1020 break;
1021 }
1022 attr = RTA_NEXT(attr, attrlen);
1023 }
1024 extra[sizeof(extra) - 1] = '\0';
1025
f2e90835
JM
1026 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1027 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1028 ifi->ifi_flags,
3f5285e8
JM
1029 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1030 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1031 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1032 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
a63063b4 1033
5e0c20ff 1034 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, &init_failed);
45e3fc72
RM
1035 if (!drv)
1036 goto event_newlink;
5e0c20ff
JM
1037 if (init_failed)
1038 return; /* do not update interface state */
45e3fc72 1039
a63063b4 1040 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
106fa1e9 1041 namebuf[0] = '\0';
59d24925 1042 if (if_indextoname(ifi->ifi_index, namebuf) &&
106fa1e9 1043 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
290834df
BG
1044 /* Re-read MAC address as it may have changed */
1045 nl80211_refresh_mac(drv, ifi->ifi_index);
59d24925
JM
1046 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1047 "event since interface %s is up", namebuf);
7a94120e 1048 drv->ignore_if_down_event = 0;
59d24925
JM
1049 return;
1050 }
106fa1e9
JM
1051 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1052 namebuf, ifname);
1053 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1054 wpa_printf(MSG_DEBUG,
1055 "nl80211: Not the main interface (%s) - do not indicate interface down",
1056 drv->first_bss->ifname);
1057 } else if (drv->ignore_if_down_event) {
7d9c3698
JM
1058 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1059 "event generated by mode change");
1060 drv->ignore_if_down_event = 0;
1061 } else {
1062 drv->if_disabled = 1;
1063 wpa_supplicant_event(drv->ctx,
1064 EVENT_INTERFACE_DISABLED, NULL);
819f096f
AO
1065
1066 /*
1067 * Try to get drv again, since it may be removed as
1068 * part of the EVENT_INTERFACE_DISABLED handling for
1069 * dynamic interfaces
1070 */
1071 drv = nl80211_find_drv(global, ifi->ifi_index,
5e0c20ff 1072 buf, len, NULL);
819f096f
AO
1073 if (!drv)
1074 return;
7d9c3698 1075 }
a63063b4
JM
1076 }
1077
1078 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
aef85ba2 1079 if (if_indextoname(ifi->ifi_index, namebuf) &&
106fa1e9 1080 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
aef85ba2
JM
1081 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1082 "event since interface %s is down",
1083 namebuf);
834ee56f 1084 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
d1f4942b
JM
1085 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1086 "event since interface %s does not exist",
834ee56f 1087 drv->first_bss->ifname);
d1f4942b
JM
1088 } else if (drv->if_removed) {
1089 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1090 "event since interface %s is marked "
834ee56f 1091 "removed", drv->first_bss->ifname);
aef85ba2 1092 } else {
3e0272ca 1093 /* Re-read MAC address as it may have changed */
290834df 1094 nl80211_refresh_mac(drv, ifi->ifi_index);
3e0272ca 1095
aef85ba2
JM
1096 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1097 drv->if_disabled = 0;
1098 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1099 NULL);
1100 }
a63063b4
JM
1101 }
1102
3f5285e8
JM
1103 /*
1104 * Some drivers send the association event before the operup event--in
1105 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1106 * fails. This will hit us when wpa_supplicant does not need to do
1107 * IEEE 802.1X authentication
1108 */
1109 if (drv->operstate == 1 &&
1110 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
90a545cc
JM
1111 !(ifi->ifi_flags & IFF_RUNNING)) {
1112 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
36d84860 1113 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 1114 -1, IF_OPER_UP);
3f5285e8 1115 }
97cfcf64 1116
45e3fc72 1117event_newlink:
90a545cc 1118 if (ifname[0])
45e3fc72
RM
1119 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1120 ifname);
90a545cc 1121
45e3fc72 1122 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
392dfd37
JM
1123 struct i802_bss *bss;
1124
97cfcf64 1125 /* device has been added to bridge */
40e76396
JM
1126 if (!if_indextoname(brid, namebuf)) {
1127 wpa_printf(MSG_DEBUG,
1128 "nl80211: Could not find bridge ifname for ifindex %u",
1129 brid);
1130 return;
1131 }
97cfcf64
B
1132 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1133 brid, namebuf);
732b1d20 1134 add_ifidx(drv, brid, ifi->ifi_index);
392dfd37
JM
1135
1136 for (bss = drv->first_bss; bss; bss = bss->next) {
1137 if (os_strcmp(ifname, bss->ifname) == 0) {
1138 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1139 break;
1140 }
1141 }
97cfcf64 1142 }
3f5285e8
JM
1143}
1144
1145
62d680c3
JM
1146static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1147 struct ifinfomsg *ifi,
1148 u8 *buf, size_t len)
3f5285e8 1149{
36d84860
BG
1150 struct nl80211_global *global = ctx;
1151 struct wpa_driver_nl80211_data *drv;
90a545cc 1152 int attrlen;
62d680c3 1153 struct rtattr *attr;
97cfcf64 1154 u32 brid = 0;
90a545cc 1155 char ifname[IFNAMSIZ + 1];
f2e90835 1156 char extra[100], *pos, *end;
3f5285e8 1157
f2e90835
JM
1158 extra[0] = '\0';
1159 pos = extra;
1160 end = pos + sizeof(extra);
90a545cc
JM
1161 ifname[0] = '\0';
1162
62d680c3
JM
1163 attrlen = len;
1164 attr = (struct rtattr *) buf;
3f5285e8 1165 while (RTA_OK(attr, attrlen)) {
90a545cc
JM
1166 switch (attr->rta_type) {
1167 case IFLA_IFNAME:
1168 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1169 break;
1170 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1171 ifname[RTA_PAYLOAD(attr)] = '\0';
1172 break;
1173 case IFLA_MASTER:
97cfcf64 1174 brid = nla_get_u32((struct nlattr *) attr);
f2e90835
JM
1175 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1176 break;
1177 case IFLA_OPERSTATE:
1178 pos += os_snprintf(pos, end - pos, " operstate=%u",
1179 nla_get_u32((struct nlattr *) attr));
1180 break;
1181 case IFLA_LINKMODE:
1182 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1183 nla_get_u32((struct nlattr *) attr));
90a545cc
JM
1184 break;
1185 }
3f5285e8
JM
1186 attr = RTA_NEXT(attr, attrlen);
1187 }
f2e90835
JM
1188 extra[sizeof(extra) - 1] = '\0';
1189
1190 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1191 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1192 ifi->ifi_flags,
1193 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1194 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1195 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1196 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
97cfcf64 1197
5e0c20ff 1198 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len, NULL);
90a545cc 1199
45e3fc72 1200 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
97cfcf64
B
1201 /* device has been removed from bridge */
1202 char namebuf[IFNAMSIZ];
40e76396
JM
1203
1204 if (!if_indextoname(brid, namebuf)) {
1205 wpa_printf(MSG_DEBUG,
1206 "nl80211: Could not find bridge ifname for ifindex %u",
1207 brid);
1208 } else {
1209 wpa_printf(MSG_DEBUG,
1210 "nl80211: Remove ifindex %u for bridge %s",
1211 brid, namebuf);
1212 }
732b1d20 1213 del_ifidx(drv, brid, ifi->ifi_index);
97cfcf64 1214 }
45e3fc72
RM
1215
1216 if (ifi->ifi_family != AF_BRIDGE || !brid)
1217 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1218 ifname);
3f5285e8
JM
1219}
1220
1221
a1fce391
JM
1222struct nl80211_get_assoc_freq_arg {
1223 struct wpa_driver_nl80211_data *drv;
1224 unsigned int assoc_freq;
1225 unsigned int ibss_freq;
1226 u8 assoc_bssid[ETH_ALEN];
ed0a4ddc
NW
1227 u8 assoc_ssid[SSID_MAX_LEN];
1228 u8 assoc_ssid_len;
a1fce391
JM
1229};
1230
1231static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1232{
1233 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1234 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1235 struct nlattr *bss[NL80211_BSS_MAX + 1];
1236 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1237 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1238 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
ed0a4ddc 1239 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
a1fce391
JM
1240 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1241 };
1242 struct nl80211_get_assoc_freq_arg *ctx = arg;
1243 enum nl80211_bss_status status;
1244
1245 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1246 genlmsg_attrlen(gnlh, 0), NULL);
1247 if (!tb[NL80211_ATTR_BSS] ||
1248 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1249 bss_policy) ||
1250 !bss[NL80211_BSS_STATUS])
1251 return NL_SKIP;
1252
1253 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1254 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1255 bss[NL80211_BSS_FREQUENCY]) {
1256 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1257 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1258 ctx->assoc_freq);
1259 }
1260 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1261 bss[NL80211_BSS_FREQUENCY]) {
1262 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1263 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1264 ctx->ibss_freq);
1265 }
1266 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1267 bss[NL80211_BSS_BSSID]) {
1268 os_memcpy(ctx->assoc_bssid,
1269 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1270 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1271 MACSTR, MAC2STR(ctx->assoc_bssid));
1272 }
1273
ed0a4ddc
NW
1274 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1275 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1276 const u8 *ie, *ssid;
1277 size_t ie_len;
1278
1279 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1280 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1281 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1282 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1283 ctx->assoc_ssid_len = ssid[1];
1284 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1285 }
1286 }
1287
a1fce391
JM
1288 return NL_SKIP;
1289}
1290
1291
ed0a4ddc
NW
1292int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
1293{
1294 struct nl_msg *msg;
1295 int ret;
1296 struct nl80211_get_assoc_freq_arg arg;
1297
1298 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1299 os_memset(&arg, 0, sizeof(arg));
1300 arg.drv = drv;
1301 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1302 &arg);
1303 if (ret == 0) {
1304 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1305 return arg.assoc_ssid_len;
1306 }
1307 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1308 ret, strerror(-ret));
1309 return ret;
1310}
1311
1312
f3407c66 1313unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
f5a8d422
JM
1314{
1315 struct nl_msg *msg;
1316 int ret;
a1fce391 1317 struct nl80211_get_assoc_freq_arg arg;
f5a8d422 1318
9725b784 1319 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
f5a8d422 1320 os_memset(&arg, 0, sizeof(arg));
f5a8d422 1321 arg.drv = drv;
a1fce391
JM
1322 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1323 &arg);
f5a8d422 1324 if (ret == 0) {
c7caac56
JM
1325 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1326 arg.ibss_freq : arg.assoc_freq;
f5a8d422 1327 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
c7caac56
JM
1328 "associated BSS from scan results: %u MHz", freq);
1329 if (freq)
1330 drv->assoc_freq = freq;
30158a0d 1331 return drv->assoc_freq;
f5a8d422
JM
1332 }
1333 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1334 "(%s)", ret, strerror(-ret));
f5a8d422
JM
1335 return drv->assoc_freq;
1336}
1337
1338
60a972a6
JM
1339static int get_link_signal(struct nl_msg *msg, void *arg)
1340{
1341 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1342 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1343 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1344 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1345 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
95783298 1346 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
74fa78b2 1347 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
60a972a6 1348 };
7ee35bf3
PS
1349 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1350 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1351 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1352 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1353 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1354 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1355 };
1c5c7273 1356 struct wpa_signal_info *sig_change = arg;
60a972a6
JM
1357
1358 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1359 genlmsg_attrlen(gnlh, 0), NULL);
1360 if (!tb[NL80211_ATTR_STA_INFO] ||
1361 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1362 tb[NL80211_ATTR_STA_INFO], policy))
1363 return NL_SKIP;
1364 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1365 return NL_SKIP;
1366
7ee35bf3
PS
1367 sig_change->current_signal =
1368 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1369
95783298
AO
1370 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1371 sig_change->avg_signal =
1372 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1373 else
1374 sig_change->avg_signal = 0;
1375
74fa78b2
JM
1376 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1377 sig_change->avg_beacon_signal =
1378 (s8)
1379 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1380 else
1381 sig_change->avg_beacon_signal = 0;
1382
7ee35bf3
PS
1383 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1384 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1385 sinfo[NL80211_STA_INFO_TX_BITRATE],
1386 rate_policy)) {
1387 sig_change->current_txrate = 0;
1388 } else {
1389 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1390 sig_change->current_txrate =
1391 nla_get_u16(rinfo[
1392 NL80211_RATE_INFO_BITRATE]) * 100;
1393 }
1394 }
1395 }
1396
60a972a6
JM
1397 return NL_SKIP;
1398}
1399
1400
f3407c66
JM
1401int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1402 struct wpa_signal_info *sig)
60a972a6
JM
1403{
1404 struct nl_msg *msg;
1405
7ee35bf3
PS
1406 sig->current_signal = -9999;
1407 sig->current_txrate = 0;
60a972a6 1408
9725b784 1409 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
a862e4a3
JM
1410 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1411 nlmsg_free(msg);
1412 return -ENOBUFS;
1413 }
60a972a6
JM
1414
1415 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
60a972a6
JM
1416}
1417
1418
7ee35bf3
PS
1419static int get_link_noise(struct nl_msg *msg, void *arg)
1420{
1421 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1422 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1423 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1424 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1425 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1426 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1427 };
1c5c7273 1428 struct wpa_signal_info *sig_change = arg;
7ee35bf3
PS
1429
1430 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1431 genlmsg_attrlen(gnlh, 0), NULL);
1432
1433 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1434 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1435 return NL_SKIP;
1436 }
1437
1438 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1439 tb[NL80211_ATTR_SURVEY_INFO],
1440 survey_policy)) {
1441 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1442 "attributes!");
1443 return NL_SKIP;
1444 }
1445
1446 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1447 return NL_SKIP;
1448
1449 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1450 sig_change->frequency)
1451 return NL_SKIP;
1452
1453 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1454 return NL_SKIP;
1455
1456 sig_change->current_noise =
1457 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1458
1459 return NL_SKIP;
1460}
1461
1462
f3407c66
JM
1463int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1464 struct wpa_signal_info *sig_change)
7ee35bf3
PS
1465{
1466 struct nl_msg *msg;
1467
1468 sig_change->current_noise = 9999;
1469 sig_change->frequency = drv->assoc_freq;
1470
9725b784 1471 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
7ee35bf3 1472 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
7ee35bf3
PS
1473}
1474
1475
97865538 1476static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
5582a5d1 1477 void *handle)
97865538 1478{
a4ae123c 1479 struct nl_cb *cb = eloop_ctx;
34068ac3 1480 int res;
97865538 1481
cc2ada86 1482 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
97865538 1483
34068ac3 1484 res = nl_recvmsgs(handle, cb);
d3d04831 1485 if (res < 0) {
34068ac3
JM
1486 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1487 __func__, res);
1488 }
97865538
JM
1489}
1490
1491
6d158490
LR
1492/**
1493 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1494 * @priv: driver_nl80211 private data
1495 * @alpha2_arg: country to which to switch to
1496 * Returns: 0 on success, -1 on failure
1497 *
1498 * This asks nl80211 to set the regulatory domain for given
1499 * country ISO / IEC alpha2.
1500 */
1501static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1502{
a2e40bb6
FF
1503 struct i802_bss *bss = priv;
1504 struct wpa_driver_nl80211_data *drv = bss->drv;
6d158490
LR
1505 char alpha2[3];
1506 struct nl_msg *msg;
1507
1508 msg = nlmsg_alloc();
1509 if (!msg)
e785c2ba 1510 return -ENOMEM;
6d158490
LR
1511
1512 alpha2[0] = alpha2_arg[0];
1513 alpha2[1] = alpha2_arg[1];
1514 alpha2[2] = '\0';
1515
a862e4a3
JM
1516 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1517 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1518 nlmsg_free(msg);
1519 return -EINVAL;
1520 }
6d158490
LR
1521 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1522 return -EINVAL;
1523 return 0;
6d158490
LR
1524}
1525
1526
f0793bf1
JM
1527static int nl80211_get_country(struct nl_msg *msg, void *arg)
1528{
1529 char *alpha2 = arg;
1530 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1531 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1532
1533 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1534 genlmsg_attrlen(gnlh, 0), NULL);
1535 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1536 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1537 return NL_SKIP;
1538 }
1539 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1540 return NL_SKIP;
1541}
1542
1543
1544static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1545{
1546 struct i802_bss *bss = priv;
1547 struct wpa_driver_nl80211_data *drv = bss->drv;
1548 struct nl_msg *msg;
1549 int ret;
1550
1551 msg = nlmsg_alloc();
1552 if (!msg)
1553 return -ENOMEM;
1554
1555 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1556 alpha2[0] = '\0';
1557 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1558 if (!alpha2[0])
1559 ret = -1;
1560
1561 return ret;
1562}
1563
1564
2a7b66f5
BG
1565static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
1566{
d6c9aab8
JB
1567 int ret;
1568
2a7b66f5
BG
1569 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1570 if (global->nl_cb == NULL) {
1571 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1572 "callbacks");
1573 return -1;
1574 }
1575
481234cf
JM
1576 global->nl = nl_create_handle(global->nl_cb, "nl");
1577 if (global->nl == NULL)
d6c9aab8 1578 goto err;
276e2d67 1579
481234cf 1580 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
335d42b1 1581 if (global->nl80211_id < 0) {
276e2d67
BG
1582 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1583 "found");
d6c9aab8 1584 goto err;
276e2d67
BG
1585 }
1586
481234cf
JM
1587 global->nl_event = nl_create_handle(global->nl_cb, "event");
1588 if (global->nl_event == NULL)
d6c9aab8 1589 goto err;
9fff9fdc 1590
d6c9aab8 1591 ret = nl_get_multicast_id(global, "nl80211", "scan");
97865538 1592 if (ret >= 0)
481234cf 1593 ret = nl_socket_add_membership(global->nl_event, ret);
97865538
JM
1594 if (ret < 0) {
1595 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1596 "membership for scan events: %d (%s)",
1597 ret, strerror(-ret));
d6c9aab8 1598 goto err;
97865538 1599 }
c2a04078 1600
d6c9aab8 1601 ret = nl_get_multicast_id(global, "nl80211", "mlme");
c2a04078 1602 if (ret >= 0)
481234cf 1603 ret = nl_socket_add_membership(global->nl_event, ret);
c2a04078
JM
1604 if (ret < 0) {
1605 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1606 "membership for mlme events: %d (%s)",
1607 ret, strerror(-ret));
d6c9aab8 1608 goto err;
c2a04078 1609 }
c2a04078 1610
d6c9aab8 1611 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
33c5deb8 1612 if (ret >= 0)
481234cf 1613 ret = nl_socket_add_membership(global->nl_event, ret);
33c5deb8
JM
1614 if (ret < 0) {
1615 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1616 "membership for regulatory events: %d (%s)",
1617 ret, strerror(-ret));
1618 /* Continue without regulatory events */
1619 }
1620
17b79e65
JM
1621 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1622 if (ret >= 0)
1623 ret = nl_socket_add_membership(global->nl_event, ret);
1624 if (ret < 0) {
1625 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1626 "membership for vendor events: %d (%s)",
1627 ret, strerror(-ret));
1628 /* Continue without vendor events */
1629 }
1630
d6c9aab8
JB
1631 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1632 no_seq_check, NULL);
1633 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1634 process_global_event, global);
1635
5f65e9f7
JB
1636 nl80211_register_eloop_read(&global->nl_event,
1637 wpa_driver_nl80211_event_receive,
40a68f33 1638 global->nl_cb, 0);
d6c9aab8
JB
1639
1640 return 0;
1641
1642err:
1643 nl_destroy_handles(&global->nl_event);
1644 nl_destroy_handles(&global->nl);
1645 nl_cb_put(global->nl_cb);
671a5039 1646 global->nl_cb = NULL;
d6c9aab8
JB
1647 return -1;
1648}
1649
1650
f51f54a0
JM
1651static void nl80211_check_global(struct nl80211_global *global)
1652{
1653 struct nl_handle *handle;
1654 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1655 int ret;
1656 unsigned int i;
1657
1658 /*
1659 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1660 * and all registration having been cleared.
1661 */
1662 handle = (void *) (((intptr_t) global->nl_event) ^
1663 ELOOP_SOCKET_INVALID);
1664
1665 for (i = 0; groups[i]; i++) {
1666 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1667 if (ret >= 0)
1668 ret = nl_socket_add_membership(handle, ret);
1669 if (ret < 0) {
1670 wpa_printf(MSG_INFO,
1671 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1672 groups[i], ret, strerror(-ret));
1673 }
1674 }
1675}
1676
1677
8401a6b0
JM
1678static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1679{
6da504a1
IP
1680 struct wpa_driver_nl80211_data *drv = ctx;
1681
8401a6b0 1682 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
6da504a1 1683
a63063b4 1684 /*
6da504a1
IP
1685 * rtnetlink ifdown handler will report interfaces other than the P2P
1686 * Device interface as disabled.
a63063b4 1687 */
6da504a1
IP
1688 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1689 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
8401a6b0
JM
1690}
1691
1692
1693static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1694{
1695 struct wpa_driver_nl80211_data *drv = ctx;
1696 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
834ee56f 1697 if (i802_set_iface_flags(drv->first_bss, 1)) {
8401a6b0
JM
1698 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1699 "after rfkill unblock");
1700 return;
1701 }
e8dc205f
AO
1702
1703 if (is_p2p_net_interface(drv->nlmode))
1704 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1705
6da504a1
IP
1706 /*
1707 * rtnetlink ifup handler will report interfaces other than the P2P
1708 * Device interface as enabled.
1709 */
1710 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1711 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
8401a6b0
JM
1712}
1713
1714
32ab4855
JB
1715static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1716 void *eloop_ctx,
1717 void *handle)
1718{
1719 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1720 u8 data[2048];
1721 struct msghdr msg;
1722 struct iovec entry;
cad0f50e 1723 u8 control[512];
32ab4855
JB
1724 struct cmsghdr *cmsg;
1725 int res, found_ee = 0, found_wifi = 0, acked = 0;
1726 union wpa_event_data event;
1727
1728 memset(&msg, 0, sizeof(msg));
1729 msg.msg_iov = &entry;
1730 msg.msg_iovlen = 1;
1731 entry.iov_base = data;
1732 entry.iov_len = sizeof(data);
1733 msg.msg_control = &control;
1734 msg.msg_controllen = sizeof(control);
1735
1736 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1737 /* if error or not fitting 802.3 header, return */
1738 if (res < 14)
1739 return;
1740
1741 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1742 {
1743 if (cmsg->cmsg_level == SOL_SOCKET &&
1744 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1745 int *ack;
1746
1747 found_wifi = 1;
1748 ack = (void *)CMSG_DATA(cmsg);
1749 acked = *ack;
1750 }
1751
1752 if (cmsg->cmsg_level == SOL_PACKET &&
1753 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1754 struct sock_extended_err *err =
1755 (struct sock_extended_err *)CMSG_DATA(cmsg);
1756
1757 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1758 found_ee = 1;
1759 }
1760 }
1761
1762 if (!found_ee || !found_wifi)
1763 return;
1764
1765 memset(&event, 0, sizeof(event));
1766 event.eapol_tx_status.dst = data;
1767 event.eapol_tx_status.data = data + 14;
1768 event.eapol_tx_status.data_len = res - 14;
1769 event.eapol_tx_status.ack = acked;
1770 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1771}
1772
1773
cc7a48d1
JB
1774static int nl80211_init_bss(struct i802_bss *bss)
1775{
1776 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1777 if (!bss->nl_cb)
1778 return -1;
1779
1780 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1781 no_seq_check, NULL);
1782 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1783 process_bss_event, bss);
1784
1785 return 0;
1786}
1787
1788
1789static void nl80211_destroy_bss(struct i802_bss *bss)
1790{
1791 nl_cb_put(bss->nl_cb);
1792 bss->nl_cb = NULL;
1793}
1794
1795
0e92fb8f
JB
1796static void
1797wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1798{
1799 struct rfkill_config *rcfg;
1800
1801 if (drv->rfkill)
1802 return;
1803
1804 rcfg = os_zalloc(sizeof(*rcfg));
1805 if (!rcfg)
1806 return;
1807
1808 rcfg->ctx = drv;
1809
1810 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1811 * not associated with a netdev, so use the name of some other interface
1812 * sharing the same wiphy as the P2P Device interface.
1813 *
1814 * Note: This is valid, as a P2P Device interface is always dynamically
1815 * created and is created only once another wpa_s interface was added.
1816 */
1817 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1818 struct nl80211_global *global = drv->global;
1819 struct wpa_driver_nl80211_data *tmp1;
1820
1821 dl_list_for_each(tmp1, &global->interfaces,
1822 struct wpa_driver_nl80211_data, list) {
1823 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1824 !tmp1->rfkill)
1825 continue;
1826
1827 wpa_printf(MSG_DEBUG,
1828 "nl80211: Use (%s) to initialize P2P Device rfkill",
1829 tmp1->first_bss->ifname);
1830 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1831 sizeof(rcfg->ifname));
1832 break;
1833 }
1834 } else {
1835 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1836 sizeof(rcfg->ifname));
1837 }
1838
1839 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1840 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1841 drv->rfkill = rfkill_init(rcfg);
1842 if (!drv->rfkill) {
1843 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1844 os_free(rcfg);
1845 }
1846}
1847
1848
0d547d5f
JM
1849static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1850 void *global_priv, int hostapd,
0ecff8d7
JM
1851 const u8 *set_addr,
1852 const char *driver_params)
9fff9fdc 1853{
9fff9fdc 1854 struct wpa_driver_nl80211_data *drv;
a2e40bb6 1855 struct i802_bss *bss;
9fff9fdc 1856
a5c696ad
JM
1857 if (global_priv == NULL)
1858 return NULL;
9fff9fdc
JM
1859 drv = os_zalloc(sizeof(*drv));
1860 if (drv == NULL)
1861 return NULL;
f2ed8023 1862 drv->global = global_priv;
9fff9fdc 1863 drv->ctx = ctx;
0d547d5f
JM
1864 drv->hostapd = !!hostapd;
1865 drv->eapol_sock = -1;
64ae2447
JM
1866
1867 /*
1868 * There is no driver capability flag for this, so assume it is
1869 * supported and disable this on first attempt to use if the driver
1870 * rejects the command due to missing support.
1871 */
1872 drv->set_rekey_offload = 1;
1873
0d547d5f
JM
1874 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1875 drv->if_indices = drv->default_if_indices;
732b1d20 1876 drv->if_indices_reason = drv->default_if_indices_reason;
834ee56f
KP
1877
1878 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1879 if (!drv->first_bss) {
1880 os_free(drv);
1881 return NULL;
1882 }
1883 bss = drv->first_bss;
a2e40bb6 1884 bss->drv = drv;
a5e1eb20
SE
1885 bss->ctx = ctx;
1886
a2e40bb6 1887 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
9fff9fdc
JM
1888 drv->monitor_ifidx = -1;
1889 drv->monitor_sock = -1;
d12dab4c 1890 drv->eapol_tx_sock = -1;
b1f625e0 1891 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
9fff9fdc 1892
cc7a48d1
JB
1893 if (nl80211_init_bss(bss))
1894 goto failed;
1895
0ecff8d7 1896 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
bbaf0837 1897 goto failed;
7524cfb1 1898
d12dab4c 1899 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
32ab4855
JB
1900 if (drv->eapol_tx_sock < 0)
1901 goto failed;
1902
1903 if (drv->data_tx_status) {
1904 int enabled = 1;
1905
1906 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1907 &enabled, sizeof(enabled)) < 0) {
1908 wpa_printf(MSG_DEBUG,
1909 "nl80211: wifi status sockopt failed\n");
1910 drv->data_tx_status = 0;
a11241fa
JB
1911 if (!drv->use_monitor)
1912 drv->capa.flags &=
1913 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
32ab4855
JB
1914 } else {
1915 eloop_register_read_sock(drv->eapol_tx_sock,
1916 wpa_driver_nl80211_handle_eapol_tx_status,
1917 drv, NULL);
1918 }
1919 }
f10bfc9a 1920
dac12351 1921 if (drv->global) {
f51f54a0 1922 nl80211_check_global(drv->global);
c4bb8817 1923 dl_list_add(&drv->global->interfaces, &drv->list);
dac12351
BG
1924 drv->in_interface_list = 1;
1925 }
c4bb8817 1926
a2e40bb6 1927 return bss;
7524cfb1 1928
bbaf0837 1929failed:
dac12351 1930 wpa_driver_nl80211_deinit(bss);
7524cfb1
JM
1931 return NULL;
1932}
1933
1934
0d547d5f
JM
1935/**
1936 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1937 * @ctx: context to be used when calling wpa_supplicant functions,
1938 * e.g., wpa_supplicant_event()
1939 * @ifname: interface name, e.g., wlan0
1940 * @global_priv: private driver global data from global_init()
1941 * Returns: Pointer to private data, %NULL on failure
1942 */
1943static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1944 void *global_priv)
1945{
0ecff8d7
JM
1946 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1947 NULL);
0d547d5f
JM
1948}
1949
1950
a11241fa 1951static int nl80211_register_frame(struct i802_bss *bss,
5582a5d1 1952 struct nl_handle *nl_handle,
bd94971e 1953 u16 type, const u8 *match, size_t match_len)
58f6fbe0 1954{
a11241fa 1955 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 1956 struct nl_msg *msg;
a862e4a3 1957 int ret;
880de885 1958 char buf[30];
58f6fbe0 1959
880de885
JM
1960 buf[0] = '\0';
1961 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
dedfa440
PF
1962 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1963 type, fc2str(type), nl_handle, buf);
36488c05 1964
56f77852 1965 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
a862e4a3
JM
1966 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1967 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1968 nlmsg_free(msg);
1969 return -1;
1970 }
58f6fbe0 1971
d6c9aab8 1972 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
58f6fbe0 1973 if (ret) {
5582a5d1
JB
1974 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1975 "failed (type=%u): ret=%d (%s)",
1976 type, ret, strerror(-ret));
1977 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
58f6fbe0 1978 match, match_len);
58f6fbe0 1979 }
58f6fbe0
JM
1980 return ret;
1981}
1982
1983
a11241fa
JB
1984static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1985{
481234cf 1986 if (bss->nl_mgmt) {
a11241fa 1987 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
36488c05 1988 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
a11241fa
JB
1989 return -1;
1990 }
1991
ce20a370 1992 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
481234cf 1993 if (bss->nl_mgmt == NULL)
a11241fa
JB
1994 return -1;
1995
a11241fa
JB
1996 return 0;
1997}
1998
1999
5f65e9f7
JB
2000static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
2001{
2002 nl80211_register_eloop_read(&bss->nl_mgmt,
2003 wpa_driver_nl80211_event_receive,
40a68f33 2004 bss->nl_cb, 0);
5f65e9f7
JB
2005}
2006
2007
a11241fa 2008static int nl80211_register_action_frame(struct i802_bss *bss,
bd94971e
JB
2009 const u8 *match, size_t match_len)
2010{
2011 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
481234cf 2012 return nl80211_register_frame(bss, bss->nl_mgmt,
5582a5d1 2013 type, match, match_len);
bd94971e
JB
2014}
2015
2016
40a68f33
SD
2017static int nl80211_init_connect_handle(struct i802_bss *bss)
2018{
2019 if (bss->nl_connect) {
2020 wpa_printf(MSG_DEBUG,
2021 "nl80211: Connect handle already created (nl_connect=%p)",
2022 bss->nl_connect);
2023 return -1;
2024 }
2025
2026 bss->nl_connect = nl_create_handle(bss->nl_cb, "connect");
2027 if (!bss->nl_connect)
2028 return -1;
2029 nl80211_register_eloop_read(&bss->nl_connect,
2030 wpa_driver_nl80211_event_receive,
2031 bss->nl_cb, 1);
2032 return 0;
2033}
2034
2035
a11241fa 2036static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
58f6fbe0 2037{
a11241fa 2038 struct wpa_driver_nl80211_data *drv = bss->drv;
6f06766e 2039 int ret = 0;
a11241fa
JB
2040
2041 if (nl80211_alloc_mgmt_handle(bss))
2042 return -1;
36488c05
JM
2043 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
2044 "handle %p", bss->nl_mgmt);
a11241fa 2045
ba71cb82
SD
2046 if (drv->nlmode == NL80211_IFTYPE_ADHOC ||
2047 ((drv->capa.flags & WPA_DRIVER_FLAGS_SAE) &&
2048 !(drv->capa.flags & WPA_DRIVER_FLAGS_SME))) {
e8d1168b
JB
2049 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2050
2051 /* register for any AUTH message */
2052 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
2053 }
2054
56f5af48
JM
2055#ifdef CONFIG_INTERWORKING
2056 /* QoS Map Configure */
2057 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
6f06766e 2058 ret = -1;
56f5af48 2059#endif /* CONFIG_INTERWORKING */
00b02149 2060#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) || defined(CONFIG_DPP)
046b26a2 2061 /* GAS Initial Request */
a11241fa 2062 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
6f06766e 2063 ret = -1;
046b26a2 2064 /* GAS Initial Response */
a11241fa 2065 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
6f06766e 2066 ret = -1;
18708aad 2067 /* GAS Comeback Request */
a11241fa 2068 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
6f06766e 2069 ret = -1;
18708aad 2070 /* GAS Comeback Response */
a11241fa 2071 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
6f06766e 2072 ret = -1;
c5a64e2d
JM
2073 /* Protected GAS Initial Request */
2074 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2075 ret = -1;
2076 /* Protected GAS Initial Response */
2077 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2078 ret = -1;
2079 /* Protected GAS Comeback Request */
2080 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2081 ret = -1;
2082 /* Protected GAS Comeback Response */
2083 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2084 ret = -1;
00b02149 2085#endif /* CONFIG_P2P || CONFIG_INTERWORKING || CONFIG_DPP */
4fe9fa0d 2086#ifdef CONFIG_P2P
046b26a2 2087 /* P2P Public Action */
a11241fa 2088 if (nl80211_register_action_frame(bss,
046b26a2
JM
2089 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2090 6) < 0)
6f06766e 2091 ret = -1;
046b26a2 2092 /* P2P Action */
a11241fa 2093 if (nl80211_register_action_frame(bss,
046b26a2
JM
2094 (u8 *) "\x7f\x50\x6f\x9a\x09",
2095 5) < 0)
6f06766e 2096 ret = -1;
046b26a2 2097#endif /* CONFIG_P2P */
00b02149
JM
2098#ifdef CONFIG_DPP
2099 /* DPP Public Action */
2100 if (nl80211_register_action_frame(bss,
2101 (u8 *) "\x04\x09\x50\x6f\x9a\x1a",
2102 6) < 0)
2103 ret = -1;
2104#endif /* CONFIG_DPP */
7d878ca7
JM
2105#ifdef CONFIG_IEEE80211W
2106 /* SA Query Response */
a11241fa 2107 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
6f06766e 2108 ret = -1;
7d878ca7 2109#endif /* CONFIG_IEEE80211W */
35287637
AN
2110#ifdef CONFIG_TDLS
2111 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2112 /* TDLS Discovery Response */
aa543c0c 2113 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
35287637 2114 0)
6f06766e 2115 ret = -1;
35287637
AN
2116 }
2117#endif /* CONFIG_TDLS */
46ab9b8c
AN
2118#ifdef CONFIG_FST
2119 /* FST Action frames */
2120 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2121 ret = -1;
2122#endif /* CONFIG_FST */
046b26a2 2123
7b90c16a 2124 /* FT Action frames */
a11241fa 2125 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
6f06766e 2126 ret = -1;
7b90c16a
JM
2127 else
2128 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2129 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2130
71269b37 2131 /* WNM - BSS Transition Management Request */
a11241fa 2132 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
6f06766e 2133 ret = -1;
bd896433
JM
2134 /* WNM-Sleep Mode Response */
2135 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
6f06766e 2136 ret = -1;
a11241fa 2137
95a3ea94
JM
2138#ifdef CONFIG_HS20
2139 /* WNM-Notification */
2140 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
3ee18569 2141 ret = -1;
95a3ea94
JM
2142#endif /* CONFIG_HS20 */
2143
dfa87878
MB
2144 /* WMM-AC ADDTS Response */
2145 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
730a0d16 2146 ret = -1;
dfa87878
MB
2147
2148 /* WMM-AC DELTS */
2149 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
730a0d16 2150 ret = -1;
dfa87878 2151
2526ccd9
AK
2152 /* Radio Measurement - Neighbor Report Response */
2153 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2154 ret = -1;
2155
864b9522
DS
2156 /* Radio Measurement - Radio Measurement Request */
2157 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2158 ret = -1;
2159
7dc03388
AO
2160 /* Radio Measurement - Link Measurement Request */
2161 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2162 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2163 ret = -1;
2164
5f65e9f7
JB
2165 nl80211_mgmt_handle_register_eloop(bss);
2166
6f06766e 2167 return ret;
a11241fa
JB
2168}
2169
2170
afb0550a
BC
2171static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2172{
2173 int ret = 0;
2174
2175 if (nl80211_alloc_mgmt_handle(bss))
2176 return -1;
2177
2178 wpa_printf(MSG_DEBUG,
2179 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2180 bss->nl_mgmt);
2181
2182 /* Auth frames for mesh SAE */
2183 if (nl80211_register_frame(bss, bss->nl_mgmt,
2184 (WLAN_FC_TYPE_MGMT << 2) |
2185 (WLAN_FC_STYPE_AUTH << 4),
2186 NULL, 0) < 0)
2187 ret = -1;
2188
2189 /* Mesh peering open */
2190 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2191 ret = -1;
2192 /* Mesh peering confirm */
2193 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2194 ret = -1;
2195 /* Mesh peering close */
2196 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2197 ret = -1;
2198
2199 nl80211_mgmt_handle_register_eloop(bss);
2200
2201 return ret;
2202}
2203
2204
02bb32c3
JB
2205static int nl80211_register_spurious_class3(struct i802_bss *bss)
2206{
02bb32c3 2207 struct nl_msg *msg;
a862e4a3 2208 int ret;
02bb32c3 2209
13f83980
JM
2210 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2211 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
2212 if (ret) {
2213 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2214 "failed: ret=%d (%s)",
2215 ret, strerror(-ret));
02bb32c3 2216 }
02bb32c3
JB
2217 return ret;
2218}
2219
2220
4d916ed6
KV
2221static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2222{
2223 int ret = 0;
2224
2225 /* Public Action frames */
2226 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2227 ret = -1;
2228 /* RRM Measurement Report */
2229 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2230 ret = -1;
e4ec6bbf
JM
2231 /* RRM Link Measurement Report */
2232 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2233 ret = -1;
4d916ed6
KV
2234 /* RRM Neighbor Report Request */
2235 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2236 ret = -1;
2237 /* FT Action frames */
2238 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2239 ret = -1;
2240#ifdef CONFIG_IEEE80211W
2241 /* SA Query */
2242 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2243 ret = -1;
2244#endif /* CONFIG_IEEE80211W */
2245 /* Protected Dual of Public Action */
2246 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2247 ret = -1;
2248 /* WNM */
2249 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2250 ret = -1;
2251 /* WMM */
2252 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2253 ret = -1;
2254#ifdef CONFIG_FST
2255 /* FST Action frames */
2256 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2257 ret = -1;
2258#endif /* CONFIG_FST */
2259 /* Vendor-specific */
2260 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2261 ret = -1;
2262
2263 return ret;
2264}
2265
2266
a11241fa
JB
2267static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2268{
2269 static const int stypes[] = {
2270 WLAN_FC_STYPE_AUTH,
2271 WLAN_FC_STYPE_ASSOC_REQ,
2272 WLAN_FC_STYPE_REASSOC_REQ,
2273 WLAN_FC_STYPE_DISASSOC,
2274 WLAN_FC_STYPE_DEAUTH,
a11241fa
JB
2275 WLAN_FC_STYPE_PROBE_REQ,
2276/* Beacon doesn't work as mac80211 doesn't currently allow
2277 * it, but it wouldn't really be the right thing anyway as
2278 * it isn't per interface ... maybe just dump the scan
2279 * results periodically for OLBC?
2280 */
0e80ea2c 2281 /* WLAN_FC_STYPE_BEACON, */
a11241fa
JB
2282 };
2283 unsigned int i;
2284
2285 if (nl80211_alloc_mgmt_handle(bss))
71269b37 2286 return -1;
36488c05
JM
2287 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2288 "handle %p", bss->nl_mgmt);
71269b37 2289
e7ecab4a 2290 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
481234cf 2291 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
2292 (WLAN_FC_TYPE_MGMT << 2) |
2293 (stypes[i] << 4),
2294 NULL, 0) < 0) {
2295 goto out_err;
2296 }
2297 }
2298
4d916ed6
KV
2299 if (nl80211_action_subscribe_ap(bss))
2300 goto out_err;
2301
02bb32c3
JB
2302 if (nl80211_register_spurious_class3(bss))
2303 goto out_err;
2304
5f65e9f7 2305 nl80211_mgmt_handle_register_eloop(bss);
58f6fbe0 2306 return 0;
a11241fa
JB
2307
2308out_err:
a11241fa
JB
2309 nl_destroy_handles(&bss->nl_mgmt);
2310 return -1;
2311}
2312
2313
a6cc0602
JM
2314static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2315{
2316 if (nl80211_alloc_mgmt_handle(bss))
2317 return -1;
2318 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2319 "handle %p (device SME)", bss->nl_mgmt);
2320
4d916ed6 2321 if (nl80211_action_subscribe_ap(bss))
a6cc0602
JM
2322 goto out_err;
2323
5f65e9f7 2324 nl80211_mgmt_handle_register_eloop(bss);
a6cc0602
JM
2325 return 0;
2326
2327out_err:
a6cc0602
JM
2328 nl_destroy_handles(&bss->nl_mgmt);
2329 return -1;
2330}
2331
2332
36488c05 2333static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 2334{
481234cf 2335 if (bss->nl_mgmt == NULL)
a11241fa 2336 return;
36488c05
JM
2337 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2338 "(%s)", bss->nl_mgmt, reason);
40a68f33 2339 nl80211_destroy_eloop_handle(&bss->nl_mgmt, 0);
7813b7c3
MH
2340
2341 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
2342}
2343
2344
8401a6b0
JM
2345static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2346{
2347 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2348}
2349
2350
eb4582f2
AS
2351static void nl80211_del_p2pdev(struct i802_bss *bss)
2352{
eb4582f2
AS
2353 struct nl_msg *msg;
2354 int ret;
2355
a3249fdf
JM
2356 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2357 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
eb4582f2
AS
2358
2359 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2360 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 2361 strerror(-ret));
eb4582f2
AS
2362}
2363
2364
eb4582f2 2365static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
f632e483 2366{
f632e483 2367 struct nl_msg *msg;
a862e4a3 2368 int ret;
f632e483 2369
a3249fdf
JM
2370 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2371 NL80211_CMD_STOP_P2P_DEVICE);
2372 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
f632e483 2373
eb4582f2
AS
2374 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2375 start ? "Start" : "Stop",
2376 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 2377 strerror(-ret));
f632e483
AS
2378 return ret;
2379}
f632e483
AS
2380
2381
91724d6f
AS
2382static int i802_set_iface_flags(struct i802_bss *bss, int up)
2383{
2384 enum nl80211_iftype nlmode;
2385
2386 nlmode = nl80211_get_ifmode(bss);
2387 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2388 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2389 bss->ifname, up);
2390 }
2391
2392 /* P2P Device has start/stop which is equivalent */
2393 return nl80211_set_p2pdev(bss, up);
2394}
2395
2396
1db718b3
JM
2397#ifdef CONFIG_TESTING_OPTIONS
2398static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2399{
2400 /* struct wpa_driver_nl80211_data *drv = arg; */
2401 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2402 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2403
2404
2405 wpa_printf(MSG_DEBUG,
2406 "nl80211: QCA vendor test command response received");
2407
2408 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2409 genlmsg_attrlen(gnlh, 0), NULL);
2410 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2411 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2412 return NL_SKIP;
2413 }
2414
2415 wpa_hexdump(MSG_DEBUG,
2416 "nl80211: Received QCA vendor test command response",
2417 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2418 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2419
2420 return NL_SKIP;
2421}
2422#endif /* CONFIG_TESTING_OPTIONS */
2423
2424
2425static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2426{
2427#ifdef CONFIG_TESTING_OPTIONS
2428 struct nl_msg *msg;
2429 struct nlattr *params;
2430 int ret;
2431
2432 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2433 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2434 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2435 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2436 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2437 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2438 nlmsg_free(msg);
2439 return;
2440 }
2441 nla_nest_end(msg, params);
2442
2443 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2444 wpa_printf(MSG_DEBUG,
2445 "nl80211: QCA vendor test command returned %d (%s)",
2446 ret, strerror(-ret));
2447#endif /* CONFIG_TESTING_OPTIONS */
2448}
2449
2450
362f781e 2451static int
0d547d5f 2452wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
0ecff8d7
JM
2453 const u8 *set_addr, int first,
2454 const char *driver_params)
7524cfb1 2455{
834ee56f 2456 struct i802_bss *bss = drv->first_bss;
8401a6b0 2457 int send_rfkill_event = 0;
0d547d5f 2458 enum nl80211_iftype nlmode;
a2e40bb6
FF
2459
2460 drv->ifindex = if_nametoindex(bss->ifname);
f632e483
AS
2461 bss->ifindex = drv->ifindex;
2462 bss->wdev_id = drv->global->if_add_wdevid;
2463 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2464
60b13c20
IP
2465 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2466 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
f632e483
AS
2467 drv->global->if_add_wdevid_set = 0;
2468
bf144cf6
AP
2469 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2470 bss->static_ap = 1;
2471
ef3866ab
IP
2472 if (first &&
2473 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2474 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2475 drv->start_iface_up = 1;
2476
f632e483
AS
2477 if (wpa_driver_nl80211_capa(drv))
2478 return -1;
a87c9d96 2479
0ecff8d7
JM
2480 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2481 return -1;
2482
5fbcb45d
AS
2483 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2484 bss->ifname, drv->phyname);
2485
0d547d5f
JM
2486 if (set_addr &&
2487 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2488 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2489 set_addr)))
2490 return -1;
2491
49b4b205
JM
2492 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2493 drv->start_mode_ap = 1;
2494
bf144cf6 2495 if (drv->hostapd || bss->static_ap)
0d547d5f 2496 nlmode = NL80211_IFTYPE_AP;
4fe726e2
FF
2497 else if (bss->if_dynamic ||
2498 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
8e12685c 2499 nlmode = nl80211_get_ifmode(bss);
0d547d5f
JM
2500 else
2501 nlmode = NL80211_IFTYPE_STATION;
f632e483 2502
8e12685c 2503 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
0d547d5f 2504 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
8e12685c 2505 return -1;
a87c9d96
JM
2506 }
2507
8c06db70 2508 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
597b94f5 2509 nl80211_get_macaddr(bss);
f632e483 2510
0e92fb8f
JB
2511 wpa_driver_nl80211_drv_init_rfkill(drv);
2512
8c06db70
MB
2513 if (!rfkill_is_blocked(drv->rfkill)) {
2514 int ret = i802_set_iface_flags(bss, 1);
2515 if (ret) {
8401a6b0
JM
2516 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2517 "interface '%s' UP", bss->ifname);
8c06db70 2518 return ret;
8401a6b0 2519 }
e8dc205f
AO
2520
2521 if (is_p2p_net_interface(nlmode))
2522 nl80211_disable_11b_rates(bss->drv,
2523 bss->drv->ifindex, 1);
2524
8c06db70
MB
2525 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2526 return ret;
2527 } else {
2528 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2529 "interface '%s' due to rfkill", bss->ifname);
6da504a1
IP
2530 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2531 drv->if_disabled = 1;
2532
8c06db70 2533 send_rfkill_event = 1;
362f781e 2534 }
3f5285e8 2535
6da504a1 2536 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
0d547d5f
JM
2537 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2538 1, IF_OPER_DORMANT);
362f781e 2539
6da504a1
IP
2540 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2541 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2542 bss->addr))
2543 return -1;
2544 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2545 }
f2ed8023 2546
8401a6b0
JM
2547 if (send_rfkill_event) {
2548 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2549 drv, drv->ctx);
2550 }
2551
1db718b3
JM
2552 if (drv->vendor_cmd_test_avail)
2553 qca_vendor_test(drv);
2554
40a68f33
SD
2555 nl80211_init_connect_handle(bss);
2556
362f781e 2557 return 0;
3f5285e8
JM
2558}
2559
2560
a70cd0db 2561static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
8a27af5c
JM
2562{
2563 struct nl_msg *msg;
a70cd0db 2564 struct wpa_driver_nl80211_data *drv = bss->drv;
8a27af5c 2565
08e55ebb
JM
2566 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2567 drv->ifindex);
a70cd0db 2568 nl80211_put_wiphy_data_ap(bss);
9725b784 2569 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
8a27af5c 2570 return send_and_recv_msgs(drv, msg, NULL, NULL);
8a27af5c 2571}
8a27af5c
JM
2572
2573
3f5285e8 2574/**
7e5ba1b9 2575 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
9ebce9c5 2576 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
2577 *
2578 * Shut down driver interface and processing of driver events. Free
2579 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2580 */
9ebce9c5 2581static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
3f5285e8 2582{
a2e40bb6 2583 struct wpa_driver_nl80211_data *drv = bss->drv;
cc9a2575 2584 unsigned int i;
3f5285e8 2585
3e208481
JM
2586 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2587 bss->ifname, drv->disabled_11b_rates);
2588
873d0fcf 2589 bss->in_deinit = 1;
32ab4855
JB
2590 if (drv->data_tx_status)
2591 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
2592 if (drv->eapol_tx_sock >= 0)
2593 close(drv->eapol_tx_sock);
f10bfc9a 2594
481234cf 2595 if (bss->nl_preq)
5582a5d1 2596 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 2597 if (bss->added_if_into_bridge) {
c81eff1a
BG
2598 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2599 bss->ifname) < 0)
94627f6c
JM
2600 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2601 "interface %s from bridge %s: %s",
e17a2477 2602 bss->ifname, bss->brname, strerror(errno));
97ed9a06 2603 if (drv->rtnl_sk)
ca3c6b4d 2604 nl80211_handle_destroy(drv->rtnl_sk);
94627f6c 2605 }
e17a2477 2606 if (bss->added_bridge) {
f2535da8
JM
2607 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2608 0) < 0)
2609 wpa_printf(MSG_INFO,
2610 "nl80211: Could not set bridge %s down",
2611 bss->brname);
c81eff1a 2612 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
2613 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2614 "bridge %s: %s",
e17a2477 2615 bss->brname, strerror(errno));
94627f6c
JM
2616 }
2617
460456f8 2618 nl80211_remove_monitor_interface(drv);
8a27af5c 2619
b1f625e0 2620 if (is_ap_interface(drv->nlmode))
a70cd0db 2621 wpa_driver_nl80211_del_beacon(bss);
0915d02c 2622
bbaf0837
JM
2623 if (drv->eapol_sock >= 0) {
2624 eloop_unregister_read_sock(drv->eapol_sock);
2625 close(drv->eapol_sock);
2626 }
2627
2628 if (drv->if_indices != drv->default_if_indices)
2629 os_free(drv->if_indices);
3f5285e8 2630
732b1d20
MB
2631 if (drv->if_indices_reason != drv->default_if_indices_reason)
2632 os_free(drv->if_indices_reason);
2633
b3af99d2 2634 if (drv->disabled_11b_rates)
4e5cb1a3
JM
2635 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2636
36d84860
BG
2637 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2638 IF_OPER_UP);
e390df05 2639 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
8401a6b0 2640 rfkill_deinit(drv->rfkill);
3f5285e8 2641
bbaf0837
JM
2642 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2643
146fa9b3
JM
2644 if (!drv->start_iface_up)
2645 (void) i802_set_iface_flags(bss, 0);
fee354c7
JM
2646
2647 if (drv->addr_changed) {
93da0498
JM
2648 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2649 0) < 0) {
2650 wpa_printf(MSG_DEBUG,
2651 "nl80211: Could not set interface down to restore permanent MAC address");
2652 }
fee354c7
JM
2653 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2654 drv->perm_addr) < 0) {
2655 wpa_printf(MSG_DEBUG,
2656 "nl80211: Could not restore permanent MAC address");
2657 }
2658 }
2659
8e12685c 2660 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
49b4b205
JM
2661 if (!drv->hostapd || !drv->start_mode_ap)
2662 wpa_driver_nl80211_set_mode(bss,
2663 NL80211_IFTYPE_STATION);
b378c41f 2664 nl80211_mgmt_unsubscribe(bss, "deinit");
8e12685c
AS
2665 } else {
2666 nl80211_mgmt_unsubscribe(bss, "deinit");
eb4582f2 2667 nl80211_del_p2pdev(bss);
8e12685c 2668 }
3f5285e8 2669
40a68f33
SD
2670 if (bss->nl_connect)
2671 nl80211_destroy_eloop_handle(&bss->nl_connect, 1);
2672
834ee56f 2673 nl80211_destroy_bss(drv->first_bss);
cc7a48d1 2674
3812464c
JM
2675 os_free(drv->filter_ssids);
2676
536fd62d
JM
2677 os_free(drv->auth_ie);
2678
dac12351 2679 if (drv->in_interface_list)
f2ed8023
JM
2680 dl_list_del(&drv->list);
2681
8cd6b7bc
JB
2682 os_free(drv->extended_capa);
2683 os_free(drv->extended_capa_mask);
cc9a2575
KV
2684 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2685 os_free(drv->iface_ext_capa[i].ext_capa);
2686 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2687 }
834ee56f 2688 os_free(drv->first_bss);
3f5285e8
JM
2689 os_free(drv);
2690}
2691
2692
de4ed4a8
JM
2693static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2694{
2695 switch (alg) {
2696 case WPA_ALG_WEP:
2697 if (key_len == 5)
a042e39a
JM
2698 return RSN_CIPHER_SUITE_WEP40;
2699 return RSN_CIPHER_SUITE_WEP104;
de4ed4a8 2700 case WPA_ALG_TKIP:
a042e39a 2701 return RSN_CIPHER_SUITE_TKIP;
de4ed4a8 2702 case WPA_ALG_CCMP:
a042e39a 2703 return RSN_CIPHER_SUITE_CCMP;
de4ed4a8 2704 case WPA_ALG_GCMP:
a042e39a 2705 return RSN_CIPHER_SUITE_GCMP;
de4ed4a8 2706 case WPA_ALG_CCMP_256:
a042e39a 2707 return RSN_CIPHER_SUITE_CCMP_256;
de4ed4a8 2708 case WPA_ALG_GCMP_256:
a042e39a 2709 return RSN_CIPHER_SUITE_GCMP_256;
de4ed4a8 2710 case WPA_ALG_IGTK:
a042e39a 2711 return RSN_CIPHER_SUITE_AES_128_CMAC;
de4ed4a8 2712 case WPA_ALG_BIP_GMAC_128:
a042e39a 2713 return RSN_CIPHER_SUITE_BIP_GMAC_128;
de4ed4a8 2714 case WPA_ALG_BIP_GMAC_256:
a042e39a 2715 return RSN_CIPHER_SUITE_BIP_GMAC_256;
de4ed4a8 2716 case WPA_ALG_BIP_CMAC_256:
a042e39a 2717 return RSN_CIPHER_SUITE_BIP_CMAC_256;
de4ed4a8 2718 case WPA_ALG_SMS4:
a042e39a 2719 return RSN_CIPHER_SUITE_SMS4;
de4ed4a8 2720 case WPA_ALG_KRK:
a042e39a 2721 return RSN_CIPHER_SUITE_KRK;
de4ed4a8
JM
2722 case WPA_ALG_NONE:
2723 case WPA_ALG_PMK:
2724 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2725 alg);
2726 return 0;
2727 }
2728
2729 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2730 alg);
2731 return 0;
2732}
2733
2734
2735static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2736{
2737 switch (cipher) {
2738 case WPA_CIPHER_CCMP_256:
a042e39a 2739 return RSN_CIPHER_SUITE_CCMP_256;
de4ed4a8 2740 case WPA_CIPHER_GCMP_256:
a042e39a 2741 return RSN_CIPHER_SUITE_GCMP_256;
de4ed4a8 2742 case WPA_CIPHER_CCMP:
a042e39a 2743 return RSN_CIPHER_SUITE_CCMP;
de4ed4a8 2744 case WPA_CIPHER_GCMP:
a042e39a 2745 return RSN_CIPHER_SUITE_GCMP;
de4ed4a8 2746 case WPA_CIPHER_TKIP:
a042e39a 2747 return RSN_CIPHER_SUITE_TKIP;
de4ed4a8 2748 case WPA_CIPHER_WEP104:
a042e39a 2749 return RSN_CIPHER_SUITE_WEP104;
de4ed4a8 2750 case WPA_CIPHER_WEP40:
a042e39a 2751 return RSN_CIPHER_SUITE_WEP40;
ae6f9272 2752 case WPA_CIPHER_GTK_NOT_USED:
a042e39a 2753 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
de4ed4a8
JM
2754 }
2755
2756 return 0;
2757}
2758
2759
2760static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2761 int max_suites)
2762{
2763 int num_suites = 0;
2764
2765 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
a042e39a 2766 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
de4ed4a8 2767 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
a042e39a 2768 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
de4ed4a8 2769 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
a042e39a 2770 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
de4ed4a8 2771 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
a042e39a 2772 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
de4ed4a8 2773 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
a042e39a 2774 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
de4ed4a8 2775 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
a042e39a 2776 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
de4ed4a8 2777 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
a042e39a 2778 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
de4ed4a8
JM
2779
2780 return num_suites;
2781}
2782
2783
b658547d 2784#ifdef CONFIG_DRIVER_NL80211_QCA
b41f2684
CL
2785static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2786 const u8 *key, size_t key_len)
2787{
2788 struct nl_msg *msg;
a862e4a3 2789 int ret;
b41f2684 2790
15badebd 2791 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
b41f2684
CL
2792 return 0;
2793
9725b784 2794 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
2795 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2796 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2797 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2798 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
bbd89bfc 2799 nl80211_nlmsg_clear(msg);
a862e4a3
JM
2800 nlmsg_free(msg);
2801 return -1;
2802 }
bbd89bfc 2803 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
b41f2684
CL
2804 if (ret) {
2805 wpa_printf(MSG_DEBUG,
2806 "nl80211: Key management set key failed: ret=%d (%s)",
2807 ret, strerror(-ret));
2808 }
2809
b41f2684
CL
2810 return ret;
2811}
b658547d 2812#endif /* CONFIG_DRIVER_NL80211_QCA */
b41f2684
CL
2813
2814
05fc7c68
AS
2815static int nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
2816 const u8 *key, size_t key_len,
2817 const u8 *addr)
2818{
2819 struct nl_msg *msg = NULL;
2820 int ret;
2821
2822 /*
2823 * If the authenticator address is not set, assume it is
2824 * the current BSSID.
2825 */
2826 if (!addr && drv->associated)
2827 addr = drv->bssid;
2828 else if (!addr)
2829 return -1;
2830
2831 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
2832 MAC2STR(addr));
2833 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
2834 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
2835 if (!msg ||
2836 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
2837 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
2838 nl80211_nlmsg_clear(msg);
2839 nlmsg_free(msg);
2840 return -ENOBUFS;
2841 }
2842
2843 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2844 if (ret) {
2845 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
2846 ret, strerror(-ret));
2847 }
2848
2849 return ret;
2850}
2851
2852
9ebce9c5 2853static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
71934751
JM
2854 enum wpa_alg alg, const u8 *addr,
2855 int key_idx, int set_tx,
642187d6
JM
2856 const u8 *seq, size_t seq_len,
2857 const u8 *key, size_t key_len)
3f5285e8 2858{
a2e40bb6 2859 struct wpa_driver_nl80211_data *drv = bss->drv;
e472e1b4 2860 int ifindex;
34651767 2861 struct nl_msg *msg = NULL;
1ad1cdc2 2862 int ret;
dc01de8a 2863 int tdls = 0;
3f5285e8 2864
e472e1b4
AS
2865 /* Ignore for P2P Device */
2866 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2867 return 0;
2868
2869 ifindex = if_nametoindex(ifname);
8393e1a0 2870 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
1ad1cdc2 2871 "set_tx=%d seq_len=%lu key_len=%lu",
8393e1a0 2872 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
3f5285e8 2873 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185 2874#ifdef CONFIG_TDLS
dc01de8a 2875 if (key_idx == -1) {
8c66e185 2876 key_idx = 0;
dc01de8a
JM
2877 tdls = 1;
2878 }
8c66e185 2879#endif /* CONFIG_TDLS */
3f5285e8 2880
b658547d 2881#ifdef CONFIG_DRIVER_NL80211_QCA
15badebd
CL
2882 if (alg == WPA_ALG_PMK &&
2883 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
b41f2684
CL
2884 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2885 __func__);
2886 ret = issue_key_mgmt_set_key(drv, key, key_len);
2887 return ret;
2888 }
b658547d 2889#endif /* CONFIG_DRIVER_NL80211_QCA */
b41f2684 2890
05fc7c68
AS
2891 if (alg == WPA_ALG_PMK &&
2892 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2893 return nl80211_set_pmk(drv, key, key_len, addr);
2894
3f5285e8 2895 if (alg == WPA_ALG_NONE) {
95376e1a
JM
2896 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2897 if (!msg)
2898 return -ENOBUFS;
3f5285e8 2899 } else {
34651767
JM
2900 u32 suite;
2901
2902 suite = wpa_alg_to_cipher_suite(alg, key_len);
2903 if (!suite)
2904 goto fail;
95376e1a
JM
2905 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2906 if (!msg ||
a862e4a3 2907 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
34651767 2908 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
a862e4a3 2909 goto fail;
e6ef73f1 2910 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
3f5285e8
JM
2911 }
2912
e6ef73f1 2913 if (seq && seq_len) {
a862e4a3
JM
2914 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2915 goto fail;
e6ef73f1
JM
2916 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2917 }
1ad1cdc2 2918
0382097e 2919 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8 2920 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
a862e4a3
JM
2921 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2922 goto fail;
89c38e32
JM
2923
2924 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2925 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
a862e4a3
JM
2926 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2927 NL80211_KEYTYPE_GROUP))
2928 goto fail;
89c38e32 2929 }
60ea8187 2930 } else if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
2931 struct nlattr *types;
2932
60ea8187 2933 wpa_printf(MSG_DEBUG, " broadcast key");
8970bae8
JB
2934
2935 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
a862e4a3
JM
2936 if (!types ||
2937 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2938 goto fail;
8970bae8 2939 nla_nest_end(msg, types);
3f5285e8 2940 }
95376e1a 2941 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
a862e4a3 2942 goto fail;
3f5285e8 2943
bbd89bfc 2944 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
15664ad0 2945 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
2946 ret = 0;
2947 if (ret)
2948 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2949 ret, strerror(-ret));
3f5285e8 2950
1ad1cdc2
JM
2951 /*
2952 * If we failed or don't need to set the default TX key (below),
2953 * we're done here.
2954 */
dc01de8a 2955 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
1ad1cdc2 2956 return ret;
b1f625e0 2957 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 2958 !is_broadcast_ether_addr(addr))
de12717a 2959 return ret;
3f5285e8 2960
95376e1a
JM
2961 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2962 if (!msg ||
a862e4a3 2963 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
86f9b1c7
JM
2964 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2965 alg == WPA_ALG_BIP_GMAC_128 ||
2966 alg == WPA_ALG_BIP_GMAC_256 ||
2967 alg == WPA_ALG_BIP_CMAC_256) ?
a862e4a3
JM
2968 NL80211_ATTR_KEY_DEFAULT_MGMT :
2969 NL80211_ATTR_KEY_DEFAULT))
2970 goto fail;
60ea8187 2971 if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
2972 struct nlattr *types;
2973
2974 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
a862e4a3
JM
2975 if (!types ||
2976 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2977 goto fail;
8970bae8 2978 nla_nest_end(msg, types);
60ea8187 2979 } else if (addr) {
8970bae8
JB
2980 struct nlattr *types;
2981
2982 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
a862e4a3
JM
2983 if (!types ||
2984 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2985 goto fail;
8970bae8 2986 nla_nest_end(msg, types);
60ea8187 2987 }
3f5285e8 2988
1ad1cdc2
JM
2989 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2990 if (ret == -ENOENT)
2991 ret = 0;
2992 if (ret)
2993 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2994 "err=%d %s)", ret, strerror(-ret));
2995 return ret;
3f5285e8 2996
a862e4a3 2997fail:
bbd89bfc 2998 nl80211_nlmsg_clear(msg);
5883168a 2999 nlmsg_free(msg);
6241fcb1 3000 return -ENOBUFS;
3f5285e8
JM
3001}
3002
3003
71934751 3004static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
3005 int key_idx, int defkey,
3006 const u8 *seq, size_t seq_len,
3007 const u8 *key, size_t key_len)
3008{
3009 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
34651767
JM
3010 u32 suite;
3011
0194fedb
JB
3012 if (!key_attr)
3013 return -1;
3014
34651767
JM
3015 suite = wpa_alg_to_cipher_suite(alg, key_len);
3016 if (!suite)
3017 return -1;
3018
a862e4a3
JM
3019 if (defkey && alg == WPA_ALG_IGTK) {
3020 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3021 return -1;
3022 } else if (defkey) {
3023 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3024 return -1;
3025 }
0194fedb 3026
a862e4a3 3027 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
34651767 3028 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
a862e4a3
JM
3029 (seq && seq_len &&
3030 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3031 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3032 return -1;
0194fedb
JB
3033
3034 nla_nest_end(msg, key_attr);
3035
3036 return 0;
0194fedb
JB
3037}
3038
c811d5bc 3039
cfaab580
ZY
3040static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3041 struct nl_msg *msg)
3042{
3043 int i, privacy = 0;
3044 struct nlattr *nl_keys, *nl_key;
3045
3046 for (i = 0; i < 4; i++) {
3047 if (!params->wep_key[i])
3048 continue;
3049 privacy = 1;
3050 break;
3051 }
ce04af5a
JM
3052 if (params->wps == WPS_MODE_PRIVACY)
3053 privacy = 1;
3054 if (params->pairwise_suite &&
3055 params->pairwise_suite != WPA_CIPHER_NONE)
3056 privacy = 1;
3057
cfaab580
ZY
3058 if (!privacy)
3059 return 0;
3060
a862e4a3
JM
3061 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3062 return -ENOBUFS;
cfaab580
ZY
3063
3064 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3065 if (!nl_keys)
a862e4a3 3066 return -ENOBUFS;
cfaab580
ZY
3067
3068 for (i = 0; i < 4; i++) {
3069 if (!params->wep_key[i])
3070 continue;
3071
3072 nl_key = nla_nest_start(msg, i);
a862e4a3
JM
3073 if (!nl_key ||
3074 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3075 params->wep_key[i]) ||
3076 nla_put_u32(msg, NL80211_KEY_CIPHER,
3077 params->wep_key_len[i] == 5 ?
a042e39a
JM
3078 RSN_CIPHER_SUITE_WEP40 :
3079 RSN_CIPHER_SUITE_WEP104) ||
a862e4a3
JM
3080 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3081 (i == params->wep_tx_keyidx &&
3082 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3083 return -ENOBUFS;
cfaab580
ZY
3084
3085 nla_nest_end(msg, nl_key);
3086 }
3087 nla_nest_end(msg, nl_keys);
3088
3089 return 0;
cfaab580
ZY
3090}
3091
3092
477af8f8
JM
3093int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3094 const u8 *addr, int cmd, u16 reason_code,
3095 int local_state_change)
c2a04078 3096{
a862e4a3 3097 int ret;
c2a04078
JM
3098 struct nl_msg *msg;
3099
9725b784 3100 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
a862e4a3
JM
3101 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3102 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3103 (local_state_change &&
3104 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3105 nlmsg_free(msg);
3106 return -1;
3107 }
c2a04078
JM
3108
3109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
c2a04078 3110 if (ret) {
3b7ea880
BG
3111 wpa_dbg(drv->ctx, MSG_DEBUG,
3112 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3113 reason_code, ret, strerror(-ret));
c2a04078 3114 }
c2a04078
JM
3115 return ret;
3116}
3f5285e8
JM
3117
3118
cfaab580 3119static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
817762d9 3120 int reason_code)
cfaab580 3121{
3f53c006
JJ
3122 int ret;
3123
817762d9 3124 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
add9b7a4 3125 nl80211_mark_disconnected(drv);
817762d9 3126 /* Disconnect command doesn't need BSSID - it uses cached value */
3f53c006
JJ
3127 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3128 reason_code, 0);
3129 /*
3130 * For locally generated disconnect, supplicant already generates a
3131 * DEAUTH event, so ignore the event from NL80211.
3132 */
3133 drv->ignore_next_local_disconnect = ret == 0;
3134
3135 return ret;
cfaab580
ZY
3136}
3137
3138
9ebce9c5
JM
3139static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3140 const u8 *addr, int reason_code)
3f5285e8 3141{
a2e40bb6 3142 struct wpa_driver_nl80211_data *drv = bss->drv;
d6a36f39 3143 int ret;
9392c9be
AS
3144
3145 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3146 nl80211_mark_disconnected(drv);
666e508c 3147 return nl80211_leave_ibss(drv, 1);
9392c9be 3148 }
cfaab580 3149 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
817762d9 3150 return wpa_driver_nl80211_disconnect(drv, reason_code);
2e75a2b3
JM
3151 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3152 __func__, MAC2STR(addr), reason_code);
add9b7a4 3153 nl80211_mark_disconnected(drv);
d6a36f39
JM
3154 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3155 reason_code, 0);
3156 /*
3157 * For locally generated deauthenticate, supplicant already generates a
3158 * DEAUTH event, so ignore the event from NL80211.
3159 */
3160 drv->ignore_next_local_deauth = ret == 0;
3161 return ret;
3f5285e8
JM
3162}
3163
3164
536fd62d
JM
3165static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3166 struct wpa_driver_auth_params *params)
3167{
3168 int i;
3169
3170 drv->auth_freq = params->freq;
3171 drv->auth_alg = params->auth_alg;
3172 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3173 drv->auth_local_state_change = params->local_state_change;
3174 drv->auth_p2p = params->p2p;
3175
3176 if (params->bssid)
3177 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3178 else
3179 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3180
3181 if (params->ssid) {
3182 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3183 drv->auth_ssid_len = params->ssid_len;
3184 } else
3185 drv->auth_ssid_len = 0;
3186
3187
3188 os_free(drv->auth_ie);
3189 drv->auth_ie = NULL;
3190 drv->auth_ie_len = 0;
3191 if (params->ie) {
3192 drv->auth_ie = os_malloc(params->ie_len);
3193 if (drv->auth_ie) {
3194 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3195 drv->auth_ie_len = params->ie_len;
3196 }
3197 }
3198
3199 for (i = 0; i < 4; i++) {
3200 if (params->wep_key[i] && params->wep_key_len[i] &&
3201 params->wep_key_len[i] <= 16) {
3202 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3203 params->wep_key_len[i]);
3204 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3205 } else
3206 drv->auth_wep_key_len[i] = 0;
3207 }
3208}
3209
3210
4bd71954
JM
3211static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3212{
3213 struct wpa_driver_nl80211_data *drv = bss->drv;
3214
3215 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3216 return;
3217
3218 /*
3219 * Looks like we failed to unmask 11b rates previously. This could
3220 * happen, e.g., if the interface was down at the point in time when a
3221 * P2P group was terminated.
3222 */
3223 wpa_printf(MSG_DEBUG,
3224 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3225 bss->ifname);
3226 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3227}
3228
3229
3c67e977
VK
3230static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3231{
3232 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3233 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3234 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3235 return NL80211_AUTHTYPE_SHARED_KEY;
3236 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3237 return NL80211_AUTHTYPE_NETWORK_EAP;
3238 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3239 return NL80211_AUTHTYPE_FT;
3240 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3241 return NL80211_AUTHTYPE_SAE;
3242 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3243 return NL80211_AUTHTYPE_FILS_SK;
3244 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3245 return NL80211_AUTHTYPE_FILS_SK_PFS;
3246
3247 return NL80211_AUTHTYPE_MAX;
3248}
3249
3250
c2a04078 3251static int wpa_driver_nl80211_authenticate(
9ebce9c5 3252 struct i802_bss *bss, struct wpa_driver_auth_params *params)
c2a04078 3253{
a2e40bb6 3254 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 3255 int ret = -1, i;
c2a04078
JM
3256 struct nl_msg *msg;
3257 enum nl80211_auth_type type;
2f4f73b1 3258 enum nl80211_iftype nlmode;
6d6f4bb8 3259 int count = 0;
536fd62d
JM
3260 int is_retry;
3261
4bd71954
JM
3262 nl80211_unmask_11b_rates(bss);
3263
536fd62d
JM
3264 is_retry = drv->retry_auth;
3265 drv->retry_auth = 0;
e8d70a73 3266 drv->ignore_deauth_event = 0;
c2a04078 3267
add9b7a4 3268 nl80211_mark_disconnected(drv);
e6b8efeb 3269 os_memset(drv->auth_bssid, 0, ETH_ALEN);
add9b7a4
JM
3270 if (params->bssid)
3271 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3272 else
3273 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
af473088 3274 /* FIX: IBSS mode */
2f4f73b1
EP
3275 nlmode = params->p2p ?
3276 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3277 if (drv->nlmode != nlmode &&
9ebce9c5 3278 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
4a867032
JM
3279 return -1;
3280
6d6f4bb8 3281retry:
c2a04078
JM
3282 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3283 drv->ifindex);
a0b2f99b 3284
9725b784
JM
3285 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3286 if (!msg)
3287 goto fail;
0194fedb 3288
a0b2f99b
JM
3289 for (i = 0; i < 4; i++) {
3290 if (!params->wep_key[i])
3291 continue;
9ebce9c5 3292 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2ea2fcc7 3293 NULL, i,
a0b2f99b
JM
3294 i == params->wep_tx_keyidx, NULL, 0,
3295 params->wep_key[i],
3296 params->wep_key_len[i]);
0194fedb
JB
3297 if (params->wep_tx_keyidx != i)
3298 continue;
3299 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
9725b784
JM
3300 params->wep_key[i], params->wep_key_len[i]))
3301 goto fail;
a0b2f99b
JM
3302 }
3303
c2a04078
JM
3304 if (params->bssid) {
3305 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3306 MAC2STR(params->bssid));
a862e4a3
JM
3307 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3308 goto fail;
c2a04078
JM
3309 }
3310 if (params->freq) {
3311 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
a862e4a3
JM
3312 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3313 goto fail;
c2a04078
JM
3314 }
3315 if (params->ssid) {
3316 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3317 params->ssid, params->ssid_len);
a862e4a3
JM
3318 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3319 params->ssid))
3320 goto fail;
c2a04078
JM
3321 }
3322 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
a862e4a3
JM
3323 if (params->ie &&
3324 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3325 goto fail;
ce16c489
JM
3326 if (params->auth_data) {
3327 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3328 params->auth_data_len);
3329 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3330 params->auth_data))
a862e4a3 3331 goto fail;
569fed90 3332 }
3c67e977 3333 type = get_nl_auth_type(params->auth_alg);
c2a04078 3334 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3c67e977
VK
3335 if (type == NL80211_AUTHTYPE_MAX ||
3336 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
a862e4a3 3337 goto fail;
77339912
JM
3338 if (params->local_state_change) {
3339 wpa_printf(MSG_DEBUG, " * Local state change only");
a862e4a3
JM
3340 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3341 goto fail;
77339912 3342 }
c2a04078
JM
3343
3344 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3345 msg = NULL;
3346 if (ret) {
3b7ea880
BG
3347 wpa_dbg(drv->ctx, MSG_DEBUG,
3348 "nl80211: MLME command failed (auth): ret=%d (%s)",
3349 ret, strerror(-ret));
6d6f4bb8 3350 count++;
77339912
JM
3351 if (ret == -EALREADY && count == 1 && params->bssid &&
3352 !params->local_state_change) {
6d6f4bb8
JM
3353 /*
3354 * mac80211 does not currently accept new
3355 * authentication if we are already authenticated. As a
3356 * workaround, force deauthentication and try again.
3357 */
3358 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3359 "after forced deauthentication");
e8d70a73 3360 drv->ignore_deauth_event = 1;
6d6f4bb8 3361 wpa_driver_nl80211_deauthenticate(
5205c4f9 3362 bss, params->bssid,
6d6f4bb8
JM
3363 WLAN_REASON_PREV_AUTH_NOT_VALID);
3364 nlmsg_free(msg);
3365 goto retry;
3366 }
536fd62d
JM
3367
3368 if (ret == -ENOENT && params->freq && !is_retry) {
3369 /*
3370 * cfg80211 has likely expired the BSS entry even
3371 * though it was previously available in our internal
3372 * BSS table. To recover quickly, start a single
3373 * channel scan on the specified channel.
3374 */
3375 struct wpa_driver_scan_params scan;
3376 int freqs[2];
3377
3378 os_memset(&scan, 0, sizeof(scan));
3379 scan.num_ssids = 1;
3380 if (params->ssid) {
3381 scan.ssids[0].ssid = params->ssid;
3382 scan.ssids[0].ssid_len = params->ssid_len;
3383 }
3384 freqs[0] = params->freq;
3385 freqs[1] = 0;
3386 scan.freqs = freqs;
3387 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3388 "channel scan to refresh cfg80211 BSS "
3389 "entry");
3390 ret = wpa_driver_nl80211_scan(bss, &scan);
3391 if (ret == 0) {
3392 nl80211_copy_auth_params(drv, params);
3393 drv->scan_for_auth = 1;
3394 }
8c3ba078
JM
3395 } else if (is_retry) {
3396 /*
3397 * Need to indicate this with an event since the return
3398 * value from the retry is not delivered to core code.
3399 */
3400 union wpa_event_data event;
3401 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3402 "failed");
3403 os_memset(&event, 0, sizeof(event));
3404 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3405 ETH_ALEN);
3406 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3407 &event);
536fd62d 3408 }
a862e4a3
JM
3409 } else {
3410 wpa_printf(MSG_DEBUG,
3411 "nl80211: Authentication request send successfully");
c2a04078 3412 }
c2a04078 3413
a862e4a3 3414fail:
c2a04078
JM
3415 nlmsg_free(msg);
3416 return ret;
3417}
3418
3419
f3407c66 3420int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
536fd62d
JM
3421{
3422 struct wpa_driver_auth_params params;
834ee56f 3423 struct i802_bss *bss = drv->first_bss;
536fd62d
JM
3424 int i;
3425
3426 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3427
3428 os_memset(&params, 0, sizeof(params));
3429 params.freq = drv->auth_freq;
3430 params.auth_alg = drv->auth_alg;
3431 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3432 params.local_state_change = drv->auth_local_state_change;
3433 params.p2p = drv->auth_p2p;
3434
3435 if (!is_zero_ether_addr(drv->auth_bssid_))
3436 params.bssid = drv->auth_bssid_;
3437
3438 if (drv->auth_ssid_len) {
3439 params.ssid = drv->auth_ssid;
3440 params.ssid_len = drv->auth_ssid_len;
3441 }
3442
3443 params.ie = drv->auth_ie;
3444 params.ie_len = drv->auth_ie_len;
3445
3446 for (i = 0; i < 4; i++) {
3447 if (drv->auth_wep_key_len[i]) {
3448 params.wep_key[i] = drv->auth_wep_key[i];
3449 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3450 }
3451 }
3452
3453 drv->retry_auth = 1;
3454 return wpa_driver_nl80211_authenticate(bss, &params);
3455}
3456
3457
a11241fa
JB
3458static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3459 const void *data, size_t len,
55231068
JM
3460 int encrypt, int noack,
3461 unsigned int freq, int no_cck,
2d3943ce
AO
3462 int offchanok, unsigned int wait_time,
3463 const u16 *csa_offs,
3464 size_t csa_offs_len)
a11241fa
JB
3465{
3466 struct wpa_driver_nl80211_data *drv = bss->drv;
3467 u64 cookie;
41cc50d1 3468 int res;
a11241fa 3469
c7caac56
JM
3470 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3471 freq = nl80211_get_assoc_freq(drv);
3472 wpa_printf(MSG_DEBUG,
3473 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3474 freq);
3475 }
af964484
JM
3476 if (freq == 0) {
3477 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3478 bss->freq);
55231068 3479 freq = bss->freq;
af964484 3480 }
55231068 3481
739faee2 3482 if (drv->use_monitor) {
981cf85a 3483 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
739faee2 3484 freq, bss->freq);
981cf85a 3485 return nl80211_send_monitor(drv, data, len, encrypt, noack);
739faee2 3486 }
a11241fa 3487
739faee2 3488 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
41cc50d1 3489 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
2d3943ce
AO
3490 &cookie, no_cck, noack, offchanok,
3491 csa_offs, csa_offs_len);
41cc50d1
JM
3492 if (res == 0 && !noack) {
3493 const struct ieee80211_mgmt *mgmt;
3494 u16 fc;
3495
3496 mgmt = (const struct ieee80211_mgmt *) data;
3497 fc = le_to_host16(mgmt->frame_control);
3498 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3499 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3500 wpa_printf(MSG_MSGDUMP,
3501 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3502 (long long unsigned int)
3503 drv->send_action_cookie,
3504 (long long unsigned int) cookie);
3505 drv->send_action_cookie = cookie;
3506 }
3507 }
3508
3509 return res;
a11241fa
JB
3510}
3511
3512
9ebce9c5
JM
3513static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3514 size_t data_len, int noack,
3515 unsigned int freq, int no_cck,
3516 int offchanok,
2d3943ce
AO
3517 unsigned int wait_time,
3518 const u16 *csa_offs,
3519 size_t csa_offs_len)
2c2010ac 3520{
a2e40bb6 3521 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 3522 struct ieee80211_mgmt *mgmt;
7a47d567 3523 int encrypt = 1;
2c2010ac
JM
3524 u16 fc;
3525
3526 mgmt = (struct ieee80211_mgmt *) data;
3527 fc = le_to_host16(mgmt->frame_control);
f95a4524
PF
3528 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3529 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3530 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3531 fc, fc2str(fc), drv->nlmode);
2c2010ac 3532
8e12685c
AS
3533 if ((is_sta_interface(drv->nlmode) ||
3534 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
5582a5d1
JB
3535 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3536 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3537 /*
3538 * The use of last_mgmt_freq is a bit of a hack,
3539 * but it works due to the single-threaded nature
3540 * of wpa_supplicant.
3541 */
af964484
JM
3542 if (freq == 0) {
3543 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3544 drv->last_mgmt_freq);
55231068 3545 freq = drv->last_mgmt_freq;
af964484 3546 }
55231068 3547 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7 3548 data, data_len, NULL, 1, noack,
2d3943ce 3549 1, csa_offs, csa_offs_len);
5582a5d1
JB
3550 }
3551
61cbe2ff 3552 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
af964484
JM
3553 if (freq == 0) {
3554 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3555 bss->freq);
55231068 3556 freq = bss->freq;
af964484 3557 }
b5671498
JM
3558 return nl80211_send_frame_cmd(bss, freq,
3559 (int) freq == bss->freq ? 0 :
3560 wait_time,
d8d6b32e
DG
3561 data, data_len,
3562 &drv->send_action_cookie,
2d3943ce
AO
3563 no_cck, noack, offchanok,
3564 csa_offs, csa_offs_len);
86957e62
JM
3565 }
3566
2c2010ac
JM
3567 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3568 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3569 /*
3570 * Only one of the authentication frame types is encrypted.
3571 * In order for static WEP encryption to work properly (i.e.,
3572 * to not encrypt the frame), we need to tell mac80211 about
3573 * the frames that must not be encrypted.
3574 */
3575 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3576 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
3577 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3578 encrypt = 0;
2c2010ac
JM
3579 }
3580
739faee2 3581 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
a11241fa 3582 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068 3583 noack, freq, no_cck, offchanok,
2d3943ce
AO
3584 wait_time, csa_offs,
3585 csa_offs_len);
55231068
JM
3586}
3587
3588
cae87abd
JM
3589static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3590{
3591 u8 rates[NL80211_MAX_SUPP_RATES];
3592 u8 rates_len = 0;
3593 int i;
3594
3595 if (!basic_rates)
3596 return 0;
3597
3598 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3599 rates[rates_len++] = basic_rates[i] / 5;
3600
3601 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3602}
3603
3604
31357268 3605static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47 3606 int slot, int ht_opmode, int ap_isolate,
cae87abd 3607 const int *basic_rates)
31357268
JM
3608{
3609 struct wpa_driver_nl80211_data *drv = bss->drv;
3610 struct nl_msg *msg;
3611
13f83980 3612 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
a862e4a3
JM
3613 (cts >= 0 &&
3614 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3615 (preamble >= 0 &&
3616 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3617 (slot >= 0 &&
3618 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3619 (ht_opmode >= 0 &&
3620 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3621 (ap_isolate >= 0 &&
cae87abd
JM
3622 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3623 nl80211_put_basic_rates(msg, basic_rates)) {
3624 nlmsg_free(msg);
3625 return -ENOBUFS;
e5693c47
JM
3626 }
3627
31357268 3628 return send_and_recv_msgs(drv, msg, NULL, NULL);
31357268
JM
3629}
3630
3631
3c4ca363
VN
3632static int wpa_driver_nl80211_set_acl(void *priv,
3633 struct hostapd_acl_params *params)
3634{
3635 struct i802_bss *bss = priv;
3636 struct wpa_driver_nl80211_data *drv = bss->drv;
3637 struct nl_msg *msg;
f429ec44 3638 struct nl_msg *acl;
3c4ca363 3639 unsigned int i;
a862e4a3 3640 int ret;
3c4ca363
VN
3641
3642 if (!(drv->capa.max_acl_mac_addrs))
3643 return -ENOTSUP;
3644
3645 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3646 return -ENOTSUP;
3647
3c4ca363
VN
3648 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3649 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3650
f429ec44
JM
3651 acl = nlmsg_alloc();
3652 if (!acl)
3653 return -ENOMEM;
3654 for (i = 0; i < params->num_mac_acl; i++) {
3655 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3656 nlmsg_free(acl);
3657 return -ENOMEM;
3658 }
3659 }
3660
9725b784 3661 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
a862e4a3
JM
3662 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3663 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3664 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
f429ec44 3665 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
a862e4a3 3666 nlmsg_free(msg);
f429ec44 3667 nlmsg_free(acl);
a862e4a3
JM
3668 return -ENOMEM;
3669 }
f429ec44 3670 nlmsg_free(acl);
3c4ca363
VN
3671
3672 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3c4ca363
VN
3673 if (ret) {
3674 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3675 ret, strerror(-ret));
3676 }
3677
3c4ca363
VN
3678 return ret;
3679}
3680
3681
85e1fad8
JM
3682static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3683{
3684 if (beacon_int > 0) {
3685 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3686 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3687 beacon_int);
3688 }
3689
3690 return 0;
3691}
3692
3693
4ac2ea57
MH
3694static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3695{
3696 if (dtim_period > 0) {
3697 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3698 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3699 }
3700
3701 return 0;
3702}
3703
3704
052b8d38
MH
3705#ifdef CONFIG_MESH
3706static int nl80211_set_mesh_config(void *priv,
3707 struct wpa_driver_mesh_bss_params *params)
3708{
3709 struct i802_bss *bss = priv;
3710 struct wpa_driver_nl80211_data *drv = bss->drv;
3711 struct nl_msg *msg;
3712 int ret;
3713
3714 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3715 if (!msg)
3716 return -1;
3717
3718 ret = nl80211_put_mesh_config(msg, params);
3719 if (ret < 0) {
3720 nlmsg_free(msg);
3721 return ret;
3722 }
3723
3724 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3725 if (ret) {
3726 wpa_printf(MSG_ERROR,
3727 "nl80211: Mesh config set failed: %d (%s)",
3728 ret, strerror(-ret));
3729 return ret;
3730 }
3731 return 0;
3732}
3733#endif /* CONFIG_MESH */
3734
3735
d4f3003c
PK
3736static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3737 struct wpa_driver_ap_params *params)
3738{
3739 struct nlattr *bands, *band;
3740 struct nl80211_txrate_vht vht_rate;
3741
3742 if (!params->freq ||
3743 (params->beacon_rate == 0 &&
3744 params->rate_type == BEACON_RATE_LEGACY))
3745 return 0;
3746
3747 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3748 if (!bands)
3749 return -1;
3750
3751 switch (params->freq->mode) {
3752 case HOSTAPD_MODE_IEEE80211B:
3753 case HOSTAPD_MODE_IEEE80211G:
3754 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3755 break;
3756 case HOSTAPD_MODE_IEEE80211A:
3757 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3758 break;
3759 case HOSTAPD_MODE_IEEE80211AD:
3760 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3761 break;
3762 default:
3763 return 0;
3764 }
3765
3766 if (!band)
3767 return -1;
3768
3769 os_memset(&vht_rate, 0, sizeof(vht_rate));
3770
3771 switch (params->rate_type) {
3772 case BEACON_RATE_LEGACY:
3773 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3774 wpa_printf(MSG_INFO,
3775 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3776 return -1;
3777 }
3778
3779 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3780 (u8) params->beacon_rate / 5) ||
3781 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3782 (params->freq->vht_enabled &&
3783 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3784 &vht_rate)))
3785 return -1;
3786
3787 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3788 params->beacon_rate);
3789 break;
3790 case BEACON_RATE_HT:
3791 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3792 wpa_printf(MSG_INFO,
3793 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3794 return -1;
3795 }
3796 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3797 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3798 (params->freq->vht_enabled &&
3799 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3800 &vht_rate)))
3801 return -1;
3802 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3803 params->beacon_rate);
3804 break;
3805 case BEACON_RATE_VHT:
3806 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3807 wpa_printf(MSG_INFO,
3808 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3809 return -1;
3810 }
3811 vht_rate.mcs[0] = BIT(params->beacon_rate);
3812 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3813 return -1;
3814 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3815 return -1;
3816 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3817 &vht_rate))
3818 return -1;
3819 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3820 params->beacon_rate);
3821 break;
3822 }
3823
3824 nla_nest_end(msg, band);
3825 nla_nest_end(msg, bands);
3826
3827 return 0;
3828}
3829
3830
34f7c699
MB
3831static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3832 int multicast_to_unicast)
3833{
3834 struct wpa_driver_nl80211_data *drv = bss->drv;
3835 struct nl_msg *msg;
3836 int ret;
3837
3838 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3839 if (!msg ||
3840 (multicast_to_unicast &&
3841 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3842 wpa_printf(MSG_ERROR,
3843 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3844 bss->ifname);
3845 nlmsg_free(msg);
3846 return -ENOBUFS;
3847 }
3848
3849 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3850
3851 switch (ret) {
3852 case 0:
3853 wpa_printf(MSG_DEBUG,
3854 "nl80211: multicast to unicast %s on interface %s",
3855 multicast_to_unicast ? "enabled" : "disabled",
3856 bss->ifname);
3857 break;
3858 case -EOPNOTSUPP:
3859 if (!multicast_to_unicast)
3860 break;
3861 wpa_printf(MSG_INFO,
3862 "nl80211: multicast to unicast not supported on interface %s",
3863 bss->ifname);
3864 break;
3865 default:
3866 wpa_printf(MSG_ERROR,
3867 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3868 multicast_to_unicast ? "enabling" : "disabling",
3869 ret, strerror(-ret), bss->ifname);
3870 break;
3871 }
3872
3873 return ret;
3874}
3875
3876
19c3b566
JM
3877static int wpa_driver_nl80211_set_ap(void *priv,
3878 struct wpa_driver_ap_params *params)
d2440ba0 3879{
a2e40bb6
FF
3880 struct i802_bss *bss = priv;
3881 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
3882 struct nl_msg *msg;
3883 u8 cmd = NL80211_CMD_NEW_BEACON;
3884 int ret;
b4fd6fab 3885 int beacon_set;
b11d1d64 3886 int num_suites;
da1080d7 3887 int smps_mode;
de4ed4a8 3888 u32 suites[10], suite;
b11d1d64 3889 u32 ver;
052b8d38
MH
3890#ifdef CONFIG_MESH
3891 struct wpa_driver_mesh_bss_params mesh_params;
3892#endif /* CONFIG_MESH */
b4fd6fab 3893
f33c8606 3894 beacon_set = params->reenable ? 0 : bss->beacon_set;
d2440ba0 3895
d2440ba0 3896 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
3897 beacon_set);
3898 if (beacon_set)
d2440ba0 3899 cmd = NL80211_CMD_SET_BEACON;
89fa633a
DL
3900 else if (!drv->device_ap_sme && !drv->use_monitor &&
3901 !nl80211_get_wiphy_data_ap(bss))
a70cd0db 3902 return -ENOBUFS;
d2440ba0 3903
b92e08fc
JM
3904 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3905 params->head, params->head_len);
b92e08fc
JM
3906 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3907 params->tail, params->tail_len);
13f83980 3908 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
b92e08fc 3909 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
d4f3003c
PK
3910 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3911 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
b92e08fc 3912 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
b92e08fc
JM
3913 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3914 params->ssid, params->ssid_len);
13f83980 3915 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
a862e4a3
JM
3916 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3917 params->head) ||
3918 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3919 params->tail) ||
85e1fad8 3920 nl80211_put_beacon_int(msg, params->beacon_int) ||
d4f3003c 3921 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
4ac2ea57 3922 nl80211_put_dtim_period(msg, params->dtim_period) ||
a862e4a3
JM
3923 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3924 goto fail;
b92e08fc
JM
3925 if (params->proberesp && params->proberesp_len) {
3926 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3927 params->proberesp, params->proberesp_len);
a862e4a3
JM
3928 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3929 params->proberesp))
3930 goto fail;
b92e08fc 3931 }
97a7a0b5
JM
3932 switch (params->hide_ssid) {
3933 case NO_SSID_HIDING:
b92e08fc 3934 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
a862e4a3
JM
3935 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3936 NL80211_HIDDEN_SSID_NOT_IN_USE))
3937 goto fail;
97a7a0b5
JM
3938 break;
3939 case HIDDEN_SSID_ZERO_LEN:
b92e08fc 3940 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
a862e4a3
JM
3941 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3942 NL80211_HIDDEN_SSID_ZERO_LEN))
3943 goto fail;
97a7a0b5
JM
3944 break;
3945 case HIDDEN_SSID_ZERO_CONTENTS:
b92e08fc 3946 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
a862e4a3
JM
3947 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3948 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3949 goto fail;
97a7a0b5
JM
3950 break;
3951 }
b92e08fc 3952 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
a862e4a3
JM
3953 if (params->privacy &&
3954 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3955 goto fail;
b92e08fc 3956 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
b11d1d64
JM
3957 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3958 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3959 /* Leave out the attribute */
a862e4a3
JM
3960 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3961 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3962 NL80211_AUTHTYPE_SHARED_KEY))
3963 goto fail;
3964 } else {
3965 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3966 NL80211_AUTHTYPE_OPEN_SYSTEM))
3967 goto fail;
3968 }
b11d1d64 3969
b92e08fc 3970 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
b11d1d64
JM
3971 ver = 0;
3972 if (params->wpa_version & WPA_PROTO_WPA)
3973 ver |= NL80211_WPA_VERSION_1;
3974 if (params->wpa_version & WPA_PROTO_RSN)
3975 ver |= NL80211_WPA_VERSION_2;
a862e4a3
JM
3976 if (ver &&
3977 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3978 goto fail;
b11d1d64 3979
b92e08fc
JM
3980 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3981 params->key_mgmt_suites);
b11d1d64
JM
3982 num_suites = 0;
3983 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3aa24db9 3984 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
b11d1d64 3985 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3aa24db9 3986 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
a862e4a3
JM
3987 if (num_suites &&
3988 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3989 suites))
3990 goto fail;
b11d1d64 3991
533fe09b 3992 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
e3429c0b
JB
3993 (!params->pairwise_ciphers ||
3994 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
3995 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
3996 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
a862e4a3 3997 goto fail;
9f12614b 3998
b92e08fc
JM
3999 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4000 params->pairwise_ciphers);
de4ed4a8
JM
4001 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4002 suites, ARRAY_SIZE(suites));
a862e4a3
JM
4003 if (num_suites &&
4004 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4005 num_suites * sizeof(u32), suites))
4006 goto fail;
b11d1d64 4007
b92e08fc
JM
4008 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4009 params->group_cipher);
de4ed4a8 4010 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
a862e4a3
JM
4011 if (suite &&
4012 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4013 goto fail;
d2440ba0 4014
ee298f1b
JM
4015 if (params->ht_opmode != -1) {
4016 switch (params->smps_mode) {
4017 case HT_CAP_INFO_SMPS_DYNAMIC:
4018 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
4019 smps_mode = NL80211_SMPS_DYNAMIC;
4020 break;
4021 case HT_CAP_INFO_SMPS_STATIC:
4022 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
4023 smps_mode = NL80211_SMPS_STATIC;
4024 break;
4025 default:
4026 /* invalid - fallback to smps off */
4027 case HT_CAP_INFO_SMPS_DISABLED:
4028 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
4029 smps_mode = NL80211_SMPS_OFF;
4030 break;
4031 }
a2426829 4032 if (nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
ee298f1b 4033 goto fail;
da1080d7 4034 }
da1080d7 4035
fb91db56 4036 if (params->beacon_ies) {
b92e08fc
JM
4037 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4038 params->beacon_ies);
a862e4a3
JM
4039 if (nla_put(msg, NL80211_ATTR_IE,
4040 wpabuf_len(params->beacon_ies),
4041 wpabuf_head(params->beacon_ies)))
4042 goto fail;
fb91db56
JM
4043 }
4044 if (params->proberesp_ies) {
b92e08fc
JM
4045 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4046 params->proberesp_ies);
a862e4a3
JM
4047 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4048 wpabuf_len(params->proberesp_ies),
4049 wpabuf_head(params->proberesp_ies)))
4050 goto fail;
fb91db56
JM
4051 }
4052 if (params->assocresp_ies) {
b92e08fc
JM
4053 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4054 params->assocresp_ies);
a862e4a3
JM
4055 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4056 wpabuf_len(params->assocresp_ies),
4057 wpabuf_head(params->assocresp_ies)))
4058 goto fail;
fb91db56
JM
4059 }
4060
a0133ee1 4061 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
b92e08fc
JM
4062 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4063 params->ap_max_inactivity);
a862e4a3
JM
4064 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4065 params->ap_max_inactivity))
4066 goto fail;
a0133ee1
VT
4067 }
4068
abb8d08b
EP
4069#ifdef CONFIG_P2P
4070 if (params->p2p_go_ctwindow > 0) {
4071 if (drv->p2p_go_ctwindow_supported) {
4072 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4073 params->p2p_go_ctwindow);
4074 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4075 params->p2p_go_ctwindow))
4076 goto fail;
4077 } else {
4078 wpa_printf(MSG_INFO,
4079 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4080 }
4081 }
4082#endif /* CONFIG_P2P */
4083
86b5c400
LD
4084 if (params->pbss) {
4085 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4086 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4087 goto fail;
4088 }
4089
d2440ba0
JM
4090 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4091 if (ret) {
4092 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4093 ret, strerror(-ret));
b4fd6fab 4094 } else {
b4fd6fab 4095 bss->beacon_set = 1;
31357268 4096 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 4097 params->short_slot_time, params->ht_opmode,
e5693c47 4098 params->isolate, params->basic_rates);
34f7c699
MB
4099 nl80211_set_multicast_to_unicast(bss,
4100 params->multicast_to_unicast);
e87ef751
PX
4101 if (beacon_set && params->freq &&
4102 params->freq->bandwidth != bss->bandwidth) {
4103 wpa_printf(MSG_DEBUG,
4104 "nl80211: Update BSS %s bandwidth: %d -> %d",
4105 bss->ifname, bss->bandwidth,
4106 params->freq->bandwidth);
4107 ret = nl80211_set_channel(bss, params->freq, 1);
4108 if (ret) {
4109 wpa_printf(MSG_DEBUG,
4110 "nl80211: Frequency set failed: %d (%s)",
4111 ret, strerror(-ret));
4112 } else {
4113 wpa_printf(MSG_DEBUG,
4114 "nl80211: Frequency set succeeded for ht2040 coex");
4115 bss->bandwidth = params->freq->bandwidth;
4116 }
f5728d0a 4117 } else if (!beacon_set && params->freq) {
e87ef751
PX
4118 /*
4119 * cfg80211 updates the driver on frequence change in AP
4120 * mode only at the point when beaconing is started, so
4121 * set the initial value here.
4122 */
4123 bss->bandwidth = params->freq->bandwidth;
4124 }
b4fd6fab 4125 }
052b8d38
MH
4126
4127#ifdef CONFIG_MESH
4128 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4129 os_memset(&mesh_params, 0, sizeof(mesh_params));
4130 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4131 mesh_params.ht_opmode = params->ht_opmode;
4132 ret = nl80211_set_mesh_config(priv, &mesh_params);
4133 if (ret < 0)
4134 return ret;
4135 }
4136#endif /* CONFIG_MESH */
4137
d2440ba0 4138 return ret;
a862e4a3 4139fail:
5883168a 4140 nlmsg_free(msg);
d2440ba0
JM
4141 return -ENOBUFS;
4142}
4143
4144
1c4ffa87 4145static int nl80211_put_freq_params(struct nl_msg *msg,
72b2605f 4146 const struct hostapd_freq_params *freq)
1581b38b 4147{
1fc4ab23 4148 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
a862e4a3
JM
4149 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4150 return -ENOBUFS;
4151
1fc4ab23
JM
4152 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4153 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4154
89b800d7 4155 if (freq->vht_enabled) {
a862e4a3
JM
4156 enum nl80211_chan_width cw;
4157
1fc4ab23 4158 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
89b800d7
JB
4159 switch (freq->bandwidth) {
4160 case 20:
a862e4a3 4161 cw = NL80211_CHAN_WIDTH_20;
89b800d7
JB
4162 break;
4163 case 40:
a862e4a3 4164 cw = NL80211_CHAN_WIDTH_40;
89b800d7
JB
4165 break;
4166 case 80:
4167 if (freq->center_freq2)
a862e4a3 4168 cw = NL80211_CHAN_WIDTH_80P80;
89b800d7 4169 else
a862e4a3 4170 cw = NL80211_CHAN_WIDTH_80;
89b800d7
JB
4171 break;
4172 case 160:
a862e4a3 4173 cw = NL80211_CHAN_WIDTH_160;
89b800d7
JB
4174 break;
4175 default:
1c4ffa87 4176 return -EINVAL;
89b800d7 4177 }
a862e4a3 4178
1fc4ab23
JM
4179 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4180 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4181 freq->center_freq1);
4182 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4183 freq->center_freq2);
a862e4a3
JM
4184 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4185 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4186 freq->center_freq1) ||
4187 (freq->center_freq2 &&
4188 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4189 freq->center_freq2)))
4190 return -ENOBUFS;
89b800d7 4191 } else if (freq->ht_enabled) {
a862e4a3
JM
4192 enum nl80211_channel_type ct;
4193
1fc4ab23
JM
4194 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4195 freq->sec_channel_offset);
89b800d7 4196 switch (freq->sec_channel_offset) {
f019981a 4197 case -1:
a862e4a3 4198 ct = NL80211_CHAN_HT40MINUS;
f019981a
JM
4199 break;
4200 case 1:
a862e4a3 4201 ct = NL80211_CHAN_HT40PLUS;
f019981a
JM
4202 break;
4203 default:
a862e4a3 4204 ct = NL80211_CHAN_HT20;
f019981a
JM
4205 break;
4206 }
a862e4a3 4207
1fc4ab23 4208 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
a862e4a3
JM
4209 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4210 return -ENOBUFS;
3388e7b9
MH
4211 } else {
4212 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4213 NL80211_CHAN_NO_HT);
4214 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4215 NL80211_CHAN_NO_HT))
4216 return -ENOBUFS;
f019981a 4217 }
1c4ffa87 4218 return 0;
1c4ffa87
AO
4219}
4220
4221
e87ef751
PX
4222static int nl80211_set_channel(struct i802_bss *bss,
4223 struct hostapd_freq_params *freq, int set_chan)
1c4ffa87
AO
4224{
4225 struct wpa_driver_nl80211_data *drv = bss->drv;
4226 struct nl_msg *msg;
4227 int ret;
4228
4229 wpa_printf(MSG_DEBUG,
4230 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4231 freq->freq, freq->ht_enabled, freq->vht_enabled,
4232 freq->bandwidth, freq->center_freq1, freq->center_freq2);
1c4ffa87 4233
9725b784
JM
4234 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4235 NL80211_CMD_SET_WIPHY);
4236 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
a862e4a3
JM
4237 nlmsg_free(msg);
4238 return -1;
4239 }
1581b38b 4240
d2440ba0 4241 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
e4fb2167 4242 if (ret == 0) {
89b800d7 4243 bss->freq = freq->freq;
1581b38b 4244 return 0;
e4fb2167 4245 }
f019981a 4246 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 4247 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b
JM
4248 return -1;
4249}
4250
0f4e8b4f 4251
95ab6063
AN
4252static u32 sta_flags_nl80211(int flags)
4253{
4254 u32 f = 0;
4255
4256 if (flags & WPA_STA_AUTHORIZED)
4257 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4258 if (flags & WPA_STA_WMM)
4259 f |= BIT(NL80211_STA_FLAG_WME);
4260 if (flags & WPA_STA_SHORT_PREAMBLE)
4261 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4262 if (flags & WPA_STA_MFP)
4263 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
4264 if (flags & WPA_STA_TDLS_PEER)
4265 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7a228b5c
BC
4266 if (flags & WPA_STA_AUTHENTICATED)
4267 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
dc55b6b6
AB
4268 if (flags & WPA_STA_ASSOCIATED)
4269 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
95ab6063
AN
4270
4271 return f;
4272}
4273
4274
7c7e7877
BC
4275#ifdef CONFIG_MESH
4276static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4277{
4278 switch (state) {
d02e5498 4279 case PLINK_IDLE:
7c7e7877 4280 return NL80211_PLINK_LISTEN;
d02e5498 4281 case PLINK_OPN_SNT:
7c7e7877 4282 return NL80211_PLINK_OPN_SNT;
d02e5498 4283 case PLINK_OPN_RCVD:
7c7e7877
BC
4284 return NL80211_PLINK_OPN_RCVD;
4285 case PLINK_CNF_RCVD:
4286 return NL80211_PLINK_CNF_RCVD;
4287 case PLINK_ESTAB:
4288 return NL80211_PLINK_ESTAB;
4289 case PLINK_HOLDING:
4290 return NL80211_PLINK_HOLDING;
4291 case PLINK_BLOCKED:
4292 return NL80211_PLINK_BLOCKED;
4293 default:
4294 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4295 state);
4296 }
4297 return -1;
4298}
4299#endif /* CONFIG_MESH */
4300
4301
62847751 4302static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
4303 struct hostapd_sta_add_params *params)
4304{
a2e40bb6
FF
4305 struct i802_bss *bss = priv;
4306 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8 4307 struct nl_msg *msg;
95ab6063 4308 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
4309 int ret = -ENOBUFS;
4310
45b722f1
AN
4311 if ((params->flags & WPA_STA_TDLS_PEER) &&
4312 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4313 return -EOPNOTSUPP;
4314
e4dea253
JM
4315 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4316 params->set ? "Set" : "Add", MAC2STR(params->addr));
13f83980
JM
4317 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4318 NL80211_CMD_NEW_STATION);
4319 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
a862e4a3 4320 goto fail;
5551bc9c 4321
dc55b6b6
AB
4322 /*
4323 * Set the below properties only in one of the following cases:
4324 * 1. New station is added, already associated.
4325 * 2. Set WPA_STA_TDLS_PEER station.
4326 * 3. Set an already added unassociated station, if driver supports
4327 * full AP client state. (Set these properties after station became
4328 * associated will be rejected by the driver).
4329 */
4330 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4331 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4332 (params->flags & WPA_STA_ASSOCIATED))) {
5551bc9c
BC
4333 wpa_hexdump(MSG_DEBUG, " * supported rates",
4334 params->supp_rates, params->supp_rates_len);
a862e4a3
JM
4335 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4336 params->capability);
4337 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4338 params->supp_rates_len, params->supp_rates) ||
4339 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4340 params->capability))
4341 goto fail;
e503861f
JM
4342
4343 if (params->ht_capabilities) {
4344 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4345 (u8 *) params->ht_capabilities,
4346 sizeof(*params->ht_capabilities));
a862e4a3
JM
4347 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4348 sizeof(*params->ht_capabilities),
4349 params->ht_capabilities))
4350 goto fail;
e503861f
JM
4351 }
4352
4353 if (params->vht_capabilities) {
4354 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4355 (u8 *) params->vht_capabilities,
4356 sizeof(*params->vht_capabilities));
a862e4a3
JM
4357 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4358 sizeof(*params->vht_capabilities),
4359 params->vht_capabilities))
4360 goto fail;
e503861f
JM
4361 }
4362
e503861f
JM
4363 if (params->ext_capab) {
4364 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4365 params->ext_capab, params->ext_capab_len);
a862e4a3
JM
4366 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4367 params->ext_capab_len, params->ext_capab))
4368 goto fail;
e503861f 4369 }
ae33239c
AB
4370
4371 if (is_ap_interface(drv->nlmode) &&
4372 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4373 params->support_p2p_ps ?
4374 NL80211_P2P_PS_SUPPORTED :
4375 NL80211_P2P_PS_UNSUPPORTED))
4376 goto fail;
5551bc9c 4377 }
45b722f1 4378 if (!params->set) {
f11b72c3
JM
4379 if (params->aid) {
4380 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
a862e4a3
JM
4381 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4382 goto fail;
f11b72c3
JM
4383 } else {
4384 /*
4385 * cfg80211 validates that AID is non-zero, so we have
4386 * to make this a non-zero value for the TDLS case where
dc55b6b6
AB
4387 * a dummy STA entry is used for now and for a station
4388 * that is still not associated.
f11b72c3 4389 */
dc55b6b6
AB
4390 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4391 (params->flags & WPA_STA_TDLS_PEER) ?
4392 "TDLS" : "UNASSOC_STA");
a862e4a3
JM
4393 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4394 goto fail;
f11b72c3 4395 }
e4dea253
JM
4396 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4397 params->listen_interval);
a862e4a3
JM
4398 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4399 params->listen_interval))
4400 goto fail;
e112764e
JM
4401 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4402 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
a862e4a3
JM
4403 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4404 goto fail;
dc55b6b6
AB
4405 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4406 (params->flags & WPA_STA_ASSOCIATED)) {
4407 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4408 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4409 params->listen_interval);
4410 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4411 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4412 params->listen_interval))
4413 goto fail;
45b722f1 4414 }
05a8d422 4415
8a458116
MK
4416 if (params->vht_opmode_enabled) {
4417 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
a862e4a3
JM
4418 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4419 params->vht_opmode))
4420 goto fail;
8a458116
MK
4421 }
4422
efc64886
SD
4423 if (params->supp_channels) {
4424 wpa_hexdump(MSG_DEBUG, " * supported channels",
4425 params->supp_channels, params->supp_channels_len);
a862e4a3
JM
4426 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4427 params->supp_channels_len, params->supp_channels))
4428 goto fail;
efc64886
SD
4429 }
4430
4431 if (params->supp_oper_classes) {
4432 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4433 params->supp_oper_classes,
4434 params->supp_oper_classes_len);
a862e4a3
JM
4435 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4436 params->supp_oper_classes_len,
4437 params->supp_oper_classes))
4438 goto fail;
efc64886
SD
4439 }
4440
95ab6063 4441 os_memset(&upd, 0, sizeof(upd));
7e31703e
BC
4442 upd.set = sta_flags_nl80211(params->flags);
4443 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
dc55b6b6
AB
4444
4445 /*
4446 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4447 * flags, as nl80211 driver moves a new station, by default, into
4448 * associated state.
4449 *
4450 * On the other hand, if the driver supports that feature and the
4451 * station is added in unauthenticated state, set the
4452 * authenticated/associated bits in the mask to prevent moving this
4453 * station to associated state before it is actually associated.
4454 *
4455 * This is irrelevant for mesh mode where the station is added to the
4456 * driver as authenticated already, and ASSOCIATED isn't part of the
4457 * nl80211 API.
4458 */
4459 if (!is_mesh_interface(drv->nlmode)) {
4460 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4461 wpa_printf(MSG_DEBUG,
4462 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4463 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4464 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4465 } else if (!params->set &&
4466 !(params->flags & WPA_STA_TDLS_PEER)) {
4467 if (!(params->flags & WPA_STA_AUTHENTICATED))
4468 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4469 if (!(params->flags & WPA_STA_ASSOCIATED))
4470 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4471 }
e347cafe
MH
4472#ifdef CONFIG_MESH
4473 } else {
4474 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4475 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4476 params->peer_aid);
4477 if (ret)
4478 goto fail;
4479 }
4480#endif /* CONFIG_MESH */
dc55b6b6
AB
4481 }
4482
e4dea253
JM
4483 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4484 upd.set, upd.mask);
a862e4a3
JM
4485 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4486 goto fail;
95ab6063 4487
7c7e7877 4488#ifdef CONFIG_MESH
a862e4a3
JM
4489 if (params->plink_state &&
4490 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4491 sta_plink_state_nl80211(params->plink_state)))
4492 goto fail;
7c7e7877
BC
4493#endif /* CONFIG_MESH */
4494
774bfa62 4495 if (params->flags & WPA_STA_WMM) {
8970bae8
JB
4496 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4497
e4dea253 4498 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
a862e4a3
JM
4499 if (!wme ||
4500 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4501 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4502 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4503 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4504 WMM_QOSINFO_STA_SP_MASK))
4505 goto fail;
8970bae8 4506 nla_nest_end(msg, wme);
774bfa62
EP
4507 }
4508
0f4e8b4f 4509 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 4510 msg = NULL;
0f4e8b4f 4511 if (ret)
45b722f1
AN
4512 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4513 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4514 strerror(-ret));
0f4e8b4f
JM
4515 if (ret == -EEXIST)
4516 ret = 0;
a862e4a3 4517fail:
5883168a 4518 nlmsg_free(msg);
0f4e8b4f
JM
4519 return ret;
4520}
4521
4522
97ed9a06
KP
4523static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4524{
4525#ifdef CONFIG_LIBNL3_ROUTE
4526 struct wpa_driver_nl80211_data *drv = bss->drv;
4527 struct rtnl_neigh *rn;
4528 struct nl_addr *nl_addr;
4529 int err;
4530
4531 rn = rtnl_neigh_alloc();
4532 if (!rn)
4533 return;
4534
4535 rtnl_neigh_set_family(rn, AF_BRIDGE);
4536 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4537 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4538 if (!nl_addr) {
4539 rtnl_neigh_put(rn);
4540 return;
4541 }
4542 rtnl_neigh_set_lladdr(rn, nl_addr);
4543
4544 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4545 if (err < 0) {
4546 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4547 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4548 bss->ifindex, nl_geterror(err));
4549 } else {
4550 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4551 MACSTR, MAC2STR(addr));
4552 }
4553
4554 nl_addr_put(nl_addr);
4555 rtnl_neigh_put(rn);
4556#endif /* CONFIG_LIBNL3_ROUTE */
4557}
4558
4559
59d7148a
JM
4560static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4561 int deauth, u16 reason_code)
0f4e8b4f 4562{
a2e40bb6 4563 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
4564 struct nl_msg *msg;
4565 int ret;
4566
13f83980 4567 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
a862e4a3
JM
4568 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4569 (deauth == 0 &&
4570 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4571 WLAN_FC_STYPE_DISASSOC)) ||
4572 (deauth == 1 &&
4573 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4574 WLAN_FC_STYPE_DEAUTH)) ||
4575 (reason_code &&
4576 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4577 nlmsg_free(msg);
4578 return -ENOBUFS;
4579 }
0f4e8b4f
JM
4580
4581 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83e7bb0e
JM
4582 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4583 " --> %d (%s)",
4584 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
97ed9a06
KP
4585
4586 if (drv->rtnl_sk)
4587 rtnl_neigh_delete_fdb_entry(bss, addr);
4588
0f4e8b4f
JM
4589 if (ret == -ENOENT)
4590 return 0;
4591 return ret;
0f4e8b4f
JM
4592}
4593
1581b38b 4594
f3407c66 4595void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
0915d02c
JM
4596{
4597 struct nl_msg *msg;
de884303 4598 struct wpa_driver_nl80211_data *drv2;
0915d02c 4599
c6e8e8e4
JM
4600 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4601
2135f224 4602 /* stop listening for EAPOL on this interface */
de884303
JM
4603 dl_list_for_each(drv2, &drv->global->interfaces,
4604 struct wpa_driver_nl80211_data, list)
732b1d20
MB
4605 {
4606 del_ifidx(drv2, ifidx, IFIDX_ANY);
4607 /* Remove all bridges learned for this iface */
4608 del_ifidx(drv2, IFIDX_ANY, ifidx);
4609 }
2135f224 4610
95376e1a 4611 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
4612 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4613 return;
e748062b 4614 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
4615}
4616
4617
cc9a2575 4618const char * nl80211_iftype_str(enum nl80211_iftype mode)
a1922f93
JM
4619{
4620 switch (mode) {
4621 case NL80211_IFTYPE_ADHOC:
4622 return "ADHOC";
4623 case NL80211_IFTYPE_STATION:
4624 return "STATION";
4625 case NL80211_IFTYPE_AP:
4626 return "AP";
1045ec36
JM
4627 case NL80211_IFTYPE_AP_VLAN:
4628 return "AP_VLAN";
4629 case NL80211_IFTYPE_WDS:
4630 return "WDS";
a1922f93
JM
4631 case NL80211_IFTYPE_MONITOR:
4632 return "MONITOR";
1045ec36
JM
4633 case NL80211_IFTYPE_MESH_POINT:
4634 return "MESH_POINT";
a1922f93
JM
4635 case NL80211_IFTYPE_P2P_CLIENT:
4636 return "P2P_CLIENT";
4637 case NL80211_IFTYPE_P2P_GO:
4638 return "P2P_GO";
7aad838c
NS
4639 case NL80211_IFTYPE_P2P_DEVICE:
4640 return "P2P_DEVICE";
a1922f93
JM
4641 default:
4642 return "unknown";
4643 }
4644}
4645
4646
a35187e7
KH
4647static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4648 const char *ifname,
4649 enum nl80211_iftype iftype,
d6dcfcda
DS
4650 const u8 *addr, int wds,
4651 int (*handler)(struct nl_msg *, void *),
4652 void *arg)
0915d02c 4653{
8970bae8 4654 struct nl_msg *msg;
0915d02c
JM
4655 int ifidx;
4656 int ret = -ENOBUFS;
4657
a1922f93
JM
4658 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4659 iftype, nl80211_iftype_str(iftype));
4660
56f77852
JM
4661 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4662 if (!msg ||
a862e4a3
JM
4663 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4664 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4665 goto fail;
0915d02c
JM
4666
4667 if (iftype == NL80211_IFTYPE_MONITOR) {
8970bae8 4668 struct nlattr *flags;
0915d02c 4669
8970bae8 4670 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
a862e4a3
JM
4671 if (!flags ||
4672 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4673 goto fail;
0915d02c 4674
8970bae8 4675 nla_nest_end(msg, flags);
fbbfcbac 4676 } else if (wds) {
a862e4a3
JM
4677 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4678 goto fail;
0915d02c
JM
4679 }
4680
52f5877a
IP
4681 /*
4682 * Tell cfg80211 that the interface belongs to the socket that created
4683 * it, and the interface should be deleted when the socket is closed.
4684 */
a862e4a3
JM
4685 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4686 goto fail;
52f5877a 4687
d6dcfcda 4688 ret = send_and_recv_msgs(drv, msg, handler, arg);
5883168a 4689 msg = NULL;
0915d02c 4690 if (ret) {
a862e4a3 4691 fail:
5883168a 4692 nlmsg_free(msg);
a35187e7
KH
4693 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4694 ifname, ret, strerror(-ret));
0915d02c
JM
4695 return ret;
4696 }
4697
f632e483 4698 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6bae92e0 4699 return 0;
6bae92e0 4700
0915d02c 4701 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
4702 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4703 ifname, ifidx);
0915d02c
JM
4704
4705 if (ifidx <= 0)
4706 return -1;
4707
147848ec
JM
4708 /*
4709 * Some virtual interfaces need to process EAPOL packets and events on
4710 * the parent interface. This is used mainly with hostapd.
4711 */
4712 if (drv->hostapd ||
4713 iftype == NL80211_IFTYPE_AP_VLAN ||
4714 iftype == NL80211_IFTYPE_WDS ||
4715 iftype == NL80211_IFTYPE_MONITOR) {
4716 /* start listening for EAPOL on this interface */
732b1d20 4717 add_ifidx(drv, ifidx, IFIDX_ANY);
147848ec 4718 }
2135f224 4719
7bfc47c3 4720 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 4721 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
4722 nl80211_remove_iface(drv, ifidx);
4723 return -1;
2135f224 4724 }
2135f224 4725
0915d02c
JM
4726 return ifidx;
4727}
22a7c9d7
JM
4728
4729
f3407c66
JM
4730int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4731 const char *ifname, enum nl80211_iftype iftype,
4732 const u8 *addr, int wds,
4733 int (*handler)(struct nl_msg *, void *),
4734 void *arg, int use_existing)
a35187e7
KH
4735{
4736 int ret;
4737
d6dcfcda
DS
4738 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4739 arg);
a35187e7 4740
ffbf1eaa 4741 /* if error occurred and interface exists already */
a35187e7 4742 if (ret == -ENFILE && if_nametoindex(ifname)) {
2aec4f3c
JM
4743 if (use_existing) {
4744 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4745 ifname);
6997f8ba
JM
4746 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4747 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4748 addr) < 0 &&
4749 (linux_set_iface_flags(drv->global->ioctl_sock,
4750 ifname, 0) < 0 ||
4751 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4752 addr) < 0 ||
4753 linux_set_iface_flags(drv->global->ioctl_sock,
4754 ifname, 1) < 0))
4755 return -1;
2aec4f3c
JM
4756 return -ENFILE;
4757 }
a35187e7
KH
4758 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4759
4760 /* Try to remove the interface that was already there. */
4761 nl80211_remove_iface(drv, if_nametoindex(ifname));
4762
4763 /* Try to create the interface again */
fbbfcbac 4764 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
d6dcfcda 4765 wds, handler, arg);
a35187e7
KH
4766 }
4767
3e208481
JM
4768 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4769 wpa_printf(MSG_DEBUG,
4770 "nl80211: Interface %s created for P2P - disable 11b rates",
4771 ifname);
4e5cb1a3 4772 nl80211_disable_11b_rates(drv, ret, 1);
3e208481 4773 }
4e5cb1a3 4774
a35187e7
KH
4775 return ret;
4776}
0915d02c 4777
2135f224 4778
3fd1cefb
JB
4779static int nl80211_setup_ap(struct i802_bss *bss)
4780{
4781 struct wpa_driver_nl80211_data *drv = bss->drv;
4782
748c0ac0
JM
4783 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4784 bss->ifname, drv->device_ap_sme, drv->use_monitor);
36488c05 4785
a11241fa
JB
4786 /*
4787 * Disable Probe Request reporting unless we need it in this way for
4788 * devices that include the AP SME, in the other case (unless using
4789 * monitor iface) we'll get it through the nl_mgmt socket instead.
4790 */
4791 if (!drv->device_ap_sme)
4792 wpa_driver_nl80211_probe_req_report(bss, 0);
4793
4794 if (!drv->device_ap_sme && !drv->use_monitor)
4795 if (nl80211_mgmt_subscribe_ap(bss))
4796 return -1;
4797
a6cc0602
JM
4798 if (drv->device_ap_sme && !drv->use_monitor)
4799 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
f4830bed
RM
4800 wpa_printf(MSG_DEBUG,
4801 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
a6cc0602 4802
a11241fa 4803 if (!drv->device_ap_sme && drv->use_monitor &&
ea19b39f
RM
4804 nl80211_create_monitor_interface(drv) &&
4805 !drv->device_ap_sme)
3fd1cefb
JB
4806 return -1;
4807
4808 if (drv->device_ap_sme &&
4809 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4810 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4811 "Probe Request frame reporting in AP mode");
4812 /* Try to survive without this */
4813 }
4814
4815 return 0;
4816}
4817
4818
4819static void nl80211_teardown_ap(struct i802_bss *bss)
4820{
4821 struct wpa_driver_nl80211_data *drv = bss->drv;
4822
748c0ac0
JM
4823 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4824 bss->ifname, drv->device_ap_sme, drv->use_monitor);
a6cc0602 4825 if (drv->device_ap_sme) {
3fd1cefb 4826 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
4827 if (!drv->use_monitor)
4828 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4829 } else if (drv->use_monitor)
3fd1cefb 4830 nl80211_remove_monitor_interface(drv);
a11241fa 4831 else
36488c05 4832 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 4833
a70cd0db 4834 nl80211_put_wiphy_data_ap(bss);
3fd1cefb
JB
4835 bss->beacon_set = 0;
4836}
4837
4838
f10bfc9a
JM
4839static int nl80211_send_eapol_data(struct i802_bss *bss,
4840 const u8 *addr, const u8 *data,
d12dab4c 4841 size_t data_len)
f10bfc9a 4842{
d12dab4c
JB
4843 struct sockaddr_ll ll;
4844 int ret;
4845
4846 if (bss->drv->eapol_tx_sock < 0) {
4847 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
4848 return -1;
4849 }
4850
d12dab4c
JB
4851 os_memset(&ll, 0, sizeof(ll));
4852 ll.sll_family = AF_PACKET;
4853 ll.sll_ifindex = bss->ifindex;
4854 ll.sll_protocol = htons(ETH_P_PAE);
4855 ll.sll_halen = ETH_ALEN;
4856 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4857 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4858 (struct sockaddr *) &ll, sizeof(ll));
4859 if (ret < 0)
4860 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4861 strerror(errno));
4862
4863 return ret;
f10bfc9a 4864}
5fb1a232 4865
f10bfc9a 4866
db149ac9
JM
4867static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4868
4869static int wpa_driver_nl80211_hapd_send_eapol(
4870 void *priv, const u8 *addr, const u8 *data,
4378fc14 4871 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 4872{
a2e40bb6
FF
4873 struct i802_bss *bss = priv;
4874 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
4875 struct ieee80211_hdr *hdr;
4876 size_t len;
4877 u8 *pos;
4878 int res;
4378fc14 4879 int qos = flags & WPA_STA_WMM;
db149ac9 4880
a11241fa 4881 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 4882 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 4883
db149ac9
JM
4884 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4885 data_len;
4886 hdr = os_zalloc(len);
4887 if (hdr == NULL) {
7ac3616d
JM
4888 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4889 (unsigned long) len);
db149ac9
JM
4890 return -1;
4891 }
4892
4893 hdr->frame_control =
4894 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4895 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4896 if (encrypt)
4897 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
4898 if (qos) {
4899 hdr->frame_control |=
4900 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4901 }
db149ac9
JM
4902
4903 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4904 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4905 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4906 pos = (u8 *) (hdr + 1);
4907
db149ac9 4908 if (qos) {
92d521d8
FF
4909 /* Set highest priority in QoS header */
4910 pos[0] = 7;
db149ac9
JM
4911 pos[1] = 0;
4912 pos += 2;
4913 }
db149ac9
JM
4914
4915 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4916 pos += sizeof(rfc1042_header);
4917 WPA_PUT_BE16(pos, ETH_P_PAE);
4918 pos += 2;
4919 memcpy(pos, data, data_len);
4920
55231068 4921 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
2d3943ce 4922 0, 0, 0, 0, NULL, 0);
db149ac9
JM
4923 if (res < 0) {
4924 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4925 "failed: %d (%s)",
4926 (unsigned long) len, errno, strerror(errno));
4927 }
7bf12757 4928 os_free(hdr);
db149ac9
JM
4929
4930 return res;
4931}
4932
a8d6ffa4 4933
3234cba4 4934static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
f97e3ce4
JM
4935 unsigned int total_flags,
4936 unsigned int flags_or,
4937 unsigned int flags_and)
a8d6ffa4 4938{
a2e40bb6 4939 struct i802_bss *bss = priv;
8970bae8
JB
4940 struct nl_msg *msg;
4941 struct nlattr *flags;
7e76ee9c 4942 struct nl80211_sta_flag_update upd;
a8d6ffa4 4943
0cd9846c
JM
4944 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4945 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4946 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4947 !!(total_flags & WPA_STA_AUTHORIZED));
4948
13f83980 4949 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
4950 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4951 goto fail;
a8d6ffa4 4952
7e76ee9c
JM
4953 /*
4954 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4955 * can be removed eventually.
4956 */
8970bae8 4957 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
a862e4a3
JM
4958 if (!flags ||
4959 ((total_flags & WPA_STA_AUTHORIZED) &&
4960 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4961 ((total_flags & WPA_STA_WMM) &&
4962 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4963 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4964 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4965 ((total_flags & WPA_STA_MFP) &&
4966 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4967 ((total_flags & WPA_STA_TDLS_PEER) &&
4968 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4969 goto fail;
45b722f1 4970
8970bae8 4971 nla_nest_end(msg, flags);
a8d6ffa4 4972
7e76ee9c
JM
4973 os_memset(&upd, 0, sizeof(upd));
4974 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4975 upd.set = sta_flags_nl80211(flags_or);
a862e4a3
JM
4976 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4977 goto fail;
7e76ee9c 4978
13f83980 4979 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a862e4a3 4980fail:
5883168a 4981 nlmsg_free(msg);
a8d6ffa4
JM
4982 return -ENOBUFS;
4983}
4984
0915d02c 4985
1581b38b
JM
4986static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4987 struct wpa_driver_associate_params *params)
4988{
708bc8e0 4989 enum nl80211_iftype nlmode, old_mode;
b1f625e0
EP
4990
4991 if (params->p2p) {
046b26a2
JM
4992 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4993 "group (GO)");
b1f625e0
EP
4994 nlmode = NL80211_IFTYPE_P2P_GO;
4995 } else
4996 nlmode = NL80211_IFTYPE_AP;
4997
708bc8e0 4998 old_mode = drv->nlmode;
834ee56f 4999 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
708bc8e0
JM
5000 nl80211_remove_monitor_interface(drv);
5001 return -1;
5002 }
5003
6e9023ea
JM
5004 if (params->freq.freq &&
5005 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
708bc8e0 5006 if (old_mode != nlmode)
834ee56f 5007 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
460456f8 5008 nl80211_remove_monitor_interface(drv);
1581b38b 5009 return -1;
0915d02c 5010 }
1581b38b 5011
1581b38b
JM
5012 return 0;
5013}
1581b38b
JM
5014
5015
666e508c
JM
5016static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5017 int reset_mode)
5cc4d64b
JM
5018{
5019 struct nl_msg *msg;
9725b784 5020 int ret;
a862e4a3 5021
9725b784 5022 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b 5023 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5cc4d64b
JM
5024 if (ret) {
5025 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5026 "(%s)", ret, strerror(-ret));
9725b784
JM
5027 } else {
5028 wpa_printf(MSG_DEBUG,
5029 "nl80211: Leave IBSS request sent successfully");
5cc4d64b
JM
5030 }
5031
666e508c
JM
5032 if (reset_mode &&
5033 wpa_driver_nl80211_set_mode(drv->first_bss,
5d4c78fb
JM
5034 NL80211_IFTYPE_STATION)) {
5035 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5036 "station mode");
5037 }
5038
5cc4d64b
JM
5039 return ret;
5040}
5041
5042
95ff3069
JD
5043static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5044 struct wpa_driver_associate_params *params)
5045{
5046 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5047 return -1;
5048
5049 if (params->htcaps && params->htcaps_mask) {
5050 int sz = sizeof(struct ieee80211_ht_capabilities);
5051 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5052 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5053 params->htcaps_mask, sz);
5054 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5055 params->htcaps) ||
5056 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5057 params->htcaps_mask))
5058 return -1;
5059 }
5060
5061#ifdef CONFIG_VHT_OVERRIDES
5062 if (params->disable_vht) {
5063 wpa_printf(MSG_DEBUG, " * VHT disabled");
5064 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5065 return -1;
5066 }
5067
5068 if (params->vhtcaps && params->vhtcaps_mask) {
5069 int sz = sizeof(struct ieee80211_vht_capabilities);
5070 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5071 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5072 params->vhtcaps_mask, sz);
5073 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5074 params->vhtcaps) ||
5075 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5076 params->vhtcaps_mask))
5077 return -1;
5078 }
5079#endif /* CONFIG_VHT_OVERRIDES */
5080
5081 return 0;
5082}
5083
5084
5cc4d64b
JM
5085static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5086 struct wpa_driver_associate_params *params)
5087{
5088 struct nl_msg *msg;
5089 int ret = -1;
5090 int count = 0;
5091
5092 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5093
4ec68377 5094 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
5cc4d64b
JM
5095 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5096 "IBSS mode");
5097 return -1;
5098 }
5099
5100retry:
9725b784 5101 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
a862e4a3
JM
5102 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5103 goto fail;
5cc4d64b
JM
5104
5105 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5106 params->ssid, params->ssid_len);
a862e4a3
JM
5107 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5108 goto fail;
5cc4d64b
JM
5109 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5110 drv->ssid_len = params->ssid_len;
5111
85e1fad8
JM
5112 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5113 nl80211_put_beacon_int(msg, params->beacon_int))
a862e4a3 5114 goto fail;
5cc4d64b
JM
5115
5116 ret = nl80211_set_conn_keys(params, msg);
5117 if (ret)
a862e4a3 5118 goto fail;
5cc4d64b 5119
913e3cf7
NC
5120 if (params->bssid && params->fixed_bssid) {
5121 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5122 MAC2STR(params->bssid));
a862e4a3
JM
5123 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5124 goto fail;
913e3cf7
NC
5125 }
5126
4d9e6fba
JD
5127 if (params->fixed_freq) {
5128 wpa_printf(MSG_DEBUG, " * fixed_freq");
5129 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5130 goto fail;
5131 }
5132
4848a38d
JM
5133 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5134 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5135 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5136 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
e640888c 5137 wpa_printf(MSG_DEBUG, " * control port");
a862e4a3
JM
5138 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5139 goto fail;
e640888c
AQ
5140 }
5141
a95795ad
JM
5142 if (params->wpa_ie) {
5143 wpa_hexdump(MSG_DEBUG,
5144 " * Extra IEs for Beacon/Probe Response frames",
5145 params->wpa_ie, params->wpa_ie_len);
a862e4a3
JM
5146 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5147 params->wpa_ie))
5148 goto fail;
a95795ad
JM
5149 }
5150
e15dcf6d
PK
5151 ret = nl80211_ht_vht_overrides(msg, params);
5152 if (ret < 0)
5153 goto fail;
95ff3069 5154
5cc4d64b
JM
5155 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5156 msg = NULL;
5157 if (ret) {
5158 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5159 ret, strerror(-ret));
5160 count++;
5161 if (ret == -EALREADY && count == 1) {
5162 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5163 "forced leave");
666e508c 5164 nl80211_leave_ibss(drv, 0);
5cc4d64b
JM
5165 nlmsg_free(msg);
5166 goto retry;
5167 }
a862e4a3
JM
5168 } else {
5169 wpa_printf(MSG_DEBUG,
5170 "nl80211: Join IBSS request sent successfully");
5cc4d64b 5171 }
5cc4d64b 5172
a862e4a3 5173fail:
5cc4d64b
JM
5174 nlmsg_free(msg);
5175 return ret;
5176}
5177
5178
ad295f3b
VK
5179static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5180 struct wpa_driver_associate_params *params,
5181 struct nl_msg *msg)
5182{
5183 if (params->fils_erp_username_len) {
5184 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5185 params->fils_erp_username,
5186 params->fils_erp_username_len);
5187 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5188 params->fils_erp_username_len,
5189 params->fils_erp_username))
5190 return -1;
5191 }
5192
5193 if (params->fils_erp_realm_len) {
5194 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5195 params->fils_erp_realm,
5196 params->fils_erp_realm_len);
5197 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5198 params->fils_erp_realm_len, params->fils_erp_realm))
5199 return -1;
5200 }
5201
5202 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5203 params->fils_erp_next_seq_num);
5204 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5205 params->fils_erp_next_seq_num))
5206 return -1;
5207
5208 if (params->fils_erp_rrk_len) {
5209 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5210 (unsigned long) params->fils_erp_rrk_len);
5211 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5212 params->fils_erp_rrk_len, params->fils_erp_rrk))
5213 return -1;
5214 }
5215
5216 return 0;
5217}
5218
5219
a0bdd191
JM
5220static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5221 struct wpa_driver_associate_params *params,
5222 struct nl_msg *msg)
cfaab580 5223{
c85dfc6f
JM
5224 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5225 return -1;
5226
cfaab580
ZY
5227 if (params->bssid) {
5228 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5229 MAC2STR(params->bssid));
a862e4a3
JM
5230 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5231 return -1;
cfaab580 5232 }
a0bdd191 5233
7ac7fd43
DS
5234 if (params->bssid_hint) {
5235 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5236 MAC2STR(params->bssid_hint));
a862e4a3
JM
5237 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5238 params->bssid_hint))
5239 return -1;
7ac7fd43
DS
5240 }
5241
4ec68377
JD
5242 if (params->freq.freq) {
5243 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
a862e4a3
JM
5244 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5245 params->freq.freq))
5246 return -1;
4ec68377 5247 drv->assoc_freq = params->freq.freq;
30158a0d
SD
5248 } else
5249 drv->assoc_freq = 0;
a0bdd191 5250
7ac7fd43
DS
5251 if (params->freq_hint) {
5252 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
a862e4a3
JM
5253 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5254 params->freq_hint))
5255 return -1;
7ac7fd43
DS
5256 }
5257
1f6c0ab8
BS
5258 if (params->bg_scan_period >= 0) {
5259 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5260 params->bg_scan_period);
a862e4a3
JM
5261 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5262 params->bg_scan_period))
5263 return -1;
1f6c0ab8 5264 }
a0bdd191 5265
cfaab580
ZY
5266 if (params->ssid) {
5267 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5268 params->ssid, params->ssid_len);
a862e4a3
JM
5269 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5270 params->ssid))
5271 return -1;
cfaab580 5272 if (params->ssid_len > sizeof(drv->ssid))
a862e4a3 5273 return -1;
cfaab580
ZY
5274 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5275 drv->ssid_len = params->ssid_len;
5276 }
a0bdd191 5277
cfaab580 5278 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
a862e4a3
JM
5279 if (params->wpa_ie &&
5280 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5281 return -1;
cfaab580 5282
64fa840a
JM
5283 if (params->wpa_proto) {
5284 enum nl80211_wpa_versions ver = 0;
cfaab580 5285
64fa840a
JM
5286 if (params->wpa_proto & WPA_PROTO_WPA)
5287 ver |= NL80211_WPA_VERSION_1;
5288 if (params->wpa_proto & WPA_PROTO_RSN)
5289 ver |= NL80211_WPA_VERSION_2;
cfaab580 5290
64fa840a 5291 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
a862e4a3
JM
5292 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5293 return -1;
cfaab580
ZY
5294 }
5295
4848a38d 5296 if (params->pairwise_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
5297 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5298 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
a862e4a3
JM
5299 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5300 cipher))
5301 return -1;
cfaab580
ZY
5302 }
5303
ae6f9272
JM
5304 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5305 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5306 /*
5307 * This is likely to work even though many drivers do not
5308 * advertise support for operations without GTK.
5309 */
5310 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5311 } else if (params->group_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
5312 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5313 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
a862e4a3
JM
5314 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5315 return -1;
cfaab580
ZY
5316 }
5317
4848a38d
JM
5318 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5319 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5320 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5321 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
163f801e 5322 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8802326f
JJ
5323 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5324 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
666497c8 5325 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5e3b5197 5326 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
ad295f3b
VK
5327 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5328 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5329 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5330 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
e005725a
SD
5331 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5332 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5333 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
3aa24db9 5334 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
cfaab580
ZY
5335
5336 switch (params->key_mgmt_suite) {
4848a38d 5337 case WPA_KEY_MGMT_CCKM:
3aa24db9 5338 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
369c8d7b 5339 break;
4848a38d 5340 case WPA_KEY_MGMT_IEEE8021X:
3aa24db9 5341 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
cfaab580 5342 break;
4848a38d 5343 case WPA_KEY_MGMT_FT_IEEE8021X:
3aa24db9 5344 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
6a1ce395 5345 break;
4848a38d 5346 case WPA_KEY_MGMT_FT_PSK:
3aa24db9 5347 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
6a1ce395 5348 break;
8802326f 5349 case WPA_KEY_MGMT_IEEE8021X_SHA256:
3aa24db9 5350 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
8802326f
JJ
5351 break;
5352 case WPA_KEY_MGMT_PSK_SHA256:
3aa24db9 5353 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
8802326f 5354 break;
163f801e 5355 case WPA_KEY_MGMT_OSEN:
3aa24db9 5356 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
163f801e 5357 break;
666497c8 5358 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
3aa24db9 5359 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
666497c8 5360 break;
5e3b5197 5361 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
3aa24db9 5362 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
5e3b5197 5363 break;
ad295f3b
VK
5364 case WPA_KEY_MGMT_FILS_SHA256:
5365 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5366 break;
5367 case WPA_KEY_MGMT_FILS_SHA384:
5368 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5369 break;
5370 case WPA_KEY_MGMT_FT_FILS_SHA256:
5371 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5372 break;
5373 case WPA_KEY_MGMT_FT_FILS_SHA384:
5374 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5375 break;
e005725a
SD
5376 case WPA_KEY_MGMT_OWE:
5377 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5378 break;
5379 case WPA_KEY_MGMT_DPP:
5380 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5381 break;
4848a38d 5382 case WPA_KEY_MGMT_PSK:
cfaab580 5383 default:
3aa24db9 5384 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
cfaab580
ZY
5385 break;
5386 }
163f801e 5387 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
a862e4a3
JM
5388 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5389 return -1;
cfaab580
ZY
5390 }
5391
730c5a1d
EP
5392 /* Add PSK in case of 4-way handshake offload */
5393 if (params->psk &&
5394 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)) {
5395 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5396 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5397 return -1;
5398 }
5399
a862e4a3
JM
5400 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5401 return -1;
a0bdd191 5402
e3429c0b
JB
5403 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5404 (params->pairwise_suite == WPA_CIPHER_NONE ||
5405 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5406 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5407 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5408 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5409 return -1;
5410
a862e4a3
JM
5411 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5412 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5413 return -1;
a565084f 5414
58162adf
AK
5415 if (params->rrm_used) {
5416 u32 drv_rrm_flags = drv->capa.rrm_flags;
b5d172e5
BL
5417 if ((!((drv_rrm_flags &
5418 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5419 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5420 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
a862e4a3
JM
5421 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5422 return -1;
58162adf
AK
5423 }
5424
95ff3069 5425 if (nl80211_ht_vht_overrides(msg, params) < 0)
a862e4a3 5426 return -1;
80e8a5ee 5427
a0bdd191
JM
5428 if (params->p2p)
5429 wpa_printf(MSG_DEBUG, " * P2P group");
5430
86b5c400
LD
5431 if (params->pbss) {
5432 wpa_printf(MSG_DEBUG, " * PBSS");
5433 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5434 return -1;
5435 }
5436
1126c078 5437 drv->connect_reassoc = 0;
00c3c4ac
JM
5438 if (params->prev_bssid) {
5439 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5440 MAC2STR(params->prev_bssid));
5441 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5442 params->prev_bssid))
5443 return -1;
1126c078 5444 drv->connect_reassoc = 1;
00c3c4ac
JM
5445 }
5446
ad295f3b
VK
5447 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5448 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5449 return -1;
5450
ba71cb82
SD
5451 if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5452 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5453 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5454 return -1;
5455
a0bdd191 5456 return 0;
a0bdd191
JM
5457}
5458
5459
5460static int wpa_driver_nl80211_try_connect(
5461 struct wpa_driver_nl80211_data *drv,
40a68f33
SD
5462 struct wpa_driver_associate_params *params,
5463 struct nl_handle *nl_connect)
a0bdd191
JM
5464{
5465 struct nl_msg *msg;
5466 enum nl80211_auth_type type;
5467 int ret;
5468 int algs;
5469
b658547d 5470#ifdef CONFIG_DRIVER_NL80211_QCA
b41f2684
CL
5471 if (params->req_key_mgmt_offload && params->psk &&
5472 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5473 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5474 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5475 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5476 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5477 if (ret)
5478 return ret;
5479 }
b658547d 5480#endif /* CONFIG_DRIVER_NL80211_QCA */
b41f2684 5481
9725b784
JM
5482 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5483 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
a0bdd191
JM
5484 if (!msg)
5485 return -1;
5486
a0bdd191
JM
5487 ret = nl80211_connect_common(drv, params, msg);
5488 if (ret)
a862e4a3 5489 goto fail;
a0bdd191
JM
5490
5491 algs = 0;
5492 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5493 algs++;
5494 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5495 algs++;
5496 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5497 algs++;
64a0a75b
JM
5498 if (params->auth_alg & WPA_AUTH_ALG_FILS)
5499 algs++;
a0bdd191
JM
5500 if (algs > 1) {
5501 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5502 "selection");
5503 goto skip_auth_type;
5504 }
5505
3c67e977 5506 type = get_nl_auth_type(params->auth_alg);
a0bdd191 5507 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3c67e977
VK
5508 if (type == NL80211_AUTHTYPE_MAX ||
5509 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
a862e4a3 5510 goto fail;
a0bdd191
JM
5511
5512skip_auth_type:
cfaab580
ZY
5513 ret = nl80211_set_conn_keys(params, msg);
5514 if (ret)
a862e4a3 5515 goto fail;
cfaab580 5516
40a68f33
SD
5517 if (nl_connect)
5518 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
5519 else
5520 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5521
cfaab580
ZY
5522 msg = NULL;
5523 if (ret) {
5524 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5525 "(%s)", ret, strerror(-ret));
a862e4a3
JM
5526 } else {
5527 wpa_printf(MSG_DEBUG,
5528 "nl80211: Connect request send successfully");
cfaab580 5529 }
cfaab580 5530
a862e4a3 5531fail:
cfaab580
ZY
5532 nlmsg_free(msg);
5533 return ret;
5534
5535}
5536
5537
a8c5b43a
CW
5538static int wpa_driver_nl80211_connect(
5539 struct wpa_driver_nl80211_data *drv,
40a68f33
SD
5540 struct wpa_driver_associate_params *params,
5541 struct nl_handle *nl_connect)
a8c5b43a 5542{
0d4e3d1d
JJ
5543 int ret;
5544
5545 /* Store the connection attempted bssid for future use */
5546 if (params->bssid)
5547 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5548 else
5549 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5550
40a68f33 5551 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
a8c5b43a
CW
5552 if (ret == -EALREADY) {
5553 /*
5554 * cfg80211 does not currently accept new connections if
5555 * we are already connected. As a workaround, force
5556 * disconnection and try again.
5557 */
5558 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5559 "disconnecting before reassociation "
5560 "attempt");
5561 if (wpa_driver_nl80211_disconnect(
5562 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5563 return -1;
40a68f33 5564 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
a8c5b43a
CW
5565 }
5566 return ret;
5567}
5568
5569
c2a04078
JM
5570static int wpa_driver_nl80211_associate(
5571 void *priv, struct wpa_driver_associate_params *params)
5572{
a2e40bb6
FF
5573 struct i802_bss *bss = priv;
5574 struct wpa_driver_nl80211_data *drv = bss->drv;
a862e4a3 5575 int ret = -1;
c2a04078
JM
5576 struct nl_msg *msg;
5577
4bd71954
JM
5578 nl80211_unmask_11b_rates(bss);
5579
5cc4d64b 5580 if (params->mode == IEEE80211_MODE_AP)
1581b38b 5581 return wpa_driver_nl80211_ap(drv, params);
1581b38b 5582
5cc4d64b
JM
5583 if (params->mode == IEEE80211_MODE_IBSS)
5584 return wpa_driver_nl80211_ibss(drv, params);
5585
4a867032 5586 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
5587 enum nl80211_iftype nlmode = params->p2p ?
5588 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
40a68f33 5589 struct nl_handle *nl_connect = NULL;
b1f625e0
EP
5590
5591 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 5592 return -1;
40a68f33
SD
5593 if (params->auth_alg & WPA_AUTH_ALG_SAE)
5594 nl_connect = bss->nl_connect;
5595 return wpa_driver_nl80211_connect(drv, params, nl_connect);
4a867032 5596 }
cfaab580 5597
add9b7a4 5598 nl80211_mark_disconnected(drv);
c2a04078 5599
c2a04078
JM
5600 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5601 drv->ifindex);
9725b784
JM
5602 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
5603 if (!msg)
5604 return -1;
c2a04078 5605
a0bdd191
JM
5606 ret = nl80211_connect_common(drv, params, msg);
5607 if (ret)
a862e4a3 5608 goto fail;
01652550 5609
40a45727
JM
5610 if (params->fils_kek) {
5611 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5612 (unsigned int) params->fils_kek_len);
5613 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5614 params->fils_kek))
5615 goto fail;
5616 }
5617 if (params->fils_nonces) {
5618 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5619 params->fils_nonces,
5620 params->fils_nonces_len);
5621 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5622 params->fils_nonces_len, params->fils_nonces))
5623 goto fail;
5624 }
5625
c2a04078
JM
5626 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5627 msg = NULL;
5628 if (ret) {
3b7ea880
BG
5629 wpa_dbg(drv->ctx, MSG_DEBUG,
5630 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5631 ret, strerror(-ret));
8856462d 5632 nl80211_dump_scan(drv);
a862e4a3
JM
5633 } else {
5634 wpa_printf(MSG_DEBUG,
5635 "nl80211: Association request send successfully");
c2a04078 5636 }
c2a04078 5637
a862e4a3 5638fail:
c2a04078
JM
5639 nlmsg_free(msg);
5640 return ret;
5641}
3f5285e8
JM
5642
5643
ad1e68e6 5644static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 5645 int ifindex, enum nl80211_iftype mode)
3f5285e8 5646{
3f5285e8 5647 struct nl_msg *msg;
ad1e68e6
JM
5648 int ret = -ENOBUFS;
5649
a1922f93
JM
5650 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5651 ifindex, mode, nl80211_iftype_str(mode));
5652
56f77852
JM
5653 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5654 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
a862e4a3 5655 goto fail;
ad1e68e6
JM
5656
5657 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5658 msg = NULL;
ad1e68e6
JM
5659 if (!ret)
5660 return 0;
a862e4a3 5661fail:
5883168a 5662 nlmsg_free(msg);
ad1e68e6
JM
5663 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5664 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5665 return ret;
5666}
5667
5668
3c5d34e3
CW
5669static int wpa_driver_nl80211_set_mode_impl(
5670 struct i802_bss *bss,
5671 enum nl80211_iftype nlmode,
5672 struct hostapd_freq_params *desired_freq_params)
ad1e68e6 5673{
a2e40bb6 5674 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 5675 int ret = -1;
26af9dca 5676 int i;
86957e62 5677 int was_ap = is_ap_interface(drv->nlmode);
671a5039 5678 int res;
ebffdbc4 5679 int mode_switch_res;
1581b38b 5680
a5a187b0
JM
5681 if (TEST_FAIL())
5682 return -1;
5683
ebffdbc4
CW
5684 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5685 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5686 mode_switch_res = 0;
8e12685c 5687
ebffdbc4 5688 if (mode_switch_res == 0) {
ad1e68e6 5689 drv->nlmode = nlmode;
460456f8
JM
5690 ret = 0;
5691 goto done;
ad1e68e6 5692 }
3f5285e8 5693
ebffdbc4 5694 if (mode_switch_res == -ENODEV)
671a5039
JM
5695 return -1;
5696
460456f8 5697 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
5698 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5699 "requested mode - ignore error");
460456f8
JM
5700 ret = 0;
5701 goto done; /* Already in the requested mode */
5702 }
3f5285e8 5703
3f5285e8
JM
5704 /* mac80211 doesn't allow mode changes while the device is up, so
5705 * take the device down, try to set the mode again, and bring the
5706 * device back up.
5707 */
26af9dca
JM
5708 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5709 "interface down");
5710 for (i = 0; i < 10; i++) {
91724d6f 5711 res = i802_set_iface_flags(bss, 0);
6e8183d7
JM
5712 if (res == -EACCES || res == -ENODEV)
5713 break;
ebffdbc4 5714 if (res != 0) {
26af9dca
JM
5715 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5716 "interface down");
ebffdbc4
CW
5717 os_sleep(0, 100000);
5718 continue;
5719 }
3c5d34e3
CW
5720
5721 /*
5722 * Setting the mode will fail for some drivers if the phy is
5723 * on a frequency that the mode is disallowed in.
5724 */
5725 if (desired_freq_params) {
bdf5ccb2 5726 res = nl80211_set_channel(bss, desired_freq_params, 0);
3c5d34e3
CW
5727 if (res) {
5728 wpa_printf(MSG_DEBUG,
5729 "nl80211: Failed to set frequency on interface");
5730 }
5731 }
5732
ebffdbc4
CW
5733 /* Try to set the mode again while the interface is down */
5734 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5735 if (mode_switch_res == -EBUSY) {
5736 wpa_printf(MSG_DEBUG,
5737 "nl80211: Delaying mode set while interface going down");
5738 os_sleep(0, 100000);
5739 continue;
5740 }
5741 ret = mode_switch_res;
5742 break;
3f5285e8
JM
5743 }
5744
c6e8e8e4
JM
5745 if (!ret) {
5746 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5747 "interface is down");
ad1e68e6 5748 drv->nlmode = nlmode;
7d9c3698 5749 drv->ignore_if_down_event = 1;
c6e8e8e4 5750 }
ad1e68e6 5751
ebffdbc4
CW
5752 /* Bring the interface back up */
5753 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5754 if (res != 0) {
5755 wpa_printf(MSG_DEBUG,
5756 "nl80211: Failed to set interface up after switching mode");
5757 ret = -1;
5758 }
5759
460456f8 5760done:
3fd1cefb
JB
5761 if (ret) {
5762 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5763 "from %d failed", nlmode, drv->nlmode);
5764 return ret;
5765 }
5766
3e208481
JM
5767 if (is_p2p_net_interface(nlmode)) {
5768 wpa_printf(MSG_DEBUG,
5769 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5770 bss->ifname);
edb9bfba 5771 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
3e208481
JM
5772 } else if (drv->disabled_11b_rates) {
5773 wpa_printf(MSG_DEBUG,
5774 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5775 bss->ifname);
1d0c6fb1 5776 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3e208481 5777 }
edb9bfba 5778
3fd1cefb 5779 if (is_ap_interface(nlmode)) {
36488c05 5780 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 5781 /* Setup additional AP mode functionality if needed */
3fd1cefb 5782 if (nl80211_setup_ap(bss))
460456f8 5783 return -1;
3fd1cefb 5784 } else if (was_ap) {
460456f8 5785 /* Remove additional AP mode functionality */
3fd1cefb 5786 nl80211_teardown_ap(bss);
a11241fa 5787 } else {
36488c05 5788 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 5789 }
460456f8 5790
afb0550a
BC
5791 if (is_mesh_interface(nlmode) &&
5792 nl80211_mgmt_subscribe_mesh(bss))
5793 return -1;
5794
873d0fcf 5795 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
afb0550a 5796 !is_mesh_interface(nlmode) &&
a11241fa
JB
5797 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5798 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5799 "frame processing - ignore for now");
08359050 5800
3fd1cefb 5801 return 0;
3f5285e8
JM
5802}
5803
5804
f3407c66
JM
5805int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5806 enum nl80211_iftype nlmode)
3c5d34e3
CW
5807{
5808 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5809}
5810
5811
4ec68377
JD
5812static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5813 struct hostapd_freq_params *freq)
3c5d34e3 5814{
3c5d34e3 5815 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
4ec68377 5816 freq);
3c5d34e3 5817}
65d645ce
AS
5818
5819
3f5285e8
JM
5820static int wpa_driver_nl80211_get_capa(void *priv,
5821 struct wpa_driver_capa *capa)
5822{
a2e40bb6
FF
5823 struct i802_bss *bss = priv;
5824 struct wpa_driver_nl80211_data *drv = bss->drv;
65d645ce 5825
3f5285e8
JM
5826 if (!drv->has_capability)
5827 return -1;
5828 os_memcpy(capa, &drv->capa, sizeof(*capa));
8cd6b7bc
JB
5829 if (drv->extended_capa && drv->extended_capa_mask) {
5830 capa->extended_capa = drv->extended_capa;
5831 capa->extended_capa_mask = drv->extended_capa_mask;
5832 capa->extended_capa_len = drv->extended_capa_len;
5833 }
851b0c55 5834
906bc4c7 5835 return 0;
3f5285e8
JM
5836}
5837
5838
5839static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5840{
a2e40bb6
FF
5841 struct i802_bss *bss = priv;
5842 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 5843
90a545cc
JM
5844 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5845 bss->ifname, drv->operstate, state,
5846 state ? "UP" : "DORMANT");
3f5285e8 5847 drv->operstate = state;
36d84860 5848 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 5849 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
5850}
5851
01652550
JM
5852
5853static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5854{
a2e40bb6
FF
5855 struct i802_bss *bss = priv;
5856 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
5857 struct nl_msg *msg;
5858 struct nl80211_sta_flag_update upd;
a862e4a3 5859 int ret;
2eef5177
JM
5860
5861 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5862 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5863 return 0;
5864 }
01652550 5865
1ba51ec0
JM
5866 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5867 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5868
01652550
JM
5869 os_memset(&upd, 0, sizeof(upd));
5870 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5871 if (authorized)
5872 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
a862e4a3 5873
13f83980 5874 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
5875 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5876 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5877 nlmsg_free(msg);
5878 return -ENOBUFS;
5879 }
01652550 5880
2eef5177 5881 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2eef5177
JM
5882 if (!ret)
5883 return 0;
2eef5177
JM
5884 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5885 ret, strerror(-ret));
5886 return ret;
01652550
JM
5887}
5888
3f5285e8 5889
f07ead6a
JM
5890/* Set kernel driver on given frequency (MHz) */
5891static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 5892{
f07ead6a 5893 struct i802_bss *bss = priv;
13a524a3 5894 return nl80211_set_channel(bss, freq, 0);
c5121837
JM
5895}
5896
f7b3920c 5897
c5121837
JM
5898static inline int min_int(int a, int b)
5899{
5900 if (a < b)
5901 return a;
5902 return b;
5903}
5904
5905
5906static int get_key_handler(struct nl_msg *msg, void *arg)
5907{
5908 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5909 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5910
5911 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5912 genlmsg_attrlen(gnlh, 0), NULL);
5913
5914 /*
5915 * TODO: validate the key index and mac address!
5916 * Otherwise, there's a race condition as soon as
5917 * the kernel starts sending key notifications.
5918 */
5919
5920 if (tb[NL80211_ATTR_KEY_SEQ])
5921 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5922 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5923 return NL_SKIP;
5924}
5925
5926
5927static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5928 int idx, u8 *seq)
5929{
a2e40bb6
FF
5930 struct i802_bss *bss = priv;
5931 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
5932 struct nl_msg *msg;
5933
95376e1a
JM
5934 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5935 NL80211_CMD_GET_KEY);
5936 if (!msg ||
a862e4a3 5937 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
95376e1a 5938 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
a862e4a3
JM
5939 nlmsg_free(msg);
5940 return -ENOBUFS;
5941 }
c5121837
JM
5942
5943 memset(seq, 0, 6);
5944
5945 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
c5121837
JM
5946}
5947
5948
c5121837
JM
5949static int i802_set_rts(void *priv, int rts)
5950{
a2e40bb6
FF
5951 struct i802_bss *bss = priv;
5952 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451 5953 struct nl_msg *msg;
a862e4a3 5954 int ret;
ad649451 5955 u32 val;
c5121837 5956
ad649451
JM
5957 if (rts >= 2347)
5958 val = (u32) -1;
5959 else
5960 val = rts;
c5121837 5961
9725b784 5962 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
a862e4a3
JM
5963 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5964 nlmsg_free(msg);
5965 return -ENOBUFS;
5966 }
ad649451
JM
5967
5968 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5969 if (!ret)
5970 return 0;
ad649451
JM
5971 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5972 "%d (%s)", rts, ret, strerror(-ret));
5973 return ret;
c5121837
JM
5974}
5975
5976
5977static int i802_set_frag(void *priv, int frag)
5978{
a2e40bb6
FF
5979 struct i802_bss *bss = priv;
5980 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451 5981 struct nl_msg *msg;
a862e4a3 5982 int ret;
ad649451 5983 u32 val;
c5121837 5984
ad649451
JM
5985 if (frag >= 2346)
5986 val = (u32) -1;
5987 else
5988 val = frag;
c5121837 5989
9725b784 5990 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
a862e4a3
JM
5991 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5992 nlmsg_free(msg);
5993 return -ENOBUFS;
5994 }
ad649451
JM
5995
5996 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5997 if (!ret)
5998 return 0;
ad649451
JM
5999 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6000 "%d: %d (%s)", frag, ret, strerror(-ret));
6001 return ret;
c5121837
JM
6002}
6003
6004
c5121837
JM
6005static int i802_flush(void *priv)
6006{
a2e40bb6 6007 struct i802_bss *bss = priv;
c5121837 6008 struct nl_msg *msg;
82554b10 6009 int res;
c5121837 6010
83e7bb0e
JM
6011 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6012 bss->ifname);
c5121837
JM
6013
6014 /*
6015 * XXX: FIX! this needs to flush all VLANs too
6016 */
13f83980
JM
6017 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6018 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
82554b10
JM
6019 if (res) {
6020 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6021 "(%s)", res, strerror(-res));
6022 }
6023 return res;
c5121837
JM
6024}
6025
6026
6027static int get_sta_handler(struct nl_msg *msg, void *arg)
6028{
6029 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6030 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6031 struct hostap_sta_driver_data *data = arg;
6032 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6033 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6034 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6035 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6036 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6037 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6038 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 6039 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
43022abd
NL
6040 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6041 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
3567641e 6042 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
6043 };
6044 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6045 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6046 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6047 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6048 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6049 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6050 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6051 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
c5121837
JM
6052 };
6053
6054 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6055 genlmsg_attrlen(gnlh, 0), NULL);
6056
6057 /*
6058 * TODO: validate the interface and mac address!
6059 * Otherwise, there's a race condition as soon as
6060 * the kernel starts sending station notifications.
6061 */
6062
6063 if (!tb[NL80211_ATTR_STA_INFO]) {
6064 wpa_printf(MSG_DEBUG, "sta stats missing!");
6065 return NL_SKIP;
6066 }
6067 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6068 tb[NL80211_ATTR_STA_INFO],
6069 stats_policy)) {
6070 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6071 return NL_SKIP;
6072 }
6073
6074 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6075 data->inactive_msec =
6076 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
43022abd 6077 /* For backwards compatibility, fetch the 32-bit counters first. */
c5121837
JM
6078 if (stats[NL80211_STA_INFO_RX_BYTES])
6079 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6080 if (stats[NL80211_STA_INFO_TX_BYTES])
6081 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
43022abd
NL
6082 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6083 stats[NL80211_STA_INFO_TX_BYTES64]) {
6084 /*
6085 * The driver supports 64-bit counters, so use them to override
6086 * the 32-bit values.
6087 */
6088 data->rx_bytes =
6089 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6090 data->tx_bytes =
6091 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6092 data->bytes_64bit = 1;
6093 }
c5121837
JM
6094 if (stats[NL80211_STA_INFO_RX_PACKETS])
6095 data->rx_packets =
6096 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6097 if (stats[NL80211_STA_INFO_TX_PACKETS])
6098 data->tx_packets =
6099 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
6100 if (stats[NL80211_STA_INFO_TX_FAILED])
6101 data->tx_retry_failed =
6102 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
3567641e 6103 if (stats[NL80211_STA_INFO_SIGNAL])
6104 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
6105
6106 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6107 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6108 stats[NL80211_STA_INFO_TX_BITRATE],
6109 rate_policy) == 0) {
6110 if (rate[NL80211_RATE_INFO_BITRATE32])
6111 data->current_tx_rate =
6112 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6113 else if (rate[NL80211_RATE_INFO_BITRATE])
6114 data->current_tx_rate =
6115 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6116
6117 if (rate[NL80211_RATE_INFO_MCS]) {
6118 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6119 data->flags |= STA_DRV_DATA_TX_MCS;
6120 }
6121 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6122 data->tx_vhtmcs =
6123 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6124 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6125 }
6126 if (rate[NL80211_RATE_INFO_SHORT_GI])
6127 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6128 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6129 data->tx_vht_nss =
6130 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6131 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6132 }
6133 }
6134
6135 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6136 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6137 stats[NL80211_STA_INFO_RX_BITRATE],
6138 rate_policy) == 0) {
6139 if (rate[NL80211_RATE_INFO_BITRATE32])
6140 data->current_rx_rate =
6141 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6142 else if (rate[NL80211_RATE_INFO_BITRATE])
6143 data->current_rx_rate =
6144 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6145
6146 if (rate[NL80211_RATE_INFO_MCS]) {
6147 data->rx_mcs =
6148 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6149 data->flags |= STA_DRV_DATA_RX_MCS;
6150 }
6151 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6152 data->rx_vhtmcs =
6153 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6154 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6155 }
6156 if (rate[NL80211_RATE_INFO_SHORT_GI])
6157 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6158 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6159 data->rx_vht_nss =
6160 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6161 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6162 }
6163 }
c5121837
JM
6164
6165 return NL_SKIP;
6166}
6167
9ebce9c5
JM
6168static int i802_read_sta_data(struct i802_bss *bss,
6169 struct hostap_sta_driver_data *data,
c5121837
JM
6170 const u8 *addr)
6171{
c5121837
JM
6172 struct nl_msg *msg;
6173
13f83980
JM
6174 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6175 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
a862e4a3
JM
6176 nlmsg_free(msg);
6177 return -ENOBUFS;
6178 }
c5121837 6179
13f83980 6180 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
c5121837
JM
6181}
6182
6183
c5121837
JM
6184static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6185 int cw_min, int cw_max, int burst_time)
6186{
a2e40bb6
FF
6187 struct i802_bss *bss = priv;
6188 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
6189 struct nl_msg *msg;
6190 struct nlattr *txq, *params;
6191
13f83980 6192 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
c5121837
JM
6193 if (!msg)
6194 return -1;
6195
c5121837
JM
6196 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6197 if (!txq)
a862e4a3 6198 goto fail;
c5121837
JM
6199
6200 /* We are only sending parameters for a single TXQ at a time */
6201 params = nla_nest_start(msg, 1);
6202 if (!params)
a862e4a3 6203 goto fail;
c5121837 6204
7e3c1781
JM
6205 switch (queue) {
6206 case 0:
a862e4a3
JM
6207 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6208 goto fail;
7e3c1781
JM
6209 break;
6210 case 1:
a862e4a3
JM
6211 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6212 goto fail;
7e3c1781
JM
6213 break;
6214 case 2:
a862e4a3
JM
6215 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6216 goto fail;
7e3c1781
JM
6217 break;
6218 case 3:
a862e4a3
JM
6219 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6220 goto fail;
7e3c1781
JM
6221 break;
6222 }
c5121837
JM
6223 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6224 * 32 usec, so need to convert the value here. */
a862e4a3
JM
6225 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6226 (burst_time * 100 + 16) / 32) ||
6227 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6228 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6229 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6230 goto fail;
c5121837
JM
6231
6232 nla_nest_end(msg, params);
6233
6234 nla_nest_end(msg, txq);
6235
6236 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6237 return 0;
9e088e74 6238 msg = NULL;
a862e4a3 6239fail:
9e088e74 6240 nlmsg_free(msg);
c5121837
JM
6241 return -1;
6242}
6243
6244
9ebce9c5 6245static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
6246 const char *ifname, int vlan_id)
6247{
a2e40bb6 6248 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 6249 struct nl_msg *msg;
a862e4a3 6250 int ret;
c5121837 6251
bbc706a3
JM
6252 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6253 ", ifname=%s[%d], vlan_id=%d)",
6254 bss->ifname, if_nametoindex(bss->ifname),
6255 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
13f83980 6256 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
6257 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6258 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6259 nlmsg_free(msg);
6260 return -ENOBUFS;
6261 }
c5121837 6262
cd1d72c1
JM
6263 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6264 if (ret < 0) {
6265 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6266 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6267 MAC2STR(addr), ifname, vlan_id, ret,
6268 strerror(-ret));
6269 }
cd1d72c1 6270 return ret;
c5121837
JM
6271}
6272
fbbfcbac 6273
c5121837
JM
6274static int i802_get_inact_sec(void *priv, const u8 *addr)
6275{
6276 struct hostap_sta_driver_data data;
6277 int ret;
6278
7824bf77 6279 os_memset(&data, 0, sizeof(data));
c5121837
JM
6280 data.inactive_msec = (unsigned long) -1;
6281 ret = i802_read_sta_data(priv, &data, addr);
b9749bac
MH
6282 if (ret == -ENOENT)
6283 return -ENOENT;
c5121837
JM
6284 if (ret || data.inactive_msec == (unsigned long) -1)
6285 return -1;
6286 return data.inactive_msec / 1000;
6287}
6288
6289
6290static int i802_sta_clear_stats(void *priv, const u8 *addr)
6291{
6292#if 0
6293 /* TODO */
6294#endif
6295 return 0;
6296}
6297
6298
731723a5
JM
6299static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6300 int reason)
c5121837 6301{
a2e40bb6 6302 struct i802_bss *bss = priv;
e1bd4e19 6303 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 6304 struct ieee80211_mgmt mgmt;
05e5e615
DL
6305 u8 channel;
6306
6307 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6308 HOSTAPD_MODE_IEEE80211AD) {
6309 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6310 * disassociate the STA instead. */
6311 return i802_sta_disassoc(priv, own_addr, addr, reason);
6312 }
c5121837 6313
0d391b03
BC
6314 if (is_mesh_interface(drv->nlmode))
6315 return -1;
6316
e1bd4e19 6317 if (drv->device_ap_sme)
59d7148a 6318 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
e1bd4e19 6319
c5121837
JM
6320 memset(&mgmt, 0, sizeof(mgmt));
6321 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6322 WLAN_FC_STYPE_DEAUTH);
6323 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
6324 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6325 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 6326 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 6327 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 6328 IEEE80211_HDRLEN +
9ebce9c5 6329 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
2d3943ce 6330 0, NULL, 0);
c5121837
JM
6331}
6332
6333
731723a5
JM
6334static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6335 int reason)
c5121837 6336{
a2e40bb6 6337 struct i802_bss *bss = priv;
e1bd4e19 6338 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
6339 struct ieee80211_mgmt mgmt;
6340
0d391b03
BC
6341 if (is_mesh_interface(drv->nlmode))
6342 return -1;
6343
e1bd4e19 6344 if (drv->device_ap_sme)
59d7148a 6345 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
e1bd4e19 6346
c5121837
JM
6347 memset(&mgmt, 0, sizeof(mgmt));
6348 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6349 WLAN_FC_STYPE_DISASSOC);
6350 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
6351 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6352 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 6353 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 6354 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 6355 IEEE80211_HDRLEN +
9ebce9c5 6356 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
2d3943ce 6357 0, NULL, 0);
c5121837
JM
6358}
6359
6360
9b4d9c8b
JM
6361static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6362{
6363 char buf[200], *pos, *end;
6364 int i, res;
6365
6366 pos = buf;
6367 end = pos + sizeof(buf);
6368
6369 for (i = 0; i < drv->num_if_indices; i++) {
6370 if (!drv->if_indices[i])
6371 continue;
732b1d20
MB
6372 res = os_snprintf(pos, end - pos, " %d(%d)",
6373 drv->if_indices[i],
6374 drv->if_indices_reason[i]);
d85e1fc8 6375 if (os_snprintf_error(end - pos, res))
9b4d9c8b
JM
6376 break;
6377 pos += res;
6378 }
6379 *pos = '\0';
6380
6381 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6382 drv->num_if_indices, buf);
6383}
6384
6385
732b1d20
MB
6386static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6387 int ifidx_reason)
f07ead6a
JM
6388{
6389 int i;
732b1d20 6390 int *old, *old_reason;
f07ead6a 6391
732b1d20
MB
6392 wpa_printf(MSG_DEBUG,
6393 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6394 ifidx, ifidx_reason);
6395 if (have_ifidx(drv, ifidx, ifidx_reason)) {
b36935be
MB
6396 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6397 ifidx);
6398 return;
6399 }
f07ead6a
JM
6400 for (i = 0; i < drv->num_if_indices; i++) {
6401 if (drv->if_indices[i] == 0) {
6402 drv->if_indices[i] = ifidx;
732b1d20 6403 drv->if_indices_reason[i] = ifidx_reason;
9b4d9c8b 6404 dump_ifidx(drv);
f07ead6a
JM
6405 return;
6406 }
6407 }
6408
6409 if (drv->if_indices != drv->default_if_indices)
6410 old = drv->if_indices;
6411 else
6412 old = NULL;
6413
732b1d20
MB
6414 if (drv->if_indices_reason != drv->default_if_indices_reason)
6415 old_reason = drv->if_indices_reason;
6416 else
6417 old_reason = NULL;
6418
067ffa26
JM
6419 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6420 sizeof(int));
732b1d20
MB
6421 drv->if_indices_reason = os_realloc_array(old_reason,
6422 drv->num_if_indices + 1,
6423 sizeof(int));
f07ead6a
JM
6424 if (!drv->if_indices) {
6425 if (!old)
6426 drv->if_indices = drv->default_if_indices;
6427 else
6428 drv->if_indices = old;
732b1d20
MB
6429 }
6430 if (!drv->if_indices_reason) {
6431 if (!old_reason)
6432 drv->if_indices_reason = drv->default_if_indices_reason;
6433 else
29eddc3d 6434 drv->if_indices_reason = old_reason;
732b1d20
MB
6435 }
6436 if (!drv->if_indices || !drv->if_indices_reason) {
f07ead6a
JM
6437 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6438 "interfaces");
6439 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6440 return;
732b1d20
MB
6441 }
6442 if (!old)
f07ead6a
JM
6443 os_memcpy(drv->if_indices, drv->default_if_indices,
6444 sizeof(drv->default_if_indices));
732b1d20
MB
6445 if (!old_reason)
6446 os_memcpy(drv->if_indices_reason,
6447 drv->default_if_indices_reason,
6448 sizeof(drv->default_if_indices_reason));
f07ead6a 6449 drv->if_indices[drv->num_if_indices] = ifidx;
732b1d20 6450 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
f07ead6a 6451 drv->num_if_indices++;
9b4d9c8b 6452 dump_ifidx(drv);
f07ead6a
JM
6453}
6454
6455
732b1d20
MB
6456static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6457 int ifidx_reason)
f07ead6a
JM
6458{
6459 int i;
6460
6461 for (i = 0; i < drv->num_if_indices; i++) {
732b1d20
MB
6462 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6463 (drv->if_indices_reason[i] == ifidx_reason ||
6464 ifidx_reason == IFIDX_ANY)) {
f07ead6a
JM
6465 drv->if_indices[i] = 0;
6466 break;
6467 }
6468 }
9b4d9c8b 6469 dump_ifidx(drv);
f07ead6a
JM
6470}
6471
6472
732b1d20
MB
6473static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6474 int ifidx_reason)
f07ead6a
JM
6475{
6476 int i;
6477
6478 for (i = 0; i < drv->num_if_indices; i++)
732b1d20
MB
6479 if (drv->if_indices[i] == ifidx &&
6480 (drv->if_indices_reason[i] == ifidx_reason ||
6481 ifidx_reason == IFIDX_ANY))
f07ead6a
JM
6482 return 1;
6483
6484 return 0;
6485}
6486
6487
6488static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
0e80ea2c 6489 const char *bridge_ifname, char *ifname_wds)
f07ead6a
JM
6490{
6491 struct i802_bss *bss = priv;
6492 struct wpa_driver_nl80211_data *drv = bss->drv;
6493 char name[IFNAMSIZ + 1];
6494
6495 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
69dd2967
SM
6496 if (ifname_wds)
6497 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6498
f07ead6a
JM
6499 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6500 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6501 if (val) {
6502 if (!if_nametoindex(name)) {
6503 if (nl80211_create_iface(drv, name,
6504 NL80211_IFTYPE_AP_VLAN,
2aec4f3c
JM
6505 bss->addr, 1, NULL, NULL, 0) <
6506 0)
f07ead6a
JM
6507 return -1;
6508 if (bridge_ifname &&
c81eff1a
BG
6509 linux_br_add_if(drv->global->ioctl_sock,
6510 bridge_ifname, name) < 0)
f07ead6a
JM
6511 return -1;
6512 }
75227f3a
JM
6513 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6514 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6515 "interface %s up", name);
6516 }
f07ead6a
JM
6517 return i802_set_sta_vlan(priv, addr, name, 0);
6518 } else {
c34e618d
FF
6519 if (bridge_ifname)
6520 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6521 name);
6522
f07ead6a 6523 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
d0595b25
FF
6524 nl80211_remove_iface(drv, if_nametoindex(name));
6525 return 0;
f07ead6a
JM
6526 }
6527}
6528
6529
6530static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6531{
6532 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6533 struct sockaddr_ll lladdr;
6534 unsigned char buf[3000];
6535 int len;
6536 socklen_t fromlen = sizeof(lladdr);
6537
6538 len = recvfrom(sock, buf, sizeof(buf), 0,
6539 (struct sockaddr *)&lladdr, &fromlen);
6540 if (len < 0) {
7ac3616d
JM
6541 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6542 strerror(errno));
f07ead6a
JM
6543 return;
6544 }
6545
732b1d20 6546 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
f07ead6a
JM
6547 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6548}
6549
6550
94627f6c 6551static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 6552 struct i802_bss *bss,
94627f6c
JM
6553 const char *brname, const char *ifname)
6554{
6c6678e7 6555 int br_ifindex;
94627f6c
JM
6556 char in_br[IFNAMSIZ];
6557
e17a2477 6558 os_strlcpy(bss->brname, brname, IFNAMSIZ);
6c6678e7 6559 br_ifindex = if_nametoindex(brname);
6c6678e7 6560 if (br_ifindex == 0) {
94627f6c
JM
6561 /*
6562 * Bridge was configured, but the bridge device does
6563 * not exist. Try to add it now.
6564 */
c81eff1a 6565 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
6566 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6567 "bridge interface %s: %s",
6568 brname, strerror(errno));
6569 return -1;
6570 }
e17a2477 6571 bss->added_bridge = 1;
8997613c 6572 br_ifindex = if_nametoindex(brname);
732b1d20 6573 add_ifidx(drv, br_ifindex, drv->ifindex);
94627f6c 6574 }
8997613c 6575 bss->br_ifindex = br_ifindex;
94627f6c
JM
6576
6577 if (linux_br_get(in_br, ifname) == 0) {
6578 if (os_strcmp(in_br, brname) == 0)
6579 return 0; /* already in the bridge */
6580
6581 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6582 "bridge %s", ifname, in_br);
c81eff1a
BG
6583 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6584 0) {
94627f6c
JM
6585 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6586 "remove interface %s from bridge "
6587 "%s: %s",
fdbfb63e 6588 ifname, in_br, strerror(errno));
94627f6c
JM
6589 return -1;
6590 }
6591 }
6592
6593 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6594 ifname, brname);
c81eff1a 6595 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
6596 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6597 "into bridge %s: %s",
6598 ifname, brname, strerror(errno));
6599 return -1;
6600 }
e17a2477 6601 bss->added_if_into_bridge = 1;
94627f6c
JM
6602
6603 return 0;
6604}
6605
6606
92f475b4
JM
6607static void *i802_init(struct hostapd_data *hapd,
6608 struct wpa_init_params *params)
c5121837
JM
6609{
6610 struct wpa_driver_nl80211_data *drv;
a2e40bb6 6611 struct i802_bss *bss;
c5121837 6612 size_t i;
cb05808c
AN
6613 char master_ifname[IFNAMSIZ];
6614 int ifindex, br_ifindex = 0;
94627f6c 6615 int br_added = 0;
c5121837 6616
0d547d5f
JM
6617 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6618 params->global_priv, 1,
0ecff8d7 6619 params->bssid, params->driver_params);
a2e40bb6 6620 if (bss == NULL)
c5121837 6621 return NULL;
c5121837 6622
a2e40bb6 6623 drv = bss->drv;
7635bfb0 6624
cb05808c 6625 if (linux_br_get(master_ifname, params->ifname) == 0) {
94627f6c 6626 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
cb05808c
AN
6627 params->ifname, master_ifname);
6628 br_ifindex = if_nametoindex(master_ifname);
6629 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6630 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6631 linux_master_get(master_ifname, params->ifname) == 0) {
6632 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6633 params->ifname, master_ifname);
6634 /* start listening for EAPOL on the master interface */
732b1d20 6635 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
f2d6c17a
DL
6636
6637 /* check if master itself is under bridge */
6638 if (linux_br_get(master_ifname, master_ifname) == 0) {
6639 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6640 master_ifname);
6641 br_ifindex = if_nametoindex(master_ifname);
6642 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6643 }
94627f6c 6644 } else {
cb05808c 6645 master_ifname[0] = '\0';
94627f6c 6646 }
cb05808c 6647
6c6678e7 6648 bss->br_ifindex = br_ifindex;
94627f6c 6649
92f475b4 6650 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
6651 if (params->bridge[i]) {
6652 ifindex = if_nametoindex(params->bridge[i]);
6653 if (ifindex)
732b1d20 6654 add_ifidx(drv, ifindex, drv->ifindex);
94627f6c
JM
6655 if (ifindex == br_ifindex)
6656 br_added = 1;
6657 }
c5121837 6658 }
c5121837 6659
ad1e68e6 6660 /* start listening for EAPOL on the default AP interface */
732b1d20 6661 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
ad1e68e6 6662
5ef8e39f
JM
6663 if (params->num_bridge && params->bridge[0]) {
6664 if (i802_check_bridge(drv, bss, params->bridge[0],
6665 params->ifname) < 0)
6666 goto failed;
cb05808c 6667 if (os_strcmp(params->bridge[0], master_ifname) != 0)
5ef8e39f
JM
6668 br_added = 1;
6669 }
6670
6671 if (!br_added && br_ifindex &&
6672 (params->num_bridge == 0 || !params->bridge[0]))
732b1d20 6673 add_ifidx(drv, br_ifindex, drv->ifindex);
94627f6c 6674
97ed9a06
KP
6675#ifdef CONFIG_LIBNL3_ROUTE
6676 if (bss->added_if_into_bridge) {
6677 drv->rtnl_sk = nl_socket_alloc();
6678 if (drv->rtnl_sk == NULL) {
6679 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6680 goto failed;
6681 }
6682
6683 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6684 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6685 strerror(errno));
6686 goto failed;
6687 }
6688 }
6689#endif /* CONFIG_LIBNL3_ROUTE */
6690
ad1e68e6
JM
6691 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6692 if (drv->eapol_sock < 0) {
7ac3616d
JM
6693 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6694 strerror(errno));
bbaf0837 6695 goto failed;
ad1e68e6
JM
6696 }
6697
6698 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6699 {
7ac3616d 6700 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
c5121837 6701 goto failed;
ad1e68e6
JM
6702 }
6703
c81eff1a
BG
6704 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6705 params->own_addr))
bbaf0837 6706 goto failed;
fee354c7 6707 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
c5121837 6708
341eebee
JB
6709 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6710
a2e40bb6 6711 return bss;
c5121837
JM
6712
6713failed:
7635bfb0 6714 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
6715 return NULL;
6716}
c5121837 6717
c5121837 6718
bbaf0837
JM
6719static void i802_deinit(void *priv)
6720{
9ebce9c5
JM
6721 struct i802_bss *bss = priv;
6722 wpa_driver_nl80211_deinit(bss);
c5121837
JM
6723}
6724
c5121837 6725
22a7c9d7
JM
6726static enum nl80211_iftype wpa_driver_nl80211_if_type(
6727 enum wpa_driver_if_type type)
6728{
6729 switch (type) {
6730 case WPA_IF_STATION:
9f51b113 6731 return NL80211_IFTYPE_STATION;
75bde05d
JM
6732 case WPA_IF_P2P_CLIENT:
6733 case WPA_IF_P2P_GROUP:
9f51b113 6734 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
6735 case WPA_IF_AP_VLAN:
6736 return NL80211_IFTYPE_AP_VLAN;
6737 case WPA_IF_AP_BSS:
6738 return NL80211_IFTYPE_AP;
9f51b113
JB
6739 case WPA_IF_P2P_GO:
6740 return NL80211_IFTYPE_P2P_GO;
7aad838c
NS
6741 case WPA_IF_P2P_DEVICE:
6742 return NL80211_IFTYPE_P2P_DEVICE;
5b78493f
MH
6743 case WPA_IF_MESH:
6744 return NL80211_IFTYPE_MESH_POINT;
98342208
AK
6745 default:
6746 return -1;
22a7c9d7 6747 }
22a7c9d7
JM
6748}
6749
6750
f2ed8023
JM
6751static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6752{
6753 struct wpa_driver_nl80211_data *drv;
6754 dl_list_for_each(drv, &global->interfaces,
6755 struct wpa_driver_nl80211_data, list) {
834ee56f 6756 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
f2ed8023
JM
6757 return 1;
6758 }
6759 return 0;
6760}
6761
6762
5b78493f 6763static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
f2ed8023
JM
6764{
6765 unsigned int idx;
6766
6767 if (!drv->global)
6768 return -1;
6769
834ee56f 6770 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
f2ed8023 6771 for (idx = 0; idx < 64; idx++) {
834ee56f 6772 new_addr[0] = drv->first_bss->addr[0] | 0x02;
f2ed8023
JM
6773 new_addr[0] ^= idx << 2;
6774 if (!nl80211_addr_in_use(drv->global, new_addr))
6775 break;
6776 }
6777 if (idx == 64)
6778 return -1;
6779
5b78493f 6780 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
f2ed8023
JM
6781 MACSTR, MAC2STR(new_addr));
6782
6783 return 0;
6784}
6785
6786
f632e483
AS
6787struct wdev_info {
6788 u64 wdev_id;
6789 int wdev_id_set;
6790 u8 macaddr[ETH_ALEN];
6791};
6792
6793static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
e472e1b4
AS
6794{
6795 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6796 struct nlattr *tb[NL80211_ATTR_MAX + 1];
f632e483 6797 struct wdev_info *wi = arg;
e472e1b4
AS
6798
6799 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6800 genlmsg_attrlen(gnlh, 0), NULL);
6801 if (tb[NL80211_ATTR_WDEV]) {
f632e483
AS
6802 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6803 wi->wdev_id_set = 1;
e472e1b4
AS
6804 }
6805
6806 if (tb[NL80211_ATTR_MAC])
f632e483 6807 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
e472e1b4
AS
6808 ETH_ALEN);
6809
6810 return NL_SKIP;
6811}
6812
6813
7ab68865 6814static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 6815 const char *ifname, const u8 *addr,
f3585c8a 6816 void *bss_ctx, void **drv_priv,
e17a2477 6817 char *force_ifname, u8 *if_addr,
d8a3b66d
AS
6818 const char *bridge, int use_existing,
6819 int setup_ap)
22a7c9d7 6820{
e472e1b4 6821 enum nl80211_iftype nlmode;
a2e40bb6
FF
6822 struct i802_bss *bss = priv;
6823 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7 6824 int ifidx;
2aec4f3c 6825 int added = 1;
22a7c9d7 6826
f3585c8a
JM
6827 if (addr)
6828 os_memcpy(if_addr, addr, ETH_ALEN);
e472e1b4
AS
6829 nlmode = wpa_driver_nl80211_if_type(type);
6830 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
f632e483
AS
6831 struct wdev_info p2pdev_info;
6832
6833 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
e472e1b4 6834 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
f632e483 6835 0, nl80211_wdev_handler,
2aec4f3c 6836 &p2pdev_info, use_existing);
f632e483 6837 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
e472e1b4
AS
6838 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6839 ifname);
e472e1b4
AS
6840 return -1;
6841 }
f632e483
AS
6842
6843 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6844 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6845 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6846 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6847 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6848 ifname,
6849 (long long unsigned int) p2pdev_info.wdev_id);
e472e1b4
AS
6850 } else {
6851 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
2aec4f3c
JM
6852 0, NULL, NULL, use_existing);
6853 if (use_existing && ifidx == -ENFILE) {
6854 added = 0;
6855 ifidx = if_nametoindex(ifname);
6856 } else if (ifidx < 0) {
e472e1b4
AS
6857 return -1;
6858 }
22a7c9d7
JM
6859 }
6860
ab7a1add 6861 if (!addr) {
8ea8a89c 6862 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
ab7a1add
AS
6863 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6864 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
8ea8a89c 6865 ifname, if_addr) < 0) {
2aec4f3c
JM
6866 if (added)
6867 nl80211_remove_iface(drv, ifidx);
ab7a1add
AS
6868 return -1;
6869 }
c55f774d
JM
6870 }
6871
c55f774d
JM
6872 if (!addr &&
6873 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8ea8a89c
JM
6874 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6875 type == WPA_IF_STATION)) {
6876 /* Enforce unique address */
ab7a1add 6877 u8 new_addr[ETH_ALEN];
c55f774d 6878
ab7a1add 6879 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
c81eff1a 6880 new_addr) < 0) {
ea39367c
MK
6881 if (added)
6882 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
6883 return -1;
6884 }
f608081c 6885 if (nl80211_addr_in_use(drv->global, new_addr)) {
c55f774d 6886 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8ea8a89c 6887 "for interface %s type %d", ifname, type);
5b78493f 6888 if (nl80211_vif_addr(drv, new_addr) < 0) {
ea39367c
MK
6889 if (added)
6890 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
6891 return -1;
6892 }
c81eff1a 6893 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d 6894 new_addr) < 0) {
ea39367c
MK
6895 if (added)
6896 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
6897 return -1;
6898 }
c55f774d 6899 }
f67eeb5c 6900 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d 6901 }
f3585c8a 6902
d8a3b66d 6903 if (type == WPA_IF_AP_BSS && setup_ap) {
f5eb9da3
JM
6904 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6905 if (new_bss == NULL) {
2aec4f3c
JM
6906 if (added)
6907 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
6908 return -1;
6909 }
6910
6911 if (bridge &&
6912 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6913 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6914 "interface %s to a bridge %s",
6915 ifname, bridge);
2aec4f3c
JM
6916 if (added)
6917 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
6918 os_free(new_bss);
6919 return -1;
6920 }
6921
c81eff1a
BG
6922 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6923 {
ea39367c
MK
6924 if (added)
6925 nl80211_remove_iface(drv, ifidx);
07179987 6926 os_free(new_bss);
22a7c9d7
JM
6927 return -1;
6928 }
a2e40bb6 6929 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 6930 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
6931 new_bss->ifindex = ifidx;
6932 new_bss->drv = drv;
834ee56f
KP
6933 new_bss->next = drv->first_bss->next;
6934 new_bss->freq = drv->first_bss->freq;
a5e1eb20 6935 new_bss->ctx = bss_ctx;
2aec4f3c 6936 new_bss->added_if = added;
834ee56f 6937 drv->first_bss->next = new_bss;
a2e40bb6
FF
6938 if (drv_priv)
6939 *drv_priv = new_bss;
cc7a48d1 6940 nl80211_init_bss(new_bss);
3dd1d890
YAP
6941
6942 /* Subscribe management frames for this WPA_IF_AP_BSS */
6943 if (nl80211_setup_ap(new_bss))
6944 return -1;
22a7c9d7 6945 }
22a7c9d7 6946
ff6a158b
JM
6947 if (drv->global)
6948 drv->global->if_add_ifindex = ifidx;
6949
d1bb7aed
JJ
6950 /*
6951 * Some virtual interfaces need to process EAPOL packets and events on
6952 * the parent interface. This is used mainly with hostapd.
6953 */
6954 if (ifidx > 0 &&
6955 (drv->hostapd ||
6956 nlmode == NL80211_IFTYPE_AP_VLAN ||
6957 nlmode == NL80211_IFTYPE_WDS ||
6958 nlmode == NL80211_IFTYPE_MONITOR))
732b1d20 6959 add_ifidx(drv, ifidx, IFIDX_ANY);
b36935be 6960
22a7c9d7
JM
6961 return 0;
6962}
6963
6964
9ebce9c5 6965static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
6966 enum wpa_driver_if_type type,
6967 const char *ifname)
6968{
a2e40bb6 6969 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
6970 int ifindex = if_nametoindex(ifname);
6971
2aec4f3c
JM
6972 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6973 __func__, type, ifname, ifindex, bss->added_if);
158b090c 6974 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
2b72df63 6975 nl80211_remove_iface(drv, ifindex);
de884303
JM
6976 else if (ifindex > 0 && !bss->added_if) {
6977 struct wpa_driver_nl80211_data *drv2;
6978 dl_list_for_each(drv2, &drv->global->interfaces,
732b1d20
MB
6979 struct wpa_driver_nl80211_data, list) {
6980 del_ifidx(drv2, ifindex, IFIDX_ANY);
6981 del_ifidx(drv2, IFIDX_ANY, ifindex);
6982 }
de884303 6983 }
c34e618d 6984
c34e618d
FF
6985 if (type != WPA_IF_AP_BSS)
6986 return 0;
6987
e17a2477 6988 if (bss->added_if_into_bridge) {
c81eff1a
BG
6989 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6990 bss->ifname) < 0)
e17a2477
JM
6991 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6992 "interface %s from bridge %s: %s",
6993 bss->ifname, bss->brname, strerror(errno));
6994 }
6995 if (bss->added_bridge) {
c81eff1a 6996 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
6997 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6998 "bridge %s: %s",
6999 bss->brname, strerror(errno));
7000 }
a2e40bb6 7001
834ee56f 7002 if (bss != drv->first_bss) {
8546ea19 7003 struct i802_bss *tbss;
a2e40bb6 7004
2aec4f3c 7005 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
834ee56f 7006 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
8546ea19
JM
7007 if (tbss->next == bss) {
7008 tbss->next = bss->next;
3dd1d890
YAP
7009 /* Unsubscribe management frames */
7010 nl80211_teardown_ap(bss);
cc7a48d1 7011 nl80211_destroy_bss(bss);
5c9da160
MB
7012 if (!bss->added_if)
7013 i802_set_iface_flags(bss, 0);
8546ea19
JM
7014 os_free(bss);
7015 bss = NULL;
7016 break;
7017 }
22a7c9d7 7018 }
8546ea19
JM
7019 if (bss)
7020 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7021 "BSS %p in the list", __func__, bss);
390e489c 7022 } else {
2aec4f3c 7023 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
390e489c 7024 nl80211_teardown_ap(bss);
2aec4f3c 7025 if (!bss->added_if && !drv->first_bss->next)
a70cd0db 7026 wpa_driver_nl80211_del_beacon(bss);
390e489c 7027 nl80211_destroy_bss(bss);
2aec4f3c
JM
7028 if (!bss->added_if)
7029 i802_set_iface_flags(bss, 0);
390e489c
KP
7030 if (drv->first_bss->next) {
7031 drv->first_bss = drv->first_bss->next;
7032 drv->ctx = drv->first_bss->ctx;
7033 os_free(bss);
7034 } else {
7035 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7036 }
22a7c9d7 7037 }
22a7c9d7
JM
7038
7039 return 0;
7040}
7041
7042
55777702
JM
7043static int cookie_handler(struct nl_msg *msg, void *arg)
7044{
7045 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7046 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7047 u64 *cookie = arg;
7048 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7049 genlmsg_attrlen(gnlh, 0), NULL);
7050 if (tb[NL80211_ATTR_COOKIE])
7051 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7052 return NL_SKIP;
7053}
7054
7055
88df0ef7 7056static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
7057 unsigned int freq, unsigned int wait,
7058 const u8 *buf, size_t buf_len,
88df0ef7 7059 u64 *cookie_out, int no_cck, int no_ack,
2d3943ce
AO
7060 int offchanok, const u16 *csa_offs,
7061 size_t csa_offs_len)
9884f9cc 7062{
88df0ef7 7063 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
7064 struct nl_msg *msg;
7065 u64 cookie;
7066 int ret = -1;
7067
cc2ada86 7068 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
2e3e4566
JM
7069 "no_ack=%d offchanok=%d",
7070 freq, wait, no_cck, no_ack, offchanok);
c91f796f 7071 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
9884f9cc 7072
56f77852 7073 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
a862e4a3
JM
7074 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7075 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7076 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7077 drv->test_use_roc_tx) &&
7078 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7079 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7080 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
2d3943ce
AO
7081 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7082 csa_offs_len * sizeof(u16), csa_offs)) ||
a862e4a3
JM
7083 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7084 goto fail;
9884f9cc
JB
7085
7086 cookie = 0;
7087 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7088 msg = NULL;
7089 if (ret) {
7090 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
7091 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7092 freq, wait);
a862e4a3
JM
7093 } else {
7094 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7095 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7096 (long long unsigned int) cookie);
9884f9cc 7097
a862e4a3
JM
7098 if (cookie_out)
7099 *cookie_out = no_ack ? (u64) -1 : cookie;
759a8a3a
JM
7100
7101 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7102 wpa_printf(MSG_DEBUG,
7103 "nl80211: Drop oldest pending send action cookie 0x%llx",
7104 (long long unsigned int)
7105 drv->send_action_cookies[0]);
7106 os_memmove(&drv->send_action_cookies[0],
7107 &drv->send_action_cookies[1],
7108 (MAX_SEND_ACTION_COOKIES - 1) *
7109 sizeof(u64));
7110 drv->num_send_action_cookies--;
7111 }
7112 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7113 drv->num_send_action_cookies++;
a862e4a3 7114 }
9884f9cc 7115
a862e4a3 7116fail:
9884f9cc
JB
7117 nlmsg_free(msg);
7118 return ret;
7119}
7120
7121
9ebce9c5
JM
7122static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7123 unsigned int freq,
190b9062 7124 unsigned int wait_time,
58f6fbe0
JM
7125 const u8 *dst, const u8 *src,
7126 const u8 *bssid,
b106173a
JM
7127 const u8 *data, size_t data_len,
7128 int no_cck)
58f6fbe0 7129{
a2e40bb6 7130 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 7131 int ret = -1;
58f6fbe0
JM
7132 u8 *buf;
7133 struct ieee80211_hdr *hdr;
58f6fbe0 7134
5dfca53f 7135 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
7136 "freq=%u MHz wait=%d ms no_cck=%d)",
7137 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
7138
7139 buf = os_zalloc(24 + data_len);
7140 if (buf == NULL)
7141 return ret;
7142 os_memcpy(buf + 24, data, data_len);
7143 hdr = (struct ieee80211_hdr *) buf;
7144 hdr->frame_control =
7145 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7146 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7147 os_memcpy(hdr->addr2, src, ETH_ALEN);
7148 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7149
8331c9b3
VK
7150 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7151 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7152 MAC2STR(src));
7153 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7154 } else {
7155 os_memset(bss->rand_addr, 0, ETH_ALEN);
7156 }
7157
f78f2785
JM
7158 if (is_ap_interface(drv->nlmode) &&
7159 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7160 (int) freq == bss->freq || drv->device_ap_sme ||
7161 !drv->use_monitor))
9ebce9c5
JM
7162 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7163 0, freq, no_cck, 1,
2d3943ce 7164 wait_time, NULL, 0);
9884f9cc 7165 else
88df0ef7 7166 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 7167 24 + data_len,
b106173a 7168 &drv->send_action_cookie,
2d3943ce 7169 no_cck, 0, 1, NULL, 0);
58f6fbe0 7170
f8bf1421 7171 os_free(buf);
58f6fbe0
JM
7172 return ret;
7173}
7174
7175
759a8a3a 7176static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
5dfca53f 7177{
5dfca53f
JB
7178 struct wpa_driver_nl80211_data *drv = bss->drv;
7179 struct nl_msg *msg;
7180 int ret;
7181
316a9e4d 7182 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
759a8a3a 7183 (long long unsigned int) cookie);
56f77852 7184 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
759a8a3a 7185 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
a862e4a3
JM
7186 nlmsg_free(msg);
7187 return;
7188 }
5dfca53f
JB
7189
7190 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5dfca53f
JB
7191 if (ret)
7192 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7193 "(%s)", ret, strerror(-ret));
5dfca53f
JB
7194}
7195
7196
759a8a3a
JM
7197static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7198{
7199 struct i802_bss *bss = priv;
7200 struct wpa_driver_nl80211_data *drv = bss->drv;
7201 unsigned int i;
7202 u64 cookie;
7203
7204 /* Cancel the last pending TX cookie */
7205 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7206
7207 /*
7208 * Cancel the other pending TX cookies, if any. This is needed since
7209 * the driver may keep a list of all pending offchannel TX operations
7210 * and free up the radio only once they have expired or cancelled.
7211 */
7212 for (i = drv->num_send_action_cookies; i > 0; i--) {
7213 cookie = drv->send_action_cookies[i - 1];
7214 if (cookie != drv->send_action_cookie)
7215 nl80211_frame_wait_cancel(bss, cookie);
7216 }
7217 drv->num_send_action_cookies = 0;
7218}
7219
7220
55777702
JM
7221static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7222 unsigned int duration)
7223{
a2e40bb6
FF
7224 struct i802_bss *bss = priv;
7225 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
7226 struct nl_msg *msg;
7227 int ret;
7228 u64 cookie;
7229
56f77852 7230 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
a862e4a3
JM
7231 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7232 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7233 nlmsg_free(msg);
7234 return -1;
7235 }
55777702
JM
7236
7237 cookie = 0;
7238 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7239 if (ret == 0) {
7240 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7241 "0x%llx for freq=%u MHz duration=%u",
7242 (long long unsigned int) cookie, freq, duration);
7243 drv->remain_on_chan_cookie = cookie;
531f0331 7244 drv->pending_remain_on_chan = 1;
55777702
JM
7245 return 0;
7246 }
7247 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
7248 "(freq=%d duration=%u): %d (%s)",
7249 freq, duration, ret, strerror(-ret));
55777702
JM
7250 return -1;
7251}
7252
7253
7254static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7255{
a2e40bb6
FF
7256 struct i802_bss *bss = priv;
7257 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
7258 struct nl_msg *msg;
7259 int ret;
7260
7261 if (!drv->pending_remain_on_chan) {
7262 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7263 "to cancel");
7264 return -1;
7265 }
7266
7267 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7268 "0x%llx",
7269 (long long unsigned int) drv->remain_on_chan_cookie);
7270
56f77852
JM
7271 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7272 if (!msg ||
a862e4a3
JM
7273 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7274 nlmsg_free(msg);
7275 return -1;
7276 }
55777702
JM
7277
7278 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7279 if (ret == 0)
7280 return 0;
7281 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7282 "%d (%s)", ret, strerror(-ret));
55777702
JM
7283 return -1;
7284}
7285
7286
9ebce9c5 7287static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 7288{
a2e40bb6 7289 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 7290
5582a5d1 7291 if (!report) {
0d891981 7292 if (bss->nl_preq && drv->device_ap_sme &&
b8d87ed2
AP
7293 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7294 !bss->static_ap) {
0d891981
JM
7295 /*
7296 * Do not disable Probe Request reporting that was
7297 * enabled in nl80211_setup_ap().
7298 */
7299 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7300 "Probe Request reporting nl_preq=%p while "
7301 "in AP mode", bss->nl_preq);
7302 } else if (bss->nl_preq) {
36488c05
JM
7303 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7304 "reporting nl_preq=%p", bss->nl_preq);
40a68f33 7305 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
5582a5d1
JB
7306 }
7307 return 0;
7308 }
7309
481234cf 7310 if (bss->nl_preq) {
5582a5d1 7311 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 7312 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
7313 return 0;
7314 }
7315
481234cf
JM
7316 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7317 if (bss->nl_preq == NULL)
5582a5d1 7318 return -1;
36488c05
JM
7319 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7320 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 7321
481234cf 7322 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
7323 (WLAN_FC_TYPE_MGMT << 2) |
7324 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
7325 NULL, 0) < 0)
7326 goto out_err;
5582a5d1 7327
5f65e9f7
JB
7328 nl80211_register_eloop_read(&bss->nl_preq,
7329 wpa_driver_nl80211_event_receive,
40a68f33 7330 bss->nl_cb, 0);
5582a5d1 7331
504e905c 7332 return 0;
5582a5d1 7333
a92dfde8 7334 out_err:
221a59c9 7335 nl_destroy_handles(&bss->nl_preq);
5582a5d1 7336 return -1;
504e905c
JM
7337}
7338
7339
4e5cb1a3
JM
7340static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7341 int ifindex, int disabled)
7342{
7343 struct nl_msg *msg;
7344 struct nlattr *bands, *band;
7345 int ret;
7346
3e208481
JM
7347 wpa_printf(MSG_DEBUG,
7348 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7349 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7350 "no NL80211_TXRATE_LEGACY constraint");
7351
95376e1a
JM
7352 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7353 NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
7354 if (!msg)
7355 return -1;
7356
4e5cb1a3
JM
7357 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7358 if (!bands)
a862e4a3 7359 goto fail;
4e5cb1a3
JM
7360
7361 /*
7362 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7363 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7364 * rates. All 5 GHz rates are left enabled.
7365 */
7366 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
a862e4a3
JM
7367 if (!band ||
7368 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7369 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7370 goto fail;
4e5cb1a3
JM
7371 nla_nest_end(msg, band);
7372
7373 nla_nest_end(msg, bands);
7374
7375 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4e5cb1a3
JM
7376 if (ret) {
7377 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7378 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
7379 } else
7380 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
7381
7382 return ret;
7383
a862e4a3 7384fail:
4e5cb1a3
JM
7385 nlmsg_free(msg);
7386 return -1;
7387}
7388
7389
af473088
JM
7390static int wpa_driver_nl80211_deinit_ap(void *priv)
7391{
a2e40bb6
FF
7392 struct i802_bss *bss = priv;
7393 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 7394 if (!is_ap_interface(drv->nlmode))
af473088 7395 return -1;
a70cd0db 7396 wpa_driver_nl80211_del_beacon(bss);
9bc5cfa3 7397 bss->beacon_set = 0;
60b13c20
IP
7398
7399 /*
7400 * If the P2P GO interface was dynamically added, then it is
7401 * possible that the interface change to station is not possible.
7402 */
7403 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7404 return 0;
7405
b1f625e0 7406 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
7407}
7408
7409
695c7038
SW
7410static int wpa_driver_nl80211_stop_ap(void *priv)
7411{
7412 struct i802_bss *bss = priv;
7413 struct wpa_driver_nl80211_data *drv = bss->drv;
7414 if (!is_ap_interface(drv->nlmode))
7415 return -1;
a70cd0db 7416 wpa_driver_nl80211_del_beacon(bss);
695c7038
SW
7417 bss->beacon_set = 0;
7418 return 0;
7419}
7420
7421
3c29244e
EP
7422static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7423{
7424 struct i802_bss *bss = priv;
7425 struct wpa_driver_nl80211_data *drv = bss->drv;
7426 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7427 return -1;
60b13c20
IP
7428
7429 /*
7430 * If the P2P Client interface was dynamically added, then it is
7431 * possible that the interface change to station is not possible.
7432 */
7433 if (bss->if_dynamic)
7434 return 0;
7435
3c29244e
EP
7436 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7437}
7438
7439
207ef3fb
JM
7440static void wpa_driver_nl80211_resume(void *priv)
7441{
a2e40bb6 7442 struct i802_bss *bss = priv;
e8dc205f 7443 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
91724d6f
AS
7444
7445 if (i802_set_iface_flags(bss, 1))
7446 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
e8dc205f
AO
7447
7448 if (is_p2p_net_interface(nlmode))
7449 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
207ef3fb
JM
7450}
7451
7452
b625473c
JM
7453static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7454{
7455 struct i802_bss *bss = priv;
7456 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
7457 struct nl_msg *msg;
7458 struct nlattr *cqm;
b625473c
JM
7459
7460 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7461 "hysteresis=%d", threshold, hysteresis);
7462
13f83980 7463 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
a862e4a3
JM
7464 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7465 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7466 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7467 nlmsg_free(msg);
7468 return -1;
7469 }
8970bae8 7470 nla_nest_end(msg, cqm);
21270bb4 7471
a862e4a3 7472 return send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
7473}
7474
7475
2cc8d8f4
AO
7476static int get_channel_width(struct nl_msg *msg, void *arg)
7477{
7478 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7479 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7480 struct wpa_signal_info *sig_change = arg;
7481
7482 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7483 genlmsg_attrlen(gnlh, 0), NULL);
7484
7485 sig_change->center_frq1 = -1;
7486 sig_change->center_frq2 = -1;
7487 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7488
7489 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7490 sig_change->chanwidth = convert2width(
7491 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7492 if (tb[NL80211_ATTR_CENTER_FREQ1])
7493 sig_change->center_frq1 =
7494 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7495 if (tb[NL80211_ATTR_CENTER_FREQ2])
7496 sig_change->center_frq2 =
7497 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7498 }
7499
7500 return NL_SKIP;
7501}
7502
7503
7504static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7505 struct wpa_signal_info *sig)
7506{
7507 struct nl_msg *msg;
7508
9725b784 7509 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
2cc8d8f4 7510 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
2cc8d8f4
AO
7511}
7512
7513
1c5c7273
PS
7514static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7515{
7516 struct i802_bss *bss = priv;
7517 struct wpa_driver_nl80211_data *drv = bss->drv;
7518 int res;
7519
7520 os_memset(si, 0, sizeof(*si));
7521 res = nl80211_get_link_signal(drv, si);
1d6955e6
JM
7522 if (res) {
7523 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7524 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7525 return res;
7526 si->current_signal = 0;
7527 }
1c5c7273 7528
2cc8d8f4
AO
7529 res = nl80211_get_channel_width(drv, si);
7530 if (res != 0)
7531 return res;
7532
1c5c7273
PS
7533 return nl80211_get_link_noise(drv, si);
7534}
7535
7536
b91ab76e
JM
7537static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7538 int encrypt)
7539{
7540 struct i802_bss *bss = priv;
55231068 7541 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
2d3943ce 7542 0, 0, 0, 0, NULL, 0);
b91ab76e
JM
7543}
7544
7545
c55f774d
JM
7546static int nl80211_set_param(void *priv, const char *param)
7547{
ef24ad3e
JM
7548 struct i802_bss *bss = priv;
7549 struct wpa_driver_nl80211_data *drv = bss->drv;
7550
c55f774d
JM
7551 if (param == NULL)
7552 return 0;
99a94f55 7553 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
c55f774d
JM
7554
7555#ifdef CONFIG_P2P
7556 if (os_strstr(param, "use_p2p_group_interface=1")) {
7557 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7558 "interface");
7559 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7560 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7561 }
7562#endif /* CONFIG_P2P */
7563
ef24ad3e 7564 if (os_strstr(param, "use_monitor=1"))
327b01d3 7565 drv->use_monitor = 1;
327b01d3
JM
7566
7567 if (os_strstr(param, "force_connect_cmd=1")) {
327b01d3 7568 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
b497a212 7569 drv->force_connect_cmd = 1;
327b01d3
JM
7570 }
7571
ef24ad3e 7572 if (os_strstr(param, "force_bss_selection=1"))
4d584d8c 7573 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
4d584d8c 7574
64abb725 7575 if (os_strstr(param, "no_offchannel_tx=1")) {
64abb725
JM
7576 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7577 drv->test_use_roc_tx = 1;
7578 }
7579
c55f774d
JM
7580 return 0;
7581}
7582
7583
45e3fc72 7584static void * nl80211_global_init(void *ctx)
f2ed8023
JM
7585{
7586 struct nl80211_global *global;
36d84860
BG
7587 struct netlink_config *cfg;
7588
f2ed8023
JM
7589 global = os_zalloc(sizeof(*global));
7590 if (global == NULL)
7591 return NULL;
45e3fc72 7592 global->ctx = ctx;
c81eff1a 7593 global->ioctl_sock = -1;
f2ed8023 7594 dl_list_init(&global->interfaces);
ff6a158b 7595 global->if_add_ifindex = -1;
36d84860
BG
7596
7597 cfg = os_zalloc(sizeof(*cfg));
7598 if (cfg == NULL)
7599 goto err;
7600
7601 cfg->ctx = global;
7602 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7603 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7604 global->netlink = netlink_init(cfg);
7605 if (global->netlink == NULL) {
7606 os_free(cfg);
7607 goto err;
7608 }
7609
2a7b66f5
BG
7610 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7611 goto err;
7612
c81eff1a
BG
7613 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7614 if (global->ioctl_sock < 0) {
7ac3616d
JM
7615 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7616 strerror(errno));
c81eff1a
BG
7617 goto err;
7618 }
7619
f2ed8023 7620 return global;
36d84860
BG
7621
7622err:
7623 nl80211_global_deinit(global);
7624 return NULL;
f2ed8023
JM
7625}
7626
7627
7628static void nl80211_global_deinit(void *priv)
7629{
7630 struct nl80211_global *global = priv;
7631 if (global == NULL)
7632 return;
7633 if (!dl_list_empty(&global->interfaces)) {
7634 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7635 "nl80211_global_deinit",
7636 dl_list_len(&global->interfaces));
7637 }
36d84860
BG
7638
7639 if (global->netlink)
7640 netlink_deinit(global->netlink);
7641
276e2d67
BG
7642 nl_destroy_handles(&global->nl);
7643
5f65e9f7 7644 if (global->nl_event)
40a68f33 7645 nl80211_destroy_eloop_handle(&global->nl_event, 0);
d6c9aab8
JB
7646
7647 nl_cb_put(global->nl_cb);
2a7b66f5 7648
c81eff1a
BG
7649 if (global->ioctl_sock >= 0)
7650 close(global->ioctl_sock);
7651
f2ed8023
JM
7652 os_free(global);
7653}
7654
7655
6859f1cb
BG
7656static const char * nl80211_get_radio_name(void *priv)
7657{
7658 struct i802_bss *bss = priv;
7659 struct wpa_driver_nl80211_data *drv = bss->drv;
7660 return drv->phyname;
7661}
7662
7663
061a3d3d
VK
7664static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7665 struct wpa_pmkid_params *params)
a6efc65d
JM
7666{
7667 struct nl_msg *msg;
0887215d 7668 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
a6efc65d 7669
13f83980 7670 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
061a3d3d
VK
7671 (params->pmkid &&
7672 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7673 (params->bssid &&
7674 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7675 (params->ssid_len &&
7676 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7677 (params->fils_cache_id &&
7678 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7679 params->fils_cache_id)) ||
0887215d 7680 (params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
061a3d3d 7681 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
a862e4a3
JM
7682 nlmsg_free(msg);
7683 return -ENOBUFS;
7684 }
a6efc65d
JM
7685
7686 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a6efc65d
JM
7687}
7688
7689
6fbb5414 7690static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
a6efc65d
JM
7691{
7692 struct i802_bss *bss = priv;
e7f6e6ee 7693 int ret;
061a3d3d
VK
7694
7695 if (params->bssid)
7696 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
7697 MAC2STR(params->bssid));
7698 else if (params->fils_cache_id && params->ssid_len) {
7699 wpa_printf(MSG_DEBUG,
7700 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
7701 params->fils_cache_id[0], params->fils_cache_id[1],
7702 wpa_ssid_txt(params->ssid, params->ssid_len));
7703 }
7704
e7f6e6ee
JM
7705 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
7706 if (ret < 0) {
7707 wpa_printf(MSG_DEBUG,
7708 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7709 ret, strerror(-ret));
7710 }
7711
7712 return ret;
a6efc65d
JM
7713}
7714
7715
6fbb5414 7716static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
a6efc65d
JM
7717{
7718 struct i802_bss *bss = priv;
e7f6e6ee 7719 int ret;
061a3d3d
VK
7720
7721 if (params->bssid)
7722 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7723 MAC2STR(params->bssid));
7724 else if (params->fils_cache_id && params->ssid_len) {
7725 wpa_printf(MSG_DEBUG,
7726 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7727 params->fils_cache_id[0], params->fils_cache_id[1],
7728 wpa_ssid_txt(params->ssid, params->ssid_len));
7729 }
7730
e7f6e6ee
JM
7731 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7732 if (ret < 0) {
7733 wpa_printf(MSG_DEBUG,
7734 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
7735 ret, strerror(-ret));
7736 }
7737
7738 return ret;
a6efc65d
JM
7739}
7740
7741
7742static int nl80211_flush_pmkid(void *priv)
7743{
7744 struct i802_bss *bss = priv;
061a3d3d
VK
7745 struct nl_msg *msg;
7746
a6efc65d 7747 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
061a3d3d
VK
7748 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
7749 if (!msg)
7750 return -ENOBUFS;
7751 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a6efc65d
JM
7752}
7753
7754
0185007c
MK
7755static void clean_survey_results(struct survey_results *survey_results)
7756{
7757 struct freq_survey *survey, *tmp;
7758
7759 if (dl_list_empty(&survey_results->survey_list))
7760 return;
7761
7762 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7763 struct freq_survey, list) {
7764 dl_list_del(&survey->list);
7765 os_free(survey);
7766 }
7767}
7768
7769
7770static void add_survey(struct nlattr **sinfo, u32 ifidx,
7771 struct dl_list *survey_list)
7772{
7773 struct freq_survey *survey;
7774
7775 survey = os_zalloc(sizeof(struct freq_survey));
7776 if (!survey)
7777 return;
7778
7779 survey->ifidx = ifidx;
7780 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7781 survey->filled = 0;
7782
7783 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7784 survey->nf = (int8_t)
7785 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7786 survey->filled |= SURVEY_HAS_NF;
7787 }
7788
7789 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7790 survey->channel_time =
7791 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7792 survey->filled |= SURVEY_HAS_CHAN_TIME;
7793 }
7794
7795 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7796 survey->channel_time_busy =
7797 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7798 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7799 }
7800
7801 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7802 survey->channel_time_rx =
7803 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7804 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7805 }
7806
7807 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7808 survey->channel_time_tx =
7809 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7810 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7811 }
7812
7813 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)",
7814 survey->freq,
7815 survey->nf,
7816 (unsigned long int) survey->channel_time,
7817 (unsigned long int) survey->channel_time_busy,
7818 (unsigned long int) survey->channel_time_tx,
7819 (unsigned long int) survey->channel_time_rx,
7820 survey->filled);
7821
7822 dl_list_add_tail(survey_list, &survey->list);
7823}
7824
7825
7826static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7827 unsigned int freq_filter)
7828{
7829 if (!freq_filter)
7830 return 1;
7831
7832 return freq_filter == surveyed_freq;
7833}
7834
7835
7836static int survey_handler(struct nl_msg *msg, void *arg)
7837{
7838 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7839 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7840 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7841 struct survey_results *survey_results;
7842 u32 surveyed_freq = 0;
7843 u32 ifidx;
7844
7845 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7846 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7847 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7848 };
7849
7850 survey_results = (struct survey_results *) arg;
7851
7852 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7853 genlmsg_attrlen(gnlh, 0), NULL);
7854
e28f39b7
SJ
7855 if (!tb[NL80211_ATTR_IFINDEX])
7856 return NL_SKIP;
7857
0185007c
MK
7858 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7859
7860 if (!tb[NL80211_ATTR_SURVEY_INFO])
7861 return NL_SKIP;
7862
7863 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7864 tb[NL80211_ATTR_SURVEY_INFO],
7865 survey_policy))
7866 return NL_SKIP;
7867
7868 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7869 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7870 return NL_SKIP;
7871 }
7872
7873 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7874
7875 if (!check_survey_ok(sinfo, surveyed_freq,
7876 survey_results->freq_filter))
7877 return NL_SKIP;
7878
7879 if (survey_results->freq_filter &&
7880 survey_results->freq_filter != surveyed_freq) {
7881 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7882 surveyed_freq);
7883 return NL_SKIP;
7884 }
7885
7886 add_survey(sinfo, ifidx, &survey_results->survey_list);
7887
7888 return NL_SKIP;
7889}
7890
7891
7892static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7893{
7894 struct i802_bss *bss = priv;
7895 struct wpa_driver_nl80211_data *drv = bss->drv;
7896 struct nl_msg *msg;
9725b784 7897 int err;
0185007c
MK
7898 union wpa_event_data data;
7899 struct survey_results *survey_results;
7900
7901 os_memset(&data, 0, sizeof(data));
7902 survey_results = &data.survey_results;
7903
7904 dl_list_init(&survey_results->survey_list);
7905
9725b784 7906 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
0185007c 7907 if (!msg)
a862e4a3 7908 return -ENOBUFS;
0185007c 7909
0185007c
MK
7910 if (freq)
7911 data.survey_results.freq_filter = freq;
7912
7913 do {
7914 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7915 err = send_and_recv_msgs(drv, msg, survey_handler,
7916 survey_results);
7917 } while (err > 0);
7918
a862e4a3 7919 if (err)
0185007c 7920 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
a862e4a3
JM
7921 else
7922 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
0185007c 7923
0185007c 7924 clean_survey_results(survey_results);
0185007c
MK
7925 return err;
7926}
7927
7928
98cd3d1c
JM
7929static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7930 const u8 *kck, size_t kck_len,
b14a210c
JB
7931 const u8 *replay_ctr)
7932{
7933 struct i802_bss *bss = priv;
7934 struct wpa_driver_nl80211_data *drv = bss->drv;
7935 struct nlattr *replay_nested;
7936 struct nl_msg *msg;
64ae2447 7937 int ret;
b14a210c 7938
64ae2447
JM
7939 if (!drv->set_rekey_offload)
7940 return;
7941
7942 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
13f83980 7943 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
a862e4a3 7944 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
98cd3d1c 7945 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
b6ea7642 7946 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
a862e4a3
JM
7947 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7948 replay_ctr)) {
bbd89bfc 7949 nl80211_nlmsg_clear(msg);
a862e4a3
JM
7950 nlmsg_free(msg);
7951 return;
7952 }
b14a210c
JB
7953
7954 nla_nest_end(msg, replay_nested);
7955
64ae2447
JM
7956 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7957 if (ret == -EOPNOTSUPP) {
7958 wpa_printf(MSG_DEBUG,
7959 "nl80211: Driver does not support rekey offload");
7960 drv->set_rekey_offload = 0;
7961 }
b14a210c
JB
7962}
7963
7964
39718852
JB
7965static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7966 const u8 *addr, int qos)
bcf24348 7967{
39718852
JB
7968 /* send data frame to poll STA and check whether
7969 * this frame is ACKed */
bcf24348
JB
7970 struct {
7971 struct ieee80211_hdr hdr;
7972 u16 qos_ctl;
7973 } STRUCT_PACKED nulldata;
7974 size_t size;
7975
7976 /* Send data frame to poll STA and check whether this frame is ACKed */
7977
7978 os_memset(&nulldata, 0, sizeof(nulldata));
7979
7980 if (qos) {
7981 nulldata.hdr.frame_control =
7982 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7983 WLAN_FC_STYPE_QOS_NULL);
7984 size = sizeof(nulldata);
7985 } else {
7986 nulldata.hdr.frame_control =
7987 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7988 WLAN_FC_STYPE_NULLFUNC);
7989 size = sizeof(struct ieee80211_hdr);
7990 }
7991
7992 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7993 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7994 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7995 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7996
9ebce9c5 7997 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
2d3943ce 7998 0, 0, NULL, 0) < 0)
bcf24348
JB
7999 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8000 "send poll frame");
8001}
8002
39718852
JB
8003static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8004 int qos)
8005{
8006 struct i802_bss *bss = priv;
8007 struct wpa_driver_nl80211_data *drv = bss->drv;
8008 struct nl_msg *msg;
fc99fab7 8009 int ret;
39718852
JB
8010
8011 if (!drv->poll_command_supported) {
8012 nl80211_send_null_frame(bss, own_addr, addr, qos);
8013 return;
8014 }
8015
13f83980 8016 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
a862e4a3
JM
8017 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8018 nlmsg_free(msg);
8019 return;
8020 }
39718852 8021
fc99fab7
JM
8022 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8023 if (ret < 0) {
8024 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8025 MACSTR " failed: ret=%d (%s)",
8026 MAC2STR(addr), ret, strerror(-ret));
8027 }
39718852
JB
8028}
8029
bcf24348 8030
29f338af
JM
8031static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8032{
8033 struct nl_msg *msg;
8034
13f83980 8035 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
a862e4a3
JM
8036 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8037 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8038 nlmsg_free(msg);
8039 return -ENOBUFS;
8040 }
29f338af 8041 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
29f338af
JM
8042}
8043
8044
8045static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8046 int ctwindow)
8047{
8048 struct i802_bss *bss = priv;
8049
8050 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8051 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8052
0de38036
JM
8053 if (opp_ps != -1 || ctwindow != -1) {
8054#ifdef ANDROID_P2P
8055 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
8056#else /* ANDROID_P2P */
29f338af 8057 return -1; /* Not yet supported */
0de38036
JM
8058#endif /* ANDROID_P2P */
8059 }
29f338af
JM
8060
8061 if (legacy_ps == -1)
8062 return 0;
8063 if (legacy_ps != 0 && legacy_ps != 1)
8064 return -1; /* Not yet supported */
8065
8066 return nl80211_set_power_save(bss, legacy_ps);
8067}
8068
8069
04e8003c
JD
8070static int nl80211_start_radar_detection(void *priv,
8071 struct hostapd_freq_params *freq)
f90e9c1c
SW
8072{
8073 struct i802_bss *bss = priv;
8074 struct wpa_driver_nl80211_data *drv = bss->drv;
8075 struct nl_msg *msg;
8076 int ret;
8077
04e8003c
JD
8078 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)",
8079 freq->freq, freq->ht_enabled, freq->vht_enabled,
8080 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8081
f90e9c1c
SW
8082 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8083 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8084 "detection");
8085 return -1;
8086 }
8087
9725b784 8088 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
a862e4a3
JM
8089 nl80211_put_freq_params(msg, freq) < 0) {
8090 nlmsg_free(msg);
8091 return -1;
8092 }
f90e9c1c
SW
8093
8094 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8095 if (ret == 0)
8096 return 0;
8097 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8098 "%d (%s)", ret, strerror(-ret));
f90e9c1c
SW
8099 return -1;
8100}
8101
03ea1786
AN
8102#ifdef CONFIG_TDLS
8103
8104static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8105 u8 dialog_token, u16 status_code,
984dadc2
AN
8106 u32 peer_capab, int initiator, const u8 *buf,
8107 size_t len)
03ea1786
AN
8108{
8109 struct i802_bss *bss = priv;
8110 struct wpa_driver_nl80211_data *drv = bss->drv;
8111 struct nl_msg *msg;
8112
8113 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8114 return -EOPNOTSUPP;
8115
8116 if (!dst)
8117 return -EINVAL;
8118
9725b784 8119 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
a862e4a3
JM
8120 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8121 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8122 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8123 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8124 goto fail;
96ecea5e
SD
8125 if (peer_capab) {
8126 /*
8127 * The internal enum tdls_peer_capability definition is
8128 * currently identical with the nl80211 enum
8129 * nl80211_tdls_peer_capability, so no conversion is needed
8130 * here.
8131 */
a862e4a3
JM
8132 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8133 peer_capab))
8134 goto fail;
96ecea5e 8135 }
a862e4a3
JM
8136 if ((initiator &&
8137 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8138 nla_put(msg, NL80211_ATTR_IE, len, buf))
8139 goto fail;
03ea1786
AN
8140
8141 return send_and_recv_msgs(drv, msg, NULL, NULL);
8142
a862e4a3 8143fail:
03ea1786
AN
8144 nlmsg_free(msg);
8145 return -ENOBUFS;
8146}
8147
8148
8149static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8150{
8151 struct i802_bss *bss = priv;
8152 struct wpa_driver_nl80211_data *drv = bss->drv;
8153 struct nl_msg *msg;
8154 enum nl80211_tdls_operation nl80211_oper;
b2442f25 8155 int res;
03ea1786
AN
8156
8157 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8158 return -EOPNOTSUPP;
8159
8160 switch (oper) {
8161 case TDLS_DISCOVERY_REQ:
8162 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8163 break;
8164 case TDLS_SETUP:
8165 nl80211_oper = NL80211_TDLS_SETUP;
8166 break;
8167 case TDLS_TEARDOWN:
8168 nl80211_oper = NL80211_TDLS_TEARDOWN;
8169 break;
8170 case TDLS_ENABLE_LINK:
8171 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8172 break;
8173 case TDLS_DISABLE_LINK:
8174 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8175 break;
8176 case TDLS_ENABLE:
8177 return 0;
8178 case TDLS_DISABLE:
8179 return 0;
8180 default:
8181 return -EINVAL;
8182 }
8183
9725b784 8184 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
a862e4a3 8185 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
a862e4a3
JM
8186 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8187 nlmsg_free(msg);
8188 return -ENOBUFS;
8189 }
03ea1786 8190
b2442f25
JM
8191 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8192 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8193 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8194 strerror(-res));
8195 return res;
03ea1786
AN
8196}
8197
72b2605f
AN
8198
8199static int
8200nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8201 const struct hostapd_freq_params *params)
8202{
8203 struct i802_bss *bss = priv;
8204 struct wpa_driver_nl80211_data *drv = bss->drv;
8205 struct nl_msg *msg;
8206 int ret = -ENOBUFS;
8207
8208 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8209 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8210 return -EOPNOTSUPP;
8211
8212 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8213 " oper_class=%u freq=%u",
8214 MAC2STR(addr), oper_class, params->freq);
8215 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8216 if (!msg ||
8217 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8218 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8219 (ret = nl80211_put_freq_params(msg, params))) {
8220 nlmsg_free(msg);
8221 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8222 return ret;
8223 }
8224
8225 return send_and_recv_msgs(drv, msg, NULL, NULL);
8226}
8227
8228
8229static int
8230nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8231{
8232 struct i802_bss *bss = priv;
8233 struct wpa_driver_nl80211_data *drv = bss->drv;
8234 struct nl_msg *msg;
8235
8236 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8237 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8238 return -EOPNOTSUPP;
8239
8240 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8241 MAC2STR(addr));
8242 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8243 if (!msg ||
8244 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8245 nlmsg_free(msg);
8246 wpa_printf(MSG_DEBUG,
8247 "nl80211: Could not build TDLS cancel chan switch");
8248 return -ENOBUFS;
8249 }
8250
8251 return send_and_recv_msgs(drv, msg, NULL, NULL);
8252}
8253
03ea1786
AN
8254#endif /* CONFIG TDLS */
8255
8256
9ebce9c5
JM
8257static int driver_nl80211_set_key(const char *ifname, void *priv,
8258 enum wpa_alg alg, const u8 *addr,
8259 int key_idx, int set_tx,
8260 const u8 *seq, size_t seq_len,
8261 const u8 *key, size_t key_len)
8262{
8263 struct i802_bss *bss = priv;
8264 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8265 set_tx, seq, seq_len, key, key_len);
8266}
8267
8268
8269static int driver_nl80211_scan2(void *priv,
8270 struct wpa_driver_scan_params *params)
8271{
8272 struct i802_bss *bss = priv;
b658547d 8273#ifdef CONFIG_DRIVER_NL80211_QCA
adcd7c4b
KV
8274 struct wpa_driver_nl80211_data *drv = bss->drv;
8275
8276 /*
8277 * Do a vendor specific scan if possible. If only_new_results is
8278 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8279 * cannot be achieved through a vendor scan. The below condition may
8280 * need to be modified if new scan flags are added in the future whose
8281 * functionality can only be achieved through a normal scan.
8282 */
8283 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8284 return wpa_driver_nl80211_vendor_scan(bss, params);
b658547d 8285#endif /* CONFIG_DRIVER_NL80211_QCA */
9ebce9c5
JM
8286 return wpa_driver_nl80211_scan(bss, params);
8287}
8288
8289
8290static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8291 int reason_code)
8292{
8293 struct i802_bss *bss = priv;
8294 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8295}
8296
8297
8298static int driver_nl80211_authenticate(void *priv,
8299 struct wpa_driver_auth_params *params)
8300{
8301 struct i802_bss *bss = priv;
8302 return wpa_driver_nl80211_authenticate(bss, params);
8303}
8304
8305
8306static void driver_nl80211_deinit(void *priv)
8307{
8308 struct i802_bss *bss = priv;
8309 wpa_driver_nl80211_deinit(bss);
8310}
8311
8312
8313static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8314 const char *ifname)
8315{
8316 struct i802_bss *bss = priv;
8317 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8318}
8319
8320
8321static int driver_nl80211_send_mlme(void *priv, const u8 *data,
5d180a77 8322 size_t data_len, int noack,
2d3943ce
AO
8323 unsigned int freq,
8324 const u16 *csa_offs, size_t csa_offs_len)
9ebce9c5
JM
8325{
8326 struct i802_bss *bss = priv;
8327 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
2d3943ce
AO
8328 freq, 0, 0, 0, csa_offs,
8329 csa_offs_len);
9ebce9c5
JM
8330}
8331
8332
8333static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8334{
8335 struct i802_bss *bss = priv;
59d7148a 8336 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
9ebce9c5
JM
8337}
8338
8339
9ebce9c5
JM
8340static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8341 const char *ifname, int vlan_id)
8342{
8343 struct i802_bss *bss = priv;
8344 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8345}
9ebce9c5
JM
8346
8347
8348static int driver_nl80211_read_sta_data(void *priv,
8349 struct hostap_sta_driver_data *data,
8350 const u8 *addr)
8351{
8352 struct i802_bss *bss = priv;
7824bf77
JC
8353
8354 os_memset(data, 0, sizeof(*data));
9ebce9c5
JM
8355 return i802_read_sta_data(bss, data, addr);
8356}
8357
8358
8359static int driver_nl80211_send_action(void *priv, unsigned int freq,
8360 unsigned int wait_time,
8361 const u8 *dst, const u8 *src,
8362 const u8 *bssid,
8363 const u8 *data, size_t data_len,
8364 int no_cck)
8365{
8366 struct i802_bss *bss = priv;
8367 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8368 bssid, data, data_len, no_cck);
8369}
8370
8371
8372static int driver_nl80211_probe_req_report(void *priv, int report)
8373{
8374 struct i802_bss *bss = priv;
8375 return wpa_driver_nl80211_probe_req_report(bss, report);
8376}
8377
8378
6a1ce395
DG
8379static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8380 const u8 *ies, size_t ies_len)
8381{
8382 int ret;
8383 struct nl_msg *msg;
8384 struct i802_bss *bss = priv;
8385 struct wpa_driver_nl80211_data *drv = bss->drv;
8386 u16 mdid = WPA_GET_LE16(md);
8387
6a1ce395 8388 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9725b784 8389 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
a862e4a3
JM
8390 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8391 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8392 nlmsg_free(msg);
8393 return -ENOBUFS;
8394 }
6a1ce395
DG
8395
8396 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8397 if (ret) {
8398 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8399 "err=%d (%s)", ret, strerror(-ret));
8400 }
8401
8402 return ret;
6a1ce395
DG
8403}
8404
8405
47754718 8406static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
597b94f5
AS
8407{
8408 struct i802_bss *bss = priv;
8409 struct wpa_driver_nl80211_data *drv = bss->drv;
8410
8411 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8412 return NULL;
8413
8414 return bss->addr;
8415}
8416
8417
a771c07d
JM
8418static const char * scan_state_str(enum scan_states scan_state)
8419{
8420 switch (scan_state) {
8421 case NO_SCAN:
8422 return "NO_SCAN";
8423 case SCAN_REQUESTED:
8424 return "SCAN_REQUESTED";
8425 case SCAN_STARTED:
8426 return "SCAN_STARTED";
8427 case SCAN_COMPLETED:
8428 return "SCAN_COMPLETED";
8429 case SCAN_ABORTED:
8430 return "SCAN_ABORTED";
8431 case SCHED_SCAN_STARTED:
8432 return "SCHED_SCAN_STARTED";
8433 case SCHED_SCAN_STOPPED:
8434 return "SCHED_SCAN_STOPPED";
8435 case SCHED_SCAN_RESULTS:
8436 return "SCHED_SCAN_RESULTS";
8437 }
8438
8439 return "??";
8440}
8441
8442
8443static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8444{
8445 struct i802_bss *bss = priv;
8446 struct wpa_driver_nl80211_data *drv = bss->drv;
8447 int res;
8448 char *pos, *end;
8449
8450 pos = buf;
8451 end = buf + buflen;
8452
8453 res = os_snprintf(pos, end - pos,
8454 "ifindex=%d\n"
8455 "ifname=%s\n"
8456 "brname=%s\n"
8457 "addr=" MACSTR "\n"
8458 "freq=%d\n"
8459 "%s%s%s%s%s",
8460 bss->ifindex,
8461 bss->ifname,
8462 bss->brname,
8463 MAC2STR(bss->addr),
8464 bss->freq,
8465 bss->beacon_set ? "beacon_set=1\n" : "",
8466 bss->added_if_into_bridge ?
8467 "added_if_into_bridge=1\n" : "",
8468 bss->added_bridge ? "added_bridge=1\n" : "",
8469 bss->in_deinit ? "in_deinit=1\n" : "",
8470 bss->if_dynamic ? "if_dynamic=1\n" : "");
d85e1fc8 8471 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8472 return pos - buf;
8473 pos += res;
8474
8475 if (bss->wdev_id_set) {
8476 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8477 (unsigned long long) bss->wdev_id);
d85e1fc8 8478 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8479 return pos - buf;
8480 pos += res;
8481 }
8482
8483 res = os_snprintf(pos, end - pos,
8484 "phyname=%s\n"
fee354c7 8485 "perm_addr=" MACSTR "\n"
a771c07d
JM
8486 "drv_ifindex=%d\n"
8487 "operstate=%d\n"
8488 "scan_state=%s\n"
8489 "auth_bssid=" MACSTR "\n"
8490 "auth_attempt_bssid=" MACSTR "\n"
8491 "bssid=" MACSTR "\n"
8492 "prev_bssid=" MACSTR "\n"
8493 "associated=%d\n"
8494 "assoc_freq=%u\n"
8495 "monitor_sock=%d\n"
8496 "monitor_ifidx=%d\n"
8497 "monitor_refcount=%d\n"
8498 "last_mgmt_freq=%u\n"
8499 "eapol_tx_sock=%d\n"
97752f79 8500 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
a771c07d 8501 drv->phyname,
fee354c7 8502 MAC2STR(drv->perm_addr),
a771c07d
JM
8503 drv->ifindex,
8504 drv->operstate,
8505 scan_state_str(drv->scan_state),
8506 MAC2STR(drv->auth_bssid),
8507 MAC2STR(drv->auth_attempt_bssid),
8508 MAC2STR(drv->bssid),
8509 MAC2STR(drv->prev_bssid),
8510 drv->associated,
8511 drv->assoc_freq,
8512 drv->monitor_sock,
8513 drv->monitor_ifidx,
8514 drv->monitor_refcount,
8515 drv->last_mgmt_freq,
8516 drv->eapol_tx_sock,
8517 drv->ignore_if_down_event ?
8518 "ignore_if_down_event=1\n" : "",
8519 drv->scan_complete_events ?
8520 "scan_complete_events=1\n" : "",
8521 drv->disabled_11b_rates ?
8522 "disabled_11b_rates=1\n" : "",
8523 drv->pending_remain_on_chan ?
8524 "pending_remain_on_chan=1\n" : "",
8525 drv->in_interface_list ? "in_interface_list=1\n" : "",
8526 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8527 drv->poll_command_supported ?
8528 "poll_command_supported=1\n" : "",
8529 drv->data_tx_status ? "data_tx_status=1\n" : "",
8530 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8531 drv->retry_auth ? "retry_auth=1\n" : "",
8532 drv->use_monitor ? "use_monitor=1\n" : "",
8533 drv->ignore_next_local_disconnect ?
8534 "ignore_next_local_disconnect=1\n" : "",
d6a36f39 8535 drv->ignore_next_local_deauth ?
97752f79 8536 "ignore_next_local_deauth=1\n" : "");
d85e1fc8 8537 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8538 return pos - buf;
8539 pos += res;
8540
8541 if (drv->has_capability) {
8542 res = os_snprintf(pos, end - pos,
8543 "capa.key_mgmt=0x%x\n"
8544 "capa.enc=0x%x\n"
8545 "capa.auth=0x%x\n"
24bd4e0b 8546 "capa.flags=0x%llx\n"
a0a34d53 8547 "capa.rrm_flags=0x%x\n"
a771c07d
JM
8548 "capa.max_scan_ssids=%d\n"
8549 "capa.max_sched_scan_ssids=%d\n"
8550 "capa.sched_scan_supported=%d\n"
8551 "capa.max_match_sets=%d\n"
8552 "capa.max_remain_on_chan=%u\n"
8553 "capa.max_stations=%u\n"
8554 "capa.probe_resp_offloads=0x%x\n"
8555 "capa.max_acl_mac_addrs=%u\n"
86056fea
IP
8556 "capa.num_multichan_concurrent=%u\n"
8557 "capa.mac_addr_rand_sched_scan_supported=%d\n"
079a28f7
AK
8558 "capa.mac_addr_rand_scan_supported=%d\n"
8559 "capa.conc_capab=%u\n"
8560 "capa.max_conc_chan_2_4=%u\n"
09ea4309
AS
8561 "capa.max_conc_chan_5_0=%u\n"
8562 "capa.max_sched_scan_plans=%u\n"
8563 "capa.max_sched_scan_plan_interval=%u\n"
8564 "capa.max_sched_scan_plan_iterations=%u\n",
a771c07d
JM
8565 drv->capa.key_mgmt,
8566 drv->capa.enc,
8567 drv->capa.auth,
24bd4e0b 8568 (unsigned long long) drv->capa.flags,
a0a34d53 8569 drv->capa.rrm_flags,
a771c07d
JM
8570 drv->capa.max_scan_ssids,
8571 drv->capa.max_sched_scan_ssids,
8572 drv->capa.sched_scan_supported,
8573 drv->capa.max_match_sets,
8574 drv->capa.max_remain_on_chan,
8575 drv->capa.max_stations,
8576 drv->capa.probe_resp_offloads,
8577 drv->capa.max_acl_mac_addrs,
86056fea
IP
8578 drv->capa.num_multichan_concurrent,
8579 drv->capa.mac_addr_rand_sched_scan_supported,
079a28f7
AK
8580 drv->capa.mac_addr_rand_scan_supported,
8581 drv->capa.conc_capab,
8582 drv->capa.max_conc_chan_2_4,
09ea4309
AS
8583 drv->capa.max_conc_chan_5_0,
8584 drv->capa.max_sched_scan_plans,
8585 drv->capa.max_sched_scan_plan_interval,
8586 drv->capa.max_sched_scan_plan_iterations);
d85e1fc8 8587 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8588 return pos - buf;
8589 pos += res;
8590 }
8591
8592 return pos - buf;
8593}
8594
8595
1c4ffa87
AO
8596static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8597{
a862e4a3
JM
8598 if ((settings->head &&
8599 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8600 settings->head_len, settings->head)) ||
8601 (settings->tail &&
8602 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8603 settings->tail_len, settings->tail)) ||
8604 (settings->beacon_ies &&
8605 nla_put(msg, NL80211_ATTR_IE,
8606 settings->beacon_ies_len, settings->beacon_ies)) ||
8607 (settings->proberesp_ies &&
8608 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8609 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8610 (settings->assocresp_ies &&
8611 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8612 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8613 (settings->probe_resp &&
8614 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8615 settings->probe_resp_len, settings->probe_resp)))
8616 return -ENOBUFS;
1c4ffa87
AO
8617
8618 return 0;
1c4ffa87
AO
8619}
8620
8621
8622static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8623{
8624 struct nl_msg *msg;
8625 struct i802_bss *bss = priv;
8626 struct wpa_driver_nl80211_data *drv = bss->drv;
8627 struct nlattr *beacon_csa;
8628 int ret = -ENOBUFS;
366179d2
AO
8629 int csa_off_len = 0;
8630 int i;
1c4ffa87 8631
8d1fdde7 8632 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
1c4ffa87 8633 settings->cs_count, settings->block_tx,
8d1fdde7
JD
8634 settings->freq_params.freq, settings->freq_params.bandwidth,
8635 settings->freq_params.center_freq1,
8636 settings->freq_params.center_freq2);
1c4ffa87 8637
991aa9c7 8638 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
1c4ffa87
AO
8639 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8640 return -EOPNOTSUPP;
8641 }
8642
8643 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
8644 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
8645 return -EOPNOTSUPP;
8646
366179d2
AO
8647 /*
8648 * Remove empty counters, assuming Probe Response and Beacon frame
8649 * counters match. This implementation assumes that there are only two
8650 * counters.
8651 */
8652 if (settings->counter_offset_beacon[0] &&
8653 !settings->counter_offset_beacon[1]) {
8654 csa_off_len = 1;
8655 } else if (settings->counter_offset_beacon[1] &&
8656 !settings->counter_offset_beacon[0]) {
8657 csa_off_len = 1;
8658 settings->counter_offset_beacon[0] =
8659 settings->counter_offset_beacon[1];
8660 settings->counter_offset_presp[0] =
8661 settings->counter_offset_presp[1];
8662 } else if (settings->counter_offset_beacon[1] &&
8663 settings->counter_offset_beacon[0]) {
8664 csa_off_len = 2;
8665 } else {
8666 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8667 return -EINVAL;
8668 }
8669
8670 /* Check CSA counters validity */
8671 if (drv->capa.max_csa_counters &&
8672 csa_off_len > drv->capa.max_csa_counters) {
8673 wpa_printf(MSG_ERROR,
8674 "nl80211: Too many CSA counters provided");
1c4ffa87 8675 return -EINVAL;
366179d2 8676 }
1c4ffa87 8677
366179d2 8678 if (!settings->beacon_csa.tail)
1c4ffa87
AO
8679 return -EINVAL;
8680
366179d2
AO
8681 for (i = 0; i < csa_off_len; i++) {
8682 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8683 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8684
8685 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8686 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8687 settings->cs_count))
8688 return -EINVAL;
8689
8690 if (settings->beacon_csa.probe_resp &&
8691 ((settings->beacon_csa.probe_resp_len <=
8692 csa_c_off_presp) ||
8693 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8694 settings->cs_count)))
8695 return -EINVAL;
8696 }
8697
13f83980 8698 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
a862e4a3
JM
8699 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8700 settings->cs_count) ||
8701 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8702 (settings->block_tx &&
8703 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
1c4ffa87
AO
8704 goto error;
8705
1c4ffa87
AO
8706 /* beacon_after params */
8707 ret = set_beacon_data(msg, &settings->beacon_after);
8708 if (ret)
8709 goto error;
8710
8711 /* beacon_csa params */
8712 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8713 if (!beacon_csa)
a862e4a3 8714 goto fail;
1c4ffa87
AO
8715
8716 ret = set_beacon_data(msg, &settings->beacon_csa);
8717 if (ret)
8718 goto error;
8719
366179d2
AO
8720 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8721 csa_off_len * sizeof(u16),
8722 settings->counter_offset_beacon) ||
a862e4a3 8723 (settings->beacon_csa.probe_resp &&
366179d2
AO
8724 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8725 csa_off_len * sizeof(u16),
8726 settings->counter_offset_presp)))
a862e4a3 8727 goto fail;
1c4ffa87
AO
8728
8729 nla_nest_end(msg, beacon_csa);
8730 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8731 if (ret) {
8732 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8733 ret, strerror(-ret));
8734 }
8735 return ret;
8736
a862e4a3 8737fail:
1c4ffa87
AO
8738 ret = -ENOBUFS;
8739error:
8740 nlmsg_free(msg);
8741 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8742 return ret;
8743}
8744
8745
dfa87878
MB
8746static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8747 u8 user_priority, u16 admitted_time)
8748{
8749 struct i802_bss *bss = priv;
8750 struct wpa_driver_nl80211_data *drv = bss->drv;
8751 struct nl_msg *msg;
8752 int ret;
8753
8754 wpa_printf(MSG_DEBUG,
8755 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8756 tsid, admitted_time, user_priority);
8757
8758 if (!is_sta_interface(drv->nlmode))
8759 return -ENOTSUP;
8760
56f77852
JM
8761 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8762 if (!msg ||
a862e4a3
JM
8763 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8764 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8765 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8766 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8767 nlmsg_free(msg);
8768 return -ENOBUFS;
8769 }
dfa87878
MB
8770
8771 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8772 if (ret)
8773 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8774 ret, strerror(-ret));
8775 return ret;
dfa87878
MB
8776}
8777
8778
8779static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8780{
8781 struct i802_bss *bss = priv;
8782 struct wpa_driver_nl80211_data *drv = bss->drv;
8783 struct nl_msg *msg;
8784 int ret;
8785
8786 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8787
8788 if (!is_sta_interface(drv->nlmode))
8789 return -ENOTSUP;
8790
56f77852 8791 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
a862e4a3
JM
8792 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8793 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8794 nlmsg_free(msg);
8795 return -ENOBUFS;
8796 }
dfa87878
MB
8797
8798 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8799 if (ret)
8800 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8801 ret, strerror(-ret));
8802 return ret;
dfa87878
MB
8803}
8804
8805
6b9f7af6
JM
8806#ifdef CONFIG_TESTING_OPTIONS
8807static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8808{
8809 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8810 struct wpabuf *buf = arg;
8811
8812 if (!buf)
8813 return NL_SKIP;
8814
8815 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8816 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8817 return NL_SKIP;
8818 }
8819
8820 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8821 genlmsg_attrlen(gnlh, 0));
8822
8823 return NL_SKIP;
8824}
8825#endif /* CONFIG_TESTING_OPTIONS */
8826
8827
adef8948
BL
8828static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8829{
8830 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8831 struct nlattr *nl_vendor_reply, *nl;
8832 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8833 struct wpabuf *buf = arg;
8834 int rem;
8835
8836 if (!buf)
8837 return NL_SKIP;
8838
8839 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8840 genlmsg_attrlen(gnlh, 0), NULL);
8841 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8842
8843 if (!nl_vendor_reply)
8844 return NL_SKIP;
8845
8846 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8847 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8848 return NL_SKIP;
8849 }
8850
8851 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8852 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8853 }
8854
8855 return NL_SKIP;
8856}
8857
8858
8859static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8860 unsigned int subcmd, const u8 *data,
8861 size_t data_len, struct wpabuf *buf)
8862{
8863 struct i802_bss *bss = priv;
8864 struct wpa_driver_nl80211_data *drv = bss->drv;
8865 struct nl_msg *msg;
8866 int ret;
8867
6b9f7af6
JM
8868#ifdef CONFIG_TESTING_OPTIONS
8869 if (vendor_id == 0xffffffff) {
56f77852
JM
8870 msg = nlmsg_alloc();
8871 if (!msg)
8872 return -ENOMEM;
8873
6b9f7af6
JM
8874 nl80211_cmd(drv, msg, 0, subcmd);
8875 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8876 0)
a862e4a3 8877 goto fail;
6b9f7af6
JM
8878 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8879 if (ret)
8880 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8881 ret);
8882 return ret;
8883 }
8884#endif /* CONFIG_TESTING_OPTIONS */
8885
56f77852 8886 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
8887 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8888 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8889 (data &&
8890 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8891 goto fail;
adef8948
BL
8892
8893 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8894 if (ret)
8895 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8896 ret);
8897 return ret;
8898
a862e4a3 8899fail:
adef8948
BL
8900 nlmsg_free(msg);
8901 return -ENOBUFS;
8902}
8903
8904
049105b4
KP
8905static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8906 u8 qos_map_set_len)
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
049105b4
KP
8913 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8914 qos_map_set, qos_map_set_len);
8915
9725b784 8916 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
a862e4a3
JM
8917 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8918 nlmsg_free(msg);
8919 return -ENOBUFS;
8920 }
049105b4
KP
8921
8922 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8923 if (ret)
8924 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8925
8926 return ret;
049105b4
KP
8927}
8928
8929
e4fa8b12
EP
8930static int nl80211_set_wowlan(void *priv,
8931 const struct wowlan_triggers *triggers)
8932{
8933 struct i802_bss *bss = priv;
8934 struct wpa_driver_nl80211_data *drv = bss->drv;
8935 struct nl_msg *msg;
8936 struct nlattr *wowlan_triggers;
8937 int ret;
8938
e4fa8b12
EP
8939 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8940
1ac977bd 8941 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
a862e4a3
JM
8942 !(wowlan_triggers = nla_nest_start(msg,
8943 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8944 (triggers->any &&
8945 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8946 (triggers->disconnect &&
8947 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8948 (triggers->magic_pkt &&
8949 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8950 (triggers->gtk_rekey_failure &&
8951 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8952 (triggers->eap_identity_req &&
8953 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8954 (triggers->four_way_handshake &&
8955 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8956 (triggers->rfkill_release &&
8957 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8958 nlmsg_free(msg);
8959 return -ENOBUFS;
8960 }
e4fa8b12
EP
8961
8962 nla_nest_end(msg, wowlan_triggers);
8963
8964 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8965 if (ret)
8966 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8967
8968 return ret;
e4fa8b12
EP
8969}
8970
8971
b658547d 8972#ifdef CONFIG_DRIVER_NL80211_QCA
0800f9ee
JM
8973static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8974{
8975 struct i802_bss *bss = priv;
8976 struct wpa_driver_nl80211_data *drv = bss->drv;
8977 struct nl_msg *msg;
8978 struct nlattr *params;
8979
8980 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8981
8982 if (!drv->roaming_vendor_cmd_avail) {
8983 wpa_printf(MSG_DEBUG,
8984 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8985 return -1;
8986 }
8987
9725b784 8988 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
8989 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8990 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8991 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8992 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8993 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8994 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8995 QCA_ROAMING_NOT_ALLOWED) ||
8996 (bssid &&
8997 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8998 nlmsg_free(msg);
8999 return -1;
9000 }
0800f9ee
JM
9001 nla_nest_end(msg, params);
9002
9003 return send_and_recv_msgs(drv, msg, NULL, NULL);
0800f9ee 9004}
b04854ce
AP
9005
9006
d98038bb 9007static int nl80211_disable_fils(void *priv, int disable)
9008{
9009 struct i802_bss *bss = priv;
9010 struct wpa_driver_nl80211_data *drv = bss->drv;
9011 struct nl_msg *msg;
9012 struct nlattr *params;
9013
9014 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9015
9016 if (!drv->set_wifi_conf_vendor_cmd_avail)
9017 return -1;
9018
9019 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9020 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9021 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9022 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9023 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9024 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9025 disable)) {
9026 nlmsg_free(msg);
9027 return -1;
9028 }
9029 nla_nest_end(msg, params);
9030
9031 return send_and_recv_msgs(drv, msg, NULL, NULL);
9032}
9033
9034
b04854ce
AP
9035/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9036#define WPA_SUPPLICANT_CLIENT_ID 1
9037
9038static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9039 const u8 *bssid)
9040{
9041 struct i802_bss *bss = priv;
9042 struct wpa_driver_nl80211_data *drv = bss->drv;
9043 struct nl_msg *msg;
9044 struct nlattr *params, *nlbssids, *attr;
9045 unsigned int i;
9046
9047 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9048 num_bssid);
9049
9050 if (!drv->roam_vendor_cmd_avail)
9051 return -1;
9052
9053 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9054 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9055 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9056 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9057 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9058 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9059 QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9060 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9061 WPA_SUPPLICANT_CLIENT_ID) ||
9062 nla_put_u32(msg,
9063 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9064 num_bssid))
9065 goto fail;
9066
9067 nlbssids = nla_nest_start(
9068 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9069 if (!nlbssids)
9070 goto fail;
9071
9072 for (i = 0; i < num_bssid; i++) {
9073 attr = nla_nest_start(msg, i);
9074 if (!attr)
9075 goto fail;
9076 if (nla_put(msg,
9077 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9078 ETH_ALEN, &bssid[i * ETH_ALEN]))
9079 goto fail;
9080 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9081 MAC2STR(&bssid[i * ETH_ALEN]));
9082 nla_nest_end(msg, attr);
9083 }
9084 nla_nest_end(msg, nlbssids);
9085 nla_nest_end(msg, params);
9086
9087 return send_and_recv_msgs(drv, msg, NULL, NULL);
9088
9089fail:
9090 nlmsg_free(msg);
9091 return -1;
9092}
9093
b658547d 9094#endif /* CONFIG_DRIVER_NL80211_QCA */
0800f9ee
JM
9095
9096
fee354c7
JM
9097static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9098{
9099 struct i802_bss *bss = priv;
9100 struct wpa_driver_nl80211_data *drv = bss->drv;
9101 int new_addr = addr != NULL;
9102
9ce3e610
JM
9103 if (TEST_FAIL())
9104 return -1;
9105
fee354c7
JM
9106 if (!addr)
9107 addr = drv->perm_addr;
9108
9109 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9110 return -1;
9111
9112 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9113 {
9114 wpa_printf(MSG_DEBUG,
9115 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9116 bss->ifname, MAC2STR(addr));
9117 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9118 1) < 0) {
9119 wpa_printf(MSG_DEBUG,
9120 "nl80211: Could not restore interface UP after failed set_mac_addr");
9121 }
9122 return -1;
9123 }
9124
9125 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9126 bss->ifname, MAC2STR(addr));
9127 drv->addr_changed = new_addr;
9128 os_memcpy(bss->addr, addr, ETH_ALEN);
9129
9130 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9131 {
9132 wpa_printf(MSG_DEBUG,
9133 "nl80211: Could not restore interface UP after set_mac_addr");
9134 }
9135
9136 return 0;
9137}
9138
9139
6c1664f6
BC
9140#ifdef CONFIG_MESH
9141
9142static int wpa_driver_nl80211_init_mesh(void *priv)
9143{
9144 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9145 wpa_printf(MSG_INFO,
9146 "nl80211: Failed to set interface into mesh mode");
9147 return -1;
9148 }
9149 return 0;
9150}
9151
9152
21c74e84
JM
9153static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9154 size_t mesh_id_len)
9155{
9156 if (mesh_id) {
9157 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
9158 mesh_id, mesh_id_len);
9159 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9160 }
9161
9162 return 0;
9163}
9164
9165
4ffb3f87
MH
9166static int nl80211_put_mesh_config(struct nl_msg *msg,
9167 struct wpa_driver_mesh_bss_params *params)
9168{
9169 struct nlattr *container;
9170
9171 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9172 if (!container)
9173 return -1;
9174
2bd62171 9175 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
963d3149
JM
9176 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9177 params->auto_plinks)) ||
2bd62171
MH
9178 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9179 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
31a856a1
MH
9180 params->max_peer_links)) ||
9181 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9182 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9183 params->rssi_threshold)))
4ffb3f87
MH
9184 return -1;
9185
9186 /*
9187 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9188 * the timer could disconnect stations even in that case.
9189 */
2bd62171
MH
9190 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9191 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4ffb3f87
MH
9192 params->peer_link_timeout)) {
9193 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9194 return -1;
9195 }
9196
052b8d38
MH
9197 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9198 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9199 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9200 return -1;
9201 }
9202
4ffb3f87
MH
9203 nla_nest_end(msg, container);
9204
9205 return 0;
9206}
9207
9208
0cb5f8d9 9209static int nl80211_join_mesh(struct i802_bss *bss,
6c1664f6
BC
9210 struct wpa_driver_mesh_join_params *params)
9211{
6c1664f6
BC
9212 struct wpa_driver_nl80211_data *drv = bss->drv;
9213 struct nl_msg *msg;
9214 struct nlattr *container;
7451a217 9215 int ret = -1;
6c1664f6 9216
6c1664f6 9217 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9725b784 9218 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
f7e889fa
JM
9219 if (!msg ||
9220 nl80211_put_freq_params(msg, &params->freq) ||
21c74e84
JM
9221 nl80211_put_basic_rates(msg, params->basic_rates) ||
9222 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
4ac2ea57
MH
9223 nl80211_put_beacon_int(msg, params->beacon_int) ||
9224 nl80211_put_dtim_period(msg, params->dtim_period))
85e1fad8 9225 goto fail;
9c58c5f7 9226
6c1664f6
BC
9227 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
9228
9229 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9230 if (!container)
a862e4a3 9231 goto fail;
6c1664f6
BC
9232
9233 if (params->ies) {
9234 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
a862e4a3
JM
9235 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9236 params->ies))
9237 goto fail;
6c1664f6
BC
9238 }
9239 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9240 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
a862e4a3
JM
9241 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9242 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9243 goto fail;
6c1664f6 9244 }
a862e4a3
JM
9245 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9246 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9247 goto fail;
9248 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9249 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9250 goto fail;
6c1664f6
BC
9251 nla_nest_end(msg, container);
9252
2bd62171
MH
9253 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9254 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9255 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
4ffb3f87 9256 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
5a2a6de6 9257 goto fail;
6c1664f6
BC
9258
9259 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9260 msg = NULL;
9261 if (ret) {
9262 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9263 ret, strerror(-ret));
a862e4a3 9264 goto fail;
6c1664f6
BC
9265 }
9266 ret = 0;
92a515b8 9267 drv->assoc_freq = bss->freq = params->freq.freq;
6c1664f6
BC
9268 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9269
a862e4a3 9270fail:
6c1664f6
BC
9271 nlmsg_free(msg);
9272 return ret;
9273}
9274
9275
0cb5f8d9
MH
9276static int
9277wpa_driver_nl80211_join_mesh(void *priv,
9278 struct wpa_driver_mesh_join_params *params)
9279{
9280 struct i802_bss *bss = priv;
9281 int ret, timeout;
9282
9283 timeout = params->conf.peer_link_timeout;
9284
9285 /* Disable kernel inactivity timer */
9286 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9287 params->conf.peer_link_timeout = 0;
9288
9289 ret = nl80211_join_mesh(bss, params);
9290 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9291 wpa_printf(MSG_DEBUG,
9292 "nl80211: Mesh join retry for peer_link_timeout");
9293 /*
9294 * Old kernel does not support setting
9295 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9296 * into future from peer_link_timeout.
9297 */
9298 params->conf.peer_link_timeout = timeout + 60;
9299 ret = nl80211_join_mesh(priv, params);
9300 }
9301
9302 params->conf.peer_link_timeout = timeout;
9303 return ret;
9304}
9305
9306
6c1664f6
BC
9307static int wpa_driver_nl80211_leave_mesh(void *priv)
9308{
9309 struct i802_bss *bss = priv;
9310 struct wpa_driver_nl80211_data *drv = bss->drv;
9311 struct nl_msg *msg;
9725b784 9312 int ret;
6c1664f6
BC
9313
9314 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9725b784 9315 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
6c1664f6 9316 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6c1664f6
BC
9317 if (ret) {
9318 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9319 ret, strerror(-ret));
a862e4a3
JM
9320 } else {
9321 wpa_printf(MSG_DEBUG,
9322 "nl80211: mesh leave request send successfully");
6c1664f6 9323 }
6c1664f6 9324
f33c82d2
JM
9325 if (wpa_driver_nl80211_set_mode(drv->first_bss,
9326 NL80211_IFTYPE_STATION)) {
9327 wpa_printf(MSG_INFO,
9328 "nl80211: Failed to set interface into station mode");
9329 }
6c1664f6
BC
9330 return ret;
9331}
9332
9333#endif /* CONFIG_MESH */
9334
9335
ed4ddb6d
KP
9336static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9337 const u8 *ipaddr, int prefixlen,
9338 const u8 *addr)
71103bed
KP
9339{
9340#ifdef CONFIG_LIBNL3_ROUTE
9341 struct i802_bss *bss = priv;
9342 struct wpa_driver_nl80211_data *drv = bss->drv;
9343 struct rtnl_neigh *rn;
9344 struct nl_addr *nl_ipaddr = NULL;
9345 struct nl_addr *nl_lladdr = NULL;
ed4ddb6d 9346 int family, addrsize;
71103bed
KP
9347 int res;
9348
ed4ddb6d 9349 if (!ipaddr || prefixlen == 0 || !addr)
71103bed
KP
9350 return -EINVAL;
9351
9352 if (bss->br_ifindex == 0) {
9353 wpa_printf(MSG_DEBUG,
9354 "nl80211: bridge must be set before adding an ip neigh to it");
9355 return -1;
9356 }
9357
9358 if (!drv->rtnl_sk) {
9359 wpa_printf(MSG_DEBUG,
9360 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9361 return -1;
9362 }
9363
ed4ddb6d
KP
9364 if (version == 4) {
9365 family = AF_INET;
9366 addrsize = 4;
9367 } else if (version == 6) {
9368 family = AF_INET6;
9369 addrsize = 16;
9370 } else {
9371 return -EINVAL;
9372 }
9373
71103bed
KP
9374 rn = rtnl_neigh_alloc();
9375 if (rn == NULL)
9376 return -ENOMEM;
9377
9378 /* set the destination ip address for neigh */
ed4ddb6d 9379 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
71103bed
KP
9380 if (nl_ipaddr == NULL) {
9381 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9382 res = -ENOMEM;
9383 goto errout;
9384 }
9385 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9386 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9387 if (res) {
9388 wpa_printf(MSG_DEBUG,
9389 "nl80211: neigh set destination addr failed");
9390 goto errout;
9391 }
9392
9393 /* set the corresponding lladdr for neigh */
9394 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9395 if (nl_lladdr == NULL) {
9396 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9397 res = -ENOMEM;
9398 goto errout;
9399 }
9400 rtnl_neigh_set_lladdr(rn, nl_lladdr);
9401
9402 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9403 rtnl_neigh_set_state(rn, NUD_PERMANENT);
9404
9405 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9406 if (res) {
9407 wpa_printf(MSG_DEBUG,
9408 "nl80211: Adding bridge ip neigh failed: %s",
9409 strerror(errno));
9410 }
9411errout:
9412 if (nl_lladdr)
9413 nl_addr_put(nl_lladdr);
9414 if (nl_ipaddr)
9415 nl_addr_put(nl_ipaddr);
9416 if (rn)
9417 rtnl_neigh_put(rn);
9418 return res;
9419#else /* CONFIG_LIBNL3_ROUTE */
9420 return -1;
9421#endif /* CONFIG_LIBNL3_ROUTE */
9422}
9423
9424
ed4ddb6d
KP
9425static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9426 const u8 *ipaddr)
71103bed
KP
9427{
9428#ifdef CONFIG_LIBNL3_ROUTE
9429 struct i802_bss *bss = priv;
9430 struct wpa_driver_nl80211_data *drv = bss->drv;
9431 struct rtnl_neigh *rn;
9432 struct nl_addr *nl_ipaddr;
ed4ddb6d 9433 int family, addrsize;
71103bed
KP
9434 int res;
9435
ed4ddb6d 9436 if (!ipaddr)
71103bed
KP
9437 return -EINVAL;
9438
ed4ddb6d
KP
9439 if (version == 4) {
9440 family = AF_INET;
9441 addrsize = 4;
9442 } else if (version == 6) {
9443 family = AF_INET6;
9444 addrsize = 16;
9445 } else {
9446 return -EINVAL;
9447 }
9448
71103bed
KP
9449 if (bss->br_ifindex == 0) {
9450 wpa_printf(MSG_DEBUG,
9451 "nl80211: bridge must be set to delete an ip neigh");
9452 return -1;
9453 }
9454
9455 if (!drv->rtnl_sk) {
9456 wpa_printf(MSG_DEBUG,
9457 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9458 return -1;
9459 }
9460
9461 rn = rtnl_neigh_alloc();
9462 if (rn == NULL)
9463 return -ENOMEM;
9464
9465 /* set the destination ip address for neigh */
ed4ddb6d 9466 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
71103bed
KP
9467 if (nl_ipaddr == NULL) {
9468 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9469 res = -ENOMEM;
9470 goto errout;
9471 }
9472 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9473 if (res) {
9474 wpa_printf(MSG_DEBUG,
9475 "nl80211: neigh set destination addr failed");
9476 goto errout;
9477 }
9478
9479 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9480
9481 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9482 if (res) {
9483 wpa_printf(MSG_DEBUG,
9484 "nl80211: Deleting bridge ip neigh failed: %s",
9485 strerror(errno));
9486 }
9487errout:
9488 if (nl_ipaddr)
9489 nl_addr_put(nl_ipaddr);
9490 if (rn)
9491 rtnl_neigh_put(rn);
9492 return res;
9493#else /* CONFIG_LIBNL3_ROUTE */
9494 return -1;
9495#endif /* CONFIG_LIBNL3_ROUTE */
9496}
9497
9498
73d2294f
KP
9499static int linux_write_system_file(const char *path, unsigned int val)
9500{
9501 char buf[50];
9502 int fd, len;
9503
9504 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
a9aaacbb 9505 if (os_snprintf_error(sizeof(buf), len))
73d2294f
KP
9506 return -1;
9507
9508 fd = open(path, O_WRONLY);
9509 if (fd < 0)
9510 return -1;
9511
9512 if (write(fd, buf, len) < 0) {
9513 wpa_printf(MSG_DEBUG,
9514 "nl80211: Failed to write Linux system file: %s with the value of %d",
9515 path, val);
9516 close(fd);
9517 return -1;
9518 }
9519 close(fd);
9520
9521 return 0;
9522}
9523
9524
9525static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9526{
9527 switch (attr) {
9528 case DRV_BR_PORT_ATTR_PROXYARP:
bea8d9a3 9529 return "proxyarp_wifi";
73d2294f
KP
9530 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9531 return "hairpin_mode";
9532 }
9533
9534 return NULL;
9535}
9536
9537
9538static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9539 unsigned int val)
9540{
9541 struct i802_bss *bss = priv;
9542 char path[128];
9543 const char *attr_txt;
9544
9545 attr_txt = drv_br_port_attr_str(attr);
9546 if (attr_txt == NULL)
9547 return -EINVAL;
9548
9549 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9550 bss->ifname, attr_txt);
9551
9552 if (linux_write_system_file(path, val))
9553 return -1;
9554
9555 return 0;
9556}
9557
9558
7565752d
KP
9559static const char * drv_br_net_param_str(enum drv_br_net_param param)
9560{
9561 switch (param) {
9562 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9563 return "arp_accept";
60eb9e17
JM
9564 default:
9565 return NULL;
7565752d 9566 }
7565752d
KP
9567}
9568
9569
9570static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9571 unsigned int val)
9572{
9573 struct i802_bss *bss = priv;
9574 char path[128];
9575 const char *param_txt;
9576 int ip_version = 4;
9577
60eb9e17
JM
9578 if (param == DRV_BR_MULTICAST_SNOOPING) {
9579 os_snprintf(path, sizeof(path),
9580 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9581 bss->brname);
9582 goto set_val;
9583 }
9584
7565752d
KP
9585 param_txt = drv_br_net_param_str(param);
9586 if (param_txt == NULL)
9587 return -EINVAL;
9588
9589 switch (param) {
9590 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9591 ip_version = 4;
9592 break;
9593 default:
9594 return -EINVAL;
9595 }
9596
9597 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9598 ip_version, bss->brname, param_txt);
9599
60eb9e17 9600set_val:
7565752d
KP
9601 if (linux_write_system_file(path, val))
9602 return -1;
9603
9604 return 0;
9605}
9606
9607
b658547d
JM
9608#ifdef CONFIG_DRIVER_NL80211_QCA
9609
16689c7c
PX
9610static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9611{
9612 switch (hw_mode) {
9613 case HOSTAPD_MODE_IEEE80211B:
9614 return QCA_ACS_MODE_IEEE80211B;
9615 case HOSTAPD_MODE_IEEE80211G:
9616 return QCA_ACS_MODE_IEEE80211G;
9617 case HOSTAPD_MODE_IEEE80211A:
9618 return QCA_ACS_MODE_IEEE80211A;
9619 case HOSTAPD_MODE_IEEE80211AD:
9620 return QCA_ACS_MODE_IEEE80211AD;
3784c058
PX
9621 case HOSTAPD_MODE_IEEE80211ANY:
9622 return QCA_ACS_MODE_IEEE80211ANY;
16689c7c
PX
9623 default:
9624 return -1;
9625 }
9626}
9627
9628
d0cdccd3
PX
9629static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9630{
9631 int i, len, ret;
9632 u32 *freqs;
9633
9634 if (!freq_list)
9635 return 0;
9636 len = int_array_len(freq_list);
9637 freqs = os_malloc(sizeof(u32) * len);
9638 if (!freqs)
9639 return -1;
9640 for (i = 0; i < len; i++)
9641 freqs[i] = freq_list[i];
9642 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9643 sizeof(u32) * len, freqs);
9644 os_free(freqs);
9645 return ret;
9646}
9647
9648
16689c7c
PX
9649static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9650{
9651 struct i802_bss *bss = priv;
9652 struct wpa_driver_nl80211_data *drv = bss->drv;
9653 struct nl_msg *msg;
9654 struct nlattr *data;
a862e4a3 9655 int ret;
16689c7c
PX
9656 int mode;
9657
9658 mode = hw_mode_to_qca_acs(params->hw_mode);
9659 if (mode < 0)
9660 return -1;
9661
9725b784 9662 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
9663 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9664 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9665 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9666 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9667 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9668 (params->ht_enabled &&
9669 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9670 (params->ht40_enabled &&
857d9422
MM
9671 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9672 (params->vht_enabled &&
9673 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9674 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9675 params->ch_width) ||
9676 (params->ch_list_len &&
9677 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
d0cdccd3
PX
9678 params->ch_list)) ||
9679 add_acs_freq_list(msg, params->freq_list)) {
a862e4a3
JM
9680 nlmsg_free(msg);
9681 return -ENOBUFS;
9682 }
16689c7c
PX
9683 nla_nest_end(msg, data);
9684
857d9422
MM
9685 wpa_printf(MSG_DEBUG,
9686 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9687 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9688 params->vht_enabled, params->ch_width, params->ch_list_len);
9689
16689c7c 9690 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
16689c7c
PX
9691 if (ret) {
9692 wpa_printf(MSG_DEBUG,
9693 "nl80211: Failed to invoke driver ACS function: %s",
9694 strerror(errno));
9695 }
16689c7c
PX
9696 return ret;
9697}
9698
9699
844dfeb8
SD
9700static int nl80211_set_band(void *priv, enum set_band band)
9701{
9702 struct i802_bss *bss = priv;
9703 struct wpa_driver_nl80211_data *drv = bss->drv;
9704 struct nl_msg *msg;
9705 struct nlattr *data;
9706 int ret;
9707 enum qca_set_band qca_band;
9708
9709 if (!drv->setband_vendor_cmd_avail)
9710 return -1;
9711
9712 switch (band) {
9713 case WPA_SETBAND_AUTO:
9714 qca_band = QCA_SETBAND_AUTO;
9715 break;
9716 case WPA_SETBAND_5G:
9717 qca_band = QCA_SETBAND_5G;
9718 break;
9719 case WPA_SETBAND_2G:
9720 qca_band = QCA_SETBAND_2G;
9721 break;
9722 default:
9723 return -1;
9724 }
9725
9726 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9727 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9728 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9729 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9730 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9731 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9732 nlmsg_free(msg);
9733 return -ENOBUFS;
9734 }
9735 nla_nest_end(msg, data);
9736
9737 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9738 if (ret) {
9739 wpa_printf(MSG_DEBUG,
9740 "nl80211: Driver setband function failed: %s",
9741 strerror(errno));
9742 }
9743 return ret;
9744}
9745
9746
98342208
AK
9747struct nl80211_pcl {
9748 unsigned int num;
9749 unsigned int *freq_list;
9750};
9751
9752static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9753{
9754 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9755 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9756 struct nl80211_pcl *param = arg;
9757 struct nlattr *nl_vend, *attr;
9758 enum qca_iface_type iface_type;
9759 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9760 unsigned int num, max_num;
9761 u32 *freqs;
9762
9763 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9764 genlmsg_attrlen(gnlh, 0), NULL);
9765
9766 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9767 if (!nl_vend)
9768 return NL_SKIP;
9769
9770 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9771 nla_data(nl_vend), nla_len(nl_vend), NULL);
9772
9773 attr = tb_vendor[
9774 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9775 if (!attr) {
9776 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9777 param->num = 0;
9778 return NL_SKIP;
9779 }
9780
9781 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9782 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9783 iface_type);
9784
9785 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9786 if (!attr) {
9787 wpa_printf(MSG_ERROR,
9788 "nl80211: preferred_freq_list couldn't be found");
9789 param->num = 0;
9790 return NL_SKIP;
9791 }
9792
9793 /*
9794 * param->num has the maximum number of entries for which there
9795 * is room in the freq_list provided by the caller.
9796 */
9797 freqs = nla_data(attr);
9798 max_num = nla_len(attr) / sizeof(u32);
9799 if (max_num > param->num)
9800 max_num = param->num;
9801 for (num = 0; num < max_num; num++)
9802 param->freq_list[num] = freqs[num];
9803 param->num = num;
9804
9805 return NL_SKIP;
9806}
9807
9808
9809static int nl80211_get_pref_freq_list(void *priv,
9810 enum wpa_driver_if_type if_type,
9811 unsigned int *num,
9812 unsigned int *freq_list)
9813{
9814 struct i802_bss *bss = priv;
9815 struct wpa_driver_nl80211_data *drv = bss->drv;
9816 struct nl_msg *msg;
9817 int ret;
9818 unsigned int i;
9819 struct nlattr *params;
9820 struct nl80211_pcl param;
9821 enum qca_iface_type iface_type;
9822
9823 if (!drv->get_pref_freq_list)
9824 return -1;
9825
9826 switch (if_type) {
9827 case WPA_IF_STATION:
9828 iface_type = QCA_IFACE_TYPE_STA;
9829 break;
9830 case WPA_IF_AP_BSS:
9831 iface_type = QCA_IFACE_TYPE_AP;
9832 break;
9833 case WPA_IF_P2P_GO:
9834 iface_type = QCA_IFACE_TYPE_P2P_GO;
9835 break;
9836 case WPA_IF_P2P_CLIENT:
9837 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9838 break;
9839 case WPA_IF_IBSS:
9840 iface_type = QCA_IFACE_TYPE_IBSS;
9841 break;
9842 case WPA_IF_TDLS:
9843 iface_type = QCA_IFACE_TYPE_TDLS;
9844 break;
9845 default:
9846 return -1;
9847 }
9848
9849 param.num = *num;
9850 param.freq_list = freq_list;
9851
9852 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9853 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9854 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9855 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9856 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9857 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9858 nla_put_u32(msg,
9859 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9860 iface_type)) {
9861 wpa_printf(MSG_ERROR,
9862 "%s: err in adding vendor_cmd and vendor_data",
9863 __func__);
9864 nlmsg_free(msg);
9865 return -1;
9866 }
9867 nla_nest_end(msg, params);
9868
9869 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9870 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9871 if (ret) {
9872 wpa_printf(MSG_ERROR,
9873 "%s: err in send_and_recv_msgs", __func__);
9874 return ret;
9875 }
9876
9877 *num = param.num;
9878
9879 for (i = 0; i < *num; i++) {
9880 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9881 i, freq_list[i]);
9882 }
9883
9884 return 0;
9885}
9886
9887
7c813acf
AK
9888static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9889{
9890 struct i802_bss *bss = priv;
9891 struct wpa_driver_nl80211_data *drv = bss->drv;
9892 struct nl_msg *msg;
9893 int ret;
9894 struct nlattr *params;
9895
9896 if (!drv->set_prob_oper_freq)
9897 return -1;
9898
9899 wpa_printf(MSG_DEBUG,
9900 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9901 freq, bss->ifindex);
9902
9903 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9904 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9905 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9906 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9907 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9908 nla_put_u32(msg,
9909 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9910 QCA_IFACE_TYPE_P2P_CLIENT) ||
9911 nla_put_u32(msg,
9912 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9913 freq)) {
9914 wpa_printf(MSG_ERROR,
9915 "%s: err in adding vendor_cmd and vendor_data",
9916 __func__);
9917 nlmsg_free(msg);
9918 return -1;
9919 }
9920 nla_nest_end(msg, params);
9921
9922 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9923 msg = NULL;
9924 if (ret) {
9925 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9926 __func__);
9927 return ret;
9928 }
9929 nlmsg_free(msg);
9930 return 0;
9931}
9932
a6f5b193
PX
9933
9934static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9935 unsigned int period, unsigned int interval,
9936 unsigned int count, const u8 *device_types,
9937 size_t dev_types_len,
9938 const u8 *ies, size_t ies_len)
9939{
9940 struct i802_bss *bss = priv;
9941 struct wpa_driver_nl80211_data *drv = bss->drv;
9942 struct nl_msg *msg;
9943 struct nlattr *container;
9944 int ret;
9945
9946 wpa_printf(MSG_DEBUG,
9947 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9948 freq, period, interval, count);
9949
9950 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9951 return -1;
9952
9953 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9954 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9955 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9956 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
9957 goto fail;
9958
9959 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9960 if (!container)
9961 goto fail;
9962
9963 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
9964 freq) ||
9965 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
9966 period) ||
9967 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
9968 interval) ||
9969 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
9970 count) ||
9971 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
9972 dev_types_len, device_types) ||
9973 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
9974 ies_len, ies))
9975 goto fail;
9976
9977 nla_nest_end(msg, container);
9978 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9979 msg = NULL;
9980 if (ret) {
9981 wpa_printf(MSG_DEBUG,
9982 "nl80211: Failed to send P2P Listen offload vendor command");
9983 goto fail;
9984 }
9985
9986 return 0;
9987
9988fail:
9989 nlmsg_free(msg);
9990 return -1;
9991}
9992
9993
9994static int nl80211_p2p_lo_stop(void *priv)
9995{
9996 struct i802_bss *bss = priv;
9997 struct wpa_driver_nl80211_data *drv = bss->drv;
9998 struct nl_msg *msg;
9999
10000 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10001
10002 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10003 return -1;
10004
10005 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10006 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10007 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10008 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10009 nlmsg_free(msg);
10010 return -1;
10011 }
10012
10013 return send_and_recv_msgs(drv, msg, NULL, NULL);
10014}
10015
2e4e4fb7
SD
10016
10017static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10018{
10019 struct i802_bss *bss = priv;
10020 struct wpa_driver_nl80211_data *drv = bss->drv;
10021 struct nl_msg *msg;
10022 struct nlattr *params;
10023 int ret;
10024 u32 tdls_mode;
10025
10026 wpa_printf(MSG_DEBUG,
10027 "nl80211: Set TDKS mode: tdls_external_control=%d",
10028 tdls_external_control);
10029
10030 if (tdls_external_control == 1)
10031 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10032 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10033 else
10034 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10035
10036 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10037 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10038 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10039 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10040 goto fail;
10041
10042 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10043 if (!params)
10044 goto fail;
10045
10046 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10047 tdls_mode))
10048 goto fail;
10049
10050 nla_nest_end(msg, params);
10051
10052 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10053 msg = NULL;
10054 if (ret) {
10055 wpa_printf(MSG_ERROR,
10056 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10057 ret, strerror(-ret));
10058 goto fail;
10059 }
10060 return 0;
10061fail:
10062 nlmsg_free(msg);
10063 return -1;
10064}
10065
3ab48492
KV
10066
10067#ifdef CONFIG_MBO
10068
10069static enum mbo_transition_reject_reason
10070nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10071{
10072 switch (status) {
10073 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10074 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10075 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10076 return MBO_TRANSITION_REJECT_REASON_DELAY;
10077 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10078 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10079 case QCA_STATUS_REJECT_LOW_RSSI:
10080 return MBO_TRANSITION_REJECT_REASON_RSSI;
10081 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10082 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10083 case QCA_STATUS_REJECT_UNKNOWN:
10084 default:
10085 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10086 }
10087}
10088
10089
10090static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10091 struct nlattr *tb[], int num)
10092{
10093 enum qca_wlan_btm_candidate_status status;
10094 char buf[50];
10095
10096 os_memcpy(candidate->bssid,
10097 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10098 ETH_ALEN);
10099
10100 status = nla_get_u32(
10101 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10102 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10103 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10104
10105 if (candidate->is_accept)
10106 os_snprintf(buf, sizeof(buf), "Accepted");
10107 else
10108 os_snprintf(buf, sizeof(buf),
10109 "Rejected, Reject_reason: %d",
10110 candidate->reject_reason);
10111 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10112 num, MAC2STR(candidate->bssid), buf);
10113}
10114
10115
10116static int
10117nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10118{
10119 struct wpa_bss_candidate_info *info = arg;
10120 struct candidate_list *candidate = info->candidates;
10121 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10122 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10123 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10124 static struct nla_policy policy[
10125 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10126 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10127 .minlen = ETH_ALEN
10128 },
10129 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10130 .type = NLA_U32,
10131 },
10132 };
10133 struct nlattr *attr;
10134 int rem;
10135 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10136 u8 num;
10137
10138 num = info->num; /* number of candidates sent to driver */
10139 info->num = 0;
10140 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10141 genlmsg_attrlen(gnlh, 0), NULL);
10142
10143 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10144 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10145 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10146 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10147 return NL_SKIP;
10148
10149 wpa_printf(MSG_DEBUG,
10150 "nl80211: WNM Candidate list received from driver");
10151 nla_for_each_nested(attr,
10152 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10153 rem) {
10154 if (info->num >= num ||
10155 nla_parse_nested(
10156 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10157 attr, policy) ||
10158 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10159 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10160 break;
10161
10162 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10163
10164 candidate++;
10165 info->num++;
10166 }
10167
10168 return NL_SKIP;
10169}
10170
10171
10172static struct wpa_bss_candidate_info *
10173nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10174{
10175 struct i802_bss *bss = priv;
10176 struct wpa_driver_nl80211_data *drv = bss->drv;
10177 struct nl_msg *msg;
10178 struct nlattr *attr, *attr1, *attr2;
10179 struct wpa_bss_candidate_info *info;
10180 u8 i;
10181 int ret;
10182 u8 *pos;
10183
10184 if (!drv->fetch_bss_trans_status)
10185 return NULL;
10186
10187 info = os_zalloc(sizeof(*info));
10188 if (!info)
10189 return NULL;
10190 /* Allocate memory for number of candidates sent to driver */
10191 info->candidates = os_calloc(params->n_candidates,
10192 sizeof(*info->candidates));
10193 if (!info->candidates) {
10194 os_free(info);
10195 return NULL;
10196 }
10197
10198 /* Copy the number of candidates being sent to driver. This is used in
10199 * nl80211_get_bss_transition_status_handler() to limit the number of
10200 * candidates that can be populated in info->candidates and will be
10201 * later overwritten with the actual number of candidates received from
10202 * the driver.
10203 */
10204 info->num = params->n_candidates;
10205
10206 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10207 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10208 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10209 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10210 goto fail;
10211
10212 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10213 if (!attr)
10214 goto fail;
10215
10216 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10217 params->mbo_transition_reason))
10218 goto fail;
10219
10220 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10221 if (!attr1)
10222 goto fail;
10223
10224 wpa_printf(MSG_DEBUG,
10225 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10226 params->mbo_transition_reason, params->n_candidates);
10227 pos = params->bssid;
10228 for (i = 0; i < params->n_candidates; i++) {
10229 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
10230 MAC2STR(pos));
10231 attr2 = nla_nest_start(msg, i);
10232 if (!attr2 ||
10233 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10234 ETH_ALEN, pos))
10235 goto fail;
10236 pos += ETH_ALEN;
10237 nla_nest_end(msg, attr2);
10238 }
10239
10240 nla_nest_end(msg, attr1);
10241 nla_nest_end(msg, attr);
10242
10243 ret = send_and_recv_msgs(drv, msg,
10244 nl80211_get_bss_transition_status_handler,
10245 info);
10246 msg = NULL;
10247 if (ret) {
10248 wpa_printf(MSG_ERROR,
10249 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10250 ret, strerror(-ret));
10251 goto fail;
10252 }
10253 return info;
10254
10255fail:
10256 nlmsg_free(msg);
10257 os_free(info->candidates);
10258 os_free(info);
10259 return NULL;
10260}
10261
178553b7
VK
10262
10263/**
10264 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10265 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10266 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10267 * Returns: 0 on success, -1 on failure
10268 */
10269static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10270{
10271 struct i802_bss *bss = priv;
10272 struct wpa_driver_nl80211_data *drv = bss->drv;
10273 struct nl_msg *msg;
10274 struct nlattr *attr;
10275 int ret = -1;
10276
10277 if (!drv->set_wifi_conf_vendor_cmd_avail)
10278 return -1;
10279
10280 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10281 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10282 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10283 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10284 goto fail;
10285
10286 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10287 if (!attr)
10288 goto fail;
10289
10290 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10291 ignore_disallow);
10292 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10293 ignore_disallow))
10294 goto fail;
10295
10296 nla_nest_end(msg, attr);
10297
10298 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10299 msg = NULL;
10300 if (ret) {
10301 wpa_printf(MSG_ERROR,
10302 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10303 ret, strerror(-ret));
10304 goto fail;
10305 }
10306
10307fail:
10308 nlmsg_free(msg);
10309 return ret;
10310}
10311
3ab48492
KV
10312#endif /* CONFIG_MBO */
10313
b658547d
JM
10314#endif /* CONFIG_DRIVER_NL80211_QCA */
10315
7c813acf 10316
6922d440
IP
10317static int nl80211_write_to_file(const char *name, unsigned int val)
10318{
10319 int fd, len;
10320 char tmp[128];
10321
10322 fd = open(name, O_RDWR);
10323 if (fd < 0) {
10324 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10325 name, strerror(errno));
10326 return fd;
10327 }
10328
10329 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10330 len = write(fd, tmp, len);
10331 if (len < 0)
10332 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10333 name, strerror(errno));
10334 close(fd);
10335
10336 return 0;
10337}
10338
10339
10340static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10341{
10342 struct i802_bss *bss = priv;
10343 char path[128];
10344 int ret;
10345
10346 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10347 filter_flags);
10348
10349 /* Configure filtering of unicast frame encrypted using GTK */
10350 ret = os_snprintf(path, sizeof(path),
10351 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10352 bss->ifname);
bd86ea08
JM
10353 if (os_snprintf_error(sizeof(path), ret))
10354 return -1;
6922d440
IP
10355
10356 ret = nl80211_write_to_file(path,
10357 !!(filter_flags &
10358 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10359 if (ret) {
10360 wpa_printf(MSG_ERROR,
10361 "nl80211: Failed to set IPv4 unicast in multicast filter");
10362 return ret;
10363 }
10364
10365 os_snprintf(path, sizeof(path),
10366 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10367 bss->ifname);
10368 ret = nl80211_write_to_file(path,
10369 !!(filter_flags &
10370 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10371
10372 if (ret) {
10373 wpa_printf(MSG_ERROR,
10374 "nl80211: Failed to set IPv6 unicast in multicast filter");
10375 return ret;
10376 }
10377
10378 /* Configure filtering of unicast frame encrypted using GTK */
10379 os_snprintf(path, sizeof(path),
10380 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10381 bss->ifname);
10382 ret = nl80211_write_to_file(path,
10383 !!(filter_flags &
10384 WPA_DATA_FRAME_FILTER_FLAG_ARP));
10385 if (ret) {
10386 wpa_printf(MSG_ERROR,
10387 "nl80211: Failed set gratuitous ARP filter");
10388 return ret;
10389 }
10390
10391 /* Configure filtering of IPv6 NA frames */
10392 os_snprintf(path, sizeof(path),
10393 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10394 bss->ifname);
10395 ret = nl80211_write_to_file(path,
10396 !!(filter_flags &
10397 WPA_DATA_FRAME_FILTER_FLAG_NA));
10398 if (ret) {
10399 wpa_printf(MSG_ERROR,
10400 "nl80211: Failed to set unsolicited NA filter");
10401 return ret;
10402 }
10403
10404 return 0;
10405}
10406
10407
cc9a2575
KV
10408static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10409 const u8 **ext_capa, const u8 **ext_capa_mask,
10410 unsigned int *ext_capa_len)
10411{
10412 struct i802_bss *bss = priv;
10413 struct wpa_driver_nl80211_data *drv = bss->drv;
10414 enum nl80211_iftype nlmode;
10415 unsigned int i;
10416
10417 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10418 return -1;
10419
10420 nlmode = wpa_driver_nl80211_if_type(type);
10421
10422 /* By default, use the per-radio values */
10423 *ext_capa = drv->extended_capa;
10424 *ext_capa_mask = drv->extended_capa_mask;
10425 *ext_capa_len = drv->extended_capa_len;
10426
10427 /* Replace the default value if a per-interface type value exists */
10428 for (i = 0; i < drv->num_iface_ext_capa; i++) {
10429 if (nlmode == drv->iface_ext_capa[i].iftype) {
10430 *ext_capa = drv->iface_ext_capa[i].ext_capa;
10431 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10432 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10433 break;
10434 }
10435 }
10436
10437 return 0;
10438}
10439
10440
3c67e977
VK
10441static int nl80211_update_connection_params(
10442 void *priv, struct wpa_driver_associate_params *params,
10443 enum wpa_drv_update_connect_params_mask mask)
10444{
10445 struct i802_bss *bss = priv;
10446 struct wpa_driver_nl80211_data *drv = bss->drv;
10447 struct nl_msg *msg;
10448 int ret = -1;
10449 enum nl80211_auth_type type;
10450
10451 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10452 if (!msg)
10453 goto fail;
10454
10455 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10456 drv->ifindex);
10457
10458 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10459 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10460 params->wpa_ie))
10461 goto fail;
10462 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
10463 params->wpa_ie_len);
10464 }
10465
10466 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10467 type = get_nl_auth_type(params->auth_alg);
10468 if (type == NL80211_AUTHTYPE_MAX ||
10469 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10470 goto fail;
10471 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
10472 }
10473
10474 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10475 nl80211_put_fils_connect_params(drv, params, msg))
10476 goto fail;
10477
10478 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10479 msg = NULL;
10480 if (ret)
10481 wpa_dbg(drv->ctx, MSG_DEBUG,
10482 "nl80211: Update connect params command failed: ret=%d (%s)",
10483 ret, strerror(-ret));
10484
10485fail:
10486 nlmsg_free(msg);
10487 return ret;
10488}
10489
10490
ba71cb82
SD
10491static int nl80211_send_external_auth_status(void *priv,
10492 struct external_auth *params)
10493{
10494 struct i802_bss *bss = priv;
10495 struct wpa_driver_nl80211_data *drv = bss->drv;
10496 struct nl_msg *msg = NULL;
10497 int ret = -1;
10498
10499 wpa_dbg(drv->ctx, MSG_DEBUG,
10500 "nl80211: External auth status: %u", params->status);
10501
10502 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10503 if (!msg ||
10504 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10505 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10506 params->ssid) ||
10507 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10508 goto fail;
10509 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10510 msg = NULL;
10511 if (ret) {
10512 wpa_printf(MSG_DEBUG,
10513 "nl80211: External Auth status update failed: ret=%d (%s)",
10514 ret, strerror(-ret));
10515 goto fail;
10516 }
10517fail:
10518 nlmsg_free(msg);
10519 return ret;
10520}
10521
10522
3f5285e8
JM
10523const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10524 .name = "nl80211",
10525 .desc = "Linux nl80211/cfg80211",
10526 .get_bssid = wpa_driver_nl80211_get_bssid,
10527 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
10528 .set_key = driver_nl80211_set_key,
10529 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
10530 .sched_scan = wpa_driver_nl80211_sched_scan,
10531 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 10532 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
4f30addb 10533 .abort_scan = wpa_driver_nl80211_abort_scan,
9ebce9c5
JM
10534 .deauthenticate = driver_nl80211_deauthenticate,
10535 .authenticate = driver_nl80211_authenticate,
3f5285e8 10536 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
10537 .global_init = nl80211_global_init,
10538 .global_deinit = nl80211_global_deinit,
10539 .init2 = wpa_driver_nl80211_init,
9ebce9c5 10540 .deinit = driver_nl80211_deinit,
3f5285e8
JM
10541 .get_capa = wpa_driver_nl80211_get_capa,
10542 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 10543 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 10544 .set_country = wpa_driver_nl80211_set_country,
f0793bf1 10545 .get_country = wpa_driver_nl80211_get_country,
19c3b566 10546 .set_ap = wpa_driver_nl80211_set_ap,
3c4ca363 10547 .set_acl = wpa_driver_nl80211_set_acl,
22a7c9d7 10548 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
10549 .if_remove = driver_nl80211_if_remove,
10550 .send_mlme = driver_nl80211_send_mlme,
0fafeb54 10551 .get_hw_feature_data = nl80211_get_hw_feature_data,
0f4e8b4f 10552 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 10553 .sta_remove = driver_nl80211_sta_remove,
db149ac9 10554 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 10555 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837 10556 .hapd_init = i802_init,
c5121837 10557 .hapd_deinit = i802_deinit,
f7b3920c 10558 .set_wds_sta = i802_set_wds_sta,
c5121837
JM
10559 .get_seqnum = i802_get_seqnum,
10560 .flush = i802_flush,
c5121837
JM
10561 .get_inact_sec = i802_get_inact_sec,
10562 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
10563 .set_rts = i802_set_rts,
10564 .set_frag = i802_set_frag,
c5121837 10565 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 10566 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
10567 .sta_deauth = i802_sta_deauth,
10568 .sta_disassoc = i802_sta_disassoc,
9ebce9c5 10569 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 10570 .set_freq = i802_set_freq,
9ebce9c5 10571 .send_action = driver_nl80211_send_action,
5dfca53f 10572 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
10573 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10574 .cancel_remain_on_channel =
10575 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 10576 .probe_req_report = driver_nl80211_probe_req_report,
af473088 10577 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 10578 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 10579 .resume = wpa_driver_nl80211_resume,
b625473c 10580 .signal_monitor = nl80211_signal_monitor,
1c5c7273 10581 .signal_poll = nl80211_signal_poll,
b91ab76e 10582 .send_frame = nl80211_send_frame,
c55f774d 10583 .set_param = nl80211_set_param,
6859f1cb 10584 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
10585 .add_pmkid = nl80211_add_pmkid,
10586 .remove_pmkid = nl80211_remove_pmkid,
10587 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 10588 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 10589 .poll_client = nl80211_poll_client,
29f338af 10590 .set_p2p_powersave = nl80211_set_p2p_powersave,
f90e9c1c 10591 .start_dfs_cac = nl80211_start_radar_detection,
695c7038 10592 .stop_ap = wpa_driver_nl80211_stop_ap,
03ea1786
AN
10593#ifdef CONFIG_TDLS
10594 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10595 .tdls_oper = nl80211_tdls_oper,
72b2605f
AN
10596 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10597 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
03ea1786 10598#endif /* CONFIG_TDLS */
6a1ce395 10599 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
597b94f5 10600 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
0185007c 10601 .get_survey = wpa_driver_nl80211_get_survey,
a771c07d 10602 .status = wpa_driver_nl80211_status,
1c4ffa87 10603 .switch_channel = nl80211_switch_channel,
0de38036
JM
10604#ifdef ANDROID_P2P
10605 .set_noa = wpa_driver_set_p2p_noa,
10606 .get_noa = wpa_driver_get_p2p_noa,
10607 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
10608#endif /* ANDROID_P2P */
5e2c3490 10609#ifdef ANDROID
ded14ce9 10610#ifndef ANDROID_LIB_STUB
5e2c3490 10611 .driver_cmd = wpa_driver_nl80211_driver_cmd,
ded14ce9 10612#endif /* !ANDROID_LIB_STUB */
5e2c3490 10613#endif /* ANDROID */
adef8948 10614 .vendor_cmd = nl80211_vendor_cmd,
049105b4 10615 .set_qos_map = nl80211_set_qos_map,
e4fa8b12 10616 .set_wowlan = nl80211_set_wowlan,
fee354c7 10617 .set_mac_addr = nl80211_set_mac_addr,
6c1664f6
BC
10618#ifdef CONFIG_MESH
10619 .init_mesh = wpa_driver_nl80211_init_mesh,
10620 .join_mesh = wpa_driver_nl80211_join_mesh,
10621 .leave_mesh = wpa_driver_nl80211_leave_mesh,
10622#endif /* CONFIG_MESH */
71103bed
KP
10623 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10624 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
73d2294f 10625 .br_port_set_attr = wpa_driver_br_port_set_attr,
7565752d 10626 .br_set_net_param = wpa_driver_br_set_net_param,
dfa87878
MB
10627 .add_tx_ts = nl80211_add_ts,
10628 .del_tx_ts = nl80211_del_ts,
45e3fc72 10629 .get_ifindex = nl80211_get_ifindex,
b658547d 10630#ifdef CONFIG_DRIVER_NL80211_QCA
9607a1ae 10631 .roaming = nl80211_roaming,
d98038bb 10632 .disable_fils = nl80211_disable_fils,
16689c7c 10633 .do_acs = wpa_driver_do_acs,
844dfeb8 10634 .set_band = nl80211_set_band,
98342208 10635 .get_pref_freq_list = nl80211_get_pref_freq_list,
7c813acf 10636 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
a6f5b193
PX
10637 .p2p_lo_start = nl80211_p2p_lo_start,
10638 .p2p_lo_stop = nl80211_p2p_lo_stop,
cc9985d1 10639 .set_default_scan_ies = nl80211_set_default_scan_ies,
2e4e4fb7 10640 .set_tdls_mode = nl80211_set_tdls_mode,
3ab48492
KV
10641#ifdef CONFIG_MBO
10642 .get_bss_transition_status = nl80211_get_bss_transition_status,
178553b7 10643 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
3ab48492 10644#endif /* CONFIG_MBO */
b04854ce 10645 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
b658547d 10646#endif /* CONFIG_DRIVER_NL80211_QCA */
6922d440 10647 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
cc9a2575 10648 .get_ext_capab = nl80211_get_ext_capab,
3c67e977 10649 .update_connect_params = nl80211_update_connection_params,
ba71cb82 10650 .send_external_auth_status = nl80211_send_external_auth_status,
3f5285e8 10651};