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