]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/drivers/driver_nl80211.c
hostapd: Add last_ack_rssi into ctrl iface cmd STA
[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 nl80211_set_pmk(struct wpa_driver_nl80211_data *drv,
2816 const u8 *key, size_t key_len,
2817 const u8 *addr)
2818 {
2819 struct nl_msg *msg = NULL;
2820 int ret;
2821
2822 /*
2823 * If the authenticator address is not set, assume it is
2824 * the current BSSID.
2825 */
2826 if (!addr && drv->associated)
2827 addr = drv->bssid;
2828 else if (!addr)
2829 return -1;
2830
2831 wpa_printf(MSG_DEBUG, "nl80211: Set PMK to the driver for " MACSTR,
2832 MAC2STR(addr));
2833 wpa_hexdump_key(MSG_DEBUG, "nl80211: PMK", key, key_len);
2834 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_PMK);
2835 if (!msg ||
2836 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
2837 nla_put(msg, NL80211_ATTR_PMK, key_len, key)) {
2838 nl80211_nlmsg_clear(msg);
2839 nlmsg_free(msg);
2840 return -ENOBUFS;
2841 }
2842
2843 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2844 if (ret) {
2845 wpa_printf(MSG_DEBUG, "nl80211: Set PMK failed: ret=%d (%s)",
2846 ret, strerror(-ret));
2847 }
2848
2849 return ret;
2850 }
2851
2852
2853 static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
2854 enum wpa_alg alg, const u8 *addr,
2855 int key_idx, int set_tx,
2856 const u8 *seq, size_t seq_len,
2857 const u8 *key, size_t key_len)
2858 {
2859 struct wpa_driver_nl80211_data *drv = bss->drv;
2860 int ifindex;
2861 struct nl_msg *msg = NULL;
2862 int ret;
2863 int tdls = 0;
2864
2865 /* Ignore for P2P Device */
2866 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2867 return 0;
2868
2869 ifindex = if_nametoindex(ifname);
2870 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
2871 "set_tx=%d seq_len=%lu key_len=%lu",
2872 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
2873 (unsigned long) seq_len, (unsigned long) key_len);
2874 #ifdef CONFIG_TDLS
2875 if (key_idx == -1) {
2876 key_idx = 0;
2877 tdls = 1;
2878 }
2879 #endif /* CONFIG_TDLS */
2880
2881 #ifdef CONFIG_DRIVER_NL80211_QCA
2882 if (alg == WPA_ALG_PMK &&
2883 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2884 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2885 __func__);
2886 ret = issue_key_mgmt_set_key(drv, key, key_len);
2887 return ret;
2888 }
2889 #endif /* CONFIG_DRIVER_NL80211_QCA */
2890
2891 if (alg == WPA_ALG_PMK &&
2892 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2893 return nl80211_set_pmk(drv, key, key_len, addr);
2894
2895 if (alg == WPA_ALG_NONE) {
2896 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2897 if (!msg)
2898 return -ENOBUFS;
2899 } else {
2900 u32 suite;
2901
2902 suite = wpa_alg_to_cipher_suite(alg, key_len);
2903 if (!suite)
2904 goto fail;
2905 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2906 if (!msg ||
2907 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
2908 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
2909 goto fail;
2910 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
2911 }
2912
2913 if (seq && seq_len) {
2914 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2915 goto fail;
2916 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2917 }
2918
2919 if (addr && !is_broadcast_ether_addr(addr)) {
2920 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
2921 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2922 goto fail;
2923
2924 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2925 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
2926 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2927 NL80211_KEYTYPE_GROUP))
2928 goto fail;
2929 }
2930 } else if (addr && is_broadcast_ether_addr(addr)) {
2931 struct nlattr *types;
2932
2933 wpa_printf(MSG_DEBUG, " broadcast key");
2934
2935 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
2936 if (!types ||
2937 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2938 goto fail;
2939 nla_nest_end(msg, types);
2940 }
2941 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2942 goto fail;
2943
2944 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
2945 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2946 ret = 0;
2947 if (ret)
2948 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2949 ret, strerror(-ret));
2950
2951 /*
2952 * If we failed or don't need to set the default TX key (below),
2953 * we're done here.
2954 */
2955 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
2956 return ret;
2957 if (is_ap_interface(drv->nlmode) && addr &&
2958 !is_broadcast_ether_addr(addr))
2959 return ret;
2960
2961 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2962 if (!msg ||
2963 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
2964 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2965 alg == WPA_ALG_BIP_GMAC_128 ||
2966 alg == WPA_ALG_BIP_GMAC_256 ||
2967 alg == WPA_ALG_BIP_CMAC_256) ?
2968 NL80211_ATTR_KEY_DEFAULT_MGMT :
2969 NL80211_ATTR_KEY_DEFAULT))
2970 goto fail;
2971 if (addr && is_broadcast_ether_addr(addr)) {
2972 struct nlattr *types;
2973
2974 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
2975 if (!types ||
2976 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2977 goto fail;
2978 nla_nest_end(msg, types);
2979 } else if (addr) {
2980 struct nlattr *types;
2981
2982 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
2983 if (!types ||
2984 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2985 goto fail;
2986 nla_nest_end(msg, types);
2987 }
2988
2989 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2990 if (ret == -ENOENT)
2991 ret = 0;
2992 if (ret)
2993 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2994 "err=%d %s)", ret, strerror(-ret));
2995 return ret;
2996
2997 fail:
2998 nl80211_nlmsg_clear(msg);
2999 nlmsg_free(msg);
3000 return -ENOBUFS;
3001 }
3002
3003
3004 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3005 int key_idx, int defkey,
3006 const u8 *seq, size_t seq_len,
3007 const u8 *key, size_t key_len)
3008 {
3009 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
3010 u32 suite;
3011
3012 if (!key_attr)
3013 return -1;
3014
3015 suite = wpa_alg_to_cipher_suite(alg, key_len);
3016 if (!suite)
3017 return -1;
3018
3019 if (defkey && alg == WPA_ALG_IGTK) {
3020 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
3021 return -1;
3022 } else if (defkey) {
3023 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
3024 return -1;
3025 }
3026
3027 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
3028 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
3029 (seq && seq_len &&
3030 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
3031 nla_put(msg, NL80211_KEY_DATA, key_len, key))
3032 return -1;
3033
3034 nla_nest_end(msg, key_attr);
3035
3036 return 0;
3037 }
3038
3039
3040 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3041 struct nl_msg *msg)
3042 {
3043 int i, privacy = 0;
3044 struct nlattr *nl_keys, *nl_key;
3045
3046 for (i = 0; i < 4; i++) {
3047 if (!params->wep_key[i])
3048 continue;
3049 privacy = 1;
3050 break;
3051 }
3052 if (params->wps == WPS_MODE_PRIVACY)
3053 privacy = 1;
3054 if (params->pairwise_suite &&
3055 params->pairwise_suite != WPA_CIPHER_NONE)
3056 privacy = 1;
3057
3058 if (!privacy)
3059 return 0;
3060
3061 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3062 return -ENOBUFS;
3063
3064 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3065 if (!nl_keys)
3066 return -ENOBUFS;
3067
3068 for (i = 0; i < 4; i++) {
3069 if (!params->wep_key[i])
3070 continue;
3071
3072 nl_key = nla_nest_start(msg, i);
3073 if (!nl_key ||
3074 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3075 params->wep_key[i]) ||
3076 nla_put_u32(msg, NL80211_KEY_CIPHER,
3077 params->wep_key_len[i] == 5 ?
3078 RSN_CIPHER_SUITE_WEP40 :
3079 RSN_CIPHER_SUITE_WEP104) ||
3080 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
3081 (i == params->wep_tx_keyidx &&
3082 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
3083 return -ENOBUFS;
3084
3085 nla_nest_end(msg, nl_key);
3086 }
3087 nla_nest_end(msg, nl_keys);
3088
3089 return 0;
3090 }
3091
3092
3093 int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3094 const u8 *addr, int cmd, u16 reason_code,
3095 int local_state_change)
3096 {
3097 int ret;
3098 struct nl_msg *msg;
3099
3100 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
3101 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3102 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3103 (local_state_change &&
3104 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3105 nlmsg_free(msg);
3106 return -1;
3107 }
3108
3109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3110 if (ret) {
3111 wpa_dbg(drv->ctx, MSG_DEBUG,
3112 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3113 reason_code, ret, strerror(-ret));
3114 }
3115 return ret;
3116 }
3117
3118
3119 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
3120 int reason_code)
3121 {
3122 int ret;
3123
3124 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
3125 nl80211_mark_disconnected(drv);
3126 /* Disconnect command doesn't need BSSID - it uses cached value */
3127 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3128 reason_code, 0);
3129 /*
3130 * For locally generated disconnect, supplicant already generates a
3131 * DEAUTH event, so ignore the event from NL80211.
3132 */
3133 drv->ignore_next_local_disconnect = ret == 0;
3134
3135 return ret;
3136 }
3137
3138
3139 static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3140 const u8 *addr, int reason_code)
3141 {
3142 struct wpa_driver_nl80211_data *drv = bss->drv;
3143 int ret;
3144
3145 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3146 nl80211_mark_disconnected(drv);
3147 return nl80211_leave_ibss(drv, 1);
3148 }
3149 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3150 return wpa_driver_nl80211_disconnect(drv, reason_code);
3151 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3152 __func__, MAC2STR(addr), reason_code);
3153 nl80211_mark_disconnected(drv);
3154 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3155 reason_code, 0);
3156 /*
3157 * For locally generated deauthenticate, supplicant already generates a
3158 * DEAUTH event, so ignore the event from NL80211.
3159 */
3160 drv->ignore_next_local_deauth = ret == 0;
3161 return ret;
3162 }
3163
3164
3165 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3166 struct wpa_driver_auth_params *params)
3167 {
3168 int i;
3169
3170 drv->auth_freq = params->freq;
3171 drv->auth_alg = params->auth_alg;
3172 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3173 drv->auth_local_state_change = params->local_state_change;
3174 drv->auth_p2p = params->p2p;
3175
3176 if (params->bssid)
3177 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3178 else
3179 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3180
3181 if (params->ssid) {
3182 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3183 drv->auth_ssid_len = params->ssid_len;
3184 } else
3185 drv->auth_ssid_len = 0;
3186
3187
3188 os_free(drv->auth_ie);
3189 drv->auth_ie = NULL;
3190 drv->auth_ie_len = 0;
3191 if (params->ie) {
3192 drv->auth_ie = os_malloc(params->ie_len);
3193 if (drv->auth_ie) {
3194 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3195 drv->auth_ie_len = params->ie_len;
3196 }
3197 }
3198
3199 for (i = 0; i < 4; i++) {
3200 if (params->wep_key[i] && params->wep_key_len[i] &&
3201 params->wep_key_len[i] <= 16) {
3202 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3203 params->wep_key_len[i]);
3204 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3205 } else
3206 drv->auth_wep_key_len[i] = 0;
3207 }
3208 }
3209
3210
3211 static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3212 {
3213 struct wpa_driver_nl80211_data *drv = bss->drv;
3214
3215 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3216 return;
3217
3218 /*
3219 * Looks like we failed to unmask 11b rates previously. This could
3220 * happen, e.g., if the interface was down at the point in time when a
3221 * P2P group was terminated.
3222 */
3223 wpa_printf(MSG_DEBUG,
3224 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3225 bss->ifname);
3226 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3227 }
3228
3229
3230 static enum nl80211_auth_type get_nl_auth_type(int wpa_auth_alg)
3231 {
3232 if (wpa_auth_alg & WPA_AUTH_ALG_OPEN)
3233 return NL80211_AUTHTYPE_OPEN_SYSTEM;
3234 if (wpa_auth_alg & WPA_AUTH_ALG_SHARED)
3235 return NL80211_AUTHTYPE_SHARED_KEY;
3236 if (wpa_auth_alg & WPA_AUTH_ALG_LEAP)
3237 return NL80211_AUTHTYPE_NETWORK_EAP;
3238 if (wpa_auth_alg & WPA_AUTH_ALG_FT)
3239 return NL80211_AUTHTYPE_FT;
3240 if (wpa_auth_alg & WPA_AUTH_ALG_SAE)
3241 return NL80211_AUTHTYPE_SAE;
3242 if (wpa_auth_alg & WPA_AUTH_ALG_FILS)
3243 return NL80211_AUTHTYPE_FILS_SK;
3244 if (wpa_auth_alg & WPA_AUTH_ALG_FILS_SK_PFS)
3245 return NL80211_AUTHTYPE_FILS_SK_PFS;
3246
3247 return NL80211_AUTHTYPE_MAX;
3248 }
3249
3250
3251 static int wpa_driver_nl80211_authenticate(
3252 struct i802_bss *bss, struct wpa_driver_auth_params *params)
3253 {
3254 struct wpa_driver_nl80211_data *drv = bss->drv;
3255 int ret = -1, i;
3256 struct nl_msg *msg;
3257 enum nl80211_auth_type type;
3258 enum nl80211_iftype nlmode;
3259 int count = 0;
3260 int is_retry;
3261
3262 nl80211_unmask_11b_rates(bss);
3263
3264 is_retry = drv->retry_auth;
3265 drv->retry_auth = 0;
3266 drv->ignore_deauth_event = 0;
3267
3268 nl80211_mark_disconnected(drv);
3269 os_memset(drv->auth_bssid, 0, ETH_ALEN);
3270 if (params->bssid)
3271 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3272 else
3273 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
3274 /* FIX: IBSS mode */
3275 nlmode = params->p2p ?
3276 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3277 if (drv->nlmode != nlmode &&
3278 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
3279 return -1;
3280
3281 retry:
3282 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3283 drv->ifindex);
3284
3285 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3286 if (!msg)
3287 goto fail;
3288
3289 for (i = 0; i < 4; i++) {
3290 if (!params->wep_key[i])
3291 continue;
3292 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
3293 NULL, i,
3294 i == params->wep_tx_keyidx, NULL, 0,
3295 params->wep_key[i],
3296 params->wep_key_len[i]);
3297 if (params->wep_tx_keyidx != i)
3298 continue;
3299 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
3300 params->wep_key[i], params->wep_key_len[i]))
3301 goto fail;
3302 }
3303
3304 if (params->bssid) {
3305 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3306 MAC2STR(params->bssid));
3307 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3308 goto fail;
3309 }
3310 if (params->freq) {
3311 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3312 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3313 goto fail;
3314 }
3315 if (params->ssid) {
3316 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3317 params->ssid, params->ssid_len);
3318 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3319 params->ssid))
3320 goto fail;
3321 }
3322 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
3323 if (params->ie &&
3324 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3325 goto fail;
3326 if (params->auth_data) {
3327 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3328 params->auth_data_len);
3329 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3330 params->auth_data))
3331 goto fail;
3332 }
3333 type = get_nl_auth_type(params->auth_alg);
3334 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3335 if (type == NL80211_AUTHTYPE_MAX ||
3336 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3337 goto fail;
3338 if (params->local_state_change) {
3339 wpa_printf(MSG_DEBUG, " * Local state change only");
3340 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3341 goto fail;
3342 }
3343
3344 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3345 msg = NULL;
3346 if (ret) {
3347 wpa_dbg(drv->ctx, MSG_DEBUG,
3348 "nl80211: MLME command failed (auth): ret=%d (%s)",
3349 ret, strerror(-ret));
3350 count++;
3351 if (ret == -EALREADY && count == 1 && params->bssid &&
3352 !params->local_state_change) {
3353 /*
3354 * mac80211 does not currently accept new
3355 * authentication if we are already authenticated. As a
3356 * workaround, force deauthentication and try again.
3357 */
3358 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3359 "after forced deauthentication");
3360 drv->ignore_deauth_event = 1;
3361 wpa_driver_nl80211_deauthenticate(
3362 bss, params->bssid,
3363 WLAN_REASON_PREV_AUTH_NOT_VALID);
3364 nlmsg_free(msg);
3365 goto retry;
3366 }
3367
3368 if (ret == -ENOENT && params->freq && !is_retry) {
3369 /*
3370 * cfg80211 has likely expired the BSS entry even
3371 * though it was previously available in our internal
3372 * BSS table. To recover quickly, start a single
3373 * channel scan on the specified channel.
3374 */
3375 struct wpa_driver_scan_params scan;
3376 int freqs[2];
3377
3378 os_memset(&scan, 0, sizeof(scan));
3379 scan.num_ssids = 1;
3380 if (params->ssid) {
3381 scan.ssids[0].ssid = params->ssid;
3382 scan.ssids[0].ssid_len = params->ssid_len;
3383 }
3384 freqs[0] = params->freq;
3385 freqs[1] = 0;
3386 scan.freqs = freqs;
3387 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3388 "channel scan to refresh cfg80211 BSS "
3389 "entry");
3390 ret = wpa_driver_nl80211_scan(bss, &scan);
3391 if (ret == 0) {
3392 nl80211_copy_auth_params(drv, params);
3393 drv->scan_for_auth = 1;
3394 }
3395 } else if (is_retry) {
3396 /*
3397 * Need to indicate this with an event since the return
3398 * value from the retry is not delivered to core code.
3399 */
3400 union wpa_event_data event;
3401 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3402 "failed");
3403 os_memset(&event, 0, sizeof(event));
3404 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3405 ETH_ALEN);
3406 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3407 &event);
3408 }
3409 } else {
3410 wpa_printf(MSG_DEBUG,
3411 "nl80211: Authentication request send successfully");
3412 }
3413
3414 fail:
3415 nlmsg_free(msg);
3416 return ret;
3417 }
3418
3419
3420 int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
3421 {
3422 struct wpa_driver_auth_params params;
3423 struct i802_bss *bss = drv->first_bss;
3424 int i;
3425
3426 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3427
3428 os_memset(&params, 0, sizeof(params));
3429 params.freq = drv->auth_freq;
3430 params.auth_alg = drv->auth_alg;
3431 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3432 params.local_state_change = drv->auth_local_state_change;
3433 params.p2p = drv->auth_p2p;
3434
3435 if (!is_zero_ether_addr(drv->auth_bssid_))
3436 params.bssid = drv->auth_bssid_;
3437
3438 if (drv->auth_ssid_len) {
3439 params.ssid = drv->auth_ssid;
3440 params.ssid_len = drv->auth_ssid_len;
3441 }
3442
3443 params.ie = drv->auth_ie;
3444 params.ie_len = drv->auth_ie_len;
3445
3446 for (i = 0; i < 4; i++) {
3447 if (drv->auth_wep_key_len[i]) {
3448 params.wep_key[i] = drv->auth_wep_key[i];
3449 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3450 }
3451 }
3452
3453 drv->retry_auth = 1;
3454 return wpa_driver_nl80211_authenticate(bss, &params);
3455 }
3456
3457
3458 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3459 const void *data, size_t len,
3460 int encrypt, int noack,
3461 unsigned int freq, int no_cck,
3462 int offchanok, unsigned int wait_time,
3463 const u16 *csa_offs,
3464 size_t csa_offs_len)
3465 {
3466 struct wpa_driver_nl80211_data *drv = bss->drv;
3467 u64 cookie;
3468 int res;
3469
3470 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3471 freq = nl80211_get_assoc_freq(drv);
3472 wpa_printf(MSG_DEBUG,
3473 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3474 freq);
3475 }
3476 if (freq == 0) {
3477 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3478 bss->freq);
3479 freq = bss->freq;
3480 }
3481
3482 if (drv->use_monitor) {
3483 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
3484 freq, bss->freq);
3485 return nl80211_send_monitor(drv, data, len, encrypt, noack);
3486 }
3487
3488 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
3489 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
3490 &cookie, no_cck, noack, offchanok,
3491 csa_offs, csa_offs_len);
3492 if (res == 0 && !noack) {
3493 const struct ieee80211_mgmt *mgmt;
3494 u16 fc;
3495
3496 mgmt = (const struct ieee80211_mgmt *) data;
3497 fc = le_to_host16(mgmt->frame_control);
3498 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3499 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3500 wpa_printf(MSG_MSGDUMP,
3501 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3502 (long long unsigned int)
3503 drv->send_action_cookie,
3504 (long long unsigned int) cookie);
3505 drv->send_action_cookie = cookie;
3506 }
3507 }
3508
3509 return res;
3510 }
3511
3512
3513 static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3514 size_t data_len, int noack,
3515 unsigned int freq, int no_cck,
3516 int offchanok,
3517 unsigned int wait_time,
3518 const u16 *csa_offs,
3519 size_t csa_offs_len)
3520 {
3521 struct wpa_driver_nl80211_data *drv = bss->drv;
3522 struct ieee80211_mgmt *mgmt;
3523 int encrypt = 1;
3524 u16 fc;
3525
3526 mgmt = (struct ieee80211_mgmt *) data;
3527 fc = le_to_host16(mgmt->frame_control);
3528 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3529 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3530 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3531 fc, fc2str(fc), drv->nlmode);
3532
3533 if ((is_sta_interface(drv->nlmode) ||
3534 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
3535 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3536 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3537 /*
3538 * The use of last_mgmt_freq is a bit of a hack,
3539 * but it works due to the single-threaded nature
3540 * of wpa_supplicant.
3541 */
3542 if (freq == 0) {
3543 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3544 drv->last_mgmt_freq);
3545 freq = drv->last_mgmt_freq;
3546 }
3547 return nl80211_send_frame_cmd(bss, freq, 0,
3548 data, data_len, NULL, 1, noack,
3549 1, csa_offs, csa_offs_len);
3550 }
3551
3552 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
3553 if (freq == 0) {
3554 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3555 bss->freq);
3556 freq = bss->freq;
3557 }
3558 return nl80211_send_frame_cmd(bss, freq,
3559 (int) freq == bss->freq ? 0 :
3560 wait_time,
3561 data, data_len,
3562 &drv->send_action_cookie,
3563 no_cck, noack, offchanok,
3564 csa_offs, csa_offs_len);
3565 }
3566
3567 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3568 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3569 /*
3570 * Only one of the authentication frame types is encrypted.
3571 * In order for static WEP encryption to work properly (i.e.,
3572 * to not encrypt the frame), we need to tell mac80211 about
3573 * the frames that must not be encrypted.
3574 */
3575 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3576 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3577 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3578 encrypt = 0;
3579 }
3580
3581 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
3582 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
3583 noack, freq, no_cck, offchanok,
3584 wait_time, csa_offs,
3585 csa_offs_len);
3586 }
3587
3588
3589 static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3590 {
3591 u8 rates[NL80211_MAX_SUPP_RATES];
3592 u8 rates_len = 0;
3593 int i;
3594
3595 if (!basic_rates)
3596 return 0;
3597
3598 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3599 rates[rates_len++] = basic_rates[i] / 5;
3600
3601 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3602 }
3603
3604
3605 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3606 int slot, int ht_opmode, int ap_isolate,
3607 const int *basic_rates)
3608 {
3609 struct wpa_driver_nl80211_data *drv = bss->drv;
3610 struct nl_msg *msg;
3611
3612 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3613 (cts >= 0 &&
3614 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3615 (preamble >= 0 &&
3616 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3617 (slot >= 0 &&
3618 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3619 (ht_opmode >= 0 &&
3620 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3621 (ap_isolate >= 0 &&
3622 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3623 nl80211_put_basic_rates(msg, basic_rates)) {
3624 nlmsg_free(msg);
3625 return -ENOBUFS;
3626 }
3627
3628 return send_and_recv_msgs(drv, msg, NULL, NULL);
3629 }
3630
3631
3632 static int wpa_driver_nl80211_set_acl(void *priv,
3633 struct hostapd_acl_params *params)
3634 {
3635 struct i802_bss *bss = priv;
3636 struct wpa_driver_nl80211_data *drv = bss->drv;
3637 struct nl_msg *msg;
3638 struct nl_msg *acl;
3639 unsigned int i;
3640 int ret;
3641
3642 if (!(drv->capa.max_acl_mac_addrs))
3643 return -ENOTSUP;
3644
3645 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3646 return -ENOTSUP;
3647
3648 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3649 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3650
3651 acl = nlmsg_alloc();
3652 if (!acl)
3653 return -ENOMEM;
3654 for (i = 0; i < params->num_mac_acl; i++) {
3655 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3656 nlmsg_free(acl);
3657 return -ENOMEM;
3658 }
3659 }
3660
3661 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3662 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3663 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3664 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3665 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3666 nlmsg_free(msg);
3667 nlmsg_free(acl);
3668 return -ENOMEM;
3669 }
3670 nlmsg_free(acl);
3671
3672 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3673 if (ret) {
3674 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3675 ret, strerror(-ret));
3676 }
3677
3678 return ret;
3679 }
3680
3681
3682 static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3683 {
3684 if (beacon_int > 0) {
3685 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3686 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3687 beacon_int);
3688 }
3689
3690 return 0;
3691 }
3692
3693
3694 static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3695 {
3696 if (dtim_period > 0) {
3697 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3698 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3699 }
3700
3701 return 0;
3702 }
3703
3704
3705 #ifdef CONFIG_MESH
3706 static int nl80211_set_mesh_config(void *priv,
3707 struct wpa_driver_mesh_bss_params *params)
3708 {
3709 struct i802_bss *bss = priv;
3710 struct wpa_driver_nl80211_data *drv = bss->drv;
3711 struct nl_msg *msg;
3712 int ret;
3713
3714 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3715 if (!msg)
3716 return -1;
3717
3718 ret = nl80211_put_mesh_config(msg, params);
3719 if (ret < 0) {
3720 nlmsg_free(msg);
3721 return ret;
3722 }
3723
3724 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3725 if (ret) {
3726 wpa_printf(MSG_ERROR,
3727 "nl80211: Mesh config set failed: %d (%s)",
3728 ret, strerror(-ret));
3729 return ret;
3730 }
3731 return 0;
3732 }
3733 #endif /* CONFIG_MESH */
3734
3735
3736 static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3737 struct wpa_driver_ap_params *params)
3738 {
3739 struct nlattr *bands, *band;
3740 struct nl80211_txrate_vht vht_rate;
3741
3742 if (!params->freq ||
3743 (params->beacon_rate == 0 &&
3744 params->rate_type == BEACON_RATE_LEGACY))
3745 return 0;
3746
3747 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3748 if (!bands)
3749 return -1;
3750
3751 switch (params->freq->mode) {
3752 case HOSTAPD_MODE_IEEE80211B:
3753 case HOSTAPD_MODE_IEEE80211G:
3754 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3755 break;
3756 case HOSTAPD_MODE_IEEE80211A:
3757 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3758 break;
3759 case HOSTAPD_MODE_IEEE80211AD:
3760 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3761 break;
3762 default:
3763 return 0;
3764 }
3765
3766 if (!band)
3767 return -1;
3768
3769 os_memset(&vht_rate, 0, sizeof(vht_rate));
3770
3771 switch (params->rate_type) {
3772 case BEACON_RATE_LEGACY:
3773 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3774 wpa_printf(MSG_INFO,
3775 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3776 return -1;
3777 }
3778
3779 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3780 (u8) params->beacon_rate / 5) ||
3781 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3782 (params->freq->vht_enabled &&
3783 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3784 &vht_rate)))
3785 return -1;
3786
3787 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3788 params->beacon_rate);
3789 break;
3790 case BEACON_RATE_HT:
3791 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3792 wpa_printf(MSG_INFO,
3793 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3794 return -1;
3795 }
3796 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3797 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3798 (params->freq->vht_enabled &&
3799 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3800 &vht_rate)))
3801 return -1;
3802 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3803 params->beacon_rate);
3804 break;
3805 case BEACON_RATE_VHT:
3806 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3807 wpa_printf(MSG_INFO,
3808 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3809 return -1;
3810 }
3811 vht_rate.mcs[0] = BIT(params->beacon_rate);
3812 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3813 return -1;
3814 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3815 return -1;
3816 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3817 &vht_rate))
3818 return -1;
3819 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3820 params->beacon_rate);
3821 break;
3822 }
3823
3824 nla_nest_end(msg, band);
3825 nla_nest_end(msg, bands);
3826
3827 return 0;
3828 }
3829
3830
3831 static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3832 int multicast_to_unicast)
3833 {
3834 struct wpa_driver_nl80211_data *drv = bss->drv;
3835 struct nl_msg *msg;
3836 int ret;
3837
3838 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3839 if (!msg ||
3840 (multicast_to_unicast &&
3841 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3842 wpa_printf(MSG_ERROR,
3843 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3844 bss->ifname);
3845 nlmsg_free(msg);
3846 return -ENOBUFS;
3847 }
3848
3849 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3850
3851 switch (ret) {
3852 case 0:
3853 wpa_printf(MSG_DEBUG,
3854 "nl80211: multicast to unicast %s on interface %s",
3855 multicast_to_unicast ? "enabled" : "disabled",
3856 bss->ifname);
3857 break;
3858 case -EOPNOTSUPP:
3859 if (!multicast_to_unicast)
3860 break;
3861 wpa_printf(MSG_INFO,
3862 "nl80211: multicast to unicast not supported on interface %s",
3863 bss->ifname);
3864 break;
3865 default:
3866 wpa_printf(MSG_ERROR,
3867 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3868 multicast_to_unicast ? "enabling" : "disabling",
3869 ret, strerror(-ret), bss->ifname);
3870 break;
3871 }
3872
3873 return ret;
3874 }
3875
3876
3877 static int wpa_driver_nl80211_set_ap(void *priv,
3878 struct wpa_driver_ap_params *params)
3879 {
3880 struct i802_bss *bss = priv;
3881 struct wpa_driver_nl80211_data *drv = bss->drv;
3882 struct nl_msg *msg;
3883 u8 cmd = NL80211_CMD_NEW_BEACON;
3884 int ret;
3885 int beacon_set;
3886 int num_suites;
3887 int smps_mode;
3888 u32 suites[10], suite;
3889 u32 ver;
3890 #ifdef CONFIG_MESH
3891 struct wpa_driver_mesh_bss_params mesh_params;
3892 #endif /* CONFIG_MESH */
3893
3894 beacon_set = params->reenable ? 0 : bss->beacon_set;
3895
3896 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3897 beacon_set);
3898 if (beacon_set)
3899 cmd = NL80211_CMD_SET_BEACON;
3900 else if (!drv->device_ap_sme && !drv->use_monitor &&
3901 !nl80211_get_wiphy_data_ap(bss))
3902 return -ENOBUFS;
3903
3904 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3905 params->head, params->head_len);
3906 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3907 params->tail, params->tail_len);
3908 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
3909 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
3910 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3911 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
3912 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
3913 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3914 params->ssid, params->ssid_len);
3915 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3916 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3917 params->head) ||
3918 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3919 params->tail) ||
3920 nl80211_put_beacon_int(msg, params->beacon_int) ||
3921 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
3922 nl80211_put_dtim_period(msg, params->dtim_period) ||
3923 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3924 goto fail;
3925 if (params->proberesp && params->proberesp_len) {
3926 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3927 params->proberesp, params->proberesp_len);
3928 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3929 params->proberesp))
3930 goto fail;
3931 }
3932 switch (params->hide_ssid) {
3933 case NO_SSID_HIDING:
3934 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
3935 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3936 NL80211_HIDDEN_SSID_NOT_IN_USE))
3937 goto fail;
3938 break;
3939 case HIDDEN_SSID_ZERO_LEN:
3940 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
3941 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3942 NL80211_HIDDEN_SSID_ZERO_LEN))
3943 goto fail;
3944 break;
3945 case HIDDEN_SSID_ZERO_CONTENTS:
3946 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
3947 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3948 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3949 goto fail;
3950 break;
3951 }
3952 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
3953 if (params->privacy &&
3954 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3955 goto fail;
3956 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
3957 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3958 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3959 /* Leave out the attribute */
3960 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3961 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3962 NL80211_AUTHTYPE_SHARED_KEY))
3963 goto fail;
3964 } else {
3965 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3966 NL80211_AUTHTYPE_OPEN_SYSTEM))
3967 goto fail;
3968 }
3969
3970 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
3971 ver = 0;
3972 if (params->wpa_version & WPA_PROTO_WPA)
3973 ver |= NL80211_WPA_VERSION_1;
3974 if (params->wpa_version & WPA_PROTO_RSN)
3975 ver |= NL80211_WPA_VERSION_2;
3976 if (ver &&
3977 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3978 goto fail;
3979
3980 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3981 params->key_mgmt_suites);
3982 num_suites = 0;
3983 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3984 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
3985 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3986 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
3987 if (num_suites &&
3988 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3989 suites))
3990 goto fail;
3991
3992 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
3993 (!params->pairwise_ciphers ||
3994 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
3995 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
3996 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
3997 goto fail;
3998
3999 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
4000 params->pairwise_ciphers);
4001 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
4002 suites, ARRAY_SIZE(suites));
4003 if (num_suites &&
4004 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4005 num_suites * sizeof(u32), suites))
4006 goto fail;
4007
4008 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
4009 params->group_cipher);
4010 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
4011 if (suite &&
4012 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
4013 goto fail;
4014
4015 if (params->ht_opmode != -1) {
4016 switch (params->smps_mode) {
4017 case HT_CAP_INFO_SMPS_DYNAMIC:
4018 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
4019 smps_mode = NL80211_SMPS_DYNAMIC;
4020 break;
4021 case HT_CAP_INFO_SMPS_STATIC:
4022 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
4023 smps_mode = NL80211_SMPS_STATIC;
4024 break;
4025 default:
4026 /* invalid - fallback to smps off */
4027 case HT_CAP_INFO_SMPS_DISABLED:
4028 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
4029 smps_mode = NL80211_SMPS_OFF;
4030 break;
4031 }
4032 if (nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
4033 goto fail;
4034 }
4035
4036 if (params->beacon_ies) {
4037 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
4038 params->beacon_ies);
4039 if (nla_put(msg, NL80211_ATTR_IE,
4040 wpabuf_len(params->beacon_ies),
4041 wpabuf_head(params->beacon_ies)))
4042 goto fail;
4043 }
4044 if (params->proberesp_ies) {
4045 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
4046 params->proberesp_ies);
4047 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
4048 wpabuf_len(params->proberesp_ies),
4049 wpabuf_head(params->proberesp_ies)))
4050 goto fail;
4051 }
4052 if (params->assocresp_ies) {
4053 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
4054 params->assocresp_ies);
4055 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
4056 wpabuf_len(params->assocresp_ies),
4057 wpabuf_head(params->assocresp_ies)))
4058 goto fail;
4059 }
4060
4061 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
4062 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
4063 params->ap_max_inactivity);
4064 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
4065 params->ap_max_inactivity))
4066 goto fail;
4067 }
4068
4069 #ifdef CONFIG_P2P
4070 if (params->p2p_go_ctwindow > 0) {
4071 if (drv->p2p_go_ctwindow_supported) {
4072 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
4073 params->p2p_go_ctwindow);
4074 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
4075 params->p2p_go_ctwindow))
4076 goto fail;
4077 } else {
4078 wpa_printf(MSG_INFO,
4079 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
4080 }
4081 }
4082 #endif /* CONFIG_P2P */
4083
4084 if (params->pbss) {
4085 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
4086 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
4087 goto fail;
4088 }
4089
4090 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4091 if (ret) {
4092 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4093 ret, strerror(-ret));
4094 } else {
4095 bss->beacon_set = 1;
4096 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4097 params->short_slot_time, params->ht_opmode,
4098 params->isolate, params->basic_rates);
4099 nl80211_set_multicast_to_unicast(bss,
4100 params->multicast_to_unicast);
4101 if (beacon_set && params->freq &&
4102 params->freq->bandwidth != bss->bandwidth) {
4103 wpa_printf(MSG_DEBUG,
4104 "nl80211: Update BSS %s bandwidth: %d -> %d",
4105 bss->ifname, bss->bandwidth,
4106 params->freq->bandwidth);
4107 ret = nl80211_set_channel(bss, params->freq, 1);
4108 if (ret) {
4109 wpa_printf(MSG_DEBUG,
4110 "nl80211: Frequency set failed: %d (%s)",
4111 ret, strerror(-ret));
4112 } else {
4113 wpa_printf(MSG_DEBUG,
4114 "nl80211: Frequency set succeeded for ht2040 coex");
4115 bss->bandwidth = params->freq->bandwidth;
4116 }
4117 } else if (!beacon_set && params->freq) {
4118 /*
4119 * cfg80211 updates the driver on frequence change in AP
4120 * mode only at the point when beaconing is started, so
4121 * set the initial value here.
4122 */
4123 bss->bandwidth = params->freq->bandwidth;
4124 }
4125 }
4126
4127 #ifdef CONFIG_MESH
4128 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4129 os_memset(&mesh_params, 0, sizeof(mesh_params));
4130 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4131 mesh_params.ht_opmode = params->ht_opmode;
4132 ret = nl80211_set_mesh_config(priv, &mesh_params);
4133 if (ret < 0)
4134 return ret;
4135 }
4136 #endif /* CONFIG_MESH */
4137
4138 return ret;
4139 fail:
4140 nlmsg_free(msg);
4141 return -ENOBUFS;
4142 }
4143
4144
4145 static int nl80211_put_freq_params(struct nl_msg *msg,
4146 const struct hostapd_freq_params *freq)
4147 {
4148 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
4149 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4150 return -ENOBUFS;
4151
4152 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4153 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4154
4155 if (freq->vht_enabled) {
4156 enum nl80211_chan_width cw;
4157
4158 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
4159 switch (freq->bandwidth) {
4160 case 20:
4161 cw = NL80211_CHAN_WIDTH_20;
4162 break;
4163 case 40:
4164 cw = NL80211_CHAN_WIDTH_40;
4165 break;
4166 case 80:
4167 if (freq->center_freq2)
4168 cw = NL80211_CHAN_WIDTH_80P80;
4169 else
4170 cw = NL80211_CHAN_WIDTH_80;
4171 break;
4172 case 160:
4173 cw = NL80211_CHAN_WIDTH_160;
4174 break;
4175 default:
4176 return -EINVAL;
4177 }
4178
4179 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4180 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4181 freq->center_freq1);
4182 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4183 freq->center_freq2);
4184 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4185 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4186 freq->center_freq1) ||
4187 (freq->center_freq2 &&
4188 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4189 freq->center_freq2)))
4190 return -ENOBUFS;
4191 } else if (freq->ht_enabled) {
4192 enum nl80211_channel_type ct;
4193
4194 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4195 freq->sec_channel_offset);
4196 switch (freq->sec_channel_offset) {
4197 case -1:
4198 ct = NL80211_CHAN_HT40MINUS;
4199 break;
4200 case 1:
4201 ct = NL80211_CHAN_HT40PLUS;
4202 break;
4203 default:
4204 ct = NL80211_CHAN_HT20;
4205 break;
4206 }
4207
4208 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
4209 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4210 return -ENOBUFS;
4211 } else {
4212 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4213 NL80211_CHAN_NO_HT);
4214 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4215 NL80211_CHAN_NO_HT))
4216 return -ENOBUFS;
4217 }
4218 return 0;
4219 }
4220
4221
4222 static int nl80211_set_channel(struct i802_bss *bss,
4223 struct hostapd_freq_params *freq, int set_chan)
4224 {
4225 struct wpa_driver_nl80211_data *drv = bss->drv;
4226 struct nl_msg *msg;
4227 int ret;
4228
4229 wpa_printf(MSG_DEBUG,
4230 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4231 freq->freq, freq->ht_enabled, freq->vht_enabled,
4232 freq->bandwidth, freq->center_freq1, freq->center_freq2);
4233
4234 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4235 NL80211_CMD_SET_WIPHY);
4236 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4237 nlmsg_free(msg);
4238 return -1;
4239 }
4240
4241 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4242 if (ret == 0) {
4243 bss->freq = freq->freq;
4244 return 0;
4245 }
4246 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
4247 "%d (%s)", freq->freq, ret, strerror(-ret));
4248 return -1;
4249 }
4250
4251
4252 static u32 sta_flags_nl80211(int flags)
4253 {
4254 u32 f = 0;
4255
4256 if (flags & WPA_STA_AUTHORIZED)
4257 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4258 if (flags & WPA_STA_WMM)
4259 f |= BIT(NL80211_STA_FLAG_WME);
4260 if (flags & WPA_STA_SHORT_PREAMBLE)
4261 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4262 if (flags & WPA_STA_MFP)
4263 f |= BIT(NL80211_STA_FLAG_MFP);
4264 if (flags & WPA_STA_TDLS_PEER)
4265 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
4266 if (flags & WPA_STA_AUTHENTICATED)
4267 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4268 if (flags & WPA_STA_ASSOCIATED)
4269 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4270
4271 return f;
4272 }
4273
4274
4275 #ifdef CONFIG_MESH
4276 static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4277 {
4278 switch (state) {
4279 case PLINK_IDLE:
4280 return NL80211_PLINK_LISTEN;
4281 case PLINK_OPN_SNT:
4282 return NL80211_PLINK_OPN_SNT;
4283 case PLINK_OPN_RCVD:
4284 return NL80211_PLINK_OPN_RCVD;
4285 case PLINK_CNF_RCVD:
4286 return NL80211_PLINK_CNF_RCVD;
4287 case PLINK_ESTAB:
4288 return NL80211_PLINK_ESTAB;
4289 case PLINK_HOLDING:
4290 return NL80211_PLINK_HOLDING;
4291 case PLINK_BLOCKED:
4292 return NL80211_PLINK_BLOCKED;
4293 default:
4294 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4295 state);
4296 }
4297 return -1;
4298 }
4299 #endif /* CONFIG_MESH */
4300
4301
4302 static int wpa_driver_nl80211_sta_add(void *priv,
4303 struct hostapd_sta_add_params *params)
4304 {
4305 struct i802_bss *bss = priv;
4306 struct wpa_driver_nl80211_data *drv = bss->drv;
4307 struct nl_msg *msg;
4308 struct nl80211_sta_flag_update upd;
4309 int ret = -ENOBUFS;
4310
4311 if ((params->flags & WPA_STA_TDLS_PEER) &&
4312 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4313 return -EOPNOTSUPP;
4314
4315 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4316 params->set ? "Set" : "Add", MAC2STR(params->addr));
4317 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4318 NL80211_CMD_NEW_STATION);
4319 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4320 goto fail;
4321
4322 /*
4323 * Set the below properties only in one of the following cases:
4324 * 1. New station is added, already associated.
4325 * 2. Set WPA_STA_TDLS_PEER station.
4326 * 3. Set an already added unassociated station, if driver supports
4327 * full AP client state. (Set these properties after station became
4328 * associated will be rejected by the driver).
4329 */
4330 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4331 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4332 (params->flags & WPA_STA_ASSOCIATED))) {
4333 wpa_hexdump(MSG_DEBUG, " * supported rates",
4334 params->supp_rates, params->supp_rates_len);
4335 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4336 params->capability);
4337 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4338 params->supp_rates_len, params->supp_rates) ||
4339 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4340 params->capability))
4341 goto fail;
4342
4343 if (params->ht_capabilities) {
4344 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4345 (u8 *) params->ht_capabilities,
4346 sizeof(*params->ht_capabilities));
4347 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4348 sizeof(*params->ht_capabilities),
4349 params->ht_capabilities))
4350 goto fail;
4351 }
4352
4353 if (params->vht_capabilities) {
4354 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4355 (u8 *) params->vht_capabilities,
4356 sizeof(*params->vht_capabilities));
4357 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4358 sizeof(*params->vht_capabilities),
4359 params->vht_capabilities))
4360 goto fail;
4361 }
4362
4363 if (params->ext_capab) {
4364 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4365 params->ext_capab, params->ext_capab_len);
4366 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4367 params->ext_capab_len, params->ext_capab))
4368 goto fail;
4369 }
4370
4371 if (is_ap_interface(drv->nlmode) &&
4372 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4373 params->support_p2p_ps ?
4374 NL80211_P2P_PS_SUPPORTED :
4375 NL80211_P2P_PS_UNSUPPORTED))
4376 goto fail;
4377 }
4378 if (!params->set) {
4379 if (params->aid) {
4380 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4381 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4382 goto fail;
4383 } else {
4384 /*
4385 * cfg80211 validates that AID is non-zero, so we have
4386 * to make this a non-zero value for the TDLS case where
4387 * a dummy STA entry is used for now and for a station
4388 * that is still not associated.
4389 */
4390 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4391 (params->flags & WPA_STA_TDLS_PEER) ?
4392 "TDLS" : "UNASSOC_STA");
4393 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4394 goto fail;
4395 }
4396 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4397 params->listen_interval);
4398 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4399 params->listen_interval))
4400 goto fail;
4401 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4402 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
4403 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4404 goto fail;
4405 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4406 (params->flags & WPA_STA_ASSOCIATED)) {
4407 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4408 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4409 params->listen_interval);
4410 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4411 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4412 params->listen_interval))
4413 goto fail;
4414 }
4415
4416 if (params->vht_opmode_enabled) {
4417 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
4418 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4419 params->vht_opmode))
4420 goto fail;
4421 }
4422
4423 if (params->supp_channels) {
4424 wpa_hexdump(MSG_DEBUG, " * supported channels",
4425 params->supp_channels, params->supp_channels_len);
4426 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4427 params->supp_channels_len, params->supp_channels))
4428 goto fail;
4429 }
4430
4431 if (params->supp_oper_classes) {
4432 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4433 params->supp_oper_classes,
4434 params->supp_oper_classes_len);
4435 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4436 params->supp_oper_classes_len,
4437 params->supp_oper_classes))
4438 goto fail;
4439 }
4440
4441 os_memset(&upd, 0, sizeof(upd));
4442 upd.set = sta_flags_nl80211(params->flags);
4443 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
4444
4445 /*
4446 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4447 * flags, as nl80211 driver moves a new station, by default, into
4448 * associated state.
4449 *
4450 * On the other hand, if the driver supports that feature and the
4451 * station is added in unauthenticated state, set the
4452 * authenticated/associated bits in the mask to prevent moving this
4453 * station to associated state before it is actually associated.
4454 *
4455 * This is irrelevant for mesh mode where the station is added to the
4456 * driver as authenticated already, and ASSOCIATED isn't part of the
4457 * nl80211 API.
4458 */
4459 if (!is_mesh_interface(drv->nlmode)) {
4460 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4461 wpa_printf(MSG_DEBUG,
4462 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4463 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4464 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4465 } else if (!params->set &&
4466 !(params->flags & WPA_STA_TDLS_PEER)) {
4467 if (!(params->flags & WPA_STA_AUTHENTICATED))
4468 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4469 if (!(params->flags & WPA_STA_ASSOCIATED))
4470 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4471 }
4472 #ifdef CONFIG_MESH
4473 } else {
4474 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4475 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4476 params->peer_aid);
4477 if (ret)
4478 goto fail;
4479 }
4480 #endif /* CONFIG_MESH */
4481 }
4482
4483 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4484 upd.set, upd.mask);
4485 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4486 goto fail;
4487
4488 #ifdef CONFIG_MESH
4489 if (params->plink_state &&
4490 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4491 sta_plink_state_nl80211(params->plink_state)))
4492 goto fail;
4493 #endif /* CONFIG_MESH */
4494
4495 if (params->flags & WPA_STA_WMM) {
4496 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4497
4498 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
4499 if (!wme ||
4500 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4501 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4502 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4503 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4504 WMM_QOSINFO_STA_SP_MASK))
4505 goto fail;
4506 nla_nest_end(msg, wme);
4507 }
4508
4509 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4510 msg = NULL;
4511 if (ret)
4512 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4513 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4514 strerror(-ret));
4515 if (ret == -EEXIST)
4516 ret = 0;
4517 fail:
4518 nlmsg_free(msg);
4519 return ret;
4520 }
4521
4522
4523 static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4524 {
4525 #ifdef CONFIG_LIBNL3_ROUTE
4526 struct wpa_driver_nl80211_data *drv = bss->drv;
4527 struct rtnl_neigh *rn;
4528 struct nl_addr *nl_addr;
4529 int err;
4530
4531 rn = rtnl_neigh_alloc();
4532 if (!rn)
4533 return;
4534
4535 rtnl_neigh_set_family(rn, AF_BRIDGE);
4536 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4537 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4538 if (!nl_addr) {
4539 rtnl_neigh_put(rn);
4540 return;
4541 }
4542 rtnl_neigh_set_lladdr(rn, nl_addr);
4543
4544 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4545 if (err < 0) {
4546 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4547 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4548 bss->ifindex, nl_geterror(err));
4549 } else {
4550 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4551 MACSTR, MAC2STR(addr));
4552 }
4553
4554 nl_addr_put(nl_addr);
4555 rtnl_neigh_put(rn);
4556 #endif /* CONFIG_LIBNL3_ROUTE */
4557 }
4558
4559
4560 static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4561 int deauth, u16 reason_code)
4562 {
4563 struct wpa_driver_nl80211_data *drv = bss->drv;
4564 struct nl_msg *msg;
4565 int ret;
4566
4567 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4568 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4569 (deauth == 0 &&
4570 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4571 WLAN_FC_STYPE_DISASSOC)) ||
4572 (deauth == 1 &&
4573 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4574 WLAN_FC_STYPE_DEAUTH)) ||
4575 (reason_code &&
4576 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4577 nlmsg_free(msg);
4578 return -ENOBUFS;
4579 }
4580
4581 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4582 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4583 " --> %d (%s)",
4584 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
4585
4586 if (drv->rtnl_sk)
4587 rtnl_neigh_delete_fdb_entry(bss, addr);
4588
4589 if (ret == -ENOENT)
4590 return 0;
4591 return ret;
4592 }
4593
4594
4595 void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
4596 {
4597 struct nl_msg *msg;
4598 struct wpa_driver_nl80211_data *drv2;
4599
4600 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4601
4602 /* stop listening for EAPOL on this interface */
4603 dl_list_for_each(drv2, &drv->global->interfaces,
4604 struct wpa_driver_nl80211_data, list)
4605 {
4606 del_ifidx(drv2, ifidx, IFIDX_ANY);
4607 /* Remove all bridges learned for this iface */
4608 del_ifidx(drv2, IFIDX_ANY, ifidx);
4609 }
4610
4611 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
4612 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4613 return;
4614 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4615 }
4616
4617
4618 const char * nl80211_iftype_str(enum nl80211_iftype mode)
4619 {
4620 switch (mode) {
4621 case NL80211_IFTYPE_ADHOC:
4622 return "ADHOC";
4623 case NL80211_IFTYPE_STATION:
4624 return "STATION";
4625 case NL80211_IFTYPE_AP:
4626 return "AP";
4627 case NL80211_IFTYPE_AP_VLAN:
4628 return "AP_VLAN";
4629 case NL80211_IFTYPE_WDS:
4630 return "WDS";
4631 case NL80211_IFTYPE_MONITOR:
4632 return "MONITOR";
4633 case NL80211_IFTYPE_MESH_POINT:
4634 return "MESH_POINT";
4635 case NL80211_IFTYPE_P2P_CLIENT:
4636 return "P2P_CLIENT";
4637 case NL80211_IFTYPE_P2P_GO:
4638 return "P2P_GO";
4639 case NL80211_IFTYPE_P2P_DEVICE:
4640 return "P2P_DEVICE";
4641 default:
4642 return "unknown";
4643 }
4644 }
4645
4646
4647 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4648 const char *ifname,
4649 enum nl80211_iftype iftype,
4650 const u8 *addr, int wds,
4651 int (*handler)(struct nl_msg *, void *),
4652 void *arg)
4653 {
4654 struct nl_msg *msg;
4655 int ifidx;
4656 int ret = -ENOBUFS;
4657
4658 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4659 iftype, nl80211_iftype_str(iftype));
4660
4661 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4662 if (!msg ||
4663 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4664 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4665 goto fail;
4666
4667 if (iftype == NL80211_IFTYPE_MONITOR) {
4668 struct nlattr *flags;
4669
4670 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
4671 if (!flags ||
4672 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4673 goto fail;
4674
4675 nla_nest_end(msg, flags);
4676 } else if (wds) {
4677 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4678 goto fail;
4679 }
4680
4681 /*
4682 * Tell cfg80211 that the interface belongs to the socket that created
4683 * it, and the interface should be deleted when the socket is closed.
4684 */
4685 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4686 goto fail;
4687
4688 ret = send_and_recv_msgs(drv, msg, handler, arg);
4689 msg = NULL;
4690 if (ret) {
4691 fail:
4692 nlmsg_free(msg);
4693 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4694 ifname, ret, strerror(-ret));
4695 return ret;
4696 }
4697
4698 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4699 return 0;
4700
4701 ifidx = if_nametoindex(ifname);
4702 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4703 ifname, ifidx);
4704
4705 if (ifidx <= 0)
4706 return -1;
4707
4708 /*
4709 * Some virtual interfaces need to process EAPOL packets and events on
4710 * the parent interface. This is used mainly with hostapd.
4711 */
4712 if (drv->hostapd ||
4713 iftype == NL80211_IFTYPE_AP_VLAN ||
4714 iftype == NL80211_IFTYPE_WDS ||
4715 iftype == NL80211_IFTYPE_MONITOR) {
4716 /* start listening for EAPOL on this interface */
4717 add_ifidx(drv, ifidx, IFIDX_ANY);
4718 }
4719
4720 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4721 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
4722 nl80211_remove_iface(drv, ifidx);
4723 return -1;
4724 }
4725
4726 return ifidx;
4727 }
4728
4729
4730 int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4731 const char *ifname, enum nl80211_iftype iftype,
4732 const u8 *addr, int wds,
4733 int (*handler)(struct nl_msg *, void *),
4734 void *arg, int use_existing)
4735 {
4736 int ret;
4737
4738 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4739 arg);
4740
4741 /* if error occurred and interface exists already */
4742 if (ret == -ENFILE && if_nametoindex(ifname)) {
4743 if (use_existing) {
4744 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4745 ifname);
4746 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4747 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4748 addr) < 0 &&
4749 (linux_set_iface_flags(drv->global->ioctl_sock,
4750 ifname, 0) < 0 ||
4751 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4752 addr) < 0 ||
4753 linux_set_iface_flags(drv->global->ioctl_sock,
4754 ifname, 1) < 0))
4755 return -1;
4756 return -ENFILE;
4757 }
4758 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4759
4760 /* Try to remove the interface that was already there. */
4761 nl80211_remove_iface(drv, if_nametoindex(ifname));
4762
4763 /* Try to create the interface again */
4764 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
4765 wds, handler, arg);
4766 }
4767
4768 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4769 wpa_printf(MSG_DEBUG,
4770 "nl80211: Interface %s created for P2P - disable 11b rates",
4771 ifname);
4772 nl80211_disable_11b_rates(drv, ret, 1);
4773 }
4774
4775 return ret;
4776 }
4777
4778
4779 static int nl80211_setup_ap(struct i802_bss *bss)
4780 {
4781 struct wpa_driver_nl80211_data *drv = bss->drv;
4782
4783 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4784 bss->ifname, drv->device_ap_sme, drv->use_monitor);
4785
4786 /*
4787 * Disable Probe Request reporting unless we need it in this way for
4788 * devices that include the AP SME, in the other case (unless using
4789 * monitor iface) we'll get it through the nl_mgmt socket instead.
4790 */
4791 if (!drv->device_ap_sme)
4792 wpa_driver_nl80211_probe_req_report(bss, 0);
4793
4794 if (!drv->device_ap_sme && !drv->use_monitor)
4795 if (nl80211_mgmt_subscribe_ap(bss))
4796 return -1;
4797
4798 if (drv->device_ap_sme && !drv->use_monitor)
4799 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4800 wpa_printf(MSG_DEBUG,
4801 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
4802
4803 if (!drv->device_ap_sme && drv->use_monitor &&
4804 nl80211_create_monitor_interface(drv) &&
4805 !drv->device_ap_sme)
4806 return -1;
4807
4808 if (drv->device_ap_sme &&
4809 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4810 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4811 "Probe Request frame reporting in AP mode");
4812 /* Try to survive without this */
4813 }
4814
4815 return 0;
4816 }
4817
4818
4819 static void nl80211_teardown_ap(struct i802_bss *bss)
4820 {
4821 struct wpa_driver_nl80211_data *drv = bss->drv;
4822
4823 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4824 bss->ifname, drv->device_ap_sme, drv->use_monitor);
4825 if (drv->device_ap_sme) {
4826 wpa_driver_nl80211_probe_req_report(bss, 0);
4827 if (!drv->use_monitor)
4828 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4829 } else if (drv->use_monitor)
4830 nl80211_remove_monitor_interface(drv);
4831 else
4832 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4833
4834 nl80211_put_wiphy_data_ap(bss);
4835 bss->beacon_set = 0;
4836 }
4837
4838
4839 static int nl80211_send_eapol_data(struct i802_bss *bss,
4840 const u8 *addr, const u8 *data,
4841 size_t data_len)
4842 {
4843 struct sockaddr_ll ll;
4844 int ret;
4845
4846 if (bss->drv->eapol_tx_sock < 0) {
4847 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4848 return -1;
4849 }
4850
4851 os_memset(&ll, 0, sizeof(ll));
4852 ll.sll_family = AF_PACKET;
4853 ll.sll_ifindex = bss->ifindex;
4854 ll.sll_protocol = htons(ETH_P_PAE);
4855 ll.sll_halen = ETH_ALEN;
4856 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4857 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4858 (struct sockaddr *) &ll, sizeof(ll));
4859 if (ret < 0)
4860 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4861 strerror(errno));
4862
4863 return ret;
4864 }
4865
4866
4867 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4868
4869 static int wpa_driver_nl80211_hapd_send_eapol(
4870 void *priv, const u8 *addr, const u8 *data,
4871 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4872 {
4873 struct i802_bss *bss = priv;
4874 struct wpa_driver_nl80211_data *drv = bss->drv;
4875 struct ieee80211_hdr *hdr;
4876 size_t len;
4877 u8 *pos;
4878 int res;
4879 int qos = flags & WPA_STA_WMM;
4880
4881 if (drv->device_ap_sme || !drv->use_monitor)
4882 return nl80211_send_eapol_data(bss, addr, data, data_len);
4883
4884 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4885 data_len;
4886 hdr = os_zalloc(len);
4887 if (hdr == NULL) {
4888 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4889 (unsigned long) len);
4890 return -1;
4891 }
4892
4893 hdr->frame_control =
4894 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4895 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4896 if (encrypt)
4897 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4898 if (qos) {
4899 hdr->frame_control |=
4900 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4901 }
4902
4903 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4904 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4905 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4906 pos = (u8 *) (hdr + 1);
4907
4908 if (qos) {
4909 /* Set highest priority in QoS header */
4910 pos[0] = 7;
4911 pos[1] = 0;
4912 pos += 2;
4913 }
4914
4915 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4916 pos += sizeof(rfc1042_header);
4917 WPA_PUT_BE16(pos, ETH_P_PAE);
4918 pos += 2;
4919 memcpy(pos, data, data_len);
4920
4921 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
4922 0, 0, 0, 0, NULL, 0);
4923 if (res < 0) {
4924 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4925 "failed: %d (%s)",
4926 (unsigned long) len, errno, strerror(errno));
4927 }
4928 os_free(hdr);
4929
4930 return res;
4931 }
4932
4933
4934 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4935 unsigned int total_flags,
4936 unsigned int flags_or,
4937 unsigned int flags_and)
4938 {
4939 struct i802_bss *bss = priv;
4940 struct nl_msg *msg;
4941 struct nlattr *flags;
4942 struct nl80211_sta_flag_update upd;
4943
4944 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4945 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4946 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4947 !!(total_flags & WPA_STA_AUTHORIZED));
4948
4949 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4950 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4951 goto fail;
4952
4953 /*
4954 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4955 * can be removed eventually.
4956 */
4957 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
4958 if (!flags ||
4959 ((total_flags & WPA_STA_AUTHORIZED) &&
4960 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4961 ((total_flags & WPA_STA_WMM) &&
4962 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4963 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4964 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4965 ((total_flags & WPA_STA_MFP) &&
4966 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4967 ((total_flags & WPA_STA_TDLS_PEER) &&
4968 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4969 goto fail;
4970
4971 nla_nest_end(msg, flags);
4972
4973 os_memset(&upd, 0, sizeof(upd));
4974 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4975 upd.set = sta_flags_nl80211(flags_or);
4976 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4977 goto fail;
4978
4979 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4980 fail:
4981 nlmsg_free(msg);
4982 return -ENOBUFS;
4983 }
4984
4985
4986 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4987 struct wpa_driver_associate_params *params)
4988 {
4989 enum nl80211_iftype nlmode, old_mode;
4990
4991 if (params->p2p) {
4992 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4993 "group (GO)");
4994 nlmode = NL80211_IFTYPE_P2P_GO;
4995 } else
4996 nlmode = NL80211_IFTYPE_AP;
4997
4998 old_mode = drv->nlmode;
4999 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
5000 nl80211_remove_monitor_interface(drv);
5001 return -1;
5002 }
5003
5004 if (params->freq.freq &&
5005 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
5006 if (old_mode != nlmode)
5007 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
5008 nl80211_remove_monitor_interface(drv);
5009 return -1;
5010 }
5011
5012 return 0;
5013 }
5014
5015
5016 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
5017 int reset_mode)
5018 {
5019 struct nl_msg *msg;
5020 int ret;
5021
5022 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
5023 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5024 if (ret) {
5025 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5026 "(%s)", ret, strerror(-ret));
5027 } else {
5028 wpa_printf(MSG_DEBUG,
5029 "nl80211: Leave IBSS request sent successfully");
5030 }
5031
5032 if (reset_mode &&
5033 wpa_driver_nl80211_set_mode(drv->first_bss,
5034 NL80211_IFTYPE_STATION)) {
5035 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5036 "station mode");
5037 }
5038
5039 return ret;
5040 }
5041
5042
5043 static int nl80211_ht_vht_overrides(struct nl_msg *msg,
5044 struct wpa_driver_associate_params *params)
5045 {
5046 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
5047 return -1;
5048
5049 if (params->htcaps && params->htcaps_mask) {
5050 int sz = sizeof(struct ieee80211_ht_capabilities);
5051 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
5052 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
5053 params->htcaps_mask, sz);
5054 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
5055 params->htcaps) ||
5056 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
5057 params->htcaps_mask))
5058 return -1;
5059 }
5060
5061 #ifdef CONFIG_VHT_OVERRIDES
5062 if (params->disable_vht) {
5063 wpa_printf(MSG_DEBUG, " * VHT disabled");
5064 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
5065 return -1;
5066 }
5067
5068 if (params->vhtcaps && params->vhtcaps_mask) {
5069 int sz = sizeof(struct ieee80211_vht_capabilities);
5070 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
5071 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
5072 params->vhtcaps_mask, sz);
5073 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
5074 params->vhtcaps) ||
5075 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
5076 params->vhtcaps_mask))
5077 return -1;
5078 }
5079 #endif /* CONFIG_VHT_OVERRIDES */
5080
5081 return 0;
5082 }
5083
5084
5085 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5086 struct wpa_driver_associate_params *params)
5087 {
5088 struct nl_msg *msg;
5089 int ret = -1;
5090 int count = 0;
5091
5092 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5093
5094 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
5095 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5096 "IBSS mode");
5097 return -1;
5098 }
5099
5100 retry:
5101 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
5102 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5103 goto fail;
5104
5105 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5106 params->ssid, params->ssid_len);
5107 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
5108 goto fail;
5109 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5110 drv->ssid_len = params->ssid_len;
5111
5112 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5113 nl80211_put_beacon_int(msg, params->beacon_int))
5114 goto fail;
5115
5116 ret = nl80211_set_conn_keys(params, msg);
5117 if (ret)
5118 goto fail;
5119
5120 if (params->bssid && params->fixed_bssid) {
5121 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5122 MAC2STR(params->bssid));
5123 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5124 goto fail;
5125 }
5126
5127 if (params->fixed_freq) {
5128 wpa_printf(MSG_DEBUG, " * fixed_freq");
5129 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5130 goto fail;
5131 }
5132
5133 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5134 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5135 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5136 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
5137 wpa_printf(MSG_DEBUG, " * control port");
5138 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5139 goto fail;
5140 }
5141
5142 if (params->wpa_ie) {
5143 wpa_hexdump(MSG_DEBUG,
5144 " * Extra IEs for Beacon/Probe Response frames",
5145 params->wpa_ie, params->wpa_ie_len);
5146 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5147 params->wpa_ie))
5148 goto fail;
5149 }
5150
5151 ret = nl80211_ht_vht_overrides(msg, params);
5152 if (ret < 0)
5153 goto fail;
5154
5155 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5156 msg = NULL;
5157 if (ret) {
5158 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5159 ret, strerror(-ret));
5160 count++;
5161 if (ret == -EALREADY && count == 1) {
5162 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5163 "forced leave");
5164 nl80211_leave_ibss(drv, 0);
5165 nlmsg_free(msg);
5166 goto retry;
5167 }
5168 } else {
5169 wpa_printf(MSG_DEBUG,
5170 "nl80211: Join IBSS request sent successfully");
5171 }
5172
5173 fail:
5174 nlmsg_free(msg);
5175 return ret;
5176 }
5177
5178
5179 static int nl80211_put_fils_connect_params(struct wpa_driver_nl80211_data *drv,
5180 struct wpa_driver_associate_params *params,
5181 struct nl_msg *msg)
5182 {
5183 if (params->fils_erp_username_len) {
5184 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP EMSKname/username",
5185 params->fils_erp_username,
5186 params->fils_erp_username_len);
5187 if (nla_put(msg, NL80211_ATTR_FILS_ERP_USERNAME,
5188 params->fils_erp_username_len,
5189 params->fils_erp_username))
5190 return -1;
5191 }
5192
5193 if (params->fils_erp_realm_len) {
5194 wpa_hexdump_ascii(MSG_DEBUG, " * FILS ERP Realm",
5195 params->fils_erp_realm,
5196 params->fils_erp_realm_len);
5197 if (nla_put(msg, NL80211_ATTR_FILS_ERP_REALM,
5198 params->fils_erp_realm_len, params->fils_erp_realm))
5199 return -1;
5200 }
5201
5202 wpa_printf(MSG_DEBUG, " * FILS ERP next seq %u",
5203 params->fils_erp_next_seq_num);
5204 if (nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
5205 params->fils_erp_next_seq_num))
5206 return -1;
5207
5208 if (params->fils_erp_rrk_len) {
5209 wpa_printf(MSG_DEBUG, " * FILS ERP rRK (len=%lu)",
5210 (unsigned long) params->fils_erp_rrk_len);
5211 if (nla_put(msg, NL80211_ATTR_FILS_ERP_RRK,
5212 params->fils_erp_rrk_len, params->fils_erp_rrk))
5213 return -1;
5214 }
5215
5216 return 0;
5217 }
5218
5219
5220 static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5221 struct wpa_driver_associate_params *params,
5222 struct nl_msg *msg)
5223 {
5224 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5225 return -1;
5226
5227 if (params->bssid) {
5228 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5229 MAC2STR(params->bssid));
5230 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5231 return -1;
5232 }
5233
5234 if (params->bssid_hint) {
5235 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5236 MAC2STR(params->bssid_hint));
5237 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5238 params->bssid_hint))
5239 return -1;
5240 }
5241
5242 if (params->freq.freq) {
5243 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
5244 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5245 params->freq.freq))
5246 return -1;
5247 drv->assoc_freq = params->freq.freq;
5248 } else
5249 drv->assoc_freq = 0;
5250
5251 if (params->freq_hint) {
5252 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
5253 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5254 params->freq_hint))
5255 return -1;
5256 }
5257
5258 if (params->bg_scan_period >= 0) {
5259 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5260 params->bg_scan_period);
5261 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5262 params->bg_scan_period))
5263 return -1;
5264 }
5265
5266 if (params->ssid) {
5267 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5268 params->ssid, params->ssid_len);
5269 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5270 params->ssid))
5271 return -1;
5272 if (params->ssid_len > sizeof(drv->ssid))
5273 return -1;
5274 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5275 drv->ssid_len = params->ssid_len;
5276 }
5277
5278 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
5279 if (params->wpa_ie &&
5280 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5281 return -1;
5282
5283 if (params->wpa_proto) {
5284 enum nl80211_wpa_versions ver = 0;
5285
5286 if (params->wpa_proto & WPA_PROTO_WPA)
5287 ver |= NL80211_WPA_VERSION_1;
5288 if (params->wpa_proto & WPA_PROTO_RSN)
5289 ver |= NL80211_WPA_VERSION_2;
5290
5291 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
5292 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5293 return -1;
5294 }
5295
5296 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5297 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5298 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
5299 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5300 cipher))
5301 return -1;
5302 }
5303
5304 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5305 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5306 /*
5307 * This is likely to work even though many drivers do not
5308 * advertise support for operations without GTK.
5309 */
5310 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5311 } else if (params->group_suite != WPA_CIPHER_NONE) {
5312 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5313 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
5314 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5315 return -1;
5316 }
5317
5318 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5319 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5320 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5321 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
5322 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
5323 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5324 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5325 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5326 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5327 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5328 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA256 ||
5329 params->key_mgmt_suite == WPA_KEY_MGMT_FILS_SHA384 ||
5330 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA256 ||
5331 params->key_mgmt_suite == WPA_KEY_MGMT_FT_FILS_SHA384 ||
5332 params->key_mgmt_suite == WPA_KEY_MGMT_OWE ||
5333 params->key_mgmt_suite == WPA_KEY_MGMT_DPP) {
5334 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
5335
5336 switch (params->key_mgmt_suite) {
5337 case WPA_KEY_MGMT_CCKM:
5338 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
5339 break;
5340 case WPA_KEY_MGMT_IEEE8021X:
5341 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
5342 break;
5343 case WPA_KEY_MGMT_FT_IEEE8021X:
5344 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
5345 break;
5346 case WPA_KEY_MGMT_FT_PSK:
5347 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
5348 break;
5349 case WPA_KEY_MGMT_IEEE8021X_SHA256:
5350 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
5351 break;
5352 case WPA_KEY_MGMT_PSK_SHA256:
5353 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
5354 break;
5355 case WPA_KEY_MGMT_OSEN:
5356 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
5357 break;
5358 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
5359 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
5360 break;
5361 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
5362 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
5363 break;
5364 case WPA_KEY_MGMT_FILS_SHA256:
5365 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA256;
5366 break;
5367 case WPA_KEY_MGMT_FILS_SHA384:
5368 mgmt = RSN_AUTH_KEY_MGMT_FILS_SHA384;
5369 break;
5370 case WPA_KEY_MGMT_FT_FILS_SHA256:
5371 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
5372 break;
5373 case WPA_KEY_MGMT_FT_FILS_SHA384:
5374 mgmt = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
5375 break;
5376 case WPA_KEY_MGMT_OWE:
5377 mgmt = RSN_AUTH_KEY_MGMT_OWE;
5378 break;
5379 case WPA_KEY_MGMT_DPP:
5380 mgmt = RSN_AUTH_KEY_MGMT_DPP;
5381 break;
5382 case WPA_KEY_MGMT_PSK:
5383 default:
5384 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
5385 break;
5386 }
5387 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
5388 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5389 return -1;
5390 }
5391
5392 /* Add PSK in case of 4-way handshake offload */
5393 if (params->psk &&
5394 (drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)) {
5395 wpa_hexdump_key(MSG_DEBUG, " * PSK", params->psk, 32);
5396 if (nla_put(msg, NL80211_ATTR_PMK, 32, params->psk))
5397 return -1;
5398 }
5399
5400 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5401 return -1;
5402
5403 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5404 (params->pairwise_suite == WPA_CIPHER_NONE ||
5405 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5406 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5407 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5408 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5409 return -1;
5410
5411 if (params->rrm_used) {
5412 u32 drv_rrm_flags = drv->capa.rrm_flags;
5413 if ((!((drv_rrm_flags &
5414 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5415 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5416 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
5417 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5418 return -1;
5419 }
5420
5421 if (nl80211_ht_vht_overrides(msg, params) < 0)
5422 return -1;
5423
5424 if (params->p2p)
5425 wpa_printf(MSG_DEBUG, " * P2P group");
5426
5427 if (params->pbss) {
5428 wpa_printf(MSG_DEBUG, " * PBSS");
5429 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5430 return -1;
5431 }
5432
5433 drv->connect_reassoc = 0;
5434 if (params->prev_bssid) {
5435 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5436 MAC2STR(params->prev_bssid));
5437 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5438 params->prev_bssid))
5439 return -1;
5440 drv->connect_reassoc = 1;
5441 }
5442
5443 if ((params->auth_alg & WPA_AUTH_ALG_FILS) &&
5444 nl80211_put_fils_connect_params(drv, params, msg) != 0)
5445 return -1;
5446
5447 if ((params->auth_alg & WPA_AUTH_ALG_SAE) &&
5448 (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) &&
5449 nla_put_flag(msg, NL80211_ATTR_EXTERNAL_AUTH_SUPPORT))
5450 return -1;
5451
5452 return 0;
5453 }
5454
5455
5456 static int wpa_driver_nl80211_try_connect(
5457 struct wpa_driver_nl80211_data *drv,
5458 struct wpa_driver_associate_params *params,
5459 struct nl_handle *nl_connect)
5460 {
5461 struct nl_msg *msg;
5462 enum nl80211_auth_type type;
5463 int ret;
5464 int algs;
5465
5466 #ifdef CONFIG_DRIVER_NL80211_QCA
5467 if (params->req_key_mgmt_offload && params->psk &&
5468 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5469 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5470 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5471 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5472 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5473 if (ret)
5474 return ret;
5475 }
5476 #endif /* CONFIG_DRIVER_NL80211_QCA */
5477
5478 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5479 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
5480 if (!msg)
5481 return -1;
5482
5483 ret = nl80211_connect_common(drv, params, msg);
5484 if (ret)
5485 goto fail;
5486
5487 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5488 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5489 goto fail;
5490
5491 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_OPTIONAL &&
5492 (drv->capa.flags & WPA_DRIVER_FLAGS_MFP_OPTIONAL) &&
5493 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_OPTIONAL))
5494 goto fail;
5495
5496 algs = 0;
5497 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5498 algs++;
5499 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5500 algs++;
5501 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5502 algs++;
5503 if (params->auth_alg & WPA_AUTH_ALG_FILS)
5504 algs++;
5505 if (algs > 1) {
5506 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5507 "selection");
5508 goto skip_auth_type;
5509 }
5510
5511 type = get_nl_auth_type(params->auth_alg);
5512 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5513 if (type == NL80211_AUTHTYPE_MAX ||
5514 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
5515 goto fail;
5516
5517 skip_auth_type:
5518 ret = nl80211_set_conn_keys(params, msg);
5519 if (ret)
5520 goto fail;
5521
5522 if (nl_connect)
5523 ret = send_and_recv(drv->global, nl_connect, msg, NULL, NULL);
5524 else
5525 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5526
5527 msg = NULL;
5528 if (ret) {
5529 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5530 "(%s)", ret, strerror(-ret));
5531 } else {
5532 wpa_printf(MSG_DEBUG,
5533 "nl80211: Connect request send successfully");
5534 }
5535
5536 fail:
5537 nlmsg_free(msg);
5538 return ret;
5539
5540 }
5541
5542
5543 static int wpa_driver_nl80211_connect(
5544 struct wpa_driver_nl80211_data *drv,
5545 struct wpa_driver_associate_params *params,
5546 struct nl_handle *nl_connect)
5547 {
5548 int ret;
5549
5550 /* Store the connection attempted bssid for future use */
5551 if (params->bssid)
5552 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5553 else
5554 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5555
5556 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
5557 if (ret == -EALREADY) {
5558 /*
5559 * cfg80211 does not currently accept new connections if
5560 * we are already connected. As a workaround, force
5561 * disconnection and try again.
5562 */
5563 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5564 "disconnecting before reassociation "
5565 "attempt");
5566 if (wpa_driver_nl80211_disconnect(
5567 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5568 return -1;
5569 ret = wpa_driver_nl80211_try_connect(drv, params, nl_connect);
5570 }
5571 return ret;
5572 }
5573
5574
5575 static int wpa_driver_nl80211_associate(
5576 void *priv, struct wpa_driver_associate_params *params)
5577 {
5578 struct i802_bss *bss = priv;
5579 struct wpa_driver_nl80211_data *drv = bss->drv;
5580 int ret = -1;
5581 struct nl_msg *msg;
5582
5583 nl80211_unmask_11b_rates(bss);
5584
5585 if (params->mode == IEEE80211_MODE_AP)
5586 return wpa_driver_nl80211_ap(drv, params);
5587
5588 if (params->mode == IEEE80211_MODE_IBSS)
5589 return wpa_driver_nl80211_ibss(drv, params);
5590
5591 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
5592 enum nl80211_iftype nlmode = params->p2p ?
5593 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5594 struct nl_handle *nl_connect = NULL;
5595
5596 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
5597 return -1;
5598 if (params->auth_alg & WPA_AUTH_ALG_SAE)
5599 nl_connect = bss->nl_connect;
5600 return wpa_driver_nl80211_connect(drv, params, nl_connect);
5601 }
5602
5603 nl80211_mark_disconnected(drv);
5604
5605 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5606 drv->ifindex);
5607 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
5608 if (!msg)
5609 return -1;
5610
5611 ret = nl80211_connect_common(drv, params, msg);
5612 if (ret)
5613 goto fail;
5614
5615 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5616 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5617 goto fail;
5618
5619 if (params->fils_kek) {
5620 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5621 (unsigned int) params->fils_kek_len);
5622 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5623 params->fils_kek))
5624 goto fail;
5625 }
5626 if (params->fils_nonces) {
5627 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5628 params->fils_nonces,
5629 params->fils_nonces_len);
5630 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5631 params->fils_nonces_len, params->fils_nonces))
5632 goto fail;
5633 }
5634
5635 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5636 msg = NULL;
5637 if (ret) {
5638 wpa_dbg(drv->ctx, MSG_DEBUG,
5639 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5640 ret, strerror(-ret));
5641 nl80211_dump_scan(drv);
5642 } else {
5643 wpa_printf(MSG_DEBUG,
5644 "nl80211: Association request send successfully");
5645 }
5646
5647 fail:
5648 nlmsg_free(msg);
5649 return ret;
5650 }
5651
5652
5653 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
5654 int ifindex, enum nl80211_iftype mode)
5655 {
5656 struct nl_msg *msg;
5657 int ret = -ENOBUFS;
5658
5659 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5660 ifindex, mode, nl80211_iftype_str(mode));
5661
5662 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5663 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5664 goto fail;
5665
5666 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5667 msg = NULL;
5668 if (!ret)
5669 return 0;
5670 fail:
5671 nlmsg_free(msg);
5672 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5673 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5674 return ret;
5675 }
5676
5677
5678 static int wpa_driver_nl80211_set_mode_impl(
5679 struct i802_bss *bss,
5680 enum nl80211_iftype nlmode,
5681 struct hostapd_freq_params *desired_freq_params)
5682 {
5683 struct wpa_driver_nl80211_data *drv = bss->drv;
5684 int ret = -1;
5685 int i;
5686 int was_ap = is_ap_interface(drv->nlmode);
5687 int res;
5688 int mode_switch_res;
5689
5690 if (TEST_FAIL())
5691 return -1;
5692
5693 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5694 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5695 mode_switch_res = 0;
5696
5697 if (mode_switch_res == 0) {
5698 drv->nlmode = nlmode;
5699 ret = 0;
5700 goto done;
5701 }
5702
5703 if (mode_switch_res == -ENODEV)
5704 return -1;
5705
5706 if (nlmode == drv->nlmode) {
5707 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5708 "requested mode - ignore error");
5709 ret = 0;
5710 goto done; /* Already in the requested mode */
5711 }
5712
5713 /* mac80211 doesn't allow mode changes while the device is up, so
5714 * take the device down, try to set the mode again, and bring the
5715 * device back up.
5716 */
5717 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5718 "interface down");
5719 for (i = 0; i < 10; i++) {
5720 res = i802_set_iface_flags(bss, 0);
5721 if (res == -EACCES || res == -ENODEV)
5722 break;
5723 if (res != 0) {
5724 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5725 "interface down");
5726 os_sleep(0, 100000);
5727 continue;
5728 }
5729
5730 /*
5731 * Setting the mode will fail for some drivers if the phy is
5732 * on a frequency that the mode is disallowed in.
5733 */
5734 if (desired_freq_params) {
5735 res = nl80211_set_channel(bss, desired_freq_params, 0);
5736 if (res) {
5737 wpa_printf(MSG_DEBUG,
5738 "nl80211: Failed to set frequency on interface");
5739 }
5740 }
5741
5742 /* Try to set the mode again while the interface is down */
5743 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5744 if (mode_switch_res == -EBUSY) {
5745 wpa_printf(MSG_DEBUG,
5746 "nl80211: Delaying mode set while interface going down");
5747 os_sleep(0, 100000);
5748 continue;
5749 }
5750 ret = mode_switch_res;
5751 break;
5752 }
5753
5754 if (!ret) {
5755 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5756 "interface is down");
5757 drv->nlmode = nlmode;
5758 drv->ignore_if_down_event = 1;
5759 }
5760
5761 /* Bring the interface back up */
5762 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5763 if (res != 0) {
5764 wpa_printf(MSG_DEBUG,
5765 "nl80211: Failed to set interface up after switching mode");
5766 ret = -1;
5767 }
5768
5769 done:
5770 if (ret) {
5771 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5772 "from %d failed", nlmode, drv->nlmode);
5773 return ret;
5774 }
5775
5776 if (is_p2p_net_interface(nlmode)) {
5777 wpa_printf(MSG_DEBUG,
5778 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5779 bss->ifname);
5780 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
5781 } else if (drv->disabled_11b_rates) {
5782 wpa_printf(MSG_DEBUG,
5783 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5784 bss->ifname);
5785 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
5786 }
5787
5788 if (is_ap_interface(nlmode)) {
5789 nl80211_mgmt_unsubscribe(bss, "start AP");
5790 /* Setup additional AP mode functionality if needed */
5791 if (nl80211_setup_ap(bss))
5792 return -1;
5793 } else if (was_ap) {
5794 /* Remove additional AP mode functionality */
5795 nl80211_teardown_ap(bss);
5796 } else {
5797 nl80211_mgmt_unsubscribe(bss, "mode change");
5798 }
5799
5800 if (is_mesh_interface(nlmode) &&
5801 nl80211_mgmt_subscribe_mesh(bss))
5802 return -1;
5803
5804 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
5805 !is_mesh_interface(nlmode) &&
5806 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5807 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5808 "frame processing - ignore for now");
5809
5810 return 0;
5811 }
5812
5813
5814 int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5815 enum nl80211_iftype nlmode)
5816 {
5817 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5818 }
5819
5820
5821 static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5822 struct hostapd_freq_params *freq)
5823 {
5824 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
5825 freq);
5826 }
5827
5828
5829 static int wpa_driver_nl80211_get_capa(void *priv,
5830 struct wpa_driver_capa *capa)
5831 {
5832 struct i802_bss *bss = priv;
5833 struct wpa_driver_nl80211_data *drv = bss->drv;
5834
5835 if (!drv->has_capability)
5836 return -1;
5837 os_memcpy(capa, &drv->capa, sizeof(*capa));
5838 if (drv->extended_capa && drv->extended_capa_mask) {
5839 capa->extended_capa = drv->extended_capa;
5840 capa->extended_capa_mask = drv->extended_capa_mask;
5841 capa->extended_capa_len = drv->extended_capa_len;
5842 }
5843
5844 return 0;
5845 }
5846
5847
5848 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5849 {
5850 struct i802_bss *bss = priv;
5851 struct wpa_driver_nl80211_data *drv = bss->drv;
5852
5853 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5854 bss->ifname, drv->operstate, state,
5855 state ? "UP" : "DORMANT");
5856 drv->operstate = state;
5857 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
5858 state ? IF_OPER_UP : IF_OPER_DORMANT);
5859 }
5860
5861
5862 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5863 {
5864 struct i802_bss *bss = priv;
5865 struct wpa_driver_nl80211_data *drv = bss->drv;
5866 struct nl_msg *msg;
5867 struct nl80211_sta_flag_update upd;
5868 int ret;
5869
5870 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5871 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5872 return 0;
5873 }
5874
5875 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5876 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5877
5878 os_memset(&upd, 0, sizeof(upd));
5879 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5880 if (authorized)
5881 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
5882
5883 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5884 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5885 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5886 nlmsg_free(msg);
5887 return -ENOBUFS;
5888 }
5889
5890 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5891 if (!ret)
5892 return 0;
5893 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5894 ret, strerror(-ret));
5895 return ret;
5896 }
5897
5898
5899 /* Set kernel driver on given frequency (MHz) */
5900 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
5901 {
5902 struct i802_bss *bss = priv;
5903 return nl80211_set_channel(bss, freq, 0);
5904 }
5905
5906
5907 static inline int min_int(int a, int b)
5908 {
5909 if (a < b)
5910 return a;
5911 return b;
5912 }
5913
5914
5915 static int get_key_handler(struct nl_msg *msg, void *arg)
5916 {
5917 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5918 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5919
5920 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5921 genlmsg_attrlen(gnlh, 0), NULL);
5922
5923 /*
5924 * TODO: validate the key index and mac address!
5925 * Otherwise, there's a race condition as soon as
5926 * the kernel starts sending key notifications.
5927 */
5928
5929 if (tb[NL80211_ATTR_KEY_SEQ])
5930 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5931 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5932 return NL_SKIP;
5933 }
5934
5935
5936 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5937 int idx, u8 *seq)
5938 {
5939 struct i802_bss *bss = priv;
5940 struct wpa_driver_nl80211_data *drv = bss->drv;
5941 struct nl_msg *msg;
5942
5943 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5944 NL80211_CMD_GET_KEY);
5945 if (!msg ||
5946 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5947 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5948 nlmsg_free(msg);
5949 return -ENOBUFS;
5950 }
5951
5952 memset(seq, 0, 6);
5953
5954 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
5955 }
5956
5957
5958 static int i802_set_rts(void *priv, int rts)
5959 {
5960 struct i802_bss *bss = priv;
5961 struct wpa_driver_nl80211_data *drv = bss->drv;
5962 struct nl_msg *msg;
5963 int ret;
5964 u32 val;
5965
5966 if (rts >= 2347)
5967 val = (u32) -1;
5968 else
5969 val = rts;
5970
5971 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5972 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5973 nlmsg_free(msg);
5974 return -ENOBUFS;
5975 }
5976
5977 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5978 if (!ret)
5979 return 0;
5980 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5981 "%d (%s)", rts, ret, strerror(-ret));
5982 return ret;
5983 }
5984
5985
5986 static int i802_set_frag(void *priv, int frag)
5987 {
5988 struct i802_bss *bss = priv;
5989 struct wpa_driver_nl80211_data *drv = bss->drv;
5990 struct nl_msg *msg;
5991 int ret;
5992 u32 val;
5993
5994 if (frag >= 2346)
5995 val = (u32) -1;
5996 else
5997 val = frag;
5998
5999 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
6000 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
6001 nlmsg_free(msg);
6002 return -ENOBUFS;
6003 }
6004
6005 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6006 if (!ret)
6007 return 0;
6008 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6009 "%d: %d (%s)", frag, ret, strerror(-ret));
6010 return ret;
6011 }
6012
6013
6014 static int i802_flush(void *priv)
6015 {
6016 struct i802_bss *bss = priv;
6017 struct nl_msg *msg;
6018 int res;
6019
6020 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
6021 bss->ifname);
6022
6023 /*
6024 * XXX: FIX! this needs to flush all VLANs too
6025 */
6026 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
6027 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
6028 if (res) {
6029 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
6030 "(%s)", res, strerror(-res));
6031 }
6032 return res;
6033 }
6034
6035
6036 static int get_sta_handler(struct nl_msg *msg, void *arg)
6037 {
6038 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6039 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6040 struct hostap_sta_driver_data *data = arg;
6041 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6042 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6043 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6044 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6045 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6046 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6047 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
6048 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
6049 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
6050 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
6051 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
6052 [NL80211_STA_INFO_ACK_SIGNAL] = { .type = NLA_U8 },
6053 };
6054 struct nlattr *rate[NL80211_RATE_INFO_MAX + 1];
6055 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
6056 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
6057 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
6058 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
6059 [NL80211_RATE_INFO_VHT_MCS] = { .type = NLA_U8 },
6060 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
6061 [NL80211_RATE_INFO_VHT_NSS] = { .type = NLA_U8 },
6062 };
6063
6064 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6065 genlmsg_attrlen(gnlh, 0), NULL);
6066
6067 /*
6068 * TODO: validate the interface and mac address!
6069 * Otherwise, there's a race condition as soon as
6070 * the kernel starts sending station notifications.
6071 */
6072
6073 if (!tb[NL80211_ATTR_STA_INFO]) {
6074 wpa_printf(MSG_DEBUG, "sta stats missing!");
6075 return NL_SKIP;
6076 }
6077 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6078 tb[NL80211_ATTR_STA_INFO],
6079 stats_policy)) {
6080 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6081 return NL_SKIP;
6082 }
6083
6084 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6085 data->inactive_msec =
6086 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
6087 /* For backwards compatibility, fetch the 32-bit counters first. */
6088 if (stats[NL80211_STA_INFO_RX_BYTES])
6089 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6090 if (stats[NL80211_STA_INFO_TX_BYTES])
6091 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
6092 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
6093 stats[NL80211_STA_INFO_TX_BYTES64]) {
6094 /*
6095 * The driver supports 64-bit counters, so use them to override
6096 * the 32-bit values.
6097 */
6098 data->rx_bytes =
6099 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
6100 data->tx_bytes =
6101 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
6102 data->bytes_64bit = 1;
6103 }
6104 if (stats[NL80211_STA_INFO_RX_PACKETS])
6105 data->rx_packets =
6106 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6107 if (stats[NL80211_STA_INFO_TX_PACKETS])
6108 data->tx_packets =
6109 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
6110 if (stats[NL80211_STA_INFO_TX_FAILED])
6111 data->tx_retry_failed =
6112 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
6113 if (stats[NL80211_STA_INFO_SIGNAL])
6114 data->signal = nla_get_u8(stats[NL80211_STA_INFO_SIGNAL]);
6115 if (stats[NL80211_STA_INFO_ACK_SIGNAL]) {
6116 data->last_ack_rssi =
6117 nla_get_u8(stats[NL80211_STA_INFO_ACK_SIGNAL]);
6118 data->flags |= STA_DRV_DATA_LAST_ACK_RSSI;
6119 }
6120
6121 if (stats[NL80211_STA_INFO_TX_BITRATE] &&
6122 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6123 stats[NL80211_STA_INFO_TX_BITRATE],
6124 rate_policy) == 0) {
6125 if (rate[NL80211_RATE_INFO_BITRATE32])
6126 data->current_tx_rate =
6127 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6128 else if (rate[NL80211_RATE_INFO_BITRATE])
6129 data->current_tx_rate =
6130 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6131
6132 if (rate[NL80211_RATE_INFO_MCS]) {
6133 data->tx_mcs = nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6134 data->flags |= STA_DRV_DATA_TX_MCS;
6135 }
6136 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6137 data->tx_vhtmcs =
6138 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6139 data->flags |= STA_DRV_DATA_TX_VHT_MCS;
6140 }
6141 if (rate[NL80211_RATE_INFO_SHORT_GI])
6142 data->flags |= STA_DRV_DATA_TX_SHORT_GI;
6143 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6144 data->tx_vht_nss =
6145 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6146 data->flags |= STA_DRV_DATA_TX_VHT_NSS;
6147 }
6148 }
6149
6150 if (stats[NL80211_STA_INFO_RX_BITRATE] &&
6151 nla_parse_nested(rate, NL80211_RATE_INFO_MAX,
6152 stats[NL80211_STA_INFO_RX_BITRATE],
6153 rate_policy) == 0) {
6154 if (rate[NL80211_RATE_INFO_BITRATE32])
6155 data->current_rx_rate =
6156 nla_get_u32(rate[NL80211_RATE_INFO_BITRATE32]);
6157 else if (rate[NL80211_RATE_INFO_BITRATE])
6158 data->current_rx_rate =
6159 nla_get_u16(rate[NL80211_RATE_INFO_BITRATE]);
6160
6161 if (rate[NL80211_RATE_INFO_MCS]) {
6162 data->rx_mcs =
6163 nla_get_u8(rate[NL80211_RATE_INFO_MCS]);
6164 data->flags |= STA_DRV_DATA_RX_MCS;
6165 }
6166 if (rate[NL80211_RATE_INFO_VHT_MCS]) {
6167 data->rx_vhtmcs =
6168 nla_get_u8(rate[NL80211_RATE_INFO_VHT_MCS]);
6169 data->flags |= STA_DRV_DATA_RX_VHT_MCS;
6170 }
6171 if (rate[NL80211_RATE_INFO_SHORT_GI])
6172 data->flags |= STA_DRV_DATA_RX_SHORT_GI;
6173 if (rate[NL80211_RATE_INFO_VHT_NSS]) {
6174 data->rx_vht_nss =
6175 nla_get_u8(rate[NL80211_RATE_INFO_VHT_NSS]);
6176 data->flags |= STA_DRV_DATA_RX_VHT_NSS;
6177 }
6178 }
6179
6180 return NL_SKIP;
6181 }
6182
6183 static int i802_read_sta_data(struct i802_bss *bss,
6184 struct hostap_sta_driver_data *data,
6185 const u8 *addr)
6186 {
6187 struct nl_msg *msg;
6188
6189 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
6190 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6191 nlmsg_free(msg);
6192 return -ENOBUFS;
6193 }
6194
6195 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
6196 }
6197
6198
6199 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6200 int cw_min, int cw_max, int burst_time)
6201 {
6202 struct i802_bss *bss = priv;
6203 struct wpa_driver_nl80211_data *drv = bss->drv;
6204 struct nl_msg *msg;
6205 struct nlattr *txq, *params;
6206
6207 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
6208 if (!msg)
6209 return -1;
6210
6211 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6212 if (!txq)
6213 goto fail;
6214
6215 /* We are only sending parameters for a single TXQ at a time */
6216 params = nla_nest_start(msg, 1);
6217 if (!params)
6218 goto fail;
6219
6220 switch (queue) {
6221 case 0:
6222 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
6223 goto fail;
6224 break;
6225 case 1:
6226 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
6227 goto fail;
6228 break;
6229 case 2:
6230 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
6231 goto fail;
6232 break;
6233 case 3:
6234 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
6235 goto fail;
6236 break;
6237 }
6238 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6239 * 32 usec, so need to convert the value here. */
6240 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
6241 (burst_time * 100 + 16) / 32) ||
6242 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
6243 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
6244 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
6245 goto fail;
6246
6247 nla_nest_end(msg, params);
6248
6249 nla_nest_end(msg, txq);
6250
6251 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6252 return 0;
6253 msg = NULL;
6254 fail:
6255 nlmsg_free(msg);
6256 return -1;
6257 }
6258
6259
6260 static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
6261 const char *ifname, int vlan_id)
6262 {
6263 struct wpa_driver_nl80211_data *drv = bss->drv;
6264 struct nl_msg *msg;
6265 int ret;
6266
6267 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
6268 ", ifname=%s[%d], vlan_id=%d)",
6269 bss->ifname, if_nametoindex(bss->ifname),
6270 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
6271 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
6272 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
6273 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
6274 nlmsg_free(msg);
6275 return -ENOBUFS;
6276 }
6277
6278 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6279 if (ret < 0) {
6280 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6281 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6282 MAC2STR(addr), ifname, vlan_id, ret,
6283 strerror(-ret));
6284 }
6285 return ret;
6286 }
6287
6288
6289 static int i802_get_inact_sec(void *priv, const u8 *addr)
6290 {
6291 struct hostap_sta_driver_data data;
6292 int ret;
6293
6294 os_memset(&data, 0, sizeof(data));
6295 data.inactive_msec = (unsigned long) -1;
6296 ret = i802_read_sta_data(priv, &data, addr);
6297 if (ret == -ENOENT)
6298 return -ENOENT;
6299 if (ret || data.inactive_msec == (unsigned long) -1)
6300 return -1;
6301 return data.inactive_msec / 1000;
6302 }
6303
6304
6305 static int i802_sta_clear_stats(void *priv, const u8 *addr)
6306 {
6307 #if 0
6308 /* TODO */
6309 #endif
6310 return 0;
6311 }
6312
6313
6314 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6315 int reason)
6316 {
6317 struct i802_bss *bss = priv;
6318 struct wpa_driver_nl80211_data *drv = bss->drv;
6319 struct ieee80211_mgmt mgmt;
6320 u8 channel;
6321
6322 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6323 HOSTAPD_MODE_IEEE80211AD) {
6324 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6325 * disassociate the STA instead. */
6326 return i802_sta_disassoc(priv, own_addr, addr, reason);
6327 }
6328
6329 if (is_mesh_interface(drv->nlmode))
6330 return -1;
6331
6332 if (drv->device_ap_sme)
6333 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
6334
6335 memset(&mgmt, 0, sizeof(mgmt));
6336 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6337 WLAN_FC_STYPE_DEAUTH);
6338 memcpy(mgmt.da, addr, ETH_ALEN);
6339 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6340 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6341 mgmt.u.deauth.reason_code = host_to_le16(reason);
6342 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6343 IEEE80211_HDRLEN +
6344 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
6345 0, NULL, 0);
6346 }
6347
6348
6349 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6350 int reason)
6351 {
6352 struct i802_bss *bss = priv;
6353 struct wpa_driver_nl80211_data *drv = bss->drv;
6354 struct ieee80211_mgmt mgmt;
6355
6356 if (is_mesh_interface(drv->nlmode))
6357 return -1;
6358
6359 if (drv->device_ap_sme)
6360 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
6361
6362 memset(&mgmt, 0, sizeof(mgmt));
6363 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6364 WLAN_FC_STYPE_DISASSOC);
6365 memcpy(mgmt.da, addr, ETH_ALEN);
6366 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6367 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6368 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6369 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6370 IEEE80211_HDRLEN +
6371 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
6372 0, NULL, 0);
6373 }
6374
6375
6376 static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6377 {
6378 char buf[200], *pos, *end;
6379 int i, res;
6380
6381 pos = buf;
6382 end = pos + sizeof(buf);
6383
6384 for (i = 0; i < drv->num_if_indices; i++) {
6385 if (!drv->if_indices[i])
6386 continue;
6387 res = os_snprintf(pos, end - pos, " %d(%d)",
6388 drv->if_indices[i],
6389 drv->if_indices_reason[i]);
6390 if (os_snprintf_error(end - pos, res))
6391 break;
6392 pos += res;
6393 }
6394 *pos = '\0';
6395
6396 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6397 drv->num_if_indices, buf);
6398 }
6399
6400
6401 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6402 int ifidx_reason)
6403 {
6404 int i;
6405 int *old, *old_reason;
6406
6407 wpa_printf(MSG_DEBUG,
6408 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6409 ifidx, ifidx_reason);
6410 if (have_ifidx(drv, ifidx, ifidx_reason)) {
6411 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6412 ifidx);
6413 return;
6414 }
6415 for (i = 0; i < drv->num_if_indices; i++) {
6416 if (drv->if_indices[i] == 0) {
6417 drv->if_indices[i] = ifidx;
6418 drv->if_indices_reason[i] = ifidx_reason;
6419 dump_ifidx(drv);
6420 return;
6421 }
6422 }
6423
6424 if (drv->if_indices != drv->default_if_indices)
6425 old = drv->if_indices;
6426 else
6427 old = NULL;
6428
6429 if (drv->if_indices_reason != drv->default_if_indices_reason)
6430 old_reason = drv->if_indices_reason;
6431 else
6432 old_reason = NULL;
6433
6434 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6435 sizeof(int));
6436 drv->if_indices_reason = os_realloc_array(old_reason,
6437 drv->num_if_indices + 1,
6438 sizeof(int));
6439 if (!drv->if_indices) {
6440 if (!old)
6441 drv->if_indices = drv->default_if_indices;
6442 else
6443 drv->if_indices = old;
6444 }
6445 if (!drv->if_indices_reason) {
6446 if (!old_reason)
6447 drv->if_indices_reason = drv->default_if_indices_reason;
6448 else
6449 drv->if_indices_reason = old_reason;
6450 }
6451 if (!drv->if_indices || !drv->if_indices_reason) {
6452 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6453 "interfaces");
6454 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6455 return;
6456 }
6457 if (!old)
6458 os_memcpy(drv->if_indices, drv->default_if_indices,
6459 sizeof(drv->default_if_indices));
6460 if (!old_reason)
6461 os_memcpy(drv->if_indices_reason,
6462 drv->default_if_indices_reason,
6463 sizeof(drv->default_if_indices_reason));
6464 drv->if_indices[drv->num_if_indices] = ifidx;
6465 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
6466 drv->num_if_indices++;
6467 dump_ifidx(drv);
6468 }
6469
6470
6471 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6472 int ifidx_reason)
6473 {
6474 int i;
6475
6476 for (i = 0; i < drv->num_if_indices; i++) {
6477 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6478 (drv->if_indices_reason[i] == ifidx_reason ||
6479 ifidx_reason == IFIDX_ANY)) {
6480 drv->if_indices[i] = 0;
6481 break;
6482 }
6483 }
6484 dump_ifidx(drv);
6485 }
6486
6487
6488 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6489 int ifidx_reason)
6490 {
6491 int i;
6492
6493 for (i = 0; i < drv->num_if_indices; i++)
6494 if (drv->if_indices[i] == ifidx &&
6495 (drv->if_indices_reason[i] == ifidx_reason ||
6496 ifidx_reason == IFIDX_ANY))
6497 return 1;
6498
6499 return 0;
6500 }
6501
6502
6503 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
6504 const char *bridge_ifname, char *ifname_wds)
6505 {
6506 struct i802_bss *bss = priv;
6507 struct wpa_driver_nl80211_data *drv = bss->drv;
6508 char name[IFNAMSIZ + 1];
6509
6510 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
6511 if (ifname_wds)
6512 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6513
6514 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6515 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6516 if (val) {
6517 if (!if_nametoindex(name)) {
6518 if (nl80211_create_iface(drv, name,
6519 NL80211_IFTYPE_AP_VLAN,
6520 bss->addr, 1, NULL, NULL, 0) <
6521 0)
6522 return -1;
6523 if (bridge_ifname &&
6524 linux_br_add_if(drv->global->ioctl_sock,
6525 bridge_ifname, name) < 0)
6526 return -1;
6527 }
6528 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6529 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6530 "interface %s up", name);
6531 }
6532 return i802_set_sta_vlan(priv, addr, name, 0);
6533 } else {
6534 if (bridge_ifname)
6535 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6536 name);
6537
6538 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
6539 nl80211_remove_iface(drv, if_nametoindex(name));
6540 return 0;
6541 }
6542 }
6543
6544
6545 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6546 {
6547 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6548 struct sockaddr_ll lladdr;
6549 unsigned char buf[3000];
6550 int len;
6551 socklen_t fromlen = sizeof(lladdr);
6552
6553 len = recvfrom(sock, buf, sizeof(buf), 0,
6554 (struct sockaddr *)&lladdr, &fromlen);
6555 if (len < 0) {
6556 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6557 strerror(errno));
6558 return;
6559 }
6560
6561 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
6562 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6563 }
6564
6565
6566 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6567 struct i802_bss *bss,
6568 const char *brname, const char *ifname)
6569 {
6570 int br_ifindex;
6571 char in_br[IFNAMSIZ];
6572
6573 os_strlcpy(bss->brname, brname, IFNAMSIZ);
6574 br_ifindex = if_nametoindex(brname);
6575 if (br_ifindex == 0) {
6576 /*
6577 * Bridge was configured, but the bridge device does
6578 * not exist. Try to add it now.
6579 */
6580 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
6581 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6582 "bridge interface %s: %s",
6583 brname, strerror(errno));
6584 return -1;
6585 }
6586 bss->added_bridge = 1;
6587 br_ifindex = if_nametoindex(brname);
6588 add_ifidx(drv, br_ifindex, drv->ifindex);
6589 }
6590 bss->br_ifindex = br_ifindex;
6591
6592 if (linux_br_get(in_br, ifname) == 0) {
6593 if (os_strcmp(in_br, brname) == 0)
6594 return 0; /* already in the bridge */
6595
6596 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6597 "bridge %s", ifname, in_br);
6598 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6599 0) {
6600 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6601 "remove interface %s from bridge "
6602 "%s: %s",
6603 ifname, in_br, strerror(errno));
6604 return -1;
6605 }
6606 }
6607
6608 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6609 ifname, brname);
6610 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
6611 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6612 "into bridge %s: %s",
6613 ifname, brname, strerror(errno));
6614 return -1;
6615 }
6616 bss->added_if_into_bridge = 1;
6617
6618 return 0;
6619 }
6620
6621
6622 static void *i802_init(struct hostapd_data *hapd,
6623 struct wpa_init_params *params)
6624 {
6625 struct wpa_driver_nl80211_data *drv;
6626 struct i802_bss *bss;
6627 size_t i;
6628 char master_ifname[IFNAMSIZ];
6629 int ifindex, br_ifindex = 0;
6630 int br_added = 0;
6631
6632 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6633 params->global_priv, 1,
6634 params->bssid, params->driver_params);
6635 if (bss == NULL)
6636 return NULL;
6637
6638 drv = bss->drv;
6639
6640 if (linux_br_get(master_ifname, params->ifname) == 0) {
6641 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
6642 params->ifname, master_ifname);
6643 br_ifindex = if_nametoindex(master_ifname);
6644 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6645 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6646 linux_master_get(master_ifname, params->ifname) == 0) {
6647 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6648 params->ifname, master_ifname);
6649 /* start listening for EAPOL on the master interface */
6650 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
6651
6652 /* check if master itself is under bridge */
6653 if (linux_br_get(master_ifname, master_ifname) == 0) {
6654 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6655 master_ifname);
6656 br_ifindex = if_nametoindex(master_ifname);
6657 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6658 }
6659 } else {
6660 master_ifname[0] = '\0';
6661 }
6662
6663 bss->br_ifindex = br_ifindex;
6664
6665 for (i = 0; i < params->num_bridge; i++) {
6666 if (params->bridge[i]) {
6667 ifindex = if_nametoindex(params->bridge[i]);
6668 if (ifindex)
6669 add_ifidx(drv, ifindex, drv->ifindex);
6670 if (ifindex == br_ifindex)
6671 br_added = 1;
6672 }
6673 }
6674
6675 /* start listening for EAPOL on the default AP interface */
6676 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
6677
6678 if (params->num_bridge && params->bridge[0]) {
6679 if (i802_check_bridge(drv, bss, params->bridge[0],
6680 params->ifname) < 0)
6681 goto failed;
6682 if (os_strcmp(params->bridge[0], master_ifname) != 0)
6683 br_added = 1;
6684 }
6685
6686 if (!br_added && br_ifindex &&
6687 (params->num_bridge == 0 || !params->bridge[0]))
6688 add_ifidx(drv, br_ifindex, drv->ifindex);
6689
6690 #ifdef CONFIG_LIBNL3_ROUTE
6691 if (bss->added_if_into_bridge) {
6692 drv->rtnl_sk = nl_socket_alloc();
6693 if (drv->rtnl_sk == NULL) {
6694 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6695 goto failed;
6696 }
6697
6698 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6699 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6700 strerror(errno));
6701 goto failed;
6702 }
6703 }
6704 #endif /* CONFIG_LIBNL3_ROUTE */
6705
6706 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6707 if (drv->eapol_sock < 0) {
6708 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6709 strerror(errno));
6710 goto failed;
6711 }
6712
6713 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6714 {
6715 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
6716 goto failed;
6717 }
6718
6719 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6720 params->own_addr))
6721 goto failed;
6722 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
6723
6724 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6725
6726 return bss;
6727
6728 failed:
6729 wpa_driver_nl80211_deinit(bss);
6730 return NULL;
6731 }
6732
6733
6734 static void i802_deinit(void *priv)
6735 {
6736 struct i802_bss *bss = priv;
6737 wpa_driver_nl80211_deinit(bss);
6738 }
6739
6740
6741 static enum nl80211_iftype wpa_driver_nl80211_if_type(
6742 enum wpa_driver_if_type type)
6743 {
6744 switch (type) {
6745 case WPA_IF_STATION:
6746 return NL80211_IFTYPE_STATION;
6747 case WPA_IF_P2P_CLIENT:
6748 case WPA_IF_P2P_GROUP:
6749 return NL80211_IFTYPE_P2P_CLIENT;
6750 case WPA_IF_AP_VLAN:
6751 return NL80211_IFTYPE_AP_VLAN;
6752 case WPA_IF_AP_BSS:
6753 return NL80211_IFTYPE_AP;
6754 case WPA_IF_P2P_GO:
6755 return NL80211_IFTYPE_P2P_GO;
6756 case WPA_IF_P2P_DEVICE:
6757 return NL80211_IFTYPE_P2P_DEVICE;
6758 case WPA_IF_MESH:
6759 return NL80211_IFTYPE_MESH_POINT;
6760 default:
6761 return -1;
6762 }
6763 }
6764
6765
6766 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6767 {
6768 struct wpa_driver_nl80211_data *drv;
6769 dl_list_for_each(drv, &global->interfaces,
6770 struct wpa_driver_nl80211_data, list) {
6771 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
6772 return 1;
6773 }
6774 return 0;
6775 }
6776
6777
6778 static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
6779 {
6780 unsigned int idx;
6781
6782 if (!drv->global)
6783 return -1;
6784
6785 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
6786 for (idx = 0; idx < 64; idx++) {
6787 new_addr[0] = drv->first_bss->addr[0] | 0x02;
6788 new_addr[0] ^= idx << 2;
6789 if (!nl80211_addr_in_use(drv->global, new_addr))
6790 break;
6791 }
6792 if (idx == 64)
6793 return -1;
6794
6795 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
6796 MACSTR, MAC2STR(new_addr));
6797
6798 return 0;
6799 }
6800
6801
6802 struct wdev_info {
6803 u64 wdev_id;
6804 int wdev_id_set;
6805 u8 macaddr[ETH_ALEN];
6806 };
6807
6808 static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6809 {
6810 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6811 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6812 struct wdev_info *wi = arg;
6813
6814 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6815 genlmsg_attrlen(gnlh, 0), NULL);
6816 if (tb[NL80211_ATTR_WDEV]) {
6817 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6818 wi->wdev_id_set = 1;
6819 }
6820
6821 if (tb[NL80211_ATTR_MAC])
6822 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6823 ETH_ALEN);
6824
6825 return NL_SKIP;
6826 }
6827
6828
6829 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6830 const char *ifname, const u8 *addr,
6831 void *bss_ctx, void **drv_priv,
6832 char *force_ifname, u8 *if_addr,
6833 const char *bridge, int use_existing,
6834 int setup_ap)
6835 {
6836 enum nl80211_iftype nlmode;
6837 struct i802_bss *bss = priv;
6838 struct wpa_driver_nl80211_data *drv = bss->drv;
6839 int ifidx;
6840 int added = 1;
6841
6842 if (addr)
6843 os_memcpy(if_addr, addr, ETH_ALEN);
6844 nlmode = wpa_driver_nl80211_if_type(type);
6845 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6846 struct wdev_info p2pdev_info;
6847
6848 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6849 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6850 0, nl80211_wdev_handler,
6851 &p2pdev_info, use_existing);
6852 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6853 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6854 ifname);
6855 return -1;
6856 }
6857
6858 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6859 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6860 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6861 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6862 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6863 ifname,
6864 (long long unsigned int) p2pdev_info.wdev_id);
6865 } else {
6866 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6867 0, NULL, NULL, use_existing);
6868 if (use_existing && ifidx == -ENFILE) {
6869 added = 0;
6870 ifidx = if_nametoindex(ifname);
6871 } else if (ifidx < 0) {
6872 return -1;
6873 }
6874 }
6875
6876 if (!addr) {
6877 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
6878 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6879 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
6880 ifname, if_addr) < 0) {
6881 if (added)
6882 nl80211_remove_iface(drv, ifidx);
6883 return -1;
6884 }
6885 }
6886
6887 if (!addr &&
6888 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
6889 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6890 type == WPA_IF_STATION)) {
6891 /* Enforce unique address */
6892 u8 new_addr[ETH_ALEN];
6893
6894 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
6895 new_addr) < 0) {
6896 if (added)
6897 nl80211_remove_iface(drv, ifidx);
6898 return -1;
6899 }
6900 if (nl80211_addr_in_use(drv->global, new_addr)) {
6901 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
6902 "for interface %s type %d", ifname, type);
6903 if (nl80211_vif_addr(drv, new_addr) < 0) {
6904 if (added)
6905 nl80211_remove_iface(drv, ifidx);
6906 return -1;
6907 }
6908 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6909 new_addr) < 0) {
6910 if (added)
6911 nl80211_remove_iface(drv, ifidx);
6912 return -1;
6913 }
6914 }
6915 os_memcpy(if_addr, new_addr, ETH_ALEN);
6916 }
6917
6918 if (type == WPA_IF_AP_BSS && setup_ap) {
6919 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6920 if (new_bss == NULL) {
6921 if (added)
6922 nl80211_remove_iface(drv, ifidx);
6923 return -1;
6924 }
6925
6926 if (bridge &&
6927 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6928 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6929 "interface %s to a bridge %s",
6930 ifname, bridge);
6931 if (added)
6932 nl80211_remove_iface(drv, ifidx);
6933 os_free(new_bss);
6934 return -1;
6935 }
6936
6937 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6938 {
6939 if (added)
6940 nl80211_remove_iface(drv, ifidx);
6941 os_free(new_bss);
6942 return -1;
6943 }
6944 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
6945 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
6946 new_bss->ifindex = ifidx;
6947 new_bss->drv = drv;
6948 new_bss->next = drv->first_bss->next;
6949 new_bss->freq = drv->first_bss->freq;
6950 new_bss->ctx = bss_ctx;
6951 new_bss->added_if = added;
6952 drv->first_bss->next = new_bss;
6953 if (drv_priv)
6954 *drv_priv = new_bss;
6955 nl80211_init_bss(new_bss);
6956
6957 /* Subscribe management frames for this WPA_IF_AP_BSS */
6958 if (nl80211_setup_ap(new_bss))
6959 return -1;
6960 }
6961
6962 if (drv->global)
6963 drv->global->if_add_ifindex = ifidx;
6964
6965 /*
6966 * Some virtual interfaces need to process EAPOL packets and events on
6967 * the parent interface. This is used mainly with hostapd.
6968 */
6969 if (ifidx > 0 &&
6970 (drv->hostapd ||
6971 nlmode == NL80211_IFTYPE_AP_VLAN ||
6972 nlmode == NL80211_IFTYPE_WDS ||
6973 nlmode == NL80211_IFTYPE_MONITOR))
6974 add_ifidx(drv, ifidx, IFIDX_ANY);
6975
6976 return 0;
6977 }
6978
6979
6980 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
6981 enum wpa_driver_if_type type,
6982 const char *ifname)
6983 {
6984 struct wpa_driver_nl80211_data *drv = bss->drv;
6985 int ifindex = if_nametoindex(ifname);
6986
6987 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6988 __func__, type, ifname, ifindex, bss->added_if);
6989 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
6990 nl80211_remove_iface(drv, ifindex);
6991 else if (ifindex > 0 && !bss->added_if) {
6992 struct wpa_driver_nl80211_data *drv2;
6993 dl_list_for_each(drv2, &drv->global->interfaces,
6994 struct wpa_driver_nl80211_data, list) {
6995 del_ifidx(drv2, ifindex, IFIDX_ANY);
6996 del_ifidx(drv2, IFIDX_ANY, ifindex);
6997 }
6998 }
6999
7000 if (type != WPA_IF_AP_BSS)
7001 return 0;
7002
7003 if (bss->added_if_into_bridge) {
7004 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7005 bss->ifname) < 0)
7006 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7007 "interface %s from bridge %s: %s",
7008 bss->ifname, bss->brname, strerror(errno));
7009 }
7010 if (bss->added_bridge) {
7011 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
7012 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7013 "bridge %s: %s",
7014 bss->brname, strerror(errno));
7015 }
7016
7017 if (bss != drv->first_bss) {
7018 struct i802_bss *tbss;
7019
7020 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
7021 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
7022 if (tbss->next == bss) {
7023 tbss->next = bss->next;
7024 /* Unsubscribe management frames */
7025 nl80211_teardown_ap(bss);
7026 nl80211_destroy_bss(bss);
7027 if (!bss->added_if)
7028 i802_set_iface_flags(bss, 0);
7029 os_free(bss);
7030 bss = NULL;
7031 break;
7032 }
7033 }
7034 if (bss)
7035 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7036 "BSS %p in the list", __func__, bss);
7037 } else {
7038 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
7039 nl80211_teardown_ap(bss);
7040 if (!bss->added_if && !drv->first_bss->next)
7041 wpa_driver_nl80211_del_beacon(bss);
7042 nl80211_destroy_bss(bss);
7043 if (!bss->added_if)
7044 i802_set_iface_flags(bss, 0);
7045 if (drv->first_bss->next) {
7046 drv->first_bss = drv->first_bss->next;
7047 drv->ctx = drv->first_bss->ctx;
7048 os_free(bss);
7049 } else {
7050 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
7051 }
7052 }
7053
7054 return 0;
7055 }
7056
7057
7058 static int cookie_handler(struct nl_msg *msg, void *arg)
7059 {
7060 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7061 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7062 u64 *cookie = arg;
7063 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7064 genlmsg_attrlen(gnlh, 0), NULL);
7065 if (tb[NL80211_ATTR_COOKIE])
7066 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7067 return NL_SKIP;
7068 }
7069
7070
7071 static int nl80211_send_frame_cmd(struct i802_bss *bss,
7072 unsigned int freq, unsigned int wait,
7073 const u8 *buf, size_t buf_len,
7074 u64 *cookie_out, int no_cck, int no_ack,
7075 int offchanok, const u16 *csa_offs,
7076 size_t csa_offs_len)
7077 {
7078 struct wpa_driver_nl80211_data *drv = bss->drv;
7079 struct nl_msg *msg;
7080 u64 cookie;
7081 int ret = -1;
7082
7083 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
7084 "no_ack=%d offchanok=%d",
7085 freq, wait, no_cck, no_ack, offchanok);
7086 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
7087
7088 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
7089 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
7090 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
7091 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7092 drv->test_use_roc_tx) &&
7093 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
7094 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
7095 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
7096 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
7097 csa_offs_len * sizeof(u16), csa_offs)) ||
7098 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
7099 goto fail;
7100
7101 cookie = 0;
7102 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7103 msg = NULL;
7104 if (ret) {
7105 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
7106 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7107 freq, wait);
7108 } else {
7109 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
7110 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7111 (long long unsigned int) cookie);
7112
7113 if (cookie_out)
7114 *cookie_out = no_ack ? (u64) -1 : cookie;
7115
7116 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
7117 wpa_printf(MSG_DEBUG,
7118 "nl80211: Drop oldest pending send action cookie 0x%llx",
7119 (long long unsigned int)
7120 drv->send_action_cookies[0]);
7121 os_memmove(&drv->send_action_cookies[0],
7122 &drv->send_action_cookies[1],
7123 (MAX_SEND_ACTION_COOKIES - 1) *
7124 sizeof(u64));
7125 drv->num_send_action_cookies--;
7126 }
7127 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
7128 drv->num_send_action_cookies++;
7129 }
7130
7131 fail:
7132 nlmsg_free(msg);
7133 return ret;
7134 }
7135
7136
7137 static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
7138 unsigned int freq,
7139 unsigned int wait_time,
7140 const u8 *dst, const u8 *src,
7141 const u8 *bssid,
7142 const u8 *data, size_t data_len,
7143 int no_cck)
7144 {
7145 struct wpa_driver_nl80211_data *drv = bss->drv;
7146 int ret = -1;
7147 u8 *buf;
7148 struct ieee80211_hdr *hdr;
7149
7150 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
7151 "freq=%u MHz wait=%d ms no_cck=%d)",
7152 drv->ifindex, freq, wait_time, no_cck);
7153
7154 buf = os_zalloc(24 + data_len);
7155 if (buf == NULL)
7156 return ret;
7157 os_memcpy(buf + 24, data, data_len);
7158 hdr = (struct ieee80211_hdr *) buf;
7159 hdr->frame_control =
7160 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
7161 os_memcpy(hdr->addr1, dst, ETH_ALEN);
7162 os_memcpy(hdr->addr2, src, ETH_ALEN);
7163 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
7164
7165 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
7166 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
7167 MAC2STR(src));
7168 os_memcpy(bss->rand_addr, src, ETH_ALEN);
7169 } else {
7170 os_memset(bss->rand_addr, 0, ETH_ALEN);
7171 }
7172
7173 if (is_ap_interface(drv->nlmode) &&
7174 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
7175 (int) freq == bss->freq || drv->device_ap_sme ||
7176 !drv->use_monitor))
7177 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
7178 0, freq, no_cck, 1,
7179 wait_time, NULL, 0);
7180 else
7181 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
7182 24 + data_len,
7183 &drv->send_action_cookie,
7184 no_cck, 0, 1, NULL, 0);
7185
7186 os_free(buf);
7187 return ret;
7188 }
7189
7190
7191 static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
7192 {
7193 struct wpa_driver_nl80211_data *drv = bss->drv;
7194 struct nl_msg *msg;
7195 int ret;
7196
7197 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
7198 (long long unsigned int) cookie);
7199 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
7200 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
7201 nlmsg_free(msg);
7202 return;
7203 }
7204
7205 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7206 if (ret)
7207 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
7208 "(%s)", ret, strerror(-ret));
7209 }
7210
7211
7212 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
7213 {
7214 struct i802_bss *bss = priv;
7215 struct wpa_driver_nl80211_data *drv = bss->drv;
7216 unsigned int i;
7217 u64 cookie;
7218
7219 /* Cancel the last pending TX cookie */
7220 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
7221
7222 /*
7223 * Cancel the other pending TX cookies, if any. This is needed since
7224 * the driver may keep a list of all pending offchannel TX operations
7225 * and free up the radio only once they have expired or cancelled.
7226 */
7227 for (i = drv->num_send_action_cookies; i > 0; i--) {
7228 cookie = drv->send_action_cookies[i - 1];
7229 if (cookie != drv->send_action_cookie)
7230 nl80211_frame_wait_cancel(bss, cookie);
7231 }
7232 drv->num_send_action_cookies = 0;
7233 }
7234
7235
7236 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
7237 unsigned int duration)
7238 {
7239 struct i802_bss *bss = priv;
7240 struct wpa_driver_nl80211_data *drv = bss->drv;
7241 struct nl_msg *msg;
7242 int ret;
7243 u64 cookie;
7244
7245 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
7246 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
7247 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
7248 nlmsg_free(msg);
7249 return -1;
7250 }
7251
7252 cookie = 0;
7253 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7254 if (ret == 0) {
7255 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
7256 "0x%llx for freq=%u MHz duration=%u",
7257 (long long unsigned int) cookie, freq, duration);
7258 drv->remain_on_chan_cookie = cookie;
7259 drv->pending_remain_on_chan = 1;
7260 return 0;
7261 }
7262 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
7263 "(freq=%d duration=%u): %d (%s)",
7264 freq, duration, ret, strerror(-ret));
7265 return -1;
7266 }
7267
7268
7269 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
7270 {
7271 struct i802_bss *bss = priv;
7272 struct wpa_driver_nl80211_data *drv = bss->drv;
7273 struct nl_msg *msg;
7274 int ret;
7275
7276 if (!drv->pending_remain_on_chan) {
7277 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
7278 "to cancel");
7279 return -1;
7280 }
7281
7282 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7283 "0x%llx",
7284 (long long unsigned int) drv->remain_on_chan_cookie);
7285
7286 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7287 if (!msg ||
7288 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7289 nlmsg_free(msg);
7290 return -1;
7291 }
7292
7293 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7294 if (ret == 0)
7295 return 0;
7296 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7297 "%d (%s)", ret, strerror(-ret));
7298 return -1;
7299 }
7300
7301
7302 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
7303 {
7304 struct wpa_driver_nl80211_data *drv = bss->drv;
7305
7306 if (!report) {
7307 if (bss->nl_preq && drv->device_ap_sme &&
7308 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7309 !bss->static_ap) {
7310 /*
7311 * Do not disable Probe Request reporting that was
7312 * enabled in nl80211_setup_ap().
7313 */
7314 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7315 "Probe Request reporting nl_preq=%p while "
7316 "in AP mode", bss->nl_preq);
7317 } else if (bss->nl_preq) {
7318 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7319 "reporting nl_preq=%p", bss->nl_preq);
7320 nl80211_destroy_eloop_handle(&bss->nl_preq, 0);
7321 }
7322 return 0;
7323 }
7324
7325 if (bss->nl_preq) {
7326 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
7327 "already on! nl_preq=%p", bss->nl_preq);
7328 return 0;
7329 }
7330
7331 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7332 if (bss->nl_preq == NULL)
7333 return -1;
7334 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7335 "reporting nl_preq=%p", bss->nl_preq);
7336
7337 if (nl80211_register_frame(bss, bss->nl_preq,
7338 (WLAN_FC_TYPE_MGMT << 2) |
7339 (WLAN_FC_STYPE_PROBE_REQ << 4),
7340 NULL, 0) < 0)
7341 goto out_err;
7342
7343 nl80211_register_eloop_read(&bss->nl_preq,
7344 wpa_driver_nl80211_event_receive,
7345 bss->nl_cb, 0);
7346
7347 return 0;
7348
7349 out_err:
7350 nl_destroy_handles(&bss->nl_preq);
7351 return -1;
7352 }
7353
7354
7355 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7356 int ifindex, int disabled)
7357 {
7358 struct nl_msg *msg;
7359 struct nlattr *bands, *band;
7360 int ret;
7361
7362 wpa_printf(MSG_DEBUG,
7363 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7364 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7365 "no NL80211_TXRATE_LEGACY constraint");
7366
7367 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7368 NL80211_CMD_SET_TX_BITRATE_MASK);
7369 if (!msg)
7370 return -1;
7371
7372 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7373 if (!bands)
7374 goto fail;
7375
7376 /*
7377 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7378 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7379 * rates. All 5 GHz rates are left enabled.
7380 */
7381 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
7382 if (!band ||
7383 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7384 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7385 goto fail;
7386 nla_nest_end(msg, band);
7387
7388 nla_nest_end(msg, bands);
7389
7390 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7391 if (ret) {
7392 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7393 "(%s)", ret, strerror(-ret));
7394 } else
7395 drv->disabled_11b_rates = disabled;
7396
7397 return ret;
7398
7399 fail:
7400 nlmsg_free(msg);
7401 return -1;
7402 }
7403
7404
7405 static int wpa_driver_nl80211_deinit_ap(void *priv)
7406 {
7407 struct i802_bss *bss = priv;
7408 struct wpa_driver_nl80211_data *drv = bss->drv;
7409 if (!is_ap_interface(drv->nlmode))
7410 return -1;
7411 wpa_driver_nl80211_del_beacon(bss);
7412 bss->beacon_set = 0;
7413
7414 /*
7415 * If the P2P GO interface was dynamically added, then it is
7416 * possible that the interface change to station is not possible.
7417 */
7418 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7419 return 0;
7420
7421 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7422 }
7423
7424
7425 static int wpa_driver_nl80211_stop_ap(void *priv)
7426 {
7427 struct i802_bss *bss = priv;
7428 struct wpa_driver_nl80211_data *drv = bss->drv;
7429 if (!is_ap_interface(drv->nlmode))
7430 return -1;
7431 wpa_driver_nl80211_del_beacon(bss);
7432 bss->beacon_set = 0;
7433 return 0;
7434 }
7435
7436
7437 static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7438 {
7439 struct i802_bss *bss = priv;
7440 struct wpa_driver_nl80211_data *drv = bss->drv;
7441 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7442 return -1;
7443
7444 /*
7445 * If the P2P Client interface was dynamically added, then it is
7446 * possible that the interface change to station is not possible.
7447 */
7448 if (bss->if_dynamic)
7449 return 0;
7450
7451 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7452 }
7453
7454
7455 static void wpa_driver_nl80211_resume(void *priv)
7456 {
7457 struct i802_bss *bss = priv;
7458 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
7459
7460 if (i802_set_iface_flags(bss, 1))
7461 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
7462
7463 if (is_p2p_net_interface(nlmode))
7464 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
7465 }
7466
7467
7468 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7469 {
7470 struct i802_bss *bss = priv;
7471 struct wpa_driver_nl80211_data *drv = bss->drv;
7472 struct nl_msg *msg;
7473 struct nlattr *cqm;
7474
7475 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7476 "hysteresis=%d", threshold, hysteresis);
7477
7478 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7479 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7480 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7481 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7482 nlmsg_free(msg);
7483 return -1;
7484 }
7485 nla_nest_end(msg, cqm);
7486
7487 return send_and_recv_msgs(drv, msg, NULL, NULL);
7488 }
7489
7490
7491 static int get_channel_width(struct nl_msg *msg, void *arg)
7492 {
7493 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7494 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7495 struct wpa_signal_info *sig_change = arg;
7496
7497 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7498 genlmsg_attrlen(gnlh, 0), NULL);
7499
7500 sig_change->center_frq1 = -1;
7501 sig_change->center_frq2 = -1;
7502 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7503
7504 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7505 sig_change->chanwidth = convert2width(
7506 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7507 if (tb[NL80211_ATTR_CENTER_FREQ1])
7508 sig_change->center_frq1 =
7509 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7510 if (tb[NL80211_ATTR_CENTER_FREQ2])
7511 sig_change->center_frq2 =
7512 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7513 }
7514
7515 return NL_SKIP;
7516 }
7517
7518
7519 static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7520 struct wpa_signal_info *sig)
7521 {
7522 struct nl_msg *msg;
7523
7524 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
7525 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
7526 }
7527
7528
7529 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7530 {
7531 struct i802_bss *bss = priv;
7532 struct wpa_driver_nl80211_data *drv = bss->drv;
7533 int res;
7534
7535 os_memset(si, 0, sizeof(*si));
7536 res = nl80211_get_link_signal(drv, si);
7537 if (res) {
7538 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7539 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7540 return res;
7541 si->current_signal = 0;
7542 }
7543
7544 res = nl80211_get_channel_width(drv, si);
7545 if (res != 0)
7546 return res;
7547
7548 return nl80211_get_link_noise(drv, si);
7549 }
7550
7551
7552 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7553 int encrypt)
7554 {
7555 struct i802_bss *bss = priv;
7556 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
7557 0, 0, 0, 0, NULL, 0);
7558 }
7559
7560
7561 static int nl80211_set_param(void *priv, const char *param)
7562 {
7563 struct i802_bss *bss = priv;
7564 struct wpa_driver_nl80211_data *drv = bss->drv;
7565
7566 if (param == NULL)
7567 return 0;
7568 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
7569
7570 #ifdef CONFIG_P2P
7571 if (os_strstr(param, "use_p2p_group_interface=1")) {
7572 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7573 "interface");
7574 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7575 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7576 }
7577 #endif /* CONFIG_P2P */
7578
7579 if (os_strstr(param, "use_monitor=1"))
7580 drv->use_monitor = 1;
7581
7582 if (os_strstr(param, "force_connect_cmd=1")) {
7583 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
7584 drv->force_connect_cmd = 1;
7585 }
7586
7587 if (os_strstr(param, "force_bss_selection=1"))
7588 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7589
7590 if (os_strstr(param, "no_offchannel_tx=1")) {
7591 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7592 drv->test_use_roc_tx = 1;
7593 }
7594
7595 return 0;
7596 }
7597
7598
7599 static void * nl80211_global_init(void *ctx)
7600 {
7601 struct nl80211_global *global;
7602 struct netlink_config *cfg;
7603
7604 global = os_zalloc(sizeof(*global));
7605 if (global == NULL)
7606 return NULL;
7607 global->ctx = ctx;
7608 global->ioctl_sock = -1;
7609 dl_list_init(&global->interfaces);
7610 global->if_add_ifindex = -1;
7611
7612 cfg = os_zalloc(sizeof(*cfg));
7613 if (cfg == NULL)
7614 goto err;
7615
7616 cfg->ctx = global;
7617 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7618 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7619 global->netlink = netlink_init(cfg);
7620 if (global->netlink == NULL) {
7621 os_free(cfg);
7622 goto err;
7623 }
7624
7625 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7626 goto err;
7627
7628 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7629 if (global->ioctl_sock < 0) {
7630 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7631 strerror(errno));
7632 goto err;
7633 }
7634
7635 return global;
7636
7637 err:
7638 nl80211_global_deinit(global);
7639 return NULL;
7640 }
7641
7642
7643 static void nl80211_global_deinit(void *priv)
7644 {
7645 struct nl80211_global *global = priv;
7646 if (global == NULL)
7647 return;
7648 if (!dl_list_empty(&global->interfaces)) {
7649 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7650 "nl80211_global_deinit",
7651 dl_list_len(&global->interfaces));
7652 }
7653
7654 if (global->netlink)
7655 netlink_deinit(global->netlink);
7656
7657 nl_destroy_handles(&global->nl);
7658
7659 if (global->nl_event)
7660 nl80211_destroy_eloop_handle(&global->nl_event, 0);
7661
7662 nl_cb_put(global->nl_cb);
7663
7664 if (global->ioctl_sock >= 0)
7665 close(global->ioctl_sock);
7666
7667 os_free(global);
7668 }
7669
7670
7671 static const char * nl80211_get_radio_name(void *priv)
7672 {
7673 struct i802_bss *bss = priv;
7674 struct wpa_driver_nl80211_data *drv = bss->drv;
7675 return drv->phyname;
7676 }
7677
7678
7679 static int nl80211_pmkid(struct i802_bss *bss, int cmd,
7680 struct wpa_pmkid_params *params)
7681 {
7682 struct nl_msg *msg;
7683 const size_t PMK_MAX_LEN = 48; /* current cfg80211 limit */
7684
7685 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
7686 (params->pmkid &&
7687 nla_put(msg, NL80211_ATTR_PMKID, 16, params->pmkid)) ||
7688 (params->bssid &&
7689 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) ||
7690 (params->ssid_len &&
7691 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) ||
7692 (params->fils_cache_id &&
7693 nla_put(msg, NL80211_ATTR_FILS_CACHE_ID, 2,
7694 params->fils_cache_id)) ||
7695 (params->pmk_len && params->pmk_len <= PMK_MAX_LEN &&
7696 nla_put(msg, NL80211_ATTR_PMK, params->pmk_len, params->pmk))) {
7697 nlmsg_free(msg);
7698 return -ENOBUFS;
7699 }
7700
7701 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
7702 }
7703
7704
7705 static int nl80211_add_pmkid(void *priv, struct wpa_pmkid_params *params)
7706 {
7707 struct i802_bss *bss = priv;
7708 int ret;
7709
7710 if (params->bssid)
7711 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR,
7712 MAC2STR(params->bssid));
7713 else if (params->fils_cache_id && params->ssid_len) {
7714 wpa_printf(MSG_DEBUG,
7715 "nl80211: Add PMKSA for cache id %02x%02x SSID %s",
7716 params->fils_cache_id[0], params->fils_cache_id[1],
7717 wpa_ssid_txt(params->ssid, params->ssid_len));
7718 }
7719
7720 ret = nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, params);
7721 if (ret < 0) {
7722 wpa_printf(MSG_DEBUG,
7723 "nl80211: NL80211_CMD_SET_PMKSA failed: %d (%s)",
7724 ret, strerror(-ret));
7725 }
7726
7727 return ret;
7728 }
7729
7730
7731 static int nl80211_remove_pmkid(void *priv, struct wpa_pmkid_params *params)
7732 {
7733 struct i802_bss *bss = priv;
7734 int ret;
7735
7736 if (params->bssid)
7737 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7738 MAC2STR(params->bssid));
7739 else if (params->fils_cache_id && params->ssid_len) {
7740 wpa_printf(MSG_DEBUG,
7741 "nl80211: Delete PMKSA for cache id %02x%02x SSID %s",
7742 params->fils_cache_id[0], params->fils_cache_id[1],
7743 wpa_ssid_txt(params->ssid, params->ssid_len));
7744 }
7745
7746 ret = nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, params);
7747 if (ret < 0) {
7748 wpa_printf(MSG_DEBUG,
7749 "nl80211: NL80211_CMD_DEL_PMKSA failed: %d (%s)",
7750 ret, strerror(-ret));
7751 }
7752
7753 return ret;
7754 }
7755
7756
7757 static int nl80211_flush_pmkid(void *priv)
7758 {
7759 struct i802_bss *bss = priv;
7760 struct nl_msg *msg;
7761
7762 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7763 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_FLUSH_PMKSA);
7764 if (!msg)
7765 return -ENOBUFS;
7766 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
7767 }
7768
7769
7770 static void clean_survey_results(struct survey_results *survey_results)
7771 {
7772 struct freq_survey *survey, *tmp;
7773
7774 if (dl_list_empty(&survey_results->survey_list))
7775 return;
7776
7777 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7778 struct freq_survey, list) {
7779 dl_list_del(&survey->list);
7780 os_free(survey);
7781 }
7782 }
7783
7784
7785 static void add_survey(struct nlattr **sinfo, u32 ifidx,
7786 struct dl_list *survey_list)
7787 {
7788 struct freq_survey *survey;
7789
7790 survey = os_zalloc(sizeof(struct freq_survey));
7791 if (!survey)
7792 return;
7793
7794 survey->ifidx = ifidx;
7795 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7796 survey->filled = 0;
7797
7798 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7799 survey->nf = (int8_t)
7800 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7801 survey->filled |= SURVEY_HAS_NF;
7802 }
7803
7804 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7805 survey->channel_time =
7806 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7807 survey->filled |= SURVEY_HAS_CHAN_TIME;
7808 }
7809
7810 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7811 survey->channel_time_busy =
7812 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7813 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7814 }
7815
7816 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7817 survey->channel_time_rx =
7818 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7819 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7820 }
7821
7822 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7823 survey->channel_time_tx =
7824 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7825 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7826 }
7827
7828 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)",
7829 survey->freq,
7830 survey->nf,
7831 (unsigned long int) survey->channel_time,
7832 (unsigned long int) survey->channel_time_busy,
7833 (unsigned long int) survey->channel_time_tx,
7834 (unsigned long int) survey->channel_time_rx,
7835 survey->filled);
7836
7837 dl_list_add_tail(survey_list, &survey->list);
7838 }
7839
7840
7841 static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7842 unsigned int freq_filter)
7843 {
7844 if (!freq_filter)
7845 return 1;
7846
7847 return freq_filter == surveyed_freq;
7848 }
7849
7850
7851 static int survey_handler(struct nl_msg *msg, void *arg)
7852 {
7853 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7854 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7855 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7856 struct survey_results *survey_results;
7857 u32 surveyed_freq = 0;
7858 u32 ifidx;
7859
7860 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7861 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7862 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7863 };
7864
7865 survey_results = (struct survey_results *) arg;
7866
7867 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7868 genlmsg_attrlen(gnlh, 0), NULL);
7869
7870 if (!tb[NL80211_ATTR_IFINDEX])
7871 return NL_SKIP;
7872
7873 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7874
7875 if (!tb[NL80211_ATTR_SURVEY_INFO])
7876 return NL_SKIP;
7877
7878 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7879 tb[NL80211_ATTR_SURVEY_INFO],
7880 survey_policy))
7881 return NL_SKIP;
7882
7883 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7884 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7885 return NL_SKIP;
7886 }
7887
7888 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7889
7890 if (!check_survey_ok(sinfo, surveyed_freq,
7891 survey_results->freq_filter))
7892 return NL_SKIP;
7893
7894 if (survey_results->freq_filter &&
7895 survey_results->freq_filter != surveyed_freq) {
7896 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7897 surveyed_freq);
7898 return NL_SKIP;
7899 }
7900
7901 add_survey(sinfo, ifidx, &survey_results->survey_list);
7902
7903 return NL_SKIP;
7904 }
7905
7906
7907 static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7908 {
7909 struct i802_bss *bss = priv;
7910 struct wpa_driver_nl80211_data *drv = bss->drv;
7911 struct nl_msg *msg;
7912 int err;
7913 union wpa_event_data data;
7914 struct survey_results *survey_results;
7915
7916 os_memset(&data, 0, sizeof(data));
7917 survey_results = &data.survey_results;
7918
7919 dl_list_init(&survey_results->survey_list);
7920
7921 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
7922 if (!msg)
7923 return -ENOBUFS;
7924
7925 if (freq)
7926 data.survey_results.freq_filter = freq;
7927
7928 do {
7929 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7930 err = send_and_recv_msgs(drv, msg, survey_handler,
7931 survey_results);
7932 } while (err > 0);
7933
7934 if (err)
7935 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
7936 else
7937 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
7938
7939 clean_survey_results(survey_results);
7940 return err;
7941 }
7942
7943
7944 static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7945 const u8 *kck, size_t kck_len,
7946 const u8 *replay_ctr)
7947 {
7948 struct i802_bss *bss = priv;
7949 struct wpa_driver_nl80211_data *drv = bss->drv;
7950 struct nlattr *replay_nested;
7951 struct nl_msg *msg;
7952 int ret;
7953
7954 if (!drv->set_rekey_offload)
7955 return;
7956
7957 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
7958 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7959 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
7960 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7961 (kck_len && nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck)) ||
7962 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7963 replay_ctr)) {
7964 nl80211_nlmsg_clear(msg);
7965 nlmsg_free(msg);
7966 return;
7967 }
7968
7969 nla_nest_end(msg, replay_nested);
7970
7971 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7972 if (ret == -EOPNOTSUPP) {
7973 wpa_printf(MSG_DEBUG,
7974 "nl80211: Driver does not support rekey offload");
7975 drv->set_rekey_offload = 0;
7976 }
7977 }
7978
7979
7980 static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7981 const u8 *addr, int qos)
7982 {
7983 /* send data frame to poll STA and check whether
7984 * this frame is ACKed */
7985 struct {
7986 struct ieee80211_hdr hdr;
7987 u16 qos_ctl;
7988 } STRUCT_PACKED nulldata;
7989 size_t size;
7990
7991 /* Send data frame to poll STA and check whether this frame is ACKed */
7992
7993 os_memset(&nulldata, 0, sizeof(nulldata));
7994
7995 if (qos) {
7996 nulldata.hdr.frame_control =
7997 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7998 WLAN_FC_STYPE_QOS_NULL);
7999 size = sizeof(nulldata);
8000 } else {
8001 nulldata.hdr.frame_control =
8002 IEEE80211_FC(WLAN_FC_TYPE_DATA,
8003 WLAN_FC_STYPE_NULLFUNC);
8004 size = sizeof(struct ieee80211_hdr);
8005 }
8006
8007 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8008 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8009 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8010 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8011
8012 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
8013 0, 0, NULL, 0) < 0)
8014 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8015 "send poll frame");
8016 }
8017
8018 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8019 int qos)
8020 {
8021 struct i802_bss *bss = priv;
8022 struct wpa_driver_nl80211_data *drv = bss->drv;
8023 struct nl_msg *msg;
8024 int ret;
8025
8026 if (!drv->poll_command_supported) {
8027 nl80211_send_null_frame(bss, own_addr, addr, qos);
8028 return;
8029 }
8030
8031 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
8032 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8033 nlmsg_free(msg);
8034 return;
8035 }
8036
8037 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8038 if (ret < 0) {
8039 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
8040 MACSTR " failed: ret=%d (%s)",
8041 MAC2STR(addr), ret, strerror(-ret));
8042 }
8043 }
8044
8045
8046 static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8047 {
8048 struct nl_msg *msg;
8049
8050 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
8051 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
8052 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
8053 nlmsg_free(msg);
8054 return -ENOBUFS;
8055 }
8056 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8057 }
8058
8059
8060 static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8061 int ctwindow)
8062 {
8063 struct i802_bss *bss = priv;
8064
8065 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8066 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8067
8068 if (opp_ps != -1 || ctwindow != -1) {
8069 #ifdef ANDROID_P2P
8070 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
8071 #else /* ANDROID_P2P */
8072 return -1; /* Not yet supported */
8073 #endif /* ANDROID_P2P */
8074 }
8075
8076 if (legacy_ps == -1)
8077 return 0;
8078 if (legacy_ps != 0 && legacy_ps != 1)
8079 return -1; /* Not yet supported */
8080
8081 return nl80211_set_power_save(bss, legacy_ps);
8082 }
8083
8084
8085 static int nl80211_start_radar_detection(void *priv,
8086 struct hostapd_freq_params *freq)
8087 {
8088 struct i802_bss *bss = priv;
8089 struct wpa_driver_nl80211_data *drv = bss->drv;
8090 struct nl_msg *msg;
8091 int ret;
8092
8093 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)",
8094 freq->freq, freq->ht_enabled, freq->vht_enabled,
8095 freq->bandwidth, freq->center_freq1, freq->center_freq2);
8096
8097 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
8098 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
8099 "detection");
8100 return -1;
8101 }
8102
8103 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
8104 nl80211_put_freq_params(msg, freq) < 0) {
8105 nlmsg_free(msg);
8106 return -1;
8107 }
8108
8109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8110 if (ret == 0)
8111 return 0;
8112 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
8113 "%d (%s)", ret, strerror(-ret));
8114 return -1;
8115 }
8116
8117 #ifdef CONFIG_TDLS
8118
8119 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8120 u8 dialog_token, u16 status_code,
8121 u32 peer_capab, int initiator, const u8 *buf,
8122 size_t len)
8123 {
8124 struct i802_bss *bss = priv;
8125 struct wpa_driver_nl80211_data *drv = bss->drv;
8126 struct nl_msg *msg;
8127
8128 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8129 return -EOPNOTSUPP;
8130
8131 if (!dst)
8132 return -EINVAL;
8133
8134 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
8135 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
8136 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
8137 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
8138 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
8139 goto fail;
8140 if (peer_capab) {
8141 /*
8142 * The internal enum tdls_peer_capability definition is
8143 * currently identical with the nl80211 enum
8144 * nl80211_tdls_peer_capability, so no conversion is needed
8145 * here.
8146 */
8147 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
8148 peer_capab))
8149 goto fail;
8150 }
8151 if ((initiator &&
8152 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
8153 nla_put(msg, NL80211_ATTR_IE, len, buf))
8154 goto fail;
8155
8156 return send_and_recv_msgs(drv, msg, NULL, NULL);
8157
8158 fail:
8159 nlmsg_free(msg);
8160 return -ENOBUFS;
8161 }
8162
8163
8164 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8165 {
8166 struct i802_bss *bss = priv;
8167 struct wpa_driver_nl80211_data *drv = bss->drv;
8168 struct nl_msg *msg;
8169 enum nl80211_tdls_operation nl80211_oper;
8170 int res;
8171
8172 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8173 return -EOPNOTSUPP;
8174
8175 switch (oper) {
8176 case TDLS_DISCOVERY_REQ:
8177 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8178 break;
8179 case TDLS_SETUP:
8180 nl80211_oper = NL80211_TDLS_SETUP;
8181 break;
8182 case TDLS_TEARDOWN:
8183 nl80211_oper = NL80211_TDLS_TEARDOWN;
8184 break;
8185 case TDLS_ENABLE_LINK:
8186 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8187 break;
8188 case TDLS_DISABLE_LINK:
8189 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8190 break;
8191 case TDLS_ENABLE:
8192 return 0;
8193 case TDLS_DISABLE:
8194 return 0;
8195 default:
8196 return -EINVAL;
8197 }
8198
8199 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
8200 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
8201 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
8202 nlmsg_free(msg);
8203 return -ENOBUFS;
8204 }
8205
8206 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8207 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
8208 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
8209 strerror(-res));
8210 return res;
8211 }
8212
8213
8214 static int
8215 nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
8216 const struct hostapd_freq_params *params)
8217 {
8218 struct i802_bss *bss = priv;
8219 struct wpa_driver_nl80211_data *drv = bss->drv;
8220 struct nl_msg *msg;
8221 int ret = -ENOBUFS;
8222
8223 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8224 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8225 return -EOPNOTSUPP;
8226
8227 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
8228 " oper_class=%u freq=%u",
8229 MAC2STR(addr), oper_class, params->freq);
8230 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
8231 if (!msg ||
8232 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8233 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
8234 (ret = nl80211_put_freq_params(msg, params))) {
8235 nlmsg_free(msg);
8236 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
8237 return ret;
8238 }
8239
8240 return send_and_recv_msgs(drv, msg, NULL, NULL);
8241 }
8242
8243
8244 static int
8245 nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
8246 {
8247 struct i802_bss *bss = priv;
8248 struct wpa_driver_nl80211_data *drv = bss->drv;
8249 struct nl_msg *msg;
8250
8251 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
8252 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
8253 return -EOPNOTSUPP;
8254
8255 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
8256 MAC2STR(addr));
8257 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
8258 if (!msg ||
8259 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8260 nlmsg_free(msg);
8261 wpa_printf(MSG_DEBUG,
8262 "nl80211: Could not build TDLS cancel chan switch");
8263 return -ENOBUFS;
8264 }
8265
8266 return send_and_recv_msgs(drv, msg, NULL, NULL);
8267 }
8268
8269 #endif /* CONFIG TDLS */
8270
8271
8272 static int driver_nl80211_set_key(const char *ifname, void *priv,
8273 enum wpa_alg alg, const u8 *addr,
8274 int key_idx, int set_tx,
8275 const u8 *seq, size_t seq_len,
8276 const u8 *key, size_t key_len)
8277 {
8278 struct i802_bss *bss = priv;
8279 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
8280 set_tx, seq, seq_len, key, key_len);
8281 }
8282
8283
8284 static int driver_nl80211_scan2(void *priv,
8285 struct wpa_driver_scan_params *params)
8286 {
8287 struct i802_bss *bss = priv;
8288 #ifdef CONFIG_DRIVER_NL80211_QCA
8289 struct wpa_driver_nl80211_data *drv = bss->drv;
8290
8291 /*
8292 * Do a vendor specific scan if possible. If only_new_results is
8293 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
8294 * cannot be achieved through a vendor scan. The below condition may
8295 * need to be modified if new scan flags are added in the future whose
8296 * functionality can only be achieved through a normal scan.
8297 */
8298 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
8299 return wpa_driver_nl80211_vendor_scan(bss, params);
8300 #endif /* CONFIG_DRIVER_NL80211_QCA */
8301 return wpa_driver_nl80211_scan(bss, params);
8302 }
8303
8304
8305 static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
8306 int reason_code)
8307 {
8308 struct i802_bss *bss = priv;
8309 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
8310 }
8311
8312
8313 static int driver_nl80211_authenticate(void *priv,
8314 struct wpa_driver_auth_params *params)
8315 {
8316 struct i802_bss *bss = priv;
8317 return wpa_driver_nl80211_authenticate(bss, params);
8318 }
8319
8320
8321 static void driver_nl80211_deinit(void *priv)
8322 {
8323 struct i802_bss *bss = priv;
8324 wpa_driver_nl80211_deinit(bss);
8325 }
8326
8327
8328 static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
8329 const char *ifname)
8330 {
8331 struct i802_bss *bss = priv;
8332 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8333 }
8334
8335
8336 static int driver_nl80211_send_mlme(void *priv, const u8 *data,
8337 size_t data_len, int noack,
8338 unsigned int freq,
8339 const u16 *csa_offs, size_t csa_offs_len)
8340 {
8341 struct i802_bss *bss = priv;
8342 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
8343 freq, 0, 0, 0, csa_offs,
8344 csa_offs_len);
8345 }
8346
8347
8348 static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8349 {
8350 struct i802_bss *bss = priv;
8351 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
8352 }
8353
8354
8355 static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8356 const char *ifname, int vlan_id)
8357 {
8358 struct i802_bss *bss = priv;
8359 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8360 }
8361
8362
8363 static int driver_nl80211_read_sta_data(void *priv,
8364 struct hostap_sta_driver_data *data,
8365 const u8 *addr)
8366 {
8367 struct i802_bss *bss = priv;
8368
8369 os_memset(data, 0, sizeof(*data));
8370 return i802_read_sta_data(bss, data, addr);
8371 }
8372
8373
8374 static int driver_nl80211_send_action(void *priv, unsigned int freq,
8375 unsigned int wait_time,
8376 const u8 *dst, const u8 *src,
8377 const u8 *bssid,
8378 const u8 *data, size_t data_len,
8379 int no_cck)
8380 {
8381 struct i802_bss *bss = priv;
8382 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8383 bssid, data, data_len, no_cck);
8384 }
8385
8386
8387 static int driver_nl80211_probe_req_report(void *priv, int report)
8388 {
8389 struct i802_bss *bss = priv;
8390 return wpa_driver_nl80211_probe_req_report(bss, report);
8391 }
8392
8393
8394 static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8395 const u8 *ies, size_t ies_len)
8396 {
8397 int ret;
8398 struct nl_msg *msg;
8399 struct i802_bss *bss = priv;
8400 struct wpa_driver_nl80211_data *drv = bss->drv;
8401 u16 mdid = WPA_GET_LE16(md);
8402
8403 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
8404 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8405 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8406 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8407 nlmsg_free(msg);
8408 return -ENOBUFS;
8409 }
8410
8411 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8412 if (ret) {
8413 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8414 "err=%d (%s)", ret, strerror(-ret));
8415 }
8416
8417 return ret;
8418 }
8419
8420
8421 static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
8422 {
8423 struct i802_bss *bss = priv;
8424 struct wpa_driver_nl80211_data *drv = bss->drv;
8425
8426 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8427 return NULL;
8428
8429 return bss->addr;
8430 }
8431
8432
8433 static const char * scan_state_str(enum scan_states scan_state)
8434 {
8435 switch (scan_state) {
8436 case NO_SCAN:
8437 return "NO_SCAN";
8438 case SCAN_REQUESTED:
8439 return "SCAN_REQUESTED";
8440 case SCAN_STARTED:
8441 return "SCAN_STARTED";
8442 case SCAN_COMPLETED:
8443 return "SCAN_COMPLETED";
8444 case SCAN_ABORTED:
8445 return "SCAN_ABORTED";
8446 case SCHED_SCAN_STARTED:
8447 return "SCHED_SCAN_STARTED";
8448 case SCHED_SCAN_STOPPED:
8449 return "SCHED_SCAN_STOPPED";
8450 case SCHED_SCAN_RESULTS:
8451 return "SCHED_SCAN_RESULTS";
8452 }
8453
8454 return "??";
8455 }
8456
8457
8458 static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8459 {
8460 struct i802_bss *bss = priv;
8461 struct wpa_driver_nl80211_data *drv = bss->drv;
8462 int res;
8463 char *pos, *end;
8464
8465 pos = buf;
8466 end = buf + buflen;
8467
8468 res = os_snprintf(pos, end - pos,
8469 "ifindex=%d\n"
8470 "ifname=%s\n"
8471 "brname=%s\n"
8472 "addr=" MACSTR "\n"
8473 "freq=%d\n"
8474 "%s%s%s%s%s",
8475 bss->ifindex,
8476 bss->ifname,
8477 bss->brname,
8478 MAC2STR(bss->addr),
8479 bss->freq,
8480 bss->beacon_set ? "beacon_set=1\n" : "",
8481 bss->added_if_into_bridge ?
8482 "added_if_into_bridge=1\n" : "",
8483 bss->added_bridge ? "added_bridge=1\n" : "",
8484 bss->in_deinit ? "in_deinit=1\n" : "",
8485 bss->if_dynamic ? "if_dynamic=1\n" : "");
8486 if (os_snprintf_error(end - pos, res))
8487 return pos - buf;
8488 pos += res;
8489
8490 if (bss->wdev_id_set) {
8491 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8492 (unsigned long long) bss->wdev_id);
8493 if (os_snprintf_error(end - pos, res))
8494 return pos - buf;
8495 pos += res;
8496 }
8497
8498 res = os_snprintf(pos, end - pos,
8499 "phyname=%s\n"
8500 "perm_addr=" MACSTR "\n"
8501 "drv_ifindex=%d\n"
8502 "operstate=%d\n"
8503 "scan_state=%s\n"
8504 "auth_bssid=" MACSTR "\n"
8505 "auth_attempt_bssid=" MACSTR "\n"
8506 "bssid=" MACSTR "\n"
8507 "prev_bssid=" MACSTR "\n"
8508 "associated=%d\n"
8509 "assoc_freq=%u\n"
8510 "monitor_sock=%d\n"
8511 "monitor_ifidx=%d\n"
8512 "monitor_refcount=%d\n"
8513 "last_mgmt_freq=%u\n"
8514 "eapol_tx_sock=%d\n"
8515 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
8516 drv->phyname,
8517 MAC2STR(drv->perm_addr),
8518 drv->ifindex,
8519 drv->operstate,
8520 scan_state_str(drv->scan_state),
8521 MAC2STR(drv->auth_bssid),
8522 MAC2STR(drv->auth_attempt_bssid),
8523 MAC2STR(drv->bssid),
8524 MAC2STR(drv->prev_bssid),
8525 drv->associated,
8526 drv->assoc_freq,
8527 drv->monitor_sock,
8528 drv->monitor_ifidx,
8529 drv->monitor_refcount,
8530 drv->last_mgmt_freq,
8531 drv->eapol_tx_sock,
8532 drv->ignore_if_down_event ?
8533 "ignore_if_down_event=1\n" : "",
8534 drv->scan_complete_events ?
8535 "scan_complete_events=1\n" : "",
8536 drv->disabled_11b_rates ?
8537 "disabled_11b_rates=1\n" : "",
8538 drv->pending_remain_on_chan ?
8539 "pending_remain_on_chan=1\n" : "",
8540 drv->in_interface_list ? "in_interface_list=1\n" : "",
8541 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8542 drv->poll_command_supported ?
8543 "poll_command_supported=1\n" : "",
8544 drv->data_tx_status ? "data_tx_status=1\n" : "",
8545 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8546 drv->retry_auth ? "retry_auth=1\n" : "",
8547 drv->use_monitor ? "use_monitor=1\n" : "",
8548 drv->ignore_next_local_disconnect ?
8549 "ignore_next_local_disconnect=1\n" : "",
8550 drv->ignore_next_local_deauth ?
8551 "ignore_next_local_deauth=1\n" : "");
8552 if (os_snprintf_error(end - pos, res))
8553 return pos - buf;
8554 pos += res;
8555
8556 if (drv->has_capability) {
8557 res = os_snprintf(pos, end - pos,
8558 "capa.key_mgmt=0x%x\n"
8559 "capa.enc=0x%x\n"
8560 "capa.auth=0x%x\n"
8561 "capa.flags=0x%llx\n"
8562 "capa.rrm_flags=0x%x\n"
8563 "capa.max_scan_ssids=%d\n"
8564 "capa.max_sched_scan_ssids=%d\n"
8565 "capa.sched_scan_supported=%d\n"
8566 "capa.max_match_sets=%d\n"
8567 "capa.max_remain_on_chan=%u\n"
8568 "capa.max_stations=%u\n"
8569 "capa.probe_resp_offloads=0x%x\n"
8570 "capa.max_acl_mac_addrs=%u\n"
8571 "capa.num_multichan_concurrent=%u\n"
8572 "capa.mac_addr_rand_sched_scan_supported=%d\n"
8573 "capa.mac_addr_rand_scan_supported=%d\n"
8574 "capa.conc_capab=%u\n"
8575 "capa.max_conc_chan_2_4=%u\n"
8576 "capa.max_conc_chan_5_0=%u\n"
8577 "capa.max_sched_scan_plans=%u\n"
8578 "capa.max_sched_scan_plan_interval=%u\n"
8579 "capa.max_sched_scan_plan_iterations=%u\n",
8580 drv->capa.key_mgmt,
8581 drv->capa.enc,
8582 drv->capa.auth,
8583 (unsigned long long) drv->capa.flags,
8584 drv->capa.rrm_flags,
8585 drv->capa.max_scan_ssids,
8586 drv->capa.max_sched_scan_ssids,
8587 drv->capa.sched_scan_supported,
8588 drv->capa.max_match_sets,
8589 drv->capa.max_remain_on_chan,
8590 drv->capa.max_stations,
8591 drv->capa.probe_resp_offloads,
8592 drv->capa.max_acl_mac_addrs,
8593 drv->capa.num_multichan_concurrent,
8594 drv->capa.mac_addr_rand_sched_scan_supported,
8595 drv->capa.mac_addr_rand_scan_supported,
8596 drv->capa.conc_capab,
8597 drv->capa.max_conc_chan_2_4,
8598 drv->capa.max_conc_chan_5_0,
8599 drv->capa.max_sched_scan_plans,
8600 drv->capa.max_sched_scan_plan_interval,
8601 drv->capa.max_sched_scan_plan_iterations);
8602 if (os_snprintf_error(end - pos, res))
8603 return pos - buf;
8604 pos += res;
8605 }
8606
8607 return pos - buf;
8608 }
8609
8610
8611 static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8612 {
8613 if ((settings->head &&
8614 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8615 settings->head_len, settings->head)) ||
8616 (settings->tail &&
8617 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8618 settings->tail_len, settings->tail)) ||
8619 (settings->beacon_ies &&
8620 nla_put(msg, NL80211_ATTR_IE,
8621 settings->beacon_ies_len, settings->beacon_ies)) ||
8622 (settings->proberesp_ies &&
8623 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8624 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8625 (settings->assocresp_ies &&
8626 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8627 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8628 (settings->probe_resp &&
8629 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8630 settings->probe_resp_len, settings->probe_resp)))
8631 return -ENOBUFS;
8632
8633 return 0;
8634 }
8635
8636
8637 static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8638 {
8639 struct nl_msg *msg;
8640 struct i802_bss *bss = priv;
8641 struct wpa_driver_nl80211_data *drv = bss->drv;
8642 struct nlattr *beacon_csa;
8643 int ret = -ENOBUFS;
8644 int csa_off_len = 0;
8645 int i;
8646
8647 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
8648 settings->cs_count, settings->block_tx,
8649 settings->freq_params.freq, settings->freq_params.bandwidth,
8650 settings->freq_params.center_freq1,
8651 settings->freq_params.center_freq2);
8652
8653 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8654 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8655 return -EOPNOTSUPP;
8656 }
8657
8658 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
8659 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
8660 return -EOPNOTSUPP;
8661
8662 /*
8663 * Remove empty counters, assuming Probe Response and Beacon frame
8664 * counters match. This implementation assumes that there are only two
8665 * counters.
8666 */
8667 if (settings->counter_offset_beacon[0] &&
8668 !settings->counter_offset_beacon[1]) {
8669 csa_off_len = 1;
8670 } else if (settings->counter_offset_beacon[1] &&
8671 !settings->counter_offset_beacon[0]) {
8672 csa_off_len = 1;
8673 settings->counter_offset_beacon[0] =
8674 settings->counter_offset_beacon[1];
8675 settings->counter_offset_presp[0] =
8676 settings->counter_offset_presp[1];
8677 } else if (settings->counter_offset_beacon[1] &&
8678 settings->counter_offset_beacon[0]) {
8679 csa_off_len = 2;
8680 } else {
8681 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8682 return -EINVAL;
8683 }
8684
8685 /* Check CSA counters validity */
8686 if (drv->capa.max_csa_counters &&
8687 csa_off_len > drv->capa.max_csa_counters) {
8688 wpa_printf(MSG_ERROR,
8689 "nl80211: Too many CSA counters provided");
8690 return -EINVAL;
8691 }
8692
8693 if (!settings->beacon_csa.tail)
8694 return -EINVAL;
8695
8696 for (i = 0; i < csa_off_len; i++) {
8697 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8698 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8699
8700 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8701 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8702 settings->cs_count))
8703 return -EINVAL;
8704
8705 if (settings->beacon_csa.probe_resp &&
8706 ((settings->beacon_csa.probe_resp_len <=
8707 csa_c_off_presp) ||
8708 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8709 settings->cs_count)))
8710 return -EINVAL;
8711 }
8712
8713 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8714 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8715 settings->cs_count) ||
8716 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8717 (settings->block_tx &&
8718 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
8719 goto error;
8720
8721 /* beacon_after params */
8722 ret = set_beacon_data(msg, &settings->beacon_after);
8723 if (ret)
8724 goto error;
8725
8726 /* beacon_csa params */
8727 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8728 if (!beacon_csa)
8729 goto fail;
8730
8731 ret = set_beacon_data(msg, &settings->beacon_csa);
8732 if (ret)
8733 goto error;
8734
8735 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8736 csa_off_len * sizeof(u16),
8737 settings->counter_offset_beacon) ||
8738 (settings->beacon_csa.probe_resp &&
8739 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8740 csa_off_len * sizeof(u16),
8741 settings->counter_offset_presp)))
8742 goto fail;
8743
8744 nla_nest_end(msg, beacon_csa);
8745 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8746 if (ret) {
8747 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8748 ret, strerror(-ret));
8749 }
8750 return ret;
8751
8752 fail:
8753 ret = -ENOBUFS;
8754 error:
8755 nlmsg_free(msg);
8756 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8757 return ret;
8758 }
8759
8760
8761 static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8762 u8 user_priority, u16 admitted_time)
8763 {
8764 struct i802_bss *bss = priv;
8765 struct wpa_driver_nl80211_data *drv = bss->drv;
8766 struct nl_msg *msg;
8767 int ret;
8768
8769 wpa_printf(MSG_DEBUG,
8770 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8771 tsid, admitted_time, user_priority);
8772
8773 if (!is_sta_interface(drv->nlmode))
8774 return -ENOTSUP;
8775
8776 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8777 if (!msg ||
8778 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8779 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8780 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8781 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8782 nlmsg_free(msg);
8783 return -ENOBUFS;
8784 }
8785
8786 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8787 if (ret)
8788 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8789 ret, strerror(-ret));
8790 return ret;
8791 }
8792
8793
8794 static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8795 {
8796 struct i802_bss *bss = priv;
8797 struct wpa_driver_nl80211_data *drv = bss->drv;
8798 struct nl_msg *msg;
8799 int ret;
8800
8801 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8802
8803 if (!is_sta_interface(drv->nlmode))
8804 return -ENOTSUP;
8805
8806 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8807 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8808 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8809 nlmsg_free(msg);
8810 return -ENOBUFS;
8811 }
8812
8813 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8814 if (ret)
8815 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8816 ret, strerror(-ret));
8817 return ret;
8818 }
8819
8820
8821 #ifdef CONFIG_TESTING_OPTIONS
8822 static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8823 {
8824 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8825 struct wpabuf *buf = arg;
8826
8827 if (!buf)
8828 return NL_SKIP;
8829
8830 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8831 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8832 return NL_SKIP;
8833 }
8834
8835 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8836 genlmsg_attrlen(gnlh, 0));
8837
8838 return NL_SKIP;
8839 }
8840 #endif /* CONFIG_TESTING_OPTIONS */
8841
8842
8843 static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8844 {
8845 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8846 struct nlattr *nl_vendor_reply, *nl;
8847 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8848 struct wpabuf *buf = arg;
8849 int rem;
8850
8851 if (!buf)
8852 return NL_SKIP;
8853
8854 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8855 genlmsg_attrlen(gnlh, 0), NULL);
8856 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8857
8858 if (!nl_vendor_reply)
8859 return NL_SKIP;
8860
8861 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8862 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8863 return NL_SKIP;
8864 }
8865
8866 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8867 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8868 }
8869
8870 return NL_SKIP;
8871 }
8872
8873
8874 static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8875 unsigned int subcmd, const u8 *data,
8876 size_t data_len, struct wpabuf *buf)
8877 {
8878 struct i802_bss *bss = priv;
8879 struct wpa_driver_nl80211_data *drv = bss->drv;
8880 struct nl_msg *msg;
8881 int ret;
8882
8883 #ifdef CONFIG_TESTING_OPTIONS
8884 if (vendor_id == 0xffffffff) {
8885 msg = nlmsg_alloc();
8886 if (!msg)
8887 return -ENOMEM;
8888
8889 nl80211_cmd(drv, msg, 0, subcmd);
8890 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8891 0)
8892 goto fail;
8893 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8894 if (ret)
8895 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8896 ret);
8897 return ret;
8898 }
8899 #endif /* CONFIG_TESTING_OPTIONS */
8900
8901 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8902 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8903 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8904 (data &&
8905 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8906 goto fail;
8907
8908 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8909 if (ret)
8910 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8911 ret);
8912 return ret;
8913
8914 fail:
8915 nlmsg_free(msg);
8916 return -ENOBUFS;
8917 }
8918
8919
8920 static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8921 u8 qos_map_set_len)
8922 {
8923 struct i802_bss *bss = priv;
8924 struct wpa_driver_nl80211_data *drv = bss->drv;
8925 struct nl_msg *msg;
8926 int ret;
8927
8928 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8929 qos_map_set, qos_map_set_len);
8930
8931 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8932 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8933 nlmsg_free(msg);
8934 return -ENOBUFS;
8935 }
8936
8937 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8938 if (ret)
8939 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8940
8941 return ret;
8942 }
8943
8944
8945 static int nl80211_set_wowlan(void *priv,
8946 const struct wowlan_triggers *triggers)
8947 {
8948 struct i802_bss *bss = priv;
8949 struct wpa_driver_nl80211_data *drv = bss->drv;
8950 struct nl_msg *msg;
8951 struct nlattr *wowlan_triggers;
8952 int ret;
8953
8954 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8955
8956 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
8957 !(wowlan_triggers = nla_nest_start(msg,
8958 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8959 (triggers->any &&
8960 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8961 (triggers->disconnect &&
8962 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8963 (triggers->magic_pkt &&
8964 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8965 (triggers->gtk_rekey_failure &&
8966 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8967 (triggers->eap_identity_req &&
8968 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8969 (triggers->four_way_handshake &&
8970 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8971 (triggers->rfkill_release &&
8972 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8973 nlmsg_free(msg);
8974 return -ENOBUFS;
8975 }
8976
8977 nla_nest_end(msg, wowlan_triggers);
8978
8979 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8980 if (ret)
8981 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8982
8983 return ret;
8984 }
8985
8986
8987 #ifdef CONFIG_DRIVER_NL80211_QCA
8988 static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8989 {
8990 struct i802_bss *bss = priv;
8991 struct wpa_driver_nl80211_data *drv = bss->drv;
8992 struct nl_msg *msg;
8993 struct nlattr *params;
8994
8995 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8996
8997 if (!drv->roaming_vendor_cmd_avail) {
8998 wpa_printf(MSG_DEBUG,
8999 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
9000 return -1;
9001 }
9002
9003 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9004 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9005 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9006 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
9007 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9008 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
9009 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
9010 QCA_ROAMING_NOT_ALLOWED) ||
9011 (bssid &&
9012 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
9013 nlmsg_free(msg);
9014 return -1;
9015 }
9016 nla_nest_end(msg, params);
9017
9018 return send_and_recv_msgs(drv, msg, NULL, NULL);
9019 }
9020
9021
9022 static int nl80211_disable_fils(void *priv, int disable)
9023 {
9024 struct i802_bss *bss = priv;
9025 struct wpa_driver_nl80211_data *drv = bss->drv;
9026 struct nl_msg *msg;
9027 struct nlattr *params;
9028
9029 wpa_printf(MSG_DEBUG, "nl80211: Disable FILS=%d", disable);
9030
9031 if (!drv->set_wifi_conf_vendor_cmd_avail)
9032 return -1;
9033
9034 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9035 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9036 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9037 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION) ||
9038 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9039 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_DISABLE_FILS,
9040 disable)) {
9041 nlmsg_free(msg);
9042 return -1;
9043 }
9044 nla_nest_end(msg, params);
9045
9046 return send_and_recv_msgs(drv, msg, NULL, NULL);
9047 }
9048
9049
9050 /* Reserved QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID value for wpa_supplicant */
9051 #define WPA_SUPPLICANT_CLIENT_ID 1
9052
9053 static int nl80211_set_bssid_blacklist(void *priv, unsigned int num_bssid,
9054 const u8 *bssid)
9055 {
9056 struct i802_bss *bss = priv;
9057 struct wpa_driver_nl80211_data *drv = bss->drv;
9058 struct nl_msg *msg;
9059 struct nlattr *params, *nlbssids, *attr;
9060 unsigned int i;
9061
9062 wpa_printf(MSG_DEBUG, "nl80211: Set blacklist BSSID (num=%u)",
9063 num_bssid);
9064
9065 if (!drv->roam_vendor_cmd_avail)
9066 return -1;
9067
9068 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9069 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9070 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9071 QCA_NL80211_VENDOR_SUBCMD_ROAM) ||
9072 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9073 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
9074 QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BLACKLIST_BSSID) ||
9075 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID,
9076 WPA_SUPPLICANT_CLIENT_ID) ||
9077 nla_put_u32(msg,
9078 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_NUM_BSSID,
9079 num_bssid))
9080 goto fail;
9081
9082 nlbssids = nla_nest_start(
9083 msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS);
9084 if (!nlbssids)
9085 goto fail;
9086
9087 for (i = 0; i < num_bssid; i++) {
9088 attr = nla_nest_start(msg, i);
9089 if (!attr)
9090 goto fail;
9091 if (nla_put(msg,
9092 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PARAMS_BSSID,
9093 ETH_ALEN, &bssid[i * ETH_ALEN]))
9094 goto fail;
9095 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%u]: " MACSTR, i,
9096 MAC2STR(&bssid[i * ETH_ALEN]));
9097 nla_nest_end(msg, attr);
9098 }
9099 nla_nest_end(msg, nlbssids);
9100 nla_nest_end(msg, params);
9101
9102 return send_and_recv_msgs(drv, msg, NULL, NULL);
9103
9104 fail:
9105 nlmsg_free(msg);
9106 return -1;
9107 }
9108
9109 #endif /* CONFIG_DRIVER_NL80211_QCA */
9110
9111
9112 static int nl80211_set_mac_addr(void *priv, const u8 *addr)
9113 {
9114 struct i802_bss *bss = priv;
9115 struct wpa_driver_nl80211_data *drv = bss->drv;
9116 int new_addr = addr != NULL;
9117
9118 if (TEST_FAIL())
9119 return -1;
9120
9121 if (!addr)
9122 addr = drv->perm_addr;
9123
9124 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
9125 return -1;
9126
9127 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
9128 {
9129 wpa_printf(MSG_DEBUG,
9130 "nl80211: failed to set_mac_addr for %s to " MACSTR,
9131 bss->ifname, MAC2STR(addr));
9132 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
9133 1) < 0) {
9134 wpa_printf(MSG_DEBUG,
9135 "nl80211: Could not restore interface UP after failed set_mac_addr");
9136 }
9137 return -1;
9138 }
9139
9140 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
9141 bss->ifname, MAC2STR(addr));
9142 drv->addr_changed = new_addr;
9143 os_memcpy(bss->addr, addr, ETH_ALEN);
9144
9145 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
9146 {
9147 wpa_printf(MSG_DEBUG,
9148 "nl80211: Could not restore interface UP after set_mac_addr");
9149 }
9150
9151 return 0;
9152 }
9153
9154
9155 #ifdef CONFIG_MESH
9156
9157 static int wpa_driver_nl80211_init_mesh(void *priv)
9158 {
9159 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
9160 wpa_printf(MSG_INFO,
9161 "nl80211: Failed to set interface into mesh mode");
9162 return -1;
9163 }
9164 return 0;
9165 }
9166
9167
9168 static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
9169 size_t mesh_id_len)
9170 {
9171 if (mesh_id) {
9172 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
9173 mesh_id, mesh_id_len);
9174 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
9175 }
9176
9177 return 0;
9178 }
9179
9180
9181 static int nl80211_put_mesh_config(struct nl_msg *msg,
9182 struct wpa_driver_mesh_bss_params *params)
9183 {
9184 struct nlattr *container;
9185
9186 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
9187 if (!container)
9188 return -1;
9189
9190 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
9191 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
9192 params->auto_plinks)) ||
9193 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
9194 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
9195 params->max_peer_links)) ||
9196 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_RSSI_THRESHOLD) &&
9197 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
9198 params->rssi_threshold)))
9199 return -1;
9200
9201 /*
9202 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
9203 * the timer could disconnect stations even in that case.
9204 */
9205 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
9206 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
9207 params->peer_link_timeout)) {
9208 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
9209 return -1;
9210 }
9211
9212 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
9213 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
9214 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
9215 return -1;
9216 }
9217
9218 nla_nest_end(msg, container);
9219
9220 return 0;
9221 }
9222
9223
9224 static int nl80211_join_mesh(struct i802_bss *bss,
9225 struct wpa_driver_mesh_join_params *params)
9226 {
9227 struct wpa_driver_nl80211_data *drv = bss->drv;
9228 struct nl_msg *msg;
9229 struct nlattr *container;
9230 int ret = -1;
9231
9232 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9233 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
9234 if (!msg ||
9235 nl80211_put_freq_params(msg, &params->freq) ||
9236 nl80211_put_basic_rates(msg, params->basic_rates) ||
9237 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
9238 nl80211_put_beacon_int(msg, params->beacon_int) ||
9239 nl80211_put_dtim_period(msg, params->dtim_period))
9240 goto fail;
9241
9242 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
9243
9244 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
9245 if (!container)
9246 goto fail;
9247
9248 if (params->ies) {
9249 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
9250 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
9251 params->ies))
9252 goto fail;
9253 }
9254 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
9255 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
9256 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
9257 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
9258 goto fail;
9259 }
9260 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
9261 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
9262 goto fail;
9263 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
9264 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
9265 goto fail;
9266 nla_nest_end(msg, container);
9267
9268 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
9269 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
9270 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
9271 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
9272 goto fail;
9273
9274 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9275 msg = NULL;
9276 if (ret) {
9277 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
9278 ret, strerror(-ret));
9279 goto fail;
9280 }
9281 ret = 0;
9282 drv->assoc_freq = bss->freq = params->freq.freq;
9283 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
9284
9285 fail:
9286 nlmsg_free(msg);
9287 return ret;
9288 }
9289
9290
9291 static int
9292 wpa_driver_nl80211_join_mesh(void *priv,
9293 struct wpa_driver_mesh_join_params *params)
9294 {
9295 struct i802_bss *bss = priv;
9296 int ret, timeout;
9297
9298 timeout = params->conf.peer_link_timeout;
9299
9300 /* Disable kernel inactivity timer */
9301 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
9302 params->conf.peer_link_timeout = 0;
9303
9304 ret = nl80211_join_mesh(bss, params);
9305 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
9306 wpa_printf(MSG_DEBUG,
9307 "nl80211: Mesh join retry for peer_link_timeout");
9308 /*
9309 * Old kernel does not support setting
9310 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
9311 * into future from peer_link_timeout.
9312 */
9313 params->conf.peer_link_timeout = timeout + 60;
9314 ret = nl80211_join_mesh(priv, params);
9315 }
9316
9317 params->conf.peer_link_timeout = timeout;
9318 return ret;
9319 }
9320
9321
9322 static int wpa_driver_nl80211_leave_mesh(void *priv)
9323 {
9324 struct i802_bss *bss = priv;
9325 struct wpa_driver_nl80211_data *drv = bss->drv;
9326 struct nl_msg *msg;
9327 int ret;
9328
9329 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9330 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
9331 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9332 if (ret) {
9333 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
9334 ret, strerror(-ret));
9335 } else {
9336 wpa_printf(MSG_DEBUG,
9337 "nl80211: mesh leave request send successfully");
9338 }
9339
9340 if (wpa_driver_nl80211_set_mode(drv->first_bss,
9341 NL80211_IFTYPE_STATION)) {
9342 wpa_printf(MSG_INFO,
9343 "nl80211: Failed to set interface into station mode");
9344 }
9345 return ret;
9346 }
9347
9348 #endif /* CONFIG_MESH */
9349
9350
9351 static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
9352 const u8 *ipaddr, int prefixlen,
9353 const u8 *addr)
9354 {
9355 #ifdef CONFIG_LIBNL3_ROUTE
9356 struct i802_bss *bss = priv;
9357 struct wpa_driver_nl80211_data *drv = bss->drv;
9358 struct rtnl_neigh *rn;
9359 struct nl_addr *nl_ipaddr = NULL;
9360 struct nl_addr *nl_lladdr = NULL;
9361 int family, addrsize;
9362 int res;
9363
9364 if (!ipaddr || prefixlen == 0 || !addr)
9365 return -EINVAL;
9366
9367 if (bss->br_ifindex == 0) {
9368 wpa_printf(MSG_DEBUG,
9369 "nl80211: bridge must be set before adding an ip neigh to it");
9370 return -1;
9371 }
9372
9373 if (!drv->rtnl_sk) {
9374 wpa_printf(MSG_DEBUG,
9375 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9376 return -1;
9377 }
9378
9379 if (version == 4) {
9380 family = AF_INET;
9381 addrsize = 4;
9382 } else if (version == 6) {
9383 family = AF_INET6;
9384 addrsize = 16;
9385 } else {
9386 return -EINVAL;
9387 }
9388
9389 rn = rtnl_neigh_alloc();
9390 if (rn == NULL)
9391 return -ENOMEM;
9392
9393 /* set the destination ip address for neigh */
9394 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9395 if (nl_ipaddr == NULL) {
9396 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9397 res = -ENOMEM;
9398 goto errout;
9399 }
9400 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
9401 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9402 if (res) {
9403 wpa_printf(MSG_DEBUG,
9404 "nl80211: neigh set destination addr failed");
9405 goto errout;
9406 }
9407
9408 /* set the corresponding lladdr for neigh */
9409 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
9410 if (nl_lladdr == NULL) {
9411 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
9412 res = -ENOMEM;
9413 goto errout;
9414 }
9415 rtnl_neigh_set_lladdr(rn, nl_lladdr);
9416
9417 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9418 rtnl_neigh_set_state(rn, NUD_PERMANENT);
9419
9420 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
9421 if (res) {
9422 wpa_printf(MSG_DEBUG,
9423 "nl80211: Adding bridge ip neigh failed: %s",
9424 strerror(errno));
9425 }
9426 errout:
9427 if (nl_lladdr)
9428 nl_addr_put(nl_lladdr);
9429 if (nl_ipaddr)
9430 nl_addr_put(nl_ipaddr);
9431 if (rn)
9432 rtnl_neigh_put(rn);
9433 return res;
9434 #else /* CONFIG_LIBNL3_ROUTE */
9435 return -1;
9436 #endif /* CONFIG_LIBNL3_ROUTE */
9437 }
9438
9439
9440 static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9441 const u8 *ipaddr)
9442 {
9443 #ifdef CONFIG_LIBNL3_ROUTE
9444 struct i802_bss *bss = priv;
9445 struct wpa_driver_nl80211_data *drv = bss->drv;
9446 struct rtnl_neigh *rn;
9447 struct nl_addr *nl_ipaddr;
9448 int family, addrsize;
9449 int res;
9450
9451 if (!ipaddr)
9452 return -EINVAL;
9453
9454 if (version == 4) {
9455 family = AF_INET;
9456 addrsize = 4;
9457 } else if (version == 6) {
9458 family = AF_INET6;
9459 addrsize = 16;
9460 } else {
9461 return -EINVAL;
9462 }
9463
9464 if (bss->br_ifindex == 0) {
9465 wpa_printf(MSG_DEBUG,
9466 "nl80211: bridge must be set to delete an ip neigh");
9467 return -1;
9468 }
9469
9470 if (!drv->rtnl_sk) {
9471 wpa_printf(MSG_DEBUG,
9472 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9473 return -1;
9474 }
9475
9476 rn = rtnl_neigh_alloc();
9477 if (rn == NULL)
9478 return -ENOMEM;
9479
9480 /* set the destination ip address for neigh */
9481 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9482 if (nl_ipaddr == NULL) {
9483 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9484 res = -ENOMEM;
9485 goto errout;
9486 }
9487 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9488 if (res) {
9489 wpa_printf(MSG_DEBUG,
9490 "nl80211: neigh set destination addr failed");
9491 goto errout;
9492 }
9493
9494 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9495
9496 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9497 if (res) {
9498 wpa_printf(MSG_DEBUG,
9499 "nl80211: Deleting bridge ip neigh failed: %s",
9500 strerror(errno));
9501 }
9502 errout:
9503 if (nl_ipaddr)
9504 nl_addr_put(nl_ipaddr);
9505 if (rn)
9506 rtnl_neigh_put(rn);
9507 return res;
9508 #else /* CONFIG_LIBNL3_ROUTE */
9509 return -1;
9510 #endif /* CONFIG_LIBNL3_ROUTE */
9511 }
9512
9513
9514 static int linux_write_system_file(const char *path, unsigned int val)
9515 {
9516 char buf[50];
9517 int fd, len;
9518
9519 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9520 if (os_snprintf_error(sizeof(buf), len))
9521 return -1;
9522
9523 fd = open(path, O_WRONLY);
9524 if (fd < 0)
9525 return -1;
9526
9527 if (write(fd, buf, len) < 0) {
9528 wpa_printf(MSG_DEBUG,
9529 "nl80211: Failed to write Linux system file: %s with the value of %d",
9530 path, val);
9531 close(fd);
9532 return -1;
9533 }
9534 close(fd);
9535
9536 return 0;
9537 }
9538
9539
9540 static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9541 {
9542 switch (attr) {
9543 case DRV_BR_PORT_ATTR_PROXYARP:
9544 return "proxyarp_wifi";
9545 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9546 return "hairpin_mode";
9547 }
9548
9549 return NULL;
9550 }
9551
9552
9553 static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9554 unsigned int val)
9555 {
9556 struct i802_bss *bss = priv;
9557 char path[128];
9558 const char *attr_txt;
9559
9560 attr_txt = drv_br_port_attr_str(attr);
9561 if (attr_txt == NULL)
9562 return -EINVAL;
9563
9564 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9565 bss->ifname, attr_txt);
9566
9567 if (linux_write_system_file(path, val))
9568 return -1;
9569
9570 return 0;
9571 }
9572
9573
9574 static const char * drv_br_net_param_str(enum drv_br_net_param param)
9575 {
9576 switch (param) {
9577 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9578 return "arp_accept";
9579 default:
9580 return NULL;
9581 }
9582 }
9583
9584
9585 static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9586 unsigned int val)
9587 {
9588 struct i802_bss *bss = priv;
9589 char path[128];
9590 const char *param_txt;
9591 int ip_version = 4;
9592
9593 if (param == DRV_BR_MULTICAST_SNOOPING) {
9594 os_snprintf(path, sizeof(path),
9595 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9596 bss->brname);
9597 goto set_val;
9598 }
9599
9600 param_txt = drv_br_net_param_str(param);
9601 if (param_txt == NULL)
9602 return -EINVAL;
9603
9604 switch (param) {
9605 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9606 ip_version = 4;
9607 break;
9608 default:
9609 return -EINVAL;
9610 }
9611
9612 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9613 ip_version, bss->brname, param_txt);
9614
9615 set_val:
9616 if (linux_write_system_file(path, val))
9617 return -1;
9618
9619 return 0;
9620 }
9621
9622
9623 #ifdef CONFIG_DRIVER_NL80211_QCA
9624
9625 static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9626 {
9627 switch (hw_mode) {
9628 case HOSTAPD_MODE_IEEE80211B:
9629 return QCA_ACS_MODE_IEEE80211B;
9630 case HOSTAPD_MODE_IEEE80211G:
9631 return QCA_ACS_MODE_IEEE80211G;
9632 case HOSTAPD_MODE_IEEE80211A:
9633 return QCA_ACS_MODE_IEEE80211A;
9634 case HOSTAPD_MODE_IEEE80211AD:
9635 return QCA_ACS_MODE_IEEE80211AD;
9636 case HOSTAPD_MODE_IEEE80211ANY:
9637 return QCA_ACS_MODE_IEEE80211ANY;
9638 default:
9639 return -1;
9640 }
9641 }
9642
9643
9644 static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9645 {
9646 int i, len, ret;
9647 u32 *freqs;
9648
9649 if (!freq_list)
9650 return 0;
9651 len = int_array_len(freq_list);
9652 freqs = os_malloc(sizeof(u32) * len);
9653 if (!freqs)
9654 return -1;
9655 for (i = 0; i < len; i++)
9656 freqs[i] = freq_list[i];
9657 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9658 sizeof(u32) * len, freqs);
9659 os_free(freqs);
9660 return ret;
9661 }
9662
9663
9664 static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9665 {
9666 struct i802_bss *bss = priv;
9667 struct wpa_driver_nl80211_data *drv = bss->drv;
9668 struct nl_msg *msg;
9669 struct nlattr *data;
9670 int ret;
9671 int mode;
9672
9673 mode = hw_mode_to_qca_acs(params->hw_mode);
9674 if (mode < 0)
9675 return -1;
9676
9677 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9678 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9679 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9680 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9681 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9682 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9683 (params->ht_enabled &&
9684 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9685 (params->ht40_enabled &&
9686 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9687 (params->vht_enabled &&
9688 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9689 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9690 params->ch_width) ||
9691 (params->ch_list_len &&
9692 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
9693 params->ch_list)) ||
9694 add_acs_freq_list(msg, params->freq_list)) {
9695 nlmsg_free(msg);
9696 return -ENOBUFS;
9697 }
9698 nla_nest_end(msg, data);
9699
9700 wpa_printf(MSG_DEBUG,
9701 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9702 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9703 params->vht_enabled, params->ch_width, params->ch_list_len);
9704
9705 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9706 if (ret) {
9707 wpa_printf(MSG_DEBUG,
9708 "nl80211: Failed to invoke driver ACS function: %s",
9709 strerror(errno));
9710 }
9711 return ret;
9712 }
9713
9714
9715 static int nl80211_set_band(void *priv, enum set_band band)
9716 {
9717 struct i802_bss *bss = priv;
9718 struct wpa_driver_nl80211_data *drv = bss->drv;
9719 struct nl_msg *msg;
9720 struct nlattr *data;
9721 int ret;
9722 enum qca_set_band qca_band;
9723
9724 if (!drv->setband_vendor_cmd_avail)
9725 return -1;
9726
9727 switch (band) {
9728 case WPA_SETBAND_AUTO:
9729 qca_band = QCA_SETBAND_AUTO;
9730 break;
9731 case WPA_SETBAND_5G:
9732 qca_band = QCA_SETBAND_5G;
9733 break;
9734 case WPA_SETBAND_2G:
9735 qca_band = QCA_SETBAND_2G;
9736 break;
9737 default:
9738 return -1;
9739 }
9740
9741 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9742 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9743 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9744 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9745 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9746 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9747 nlmsg_free(msg);
9748 return -ENOBUFS;
9749 }
9750 nla_nest_end(msg, data);
9751
9752 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9753 if (ret) {
9754 wpa_printf(MSG_DEBUG,
9755 "nl80211: Driver setband function failed: %s",
9756 strerror(errno));
9757 }
9758 return ret;
9759 }
9760
9761
9762 struct nl80211_pcl {
9763 unsigned int num;
9764 unsigned int *freq_list;
9765 };
9766
9767 static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9768 {
9769 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9770 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9771 struct nl80211_pcl *param = arg;
9772 struct nlattr *nl_vend, *attr;
9773 enum qca_iface_type iface_type;
9774 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9775 unsigned int num, max_num;
9776 u32 *freqs;
9777
9778 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9779 genlmsg_attrlen(gnlh, 0), NULL);
9780
9781 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9782 if (!nl_vend)
9783 return NL_SKIP;
9784
9785 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9786 nla_data(nl_vend), nla_len(nl_vend), NULL);
9787
9788 attr = tb_vendor[
9789 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9790 if (!attr) {
9791 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9792 param->num = 0;
9793 return NL_SKIP;
9794 }
9795
9796 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9797 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9798 iface_type);
9799
9800 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9801 if (!attr) {
9802 wpa_printf(MSG_ERROR,
9803 "nl80211: preferred_freq_list couldn't be found");
9804 param->num = 0;
9805 return NL_SKIP;
9806 }
9807
9808 /*
9809 * param->num has the maximum number of entries for which there
9810 * is room in the freq_list provided by the caller.
9811 */
9812 freqs = nla_data(attr);
9813 max_num = nla_len(attr) / sizeof(u32);
9814 if (max_num > param->num)
9815 max_num = param->num;
9816 for (num = 0; num < max_num; num++)
9817 param->freq_list[num] = freqs[num];
9818 param->num = num;
9819
9820 return NL_SKIP;
9821 }
9822
9823
9824 static int nl80211_get_pref_freq_list(void *priv,
9825 enum wpa_driver_if_type if_type,
9826 unsigned int *num,
9827 unsigned int *freq_list)
9828 {
9829 struct i802_bss *bss = priv;
9830 struct wpa_driver_nl80211_data *drv = bss->drv;
9831 struct nl_msg *msg;
9832 int ret;
9833 unsigned int i;
9834 struct nlattr *params;
9835 struct nl80211_pcl param;
9836 enum qca_iface_type iface_type;
9837
9838 if (!drv->get_pref_freq_list)
9839 return -1;
9840
9841 switch (if_type) {
9842 case WPA_IF_STATION:
9843 iface_type = QCA_IFACE_TYPE_STA;
9844 break;
9845 case WPA_IF_AP_BSS:
9846 iface_type = QCA_IFACE_TYPE_AP;
9847 break;
9848 case WPA_IF_P2P_GO:
9849 iface_type = QCA_IFACE_TYPE_P2P_GO;
9850 break;
9851 case WPA_IF_P2P_CLIENT:
9852 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9853 break;
9854 case WPA_IF_IBSS:
9855 iface_type = QCA_IFACE_TYPE_IBSS;
9856 break;
9857 case WPA_IF_TDLS:
9858 iface_type = QCA_IFACE_TYPE_TDLS;
9859 break;
9860 default:
9861 return -1;
9862 }
9863
9864 param.num = *num;
9865 param.freq_list = freq_list;
9866
9867 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9868 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9869 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9870 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9871 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9872 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9873 nla_put_u32(msg,
9874 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9875 iface_type)) {
9876 wpa_printf(MSG_ERROR,
9877 "%s: err in adding vendor_cmd and vendor_data",
9878 __func__);
9879 nlmsg_free(msg);
9880 return -1;
9881 }
9882 nla_nest_end(msg, params);
9883
9884 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9885 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9886 if (ret) {
9887 wpa_printf(MSG_ERROR,
9888 "%s: err in send_and_recv_msgs", __func__);
9889 return ret;
9890 }
9891
9892 *num = param.num;
9893
9894 for (i = 0; i < *num; i++) {
9895 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9896 i, freq_list[i]);
9897 }
9898
9899 return 0;
9900 }
9901
9902
9903 static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9904 {
9905 struct i802_bss *bss = priv;
9906 struct wpa_driver_nl80211_data *drv = bss->drv;
9907 struct nl_msg *msg;
9908 int ret;
9909 struct nlattr *params;
9910
9911 if (!drv->set_prob_oper_freq)
9912 return -1;
9913
9914 wpa_printf(MSG_DEBUG,
9915 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9916 freq, bss->ifindex);
9917
9918 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9919 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9920 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9921 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9922 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9923 nla_put_u32(msg,
9924 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9925 QCA_IFACE_TYPE_P2P_CLIENT) ||
9926 nla_put_u32(msg,
9927 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9928 freq)) {
9929 wpa_printf(MSG_ERROR,
9930 "%s: err in adding vendor_cmd and vendor_data",
9931 __func__);
9932 nlmsg_free(msg);
9933 return -1;
9934 }
9935 nla_nest_end(msg, params);
9936
9937 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9938 msg = NULL;
9939 if (ret) {
9940 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9941 __func__);
9942 return ret;
9943 }
9944 nlmsg_free(msg);
9945 return 0;
9946 }
9947
9948
9949 static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9950 unsigned int period, unsigned int interval,
9951 unsigned int count, const u8 *device_types,
9952 size_t dev_types_len,
9953 const u8 *ies, size_t ies_len)
9954 {
9955 struct i802_bss *bss = priv;
9956 struct wpa_driver_nl80211_data *drv = bss->drv;
9957 struct nl_msg *msg;
9958 struct nlattr *container;
9959 int ret;
9960
9961 wpa_printf(MSG_DEBUG,
9962 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9963 freq, period, interval, count);
9964
9965 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9966 return -1;
9967
9968 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9969 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9970 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9971 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
9972 goto fail;
9973
9974 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9975 if (!container)
9976 goto fail;
9977
9978 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
9979 freq) ||
9980 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
9981 period) ||
9982 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
9983 interval) ||
9984 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
9985 count) ||
9986 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
9987 dev_types_len, device_types) ||
9988 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
9989 ies_len, ies))
9990 goto fail;
9991
9992 nla_nest_end(msg, container);
9993 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9994 msg = NULL;
9995 if (ret) {
9996 wpa_printf(MSG_DEBUG,
9997 "nl80211: Failed to send P2P Listen offload vendor command");
9998 goto fail;
9999 }
10000
10001 return 0;
10002
10003 fail:
10004 nlmsg_free(msg);
10005 return -1;
10006 }
10007
10008
10009 static int nl80211_p2p_lo_stop(void *priv)
10010 {
10011 struct i802_bss *bss = priv;
10012 struct wpa_driver_nl80211_data *drv = bss->drv;
10013 struct nl_msg *msg;
10014
10015 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
10016
10017 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
10018 return -1;
10019
10020 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10021 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10022 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10023 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
10024 nlmsg_free(msg);
10025 return -1;
10026 }
10027
10028 return send_and_recv_msgs(drv, msg, NULL, NULL);
10029 }
10030
10031
10032 static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
10033 {
10034 struct i802_bss *bss = priv;
10035 struct wpa_driver_nl80211_data *drv = bss->drv;
10036 struct nl_msg *msg;
10037 struct nlattr *params;
10038 int ret;
10039 u32 tdls_mode;
10040
10041 wpa_printf(MSG_DEBUG,
10042 "nl80211: Set TDKS mode: tdls_external_control=%d",
10043 tdls_external_control);
10044
10045 if (tdls_external_control == 1)
10046 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
10047 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
10048 else
10049 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
10050
10051 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10052 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10053 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10054 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
10055 goto fail;
10056
10057 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10058 if (!params)
10059 goto fail;
10060
10061 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
10062 tdls_mode))
10063 goto fail;
10064
10065 nla_nest_end(msg, params);
10066
10067 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10068 msg = NULL;
10069 if (ret) {
10070 wpa_printf(MSG_ERROR,
10071 "nl80211: Set TDLS mode failed: ret=%d (%s)",
10072 ret, strerror(-ret));
10073 goto fail;
10074 }
10075 return 0;
10076 fail:
10077 nlmsg_free(msg);
10078 return -1;
10079 }
10080
10081
10082 #ifdef CONFIG_MBO
10083
10084 static enum mbo_transition_reject_reason
10085 nl80211_mbo_reject_reason_mapping(enum qca_wlan_btm_candidate_status status)
10086 {
10087 switch (status) {
10088 case QCA_STATUS_REJECT_EXCESSIVE_FRAME_LOSS_EXPECTED:
10089 return MBO_TRANSITION_REJECT_REASON_FRAME_LOSS;
10090 case QCA_STATUS_REJECT_EXCESSIVE_DELAY_EXPECTED:
10091 return MBO_TRANSITION_REJECT_REASON_DELAY;
10092 case QCA_STATUS_REJECT_INSUFFICIENT_QOS_CAPACITY:
10093 return MBO_TRANSITION_REJECT_REASON_QOS_CAPACITY;
10094 case QCA_STATUS_REJECT_LOW_RSSI:
10095 return MBO_TRANSITION_REJECT_REASON_RSSI;
10096 case QCA_STATUS_REJECT_HIGH_INTERFERENCE:
10097 return MBO_TRANSITION_REJECT_REASON_INTERFERENCE;
10098 case QCA_STATUS_REJECT_UNKNOWN:
10099 default:
10100 return MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
10101 }
10102 }
10103
10104
10105 static void nl80211_parse_btm_candidate_info(struct candidate_list *candidate,
10106 struct nlattr *tb[], int num)
10107 {
10108 enum qca_wlan_btm_candidate_status status;
10109 char buf[50];
10110
10111 os_memcpy(candidate->bssid,
10112 nla_data(tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID]),
10113 ETH_ALEN);
10114
10115 status = nla_get_u32(
10116 tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS]);
10117 candidate->is_accept = status == QCA_STATUS_ACCEPT;
10118 candidate->reject_reason = nl80211_mbo_reject_reason_mapping(status);
10119
10120 if (candidate->is_accept)
10121 os_snprintf(buf, sizeof(buf), "Accepted");
10122 else
10123 os_snprintf(buf, sizeof(buf),
10124 "Rejected, Reject_reason: %d",
10125 candidate->reject_reason);
10126 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR " %s",
10127 num, MAC2STR(candidate->bssid), buf);
10128 }
10129
10130
10131 static int
10132 nl80211_get_bss_transition_status_handler(struct nl_msg *msg, void *arg)
10133 {
10134 struct wpa_bss_candidate_info *info = arg;
10135 struct candidate_list *candidate = info->candidates;
10136 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
10137 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
10138 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1];
10139 static struct nla_policy policy[
10140 QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX + 1] = {
10141 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] = {
10142 .minlen = ETH_ALEN
10143 },
10144 [QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS] = {
10145 .type = NLA_U32,
10146 },
10147 };
10148 struct nlattr *attr;
10149 int rem;
10150 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10151 u8 num;
10152
10153 num = info->num; /* number of candidates sent to driver */
10154 info->num = 0;
10155 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10156 genlmsg_attrlen(gnlh, 0), NULL);
10157
10158 if (!tb_msg[NL80211_ATTR_VENDOR_DATA] ||
10159 nla_parse_nested(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
10160 tb_msg[NL80211_ATTR_VENDOR_DATA], NULL) ||
10161 !tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO])
10162 return NL_SKIP;
10163
10164 wpa_printf(MSG_DEBUG,
10165 "nl80211: WNM Candidate list received from driver");
10166 nla_for_each_nested(attr,
10167 tb_vendor[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO],
10168 rem) {
10169 if (info->num >= num ||
10170 nla_parse_nested(
10171 tb, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_MAX,
10172 attr, policy) ||
10173 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID] ||
10174 !tb[QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_STATUS])
10175 break;
10176
10177 nl80211_parse_btm_candidate_info(candidate, tb, info->num);
10178
10179 candidate++;
10180 info->num++;
10181 }
10182
10183 return NL_SKIP;
10184 }
10185
10186
10187 static struct wpa_bss_candidate_info *
10188 nl80211_get_bss_transition_status(void *priv, struct wpa_bss_trans_info *params)
10189 {
10190 struct i802_bss *bss = priv;
10191 struct wpa_driver_nl80211_data *drv = bss->drv;
10192 struct nl_msg *msg;
10193 struct nlattr *attr, *attr1, *attr2;
10194 struct wpa_bss_candidate_info *info;
10195 u8 i;
10196 int ret;
10197 u8 *pos;
10198
10199 if (!drv->fetch_bss_trans_status)
10200 return NULL;
10201
10202 info = os_zalloc(sizeof(*info));
10203 if (!info)
10204 return NULL;
10205 /* Allocate memory for number of candidates sent to driver */
10206 info->candidates = os_calloc(params->n_candidates,
10207 sizeof(*info->candidates));
10208 if (!info->candidates) {
10209 os_free(info);
10210 return NULL;
10211 }
10212
10213 /* Copy the number of candidates being sent to driver. This is used in
10214 * nl80211_get_bss_transition_status_handler() to limit the number of
10215 * candidates that can be populated in info->candidates and will be
10216 * later overwritten with the actual number of candidates received from
10217 * the driver.
10218 */
10219 info->num = params->n_candidates;
10220
10221 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10222 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10223 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10224 QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS))
10225 goto fail;
10226
10227 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10228 if (!attr)
10229 goto fail;
10230
10231 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_BTM_MBO_TRANSITION_REASON,
10232 params->mbo_transition_reason))
10233 goto fail;
10234
10235 attr1 = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO);
10236 if (!attr1)
10237 goto fail;
10238
10239 wpa_printf(MSG_DEBUG,
10240 "nl80211: WNM Candidate list info sending to driver: mbo_transition_reason: %d n_candidates: %d",
10241 params->mbo_transition_reason, params->n_candidates);
10242 pos = params->bssid;
10243 for (i = 0; i < params->n_candidates; i++) {
10244 wpa_printf(MSG_DEBUG, "nl80211: BSSID[%d]: " MACSTR, i,
10245 MAC2STR(pos));
10246 attr2 = nla_nest_start(msg, i);
10247 if (!attr2 ||
10248 nla_put(msg, QCA_WLAN_VENDOR_ATTR_BTM_CANDIDATE_INFO_BSSID,
10249 ETH_ALEN, pos))
10250 goto fail;
10251 pos += ETH_ALEN;
10252 nla_nest_end(msg, attr2);
10253 }
10254
10255 nla_nest_end(msg, attr1);
10256 nla_nest_end(msg, attr);
10257
10258 ret = send_and_recv_msgs(drv, msg,
10259 nl80211_get_bss_transition_status_handler,
10260 info);
10261 msg = NULL;
10262 if (ret) {
10263 wpa_printf(MSG_ERROR,
10264 "nl80211: WNM Get BSS transition status failed: ret=%d (%s)",
10265 ret, strerror(-ret));
10266 goto fail;
10267 }
10268 return info;
10269
10270 fail:
10271 nlmsg_free(msg);
10272 os_free(info->candidates);
10273 os_free(info);
10274 return NULL;
10275 }
10276
10277
10278 /**
10279 * nl80211_ignore_assoc_disallow - Configure driver to ignore assoc_disallow
10280 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
10281 * @ignore_assoc_disallow: 0 to not ignore, 1 to ignore
10282 * Returns: 0 on success, -1 on failure
10283 */
10284 static int nl80211_ignore_assoc_disallow(void *priv, int ignore_disallow)
10285 {
10286 struct i802_bss *bss = priv;
10287 struct wpa_driver_nl80211_data *drv = bss->drv;
10288 struct nl_msg *msg;
10289 struct nlattr *attr;
10290 int ret = -1;
10291
10292 if (!drv->set_wifi_conf_vendor_cmd_avail)
10293 return -1;
10294
10295 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
10296 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
10297 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
10298 QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION))
10299 goto fail;
10300
10301 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
10302 if (!attr)
10303 goto fail;
10304
10305 wpa_printf(MSG_DEBUG, "nl80211: Set ignore_assoc_disallow %d",
10306 ignore_disallow);
10307 if (nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_CONFIG_IGNORE_ASSOC_DISALLOWED,
10308 ignore_disallow))
10309 goto fail;
10310
10311 nla_nest_end(msg, attr);
10312
10313 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10314 msg = NULL;
10315 if (ret) {
10316 wpa_printf(MSG_ERROR,
10317 "nl80211: Set ignore_assoc_disallow failed: ret=%d (%s)",
10318 ret, strerror(-ret));
10319 goto fail;
10320 }
10321
10322 fail:
10323 nlmsg_free(msg);
10324 return ret;
10325 }
10326
10327 #endif /* CONFIG_MBO */
10328
10329 #endif /* CONFIG_DRIVER_NL80211_QCA */
10330
10331
10332 static int nl80211_write_to_file(const char *name, unsigned int val)
10333 {
10334 int fd, len;
10335 char tmp[128];
10336
10337 fd = open(name, O_RDWR);
10338 if (fd < 0) {
10339 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
10340 name, strerror(errno));
10341 return fd;
10342 }
10343
10344 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
10345 len = write(fd, tmp, len);
10346 if (len < 0)
10347 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
10348 name, strerror(errno));
10349 close(fd);
10350
10351 return 0;
10352 }
10353
10354
10355 static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
10356 {
10357 struct i802_bss *bss = priv;
10358 char path[128];
10359 int ret;
10360
10361 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
10362 filter_flags);
10363
10364 /* Configure filtering of unicast frame encrypted using GTK */
10365 ret = os_snprintf(path, sizeof(path),
10366 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
10367 bss->ifname);
10368 if (os_snprintf_error(sizeof(path), ret))
10369 return -1;
10370
10371 ret = nl80211_write_to_file(path,
10372 !!(filter_flags &
10373 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10374 if (ret) {
10375 wpa_printf(MSG_ERROR,
10376 "nl80211: Failed to set IPv4 unicast in multicast filter");
10377 return ret;
10378 }
10379
10380 os_snprintf(path, sizeof(path),
10381 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
10382 bss->ifname);
10383 ret = nl80211_write_to_file(path,
10384 !!(filter_flags &
10385 WPA_DATA_FRAME_FILTER_FLAG_GTK));
10386
10387 if (ret) {
10388 wpa_printf(MSG_ERROR,
10389 "nl80211: Failed to set IPv6 unicast in multicast filter");
10390 return ret;
10391 }
10392
10393 /* Configure filtering of unicast frame encrypted using GTK */
10394 os_snprintf(path, sizeof(path),
10395 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
10396 bss->ifname);
10397 ret = nl80211_write_to_file(path,
10398 !!(filter_flags &
10399 WPA_DATA_FRAME_FILTER_FLAG_ARP));
10400 if (ret) {
10401 wpa_printf(MSG_ERROR,
10402 "nl80211: Failed set gratuitous ARP filter");
10403 return ret;
10404 }
10405
10406 /* Configure filtering of IPv6 NA frames */
10407 os_snprintf(path, sizeof(path),
10408 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
10409 bss->ifname);
10410 ret = nl80211_write_to_file(path,
10411 !!(filter_flags &
10412 WPA_DATA_FRAME_FILTER_FLAG_NA));
10413 if (ret) {
10414 wpa_printf(MSG_ERROR,
10415 "nl80211: Failed to set unsolicited NA filter");
10416 return ret;
10417 }
10418
10419 return 0;
10420 }
10421
10422
10423 static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
10424 const u8 **ext_capa, const u8 **ext_capa_mask,
10425 unsigned int *ext_capa_len)
10426 {
10427 struct i802_bss *bss = priv;
10428 struct wpa_driver_nl80211_data *drv = bss->drv;
10429 enum nl80211_iftype nlmode;
10430 unsigned int i;
10431
10432 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
10433 return -1;
10434
10435 nlmode = wpa_driver_nl80211_if_type(type);
10436
10437 /* By default, use the per-radio values */
10438 *ext_capa = drv->extended_capa;
10439 *ext_capa_mask = drv->extended_capa_mask;
10440 *ext_capa_len = drv->extended_capa_len;
10441
10442 /* Replace the default value if a per-interface type value exists */
10443 for (i = 0; i < drv->num_iface_ext_capa; i++) {
10444 if (nlmode == drv->iface_ext_capa[i].iftype) {
10445 *ext_capa = drv->iface_ext_capa[i].ext_capa;
10446 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
10447 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
10448 break;
10449 }
10450 }
10451
10452 return 0;
10453 }
10454
10455
10456 static int nl80211_update_connection_params(
10457 void *priv, struct wpa_driver_associate_params *params,
10458 enum wpa_drv_update_connect_params_mask mask)
10459 {
10460 struct i802_bss *bss = priv;
10461 struct wpa_driver_nl80211_data *drv = bss->drv;
10462 struct nl_msg *msg;
10463 int ret = -1;
10464 enum nl80211_auth_type type;
10465
10466 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_CONNECT_PARAMS);
10467 if (!msg)
10468 goto fail;
10469
10470 wpa_printf(MSG_DEBUG, "nl80211: Update connection params (ifindex=%d)",
10471 drv->ifindex);
10472
10473 if ((mask & WPA_DRV_UPDATE_ASSOC_IES) && params->wpa_ie) {
10474 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
10475 params->wpa_ie))
10476 goto fail;
10477 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie,
10478 params->wpa_ie_len);
10479 }
10480
10481 if (mask & WPA_DRV_UPDATE_AUTH_TYPE) {
10482 type = get_nl_auth_type(params->auth_alg);
10483 if (type == NL80211_AUTHTYPE_MAX ||
10484 nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
10485 goto fail;
10486 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
10487 }
10488
10489 if ((mask & WPA_DRV_UPDATE_FILS_ERP_INFO) &&
10490 nl80211_put_fils_connect_params(drv, params, msg))
10491 goto fail;
10492
10493 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10494 msg = NULL;
10495 if (ret)
10496 wpa_dbg(drv->ctx, MSG_DEBUG,
10497 "nl80211: Update connect params command failed: ret=%d (%s)",
10498 ret, strerror(-ret));
10499
10500 fail:
10501 nlmsg_free(msg);
10502 return ret;
10503 }
10504
10505
10506 static int nl80211_send_external_auth_status(void *priv,
10507 struct external_auth *params)
10508 {
10509 struct i802_bss *bss = priv;
10510 struct wpa_driver_nl80211_data *drv = bss->drv;
10511 struct nl_msg *msg = NULL;
10512 int ret = -1;
10513
10514 wpa_dbg(drv->ctx, MSG_DEBUG,
10515 "nl80211: External auth status: %u", params->status);
10516
10517 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_EXTERNAL_AUTH);
10518 if (!msg ||
10519 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, params->status) ||
10520 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
10521 params->ssid) ||
10522 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid))
10523 goto fail;
10524 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10525 msg = NULL;
10526 if (ret) {
10527 wpa_printf(MSG_DEBUG,
10528 "nl80211: External Auth status update failed: ret=%d (%s)",
10529 ret, strerror(-ret));
10530 goto fail;
10531 }
10532 fail:
10533 nlmsg_free(msg);
10534 return ret;
10535 }
10536
10537
10538 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10539 .name = "nl80211",
10540 .desc = "Linux nl80211/cfg80211",
10541 .get_bssid = wpa_driver_nl80211_get_bssid,
10542 .get_ssid = wpa_driver_nl80211_get_ssid,
10543 .set_key = driver_nl80211_set_key,
10544 .scan2 = driver_nl80211_scan2,
10545 .sched_scan = wpa_driver_nl80211_sched_scan,
10546 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
10547 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
10548 .abort_scan = wpa_driver_nl80211_abort_scan,
10549 .deauthenticate = driver_nl80211_deauthenticate,
10550 .authenticate = driver_nl80211_authenticate,
10551 .associate = wpa_driver_nl80211_associate,
10552 .global_init = nl80211_global_init,
10553 .global_deinit = nl80211_global_deinit,
10554 .init2 = wpa_driver_nl80211_init,
10555 .deinit = driver_nl80211_deinit,
10556 .get_capa = wpa_driver_nl80211_get_capa,
10557 .set_operstate = wpa_driver_nl80211_set_operstate,
10558 .set_supp_port = wpa_driver_nl80211_set_supp_port,
10559 .set_country = wpa_driver_nl80211_set_country,
10560 .get_country = wpa_driver_nl80211_get_country,
10561 .set_ap = wpa_driver_nl80211_set_ap,
10562 .set_acl = wpa_driver_nl80211_set_acl,
10563 .if_add = wpa_driver_nl80211_if_add,
10564 .if_remove = driver_nl80211_if_remove,
10565 .send_mlme = driver_nl80211_send_mlme,
10566 .get_hw_feature_data = nl80211_get_hw_feature_data,
10567 .sta_add = wpa_driver_nl80211_sta_add,
10568 .sta_remove = driver_nl80211_sta_remove,
10569 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
10570 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
10571 .hapd_init = i802_init,
10572 .hapd_deinit = i802_deinit,
10573 .set_wds_sta = i802_set_wds_sta,
10574 .get_seqnum = i802_get_seqnum,
10575 .flush = i802_flush,
10576 .get_inact_sec = i802_get_inact_sec,
10577 .sta_clear_stats = i802_sta_clear_stats,
10578 .set_rts = i802_set_rts,
10579 .set_frag = i802_set_frag,
10580 .set_tx_queue_params = i802_set_tx_queue_params,
10581 .set_sta_vlan = driver_nl80211_set_sta_vlan,
10582 .sta_deauth = i802_sta_deauth,
10583 .sta_disassoc = i802_sta_disassoc,
10584 .read_sta_data = driver_nl80211_read_sta_data,
10585 .set_freq = i802_set_freq,
10586 .send_action = driver_nl80211_send_action,
10587 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
10588 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10589 .cancel_remain_on_channel =
10590 wpa_driver_nl80211_cancel_remain_on_channel,
10591 .probe_req_report = driver_nl80211_probe_req_report,
10592 .deinit_ap = wpa_driver_nl80211_deinit_ap,
10593 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
10594 .resume = wpa_driver_nl80211_resume,
10595 .signal_monitor = nl80211_signal_monitor,
10596 .signal_poll = nl80211_signal_poll,
10597 .send_frame = nl80211_send_frame,
10598 .set_param = nl80211_set_param,
10599 .get_radio_name = nl80211_get_radio_name,
10600 .add_pmkid = nl80211_add_pmkid,
10601 .remove_pmkid = nl80211_remove_pmkid,
10602 .flush_pmkid = nl80211_flush_pmkid,
10603 .set_rekey_info = nl80211_set_rekey_info,
10604 .poll_client = nl80211_poll_client,
10605 .set_p2p_powersave = nl80211_set_p2p_powersave,
10606 .start_dfs_cac = nl80211_start_radar_detection,
10607 .stop_ap = wpa_driver_nl80211_stop_ap,
10608 #ifdef CONFIG_TDLS
10609 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10610 .tdls_oper = nl80211_tdls_oper,
10611 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
10612 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
10613 #endif /* CONFIG_TDLS */
10614 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
10615 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
10616 .get_survey = wpa_driver_nl80211_get_survey,
10617 .status = wpa_driver_nl80211_status,
10618 .switch_channel = nl80211_switch_channel,
10619 #ifdef ANDROID_P2P
10620 .set_noa = wpa_driver_set_p2p_noa,
10621 .get_noa = wpa_driver_get_p2p_noa,
10622 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
10623 #endif /* ANDROID_P2P */
10624 #ifdef ANDROID
10625 #ifndef ANDROID_LIB_STUB
10626 .driver_cmd = wpa_driver_nl80211_driver_cmd,
10627 #endif /* !ANDROID_LIB_STUB */
10628 #endif /* ANDROID */
10629 .vendor_cmd = nl80211_vendor_cmd,
10630 .set_qos_map = nl80211_set_qos_map,
10631 .set_wowlan = nl80211_set_wowlan,
10632 .set_mac_addr = nl80211_set_mac_addr,
10633 #ifdef CONFIG_MESH
10634 .init_mesh = wpa_driver_nl80211_init_mesh,
10635 .join_mesh = wpa_driver_nl80211_join_mesh,
10636 .leave_mesh = wpa_driver_nl80211_leave_mesh,
10637 #endif /* CONFIG_MESH */
10638 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
10639 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
10640 .br_port_set_attr = wpa_driver_br_port_set_attr,
10641 .br_set_net_param = wpa_driver_br_set_net_param,
10642 .add_tx_ts = nl80211_add_ts,
10643 .del_tx_ts = nl80211_del_ts,
10644 .get_ifindex = nl80211_get_ifindex,
10645 #ifdef CONFIG_DRIVER_NL80211_QCA
10646 .roaming = nl80211_roaming,
10647 .disable_fils = nl80211_disable_fils,
10648 .do_acs = wpa_driver_do_acs,
10649 .set_band = nl80211_set_band,
10650 .get_pref_freq_list = nl80211_get_pref_freq_list,
10651 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
10652 .p2p_lo_start = nl80211_p2p_lo_start,
10653 .p2p_lo_stop = nl80211_p2p_lo_stop,
10654 .set_default_scan_ies = nl80211_set_default_scan_ies,
10655 .set_tdls_mode = nl80211_set_tdls_mode,
10656 #ifdef CONFIG_MBO
10657 .get_bss_transition_status = nl80211_get_bss_transition_status,
10658 .ignore_assoc_disallow = nl80211_ignore_assoc_disallow,
10659 #endif /* CONFIG_MBO */
10660 .set_bssid_blacklist = nl80211_set_bssid_blacklist,
10661 #endif /* CONFIG_DRIVER_NL80211_QCA */
10662 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
10663 .get_ext_capab = nl80211_get_ext_capab,
10664 .update_connect_params = nl80211_update_connection_params,
10665 .send_external_auth_status = nl80211_send_external_auth_status,
10666 };