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