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