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