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