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