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