]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/drivers/driver_nl80211.c
nl80211: Verify that global driver pointer is not NULL
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
1 /*
2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2010, 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 program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Alternatively, this software may be distributed under the terms of BSD
14 * license.
15 *
16 * See README and COPYING for more details.
17 */
18
19 #include "includes.h"
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <net/if.h>
25 #include <netlink/genl/genl.h>
26 #include <netlink/genl/family.h>
27 #include <netlink/genl/ctrl.h>
28 #include <linux/rtnetlink.h>
29 #include <netpacket/packet.h>
30 #include <linux/filter.h>
31 #include "nl80211_copy.h"
32
33 #include "common.h"
34 #include "eloop.h"
35 #include "utils/list.h"
36 #include "common/ieee802_11_defs.h"
37 #include "common/ieee802_11_common.h"
38 #include "l2_packet/l2_packet.h"
39 #include "netlink.h"
40 #include "linux_ioctl.h"
41 #include "radiotap.h"
42 #include "radiotap_iter.h"
43 #include "rfkill.h"
44 #include "driver.h"
45
46 #ifdef CONFIG_LIBNL20
47 /* libnl 2.0 compatibility code */
48 #define nl_handle nl_sock
49 #define nl80211_handle_alloc nl_socket_alloc_cb
50 #define nl80211_handle_destroy nl_socket_free
51 #else
52 /*
53 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
54 * but when you free a socket again it will mess up its bitmap and
55 * and use the wrong number the next time it needs a socket ID.
56 * Therefore, we wrap the handle alloc/destroy and add our own pid
57 * accounting.
58 */
59 static uint32_t port_bitmap[32] = { 0 };
60
61 static struct nl_handle *nl80211_handle_alloc(void *cb)
62 {
63 struct nl_handle *handle;
64 uint32_t pid = getpid() & 0x3FFFFF;
65 int i;
66
67 handle = nl_handle_alloc_cb(cb);
68
69 for (i = 0; i < 1024; i++) {
70 if (port_bitmap[i / 32] & (1 << (i % 32)))
71 continue;
72 port_bitmap[i / 32] |= 1 << (i % 32);
73 pid += i << 22;
74 break;
75 }
76
77 nl_socket_set_local_port(handle, pid);
78
79 return handle;
80 }
81
82 static void nl80211_handle_destroy(struct nl_handle *handle)
83 {
84 uint32_t port = nl_socket_get_local_port(handle);
85
86 port >>= 22;
87 port_bitmap[port / 32] &= ~(1 << (port % 32));
88
89 nl_handle_destroy(handle);
90 }
91
92 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h,
93 struct nl_cache **cache)
94 {
95 struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
96 if (!tmp)
97 return -ENOMEM;
98 *cache = tmp;
99 return 0;
100 }
101 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
102 #endif /* CONFIG_LIBNL20 */
103
104
105 struct nl80211_handles {
106 struct nl_handle *handle;
107 struct nl_cache *cache;
108 };
109
110
111 static int nl_create_handles(struct nl80211_handles *handles, struct nl_cb *cb,
112 const char *dbg)
113 {
114 if (!handles)
115 return -1;
116
117 handles->handle = nl80211_handle_alloc(cb);
118 if (handles->handle == NULL) {
119 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
120 "callbacks (%s)", dbg);
121 return -1;
122 }
123
124 if (genl_connect(handles->handle)) {
125 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
126 "netlink (%s)", dbg);
127 goto err;
128 }
129
130 if (genl_ctrl_alloc_cache(handles->handle, &handles->cache) < 0) {
131 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
132 "netlink cache (%s)", dbg);
133 goto err;
134 }
135
136 return 0;
137 err:
138 nl80211_handle_destroy(handles->handle);
139 return -1;
140 }
141
142
143 static void nl_destroy_handles(struct nl80211_handles *handles)
144 {
145 if (handles->handle == NULL)
146 return;
147 nl_cache_free(handles->cache);
148 nl80211_handle_destroy(handles->handle);
149 handles->handle = NULL;
150 }
151
152
153 #ifndef IFF_LOWER_UP
154 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
155 #endif
156 #ifndef IFF_DORMANT
157 #define IFF_DORMANT 0x20000 /* driver signals dormant */
158 #endif
159
160 #ifndef IF_OPER_DORMANT
161 #define IF_OPER_DORMANT 5
162 #endif
163 #ifndef IF_OPER_UP
164 #define IF_OPER_UP 6
165 #endif
166
167 struct nl80211_global {
168 struct dl_list interfaces;
169 int if_add_ifindex;
170 struct netlink_data *netlink;
171 struct nl_cb *nl_cb;
172 struct nl80211_handles nl;
173 struct genl_family *nl80211;
174 int ioctl_sock; /* socket for ioctl() use */
175 };
176
177 static void nl80211_global_deinit(void *priv);
178 static void wpa_driver_nl80211_deinit(void *priv);
179
180 struct i802_bss {
181 struct wpa_driver_nl80211_data *drv;
182 struct i802_bss *next;
183 int ifindex;
184 char ifname[IFNAMSIZ + 1];
185 char brname[IFNAMSIZ];
186 unsigned int beacon_set:1;
187 unsigned int added_if_into_bridge:1;
188 unsigned int added_bridge:1;
189 };
190
191 struct wpa_driver_nl80211_data {
192 struct nl80211_global *global;
193 struct dl_list list;
194 u8 addr[ETH_ALEN];
195 char phyname[32];
196 void *ctx;
197 int ifindex;
198 int if_removed;
199 int if_disabled;
200 int ignore_if_down_event;
201 struct rfkill_data *rfkill;
202 struct wpa_driver_capa capa;
203 int has_capability;
204
205 int operstate;
206
207 int scan_complete_events;
208
209 struct nl80211_handles nl_event, nl_preq;
210
211 u8 auth_bssid[ETH_ALEN];
212 u8 bssid[ETH_ALEN];
213 int associated;
214 u8 ssid[32];
215 size_t ssid_len;
216 enum nl80211_iftype nlmode;
217 enum nl80211_iftype ap_scan_as_station;
218 unsigned int assoc_freq;
219
220 int monitor_sock;
221 int monitor_ifidx;
222 int no_monitor_iface_capab;
223
224 unsigned int disabled_11b_rates:1;
225 unsigned int pending_remain_on_chan:1;
226 unsigned int in_interface_list:1;
227
228 u64 remain_on_chan_cookie;
229 u64 send_action_cookie;
230
231 unsigned int last_mgmt_freq;
232 unsigned int ap_oper_freq;
233
234 struct wpa_driver_scan_filter *filter_ssids;
235 size_t num_filter_ssids;
236
237 struct i802_bss first_bss;
238
239 #ifdef CONFIG_AP
240 struct l2_packet_data *l2;
241 #endif /* CONFIG_AP */
242
243 #ifdef HOSTAPD
244 int eapol_sock; /* socket for EAPOL frames */
245
246 int default_if_indices[16];
247 int *if_indices;
248 int num_if_indices;
249
250 int last_freq;
251 int last_freq_ht;
252 #endif /* HOSTAPD */
253 };
254
255
256 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
257 void *timeout_ctx);
258 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
259 enum nl80211_iftype nlmode);
260 static int
261 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
262 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
263 const u8 *addr, int cmd, u16 reason_code,
264 int local_state_change);
265 static void nl80211_remove_monitor_interface(
266 struct wpa_driver_nl80211_data *drv);
267 static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
268 unsigned int freq, unsigned int wait,
269 const u8 *buf, size_t buf_len, u64 *cookie,
270 int no_cck);
271 static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
272
273 #ifdef HOSTAPD
274 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
275 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
276 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
277 static int wpa_driver_nl80211_if_remove(void *priv,
278 enum wpa_driver_if_type type,
279 const char *ifname);
280 #else /* HOSTAPD */
281 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
282 {
283 }
284
285 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
286 {
287 }
288
289 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
290 {
291 return 0;
292 }
293 #endif /* HOSTAPD */
294
295 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
296 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
297 int ifindex, int disabled);
298
299 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
300
301
302 static int is_ap_interface(enum nl80211_iftype nlmode)
303 {
304 return (nlmode == NL80211_IFTYPE_AP ||
305 nlmode == NL80211_IFTYPE_P2P_GO);
306 }
307
308
309 static int is_sta_interface(enum nl80211_iftype nlmode)
310 {
311 return (nlmode == NL80211_IFTYPE_STATION ||
312 nlmode == NL80211_IFTYPE_P2P_CLIENT);
313 }
314
315
316 static int is_p2p_interface(enum nl80211_iftype nlmode)
317 {
318 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
319 nlmode == NL80211_IFTYPE_P2P_GO);
320 }
321
322
323 struct nl80211_bss_info_arg {
324 struct wpa_driver_nl80211_data *drv;
325 struct wpa_scan_results *res;
326 unsigned int assoc_freq;
327 u8 assoc_bssid[ETH_ALEN];
328 };
329
330 static int bss_info_handler(struct nl_msg *msg, void *arg);
331
332
333 /* nl80211 code */
334 static int ack_handler(struct nl_msg *msg, void *arg)
335 {
336 int *err = arg;
337 *err = 0;
338 return NL_STOP;
339 }
340
341 static int finish_handler(struct nl_msg *msg, void *arg)
342 {
343 int *ret = arg;
344 *ret = 0;
345 return NL_SKIP;
346 }
347
348 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
349 void *arg)
350 {
351 int *ret = arg;
352 *ret = err->error;
353 return NL_SKIP;
354 }
355
356
357 static int no_seq_check(struct nl_msg *msg, void *arg)
358 {
359 return NL_OK;
360 }
361
362
363 static int send_and_recv(struct wpa_driver_nl80211_data *drv,
364 struct nl_handle *nl_handle, struct nl_msg *msg,
365 int (*valid_handler)(struct nl_msg *, void *),
366 void *valid_data)
367 {
368 struct nl_cb *cb;
369 int err = -ENOMEM;
370
371 cb = nl_cb_clone(drv->global->nl_cb);
372 if (!cb)
373 goto out;
374
375 err = nl_send_auto_complete(nl_handle, msg);
376 if (err < 0)
377 goto out;
378
379 err = 1;
380
381 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
382 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
383 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
384
385 if (valid_handler)
386 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
387 valid_handler, valid_data);
388
389 while (err > 0)
390 nl_recvmsgs(nl_handle, cb);
391 out:
392 nl_cb_put(cb);
393 nlmsg_free(msg);
394 return err;
395 }
396
397
398 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
399 struct nl_msg *msg,
400 int (*valid_handler)(struct nl_msg *, void *),
401 void *valid_data)
402 {
403 return send_and_recv(drv, drv->global->nl.handle, msg, valid_handler,
404 valid_data);
405 }
406
407
408 struct family_data {
409 const char *group;
410 int id;
411 };
412
413
414 static int family_handler(struct nl_msg *msg, void *arg)
415 {
416 struct family_data *res = arg;
417 struct nlattr *tb[CTRL_ATTR_MAX + 1];
418 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
419 struct nlattr *mcgrp;
420 int i;
421
422 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
423 genlmsg_attrlen(gnlh, 0), NULL);
424 if (!tb[CTRL_ATTR_MCAST_GROUPS])
425 return NL_SKIP;
426
427 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
428 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
429 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
430 nla_len(mcgrp), NULL);
431 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
432 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
433 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
434 res->group,
435 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
436 continue;
437 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
438 break;
439 };
440
441 return NL_SKIP;
442 }
443
444
445 static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
446 const char *family, const char *group)
447 {
448 struct nl_msg *msg;
449 int ret = -1;
450 struct family_data res = { group, -ENOENT };
451
452 msg = nlmsg_alloc();
453 if (!msg)
454 return -ENOMEM;
455 genlmsg_put(msg, 0, 0,
456 genl_ctrl_resolve(drv->global->nl.handle, "nlctrl"),
457 0, 0, CTRL_CMD_GETFAMILY, 0);
458 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
459
460 ret = send_and_recv_msgs(drv, msg, family_handler, &res);
461 msg = NULL;
462 if (ret == 0)
463 ret = res.id;
464
465 nla_put_failure:
466 nlmsg_free(msg);
467 return ret;
468 }
469
470
471 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
472 struct nl_msg *msg, int flags, uint8_t cmd)
473 {
474 return genlmsg_put(msg, 0, 0, genl_family_get_id(drv->global->nl80211),
475 0, flags, cmd, 0);
476 }
477
478
479 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
480 {
481 struct i802_bss *bss = priv;
482 struct wpa_driver_nl80211_data *drv = bss->drv;
483 if (!drv->associated)
484 return -1;
485 os_memcpy(bssid, drv->bssid, ETH_ALEN);
486 return 0;
487 }
488
489
490 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
491 {
492 struct i802_bss *bss = priv;
493 struct wpa_driver_nl80211_data *drv = bss->drv;
494 if (!drv->associated)
495 return -1;
496 os_memcpy(ssid, drv->ssid, drv->ssid_len);
497 return drv->ssid_len;
498 }
499
500
501 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
502 char *buf, size_t len, int del)
503 {
504 union wpa_event_data event;
505
506 os_memset(&event, 0, sizeof(event));
507 if (len > sizeof(event.interface_status.ifname))
508 len = sizeof(event.interface_status.ifname) - 1;
509 os_memcpy(event.interface_status.ifname, buf, len);
510 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
511 EVENT_INTERFACE_ADDED;
512
513 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
514 del ? "DEL" : "NEW",
515 event.interface_status.ifname,
516 del ? "removed" : "added");
517
518 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
519 if (del)
520 drv->if_removed = 1;
521 else
522 drv->if_removed = 0;
523 }
524
525 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
526 }
527
528
529 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
530 u8 *buf, size_t len)
531 {
532 int attrlen, rta_len;
533 struct rtattr *attr;
534
535 attrlen = len;
536 attr = (struct rtattr *) buf;
537
538 rta_len = RTA_ALIGN(sizeof(struct rtattr));
539 while (RTA_OK(attr, attrlen)) {
540 if (attr->rta_type == IFLA_IFNAME) {
541 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
542 == 0)
543 return 1;
544 else
545 break;
546 }
547 attr = RTA_NEXT(attr, attrlen);
548 }
549
550 return 0;
551 }
552
553
554 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
555 int ifindex, u8 *buf, size_t len)
556 {
557 if (drv->ifindex == ifindex)
558 return 1;
559
560 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
561 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
562 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
563 "interface");
564 wpa_driver_nl80211_finish_drv_init(drv);
565 return 1;
566 }
567
568 return 0;
569 }
570
571
572 static struct wpa_driver_nl80211_data *
573 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
574 {
575 struct wpa_driver_nl80211_data *drv;
576 dl_list_for_each(drv, &global->interfaces,
577 struct wpa_driver_nl80211_data, list) {
578 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
579 have_ifidx(drv, idx))
580 return drv;
581 }
582 return NULL;
583 }
584
585
586 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
587 struct ifinfomsg *ifi,
588 u8 *buf, size_t len)
589 {
590 struct nl80211_global *global = ctx;
591 struct wpa_driver_nl80211_data *drv;
592 int attrlen, rta_len;
593 struct rtattr *attr;
594 u32 brid = 0;
595
596 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
597 if (!drv) {
598 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
599 "ifindex %d", ifi->ifi_index);
600 return;
601 }
602
603 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
604 "(%s%s%s%s)",
605 drv->operstate, ifi->ifi_flags,
606 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
607 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
608 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
609 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
610
611 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
612 char namebuf[IFNAMSIZ];
613 if (if_indextoname(ifi->ifi_index, namebuf) &&
614 linux_iface_up(drv->global->ioctl_sock,
615 drv->first_bss.ifname) > 0) {
616 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
617 "event since interface %s is up", namebuf);
618 return;
619 }
620 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
621 if (drv->ignore_if_down_event) {
622 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
623 "event generated by mode change");
624 drv->ignore_if_down_event = 0;
625 } else {
626 drv->if_disabled = 1;
627 wpa_supplicant_event(drv->ctx,
628 EVENT_INTERFACE_DISABLED, NULL);
629 }
630 }
631
632 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
633 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
634 drv->if_disabled = 0;
635 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
636 }
637
638 /*
639 * Some drivers send the association event before the operup event--in
640 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
641 * fails. This will hit us when wpa_supplicant does not need to do
642 * IEEE 802.1X authentication
643 */
644 if (drv->operstate == 1 &&
645 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
646 !(ifi->ifi_flags & IFF_RUNNING))
647 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
648 -1, IF_OPER_UP);
649
650 attrlen = len;
651 attr = (struct rtattr *) buf;
652 rta_len = RTA_ALIGN(sizeof(struct rtattr));
653 while (RTA_OK(attr, attrlen)) {
654 if (attr->rta_type == IFLA_IFNAME) {
655 wpa_driver_nl80211_event_link(
656 drv,
657 ((char *) attr) + rta_len,
658 attr->rta_len - rta_len, 0);
659 } else if (attr->rta_type == IFLA_MASTER)
660 brid = nla_get_u32((struct nlattr *) attr);
661 attr = RTA_NEXT(attr, attrlen);
662 }
663
664 if (ifi->ifi_family == AF_BRIDGE && brid) {
665 /* device has been added to bridge */
666 char namebuf[IFNAMSIZ];
667 if_indextoname(brid, namebuf);
668 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
669 brid, namebuf);
670 add_ifidx(drv, brid);
671 }
672 }
673
674
675 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
676 struct ifinfomsg *ifi,
677 u8 *buf, size_t len)
678 {
679 struct nl80211_global *global = ctx;
680 struct wpa_driver_nl80211_data *drv;
681 int attrlen, rta_len;
682 struct rtattr *attr;
683 u32 brid = 0;
684
685 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
686 if (!drv) {
687 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
688 "foreign ifindex %d", ifi->ifi_index);
689 return;
690 }
691
692 attrlen = len;
693 attr = (struct rtattr *) buf;
694
695 rta_len = RTA_ALIGN(sizeof(struct rtattr));
696 while (RTA_OK(attr, attrlen)) {
697 if (attr->rta_type == IFLA_IFNAME) {
698 wpa_driver_nl80211_event_link(
699 drv,
700 ((char *) attr) + rta_len,
701 attr->rta_len - rta_len, 1);
702 } else if (attr->rta_type == IFLA_MASTER)
703 brid = nla_get_u32((struct nlattr *) attr);
704 attr = RTA_NEXT(attr, attrlen);
705 }
706
707 if (ifi->ifi_family == AF_BRIDGE && brid) {
708 /* device has been removed from bridge */
709 char namebuf[IFNAMSIZ];
710 if_indextoname(brid, namebuf);
711 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
712 "%s", brid, namebuf);
713 del_ifidx(drv, brid);
714 }
715 }
716
717
718 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
719 const u8 *frame, size_t len)
720 {
721 const struct ieee80211_mgmt *mgmt;
722 union wpa_event_data event;
723
724 mgmt = (const struct ieee80211_mgmt *) frame;
725 if (len < 24 + sizeof(mgmt->u.auth)) {
726 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
727 "frame");
728 return;
729 }
730
731 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
732 os_memset(&event, 0, sizeof(event));
733 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
734 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
735 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
736 if (len > 24 + sizeof(mgmt->u.auth)) {
737 event.auth.ies = mgmt->u.auth.variable;
738 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
739 }
740
741 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
742 }
743
744
745 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
746 {
747 struct nl_msg *msg;
748 int ret;
749 struct nl80211_bss_info_arg arg;
750
751 os_memset(&arg, 0, sizeof(arg));
752 msg = nlmsg_alloc();
753 if (!msg)
754 goto nla_put_failure;
755
756 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
757 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
758
759 arg.drv = drv;
760 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
761 msg = NULL;
762 if (ret == 0) {
763 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
764 "associated BSS from scan results: %u MHz",
765 arg.assoc_freq);
766 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
767 }
768 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
769 "(%s)", ret, strerror(-ret));
770 nla_put_failure:
771 nlmsg_free(msg);
772 return drv->assoc_freq;
773 }
774
775
776 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
777 const u8 *frame, size_t len)
778 {
779 const struct ieee80211_mgmt *mgmt;
780 union wpa_event_data event;
781 u16 status;
782
783 mgmt = (const struct ieee80211_mgmt *) frame;
784 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
785 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
786 "frame");
787 return;
788 }
789
790 status = le_to_host16(mgmt->u.assoc_resp.status_code);
791 if (status != WLAN_STATUS_SUCCESS) {
792 os_memset(&event, 0, sizeof(event));
793 event.assoc_reject.bssid = mgmt->bssid;
794 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
795 event.assoc_reject.resp_ies =
796 (u8 *) mgmt->u.assoc_resp.variable;
797 event.assoc_reject.resp_ies_len =
798 len - 24 - sizeof(mgmt->u.assoc_resp);
799 }
800 event.assoc_reject.status_code = status;
801
802 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
803 return;
804 }
805
806 drv->associated = 1;
807 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
808
809 os_memset(&event, 0, sizeof(event));
810 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
811 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
812 event.assoc_info.resp_ies_len =
813 len - 24 - sizeof(mgmt->u.assoc_resp);
814 }
815
816 event.assoc_info.freq = drv->assoc_freq;
817
818 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
819 }
820
821
822 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
823 enum nl80211_commands cmd, struct nlattr *status,
824 struct nlattr *addr, struct nlattr *req_ie,
825 struct nlattr *resp_ie)
826 {
827 union wpa_event_data event;
828
829 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
830 /*
831 * Avoid reporting two association events that would confuse
832 * the core code.
833 */
834 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
835 "when using userspace SME", cmd);
836 return;
837 }
838
839 os_memset(&event, 0, sizeof(event));
840 if (cmd == NL80211_CMD_CONNECT &&
841 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
842 if (addr)
843 event.assoc_reject.bssid = nla_data(addr);
844 if (resp_ie) {
845 event.assoc_reject.resp_ies = nla_data(resp_ie);
846 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
847 }
848 event.assoc_reject.status_code = nla_get_u16(status);
849 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
850 return;
851 }
852
853 drv->associated = 1;
854 if (addr)
855 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
856
857 if (req_ie) {
858 event.assoc_info.req_ies = nla_data(req_ie);
859 event.assoc_info.req_ies_len = nla_len(req_ie);
860 }
861 if (resp_ie) {
862 event.assoc_info.resp_ies = nla_data(resp_ie);
863 event.assoc_info.resp_ies_len = nla_len(resp_ie);
864 }
865
866 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
867
868 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
869 }
870
871
872 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
873 struct nlattr *reason, struct nlattr *addr)
874 {
875 union wpa_event_data data;
876
877 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
878 /*
879 * Avoid reporting two disassociation events that could
880 * confuse the core code.
881 */
882 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
883 "event when using userspace SME");
884 return;
885 }
886
887 drv->associated = 0;
888 os_memset(&data, 0, sizeof(data));
889 if (reason)
890 data.disassoc_info.reason_code = nla_get_u16(reason);
891 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
892 }
893
894
895 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
896 enum nl80211_commands cmd, struct nlattr *addr)
897 {
898 union wpa_event_data event;
899 enum wpa_event_type ev;
900
901 if (nla_len(addr) != ETH_ALEN)
902 return;
903
904 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
905 cmd, MAC2STR((u8 *) nla_data(addr)));
906
907 if (cmd == NL80211_CMD_AUTHENTICATE)
908 ev = EVENT_AUTH_TIMED_OUT;
909 else if (cmd == NL80211_CMD_ASSOCIATE)
910 ev = EVENT_ASSOC_TIMED_OUT;
911 else
912 return;
913
914 os_memset(&event, 0, sizeof(event));
915 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
916 wpa_supplicant_event(drv->ctx, ev, &event);
917 }
918
919
920 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
921 struct nlattr *freq, const u8 *frame, size_t len)
922 {
923 const struct ieee80211_mgmt *mgmt;
924 union wpa_event_data event;
925 u16 fc, stype;
926
927 mgmt = (const struct ieee80211_mgmt *) frame;
928 if (len < 24) {
929 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
930 return;
931 }
932
933 fc = le_to_host16(mgmt->frame_control);
934 stype = WLAN_FC_GET_STYPE(fc);
935
936 os_memset(&event, 0, sizeof(event));
937 if (freq) {
938 event.rx_action.freq = nla_get_u32(freq);
939 drv->last_mgmt_freq = event.rx_action.freq;
940 }
941 if (stype == WLAN_FC_STYPE_ACTION) {
942 event.rx_action.da = mgmt->da;
943 event.rx_action.sa = mgmt->sa;
944 event.rx_action.bssid = mgmt->bssid;
945 event.rx_action.category = mgmt->u.action.category;
946 event.rx_action.data = &mgmt->u.action.category + 1;
947 event.rx_action.len = frame + len - event.rx_action.data;
948 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
949 } else {
950 event.rx_mgmt.frame = frame;
951 event.rx_mgmt.frame_len = len;
952 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
953 }
954 }
955
956
957 static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv,
958 struct nlattr *cookie, const u8 *frame,
959 size_t len, struct nlattr *ack)
960 {
961 union wpa_event_data event;
962 const struct ieee80211_hdr *hdr;
963 u16 fc;
964 u64 cookie_val;
965
966 if (!cookie)
967 return;
968
969 cookie_val = nla_get_u64(cookie);
970 wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s "
971 "(ack=%d)",
972 (long long unsigned int) cookie_val,
973 cookie_val == drv->send_action_cookie ?
974 " (match)" : " (unknown)", ack != NULL);
975 if (cookie_val != drv->send_action_cookie)
976 return;
977
978 hdr = (const struct ieee80211_hdr *) frame;
979 fc = le_to_host16(hdr->frame_control);
980
981 os_memset(&event, 0, sizeof(event));
982 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
983 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
984 event.tx_status.dst = hdr->addr1;
985 event.tx_status.data = frame;
986 event.tx_status.data_len = len;
987 event.tx_status.ack = ack != NULL;
988 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
989 }
990
991
992 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
993 enum wpa_event_type type,
994 const u8 *frame, size_t len)
995 {
996 const struct ieee80211_mgmt *mgmt;
997 union wpa_event_data event;
998 const u8 *bssid = NULL;
999 u16 reason_code = 0;
1000
1001 mgmt = (const struct ieee80211_mgmt *) frame;
1002 if (len >= 24) {
1003 bssid = mgmt->bssid;
1004
1005 if (drv->associated != 0 &&
1006 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1007 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1008 /*
1009 * We have presumably received this deauth as a
1010 * response to a clear_state_mismatch() outgoing
1011 * deauth. Don't let it take us offline!
1012 */
1013 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1014 "from Unknown BSSID " MACSTR " -- ignoring",
1015 MAC2STR(bssid));
1016 return;
1017 }
1018 }
1019
1020 drv->associated = 0;
1021 os_memset(&event, 0, sizeof(event));
1022
1023 /* Note: Same offset for Reason Code in both frame subtypes */
1024 if (len >= 24 + sizeof(mgmt->u.deauth))
1025 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1026
1027 if (type == EVENT_DISASSOC) {
1028 event.disassoc_info.addr = bssid;
1029 event.disassoc_info.reason_code = reason_code;
1030 if (frame + len > mgmt->u.disassoc.variable) {
1031 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1032 event.disassoc_info.ie_len = frame + len -
1033 mgmt->u.disassoc.variable;
1034 }
1035 } else {
1036 event.deauth_info.addr = bssid;
1037 event.deauth_info.reason_code = reason_code;
1038 if (frame + len > mgmt->u.deauth.variable) {
1039 event.deauth_info.ie = mgmt->u.deauth.variable;
1040 event.deauth_info.ie_len = frame + len -
1041 mgmt->u.deauth.variable;
1042 }
1043 }
1044
1045 wpa_supplicant_event(drv->ctx, type, &event);
1046 }
1047
1048
1049 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1050 enum wpa_event_type type,
1051 const u8 *frame, size_t len)
1052 {
1053 const struct ieee80211_mgmt *mgmt;
1054 union wpa_event_data event;
1055 u16 reason_code = 0;
1056
1057 if (len < 24)
1058 return;
1059
1060 mgmt = (const struct ieee80211_mgmt *) frame;
1061
1062 os_memset(&event, 0, sizeof(event));
1063 /* Note: Same offset for Reason Code in both frame subtypes */
1064 if (len >= 24 + sizeof(mgmt->u.deauth))
1065 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1066
1067 if (type == EVENT_UNPROT_DISASSOC) {
1068 event.unprot_disassoc.sa = mgmt->sa;
1069 event.unprot_disassoc.da = mgmt->da;
1070 event.unprot_disassoc.reason_code = reason_code;
1071 } else {
1072 event.unprot_deauth.sa = mgmt->sa;
1073 event.unprot_deauth.da = mgmt->da;
1074 event.unprot_deauth.reason_code = reason_code;
1075 }
1076
1077 wpa_supplicant_event(drv->ctx, type, &event);
1078 }
1079
1080
1081 static void mlme_event(struct wpa_driver_nl80211_data *drv,
1082 enum nl80211_commands cmd, struct nlattr *frame,
1083 struct nlattr *addr, struct nlattr *timed_out,
1084 struct nlattr *freq, struct nlattr *ack,
1085 struct nlattr *cookie)
1086 {
1087 if (timed_out && addr) {
1088 mlme_timeout_event(drv, cmd, addr);
1089 return;
1090 }
1091
1092 if (frame == NULL) {
1093 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1094 "data", cmd);
1095 return;
1096 }
1097
1098 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1099 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1100 nla_data(frame), nla_len(frame));
1101
1102 switch (cmd) {
1103 case NL80211_CMD_AUTHENTICATE:
1104 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1105 break;
1106 case NL80211_CMD_ASSOCIATE:
1107 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1108 break;
1109 case NL80211_CMD_DEAUTHENTICATE:
1110 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1111 nla_data(frame), nla_len(frame));
1112 break;
1113 case NL80211_CMD_DISASSOCIATE:
1114 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1115 nla_data(frame), nla_len(frame));
1116 break;
1117 case NL80211_CMD_FRAME:
1118 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
1119 break;
1120 case NL80211_CMD_FRAME_TX_STATUS:
1121 mlme_event_action_tx_status(drv, cookie, nla_data(frame),
1122 nla_len(frame), ack);
1123 break;
1124 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1125 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1126 nla_data(frame), nla_len(frame));
1127 break;
1128 case NL80211_CMD_UNPROT_DISASSOCIATE:
1129 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1130 nla_data(frame), nla_len(frame));
1131 break;
1132 default:
1133 break;
1134 }
1135 }
1136
1137
1138 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1139 struct nlattr *tb[])
1140 {
1141 union wpa_event_data data;
1142
1143 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1144 os_memset(&data, 0, sizeof(data));
1145 if (tb[NL80211_ATTR_MAC]) {
1146 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1147 nla_data(tb[NL80211_ATTR_MAC]),
1148 nla_len(tb[NL80211_ATTR_MAC]));
1149 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1150 }
1151 if (tb[NL80211_ATTR_KEY_SEQ]) {
1152 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1153 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1154 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1155 }
1156 if (tb[NL80211_ATTR_KEY_TYPE]) {
1157 enum nl80211_key_type key_type =
1158 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1159 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1160 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1161 data.michael_mic_failure.unicast = 1;
1162 } else
1163 data.michael_mic_failure.unicast = 1;
1164
1165 if (tb[NL80211_ATTR_KEY_IDX]) {
1166 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1167 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1168 }
1169
1170 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1171 }
1172
1173
1174 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1175 struct nlattr *tb[])
1176 {
1177 if (tb[NL80211_ATTR_MAC] == NULL) {
1178 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1179 "event");
1180 return;
1181 }
1182 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1183 drv->associated = 1;
1184 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1185 MAC2STR(drv->bssid));
1186
1187 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1188 }
1189
1190
1191 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1192 int cancel_event, struct nlattr *tb[])
1193 {
1194 unsigned int freq, chan_type, duration;
1195 union wpa_event_data data;
1196 u64 cookie;
1197
1198 if (tb[NL80211_ATTR_WIPHY_FREQ])
1199 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1200 else
1201 freq = 0;
1202
1203 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1204 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1205 else
1206 chan_type = 0;
1207
1208 if (tb[NL80211_ATTR_DURATION])
1209 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1210 else
1211 duration = 0;
1212
1213 if (tb[NL80211_ATTR_COOKIE])
1214 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1215 else
1216 cookie = 0;
1217
1218 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1219 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1220 cancel_event, freq, chan_type, duration,
1221 (long long unsigned int) cookie,
1222 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1223
1224 if (cookie != drv->remain_on_chan_cookie)
1225 return; /* not for us */
1226
1227 if (cancel_event)
1228 drv->pending_remain_on_chan = 0;
1229
1230 os_memset(&data, 0, sizeof(data));
1231 data.remain_on_channel.freq = freq;
1232 data.remain_on_channel.duration = duration;
1233 wpa_supplicant_event(drv->ctx, cancel_event ?
1234 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1235 EVENT_REMAIN_ON_CHANNEL, &data);
1236 }
1237
1238
1239 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1240 struct nlattr *tb[])
1241 {
1242 union wpa_event_data event;
1243 struct nlattr *nl;
1244 int rem;
1245 struct scan_info *info;
1246 #define MAX_REPORT_FREQS 50
1247 int freqs[MAX_REPORT_FREQS];
1248 int num_freqs = 0;
1249
1250 os_memset(&event, 0, sizeof(event));
1251 info = &event.scan_info;
1252 info->aborted = aborted;
1253
1254 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1255 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1256 struct wpa_driver_scan_ssid *s =
1257 &info->ssids[info->num_ssids];
1258 s->ssid = nla_data(nl);
1259 s->ssid_len = nla_len(nl);
1260 info->num_ssids++;
1261 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1262 break;
1263 }
1264 }
1265 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1266 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1267 {
1268 freqs[num_freqs] = nla_get_u32(nl);
1269 num_freqs++;
1270 if (num_freqs == MAX_REPORT_FREQS - 1)
1271 break;
1272 }
1273 info->freqs = freqs;
1274 info->num_freqs = num_freqs;
1275 }
1276 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1277 }
1278
1279
1280 static int get_link_signal(struct nl_msg *msg, void *arg)
1281 {
1282 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1283 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1284 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1285 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1286 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1287 };
1288 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1289 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1290 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1291 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1292 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1293 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1294 };
1295 struct wpa_signal_info *sig_change = arg;
1296
1297 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1298 genlmsg_attrlen(gnlh, 0), NULL);
1299 if (!tb[NL80211_ATTR_STA_INFO] ||
1300 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1301 tb[NL80211_ATTR_STA_INFO], policy))
1302 return NL_SKIP;
1303 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1304 return NL_SKIP;
1305
1306 sig_change->current_signal =
1307 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1308
1309 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1310 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1311 sinfo[NL80211_STA_INFO_TX_BITRATE],
1312 rate_policy)) {
1313 sig_change->current_txrate = 0;
1314 } else {
1315 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1316 sig_change->current_txrate =
1317 nla_get_u16(rinfo[
1318 NL80211_RATE_INFO_BITRATE]) * 100;
1319 }
1320 }
1321 }
1322
1323 return NL_SKIP;
1324 }
1325
1326
1327 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1328 struct wpa_signal_info *sig)
1329 {
1330 struct nl_msg *msg;
1331
1332 sig->current_signal = -9999;
1333 sig->current_txrate = 0;
1334
1335 msg = nlmsg_alloc();
1336 if (!msg)
1337 return -ENOMEM;
1338
1339 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
1340
1341 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1342 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1343
1344 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1345 nla_put_failure:
1346 return -ENOBUFS;
1347 }
1348
1349
1350 static int get_link_noise(struct nl_msg *msg, void *arg)
1351 {
1352 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1353 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1354 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1355 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1356 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1357 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1358 };
1359 struct wpa_signal_info *sig_change = arg;
1360
1361 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1362 genlmsg_attrlen(gnlh, 0), NULL);
1363
1364 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1365 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1366 return NL_SKIP;
1367 }
1368
1369 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1370 tb[NL80211_ATTR_SURVEY_INFO],
1371 survey_policy)) {
1372 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1373 "attributes!");
1374 return NL_SKIP;
1375 }
1376
1377 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1378 return NL_SKIP;
1379
1380 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1381 sig_change->frequency)
1382 return NL_SKIP;
1383
1384 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1385 return NL_SKIP;
1386
1387 sig_change->current_noise =
1388 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1389
1390 return NL_SKIP;
1391 }
1392
1393
1394 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1395 struct wpa_signal_info *sig_change)
1396 {
1397 struct nl_msg *msg;
1398
1399 sig_change->current_noise = 9999;
1400 sig_change->frequency = drv->assoc_freq;
1401
1402 msg = nlmsg_alloc();
1403 if (!msg)
1404 return -ENOMEM;
1405
1406 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1407
1408 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1409
1410 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1411 nla_put_failure:
1412 return -ENOBUFS;
1413 }
1414
1415
1416 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1417 {
1418 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1419 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1420 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1421 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1422 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1423 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1424 };
1425 struct wpa_scan_results *scan_results = arg;
1426 struct wpa_scan_res *scan_res;
1427 size_t i;
1428
1429 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1430 genlmsg_attrlen(gnlh, 0), NULL);
1431
1432 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1433 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1434 return NL_SKIP;
1435 }
1436
1437 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1438 tb[NL80211_ATTR_SURVEY_INFO],
1439 survey_policy)) {
1440 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1441 "attributes");
1442 return NL_SKIP;
1443 }
1444
1445 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1446 return NL_SKIP;
1447
1448 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1449 return NL_SKIP;
1450
1451 for (i = 0; i < scan_results->num; ++i) {
1452 scan_res = scan_results->res[i];
1453 if (!scan_res)
1454 continue;
1455 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1456 scan_res->freq)
1457 continue;
1458 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1459 continue;
1460 scan_res->noise = (s8)
1461 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1462 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1463 }
1464
1465 return NL_SKIP;
1466 }
1467
1468
1469 static int nl80211_get_noise_for_scan_results(
1470 struct wpa_driver_nl80211_data *drv,
1471 struct wpa_scan_results *scan_res)
1472 {
1473 struct nl_msg *msg;
1474
1475 msg = nlmsg_alloc();
1476 if (!msg)
1477 return -ENOMEM;
1478
1479 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1480
1481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1482
1483 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1484 scan_res);
1485 nla_put_failure:
1486 return -ENOBUFS;
1487 }
1488
1489
1490 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1491 struct nlattr *tb[])
1492 {
1493 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1494 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1495 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1496 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1497 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1498 };
1499 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1500 enum nl80211_cqm_rssi_threshold_event event;
1501 union wpa_event_data ed;
1502 struct wpa_signal_info sig;
1503 int res;
1504
1505 if (tb[NL80211_ATTR_CQM] == NULL ||
1506 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1507 cqm_policy)) {
1508 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1509 return;
1510 }
1511
1512 os_memset(&ed, 0, sizeof(ed));
1513
1514 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1515 if (!tb[NL80211_ATTR_MAC])
1516 return;
1517 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1518 ETH_ALEN);
1519 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1520 return;
1521 }
1522
1523 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1524 return;
1525 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1526
1527 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1528 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1529 "event: RSSI high");
1530 ed.signal_change.above_threshold = 1;
1531 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1532 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1533 "event: RSSI low");
1534 ed.signal_change.above_threshold = 0;
1535 } else
1536 return;
1537
1538 res = nl80211_get_link_signal(drv, &sig);
1539 if (res == 0) {
1540 ed.signal_change.current_signal = sig.current_signal;
1541 ed.signal_change.current_txrate = sig.current_txrate;
1542 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1543 sig.current_signal, sig.current_txrate);
1544 }
1545
1546 res = nl80211_get_link_noise(drv, &sig);
1547 if (res == 0) {
1548 ed.signal_change.current_noise = sig.current_noise;
1549 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1550 sig.current_noise);
1551 }
1552
1553 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1554 }
1555
1556
1557 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1558 struct nlattr **tb)
1559 {
1560 u8 *addr;
1561 union wpa_event_data data;
1562
1563 if (tb[NL80211_ATTR_MAC] == NULL)
1564 return;
1565 addr = nla_data(tb[NL80211_ATTR_MAC]);
1566 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
1567
1568 if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) {
1569 u8 *ies = NULL;
1570 size_t ies_len = 0;
1571 if (tb[NL80211_ATTR_IE]) {
1572 ies = nla_data(tb[NL80211_ATTR_IE]);
1573 ies_len = nla_len(tb[NL80211_ATTR_IE]);
1574 }
1575 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1576 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1577 return;
1578 }
1579
1580 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1581 return;
1582
1583 os_memset(&data, 0, sizeof(data));
1584 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1585 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1586 }
1587
1588
1589 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1590 struct nlattr **tb)
1591 {
1592 u8 *addr;
1593 union wpa_event_data data;
1594
1595 if (tb[NL80211_ATTR_MAC] == NULL)
1596 return;
1597 addr = nla_data(tb[NL80211_ATTR_MAC]);
1598 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1599 MAC2STR(addr));
1600
1601 if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) {
1602 drv_event_disassoc(drv->ctx, addr);
1603 return;
1604 }
1605
1606 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1607 return;
1608
1609 os_memset(&data, 0, sizeof(data));
1610 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1611 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
1612 }
1613
1614
1615 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
1616 struct nlattr **tb)
1617 {
1618 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
1619 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
1620 [NL80211_REKEY_DATA_KEK] = {
1621 .minlen = NL80211_KEK_LEN,
1622 .maxlen = NL80211_KEK_LEN,
1623 },
1624 [NL80211_REKEY_DATA_KCK] = {
1625 .minlen = NL80211_KCK_LEN,
1626 .maxlen = NL80211_KCK_LEN,
1627 },
1628 [NL80211_REKEY_DATA_REPLAY_CTR] = {
1629 .minlen = NL80211_REPLAY_CTR_LEN,
1630 .maxlen = NL80211_REPLAY_CTR_LEN,
1631 },
1632 };
1633 union wpa_event_data data;
1634
1635 if (!tb[NL80211_ATTR_MAC])
1636 return;
1637 if (!tb[NL80211_ATTR_REKEY_DATA])
1638 return;
1639 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
1640 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
1641 return;
1642 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
1643 return;
1644
1645 os_memset(&data, 0, sizeof(data));
1646 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
1647 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
1648 MAC2STR(data.driver_gtk_rekey.bssid));
1649 data.driver_gtk_rekey.replay_ctr =
1650 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
1651 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
1652 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
1653 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
1654 }
1655
1656
1657 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
1658 struct nlattr **tb)
1659 {
1660 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
1661 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
1662 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
1663 [NL80211_PMKSA_CANDIDATE_BSSID] = {
1664 .minlen = ETH_ALEN,
1665 .maxlen = ETH_ALEN,
1666 },
1667 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
1668 };
1669 union wpa_event_data data;
1670
1671 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
1672 return;
1673 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
1674 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
1675 return;
1676 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
1677 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
1678 return;
1679
1680 os_memset(&data, 0, sizeof(data));
1681 os_memcpy(data.pmkid_candidate.bssid,
1682 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
1683 data.pmkid_candidate.index =
1684 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
1685 data.pmkid_candidate.preauth =
1686 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
1687 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
1688 }
1689
1690
1691 static int process_event(struct nl_msg *msg, void *arg)
1692 {
1693 struct wpa_driver_nl80211_data *drv = arg;
1694 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1695 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1696
1697 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1698 genlmsg_attrlen(gnlh, 0), NULL);
1699
1700 if (tb[NL80211_ATTR_IFINDEX]) {
1701 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1702 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
1703 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
1704 " for foreign interface (ifindex %d)",
1705 gnlh->cmd, ifindex);
1706 return NL_SKIP;
1707 }
1708 }
1709
1710 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
1711 (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
1712 gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
1713 wpa_driver_nl80211_set_mode(&drv->first_bss,
1714 drv->ap_scan_as_station);
1715 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
1716 }
1717
1718 switch (gnlh->cmd) {
1719 case NL80211_CMD_TRIGGER_SCAN:
1720 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
1721 break;
1722 case NL80211_CMD_START_SCHED_SCAN:
1723 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
1724 break;
1725 case NL80211_CMD_SCHED_SCAN_STOPPED:
1726 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
1727 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
1728 break;
1729 case NL80211_CMD_NEW_SCAN_RESULTS:
1730 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
1731 drv->scan_complete_events = 1;
1732 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
1733 drv->ctx);
1734 send_scan_event(drv, 0, tb);
1735 break;
1736 case NL80211_CMD_SCHED_SCAN_RESULTS:
1737 wpa_printf(MSG_DEBUG,
1738 "nl80211: New sched scan results available");
1739 send_scan_event(drv, 0, tb);
1740 break;
1741 case NL80211_CMD_SCAN_ABORTED:
1742 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
1743 /*
1744 * Need to indicate that scan results are available in order
1745 * not to make wpa_supplicant stop its scanning.
1746 */
1747 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
1748 drv->ctx);
1749 send_scan_event(drv, 1, tb);
1750 break;
1751 case NL80211_CMD_AUTHENTICATE:
1752 case NL80211_CMD_ASSOCIATE:
1753 case NL80211_CMD_DEAUTHENTICATE:
1754 case NL80211_CMD_DISASSOCIATE:
1755 case NL80211_CMD_FRAME:
1756 case NL80211_CMD_FRAME_TX_STATUS:
1757 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1758 case NL80211_CMD_UNPROT_DISASSOCIATE:
1759 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
1760 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
1761 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
1762 tb[NL80211_ATTR_COOKIE]);
1763 break;
1764 case NL80211_CMD_CONNECT:
1765 case NL80211_CMD_ROAM:
1766 mlme_event_connect(drv, gnlh->cmd,
1767 tb[NL80211_ATTR_STATUS_CODE],
1768 tb[NL80211_ATTR_MAC],
1769 tb[NL80211_ATTR_REQ_IE],
1770 tb[NL80211_ATTR_RESP_IE]);
1771 break;
1772 case NL80211_CMD_DISCONNECT:
1773 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
1774 tb[NL80211_ATTR_MAC]);
1775 break;
1776 case NL80211_CMD_MICHAEL_MIC_FAILURE:
1777 mlme_event_michael_mic_failure(drv, tb);
1778 break;
1779 case NL80211_CMD_JOIN_IBSS:
1780 mlme_event_join_ibss(drv, tb);
1781 break;
1782 case NL80211_CMD_REMAIN_ON_CHANNEL:
1783 mlme_event_remain_on_channel(drv, 0, tb);
1784 break;
1785 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
1786 mlme_event_remain_on_channel(drv, 1, tb);
1787 break;
1788 case NL80211_CMD_NOTIFY_CQM:
1789 nl80211_cqm_event(drv, tb);
1790 break;
1791 case NL80211_CMD_REG_CHANGE:
1792 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
1793 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
1794 NULL);
1795 break;
1796 case NL80211_CMD_REG_BEACON_HINT:
1797 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
1798 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
1799 NULL);
1800 break;
1801 case NL80211_CMD_NEW_STATION:
1802 nl80211_new_station_event(drv, tb);
1803 break;
1804 case NL80211_CMD_DEL_STATION:
1805 nl80211_del_station_event(drv, tb);
1806 break;
1807 case NL80211_CMD_SET_REKEY_OFFLOAD:
1808 nl80211_rekey_offload_event(drv, tb);
1809 break;
1810 case NL80211_CMD_PMKSA_CANDIDATE:
1811 nl80211_pmksa_candidate_event(drv, tb);
1812 break;
1813 default:
1814 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
1815 "(cmd=%d)", gnlh->cmd);
1816 break;
1817 }
1818
1819 return NL_SKIP;
1820 }
1821
1822
1823 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1824 void *handle)
1825 {
1826 struct nl_cb *cb;
1827 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1828
1829 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
1830
1831 cb = nl_cb_clone(drv->global->nl_cb);
1832 if (!cb)
1833 return;
1834 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
1835 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
1836 nl_recvmsgs(handle, cb);
1837 nl_cb_put(cb);
1838 }
1839
1840
1841 /**
1842 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1843 * @priv: driver_nl80211 private data
1844 * @alpha2_arg: country to which to switch to
1845 * Returns: 0 on success, -1 on failure
1846 *
1847 * This asks nl80211 to set the regulatory domain for given
1848 * country ISO / IEC alpha2.
1849 */
1850 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1851 {
1852 struct i802_bss *bss = priv;
1853 struct wpa_driver_nl80211_data *drv = bss->drv;
1854 char alpha2[3];
1855 struct nl_msg *msg;
1856
1857 msg = nlmsg_alloc();
1858 if (!msg)
1859 return -ENOMEM;
1860
1861 alpha2[0] = alpha2_arg[0];
1862 alpha2[1] = alpha2_arg[1];
1863 alpha2[2] = '\0';
1864
1865 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
1866
1867 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1868 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1869 return -EINVAL;
1870 return 0;
1871 nla_put_failure:
1872 return -EINVAL;
1873 }
1874
1875
1876 struct wiphy_info_data {
1877 struct wpa_driver_capa *capa;
1878
1879 unsigned int error:1;
1880 };
1881
1882
1883 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
1884 {
1885 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1886 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1887 struct wiphy_info_data *info = arg;
1888 int p2p_go_supported = 0, p2p_client_supported = 0;
1889 int p2p_concurrent = 0;
1890 int auth_supported = 0, connect_supported = 0;
1891 struct wpa_driver_capa *capa = info->capa;
1892 static struct nla_policy
1893 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
1894 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
1895 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
1896 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
1897 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
1898 },
1899 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
1900 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
1901 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
1902 };
1903
1904 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1905 genlmsg_attrlen(gnlh, 0), NULL);
1906
1907 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
1908 capa->max_scan_ssids =
1909 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
1910
1911 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
1912 capa->max_sched_scan_ssids =
1913 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
1914
1915 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
1916 capa->max_match_sets =
1917 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
1918
1919 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
1920 struct nlattr *nl_mode;
1921 int i;
1922 nla_for_each_nested(nl_mode,
1923 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
1924 switch (nla_type(nl_mode)) {
1925 case NL80211_IFTYPE_AP:
1926 capa->flags |= WPA_DRIVER_FLAGS_AP;
1927 break;
1928 case NL80211_IFTYPE_P2P_GO:
1929 p2p_go_supported = 1;
1930 break;
1931 case NL80211_IFTYPE_P2P_CLIENT:
1932 p2p_client_supported = 1;
1933 break;
1934 }
1935 }
1936 }
1937
1938 if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
1939 struct nlattr *nl_combi;
1940 int rem_combi;
1941
1942 nla_for_each_nested(nl_combi,
1943 tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
1944 rem_combi) {
1945 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
1946 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
1947 struct nlattr *nl_limit, *nl_mode;
1948 int err, rem_limit, rem_mode;
1949 int combination_has_p2p = 0, combination_has_mgd = 0;
1950
1951 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
1952 nl_combi,
1953 iface_combination_policy);
1954 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
1955 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
1956 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
1957 goto broken_combination;
1958
1959 nla_for_each_nested(nl_limit,
1960 tb_comb[NL80211_IFACE_COMB_LIMITS],
1961 rem_limit) {
1962 err = nla_parse_nested(tb_limit,
1963 MAX_NL80211_IFACE_LIMIT,
1964 nl_limit,
1965 iface_limit_policy);
1966 if (err ||
1967 !tb_limit[NL80211_IFACE_LIMIT_TYPES])
1968 goto broken_combination;
1969
1970 nla_for_each_nested(
1971 nl_mode,
1972 tb_limit[NL80211_IFACE_LIMIT_TYPES],
1973 rem_mode) {
1974 int ift = nla_type(nl_mode);
1975 if (ift == NL80211_IFTYPE_P2P_GO ||
1976 ift == NL80211_IFTYPE_P2P_CLIENT)
1977 combination_has_p2p = 1;
1978 if (ift == NL80211_IFTYPE_STATION)
1979 combination_has_mgd = 1;
1980 }
1981 if (combination_has_p2p && combination_has_mgd)
1982 break;
1983 }
1984
1985 if (combination_has_p2p && combination_has_mgd) {
1986 p2p_concurrent = 1;
1987 break;
1988 }
1989
1990 broken_combination:
1991 ;
1992 }
1993 }
1994
1995 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
1996 struct nlattr *nl_cmd;
1997 int i;
1998
1999 nla_for_each_nested(nl_cmd,
2000 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
2001 switch (nla_get_u32(nl_cmd)) {
2002 case NL80211_CMD_AUTHENTICATE:
2003 auth_supported = 1;
2004 break;
2005 case NL80211_CMD_CONNECT:
2006 connect_supported = 1;
2007 break;
2008 case NL80211_CMD_START_SCHED_SCAN:
2009 capa->sched_scan_supported = 1;
2010 break;
2011 }
2012 }
2013 }
2014
2015 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2016 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2017 "off-channel TX");
2018 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2019 }
2020
2021 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2022 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2023 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2024 }
2025
2026 /* default to 5000 since early versions of mac80211 don't set it */
2027 capa->max_remain_on_chan = 5000;
2028
2029 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
2030 capa->max_remain_on_chan =
2031 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
2032
2033 if (auth_supported)
2034 capa->flags |= WPA_DRIVER_FLAGS_SME;
2035 else if (!connect_supported) {
2036 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2037 "authentication/association or connect commands");
2038 info->error = 1;
2039 }
2040
2041 if (p2p_go_supported && p2p_client_supported)
2042 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2043 if (p2p_concurrent) {
2044 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2045 "interface (driver advertised support)");
2046 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2047 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2048 }
2049
2050 if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
2051 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2052 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2053
2054 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
2055 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2056 capa->flags |=
2057 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2058 }
2059 }
2060
2061 return NL_SKIP;
2062 }
2063
2064
2065 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2066 struct wiphy_info_data *info)
2067 {
2068 struct nl_msg *msg;
2069
2070 os_memset(info, 0, sizeof(*info));
2071 info->capa = &drv->capa;
2072
2073 msg = nlmsg_alloc();
2074 if (!msg)
2075 return -1;
2076
2077 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
2078
2079 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2080
2081 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
2082 return 0;
2083 msg = NULL;
2084 nla_put_failure:
2085 nlmsg_free(msg);
2086 return -1;
2087 }
2088
2089
2090 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2091 {
2092 struct wiphy_info_data info;
2093 if (wpa_driver_nl80211_get_info(drv, &info))
2094 return -1;
2095
2096 if (info.error)
2097 return -1;
2098
2099 drv->has_capability = 1;
2100 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2101 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2102 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2103 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2104 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2105 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2106 WPA_DRIVER_CAPA_ENC_WEP104 |
2107 WPA_DRIVER_CAPA_ENC_TKIP |
2108 WPA_DRIVER_CAPA_ENC_CCMP;
2109 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2110 WPA_DRIVER_AUTH_SHARED |
2111 WPA_DRIVER_AUTH_LEAP;
2112
2113 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2114 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2115 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2116 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
2117
2118 return 0;
2119 }
2120
2121
2122 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
2123 {
2124 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2125 if (global->nl_cb == NULL) {
2126 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2127 "callbacks");
2128 return -1;
2129 }
2130
2131 if (nl_create_handles(&global->nl, global->nl_cb, "nl"))
2132 return -1;
2133
2134 global->nl80211 = genl_ctrl_search_by_name(global->nl.cache,
2135 "nl80211");
2136 if (global->nl80211 == NULL) {
2137 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2138 "found");
2139 return -1;
2140 }
2141
2142 return 0;
2143 }
2144
2145
2146 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2147 {
2148 struct nl80211_global *global = drv->global;
2149 int ret;
2150
2151 /* Initialize generic netlink and nl80211 */
2152
2153 if (nl_create_handles(&drv->nl_event, global->nl_cb, "event"))
2154 goto err3;
2155
2156 ret = nl_get_multicast_id(drv, "nl80211", "scan");
2157 if (ret >= 0)
2158 ret = nl_socket_add_membership(drv->nl_event.handle, ret);
2159 if (ret < 0) {
2160 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2161 "membership for scan events: %d (%s)",
2162 ret, strerror(-ret));
2163 goto err4;
2164 }
2165
2166 ret = nl_get_multicast_id(drv, "nl80211", "mlme");
2167 if (ret >= 0)
2168 ret = nl_socket_add_membership(drv->nl_event.handle, ret);
2169 if (ret < 0) {
2170 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2171 "membership for mlme events: %d (%s)",
2172 ret, strerror(-ret));
2173 goto err4;
2174 }
2175
2176 ret = nl_get_multicast_id(drv, "nl80211", "regulatory");
2177 if (ret >= 0)
2178 ret = nl_socket_add_membership(drv->nl_event.handle, ret);
2179 if (ret < 0) {
2180 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2181 "membership for regulatory events: %d (%s)",
2182 ret, strerror(-ret));
2183 /* Continue without regulatory events */
2184 }
2185
2186 eloop_register_read_sock(nl_socket_get_fd(drv->nl_event.handle),
2187 wpa_driver_nl80211_event_receive, drv,
2188 drv->nl_event.handle);
2189
2190 return 0;
2191
2192 err4:
2193 nl_destroy_handles(&drv->nl_event);
2194 err3:
2195 return -1;
2196 }
2197
2198
2199 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2200 {
2201 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
2202 /*
2203 * This may be for any interface; use ifdown event to disable
2204 * interface.
2205 */
2206 }
2207
2208
2209 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2210 {
2211 struct wpa_driver_nl80211_data *drv = ctx;
2212 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
2213 if (linux_set_iface_flags(drv->global->ioctl_sock,
2214 drv->first_bss.ifname, 1)) {
2215 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2216 "after rfkill unblock");
2217 return;
2218 }
2219 /* rtnetlink ifup handler will report interface as enabled */
2220 }
2221
2222
2223 static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
2224 {
2225 /* Find phy (radio) to which this interface belongs */
2226 char buf[90], *pos;
2227 int f, rv;
2228
2229 drv->phyname[0] = '\0';
2230 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
2231 drv->first_bss.ifname);
2232 f = open(buf, O_RDONLY);
2233 if (f < 0) {
2234 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
2235 buf, strerror(errno));
2236 return;
2237 }
2238
2239 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
2240 close(f);
2241 if (rv < 0) {
2242 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
2243 buf, strerror(errno));
2244 return;
2245 }
2246
2247 drv->phyname[rv] = '\0';
2248 pos = os_strchr(drv->phyname, '\n');
2249 if (pos)
2250 *pos = '\0';
2251 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2252 drv->first_bss.ifname, drv->phyname);
2253 }
2254
2255
2256 #ifdef CONFIG_AP
2257 static void nl80211_l2_read(void *ctx, const u8 *src_addr, const u8 *buf,
2258 size_t len)
2259 {
2260 wpa_printf(MSG_DEBUG, "nl80211: l2_packet read %u",
2261 (unsigned int) len);
2262 }
2263 #endif /* CONFIG_AP */
2264
2265
2266 /**
2267 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2268 * @ctx: context to be used when calling wpa_supplicant functions,
2269 * e.g., wpa_supplicant_event()
2270 * @ifname: interface name, e.g., wlan0
2271 * @global_priv: private driver global data from global_init()
2272 * Returns: Pointer to private data, %NULL on failure
2273 */
2274 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2275 void *global_priv)
2276 {
2277 struct wpa_driver_nl80211_data *drv;
2278 struct rfkill_config *rcfg;
2279 struct i802_bss *bss;
2280
2281 if (global_priv == NULL)
2282 return NULL;
2283 drv = os_zalloc(sizeof(*drv));
2284 if (drv == NULL)
2285 return NULL;
2286 drv->global = global_priv;
2287 drv->ctx = ctx;
2288 bss = &drv->first_bss;
2289 bss->drv = drv;
2290 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2291 drv->monitor_ifidx = -1;
2292 drv->monitor_sock = -1;
2293 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2294
2295 if (wpa_driver_nl80211_init_nl(drv)) {
2296 os_free(drv);
2297 return NULL;
2298 }
2299
2300 nl80211_get_phy_name(drv);
2301
2302 rcfg = os_zalloc(sizeof(*rcfg));
2303 if (rcfg == NULL)
2304 goto failed;
2305 rcfg->ctx = drv;
2306 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
2307 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2308 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2309 drv->rfkill = rfkill_init(rcfg);
2310 if (drv->rfkill == NULL) {
2311 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2312 os_free(rcfg);
2313 }
2314
2315 if (wpa_driver_nl80211_finish_drv_init(drv))
2316 goto failed;
2317
2318 #ifdef CONFIG_AP
2319 drv->l2 = l2_packet_init(ifname, NULL, ETH_P_EAPOL,
2320 nl80211_l2_read, drv, 0);
2321 #endif /* CONFIG_AP */
2322
2323 if (drv->global) {
2324 dl_list_add(&drv->global->interfaces, &drv->list);
2325 drv->in_interface_list = 1;
2326 }
2327
2328 return bss;
2329
2330 failed:
2331 wpa_driver_nl80211_deinit(bss);
2332 return NULL;
2333 }
2334
2335
2336 static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv,
2337 struct nl_handle *nl_handle,
2338 u16 type, const u8 *match, size_t match_len)
2339 {
2340 struct nl_msg *msg;
2341 int ret = -1;
2342
2343 msg = nlmsg_alloc();
2344 if (!msg)
2345 return -1;
2346
2347 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
2348
2349 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2350 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
2351 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
2352
2353 ret = send_and_recv(drv, nl_handle, msg, NULL, NULL);
2354 msg = NULL;
2355 if (ret) {
2356 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2357 "failed (type=%u): ret=%d (%s)",
2358 type, ret, strerror(-ret));
2359 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2360 match, match_len);
2361 goto nla_put_failure;
2362 }
2363 ret = 0;
2364 nla_put_failure:
2365 nlmsg_free(msg);
2366 return ret;
2367 }
2368
2369
2370 static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
2371 const u8 *match, size_t match_len)
2372 {
2373 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
2374 return nl80211_register_frame(drv, drv->nl_event.handle,
2375 type, match, match_len);
2376 }
2377
2378
2379 static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
2380 {
2381 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
2382 /* GAS Initial Request */
2383 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0)
2384 return -1;
2385 /* GAS Initial Response */
2386 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0)
2387 return -1;
2388 /* GAS Comeback Request */
2389 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0)
2390 return -1;
2391 /* GAS Comeback Response */
2392 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0)
2393 return -1;
2394 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
2395 #ifdef CONFIG_P2P
2396 /* P2P Public Action */
2397 if (nl80211_register_action_frame(drv,
2398 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2399 6) < 0)
2400 return -1;
2401 /* P2P Action */
2402 if (nl80211_register_action_frame(drv,
2403 (u8 *) "\x7f\x50\x6f\x9a\x09",
2404 5) < 0)
2405 return -1;
2406 #endif /* CONFIG_P2P */
2407 #ifdef CONFIG_IEEE80211W
2408 /* SA Query Response */
2409 if (nl80211_register_action_frame(drv, (u8 *) "\x08\x01", 2) < 0)
2410 return -1;
2411 #endif /* CONFIG_IEEE80211W */
2412 #ifdef CONFIG_TDLS
2413 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2414 /* TDLS Discovery Response */
2415 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0e", 2) <
2416 0)
2417 return -1;
2418 }
2419 #endif /* CONFIG_TDLS */
2420
2421 /* FT Action frames */
2422 if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
2423 return -1;
2424 else
2425 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2426 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2427
2428 /* WNM - BSS Transition Management Request */
2429 if (nl80211_register_action_frame(drv, (u8 *) "\x0a\x07", 2) < 0)
2430 return -1;
2431
2432 return 0;
2433 }
2434
2435
2436 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2437 {
2438 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2439 }
2440
2441
2442 static int
2443 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
2444 {
2445 struct i802_bss *bss = &drv->first_bss;
2446 int send_rfkill_event = 0;
2447
2448 drv->ifindex = if_nametoindex(bss->ifname);
2449 drv->first_bss.ifindex = drv->ifindex;
2450
2451 #ifndef HOSTAPD
2452 /*
2453 * Make sure the interface starts up in station mode unless this is a
2454 * dynamically added interface (e.g., P2P) that was already configured
2455 * with proper iftype.
2456 */
2457 if (drv->ifindex != drv->global->if_add_ifindex &&
2458 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
2459 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
2460 "use managed mode");
2461 return -1;
2462 }
2463
2464 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
2465 if (rfkill_is_blocked(drv->rfkill)) {
2466 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2467 "interface '%s' due to rfkill",
2468 bss->ifname);
2469 drv->if_disabled = 1;
2470 send_rfkill_event = 1;
2471 } else {
2472 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2473 "interface '%s' UP", bss->ifname);
2474 return -1;
2475 }
2476 }
2477
2478 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2479 1, IF_OPER_DORMANT);
2480 #endif /* HOSTAPD */
2481
2482 if (wpa_driver_nl80211_capa(drv))
2483 return -1;
2484
2485 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2486 drv->addr))
2487 return -1;
2488
2489 if (nl80211_register_action_frames(drv) < 0) {
2490 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
2491 "frame processing - ignore for now");
2492 /*
2493 * Older kernel versions did not support this, so ignore the
2494 * error for now. Some functionality may not be available
2495 * because of this.
2496 */
2497 }
2498
2499 if (send_rfkill_event) {
2500 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2501 drv, drv->ctx);
2502 }
2503
2504 return 0;
2505 }
2506
2507
2508 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2509 {
2510 struct nl_msg *msg;
2511
2512 msg = nlmsg_alloc();
2513 if (!msg)
2514 return -ENOMEM;
2515
2516 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
2517 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2518
2519 return send_and_recv_msgs(drv, msg, NULL, NULL);
2520 nla_put_failure:
2521 return -ENOBUFS;
2522 }
2523
2524
2525 /**
2526 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
2527 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
2528 *
2529 * Shut down driver interface and processing of driver events. Free
2530 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2531 */
2532 static void wpa_driver_nl80211_deinit(void *priv)
2533 {
2534 struct i802_bss *bss = priv;
2535 struct wpa_driver_nl80211_data *drv = bss->drv;
2536
2537 #ifdef CONFIG_AP
2538 if (drv->l2)
2539 l2_packet_deinit(drv->l2);
2540 #endif /* CONFIG_AP */
2541
2542 if (drv->nl_preq.handle)
2543 wpa_driver_nl80211_probe_req_report(bss, 0);
2544 if (bss->added_if_into_bridge) {
2545 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2546 bss->ifname) < 0)
2547 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2548 "interface %s from bridge %s: %s",
2549 bss->ifname, bss->brname, strerror(errno));
2550 }
2551 if (bss->added_bridge) {
2552 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
2553 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2554 "bridge %s: %s",
2555 bss->brname, strerror(errno));
2556 }
2557
2558 nl80211_remove_monitor_interface(drv);
2559
2560 if (is_ap_interface(drv->nlmode))
2561 wpa_driver_nl80211_del_beacon(drv);
2562
2563 #ifdef HOSTAPD
2564 if (drv->last_freq_ht) {
2565 /* Clear HT flags from the driver */
2566 struct hostapd_freq_params freq;
2567 os_memset(&freq, 0, sizeof(freq));
2568 freq.freq = drv->last_freq;
2569 i802_set_freq(priv, &freq);
2570 }
2571
2572 if (drv->eapol_sock >= 0) {
2573 eloop_unregister_read_sock(drv->eapol_sock);
2574 close(drv->eapol_sock);
2575 }
2576
2577 if (drv->if_indices != drv->default_if_indices)
2578 os_free(drv->if_indices);
2579 #endif /* HOSTAPD */
2580
2581 if (drv->disabled_11b_rates)
2582 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2583
2584 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2585 IF_OPER_UP);
2586 rfkill_deinit(drv->rfkill);
2587
2588 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2589
2590 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
2591 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
2592
2593 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_event.handle));
2594 nl_destroy_handles(&drv->nl_event);
2595
2596 os_free(drv->filter_ssids);
2597
2598 if (drv->in_interface_list)
2599 dl_list_del(&drv->list);
2600
2601 os_free(drv);
2602 }
2603
2604
2605 /**
2606 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
2607 * @eloop_ctx: Driver private data
2608 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
2609 *
2610 * This function can be used as registered timeout when starting a scan to
2611 * generate a scan completed event if the driver does not report this.
2612 */
2613 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
2614 {
2615 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2616 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
2617 wpa_driver_nl80211_set_mode(&drv->first_bss,
2618 drv->ap_scan_as_station);
2619 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2620 }
2621 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
2622 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
2623 }
2624
2625
2626 /**
2627 * wpa_driver_nl80211_scan - Request the driver to initiate scan
2628 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2629 * @params: Scan parameters
2630 * Returns: 0 on success, -1 on failure
2631 */
2632 static int wpa_driver_nl80211_scan(void *priv,
2633 struct wpa_driver_scan_params *params)
2634 {
2635 struct i802_bss *bss = priv;
2636 struct wpa_driver_nl80211_data *drv = bss->drv;
2637 int ret = 0, timeout;
2638 struct nl_msg *msg, *ssids, *freqs, *rates;
2639 size_t i;
2640
2641 msg = nlmsg_alloc();
2642 ssids = nlmsg_alloc();
2643 freqs = nlmsg_alloc();
2644 rates = nlmsg_alloc();
2645 if (!msg || !ssids || !freqs || !rates) {
2646 nlmsg_free(msg);
2647 nlmsg_free(ssids);
2648 nlmsg_free(freqs);
2649 nlmsg_free(rates);
2650 return -1;
2651 }
2652
2653 os_free(drv->filter_ssids);
2654 drv->filter_ssids = params->filter_ssids;
2655 params->filter_ssids = NULL;
2656 drv->num_filter_ssids = params->num_filter_ssids;
2657
2658 nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN);
2659
2660 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2661
2662 for (i = 0; i < params->num_ssids; i++) {
2663 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
2664 params->ssids[i].ssid,
2665 params->ssids[i].ssid_len);
2666 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
2667 params->ssids[i].ssid);
2668 }
2669 if (params->num_ssids)
2670 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
2671
2672 if (params->extra_ies) {
2673 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs",
2674 params->extra_ies, params->extra_ies_len);
2675 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
2676 params->extra_ies);
2677 }
2678
2679 if (params->freqs) {
2680 for (i = 0; params->freqs[i]; i++) {
2681 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
2682 "MHz", params->freqs[i]);
2683 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
2684 }
2685 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
2686 }
2687
2688 if (params->p2p_probe) {
2689 /*
2690 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
2691 * by masking out everything else apart from the OFDM rates 6,
2692 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
2693 * rates are left enabled.
2694 */
2695 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
2696 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
2697 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates);
2698
2699 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
2700 }
2701
2702 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2703 msg = NULL;
2704 if (ret) {
2705 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
2706 "(%s)", ret, strerror(-ret));
2707 #ifdef HOSTAPD
2708 if (is_ap_interface(drv->nlmode)) {
2709 /*
2710 * mac80211 does not allow scan requests in AP mode, so
2711 * try to do this in station mode.
2712 */
2713 if (wpa_driver_nl80211_set_mode(
2714 bss, NL80211_IFTYPE_STATION))
2715 goto nla_put_failure;
2716
2717 if (wpa_driver_nl80211_scan(drv, params)) {
2718 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
2719 goto nla_put_failure;
2720 }
2721
2722 /* Restore AP mode when processing scan results */
2723 drv->ap_scan_as_station = drv->nlmode;
2724 ret = 0;
2725 } else
2726 goto nla_put_failure;
2727 #else /* HOSTAPD */
2728 goto nla_put_failure;
2729 #endif /* HOSTAPD */
2730 }
2731
2732 /* Not all drivers generate "scan completed" wireless event, so try to
2733 * read results after a timeout. */
2734 timeout = 10;
2735 if (drv->scan_complete_events) {
2736 /*
2737 * The driver seems to deliver events to notify when scan is
2738 * complete, so use longer timeout to avoid race conditions
2739 * with scanning and following association request.
2740 */
2741 timeout = 30;
2742 }
2743 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
2744 "seconds", ret, timeout);
2745 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2746 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
2747 drv, drv->ctx);
2748
2749 nla_put_failure:
2750 nlmsg_free(ssids);
2751 nlmsg_free(msg);
2752 nlmsg_free(freqs);
2753 nlmsg_free(rates);
2754 return ret;
2755 }
2756
2757
2758 /**
2759 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
2760 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2761 * @params: Scan parameters
2762 * @interval: Interval between scan cycles in milliseconds
2763 * Returns: 0 on success, -1 on failure or if not supported
2764 */
2765 static int wpa_driver_nl80211_sched_scan(void *priv,
2766 struct wpa_driver_scan_params *params,
2767 u32 interval)
2768 {
2769 struct i802_bss *bss = priv;
2770 struct wpa_driver_nl80211_data *drv = bss->drv;
2771 int ret = 0;
2772 struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets;
2773 size_t i;
2774
2775 msg = nlmsg_alloc();
2776 ssids = nlmsg_alloc();
2777 freqs = nlmsg_alloc();
2778 if (!msg || !ssids || !freqs) {
2779 nlmsg_free(msg);
2780 nlmsg_free(ssids);
2781 nlmsg_free(freqs);
2782 return -1;
2783 }
2784
2785 os_free(drv->filter_ssids);
2786 drv->filter_ssids = params->filter_ssids;
2787 params->filter_ssids = NULL;
2788 drv->num_filter_ssids = params->num_filter_ssids;
2789
2790 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN);
2791
2792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2793
2794 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
2795
2796 if (drv->num_filter_ssids) {
2797 match_sets = nlmsg_alloc();
2798
2799 for (i = 0; i < drv->num_filter_ssids; i++) {
2800 wpa_hexdump_ascii(MSG_MSGDUMP,
2801 "nl80211: Sched scan filter SSID",
2802 drv->filter_ssids[i].ssid,
2803 drv->filter_ssids[i].ssid_len);
2804
2805 match_set_ssid = nlmsg_alloc();
2806 nla_put(match_set_ssid,
2807 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
2808 drv->filter_ssids[i].ssid_len,
2809 drv->filter_ssids[i].ssid);
2810
2811 nla_put_nested(match_sets, i + 1, match_set_ssid);
2812
2813 nlmsg_free(match_set_ssid);
2814 }
2815
2816 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
2817 match_sets);
2818 nlmsg_free(match_sets);
2819 }
2820
2821 for (i = 0; i < params->num_ssids; i++) {
2822 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID",
2823 params->ssids[i].ssid,
2824 params->ssids[i].ssid_len);
2825 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
2826 params->ssids[i].ssid);
2827 }
2828 if (params->num_ssids)
2829 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
2830
2831 if (params->extra_ies) {
2832 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs",
2833 params->extra_ies, params->extra_ies_len);
2834 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
2835 params->extra_ies);
2836 }
2837
2838 if (params->freqs) {
2839 for (i = 0; params->freqs[i]; i++) {
2840 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
2841 "MHz", params->freqs[i]);
2842 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
2843 }
2844 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
2845 }
2846
2847 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2848
2849 /* TODO: if we get an error here, we should fall back to normal scan */
2850
2851 msg = NULL;
2852 if (ret) {
2853 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
2854 "ret=%d (%s)", ret, strerror(-ret));
2855 goto nla_put_failure;
2856 }
2857
2858 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
2859 "scan interval %d msec", ret, interval);
2860
2861 nla_put_failure:
2862 nlmsg_free(ssids);
2863 nlmsg_free(msg);
2864 nlmsg_free(freqs);
2865 return ret;
2866 }
2867
2868
2869 /**
2870 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
2871 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2872 * Returns: 0 on success, -1 on failure or if not supported
2873 */
2874 static int wpa_driver_nl80211_stop_sched_scan(void *priv)
2875 {
2876 struct i802_bss *bss = priv;
2877 struct wpa_driver_nl80211_data *drv = bss->drv;
2878 int ret = 0;
2879 struct nl_msg *msg;
2880
2881 msg = nlmsg_alloc();
2882 if (!msg)
2883 return -1;
2884
2885 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
2886
2887 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2888
2889 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2890 msg = NULL;
2891 if (ret) {
2892 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
2893 "ret=%d (%s)", ret, strerror(-ret));
2894 goto nla_put_failure;
2895 }
2896
2897 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
2898
2899 nla_put_failure:
2900 nlmsg_free(msg);
2901 return ret;
2902 }
2903
2904
2905 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
2906 {
2907 const u8 *end, *pos;
2908
2909 if (ies == NULL)
2910 return NULL;
2911
2912 pos = ies;
2913 end = ies + ies_len;
2914
2915 while (pos + 1 < end) {
2916 if (pos + 2 + pos[1] > end)
2917 break;
2918 if (pos[0] == ie)
2919 return pos;
2920 pos += 2 + pos[1];
2921 }
2922
2923 return NULL;
2924 }
2925
2926
2927 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
2928 const u8 *ie, size_t ie_len)
2929 {
2930 const u8 *ssid;
2931 size_t i;
2932
2933 if (drv->filter_ssids == NULL)
2934 return 0;
2935
2936 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
2937 if (ssid == NULL)
2938 return 1;
2939
2940 for (i = 0; i < drv->num_filter_ssids; i++) {
2941 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
2942 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
2943 0)
2944 return 0;
2945 }
2946
2947 return 1;
2948 }
2949
2950
2951 static int bss_info_handler(struct nl_msg *msg, void *arg)
2952 {
2953 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2954 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2955 struct nlattr *bss[NL80211_BSS_MAX + 1];
2956 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2957 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
2958 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2959 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2960 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2961 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2962 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
2963 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2964 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2965 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2966 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2967 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
2968 };
2969 struct nl80211_bss_info_arg *_arg = arg;
2970 struct wpa_scan_results *res = _arg->res;
2971 struct wpa_scan_res **tmp;
2972 struct wpa_scan_res *r;
2973 const u8 *ie, *beacon_ie;
2974 size_t ie_len, beacon_ie_len;
2975 u8 *pos;
2976 size_t i;
2977
2978 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2979 genlmsg_attrlen(gnlh, 0), NULL);
2980 if (!tb[NL80211_ATTR_BSS])
2981 return NL_SKIP;
2982 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2983 bss_policy))
2984 return NL_SKIP;
2985 if (bss[NL80211_BSS_STATUS]) {
2986 enum nl80211_bss_status status;
2987 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
2988 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
2989 bss[NL80211_BSS_FREQUENCY]) {
2990 _arg->assoc_freq =
2991 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
2992 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
2993 _arg->assoc_freq);
2994 }
2995 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
2996 bss[NL80211_BSS_BSSID]) {
2997 os_memcpy(_arg->assoc_bssid,
2998 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
2999 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
3000 MACSTR, MAC2STR(_arg->assoc_bssid));
3001 }
3002 }
3003 if (!res)
3004 return NL_SKIP;
3005 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
3006 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3007 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3008 } else {
3009 ie = NULL;
3010 ie_len = 0;
3011 }
3012 if (bss[NL80211_BSS_BEACON_IES]) {
3013 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
3014 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
3015 } else {
3016 beacon_ie = NULL;
3017 beacon_ie_len = 0;
3018 }
3019
3020 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
3021 ie ? ie_len : beacon_ie_len))
3022 return NL_SKIP;
3023
3024 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3025 if (r == NULL)
3026 return NL_SKIP;
3027 if (bss[NL80211_BSS_BSSID])
3028 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
3029 ETH_ALEN);
3030 if (bss[NL80211_BSS_FREQUENCY])
3031 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3032 if (bss[NL80211_BSS_BEACON_INTERVAL])
3033 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
3034 if (bss[NL80211_BSS_CAPABILITY])
3035 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
3036 r->flags |= WPA_SCAN_NOISE_INVALID;
3037 if (bss[NL80211_BSS_SIGNAL_MBM]) {
3038 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
3039 r->level /= 100; /* mBm to dBm */
3040 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
3041 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
3042 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
3043 r->flags |= WPA_SCAN_LEVEL_INVALID;
3044 } else
3045 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
3046 if (bss[NL80211_BSS_TSF])
3047 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
3048 if (bss[NL80211_BSS_SEEN_MS_AGO])
3049 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
3050 r->ie_len = ie_len;
3051 pos = (u8 *) (r + 1);
3052 if (ie) {
3053 os_memcpy(pos, ie, ie_len);
3054 pos += ie_len;
3055 }
3056 r->beacon_ie_len = beacon_ie_len;
3057 if (beacon_ie)
3058 os_memcpy(pos, beacon_ie, beacon_ie_len);
3059
3060 if (bss[NL80211_BSS_STATUS]) {
3061 enum nl80211_bss_status status;
3062 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3063 switch (status) {
3064 case NL80211_BSS_STATUS_AUTHENTICATED:
3065 r->flags |= WPA_SCAN_AUTHENTICATED;
3066 break;
3067 case NL80211_BSS_STATUS_ASSOCIATED:
3068 r->flags |= WPA_SCAN_ASSOCIATED;
3069 break;
3070 default:
3071 break;
3072 }
3073 }
3074
3075 /*
3076 * cfg80211 maintains separate BSS table entries for APs if the same
3077 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
3078 * not use frequency as a separate key in the BSS table, so filter out
3079 * duplicated entries. Prefer associated BSS entry in such a case in
3080 * order to get the correct frequency into the BSS table.
3081 */
3082 for (i = 0; i < res->num; i++) {
3083 const u8 *s1, *s2;
3084 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
3085 continue;
3086
3087 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
3088 res->res[i]->ie_len, WLAN_EID_SSID);
3089 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
3090 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
3091 os_memcmp(s1, s2, 2 + s1[1]) != 0)
3092 continue;
3093
3094 /* Same BSSID,SSID was already included in scan results */
3095 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
3096 "for " MACSTR, MAC2STR(r->bssid));
3097
3098 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
3099 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
3100 os_free(res->res[i]);
3101 res->res[i] = r;
3102 } else
3103 os_free(r);
3104 return NL_SKIP;
3105 }
3106
3107 tmp = os_realloc(res->res,
3108 (res->num + 1) * sizeof(struct wpa_scan_res *));
3109 if (tmp == NULL) {
3110 os_free(r);
3111 return NL_SKIP;
3112 }
3113 tmp[res->num++] = r;
3114 res->res = tmp;
3115
3116 return NL_SKIP;
3117 }
3118
3119
3120 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
3121 const u8 *addr)
3122 {
3123 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
3124 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
3125 "mismatch (" MACSTR ")", MAC2STR(addr));
3126 wpa_driver_nl80211_mlme(drv, addr,
3127 NL80211_CMD_DEAUTHENTICATE,
3128 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
3129 }
3130 }
3131
3132
3133 static void wpa_driver_nl80211_check_bss_status(
3134 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
3135 {
3136 size_t i;
3137
3138 for (i = 0; i < res->num; i++) {
3139 struct wpa_scan_res *r = res->res[i];
3140 if (r->flags & WPA_SCAN_AUTHENTICATED) {
3141 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3142 "indicates BSS status with " MACSTR
3143 " as authenticated",
3144 MAC2STR(r->bssid));
3145 if (is_sta_interface(drv->nlmode) &&
3146 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
3147 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
3148 0) {
3149 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
3150 " in local state (auth=" MACSTR
3151 " assoc=" MACSTR ")",
3152 MAC2STR(drv->auth_bssid),
3153 MAC2STR(drv->bssid));
3154 clear_state_mismatch(drv, r->bssid);
3155 }
3156 }
3157
3158 if (r->flags & WPA_SCAN_ASSOCIATED) {
3159 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3160 "indicate BSS status with " MACSTR
3161 " as associated",
3162 MAC2STR(r->bssid));
3163 if (is_sta_interface(drv->nlmode) &&
3164 !drv->associated) {
3165 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3166 "(not associated) does not match "
3167 "with BSS state");
3168 clear_state_mismatch(drv, r->bssid);
3169 } else if (is_sta_interface(drv->nlmode) &&
3170 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
3171 0) {
3172 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3173 "(associated with " MACSTR ") does "
3174 "not match with BSS state",
3175 MAC2STR(drv->bssid));
3176 clear_state_mismatch(drv, r->bssid);
3177 clear_state_mismatch(drv, drv->bssid);
3178 }
3179 }
3180 }
3181 }
3182
3183
3184 static struct wpa_scan_results *
3185 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3186 {
3187 struct nl_msg *msg;
3188 struct wpa_scan_results *res;
3189 int ret;
3190 struct nl80211_bss_info_arg arg;
3191
3192 res = os_zalloc(sizeof(*res));
3193 if (res == NULL)
3194 return NULL;
3195 msg = nlmsg_alloc();
3196 if (!msg)
3197 goto nla_put_failure;
3198
3199 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
3200 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3201
3202 arg.drv = drv;
3203 arg.res = res;
3204 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
3205 msg = NULL;
3206 if (ret == 0) {
3207 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
3208 "BSSes)", (unsigned long) res->num);
3209 nl80211_get_noise_for_scan_results(drv, res);
3210 return res;
3211 }
3212 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
3213 "(%s)", ret, strerror(-ret));
3214 nla_put_failure:
3215 nlmsg_free(msg);
3216 wpa_scan_results_free(res);
3217 return NULL;
3218 }
3219
3220
3221 /**
3222 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
3223 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
3224 * Returns: Scan results on success, -1 on failure
3225 */
3226 static struct wpa_scan_results *
3227 wpa_driver_nl80211_get_scan_results(void *priv)
3228 {
3229 struct i802_bss *bss = priv;
3230 struct wpa_driver_nl80211_data *drv = bss->drv;
3231 struct wpa_scan_results *res;
3232
3233 res = nl80211_get_scan_results(drv);
3234 if (res)
3235 wpa_driver_nl80211_check_bss_status(drv, res);
3236 return res;
3237 }
3238
3239
3240 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
3241 {
3242 struct wpa_scan_results *res;
3243 size_t i;
3244
3245 res = nl80211_get_scan_results(drv);
3246 if (res == NULL) {
3247 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
3248 return;
3249 }
3250
3251 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
3252 for (i = 0; i < res->num; i++) {
3253 struct wpa_scan_res *r = res->res[i];
3254 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
3255 (int) i, (int) res->num, MAC2STR(r->bssid),
3256 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
3257 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
3258 }
3259
3260 wpa_scan_results_free(res);
3261 }
3262
3263
3264 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
3265 enum wpa_alg alg, const u8 *addr,
3266 int key_idx, int set_tx,
3267 const u8 *seq, size_t seq_len,
3268 const u8 *key, size_t key_len)
3269 {
3270 struct i802_bss *bss = priv;
3271 struct wpa_driver_nl80211_data *drv = bss->drv;
3272 int ifindex = if_nametoindex(ifname);
3273 struct nl_msg *msg;
3274 int ret;
3275
3276 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
3277 "set_tx=%d seq_len=%lu key_len=%lu",
3278 __func__, ifindex, alg, addr, key_idx, set_tx,
3279 (unsigned long) seq_len, (unsigned long) key_len);
3280 #ifdef CONFIG_TDLS
3281 if (key_idx == -1)
3282 key_idx = 0;
3283 #endif /* CONFIG_TDLS */
3284
3285 msg = nlmsg_alloc();
3286 if (!msg)
3287 return -ENOMEM;
3288
3289 if (alg == WPA_ALG_NONE) {
3290 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3291 } else {
3292 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3293 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
3294 switch (alg) {
3295 case WPA_ALG_WEP:
3296 if (key_len == 5)
3297 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3298 WLAN_CIPHER_SUITE_WEP40);
3299 else
3300 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3301 WLAN_CIPHER_SUITE_WEP104);
3302 break;
3303 case WPA_ALG_TKIP:
3304 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3305 WLAN_CIPHER_SUITE_TKIP);
3306 break;
3307 case WPA_ALG_CCMP:
3308 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3309 WLAN_CIPHER_SUITE_CCMP);
3310 break;
3311 case WPA_ALG_IGTK:
3312 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3313 WLAN_CIPHER_SUITE_AES_CMAC);
3314 break;
3315 default:
3316 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
3317 "algorithm %d", __func__, alg);
3318 nlmsg_free(msg);
3319 return -1;
3320 }
3321 }
3322
3323 if (seq && seq_len)
3324 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
3325
3326 if (addr && !is_broadcast_ether_addr(addr)) {
3327 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
3328 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3329
3330 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
3331 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
3332 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
3333 NL80211_KEYTYPE_GROUP);
3334 }
3335 } else if (addr && is_broadcast_ether_addr(addr)) {
3336 struct nl_msg *types;
3337 int err;
3338 wpa_printf(MSG_DEBUG, " broadcast key");
3339 types = nlmsg_alloc();
3340 if (!types)
3341 goto nla_put_failure;
3342 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
3343 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
3344 types);
3345 nlmsg_free(types);
3346 if (err)
3347 goto nla_put_failure;
3348 }
3349 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
3350 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3351
3352 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3353 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3354 ret = 0;
3355 if (ret)
3356 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
3357 ret, strerror(-ret));
3358
3359 /*
3360 * If we failed or don't need to set the default TX key (below),
3361 * we're done here.
3362 */
3363 if (ret || !set_tx || alg == WPA_ALG_NONE)
3364 return ret;
3365 if (is_ap_interface(drv->nlmode) && addr &&
3366 !is_broadcast_ether_addr(addr))
3367 return ret;
3368
3369 msg = nlmsg_alloc();
3370 if (!msg)
3371 return -ENOMEM;
3372
3373 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
3374 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
3375 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3376 if (alg == WPA_ALG_IGTK)
3377 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
3378 else
3379 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
3380 if (addr && is_broadcast_ether_addr(addr)) {
3381 struct nl_msg *types;
3382 int err;
3383 types = nlmsg_alloc();
3384 if (!types)
3385 goto nla_put_failure;
3386 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
3387 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
3388 types);
3389 nlmsg_free(types);
3390 if (err)
3391 goto nla_put_failure;
3392 } else if (addr) {
3393 struct nl_msg *types;
3394 int err;
3395 types = nlmsg_alloc();
3396 if (!types)
3397 goto nla_put_failure;
3398 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
3399 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
3400 types);
3401 nlmsg_free(types);
3402 if (err)
3403 goto nla_put_failure;
3404 }
3405
3406 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3407 if (ret == -ENOENT)
3408 ret = 0;
3409 if (ret)
3410 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
3411 "err=%d %s)", ret, strerror(-ret));
3412 return ret;
3413
3414 nla_put_failure:
3415 return -ENOBUFS;
3416 }
3417
3418
3419 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3420 int key_idx, int defkey,
3421 const u8 *seq, size_t seq_len,
3422 const u8 *key, size_t key_len)
3423 {
3424 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
3425 if (!key_attr)
3426 return -1;
3427
3428 if (defkey && alg == WPA_ALG_IGTK)
3429 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
3430 else if (defkey)
3431 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
3432
3433 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
3434
3435 switch (alg) {
3436 case WPA_ALG_WEP:
3437 if (key_len == 5)
3438 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3439 WLAN_CIPHER_SUITE_WEP40);
3440 else
3441 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3442 WLAN_CIPHER_SUITE_WEP104);
3443 break;
3444 case WPA_ALG_TKIP:
3445 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
3446 break;
3447 case WPA_ALG_CCMP:
3448 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
3449 break;
3450 case WPA_ALG_IGTK:
3451 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3452 WLAN_CIPHER_SUITE_AES_CMAC);
3453 break;
3454 default:
3455 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
3456 "algorithm %d", __func__, alg);
3457 return -1;
3458 }
3459
3460 if (seq && seq_len)
3461 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
3462
3463 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
3464
3465 nla_nest_end(msg, key_attr);
3466
3467 return 0;
3468 nla_put_failure:
3469 return -1;
3470 }
3471
3472
3473 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3474 struct nl_msg *msg)
3475 {
3476 int i, privacy = 0;
3477 struct nlattr *nl_keys, *nl_key;
3478
3479 for (i = 0; i < 4; i++) {
3480 if (!params->wep_key[i])
3481 continue;
3482 privacy = 1;
3483 break;
3484 }
3485 if (params->wps == WPS_MODE_PRIVACY)
3486 privacy = 1;
3487 if (params->pairwise_suite &&
3488 params->pairwise_suite != WPA_CIPHER_NONE)
3489 privacy = 1;
3490
3491 if (!privacy)
3492 return 0;
3493
3494 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
3495
3496 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3497 if (!nl_keys)
3498 goto nla_put_failure;
3499
3500 for (i = 0; i < 4; i++) {
3501 if (!params->wep_key[i])
3502 continue;
3503
3504 nl_key = nla_nest_start(msg, i);
3505 if (!nl_key)
3506 goto nla_put_failure;
3507
3508 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3509 params->wep_key[i]);
3510 if (params->wep_key_len[i] == 5)
3511 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3512 WLAN_CIPHER_SUITE_WEP40);
3513 else
3514 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3515 WLAN_CIPHER_SUITE_WEP104);
3516
3517 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
3518
3519 if (i == params->wep_tx_keyidx)
3520 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
3521
3522 nla_nest_end(msg, nl_key);
3523 }
3524 nla_nest_end(msg, nl_keys);
3525
3526 return 0;
3527
3528 nla_put_failure:
3529 return -ENOBUFS;
3530 }
3531
3532
3533 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3534 const u8 *addr, int cmd, u16 reason_code,
3535 int local_state_change)
3536 {
3537 int ret = -1;
3538 struct nl_msg *msg;
3539
3540 msg = nlmsg_alloc();
3541 if (!msg)
3542 return -1;
3543
3544 nl80211_cmd(drv, msg, 0, cmd);
3545
3546 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3547 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
3548 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3549 if (local_state_change)
3550 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
3551
3552 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3553 msg = NULL;
3554 if (ret) {
3555 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3556 "(%s)", ret, strerror(-ret));
3557 goto nla_put_failure;
3558 }
3559 ret = 0;
3560
3561 nla_put_failure:
3562 nlmsg_free(msg);
3563 return ret;
3564 }
3565
3566
3567 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
3568 const u8 *addr, int reason_code)
3569 {
3570 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3571 __func__, MAC2STR(addr), reason_code);
3572 drv->associated = 0;
3573 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
3574 reason_code, 0);
3575 }
3576
3577
3578 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
3579 int reason_code)
3580 {
3581 struct i802_bss *bss = priv;
3582 struct wpa_driver_nl80211_data *drv = bss->drv;
3583 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3584 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
3585 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3586 __func__, MAC2STR(addr), reason_code);
3587 drv->associated = 0;
3588 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
3589 return nl80211_leave_ibss(drv);
3590 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3591 reason_code, 0);
3592 }
3593
3594
3595 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
3596 int reason_code)
3597 {
3598 struct i802_bss *bss = priv;
3599 struct wpa_driver_nl80211_data *drv = bss->drv;
3600 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3601 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
3602 wpa_printf(MSG_DEBUG, "%s", __func__);
3603 drv->associated = 0;
3604 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
3605 reason_code, 0);
3606 }
3607
3608
3609 static int wpa_driver_nl80211_authenticate(
3610 void *priv, struct wpa_driver_auth_params *params)
3611 {
3612 struct i802_bss *bss = priv;
3613 struct wpa_driver_nl80211_data *drv = bss->drv;
3614 int ret = -1, i;
3615 struct nl_msg *msg;
3616 enum nl80211_auth_type type;
3617 enum nl80211_iftype nlmode;
3618 int count = 0;
3619
3620 drv->associated = 0;
3621 os_memset(drv->auth_bssid, 0, ETH_ALEN);
3622 /* FIX: IBSS mode */
3623 nlmode = params->p2p ?
3624 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3625 if (drv->nlmode != nlmode &&
3626 wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
3627 return -1;
3628
3629 retry:
3630 msg = nlmsg_alloc();
3631 if (!msg)
3632 return -1;
3633
3634 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3635 drv->ifindex);
3636
3637 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
3638
3639 for (i = 0; i < 4; i++) {
3640 if (!params->wep_key[i])
3641 continue;
3642 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
3643 NULL, i,
3644 i == params->wep_tx_keyidx, NULL, 0,
3645 params->wep_key[i],
3646 params->wep_key_len[i]);
3647 if (params->wep_tx_keyidx != i)
3648 continue;
3649 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
3650 params->wep_key[i], params->wep_key_len[i])) {
3651 nlmsg_free(msg);
3652 return -1;
3653 }
3654 }
3655
3656 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3657 if (params->bssid) {
3658 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3659 MAC2STR(params->bssid));
3660 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3661 }
3662 if (params->freq) {
3663 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3664 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3665 }
3666 if (params->ssid) {
3667 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3668 params->ssid, params->ssid_len);
3669 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3670 params->ssid);
3671 }
3672 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
3673 if (params->ie)
3674 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
3675 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3676 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3677 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3678 type = NL80211_AUTHTYPE_SHARED_KEY;
3679 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3680 type = NL80211_AUTHTYPE_NETWORK_EAP;
3681 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3682 type = NL80211_AUTHTYPE_FT;
3683 else
3684 goto nla_put_failure;
3685 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3686 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
3687 if (params->local_state_change) {
3688 wpa_printf(MSG_DEBUG, " * Local state change only");
3689 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
3690 }
3691
3692 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3693 msg = NULL;
3694 if (ret) {
3695 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3696 "(%s)", ret, strerror(-ret));
3697 count++;
3698 if (ret == -EALREADY && count == 1 && params->bssid &&
3699 !params->local_state_change) {
3700 /*
3701 * mac80211 does not currently accept new
3702 * authentication if we are already authenticated. As a
3703 * workaround, force deauthentication and try again.
3704 */
3705 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3706 "after forced deauthentication");
3707 wpa_driver_nl80211_deauthenticate(
3708 bss, params->bssid,
3709 WLAN_REASON_PREV_AUTH_NOT_VALID);
3710 nlmsg_free(msg);
3711 goto retry;
3712 }
3713 goto nla_put_failure;
3714 }
3715 ret = 0;
3716 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
3717 "successfully");
3718
3719 nla_put_failure:
3720 nlmsg_free(msg);
3721 return ret;
3722 }
3723
3724
3725 struct phy_info_arg {
3726 u16 *num_modes;
3727 struct hostapd_hw_modes *modes;
3728 };
3729
3730 static int phy_info_handler(struct nl_msg *msg, void *arg)
3731 {
3732 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3733 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3734 struct phy_info_arg *phy_info = arg;
3735
3736 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
3737
3738 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
3739 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
3740 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
3741 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
3742 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
3743 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
3744 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
3745 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
3746 };
3747
3748 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
3749 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
3750 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
3751 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
3752 };
3753
3754 struct nlattr *nl_band;
3755 struct nlattr *nl_freq;
3756 struct nlattr *nl_rate;
3757 int rem_band, rem_freq, rem_rate;
3758 struct hostapd_hw_modes *mode;
3759 int idx, mode_is_set;
3760
3761 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3762 genlmsg_attrlen(gnlh, 0), NULL);
3763
3764 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
3765 return NL_SKIP;
3766
3767 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
3768 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
3769 if (!mode)
3770 return NL_SKIP;
3771 phy_info->modes = mode;
3772
3773 mode_is_set = 0;
3774
3775 mode = &phy_info->modes[*(phy_info->num_modes)];
3776 memset(mode, 0, sizeof(*mode));
3777 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
3778 *(phy_info->num_modes) += 1;
3779
3780 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
3781 nla_len(nl_band), NULL);
3782
3783 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
3784 mode->ht_capab = nla_get_u16(
3785 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
3786 }
3787
3788 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
3789 mode->a_mpdu_params |= nla_get_u8(
3790 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
3791 0x03;
3792 }
3793
3794 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
3795 mode->a_mpdu_params |= nla_get_u8(
3796 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
3797 2;
3798 }
3799
3800 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
3801 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
3802 u8 *mcs;
3803 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
3804 os_memcpy(mode->mcs_set, mcs, 16);
3805 }
3806
3807 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
3808 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
3809 nla_len(nl_freq), freq_policy);
3810 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
3811 continue;
3812 mode->num_channels++;
3813 }
3814
3815 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
3816 if (!mode->channels)
3817 return NL_SKIP;
3818
3819 idx = 0;
3820
3821 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
3822 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
3823 nla_len(nl_freq), freq_policy);
3824 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
3825 continue;
3826
3827 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
3828 mode->channels[idx].flag = 0;
3829
3830 if (!mode_is_set) {
3831 /* crude heuristic */
3832 if (mode->channels[idx].freq < 4000)
3833 mode->mode = HOSTAPD_MODE_IEEE80211B;
3834 else
3835 mode->mode = HOSTAPD_MODE_IEEE80211A;
3836 mode_is_set = 1;
3837 }
3838
3839 /* crude heuristic */
3840 if (mode->channels[idx].freq < 4000)
3841 if (mode->channels[idx].freq == 2484)
3842 mode->channels[idx].chan = 14;
3843 else
3844 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
3845 else
3846 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
3847
3848 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
3849 mode->channels[idx].flag |=
3850 HOSTAPD_CHAN_DISABLED;
3851 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
3852 mode->channels[idx].flag |=
3853 HOSTAPD_CHAN_PASSIVE_SCAN;
3854 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
3855 mode->channels[idx].flag |=
3856 HOSTAPD_CHAN_NO_IBSS;
3857 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
3858 mode->channels[idx].flag |=
3859 HOSTAPD_CHAN_RADAR;
3860
3861 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
3862 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
3863 mode->channels[idx].max_tx_power =
3864 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
3865
3866 idx++;
3867 }
3868
3869 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
3870 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
3871 nla_len(nl_rate), rate_policy);
3872 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
3873 continue;
3874 mode->num_rates++;
3875 }
3876
3877 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
3878 if (!mode->rates)
3879 return NL_SKIP;
3880
3881 idx = 0;
3882
3883 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
3884 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
3885 nla_len(nl_rate), rate_policy);
3886 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
3887 continue;
3888 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
3889
3890 /* crude heuristic */
3891 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
3892 mode->rates[idx] > 200)
3893 mode->mode = HOSTAPD_MODE_IEEE80211G;
3894
3895 idx++;
3896 }
3897 }
3898
3899 return NL_SKIP;
3900 }
3901
3902 static struct hostapd_hw_modes *
3903 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
3904 {
3905 u16 m;
3906 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
3907 int i, mode11g_idx = -1;
3908
3909 /* If only 802.11g mode is included, use it to construct matching
3910 * 802.11b mode data. */
3911
3912 for (m = 0; m < *num_modes; m++) {
3913 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
3914 return modes; /* 802.11b already included */
3915 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
3916 mode11g_idx = m;
3917 }
3918
3919 if (mode11g_idx < 0)
3920 return modes; /* 2.4 GHz band not supported at all */
3921
3922 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
3923 if (nmodes == NULL)
3924 return modes; /* Could not add 802.11b mode */
3925
3926 mode = &nmodes[*num_modes];
3927 os_memset(mode, 0, sizeof(*mode));
3928 (*num_modes)++;
3929 modes = nmodes;
3930
3931 mode->mode = HOSTAPD_MODE_IEEE80211B;
3932
3933 mode11g = &modes[mode11g_idx];
3934 mode->num_channels = mode11g->num_channels;
3935 mode->channels = os_malloc(mode11g->num_channels *
3936 sizeof(struct hostapd_channel_data));
3937 if (mode->channels == NULL) {
3938 (*num_modes)--;
3939 return modes; /* Could not add 802.11b mode */
3940 }
3941 os_memcpy(mode->channels, mode11g->channels,
3942 mode11g->num_channels * sizeof(struct hostapd_channel_data));
3943
3944 mode->num_rates = 0;
3945 mode->rates = os_malloc(4 * sizeof(int));
3946 if (mode->rates == NULL) {
3947 os_free(mode->channels);
3948 (*num_modes)--;
3949 return modes; /* Could not add 802.11b mode */
3950 }
3951
3952 for (i = 0; i < mode11g->num_rates; i++) {
3953 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
3954 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
3955 continue;
3956 mode->rates[mode->num_rates] = mode11g->rates[i];
3957 mode->num_rates++;
3958 if (mode->num_rates == 4)
3959 break;
3960 }
3961
3962 if (mode->num_rates == 0) {
3963 os_free(mode->channels);
3964 os_free(mode->rates);
3965 (*num_modes)--;
3966 return modes; /* No 802.11b rates */
3967 }
3968
3969 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
3970 "information");
3971
3972 return modes;
3973 }
3974
3975
3976 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
3977 int end)
3978 {
3979 int c;
3980
3981 for (c = 0; c < mode->num_channels; c++) {
3982 struct hostapd_channel_data *chan = &mode->channels[c];
3983 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
3984 chan->flag |= HOSTAPD_CHAN_HT40;
3985 }
3986 }
3987
3988
3989 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
3990 int end)
3991 {
3992 int c;
3993
3994 for (c = 0; c < mode->num_channels; c++) {
3995 struct hostapd_channel_data *chan = &mode->channels[c];
3996 if (!(chan->flag & HOSTAPD_CHAN_HT40))
3997 continue;
3998 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
3999 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
4000 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
4001 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
4002 }
4003 }
4004
4005
4006 static void nl80211_reg_rule_ht40(struct nlattr *tb[],
4007 struct phy_info_arg *results)
4008 {
4009 u32 start, end, max_bw;
4010 u16 m;
4011
4012 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
4013 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
4014 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
4015 return;
4016
4017 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
4018 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
4019 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
4020
4021 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
4022 start, end, max_bw);
4023 if (max_bw < 40)
4024 return;
4025
4026 for (m = 0; m < *results->num_modes; m++) {
4027 if (!(results->modes[m].ht_capab &
4028 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
4029 continue;
4030 nl80211_set_ht40_mode(&results->modes[m], start, end);
4031 }
4032 }
4033
4034
4035 static void nl80211_reg_rule_sec(struct nlattr *tb[],
4036 struct phy_info_arg *results)
4037 {
4038 u32 start, end, max_bw;
4039 u16 m;
4040
4041 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
4042 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
4043 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
4044 return;
4045
4046 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
4047 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
4048 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
4049
4050 if (max_bw < 20)
4051 return;
4052
4053 for (m = 0; m < *results->num_modes; m++) {
4054 if (!(results->modes[m].ht_capab &
4055 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
4056 continue;
4057 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
4058 }
4059 }
4060
4061
4062 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
4063 {
4064 struct phy_info_arg *results = arg;
4065 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4066 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4067 struct nlattr *nl_rule;
4068 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
4069 int rem_rule;
4070 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4071 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4072 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4073 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4074 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4075 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4076 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4077 };
4078
4079 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4080 genlmsg_attrlen(gnlh, 0), NULL);
4081 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
4082 !tb_msg[NL80211_ATTR_REG_RULES]) {
4083 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
4084 "available");
4085 return NL_SKIP;
4086 }
4087
4088 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
4089 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
4090
4091 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
4092 {
4093 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
4094 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
4095 nl80211_reg_rule_ht40(tb_rule, results);
4096 }
4097
4098 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
4099 {
4100 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
4101 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
4102 nl80211_reg_rule_sec(tb_rule, results);
4103 }
4104
4105 return NL_SKIP;
4106 }
4107
4108
4109 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
4110 struct phy_info_arg *results)
4111 {
4112 struct nl_msg *msg;
4113
4114 msg = nlmsg_alloc();
4115 if (!msg)
4116 return -ENOMEM;
4117
4118 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
4119 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
4120 }
4121
4122
4123 static struct hostapd_hw_modes *
4124 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
4125 {
4126 struct i802_bss *bss = priv;
4127 struct wpa_driver_nl80211_data *drv = bss->drv;
4128 struct nl_msg *msg;
4129 struct phy_info_arg result = {
4130 .num_modes = num_modes,
4131 .modes = NULL,
4132 };
4133
4134 *num_modes = 0;
4135 *flags = 0;
4136
4137 msg = nlmsg_alloc();
4138 if (!msg)
4139 return NULL;
4140
4141 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
4142
4143 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4144
4145 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
4146 nl80211_set_ht40_flags(drv, &result);
4147 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
4148 }
4149 nla_put_failure:
4150 return NULL;
4151 }
4152
4153
4154 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
4155 const void *data, size_t len,
4156 int encrypt)
4157 {
4158 __u8 rtap_hdr[] = {
4159 0x00, 0x00, /* radiotap version */
4160 0x0e, 0x00, /* radiotap length */
4161 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
4162 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
4163 0x00, /* padding */
4164 0x00, 0x00, /* RX and TX flags to indicate that */
4165 0x00, 0x00, /* this is the injected frame directly */
4166 };
4167 struct iovec iov[2] = {
4168 {
4169 .iov_base = &rtap_hdr,
4170 .iov_len = sizeof(rtap_hdr),
4171 },
4172 {
4173 .iov_base = (void *) data,
4174 .iov_len = len,
4175 }
4176 };
4177 struct msghdr msg = {
4178 .msg_name = NULL,
4179 .msg_namelen = 0,
4180 .msg_iov = iov,
4181 .msg_iovlen = 2,
4182 .msg_control = NULL,
4183 .msg_controllen = 0,
4184 .msg_flags = 0,
4185 };
4186 int res;
4187
4188 if (encrypt)
4189 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
4190
4191 if (drv->monitor_sock < 0) {
4192 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
4193 "for %s", __func__);
4194 return -1;
4195 }
4196
4197 res = sendmsg(drv->monitor_sock, &msg, 0);
4198 if (res < 0) {
4199 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
4200 return -1;
4201 }
4202 return 0;
4203 }
4204
4205
4206 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
4207 size_t data_len)
4208 {
4209 struct i802_bss *bss = priv;
4210 struct wpa_driver_nl80211_data *drv = bss->drv;
4211 struct ieee80211_mgmt *mgmt;
4212 int encrypt = 1;
4213 u16 fc;
4214
4215 mgmt = (struct ieee80211_mgmt *) data;
4216 fc = le_to_host16(mgmt->frame_control);
4217
4218 if (is_sta_interface(drv->nlmode) &&
4219 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4220 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
4221 /*
4222 * The use of last_mgmt_freq is a bit of a hack,
4223 * but it works due to the single-threaded nature
4224 * of wpa_supplicant.
4225 */
4226 return nl80211_send_frame_cmd(drv, drv->last_mgmt_freq, 0,
4227 data, data_len, NULL, 1);
4228 }
4229
4230 if (drv->no_monitor_iface_capab && is_ap_interface(drv->nlmode)) {
4231 return nl80211_send_frame_cmd(drv, drv->ap_oper_freq, 0,
4232 data, data_len, NULL, 0);
4233 }
4234
4235 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4236 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
4237 /*
4238 * Only one of the authentication frame types is encrypted.
4239 * In order for static WEP encryption to work properly (i.e.,
4240 * to not encrypt the frame), we need to tell mac80211 about
4241 * the frames that must not be encrypted.
4242 */
4243 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4244 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
4245 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
4246 encrypt = 0;
4247 }
4248
4249 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
4250 }
4251
4252
4253 static int nl80211_set_ap_isolate(struct i802_bss *bss, int enabled)
4254 {
4255 struct wpa_driver_nl80211_data *drv = bss->drv;
4256 struct nl_msg *msg;
4257
4258 msg = nlmsg_alloc();
4259 if (!msg)
4260 return -ENOMEM;
4261
4262 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
4263
4264 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
4265 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, enabled);
4266
4267 return send_and_recv_msgs(drv, msg, NULL, NULL);
4268 nla_put_failure:
4269 return -ENOBUFS;
4270 }
4271
4272
4273 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
4274 int slot, int ht_opmode)
4275 {
4276 struct wpa_driver_nl80211_data *drv = bss->drv;
4277 struct nl_msg *msg;
4278
4279 msg = nlmsg_alloc();
4280 if (!msg)
4281 return -ENOMEM;
4282
4283 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
4284
4285 if (cts >= 0)
4286 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
4287 if (preamble >= 0)
4288 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
4289 if (slot >= 0)
4290 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
4291 if (ht_opmode >= 0)
4292 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
4293 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
4294
4295 return send_and_recv_msgs(drv, msg, NULL, NULL);
4296 nla_put_failure:
4297 return -ENOBUFS;
4298 }
4299
4300
4301 static int wpa_driver_nl80211_set_ap(void *priv,
4302 struct wpa_driver_ap_params *params)
4303 {
4304 struct i802_bss *bss = priv;
4305 struct wpa_driver_nl80211_data *drv = bss->drv;
4306 struct nl_msg *msg;
4307 u8 cmd = NL80211_CMD_NEW_BEACON;
4308 int ret;
4309 int beacon_set;
4310 int ifindex = if_nametoindex(bss->ifname);
4311 int num_suites;
4312 u32 suites[10];
4313 u32 ver;
4314
4315 beacon_set = bss->beacon_set;
4316
4317 msg = nlmsg_alloc();
4318 if (!msg)
4319 return -ENOMEM;
4320
4321 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
4322 beacon_set);
4323 if (beacon_set)
4324 cmd = NL80211_CMD_SET_BEACON;
4325
4326 nl80211_cmd(drv, msg, 0, cmd);
4327 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
4328 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
4329 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4330 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
4331 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
4332 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4333 params->ssid);
4334 switch (params->hide_ssid) {
4335 case NO_SSID_HIDING:
4336 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
4337 NL80211_HIDDEN_SSID_NOT_IN_USE);
4338 break;
4339 case HIDDEN_SSID_ZERO_LEN:
4340 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
4341 NL80211_HIDDEN_SSID_ZERO_LEN);
4342 break;
4343 case HIDDEN_SSID_ZERO_CONTENTS:
4344 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
4345 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
4346 break;
4347 }
4348 if (params->privacy)
4349 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4350 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
4351 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
4352 /* Leave out the attribute */
4353 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
4354 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
4355 NL80211_AUTHTYPE_SHARED_KEY);
4356 else
4357 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
4358 NL80211_AUTHTYPE_OPEN_SYSTEM);
4359
4360 ver = 0;
4361 if (params->wpa_version & WPA_PROTO_WPA)
4362 ver |= NL80211_WPA_VERSION_1;
4363 if (params->wpa_version & WPA_PROTO_RSN)
4364 ver |= NL80211_WPA_VERSION_2;
4365 if (ver)
4366 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
4367
4368 num_suites = 0;
4369 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
4370 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
4371 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
4372 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
4373 if (num_suites) {
4374 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
4375 num_suites * sizeof(u32), suites);
4376 }
4377
4378 num_suites = 0;
4379 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
4380 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
4381 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
4382 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
4383 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
4384 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
4385 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
4386 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
4387 if (num_suites) {
4388 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4389 num_suites * sizeof(u32), suites);
4390 }
4391
4392 switch (params->group_cipher) {
4393 case WPA_CIPHER_CCMP:
4394 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4395 WLAN_CIPHER_SUITE_CCMP);
4396 break;
4397 case WPA_CIPHER_TKIP:
4398 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4399 WLAN_CIPHER_SUITE_TKIP);
4400 break;
4401 case WPA_CIPHER_WEP104:
4402 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4403 WLAN_CIPHER_SUITE_WEP104);
4404 break;
4405 case WPA_CIPHER_WEP40:
4406 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4407 WLAN_CIPHER_SUITE_WEP40);
4408 break;
4409 }
4410
4411 if (params->beacon_ies) {
4412 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
4413 wpabuf_head(params->beacon_ies));
4414 }
4415 if (params->proberesp_ies) {
4416 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
4417 wpabuf_len(params->proberesp_ies),
4418 wpabuf_head(params->proberesp_ies));
4419 }
4420 if (params->assocresp_ies) {
4421 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
4422 wpabuf_len(params->assocresp_ies),
4423 wpabuf_head(params->assocresp_ies));
4424 }
4425
4426 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4427 if (ret) {
4428 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4429 ret, strerror(-ret));
4430 } else {
4431 bss->beacon_set = 1;
4432 ret = nl80211_set_ap_isolate(bss, params->isolate);
4433 if (!params->isolate && ret) {
4434 wpa_printf(MSG_DEBUG, "nl80211: Ignore AP isolation "
4435 "configuration error since isolation is "
4436 "not used");
4437 ret = 0;
4438 }
4439
4440 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4441 params->short_slot_time, params->ht_opmode);
4442 }
4443 return ret;
4444 nla_put_failure:
4445 return -ENOBUFS;
4446 }
4447
4448
4449 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
4450 int freq, int ht_enabled,
4451 int sec_channel_offset)
4452 {
4453 struct nl_msg *msg;
4454 int ret;
4455
4456 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
4457 "sec_channel_offset=%d)",
4458 freq, ht_enabled, sec_channel_offset);
4459 msg = nlmsg_alloc();
4460 if (!msg)
4461 return -1;
4462
4463 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
4464
4465 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4466 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
4467 if (ht_enabled) {
4468 switch (sec_channel_offset) {
4469 case -1:
4470 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4471 NL80211_CHAN_HT40MINUS);
4472 break;
4473 case 1:
4474 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4475 NL80211_CHAN_HT40PLUS);
4476 break;
4477 default:
4478 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4479 NL80211_CHAN_HT20);
4480 break;
4481 }
4482 }
4483
4484 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4485 if (ret == 0)
4486 return 0;
4487 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
4488 "%d (%s)", freq, ret, strerror(-ret));
4489 nla_put_failure:
4490 return -1;
4491 }
4492
4493
4494 static u32 sta_flags_nl80211(int flags)
4495 {
4496 u32 f = 0;
4497
4498 if (flags & WPA_STA_AUTHORIZED)
4499 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4500 if (flags & WPA_STA_WMM)
4501 f |= BIT(NL80211_STA_FLAG_WME);
4502 if (flags & WPA_STA_SHORT_PREAMBLE)
4503 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4504 if (flags & WPA_STA_MFP)
4505 f |= BIT(NL80211_STA_FLAG_MFP);
4506 if (flags & WPA_STA_TDLS_PEER)
4507 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
4508
4509 return f;
4510 }
4511
4512
4513 static int wpa_driver_nl80211_sta_add(void *priv,
4514 struct hostapd_sta_add_params *params)
4515 {
4516 struct i802_bss *bss = priv;
4517 struct wpa_driver_nl80211_data *drv = bss->drv;
4518 struct nl_msg *msg;
4519 struct nl80211_sta_flag_update upd;
4520 int ret = -ENOBUFS;
4521
4522 if ((params->flags & WPA_STA_TDLS_PEER) &&
4523 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4524 return -EOPNOTSUPP;
4525
4526 msg = nlmsg_alloc();
4527 if (!msg)
4528 return -ENOMEM;
4529
4530 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
4531 NL80211_CMD_NEW_STATION);
4532
4533 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
4534 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
4535 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
4536 params->supp_rates);
4537 if (!params->set) {
4538 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
4539 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4540 params->listen_interval);
4541 }
4542 if (params->ht_capabilities) {
4543 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
4544 sizeof(*params->ht_capabilities),
4545 params->ht_capabilities);
4546 }
4547
4548 os_memset(&upd, 0, sizeof(upd));
4549 upd.mask = sta_flags_nl80211(params->flags);
4550 upd.set = upd.mask;
4551 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
4552
4553 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4554 if (ret)
4555 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4556 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4557 strerror(-ret));
4558 if (ret == -EEXIST)
4559 ret = 0;
4560 nla_put_failure:
4561 return ret;
4562 }
4563
4564
4565 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
4566 {
4567 struct i802_bss *bss = priv;
4568 struct wpa_driver_nl80211_data *drv = bss->drv;
4569 struct nl_msg *msg;
4570 int ret;
4571
4572 msg = nlmsg_alloc();
4573 if (!msg)
4574 return -ENOMEM;
4575
4576 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
4577
4578 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
4579 if_nametoindex(bss->ifname));
4580 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4581
4582 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4583 if (ret == -ENOENT)
4584 return 0;
4585 return ret;
4586 nla_put_failure:
4587 return -ENOBUFS;
4588 }
4589
4590
4591 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
4592 int ifidx)
4593 {
4594 struct nl_msg *msg;
4595
4596 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4597
4598 /* stop listening for EAPOL on this interface */
4599 del_ifidx(drv, ifidx);
4600
4601 msg = nlmsg_alloc();
4602 if (!msg)
4603 goto nla_put_failure;
4604
4605 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4606 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
4607
4608 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4609 return;
4610 nla_put_failure:
4611 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4612 }
4613
4614
4615 static const char * nl80211_iftype_str(enum nl80211_iftype mode)
4616 {
4617 switch (mode) {
4618 case NL80211_IFTYPE_ADHOC:
4619 return "ADHOC";
4620 case NL80211_IFTYPE_STATION:
4621 return "STATION";
4622 case NL80211_IFTYPE_AP:
4623 return "AP";
4624 case NL80211_IFTYPE_MONITOR:
4625 return "MONITOR";
4626 case NL80211_IFTYPE_P2P_CLIENT:
4627 return "P2P_CLIENT";
4628 case NL80211_IFTYPE_P2P_GO:
4629 return "P2P_GO";
4630 default:
4631 return "unknown";
4632 }
4633 }
4634
4635
4636 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4637 const char *ifname,
4638 enum nl80211_iftype iftype,
4639 const u8 *addr, int wds)
4640 {
4641 struct nl_msg *msg, *flags = NULL;
4642 int ifidx;
4643 int ret = -ENOBUFS;
4644
4645 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4646 iftype, nl80211_iftype_str(iftype));
4647
4648 msg = nlmsg_alloc();
4649 if (!msg)
4650 return -1;
4651
4652 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
4653 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4654 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
4655 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
4656
4657 if (iftype == NL80211_IFTYPE_MONITOR) {
4658 int err;
4659
4660 flags = nlmsg_alloc();
4661 if (!flags)
4662 goto nla_put_failure;
4663
4664 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
4665
4666 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
4667
4668 nlmsg_free(flags);
4669
4670 if (err)
4671 goto nla_put_failure;
4672 } else if (wds) {
4673 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
4674 }
4675
4676 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4677 if (ret) {
4678 nla_put_failure:
4679 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4680 ifname, ret, strerror(-ret));
4681 return ret;
4682 }
4683
4684 ifidx = if_nametoindex(ifname);
4685 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4686 ifname, ifidx);
4687
4688 if (ifidx <= 0)
4689 return -1;
4690
4691 /* start listening for EAPOL on this interface */
4692 add_ifidx(drv, ifidx);
4693
4694 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4695 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
4696 nl80211_remove_iface(drv, ifidx);
4697 return -1;
4698 }
4699
4700 return ifidx;
4701 }
4702
4703
4704 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4705 const char *ifname, enum nl80211_iftype iftype,
4706 const u8 *addr, int wds)
4707 {
4708 int ret;
4709
4710 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
4711
4712 /* if error occurred and interface exists already */
4713 if (ret == -ENFILE && if_nametoindex(ifname)) {
4714 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4715
4716 /* Try to remove the interface that was already there. */
4717 nl80211_remove_iface(drv, if_nametoindex(ifname));
4718
4719 /* Try to create the interface again */
4720 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
4721 wds);
4722 }
4723
4724 if (ret >= 0 && is_p2p_interface(iftype))
4725 nl80211_disable_11b_rates(drv, ret, 1);
4726
4727 return ret;
4728 }
4729
4730
4731 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
4732 {
4733 struct ieee80211_hdr *hdr;
4734 u16 fc;
4735 union wpa_event_data event;
4736
4737 hdr = (struct ieee80211_hdr *) buf;
4738 fc = le_to_host16(hdr->frame_control);
4739
4740 os_memset(&event, 0, sizeof(event));
4741 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
4742 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
4743 event.tx_status.dst = hdr->addr1;
4744 event.tx_status.data = buf;
4745 event.tx_status.data_len = len;
4746 event.tx_status.ack = ok;
4747 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
4748 }
4749
4750
4751 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
4752 u8 *buf, size_t len)
4753 {
4754 struct ieee80211_hdr *hdr = (void *)buf;
4755 u16 fc;
4756 union wpa_event_data event;
4757
4758 if (len < sizeof(*hdr))
4759 return;
4760
4761 fc = le_to_host16(hdr->frame_control);
4762
4763 os_memset(&event, 0, sizeof(event));
4764 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
4765 event.rx_from_unknown.addr = hdr->addr2;
4766 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
4767 (WLAN_FC_FROMDS | WLAN_FC_TODS);
4768 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4769 }
4770
4771
4772 static void handle_frame(struct wpa_driver_nl80211_data *drv,
4773 u8 *buf, size_t len, int datarate, int ssi_signal)
4774 {
4775 struct ieee80211_hdr *hdr;
4776 u16 fc;
4777 union wpa_event_data event;
4778
4779 hdr = (struct ieee80211_hdr *) buf;
4780 fc = le_to_host16(hdr->frame_control);
4781
4782 switch (WLAN_FC_GET_TYPE(fc)) {
4783 case WLAN_FC_TYPE_MGMT:
4784 os_memset(&event, 0, sizeof(event));
4785 event.rx_mgmt.frame = buf;
4786 event.rx_mgmt.frame_len = len;
4787 event.rx_mgmt.datarate = datarate;
4788 event.rx_mgmt.ssi_signal = ssi_signal;
4789 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
4790 break;
4791 case WLAN_FC_TYPE_CTRL:
4792 /* can only get here with PS-Poll frames */
4793 wpa_printf(MSG_DEBUG, "CTRL");
4794 from_unknown_sta(drv, buf, len);
4795 break;
4796 case WLAN_FC_TYPE_DATA:
4797 from_unknown_sta(drv, buf, len);
4798 break;
4799 }
4800 }
4801
4802
4803 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
4804 {
4805 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4806 int len;
4807 unsigned char buf[3000];
4808 struct ieee80211_radiotap_iterator iter;
4809 int ret;
4810 int datarate = 0, ssi_signal = 0;
4811 int injected = 0, failed = 0, rxflags = 0;
4812
4813 len = recv(sock, buf, sizeof(buf), 0);
4814 if (len < 0) {
4815 perror("recv");
4816 return;
4817 }
4818
4819 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
4820 printf("received invalid radiotap frame\n");
4821 return;
4822 }
4823
4824 while (1) {
4825 ret = ieee80211_radiotap_iterator_next(&iter);
4826 if (ret == -ENOENT)
4827 break;
4828 if (ret) {
4829 printf("received invalid radiotap frame (%d)\n", ret);
4830 return;
4831 }
4832 switch (iter.this_arg_index) {
4833 case IEEE80211_RADIOTAP_FLAGS:
4834 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
4835 len -= 4;
4836 break;
4837 case IEEE80211_RADIOTAP_RX_FLAGS:
4838 rxflags = 1;
4839 break;
4840 case IEEE80211_RADIOTAP_TX_FLAGS:
4841 injected = 1;
4842 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
4843 IEEE80211_RADIOTAP_F_TX_FAIL;
4844 break;
4845 case IEEE80211_RADIOTAP_DATA_RETRIES:
4846 break;
4847 case IEEE80211_RADIOTAP_CHANNEL:
4848 /* TODO: convert from freq/flags to channel number */
4849 break;
4850 case IEEE80211_RADIOTAP_RATE:
4851 datarate = *iter.this_arg * 5;
4852 break;
4853 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
4854 ssi_signal = *iter.this_arg;
4855 break;
4856 }
4857 }
4858
4859 if (rxflags && injected)
4860 return;
4861
4862 if (!injected)
4863 handle_frame(drv, buf + iter.max_length,
4864 len - iter.max_length, datarate, ssi_signal);
4865 else
4866 handle_tx_callback(drv->ctx, buf + iter.max_length,
4867 len - iter.max_length, !failed);
4868 }
4869
4870
4871 /*
4872 * we post-process the filter code later and rewrite
4873 * this to the offset to the last instruction
4874 */
4875 #define PASS 0xFF
4876 #define FAIL 0xFE
4877
4878 static struct sock_filter msock_filter_insns[] = {
4879 /*
4880 * do a little-endian load of the radiotap length field
4881 */
4882 /* load lower byte into A */
4883 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
4884 /* put it into X (== index register) */
4885 BPF_STMT(BPF_MISC| BPF_TAX, 0),
4886 /* load upper byte into A */
4887 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
4888 /* left-shift it by 8 */
4889 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
4890 /* or with X */
4891 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
4892 /* put result into X */
4893 BPF_STMT(BPF_MISC| BPF_TAX, 0),
4894
4895 /*
4896 * Allow management frames through, this also gives us those
4897 * management frames that we sent ourselves with status
4898 */
4899 /* load the lower byte of the IEEE 802.11 frame control field */
4900 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
4901 /* mask off frame type and version */
4902 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
4903 /* accept frame if it's both 0, fall through otherwise */
4904 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
4905
4906 /*
4907 * TODO: add a bit to radiotap RX flags that indicates
4908 * that the sending station is not associated, then
4909 * add a filter here that filters on our DA and that flag
4910 * to allow us to deauth frames to that bad station.
4911 *
4912 * For now allow all To DS data frames through.
4913 */
4914 /* load the IEEE 802.11 frame control field */
4915 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
4916 /* mask off frame type, version and DS status */
4917 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
4918 /* accept frame if version 0, type 2 and To DS, fall through otherwise
4919 */
4920 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
4921
4922 #if 0
4923 /*
4924 * drop non-data frames
4925 */
4926 /* load the lower byte of the frame control field */
4927 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
4928 /* mask off QoS bit */
4929 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
4930 /* drop non-data frames */
4931 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
4932 #endif
4933 /* load the upper byte of the frame control field */
4934 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
4935 /* mask off toDS/fromDS */
4936 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
4937 /* accept WDS frames */
4938 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
4939
4940 /*
4941 * add header length to index
4942 */
4943 /* load the lower byte of the frame control field */
4944 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
4945 /* mask off QoS bit */
4946 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
4947 /* right shift it by 6 to give 0 or 2 */
4948 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
4949 /* add data frame header length */
4950 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
4951 /* add index, was start of 802.11 header */
4952 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
4953 /* move to index, now start of LL header */
4954 BPF_STMT(BPF_MISC | BPF_TAX, 0),
4955
4956 /*
4957 * Accept empty data frames, we use those for
4958 * polling activity.
4959 */
4960 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
4961 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
4962
4963 /*
4964 * Accept EAPOL frames
4965 */
4966 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
4967 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
4968 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
4969 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
4970
4971 /* keep these last two statements or change the code below */
4972 /* return 0 == "DROP" */
4973 BPF_STMT(BPF_RET | BPF_K, 0),
4974 /* return ~0 == "keep all" */
4975 BPF_STMT(BPF_RET | BPF_K, ~0),
4976 };
4977
4978 static struct sock_fprog msock_filter = {
4979 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
4980 .filter = msock_filter_insns,
4981 };
4982
4983
4984 static int add_monitor_filter(int s)
4985 {
4986 int idx;
4987
4988 /* rewrite all PASS/FAIL jump offsets */
4989 for (idx = 0; idx < msock_filter.len; idx++) {
4990 struct sock_filter *insn = &msock_filter_insns[idx];
4991
4992 if (BPF_CLASS(insn->code) == BPF_JMP) {
4993 if (insn->code == (BPF_JMP|BPF_JA)) {
4994 if (insn->k == PASS)
4995 insn->k = msock_filter.len - idx - 2;
4996 else if (insn->k == FAIL)
4997 insn->k = msock_filter.len - idx - 3;
4998 }
4999
5000 if (insn->jt == PASS)
5001 insn->jt = msock_filter.len - idx - 2;
5002 else if (insn->jt == FAIL)
5003 insn->jt = msock_filter.len - idx - 3;
5004
5005 if (insn->jf == PASS)
5006 insn->jf = msock_filter.len - idx - 2;
5007 else if (insn->jf == FAIL)
5008 insn->jf = msock_filter.len - idx - 3;
5009 }
5010 }
5011
5012 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
5013 &msock_filter, sizeof(msock_filter))) {
5014 perror("SO_ATTACH_FILTER");
5015 return -1;
5016 }
5017
5018 return 0;
5019 }
5020
5021
5022 static void nl80211_remove_monitor_interface(
5023 struct wpa_driver_nl80211_data *drv)
5024 {
5025 if (drv->monitor_ifidx >= 0) {
5026 nl80211_remove_iface(drv, drv->monitor_ifidx);
5027 drv->monitor_ifidx = -1;
5028 }
5029 if (drv->monitor_sock >= 0) {
5030 eloop_unregister_read_sock(drv->monitor_sock);
5031 close(drv->monitor_sock);
5032 drv->monitor_sock = -1;
5033 }
5034 }
5035
5036
5037 static int
5038 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
5039 {
5040 char buf[IFNAMSIZ];
5041 struct sockaddr_ll ll;
5042 int optval;
5043 socklen_t optlen;
5044
5045 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
5046 /*
5047 * P2P interface name is of the format p2p-%s-%d. For monitor
5048 * interface name corresponding to P2P GO, replace "p2p-" with
5049 * "mon-" to retain the same interface name length and to
5050 * indicate that it is a monitor interface.
5051 */
5052 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
5053 } else {
5054 /* Non-P2P interface with AP functionality. */
5055 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
5056 }
5057
5058 buf[IFNAMSIZ - 1] = '\0';
5059
5060 drv->monitor_ifidx =
5061 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
5062 0);
5063
5064 if (drv->monitor_ifidx == -EOPNOTSUPP) {
5065 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
5066 "monitor interface type - try to run without it");
5067 drv->no_monitor_iface_capab = 1;
5068 }
5069
5070 if (drv->monitor_ifidx < 0)
5071 return -1;
5072
5073 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
5074 goto error;
5075
5076 memset(&ll, 0, sizeof(ll));
5077 ll.sll_family = AF_PACKET;
5078 ll.sll_ifindex = drv->monitor_ifidx;
5079 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
5080 if (drv->monitor_sock < 0) {
5081 perror("socket[PF_PACKET,SOCK_RAW]");
5082 goto error;
5083 }
5084
5085 if (add_monitor_filter(drv->monitor_sock)) {
5086 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
5087 "interface; do filtering in user space");
5088 /* This works, but will cost in performance. */
5089 }
5090
5091 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
5092 perror("monitor socket bind");
5093 goto error;
5094 }
5095
5096 optlen = sizeof(optval);
5097 optval = 20;
5098 if (setsockopt
5099 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
5100 perror("Failed to set socket priority");
5101 goto error;
5102 }
5103
5104 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
5105 drv, NULL)) {
5106 printf("Could not register monitor read socket\n");
5107 goto error;
5108 }
5109
5110 return 0;
5111 error:
5112 nl80211_remove_monitor_interface(drv);
5113 return -1;
5114 }
5115
5116
5117 #ifdef CONFIG_AP
5118 static int nl80211_send_eapol_data(struct i802_bss *bss,
5119 const u8 *addr, const u8 *data,
5120 size_t data_len, const u8 *own_addr)
5121 {
5122 if (bss->drv->l2 == NULL) {
5123 wpa_printf(MSG_DEBUG, "nl80211: No l2_packet to send EAPOL");
5124 return -1;
5125 }
5126
5127 if (l2_packet_send(bss->drv->l2, addr, ETH_P_EAPOL, data, data_len) <
5128 0)
5129 return -1;
5130 return 0;
5131 }
5132 #endif /* CONFIG_AP */
5133
5134
5135 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
5136
5137 static int wpa_driver_nl80211_hapd_send_eapol(
5138 void *priv, const u8 *addr, const u8 *data,
5139 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
5140 {
5141 struct i802_bss *bss = priv;
5142 struct wpa_driver_nl80211_data *drv = bss->drv;
5143 struct ieee80211_hdr *hdr;
5144 size_t len;
5145 u8 *pos;
5146 int res;
5147 int qos = flags & WPA_STA_WMM;
5148
5149 #ifdef CONFIG_AP
5150 if (drv->no_monitor_iface_capab)
5151 return nl80211_send_eapol_data(bss, addr, data, data_len,
5152 own_addr);
5153 #endif /* CONFIG_AP */
5154
5155 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
5156 data_len;
5157 hdr = os_zalloc(len);
5158 if (hdr == NULL) {
5159 printf("malloc() failed for i802_send_data(len=%lu)\n",
5160 (unsigned long) len);
5161 return -1;
5162 }
5163
5164 hdr->frame_control =
5165 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
5166 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
5167 if (encrypt)
5168 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
5169 if (qos) {
5170 hdr->frame_control |=
5171 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
5172 }
5173
5174 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
5175 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
5176 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
5177 pos = (u8 *) (hdr + 1);
5178
5179 if (qos) {
5180 /* add an empty QoS header if needed */
5181 pos[0] = 0;
5182 pos[1] = 0;
5183 pos += 2;
5184 }
5185
5186 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5187 pos += sizeof(rfc1042_header);
5188 WPA_PUT_BE16(pos, ETH_P_PAE);
5189 pos += 2;
5190 memcpy(pos, data, data_len);
5191
5192 res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
5193 if (res < 0) {
5194 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
5195 "failed: %d (%s)",
5196 (unsigned long) len, errno, strerror(errno));
5197 }
5198 os_free(hdr);
5199
5200 return res;
5201 }
5202
5203
5204 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
5205 int total_flags,
5206 int flags_or, int flags_and)
5207 {
5208 struct i802_bss *bss = priv;
5209 struct wpa_driver_nl80211_data *drv = bss->drv;
5210 struct nl_msg *msg, *flags = NULL;
5211 struct nl80211_sta_flag_update upd;
5212
5213 msg = nlmsg_alloc();
5214 if (!msg)
5215 return -ENOMEM;
5216
5217 flags = nlmsg_alloc();
5218 if (!flags) {
5219 nlmsg_free(msg);
5220 return -ENOMEM;
5221 }
5222
5223 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
5224
5225 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5226 if_nametoindex(bss->ifname));
5227 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5228
5229 /*
5230 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5231 * can be removed eventually.
5232 */
5233 if (total_flags & WPA_STA_AUTHORIZED)
5234 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
5235
5236 if (total_flags & WPA_STA_WMM)
5237 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
5238
5239 if (total_flags & WPA_STA_SHORT_PREAMBLE)
5240 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
5241
5242 if (total_flags & WPA_STA_MFP)
5243 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
5244
5245 if (total_flags & WPA_STA_TDLS_PEER)
5246 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
5247
5248 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
5249 goto nla_put_failure;
5250
5251 os_memset(&upd, 0, sizeof(upd));
5252 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5253 upd.set = sta_flags_nl80211(flags_or);
5254 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5255
5256 nlmsg_free(flags);
5257
5258 return send_and_recv_msgs(drv, msg, NULL, NULL);
5259 nla_put_failure:
5260 nlmsg_free(flags);
5261 return -ENOBUFS;
5262 }
5263
5264
5265 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5266 struct wpa_driver_associate_params *params)
5267 {
5268 enum nl80211_iftype nlmode;
5269
5270 if (params->p2p) {
5271 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5272 "group (GO)");
5273 nlmode = NL80211_IFTYPE_P2P_GO;
5274 } else
5275 nlmode = NL80211_IFTYPE_AP;
5276
5277 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
5278 wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
5279 nl80211_remove_monitor_interface(drv);
5280 return -1;
5281 }
5282
5283 if (drv->no_monitor_iface_capab) {
5284 if (wpa_driver_nl80211_probe_req_report(&drv->first_bss, 1) < 0)
5285 {
5286 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
5287 "Probe Request frame reporting in AP mode");
5288 /* Try to survive without this */
5289 }
5290 }
5291
5292 drv->ap_oper_freq = params->freq;
5293
5294 return 0;
5295 }
5296
5297
5298 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
5299 {
5300 struct nl_msg *msg;
5301 int ret = -1;
5302
5303 msg = nlmsg_alloc();
5304 if (!msg)
5305 return -1;
5306
5307 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5308 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5309 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5310 msg = NULL;
5311 if (ret) {
5312 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5313 "(%s)", ret, strerror(-ret));
5314 goto nla_put_failure;
5315 }
5316
5317 ret = 0;
5318 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
5319
5320 nla_put_failure:
5321 nlmsg_free(msg);
5322 return ret;
5323 }
5324
5325
5326 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5327 struct wpa_driver_associate_params *params)
5328 {
5329 struct nl_msg *msg;
5330 int ret = -1;
5331 int count = 0;
5332
5333 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5334
5335 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
5336 NL80211_IFTYPE_ADHOC)) {
5337 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5338 "IBSS mode");
5339 return -1;
5340 }
5341
5342 retry:
5343 msg = nlmsg_alloc();
5344 if (!msg)
5345 return -1;
5346
5347 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5348 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5349
5350 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5351 goto nla_put_failure;
5352
5353 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5354 params->ssid, params->ssid_len);
5355 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5356 params->ssid);
5357 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5358 drv->ssid_len = params->ssid_len;
5359
5360 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5361 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5362
5363 ret = nl80211_set_conn_keys(params, msg);
5364 if (ret)
5365 goto nla_put_failure;
5366
5367 if (params->wpa_ie) {
5368 wpa_hexdump(MSG_DEBUG,
5369 " * Extra IEs for Beacon/Probe Response frames",
5370 params->wpa_ie, params->wpa_ie_len);
5371 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5372 params->wpa_ie);
5373 }
5374
5375 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5376 msg = NULL;
5377 if (ret) {
5378 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5379 ret, strerror(-ret));
5380 count++;
5381 if (ret == -EALREADY && count == 1) {
5382 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5383 "forced leave");
5384 nl80211_leave_ibss(drv);
5385 nlmsg_free(msg);
5386 goto retry;
5387 }
5388
5389 goto nla_put_failure;
5390 }
5391 ret = 0;
5392 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
5393
5394 nla_put_failure:
5395 nlmsg_free(msg);
5396 return ret;
5397 }
5398
5399
5400 static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
5401 u8 *bssid)
5402 {
5403 struct nl_msg *msg;
5404 int ret;
5405 struct nl80211_bss_info_arg arg;
5406
5407 os_memset(&arg, 0, sizeof(arg));
5408 msg = nlmsg_alloc();
5409 if (!msg)
5410 goto nla_put_failure;
5411
5412 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
5413 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5414
5415 arg.drv = drv;
5416 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5417 msg = NULL;
5418 if (ret == 0) {
5419 if (is_zero_ether_addr(arg.assoc_bssid))
5420 return -ENOTCONN;
5421 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
5422 return 0;
5423 }
5424 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5425 "(%s)", ret, strerror(-ret));
5426 nla_put_failure:
5427 nlmsg_free(msg);
5428 return drv->assoc_freq;
5429 }
5430
5431
5432 static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
5433 const u8 *bssid)
5434 {
5435 u8 addr[ETH_ALEN];
5436
5437 if (bssid == NULL) {
5438 int res = nl80211_get_assoc_bssid(drv, addr);
5439 if (res)
5440 return res;
5441 bssid = addr;
5442 }
5443
5444 return wpa_driver_nl80211_disconnect(drv, bssid,
5445 WLAN_REASON_PREV_AUTH_NOT_VALID);
5446 }
5447
5448
5449 static int wpa_driver_nl80211_connect(
5450 struct wpa_driver_nl80211_data *drv,
5451 struct wpa_driver_associate_params *params)
5452 {
5453 struct nl_msg *msg;
5454 enum nl80211_auth_type type;
5455 int ret = 0;
5456 int algs;
5457
5458 msg = nlmsg_alloc();
5459 if (!msg)
5460 return -1;
5461
5462 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5463 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
5464
5465 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5466 if (params->bssid) {
5467 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5468 MAC2STR(params->bssid));
5469 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5470 }
5471 if (params->freq) {
5472 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5473 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5474 }
5475 if (params->ssid) {
5476 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5477 params->ssid, params->ssid_len);
5478 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5479 params->ssid);
5480 if (params->ssid_len > sizeof(drv->ssid))
5481 goto nla_put_failure;
5482 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5483 drv->ssid_len = params->ssid_len;
5484 }
5485 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
5486 if (params->wpa_ie)
5487 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5488 params->wpa_ie);
5489
5490 algs = 0;
5491 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5492 algs++;
5493 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5494 algs++;
5495 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5496 algs++;
5497 if (algs > 1) {
5498 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5499 "selection");
5500 goto skip_auth_type;
5501 }
5502
5503 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5504 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5505 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5506 type = NL80211_AUTHTYPE_SHARED_KEY;
5507 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5508 type = NL80211_AUTHTYPE_NETWORK_EAP;
5509 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5510 type = NL80211_AUTHTYPE_FT;
5511 else
5512 goto nla_put_failure;
5513
5514 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5515 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5516
5517 skip_auth_type:
5518 if (params->wpa_proto) {
5519 enum nl80211_wpa_versions ver = 0;
5520
5521 if (params->wpa_proto & WPA_PROTO_WPA)
5522 ver |= NL80211_WPA_VERSION_1;
5523 if (params->wpa_proto & WPA_PROTO_RSN)
5524 ver |= NL80211_WPA_VERSION_2;
5525
5526 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
5527 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5528 }
5529
5530 if (params->pairwise_suite != CIPHER_NONE) {
5531 int cipher;
5532
5533 switch (params->pairwise_suite) {
5534 case CIPHER_WEP40:
5535 cipher = WLAN_CIPHER_SUITE_WEP40;
5536 break;
5537 case CIPHER_WEP104:
5538 cipher = WLAN_CIPHER_SUITE_WEP104;
5539 break;
5540 case CIPHER_CCMP:
5541 cipher = WLAN_CIPHER_SUITE_CCMP;
5542 break;
5543 case CIPHER_TKIP:
5544 default:
5545 cipher = WLAN_CIPHER_SUITE_TKIP;
5546 break;
5547 }
5548 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
5549 }
5550
5551 if (params->group_suite != CIPHER_NONE) {
5552 int cipher;
5553
5554 switch (params->group_suite) {
5555 case CIPHER_WEP40:
5556 cipher = WLAN_CIPHER_SUITE_WEP40;
5557 break;
5558 case CIPHER_WEP104:
5559 cipher = WLAN_CIPHER_SUITE_WEP104;
5560 break;
5561 case CIPHER_CCMP:
5562 cipher = WLAN_CIPHER_SUITE_CCMP;
5563 break;
5564 case CIPHER_TKIP:
5565 default:
5566 cipher = WLAN_CIPHER_SUITE_TKIP;
5567 break;
5568 }
5569 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
5570 }
5571
5572 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
5573 params->key_mgmt_suite == KEY_MGMT_PSK) {
5574 int mgmt = WLAN_AKM_SUITE_PSK;
5575
5576 switch (params->key_mgmt_suite) {
5577 case KEY_MGMT_802_1X:
5578 mgmt = WLAN_AKM_SUITE_8021X;
5579 break;
5580 case KEY_MGMT_PSK:
5581 default:
5582 mgmt = WLAN_AKM_SUITE_PSK;
5583 break;
5584 }
5585 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
5586 }
5587
5588 ret = nl80211_set_conn_keys(params, msg);
5589 if (ret)
5590 goto nla_put_failure;
5591
5592 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5593 msg = NULL;
5594 if (ret) {
5595 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5596 "(%s)", ret, strerror(-ret));
5597 /*
5598 * cfg80211 does not currently accept new connection if we are
5599 * already connected. As a workaround, force disconnection and
5600 * try again once the driver indicates it completed
5601 * disconnection.
5602 */
5603 if (ret == -EALREADY)
5604 nl80211_disconnect(drv, params->bssid);
5605 goto nla_put_failure;
5606 }
5607 ret = 0;
5608 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
5609
5610 nla_put_failure:
5611 nlmsg_free(msg);
5612 return ret;
5613
5614 }
5615
5616
5617 static int wpa_driver_nl80211_associate(
5618 void *priv, struct wpa_driver_associate_params *params)
5619 {
5620 struct i802_bss *bss = priv;
5621 struct wpa_driver_nl80211_data *drv = bss->drv;
5622 int ret = -1;
5623 struct nl_msg *msg;
5624
5625 if (params->mode == IEEE80211_MODE_AP)
5626 return wpa_driver_nl80211_ap(drv, params);
5627
5628 if (params->mode == IEEE80211_MODE_IBSS)
5629 return wpa_driver_nl80211_ibss(drv, params);
5630
5631 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
5632 enum nl80211_iftype nlmode = params->p2p ?
5633 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5634
5635 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
5636 return -1;
5637 return wpa_driver_nl80211_connect(drv, params);
5638 }
5639
5640 drv->associated = 0;
5641
5642 msg = nlmsg_alloc();
5643 if (!msg)
5644 return -1;
5645
5646 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5647 drv->ifindex);
5648 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
5649
5650 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5651 if (params->bssid) {
5652 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5653 MAC2STR(params->bssid));
5654 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5655 }
5656 if (params->freq) {
5657 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5658 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5659 drv->assoc_freq = params->freq;
5660 } else
5661 drv->assoc_freq = 0;
5662 if (params->ssid) {
5663 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5664 params->ssid, params->ssid_len);
5665 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5666 params->ssid);
5667 if (params->ssid_len > sizeof(drv->ssid))
5668 goto nla_put_failure;
5669 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5670 drv->ssid_len = params->ssid_len;
5671 }
5672 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
5673 if (params->wpa_ie)
5674 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5675 params->wpa_ie);
5676
5677 if (params->pairwise_suite != CIPHER_NONE) {
5678 int cipher;
5679
5680 switch (params->pairwise_suite) {
5681 case CIPHER_WEP40:
5682 cipher = WLAN_CIPHER_SUITE_WEP40;
5683 break;
5684 case CIPHER_WEP104:
5685 cipher = WLAN_CIPHER_SUITE_WEP104;
5686 break;
5687 case CIPHER_CCMP:
5688 cipher = WLAN_CIPHER_SUITE_CCMP;
5689 break;
5690 case CIPHER_TKIP:
5691 default:
5692 cipher = WLAN_CIPHER_SUITE_TKIP;
5693 break;
5694 }
5695 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
5696 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
5697 }
5698
5699 if (params->group_suite != CIPHER_NONE) {
5700 int cipher;
5701
5702 switch (params->group_suite) {
5703 case CIPHER_WEP40:
5704 cipher = WLAN_CIPHER_SUITE_WEP40;
5705 break;
5706 case CIPHER_WEP104:
5707 cipher = WLAN_CIPHER_SUITE_WEP104;
5708 break;
5709 case CIPHER_CCMP:
5710 cipher = WLAN_CIPHER_SUITE_CCMP;
5711 break;
5712 case CIPHER_TKIP:
5713 default:
5714 cipher = WLAN_CIPHER_SUITE_TKIP;
5715 break;
5716 }
5717 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
5718 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
5719 }
5720
5721 #ifdef CONFIG_IEEE80211W
5722 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
5723 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
5724 #endif /* CONFIG_IEEE80211W */
5725
5726 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
5727
5728 if (params->prev_bssid) {
5729 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5730 MAC2STR(params->prev_bssid));
5731 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5732 params->prev_bssid);
5733 }
5734
5735 if (params->p2p)
5736 wpa_printf(MSG_DEBUG, " * P2P group");
5737
5738 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5739 msg = NULL;
5740 if (ret) {
5741 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
5742 "(%s)", ret, strerror(-ret));
5743 nl80211_dump_scan(drv);
5744 goto nla_put_failure;
5745 }
5746 ret = 0;
5747 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
5748 "successfully");
5749
5750 nla_put_failure:
5751 nlmsg_free(msg);
5752 return ret;
5753 }
5754
5755
5756 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
5757 int ifindex, enum nl80211_iftype mode)
5758 {
5759 struct nl_msg *msg;
5760 int ret = -ENOBUFS;
5761
5762 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5763 ifindex, mode, nl80211_iftype_str(mode));
5764
5765 msg = nlmsg_alloc();
5766 if (!msg)
5767 return -ENOMEM;
5768
5769 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
5770 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5771 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
5772
5773 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5774 if (!ret)
5775 return 0;
5776 nla_put_failure:
5777 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5778 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5779 return ret;
5780 }
5781
5782
5783 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5784 enum nl80211_iftype nlmode)
5785 {
5786 struct wpa_driver_nl80211_data *drv = bss->drv;
5787 int ret = -1;
5788 int i;
5789 int was_ap = is_ap_interface(drv->nlmode);
5790
5791 if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
5792 drv->nlmode = nlmode;
5793 ret = 0;
5794 goto done;
5795 }
5796
5797 if (nlmode == drv->nlmode) {
5798 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5799 "requested mode - ignore error");
5800 ret = 0;
5801 goto done; /* Already in the requested mode */
5802 }
5803
5804 /* mac80211 doesn't allow mode changes while the device is up, so
5805 * take the device down, try to set the mode again, and bring the
5806 * device back up.
5807 */
5808 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5809 "interface down");
5810 for (i = 0; i < 10; i++) {
5811 int res;
5812 res = linux_set_iface_flags(drv->global->ioctl_sock,
5813 bss->ifname, 0);
5814 if (res == -EACCES || res == -ENODEV)
5815 break;
5816 if (res == 0) {
5817 /* Try to set the mode again while the interface is
5818 * down */
5819 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
5820 if (ret == -EACCES)
5821 break;
5822 res = linux_set_iface_flags(drv->global->ioctl_sock,
5823 bss->ifname, 1);
5824 if (res && !ret)
5825 ret = -1;
5826 else if (ret != -EBUSY)
5827 break;
5828 } else
5829 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5830 "interface down");
5831 os_sleep(0, 100000);
5832 }
5833
5834 if (!ret) {
5835 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5836 "interface is down");
5837 drv->nlmode = nlmode;
5838 drv->ignore_if_down_event = 1;
5839 }
5840
5841 done:
5842 if (!ret && is_ap_interface(nlmode)) {
5843 /* Setup additional AP mode functionality if needed */
5844 if (!drv->no_monitor_iface_capab && drv->monitor_ifidx < 0 &&
5845 nl80211_create_monitor_interface(drv) &&
5846 !drv->no_monitor_iface_capab)
5847 return -1;
5848 } else if (!ret && !is_ap_interface(nlmode)) {
5849 /* Remove additional AP mode functionality */
5850 if (was_ap && drv->no_monitor_iface_capab)
5851 wpa_driver_nl80211_probe_req_report(bss, 0);
5852 nl80211_remove_monitor_interface(drv);
5853 bss->beacon_set = 0;
5854 }
5855
5856 if (!ret && is_p2p_interface(drv->nlmode)) {
5857 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
5858 drv->disabled_11b_rates = 1;
5859 } else if (!ret && drv->disabled_11b_rates) {
5860 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
5861 drv->disabled_11b_rates = 0;
5862 }
5863
5864 if (ret)
5865 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5866 "from %d failed", nlmode, drv->nlmode);
5867
5868 return ret;
5869 }
5870
5871
5872 static int wpa_driver_nl80211_get_capa(void *priv,
5873 struct wpa_driver_capa *capa)
5874 {
5875 struct i802_bss *bss = priv;
5876 struct wpa_driver_nl80211_data *drv = bss->drv;
5877 if (!drv->has_capability)
5878 return -1;
5879 os_memcpy(capa, &drv->capa, sizeof(*capa));
5880 return 0;
5881 }
5882
5883
5884 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5885 {
5886 struct i802_bss *bss = priv;
5887 struct wpa_driver_nl80211_data *drv = bss->drv;
5888
5889 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
5890 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
5891 drv->operstate = state;
5892 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
5893 state ? IF_OPER_UP : IF_OPER_DORMANT);
5894 }
5895
5896
5897 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5898 {
5899 struct i802_bss *bss = priv;
5900 struct wpa_driver_nl80211_data *drv = bss->drv;
5901 struct nl_msg *msg;
5902 struct nl80211_sta_flag_update upd;
5903
5904 msg = nlmsg_alloc();
5905 if (!msg)
5906 return -ENOMEM;
5907
5908 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
5909
5910 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5911 if_nametoindex(bss->ifname));
5912 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
5913
5914 os_memset(&upd, 0, sizeof(upd));
5915 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5916 if (authorized)
5917 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
5918 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5919
5920 return send_and_recv_msgs(drv, msg, NULL, NULL);
5921 nla_put_failure:
5922 return -ENOBUFS;
5923 }
5924
5925
5926 /* Set kernel driver on given frequency (MHz) */
5927 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
5928 {
5929 struct i802_bss *bss = priv;
5930 struct wpa_driver_nl80211_data *drv = bss->drv;
5931 return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
5932 freq->sec_channel_offset);
5933 }
5934
5935
5936 #if defined(HOSTAPD) || defined(CONFIG_AP)
5937
5938 static inline int min_int(int a, int b)
5939 {
5940 if (a < b)
5941 return a;
5942 return b;
5943 }
5944
5945
5946 static int get_key_handler(struct nl_msg *msg, void *arg)
5947 {
5948 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5949 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5950
5951 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5952 genlmsg_attrlen(gnlh, 0), NULL);
5953
5954 /*
5955 * TODO: validate the key index and mac address!
5956 * Otherwise, there's a race condition as soon as
5957 * the kernel starts sending key notifications.
5958 */
5959
5960 if (tb[NL80211_ATTR_KEY_SEQ])
5961 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5962 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5963 return NL_SKIP;
5964 }
5965
5966
5967 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5968 int idx, u8 *seq)
5969 {
5970 struct i802_bss *bss = priv;
5971 struct wpa_driver_nl80211_data *drv = bss->drv;
5972 struct nl_msg *msg;
5973
5974 msg = nlmsg_alloc();
5975 if (!msg)
5976 return -ENOMEM;
5977
5978 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
5979
5980 if (addr)
5981 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5982 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
5983 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
5984
5985 memset(seq, 0, 6);
5986
5987 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
5988 nla_put_failure:
5989 return -ENOBUFS;
5990 }
5991
5992
5993 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
5994 int mode)
5995 {
5996 struct i802_bss *bss = priv;
5997 struct wpa_driver_nl80211_data *drv = bss->drv;
5998 struct nl_msg *msg;
5999 u8 rates[NL80211_MAX_SUPP_RATES];
6000 u8 rates_len = 0;
6001 int i;
6002
6003 msg = nlmsg_alloc();
6004 if (!msg)
6005 return -ENOMEM;
6006
6007 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6008
6009 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
6010 rates[rates_len++] = basic_rates[i] / 5;
6011
6012 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6013
6014 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6015
6016 return send_and_recv_msgs(drv, msg, NULL, NULL);
6017 nla_put_failure:
6018 return -ENOBUFS;
6019 }
6020
6021
6022 static int i802_set_rts(void *priv, int rts)
6023 {
6024 struct i802_bss *bss = priv;
6025 struct wpa_driver_nl80211_data *drv = bss->drv;
6026 struct nl_msg *msg;
6027 int ret = -ENOBUFS;
6028 u32 val;
6029
6030 msg = nlmsg_alloc();
6031 if (!msg)
6032 return -ENOMEM;
6033
6034 if (rts >= 2347)
6035 val = (u32) -1;
6036 else
6037 val = rts;
6038
6039 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6040 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6041 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
6042
6043 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6044 if (!ret)
6045 return 0;
6046 nla_put_failure:
6047 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
6048 "%d (%s)", rts, ret, strerror(-ret));
6049 return ret;
6050 }
6051
6052
6053 static int i802_set_frag(void *priv, int frag)
6054 {
6055 struct i802_bss *bss = priv;
6056 struct wpa_driver_nl80211_data *drv = bss->drv;
6057 struct nl_msg *msg;
6058 int ret = -ENOBUFS;
6059 u32 val;
6060
6061 msg = nlmsg_alloc();
6062 if (!msg)
6063 return -ENOMEM;
6064
6065 if (frag >= 2346)
6066 val = (u32) -1;
6067 else
6068 val = frag;
6069
6070 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6071 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6072 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
6073
6074 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6075 if (!ret)
6076 return 0;
6077 nla_put_failure:
6078 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
6079 "%d: %d (%s)", frag, ret, strerror(-ret));
6080 return ret;
6081 }
6082
6083
6084 static int i802_flush(void *priv)
6085 {
6086 struct i802_bss *bss = priv;
6087 struct wpa_driver_nl80211_data *drv = bss->drv;
6088 struct nl_msg *msg;
6089
6090 msg = nlmsg_alloc();
6091 if (!msg)
6092 return -1;
6093
6094 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
6095
6096 /*
6097 * XXX: FIX! this needs to flush all VLANs too
6098 */
6099 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6100 if_nametoindex(bss->ifname));
6101
6102 return send_and_recv_msgs(drv, msg, NULL, NULL);
6103 nla_put_failure:
6104 return -ENOBUFS;
6105 }
6106
6107
6108 static int get_sta_handler(struct nl_msg *msg, void *arg)
6109 {
6110 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6111 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6112 struct hostap_sta_driver_data *data = arg;
6113 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
6114 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
6115 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
6116 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
6117 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
6118 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
6119 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
6120 };
6121
6122 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6123 genlmsg_attrlen(gnlh, 0), NULL);
6124
6125 /*
6126 * TODO: validate the interface and mac address!
6127 * Otherwise, there's a race condition as soon as
6128 * the kernel starts sending station notifications.
6129 */
6130
6131 if (!tb[NL80211_ATTR_STA_INFO]) {
6132 wpa_printf(MSG_DEBUG, "sta stats missing!");
6133 return NL_SKIP;
6134 }
6135 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6136 tb[NL80211_ATTR_STA_INFO],
6137 stats_policy)) {
6138 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6139 return NL_SKIP;
6140 }
6141
6142 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6143 data->inactive_msec =
6144 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
6145 if (stats[NL80211_STA_INFO_RX_BYTES])
6146 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6147 if (stats[NL80211_STA_INFO_TX_BYTES])
6148 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
6149 if (stats[NL80211_STA_INFO_RX_PACKETS])
6150 data->rx_packets =
6151 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6152 if (stats[NL80211_STA_INFO_TX_PACKETS])
6153 data->tx_packets =
6154 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
6155
6156 return NL_SKIP;
6157 }
6158
6159 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
6160 const u8 *addr)
6161 {
6162 struct i802_bss *bss = priv;
6163 struct wpa_driver_nl80211_data *drv = bss->drv;
6164 struct nl_msg *msg;
6165
6166 os_memset(data, 0, sizeof(*data));
6167 msg = nlmsg_alloc();
6168 if (!msg)
6169 return -ENOMEM;
6170
6171 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
6172
6173 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6174 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6175
6176 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
6177 nla_put_failure:
6178 return -ENOBUFS;
6179 }
6180
6181
6182 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6183 int cw_min, int cw_max, int burst_time)
6184 {
6185 struct i802_bss *bss = priv;
6186 struct wpa_driver_nl80211_data *drv = bss->drv;
6187 struct nl_msg *msg;
6188 struct nlattr *txq, *params;
6189
6190 msg = nlmsg_alloc();
6191 if (!msg)
6192 return -1;
6193
6194 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6195
6196 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6197
6198 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6199 if (!txq)
6200 goto nla_put_failure;
6201
6202 /* We are only sending parameters for a single TXQ at a time */
6203 params = nla_nest_start(msg, 1);
6204 if (!params)
6205 goto nla_put_failure;
6206
6207 switch (queue) {
6208 case 0:
6209 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
6210 break;
6211 case 1:
6212 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
6213 break;
6214 case 2:
6215 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
6216 break;
6217 case 3:
6218 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
6219 break;
6220 }
6221 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6222 * 32 usec, so need to convert the value here. */
6223 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
6224 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
6225 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
6226 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
6227
6228 nla_nest_end(msg, params);
6229
6230 nla_nest_end(msg, txq);
6231
6232 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6233 return 0;
6234 nla_put_failure:
6235 return -1;
6236 }
6237
6238
6239 static int i802_set_sta_vlan(void *priv, const u8 *addr,
6240 const char *ifname, int vlan_id)
6241 {
6242 struct i802_bss *bss = priv;
6243 struct wpa_driver_nl80211_data *drv = bss->drv;
6244 struct nl_msg *msg;
6245 int ret = -ENOBUFS;
6246
6247 msg = nlmsg_alloc();
6248 if (!msg)
6249 return -ENOMEM;
6250
6251 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
6252
6253 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6254 if_nametoindex(bss->ifname));
6255 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6256 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
6257 if_nametoindex(ifname));
6258
6259 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6260 if (ret < 0) {
6261 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6262 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6263 MAC2STR(addr), ifname, vlan_id, ret,
6264 strerror(-ret));
6265 }
6266 nla_put_failure:
6267 return ret;
6268 }
6269
6270
6271 static int i802_get_inact_sec(void *priv, const u8 *addr)
6272 {
6273 struct hostap_sta_driver_data data;
6274 int ret;
6275
6276 data.inactive_msec = (unsigned long) -1;
6277 ret = i802_read_sta_data(priv, &data, addr);
6278 if (ret || data.inactive_msec == (unsigned long) -1)
6279 return -1;
6280 return data.inactive_msec / 1000;
6281 }
6282
6283
6284 static int i802_sta_clear_stats(void *priv, const u8 *addr)
6285 {
6286 #if 0
6287 /* TODO */
6288 #endif
6289 return 0;
6290 }
6291
6292
6293 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6294 int reason)
6295 {
6296 struct i802_bss *bss = priv;
6297 struct ieee80211_mgmt mgmt;
6298
6299 memset(&mgmt, 0, sizeof(mgmt));
6300 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6301 WLAN_FC_STYPE_DEAUTH);
6302 memcpy(mgmt.da, addr, ETH_ALEN);
6303 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6304 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6305 mgmt.u.deauth.reason_code = host_to_le16(reason);
6306 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6307 IEEE80211_HDRLEN +
6308 sizeof(mgmt.u.deauth));
6309 }
6310
6311
6312 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6313 int reason)
6314 {
6315 struct i802_bss *bss = priv;
6316 struct ieee80211_mgmt mgmt;
6317
6318 memset(&mgmt, 0, sizeof(mgmt));
6319 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6320 WLAN_FC_STYPE_DISASSOC);
6321 memcpy(mgmt.da, addr, ETH_ALEN);
6322 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6323 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6324 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6325 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6326 IEEE80211_HDRLEN +
6327 sizeof(mgmt.u.disassoc));
6328 }
6329
6330 #endif /* HOSTAPD || CONFIG_AP */
6331
6332 #ifdef HOSTAPD
6333
6334 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
6335 {
6336 int i;
6337 int *old;
6338
6339 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
6340 ifidx);
6341 for (i = 0; i < drv->num_if_indices; i++) {
6342 if (drv->if_indices[i] == 0) {
6343 drv->if_indices[i] = ifidx;
6344 return;
6345 }
6346 }
6347
6348 if (drv->if_indices != drv->default_if_indices)
6349 old = drv->if_indices;
6350 else
6351 old = NULL;
6352
6353 drv->if_indices = os_realloc(old,
6354 sizeof(int) * (drv->num_if_indices + 1));
6355 if (!drv->if_indices) {
6356 if (!old)
6357 drv->if_indices = drv->default_if_indices;
6358 else
6359 drv->if_indices = old;
6360 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6361 "interfaces");
6362 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6363 return;
6364 } else if (!old)
6365 os_memcpy(drv->if_indices, drv->default_if_indices,
6366 sizeof(drv->default_if_indices));
6367 drv->if_indices[drv->num_if_indices] = ifidx;
6368 drv->num_if_indices++;
6369 }
6370
6371
6372 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
6373 {
6374 int i;
6375
6376 for (i = 0; i < drv->num_if_indices; i++) {
6377 if (drv->if_indices[i] == ifidx) {
6378 drv->if_indices[i] = 0;
6379 break;
6380 }
6381 }
6382 }
6383
6384
6385 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
6386 {
6387 int i;
6388
6389 for (i = 0; i < drv->num_if_indices; i++)
6390 if (drv->if_indices[i] == ifidx)
6391 return 1;
6392
6393 return 0;
6394 }
6395
6396
6397 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
6398 const char *bridge_ifname)
6399 {
6400 struct i802_bss *bss = priv;
6401 struct wpa_driver_nl80211_data *drv = bss->drv;
6402 char name[IFNAMSIZ + 1];
6403
6404 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
6405 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6406 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6407 if (val) {
6408 if (!if_nametoindex(name)) {
6409 if (nl80211_create_iface(drv, name,
6410 NL80211_IFTYPE_AP_VLAN,
6411 NULL, 1) < 0)
6412 return -1;
6413 if (bridge_ifname &&
6414 linux_br_add_if(drv->global->ioctl_sock,
6415 bridge_ifname, name) < 0)
6416 return -1;
6417 }
6418 linux_set_iface_flags(drv->global->ioctl_sock, name, 1);
6419 return i802_set_sta_vlan(priv, addr, name, 0);
6420 } else {
6421 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
6422 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
6423 name);
6424 }
6425 }
6426
6427
6428 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6429 {
6430 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6431 struct sockaddr_ll lladdr;
6432 unsigned char buf[3000];
6433 int len;
6434 socklen_t fromlen = sizeof(lladdr);
6435
6436 len = recvfrom(sock, buf, sizeof(buf), 0,
6437 (struct sockaddr *)&lladdr, &fromlen);
6438 if (len < 0) {
6439 perror("recv");
6440 return;
6441 }
6442
6443 if (have_ifidx(drv, lladdr.sll_ifindex))
6444 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6445 }
6446
6447
6448 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6449 struct i802_bss *bss,
6450 const char *brname, const char *ifname)
6451 {
6452 int ifindex;
6453 char in_br[IFNAMSIZ];
6454
6455 os_strlcpy(bss->brname, brname, IFNAMSIZ);
6456 ifindex = if_nametoindex(brname);
6457 if (ifindex == 0) {
6458 /*
6459 * Bridge was configured, but the bridge device does
6460 * not exist. Try to add it now.
6461 */
6462 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
6463 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6464 "bridge interface %s: %s",
6465 brname, strerror(errno));
6466 return -1;
6467 }
6468 bss->added_bridge = 1;
6469 add_ifidx(drv, if_nametoindex(brname));
6470 }
6471
6472 if (linux_br_get(in_br, ifname) == 0) {
6473 if (os_strcmp(in_br, brname) == 0)
6474 return 0; /* already in the bridge */
6475
6476 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6477 "bridge %s", ifname, in_br);
6478 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6479 0) {
6480 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6481 "remove interface %s from bridge "
6482 "%s: %s",
6483 ifname, brname, strerror(errno));
6484 return -1;
6485 }
6486 }
6487
6488 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6489 ifname, brname);
6490 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
6491 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6492 "into bridge %s: %s",
6493 ifname, brname, strerror(errno));
6494 return -1;
6495 }
6496 bss->added_if_into_bridge = 1;
6497
6498 return 0;
6499 }
6500
6501
6502 static void *i802_init(struct hostapd_data *hapd,
6503 struct wpa_init_params *params)
6504 {
6505 struct wpa_driver_nl80211_data *drv;
6506 struct i802_bss *bss;
6507 size_t i;
6508 char brname[IFNAMSIZ];
6509 int ifindex, br_ifindex;
6510 int br_added = 0;
6511
6512 bss = wpa_driver_nl80211_init(hapd, params->ifname,
6513 params->global_priv);
6514 if (bss == NULL)
6515 return NULL;
6516
6517 drv = bss->drv;
6518 drv->nlmode = NL80211_IFTYPE_AP;
6519 drv->eapol_sock = -1;
6520
6521 if (linux_br_get(brname, params->ifname) == 0) {
6522 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
6523 params->ifname, brname);
6524 br_ifindex = if_nametoindex(brname);
6525 } else {
6526 brname[0] = '\0';
6527 br_ifindex = 0;
6528 }
6529
6530 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
6531 drv->if_indices = drv->default_if_indices;
6532 for (i = 0; i < params->num_bridge; i++) {
6533 if (params->bridge[i]) {
6534 ifindex = if_nametoindex(params->bridge[i]);
6535 if (ifindex)
6536 add_ifidx(drv, ifindex);
6537 if (ifindex == br_ifindex)
6538 br_added = 1;
6539 }
6540 }
6541 if (!br_added && br_ifindex &&
6542 (params->num_bridge == 0 || !params->bridge[0]))
6543 add_ifidx(drv, br_ifindex);
6544
6545 /* start listening for EAPOL on the default AP interface */
6546 add_ifidx(drv, drv->ifindex);
6547
6548 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
6549 goto failed;
6550
6551 if (params->bssid) {
6552 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6553 params->bssid))
6554 goto failed;
6555 }
6556
6557 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
6558 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
6559 "into AP mode", bss->ifname);
6560 goto failed;
6561 }
6562
6563 if (params->num_bridge && params->bridge[0] &&
6564 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
6565 goto failed;
6566
6567 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
6568 goto failed;
6569
6570 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6571 if (drv->eapol_sock < 0) {
6572 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
6573 goto failed;
6574 }
6575
6576 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6577 {
6578 printf("Could not register read socket for eapol\n");
6579 goto failed;
6580 }
6581
6582 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6583 params->own_addr))
6584 goto failed;
6585
6586 return bss;
6587
6588 failed:
6589 wpa_driver_nl80211_deinit(bss);
6590 return NULL;
6591 }
6592
6593
6594 static void i802_deinit(void *priv)
6595 {
6596 wpa_driver_nl80211_deinit(priv);
6597 }
6598
6599 #endif /* HOSTAPD */
6600
6601
6602 static enum nl80211_iftype wpa_driver_nl80211_if_type(
6603 enum wpa_driver_if_type type)
6604 {
6605 switch (type) {
6606 case WPA_IF_STATION:
6607 return NL80211_IFTYPE_STATION;
6608 case WPA_IF_P2P_CLIENT:
6609 case WPA_IF_P2P_GROUP:
6610 return NL80211_IFTYPE_P2P_CLIENT;
6611 case WPA_IF_AP_VLAN:
6612 return NL80211_IFTYPE_AP_VLAN;
6613 case WPA_IF_AP_BSS:
6614 return NL80211_IFTYPE_AP;
6615 case WPA_IF_P2P_GO:
6616 return NL80211_IFTYPE_P2P_GO;
6617 }
6618 return -1;
6619 }
6620
6621
6622 #ifdef CONFIG_P2P
6623
6624 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6625 {
6626 struct wpa_driver_nl80211_data *drv;
6627 dl_list_for_each(drv, &global->interfaces,
6628 struct wpa_driver_nl80211_data, list) {
6629 if (os_memcmp(addr, drv->addr, ETH_ALEN) == 0)
6630 return 1;
6631 }
6632 return 0;
6633 }
6634
6635
6636 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
6637 u8 *new_addr)
6638 {
6639 unsigned int idx;
6640
6641 if (!drv->global)
6642 return -1;
6643
6644 os_memcpy(new_addr, drv->addr, ETH_ALEN);
6645 for (idx = 0; idx < 64; idx++) {
6646 new_addr[0] = drv->addr[0] | 0x02;
6647 new_addr[0] ^= idx << 2;
6648 if (!nl80211_addr_in_use(drv->global, new_addr))
6649 break;
6650 }
6651 if (idx == 64)
6652 return -1;
6653
6654 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
6655 MACSTR, MAC2STR(new_addr));
6656
6657 return 0;
6658 }
6659
6660 #endif /* CONFIG_P2P */
6661
6662
6663 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6664 const char *ifname, const u8 *addr,
6665 void *bss_ctx, void **drv_priv,
6666 char *force_ifname, u8 *if_addr,
6667 const char *bridge)
6668 {
6669 struct i802_bss *bss = priv;
6670 struct wpa_driver_nl80211_data *drv = bss->drv;
6671 int ifidx;
6672 #ifdef HOSTAPD
6673 struct i802_bss *new_bss = NULL;
6674
6675 if (type == WPA_IF_AP_BSS) {
6676 new_bss = os_zalloc(sizeof(*new_bss));
6677 if (new_bss == NULL)
6678 return -1;
6679 }
6680 #endif /* HOSTAPD */
6681
6682 if (addr)
6683 os_memcpy(if_addr, addr, ETH_ALEN);
6684 ifidx = nl80211_create_iface(drv, ifname,
6685 wpa_driver_nl80211_if_type(type), addr,
6686 0);
6687 if (ifidx < 0) {
6688 #ifdef HOSTAPD
6689 os_free(new_bss);
6690 #endif /* HOSTAPD */
6691 return -1;
6692 }
6693
6694 if (!addr &&
6695 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6696 if_addr) < 0) {
6697 nl80211_remove_iface(drv, ifidx);
6698 return -1;
6699 }
6700
6701 #ifdef CONFIG_P2P
6702 if (!addr &&
6703 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
6704 type == WPA_IF_P2P_GO)) {
6705 /* Enforce unique P2P Interface Address */
6706 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
6707
6708 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6709 own_addr) < 0 ||
6710 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
6711 new_addr) < 0) {
6712 nl80211_remove_iface(drv, ifidx);
6713 return -1;
6714 }
6715 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
6716 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
6717 "for P2P group interface");
6718 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
6719 nl80211_remove_iface(drv, ifidx);
6720 return -1;
6721 }
6722 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6723 new_addr) < 0) {
6724 nl80211_remove_iface(drv, ifidx);
6725 return -1;
6726 }
6727 }
6728 os_memcpy(if_addr, new_addr, ETH_ALEN);
6729 }
6730 #endif /* CONFIG_P2P */
6731
6732 #ifdef HOSTAPD
6733 if (bridge &&
6734 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6735 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6736 "interface %s to a bridge %s", ifname, bridge);
6737 nl80211_remove_iface(drv, ifidx);
6738 os_free(new_bss);
6739 return -1;
6740 }
6741
6742 if (type == WPA_IF_AP_BSS) {
6743 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6744 {
6745 nl80211_remove_iface(drv, ifidx);
6746 os_free(new_bss);
6747 return -1;
6748 }
6749 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
6750 new_bss->ifindex = ifidx;
6751 new_bss->drv = drv;
6752 new_bss->next = drv->first_bss.next;
6753 drv->first_bss.next = new_bss;
6754 if (drv_priv)
6755 *drv_priv = new_bss;
6756 }
6757 #endif /* HOSTAPD */
6758
6759 if (drv->global)
6760 drv->global->if_add_ifindex = ifidx;
6761
6762 return 0;
6763 }
6764
6765
6766 static int wpa_driver_nl80211_if_remove(void *priv,
6767 enum wpa_driver_if_type type,
6768 const char *ifname)
6769 {
6770 struct i802_bss *bss = priv;
6771 struct wpa_driver_nl80211_data *drv = bss->drv;
6772 int ifindex = if_nametoindex(ifname);
6773
6774 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
6775 __func__, type, ifname, ifindex);
6776 if (ifindex <= 0)
6777 return -1;
6778
6779 #ifdef HOSTAPD
6780 if (bss->added_if_into_bridge) {
6781 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6782 bss->ifname) < 0)
6783 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6784 "interface %s from bridge %s: %s",
6785 bss->ifname, bss->brname, strerror(errno));
6786 }
6787 if (bss->added_bridge) {
6788 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
6789 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6790 "bridge %s: %s",
6791 bss->brname, strerror(errno));
6792 }
6793 #endif /* HOSTAPD */
6794
6795 nl80211_remove_iface(drv, ifindex);
6796
6797 #ifdef HOSTAPD
6798 if (type != WPA_IF_AP_BSS)
6799 return 0;
6800
6801 if (bss != &drv->first_bss) {
6802 struct i802_bss *tbss;
6803
6804 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
6805 if (tbss->next == bss) {
6806 tbss->next = bss->next;
6807 os_free(bss);
6808 bss = NULL;
6809 break;
6810 }
6811 }
6812 if (bss)
6813 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6814 "BSS %p in the list", __func__, bss);
6815 }
6816 #endif /* HOSTAPD */
6817
6818 return 0;
6819 }
6820
6821
6822 static int cookie_handler(struct nl_msg *msg, void *arg)
6823 {
6824 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6825 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6826 u64 *cookie = arg;
6827 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6828 genlmsg_attrlen(gnlh, 0), NULL);
6829 if (tb[NL80211_ATTR_COOKIE])
6830 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6831 return NL_SKIP;
6832 }
6833
6834
6835 static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
6836 unsigned int freq, unsigned int wait,
6837 const u8 *buf, size_t buf_len,
6838 u64 *cookie_out, int no_cck)
6839 {
6840 struct nl_msg *msg;
6841 u64 cookie;
6842 int ret = -1;
6843
6844 msg = nlmsg_alloc();
6845 if (!msg)
6846 return -1;
6847
6848 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
6849
6850 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6851 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6852 if (wait)
6853 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
6854 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
6855 if (no_cck)
6856 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
6857
6858 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
6859
6860 cookie = 0;
6861 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6862 msg = NULL;
6863 if (ret) {
6864 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
6865 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6866 freq, wait);
6867 goto nla_put_failure;
6868 }
6869 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; "
6870 "cookie 0x%llx", (long long unsigned int) cookie);
6871
6872 if (cookie_out)
6873 *cookie_out = cookie;
6874
6875 nla_put_failure:
6876 nlmsg_free(msg);
6877 return ret;
6878 }
6879
6880
6881 static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
6882 unsigned int wait_time,
6883 const u8 *dst, const u8 *src,
6884 const u8 *bssid,
6885 const u8 *data, size_t data_len,
6886 int no_cck)
6887 {
6888 struct i802_bss *bss = priv;
6889 struct wpa_driver_nl80211_data *drv = bss->drv;
6890 int ret = -1;
6891 u8 *buf;
6892 struct ieee80211_hdr *hdr;
6893
6894 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
6895 "wait=%d ms no_cck=%d)", drv->ifindex, wait_time, no_cck);
6896
6897 buf = os_zalloc(24 + data_len);
6898 if (buf == NULL)
6899 return ret;
6900 os_memcpy(buf + 24, data, data_len);
6901 hdr = (struct ieee80211_hdr *) buf;
6902 hdr->frame_control =
6903 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6904 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6905 os_memcpy(hdr->addr2, src, ETH_ALEN);
6906 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6907
6908 if (is_ap_interface(drv->nlmode))
6909 ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
6910 else
6911 ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf,
6912 24 + data_len,
6913 &drv->send_action_cookie,
6914 no_cck);
6915
6916 os_free(buf);
6917 return ret;
6918 }
6919
6920
6921 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6922 {
6923 struct i802_bss *bss = priv;
6924 struct wpa_driver_nl80211_data *drv = bss->drv;
6925 struct nl_msg *msg;
6926 int ret;
6927
6928 msg = nlmsg_alloc();
6929 if (!msg)
6930 return;
6931
6932 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
6933
6934 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6935 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
6936
6937 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6938 msg = NULL;
6939 if (ret)
6940 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6941 "(%s)", ret, strerror(-ret));
6942
6943 nla_put_failure:
6944 nlmsg_free(msg);
6945 }
6946
6947
6948 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6949 unsigned int duration)
6950 {
6951 struct i802_bss *bss = priv;
6952 struct wpa_driver_nl80211_data *drv = bss->drv;
6953 struct nl_msg *msg;
6954 int ret;
6955 u64 cookie;
6956
6957 msg = nlmsg_alloc();
6958 if (!msg)
6959 return -1;
6960
6961 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
6962
6963 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6964 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6965 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6966
6967 cookie = 0;
6968 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6969 if (ret == 0) {
6970 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6971 "0x%llx for freq=%u MHz duration=%u",
6972 (long long unsigned int) cookie, freq, duration);
6973 drv->remain_on_chan_cookie = cookie;
6974 drv->pending_remain_on_chan = 1;
6975 return 0;
6976 }
6977 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6978 "(freq=%d duration=%u): %d (%s)",
6979 freq, duration, ret, strerror(-ret));
6980 nla_put_failure:
6981 return -1;
6982 }
6983
6984
6985 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6986 {
6987 struct i802_bss *bss = priv;
6988 struct wpa_driver_nl80211_data *drv = bss->drv;
6989 struct nl_msg *msg;
6990 int ret;
6991
6992 if (!drv->pending_remain_on_chan) {
6993 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6994 "to cancel");
6995 return -1;
6996 }
6997
6998 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6999 "0x%llx",
7000 (long long unsigned int) drv->remain_on_chan_cookie);
7001
7002 msg = nlmsg_alloc();
7003 if (!msg)
7004 return -1;
7005
7006 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7007
7008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7009 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
7010
7011 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7012 if (ret == 0)
7013 return 0;
7014 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7015 "%d (%s)", ret, strerror(-ret));
7016 nla_put_failure:
7017 return -1;
7018 }
7019
7020
7021 static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
7022 {
7023 struct i802_bss *bss = priv;
7024 struct wpa_driver_nl80211_data *drv = bss->drv;
7025
7026 if (!report) {
7027 if (drv->nl_preq.handle) {
7028 eloop_unregister_read_sock(
7029 nl_socket_get_fd(drv->nl_preq.handle));
7030 nl_destroy_handles(&drv->nl_preq);
7031 }
7032 return 0;
7033 }
7034
7035 if (drv->nl_preq.handle) {
7036 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
7037 "already on!");
7038 return 0;
7039 }
7040
7041 if (nl_create_handles(&drv->nl_preq, drv->global->nl_cb, "preq"))
7042 return -1;
7043
7044 if (nl80211_register_frame(drv, drv->nl_preq.handle,
7045 (WLAN_FC_TYPE_MGMT << 2) |
7046 (WLAN_FC_STYPE_PROBE_REQ << 4),
7047 NULL, 0) < 0)
7048 goto out_err;
7049
7050 eloop_register_read_sock(nl_socket_get_fd(drv->nl_preq.handle),
7051 wpa_driver_nl80211_event_receive, drv,
7052 drv->nl_preq.handle);
7053
7054 return 0;
7055
7056 out_err:
7057 nl_destroy_handles(&drv->nl_preq);
7058 return -1;
7059 }
7060
7061
7062 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7063 int ifindex, int disabled)
7064 {
7065 struct nl_msg *msg;
7066 struct nlattr *bands, *band;
7067 int ret;
7068
7069 msg = nlmsg_alloc();
7070 if (!msg)
7071 return -1;
7072
7073 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
7074 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7075
7076 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7077 if (!bands)
7078 goto nla_put_failure;
7079
7080 /*
7081 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7082 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7083 * rates. All 5 GHz rates are left enabled.
7084 */
7085 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
7086 if (!band)
7087 goto nla_put_failure;
7088 if (disabled) {
7089 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
7090 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
7091 }
7092 nla_nest_end(msg, band);
7093
7094 nla_nest_end(msg, bands);
7095
7096 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7097 msg = NULL;
7098 if (ret) {
7099 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7100 "(%s)", ret, strerror(-ret));
7101 }
7102
7103 return ret;
7104
7105 nla_put_failure:
7106 nlmsg_free(msg);
7107 return -1;
7108 }
7109
7110
7111 static int wpa_driver_nl80211_deinit_ap(void *priv)
7112 {
7113 struct i802_bss *bss = priv;
7114 struct wpa_driver_nl80211_data *drv = bss->drv;
7115 if (!is_ap_interface(drv->nlmode))
7116 return -1;
7117 wpa_driver_nl80211_del_beacon(drv);
7118 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7119 }
7120
7121
7122 static void wpa_driver_nl80211_resume(void *priv)
7123 {
7124 struct i802_bss *bss = priv;
7125 struct wpa_driver_nl80211_data *drv = bss->drv;
7126 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
7127 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
7128 "resume event");
7129 }
7130 }
7131
7132
7133 static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
7134 const u8 *ies, size_t ies_len)
7135 {
7136 struct i802_bss *bss = priv;
7137 struct wpa_driver_nl80211_data *drv = bss->drv;
7138 int ret;
7139 u8 *data, *pos;
7140 size_t data_len;
7141 u8 own_addr[ETH_ALEN];
7142
7143 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7144 own_addr) < 0)
7145 return -1;
7146
7147 if (action != 1) {
7148 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
7149 "action %d", action);
7150 return -1;
7151 }
7152
7153 /*
7154 * Action frame payload:
7155 * Category[1] = 6 (Fast BSS Transition)
7156 * Action[1] = 1 (Fast BSS Transition Request)
7157 * STA Address
7158 * Target AP Address
7159 * FT IEs
7160 */
7161
7162 data_len = 2 + 2 * ETH_ALEN + ies_len;
7163 data = os_malloc(data_len);
7164 if (data == NULL)
7165 return -1;
7166 pos = data;
7167 *pos++ = 0x06; /* FT Action category */
7168 *pos++ = action;
7169 os_memcpy(pos, own_addr, ETH_ALEN);
7170 pos += ETH_ALEN;
7171 os_memcpy(pos, target_ap, ETH_ALEN);
7172 pos += ETH_ALEN;
7173 os_memcpy(pos, ies, ies_len);
7174
7175 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
7176 drv->bssid, own_addr, drv->bssid,
7177 data, data_len, 0);
7178 os_free(data);
7179
7180 return ret;
7181 }
7182
7183
7184 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7185 {
7186 struct i802_bss *bss = priv;
7187 struct wpa_driver_nl80211_data *drv = bss->drv;
7188 struct nl_msg *msg, *cqm = NULL;
7189
7190 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7191 "hysteresis=%d", threshold, hysteresis);
7192
7193 msg = nlmsg_alloc();
7194 if (!msg)
7195 return -1;
7196
7197 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
7198
7199 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
7200
7201 cqm = nlmsg_alloc();
7202 if (cqm == NULL)
7203 return -1;
7204
7205 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
7206 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
7207 nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
7208
7209 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7210 return 0;
7211 msg = NULL;
7212
7213 nla_put_failure:
7214 if (cqm)
7215 nlmsg_free(cqm);
7216 nlmsg_free(msg);
7217 return -1;
7218 }
7219
7220
7221 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7222 {
7223 struct i802_bss *bss = priv;
7224 struct wpa_driver_nl80211_data *drv = bss->drv;
7225 int res;
7226
7227 os_memset(si, 0, sizeof(*si));
7228 res = nl80211_get_link_signal(drv, si);
7229 if (res != 0)
7230 return res;
7231
7232 return nl80211_get_link_noise(drv, si);
7233 }
7234
7235
7236 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7237 int encrypt)
7238 {
7239 struct i802_bss *bss = priv;
7240 struct wpa_driver_nl80211_data *drv = bss->drv;
7241 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
7242 }
7243
7244
7245 static int nl80211_set_param(void *priv, const char *param)
7246 {
7247 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
7248 if (param == NULL)
7249 return 0;
7250
7251 #ifdef CONFIG_P2P
7252 if (os_strstr(param, "use_p2p_group_interface=1")) {
7253 struct i802_bss *bss = priv;
7254 struct wpa_driver_nl80211_data *drv = bss->drv;
7255
7256 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7257 "interface");
7258 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7259 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7260 }
7261 #endif /* CONFIG_P2P */
7262
7263 return 0;
7264 }
7265
7266
7267 static void * nl80211_global_init(void)
7268 {
7269 struct nl80211_global *global;
7270 struct netlink_config *cfg;
7271
7272 global = os_zalloc(sizeof(*global));
7273 if (global == NULL)
7274 return NULL;
7275 global->ioctl_sock = -1;
7276 dl_list_init(&global->interfaces);
7277 global->if_add_ifindex = -1;
7278
7279 cfg = os_zalloc(sizeof(*cfg));
7280 if (cfg == NULL)
7281 goto err;
7282
7283 cfg->ctx = global;
7284 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7285 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7286 global->netlink = netlink_init(cfg);
7287 if (global->netlink == NULL) {
7288 os_free(cfg);
7289 goto err;
7290 }
7291
7292 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7293 goto err;
7294
7295 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7296 if (global->ioctl_sock < 0) {
7297 perror("socket(PF_INET,SOCK_DGRAM)");
7298 goto err;
7299 }
7300
7301 return global;
7302
7303 err:
7304 nl80211_global_deinit(global);
7305 return NULL;
7306 }
7307
7308
7309 static void nl80211_global_deinit(void *priv)
7310 {
7311 struct nl80211_global *global = priv;
7312 if (global == NULL)
7313 return;
7314 if (!dl_list_empty(&global->interfaces)) {
7315 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7316 "nl80211_global_deinit",
7317 dl_list_len(&global->interfaces));
7318 }
7319
7320 if (global->netlink)
7321 netlink_deinit(global->netlink);
7322
7323 if (global->nl80211)
7324 genl_family_put(global->nl80211);
7325 nl_destroy_handles(&global->nl);
7326
7327 if (global->nl_cb)
7328 nl_cb_put(global->nl_cb);
7329
7330 if (global->ioctl_sock >= 0)
7331 close(global->ioctl_sock);
7332
7333 os_free(global);
7334 }
7335
7336
7337 static const char * nl80211_get_radio_name(void *priv)
7338 {
7339 struct i802_bss *bss = priv;
7340 struct wpa_driver_nl80211_data *drv = bss->drv;
7341 return drv->phyname;
7342 }
7343
7344
7345 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
7346 const u8 *pmkid)
7347 {
7348 struct nl_msg *msg;
7349
7350 msg = nlmsg_alloc();
7351 if (!msg)
7352 return -ENOMEM;
7353
7354 nl80211_cmd(bss->drv, msg, 0, cmd);
7355
7356 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7357 if (pmkid)
7358 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
7359 if (bssid)
7360 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7361
7362 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
7363 nla_put_failure:
7364 return -ENOBUFS;
7365 }
7366
7367
7368 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7369 {
7370 struct i802_bss *bss = priv;
7371 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
7372 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
7373 }
7374
7375
7376 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7377 {
7378 struct i802_bss *bss = priv;
7379 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7380 MAC2STR(bssid));
7381 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
7382 }
7383
7384
7385 static int nl80211_flush_pmkid(void *priv)
7386 {
7387 struct i802_bss *bss = priv;
7388 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7389 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
7390 }
7391
7392
7393 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
7394 const u8 *replay_ctr)
7395 {
7396 struct i802_bss *bss = priv;
7397 struct wpa_driver_nl80211_data *drv = bss->drv;
7398 struct nlattr *replay_nested;
7399 struct nl_msg *msg;
7400
7401 msg = nlmsg_alloc();
7402 if (!msg)
7403 return;
7404
7405 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7406
7407 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
7408
7409 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7410 if (!replay_nested)
7411 goto nla_put_failure;
7412
7413 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
7414 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
7415 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7416 replay_ctr);
7417
7418 nla_nest_end(msg, replay_nested);
7419
7420 send_and_recv_msgs(drv, msg, NULL, NULL);
7421 return;
7422 nla_put_failure:
7423 nlmsg_free(msg);
7424 }
7425
7426
7427 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7428 int qos)
7429 {
7430 struct i802_bss *bss = priv;
7431 struct {
7432 struct ieee80211_hdr hdr;
7433 u16 qos_ctl;
7434 } STRUCT_PACKED nulldata;
7435 size_t size;
7436
7437 /* Send data frame to poll STA and check whether this frame is ACKed */
7438
7439 os_memset(&nulldata, 0, sizeof(nulldata));
7440
7441 if (qos) {
7442 nulldata.hdr.frame_control =
7443 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7444 WLAN_FC_STYPE_QOS_NULL);
7445 size = sizeof(nulldata);
7446 } else {
7447 nulldata.hdr.frame_control =
7448 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7449 WLAN_FC_STYPE_NULLFUNC);
7450 size = sizeof(struct ieee80211_hdr);
7451 }
7452
7453 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7454 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7455 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7456 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7457
7458 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size) < 0)
7459 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7460 "send poll frame");
7461 }
7462
7463
7464 #ifdef CONFIG_TDLS
7465
7466 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7467 u8 dialog_token, u16 status_code,
7468 const u8 *buf, size_t len)
7469 {
7470 struct i802_bss *bss = priv;
7471 struct wpa_driver_nl80211_data *drv = bss->drv;
7472 struct nl_msg *msg;
7473
7474 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7475 return -EOPNOTSUPP;
7476
7477 if (!dst)
7478 return -EINVAL;
7479
7480 msg = nlmsg_alloc();
7481 if (!msg)
7482 return -ENOMEM;
7483
7484 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
7485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7486 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
7487 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
7488 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
7489 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
7490 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
7491
7492 return send_and_recv_msgs(drv, msg, NULL, NULL);
7493
7494 nla_put_failure:
7495 nlmsg_free(msg);
7496 return -ENOBUFS;
7497 }
7498
7499
7500 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7501 {
7502 struct i802_bss *bss = priv;
7503 struct wpa_driver_nl80211_data *drv = bss->drv;
7504 struct nl_msg *msg;
7505 enum nl80211_tdls_operation nl80211_oper;
7506
7507 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7508 return -EOPNOTSUPP;
7509
7510 switch (oper) {
7511 case TDLS_DISCOVERY_REQ:
7512 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7513 break;
7514 case TDLS_SETUP:
7515 nl80211_oper = NL80211_TDLS_SETUP;
7516 break;
7517 case TDLS_TEARDOWN:
7518 nl80211_oper = NL80211_TDLS_TEARDOWN;
7519 break;
7520 case TDLS_ENABLE_LINK:
7521 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7522 break;
7523 case TDLS_DISABLE_LINK:
7524 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7525 break;
7526 case TDLS_ENABLE:
7527 return 0;
7528 case TDLS_DISABLE:
7529 return 0;
7530 default:
7531 return -EINVAL;
7532 }
7533
7534 msg = nlmsg_alloc();
7535 if (!msg)
7536 return -ENOMEM;
7537
7538 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
7539 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
7540 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7541 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7542
7543 return send_and_recv_msgs(drv, msg, NULL, NULL);
7544
7545 nla_put_failure:
7546 nlmsg_free(msg);
7547 return -ENOBUFS;
7548 }
7549
7550 #endif /* CONFIG TDLS */
7551
7552
7553 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
7554 .name = "nl80211",
7555 .desc = "Linux nl80211/cfg80211",
7556 .get_bssid = wpa_driver_nl80211_get_bssid,
7557 .get_ssid = wpa_driver_nl80211_get_ssid,
7558 .set_key = wpa_driver_nl80211_set_key,
7559 .scan2 = wpa_driver_nl80211_scan,
7560 .sched_scan = wpa_driver_nl80211_sched_scan,
7561 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
7562 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
7563 .deauthenticate = wpa_driver_nl80211_deauthenticate,
7564 .disassociate = wpa_driver_nl80211_disassociate,
7565 .authenticate = wpa_driver_nl80211_authenticate,
7566 .associate = wpa_driver_nl80211_associate,
7567 .global_init = nl80211_global_init,
7568 .global_deinit = nl80211_global_deinit,
7569 .init2 = wpa_driver_nl80211_init,
7570 .deinit = wpa_driver_nl80211_deinit,
7571 .get_capa = wpa_driver_nl80211_get_capa,
7572 .set_operstate = wpa_driver_nl80211_set_operstate,
7573 .set_supp_port = wpa_driver_nl80211_set_supp_port,
7574 .set_country = wpa_driver_nl80211_set_country,
7575 .set_ap = wpa_driver_nl80211_set_ap,
7576 .if_add = wpa_driver_nl80211_if_add,
7577 .if_remove = wpa_driver_nl80211_if_remove,
7578 .send_mlme = wpa_driver_nl80211_send_mlme,
7579 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
7580 .sta_add = wpa_driver_nl80211_sta_add,
7581 .sta_remove = wpa_driver_nl80211_sta_remove,
7582 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
7583 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
7584 #ifdef HOSTAPD
7585 .hapd_init = i802_init,
7586 .hapd_deinit = i802_deinit,
7587 .set_wds_sta = i802_set_wds_sta,
7588 #endif /* HOSTAPD */
7589 #if defined(HOSTAPD) || defined(CONFIG_AP)
7590 .get_seqnum = i802_get_seqnum,
7591 .flush = i802_flush,
7592 .read_sta_data = i802_read_sta_data,
7593 .get_inact_sec = i802_get_inact_sec,
7594 .sta_clear_stats = i802_sta_clear_stats,
7595 .set_rts = i802_set_rts,
7596 .set_frag = i802_set_frag,
7597 .set_tx_queue_params = i802_set_tx_queue_params,
7598 .set_sta_vlan = i802_set_sta_vlan,
7599 .set_rate_sets = i802_set_rate_sets,
7600 .sta_deauth = i802_sta_deauth,
7601 .sta_disassoc = i802_sta_disassoc,
7602 #endif /* HOSTAPD || CONFIG_AP */
7603 .set_freq = i802_set_freq,
7604 .send_action = wpa_driver_nl80211_send_action,
7605 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
7606 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
7607 .cancel_remain_on_channel =
7608 wpa_driver_nl80211_cancel_remain_on_channel,
7609 .probe_req_report = wpa_driver_nl80211_probe_req_report,
7610 .deinit_ap = wpa_driver_nl80211_deinit_ap,
7611 .resume = wpa_driver_nl80211_resume,
7612 .send_ft_action = nl80211_send_ft_action,
7613 .signal_monitor = nl80211_signal_monitor,
7614 .signal_poll = nl80211_signal_poll,
7615 .send_frame = nl80211_send_frame,
7616 .set_param = nl80211_set_param,
7617 .get_radio_name = nl80211_get_radio_name,
7618 .add_pmkid = nl80211_add_pmkid,
7619 .remove_pmkid = nl80211_remove_pmkid,
7620 .flush_pmkid = nl80211_flush_pmkid,
7621 .set_rekey_info = nl80211_set_rekey_info,
7622 .poll_client = nl80211_poll_client,
7623 #ifdef CONFIG_TDLS
7624 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
7625 .tdls_oper = nl80211_tdls_oper,
7626 #endif /* CONFIG_TDLS */
7627 };