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