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