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