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