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