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