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