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