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