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