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