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