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