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