]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
nl80211: Debug print details from the beacon hint events
[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;
5b82cdbe 4040 int ret = -ENOBUFS;
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
5b82cdbe
JB
4246 if (params->ftm_responder) {
4247 struct nlattr *ftm;
4248
4249 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_FTM_RESPONDER)) {
4250 ret = -ENOTSUP;
4251 goto fail;
4252 }
4253
4254 ftm = nla_nest_start(msg, NL80211_ATTR_FTM_RESPONDER);
4255 if (!ftm ||
4256 nla_put_flag(msg, NL80211_FTM_RESP_ATTR_ENABLED) ||
4257 (params->lci &&
4258 nla_put(msg, NL80211_FTM_RESP_ATTR_LCI,
4259 wpabuf_len(params->lci),
4260 wpabuf_head(params->lci))) ||
4261 (params->civic &&
4262 nla_put(msg, NL80211_FTM_RESP_ATTR_CIVICLOC,
4263 wpabuf_len(params->civic),
4264 wpabuf_head(params->civic))))
4265 goto fail;
4266 nla_nest_end(msg, ftm);
4267 }
4268
d2440ba0
JM
4269 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4270 if (ret) {
4271 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4272 ret, strerror(-ret));
b4fd6fab 4273 } else {
b4fd6fab 4274 bss->beacon_set = 1;
31357268 4275 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 4276 params->short_slot_time, params->ht_opmode,
e5693c47 4277 params->isolate, params->basic_rates);
34f7c699
MB
4278 nl80211_set_multicast_to_unicast(bss,
4279 params->multicast_to_unicast);
e87ef751
PX
4280 if (beacon_set && params->freq &&
4281 params->freq->bandwidth != bss->bandwidth) {
4282 wpa_printf(MSG_DEBUG,
4283 "nl80211: Update BSS %s bandwidth: %d -> %d",
4284 bss->ifname, bss->bandwidth,
4285 params->freq->bandwidth);
4286 ret = nl80211_set_channel(bss, params->freq, 1);
4287 if (ret) {
4288 wpa_printf(MSG_DEBUG,
4289 "nl80211: Frequency set failed: %d (%s)",
4290 ret, strerror(-ret));
4291 } else {
4292 wpa_printf(MSG_DEBUG,
4293 "nl80211: Frequency set succeeded for ht2040 coex");
4294 bss->bandwidth = params->freq->bandwidth;
4295 }
f5728d0a 4296 } else if (!beacon_set && params->freq) {
e87ef751
PX
4297 /*
4298 * cfg80211 updates the driver on frequence change in AP
4299 * mode only at the point when beaconing is started, so
4300 * set the initial value here.
4301 */
4302 bss->bandwidth = params->freq->bandwidth;
4303 }
b4fd6fab 4304 }
052b8d38
MH
4305
4306#ifdef CONFIG_MESH
4307 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4308 os_memset(&mesh_params, 0, sizeof(mesh_params));
4309 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4310 mesh_params.ht_opmode = params->ht_opmode;
4311 ret = nl80211_set_mesh_config(priv, &mesh_params);
4312 if (ret < 0)
4313 return ret;
4314 }
4315#endif /* CONFIG_MESH */
4316
d2440ba0 4317 return ret;
a862e4a3 4318fail:
5883168a 4319 nlmsg_free(msg);
5b82cdbe 4320 return ret;
d2440ba0
JM
4321}
4322
4323
1c4ffa87 4324static int nl80211_put_freq_params(struct nl_msg *msg,
72b2605f 4325 const struct hostapd_freq_params *freq)
1581b38b 4326{
1fc4ab23 4327 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
a862e4a3
JM
4328 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4329 return -ENOBUFS;
4330
1fc4ab23
JM
4331 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4332 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4333
89b800d7 4334 if (freq->vht_enabled) {
a862e4a3
JM
4335 enum nl80211_chan_width cw;
4336
1fc4ab23 4337 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
89b800d7
JB
4338 switch (freq->bandwidth) {
4339 case 20:
a862e4a3 4340 cw = NL80211_CHAN_WIDTH_20;
89b800d7
JB
4341 break;
4342 case 40:
a862e4a3 4343 cw = NL80211_CHAN_WIDTH_40;
89b800d7
JB
4344 break;
4345 case 80:
4346 if (freq->center_freq2)
a862e4a3 4347 cw = NL80211_CHAN_WIDTH_80P80;
89b800d7 4348 else
a862e4a3 4349 cw = NL80211_CHAN_WIDTH_80;
89b800d7
JB
4350 break;
4351 case 160:
a862e4a3 4352 cw = NL80211_CHAN_WIDTH_160;
89b800d7
JB
4353 break;
4354 default:
1c4ffa87 4355 return -EINVAL;
89b800d7 4356 }
a862e4a3 4357
1fc4ab23
JM
4358 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4359 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4360 freq->center_freq1);
4361 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4362 freq->center_freq2);
a862e4a3
JM
4363 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4364 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4365 freq->center_freq1) ||
4366 (freq->center_freq2 &&
4367 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4368 freq->center_freq2)))
4369 return -ENOBUFS;
89b800d7 4370 } else if (freq->ht_enabled) {
a862e4a3
JM
4371 enum nl80211_channel_type ct;
4372
1fc4ab23
JM
4373 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4374 freq->sec_channel_offset);
89b800d7 4375 switch (freq->sec_channel_offset) {
f019981a 4376 case -1:
a862e4a3 4377 ct = NL80211_CHAN_HT40MINUS;
f019981a
JM
4378 break;
4379 case 1:
a862e4a3 4380 ct = NL80211_CHAN_HT40PLUS;
f019981a
JM
4381 break;
4382 default:
a862e4a3 4383 ct = NL80211_CHAN_HT20;
f019981a
JM
4384 break;
4385 }
a862e4a3 4386
1fc4ab23 4387 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
a862e4a3
JM
4388 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4389 return -ENOBUFS;
3388e7b9
MH
4390 } else {
4391 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4392 NL80211_CHAN_NO_HT);
4393 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4394 NL80211_CHAN_NO_HT))
4395 return -ENOBUFS;
f019981a 4396 }
1c4ffa87 4397 return 0;
1c4ffa87
AO
4398}
4399
4400
e87ef751
PX
4401static int nl80211_set_channel(struct i802_bss *bss,
4402 struct hostapd_freq_params *freq, int set_chan)
1c4ffa87
AO
4403{
4404 struct wpa_driver_nl80211_data *drv = bss->drv;
4405 struct nl_msg *msg;
4406 int ret;
4407
4408 wpa_printf(MSG_DEBUG,
4409 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4410 freq->freq, freq->ht_enabled, freq->vht_enabled,
4411 freq->bandwidth, freq->center_freq1, freq->center_freq2);
1c4ffa87 4412
9725b784
JM
4413 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4414 NL80211_CMD_SET_WIPHY);
4415 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
a862e4a3
JM
4416 nlmsg_free(msg);
4417 return -1;
4418 }
1581b38b 4419
d2440ba0 4420 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
e4fb2167 4421 if (ret == 0) {
89b800d7 4422 bss->freq = freq->freq;
1581b38b 4423 return 0;
e4fb2167 4424 }
f019981a 4425 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 4426 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b
JM
4427 return -1;
4428}
4429
0f4e8b4f 4430
95ab6063
AN
4431static u32 sta_flags_nl80211(int flags)
4432{
4433 u32 f = 0;
4434
4435 if (flags & WPA_STA_AUTHORIZED)
4436 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4437 if (flags & WPA_STA_WMM)
4438 f |= BIT(NL80211_STA_FLAG_WME);
4439 if (flags & WPA_STA_SHORT_PREAMBLE)
4440 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4441 if (flags & WPA_STA_MFP)
4442 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
4443 if (flags & WPA_STA_TDLS_PEER)
4444 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7a228b5c
BC
4445 if (flags & WPA_STA_AUTHENTICATED)
4446 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
dc55b6b6
AB
4447 if (flags & WPA_STA_ASSOCIATED)
4448 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
95ab6063
AN
4449
4450 return f;
4451}
4452
4453
7c7e7877
BC
4454#ifdef CONFIG_MESH
4455static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4456{
4457 switch (state) {
d02e5498 4458 case PLINK_IDLE:
7c7e7877 4459 return NL80211_PLINK_LISTEN;
d02e5498 4460 case PLINK_OPN_SNT:
7c7e7877 4461 return NL80211_PLINK_OPN_SNT;
d02e5498 4462 case PLINK_OPN_RCVD:
7c7e7877
BC
4463 return NL80211_PLINK_OPN_RCVD;
4464 case PLINK_CNF_RCVD:
4465 return NL80211_PLINK_CNF_RCVD;
4466 case PLINK_ESTAB:
4467 return NL80211_PLINK_ESTAB;
4468 case PLINK_HOLDING:
4469 return NL80211_PLINK_HOLDING;
4470 case PLINK_BLOCKED:
4471 return NL80211_PLINK_BLOCKED;
4472 default:
4473 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4474 state);
4475 }
4476 return -1;
4477}
4478#endif /* CONFIG_MESH */
4479
4480
62847751 4481static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
4482 struct hostapd_sta_add_params *params)
4483{
a2e40bb6
FF
4484 struct i802_bss *bss = priv;
4485 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8 4486 struct nl_msg *msg;
95ab6063 4487 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
4488 int ret = -ENOBUFS;
4489
45b722f1
AN
4490 if ((params->flags & WPA_STA_TDLS_PEER) &&
4491 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4492 return -EOPNOTSUPP;
4493
e4dea253
JM
4494 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4495 params->set ? "Set" : "Add", MAC2STR(params->addr));
13f83980
JM
4496 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4497 NL80211_CMD_NEW_STATION);
4498 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
a862e4a3 4499 goto fail;
5551bc9c 4500
dc55b6b6
AB
4501 /*
4502 * Set the below properties only in one of the following cases:
4503 * 1. New station is added, already associated.
4504 * 2. Set WPA_STA_TDLS_PEER station.
4505 * 3. Set an already added unassociated station, if driver supports
4506 * full AP client state. (Set these properties after station became
4507 * associated will be rejected by the driver).
4508 */
4509 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4510 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4511 (params->flags & WPA_STA_ASSOCIATED))) {
5551bc9c
BC
4512 wpa_hexdump(MSG_DEBUG, " * supported rates",
4513 params->supp_rates, params->supp_rates_len);
a862e4a3
JM
4514 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4515 params->capability);
4516 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4517 params->supp_rates_len, params->supp_rates) ||
4518 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4519 params->capability))
4520 goto fail;
e503861f
JM
4521
4522 if (params->ht_capabilities) {
4523 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4524 (u8 *) params->ht_capabilities,
4525 sizeof(*params->ht_capabilities));
a862e4a3
JM
4526 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4527 sizeof(*params->ht_capabilities),
4528 params->ht_capabilities))
4529 goto fail;
e503861f
JM
4530 }
4531
4532 if (params->vht_capabilities) {
4533 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4534 (u8 *) params->vht_capabilities,
4535 sizeof(*params->vht_capabilities));
a862e4a3
JM
4536 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4537 sizeof(*params->vht_capabilities),
4538 params->vht_capabilities))
4539 goto fail;
e503861f
JM
4540 }
4541
e503861f
JM
4542 if (params->ext_capab) {
4543 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4544 params->ext_capab, params->ext_capab_len);
a862e4a3
JM
4545 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4546 params->ext_capab_len, params->ext_capab))
4547 goto fail;
e503861f 4548 }
ae33239c
AB
4549
4550 if (is_ap_interface(drv->nlmode) &&
4551 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4552 params->support_p2p_ps ?
4553 NL80211_P2P_PS_SUPPORTED :
4554 NL80211_P2P_PS_UNSUPPORTED))
4555 goto fail;
5551bc9c 4556 }
45b722f1 4557 if (!params->set) {
f11b72c3
JM
4558 if (params->aid) {
4559 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
a862e4a3
JM
4560 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4561 goto fail;
f11b72c3
JM
4562 } else {
4563 /*
4564 * cfg80211 validates that AID is non-zero, so we have
4565 * to make this a non-zero value for the TDLS case where
dc55b6b6
AB
4566 * a dummy STA entry is used for now and for a station
4567 * that is still not associated.
f11b72c3 4568 */
dc55b6b6
AB
4569 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4570 (params->flags & WPA_STA_TDLS_PEER) ?
4571 "TDLS" : "UNASSOC_STA");
a862e4a3
JM
4572 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4573 goto fail;
f11b72c3 4574 }
e4dea253
JM
4575 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4576 params->listen_interval);
a862e4a3
JM
4577 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4578 params->listen_interval))
4579 goto fail;
e112764e
JM
4580 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4581 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
a862e4a3
JM
4582 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4583 goto fail;
dc55b6b6
AB
4584 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4585 (params->flags & WPA_STA_ASSOCIATED)) {
4586 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4587 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4588 params->listen_interval);
4589 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4590 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4591 params->listen_interval))
4592 goto fail;
45b722f1 4593 }
05a8d422 4594
8a458116
MK
4595 if (params->vht_opmode_enabled) {
4596 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
a862e4a3
JM
4597 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4598 params->vht_opmode))
4599 goto fail;
8a458116
MK
4600 }
4601
efc64886
SD
4602 if (params->supp_channels) {
4603 wpa_hexdump(MSG_DEBUG, " * supported channels",
4604 params->supp_channels, params->supp_channels_len);
a862e4a3
JM
4605 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4606 params->supp_channels_len, params->supp_channels))
4607 goto fail;
efc64886
SD
4608 }
4609
4610 if (params->supp_oper_classes) {
4611 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4612 params->supp_oper_classes,
4613 params->supp_oper_classes_len);
a862e4a3
JM
4614 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4615 params->supp_oper_classes_len,
4616 params->supp_oper_classes))
4617 goto fail;
efc64886
SD
4618 }
4619
95ab6063 4620 os_memset(&upd, 0, sizeof(upd));
7e31703e
BC
4621 upd.set = sta_flags_nl80211(params->flags);
4622 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
dc55b6b6
AB
4623
4624 /*
4625 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4626 * flags, as nl80211 driver moves a new station, by default, into
4627 * associated state.
4628 *
4629 * On the other hand, if the driver supports that feature and the
4630 * station is added in unauthenticated state, set the
4631 * authenticated/associated bits in the mask to prevent moving this
4632 * station to associated state before it is actually associated.
4633 *
4634 * This is irrelevant for mesh mode where the station is added to the
4635 * driver as authenticated already, and ASSOCIATED isn't part of the
4636 * nl80211 API.
4637 */
4638 if (!is_mesh_interface(drv->nlmode)) {
4639 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4640 wpa_printf(MSG_DEBUG,
4641 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4642 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4643 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4644 } else if (!params->set &&
4645 !(params->flags & WPA_STA_TDLS_PEER)) {
4646 if (!(params->flags & WPA_STA_AUTHENTICATED))
4647 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4648 if (!(params->flags & WPA_STA_ASSOCIATED))
4649 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4650 }
e347cafe
MH
4651#ifdef CONFIG_MESH
4652 } else {
4653 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4654 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4655 params->peer_aid);
4656 if (ret)
4657 goto fail;
4658 }
4659#endif /* CONFIG_MESH */
dc55b6b6
AB
4660 }
4661
e4dea253
JM
4662 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4663 upd.set, upd.mask);
a862e4a3
JM
4664 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4665 goto fail;
95ab6063 4666
7c7e7877 4667#ifdef CONFIG_MESH
a862e4a3
JM
4668 if (params->plink_state &&
4669 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4670 sta_plink_state_nl80211(params->plink_state)))
4671 goto fail;
7c7e7877
BC
4672#endif /* CONFIG_MESH */
4673
774bfa62 4674 if (params->flags & WPA_STA_WMM) {
8970bae8
JB
4675 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4676
e4dea253 4677 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
a862e4a3
JM
4678 if (!wme ||
4679 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4680 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4681 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4682 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4683 WMM_QOSINFO_STA_SP_MASK))
4684 goto fail;
8970bae8 4685 nla_nest_end(msg, wme);
774bfa62
EP
4686 }
4687
0f4e8b4f 4688 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 4689 msg = NULL;
0f4e8b4f 4690 if (ret)
45b722f1
AN
4691 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4692 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4693 strerror(-ret));
0f4e8b4f
JM
4694 if (ret == -EEXIST)
4695 ret = 0;
a862e4a3 4696fail:
5883168a 4697 nlmsg_free(msg);
0f4e8b4f
JM
4698 return ret;
4699}
4700
4701
97ed9a06
KP
4702static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4703{
4704#ifdef CONFIG_LIBNL3_ROUTE
4705 struct wpa_driver_nl80211_data *drv = bss->drv;
4706 struct rtnl_neigh *rn;
4707 struct nl_addr *nl_addr;
4708 int err;
4709
4710 rn = rtnl_neigh_alloc();
4711 if (!rn)
4712 return;
4713
4714 rtnl_neigh_set_family(rn, AF_BRIDGE);
4715 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4716 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4717 if (!nl_addr) {
4718 rtnl_neigh_put(rn);
4719 return;
4720 }
4721 rtnl_neigh_set_lladdr(rn, nl_addr);
4722
4723 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4724 if (err < 0) {
4725 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4726 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4727 bss->ifindex, nl_geterror(err));
4728 } else {
4729 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4730 MACSTR, MAC2STR(addr));
4731 }
4732
4733 nl_addr_put(nl_addr);
4734 rtnl_neigh_put(rn);
4735#endif /* CONFIG_LIBNL3_ROUTE */
4736}
4737
4738
59d7148a
JM
4739static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4740 int deauth, u16 reason_code)
0f4e8b4f 4741{
a2e40bb6 4742 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
4743 struct nl_msg *msg;
4744 int ret;
4745
13f83980 4746 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
a862e4a3
JM
4747 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4748 (deauth == 0 &&
4749 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4750 WLAN_FC_STYPE_DISASSOC)) ||
4751 (deauth == 1 &&
4752 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4753 WLAN_FC_STYPE_DEAUTH)) ||
4754 (reason_code &&
4755 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4756 nlmsg_free(msg);
4757 return -ENOBUFS;
4758 }
0f4e8b4f
JM
4759
4760 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83e7bb0e
JM
4761 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4762 " --> %d (%s)",
4763 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
97ed9a06
KP
4764
4765 if (drv->rtnl_sk)
4766 rtnl_neigh_delete_fdb_entry(bss, addr);
4767
0f4e8b4f
JM
4768 if (ret == -ENOENT)
4769 return 0;
4770 return ret;
0f4e8b4f
JM
4771}
4772
1581b38b 4773
f3407c66 4774void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
0915d02c
JM
4775{
4776 struct nl_msg *msg;
de884303 4777 struct wpa_driver_nl80211_data *drv2;
0915d02c 4778
c6e8e8e4
JM
4779 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4780
2135f224 4781 /* stop listening for EAPOL on this interface */
de884303
JM
4782 dl_list_for_each(drv2, &drv->global->interfaces,
4783 struct wpa_driver_nl80211_data, list)
732b1d20
MB
4784 {
4785 del_ifidx(drv2, ifidx, IFIDX_ANY);
4786 /* Remove all bridges learned for this iface */
4787 del_ifidx(drv2, IFIDX_ANY, ifidx);
4788 }
2135f224 4789
95376e1a 4790 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
4791 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4792 return;
e748062b 4793 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
4794}
4795
4796
cc9a2575 4797const char * nl80211_iftype_str(enum nl80211_iftype mode)
a1922f93
JM
4798{
4799 switch (mode) {
4800 case NL80211_IFTYPE_ADHOC:
4801 return "ADHOC";
4802 case NL80211_IFTYPE_STATION:
4803 return "STATION";
4804 case NL80211_IFTYPE_AP:
4805 return "AP";
1045ec36
JM
4806 case NL80211_IFTYPE_AP_VLAN:
4807 return "AP_VLAN";
4808 case NL80211_IFTYPE_WDS:
4809 return "WDS";
a1922f93
JM
4810 case NL80211_IFTYPE_MONITOR:
4811 return "MONITOR";
1045ec36
JM
4812 case NL80211_IFTYPE_MESH_POINT:
4813 return "MESH_POINT";
a1922f93
JM
4814 case NL80211_IFTYPE_P2P_CLIENT:
4815 return "P2P_CLIENT";
4816 case NL80211_IFTYPE_P2P_GO:
4817 return "P2P_GO";
7aad838c
NS
4818 case NL80211_IFTYPE_P2P_DEVICE:
4819 return "P2P_DEVICE";
a1922f93
JM
4820 default:
4821 return "unknown";
4822 }
4823}
4824
4825
a35187e7
KH
4826static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4827 const char *ifname,
4828 enum nl80211_iftype iftype,
d6dcfcda
DS
4829 const u8 *addr, int wds,
4830 int (*handler)(struct nl_msg *, void *),
4831 void *arg)
0915d02c 4832{
8970bae8 4833 struct nl_msg *msg;
0915d02c
JM
4834 int ifidx;
4835 int ret = -ENOBUFS;
4836
a1922f93
JM
4837 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4838 iftype, nl80211_iftype_str(iftype));
4839
56f77852
JM
4840 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4841 if (!msg ||
a862e4a3
JM
4842 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4843 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4844 goto fail;
0915d02c
JM
4845
4846 if (iftype == NL80211_IFTYPE_MONITOR) {
8970bae8 4847 struct nlattr *flags;
0915d02c 4848
8970bae8 4849 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
a862e4a3
JM
4850 if (!flags ||
4851 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4852 goto fail;
0915d02c 4853
8970bae8 4854 nla_nest_end(msg, flags);
fbbfcbac 4855 } else if (wds) {
a862e4a3
JM
4856 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4857 goto fail;
0915d02c
JM
4858 }
4859
52f5877a
IP
4860 /*
4861 * Tell cfg80211 that the interface belongs to the socket that created
4862 * it, and the interface should be deleted when the socket is closed.
4863 */
a862e4a3
JM
4864 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4865 goto fail;
52f5877a 4866
d6dcfcda 4867 ret = send_and_recv_msgs(drv, msg, handler, arg);
5883168a 4868 msg = NULL;
0915d02c 4869 if (ret) {
a862e4a3 4870 fail:
5883168a 4871 nlmsg_free(msg);
a35187e7
KH
4872 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4873 ifname, ret, strerror(-ret));
0915d02c
JM
4874 return ret;
4875 }
4876
f632e483 4877 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6bae92e0 4878 return 0;
6bae92e0 4879
0915d02c 4880 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
4881 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4882 ifname, ifidx);
0915d02c
JM
4883
4884 if (ifidx <= 0)
4885 return -1;
4886
147848ec
JM
4887 /*
4888 * Some virtual interfaces need to process EAPOL packets and events on
4889 * the parent interface. This is used mainly with hostapd.
4890 */
4891 if (drv->hostapd ||
4892 iftype == NL80211_IFTYPE_AP_VLAN ||
4893 iftype == NL80211_IFTYPE_WDS ||
4894 iftype == NL80211_IFTYPE_MONITOR) {
4895 /* start listening for EAPOL on this interface */
732b1d20 4896 add_ifidx(drv, ifidx, IFIDX_ANY);
147848ec 4897 }
2135f224 4898
7bfc47c3 4899 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 4900 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
4901 nl80211_remove_iface(drv, ifidx);
4902 return -1;
2135f224 4903 }
2135f224 4904
0915d02c
JM
4905 return ifidx;
4906}
22a7c9d7
JM
4907
4908
f3407c66
JM
4909int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4910 const char *ifname, enum nl80211_iftype iftype,
4911 const u8 *addr, int wds,
4912 int (*handler)(struct nl_msg *, void *),
4913 void *arg, int use_existing)
a35187e7
KH
4914{
4915 int ret;
4916
d6dcfcda
DS
4917 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4918 arg);
a35187e7 4919
ffbf1eaa 4920 /* if error occurred and interface exists already */
a35187e7 4921 if (ret == -ENFILE && if_nametoindex(ifname)) {
2aec4f3c
JM
4922 if (use_existing) {
4923 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4924 ifname);
6997f8ba
JM
4925 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4926 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4927 addr) < 0 &&
4928 (linux_set_iface_flags(drv->global->ioctl_sock,
4929 ifname, 0) < 0 ||
4930 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4931 addr) < 0 ||
4932 linux_set_iface_flags(drv->global->ioctl_sock,
4933 ifname, 1) < 0))
4934 return -1;
2aec4f3c
JM
4935 return -ENFILE;
4936 }
a35187e7
KH
4937 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4938
4939 /* Try to remove the interface that was already there. */
4940 nl80211_remove_iface(drv, if_nametoindex(ifname));
4941
4942 /* Try to create the interface again */
fbbfcbac 4943 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
d6dcfcda 4944 wds, handler, arg);
a35187e7
KH
4945 }
4946
3e208481
JM
4947 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4948 wpa_printf(MSG_DEBUG,
4949 "nl80211: Interface %s created for P2P - disable 11b rates",
4950 ifname);
4e5cb1a3 4951 nl80211_disable_11b_rates(drv, ret, 1);
3e208481 4952 }
4e5cb1a3 4953
a35187e7
KH
4954 return ret;
4955}
0915d02c 4956
2135f224 4957
3fd1cefb
JB
4958static int nl80211_setup_ap(struct i802_bss *bss)
4959{
4960 struct wpa_driver_nl80211_data *drv = bss->drv;
4961
748c0ac0
JM
4962 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4963 bss->ifname, drv->device_ap_sme, drv->use_monitor);
36488c05 4964
a11241fa
JB
4965 /*
4966 * Disable Probe Request reporting unless we need it in this way for
4967 * devices that include the AP SME, in the other case (unless using
4968 * monitor iface) we'll get it through the nl_mgmt socket instead.
4969 */
4970 if (!drv->device_ap_sme)
4971 wpa_driver_nl80211_probe_req_report(bss, 0);
4972
4973 if (!drv->device_ap_sme && !drv->use_monitor)
4974 if (nl80211_mgmt_subscribe_ap(bss))
4975 return -1;
4976
a6cc0602
JM
4977 if (drv->device_ap_sme && !drv->use_monitor)
4978 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
f4830bed
RM
4979 wpa_printf(MSG_DEBUG,
4980 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
a6cc0602 4981
a11241fa 4982 if (!drv->device_ap_sme && drv->use_monitor &&
ea19b39f
RM
4983 nl80211_create_monitor_interface(drv) &&
4984 !drv->device_ap_sme)
3fd1cefb
JB
4985 return -1;
4986
4987 if (drv->device_ap_sme &&
4988 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4989 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4990 "Probe Request frame reporting in AP mode");
4991 /* Try to survive without this */
4992 }
4993
4994 return 0;
4995}
4996
4997
4998static void nl80211_teardown_ap(struct i802_bss *bss)
4999{
5000 struct wpa_driver_nl80211_data *drv = bss->drv;
5001
748c0ac0
JM
5002 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
5003 bss->ifname, drv->device_ap_sme, drv->use_monitor);
a6cc0602 5004 if (drv->device_ap_sme) {
3fd1cefb 5005 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
5006 if (!drv->use_monitor)
5007 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
5008 } else if (drv->use_monitor)
3fd1cefb 5009 nl80211_remove_monitor_interface(drv);
a11241fa 5010 else
36488c05 5011 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 5012
a70cd0db 5013 nl80211_put_wiphy_data_ap(bss);
3fd1cefb
JB
5014 bss->beacon_set = 0;
5015}
5016
5017
f10bfc9a
JM
5018static int nl80211_send_eapol_data(struct i802_bss *bss,
5019 const u8 *addr, const u8 *data,
d12dab4c 5020 size_t data_len)
f10bfc9a 5021{
d12dab4c
JB
5022 struct sockaddr_ll ll;
5023 int ret;
5024
5025 if (bss->drv->eapol_tx_sock < 0) {
5026 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
5027 return -1;
5028 }
5029
d12dab4c
JB
5030 os_memset(&ll, 0, sizeof(ll));
5031 ll.sll_family = AF_PACKET;
5032 ll.sll_ifindex = bss->ifindex;
5033 ll.sll_protocol = htons(ETH_P_PAE);
5034 ll.sll_halen = ETH_ALEN;
5035 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
5036 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
5037 (struct sockaddr *) &ll, sizeof(ll));
5038 if (ret < 0)
5039 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
5040 strerror(errno));
5041
5042 return ret;
f10bfc9a 5043}
5fb1a232 5044
f10bfc9a 5045
db149ac9
JM
5046static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
5047
5048static int wpa_driver_nl80211_hapd_send_eapol(
5049 void *priv, const u8 *addr, const u8 *data,
4378fc14 5050 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 5051{
a2e40bb6
FF
5052 struct i802_bss *bss = priv;
5053 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
5054 struct ieee80211_hdr *hdr;
5055 size_t len;
5056 u8 *pos;
5057 int res;
4378fc14 5058 int qos = flags & WPA_STA_WMM;
db149ac9 5059
a11241fa 5060 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 5061 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 5062
db149ac9
JM
5063 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
5064 data_len;
5065 hdr = os_zalloc(len);
5066 if (hdr == NULL) {
7ac3616d
JM
5067 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
5068 (unsigned long) len);
db149ac9
JM
5069 return -1;
5070 }
5071
5072 hdr->frame_control =
5073 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
5074 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
5075 if (encrypt)
5076 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
5077 if (qos) {
5078 hdr->frame_control |=
5079 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
5080 }
db149ac9
JM
5081
5082 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
5083 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
5084 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
5085 pos = (u8 *) (hdr + 1);
5086
db149ac9 5087 if (qos) {
92d521d8
FF
5088 /* Set highest priority in QoS header */
5089 pos[0] = 7;
db149ac9
JM
5090 pos[1] = 0;
5091 pos += 2;
5092 }
db149ac9
JM
5093
5094 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5095 pos += sizeof(rfc1042_header);
5096 WPA_PUT_BE16(pos, ETH_P_PAE);
5097 pos += 2;
5098 memcpy(pos, data, data_len);
5099
55231068 5100 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
2d3943ce 5101 0, 0, 0, 0, NULL, 0);
db149ac9
JM
5102 if (res < 0) {
5103 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
5104 "failed: %d (%s)",
5105 (unsigned long) len, errno, strerror(errno));
5106 }
7bf12757 5107 os_free(hdr);
db149ac9
JM
5108
5109 return res;
5110}
5111
a8d6ffa4 5112
3234cba4 5113static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
f97e3ce4
JM
5114 unsigned int total_flags,
5115 unsigned int flags_or,
5116 unsigned int flags_and)
a8d6ffa4 5117{
a2e40bb6 5118 struct i802_bss *bss = priv;
8970bae8
JB
5119 struct nl_msg *msg;
5120 struct nlattr *flags;
7e76ee9c 5121 struct nl80211_sta_flag_update upd;
a8d6ffa4 5122
0cd9846c
JM
5123 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
5124 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
5125 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
5126 !!(total_flags & WPA_STA_AUTHORIZED));
5127
13f83980 5128 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
5129 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
5130 goto fail;
a8d6ffa4 5131
7e76ee9c
JM
5132 /*
5133 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5134 * can be removed eventually.
5135 */
8970bae8 5136 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
a862e4a3
JM
5137 if (!flags ||
5138 ((total_flags & WPA_STA_AUTHORIZED) &&
5139 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
5140 ((total_flags & WPA_STA_WMM) &&
5141 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
5142 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
5143 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
5144 ((total_flags & WPA_STA_MFP) &&
5145 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
5146 ((total_flags & WPA_STA_TDLS_PEER) &&
5147 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
5148 goto fail;
45b722f1 5149
8970bae8 5150 nla_nest_end(msg, flags);
a8d6ffa4 5151
7e76ee9c
JM
5152 os_memset(&upd, 0, sizeof(upd));
5153 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5154 upd.set = sta_flags_nl80211(flags_or);
a862e4a3
JM
5155 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
5156 goto fail;
7e76ee9c 5157
13f83980 5158 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a862e4a3 5159fail:
5883168a 5160 nlmsg_free(msg);
a8d6ffa4
JM
5161 return -ENOBUFS;
5162}
5163
0915d02c 5164
1581b38b
JM
5165static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5166 struct wpa_driver_associate_params *params)
5167{
708bc8e0 5168 enum nl80211_iftype nlmode, old_mode;
b1f625e0
EP
5169
5170 if (params->p2p) {
046b26a2
JM
5171 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5172 "group (GO)");
b1f625e0
EP
5173 nlmode = NL80211_IFTYPE_P2P_GO;
5174 } else
5175 nlmode = NL80211_IFTYPE_AP;
5176
708bc8e0 5177 old_mode = drv->nlmode;
834ee56f 5178 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
708bc8e0
JM
5179 nl80211_remove_monitor_interface(drv);
5180 return -1;
5181 }
5182
6e9023ea
JM
5183 if (params->freq.freq &&
5184 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
708bc8e0 5185 if (old_mode != nlmode)
834ee56f 5186 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
460456f8 5187 nl80211_remove_monitor_interface(drv);
1581b38b 5188 return -1;
0915d02c 5189 }
1581b38b 5190
1581b38b
JM
5191 return 0;
5192}
1581b38b
JM
5193
5194
666e508c
JM
5195static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5196 int reset_mode)
5cc4d64b
JM
5197{
5198 struct nl_msg *msg;
9725b784 5199 int ret;
a862e4a3 5200
9725b784 5201 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b 5202 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5cc4d64b
JM
5203 if (ret) {
5204 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5205 "(%s)", ret, strerror(-ret));
9725b784
JM
5206 } else {
5207 wpa_printf(MSG_DEBUG,
5208 "nl80211: Leave IBSS request sent successfully");
5cc4d64b
JM
5209 }
5210
666e508c
JM
5211 if (reset_mode &&
5212 wpa_driver_nl80211_set_mode(drv->first_bss,
5d4c78fb
JM
5213 NL80211_IFTYPE_STATION)) {
5214 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5215 "station mode");
5216 }
5217
5cc4d64b
JM
5218 return ret;
5219}
5220
5221
95ff3069
JD
5222static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5223 struct wpa_driver_associate_params *params)
5224{
5225 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5226 return -1;
5227
5228 if (params->htcaps && params->htcaps_mask) {
5229 int sz = sizeof(struct ieee80211_ht_capabilities);
5230 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5231 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5232 params->htcaps_mask, sz);
5233 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5234 params->htcaps) ||
5235 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5236 params->htcaps_mask))
5237 return -1;
5238 }
5239
5240#ifdef CONFIG_VHT_OVERRIDES
5241 if (params->disable_vht) {
5242 wpa_printf(MSG_DEBUG, " * VHT disabled");
5243 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5244 return -1;
5245 }
5246
5247 if (params->vhtcaps && params->vhtcaps_mask) {
5248 int sz = sizeof(struct ieee80211_vht_capabilities);
5249 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5250 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5251 params->vhtcaps_mask, sz);
5252 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5253 params->vhtcaps) ||
5254 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5255 params->vhtcaps_mask))
5256 return -1;
5257 }
5258#endif /* CONFIG_VHT_OVERRIDES */
5259
5260 return 0;
5261}
5262
5263
5cc4d64b
JM
5264static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5265 struct wpa_driver_associate_params *params)
5266{
5267 struct nl_msg *msg;
5268 int ret = -1;
5269 int count = 0;
5270
5271 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5272
4ec68377 5273 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
5cc4d64b
JM
5274 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5275 "IBSS mode");
5276 return -1;
5277 }
5278
5279retry:
9725b784 5280 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
a862e4a3
JM
5281 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5282 goto fail;
5cc4d64b
JM
5283
5284 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5285 params->ssid, params->ssid_len);
a862e4a3
JM
5286 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5287 goto fail;
5cc4d64b
JM
5288 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5289 drv->ssid_len = params->ssid_len;
5290
85e1fad8
JM
5291 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5292 nl80211_put_beacon_int(msg, params->beacon_int))
a862e4a3 5293 goto fail;
5cc4d64b
JM
5294
5295 ret = nl80211_set_conn_keys(params, msg);
5296 if (ret)
a862e4a3 5297 goto fail;
5cc4d64b 5298
913e3cf7
NC
5299 if (params->bssid && params->fixed_bssid) {
5300 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5301 MAC2STR(params->bssid));
a862e4a3
JM
5302 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5303 goto fail;
913e3cf7
NC
5304 }
5305
4d9e6fba
JD
5306 if (params->fixed_freq) {
5307 wpa_printf(MSG_DEBUG, " * fixed_freq");
5308 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5309 goto fail;
5310 }
5311
4848a38d
JM
5312 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5313 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5314 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5315 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
e640888c 5316 wpa_printf(MSG_DEBUG, " * control port");
a862e4a3
JM
5317 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5318 goto fail;
e640888c
AQ
5319 }
5320
a95795ad
JM
5321 if (params->wpa_ie) {
5322 wpa_hexdump(MSG_DEBUG,
5323 " * Extra IEs for Beacon/Probe Response frames",
5324 params->wpa_ie, params->wpa_ie_len);
a862e4a3
JM
5325 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5326 params->wpa_ie))
5327 goto fail;
a95795ad
JM
5328 }
5329
e15dcf6d
PK
5330 ret = nl80211_ht_vht_overrides(msg, params);
5331 if (ret < 0)
5332 goto fail;
95ff3069 5333
5cc4d64b
JM
5334 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5335 msg = NULL;
5336 if (ret) {
5337 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5338 ret, strerror(-ret));
5339 count++;
5340 if (ret == -EALREADY && count == 1) {
5341 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5342 "forced leave");
666e508c 5343 nl80211_leave_ibss(drv, 0);
5cc4d64b
JM
5344 nlmsg_free(msg);
5345 goto retry;
5346 }
a862e4a3
JM
5347 } else {
5348 wpa_printf(MSG_DEBUG,
5349 "nl80211: Join IBSS request sent successfully");
5cc4d64b 5350 }
5cc4d64b 5351
a862e4a3 5352fail:
5cc4d64b
JM
5353 nlmsg_free(msg);
5354 return ret;
5355}
5356
5357
ad295f3b
VK
5358static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5359 struct wpa_driver_associate_params *params,
5360 struct nl_msg *msg)
5361{
5362 if (params->fils_erp_username_len) {
5363 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5364 params->fils_erp_username,
5365 params->fils_erp_username_len);
5366 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5367 params->fils_erp_username_len,
5368 params->fils_erp_username))
5369 return -1;
5370 }
5371
5372 if (params->fils_erp_realm_len) {
5373 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5374 params->fils_erp_realm,
5375 params->fils_erp_realm_len);
5376 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5377 params->fils_erp_realm_len, params->fils_erp_realm))
5378 return -1;
5379 }
5380
5381 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5382 params->fils_erp_next_seq_num);
5383 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5384 params->fils_erp_next_seq_num))
5385 return -1;
5386
5387 if (params->fils_erp_rrk_len) {
5388 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5389 (unsigned long) params->fils_erp_rrk_len);
5390 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5391 params->fils_erp_rrk_len, params->fils_erp_rrk))
5392 return -1;
5393 }
5394
5395 return 0;
5396}
5397
5398
a0bdd191
JM
5399static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5400 struct wpa_driver_associate_params *params,
5401 struct nl_msg *msg)
cfaab580 5402{
c85dfc6f
JM
5403 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5404 return -1;
5405
cfaab580
ZY
5406 if (params->bssid) {
5407 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5408 MAC2STR(params->bssid));
a862e4a3
JM
5409 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5410 return -1;
cfaab580 5411 }
a0bdd191 5412
7ac7fd43
DS
5413 if (params->bssid_hint) {
5414 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5415 MAC2STR(params->bssid_hint));
a862e4a3
JM
5416 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5417 params->bssid_hint))
5418 return -1;
7ac7fd43
DS
5419 }
5420
4ec68377
JD
5421 if (params->freq.freq) {
5422 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
a862e4a3
JM
5423 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5424 params->freq.freq))
5425 return -1;
4ec68377 5426 drv->assoc_freq = params->freq.freq;
30158a0d
SD
5427 } else
5428 drv->assoc_freq = 0;
a0bdd191 5429
7ac7fd43
DS
5430 if (params->freq_hint) {
5431 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
a862e4a3
JM
5432 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5433 params->freq_hint))
5434 return -1;
7ac7fd43
DS
5435 }
5436
1f6c0ab8
BS
5437 if (params->bg_scan_period >= 0) {
5438 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5439 params->bg_scan_period);
a862e4a3
JM
5440 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5441 params->bg_scan_period))
5442 return -1;
1f6c0ab8 5443 }
a0bdd191 5444
cfaab580
ZY
5445 if (params->ssid) {
5446 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5447 params->ssid, params->ssid_len);
a862e4a3
JM
5448 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5449 params->ssid))
5450 return -1;
cfaab580 5451 if (params->ssid_len > sizeof(drv->ssid))
a862e4a3 5452 return -1;
cfaab580
ZY
5453 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5454 drv->ssid_len = params->ssid_len;
5455 }
a0bdd191 5456
cfaab580 5457 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
a862e4a3
JM
5458 if (params->wpa_ie &&
5459 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5460 return -1;
cfaab580 5461
64fa840a
JM
5462 if (params->wpa_proto) {
5463 enum nl80211_wpa_versions ver = 0;
cfaab580 5464
64fa840a
JM
5465 if (params->wpa_proto & WPA_PROTO_WPA)
5466 ver |= NL80211_WPA_VERSION_1;
5467 if (params->wpa_proto & WPA_PROTO_RSN)
5468 ver |= NL80211_WPA_VERSION_2;
cfaab580 5469
64fa840a 5470 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
a862e4a3
JM
5471 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5472 return -1;
cfaab580
ZY
5473 }
5474
4848a38d 5475 if (params->pairwise_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
5476 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5477 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
a862e4a3
JM
5478 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5479 cipher))
5480 return -1;
cfaab580
ZY
5481 }
5482
ae6f9272
JM
5483 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5484 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5485 /*
5486 * This is likely to work even though many drivers do not
5487 * advertise support for operations without GTK.
5488 */
5489 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5490 } else if (params->group_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
5491 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5492 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
a862e4a3
JM
5493 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5494 return -1;
cfaab580
ZY
5495 }
5496
4848a38d
JM
5497 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5498 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5499 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5500 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
163f801e 5501 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8802326f
JJ
5502 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5503 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
666497c8 5504 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5e3b5197 5505 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
ad295f3b
VK
5506 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5507 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5508 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5509 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
e005725a
SD
5510 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5511 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5512 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
3aa24db9 5513 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
cfaab580
ZY
5514
5515 switch (params->key_mgmt_suite) {
4848a38d 5516 case WPA_KEY_MGMT_CCKM:
3aa24db9 5517 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
369c8d7b 5518 break;
4848a38d 5519 case WPA_KEY_MGMT_IEEE8021X:
3aa24db9 5520 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
cfaab580 5521 break;
4848a38d 5522 case WPA_KEY_MGMT_FT_IEEE8021X:
3aa24db9 5523 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
6a1ce395 5524 break;
4848a38d 5525 case WPA_KEY_MGMT_FT_PSK:
3aa24db9 5526 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
6a1ce395 5527 break;
8802326f 5528 case WPA_KEY_MGMT_IEEE8021X_SHA256:
3aa24db9 5529 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
8802326f
JJ
5530 break;
5531 case WPA_KEY_MGMT_PSK_SHA256:
3aa24db9 5532 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
8802326f 5533 break;
163f801e 5534 case WPA_KEY_MGMT_OSEN:
3aa24db9 5535 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
163f801e 5536 break;
666497c8 5537 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
3aa24db9 5538 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
666497c8 5539 break;
5e3b5197 5540 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
3aa24db9 5541 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
5e3b5197 5542 break;
ad295f3b
VK
5543 case WPA_KEY_MGMT_FILS_SHA256:
5544 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5545 break;
5546 case WPA_KEY_MGMT_FILS_SHA384:
5547 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5548 break;
5549 case WPA_KEY_MGMT_FT_FILS_SHA256:
5550 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5551 break;
5552 case WPA_KEY_MGMT_FT_FILS_SHA384:
5553 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5554 break;
e005725a
SD
5555 case WPA_KEY_MGMT_OWE:
5556 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5557 break;
5558 case WPA_KEY_MGMT_DPP:
5559 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5560 break;
4848a38d 5561 case WPA_KEY_MGMT_PSK:
cfaab580 5562 default:
3aa24db9 5563 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
cfaab580
ZY
5564 break;
5565 }
163f801e 5566 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
a862e4a3
JM
5567 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5568 return -1;
cfaab580
ZY
5569 }
5570
730c5a1d
EP
5571 /* Add PSK in case of 4-way handshake offload */
5572 if (params->psk &&
5573 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)) {
5574 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5575 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5576 return -1;
5577 }
5578
a862e4a3
JM
5579 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5580 return -1;
a0bdd191 5581
e3429c0b
JB
5582 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5583 (params->pairwise_suite == WPA_CIPHER_NONE ||
5584 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5585 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5586 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5587 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5588 return -1;
5589
58162adf
AK
5590 if (params->rrm_used) {
5591 u32 drv_rrm_flags = drv->capa.rrm_flags;
b5d172e5
BL
5592 if ((!((drv_rrm_flags &
5593 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5594 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5595 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
a862e4a3
JM
5596 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5597 return -1;
58162adf
AK
5598 }
5599
95ff3069 5600 if (nl80211_ht_vht_overrides(msg, params) < 0)
a862e4a3 5601 return -1;
80e8a5ee 5602
a0bdd191
JM
5603 if (params->p2p)
5604 wpa_printf(MSG_DEBUG, " * P2P group");
5605
86b5c400
LD
5606 if (params->pbss) {
5607 wpa_printf(MSG_DEBUG, " * PBSS");
5608 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5609 return -1;
5610 }
5611
1126c078 5612 drv->connect_reassoc = 0;
00c3c4ac
JM
5613 if (params->prev_bssid) {
5614 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5615 MAC2STR(params->prev_bssid));
5616 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5617 params->prev_bssid))
5618 return -1;
1126c078 5619 drv->connect_reassoc = 1;
00c3c4ac
JM
5620 }
5621
ad295f3b
VK
5622 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5623 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5624 return -1;
5625
ba71cb82
SD
5626 if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5627 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5628 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5629 return -1;
5630
a0bdd191 5631 return 0;
a0bdd191
JM
5632}
5633
5634
5635static int wpa_driver_nl80211_try_connect(
5636 struct wpa_driver_nl80211_data *drv,
40a68f33
SD
5637 struct wpa_driver_associate_params *params,
5638 struct nl_handle *nl_connect)
a0bdd191
JM
5639{
5640 struct nl_msg *msg;
5641 enum nl80211_auth_type type;
5642 int ret;
5643 int algs;
5644
b658547d 5645#ifdef CONFIG_DRIVER_NL80211_QCA
b41f2684
CL
5646 if (params->req_key_mgmt_offload && params->psk &&
5647 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5648 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5649 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5650 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5651 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5652 if (ret)
5653 return ret;
5654 }
b658547d 5655#endif /* CONFIG_DRIVER_NL80211_QCA */
b41f2684 5656
9725b784
JM
5657 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5658 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
a0bdd191
JM
5659 if (!msg)
5660 return -1;
5661
a0bdd191
JM
5662 ret = nl80211_connect_common(drv, params, msg);
5663 if (ret)
a862e4a3 5664 goto fail;
a0bdd191 5665
299d21e8
EG
5666 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5667 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5668 goto fail;
5669
5670 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
5671 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
5672 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
5673 goto fail;
5674
a0bdd191
JM
5675 algs = 0;
5676 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5677 algs++;
5678 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5679 algs++;
5680 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5681 algs++;
64a0a75b
JM
5682 if (params->auth_alg & WPA_AUTH_ALG_FILS)
5683 algs++;
86c998d3
AM
5684 if (params->auth_alg & WPA_AUTH_ALG_FT)
5685 algs++;
a0bdd191
JM
5686 if (algs > 1) {
5687 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5688 "selection");
5689 goto skip_auth_type;
5690 }
5691
3c67e977 5692 type = get_nl_auth_type(params->auth_alg);
a0bdd191 5693 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3c67e977
VK
5694 if (type == NL80211_AUTHTYPE_MAX ||
5695 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
a862e4a3 5696 goto fail;
a0bdd191
JM
5697
5698skip_auth_type:
cfaab580
ZY
5699 ret = nl80211_set_conn_keys(params, msg);
5700 if (ret)
a862e4a3 5701 goto fail;
cfaab580 5702
40a68f33
SD
5703 if (nl_connect)
5704 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
5705 else
5706 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5707
cfaab580
ZY
5708 msg = NULL;
5709 if (ret) {
5710 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5711 "(%s)", ret, strerror(-ret));
a862e4a3
JM
5712 } else {
5713 wpa_printf(MSG_DEBUG,
5714 "nl80211: Connect request send successfully");
cfaab580 5715 }
cfaab580 5716
a862e4a3 5717fail:
cfaab580
ZY
5718 nlmsg_free(msg);
5719 return ret;
5720
5721}
5722
5723
a8c5b43a
CW
5724static int wpa_driver_nl80211_connect(
5725 struct wpa_driver_nl80211_data *drv,
40a68f33
SD
5726 struct wpa_driver_associate_params *params,
5727 struct nl_handle *nl_connect)
a8c5b43a 5728{
0d4e3d1d
JJ
5729 int ret;
5730
5731 /* Store the connection attempted bssid for future use */
5732 if (params->bssid)
5733 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5734 else
5735 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5736
40a68f33 5737 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
a8c5b43a
CW
5738 if (ret == -EALREADY) {
5739 /*
5740 * cfg80211 does not currently accept new connections if
5741 * we are already connected. As a workaround, force
5742 * disconnection and try again.
5743 */
5744 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5745 "disconnecting before reassociation "
5746 "attempt");
5747 if (wpa_driver_nl80211_disconnect(
10d32e2c 5748 drv, WLAN_REASON_PREV_AUTH_NOT_VALID, nl_connect))
a8c5b43a 5749 return -1;
40a68f33 5750 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
a8c5b43a
CW
5751 }
5752 return ret;
5753}
5754
5755
c2a04078
JM
5756static int wpa_driver_nl80211_associate(
5757 void *priv, struct wpa_driver_associate_params *params)
5758{
a2e40bb6
FF
5759 struct i802_bss *bss = priv;
5760 struct wpa_driver_nl80211_data *drv = bss->drv;
a862e4a3 5761 int ret = -1;
c2a04078
JM
5762 struct nl_msg *msg;
5763
4bd71954
JM
5764 nl80211_unmask_11b_rates(bss);
5765
5cc4d64b 5766 if (params->mode == IEEE80211_MODE_AP)
1581b38b 5767 return wpa_driver_nl80211_ap(drv, params);
1581b38b 5768
5cc4d64b
JM
5769 if (params->mode == IEEE80211_MODE_IBSS)
5770 return wpa_driver_nl80211_ibss(drv, params);
5771
4a867032 5772 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
5773 enum nl80211_iftype nlmode = params->p2p ?
5774 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
40a68f33 5775 struct nl_handle *nl_connect = NULL;
b1f625e0
EP
5776
5777 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 5778 return -1;
10d32e2c 5779 if (params->auth_alg & WPA_AUTH_ALG_SAE) {
40a68f33 5780 nl_connect = bss->nl_connect;
10d32e2c
CI
5781 bss->use_nl_connect = 1;
5782 } else {
5783 bss->use_nl_connect = 0;
5784 }
5785
40a68f33 5786 return wpa_driver_nl80211_connect(drv, params, nl_connect);
4a867032 5787 }
cfaab580 5788
add9b7a4 5789 nl80211_mark_disconnected(drv);
c2a04078 5790
c2a04078
JM
5791 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5792 drv->ifindex);
9725b784
JM
5793 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
5794 if (!msg)
5795 return -1;
c2a04078 5796
a0bdd191
JM
5797 ret = nl80211_connect_common(drv, params, msg);
5798 if (ret)
a862e4a3 5799 goto fail;
01652550 5800
299d21e8
EG
5801 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5802 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5803 goto fail;
5804
40a45727
JM
5805 if (params->fils_kek) {
5806 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5807 (unsigned int) params->fils_kek_len);
5808 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5809 params->fils_kek))
5810 goto fail;
5811 }
5812 if (params->fils_nonces) {
5813 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5814 params->fils_nonces,
5815 params->fils_nonces_len);
5816 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5817 params->fils_nonces_len, params->fils_nonces))
5818 goto fail;
5819 }
5820
c2a04078
JM
5821 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5822 msg = NULL;
5823 if (ret) {
3b7ea880
BG
5824 wpa_dbg(drv->ctx, MSG_DEBUG,
5825 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5826 ret, strerror(-ret));
8856462d 5827 nl80211_dump_scan(drv);
a862e4a3
JM
5828 } else {
5829 wpa_printf(MSG_DEBUG,
5830 "nl80211: Association request send successfully");
c2a04078 5831 }
c2a04078 5832
a862e4a3 5833fail:
c2a04078
JM
5834 nlmsg_free(msg);
5835 return ret;
5836}
3f5285e8
JM
5837
5838
ad1e68e6 5839static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 5840 int ifindex, enum nl80211_iftype mode)
3f5285e8 5841{
3f5285e8 5842 struct nl_msg *msg;
ad1e68e6
JM
5843 int ret = -ENOBUFS;
5844
a1922f93
JM
5845 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5846 ifindex, mode, nl80211_iftype_str(mode));
5847
56f77852
JM
5848 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5849 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
a862e4a3 5850 goto fail;
ad1e68e6
JM
5851
5852 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5853 msg = NULL;
ad1e68e6
JM
5854 if (!ret)
5855 return 0;
a862e4a3 5856fail:
5883168a 5857 nlmsg_free(msg);
ad1e68e6
JM
5858 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5859 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5860 return ret;
5861}
5862
5863
3c5d34e3
CW
5864static int wpa_driver_nl80211_set_mode_impl(
5865 struct i802_bss *bss,
5866 enum nl80211_iftype nlmode,
5867 struct hostapd_freq_params *desired_freq_params)
ad1e68e6 5868{
a2e40bb6 5869 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 5870 int ret = -1;
26af9dca 5871 int i;
86957e62 5872 int was_ap = is_ap_interface(drv->nlmode);
671a5039 5873 int res;
ebffdbc4 5874 int mode_switch_res;
1581b38b 5875
a5a187b0
JM
5876 if (TEST_FAIL())
5877 return -1;
5878
ebffdbc4
CW
5879 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5880 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5881 mode_switch_res = 0;
8e12685c 5882
ebffdbc4 5883 if (mode_switch_res == 0) {
ad1e68e6 5884 drv->nlmode = nlmode;
460456f8
JM
5885 ret = 0;
5886 goto done;
ad1e68e6 5887 }
3f5285e8 5888
ebffdbc4 5889 if (mode_switch_res == -ENODEV)
671a5039
JM
5890 return -1;
5891
460456f8 5892 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
5893 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5894 "requested mode - ignore error");
460456f8
JM
5895 ret = 0;
5896 goto done; /* Already in the requested mode */
5897 }
3f5285e8 5898
3f5285e8
JM
5899 /* mac80211 doesn't allow mode changes while the device is up, so
5900 * take the device down, try to set the mode again, and bring the
5901 * device back up.
5902 */
26af9dca
JM
5903 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5904 "interface down");
5905 for (i = 0; i < 10; i++) {
91724d6f 5906 res = i802_set_iface_flags(bss, 0);
6e8183d7
JM
5907 if (res == -EACCES || res == -ENODEV)
5908 break;
ebffdbc4 5909 if (res != 0) {
26af9dca
JM
5910 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5911 "interface down");
ebffdbc4
CW
5912 os_sleep(0, 100000);
5913 continue;
5914 }
3c5d34e3
CW
5915
5916 /*
5917 * Setting the mode will fail for some drivers if the phy is
5918 * on a frequency that the mode is disallowed in.
5919 */
5920 if (desired_freq_params) {
bdf5ccb2 5921 res = nl80211_set_channel(bss, desired_freq_params, 0);
3c5d34e3
CW
5922 if (res) {
5923 wpa_printf(MSG_DEBUG,
5924 "nl80211: Failed to set frequency on interface");
5925 }
5926 }
5927
ebffdbc4
CW
5928 /* Try to set the mode again while the interface is down */
5929 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5930 if (mode_switch_res == -EBUSY) {
5931 wpa_printf(MSG_DEBUG,
5932 "nl80211: Delaying mode set while interface going down");
5933 os_sleep(0, 100000);
5934 continue;
5935 }
5936 ret = mode_switch_res;
5937 break;
3f5285e8
JM
5938 }
5939
c6e8e8e4
JM
5940 if (!ret) {
5941 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5942 "interface is down");
ad1e68e6 5943 drv->nlmode = nlmode;
7d9c3698 5944 drv->ignore_if_down_event = 1;
c6e8e8e4 5945 }
ad1e68e6 5946
ebffdbc4
CW
5947 /* Bring the interface back up */
5948 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5949 if (res != 0) {
5950 wpa_printf(MSG_DEBUG,
5951 "nl80211: Failed to set interface up after switching mode");
5952 ret = -1;
5953 }
5954
460456f8 5955done:
3fd1cefb
JB
5956 if (ret) {
5957 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5958 "from %d failed", nlmode, drv->nlmode);
5959 return ret;
5960 }
5961
3e208481
JM
5962 if (is_p2p_net_interface(nlmode)) {
5963 wpa_printf(MSG_DEBUG,
5964 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5965 bss->ifname);
edb9bfba 5966 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
3e208481
JM
5967 } else if (drv->disabled_11b_rates) {
5968 wpa_printf(MSG_DEBUG,
5969 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5970 bss->ifname);
1d0c6fb1 5971 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3e208481 5972 }
edb9bfba 5973
3fd1cefb 5974 if (is_ap_interface(nlmode)) {
36488c05 5975 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 5976 /* Setup additional AP mode functionality if needed */
3fd1cefb 5977 if (nl80211_setup_ap(bss))
460456f8 5978 return -1;
3fd1cefb 5979 } else if (was_ap) {
460456f8 5980 /* Remove additional AP mode functionality */
3fd1cefb 5981 nl80211_teardown_ap(bss);
a11241fa 5982 } else {
36488c05 5983 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 5984 }
460456f8 5985
afb0550a
BC
5986 if (is_mesh_interface(nlmode) &&
5987 nl80211_mgmt_subscribe_mesh(bss))
5988 return -1;
5989
873d0fcf 5990 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
afb0550a 5991 !is_mesh_interface(nlmode) &&
a11241fa
JB
5992 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5993 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5994 "frame processing - ignore for now");
08359050 5995
3fd1cefb 5996 return 0;
3f5285e8
JM
5997}
5998
5999
f3407c66
JM
6000int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
6001 enum nl80211_iftype nlmode)
3c5d34e3
CW
6002{
6003 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
6004}
6005
6006
4ec68377
JD
6007static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
6008 struct hostapd_freq_params *freq)
3c5d34e3 6009{
3c5d34e3 6010 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
4ec68377 6011 freq);
3c5d34e3 6012}
65d645ce
AS
6013
6014
3f5285e8
JM
6015static int wpa_driver_nl80211_get_capa(void *priv,
6016 struct wpa_driver_capa *capa)
6017{
a2e40bb6
FF
6018 struct i802_bss *bss = priv;
6019 struct wpa_driver_nl80211_data *drv = bss->drv;
65d645ce 6020
3f5285e8
JM
6021 if (!drv->has_capability)
6022 return -1;
6023 os_memcpy(capa, &drv->capa, sizeof(*capa));
8cd6b7bc
JB
6024 if (drv->extended_capa && drv->extended_capa_mask) {
6025 capa->extended_capa = drv->extended_capa;
6026 capa->extended_capa_mask = drv->extended_capa_mask;
6027 capa->extended_capa_len = drv->extended_capa_len;
6028 }
851b0c55 6029
906bc4c7 6030 return 0;
3f5285e8
JM
6031}
6032
6033
6034static int wpa_driver_nl80211_set_operstate(void *priv, int state)
6035{
a2e40bb6
FF
6036 struct i802_bss *bss = priv;
6037 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 6038
90a545cc
JM
6039 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
6040 bss->ifname, drv->operstate, state,
6041 state ? "UP" : "DORMANT");
3f5285e8 6042 drv->operstate = state;
36d84860 6043 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 6044 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
6045}
6046
01652550
JM
6047
6048static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
6049{
a2e40bb6
FF
6050 struct i802_bss *bss = priv;
6051 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
6052 struct nl_msg *msg;
6053 struct nl80211_sta_flag_update upd;
a862e4a3 6054 int ret;
2eef5177
JM
6055
6056 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
6057 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
6058 return 0;
6059 }
01652550 6060
1ba51ec0
JM
6061 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
6062 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
6063
01652550
JM
6064 os_memset(&upd, 0, sizeof(upd));
6065 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
6066 if (authorized)
6067 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
a862e4a3 6068
13f83980 6069 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
6070 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
6071 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
6072 nlmsg_free(msg);
6073 return -ENOBUFS;
6074 }
01652550 6075
2eef5177 6076 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2eef5177
JM
6077 if (!ret)
6078 return 0;
2eef5177
JM
6079 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
6080 ret, strerror(-ret));
6081 return ret;
01652550
JM
6082}
6083
3f5285e8 6084
f07ead6a
JM
6085/* Set kernel driver on given frequency (MHz) */
6086static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 6087{
f07ead6a 6088 struct i802_bss *bss = priv;
13a524a3 6089 return nl80211_set_channel(bss, freq, 0);
c5121837
JM
6090}
6091
f7b3920c 6092
c5121837
JM
6093static inline int min_int(int a, int b)
6094{
6095 if (a < b)
6096 return a;
6097 return b;
6098}
6099
6100
6101static int get_key_handler(struct nl_msg *msg, void *arg)
6102{
6103 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6104 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6105
6106 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6107 genlmsg_attrlen(gnlh, 0), NULL);
6108
6109 /*
6110 * TODO: validate the key index and mac address!
6111 * Otherwise, there's a race condition as soon as
6112 * the kernel starts sending key notifications.
6113 */
6114
6115 if (tb[NL80211_ATTR_KEY_SEQ])
6116 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
6117 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
6118 return NL_SKIP;
6119}
6120
6121
6122static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
6123 int idx, u8 *seq)
6124{
a2e40bb6
FF
6125 struct i802_bss *bss = priv;
6126 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
6127 struct nl_msg *msg;
6128
95376e1a
JM
6129 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
6130 NL80211_CMD_GET_KEY);
6131 if (!msg ||
a862e4a3 6132 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
95376e1a 6133 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
a862e4a3
JM
6134 nlmsg_free(msg);
6135 return -ENOBUFS;
6136 }
c5121837
JM
6137
6138 memset(seq, 0, 6);
6139
6140 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
c5121837
JM
6141}
6142
6143
c5121837
JM
6144static int i802_set_rts(void *priv, int rts)
6145{
a2e40bb6
FF
6146 struct i802_bss *bss = priv;
6147 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451 6148 struct nl_msg *msg;
a862e4a3 6149 int ret;
ad649451 6150 u32 val;
c5121837 6151
ad649451
JM
6152 if (rts >= 2347)
6153 val = (u32) -1;
6154 else
6155 val = rts;
c5121837 6156
9725b784 6157 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
a862e4a3
JM
6158 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
6159 nlmsg_free(msg);
6160 return -ENOBUFS;
6161 }
ad649451
JM
6162
6163 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6164 if (!ret)
6165 return 0;
ad649451
JM
6166 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
6167 "%d (%s)", rts, ret, strerror(-ret));
6168 return ret;
c5121837
JM
6169}
6170
6171
6172static int i802_set_frag(void *priv, int frag)
6173{
a2e40bb6
FF
6174 struct i802_bss *bss = priv;
6175 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451 6176 struct nl_msg *msg;
a862e4a3 6177 int ret;
ad649451 6178 u32 val;
c5121837 6179
ad649451
JM
6180 if (frag >= 2346)
6181 val = (u32) -1;
6182 else
6183 val = frag;
c5121837 6184
9725b784 6185 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
a862e4a3
JM
6186 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6187 nlmsg_free(msg);
6188 return -ENOBUFS;
6189 }
ad649451
JM
6190
6191 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6192 if (!ret)
6193 return 0;
ad649451
JM
6194 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6195 "%d: %d (%s)", frag, ret, strerror(-ret));
6196 return ret;
c5121837
JM
6197}
6198
6199
c5121837
JM
6200static int i802_flush(void *priv)
6201{
a2e40bb6 6202 struct i802_bss *bss = priv;
c5121837 6203 struct nl_msg *msg;
82554b10 6204 int res;
c5121837 6205
83e7bb0e
JM
6206 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6207 bss->ifname);
c5121837
JM
6208
6209 /*
6210 * XXX: FIX! this needs to flush all VLANs too
6211 */
13f83980
JM
6212 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6213 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
82554b10
JM
6214 if (res) {
6215 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6216 "(%s)", res, strerror(-res));
6217 }
6218 return res;
c5121837
JM
6219}
6220
6221
6222static int get_sta_handler(struct nl_msg *msg, void *arg)
6223{
6224 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6225 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6226 struct hostap_sta_driver_data *data = arg;
6227 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6228 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6229 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6230 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6231 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6232 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6233 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 6234 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
43022abd
NL
6235 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6236 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
3567641e 6237 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
72123a84 6238 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
3567641e 6239 };
6240 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6241 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6242 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6243 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6244 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6245 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6246 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6247 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
c5121837
JM
6248 };
6249
6250 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6251 genlmsg_attrlen(gnlh, 0), NULL);
6252
6253 /*
6254 * TODO: validate the interface and mac address!
6255 * Otherwise, there's a race condition as soon as
6256 * the kernel starts sending station notifications.
6257 */
6258
6259 if (!tb[NL80211_ATTR_STA_INFO]) {
6260 wpa_printf(MSG_DEBUG, "sta stats missing!");
6261 return NL_SKIP;
6262 }
6263 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6264 tb[NL80211_ATTR_STA_INFO],
6265 stats_policy)) {
6266 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6267 return NL_SKIP;
6268 }
6269
6270 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6271 data->inactive_msec =
6272 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
43022abd 6273 /* For backwards compatibility, fetch the 32-bit counters first. */
c5121837
JM
6274 if (stats[NL80211_STA_INFO_RX_BYTES])
6275 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6276 if (stats[NL80211_STA_INFO_TX_BYTES])
6277 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
43022abd
NL
6278 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6279 stats[NL80211_STA_INFO_TX_BYTES64]) {
6280 /*
6281 * The driver supports 64-bit counters, so use them to override
6282 * the 32-bit values.
6283 */
6284 data->rx_bytes =
6285 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6286 data->tx_bytes =
6287 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6288 data->bytes_64bit = 1;
6289 }
c5121837
JM
6290 if (stats[NL80211_STA_INFO_RX_PACKETS])
6291 data->rx_packets =
6292 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6293 if (stats[NL80211_STA_INFO_TX_PACKETS])
6294 data->tx_packets =
6295 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
6296 if (stats[NL80211_STA_INFO_TX_FAILED])
6297 data->tx_retry_failed =
6298 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
3567641e 6299 if (stats[NL80211_STA_INFO_SIGNAL])
6300 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
72123a84
BP
6301 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6302 data->last_ack_rssi =
6303 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6304 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6305 }
3567641e 6306
6307 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6308 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6309 stats[NL80211_STA_INFO_TX_BITRATE],
6310 rate_policy) == 0) {
6311 if (rate[NL80211_RATE_INFO_BITRATE32])
6312 data->current_tx_rate =
6313 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6314 else if (rate[NL80211_RATE_INFO_BITRATE])
6315 data->current_tx_rate =
6316 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6317
6318 if (rate[NL80211_RATE_INFO_MCS]) {
6319 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6320 data->flags |= STA_DRV_DATA_TX_MCS;
6321 }
6322 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6323 data->tx_vhtmcs =
6324 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6325 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6326 }
6327 if (rate[NL80211_RATE_INFO_SHORT_GI])
6328 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6329 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6330 data->tx_vht_nss =
6331 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6332 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6333 }
6334 }
6335
6336 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6337 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6338 stats[NL80211_STA_INFO_RX_BITRATE],
6339 rate_policy) == 0) {
6340 if (rate[NL80211_RATE_INFO_BITRATE32])
6341 data->current_rx_rate =
6342 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6343 else if (rate[NL80211_RATE_INFO_BITRATE])
6344 data->current_rx_rate =
6345 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6346
6347 if (rate[NL80211_RATE_INFO_MCS]) {
6348 data->rx_mcs =
6349 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6350 data->flags |= STA_DRV_DATA_RX_MCS;
6351 }
6352 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6353 data->rx_vhtmcs =
6354 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6355 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6356 }
6357 if (rate[NL80211_RATE_INFO_SHORT_GI])
6358 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6359 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6360 data->rx_vht_nss =
6361 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6362 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6363 }
6364 }
c5121837
JM
6365
6366 return NL_SKIP;
6367}
6368
9ebce9c5
JM
6369static int i802_read_sta_data(struct i802_bss *bss,
6370 struct hostap_sta_driver_data *data,
c5121837
JM
6371 const u8 *addr)
6372{
c5121837
JM
6373 struct nl_msg *msg;
6374
13f83980
JM
6375 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6376 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
a862e4a3
JM
6377 nlmsg_free(msg);
6378 return -ENOBUFS;
6379 }
c5121837 6380
13f83980 6381 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
c5121837
JM
6382}
6383
6384
c5121837
JM
6385static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6386 int cw_min, int cw_max, int burst_time)
6387{
a2e40bb6
FF
6388 struct i802_bss *bss = priv;
6389 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
6390 struct nl_msg *msg;
6391 struct nlattr *txq, *params;
bd512469 6392 int res;
c5121837 6393
13f83980 6394 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
c5121837
JM
6395 if (!msg)
6396 return -1;
6397
c5121837
JM
6398 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6399 if (!txq)
a862e4a3 6400 goto fail;
c5121837
JM
6401
6402 /* We are only sending parameters for a single TXQ at a time */
6403 params = nla_nest_start(msg, 1);
6404 if (!params)
a862e4a3 6405 goto fail;
c5121837 6406
7e3c1781
JM
6407 switch (queue) {
6408 case 0:
a862e4a3
JM
6409 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6410 goto fail;
7e3c1781
JM
6411 break;
6412 case 1:
a862e4a3
JM
6413 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6414 goto fail;
7e3c1781
JM
6415 break;
6416 case 2:
a862e4a3
JM
6417 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6418 goto fail;
7e3c1781
JM
6419 break;
6420 case 3:
a862e4a3
JM
6421 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6422 goto fail;
7e3c1781
JM
6423 break;
6424 }
c5121837
JM
6425 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6426 * 32 usec, so need to convert the value here. */
a862e4a3
JM
6427 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6428 (burst_time * 100 + 16) / 32) ||
6429 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6430 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6431 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6432 goto fail;
c5121837
JM
6433
6434 nla_nest_end(msg, params);
6435
6436 nla_nest_end(msg, txq);
6437
bd512469
JM
6438 res = send_and_recv_msgs(drv, msg, NULL, NULL);
6439 wpa_printf(MSG_DEBUG,
6440 "nl80211: TX queue param set: queue=%d aifs=%d cw_min=%d cw_max=%d burst_time=%d --> res=%d",
6441 queue, aifs, cw_min, cw_max, burst_time, res);
6442 if (res == 0)
c5121837 6443 return 0;
9e088e74 6444 msg = NULL;
a862e4a3 6445fail:
9e088e74 6446 nlmsg_free(msg);
c5121837
JM
6447 return -1;
6448}
6449
6450
9ebce9c5 6451static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
6452 const char *ifname, int vlan_id)
6453{
a2e40bb6 6454 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 6455 struct nl_msg *msg;
a862e4a3 6456 int ret;
c5121837 6457
bbc706a3
JM
6458 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6459 ", ifname=%s[%d], vlan_id=%d)",
6460 bss->ifname, if_nametoindex(bss->ifname),
6461 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
13f83980 6462 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
6463 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6464 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6465 nlmsg_free(msg);
6466 return -ENOBUFS;
6467 }
c5121837 6468
cd1d72c1
JM
6469 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6470 if (ret < 0) {
6471 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6472 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6473 MAC2STR(addr), ifname, vlan_id, ret,
6474 strerror(-ret));
6475 }
cd1d72c1 6476 return ret;
c5121837
JM
6477}
6478
fbbfcbac 6479
c5121837
JM
6480static int i802_get_inact_sec(void *priv, const u8 *addr)
6481{
6482 struct hostap_sta_driver_data data;
6483 int ret;
6484
7824bf77 6485 os_memset(&data, 0, sizeof(data));
c5121837
JM
6486 data.inactive_msec = (unsigned long) -1;
6487 ret = i802_read_sta_data(priv, &data, addr);
b9749bac
MH
6488 if (ret == -ENOENT)
6489 return -ENOENT;
c5121837
JM
6490 if (ret || data.inactive_msec == (unsigned long) -1)
6491 return -1;
6492 return data.inactive_msec / 1000;
6493}
6494
6495
6496static int i802_sta_clear_stats(void *priv, const u8 *addr)
6497{
6498#if 0
6499 /* TODO */
6500#endif
6501 return 0;
6502}
6503
6504
731723a5
JM
6505static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6506 int reason)
c5121837 6507{
a2e40bb6 6508 struct i802_bss *bss = priv;
e1bd4e19 6509 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 6510 struct ieee80211_mgmt mgmt;
05e5e615
DL
6511 u8 channel;
6512
6513 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6514 HOSTAPD_MODE_IEEE80211AD) {
6515 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6516 * disassociate the STA instead. */
6517 return i802_sta_disassoc(priv, own_addr, addr, reason);
6518 }
c5121837 6519
0d391b03
BC
6520 if (is_mesh_interface(drv->nlmode))
6521 return -1;
6522
e1bd4e19 6523 if (drv->device_ap_sme)
59d7148a 6524 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
e1bd4e19 6525
c5121837
JM
6526 memset(&mgmt, 0, sizeof(mgmt));
6527 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6528 WLAN_FC_STYPE_DEAUTH);
6529 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
6530 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6531 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 6532 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 6533 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 6534 IEEE80211_HDRLEN +
9ebce9c5 6535 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
2d3943ce 6536 0, NULL, 0);
c5121837
JM
6537}
6538
6539
731723a5
JM
6540static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6541 int reason)
c5121837 6542{
a2e40bb6 6543 struct i802_bss *bss = priv;
e1bd4e19 6544 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
6545 struct ieee80211_mgmt mgmt;
6546
0d391b03
BC
6547 if (is_mesh_interface(drv->nlmode))
6548 return -1;
6549
e1bd4e19 6550 if (drv->device_ap_sme)
59d7148a 6551 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
e1bd4e19 6552
c5121837
JM
6553 memset(&mgmt, 0, sizeof(mgmt));
6554 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6555 WLAN_FC_STYPE_DISASSOC);
6556 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
6557 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6558 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 6559 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 6560 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 6561 IEEE80211_HDRLEN +
9ebce9c5 6562 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
2d3943ce 6563 0, NULL, 0);
c5121837
JM
6564}
6565
6566
9b4d9c8b
JM
6567static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6568{
6569 char buf[200], *pos, *end;
6570 int i, res;
6571
6572 pos = buf;
6573 end = pos + sizeof(buf);
6574
6575 for (i = 0; i < drv->num_if_indices; i++) {
6576 if (!drv->if_indices[i])
6577 continue;
732b1d20
MB
6578 res = os_snprintf(pos, end - pos, " %d(%d)",
6579 drv->if_indices[i],
6580 drv->if_indices_reason[i]);
d85e1fc8 6581 if (os_snprintf_error(end - pos, res))
9b4d9c8b
JM
6582 break;
6583 pos += res;
6584 }
6585 *pos = '\0';
6586
6587 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6588 drv->num_if_indices, buf);
6589}
6590
6591
732b1d20
MB
6592static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6593 int ifidx_reason)
f07ead6a
JM
6594{
6595 int i;
732b1d20 6596 int *old, *old_reason;
f07ead6a 6597
732b1d20
MB
6598 wpa_printf(MSG_DEBUG,
6599 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6600 ifidx, ifidx_reason);
6601 if (have_ifidx(drv, ifidx, ifidx_reason)) {
b36935be
MB
6602 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6603 ifidx);
6604 return;
6605 }
f07ead6a
JM
6606 for (i = 0; i < drv->num_if_indices; i++) {
6607 if (drv->if_indices[i] == 0) {
6608 drv->if_indices[i] = ifidx;
732b1d20 6609 drv->if_indices_reason[i] = ifidx_reason;
9b4d9c8b 6610 dump_ifidx(drv);
f07ead6a
JM
6611 return;
6612 }
6613 }
6614
6615 if (drv->if_indices != drv->default_if_indices)
6616 old = drv->if_indices;
6617 else
6618 old = NULL;
6619
732b1d20
MB
6620 if (drv->if_indices_reason != drv->default_if_indices_reason)
6621 old_reason = drv->if_indices_reason;
6622 else
6623 old_reason = NULL;
6624
067ffa26
JM
6625 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6626 sizeof(int));
732b1d20
MB
6627 drv->if_indices_reason = os_realloc_array(old_reason,
6628 drv->num_if_indices + 1,
6629 sizeof(int));
f07ead6a
JM
6630 if (!drv->if_indices) {
6631 if (!old)
6632 drv->if_indices = drv->default_if_indices;
6633 else
6634 drv->if_indices = old;
732b1d20
MB
6635 }
6636 if (!drv->if_indices_reason) {
6637 if (!old_reason)
6638 drv->if_indices_reason = drv->default_if_indices_reason;
6639 else
29eddc3d 6640 drv->if_indices_reason = old_reason;
732b1d20
MB
6641 }
6642 if (!drv->if_indices || !drv->if_indices_reason) {
f07ead6a
JM
6643 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6644 "interfaces");
6645 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6646 return;
732b1d20
MB
6647 }
6648 if (!old)
f07ead6a
JM
6649 os_memcpy(drv->if_indices, drv->default_if_indices,
6650 sizeof(drv->default_if_indices));
732b1d20
MB
6651 if (!old_reason)
6652 os_memcpy(drv->if_indices_reason,
6653 drv->default_if_indices_reason,
6654 sizeof(drv->default_if_indices_reason));
f07ead6a 6655 drv->if_indices[drv->num_if_indices] = ifidx;
732b1d20 6656 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
f07ead6a 6657 drv->num_if_indices++;
9b4d9c8b 6658 dump_ifidx(drv);
f07ead6a
JM
6659}
6660
6661
732b1d20
MB
6662static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6663 int ifidx_reason)
f07ead6a
JM
6664{
6665 int i;
6666
6667 for (i = 0; i < drv->num_if_indices; i++) {
732b1d20
MB
6668 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6669 (drv->if_indices_reason[i] == ifidx_reason ||
6670 ifidx_reason == IFIDX_ANY)) {
f07ead6a
JM
6671 drv->if_indices[i] = 0;
6672 break;
6673 }
6674 }
9b4d9c8b 6675 dump_ifidx(drv);
f07ead6a
JM
6676}
6677
6678
732b1d20
MB
6679static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6680 int ifidx_reason)
f07ead6a
JM
6681{
6682 int i;
6683
6684 for (i = 0; i < drv->num_if_indices; i++)
732b1d20
MB
6685 if (drv->if_indices[i] == ifidx &&
6686 (drv->if_indices_reason[i] == ifidx_reason ||
6687 ifidx_reason == IFIDX_ANY))
f07ead6a
JM
6688 return 1;
6689
6690 return 0;
6691}
6692
6693
6694static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
0e80ea2c 6695 const char *bridge_ifname, char *ifname_wds)
f07ead6a
JM
6696{
6697 struct i802_bss *bss = priv;
6698 struct wpa_driver_nl80211_data *drv = bss->drv;
6699 char name[IFNAMSIZ + 1];
1952b626 6700 union wpa_event_data event;
d577f7f3
AO
6701 int ret;
6702
6703 ret = os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
6704 if (ret >= (int) sizeof(name))
6705 wpa_printf(MSG_WARNING,
6706 "nl80211: WDS interface name was truncated");
6707 else if (ret < 0)
6708 return ret;
f07ead6a 6709
69dd2967
SM
6710 if (ifname_wds)
6711 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6712
f07ead6a
JM
6713 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6714 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6715 if (val) {
6716 if (!if_nametoindex(name)) {
6717 if (nl80211_create_iface(drv, name,
6718 NL80211_IFTYPE_AP_VLAN,
2aec4f3c
JM
6719 bss->addr, 1, NULL, NULL, 0) <
6720 0)
f07ead6a
JM
6721 return -1;
6722 if (bridge_ifname &&
c81eff1a
BG
6723 linux_br_add_if(drv->global->ioctl_sock,
6724 bridge_ifname, name) < 0)
f07ead6a 6725 return -1;
1952b626
BP
6726
6727 os_memset(&event, 0, sizeof(event));
6728 event.wds_sta_interface.sta_addr = addr;
6729 event.wds_sta_interface.ifname = name;
6730 event.wds_sta_interface.istatus = INTERFACE_ADDED;
8bfbb295 6731 wpa_supplicant_event(bss->ctx,
1952b626
BP
6732 EVENT_WDS_STA_INTERFACE_STATUS,
6733 &event);
f07ead6a 6734 }
75227f3a
JM
6735 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6736 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6737 "interface %s up", name);
6738 }
f07ead6a
JM
6739 return i802_set_sta_vlan(priv, addr, name, 0);
6740 } else {
f9052d6e
JM
6741 if (bridge_ifname &&
6742 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6743 name) < 0)
6744 wpa_printf(MSG_INFO,
6745 "nl80211: Failed to remove interface %s from bridge %s: %s",
6746 name, bridge_ifname, strerror(errno));
c34e618d 6747
f07ead6a 6748 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
d0595b25 6749 nl80211_remove_iface(drv, if_nametoindex(name));
1952b626
BP
6750 os_memset(&event, 0, sizeof(event));
6751 event.wds_sta_interface.sta_addr = addr;
6752 event.wds_sta_interface.ifname = name;
6753 event.wds_sta_interface.istatus = INTERFACE_REMOVED;
8bfbb295 6754 wpa_supplicant_event(bss->ctx, EVENT_WDS_STA_INTERFACE_STATUS,
1952b626 6755 &event);
d0595b25 6756 return 0;
f07ead6a
JM
6757 }
6758}
6759
6760
6761static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6762{
6763 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6764 struct sockaddr_ll lladdr;
6765 unsigned char buf[3000];
6766 int len;
6767 socklen_t fromlen = sizeof(lladdr);
6768
6769 len = recvfrom(sock, buf, sizeof(buf), 0,
6770 (struct sockaddr *)&lladdr, &fromlen);
6771 if (len < 0) {
7ac3616d
JM
6772 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6773 strerror(errno));
f07ead6a
JM
6774 return;
6775 }
6776
732b1d20 6777 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
f07ead6a
JM
6778 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6779}
6780
6781
94627f6c 6782static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 6783 struct i802_bss *bss,
94627f6c
JM
6784 const char *brname, const char *ifname)
6785{
6c6678e7 6786 int br_ifindex;
94627f6c
JM
6787 char in_br[IFNAMSIZ];
6788
e17a2477 6789 os_strlcpy(bss->brname, brname, IFNAMSIZ);
6c6678e7 6790 br_ifindex = if_nametoindex(brname);
6c6678e7 6791 if (br_ifindex == 0) {
94627f6c
JM
6792 /*
6793 * Bridge was configured, but the bridge device does
6794 * not exist. Try to add it now.
6795 */
c81eff1a 6796 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
6797 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6798 "bridge interface %s: %s",
6799 brname, strerror(errno));
6800 return -1;
6801 }
e17a2477 6802 bss->added_bridge = 1;
8997613c 6803 br_ifindex = if_nametoindex(brname);
732b1d20 6804 add_ifidx(drv, br_ifindex, drv->ifindex);
94627f6c 6805 }
8997613c 6806 bss->br_ifindex = br_ifindex;
94627f6c
JM
6807
6808 if (linux_br_get(in_br, ifname) == 0) {
c809756f
SM
6809 if (os_strcmp(in_br, brname) == 0) {
6810 bss->already_in_bridge = 1;
94627f6c 6811 return 0; /* already in the bridge */
c809756f 6812 }
94627f6c
JM
6813
6814 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6815 "bridge %s", ifname, in_br);
c81eff1a
BG
6816 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6817 0) {
94627f6c
JM
6818 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6819 "remove interface %s from bridge "
6820 "%s: %s",
fdbfb63e 6821 ifname, in_br, strerror(errno));
94627f6c
JM
6822 return -1;
6823 }
6824 }
6825
6826 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6827 ifname, brname);
c81eff1a 6828 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
6829 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6830 "into bridge %s: %s",
6831 ifname, brname, strerror(errno));
6832 return -1;
6833 }
e17a2477 6834 bss->added_if_into_bridge = 1;
94627f6c
JM
6835
6836 return 0;
6837}
6838
6839
92f475b4
JM
6840static void *i802_init(struct hostapd_data *hapd,
6841 struct wpa_init_params *params)
c5121837
JM
6842{
6843 struct wpa_driver_nl80211_data *drv;
a2e40bb6 6844 struct i802_bss *bss;
c5121837 6845 size_t i;
cb05808c
AN
6846 char master_ifname[IFNAMSIZ];
6847 int ifindex, br_ifindex = 0;
94627f6c 6848 int br_added = 0;
c5121837 6849
0d547d5f
JM
6850 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6851 params->global_priv, 1,
0ecff8d7 6852 params->bssid, params->driver_params);
a2e40bb6 6853 if (bss == NULL)
c5121837 6854 return NULL;
c5121837 6855
a2e40bb6 6856 drv = bss->drv;
7635bfb0 6857
cb05808c 6858 if (linux_br_get(master_ifname, params->ifname) == 0) {
94627f6c 6859 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
cb05808c
AN
6860 params->ifname, master_ifname);
6861 br_ifindex = if_nametoindex(master_ifname);
6862 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6863 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6864 linux_master_get(master_ifname, params->ifname) == 0) {
6865 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6866 params->ifname, master_ifname);
6867 /* start listening for EAPOL on the master interface */
732b1d20 6868 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
f2d6c17a
DL
6869
6870 /* check if master itself is under bridge */
6871 if (linux_br_get(master_ifname, master_ifname) == 0) {
6872 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6873 master_ifname);
6874 br_ifindex = if_nametoindex(master_ifname);
6875 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6876 }
94627f6c 6877 } else {
cb05808c 6878 master_ifname[0] = '\0';
94627f6c 6879 }
cb05808c 6880
6c6678e7 6881 bss->br_ifindex = br_ifindex;
94627f6c 6882
92f475b4 6883 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
6884 if (params->bridge[i]) {
6885 ifindex = if_nametoindex(params->bridge[i]);
6886 if (ifindex)
732b1d20 6887 add_ifidx(drv, ifindex, drv->ifindex);
94627f6c
JM
6888 if (ifindex == br_ifindex)
6889 br_added = 1;
6890 }
c5121837 6891 }
c5121837 6892
ad1e68e6 6893 /* start listening for EAPOL on the default AP interface */
732b1d20 6894 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
ad1e68e6 6895
5ef8e39f
JM
6896 if (params->num_bridge && params->bridge[0]) {
6897 if (i802_check_bridge(drv, bss, params->bridge[0],
6898 params->ifname) < 0)
6899 goto failed;
cb05808c 6900 if (os_strcmp(params->bridge[0], master_ifname) != 0)
5ef8e39f
JM
6901 br_added = 1;
6902 }
6903
6904 if (!br_added && br_ifindex &&
6905 (params->num_bridge == 0 || !params->bridge[0]))
732b1d20 6906 add_ifidx(drv, br_ifindex, drv->ifindex);
94627f6c 6907
97ed9a06 6908#ifdef CONFIG_LIBNL3_ROUTE
c809756f 6909 if (bss->added_if_into_bridge || bss->already_in_bridge) {
97ed9a06
KP
6910 drv->rtnl_sk = nl_socket_alloc();
6911 if (drv->rtnl_sk == NULL) {
6912 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6913 goto failed;
6914 }
6915
6916 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6917 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6918 strerror(errno));
6919 goto failed;
6920 }
6921 }
6922#endif /* CONFIG_LIBNL3_ROUTE */
6923
ad1e68e6
JM
6924 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6925 if (drv->eapol_sock < 0) {
7ac3616d
JM
6926 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6927 strerror(errno));
bbaf0837 6928 goto failed;
ad1e68e6
JM
6929 }
6930
6931 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6932 {
7ac3616d 6933 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
c5121837 6934 goto failed;
ad1e68e6
JM
6935 }
6936
c81eff1a
BG
6937 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6938 params->own_addr))
bbaf0837 6939 goto failed;
fee354c7 6940 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
c5121837 6941
341eebee
JB
6942 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6943
a2e40bb6 6944 return bss;
c5121837
JM
6945
6946failed:
7635bfb0 6947 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
6948 return NULL;
6949}
c5121837 6950
c5121837 6951
bbaf0837
JM
6952static void i802_deinit(void *priv)
6953{
9ebce9c5
JM
6954 struct i802_bss *bss = priv;
6955 wpa_driver_nl80211_deinit(bss);
c5121837
JM
6956}
6957
c5121837 6958
22a7c9d7
JM
6959static enum nl80211_iftype wpa_driver_nl80211_if_type(
6960 enum wpa_driver_if_type type)
6961{
6962 switch (type) {
6963 case WPA_IF_STATION:
9f51b113 6964 return NL80211_IFTYPE_STATION;
75bde05d
JM
6965 case WPA_IF_P2P_CLIENT:
6966 case WPA_IF_P2P_GROUP:
9f51b113 6967 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
6968 case WPA_IF_AP_VLAN:
6969 return NL80211_IFTYPE_AP_VLAN;
6970 case WPA_IF_AP_BSS:
6971 return NL80211_IFTYPE_AP;
9f51b113
JB
6972 case WPA_IF_P2P_GO:
6973 return NL80211_IFTYPE_P2P_GO;
7aad838c
NS
6974 case WPA_IF_P2P_DEVICE:
6975 return NL80211_IFTYPE_P2P_DEVICE;
5b78493f
MH
6976 case WPA_IF_MESH:
6977 return NL80211_IFTYPE_MESH_POINT;
98342208
AK
6978 default:
6979 return -1;
22a7c9d7 6980 }
22a7c9d7
JM
6981}
6982
6983
f2ed8023
JM
6984static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6985{
6986 struct wpa_driver_nl80211_data *drv;
6987 dl_list_for_each(drv, &global->interfaces,
6988 struct wpa_driver_nl80211_data, list) {
834ee56f 6989 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
f2ed8023
JM
6990 return 1;
6991 }
6992 return 0;
6993}
6994
6995
5b78493f 6996static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
f2ed8023
JM
6997{
6998 unsigned int idx;
6999
7000 if (!drv->global)
7001 return -1;
7002
834ee56f 7003 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
f2ed8023 7004 for (idx = 0; idx < 64; idx++) {
834ee56f 7005 new_addr[0] = drv->first_bss->addr[0] | 0x02;
f2ed8023
JM
7006 new_addr[0] ^= idx << 2;
7007 if (!nl80211_addr_in_use(drv->global, new_addr))
7008 break;
7009 }
7010 if (idx == 64)
7011 return -1;
7012
5b78493f 7013 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
f2ed8023
JM
7014 MACSTR, MAC2STR(new_addr));
7015
7016 return 0;
7017}
7018
7019
f632e483
AS
7020struct wdev_info {
7021 u64 wdev_id;
7022 int wdev_id_set;
7023 u8 macaddr[ETH_ALEN];
7024};
7025
7026static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
e472e1b4
AS
7027{
7028 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7029 struct nlattr *tb[NL80211_ATTR_MAX + 1];
f632e483 7030 struct wdev_info *wi = arg;
e472e1b4
AS
7031
7032 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7033 genlmsg_attrlen(gnlh, 0), NULL);
7034 if (tb[NL80211_ATTR_WDEV]) {
f632e483
AS
7035 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
7036 wi->wdev_id_set = 1;
e472e1b4
AS
7037 }
7038
7039 if (tb[NL80211_ATTR_MAC])
f632e483 7040 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
e472e1b4
AS
7041 ETH_ALEN);
7042
7043 return NL_SKIP;
7044}
7045
7046
7ab68865 7047static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 7048 const char *ifname, const u8 *addr,
f3585c8a 7049 void *bss_ctx, void **drv_priv,
e17a2477 7050 char *force_ifname, u8 *if_addr,
d8a3b66d
AS
7051 const char *bridge, int use_existing,
7052 int setup_ap)
22a7c9d7 7053{
e472e1b4 7054 enum nl80211_iftype nlmode;
a2e40bb6
FF
7055 struct i802_bss *bss = priv;
7056 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7 7057 int ifidx;
2aec4f3c 7058 int added = 1;
22a7c9d7 7059
f3585c8a
JM
7060 if (addr)
7061 os_memcpy(if_addr, addr, ETH_ALEN);
e472e1b4
AS
7062 nlmode = wpa_driver_nl80211_if_type(type);
7063 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
f632e483
AS
7064 struct wdev_info p2pdev_info;
7065
7066 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
e472e1b4 7067 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
f632e483 7068 0, nl80211_wdev_handler,
2aec4f3c 7069 &p2pdev_info, use_existing);
f632e483 7070 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
e472e1b4
AS
7071 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
7072 ifname);
e472e1b4
AS
7073 return -1;
7074 }
f632e483
AS
7075
7076 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
7077 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
7078 if (!is_zero_ether_addr(p2pdev_info.macaddr))
7079 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
7080 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
7081 ifname,
7082 (long long unsigned int) p2pdev_info.wdev_id);
e472e1b4
AS
7083 } else {
7084 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
2aec4f3c
JM
7085 0, NULL, NULL, use_existing);
7086 if (use_existing && ifidx == -ENFILE) {
7087 added = 0;
7088 ifidx = if_nametoindex(ifname);
7089 } else if (ifidx < 0) {
e472e1b4
AS
7090 return -1;
7091 }
22a7c9d7
JM
7092 }
7093
ab7a1add 7094 if (!addr) {
8ea8a89c 7095 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
ab7a1add
AS
7096 os_memcpy(if_addr, bss->addr, ETH_ALEN);
7097 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
8ea8a89c 7098 ifname, if_addr) < 0) {
2aec4f3c
JM
7099 if (added)
7100 nl80211_remove_iface(drv, ifidx);
ab7a1add
AS
7101 return -1;
7102 }
c55f774d
JM
7103 }
7104
c55f774d
JM
7105 if (!addr &&
7106 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8ea8a89c
JM
7107 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
7108 type == WPA_IF_STATION)) {
7109 /* Enforce unique address */
ab7a1add 7110 u8 new_addr[ETH_ALEN];
c55f774d 7111
ab7a1add 7112 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
c81eff1a 7113 new_addr) < 0) {
ea39367c
MK
7114 if (added)
7115 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
7116 return -1;
7117 }
f608081c 7118 if (nl80211_addr_in_use(drv->global, new_addr)) {
c55f774d 7119 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8ea8a89c 7120 "for interface %s type %d", ifname, type);
5b78493f 7121 if (nl80211_vif_addr(drv, new_addr) < 0) {
ea39367c
MK
7122 if (added)
7123 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
7124 return -1;
7125 }
c81eff1a 7126 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d 7127 new_addr) < 0) {
ea39367c
MK
7128 if (added)
7129 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
7130 return -1;
7131 }
c55f774d 7132 }
f67eeb5c 7133 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d 7134 }
f3585c8a 7135
d8a3b66d 7136 if (type == WPA_IF_AP_BSS && setup_ap) {
f5eb9da3
JM
7137 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
7138 if (new_bss == NULL) {
2aec4f3c
JM
7139 if (added)
7140 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
7141 return -1;
7142 }
7143
7144 if (bridge &&
7145 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7146 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7147 "interface %s to a bridge %s",
7148 ifname, bridge);
2aec4f3c
JM
7149 if (added)
7150 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
7151 os_free(new_bss);
7152 return -1;
7153 }
7154
c81eff1a
BG
7155 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7156 {
ea39367c
MK
7157 if (added)
7158 nl80211_remove_iface(drv, ifidx);
07179987 7159 os_free(new_bss);
22a7c9d7
JM
7160 return -1;
7161 }
a2e40bb6 7162 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 7163 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
7164 new_bss->ifindex = ifidx;
7165 new_bss->drv = drv;
834ee56f
KP
7166 new_bss->next = drv->first_bss->next;
7167 new_bss->freq = drv->first_bss->freq;
a5e1eb20 7168 new_bss->ctx = bss_ctx;
2aec4f3c 7169 new_bss->added_if = added;
834ee56f 7170 drv->first_bss->next = new_bss;
a2e40bb6
FF
7171 if (drv_priv)
7172 *drv_priv = new_bss;
cc7a48d1 7173 nl80211_init_bss(new_bss);
3dd1d890
YAP
7174
7175 /* Subscribe management frames for this WPA_IF_AP_BSS */
7176 if (nl80211_setup_ap(new_bss))
7177 return -1;
22a7c9d7 7178 }
22a7c9d7 7179
ff6a158b
JM
7180 if (drv->global)
7181 drv->global->if_add_ifindex = ifidx;
7182
d1bb7aed
JJ
7183 /*
7184 * Some virtual interfaces need to process EAPOL packets and events on
7185 * the parent interface. This is used mainly with hostapd.
7186 */
7187 if (ifidx > 0 &&
7188 (drv->hostapd ||
7189 nlmode == NL80211_IFTYPE_AP_VLAN ||
7190 nlmode == NL80211_IFTYPE_WDS ||
7191 nlmode == NL80211_IFTYPE_MONITOR))
732b1d20 7192 add_ifidx(drv, ifidx, IFIDX_ANY);
b36935be 7193
22a7c9d7
JM
7194 return 0;
7195}
7196
7197
9ebce9c5 7198static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
7199 enum wpa_driver_if_type type,
7200 const char *ifname)
7201{
a2e40bb6 7202 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
7203 int ifindex = if_nametoindex(ifname);
7204
2aec4f3c
JM
7205 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
7206 __func__, type, ifname, ifindex, bss->added_if);
158b090c 7207 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
2b72df63 7208 nl80211_remove_iface(drv, ifindex);
de884303
JM
7209 else if (ifindex > 0 && !bss->added_if) {
7210 struct wpa_driver_nl80211_data *drv2;
7211 dl_list_for_each(drv2, &drv->global->interfaces,
732b1d20
MB
7212 struct wpa_driver_nl80211_data, list) {
7213 del_ifidx(drv2, ifindex, IFIDX_ANY);
7214 del_ifidx(drv2, IFIDX_ANY, ifindex);
7215 }
de884303 7216 }
c34e618d 7217
c34e618d
FF
7218 if (type != WPA_IF_AP_BSS)
7219 return 0;
7220
e17a2477 7221 if (bss->added_if_into_bridge) {
c81eff1a
BG
7222 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7223 bss->ifname) < 0)
e17a2477
JM
7224 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7225 "interface %s from bridge %s: %s",
7226 bss->ifname, bss->brname, strerror(errno));
7227 }
7228 if (bss->added_bridge) {
c81eff1a 7229 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
7230 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7231 "bridge %s: %s",
7232 bss->brname, strerror(errno));
7233 }
a2e40bb6 7234
834ee56f 7235 if (bss != drv->first_bss) {
8546ea19 7236 struct i802_bss *tbss;
a2e40bb6 7237
2aec4f3c 7238 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
834ee56f 7239 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
8546ea19
JM
7240 if (tbss->next == bss) {
7241 tbss->next = bss->next;
3dd1d890
YAP
7242 /* Unsubscribe management frames */
7243 nl80211_teardown_ap(bss);
cc7a48d1 7244 nl80211_destroy_bss(bss);
5c9da160
MB
7245 if (!bss->added_if)
7246 i802_set_iface_flags(bss, 0);
8546ea19
JM
7247 os_free(bss);
7248 bss = NULL;
7249 break;
7250 }
22a7c9d7 7251 }
8546ea19
JM
7252 if (bss)
7253 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7254 "BSS %p in the list", __func__, bss);
390e489c 7255 } else {
2aec4f3c 7256 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
390e489c 7257 nl80211_teardown_ap(bss);
2aec4f3c 7258 if (!bss->added_if && !drv->first_bss->next)
a70cd0db 7259 wpa_driver_nl80211_del_beacon(bss);
390e489c 7260 nl80211_destroy_bss(bss);
2aec4f3c
JM
7261 if (!bss->added_if)
7262 i802_set_iface_flags(bss, 0);
390e489c
KP
7263 if (drv->first_bss->next) {
7264 drv->first_bss = drv->first_bss->next;
7265 drv->ctx = drv->first_bss->ctx;
7266 os_free(bss);
7267 } else {
7268 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7269 }
22a7c9d7 7270 }
22a7c9d7
JM
7271
7272 return 0;
7273}
7274
7275
55777702
JM
7276static int cookie_handler(struct nl_msg *msg, void *arg)
7277{
7278 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7279 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7280 u64 *cookie = arg;
7281 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7282 genlmsg_attrlen(gnlh, 0), NULL);
7283 if (tb[NL80211_ATTR_COOKIE])
7284 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7285 return NL_SKIP;
7286}
7287
7288
88df0ef7 7289static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
7290 unsigned int freq, unsigned int wait,
7291 const u8 *buf, size_t buf_len,
88df0ef7 7292 u64 *cookie_out, int no_cck, int no_ack,
2d3943ce
AO
7293 int offchanok, const u16 *csa_offs,
7294 size_t csa_offs_len)
9884f9cc 7295{
88df0ef7 7296 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
7297 struct nl_msg *msg;
7298 u64 cookie;
7299 int ret = -1;
7300
cc2ada86 7301 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
2e3e4566
JM
7302 "no_ack=%d offchanok=%d",
7303 freq, wait, no_cck, no_ack, offchanok);
c91f796f 7304 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
9884f9cc 7305
56f77852 7306 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
a862e4a3
JM
7307 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7308 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7309 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7310 drv->test_use_roc_tx) &&
7311 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7312 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7313 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
2d3943ce
AO
7314 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7315 csa_offs_len * sizeof(u16), csa_offs)) ||
a862e4a3
JM
7316 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7317 goto fail;
9884f9cc
JB
7318
7319 cookie = 0;
7320 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7321 msg = NULL;
7322 if (ret) {
7323 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
7324 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7325 freq, wait);
a862e4a3
JM
7326 } else {
7327 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7328 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7329 (long long unsigned int) cookie);
9884f9cc 7330
a862e4a3
JM
7331 if (cookie_out)
7332 *cookie_out = no_ack ? (u64) -1 : cookie;
759a8a3a
JM
7333
7334 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7335 wpa_printf(MSG_DEBUG,
7336 "nl80211: Drop oldest pending send action cookie 0x%llx",
7337 (long long unsigned int)
7338 drv->send_action_cookies[0]);
7339 os_memmove(&drv->send_action_cookies[0],
7340 &drv->send_action_cookies[1],
7341 (MAX_SEND_ACTION_COOKIES - 1) *
7342 sizeof(u64));
7343 drv->num_send_action_cookies--;
7344 }
7345 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7346 drv->num_send_action_cookies++;
a862e4a3 7347 }
9884f9cc 7348
a862e4a3 7349fail:
9884f9cc
JB
7350 nlmsg_free(msg);
7351 return ret;
7352}
7353
7354
9ebce9c5
JM
7355static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7356 unsigned int freq,
190b9062 7357 unsigned int wait_time,
58f6fbe0
JM
7358 const u8 *dst, const u8 *src,
7359 const u8 *bssid,
b106173a
JM
7360 const u8 *data, size_t data_len,
7361 int no_cck)
58f6fbe0 7362{
a2e40bb6 7363 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 7364 int ret = -1;
58f6fbe0
JM
7365 u8 *buf;
7366 struct ieee80211_hdr *hdr;
58f6fbe0 7367
5dfca53f 7368 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
7369 "freq=%u MHz wait=%d ms no_cck=%d)",
7370 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
7371
7372 buf = os_zalloc(24 + data_len);
7373 if (buf == NULL)
7374 return ret;
7375 os_memcpy(buf + 24, data, data_len);
7376 hdr = (struct ieee80211_hdr *) buf;
7377 hdr->frame_control =
7378 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7379 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7380 os_memcpy(hdr->addr2, src, ETH_ALEN);
7381 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7382
8331c9b3
VK
7383 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7384 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7385 MAC2STR(src));
7386 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7387 } else {
7388 os_memset(bss->rand_addr, 0, ETH_ALEN);
7389 }
7390
f78f2785
JM
7391 if (is_ap_interface(drv->nlmode) &&
7392 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7393 (int) freq == bss->freq || drv->device_ap_sme ||
7394 !drv->use_monitor))
9ebce9c5
JM
7395 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7396 0, freq, no_cck, 1,
2d3943ce 7397 wait_time, NULL, 0);
9884f9cc 7398 else
88df0ef7 7399 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 7400 24 + data_len,
b106173a 7401 &drv->send_action_cookie,
2d3943ce 7402 no_cck, 0, 1, NULL, 0);
58f6fbe0 7403
f8bf1421 7404 os_free(buf);
58f6fbe0
JM
7405 return ret;
7406}
7407
7408
759a8a3a 7409static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
5dfca53f 7410{
5dfca53f
JB
7411 struct wpa_driver_nl80211_data *drv = bss->drv;
7412 struct nl_msg *msg;
7413 int ret;
7414
316a9e4d 7415 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
759a8a3a 7416 (long long unsigned int) cookie);
56f77852 7417 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
759a8a3a 7418 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
a862e4a3
JM
7419 nlmsg_free(msg);
7420 return;
7421 }
5dfca53f
JB
7422
7423 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5dfca53f
JB
7424 if (ret)
7425 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7426 "(%s)", ret, strerror(-ret));
5dfca53f
JB
7427}
7428
7429
759a8a3a
JM
7430static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7431{
7432 struct i802_bss *bss = priv;
7433 struct wpa_driver_nl80211_data *drv = bss->drv;
7434 unsigned int i;
7435 u64 cookie;
7436
7437 /* Cancel the last pending TX cookie */
7438 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7439
7440 /*
7441 * Cancel the other pending TX cookies, if any. This is needed since
7442 * the driver may keep a list of all pending offchannel TX operations
7443 * and free up the radio only once they have expired or cancelled.
7444 */
7445 for (i = drv->num_send_action_cookies; i > 0; i--) {
7446 cookie = drv->send_action_cookies[i - 1];
7447 if (cookie != drv->send_action_cookie)
7448 nl80211_frame_wait_cancel(bss, cookie);
7449 }
7450 drv->num_send_action_cookies = 0;
7451}
7452
7453
55777702
JM
7454static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7455 unsigned int duration)
7456{
a2e40bb6
FF
7457 struct i802_bss *bss = priv;
7458 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
7459 struct nl_msg *msg;
7460 int ret;
7461 u64 cookie;
7462
56f77852 7463 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
a862e4a3
JM
7464 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7465 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7466 nlmsg_free(msg);
7467 return -1;
7468 }
55777702
JM
7469
7470 cookie = 0;
7471 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7472 if (ret == 0) {
7473 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7474 "0x%llx for freq=%u MHz duration=%u",
7475 (long long unsigned int) cookie, freq, duration);
7476 drv->remain_on_chan_cookie = cookie;
531f0331 7477 drv->pending_remain_on_chan = 1;
55777702
JM
7478 return 0;
7479 }
7480 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
7481 "(freq=%d duration=%u): %d (%s)",
7482 freq, duration, ret, strerror(-ret));
55777702
JM
7483 return -1;
7484}
7485
7486
7487static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7488{
a2e40bb6
FF
7489 struct i802_bss *bss = priv;
7490 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
7491 struct nl_msg *msg;
7492 int ret;
7493
7494 if (!drv->pending_remain_on_chan) {
7495 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7496 "to cancel");
7497 return -1;
7498 }
7499
7500 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7501 "0x%llx",
7502 (long long unsigned int) drv->remain_on_chan_cookie);
7503
56f77852
JM
7504 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7505 if (!msg ||
a862e4a3
JM
7506 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7507 nlmsg_free(msg);
7508 return -1;
7509 }
55777702
JM
7510
7511 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7512 if (ret == 0)
7513 return 0;
7514 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7515 "%d (%s)", ret, strerror(-ret));
55777702
JM
7516 return -1;
7517}
7518
7519
9ebce9c5 7520static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 7521{
a2e40bb6 7522 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 7523
5582a5d1 7524 if (!report) {
0d891981 7525 if (bss->nl_preq && drv->device_ap_sme &&
b8d87ed2
AP
7526 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7527 !bss->static_ap) {
0d891981
JM
7528 /*
7529 * Do not disable Probe Request reporting that was
7530 * enabled in nl80211_setup_ap().
7531 */
7532 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7533 "Probe Request reporting nl_preq=%p while "
7534 "in AP mode", bss->nl_preq);
7535 } else if (bss->nl_preq) {
36488c05
JM
7536 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7537 "reporting nl_preq=%p", bss->nl_preq);
40a68f33 7538 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
5582a5d1
JB
7539 }
7540 return 0;
7541 }
7542
481234cf 7543 if (bss->nl_preq) {
5582a5d1 7544 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 7545 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
7546 return 0;
7547 }
7548
481234cf
JM
7549 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7550 if (bss->nl_preq == NULL)
5582a5d1 7551 return -1;
36488c05
JM
7552 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7553 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 7554
481234cf 7555 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
7556 (WLAN_FC_TYPE_MGMT << 2) |
7557 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
7558 NULL, 0) < 0)
7559 goto out_err;
5582a5d1 7560
5f65e9f7
JB
7561 nl80211_register_eloop_read(&bss->nl_preq,
7562 wpa_driver_nl80211_event_receive,
40a68f33 7563 bss->nl_cb, 0);
5582a5d1 7564
504e905c 7565 return 0;
5582a5d1 7566
a92dfde8 7567 out_err:
221a59c9 7568 nl_destroy_handles(&bss->nl_preq);
5582a5d1 7569 return -1;
504e905c
JM
7570}
7571
7572
4e5cb1a3
JM
7573static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7574 int ifindex, int disabled)
7575{
7576 struct nl_msg *msg;
7577 struct nlattr *bands, *band;
7578 int ret;
7579
3e208481
JM
7580 wpa_printf(MSG_DEBUG,
7581 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7582 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7583 "no NL80211_TXRATE_LEGACY constraint");
7584
95376e1a
JM
7585 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7586 NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
7587 if (!msg)
7588 return -1;
7589
4e5cb1a3
JM
7590 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7591 if (!bands)
a862e4a3 7592 goto fail;
4e5cb1a3
JM
7593
7594 /*
7595 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7596 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7597 * rates. All 5 GHz rates are left enabled.
7598 */
7599 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
a862e4a3
JM
7600 if (!band ||
7601 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7602 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7603 goto fail;
4e5cb1a3
JM
7604 nla_nest_end(msg, band);
7605
7606 nla_nest_end(msg, bands);
7607
7608 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4e5cb1a3
JM
7609 if (ret) {
7610 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7611 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
7612 } else
7613 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
7614
7615 return ret;
7616
a862e4a3 7617fail:
4e5cb1a3
JM
7618 nlmsg_free(msg);
7619 return -1;
7620}
7621
7622
af473088
JM
7623static int wpa_driver_nl80211_deinit_ap(void *priv)
7624{
a2e40bb6
FF
7625 struct i802_bss *bss = priv;
7626 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 7627 if (!is_ap_interface(drv->nlmode))
af473088 7628 return -1;
a70cd0db 7629 wpa_driver_nl80211_del_beacon(bss);
9bc5cfa3 7630 bss->beacon_set = 0;
60b13c20
IP
7631
7632 /*
7633 * If the P2P GO interface was dynamically added, then it is
7634 * possible that the interface change to station is not possible.
7635 */
7636 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7637 return 0;
7638
b1f625e0 7639 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
7640}
7641
7642
695c7038
SW
7643static int wpa_driver_nl80211_stop_ap(void *priv)
7644{
7645 struct i802_bss *bss = priv;
7646 struct wpa_driver_nl80211_data *drv = bss->drv;
7647 if (!is_ap_interface(drv->nlmode))
7648 return -1;
a70cd0db 7649 wpa_driver_nl80211_del_beacon(bss);
695c7038
SW
7650 bss->beacon_set = 0;
7651 return 0;
7652}
7653
7654
3c29244e
EP
7655static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7656{
7657 struct i802_bss *bss = priv;
7658 struct wpa_driver_nl80211_data *drv = bss->drv;
7659 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7660 return -1;
60b13c20
IP
7661
7662 /*
7663 * If the P2P Client interface was dynamically added, then it is
7664 * possible that the interface change to station is not possible.
7665 */
7666 if (bss->if_dynamic)
7667 return 0;
7668
3c29244e
EP
7669 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7670}
7671
7672
207ef3fb
JM
7673static void wpa_driver_nl80211_resume(void *priv)
7674{
a2e40bb6 7675 struct i802_bss *bss = priv;
e8dc205f 7676 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
91724d6f
AS
7677
7678 if (i802_set_iface_flags(bss, 1))
7679 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
e8dc205f
AO
7680
7681 if (is_p2p_net_interface(nlmode))
7682 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
207ef3fb
JM
7683}
7684
7685
b625473c
JM
7686static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7687{
7688 struct i802_bss *bss = priv;
7689 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
7690 struct nl_msg *msg;
7691 struct nlattr *cqm;
b625473c
JM
7692
7693 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7694 "hysteresis=%d", threshold, hysteresis);
7695
13f83980 7696 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
a862e4a3
JM
7697 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7698 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7699 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7700 nlmsg_free(msg);
7701 return -1;
7702 }
8970bae8 7703 nla_nest_end(msg, cqm);
21270bb4 7704
a862e4a3 7705 return send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
7706}
7707
7708
2cc8d8f4
AO
7709static int get_channel_width(struct nl_msg *msg, void *arg)
7710{
7711 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7712 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7713 struct wpa_signal_info *sig_change = arg;
7714
7715 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7716 genlmsg_attrlen(gnlh, 0), NULL);
7717
7718 sig_change->center_frq1 = -1;
7719 sig_change->center_frq2 = -1;
7720 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7721
7722 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7723 sig_change->chanwidth = convert2width(
7724 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7725 if (tb[NL80211_ATTR_CENTER_FREQ1])
7726 sig_change->center_frq1 =
7727 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7728 if (tb[NL80211_ATTR_CENTER_FREQ2])
7729 sig_change->center_frq2 =
7730 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7731 }
7732
7733 return NL_SKIP;
7734}
7735
7736
7737static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7738 struct wpa_signal_info *sig)
7739{
7740 struct nl_msg *msg;
7741
9725b784 7742 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
2cc8d8f4 7743 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
2cc8d8f4
AO
7744}
7745
7746
1c5c7273
PS
7747static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7748{
7749 struct i802_bss *bss = priv;
7750 struct wpa_driver_nl80211_data *drv = bss->drv;
7751 int res;
7752
7753 os_memset(si, 0, sizeof(*si));
7754 res = nl80211_get_link_signal(drv, si);
1d6955e6
JM
7755 if (res) {
7756 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7757 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7758 return res;
7759 si->current_signal = 0;
7760 }
1c5c7273 7761
2cc8d8f4
AO
7762 res = nl80211_get_channel_width(drv, si);
7763 if (res != 0)
7764 return res;
7765
1c5c7273
PS
7766 return nl80211_get_link_noise(drv, si);
7767}
7768
7769
b91ab76e
JM
7770static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7771 int encrypt)
7772{
7773 struct i802_bss *bss = priv;
55231068 7774 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
2d3943ce 7775 0, 0, 0, 0, NULL, 0);
b91ab76e
JM
7776}
7777
7778
c55f774d
JM
7779static int nl80211_set_param(void *priv, const char *param)
7780{
ef24ad3e
JM
7781 struct i802_bss *bss = priv;
7782 struct wpa_driver_nl80211_data *drv = bss->drv;
7783
c55f774d
JM
7784 if (param == NULL)
7785 return 0;
99a94f55 7786 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
c55f774d
JM
7787
7788#ifdef CONFIG_P2P
7789 if (os_strstr(param, "use_p2p_group_interface=1")) {
7790 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7791 "interface");
7792 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7793 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7794 }
7795#endif /* CONFIG_P2P */
7796
ef24ad3e 7797 if (os_strstr(param, "use_monitor=1"))
327b01d3 7798 drv->use_monitor = 1;
327b01d3
JM
7799
7800 if (os_strstr(param, "force_connect_cmd=1")) {
327b01d3 7801 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
b497a212 7802 drv->force_connect_cmd = 1;
327b01d3
JM
7803 }
7804
ef24ad3e 7805 if (os_strstr(param, "force_bss_selection=1"))
4d584d8c 7806 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
4d584d8c 7807
64abb725 7808 if (os_strstr(param, "no_offchannel_tx=1")) {
64abb725
JM
7809 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7810 drv->test_use_roc_tx = 1;
7811 }
7812
c55f774d
JM
7813 return 0;
7814}
7815
7816
45e3fc72 7817static void * nl80211_global_init(void *ctx)
f2ed8023
JM
7818{
7819 struct nl80211_global *global;
36d84860
BG
7820 struct netlink_config *cfg;
7821
f2ed8023
JM
7822 global = os_zalloc(sizeof(*global));
7823 if (global == NULL)
7824 return NULL;
45e3fc72 7825 global->ctx = ctx;
c81eff1a 7826 global->ioctl_sock = -1;
f2ed8023 7827 dl_list_init(&global->interfaces);
ff6a158b 7828 global->if_add_ifindex = -1;
36d84860
BG
7829
7830 cfg = os_zalloc(sizeof(*cfg));
7831 if (cfg == NULL)
7832 goto err;
7833
7834 cfg->ctx = global;
7835 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7836 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7837 global->netlink = netlink_init(cfg);
7838 if (global->netlink == NULL) {
7839 os_free(cfg);
7840 goto err;
7841 }
7842
2a7b66f5
BG
7843 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7844 goto err;
7845
c81eff1a
BG
7846 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7847 if (global->ioctl_sock < 0) {
7ac3616d
JM
7848 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7849 strerror(errno));
c81eff1a
BG
7850 goto err;
7851 }
7852
f2ed8023 7853 return global;
36d84860
BG
7854
7855err:
7856 nl80211_global_deinit(global);
7857 return NULL;
f2ed8023
JM
7858}
7859
7860
7861static void nl80211_global_deinit(void *priv)
7862{
7863 struct nl80211_global *global = priv;
7864 if (global == NULL)
7865 return;
7866 if (!dl_list_empty(&global->interfaces)) {
7867 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7868 "nl80211_global_deinit",
7869 dl_list_len(&global->interfaces));
7870 }
36d84860
BG
7871
7872 if (global->netlink)
7873 netlink_deinit(global->netlink);
7874
276e2d67
BG
7875 nl_destroy_handles(&global->nl);
7876
5f65e9f7 7877 if (global->nl_event)
40a68f33 7878 nl80211_destroy_eloop_handle(&global->nl_event, 0);
d6c9aab8
JB
7879
7880 nl_cb_put(global->nl_cb);
2a7b66f5 7881
c81eff1a
BG
7882 if (global->ioctl_sock >= 0)
7883 close(global->ioctl_sock);
7884
f2ed8023
JM
7885 os_free(global);
7886}
7887
7888
6859f1cb
BG
7889static const char * nl80211_get_radio_name(void *priv)
7890{
7891 struct i802_bss *bss = priv;
7892 struct wpa_driver_nl80211_data *drv = bss->drv;
7893 return drv->phyname;
7894}
7895
7896
061a3d3d
VK
7897static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7898 struct wpa_pmkid_params *params)
a6efc65d
JM
7899{
7900 struct nl_msg *msg;
0887215d 7901 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
a6efc65d 7902
13f83980 7903 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
061a3d3d
VK
7904 (params->pmkid &&
7905 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7906 (params->bssid &&
7907 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7908 (params->ssid_len &&
7909 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7910 (params->fils_cache_id &&
7911 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7912 params->fils_cache_id)) ||
0887215d 7913 (params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
061a3d3d 7914 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
a862e4a3
JM
7915 nlmsg_free(msg);
7916 return -ENOBUFS;
7917 }
a6efc65d
JM
7918
7919 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a6efc65d
JM
7920}
7921
7922
6fbb5414 7923static int nl80211_add_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: Add PMKID for " MACSTR,
7930 MAC2STR(params->bssid));
7931 else if (params->fils_cache_id && params->ssid_len) {
7932 wpa_printf(MSG_DEBUG,
7933 "nl80211: Add 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_SET_PMKSA, params);
7939 if (ret < 0) {
7940 wpa_printf(MSG_DEBUG,
7941 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7942 ret, strerror(-ret));
7943 }
7944
7945 return ret;
a6efc65d
JM
7946}
7947
7948
6fbb5414 7949static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
a6efc65d
JM
7950{
7951 struct i802_bss *bss = priv;
e7f6e6ee 7952 int ret;
061a3d3d
VK
7953
7954 if (params->bssid)
7955 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7956 MAC2STR(params->bssid));
7957 else if (params->fils_cache_id && params->ssid_len) {
7958 wpa_printf(MSG_DEBUG,
7959 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7960 params->fils_cache_id[0], params->fils_cache_id[1],
7961 wpa_ssid_txt(params->ssid, params->ssid_len));
7962 }
7963
e7f6e6ee
JM
7964 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7965 if (ret < 0) {
7966 wpa_printf(MSG_DEBUG,
7967 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
7968 ret, strerror(-ret));
7969 }
7970
7971 return ret;
a6efc65d
JM
7972}
7973
7974
7975static int nl80211_flush_pmkid(void *priv)
7976{
7977 struct i802_bss *bss = priv;
061a3d3d
VK
7978 struct nl_msg *msg;
7979
a6efc65d 7980 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
061a3d3d
VK
7981 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
7982 if (!msg)
7983 return -ENOBUFS;
7984 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a6efc65d
JM
7985}
7986
7987
0185007c
MK
7988static void clean_survey_results(struct survey_results *survey_results)
7989{
7990 struct freq_survey *survey, *tmp;
7991
7992 if (dl_list_empty(&survey_results->survey_list))
7993 return;
7994
7995 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7996 struct freq_survey, list) {
7997 dl_list_del(&survey->list);
7998 os_free(survey);
7999 }
8000}
8001
8002
8003static void add_survey(struct nlattr **sinfo, u32 ifidx,
8004 struct dl_list *survey_list)
8005{
8006 struct freq_survey *survey;
8007
8008 survey = os_zalloc(sizeof(struct freq_survey));
8009 if (!survey)
8010 return;
8011
8012 survey->ifidx = ifidx;
8013 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8014 survey->filled = 0;
8015
8016 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
8017 survey->nf = (int8_t)
8018 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
8019 survey->filled |= SURVEY_HAS_NF;
8020 }
8021
8022 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
8023 survey->channel_time =
8024 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
8025 survey->filled |= SURVEY_HAS_CHAN_TIME;
8026 }
8027
8028 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
8029 survey->channel_time_busy =
8030 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
8031 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
8032 }
8033
8034 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
8035 survey->channel_time_rx =
8036 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
8037 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
8038 }
8039
8040 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
8041 survey->channel_time_tx =
8042 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
8043 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
8044 }
8045
8046 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)",
8047 survey->freq,
8048 survey->nf,
8049 (unsigned long int) survey->channel_time,
8050 (unsigned long int) survey->channel_time_busy,
8051 (unsigned long int) survey->channel_time_tx,
8052 (unsigned long int) survey->channel_time_rx,
8053 survey->filled);
8054
8055 dl_list_add_tail(survey_list, &survey->list);
8056}
8057
8058
8059static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
8060 unsigned int freq_filter)
8061{
8062 if (!freq_filter)
8063 return 1;
8064
8065 return freq_filter == surveyed_freq;
8066}
8067
8068
8069static int survey_handler(struct nl_msg *msg, void *arg)
8070{
8071 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8072 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8073 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
8074 struct survey_results *survey_results;
8075 u32 surveyed_freq = 0;
8076 u32 ifidx;
8077
8078 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
8079 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
8080 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
8081 };
8082
8083 survey_results = (struct survey_results *) arg;
8084
8085 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8086 genlmsg_attrlen(gnlh, 0), NULL);
8087
e28f39b7
SJ
8088 if (!tb[NL80211_ATTR_IFINDEX])
8089 return NL_SKIP;
8090
0185007c
MK
8091 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
8092
8093 if (!tb[NL80211_ATTR_SURVEY_INFO])
8094 return NL_SKIP;
8095
8096 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
8097 tb[NL80211_ATTR_SURVEY_INFO],
8098 survey_policy))
8099 return NL_SKIP;
8100
8101 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
8102 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
8103 return NL_SKIP;
8104 }
8105
8106 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
8107
8108 if (!check_survey_ok(sinfo, surveyed_freq,
8109 survey_results->freq_filter))
8110 return NL_SKIP;
8111
8112 if (survey_results->freq_filter &&
8113 survey_results->freq_filter != surveyed_freq) {
8114 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
8115 surveyed_freq);
8116 return NL_SKIP;
8117 }
8118
8119 add_survey(sinfo, ifidx, &survey_results->survey_list);
8120
8121 return NL_SKIP;
8122}
8123
8124
8125static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
8126{
8127 struct i802_bss *bss = priv;
8128 struct wpa_driver_nl80211_data *drv = bss->drv;
8129 struct nl_msg *msg;
9725b784 8130 int err;
0185007c
MK
8131 union wpa_event_data data;
8132 struct survey_results *survey_results;
8133
8134 os_memset(&data, 0, sizeof(data));
8135 survey_results = &data.survey_results;
8136
8137 dl_list_init(&survey_results->survey_list);
8138
9725b784 8139 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
0185007c 8140 if (!msg)
a862e4a3 8141 return -ENOBUFS;
0185007c 8142
0185007c
MK
8143 if (freq)
8144 data.survey_results.freq_filter = freq;
8145
8146 do {
8147 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
8148 err = send_and_recv_msgs(drv, msg, survey_handler,
8149 survey_results);
8150 } while (err > 0);
8151
a862e4a3 8152 if (err)
0185007c 8153 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
a862e4a3
JM
8154 else
8155 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
0185007c 8156
0185007c 8157 clean_survey_results(survey_results);
0185007c
MK
8158 return err;
8159}
8160
8161
98cd3d1c
JM
8162static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
8163 const u8 *kck, size_t kck_len,
b14a210c
JB
8164 const u8 *replay_ctr)
8165{
8166 struct i802_bss *bss = priv;
8167 struct wpa_driver_nl80211_data *drv = bss->drv;
8168 struct nlattr *replay_nested;
8169 struct nl_msg *msg;
64ae2447 8170 int ret;
b14a210c 8171
64ae2447
JM
8172 if (!drv->set_rekey_offload)
8173 return;
8174
8175 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
13f83980 8176 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
a862e4a3 8177 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
98cd3d1c 8178 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
b6ea7642 8179 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
a862e4a3
JM
8180 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8181 replay_ctr)) {
bbd89bfc 8182 nl80211_nlmsg_clear(msg);
a862e4a3
JM
8183 nlmsg_free(msg);
8184 return;
8185 }
b14a210c
JB
8186
8187 nla_nest_end(msg, replay_nested);
8188
64ae2447
JM
8189 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
8190 if (ret == -EOPNOTSUPP) {
8191 wpa_printf(MSG_DEBUG,
8192 "nl80211: Driver does not support rekey offload");
8193 drv->set_rekey_offload = 0;
8194 }
b14a210c
JB
8195}
8196
8197
39718852
JB
8198static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8199 const u8 *addr, int qos)
bcf24348 8200{
39718852
JB
8201 /* send data frame to poll STA and check whether
8202 * this frame is ACKed */
bcf24348
JB
8203 struct {
8204 struct ieee80211_hdr hdr;
8205 u16 qos_ctl;
8206 } STRUCT_PACKED nulldata;
8207 size_t size;
8208
8209 /* Send data frame to poll STA and check whether this frame is ACKed */
8210
8211 os_memset(&nulldata, 0, sizeof(nulldata));
8212
8213 if (qos) {
8214 nulldata.hdr.frame_control =
8215 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8216 WLAN_FC_STYPE_QOS_NULL);
8217 size = sizeof(nulldata);
8218 } else {
8219 nulldata.hdr.frame_control =
8220 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8221 WLAN_FC_STYPE_NULLFUNC);
8222 size = sizeof(struct ieee80211_hdr);
8223 }
8224
8225 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8226 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8227 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8228 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8229
9ebce9c5 8230 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
2d3943ce 8231 0, 0, NULL, 0) < 0)
bcf24348
JB
8232 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8233 "send poll frame");
8234}
8235
39718852
JB
8236static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8237 int qos)
8238{
8239 struct i802_bss *bss = priv;
8240 struct wpa_driver_nl80211_data *drv = bss->drv;
8241 struct nl_msg *msg;
fc99fab7 8242 int ret;
39718852
JB
8243
8244 if (!drv->poll_command_supported) {
8245 nl80211_send_null_frame(bss, own_addr, addr, qos);
8246 return;
8247 }
8248
13f83980 8249 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
a862e4a3
JM
8250 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8251 nlmsg_free(msg);
8252 return;
8253 }
39718852 8254
fc99fab7
JM
8255 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8256 if (ret < 0) {
8257 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8258 MACSTR " failed: ret=%d (%s)",
8259 MAC2STR(addr), ret, strerror(-ret));
8260 }
39718852
JB
8261}
8262
bcf24348 8263
29f338af
JM
8264static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8265{
8266 struct nl_msg *msg;
1baa130b 8267 int ret;
29f338af 8268
13f83980 8269 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
a862e4a3
JM
8270 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8271 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8272 nlmsg_free(msg);
8273 return -ENOBUFS;
8274 }
1baa130b
JM
8275
8276 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8277 if (ret < 0) {
8278 wpa_printf(MSG_DEBUG,
8279 "nl80211: Setting PS state %s failed: %d (%s)",
8280 enabled ? "enabled" : "disabled",
8281 ret, strerror(-ret));
8282 }
8283 return ret;
29f338af
JM
8284}
8285
8286
8287static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8288 int ctwindow)
8289{
8290 struct i802_bss *bss = priv;
8291
8292 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8293 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8294
0de38036
JM
8295 if (opp_ps != -1 || ctwindow != -1) {
8296#ifdef ANDROID_P2P
8297 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
8298#else /* ANDROID_P2P */
29f338af 8299 return -1; /* Not yet supported */
0de38036
JM
8300#endif /* ANDROID_P2P */
8301 }
29f338af
JM
8302
8303 if (legacy_ps == -1)
8304 return 0;
8305 if (legacy_ps != 0 && legacy_ps != 1)
8306 return -1; /* Not yet supported */
8307
8308 return nl80211_set_power_save(bss, legacy_ps);
8309}
8310
8311
04e8003c
JD
8312static int nl80211_start_radar_detection(void *priv,
8313 struct hostapd_freq_params *freq)
f90e9c1c
SW
8314{
8315 struct i802_bss *bss = priv;
8316 struct wpa_driver_nl80211_data *drv = bss->drv;
8317 struct nl_msg *msg;
8318 int ret;
8319
04e8003c
JD
8320 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)",
8321 freq->freq, freq->ht_enabled, freq->vht_enabled,
8322 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8323
f90e9c1c
SW
8324 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8325 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8326 "detection");
8327 return -1;
8328 }
8329
9725b784 8330 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
a862e4a3
JM
8331 nl80211_put_freq_params(msg, freq) < 0) {
8332 nlmsg_free(msg);
8333 return -1;
8334 }
f90e9c1c
SW
8335
8336 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8337 if (ret == 0)
8338 return 0;
8339 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8340 "%d (%s)", ret, strerror(-ret));
f90e9c1c
SW
8341 return -1;
8342}
8343
03ea1786
AN
8344#ifdef CONFIG_TDLS
8345
8346static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8347 u8 dialog_token, u16 status_code,
984dadc2
AN
8348 u32 peer_capab, int initiator, const u8 *buf,
8349 size_t len)
03ea1786
AN
8350{
8351 struct i802_bss *bss = priv;
8352 struct wpa_driver_nl80211_data *drv = bss->drv;
8353 struct nl_msg *msg;
8354
8355 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8356 return -EOPNOTSUPP;
8357
8358 if (!dst)
8359 return -EINVAL;
8360
9725b784 8361 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
a862e4a3
JM
8362 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8363 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8364 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8365 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8366 goto fail;
96ecea5e
SD
8367 if (peer_capab) {
8368 /*
8369 * The internal enum tdls_peer_capability definition is
8370 * currently identical with the nl80211 enum
8371 * nl80211_tdls_peer_capability, so no conversion is needed
8372 * here.
8373 */
a862e4a3
JM
8374 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8375 peer_capab))
8376 goto fail;
96ecea5e 8377 }
a862e4a3
JM
8378 if ((initiator &&
8379 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8380 nla_put(msg, NL80211_ATTR_IE, len, buf))
8381 goto fail;
03ea1786
AN
8382
8383 return send_and_recv_msgs(drv, msg, NULL, NULL);
8384
a862e4a3 8385fail:
03ea1786
AN
8386 nlmsg_free(msg);
8387 return -ENOBUFS;
8388}
8389
8390
8391static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8392{
8393 struct i802_bss *bss = priv;
8394 struct wpa_driver_nl80211_data *drv = bss->drv;
8395 struct nl_msg *msg;
8396 enum nl80211_tdls_operation nl80211_oper;
b2442f25 8397 int res;
03ea1786
AN
8398
8399 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8400 return -EOPNOTSUPP;
8401
8402 switch (oper) {
8403 case TDLS_DISCOVERY_REQ:
8404 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8405 break;
8406 case TDLS_SETUP:
8407 nl80211_oper = NL80211_TDLS_SETUP;
8408 break;
8409 case TDLS_TEARDOWN:
8410 nl80211_oper = NL80211_TDLS_TEARDOWN;
8411 break;
8412 case TDLS_ENABLE_LINK:
8413 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8414 break;
8415 case TDLS_DISABLE_LINK:
8416 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8417 break;
8418 case TDLS_ENABLE:
8419 return 0;
8420 case TDLS_DISABLE:
8421 return 0;
8422 default:
8423 return -EINVAL;
8424 }
8425
9725b784 8426 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
a862e4a3 8427 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
a862e4a3
JM
8428 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8429 nlmsg_free(msg);
8430 return -ENOBUFS;
8431 }
03ea1786 8432
b2442f25
JM
8433 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8434 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8435 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8436 strerror(-res));
8437 return res;
03ea1786
AN
8438}
8439
72b2605f
AN
8440
8441static int
8442nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8443 const struct hostapd_freq_params *params)
8444{
8445 struct i802_bss *bss = priv;
8446 struct wpa_driver_nl80211_data *drv = bss->drv;
8447 struct nl_msg *msg;
8448 int ret = -ENOBUFS;
8449
8450 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8451 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8452 return -EOPNOTSUPP;
8453
8454 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8455 " oper_class=%u freq=%u",
8456 MAC2STR(addr), oper_class, params->freq);
8457 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8458 if (!msg ||
8459 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8460 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8461 (ret = nl80211_put_freq_params(msg, params))) {
8462 nlmsg_free(msg);
8463 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8464 return ret;
8465 }
8466
8467 return send_and_recv_msgs(drv, msg, NULL, NULL);
8468}
8469
8470
8471static int
8472nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8473{
8474 struct i802_bss *bss = priv;
8475 struct wpa_driver_nl80211_data *drv = bss->drv;
8476 struct nl_msg *msg;
8477
8478 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8479 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8480 return -EOPNOTSUPP;
8481
8482 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8483 MAC2STR(addr));
8484 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8485 if (!msg ||
8486 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8487 nlmsg_free(msg);
8488 wpa_printf(MSG_DEBUG,
8489 "nl80211: Could not build TDLS cancel chan switch");
8490 return -ENOBUFS;
8491 }
8492
8493 return send_and_recv_msgs(drv, msg, NULL, NULL);
8494}
8495
03ea1786
AN
8496#endif /* CONFIG TDLS */
8497
8498
9ebce9c5
JM
8499static int driver_nl80211_set_key(const char *ifname, void *priv,
8500 enum wpa_alg alg, const u8 *addr,
8501 int key_idx, int set_tx,
8502 const u8 *seq, size_t seq_len,
8503 const u8 *key, size_t key_len)
8504{
8505 struct i802_bss *bss = priv;
8506 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8507 set_tx, seq, seq_len, key, key_len);
8508}
8509
8510
8511static int driver_nl80211_scan2(void *priv,
8512 struct wpa_driver_scan_params *params)
8513{
8514 struct i802_bss *bss = priv;
b658547d 8515#ifdef CONFIG_DRIVER_NL80211_QCA
adcd7c4b
KV
8516 struct wpa_driver_nl80211_data *drv = bss->drv;
8517
8518 /*
8519 * Do a vendor specific scan if possible. If only_new_results is
8520 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8521 * cannot be achieved through a vendor scan. The below condition may
8522 * need to be modified if new scan flags are added in the future whose
8523 * functionality can only be achieved through a normal scan.
8524 */
8525 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8526 return wpa_driver_nl80211_vendor_scan(bss, params);
b658547d 8527#endif /* CONFIG_DRIVER_NL80211_QCA */
9ebce9c5
JM
8528 return wpa_driver_nl80211_scan(bss, params);
8529}
8530
8531
8532static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8533 int reason_code)
8534{
8535 struct i802_bss *bss = priv;
8536 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8537}
8538
8539
8540static int driver_nl80211_authenticate(void *priv,
8541 struct wpa_driver_auth_params *params)
8542{
8543 struct i802_bss *bss = priv;
8544 return wpa_driver_nl80211_authenticate(bss, params);
8545}
8546
8547
8548static void driver_nl80211_deinit(void *priv)
8549{
8550 struct i802_bss *bss = priv;
8551 wpa_driver_nl80211_deinit(bss);
8552}
8553
8554
8555static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8556 const char *ifname)
8557{
8558 struct i802_bss *bss = priv;
8559 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8560}
8561
8562
8563static int driver_nl80211_send_mlme(void *priv, const u8 *data,
5d180a77 8564 size_t data_len, int noack,
2d3943ce
AO
8565 unsigned int freq,
8566 const u16 *csa_offs, size_t csa_offs_len)
9ebce9c5
JM
8567{
8568 struct i802_bss *bss = priv;
8569 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
2d3943ce
AO
8570 freq, 0, 0, 0, csa_offs,
8571 csa_offs_len);
9ebce9c5
JM
8572}
8573
8574
8575static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8576{
8577 struct i802_bss *bss = priv;
59d7148a 8578 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
9ebce9c5
JM
8579}
8580
8581
9ebce9c5
JM
8582static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8583 const char *ifname, int vlan_id)
8584{
8585 struct i802_bss *bss = priv;
8586 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8587}
9ebce9c5
JM
8588
8589
8590static int driver_nl80211_read_sta_data(void *priv,
8591 struct hostap_sta_driver_data *data,
8592 const u8 *addr)
8593{
8594 struct i802_bss *bss = priv;
7824bf77
JC
8595
8596 os_memset(data, 0, sizeof(*data));
9ebce9c5
JM
8597 return i802_read_sta_data(bss, data, addr);
8598}
8599
8600
8601static int driver_nl80211_send_action(void *priv, unsigned int freq,
8602 unsigned int wait_time,
8603 const u8 *dst, const u8 *src,
8604 const u8 *bssid,
8605 const u8 *data, size_t data_len,
8606 int no_cck)
8607{
8608 struct i802_bss *bss = priv;
8609 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8610 bssid, data, data_len, no_cck);
8611}
8612
8613
8614static int driver_nl80211_probe_req_report(void *priv, int report)
8615{
8616 struct i802_bss *bss = priv;
8617 return wpa_driver_nl80211_probe_req_report(bss, report);
8618}
8619
8620
6a1ce395
DG
8621static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8622 const u8 *ies, size_t ies_len)
8623{
8624 int ret;
8625 struct nl_msg *msg;
8626 struct i802_bss *bss = priv;
8627 struct wpa_driver_nl80211_data *drv = bss->drv;
8628 u16 mdid = WPA_GET_LE16(md);
8629
6a1ce395 8630 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9725b784 8631 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
a862e4a3
JM
8632 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8633 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8634 nlmsg_free(msg);
8635 return -ENOBUFS;
8636 }
6a1ce395
DG
8637
8638 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8639 if (ret) {
8640 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8641 "err=%d (%s)", ret, strerror(-ret));
8642 }
8643
8644 return ret;
6a1ce395
DG
8645}
8646
8647
47754718 8648static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
597b94f5
AS
8649{
8650 struct i802_bss *bss = priv;
8651 struct wpa_driver_nl80211_data *drv = bss->drv;
8652
8653 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8654 return NULL;
8655
8656 return bss->addr;
8657}
8658
8659
a771c07d
JM
8660static const char * scan_state_str(enum scan_states scan_state)
8661{
8662 switch (scan_state) {
8663 case NO_SCAN:
8664 return "NO_SCAN";
8665 case SCAN_REQUESTED:
8666 return "SCAN_REQUESTED";
8667 case SCAN_STARTED:
8668 return "SCAN_STARTED";
8669 case SCAN_COMPLETED:
8670 return "SCAN_COMPLETED";
8671 case SCAN_ABORTED:
8672 return "SCAN_ABORTED";
8673 case SCHED_SCAN_STARTED:
8674 return "SCHED_SCAN_STARTED";
8675 case SCHED_SCAN_STOPPED:
8676 return "SCHED_SCAN_STOPPED";
8677 case SCHED_SCAN_RESULTS:
8678 return "SCHED_SCAN_RESULTS";
8679 }
8680
8681 return "??";
8682}
8683
8684
8685static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8686{
8687 struct i802_bss *bss = priv;
8688 struct wpa_driver_nl80211_data *drv = bss->drv;
8689 int res;
8690 char *pos, *end;
8691
8692 pos = buf;
8693 end = buf + buflen;
8694
8695 res = os_snprintf(pos, end - pos,
8696 "ifindex=%d\n"
8697 "ifname=%s\n"
8698 "brname=%s\n"
8699 "addr=" MACSTR "\n"
8700 "freq=%d\n"
c809756f 8701 "%s%s%s%s%s%s",
a771c07d
JM
8702 bss->ifindex,
8703 bss->ifname,
8704 bss->brname,
8705 MAC2STR(bss->addr),
8706 bss->freq,
8707 bss->beacon_set ? "beacon_set=1\n" : "",
8708 bss->added_if_into_bridge ?
8709 "added_if_into_bridge=1\n" : "",
c809756f 8710 bss->already_in_bridge ? "already_in_bridge=1\n" : "",
a771c07d
JM
8711 bss->added_bridge ? "added_bridge=1\n" : "",
8712 bss->in_deinit ? "in_deinit=1\n" : "",
8713 bss->if_dynamic ? "if_dynamic=1\n" : "");
d85e1fc8 8714 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8715 return pos - buf;
8716 pos += res;
8717
8718 if (bss->wdev_id_set) {
8719 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8720 (unsigned long long) bss->wdev_id);
d85e1fc8 8721 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8722 return pos - buf;
8723 pos += res;
8724 }
8725
8726 res = os_snprintf(pos, end - pos,
8727 "phyname=%s\n"
fee354c7 8728 "perm_addr=" MACSTR "\n"
a771c07d
JM
8729 "drv_ifindex=%d\n"
8730 "operstate=%d\n"
8731 "scan_state=%s\n"
8732 "auth_bssid=" MACSTR "\n"
8733 "auth_attempt_bssid=" MACSTR "\n"
8734 "bssid=" MACSTR "\n"
8735 "prev_bssid=" MACSTR "\n"
8736 "associated=%d\n"
8737 "assoc_freq=%u\n"
8738 "monitor_sock=%d\n"
8739 "monitor_ifidx=%d\n"
8740 "monitor_refcount=%d\n"
8741 "last_mgmt_freq=%u\n"
8742 "eapol_tx_sock=%d\n"
97752f79 8743 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
a771c07d 8744 drv->phyname,
fee354c7 8745 MAC2STR(drv->perm_addr),
a771c07d
JM
8746 drv->ifindex,
8747 drv->operstate,
8748 scan_state_str(drv->scan_state),
8749 MAC2STR(drv->auth_bssid),
8750 MAC2STR(drv->auth_attempt_bssid),
8751 MAC2STR(drv->bssid),
8752 MAC2STR(drv->prev_bssid),
8753 drv->associated,
8754 drv->assoc_freq,
8755 drv->monitor_sock,
8756 drv->monitor_ifidx,
8757 drv->monitor_refcount,
8758 drv->last_mgmt_freq,
8759 drv->eapol_tx_sock,
8760 drv->ignore_if_down_event ?
8761 "ignore_if_down_event=1\n" : "",
8762 drv->scan_complete_events ?
8763 "scan_complete_events=1\n" : "",
8764 drv->disabled_11b_rates ?
8765 "disabled_11b_rates=1\n" : "",
8766 drv->pending_remain_on_chan ?
8767 "pending_remain_on_chan=1\n" : "",
8768 drv->in_interface_list ? "in_interface_list=1\n" : "",
8769 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8770 drv->poll_command_supported ?
8771 "poll_command_supported=1\n" : "",
8772 drv->data_tx_status ? "data_tx_status=1\n" : "",
8773 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8774 drv->retry_auth ? "retry_auth=1\n" : "",
8775 drv->use_monitor ? "use_monitor=1\n" : "",
8776 drv->ignore_next_local_disconnect ?
8777 "ignore_next_local_disconnect=1\n" : "",
d6a36f39 8778 drv->ignore_next_local_deauth ?
97752f79 8779 "ignore_next_local_deauth=1\n" : "");
d85e1fc8 8780 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8781 return pos - buf;
8782 pos += res;
8783
8784 if (drv->has_capability) {
8785 res = os_snprintf(pos, end - pos,
8786 "capa.key_mgmt=0x%x\n"
8787 "capa.enc=0x%x\n"
8788 "capa.auth=0x%x\n"
24bd4e0b 8789 "capa.flags=0x%llx\n"
a0a34d53 8790 "capa.rrm_flags=0x%x\n"
a771c07d
JM
8791 "capa.max_scan_ssids=%d\n"
8792 "capa.max_sched_scan_ssids=%d\n"
8793 "capa.sched_scan_supported=%d\n"
8794 "capa.max_match_sets=%d\n"
8795 "capa.max_remain_on_chan=%u\n"
8796 "capa.max_stations=%u\n"
8797 "capa.probe_resp_offloads=0x%x\n"
8798 "capa.max_acl_mac_addrs=%u\n"
86056fea
IP
8799 "capa.num_multichan_concurrent=%u\n"
8800 "capa.mac_addr_rand_sched_scan_supported=%d\n"
079a28f7
AK
8801 "capa.mac_addr_rand_scan_supported=%d\n"
8802 "capa.conc_capab=%u\n"
8803 "capa.max_conc_chan_2_4=%u\n"
09ea4309
AS
8804 "capa.max_conc_chan_5_0=%u\n"
8805 "capa.max_sched_scan_plans=%u\n"
8806 "capa.max_sched_scan_plan_interval=%u\n"
8807 "capa.max_sched_scan_plan_iterations=%u\n",
a771c07d
JM
8808 drv->capa.key_mgmt,
8809 drv->capa.enc,
8810 drv->capa.auth,
24bd4e0b 8811 (unsigned long long) drv->capa.flags,
a0a34d53 8812 drv->capa.rrm_flags,
a771c07d
JM
8813 drv->capa.max_scan_ssids,
8814 drv->capa.max_sched_scan_ssids,
8815 drv->capa.sched_scan_supported,
8816 drv->capa.max_match_sets,
8817 drv->capa.max_remain_on_chan,
8818 drv->capa.max_stations,
8819 drv->capa.probe_resp_offloads,
8820 drv->capa.max_acl_mac_addrs,
86056fea
IP
8821 drv->capa.num_multichan_concurrent,
8822 drv->capa.mac_addr_rand_sched_scan_supported,
079a28f7
AK
8823 drv->capa.mac_addr_rand_scan_supported,
8824 drv->capa.conc_capab,
8825 drv->capa.max_conc_chan_2_4,
09ea4309
AS
8826 drv->capa.max_conc_chan_5_0,
8827 drv->capa.max_sched_scan_plans,
8828 drv->capa.max_sched_scan_plan_interval,
8829 drv->capa.max_sched_scan_plan_iterations);
d85e1fc8 8830 if (os_snprintf_error(end - pos, res))
a771c07d
JM
8831 return pos - buf;
8832 pos += res;
8833 }
8834
8835 return pos - buf;
8836}
8837
8838
1c4ffa87
AO
8839static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8840{
a862e4a3
JM
8841 if ((settings->head &&
8842 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8843 settings->head_len, settings->head)) ||
8844 (settings->tail &&
8845 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8846 settings->tail_len, settings->tail)) ||
8847 (settings->beacon_ies &&
8848 nla_put(msg, NL80211_ATTR_IE,
8849 settings->beacon_ies_len, settings->beacon_ies)) ||
8850 (settings->proberesp_ies &&
8851 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8852 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8853 (settings->assocresp_ies &&
8854 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8855 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8856 (settings->probe_resp &&
8857 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8858 settings->probe_resp_len, settings->probe_resp)))
8859 return -ENOBUFS;
1c4ffa87
AO
8860
8861 return 0;
1c4ffa87
AO
8862}
8863
8864
8865static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8866{
8867 struct nl_msg *msg;
8868 struct i802_bss *bss = priv;
8869 struct wpa_driver_nl80211_data *drv = bss->drv;
8870 struct nlattr *beacon_csa;
8871 int ret = -ENOBUFS;
366179d2
AO
8872 int csa_off_len = 0;
8873 int i;
1c4ffa87 8874
8d1fdde7 8875 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
1c4ffa87 8876 settings->cs_count, settings->block_tx,
8d1fdde7
JD
8877 settings->freq_params.freq, settings->freq_params.bandwidth,
8878 settings->freq_params.center_freq1,
8879 settings->freq_params.center_freq2);
1c4ffa87 8880
991aa9c7 8881 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
1c4ffa87
AO
8882 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8883 return -EOPNOTSUPP;
8884 }
8885
0928b629
PO
8886 if (drv->nlmode != NL80211_IFTYPE_AP &&
8887 drv->nlmode != NL80211_IFTYPE_P2P_GO &&
8888 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
1c4ffa87
AO
8889 return -EOPNOTSUPP;
8890
366179d2
AO
8891 /*
8892 * Remove empty counters, assuming Probe Response and Beacon frame
8893 * counters match. This implementation assumes that there are only two
8894 * counters.
8895 */
8896 if (settings->counter_offset_beacon[0] &&
8897 !settings->counter_offset_beacon[1]) {
8898 csa_off_len = 1;
8899 } else if (settings->counter_offset_beacon[1] &&
8900 !settings->counter_offset_beacon[0]) {
8901 csa_off_len = 1;
8902 settings->counter_offset_beacon[0] =
8903 settings->counter_offset_beacon[1];
8904 settings->counter_offset_presp[0] =
8905 settings->counter_offset_presp[1];
8906 } else if (settings->counter_offset_beacon[1] &&
8907 settings->counter_offset_beacon[0]) {
8908 csa_off_len = 2;
8909 } else {
8910 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8911 return -EINVAL;
8912 }
8913
8914 /* Check CSA counters validity */
8915 if (drv->capa.max_csa_counters &&
8916 csa_off_len > drv->capa.max_csa_counters) {
8917 wpa_printf(MSG_ERROR,
8918 "nl80211: Too many CSA counters provided");
1c4ffa87 8919 return -EINVAL;
366179d2 8920 }
1c4ffa87 8921
366179d2 8922 if (!settings->beacon_csa.tail)
1c4ffa87
AO
8923 return -EINVAL;
8924
366179d2
AO
8925 for (i = 0; i < csa_off_len; i++) {
8926 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8927 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8928
8929 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8930 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8931 settings->cs_count))
8932 return -EINVAL;
8933
8934 if (settings->beacon_csa.probe_resp &&
8935 ((settings->beacon_csa.probe_resp_len <=
8936 csa_c_off_presp) ||
8937 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8938 settings->cs_count)))
8939 return -EINVAL;
8940 }
8941
13f83980 8942 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
a862e4a3
JM
8943 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8944 settings->cs_count) ||
8945 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8946 (settings->block_tx &&
8947 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
1c4ffa87
AO
8948 goto error;
8949
1c4ffa87
AO
8950 /* beacon_after params */
8951 ret = set_beacon_data(msg, &settings->beacon_after);
8952 if (ret)
8953 goto error;
8954
8955 /* beacon_csa params */
8956 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8957 if (!beacon_csa)
a862e4a3 8958 goto fail;
1c4ffa87
AO
8959
8960 ret = set_beacon_data(msg, &settings->beacon_csa);
8961 if (ret)
8962 goto error;
8963
366179d2
AO
8964 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8965 csa_off_len * sizeof(u16),
8966 settings->counter_offset_beacon) ||
a862e4a3 8967 (settings->beacon_csa.probe_resp &&
366179d2
AO
8968 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8969 csa_off_len * sizeof(u16),
8970 settings->counter_offset_presp)))
a862e4a3 8971 goto fail;
1c4ffa87
AO
8972
8973 nla_nest_end(msg, beacon_csa);
8974 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8975 if (ret) {
8976 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8977 ret, strerror(-ret));
8978 }
8979 return ret;
8980
a862e4a3 8981fail:
1c4ffa87
AO
8982 ret = -ENOBUFS;
8983error:
8984 nlmsg_free(msg);
8985 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8986 return ret;
8987}
8988
8989
dfa87878
MB
8990static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8991 u8 user_priority, u16 admitted_time)
8992{
8993 struct i802_bss *bss = priv;
8994 struct wpa_driver_nl80211_data *drv = bss->drv;
8995 struct nl_msg *msg;
8996 int ret;
8997
8998 wpa_printf(MSG_DEBUG,
8999 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
9000 tsid, admitted_time, user_priority);
9001
9002 if (!is_sta_interface(drv->nlmode))
9003 return -ENOTSUP;
9004
56f77852
JM
9005 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
9006 if (!msg ||
a862e4a3
JM
9007 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9008 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9009 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
9010 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
9011 nlmsg_free(msg);
9012 return -ENOBUFS;
9013 }
dfa87878
MB
9014
9015 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9016 if (ret)
9017 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
9018 ret, strerror(-ret));
9019 return ret;
dfa87878
MB
9020}
9021
9022
9023static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
9024{
9025 struct i802_bss *bss = priv;
9026 struct wpa_driver_nl80211_data *drv = bss->drv;
9027 struct nl_msg *msg;
9028 int ret;
9029
9030 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
9031
9032 if (!is_sta_interface(drv->nlmode))
9033 return -ENOTSUP;
9034
56f77852 9035 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
a862e4a3
JM
9036 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
9037 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
9038 nlmsg_free(msg);
9039 return -ENOBUFS;
9040 }
dfa87878
MB
9041
9042 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9043 if (ret)
9044 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
9045 ret, strerror(-ret));
9046 return ret;
dfa87878
MB
9047}
9048
9049
6b9f7af6
JM
9050#ifdef CONFIG_TESTING_OPTIONS
9051static int cmd_reply_handler(struct nl_msg *msg, void *arg)
9052{
9053 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9054 struct wpabuf *buf = arg;
9055
9056 if (!buf)
9057 return NL_SKIP;
9058
9059 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
9060 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
9061 return NL_SKIP;
9062 }
9063
9064 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
9065 genlmsg_attrlen(gnlh, 0));
9066
9067 return NL_SKIP;
9068}
9069#endif /* CONFIG_TESTING_OPTIONS */
9070
9071
adef8948
BL
9072static int vendor_reply_handler(struct nl_msg *msg, void *arg)
9073{
9074 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9075 struct nlattr *nl_vendor_reply, *nl;
9076 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9077 struct wpabuf *buf = arg;
9078 int rem;
9079
9080 if (!buf)
9081 return NL_SKIP;
9082
9083 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9084 genlmsg_attrlen(gnlh, 0), NULL);
9085 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
9086
9087 if (!nl_vendor_reply)
9088 return NL_SKIP;
9089
9090 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
9091 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
9092 return NL_SKIP;
9093 }
9094
9095 nla_for_each_nested(nl, nl_vendor_reply, rem) {
9096 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
9097 }
9098
9099 return NL_SKIP;
9100}
9101
9102
9103static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
9104 unsigned int subcmd, const u8 *data,
9105 size_t data_len, struct wpabuf *buf)
9106{
9107 struct i802_bss *bss = priv;
9108 struct wpa_driver_nl80211_data *drv = bss->drv;
9109 struct nl_msg *msg;
9110 int ret;
9111
6b9f7af6
JM
9112#ifdef CONFIG_TESTING_OPTIONS
9113 if (vendor_id == 0xffffffff) {
56f77852
JM
9114 msg = nlmsg_alloc();
9115 if (!msg)
9116 return -ENOMEM;
9117
6b9f7af6
JM
9118 nl80211_cmd(drv, msg, 0, subcmd);
9119 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
9120 0)
a862e4a3 9121 goto fail;
6b9f7af6
JM
9122 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
9123 if (ret)
9124 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
9125 ret);
9126 return ret;
9127 }
9128#endif /* CONFIG_TESTING_OPTIONS */
9129
56f77852 9130 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
9131 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
9132 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
9133 (data &&
9134 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
9135 goto fail;
adef8948
BL
9136
9137 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
9138 if (ret)
9139 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
9140 ret);
9141 return ret;
9142
a862e4a3 9143fail:
adef8948
BL
9144 nlmsg_free(msg);
9145 return -ENOBUFS;
9146}
9147
9148
049105b4
KP
9149static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
9150 u8 qos_map_set_len)
9151{
9152 struct i802_bss *bss = priv;
9153 struct wpa_driver_nl80211_data *drv = bss->drv;
9154 struct nl_msg *msg;
9155 int ret;
9156
049105b4
KP
9157 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
9158 qos_map_set, qos_map_set_len);
9159
9725b784 9160 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
a862e4a3
JM
9161 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
9162 nlmsg_free(msg);
9163 return -ENOBUFS;
9164 }
049105b4
KP
9165
9166 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9167 if (ret)
9168 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
9169
9170 return ret;
049105b4
KP
9171}
9172
9173
e4fa8b12
EP
9174static int nl80211_set_wowlan(void *priv,
9175 const struct wowlan_triggers *triggers)
9176{
9177 struct i802_bss *bss = priv;
9178 struct wpa_driver_nl80211_data *drv = bss->drv;
9179 struct nl_msg *msg;
9180 struct nlattr *wowlan_triggers;
9181 int ret;
9182
e4fa8b12
EP
9183 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
9184
1ac977bd 9185 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
a862e4a3
JM
9186 !(wowlan_triggers = nla_nest_start(msg,
9187 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
9188 (triggers->any &&
9189 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9190 (triggers->disconnect &&
9191 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9192 (triggers->magic_pkt &&
9193 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9194 (triggers->gtk_rekey_failure &&
9195 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9196 (triggers->eap_identity_req &&
9197 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9198 (triggers->four_way_handshake &&
9199 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9200 (triggers->rfkill_release &&
9201 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
9202 nlmsg_free(msg);
9203 return -ENOBUFS;
9204 }
e4fa8b12
EP
9205
9206 nla_nest_end(msg, wowlan_triggers);
9207
9208 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9209 if (ret)
9210 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
9211
9212 return ret;
e4fa8b12
EP
9213}
9214
9215
b658547d 9216#ifdef CONFIG_DRIVER_NL80211_QCA
0800f9ee
JM
9217static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
9218{
9219 struct i802_bss *bss = priv;
9220 struct wpa_driver_nl80211_data *drv = bss->drv;
9221 struct nl_msg *msg;
9222 struct nlattr *params;
9223
9224 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
9225
9226 if (!drv->roaming_vendor_cmd_avail) {
9227 wpa_printf(MSG_DEBUG,
9228 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9229 return -1;
9230 }
9231
9725b784 9232 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
9233 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9234 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9235 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9236 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9237 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9238 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9239 QCA_ROAMING_NOT_ALLOWED) ||
9240 (bssid &&
9241 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9242 nlmsg_free(msg);
9243 return -1;
9244 }
0800f9ee
JM
9245 nla_nest_end(msg, params);
9246
9247 return send_and_recv_msgs(drv, msg, NULL, NULL);
0800f9ee 9248}
b04854ce
AP
9249
9250
d98038bb 9251static int nl80211_disable_fils(void *priv, int disable)
9252{
9253 struct i802_bss *bss = priv;
9254 struct wpa_driver_nl80211_data *drv = bss->drv;
9255 struct nl_msg *msg;
9256 struct nlattr *params;
9257
9258 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9259
9260 if (!drv->set_wifi_conf_vendor_cmd_avail)
9261 return -1;
9262
9263 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9264 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9265 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9266 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9267 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9268 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9269 disable)) {
9270 nlmsg_free(msg);
9271 return -1;
9272 }
9273 nla_nest_end(msg, params);
9274
9275 return send_and_recv_msgs(drv, msg, NULL, NULL);
9276}
9277
9278
b04854ce
AP
9279/* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9280#define WPA_SUPPLICANT_CLIENT_ID 1
9281
9282static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9283 const u8 *bssid)
9284{
9285 struct i802_bss *bss = priv;
9286 struct wpa_driver_nl80211_data *drv = bss->drv;
9287 struct nl_msg *msg;
9288 struct nlattr *params, *nlbssids, *attr;
9289 unsigned int i;
9290
9291 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9292 num_bssid);
9293
9294 if (!drv->roam_vendor_cmd_avail)
9295 return -1;
9296
9297 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9298 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9299 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9300 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9301 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9302 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9303 QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9304 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9305 WPA_SUPPLICANT_CLIENT_ID) ||
9306 nla_put_u32(msg,
9307 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9308 num_bssid))
9309 goto fail;
9310
9311 nlbssids = nla_nest_start(
9312 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9313 if (!nlbssids)
9314 goto fail;
9315
9316 for (i = 0; i < num_bssid; i++) {
9317 attr = nla_nest_start(msg, i);
9318 if (!attr)
9319 goto fail;
9320 if (nla_put(msg,
9321 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9322 ETH_ALEN, &bssid[i * ETH_ALEN]))
9323 goto fail;
9324 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9325 MAC2STR(&bssid[i * ETH_ALEN]));
9326 nla_nest_end(msg, attr);
9327 }
9328 nla_nest_end(msg, nlbssids);
9329 nla_nest_end(msg, params);
9330
9331 return send_and_recv_msgs(drv, msg, NULL, NULL);
9332
9333fail:
9334 nlmsg_free(msg);
9335 return -1;
9336}
9337
b658547d 9338#endif /* CONFIG_DRIVER_NL80211_QCA */
0800f9ee
JM
9339
9340
fee354c7
JM
9341static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9342{
9343 struct i802_bss *bss = priv;
9344 struct wpa_driver_nl80211_data *drv = bss->drv;
9345 int new_addr = addr != NULL;
9346
9ce3e610
JM
9347 if (TEST_FAIL())
9348 return -1;
9349
fee354c7
JM
9350 if (!addr)
9351 addr = drv->perm_addr;
9352
9353 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9354 return -1;
9355
9356 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9357 {
9358 wpa_printf(MSG_DEBUG,
9359 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9360 bss->ifname, MAC2STR(addr));
9361 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9362 1) < 0) {
9363 wpa_printf(MSG_DEBUG,
9364 "nl80211: Could not restore interface UP after failed set_mac_addr");
9365 }
9366 return -1;
9367 }
9368
9369 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9370 bss->ifname, MAC2STR(addr));
9371 drv->addr_changed = new_addr;
9372 os_memcpy(bss->addr, addr, ETH_ALEN);
9373
9374 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9375 {
9376 wpa_printf(MSG_DEBUG,
9377 "nl80211: Could not restore interface UP after set_mac_addr");
9378 }
9379
9380 return 0;
9381}
9382
9383
6c1664f6
BC
9384#ifdef CONFIG_MESH
9385
9386static int wpa_driver_nl80211_init_mesh(void *priv)
9387{
9388 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9389 wpa_printf(MSG_INFO,
9390 "nl80211: Failed to set interface into mesh mode");
9391 return -1;
9392 }
9393 return 0;
9394}
9395
9396
21c74e84
JM
9397static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9398 size_t mesh_id_len)
9399{
9400 if (mesh_id) {
9401 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
9402 mesh_id, mesh_id_len);
9403 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9404 }
9405
9406 return 0;
9407}
9408
9409
4ffb3f87
MH
9410static int nl80211_put_mesh_config(struct nl_msg *msg,
9411 struct wpa_driver_mesh_bss_params *params)
9412{
9413 struct nlattr *container;
9414
9415 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9416 if (!container)
9417 return -1;
9418
2bd62171 9419 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
963d3149
JM
9420 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9421 params->auto_plinks)) ||
2bd62171
MH
9422 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9423 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
31a856a1
MH
9424 params->max_peer_links)) ||
9425 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9426 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9427 params->rssi_threshold)))
4ffb3f87
MH
9428 return -1;
9429
9430 /*
9431 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9432 * the timer could disconnect stations even in that case.
9433 */
2bd62171
MH
9434 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9435 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4ffb3f87
MH
9436 params->peer_link_timeout)) {
9437 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9438 return -1;
9439 }
9440
052b8d38
MH
9441 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9442 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9443 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9444 return -1;
9445 }
9446
4ffb3f87
MH
9447 nla_nest_end(msg, container);
9448
9449 return 0;
9450}
9451
9452
0cb5f8d9 9453static int nl80211_join_mesh(struct i802_bss *bss,
6c1664f6
BC
9454 struct wpa_driver_mesh_join_params *params)
9455{
6c1664f6
BC
9456 struct wpa_driver_nl80211_data *drv = bss->drv;
9457 struct nl_msg *msg;
9458 struct nlattr *container;
7451a217 9459 int ret = -1;
6c1664f6 9460
6c1664f6 9461 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9725b784 9462 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
f7e889fa
JM
9463 if (!msg ||
9464 nl80211_put_freq_params(msg, &params->freq) ||
21c74e84
JM
9465 nl80211_put_basic_rates(msg, params->basic_rates) ||
9466 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
4ac2ea57
MH
9467 nl80211_put_beacon_int(msg, params->beacon_int) ||
9468 nl80211_put_dtim_period(msg, params->dtim_period))
85e1fad8 9469 goto fail;
9c58c5f7 9470
6c1664f6
BC
9471 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
9472
9473 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9474 if (!container)
a862e4a3 9475 goto fail;
6c1664f6
BC
9476
9477 if (params->ies) {
9478 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
a862e4a3
JM
9479 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9480 params->ies))
9481 goto fail;
6c1664f6
BC
9482 }
9483 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9484 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
a862e4a3
JM
9485 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9486 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9487 goto fail;
6c1664f6 9488 }
a862e4a3
JM
9489 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9490 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9491 goto fail;
9492 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9493 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9494 goto fail;
6c1664f6
BC
9495 nla_nest_end(msg, container);
9496
2bd62171
MH
9497 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9498 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9499 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
4ffb3f87 9500 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
5a2a6de6 9501 goto fail;
6c1664f6
BC
9502
9503 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9504 msg = NULL;
9505 if (ret) {
9506 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9507 ret, strerror(-ret));
a862e4a3 9508 goto fail;
6c1664f6
BC
9509 }
9510 ret = 0;
92a515b8 9511 drv->assoc_freq = bss->freq = params->freq.freq;
6c1664f6
BC
9512 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9513
a862e4a3 9514fail:
6c1664f6
BC
9515 nlmsg_free(msg);
9516 return ret;
9517}
9518
9519
0cb5f8d9
MH
9520static int
9521wpa_driver_nl80211_join_mesh(void *priv,
9522 struct wpa_driver_mesh_join_params *params)
9523{
9524 struct i802_bss *bss = priv;
9525 int ret, timeout;
9526
9527 timeout = params->conf.peer_link_timeout;
9528
9529 /* Disable kernel inactivity timer */
9530 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9531 params->conf.peer_link_timeout = 0;
9532
9533 ret = nl80211_join_mesh(bss, params);
9534 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9535 wpa_printf(MSG_DEBUG,
9536 "nl80211: Mesh join retry for peer_link_timeout");
9537 /*
9538 * Old kernel does not support setting
9539 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9540 * into future from peer_link_timeout.
9541 */
9542 params->conf.peer_link_timeout = timeout + 60;
9543 ret = nl80211_join_mesh(priv, params);
9544 }
9545
9546 params->conf.peer_link_timeout = timeout;
9547 return ret;
9548}
9549
9550
6c1664f6
BC
9551static int wpa_driver_nl80211_leave_mesh(void *priv)
9552{
9553 struct i802_bss *bss = priv;
9554 struct wpa_driver_nl80211_data *drv = bss->drv;
9555 struct nl_msg *msg;
9725b784 9556 int ret;
6c1664f6
BC
9557
9558 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9725b784 9559 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
6c1664f6 9560 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6c1664f6
BC
9561 if (ret) {
9562 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9563 ret, strerror(-ret));
a862e4a3
JM
9564 } else {
9565 wpa_printf(MSG_DEBUG,
9566 "nl80211: mesh leave request send successfully");
6c1664f6 9567 }
6c1664f6 9568
f33c82d2
JM
9569 if (wpa_driver_nl80211_set_mode(drv->first_bss,
9570 NL80211_IFTYPE_STATION)) {
9571 wpa_printf(MSG_INFO,
9572 "nl80211: Failed to set interface into station mode");
9573 }
6c1664f6
BC
9574 return ret;
9575}
9576
9577#endif /* CONFIG_MESH */
9578
9579
ed4ddb6d
KP
9580static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9581 const u8 *ipaddr, int prefixlen,
9582 const u8 *addr)
71103bed
KP
9583{
9584#ifdef CONFIG_LIBNL3_ROUTE
9585 struct i802_bss *bss = priv;
9586 struct wpa_driver_nl80211_data *drv = bss->drv;
9587 struct rtnl_neigh *rn;
9588 struct nl_addr *nl_ipaddr = NULL;
9589 struct nl_addr *nl_lladdr = NULL;
ed4ddb6d 9590 int family, addrsize;
71103bed
KP
9591 int res;
9592
ed4ddb6d 9593 if (!ipaddr || prefixlen == 0 || !addr)
71103bed
KP
9594 return -EINVAL;
9595
9596 if (bss->br_ifindex == 0) {
9597 wpa_printf(MSG_DEBUG,
9598 "nl80211: bridge must be set before adding an ip neigh to it");
9599 return -1;
9600 }
9601
9602 if (!drv->rtnl_sk) {
9603 wpa_printf(MSG_DEBUG,
9604 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9605 return -1;
9606 }
9607
ed4ddb6d
KP
9608 if (version == 4) {
9609 family = AF_INET;
9610 addrsize = 4;
9611 } else if (version == 6) {
9612 family = AF_INET6;
9613 addrsize = 16;
9614 } else {
9615 return -EINVAL;
9616 }
9617
71103bed
KP
9618 rn = rtnl_neigh_alloc();
9619 if (rn == NULL)
9620 return -ENOMEM;
9621
9622 /* set the destination ip address for neigh */
ed4ddb6d 9623 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
71103bed
KP
9624 if (nl_ipaddr == NULL) {
9625 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9626 res = -ENOMEM;
9627 goto errout;
9628 }
9629 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9630 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9631 if (res) {
9632 wpa_printf(MSG_DEBUG,
9633 "nl80211: neigh set destination addr failed");
9634 goto errout;
9635 }
9636
9637 /* set the corresponding lladdr for neigh */
9638 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9639 if (nl_lladdr == NULL) {
9640 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9641 res = -ENOMEM;
9642 goto errout;
9643 }
9644 rtnl_neigh_set_lladdr(rn, nl_lladdr);
9645
9646 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9647 rtnl_neigh_set_state(rn, NUD_PERMANENT);
9648
9649 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9650 if (res) {
9651 wpa_printf(MSG_DEBUG,
9652 "nl80211: Adding bridge ip neigh failed: %s",
9653 strerror(errno));
9654 }
9655errout:
9656 if (nl_lladdr)
9657 nl_addr_put(nl_lladdr);
9658 if (nl_ipaddr)
9659 nl_addr_put(nl_ipaddr);
9660 if (rn)
9661 rtnl_neigh_put(rn);
9662 return res;
9663#else /* CONFIG_LIBNL3_ROUTE */
9664 return -1;
9665#endif /* CONFIG_LIBNL3_ROUTE */
9666}
9667
9668
ed4ddb6d
KP
9669static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9670 const u8 *ipaddr)
71103bed
KP
9671{
9672#ifdef CONFIG_LIBNL3_ROUTE
9673 struct i802_bss *bss = priv;
9674 struct wpa_driver_nl80211_data *drv = bss->drv;
9675 struct rtnl_neigh *rn;
9676 struct nl_addr *nl_ipaddr;
ed4ddb6d 9677 int family, addrsize;
71103bed
KP
9678 int res;
9679
ed4ddb6d 9680 if (!ipaddr)
71103bed
KP
9681 return -EINVAL;
9682
ed4ddb6d
KP
9683 if (version == 4) {
9684 family = AF_INET;
9685 addrsize = 4;
9686 } else if (version == 6) {
9687 family = AF_INET6;
9688 addrsize = 16;
9689 } else {
9690 return -EINVAL;
9691 }
9692
71103bed
KP
9693 if (bss->br_ifindex == 0) {
9694 wpa_printf(MSG_DEBUG,
9695 "nl80211: bridge must be set to delete an ip neigh");
9696 return -1;
9697 }
9698
9699 if (!drv->rtnl_sk) {
9700 wpa_printf(MSG_DEBUG,
9701 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9702 return -1;
9703 }
9704
9705 rn = rtnl_neigh_alloc();
9706 if (rn == NULL)
9707 return -ENOMEM;
9708
9709 /* set the destination ip address for neigh */
ed4ddb6d 9710 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
71103bed
KP
9711 if (nl_ipaddr == NULL) {
9712 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9713 res = -ENOMEM;
9714 goto errout;
9715 }
9716 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9717 if (res) {
9718 wpa_printf(MSG_DEBUG,
9719 "nl80211: neigh set destination addr failed");
9720 goto errout;
9721 }
9722
9723 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9724
9725 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9726 if (res) {
9727 wpa_printf(MSG_DEBUG,
9728 "nl80211: Deleting bridge ip neigh failed: %s",
9729 strerror(errno));
9730 }
9731errout:
9732 if (nl_ipaddr)
9733 nl_addr_put(nl_ipaddr);
9734 if (rn)
9735 rtnl_neigh_put(rn);
9736 return res;
9737#else /* CONFIG_LIBNL3_ROUTE */
9738 return -1;
9739#endif /* CONFIG_LIBNL3_ROUTE */
9740}
9741
9742
73d2294f
KP
9743static int linux_write_system_file(const char *path, unsigned int val)
9744{
9745 char buf[50];
9746 int fd, len;
9747
9748 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
a9aaacbb 9749 if (os_snprintf_error(sizeof(buf), len))
73d2294f
KP
9750 return -1;
9751
9752 fd = open(path, O_WRONLY);
9753 if (fd < 0)
9754 return -1;
9755
9756 if (write(fd, buf, len) < 0) {
9757 wpa_printf(MSG_DEBUG,
9758 "nl80211: Failed to write Linux system file: %s with the value of %d",
9759 path, val);
9760 close(fd);
9761 return -1;
9762 }
9763 close(fd);
9764
9765 return 0;
9766}
9767
9768
9769static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9770{
9771 switch (attr) {
9772 case DRV_BR_PORT_ATTR_PROXYARP:
bea8d9a3 9773 return "proxyarp_wifi";
73d2294f
KP
9774 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9775 return "hairpin_mode";
9776 }
9777
9778 return NULL;
9779}
9780
9781
9782static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9783 unsigned int val)
9784{
9785 struct i802_bss *bss = priv;
9786 char path[128];
9787 const char *attr_txt;
9788
9789 attr_txt = drv_br_port_attr_str(attr);
9790 if (attr_txt == NULL)
9791 return -EINVAL;
9792
9793 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9794 bss->ifname, attr_txt);
9795
9796 if (linux_write_system_file(path, val))
9797 return -1;
9798
9799 return 0;
9800}
9801
9802
7565752d
KP
9803static const char * drv_br_net_param_str(enum drv_br_net_param param)
9804{
9805 switch (param) {
9806 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9807 return "arp_accept";
60eb9e17
JM
9808 default:
9809 return NULL;
7565752d 9810 }
7565752d
KP
9811}
9812
9813
9814static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9815 unsigned int val)
9816{
9817 struct i802_bss *bss = priv;
9818 char path[128];
9819 const char *param_txt;
9820 int ip_version = 4;
9821
60eb9e17
JM
9822 if (param == DRV_BR_MULTICAST_SNOOPING) {
9823 os_snprintf(path, sizeof(path),
9824 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9825 bss->brname);
9826 goto set_val;
9827 }
9828
7565752d
KP
9829 param_txt = drv_br_net_param_str(param);
9830 if (param_txt == NULL)
9831 return -EINVAL;
9832
9833 switch (param) {
9834 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9835 ip_version = 4;
9836 break;
9837 default:
9838 return -EINVAL;
9839 }
9840
9841 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9842 ip_version, bss->brname, param_txt);
9843
60eb9e17 9844set_val:
7565752d
KP
9845 if (linux_write_system_file(path, val))
9846 return -1;
9847
9848 return 0;
9849}
9850
9851
b658547d
JM
9852#ifdef CONFIG_DRIVER_NL80211_QCA
9853
16689c7c
PX
9854static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9855{
9856 switch (hw_mode) {
9857 case HOSTAPD_MODE_IEEE80211B:
9858 return QCA_ACS_MODE_IEEE80211B;
9859 case HOSTAPD_MODE_IEEE80211G:
9860 return QCA_ACS_MODE_IEEE80211G;
9861 case HOSTAPD_MODE_IEEE80211A:
9862 return QCA_ACS_MODE_IEEE80211A;
9863 case HOSTAPD_MODE_IEEE80211AD:
9864 return QCA_ACS_MODE_IEEE80211AD;
3784c058
PX
9865 case HOSTAPD_MODE_IEEE80211ANY:
9866 return QCA_ACS_MODE_IEEE80211ANY;
16689c7c
PX
9867 default:
9868 return -1;
9869 }
9870}
9871
9872
d0cdccd3
PX
9873static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9874{
9875 int i, len, ret;
9876 u32 *freqs;
9877
9878 if (!freq_list)
9879 return 0;
9880 len = int_array_len(freq_list);
9881 freqs = os_malloc(sizeof(u32) * len);
9882 if (!freqs)
9883 return -1;
9884 for (i = 0; i < len; i++)
9885 freqs[i] = freq_list[i];
9886 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9887 sizeof(u32) * len, freqs);
9888 os_free(freqs);
9889 return ret;
9890}
9891
9892
16689c7c
PX
9893static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9894{
9895 struct i802_bss *bss = priv;
9896 struct wpa_driver_nl80211_data *drv = bss->drv;
9897 struct nl_msg *msg;
9898 struct nlattr *data;
a862e4a3 9899 int ret;
16689c7c
PX
9900 int mode;
9901
9902 mode = hw_mode_to_qca_acs(params->hw_mode);
9903 if (mode < 0)
9904 return -1;
9905
9725b784 9906 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
9907 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9908 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9909 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9910 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9911 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9912 (params->ht_enabled &&
9913 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9914 (params->ht40_enabled &&
857d9422
MM
9915 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9916 (params->vht_enabled &&
9917 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9918 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9919 params->ch_width) ||
9920 (params->ch_list_len &&
9921 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
d0cdccd3
PX
9922 params->ch_list)) ||
9923 add_acs_freq_list(msg, params->freq_list)) {
a862e4a3
JM
9924 nlmsg_free(msg);
9925 return -ENOBUFS;
9926 }
16689c7c
PX
9927 nla_nest_end(msg, data);
9928
857d9422
MM
9929 wpa_printf(MSG_DEBUG,
9930 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9931 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9932 params->vht_enabled, params->ch_width, params->ch_list_len);
9933
16689c7c 9934 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
16689c7c
PX
9935 if (ret) {
9936 wpa_printf(MSG_DEBUG,
9937 "nl80211: Failed to invoke driver ACS function: %s",
9938 strerror(errno));
9939 }
16689c7c
PX
9940 return ret;
9941}
9942
9943
844dfeb8
SD
9944static int nl80211_set_band(void *priv, enum set_band band)
9945{
9946 struct i802_bss *bss = priv;
9947 struct wpa_driver_nl80211_data *drv = bss->drv;
9948 struct nl_msg *msg;
9949 struct nlattr *data;
9950 int ret;
9951 enum qca_set_band qca_band;
9952
9953 if (!drv->setband_vendor_cmd_avail)
9954 return -1;
9955
9956 switch (band) {
9957 case WPA_SETBAND_AUTO:
9958 qca_band = QCA_SETBAND_AUTO;
9959 break;
9960 case WPA_SETBAND_5G:
9961 qca_band = QCA_SETBAND_5G;
9962 break;
9963 case WPA_SETBAND_2G:
9964 qca_band = QCA_SETBAND_2G;
9965 break;
9966 default:
9967 return -1;
9968 }
9969
9970 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9971 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9972 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9973 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9974 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9975 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9976 nlmsg_free(msg);
9977 return -ENOBUFS;
9978 }
9979 nla_nest_end(msg, data);
9980
9981 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9982 if (ret) {
9983 wpa_printf(MSG_DEBUG,
9984 "nl80211: Driver setband function failed: %s",
9985 strerror(errno));
9986 }
9987 return ret;
9988}
9989
9990
98342208
AK
9991struct nl80211_pcl {
9992 unsigned int num;
9993 unsigned int *freq_list;
9994};
9995
9996static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9997{
9998 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9999 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10000 struct nl80211_pcl *param = arg;
10001 struct nlattr *nl_vend, *attr;
10002 enum qca_iface_type iface_type;
10003 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10004 unsigned int num, max_num;
10005 u32 *freqs;
10006
10007 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10008 genlmsg_attrlen(gnlh, 0), NULL);
10009
10010 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
10011 if (!nl_vend)
10012 return NL_SKIP;
10013
10014 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10015 nla_data(nl_vend), nla_len(nl_vend), NULL);
10016
10017 attr = tb_vendor[
10018 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
10019 if (!attr) {
10020 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
10021 param->num = 0;
10022 return NL_SKIP;
10023 }
10024
10025 iface_type = (enum qca_iface_type) nla_get_u32(attr);
10026 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
10027 iface_type);
10028
10029 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
10030 if (!attr) {
10031 wpa_printf(MSG_ERROR,
10032 "nl80211: preferred_freq_list couldn't be found");
10033 param->num = 0;
10034 return NL_SKIP;
10035 }
10036
10037 /*
10038 * param->num has the maximum number of entries for which there
10039 * is room in the freq_list provided by the caller.
10040 */
10041 freqs = nla_data(attr);
10042 max_num = nla_len(attr) / sizeof(u32);
10043 if (max_num > param->num)
10044 max_num = param->num;
10045 for (num = 0; num < max_num; num++)
10046 param->freq_list[num] = freqs[num];
10047 param->num = num;
10048
10049 return NL_SKIP;
10050}
10051
10052
10053static int nl80211_get_pref_freq_list(void *priv,
10054 enum wpa_driver_if_type if_type,
10055 unsigned int *num,
10056 unsigned int *freq_list)
10057{
10058 struct i802_bss *bss = priv;
10059 struct wpa_driver_nl80211_data *drv = bss->drv;
10060 struct nl_msg *msg;
10061 int ret;
10062 unsigned int i;
10063 struct nlattr *params;
10064 struct nl80211_pcl param;
10065 enum qca_iface_type iface_type;
10066
10067 if (!drv->get_pref_freq_list)
10068 return -1;
10069
10070 switch (if_type) {
10071 case WPA_IF_STATION:
10072 iface_type = QCA_IFACE_TYPE_STA;
10073 break;
10074 case WPA_IF_AP_BSS:
10075 iface_type = QCA_IFACE_TYPE_AP;
10076 break;
10077 case WPA_IF_P2P_GO:
10078 iface_type = QCA_IFACE_TYPE_P2P_GO;
10079 break;
10080 case WPA_IF_P2P_CLIENT:
10081 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
10082 break;
10083 case WPA_IF_IBSS:
10084 iface_type = QCA_IFACE_TYPE_IBSS;
10085 break;
10086 case WPA_IF_TDLS:
10087 iface_type = QCA_IFACE_TYPE_TDLS;
10088 break;
10089 default:
10090 return -1;
10091 }
10092
10093 param.num = *num;
10094 param.freq_list = freq_list;
10095
10096 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10097 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
10098 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10099 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10100 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
10101 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10102 nla_put_u32(msg,
10103 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
10104 iface_type)) {
10105 wpa_printf(MSG_ERROR,
10106 "%s: err in adding vendor_cmd and vendor_data",
10107 __func__);
10108 nlmsg_free(msg);
10109 return -1;
10110 }
10111 nla_nest_end(msg, params);
10112
10113 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
10114 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
10115 if (ret) {
10116 wpa_printf(MSG_ERROR,
10117 "%s: err in send_and_recv_msgs", __func__);
10118 return ret;
10119 }
10120
10121 *num = param.num;
10122
10123 for (i = 0; i < *num; i++) {
10124 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
10125 i, freq_list[i]);
10126 }
10127
10128 return 0;
10129}
10130
10131
7c813acf
AK
10132static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
10133{
10134 struct i802_bss *bss = priv;
10135 struct wpa_driver_nl80211_data *drv = bss->drv;
10136 struct nl_msg *msg;
10137 int ret;
10138 struct nlattr *params;
10139
10140 if (!drv->set_prob_oper_freq)
10141 return -1;
10142
10143 wpa_printf(MSG_DEBUG,
10144 "nl80211: Set P2P probable operating freq %u for ifindex %d",
10145 freq, bss->ifindex);
10146
10147 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10148 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10149 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10150 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
10151 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
10152 nla_put_u32(msg,
10153 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
10154 QCA_IFACE_TYPE_P2P_CLIENT) ||
10155 nla_put_u32(msg,
10156 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
10157 freq)) {
10158 wpa_printf(MSG_ERROR,
10159 "%s: err in adding vendor_cmd and vendor_data",
10160 __func__);
10161 nlmsg_free(msg);
10162 return -1;
10163 }
10164 nla_nest_end(msg, params);
10165
10166 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10167 msg = NULL;
10168 if (ret) {
10169 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
10170 __func__);
10171 return ret;
10172 }
10173 nlmsg_free(msg);
10174 return 0;
10175}
10176
a6f5b193
PX
10177
10178static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
10179 unsigned int period, unsigned int interval,
10180 unsigned int count, const u8 *device_types,
10181 size_t dev_types_len,
10182 const u8 *ies, size_t ies_len)
10183{
10184 struct i802_bss *bss = priv;
10185 struct wpa_driver_nl80211_data *drv = bss->drv;
10186 struct nl_msg *msg;
10187 struct nlattr *container;
10188 int ret;
10189
10190 wpa_printf(MSG_DEBUG,
10191 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
10192 freq, period, interval, count);
10193
10194 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10195 return -1;
10196
10197 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10198 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10199 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10200 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
10201 goto fail;
10202
10203 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10204 if (!container)
10205 goto fail;
10206
10207 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
10208 freq) ||
10209 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
10210 period) ||
10211 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
10212 interval) ||
10213 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
10214 count) ||
10215 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
10216 dev_types_len, device_types) ||
10217 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
10218 ies_len, ies))
10219 goto fail;
10220
10221 nla_nest_end(msg, container);
10222 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10223 msg = NULL;
10224 if (ret) {
10225 wpa_printf(MSG_DEBUG,
10226 "nl80211: Failed to send P2P Listen offload vendor command");
10227 goto fail;
10228 }
10229
10230 return 0;
10231
10232fail:
10233 nlmsg_free(msg);
10234 return -1;
10235}
10236
10237
10238static int nl80211_p2p_lo_stop(void *priv)
10239{
10240 struct i802_bss *bss = priv;
10241 struct wpa_driver_nl80211_data *drv = bss->drv;
10242 struct nl_msg *msg;
10243
10244 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10245
10246 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10247 return -1;
10248
10249 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10250 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10251 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10252 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10253 nlmsg_free(msg);
10254 return -1;
10255 }
10256
10257 return send_and_recv_msgs(drv, msg, NULL, NULL);
10258}
10259
2e4e4fb7
SD
10260
10261static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10262{
10263 struct i802_bss *bss = priv;
10264 struct wpa_driver_nl80211_data *drv = bss->drv;
10265 struct nl_msg *msg;
10266 struct nlattr *params;
10267 int ret;
10268 u32 tdls_mode;
10269
10270 wpa_printf(MSG_DEBUG,
10271 "nl80211: Set TDKS mode: tdls_external_control=%d",
10272 tdls_external_control);
10273
10274 if (tdls_external_control == 1)
10275 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10276 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10277 else
10278 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10279
10280 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10281 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10282 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10283 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10284 goto fail;
10285
10286 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10287 if (!params)
10288 goto fail;
10289
10290 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10291 tdls_mode))
10292 goto fail;
10293
10294 nla_nest_end(msg, params);
10295
10296 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10297 msg = NULL;
10298 if (ret) {
10299 wpa_printf(MSG_ERROR,
10300 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10301 ret, strerror(-ret));
10302 goto fail;
10303 }
10304 return 0;
10305fail:
10306 nlmsg_free(msg);
10307 return -1;
10308}
10309
3ab48492
KV
10310
10311#ifdef CONFIG_MBO
10312
10313static enum mbo_transition_reject_reason
10314nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10315{
10316 switch (status) {
10317 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10318 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10319 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10320 return MBO_TRANSITION_REJECT_REASON_DELAY;
10321 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10322 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10323 case QCA_STATUS_REJECT_LOW_RSSI:
10324 return MBO_TRANSITION_REJECT_REASON_RSSI;
10325 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10326 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10327 case QCA_STATUS_REJECT_UNKNOWN:
10328 default:
10329 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10330 }
10331}
10332
10333
10334static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10335 struct nlattr *tb[], int num)
10336{
10337 enum qca_wlan_btm_candidate_status status;
10338 char buf[50];
10339
10340 os_memcpy(candidate->bssid,
10341 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10342 ETH_ALEN);
10343
10344 status = nla_get_u32(
10345 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10346 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10347 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10348
10349 if (candidate->is_accept)
10350 os_snprintf(buf, sizeof(buf), "Accepted");
10351 else
10352 os_snprintf(buf, sizeof(buf),
10353 "Rejected, Reject_reason: %d",
10354 candidate->reject_reason);
10355 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10356 num, MAC2STR(candidate->bssid), buf);
10357}
10358
10359
10360static int
10361nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10362{
10363 struct wpa_bss_candidate_info *info = arg;
10364 struct candidate_list *candidate = info->candidates;
10365 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10366 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10367 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10368 static struct nla_policy policy[
10369 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10370 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10371 .minlen = ETH_ALEN
10372 },
10373 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10374 .type = NLA_U32,
10375 },
10376 };
10377 struct nlattr *attr;
10378 int rem;
10379 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10380 u8 num;
10381
10382 num = info->num; /* number of candidates sent to driver */
10383 info->num = 0;
10384 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10385 genlmsg_attrlen(gnlh, 0), NULL);
10386
10387 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10388 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10389 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10390 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10391 return NL_SKIP;
10392
10393 wpa_printf(MSG_DEBUG,
10394 "nl80211: WNM Candidate list received from driver");
10395 nla_for_each_nested(attr,
10396 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10397 rem) {
10398 if (info->num >= num ||
10399 nla_parse_nested(
10400 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10401 attr, policy) ||
10402 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10403 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10404 break;
10405
10406 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10407
10408 candidate++;
10409 info->num++;
10410 }
10411
10412 return NL_SKIP;
10413}
10414
10415
10416static struct wpa_bss_candidate_info *
10417nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10418{
10419 struct i802_bss *bss = priv;
10420 struct wpa_driver_nl80211_data *drv = bss->drv;
10421 struct nl_msg *msg;
10422 struct nlattr *attr, *attr1, *attr2;
10423 struct wpa_bss_candidate_info *info;
10424 u8 i;
10425 int ret;
10426 u8 *pos;
10427
10428 if (!drv->fetch_bss_trans_status)
10429 return NULL;
10430
10431 info = os_zalloc(sizeof(*info));
10432 if (!info)
10433 return NULL;
10434 /* Allocate memory for number of candidates sent to driver */
10435 info->candidates = os_calloc(params->n_candidates,
10436 sizeof(*info->candidates));
10437 if (!info->candidates) {
10438 os_free(info);
10439 return NULL;
10440 }
10441
10442 /* Copy the number of candidates being sent to driver. This is used in
10443 * nl80211_get_bss_transition_status_handler() to limit the number of
10444 * candidates that can be populated in info->candidates and will be
10445 * later overwritten with the actual number of candidates received from
10446 * the driver.
10447 */
10448 info->num = params->n_candidates;
10449
10450 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10451 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10452 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10453 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10454 goto fail;
10455
10456 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10457 if (!attr)
10458 goto fail;
10459
10460 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10461 params->mbo_transition_reason))
10462 goto fail;
10463
10464 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10465 if (!attr1)
10466 goto fail;
10467
10468 wpa_printf(MSG_DEBUG,
10469 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10470 params->mbo_transition_reason, params->n_candidates);
10471 pos = params->bssid;
10472 for (i = 0; i < params->n_candidates; i++) {
10473 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
10474 MAC2STR(pos));
10475 attr2 = nla_nest_start(msg, i);
10476 if (!attr2 ||
10477 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10478 ETH_ALEN, pos))
10479 goto fail;
10480 pos += ETH_ALEN;
10481 nla_nest_end(msg, attr2);
10482 }
10483
10484 nla_nest_end(msg, attr1);
10485 nla_nest_end(msg, attr);
10486
10487 ret = send_and_recv_msgs(drv, msg,
10488 nl80211_get_bss_transition_status_handler,
10489 info);
10490 msg = NULL;
10491 if (ret) {
10492 wpa_printf(MSG_ERROR,
10493 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10494 ret, strerror(-ret));
10495 goto fail;
10496 }
10497 return info;
10498
10499fail:
10500 nlmsg_free(msg);
10501 os_free(info->candidates);
10502 os_free(info);
10503 return NULL;
10504}
10505
178553b7
VK
10506
10507/**
10508 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10509 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10510 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10511 * Returns: 0 on success, -1 on failure
10512 */
10513static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10514{
10515 struct i802_bss *bss = priv;
10516 struct wpa_driver_nl80211_data *drv = bss->drv;
10517 struct nl_msg *msg;
10518 struct nlattr *attr;
10519 int ret = -1;
10520
10521 if (!drv->set_wifi_conf_vendor_cmd_avail)
10522 return -1;
10523
10524 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10525 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10526 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10527 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10528 goto fail;
10529
10530 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10531 if (!attr)
10532 goto fail;
10533
10534 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10535 ignore_disallow);
10536 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10537 ignore_disallow))
10538 goto fail;
10539
10540 nla_nest_end(msg, attr);
10541
10542 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10543 msg = NULL;
10544 if (ret) {
10545 wpa_printf(MSG_ERROR,
10546 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10547 ret, strerror(-ret));
10548 goto fail;
10549 }
10550
10551fail:
10552 nlmsg_free(msg);
10553 return ret;
10554}
10555
3ab48492
KV
10556#endif /* CONFIG_MBO */
10557
b658547d
JM
10558#endif /* CONFIG_DRIVER_NL80211_QCA */
10559
7c813acf 10560
6922d440
IP
10561static int nl80211_write_to_file(const char *name, unsigned int val)
10562{
10563 int fd, len;
10564 char tmp[128];
10565
10566 fd = open(name, O_RDWR);
10567 if (fd < 0) {
10568 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10569 name, strerror(errno));
10570 return fd;
10571 }
10572
10573 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10574 len = write(fd, tmp, len);
10575 if (len < 0)
10576 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10577 name, strerror(errno));
10578 close(fd);
10579
10580 return 0;
10581}
10582
10583
10584static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10585{
10586 struct i802_bss *bss = priv;
10587 char path[128];
10588 int ret;
10589
10590 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10591 filter_flags);
10592
10593 /* Configure filtering of unicast frame encrypted using GTK */
10594 ret = os_snprintf(path, sizeof(path),
10595 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10596 bss->ifname);
bd86ea08
JM
10597 if (os_snprintf_error(sizeof(path), ret))
10598 return -1;
6922d440
IP
10599
10600 ret = nl80211_write_to_file(path,
10601 !!(filter_flags &
10602 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10603 if (ret) {
10604 wpa_printf(MSG_ERROR,
10605 "nl80211: Failed to set IPv4 unicast in multicast filter");
10606 return ret;
10607 }
10608
10609 os_snprintf(path, sizeof(path),
10610 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10611 bss->ifname);
10612 ret = nl80211_write_to_file(path,
10613 !!(filter_flags &
10614 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10615
10616 if (ret) {
10617 wpa_printf(MSG_ERROR,
10618 "nl80211: Failed to set IPv6 unicast in multicast filter");
10619 return ret;
10620 }
10621
10622 /* Configure filtering of unicast frame encrypted using GTK */
10623 os_snprintf(path, sizeof(path),
10624 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10625 bss->ifname);
10626 ret = nl80211_write_to_file(path,
10627 !!(filter_flags &
10628 WPA_DATA_FRAME_FILTER_FLAG_ARP));
10629 if (ret) {
10630 wpa_printf(MSG_ERROR,
10631 "nl80211: Failed set gratuitous ARP filter");
10632 return ret;
10633 }
10634
10635 /* Configure filtering of IPv6 NA frames */
10636 os_snprintf(path, sizeof(path),
10637 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10638 bss->ifname);
10639 ret = nl80211_write_to_file(path,
10640 !!(filter_flags &
10641 WPA_DATA_FRAME_FILTER_FLAG_NA));
10642 if (ret) {
10643 wpa_printf(MSG_ERROR,
10644 "nl80211: Failed to set unsolicited NA filter");
10645 return ret;
10646 }
10647
10648 return 0;
10649}
10650
10651
cc9a2575
KV
10652static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10653 const u8 **ext_capa, const u8 **ext_capa_mask,
10654 unsigned int *ext_capa_len)
10655{
10656 struct i802_bss *bss = priv;
10657 struct wpa_driver_nl80211_data *drv = bss->drv;
10658 enum nl80211_iftype nlmode;
10659 unsigned int i;
10660
10661 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10662 return -1;
10663
10664 nlmode = wpa_driver_nl80211_if_type(type);
10665
10666 /* By default, use the per-radio values */
10667 *ext_capa = drv->extended_capa;
10668 *ext_capa_mask = drv->extended_capa_mask;
10669 *ext_capa_len = drv->extended_capa_len;
10670
10671 /* Replace the default value if a per-interface type value exists */
10672 for (i = 0; i < drv->num_iface_ext_capa; i++) {
10673 if (nlmode == drv->iface_ext_capa[i].iftype) {
10674 *ext_capa = drv->iface_ext_capa[i].ext_capa;
10675 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10676 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10677 break;
10678 }
10679 }
10680
10681 return 0;
10682}
10683
10684
3c67e977
VK
10685static int nl80211_update_connection_params(
10686 void *priv, struct wpa_driver_associate_params *params,
10687 enum wpa_drv_update_connect_params_mask mask)
10688{
10689 struct i802_bss *bss = priv;
10690 struct wpa_driver_nl80211_data *drv = bss->drv;
10691 struct nl_msg *msg;
10692 int ret = -1;
10693 enum nl80211_auth_type type;
10694
10695 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10696 if (!msg)
10697 goto fail;
10698
10699 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10700 drv->ifindex);
10701
10702 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10703 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10704 params->wpa_ie))
10705 goto fail;
10706 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
10707 params->wpa_ie_len);
10708 }
10709
10710 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10711 type = get_nl_auth_type(params->auth_alg);
10712 if (type == NL80211_AUTHTYPE_MAX ||
10713 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10714 goto fail;
10715 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
10716 }
10717
10718 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10719 nl80211_put_fils_connect_params(drv, params, msg))
10720 goto fail;
10721
10722 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10723 msg = NULL;
10724 if (ret)
10725 wpa_dbg(drv->ctx, MSG_DEBUG,
10726 "nl80211: Update connect params command failed: ret=%d (%s)",
10727 ret, strerror(-ret));
10728
10729fail:
10730 nlmsg_free(msg);
10731 return ret;
10732}
10733
10734
ba71cb82
SD
10735static int nl80211_send_external_auth_status(void *priv,
10736 struct external_auth *params)
10737{
10738 struct i802_bss *bss = priv;
10739 struct wpa_driver_nl80211_data *drv = bss->drv;
10740 struct nl_msg *msg = NULL;
10741 int ret = -1;
10742
10743 wpa_dbg(drv->ctx, MSG_DEBUG,
10744 "nl80211: External auth status: %u", params->status);
10745
10746 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10747 if (!msg ||
10748 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10749 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10750 params->ssid) ||
10751 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10752 goto fail;
10753 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10754 msg = NULL;
10755 if (ret) {
10756 wpa_printf(MSG_DEBUG,
10757 "nl80211: External Auth status update failed: ret=%d (%s)",
10758 ret, strerror(-ret));
10759 goto fail;
10760 }
10761fail:
10762 nlmsg_free(msg);
10763 return ret;
10764}
10765
10766
5abc7823
VN
10767static int nl80211_set_4addr_mode(void *priv, const char *bridge_ifname,
10768 int val)
10769{
10770 struct i802_bss *bss = priv;
10771 struct wpa_driver_nl80211_data *drv = bss->drv;
10772 struct nl_msg *msg;
10773 int ret = -ENOBUFS;
10774
10775 wpa_printf(MSG_DEBUG, "nl80211: %s 4addr mode (bridge_ifname: %s)",
10776 val ? "Enable" : "Disable", bridge_ifname);
10777
10778 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
10779 if (!msg || nla_put_u8(msg, NL80211_ATTR_4ADDR, val))
10780 goto fail;
10781
10782 if (bridge_ifname[0] && bss->added_if_into_bridge && !val) {
10783 if (linux_br_del_if(drv->global->ioctl_sock,
10784 bridge_ifname, bss->ifname)) {
10785 wpa_printf(MSG_ERROR,
10786 "nl80211: Failed to remove interface %s from bridge %s",
10787 bss->ifname, bridge_ifname);
10788 return -1;
10789 }
10790 bss->added_if_into_bridge = 0;
10791 }
10792
10793 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10794 msg = NULL;
10795 if (!ret) {
10796 if (bridge_ifname[0] && val &&
10797 i802_check_bridge(drv, bss, bridge_ifname, bss->ifname) < 0)
10798 return -1;
10799 return 0;
10800 }
10801
10802fail:
10803 nlmsg_free(msg);
10804 wpa_printf(MSG_ERROR, "nl80211: Failed to enable/disable 4addr");
10805
10806 return ret;
10807}
10808
10809
3f5285e8
JM
10810const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10811 .name = "nl80211",
10812 .desc = "Linux nl80211/cfg80211",
10813 .get_bssid = wpa_driver_nl80211_get_bssid,
10814 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
10815 .set_key = driver_nl80211_set_key,
10816 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
10817 .sched_scan = wpa_driver_nl80211_sched_scan,
10818 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 10819 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
4f30addb 10820 .abort_scan = wpa_driver_nl80211_abort_scan,
9ebce9c5
JM
10821 .deauthenticate = driver_nl80211_deauthenticate,
10822 .authenticate = driver_nl80211_authenticate,
3f5285e8 10823 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
10824 .global_init = nl80211_global_init,
10825 .global_deinit = nl80211_global_deinit,
10826 .init2 = wpa_driver_nl80211_init,
9ebce9c5 10827 .deinit = driver_nl80211_deinit,
3f5285e8
JM
10828 .get_capa = wpa_driver_nl80211_get_capa,
10829 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 10830 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 10831 .set_country = wpa_driver_nl80211_set_country,
f0793bf1 10832 .get_country = wpa_driver_nl80211_get_country,
19c3b566 10833 .set_ap = wpa_driver_nl80211_set_ap,
3c4ca363 10834 .set_acl = wpa_driver_nl80211_set_acl,
22a7c9d7 10835 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
10836 .if_remove = driver_nl80211_if_remove,
10837 .send_mlme = driver_nl80211_send_mlme,
0fafeb54 10838 .get_hw_feature_data = nl80211_get_hw_feature_data,
0f4e8b4f 10839 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 10840 .sta_remove = driver_nl80211_sta_remove,
db149ac9 10841 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 10842 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837 10843 .hapd_init = i802_init,
c5121837 10844 .hapd_deinit = i802_deinit,
f7b3920c 10845 .set_wds_sta = i802_set_wds_sta,
c5121837
JM
10846 .get_seqnum = i802_get_seqnum,
10847 .flush = i802_flush,
c5121837
JM
10848 .get_inact_sec = i802_get_inact_sec,
10849 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
10850 .set_rts = i802_set_rts,
10851 .set_frag = i802_set_frag,
c5121837 10852 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 10853 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
10854 .sta_deauth = i802_sta_deauth,
10855 .sta_disassoc = i802_sta_disassoc,
9ebce9c5 10856 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 10857 .set_freq = i802_set_freq,
9ebce9c5 10858 .send_action = driver_nl80211_send_action,
5dfca53f 10859 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
10860 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10861 .cancel_remain_on_channel =
10862 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 10863 .probe_req_report = driver_nl80211_probe_req_report,
af473088 10864 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 10865 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 10866 .resume = wpa_driver_nl80211_resume,
b625473c 10867 .signal_monitor = nl80211_signal_monitor,
1c5c7273 10868 .signal_poll = nl80211_signal_poll,
7f00dc6e 10869 .channel_info = nl80211_channel_info,
b91ab76e 10870 .send_frame = nl80211_send_frame,
c55f774d 10871 .set_param = nl80211_set_param,
6859f1cb 10872 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
10873 .add_pmkid = nl80211_add_pmkid,
10874 .remove_pmkid = nl80211_remove_pmkid,
10875 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 10876 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 10877 .poll_client = nl80211_poll_client,
29f338af 10878 .set_p2p_powersave = nl80211_set_p2p_powersave,
f90e9c1c 10879 .start_dfs_cac = nl80211_start_radar_detection,
695c7038 10880 .stop_ap = wpa_driver_nl80211_stop_ap,
03ea1786
AN
10881#ifdef CONFIG_TDLS
10882 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10883 .tdls_oper = nl80211_tdls_oper,
72b2605f
AN
10884 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10885 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
03ea1786 10886#endif /* CONFIG_TDLS */
6a1ce395 10887 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
597b94f5 10888 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
0185007c 10889 .get_survey = wpa_driver_nl80211_get_survey,
a771c07d 10890 .status = wpa_driver_nl80211_status,
1c4ffa87 10891 .switch_channel = nl80211_switch_channel,
0de38036
JM
10892#ifdef ANDROID_P2P
10893 .set_noa = wpa_driver_set_p2p_noa,
10894 .get_noa = wpa_driver_get_p2p_noa,
10895 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
10896#endif /* ANDROID_P2P */
5e2c3490 10897#ifdef ANDROID
ded14ce9 10898#ifndef ANDROID_LIB_STUB
5e2c3490 10899 .driver_cmd = wpa_driver_nl80211_driver_cmd,
ded14ce9 10900#endif /* !ANDROID_LIB_STUB */
5e2c3490 10901#endif /* ANDROID */
adef8948 10902 .vendor_cmd = nl80211_vendor_cmd,
049105b4 10903 .set_qos_map = nl80211_set_qos_map,
e4fa8b12 10904 .set_wowlan = nl80211_set_wowlan,
fee354c7 10905 .set_mac_addr = nl80211_set_mac_addr,
6c1664f6
BC
10906#ifdef CONFIG_MESH
10907 .init_mesh = wpa_driver_nl80211_init_mesh,
10908 .join_mesh = wpa_driver_nl80211_join_mesh,
10909 .leave_mesh = wpa_driver_nl80211_leave_mesh,
10910#endif /* CONFIG_MESH */
71103bed
KP
10911 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10912 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
73d2294f 10913 .br_port_set_attr = wpa_driver_br_port_set_attr,
7565752d 10914 .br_set_net_param = wpa_driver_br_set_net_param,
dfa87878
MB
10915 .add_tx_ts = nl80211_add_ts,
10916 .del_tx_ts = nl80211_del_ts,
45e3fc72 10917 .get_ifindex = nl80211_get_ifindex,
b658547d 10918#ifdef CONFIG_DRIVER_NL80211_QCA
9607a1ae 10919 .roaming = nl80211_roaming,
d98038bb 10920 .disable_fils = nl80211_disable_fils,
16689c7c 10921 .do_acs = wpa_driver_do_acs,
844dfeb8 10922 .set_band = nl80211_set_band,
98342208 10923 .get_pref_freq_list = nl80211_get_pref_freq_list,
7c813acf 10924 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
a6f5b193
PX
10925 .p2p_lo_start = nl80211_p2p_lo_start,
10926 .p2p_lo_stop = nl80211_p2p_lo_stop,
cc9985d1 10927 .set_default_scan_ies = nl80211_set_default_scan_ies,
2e4e4fb7 10928 .set_tdls_mode = nl80211_set_tdls_mode,
3ab48492
KV
10929#ifdef CONFIG_MBO
10930 .get_bss_transition_status = nl80211_get_bss_transition_status,
178553b7 10931 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
3ab48492 10932#endif /* CONFIG_MBO */
b04854ce 10933 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
b658547d 10934#endif /* CONFIG_DRIVER_NL80211_QCA */
6922d440 10935 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
cc9a2575 10936 .get_ext_capab = nl80211_get_ext_capab,
3c67e977 10937 .update_connect_params = nl80211_update_connection_params,
ba71cb82 10938 .send_external_auth_status = nl80211_send_external_auth_status,
5abc7823 10939 .set_4addr_mode = nl80211_set_4addr_mode,
3f5285e8 10940};