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