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