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