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