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