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