]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
HT: Pass the smps_mode in AP parameters
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
CommitLineData
3f5285e8 1/*
c5121837 2 * Driver interaction with Linux nl80211/cfg80211
90a545cc 3 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
072ad14c
JM
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>
58f6fbe0 7 * Copyright (c) 2009-2010, Atheros Communications
3f5285e8 8 *
0f3d578e
JM
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
3f5285e8
JM
11 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
6859f1cb
BG
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
37b7d082 18#include <net/if.h>
3f5285e8
JM
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
97ed9a06
KP
22#ifdef CONFIG_LIBNL3_ROUTE
23#include <netlink/route/neighbour.h>
24#endif /* CONFIG_LIBNL3_ROUTE */
8602b0f2 25#include <linux/rtnetlink.h>
1b648c7e
JM
26#include <netpacket/packet.h>
27#include <linux/filter.h>
32ab4855 28#include <linux/errqueue.h>
7e45830a 29#include "nl80211_copy.h"
625f587b 30
3f5285e8 31#include "common.h"
1b648c7e 32#include "eloop.h"
f2ed8023 33#include "utils/list.h"
1682c623 34#include "common/qca-vendor.h"
a26582cb 35#include "common/qca-vendor-attr.h"
1b648c7e 36#include "common/ieee802_11_defs.h"
9b90955e 37#include "common/ieee802_11_common.h"
f10bfc9a 38#include "l2_packet/l2_packet.h"
e2d02c29 39#include "netlink.h"
34f2f814 40#include "linux_ioctl.h"
e2d02c29
JM
41#include "radiotap.h"
42#include "radiotap_iter.h"
8401a6b0 43#include "rfkill.h"
1b648c7e 44#include "driver.h"
0915d02c 45
32ab4855
JB
46#ifndef SO_WIFI_STATUS
47# if defined(__sparc__)
48# define SO_WIFI_STATUS 0x0025
49# elif defined(__parisc__)
50# define SO_WIFI_STATUS 0x4022
51# else
52# define SO_WIFI_STATUS 41
53# endif
54
55# define SCM_WIFI_STATUS SO_WIFI_STATUS
56#endif
57
58#ifndef SO_EE_ORIGIN_TXSTATUS
59#define SO_EE_ORIGIN_TXSTATUS 4
60#endif
61
62#ifndef PACKET_TX_TIMESTAMP
63#define PACKET_TX_TIMESTAMP 16
64#endif
65
216eede8
DS
66#ifdef ANDROID
67#include "android_drv.h"
68#endif /* ANDROID */
c5121837
JM
69#ifdef CONFIG_LIBNL20
70/* libnl 2.0 compatibility code */
2e8eac2d 71#define nl_handle nl_sock
a65a9aed
JB
72#define nl80211_handle_alloc nl_socket_alloc_cb
73#define nl80211_handle_destroy nl_socket_free
74#else
75/*
76 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
77 * but when you free a socket again it will mess up its bitmap and
78 * and use the wrong number the next time it needs a socket ID.
79 * Therefore, we wrap the handle alloc/destroy and add our own pid
80 * accounting.
81 */
82static uint32_t port_bitmap[32] = { 0 };
83
84static struct nl_handle *nl80211_handle_alloc(void *cb)
85{
86 struct nl_handle *handle;
87 uint32_t pid = getpid() & 0x3FFFFF;
88 int i;
89
90 handle = nl_handle_alloc_cb(cb);
91
92 for (i = 0; i < 1024; i++) {
93 if (port_bitmap[i / 32] & (1 << (i % 32)))
94 continue;
95 port_bitmap[i / 32] |= 1 << (i % 32);
96 pid += i << 22;
97 break;
98 }
99
100 nl_socket_set_local_port(handle, pid);
101
102 return handle;
103}
104
105static void nl80211_handle_destroy(struct nl_handle *handle)
106{
107 uint32_t port = nl_socket_get_local_port(handle);
108
109 port >>= 22;
110 port_bitmap[port / 32] &= ~(1 << (port % 32));
111
112 nl_handle_destroy(handle);
113}
c5121837
JM
114#endif /* CONFIG_LIBNL20 */
115
c5121837 116
1c6edec6
JM
117#ifdef ANDROID
118/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
119static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
120{
121 return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
122}
123#undef nl_socket_set_nonblocking
124#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
125#endif /* ANDROID */
126
127
481234cf 128static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
a92dfde8 129{
481234cf 130 struct nl_handle *handle;
a92dfde8 131
481234cf
JM
132 handle = nl80211_handle_alloc(cb);
133 if (handle == NULL) {
a92dfde8
JB
134 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
135 "callbacks (%s)", dbg);
481234cf 136 return NULL;
a92dfde8
JB
137 }
138
481234cf 139 if (genl_connect(handle)) {
a92dfde8
JB
140 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
141 "netlink (%s)", dbg);
481234cf
JM
142 nl80211_handle_destroy(handle);
143 return NULL;
a92dfde8
JB
144 }
145
481234cf 146 return handle;
a92dfde8
JB
147}
148
149
481234cf 150static void nl_destroy_handles(struct nl_handle **handle)
a92dfde8 151{
481234cf 152 if (*handle == NULL)
a92dfde8 153 return;
481234cf
JM
154 nl80211_handle_destroy(*handle);
155 *handle = NULL;
a92dfde8
JB
156}
157
158
10b85921
JB
159#if __WORDSIZE == 64
160#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
161#else
162#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
163#endif
164
5f65e9f7
JB
165static void nl80211_register_eloop_read(struct nl_handle **handle,
166 eloop_sock_handler handler,
167 void *eloop_data)
168{
10b85921 169 nl_socket_set_nonblocking(*handle);
5f65e9f7
JB
170 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
171 eloop_data, *handle);
10b85921 172 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
5f65e9f7
JB
173}
174
175
176static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
177{
10b85921 178 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
5f65e9f7
JB
179 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
180 nl_destroy_handles(handle);
181}
182
183
3f5285e8
JM
184#ifndef IFF_LOWER_UP
185#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
186#endif
187#ifndef IFF_DORMANT
188#define IFF_DORMANT 0x20000 /* driver signals dormant */
189#endif
190
191#ifndef IF_OPER_DORMANT
192#define IF_OPER_DORMANT 5
193#endif
194#ifndef IF_OPER_UP
195#define IF_OPER_UP 6
196#endif
197
f2ed8023
JM
198struct nl80211_global {
199 struct dl_list interfaces;
ff6a158b 200 int if_add_ifindex;
f632e483
AS
201 u64 if_add_wdevid;
202 int if_add_wdevid_set;
36d84860 203 struct netlink_data *netlink;
2a7b66f5 204 struct nl_cb *nl_cb;
481234cf 205 struct nl_handle *nl;
335d42b1 206 int nl80211_id;
c81eff1a 207 int ioctl_sock; /* socket for ioctl() use */
d6c9aab8 208
481234cf 209 struct nl_handle *nl_event;
f2ed8023
JM
210};
211
e32ad281
JB
212struct nl80211_wiphy_data {
213 struct dl_list list;
214 struct dl_list bsss;
215 struct dl_list drvs;
216
481234cf 217 struct nl_handle *nl_beacons;
e32ad281
JB
218 struct nl_cb *nl_cb;
219
220 int wiphy_idx;
221};
222
36d84860
BG
223static void nl80211_global_deinit(void *priv);
224
c5121837 225struct i802_bss {
a2e40bb6 226 struct wpa_driver_nl80211_data *drv;
c5121837 227 struct i802_bss *next;
b4fd6fab 228 int ifindex;
d3aaef80 229 u64 wdev_id;
a2e40bb6 230 char ifname[IFNAMSIZ + 1];
e17a2477 231 char brname[IFNAMSIZ];
c5121837 232 unsigned int beacon_set:1;
e17a2477
JM
233 unsigned int added_if_into_bridge:1;
234 unsigned int added_bridge:1;
873d0fcf 235 unsigned int in_deinit:1;
d3aaef80 236 unsigned int wdev_id_set:1;
390e489c 237 unsigned int added_if:1;
bf144cf6 238 unsigned int static_ap:1;
221a59c9 239
341eebee
JB
240 u8 addr[ETH_ALEN];
241
e4fb2167 242 int freq;
e87ef751 243 int bandwidth;
60b13c20 244 int if_dynamic;
e4fb2167 245
a5e1eb20 246 void *ctx;
481234cf 247 struct nl_handle *nl_preq, *nl_mgmt;
cc7a48d1 248 struct nl_cb *nl_cb;
e32ad281
JB
249
250 struct nl80211_wiphy_data *wiphy_data;
251 struct dl_list wiphy_list;
c5121837 252};
3f5285e8
JM
253
254struct wpa_driver_nl80211_data {
f2ed8023
JM
255 struct nl80211_global *global;
256 struct dl_list list;
e32ad281 257 struct dl_list wiphy_list;
6859f1cb 258 char phyname[32];
fee354c7 259 u8 perm_addr[ETH_ALEN];
3f5285e8 260 void *ctx;
3f5285e8 261 int ifindex;
7524cfb1 262 int if_removed;
a63063b4 263 int if_disabled;
7d9c3698 264 int ignore_if_down_event;
8401a6b0 265 struct rfkill_data *rfkill;
c2a04078 266 struct wpa_driver_capa capa;
8cd6b7bc
JB
267 u8 *extended_capa, *extended_capa_mask;
268 unsigned int extended_capa_len;
c2a04078 269 int has_capability;
c2a04078 270
3f5285e8
JM
271 int operstate;
272
3f5285e8 273 int scan_complete_events;
a771c07d
JM
274 enum scan_states {
275 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
276 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
277 SCHED_SCAN_RESULTS
278 } scan_state;
3f5285e8 279
1afc986d 280 struct nl_cb *nl_cb;
1c873584 281
e6b8efeb 282 u8 auth_bssid[ETH_ALEN];
add9b7a4 283 u8 auth_attempt_bssid[ETH_ALEN];
c2a04078 284 u8 bssid[ETH_ALEN];
add9b7a4 285 u8 prev_bssid[ETH_ALEN];
c2a04078 286 int associated;
fd05d64e
JM
287 u8 ssid[32];
288 size_t ssid_len;
b1f625e0
EP
289 enum nl80211_iftype nlmode;
290 enum nl80211_iftype ap_scan_as_station;
4832ecd7 291 unsigned int assoc_freq;
d2440ba0 292
0915d02c
JM
293 int monitor_sock;
294 int monitor_ifidx;
3fd1cefb 295 int monitor_refcount;
7da3abe7 296
b3af99d2 297 unsigned int disabled_11b_rates:1;
55777702 298 unsigned int pending_remain_on_chan:1;
dac12351 299 unsigned int in_interface_list:1;
61cbe2ff 300 unsigned int device_ap_sme:1;
39718852 301 unsigned int poll_command_supported:1;
32ab4855 302 unsigned int data_tx_status:1;
536fd62d
JM
303 unsigned int scan_for_auth:1;
304 unsigned int retry_auth:1;
a11241fa 305 unsigned int use_monitor:1;
a8c5b43a 306 unsigned int ignore_next_local_disconnect:1;
d6a36f39 307 unsigned int ignore_next_local_deauth:1;
0d547d5f 308 unsigned int hostapd:1;
49b4b205 309 unsigned int start_mode_ap:1;
146fa9b3 310 unsigned int start_iface_up:1;
64abb725 311 unsigned int test_use_roc_tx:1;
e8d70a73 312 unsigned int ignore_deauth_event:1;
0800f9ee 313 unsigned int roaming_vendor_cmd_avail:1;
65d645ce 314 unsigned int dfs_vendor_cmd_avail:1;
57a8f8af 315 unsigned int have_low_prio_scan:1;
b497a212 316 unsigned int force_connect_cmd:1;
fee354c7 317 unsigned int addr_changed:1;
b41f2684
CL
318 unsigned int key_mgmt_set_key_vendor_cmd_avail:1;
319 unsigned int roam_auth_vendor_event_avail:1;
55777702
JM
320
321 u64 remain_on_chan_cookie;
58f6fbe0 322 u64 send_action_cookie;
c5121837 323
5582a5d1
JB
324 unsigned int last_mgmt_freq;
325
3812464c
JM
326 struct wpa_driver_scan_filter *filter_ssids;
327 size_t num_filter_ssids;
328
834ee56f 329 struct i802_bss *first_bss;
a2e40bb6 330
d12dab4c 331 int eapol_tx_sock;
f10bfc9a 332
c5121837 333 int eapol_sock; /* socket for EAPOL frames */
c5121837 334
ca3c6b4d 335 struct nl_handle *rtnl_sk; /* nl_sock for NETLINK_ROUTE */
97ed9a06 336
c5121837
JM
337 int default_if_indices[16];
338 int *if_indices;
339 int num_if_indices;
536fd62d
JM
340
341 /* From failed authentication command */
342 int auth_freq;
343 u8 auth_bssid_[ETH_ALEN];
344 u8 auth_ssid[32];
345 size_t auth_ssid_len;
346 int auth_alg;
347 u8 *auth_ie;
348 size_t auth_ie_len;
349 u8 auth_wep_key[4][16];
350 size_t auth_wep_key_len[4];
351 int auth_wep_tx_keyidx;
352 int auth_local_state_change;
353 int auth_p2p;
3f5285e8
JM
354};
355
356
9ebce9c5 357static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
3f5285e8
JM
358static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
359 void *timeout_ctx);
b1f625e0
EP
360static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
361 enum nl80211_iftype nlmode);
4ec68377
JD
362static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
363 struct hostapd_freq_params *freq);
3c5d34e3 364
362f781e 365static int
0d547d5f 366wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
49b4b205 367 const u8 *set_addr, int first);
d72aad94 368static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
369 const u8 *addr, int cmd, u16 reason_code,
370 int local_state_change);
460456f8
JM
371static void nl80211_remove_monitor_interface(
372 struct wpa_driver_nl80211_data *drv);
88df0ef7 373static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f 374 unsigned int freq, unsigned int wait,
b106173a 375 const u8 *buf, size_t buf_len, u64 *cookie,
88df0ef7 376 int no_cck, int no_ack, int offchanok);
b21990b4
AQ
377static int nl80211_register_frame(struct i802_bss *bss,
378 struct nl_handle *hl_handle,
379 u16 type, const u8 *match, size_t match_len);
9ebce9c5
JM
380static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
381 int report);
216eede8
DS
382#ifdef ANDROID
383static int android_pno_start(struct i802_bss *bss,
384 struct wpa_driver_scan_params *params);
385static int android_pno_stop(struct i802_bss *bss);
5e2c3490
JM
386extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
387 size_t buf_len);
216eede8 388#endif /* ANDROID */
0de38036 389#ifdef ANDROID_P2P
959214b2
DS
390#ifdef ANDROID_P2P_STUB
391int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration) {
392 return 0;
393}
394int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len) {
395 return 0;
396}
397int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow) {
398 return -1;
399}
400int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
401 const struct wpabuf *proberesp,
402 const struct wpabuf *assocresp) {
403 return 0;
404}
405#else /* ANDROID_P2P_STUB */
0de38036
JM
406int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
407int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
408int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
409int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
410 const struct wpabuf *proberesp,
411 const struct wpabuf *assocresp);
959214b2 412#endif /* ANDROID_P2P_STUB */
0de38036 413#endif /* ANDROID_P2P */
0915d02c 414
2135f224
JM
415static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
416static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
97cfcf64 417static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
9ebce9c5 418static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
fbbfcbac
FF
419 enum wpa_driver_if_type type,
420 const char *ifname);
072ad14c 421
e87ef751
PX
422static int nl80211_set_channel(struct i802_bss *bss,
423 struct hostapd_freq_params *freq, int set_chan);
4e5cb1a3
JM
424static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
425 int ifindex, int disabled);
504e905c 426
21bdbe38 427static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
536fd62d
JM
428static int wpa_driver_nl80211_authenticate_retry(
429 struct wpa_driver_nl80211_data *drv);
21bdbe38 430
3c5d34e3 431static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
bf83eab5
IP
432static int i802_set_iface_flags(struct i802_bss *bss, int up);
433
3f5285e8 434
2090a0b4
DS
435static const char * nl80211_command_to_string(enum nl80211_commands cmd)
436{
437#define C2S(x) case x: return #x;
438 switch (cmd) {
439 C2S(NL80211_CMD_UNSPEC)
440 C2S(NL80211_CMD_GET_WIPHY)
441 C2S(NL80211_CMD_SET_WIPHY)
442 C2S(NL80211_CMD_NEW_WIPHY)
443 C2S(NL80211_CMD_DEL_WIPHY)
444 C2S(NL80211_CMD_GET_INTERFACE)
445 C2S(NL80211_CMD_SET_INTERFACE)
446 C2S(NL80211_CMD_NEW_INTERFACE)
447 C2S(NL80211_CMD_DEL_INTERFACE)
448 C2S(NL80211_CMD_GET_KEY)
449 C2S(NL80211_CMD_SET_KEY)
450 C2S(NL80211_CMD_NEW_KEY)
451 C2S(NL80211_CMD_DEL_KEY)
452 C2S(NL80211_CMD_GET_BEACON)
453 C2S(NL80211_CMD_SET_BEACON)
454 C2S(NL80211_CMD_START_AP)
455 C2S(NL80211_CMD_STOP_AP)
456 C2S(NL80211_CMD_GET_STATION)
457 C2S(NL80211_CMD_SET_STATION)
458 C2S(NL80211_CMD_NEW_STATION)
459 C2S(NL80211_CMD_DEL_STATION)
460 C2S(NL80211_CMD_GET_MPATH)
461 C2S(NL80211_CMD_SET_MPATH)
462 C2S(NL80211_CMD_NEW_MPATH)
463 C2S(NL80211_CMD_DEL_MPATH)
464 C2S(NL80211_CMD_SET_BSS)
465 C2S(NL80211_CMD_SET_REG)
466 C2S(NL80211_CMD_REQ_SET_REG)
467 C2S(NL80211_CMD_GET_MESH_CONFIG)
468 C2S(NL80211_CMD_SET_MESH_CONFIG)
469 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
470 C2S(NL80211_CMD_GET_REG)
471 C2S(NL80211_CMD_GET_SCAN)
472 C2S(NL80211_CMD_TRIGGER_SCAN)
473 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
474 C2S(NL80211_CMD_SCAN_ABORTED)
475 C2S(NL80211_CMD_REG_CHANGE)
476 C2S(NL80211_CMD_AUTHENTICATE)
477 C2S(NL80211_CMD_ASSOCIATE)
478 C2S(NL80211_CMD_DEAUTHENTICATE)
479 C2S(NL80211_CMD_DISASSOCIATE)
480 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
481 C2S(NL80211_CMD_REG_BEACON_HINT)
482 C2S(NL80211_CMD_JOIN_IBSS)
483 C2S(NL80211_CMD_LEAVE_IBSS)
484 C2S(NL80211_CMD_TESTMODE)
485 C2S(NL80211_CMD_CONNECT)
486 C2S(NL80211_CMD_ROAM)
487 C2S(NL80211_CMD_DISCONNECT)
488 C2S(NL80211_CMD_SET_WIPHY_NETNS)
489 C2S(NL80211_CMD_GET_SURVEY)
490 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
491 C2S(NL80211_CMD_SET_PMKSA)
492 C2S(NL80211_CMD_DEL_PMKSA)
493 C2S(NL80211_CMD_FLUSH_PMKSA)
494 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
495 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
496 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
497 C2S(NL80211_CMD_REGISTER_FRAME)
498 C2S(NL80211_CMD_FRAME)
499 C2S(NL80211_CMD_FRAME_TX_STATUS)
500 C2S(NL80211_CMD_SET_POWER_SAVE)
501 C2S(NL80211_CMD_GET_POWER_SAVE)
502 C2S(NL80211_CMD_SET_CQM)
503 C2S(NL80211_CMD_NOTIFY_CQM)
504 C2S(NL80211_CMD_SET_CHANNEL)
505 C2S(NL80211_CMD_SET_WDS_PEER)
506 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
507 C2S(NL80211_CMD_JOIN_MESH)
508 C2S(NL80211_CMD_LEAVE_MESH)
509 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
510 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
511 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
512 C2S(NL80211_CMD_GET_WOWLAN)
513 C2S(NL80211_CMD_SET_WOWLAN)
514 C2S(NL80211_CMD_START_SCHED_SCAN)
515 C2S(NL80211_CMD_STOP_SCHED_SCAN)
516 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
517 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
518 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
519 C2S(NL80211_CMD_PMKSA_CANDIDATE)
520 C2S(NL80211_CMD_TDLS_OPER)
521 C2S(NL80211_CMD_TDLS_MGMT)
522 C2S(NL80211_CMD_UNEXPECTED_FRAME)
523 C2S(NL80211_CMD_PROBE_CLIENT)
524 C2S(NL80211_CMD_REGISTER_BEACONS)
525 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
526 C2S(NL80211_CMD_SET_NOACK_MAP)
527 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
528 C2S(NL80211_CMD_START_P2P_DEVICE)
529 C2S(NL80211_CMD_STOP_P2P_DEVICE)
530 C2S(NL80211_CMD_CONN_FAILED)
531 C2S(NL80211_CMD_SET_MCAST_RATE)
532 C2S(NL80211_CMD_SET_MAC_ACL)
533 C2S(NL80211_CMD_RADAR_DETECT)
534 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
535 C2S(NL80211_CMD_UPDATE_FT_IES)
536 C2S(NL80211_CMD_FT_EVENT)
537 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
538 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
17b79e65
JM
539 C2S(NL80211_CMD_GET_COALESCE)
540 C2S(NL80211_CMD_SET_COALESCE)
541 C2S(NL80211_CMD_CHANNEL_SWITCH)
542 C2S(NL80211_CMD_VENDOR)
543 C2S(NL80211_CMD_SET_QOS_MAP)
2090a0b4
DS
544 default:
545 return "NL80211_CMD_UNKNOWN";
546 }
547#undef C2S
548}
549
550
8d1fdde7
JD
551/* Converts nl80211_chan_width to a common format */
552static enum chan_width convert2width(int width)
553{
554 switch (width) {
555 case NL80211_CHAN_WIDTH_20_NOHT:
556 return CHAN_WIDTH_20_NOHT;
557 case NL80211_CHAN_WIDTH_20:
558 return CHAN_WIDTH_20;
559 case NL80211_CHAN_WIDTH_40:
560 return CHAN_WIDTH_40;
561 case NL80211_CHAN_WIDTH_80:
562 return CHAN_WIDTH_80;
563 case NL80211_CHAN_WIDTH_80P80:
564 return CHAN_WIDTH_80P80;
565 case NL80211_CHAN_WIDTH_160:
566 return CHAN_WIDTH_160;
567 }
568 return CHAN_WIDTH_UNKNOWN;
569}
570
571
b1f625e0
EP
572static int is_ap_interface(enum nl80211_iftype nlmode)
573{
0e80ea2c
JM
574 return nlmode == NL80211_IFTYPE_AP ||
575 nlmode == NL80211_IFTYPE_P2P_GO;
b1f625e0
EP
576}
577
578
579static int is_sta_interface(enum nl80211_iftype nlmode)
580{
0e80ea2c
JM
581 return nlmode == NL80211_IFTYPE_STATION ||
582 nlmode == NL80211_IFTYPE_P2P_CLIENT;
b1f625e0
EP
583}
584
585
6a71413e 586static int is_p2p_net_interface(enum nl80211_iftype nlmode)
b3af99d2 587{
0e80ea2c
JM
588 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
589 nlmode == NL80211_IFTYPE_P2P_GO;
b3af99d2
JM
590}
591
592
5dfbd725
JM
593static struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
594 int ifindex)
595{
596 struct i802_bss *bss;
597
598 for (bss = drv->first_bss; bss; bss = bss->next) {
599 if (bss->ifindex == ifindex)
600 return bss;
601 }
602
603 return NULL;
604}
605
606
add9b7a4
JM
607static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
608{
609 if (drv->associated)
610 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
611 drv->associated = 0;
612 os_memset(drv->bssid, 0, ETH_ALEN);
613}
614
615
f5a8d422
JM
616struct nl80211_bss_info_arg {
617 struct wpa_driver_nl80211_data *drv;
618 struct wpa_scan_results *res;
619 unsigned int assoc_freq;
c7caac56 620 unsigned int ibss_freq;
20f5a4c2 621 u8 assoc_bssid[ETH_ALEN];
f5a8d422
JM
622};
623
624static int bss_info_handler(struct nl_msg *msg, void *arg);
625
626
6241fcb1
JM
627/* nl80211 code */
628static int ack_handler(struct nl_msg *msg, void *arg)
629{
630 int *err = arg;
631 *err = 0;
632 return NL_STOP;
633}
634
635static int finish_handler(struct nl_msg *msg, void *arg)
636{
8e8df255
JM
637 int *ret = arg;
638 *ret = 0;
6241fcb1
JM
639 return NL_SKIP;
640}
641
642static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
643 void *arg)
644{
645 int *ret = arg;
646 *ret = err->error;
647 return NL_SKIP;
648}
649
5b7b85f6
JM
650
651static int no_seq_check(struct nl_msg *msg, void *arg)
652{
653 return NL_OK;
654}
655
656
d6c9aab8 657static int send_and_recv(struct nl80211_global *global,
58f6fbe0
JM
658 struct nl_handle *nl_handle, struct nl_msg *msg,
659 int (*valid_handler)(struct nl_msg *, void *),
660 void *valid_data)
6241fcb1
JM
661{
662 struct nl_cb *cb;
663 int err = -ENOMEM;
664
d6c9aab8 665 cb = nl_cb_clone(global->nl_cb);
6241fcb1
JM
666 if (!cb)
667 goto out;
668
58f6fbe0 669 err = nl_send_auto_complete(nl_handle, msg);
6241fcb1
JM
670 if (err < 0)
671 goto out;
672
673 err = 1;
674
675 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
8e8df255 676 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6241fcb1
JM
677 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
678
679 if (valid_handler)
680 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
681 valid_handler, valid_data);
682
34068ac3
JM
683 while (err > 0) {
684 int res = nl_recvmsgs(nl_handle, cb);
d3d04831 685 if (res < 0) {
34068ac3
JM
686 wpa_printf(MSG_INFO,
687 "nl80211: %s->nl_recvmsgs failed: %d",
688 __func__, res);
689 }
690 }
6241fcb1
JM
691 out:
692 nl_cb_put(cb);
693 nlmsg_free(msg);
694 return err;
695}
696
697
d6c9aab8
JB
698static int send_and_recv_msgs_global(struct nl80211_global *global,
699 struct nl_msg *msg,
700 int (*valid_handler)(struct nl_msg *, void *),
701 void *valid_data)
702{
481234cf 703 return send_and_recv(global, global->nl, msg, valid_handler,
d6c9aab8
JB
704 valid_data);
705}
706
707
98218963
DS
708static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
709 struct nl_msg *msg,
710 int (*valid_handler)(struct nl_msg *, void *),
711 void *valid_data)
58f6fbe0 712{
481234cf 713 return send_and_recv(drv->global, drv->global->nl, msg,
d6c9aab8 714 valid_handler, valid_data);
58f6fbe0
JM
715}
716
717
97865538
JM
718struct family_data {
719 const char *group;
720 int id;
721};
722
723
d3aaef80
DS
724static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
725{
726 if (bss->wdev_id_set)
727 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
728 else
729 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
730 return 0;
731
732nla_put_failure:
733 return -1;
734}
735
736
97865538
JM
737static int family_handler(struct nl_msg *msg, void *arg)
738{
739 struct family_data *res = arg;
740 struct nlattr *tb[CTRL_ATTR_MAX + 1];
741 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
742 struct nlattr *mcgrp;
743 int i;
744
745 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
746 genlmsg_attrlen(gnlh, 0), NULL);
747 if (!tb[CTRL_ATTR_MCAST_GROUPS])
748 return NL_SKIP;
749
750 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
751 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
752 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
753 nla_len(mcgrp), NULL);
754 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
755 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
756 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
757 res->group,
758 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
759 continue;
760 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
761 break;
762 };
763
764 return NL_SKIP;
765}
766
767
d6c9aab8 768static int nl_get_multicast_id(struct nl80211_global *global,
97865538
JM
769 const char *family, const char *group)
770{
771 struct nl_msg *msg;
772 int ret = -1;
773 struct family_data res = { group, -ENOENT };
774
775 msg = nlmsg_alloc();
776 if (!msg)
777 return -ENOMEM;
481234cf 778 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
97865538
JM
779 0, 0, CTRL_CMD_GETFAMILY, 0);
780 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
781
d6c9aab8 782 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
97865538
JM
783 msg = NULL;
784 if (ret == 0)
785 ret = res.id;
786
787nla_put_failure:
788 nlmsg_free(msg);
789 return ret;
790}
791
792
9fb04070
JM
793static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
794 struct nl_msg *msg, int flags, uint8_t cmd)
795{
335d42b1 796 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
276e2d67 797 0, flags, cmd, 0);
9fb04070
JM
798}
799
800
e32ad281
JB
801struct wiphy_idx_data {
802 int wiphy_idx;
01517c8b 803 enum nl80211_iftype nlmode;
597b94f5 804 u8 *macaddr;
e32ad281
JB
805};
806
807
808static int netdev_info_handler(struct nl_msg *msg, void *arg)
809{
810 struct nlattr *tb[NL80211_ATTR_MAX + 1];
811 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
812 struct wiphy_idx_data *info = arg;
813
814 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
815 genlmsg_attrlen(gnlh, 0), NULL);
816
817 if (tb[NL80211_ATTR_WIPHY])
818 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
819
01517c8b
JB
820 if (tb[NL80211_ATTR_IFTYPE])
821 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
822
597b94f5
AS
823 if (tb[NL80211_ATTR_MAC] && info->macaddr)
824 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
825 ETH_ALEN);
826
e32ad281
JB
827 return NL_SKIP;
828}
829
830
831static int nl80211_get_wiphy_index(struct i802_bss *bss)
832{
833 struct nl_msg *msg;
834 struct wiphy_idx_data data = {
835 .wiphy_idx = -1,
597b94f5 836 .macaddr = NULL,
e32ad281
JB
837 };
838
839 msg = nlmsg_alloc();
840 if (!msg)
8e12685c 841 return NL80211_IFTYPE_UNSPECIFIED;
e32ad281
JB
842
843 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
844
27ce1d64
AS
845 if (nl80211_set_iface_id(msg, bss) < 0)
846 goto nla_put_failure;
e32ad281
JB
847
848 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
849 return data.wiphy_idx;
850 msg = NULL;
851nla_put_failure:
852 nlmsg_free(msg);
853 return -1;
854}
855
856
01517c8b
JB
857static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
858{
859 struct nl_msg *msg;
860 struct wiphy_idx_data data = {
597b94f5
AS
861 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
862 .macaddr = NULL,
01517c8b
JB
863 };
864
865 msg = nlmsg_alloc();
866 if (!msg)
867 return -1;
868
869 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
870
f632e483
AS
871 if (nl80211_set_iface_id(msg, bss) < 0)
872 goto nla_put_failure;
01517c8b
JB
873
874 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
875 return data.nlmode;
876 msg = NULL;
877nla_put_failure:
878 nlmsg_free(msg);
879 return NL80211_IFTYPE_UNSPECIFIED;
880}
01517c8b
JB
881
882
597b94f5
AS
883static int nl80211_get_macaddr(struct i802_bss *bss)
884{
885 struct nl_msg *msg;
886 struct wiphy_idx_data data = {
887 .macaddr = bss->addr,
888 };
889
890 msg = nlmsg_alloc();
891 if (!msg)
892 return NL80211_IFTYPE_UNSPECIFIED;
893
894 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
895 if (nl80211_set_iface_id(msg, bss) < 0)
896 goto nla_put_failure;
897
898 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
899
900nla_put_failure:
901 nlmsg_free(msg);
902 return NL80211_IFTYPE_UNSPECIFIED;
903}
597b94f5
AS
904
905
e32ad281
JB
906static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
907 struct nl80211_wiphy_data *w)
908{
909 struct nl_msg *msg;
910 int ret = -1;
911
912 msg = nlmsg_alloc();
913 if (!msg)
914 return -1;
915
916 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
917
918 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
919
481234cf 920 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
e32ad281
JB
921 msg = NULL;
922 if (ret) {
923 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
924 "failed: ret=%d (%s)",
925 ret, strerror(-ret));
926 goto nla_put_failure;
927 }
928 ret = 0;
929nla_put_failure:
930 nlmsg_free(msg);
931 return ret;
932}
933
934
935static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
936{
937 struct nl80211_wiphy_data *w = eloop_ctx;
34068ac3 938 int res;
e32ad281 939
ee9fc67a 940 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
e32ad281 941
34068ac3 942 res = nl_recvmsgs(handle, w->nl_cb);
d3d04831 943 if (res < 0) {
34068ac3
JM
944 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
945 __func__, res);
946 }
e32ad281
JB
947}
948
949
950static int process_beacon_event(struct nl_msg *msg, void *arg)
951{
952 struct nl80211_wiphy_data *w = arg;
953 struct wpa_driver_nl80211_data *drv;
954 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
955 struct nlattr *tb[NL80211_ATTR_MAX + 1];
956 union wpa_event_data event;
957
958 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
959 genlmsg_attrlen(gnlh, 0), NULL);
960
961 if (gnlh->cmd != NL80211_CMD_FRAME) {
962 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
963 gnlh->cmd);
964 return NL_SKIP;
965 }
966
967 if (!tb[NL80211_ATTR_FRAME])
968 return NL_SKIP;
969
970 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
971 wiphy_list) {
972 os_memset(&event, 0, sizeof(event));
973 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
974 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
975 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
976 }
977
978 return NL_SKIP;
979}
980
981
982static struct nl80211_wiphy_data *
983nl80211_get_wiphy_data_ap(struct i802_bss *bss)
984{
985 static DEFINE_DL_LIST(nl80211_wiphys);
986 struct nl80211_wiphy_data *w;
987 int wiphy_idx, found = 0;
988 struct i802_bss *tmp_bss;
989
990 if (bss->wiphy_data != NULL)
991 return bss->wiphy_data;
992
993 wiphy_idx = nl80211_get_wiphy_index(bss);
994
995 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
996 if (w->wiphy_idx == wiphy_idx)
997 goto add;
998 }
999
1000 /* alloc new one */
1001 w = os_zalloc(sizeof(*w));
1002 if (w == NULL)
1003 return NULL;
1004 w->wiphy_idx = wiphy_idx;
1005 dl_list_init(&w->bsss);
1006 dl_list_init(&w->drvs);
1007
1008 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1009 if (!w->nl_cb) {
1010 os_free(w);
1011 return NULL;
1012 }
1013 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
1014 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
1015 w);
1016
481234cf
JM
1017 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
1018 "wiphy beacons");
1019 if (w->nl_beacons == NULL) {
e32ad281
JB
1020 os_free(w);
1021 return NULL;
1022 }
1023
1024 if (nl80211_register_beacons(bss->drv, w)) {
1025 nl_destroy_handles(&w->nl_beacons);
1026 os_free(w);
1027 return NULL;
1028 }
1029
5f65e9f7 1030 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
e32ad281
JB
1031
1032 dl_list_add(&nl80211_wiphys, &w->list);
1033
1034add:
1035 /* drv entry for this bss already there? */
1036 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1037 if (tmp_bss->drv == bss->drv) {
1038 found = 1;
1039 break;
1040 }
1041 }
1042 /* if not add it */
1043 if (!found)
1044 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
1045
1046 dl_list_add(&w->bsss, &bss->wiphy_list);
1047 bss->wiphy_data = w;
1048 return w;
1049}
1050
1051
1052static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1053{
1054 struct nl80211_wiphy_data *w = bss->wiphy_data;
1055 struct i802_bss *tmp_bss;
1056 int found = 0;
1057
1058 if (w == NULL)
1059 return;
1060 bss->wiphy_data = NULL;
1061 dl_list_del(&bss->wiphy_list);
1062
1063 /* still any for this drv present? */
1064 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1065 if (tmp_bss->drv == bss->drv) {
1066 found = 1;
1067 break;
1068 }
1069 }
1070 /* if not remove it */
1071 if (!found)
1072 dl_list_del(&bss->drv->wiphy_list);
1073
1074 if (!dl_list_empty(&w->bsss))
1075 return;
1076
5f65e9f7 1077 nl80211_destroy_eloop_handle(&w->nl_beacons);
e32ad281
JB
1078
1079 nl_cb_put(w->nl_cb);
e32ad281
JB
1080 dl_list_del(&w->list);
1081 os_free(w);
1082}
1083
1084
3f5285e8
JM
1085static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1086{
a2e40bb6
FF
1087 struct i802_bss *bss = priv;
1088 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
1089 if (!drv->associated)
1090 return -1;
1091 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1092 return 0;
3f5285e8
JM
1093}
1094
1095
3f5285e8
JM
1096static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1097{
a2e40bb6
FF
1098 struct i802_bss *bss = priv;
1099 struct wpa_driver_nl80211_data *drv = bss->drv;
fd05d64e
JM
1100 if (!drv->associated)
1101 return -1;
1102 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1103 return drv->ssid_len;
3f5285e8
JM
1104}
1105
1106
90a545cc
JM
1107static void wpa_driver_nl80211_event_newlink(
1108 struct wpa_driver_nl80211_data *drv, char *ifname)
3f5285e8
JM
1109{
1110 union wpa_event_data event;
1111
90a545cc
JM
1112 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1113 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1114 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1115 drv->first_bss->ifname);
1116 return;
1117 }
1118 if (!drv->if_removed)
1119 return;
1120 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1121 drv->first_bss->ifname);
1122 drv->if_removed = 0;
1123 }
1124
3f5285e8 1125 os_memset(&event, 0, sizeof(event));
90a545cc
JM
1126 os_strlcpy(event.interface_status.ifname, ifname,
1127 sizeof(event.interface_status.ifname));
1128 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1129 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1130}
1131
1132
1133static void wpa_driver_nl80211_event_dellink(
1134 struct wpa_driver_nl80211_data *drv, char *ifname)
1135{
1136 union wpa_event_data event;
1137
1138 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1139 if (drv->if_removed) {
1140 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1141 ifname);
1142 return;
d1f4942b 1143 }
90a545cc
JM
1144 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1145 ifname);
1146 drv->if_removed = 1;
1147 } else {
1148 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1149 ifname);
7524cfb1
JM
1150 }
1151
90a545cc
JM
1152 os_memset(&event, 0, sizeof(event));
1153 os_strlcpy(event.interface_status.ifname, ifname,
1154 sizeof(event.interface_status.ifname));
1155 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
08063178 1156 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
3f5285e8
JM
1157}
1158
1159
7524cfb1 1160static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
62d680c3 1161 u8 *buf, size_t len)
7524cfb1 1162{
62d680c3 1163 int attrlen, rta_len;
7524cfb1
JM
1164 struct rtattr *attr;
1165
62d680c3
JM
1166 attrlen = len;
1167 attr = (struct rtattr *) buf;
7524cfb1
JM
1168
1169 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1170 while (RTA_OK(attr, attrlen)) {
1171 if (attr->rta_type == IFLA_IFNAME) {
834ee56f
KP
1172 if (os_strcmp(((char *) attr) + rta_len,
1173 drv->first_bss->ifname) == 0)
7524cfb1
JM
1174 return 1;
1175 else
1176 break;
1177 }
1178 attr = RTA_NEXT(attr, attrlen);
1179 }
1180
1181 return 0;
1182}
1183
1184
1185static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
62d680c3 1186 int ifindex, u8 *buf, size_t len)
7524cfb1
JM
1187{
1188 if (drv->ifindex == ifindex)
1189 return 1;
1190
62d680c3 1191 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
7524cfb1
JM
1192 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1193 "interface");
49b4b205 1194 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
7524cfb1
JM
1195 return 1;
1196 }
1197
1198 return 0;
1199}
1200
1201
36d84860
BG
1202static struct wpa_driver_nl80211_data *
1203nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1204{
1205 struct wpa_driver_nl80211_data *drv;
1206 dl_list_for_each(drv, &global->interfaces,
1207 struct wpa_driver_nl80211_data, list) {
1208 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1209 have_ifidx(drv, idx))
1210 return drv;
1211 }
1212 return NULL;
1213}
1214
1215
62d680c3
JM
1216static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1217 struct ifinfomsg *ifi,
1218 u8 *buf, size_t len)
3f5285e8 1219{
36d84860
BG
1220 struct nl80211_global *global = ctx;
1221 struct wpa_driver_nl80211_data *drv;
90a545cc 1222 int attrlen;
62d680c3 1223 struct rtattr *attr;
97cfcf64 1224 u32 brid = 0;
aef85ba2 1225 char namebuf[IFNAMSIZ];
90a545cc
JM
1226 char ifname[IFNAMSIZ + 1];
1227 char extra[100], *pos, *end;
3f5285e8 1228
36d84860
BG
1229 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1230 if (!drv) {
90a545cc
JM
1231 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1232 ifi->ifi_index);
3f5285e8
JM
1233 return;
1234 }
1235
90a545cc
JM
1236 extra[0] = '\0';
1237 pos = extra;
1238 end = pos + sizeof(extra);
1239 ifname[0] = '\0';
1240
1241 attrlen = len;
1242 attr = (struct rtattr *) buf;
1243 while (RTA_OK(attr, attrlen)) {
1244 switch (attr->rta_type) {
1245 case IFLA_IFNAME:
1246 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1247 break;
1248 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1249 ifname[RTA_PAYLOAD(attr)] = '\0';
1250 break;
1251 case IFLA_MASTER:
1252 brid = nla_get_u32((struct nlattr *) attr);
1253 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1254 break;
1255 case IFLA_WIRELESS:
1256 pos += os_snprintf(pos, end - pos, " wext");
1257 break;
1258 case IFLA_OPERSTATE:
1259 pos += os_snprintf(pos, end - pos, " operstate=%u",
1260 nla_get_u32((struct nlattr *) attr));
1261 break;
1262 case IFLA_LINKMODE:
1263 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1264 nla_get_u32((struct nlattr *) attr));
1265 break;
1266 }
1267 attr = RTA_NEXT(attr, attrlen);
1268 }
1269 extra[sizeof(extra) - 1] = '\0';
1270
f2e90835
JM
1271 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1272 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1273 ifi->ifi_flags,
3f5285e8
JM
1274 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1275 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1276 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1277 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
a63063b4
JM
1278
1279 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
59d24925
JM
1280 if (if_indextoname(ifi->ifi_index, namebuf) &&
1281 linux_iface_up(drv->global->ioctl_sock,
834ee56f 1282 drv->first_bss->ifname) > 0) {
59d24925
JM
1283 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1284 "event since interface %s is up", namebuf);
1285 return;
1286 }
a63063b4 1287 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
7d9c3698
JM
1288 if (drv->ignore_if_down_event) {
1289 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1290 "event generated by mode change");
1291 drv->ignore_if_down_event = 0;
1292 } else {
1293 drv->if_disabled = 1;
1294 wpa_supplicant_event(drv->ctx,
1295 EVENT_INTERFACE_DISABLED, NULL);
819f096f
AO
1296
1297 /*
1298 * Try to get drv again, since it may be removed as
1299 * part of the EVENT_INTERFACE_DISABLED handling for
1300 * dynamic interfaces
1301 */
1302 drv = nl80211_find_drv(global, ifi->ifi_index,
1303 buf, len);
1304 if (!drv)
1305 return;
7d9c3698 1306 }
a63063b4
JM
1307 }
1308
1309 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
aef85ba2
JM
1310 if (if_indextoname(ifi->ifi_index, namebuf) &&
1311 linux_iface_up(drv->global->ioctl_sock,
834ee56f 1312 drv->first_bss->ifname) == 0) {
aef85ba2
JM
1313 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1314 "event since interface %s is down",
1315 namebuf);
834ee56f 1316 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
d1f4942b
JM
1317 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1318 "event since interface %s does not exist",
834ee56f 1319 drv->first_bss->ifname);
d1f4942b
JM
1320 } else if (drv->if_removed) {
1321 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1322 "event since interface %s is marked "
834ee56f 1323 "removed", drv->first_bss->ifname);
aef85ba2 1324 } else {
3e0272ca
DW
1325 struct i802_bss *bss;
1326 u8 addr[ETH_ALEN];
1327
1328 /* Re-read MAC address as it may have changed */
1329 bss = get_bss_ifindex(drv, ifi->ifi_index);
1330 if (bss &&
1331 linux_get_ifhwaddr(drv->global->ioctl_sock,
1332 bss->ifname, addr) < 0) {
1333 wpa_printf(MSG_DEBUG,
1334 "nl80211: %s: failed to re-read MAC address",
1335 bss->ifname);
1336 } else if (bss &&
1337 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1338 wpa_printf(MSG_DEBUG,
1339 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1340 MACSTR " to " MACSTR,
1341 ifi->ifi_index, bss->ifname,
1342 MAC2STR(bss->addr),
1343 MAC2STR(addr));
1344 os_memcpy(bss->addr, addr, ETH_ALEN);
1345 }
1346
aef85ba2
JM
1347 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1348 drv->if_disabled = 0;
1349 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1350 NULL);
1351 }
a63063b4
JM
1352 }
1353
3f5285e8
JM
1354 /*
1355 * Some drivers send the association event before the operup event--in
1356 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1357 * fails. This will hit us when wpa_supplicant does not need to do
1358 * IEEE 802.1X authentication
1359 */
1360 if (drv->operstate == 1 &&
1361 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
90a545cc
JM
1362 !(ifi->ifi_flags & IFF_RUNNING)) {
1363 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
36d84860 1364 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 1365 -1, IF_OPER_UP);
3f5285e8 1366 }
97cfcf64 1367
90a545cc
JM
1368 if (ifname[0])
1369 wpa_driver_nl80211_event_newlink(drv, ifname);
1370
97cfcf64
B
1371 if (ifi->ifi_family == AF_BRIDGE && brid) {
1372 /* device has been added to bridge */
97cfcf64
B
1373 if_indextoname(brid, namebuf);
1374 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1375 brid, namebuf);
1376 add_ifidx(drv, brid);
1377 }
3f5285e8
JM
1378}
1379
1380
62d680c3
JM
1381static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1382 struct ifinfomsg *ifi,
1383 u8 *buf, size_t len)
3f5285e8 1384{
36d84860
BG
1385 struct nl80211_global *global = ctx;
1386 struct wpa_driver_nl80211_data *drv;
90a545cc 1387 int attrlen;
62d680c3 1388 struct rtattr *attr;
97cfcf64 1389 u32 brid = 0;
90a545cc 1390 char ifname[IFNAMSIZ + 1];
f2e90835 1391 char extra[100], *pos, *end;
3f5285e8 1392
36d84860
BG
1393 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1394 if (!drv) {
90a545cc
JM
1395 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1396 ifi->ifi_index);
36d84860
BG
1397 return;
1398 }
1399
f2e90835
JM
1400 extra[0] = '\0';
1401 pos = extra;
1402 end = pos + sizeof(extra);
90a545cc
JM
1403 ifname[0] = '\0';
1404
62d680c3
JM
1405 attrlen = len;
1406 attr = (struct rtattr *) buf;
3f5285e8 1407 while (RTA_OK(attr, attrlen)) {
90a545cc
JM
1408 switch (attr->rta_type) {
1409 case IFLA_IFNAME:
1410 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1411 break;
1412 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1413 ifname[RTA_PAYLOAD(attr)] = '\0';
1414 break;
1415 case IFLA_MASTER:
97cfcf64 1416 brid = nla_get_u32((struct nlattr *) attr);
f2e90835
JM
1417 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1418 break;
1419 case IFLA_OPERSTATE:
1420 pos += os_snprintf(pos, end - pos, " operstate=%u",
1421 nla_get_u32((struct nlattr *) attr));
1422 break;
1423 case IFLA_LINKMODE:
1424 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1425 nla_get_u32((struct nlattr *) attr));
90a545cc
JM
1426 break;
1427 }
3f5285e8
JM
1428 attr = RTA_NEXT(attr, attrlen);
1429 }
f2e90835
JM
1430 extra[sizeof(extra) - 1] = '\0';
1431
1432 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1433 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1434 ifi->ifi_flags,
1435 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1436 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1437 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1438 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
97cfcf64 1439
728ff2f4 1440 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
90a545cc
JM
1441 wpa_driver_nl80211_event_dellink(drv, ifname);
1442
97cfcf64
B
1443 if (ifi->ifi_family == AF_BRIDGE && brid) {
1444 /* device has been removed from bridge */
1445 char namebuf[IFNAMSIZ];
1446 if_indextoname(brid, namebuf);
1447 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1448 "%s", brid, namebuf);
1449 del_ifidx(drv, brid);
1450 }
3f5285e8
JM
1451}
1452
1453
c2a04078
JM
1454static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1455 const u8 *frame, size_t len)
1456{
1457 const struct ieee80211_mgmt *mgmt;
1458 union wpa_event_data event;
1459
b497a212
JM
1460 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1461 drv->force_connect_cmd) {
1462 /*
1463 * Avoid reporting two association events that would confuse
1464 * the core code.
1465 */
1466 wpa_printf(MSG_DEBUG,
1467 "nl80211: Ignore auth event when using driver SME");
1468 return;
1469 }
1470
7d81932d 1471 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
c2a04078
JM
1472 mgmt = (const struct ieee80211_mgmt *) frame;
1473 if (len < 24 + sizeof(mgmt->u.auth)) {
1474 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1475 "frame");
1476 return;
1477 }
1478
e6b8efeb 1479 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
add9b7a4 1480 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
c2a04078
JM
1481 os_memset(&event, 0, sizeof(event));
1482 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1483 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
569fed90
JM
1484 event.auth.auth_transaction =
1485 le_to_host16(mgmt->u.auth.auth_transaction);
c2a04078
JM
1486 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1487 if (len > 24 + sizeof(mgmt->u.auth)) {
1488 event.auth.ies = mgmt->u.auth.variable;
1489 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1490 }
1491
1492 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1493}
1494
1495
f5a8d422
JM
1496static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1497{
1498 struct nl_msg *msg;
1499 int ret;
1500 struct nl80211_bss_info_arg arg;
1501
1502 os_memset(&arg, 0, sizeof(arg));
1503 msg = nlmsg_alloc();
1504 if (!msg)
1505 goto nla_put_failure;
1506
9fb04070 1507 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
f5a8d422
JM
1508 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1509
1510 arg.drv = drv;
1511 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1512 msg = NULL;
1513 if (ret == 0) {
c7caac56
JM
1514 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1515 arg.ibss_freq : arg.assoc_freq;
f5a8d422 1516 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
c7caac56
JM
1517 "associated BSS from scan results: %u MHz", freq);
1518 if (freq)
1519 drv->assoc_freq = freq;
30158a0d 1520 return drv->assoc_freq;
f5a8d422
JM
1521 }
1522 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1523 "(%s)", ret, strerror(-ret));
1524nla_put_failure:
1525 nlmsg_free(msg);
1526 return drv->assoc_freq;
1527}
1528
1529
c2a04078
JM
1530static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1531 const u8 *frame, size_t len)
1532{
1533 const struct ieee80211_mgmt *mgmt;
1534 union wpa_event_data event;
1535 u16 status;
1536
b497a212
JM
1537 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1538 drv->force_connect_cmd) {
1539 /*
1540 * Avoid reporting two association events that would confuse
1541 * the core code.
1542 */
1543 wpa_printf(MSG_DEBUG,
1544 "nl80211: Ignore assoc event when using driver SME");
1545 return;
1546 }
1547
7d81932d 1548 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
c2a04078
JM
1549 mgmt = (const struct ieee80211_mgmt *) frame;
1550 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1551 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1552 "frame");
1553 return;
1554 }
1555
1556 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1557 if (status != WLAN_STATUS_SUCCESS) {
efa46078 1558 os_memset(&event, 0, sizeof(event));
59ddf221 1559 event.assoc_reject.bssid = mgmt->bssid;
efa46078
JM
1560 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1561 event.assoc_reject.resp_ies =
1562 (u8 *) mgmt->u.assoc_resp.variable;
1563 event.assoc_reject.resp_ies_len =
1564 len - 24 - sizeof(mgmt->u.assoc_resp);
1565 }
1566 event.assoc_reject.status_code = status;
1567
1568 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
c2a04078
JM
1569 return;
1570 }
1571
1572 drv->associated = 1;
1573 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
add9b7a4 1574 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
c2a04078
JM
1575
1576 os_memset(&event, 0, sizeof(event));
1577 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1578 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1579 event.assoc_info.resp_ies_len =
efa46078 1580 len - 24 - sizeof(mgmt->u.assoc_resp);
c2a04078
JM
1581 }
1582
4832ecd7
JM
1583 event.assoc_info.freq = drv->assoc_freq;
1584
c2a04078
JM
1585 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1586}
1587
c1bb3e0a 1588
da72a1c1
ZY
1589static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1590 enum nl80211_commands cmd, struct nlattr *status,
1591 struct nlattr *addr, struct nlattr *req_ie,
b41f2684
CL
1592 struct nlattr *resp_ie,
1593 struct nlattr *authorized,
1594 struct nlattr *key_replay_ctr,
1595 struct nlattr *ptk_kck,
1596 struct nlattr *ptk_kek)
da72a1c1
ZY
1597{
1598 union wpa_event_data event;
1599
7da2c527
JM
1600 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1601 /*
1602 * Avoid reporting two association events that would confuse
1603 * the core code.
1604 */
1605 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1606 "when using userspace SME", cmd);
1607 return;
1608 }
1609
7d81932d
JM
1610 if (cmd == NL80211_CMD_CONNECT)
1611 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1612 else if (cmd == NL80211_CMD_ROAM)
1613 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1614
da72a1c1
ZY
1615 os_memset(&event, 0, sizeof(event));
1616 if (cmd == NL80211_CMD_CONNECT &&
1617 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
df89c1c8
JM
1618 if (addr)
1619 event.assoc_reject.bssid = nla_data(addr);
da72a1c1
ZY
1620 if (resp_ie) {
1621 event.assoc_reject.resp_ies = nla_data(resp_ie);
1622 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1623 }
1624 event.assoc_reject.status_code = nla_get_u16(status);
1625 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1626 return;
1627 }
1628
1629 drv->associated = 1;
add9b7a4 1630 if (addr) {
da72a1c1 1631 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
add9b7a4
JM
1632 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1633 }
da72a1c1
ZY
1634
1635 if (req_ie) {
1636 event.assoc_info.req_ies = nla_data(req_ie);
1637 event.assoc_info.req_ies_len = nla_len(req_ie);
1638 }
1639 if (resp_ie) {
1640 event.assoc_info.resp_ies = nla_data(resp_ie);
1641 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1642 }
1643
f5a8d422
JM
1644 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1645
b41f2684
CL
1646 if (authorized && nla_get_u8(authorized)) {
1647 event.assoc_info.authorized = 1;
1648 wpa_printf(MSG_DEBUG, "nl80211: connection authorized");
1649 }
1650 if (key_replay_ctr) {
1651 event.assoc_info.key_replay_ctr = nla_data(key_replay_ctr);
1652 event.assoc_info.key_replay_ctr_len = nla_len(key_replay_ctr);
1653 }
1654 if (ptk_kck) {
1655 event.assoc_info.ptk_kck = nla_data(ptk_kck);
1656 event.assoc_info.ptk_kck_len = nla_len(ptk_kek);
1657 }
1658 if (ptk_kek) {
1659 event.assoc_info.ptk_kek = nla_data(ptk_kek);
1660 event.assoc_info.ptk_kek_len = nla_len(ptk_kek);
1661 }
1662
da72a1c1
ZY
1663 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1664}
c2a04078 1665
c1bb3e0a 1666
20f5a4c2 1667static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
3d9975d5
JM
1668 struct nlattr *reason, struct nlattr *addr,
1669 struct nlattr *by_ap)
20f5a4c2
JM
1670{
1671 union wpa_event_data data;
a8c5b43a 1672 unsigned int locally_generated = by_ap == NULL;
20f5a4c2
JM
1673
1674 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1675 /*
1676 * Avoid reporting two disassociation events that could
1677 * confuse the core code.
1678 */
1679 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1680 "event when using userspace SME");
1681 return;
1682 }
1683
a8c5b43a
CW
1684 if (drv->ignore_next_local_disconnect) {
1685 drv->ignore_next_local_disconnect = 0;
1686 if (locally_generated) {
1687 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1688 "event triggered during reassociation");
1689 return;
1690 }
1691 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1692 "disconnect but got another disconnect "
1693 "event first");
1694 }
1695
7d81932d 1696 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
add9b7a4 1697 nl80211_mark_disconnected(drv);
20f5a4c2
JM
1698 os_memset(&data, 0, sizeof(data));
1699 if (reason)
2e9f078c
JM
1700 data.deauth_info.reason_code = nla_get_u16(reason);
1701 data.deauth_info.locally_generated = by_ap == NULL;
1702 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
20f5a4c2
JM
1703}
1704
1705
4b0f2282
MK
1706static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1707{
1708 int freq1 = 0;
1709
1710 switch (convert2width(width)) {
1711 case CHAN_WIDTH_20_NOHT:
1712 case CHAN_WIDTH_20:
1713 return 0;
1714 case CHAN_WIDTH_40:
1715 freq1 = cf1 - 10;
1716 break;
1717 case CHAN_WIDTH_80:
1718 freq1 = cf1 - 30;
1719 break;
1720 case CHAN_WIDTH_160:
1721 freq1 = cf1 - 70;
1722 break;
1723 case CHAN_WIDTH_UNKNOWN:
1724 case CHAN_WIDTH_80P80:
1725 /* FIXME: implement this */
1726 return 0;
1727 }
1728
1729 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1730}
1731
1732
1b487b8b 1733static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
8d1fdde7
JD
1734 struct nlattr *ifindex, struct nlattr *freq,
1735 struct nlattr *type, struct nlattr *bw,
1736 struct nlattr *cf1, struct nlattr *cf2)
1b487b8b 1737{
8d1fdde7 1738 struct i802_bss *bss;
1b487b8b
TP
1739 union wpa_event_data data;
1740 int ht_enabled = 1;
1741 int chan_offset = 0;
8d1fdde7 1742 int ifidx;
1b487b8b
TP
1743
1744 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1745
8d1fdde7 1746 if (!freq)
1b487b8b
TP
1747 return;
1748
8d1fdde7 1749 ifidx = nla_get_u32(ifindex);
5dfbd725 1750 bss = get_bss_ifindex(drv, ifidx);
8d1fdde7
JD
1751 if (bss == NULL) {
1752 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1753 ifidx);
1754 return;
1b487b8b
TP
1755 }
1756
8d1fdde7
JD
1757 if (type) {
1758 switch (nla_get_u32(type)) {
1759 case NL80211_CHAN_NO_HT:
1760 ht_enabled = 0;
1761 break;
1762 case NL80211_CHAN_HT20:
1763 break;
1764 case NL80211_CHAN_HT40PLUS:
1765 chan_offset = 1;
1766 break;
1767 case NL80211_CHAN_HT40MINUS:
1768 chan_offset = -1;
1769 break;
1770 }
4b0f2282
MK
1771 } else if (bw && cf1) {
1772 /* This can happen for example with VHT80 ch switch */
1773 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1774 nla_get_u32(freq),
1775 nla_get_u32(cf1),
1776 cf2 ? nla_get_u32(cf2) : 0);
1777 } else {
1778 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
8d1fdde7
JD
1779 }
1780
1781 os_memset(&data, 0, sizeof(data));
1b487b8b
TP
1782 data.ch_switch.freq = nla_get_u32(freq);
1783 data.ch_switch.ht_enabled = ht_enabled;
1784 data.ch_switch.ch_offset = chan_offset;
8d1fdde7
JD
1785 if (bw)
1786 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1787 if (cf1)
1788 data.ch_switch.cf1 = nla_get_u32(cf1);
1789 if (cf2)
1790 data.ch_switch.cf2 = nla_get_u32(cf2);
1b487b8b 1791
8d1fdde7 1792 bss->freq = data.ch_switch.freq;
1c4ffa87 1793
6782b684 1794 wpa_supplicant_event(bss->ctx, EVENT_CH_SWITCH, &data);
1b487b8b
TP
1795}
1796
1797
da1fb17c
JM
1798static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1799 enum nl80211_commands cmd, struct nlattr *addr)
1800{
1801 union wpa_event_data event;
1802 enum wpa_event_type ev;
1803
1804 if (nla_len(addr) != ETH_ALEN)
1805 return;
1806
1807 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1808 cmd, MAC2STR((u8 *) nla_data(addr)));
1809
1810 if (cmd == NL80211_CMD_AUTHENTICATE)
1811 ev = EVENT_AUTH_TIMED_OUT;
1812 else if (cmd == NL80211_CMD_ASSOCIATE)
1813 ev = EVENT_ASSOC_TIMED_OUT;
1814 else
1815 return;
1816
1817 os_memset(&event, 0, sizeof(event));
1818 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1819 wpa_supplicant_event(drv->ctx, ev, &event);
1820}
1821
1822
1d91f504 1823static void mlme_event_mgmt(struct i802_bss *bss,
da873dbb
JB
1824 struct nlattr *freq, struct nlattr *sig,
1825 const u8 *frame, size_t len)
58f6fbe0 1826{
1d91f504 1827 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0
JM
1828 const struct ieee80211_mgmt *mgmt;
1829 union wpa_event_data event;
1830 u16 fc, stype;
da873dbb 1831 int ssi_signal = 0;
f18b7817 1832 int rx_freq = 0;
58f6fbe0 1833
cc2ada86 1834 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
58f6fbe0
JM
1835 mgmt = (const struct ieee80211_mgmt *) frame;
1836 if (len < 24) {
dbfb8e82 1837 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
58f6fbe0
JM
1838 return;
1839 }
1840
1841 fc = le_to_host16(mgmt->frame_control);
1842 stype = WLAN_FC_GET_STYPE(fc);
1843
da873dbb
JB
1844 if (sig)
1845 ssi_signal = (s32) nla_get_u32(sig);
1846
58f6fbe0 1847 os_memset(&event, 0, sizeof(event));
5582a5d1 1848 if (freq) {
dbfb8e82
JM
1849 event.rx_mgmt.freq = nla_get_u32(freq);
1850 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
5582a5d1 1851 }
f18b7817 1852 wpa_printf(MSG_DEBUG,
f95a4524
PF
1853 "nl80211: RX frame sa=" MACSTR
1854 " freq=%d ssi_signal=%d stype=%u (%s) len=%u",
1855 MAC2STR(mgmt->sa), rx_freq, ssi_signal, stype, fc2str(fc),
dedfa440 1856 (unsigned int) len);
dbfb8e82
JM
1857 event.rx_mgmt.frame = frame;
1858 event.rx_mgmt.frame_len = len;
1859 event.rx_mgmt.ssi_signal = ssi_signal;
1d91f504 1860 event.rx_mgmt.drv_priv = bss;
dbfb8e82 1861 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
58f6fbe0
JM
1862}
1863
1864
a11241fa
JB
1865static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1866 struct nlattr *cookie, const u8 *frame,
1867 size_t len, struct nlattr *ack)
58f6fbe0
JM
1868{
1869 union wpa_event_data event;
1870 const struct ieee80211_hdr *hdr;
1871 u16 fc;
58f6fbe0 1872
7d81932d 1873 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
a11241fa
JB
1874 if (!is_ap_interface(drv->nlmode)) {
1875 u64 cookie_val;
58f6fbe0 1876
a11241fa
JB
1877 if (!cookie)
1878 return;
1879
1880 cookie_val = nla_get_u64(cookie);
1881 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1882 " cookie=0%llx%s (ack=%d)",
1883 (long long unsigned int) cookie_val,
1884 cookie_val == drv->send_action_cookie ?
1885 " (match)" : " (unknown)", ack != NULL);
1886 if (cookie_val != drv->send_action_cookie)
1887 return;
1888 }
58f6fbe0
JM
1889
1890 hdr = (const struct ieee80211_hdr *) frame;
1891 fc = le_to_host16(hdr->frame_control);
1892
1893 os_memset(&event, 0, sizeof(event));
1894 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1895 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1896 event.tx_status.dst = hdr->addr1;
1897 event.tx_status.data = frame;
1898 event.tx_status.data_len = len;
1899 event.tx_status.ack = ack != NULL;
1900 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1901}
1902
1903
0544b242
JM
1904static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1905 enum wpa_event_type type,
1906 const u8 *frame, size_t len)
1907{
1908 const struct ieee80211_mgmt *mgmt;
1909 union wpa_event_data event;
1910 const u8 *bssid = NULL;
1911 u16 reason_code = 0;
1912
7d81932d
JM
1913 if (type == EVENT_DEAUTH)
1914 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1915 else
1916 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1917
cb30b297
PS
1918 mgmt = (const struct ieee80211_mgmt *) frame;
1919 if (len >= 24) {
1920 bssid = mgmt->bssid;
1921
add9b7a4
JM
1922 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1923 !drv->associated &&
1924 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1925 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1926 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1927 /*
1928 * Avoid issues with some roaming cases where
1929 * disconnection event for the old AP may show up after
1930 * we have started connection with the new AP.
1931 */
1932 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1933 MAC2STR(bssid),
1934 MAC2STR(drv->auth_attempt_bssid));
1935 return;
1936 }
1937
cb30b297
PS
1938 if (drv->associated != 0 &&
1939 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1940 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1941 /*
1942 * We have presumably received this deauth as a
1943 * response to a clear_state_mismatch() outgoing
1944 * deauth. Don't let it take us offline!
1945 */
1946 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1947 "from Unknown BSSID " MACSTR " -- ignoring",
1948 MAC2STR(bssid));
1949 return;
1950 }
1951 }
1952
add9b7a4 1953 nl80211_mark_disconnected(drv);
0544b242
JM
1954 os_memset(&event, 0, sizeof(event));
1955
0544b242
JM
1956 /* Note: Same offset for Reason Code in both frame subtypes */
1957 if (len >= 24 + sizeof(mgmt->u.deauth))
1958 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1959
1960 if (type == EVENT_DISASSOC) {
3d9975d5 1961 event.disassoc_info.locally_generated =
834ee56f 1962 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
0544b242
JM
1963 event.disassoc_info.addr = bssid;
1964 event.disassoc_info.reason_code = reason_code;
046b26a2
JM
1965 if (frame + len > mgmt->u.disassoc.variable) {
1966 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1967 event.disassoc_info.ie_len = frame + len -
1968 mgmt->u.disassoc.variable;
1969 }
0544b242 1970 } else {
e8d70a73
JM
1971 if (drv->ignore_deauth_event) {
1972 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1973 drv->ignore_deauth_event = 0;
1974 return;
1975 }
3d9975d5 1976 event.deauth_info.locally_generated =
834ee56f 1977 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
d6a36f39
JM
1978 if (drv->ignore_next_local_deauth) {
1979 drv->ignore_next_local_deauth = 0;
1980 if (event.deauth_info.locally_generated) {
1981 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1982 return;
1983 }
1984 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1985 }
0544b242
JM
1986 event.deauth_info.addr = bssid;
1987 event.deauth_info.reason_code = reason_code;
046b26a2
JM
1988 if (frame + len > mgmt->u.deauth.variable) {
1989 event.deauth_info.ie = mgmt->u.deauth.variable;
1990 event.deauth_info.ie_len = frame + len -
1991 mgmt->u.deauth.variable;
1992 }
0544b242
JM
1993 }
1994
1995 wpa_supplicant_event(drv->ctx, type, &event);
1996}
1997
1998
7d878ca7
JM
1999static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
2000 enum wpa_event_type type,
2001 const u8 *frame, size_t len)
2002{
2003 const struct ieee80211_mgmt *mgmt;
2004 union wpa_event_data event;
2005 u16 reason_code = 0;
2006
7d81932d
JM
2007 if (type == EVENT_UNPROT_DEAUTH)
2008 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
2009 else
2010 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
2011
7d878ca7
JM
2012 if (len < 24)
2013 return;
2014
2015 mgmt = (const struct ieee80211_mgmt *) frame;
2016
2017 os_memset(&event, 0, sizeof(event));
2018 /* Note: Same offset for Reason Code in both frame subtypes */
2019 if (len >= 24 + sizeof(mgmt->u.deauth))
2020 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
2021
2022 if (type == EVENT_UNPROT_DISASSOC) {
2023 event.unprot_disassoc.sa = mgmt->sa;
2024 event.unprot_disassoc.da = mgmt->da;
2025 event.unprot_disassoc.reason_code = reason_code;
2026 } else {
2027 event.unprot_deauth.sa = mgmt->sa;
2028 event.unprot_deauth.da = mgmt->da;
2029 event.unprot_deauth.reason_code = reason_code;
2030 }
2031
2032 wpa_supplicant_event(drv->ctx, type, &event);
2033}
2034
2035
97279d8d 2036static void mlme_event(struct i802_bss *bss,
da1fb17c 2037 enum nl80211_commands cmd, struct nlattr *frame,
58f6fbe0
JM
2038 struct nlattr *addr, struct nlattr *timed_out,
2039 struct nlattr *freq, struct nlattr *ack,
da873dbb 2040 struct nlattr *cookie, struct nlattr *sig)
c2a04078 2041{
97279d8d
JM
2042 struct wpa_driver_nl80211_data *drv = bss->drv;
2043 const u8 *data;
2044 size_t len;
2045
da1fb17c
JM
2046 if (timed_out && addr) {
2047 mlme_timeout_event(drv, cmd, addr);
2048 return;
2049 }
2050
c2a04078 2051 if (frame == NULL) {
2090a0b4
DS
2052 wpa_printf(MSG_DEBUG,
2053 "nl80211: MLME event %d (%s) without frame data",
2054 cmd, nl80211_command_to_string(cmd));
c2a04078
JM
2055 return;
2056 }
2057
97279d8d
JM
2058 data = nla_data(frame);
2059 len = nla_len(frame);
455299fb 2060 if (len < 4 + 2 * ETH_ALEN) {
2090a0b4
DS
2061 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
2062 MACSTR ") - too short",
2063 cmd, nl80211_command_to_string(cmd), bss->ifname,
2064 MAC2STR(bss->addr));
97279d8d
JM
2065 return;
2066 }
2090a0b4
DS
2067 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
2068 ") A1=" MACSTR " A2=" MACSTR, cmd,
2069 nl80211_command_to_string(cmd), bss->ifname,
2070 MAC2STR(bss->addr), MAC2STR(data + 4),
2071 MAC2STR(data + 4 + ETH_ALEN));
97279d8d 2072 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
455299fb
JM
2073 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
2074 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
97279d8d
JM
2075 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
2076 "for foreign address", bss->ifname);
2077 return;
2078 }
c2a04078
JM
2079 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
2080 nla_data(frame), nla_len(frame));
2081
2082 switch (cmd) {
2083 case NL80211_CMD_AUTHENTICATE:
2084 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
2085 break;
2086 case NL80211_CMD_ASSOCIATE:
2087 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
2088 break;
2089 case NL80211_CMD_DEAUTHENTICATE:
0544b242
JM
2090 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
2091 nla_data(frame), nla_len(frame));
c2a04078
JM
2092 break;
2093 case NL80211_CMD_DISASSOCIATE:
0544b242
JM
2094 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
2095 nla_data(frame), nla_len(frame));
c2a04078 2096 break;
bd94971e 2097 case NL80211_CMD_FRAME:
1d91f504 2098 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
da873dbb 2099 nla_len(frame));
58f6fbe0 2100 break;
bd94971e 2101 case NL80211_CMD_FRAME_TX_STATUS:
a11241fa
JB
2102 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
2103 nla_len(frame), ack);
58f6fbe0 2104 break;
7d878ca7
JM
2105 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2106 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
2107 nla_data(frame), nla_len(frame));
2108 break;
2109 case NL80211_CMD_UNPROT_DISASSOCIATE:
2110 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
2111 nla_data(frame), nla_len(frame));
2112 break;
c2a04078
JM
2113 default:
2114 break;
2115 }
2116}
2117
2118
a5e1eb20 2119static void mlme_event_michael_mic_failure(struct i802_bss *bss,
35583f3f
JM
2120 struct nlattr *tb[])
2121{
2122 union wpa_event_data data;
2123
2124 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2125 os_memset(&data, 0, sizeof(data));
2126 if (tb[NL80211_ATTR_MAC]) {
2127 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2128 nla_data(tb[NL80211_ATTR_MAC]),
2129 nla_len(tb[NL80211_ATTR_MAC]));
ad1e68e6 2130 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
35583f3f
JM
2131 }
2132 if (tb[NL80211_ATTR_KEY_SEQ]) {
2133 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2134 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2135 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2136 }
2137 if (tb[NL80211_ATTR_KEY_TYPE]) {
2138 enum nl80211_key_type key_type =
2139 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2140 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2141 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2142 data.michael_mic_failure.unicast = 1;
2143 } else
2144 data.michael_mic_failure.unicast = 1;
2145
2146 if (tb[NL80211_ATTR_KEY_IDX]) {
2147 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2148 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2149 }
2150
a5e1eb20 2151 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
35583f3f
JM
2152}
2153
2154
5cc4d64b
JM
2155static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2156 struct nlattr *tb[])
2157{
c7caac56
JM
2158 unsigned int freq;
2159
5cc4d64b
JM
2160 if (tb[NL80211_ATTR_MAC] == NULL) {
2161 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2162 "event");
2163 return;
2164 }
2165 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
b21990b4 2166
5cc4d64b
JM
2167 drv->associated = 1;
2168 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2169 MAC2STR(drv->bssid));
2170
c7caac56
JM
2171 freq = nl80211_get_assoc_freq(drv);
2172 if (freq) {
2173 wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
2174 freq);
2175 drv->first_bss->freq = freq;
2176 }
2177
5cc4d64b
JM
2178 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2179}
2180
2181
55777702
JM
2182static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2183 int cancel_event, struct nlattr *tb[])
2184{
2185 unsigned int freq, chan_type, duration;
2186 union wpa_event_data data;
2187 u64 cookie;
2188
2189 if (tb[NL80211_ATTR_WIPHY_FREQ])
2190 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2191 else
2192 freq = 0;
2193
2194 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2195 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2196 else
2197 chan_type = 0;
2198
2199 if (tb[NL80211_ATTR_DURATION])
2200 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2201 else
2202 duration = 0;
2203
2204 if (tb[NL80211_ATTR_COOKIE])
2205 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2206 else
2207 cookie = 0;
2208
2209 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2210 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2211 cancel_event, freq, chan_type, duration,
2212 (long long unsigned int) cookie,
2213 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2214
2215 if (cookie != drv->remain_on_chan_cookie)
2216 return; /* not for us */
2217
531f0331
JB
2218 if (cancel_event)
2219 drv->pending_remain_on_chan = 0;
55777702
JM
2220
2221 os_memset(&data, 0, sizeof(data));
2222 data.remain_on_channel.freq = freq;
2223 data.remain_on_channel.duration = duration;
2224 wpa_supplicant_event(drv->ctx, cancel_event ?
2225 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2226 EVENT_REMAIN_ON_CHANNEL, &data);
2227}
2228
2229
6a1ce395
DG
2230static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2231 struct nlattr *tb[])
2232{
2233 union wpa_event_data data;
2234
2235 os_memset(&data, 0, sizeof(data));
2236
2237 if (tb[NL80211_ATTR_IE]) {
2238 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2239 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2240 }
2241
2242 if (tb[NL80211_ATTR_IE_RIC]) {
2243 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2244 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2245 }
2246
2247 if (tb[NL80211_ATTR_MAC])
2248 os_memcpy(data.ft_ies.target_ap,
2249 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2250
2251 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2252 MAC2STR(data.ft_ies.target_ap));
2253
2254 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2255}
2256
2257
8d923a4a
JM
2258static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2259 struct nlattr *tb[])
2260{
2261 union wpa_event_data event;
2262 struct nlattr *nl;
2263 int rem;
2264 struct scan_info *info;
2265#define MAX_REPORT_FREQS 50
2266 int freqs[MAX_REPORT_FREQS];
2267 int num_freqs = 0;
2268
536fd62d
JM
2269 if (drv->scan_for_auth) {
2270 drv->scan_for_auth = 0;
2271 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2272 "cfg80211 BSS entry");
2273 wpa_driver_nl80211_authenticate_retry(drv);
2274 return;
2275 }
2276
8d923a4a
JM
2277 os_memset(&event, 0, sizeof(event));
2278 info = &event.scan_info;
2279 info->aborted = aborted;
2280
2281 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2282 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2283 struct wpa_driver_scan_ssid *s =
2284 &info->ssids[info->num_ssids];
2285 s->ssid = nla_data(nl);
2286 s->ssid_len = nla_len(nl);
3ae3ec27
JM
2287 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2288 wpa_ssid_txt(s->ssid, s->ssid_len));
8d923a4a
JM
2289 info->num_ssids++;
2290 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2291 break;
2292 }
2293 }
2294 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
3ae3ec27
JM
2295 char msg[200], *pos, *end;
2296 int res;
2297
2298 pos = msg;
2299 end = pos + sizeof(msg);
2300 *pos = '\0';
2301
8d923a4a
JM
2302 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2303 {
2304 freqs[num_freqs] = nla_get_u32(nl);
3ae3ec27
JM
2305 res = os_snprintf(pos, end - pos, " %d",
2306 freqs[num_freqs]);
2307 if (res > 0 && end - pos > res)
2308 pos += res;
8d923a4a
JM
2309 num_freqs++;
2310 if (num_freqs == MAX_REPORT_FREQS - 1)
2311 break;
2312 }
2313 info->freqs = freqs;
2314 info->num_freqs = num_freqs;
3ae3ec27
JM
2315 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2316 msg);
8d923a4a
JM
2317 }
2318 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2319}
2320
2321
60a972a6
JM
2322static int get_link_signal(struct nl_msg *msg, void *arg)
2323{
2324 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2325 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2326 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2327 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2328 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
95783298 2329 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
60a972a6 2330 };
7ee35bf3
PS
2331 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2332 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2333 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2334 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2335 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2336 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2337 };
1c5c7273 2338 struct wpa_signal_info *sig_change = arg;
60a972a6
JM
2339
2340 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2341 genlmsg_attrlen(gnlh, 0), NULL);
2342 if (!tb[NL80211_ATTR_STA_INFO] ||
2343 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2344 tb[NL80211_ATTR_STA_INFO], policy))
2345 return NL_SKIP;
2346 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2347 return NL_SKIP;
2348
7ee35bf3
PS
2349 sig_change->current_signal =
2350 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2351
95783298
AO
2352 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2353 sig_change->avg_signal =
2354 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2355 else
2356 sig_change->avg_signal = 0;
2357
7ee35bf3
PS
2358 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2359 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2360 sinfo[NL80211_STA_INFO_TX_BITRATE],
2361 rate_policy)) {
2362 sig_change->current_txrate = 0;
2363 } else {
2364 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2365 sig_change->current_txrate =
2366 nla_get_u16(rinfo[
2367 NL80211_RATE_INFO_BITRATE]) * 100;
2368 }
2369 }
2370 }
2371
60a972a6
JM
2372 return NL_SKIP;
2373}
2374
2375
2376static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1c5c7273 2377 struct wpa_signal_info *sig)
60a972a6
JM
2378{
2379 struct nl_msg *msg;
2380
7ee35bf3
PS
2381 sig->current_signal = -9999;
2382 sig->current_txrate = 0;
60a972a6
JM
2383
2384 msg = nlmsg_alloc();
2385 if (!msg)
2386 return -ENOMEM;
2387
9fb04070 2388 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
60a972a6
JM
2389
2390 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2391 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2392
2393 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2394 nla_put_failure:
5883168a 2395 nlmsg_free(msg);
60a972a6
JM
2396 return -ENOBUFS;
2397}
2398
2399
7ee35bf3
PS
2400static int get_link_noise(struct nl_msg *msg, void *arg)
2401{
2402 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2403 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2404 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2405 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2406 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2407 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2408 };
1c5c7273 2409 struct wpa_signal_info *sig_change = arg;
7ee35bf3
PS
2410
2411 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2412 genlmsg_attrlen(gnlh, 0), NULL);
2413
2414 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2415 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2416 return NL_SKIP;
2417 }
2418
2419 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2420 tb[NL80211_ATTR_SURVEY_INFO],
2421 survey_policy)) {
2422 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2423 "attributes!");
2424 return NL_SKIP;
2425 }
2426
2427 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2428 return NL_SKIP;
2429
2430 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2431 sig_change->frequency)
2432 return NL_SKIP;
2433
2434 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2435 return NL_SKIP;
2436
2437 sig_change->current_noise =
2438 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2439
2440 return NL_SKIP;
2441}
2442
2443
2444static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1c5c7273 2445 struct wpa_signal_info *sig_change)
7ee35bf3
PS
2446{
2447 struct nl_msg *msg;
2448
2449 sig_change->current_noise = 9999;
2450 sig_change->frequency = drv->assoc_freq;
2451
2452 msg = nlmsg_alloc();
2453 if (!msg)
2454 return -ENOMEM;
2455
9fb04070 2456 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
7ee35bf3
PS
2457
2458 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2459
2460 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2461 nla_put_failure:
5883168a 2462 nlmsg_free(msg);
7ee35bf3
PS
2463 return -ENOBUFS;
2464}
2465
2466
577db0ae
GM
2467static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2468{
2469 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2470 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2471 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2472 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2473 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2474 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2475 };
2476 struct wpa_scan_results *scan_results = arg;
2477 struct wpa_scan_res *scan_res;
2478 size_t i;
2479
2480 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2481 genlmsg_attrlen(gnlh, 0), NULL);
2482
2483 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2484 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2485 return NL_SKIP;
2486 }
2487
2488 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2489 tb[NL80211_ATTR_SURVEY_INFO],
2490 survey_policy)) {
2491 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2492 "attributes");
2493 return NL_SKIP;
2494 }
2495
2496 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2497 return NL_SKIP;
2498
2499 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2500 return NL_SKIP;
2501
2502 for (i = 0; i < scan_results->num; ++i) {
2503 scan_res = scan_results->res[i];
2504 if (!scan_res)
2505 continue;
2506 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2507 scan_res->freq)
2508 continue;
2509 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2510 continue;
2511 scan_res->noise = (s8)
2512 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2513 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2514 }
2515
2516 return NL_SKIP;
2517}
2518
2519
2520static int nl80211_get_noise_for_scan_results(
2521 struct wpa_driver_nl80211_data *drv,
2522 struct wpa_scan_results *scan_res)
2523{
2524 struct nl_msg *msg;
2525
2526 msg = nlmsg_alloc();
2527 if (!msg)
2528 return -ENOMEM;
2529
2530 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2531
2532 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2533
2534 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2535 scan_res);
2536 nla_put_failure:
9e088e74 2537 nlmsg_free(msg);
577db0ae
GM
2538 return -ENOBUFS;
2539}
2540
2541
93910401
JM
2542static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2543 struct nlattr *tb[])
2544{
2545 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2546 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2547 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2548 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
0d7e5a3a 2549 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
93910401
JM
2550 };
2551 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2552 enum nl80211_cqm_rssi_threshold_event event;
b625473c 2553 union wpa_event_data ed;
1c5c7273 2554 struct wpa_signal_info sig;
7ee35bf3 2555 int res;
93910401
JM
2556
2557 if (tb[NL80211_ATTR_CQM] == NULL ||
2558 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2559 cqm_policy)) {
2560 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2561 return;
2562 }
2563
0d7e5a3a
JB
2564 os_memset(&ed, 0, sizeof(ed));
2565
2566 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2567 if (!tb[NL80211_ATTR_MAC])
2568 return;
2569 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2570 ETH_ALEN);
2571 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2572 return;
2573 }
2574
93910401
JM
2575 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2576 return;
2577 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
b625473c 2578
93910401
JM
2579 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2580 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2581 "event: RSSI high");
b625473c 2582 ed.signal_change.above_threshold = 1;
93910401
JM
2583 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2584 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2585 "event: RSSI low");
b625473c
JM
2586 ed.signal_change.above_threshold = 0;
2587 } else
2588 return;
2589
60a972a6
JM
2590 res = nl80211_get_link_signal(drv, &sig);
2591 if (res == 0) {
7ee35bf3
PS
2592 ed.signal_change.current_signal = sig.current_signal;
2593 ed.signal_change.current_txrate = sig.current_txrate;
2594 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2595 sig.current_signal, sig.current_txrate);
2596 }
2597
2598 res = nl80211_get_link_noise(drv, &sig);
2599 if (res == 0) {
2600 ed.signal_change.current_noise = sig.current_noise;
2601 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2602 sig.current_noise);
60a972a6
JM
2603 }
2604
b625473c 2605 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
93910401
JM
2606}
2607
2608
a52024c9
BC
2609static void nl80211_new_peer_candidate(struct wpa_driver_nl80211_data *drv,
2610 struct nlattr **tb)
2611{
2612 const u8 *addr;
2613 union wpa_event_data data;
2614
2615 if (drv->nlmode != NL80211_IFTYPE_MESH_POINT)
2616 return;
2617
2618 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_IE])
2619 return;
2620
2621 addr = nla_data(tb[NL80211_ATTR_MAC]);
2622 wpa_printf(MSG_DEBUG, "nl80211: New peer candidate" MACSTR,
2623 MAC2STR(addr));
2624
2625 os_memset(&data, 0, sizeof(data));
2626 data.mesh_peer.peer = addr;
2627 data.mesh_peer.ies = nla_data(tb[NL80211_ATTR_IE]);
2628 data.mesh_peer.ie_len = nla_len(tb[NL80211_ATTR_IE]);
2629 wpa_supplicant_event(drv->ctx, EVENT_NEW_PEER_CANDIDATE, &data);
2630}
2631
2632
18d2ba08
JM
2633static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2634 struct nlattr **tb)
2635{
2636 u8 *addr;
2637 union wpa_event_data data;
2638
2639 if (tb[NL80211_ATTR_MAC] == NULL)
2640 return;
2641 addr = nla_data(tb[NL80211_ATTR_MAC]);
2642 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
5f310a9e 2643
61cbe2ff 2644 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
2645 u8 *ies = NULL;
2646 size_t ies_len = 0;
2647 if (tb[NL80211_ATTR_IE]) {
2648 ies = nla_data(tb[NL80211_ATTR_IE]);
2649 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2650 }
2651 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2652 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2653 return;
2654 }
2655
18d2ba08
JM
2656 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2657 return;
2658
2659 os_memset(&data, 0, sizeof(data));
2660 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2661 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2662}
2663
2664
ef985058
JM
2665static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2666 struct nlattr **tb)
2667{
2668 u8 *addr;
2669 union wpa_event_data data;
2670
2671 if (tb[NL80211_ATTR_MAC] == NULL)
2672 return;
2673 addr = nla_data(tb[NL80211_ATTR_MAC]);
2674 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2675 MAC2STR(addr));
5f310a9e 2676
61cbe2ff 2677 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
2678 drv_event_disassoc(drv->ctx, addr);
2679 return;
2680 }
2681
ef985058
JM
2682 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2683 return;
2684
2685 os_memset(&data, 0, sizeof(data));
2686 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2687 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2688}
2689
2690
b14a210c
JB
2691static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2692 struct nlattr **tb)
2693{
2694 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2695 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2696 [NL80211_REKEY_DATA_KEK] = {
2697 .minlen = NL80211_KEK_LEN,
2698 .maxlen = NL80211_KEK_LEN,
2699 },
2700 [NL80211_REKEY_DATA_KCK] = {
2701 .minlen = NL80211_KCK_LEN,
2702 .maxlen = NL80211_KCK_LEN,
2703 },
2704 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2705 .minlen = NL80211_REPLAY_CTR_LEN,
2706 .maxlen = NL80211_REPLAY_CTR_LEN,
2707 },
2708 };
2709 union wpa_event_data data;
2710
2711 if (!tb[NL80211_ATTR_MAC])
2712 return;
2713 if (!tb[NL80211_ATTR_REKEY_DATA])
2714 return;
2715 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2716 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2717 return;
2718 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2719 return;
2720
2721 os_memset(&data, 0, sizeof(data));
2722 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2723 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2724 MAC2STR(data.driver_gtk_rekey.bssid));
2725 data.driver_gtk_rekey.replay_ctr =
2726 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2727 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2728 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2729 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2730}
2731
2732
c36d5242
JM
2733static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2734 struct nlattr **tb)
2735{
2736 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2737 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2738 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2739 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2740 .minlen = ETH_ALEN,
2741 .maxlen = ETH_ALEN,
2742 },
2743 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2744 };
2745 union wpa_event_data data;
2746
7d81932d
JM
2747 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2748
c36d5242
JM
2749 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2750 return;
2751 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2752 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2753 return;
2754 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2755 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2756 return;
2757
2758 os_memset(&data, 0, sizeof(data));
2759 os_memcpy(data.pmkid_candidate.bssid,
2760 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2761 data.pmkid_candidate.index =
2762 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2763 data.pmkid_candidate.preauth =
2764 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2765 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2766}
2767
2768
39718852
JB
2769static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2770 struct nlattr **tb)
2771{
2772 union wpa_event_data data;
2773
7d81932d
JM
2774 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2775
39718852
JB
2776 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2777 return;
2778
2779 os_memset(&data, 0, sizeof(data));
2780 os_memcpy(data.client_poll.addr,
2781 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2782
2783 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2784}
2785
2786
6201b052
JM
2787static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2788 struct nlattr **tb)
2789{
2790 union wpa_event_data data;
2791
2792 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2793
2794 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2795 return;
2796
2797 os_memset(&data, 0, sizeof(data));
2798 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2799 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2800 case NL80211_TDLS_SETUP:
2801 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2802 MACSTR, MAC2STR(data.tdls.peer));
2803 data.tdls.oper = TDLS_REQUEST_SETUP;
2804 break;
2805 case NL80211_TDLS_TEARDOWN:
2806 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2807 MACSTR, MAC2STR(data.tdls.peer));
2808 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2809 break;
2810 default:
2811 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2812 "event");
2813 return;
2814 }
2815 if (tb[NL80211_ATTR_REASON_CODE]) {
2816 data.tdls.reason_code =
2817 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2818 }
2819
2820 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2821}
2822
2823
7239ea7f
ST
2824static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2825 struct nlattr **tb)
2826{
2827 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2828}
2829
2830
3140803b
RM
2831static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2832 struct nlattr **tb)
2833{
2834 union wpa_event_data data;
2835 u32 reason;
2836
2837 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2838
2839 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2840 return;
2841
2842 os_memset(&data, 0, sizeof(data));
2843 os_memcpy(data.connect_failed_reason.addr,
2844 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2845
2846 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2847 switch (reason) {
2848 case NL80211_CONN_FAIL_MAX_CLIENTS:
2849 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2850 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2851 break;
2852 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2853 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2854 " tried to connect",
2855 MAC2STR(data.connect_failed_reason.addr));
2856 data.connect_failed_reason.code = BLOCKED_CLIENT;
2857 break;
2858 default:
2859 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2860 "%u", reason);
2861 return;
2862 }
2863
2864 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2865}
2866
2867
04be54fa
SW
2868static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2869 struct nlattr **tb)
2870{
2871 union wpa_event_data data;
2872 enum nl80211_radar_event event_type;
2873
2874 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2875 return;
2876
2877 os_memset(&data, 0, sizeof(data));
cd3b0700
MK
2878 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2879 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
04be54fa 2880
846de15d
JD
2881 /* Check HT params */
2882 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2883 data.dfs_event.ht_enabled = 1;
2884 data.dfs_event.chan_offset = 0;
2885
2886 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2887 case NL80211_CHAN_NO_HT:
2888 data.dfs_event.ht_enabled = 0;
2889 break;
2890 case NL80211_CHAN_HT20:
2891 break;
2892 case NL80211_CHAN_HT40PLUS:
2893 data.dfs_event.chan_offset = 1;
2894 break;
2895 case NL80211_CHAN_HT40MINUS:
2896 data.dfs_event.chan_offset = -1;
2897 break;
2898 }
2899 }
2900
2901 /* Get VHT params */
884f1a3c
JM
2902 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2903 data.dfs_event.chan_width =
2904 convert2width(nla_get_u32(
2905 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2906 if (tb[NL80211_ATTR_CENTER_FREQ1])
2907 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
846de15d
JD
2908 if (tb[NL80211_ATTR_CENTER_FREQ2])
2909 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2910
2911 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2912 data.dfs_event.freq, data.dfs_event.ht_enabled,
2913 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2914 data.dfs_event.cf1, data.dfs_event.cf2);
04be54fa
SW
2915
2916 switch (event_type) {
2917 case NL80211_RADAR_DETECTED:
2918 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2919 break;
2920 case NL80211_RADAR_CAC_FINISHED:
2921 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2922 break;
2923 case NL80211_RADAR_CAC_ABORTED:
2924 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2925 break;
2926 case NL80211_RADAR_NOP_FINISHED:
2927 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2928 break;
2929 default:
2930 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2931 "received", event_type);
2932 break;
2933 }
2934}
2935
2936
3088e4e5
JB
2937static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2938 int wds)
02bb32c3
JB
2939{
2940 struct wpa_driver_nl80211_data *drv = bss->drv;
2941 union wpa_event_data event;
02bb32c3
JB
2942
2943 if (!tb[NL80211_ATTR_MAC])
2944 return;
2945
02bb32c3 2946 os_memset(&event, 0, sizeof(event));
341eebee 2947 event.rx_from_unknown.bssid = bss->addr;
02bb32c3 2948 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
3088e4e5 2949 event.rx_from_unknown.wds = wds;
02bb32c3
JB
2950
2951 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2952}
2953
2954
253f2e37
AH
2955static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2956 const u8 *data, size_t len)
2957{
2958 u32 i, count;
2959 union wpa_event_data event;
2960 struct wpa_freq_range *range = NULL;
2961 const struct qca_avoid_freq_list *freq_range;
2962
2963 freq_range = (const struct qca_avoid_freq_list *) data;
2964 if (len < sizeof(freq_range->count))
2965 return;
2966
2967 count = freq_range->count;
2968 if (len < sizeof(freq_range->count) +
2969 count * sizeof(struct qca_avoid_freq_range)) {
2970 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2971 (unsigned int) len);
2972 return;
2973 }
2974
2975 if (count > 0) {
2976 range = os_calloc(count, sizeof(struct wpa_freq_range));
2977 if (range == NULL)
2978 return;
2979 }
2980
2981 os_memset(&event, 0, sizeof(event));
2982 for (i = 0; i < count; i++) {
2983 unsigned int idx = event.freq_range.num;
2984 range[idx].min = freq_range->range[i].start_freq;
2985 range[idx].max = freq_range->range[i].end_freq;
2986 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2987 range[idx].min, range[idx].max);
2988 if (range[idx].min > range[idx].max) {
2989 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2990 continue;
2991 }
2992 event.freq_range.num++;
2993 }
2994 event.freq_range.range = range;
2995
2996 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2997
2998 os_free(range);
2999}
3000
3001
b41f2684
CL
3002static void qca_nl80211_key_mgmt_auth(struct wpa_driver_nl80211_data *drv,
3003 const u8 *data, size_t len)
3004{
3005 struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_MAX + 1];
3006 u8 *bssid;
3007
3008 wpa_printf(MSG_DEBUG,
3009 "nl80211: Key management roam+auth vendor event received");
3010
3011 if (nla_parse(tb, QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_MAX,
3012 (struct nlattr *) data, len, NULL))
3013 return;
3014 if (!tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID] ||
3015 nla_len(tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID]) != ETH_ALEN ||
3016 !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_REQ_IE] ||
3017 !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_RESP_IE] ||
3018 !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_AUTHORIZED])
3019 return;
3020
3021 bssid = nla_data(tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID]);
3022 wpa_printf(MSG_DEBUG, " * roam BSSID " MACSTR, MAC2STR(bssid));
3023
3024 mlme_event_connect(drv, NL80211_CMD_ROAM, NULL,
3025 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID],
3026 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_REQ_IE],
3027 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_RESP_IE],
3028 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_AUTHORIZED],
3029 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_KEY_REPLAY_CTR],
3030 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_PTK_KCK],
3031 tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_PTK_KEK]);
3032}
3033
3034
1682c623
JM
3035static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
3036 u32 subcmd, u8 *data, size_t len)
3037{
3038 switch (subcmd) {
253f2e37
AH
3039 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
3040 qca_nl80211_avoid_freq(drv, data, len);
3041 break;
b41f2684
CL
3042 case QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_ROAM_AUTH:
3043 qca_nl80211_key_mgmt_auth(drv, data, len);
3044 break;
1682c623
JM
3045 default:
3046 wpa_printf(MSG_DEBUG,
3047 "nl80211: Ignore unsupported QCA vendor event %u",
3048 subcmd);
3049 break;
3050 }
3051}
3052
3053
17b79e65
JM
3054static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
3055 struct nlattr **tb)
3056{
3057 u32 vendor_id, subcmd, wiphy = 0;
3058 int wiphy_idx;
3059 u8 *data = NULL;
3060 size_t len = 0;
3061
3062 if (!tb[NL80211_ATTR_VENDOR_ID] ||
3063 !tb[NL80211_ATTR_VENDOR_SUBCMD])
3064 return;
3065
3066 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
3067 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
3068
3069 if (tb[NL80211_ATTR_WIPHY])
3070 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3071
3072 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
3073 wiphy, vendor_id, subcmd);
3074
3075 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3076 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
3077 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
3078 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
3079 }
3080
3081 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
3082 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
3083 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
3084 wiphy, wiphy_idx);
3085 return;
3086 }
3087
3088 switch (vendor_id) {
1682c623
JM
3089 case OUI_QCA:
3090 nl80211_vendor_event_qca(drv, subcmd, data, len);
3091 break;
17b79e65
JM
3092 default:
3093 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
3094 break;
3095 }
3096}
3097
3098
142817b2
JM
3099static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
3100 struct nlattr *tb[])
3101{
3102 union wpa_event_data data;
3103 enum nl80211_reg_initiator init;
3104
3105 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
3106
3107 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
3108 return;
3109
3110 os_memset(&data, 0, sizeof(data));
3111 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
3112 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
3113 switch (init) {
3114 case NL80211_REGDOM_SET_BY_CORE:
3115 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
3116 break;
3117 case NL80211_REGDOM_SET_BY_USER:
3118 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
3119 break;
3120 case NL80211_REGDOM_SET_BY_DRIVER:
3121 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
3122 break;
3123 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3124 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
3125 break;
3126 }
3127
3128 if (tb[NL80211_ATTR_REG_TYPE]) {
3129 enum nl80211_reg_type type;
3130 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
3131 wpa_printf(MSG_DEBUG, " * type=%d", type);
3132 switch (type) {
3133 case NL80211_REGDOM_TYPE_COUNTRY:
3134 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
3135 break;
3136 case NL80211_REGDOM_TYPE_WORLD:
3137 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
3138 break;
3139 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
3140 data.channel_list_changed.type =
3141 REGDOM_TYPE_CUSTOM_WORLD;
3142 break;
3143 case NL80211_REGDOM_TYPE_INTERSECTION:
3144 data.channel_list_changed.type =
3145 REGDOM_TYPE_INTERSECTION;
3146 break;
3147 }
3148 }
3149
3150 if (tb[NL80211_ATTR_REG_ALPHA2]) {
3151 os_strlcpy(data.channel_list_changed.alpha2,
3152 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
3153 sizeof(data.channel_list_changed.alpha2));
3154 wpa_printf(MSG_DEBUG, " * alpha2=%s",
3155 data.channel_list_changed.alpha2);
3156 }
3157
3158 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
3159}
3160
3161
a5e1eb20
SE
3162static void do_process_drv_event(struct i802_bss *bss, int cmd,
3163 struct nlattr **tb)
97865538 3164{
a5e1eb20 3165 struct wpa_driver_nl80211_data *drv = bss->drv;
795baf77 3166 union wpa_event_data data;
a5e1eb20 3167
2090a0b4
DS
3168 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
3169 cmd, nl80211_command_to_string(cmd), bss->ifname);
3170
b41f2684
CL
3171 if (cmd == NL80211_CMD_ROAM && drv->roam_auth_vendor_event_avail) {
3172 /*
3173 * Device will use roam+auth vendor event to indicate
3174 * roaming, so ignore the regular roam event.
3175 */
3176 wpa_printf(MSG_DEBUG,
3177 "nl80211: Ignore roam event (cmd=%d), device will use vendor event roam+auth",
3178 cmd);
3179 return;
3180 }
3181
b1f625e0 3182 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
d6c9aab8
JB
3183 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
3184 cmd == NL80211_CMD_SCAN_ABORTED)) {
834ee56f 3185 wpa_driver_nl80211_set_mode(drv->first_bss,
b1f625e0
EP
3186 drv->ap_scan_as_station);
3187 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6
JM
3188 }
3189
d6c9aab8 3190 switch (cmd) {
d942a79e 3191 case NL80211_CMD_TRIGGER_SCAN:
565110cd 3192 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
a771c07d 3193 drv->scan_state = SCAN_STARTED;
1b5df9e5
JM
3194 if (drv->scan_for_auth) {
3195 /*
3196 * Cannot indicate EVENT_SCAN_STARTED here since we skip
3197 * EVENT_SCAN_RESULTS in scan_for_auth case and the
3198 * upper layer implementation could get confused about
3199 * scanning state.
3200 */
3201 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3202 break;
3203 }
a5f40eff 3204 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
d942a79e 3205 break;
d21c63b9 3206 case NL80211_CMD_START_SCHED_SCAN:
565110cd 3207 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
a771c07d 3208 drv->scan_state = SCHED_SCAN_STARTED;
d21c63b9
LC
3209 break;
3210 case NL80211_CMD_SCHED_SCAN_STOPPED:
565110cd 3211 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
a771c07d 3212 drv->scan_state = SCHED_SCAN_STOPPED;
d21c63b9
LC
3213 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3214 break;
97865538 3215 case NL80211_CMD_NEW_SCAN_RESULTS:
565110cd
JM
3216 wpa_dbg(drv->ctx, MSG_DEBUG,
3217 "nl80211: New scan results available");
a771c07d 3218 drv->scan_state = SCAN_COMPLETED;
97865538
JM
3219 drv->scan_complete_events = 1;
3220 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3221 drv->ctx);
8d923a4a 3222 send_scan_event(drv, 0, tb);
97865538 3223 break;
d21c63b9 3224 case NL80211_CMD_SCHED_SCAN_RESULTS:
565110cd
JM
3225 wpa_dbg(drv->ctx, MSG_DEBUG,
3226 "nl80211: New sched scan results available");
a771c07d 3227 drv->scan_state = SCHED_SCAN_RESULTS;
d21c63b9
LC
3228 send_scan_event(drv, 0, tb);
3229 break;
97865538 3230 case NL80211_CMD_SCAN_ABORTED:
565110cd 3231 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
a771c07d 3232 drv->scan_state = SCAN_ABORTED;
97865538
JM
3233 /*
3234 * Need to indicate that scan results are available in order
3235 * not to make wpa_supplicant stop its scanning.
3236 */
3237 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3238 drv->ctx);
8d923a4a 3239 send_scan_event(drv, 1, tb);
97865538 3240 break;
c2a04078
JM
3241 case NL80211_CMD_AUTHENTICATE:
3242 case NL80211_CMD_ASSOCIATE:
3243 case NL80211_CMD_DEAUTHENTICATE:
3244 case NL80211_CMD_DISASSOCIATE:
bd94971e 3245 case NL80211_CMD_FRAME_TX_STATUS:
7d878ca7
JM
3246 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3247 case NL80211_CMD_UNPROT_DISASSOCIATE:
97279d8d 3248 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
58f6fbe0
JM
3249 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3250 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
da873dbb
JB
3251 tb[NL80211_ATTR_COOKIE],
3252 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
c2a04078 3253 break;
da72a1c1
ZY
3254 case NL80211_CMD_CONNECT:
3255 case NL80211_CMD_ROAM:
d6c9aab8 3256 mlme_event_connect(drv, cmd,
da72a1c1
ZY
3257 tb[NL80211_ATTR_STATUS_CODE],
3258 tb[NL80211_ATTR_MAC],
3259 tb[NL80211_ATTR_REQ_IE],
b41f2684
CL
3260 tb[NL80211_ATTR_RESP_IE],
3261 NULL, NULL, NULL, NULL);
da72a1c1 3262 break;
1b487b8b 3263 case NL80211_CMD_CH_SWITCH_NOTIFY:
8d1fdde7
JD
3264 mlme_event_ch_switch(drv,
3265 tb[NL80211_ATTR_IFINDEX],
3266 tb[NL80211_ATTR_WIPHY_FREQ],
3267 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3268 tb[NL80211_ATTR_CHANNEL_WIDTH],
3269 tb[NL80211_ATTR_CENTER_FREQ1],
3270 tb[NL80211_ATTR_CENTER_FREQ2]);
1b487b8b 3271 break;
da72a1c1 3272 case NL80211_CMD_DISCONNECT:
20f5a4c2 3273 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
3d9975d5
JM
3274 tb[NL80211_ATTR_MAC],
3275 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
da72a1c1 3276 break;
35583f3f 3277 case NL80211_CMD_MICHAEL_MIC_FAILURE:
a5e1eb20 3278 mlme_event_michael_mic_failure(bss, tb);
35583f3f 3279 break;
5cc4d64b
JM
3280 case NL80211_CMD_JOIN_IBSS:
3281 mlme_event_join_ibss(drv, tb);
3282 break;
55777702
JM
3283 case NL80211_CMD_REMAIN_ON_CHANNEL:
3284 mlme_event_remain_on_channel(drv, 0, tb);
3285 break;
3286 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3287 mlme_event_remain_on_channel(drv, 1, tb);
3288 break;
93910401
JM
3289 case NL80211_CMD_NOTIFY_CQM:
3290 nl80211_cqm_event(drv, tb);
3291 break;
33c5deb8 3292 case NL80211_CMD_REG_CHANGE:
142817b2 3293 nl80211_reg_change_event(drv, tb);
33c5deb8
JM
3294 break;
3295 case NL80211_CMD_REG_BEACON_HINT:
3296 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
8597ebdb
JM
3297 os_memset(&data, 0, sizeof(data));
3298 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
33c5deb8 3299 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
8597ebdb 3300 &data);
33c5deb8 3301 break;
18d2ba08
JM
3302 case NL80211_CMD_NEW_STATION:
3303 nl80211_new_station_event(drv, tb);
3304 break;
ef985058
JM
3305 case NL80211_CMD_DEL_STATION:
3306 nl80211_del_station_event(drv, tb);
3307 break;
b14a210c
JB
3308 case NL80211_CMD_SET_REKEY_OFFLOAD:
3309 nl80211_rekey_offload_event(drv, tb);
3310 break;
c36d5242
JM
3311 case NL80211_CMD_PMKSA_CANDIDATE:
3312 nl80211_pmksa_candidate_event(drv, tb);
3313 break;
39718852
JB
3314 case NL80211_CMD_PROBE_CLIENT:
3315 nl80211_client_probe_event(drv, tb);
3316 break;
6201b052
JM
3317 case NL80211_CMD_TDLS_OPER:
3318 nl80211_tdls_oper_event(drv, tb);
3319 break;
3140803b
RM
3320 case NL80211_CMD_CONN_FAILED:
3321 nl80211_connect_failed_event(drv, tb);
3322 break;
6a1ce395
DG
3323 case NL80211_CMD_FT_EVENT:
3324 mlme_event_ft_event(drv, tb);
3325 break;
04be54fa
SW
3326 case NL80211_CMD_RADAR_DETECT:
3327 nl80211_radar_event(drv, tb);
3328 break;
7239ea7f
ST
3329 case NL80211_CMD_STOP_AP:
3330 nl80211_stop_ap(drv, tb);
3331 break;
17b79e65
JM
3332 case NL80211_CMD_VENDOR:
3333 nl80211_vendor_event(drv, tb);
3334 break;
a52024c9
BC
3335 case NL80211_CMD_NEW_PEER_CANDIDATE:
3336 nl80211_new_peer_candidate(drv, tb);
3337 break;
97865538 3338 default:
565110cd
JM
3339 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3340 "(cmd=%d)", cmd);
97865538
JM
3341 break;
3342 }
d6c9aab8
JB
3343}
3344
3345
3346static int process_drv_event(struct nl_msg *msg, void *arg)
3347{
3348 struct wpa_driver_nl80211_data *drv = arg;
3349 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3350 struct nlattr *tb[NL80211_ATTR_MAX + 1];
a5e1eb20
SE
3351 struct i802_bss *bss;
3352 int ifidx = -1;
d6c9aab8
JB
3353
3354 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3355 genlmsg_attrlen(gnlh, 0), NULL);
3356
d3aaef80 3357 if (tb[NL80211_ATTR_IFINDEX]) {
a5e1eb20
SE
3358 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3359
834ee56f 3360 for (bss = drv->first_bss; bss; bss = bss->next)
d3aaef80
DS
3361 if (ifidx == -1 || ifidx == bss->ifindex) {
3362 do_process_drv_event(bss, gnlh->cmd, tb);
3363 return NL_SKIP;
3364 }
3365 wpa_printf(MSG_DEBUG,
3366 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3367 gnlh->cmd, ifidx);
3368 } else if (tb[NL80211_ATTR_WDEV]) {
3369 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3370 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
834ee56f 3371 for (bss = drv->first_bss; bss; bss = bss->next) {
d3aaef80
DS
3372 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3373 do_process_drv_event(bss, gnlh->cmd, tb);
3374 return NL_SKIP;
3375 }
d6c9aab8 3376 }
d3aaef80
DS
3377 wpa_printf(MSG_DEBUG,
3378 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3379 gnlh->cmd, (long long unsigned int) wdev_id);
d6c9aab8
JB
3380 }
3381
d6c9aab8
JB
3382 return NL_SKIP;
3383}
3384
3385
3386static int process_global_event(struct nl_msg *msg, void *arg)
3387{
3388 struct nl80211_global *global = arg;
3389 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3390 struct nlattr *tb[NL80211_ATTR_MAX + 1];
24b5bd8b 3391 struct wpa_driver_nl80211_data *drv, *tmp;
d6c9aab8 3392 int ifidx = -1;
a5e1eb20 3393 struct i802_bss *bss;
54d4ba42 3394 u64 wdev_id = 0;
d3aaef80 3395 int wdev_id_set = 0;
d6c9aab8
JB
3396
3397 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3398 genlmsg_attrlen(gnlh, 0), NULL);
3399
3400 if (tb[NL80211_ATTR_IFINDEX])
3401 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
d3aaef80
DS
3402 else if (tb[NL80211_ATTR_WDEV]) {
3403 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3404 wdev_id_set = 1;
3405 }
d6c9aab8 3406
24b5bd8b
JB
3407 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3408 struct wpa_driver_nl80211_data, list) {
834ee56f 3409 for (bss = drv->first_bss; bss; bss = bss->next) {
d3aaef80
DS
3410 if ((ifidx == -1 && !wdev_id_set) ||
3411 ifidx == bss->ifindex ||
3412 (wdev_id_set && bss->wdev_id_set &&
3413 wdev_id == bss->wdev_id)) {
a5e1eb20
SE
3414 do_process_drv_event(bss, gnlh->cmd, tb);
3415 return NL_SKIP;
3416 }
3417 }
d6c9aab8 3418 }
97865538
JM
3419
3420 return NL_SKIP;
3421}
3422
3423
cc7a48d1
JB
3424static int process_bss_event(struct nl_msg *msg, void *arg)
3425{
a11241fa 3426 struct i802_bss *bss = arg;
cc7a48d1
JB
3427 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3428 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3429
3430 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3431 genlmsg_attrlen(gnlh, 0), NULL);
3432
2090a0b4
DS
3433 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3434 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3435 bss->ifname);
3436
cc7a48d1 3437 switch (gnlh->cmd) {
a11241fa
JB
3438 case NL80211_CMD_FRAME:
3439 case NL80211_CMD_FRAME_TX_STATUS:
97279d8d 3440 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
a11241fa
JB
3441 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3442 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
da873dbb
JB
3443 tb[NL80211_ATTR_COOKIE],
3444 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
a11241fa 3445 break;
02bb32c3 3446 case NL80211_CMD_UNEXPECTED_FRAME:
3088e4e5
JB
3447 nl80211_spurious_frame(bss, tb, 0);
3448 break;
3449 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3450 nl80211_spurious_frame(bss, tb, 1);
02bb32c3 3451 break;
cc7a48d1
JB
3452 default:
3453 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3454 "(cmd=%d)", gnlh->cmd);
3455 break;
3456 }
3457
3458 return NL_SKIP;
3459}
3460
3461
97865538 3462static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
5582a5d1 3463 void *handle)
97865538 3464{
a4ae123c 3465 struct nl_cb *cb = eloop_ctx;
34068ac3 3466 int res;
97865538 3467
cc2ada86 3468 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
97865538 3469
34068ac3 3470 res = nl_recvmsgs(handle, cb);
d3d04831 3471 if (res < 0) {
34068ac3
JM
3472 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3473 __func__, res);
3474 }
97865538
JM
3475}
3476
3477
6d158490
LR
3478/**
3479 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3480 * @priv: driver_nl80211 private data
3481 * @alpha2_arg: country to which to switch to
3482 * Returns: 0 on success, -1 on failure
3483 *
3484 * This asks nl80211 to set the regulatory domain for given
3485 * country ISO / IEC alpha2.
3486 */
3487static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3488{
a2e40bb6
FF
3489 struct i802_bss *bss = priv;
3490 struct wpa_driver_nl80211_data *drv = bss->drv;
6d158490
LR
3491 char alpha2[3];
3492 struct nl_msg *msg;
3493
3494 msg = nlmsg_alloc();
3495 if (!msg)
e785c2ba 3496 return -ENOMEM;
6d158490
LR
3497
3498 alpha2[0] = alpha2_arg[0];
3499 alpha2[1] = alpha2_arg[1];
3500 alpha2[2] = '\0';
3501
9fb04070 3502 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
6d158490
LR
3503
3504 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3505 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3506 return -EINVAL;
3507 return 0;
3508nla_put_failure:
5883168a 3509 nlmsg_free(msg);
6d158490
LR
3510 return -EINVAL;
3511}
3512
3513
f0793bf1
JM
3514static int nl80211_get_country(struct nl_msg *msg, void *arg)
3515{
3516 char *alpha2 = arg;
3517 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3518 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3519
3520 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3521 genlmsg_attrlen(gnlh, 0), NULL);
3522 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3523 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3524 return NL_SKIP;
3525 }
3526 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3527 return NL_SKIP;
3528}
3529
3530
3531static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3532{
3533 struct i802_bss *bss = priv;
3534 struct wpa_driver_nl80211_data *drv = bss->drv;
3535 struct nl_msg *msg;
3536 int ret;
3537
3538 msg = nlmsg_alloc();
3539 if (!msg)
3540 return -ENOMEM;
3541
3542 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3543 alpha2[0] = '\0';
3544 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3545 if (!alpha2[0])
3546 ret = -1;
3547
3548 return ret;
3549}
3550
3551
43245552
DJ
3552static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3553{
3554 u32 *feat = arg;
3555 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3556 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3557
3558 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3559 genlmsg_attrlen(gnlh, 0), NULL);
3560
3561 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3562 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3563
3564 return NL_SKIP;
3565}
3566
3567
3568static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3569{
3570 u32 feat = 0;
3571 struct nl_msg *msg;
3572
3573 msg = nlmsg_alloc();
3574 if (!msg)
3575 goto nla_put_failure;
3576
3577 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3578 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3579 return feat;
3580
3581 msg = NULL;
3582nla_put_failure:
3583 nlmsg_free(msg);
3584 return 0;
3585}
3586
3587
80bc75f1 3588struct wiphy_info_data {
8cd6b7bc 3589 struct wpa_driver_nl80211_data *drv;
e8b5e24e
JB
3590 struct wpa_driver_capa *capa;
3591
4752147d
IP
3592 unsigned int num_multichan_concurrent;
3593
e8b5e24e 3594 unsigned int error:1;
61cbe2ff 3595 unsigned int device_ap_sme:1;
39718852 3596 unsigned int poll_command_supported:1;
32ab4855 3597 unsigned int data_tx_status:1;
536062f2 3598 unsigned int monitor_supported:1;
43245552
DJ
3599 unsigned int auth_supported:1;
3600 unsigned int connect_supported:1;
3601 unsigned int p2p_go_supported:1;
3602 unsigned int p2p_client_supported:1;
3603 unsigned int p2p_concurrent:1;
1c4ffa87 3604 unsigned int channel_switch_supported:1;
429dd9af 3605 unsigned int set_qos_map_supported:1;
57a8f8af 3606 unsigned int have_low_prio_scan:1;
80bc75f1
JM
3607};
3608
3609
562c9d97
AN
3610static unsigned int probe_resp_offload_support(int supp_protocols)
3611{
3612 unsigned int prot = 0;
3613
3614 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3615 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3616 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3617 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3618 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3619 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3620 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3621 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3622
3623 return prot;
3624}
3625
3626
5f439107
JM
3627static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3628 struct nlattr *tb)
80bc75f1 3629{
5f439107
JM
3630 struct nlattr *nl_mode;
3631 int i;
3632
3633 if (tb == NULL)
3634 return;
3635
3636 nla_for_each_nested(nl_mode, tb, i) {
3637 switch (nla_type(nl_mode)) {
3638 case NL80211_IFTYPE_AP:
3639 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3640 break;
24bd4e0b
BC
3641 case NL80211_IFTYPE_MESH_POINT:
3642 info->capa->flags |= WPA_DRIVER_FLAGS_MESH;
3643 break;
65d52fc1
BR
3644 case NL80211_IFTYPE_ADHOC:
3645 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3646 break;
7aad838c
NS
3647 case NL80211_IFTYPE_P2P_DEVICE:
3648 info->capa->flags |=
3649 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3650 break;
5f439107
JM
3651 case NL80211_IFTYPE_P2P_GO:
3652 info->p2p_go_supported = 1;
3653 break;
3654 case NL80211_IFTYPE_P2P_CLIENT:
3655 info->p2p_client_supported = 1;
3656 break;
3657 case NL80211_IFTYPE_MONITOR:
3658 info->monitor_supported = 1;
3659 break;
3660 }
3661 }
3662}
3663
3664
3665static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3666 struct nlattr *nl_combi)
3667{
3668 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3669 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3670 struct nlattr *nl_limit, *nl_mode;
3671 int err, rem_limit, rem_mode;
3672 int combination_has_p2p = 0, combination_has_mgd = 0;
7626850d
JB
3673 static struct nla_policy
3674 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3675 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3676 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3677 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3678 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
f295d0c8 3679 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
7626850d
JB
3680 },
3681 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3682 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3683 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3684 };
80bc75f1 3685
5f439107
JM
3686 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3687 nl_combi, iface_combination_policy);
3688 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3689 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3690 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3691 return 0; /* broken combination */
3692
f295d0c8
SW
3693 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3694 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3695
5f439107
JM
3696 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3697 rem_limit) {
3698 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3699 nl_limit, iface_limit_policy);
3700 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3701 return 0; /* broken combination */
3702
3703 nla_for_each_nested(nl_mode,
3704 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3705 rem_mode) {
3706 int ift = nla_type(nl_mode);
3707 if (ift == NL80211_IFTYPE_P2P_GO ||
3708 ift == NL80211_IFTYPE_P2P_CLIENT)
3709 combination_has_p2p = 1;
3710 if (ift == NL80211_IFTYPE_STATION)
3711 combination_has_mgd = 1;
3712 }
3713 if (combination_has_p2p && combination_has_mgd)
3714 break;
3715 }
3716
3717 if (combination_has_p2p && combination_has_mgd) {
83c4cb52 3718 unsigned int num_channels =
4752147d 3719 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
83c4cb52
FF
3720
3721 info->p2p_concurrent = 1;
3722 if (info->num_multichan_concurrent < num_channels)
3723 info->num_multichan_concurrent = num_channels;
5f439107
JM
3724 }
3725
3726 return 0;
3727}
3728
3729
3730static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3731 struct nlattr *tb)
3732{
3733 struct nlattr *nl_combi;
3734 int rem_combi;
3735
3736 if (tb == NULL)
3737 return;
3738
3739 nla_for_each_nested(nl_combi, tb, rem_combi) {
3740 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3741 break;
3742 }
3743}
3744
3745
3746static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3747 struct nlattr *tb)
3748{
3749 struct nlattr *nl_cmd;
3750 int i;
3751
3752 if (tb == NULL)
3753 return;
3754
3755 nla_for_each_nested(nl_cmd, tb, i) {
3756 switch (nla_get_u32(nl_cmd)) {
3757 case NL80211_CMD_AUTHENTICATE:
3758 info->auth_supported = 1;
3759 break;
3760 case NL80211_CMD_CONNECT:
3761 info->connect_supported = 1;
3762 break;
3763 case NL80211_CMD_START_SCHED_SCAN:
3764 info->capa->sched_scan_supported = 1;
3765 break;
3766 case NL80211_CMD_PROBE_CLIENT:
3767 info->poll_command_supported = 1;
3768 break;
1c4ffa87
AO
3769 case NL80211_CMD_CHANNEL_SWITCH:
3770 info->channel_switch_supported = 1;
3771 break;
429dd9af
JM
3772 case NL80211_CMD_SET_QOS_MAP:
3773 info->set_qos_map_supported = 1;
3774 break;
5f439107
JM
3775 }
3776 }
3777}
3778
3779
bee25cc9
JM
3780static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3781 struct nlattr *tb)
3782{
3783 int i, num;
3784 u32 *ciphers;
3785
3786 if (tb == NULL)
3787 return;
3788
3789 num = nla_len(tb) / sizeof(u32);
3790 ciphers = nla_data(tb);
3791 for (i = 0; i < num; i++) {
3792 u32 c = ciphers[i];
3793
3794 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3795 c >> 24, (c >> 16) & 0xff,
3796 (c >> 8) & 0xff, c & 0xff);
3797 switch (c) {
3798 case WLAN_CIPHER_SUITE_CCMP_256:
3799 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3800 break;
3801 case WLAN_CIPHER_SUITE_GCMP_256:
3802 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3803 break;
3804 case WLAN_CIPHER_SUITE_CCMP:
3805 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3806 break;
3807 case WLAN_CIPHER_SUITE_GCMP:
3808 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3809 break;
3810 case WLAN_CIPHER_SUITE_TKIP:
3811 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3812 break;
3813 case WLAN_CIPHER_SUITE_WEP104:
3814 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3815 break;
3816 case WLAN_CIPHER_SUITE_WEP40:
3817 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3818 break;
3819 case WLAN_CIPHER_SUITE_AES_CMAC:
3820 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3821 break;
3822 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3823 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3824 break;
3825 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3826 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3827 break;
3828 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3829 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3830 break;
ae6f9272
JM
3831 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3832 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3833 break;
bee25cc9
JM
3834 }
3835 }
3836}
3837
3838
5f439107
JM
3839static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3840 struct nlattr *tb)
3841{
5f439107
JM
3842 if (tb)
3843 capa->max_remain_on_chan = nla_get_u32(tb);
3844}
3845
3846
3847static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3848 struct nlattr *ext_setup)
3849{
3850 if (tdls == NULL)
3851 return;
3852
3853 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3854 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3855
3856 if (ext_setup) {
3857 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3858 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3859 }
3860}
3861
3862
3863static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3864 struct nlattr *tb)
3865{
3866 u32 flags;
3867 struct wpa_driver_capa *capa = info->capa;
3868
3869 if (tb == NULL)
3870 return;
3871
3872 flags = nla_get_u32(tb);
3873
3874 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3875 info->data_tx_status = 1;
3876
3877 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3878 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3879
3880 if (flags & NL80211_FEATURE_SAE)
3881 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3882
3883 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3884 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
e87ef751
PX
3885
3886 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3887 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
57a8f8af
JB
3888
3889 if (flags & NL80211_FEATURE_LOW_PRIORITY_SCAN)
3890 info->have_low_prio_scan = 1;
5f439107
JM
3891}
3892
3893
3894static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3895 struct nlattr *tb)
3896{
3897 u32 protocols;
3898
3899 if (tb == NULL)
3900 return;
3901
3902 protocols = nla_get_u32(tb);
3903 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3904 "mode");
3905 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3906 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3907}
3908
3909
e4fa8b12
EP
3910static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3911 struct nlattr *tb)
3912{
3913 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3914
3915 if (tb == NULL)
3916 return;
3917
3918 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3919 tb, NULL))
3920 return;
3921
3922 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3923 capa->wowlan_triggers.any = 1;
3924 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3925 capa->wowlan_triggers.disconnect = 1;
3926 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3927 capa->wowlan_triggers.magic_pkt = 1;
3928 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3929 capa->wowlan_triggers.gtk_rekey_failure = 1;
3930 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3931 capa->wowlan_triggers.eap_identity_req = 1;
3932 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3933 capa->wowlan_triggers.four_way_handshake = 1;
3934 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3935 capa->wowlan_triggers.rfkill_release = 1;
3936}
3937
3938
5f439107
JM
3939static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3940{
3941 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3942 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3943 struct wiphy_info_data *info = arg;
3944 struct wpa_driver_capa *capa = info->capa;
8cd6b7bc 3945 struct wpa_driver_nl80211_data *drv = info->drv;
5f439107 3946
80bc75f1
JM
3947 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3948 genlmsg_attrlen(gnlh, 0), NULL);
3949
5fbcb45d 3950 if (tb[NL80211_ATTR_WIPHY_NAME])
24f051eb 3951 os_strlcpy(drv->phyname,
5fbcb45d
AS
3952 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3953 sizeof(drv->phyname));
80bc75f1 3954 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
e8b5e24e 3955 capa->max_scan_ssids =
80bc75f1
JM
3956 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3957
d21c63b9 3958 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
e8b5e24e 3959 capa->max_sched_scan_ssids =
d21c63b9
LC
3960 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3961
bd525934 3962 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
e8b5e24e 3963 capa->max_match_sets =
bd525934
LC
3964 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3965
3c4ca363
VN
3966 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3967 capa->max_acl_mac_addrs =
3968 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3969
5f439107
JM
3970 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3971 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3972 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
bee25cc9 3973 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
93d11400 3974
e8b5e24e
JB
3975 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3976 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3977 "off-channel TX");
3978 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3979 }
3980
3981 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3982 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3983 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3984 }
5dfca53f 3985
5f439107
JM
3986 wiphy_info_max_roc(capa,
3987 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
004ba773 3988
70619a5d
EP
3989 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3990 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
3991
5f439107
JM
3992 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3993 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
03ea1786 3994
61cbe2ff
JB
3995 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3996 info->device_ap_sme = 1;
3997
5f439107
JM
3998 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3999 wiphy_info_probe_resp_offload(capa,
4000 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
562c9d97 4001
8cd6b7bc
JB
4002 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
4003 drv->extended_capa == NULL) {
4004 drv->extended_capa =
4005 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
4006 if (drv->extended_capa) {
4007 os_memcpy(drv->extended_capa,
4008 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
4009 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
4010 drv->extended_capa_len =
4011 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
4012 }
4013 drv->extended_capa_mask =
4014 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
4015 if (drv->extended_capa_mask) {
4016 os_memcpy(drv->extended_capa_mask,
4017 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
4018 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
4019 } else {
4020 os_free(drv->extended_capa);
4021 drv->extended_capa = NULL;
4022 drv->extended_capa_len = 0;
4023 }
4024 }
4025
17b79e65
JM
4026 if (tb[NL80211_ATTR_VENDOR_DATA]) {
4027 struct nlattr *nl;
4028 int rem;
4029
4030 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
4031 struct nl80211_vendor_cmd_info *vinfo;
080cc445 4032 if (nla_len(nl) != sizeof(*vinfo)) {
17b79e65
JM
4033 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
4034 continue;
4035 }
4036 vinfo = nla_data(nl);
0800f9ee
JM
4037 switch (vinfo->subcmd) {
4038 case QCA_NL80211_VENDOR_SUBCMD_ROAMING:
4039 drv->roaming_vendor_cmd_avail = 1;
4040 break;
4041 case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
65d645ce 4042 drv->dfs_vendor_cmd_avail = 1;
0800f9ee 4043 break;
b41f2684
CL
4044 case QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY:
4045 drv->key_mgmt_set_key_vendor_cmd_avail = 1;
4046 break;
0800f9ee 4047 }
65d645ce 4048
17b79e65
JM
4049 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
4050 vinfo->vendor_id, vinfo->subcmd);
4051 }
4052 }
4053
4054 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
4055 struct nlattr *nl;
4056 int rem;
4057
4058 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
4059 struct nl80211_vendor_cmd_info *vinfo;
080cc445 4060 if (nla_len(nl) != sizeof(*vinfo)) {
17b79e65
JM
4061 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
4062 continue;
4063 }
4064 vinfo = nla_data(nl);
b41f2684
CL
4065 if (vinfo->subcmd ==
4066 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_ROAM_AUTH)
4067 drv->roam_auth_vendor_event_avail = 1;
17b79e65
JM
4068 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
4069 vinfo->vendor_id, vinfo->subcmd);
4070 }
4071 }
4072
e4fa8b12
EP
4073 wiphy_info_wowlan_triggers(capa,
4074 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
4075
ea40a575
CM
4076 if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
4077 capa->max_stations =
4078 nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
4079
80bc75f1
JM
4080 return NL_SKIP;
4081}
4082
4083
4084static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
4085 struct wiphy_info_data *info)
4086{
43245552 4087 u32 feat;
80bc75f1
JM
4088 struct nl_msg *msg;
4089
4090 os_memset(info, 0, sizeof(*info));
e8b5e24e 4091 info->capa = &drv->capa;
8cd6b7bc 4092 info->drv = drv;
89e07afb 4093
80bc75f1
JM
4094 msg = nlmsg_alloc();
4095 if (!msg)
4096 return -1;
4097
43245552
DJ
4098 feat = get_nl80211_protocol_features(drv);
4099 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
4100 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
4101 else
4102 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
80bc75f1 4103
43245552 4104 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
834ee56f 4105 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
f632e483 4106 goto nla_put_failure;
80bc75f1 4107
43245552
DJ
4108 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
4109 return -1;
4110
4111 if (info->auth_supported)
4112 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
4113 else if (!info->connect_supported) {
4114 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
4115 "authentication/association or connect commands");
4116 info->error = 1;
4117 }
4118
4119 if (info->p2p_go_supported && info->p2p_client_supported)
4120 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
4121 if (info->p2p_concurrent) {
4122 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
4123 "interface (driver advertised support)");
4124 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
4125 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
4126 }
4752147d 4127 if (info->num_multichan_concurrent > 1) {
43245552
DJ
4128 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
4129 "concurrent (driver advertised support)");
4752147d
IP
4130 drv->capa.num_multichan_concurrent =
4131 info->num_multichan_concurrent;
43245552 4132 }
97752f79
JB
4133 if (drv->capa.flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
4134 wpa_printf(MSG_DEBUG, "nl80211: use P2P_DEVICE support");
b691dcb1
IP
4135
4136 /* default to 5000 since early versions of mac80211 don't set it */
4137 if (!drv->capa.max_remain_on_chan)
4138 drv->capa.max_remain_on_chan = 5000;
4139
991aa9c7
AO
4140 if (info->channel_switch_supported)
4141 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
4142
43245552 4143 return 0;
80bc75f1
JM
4144nla_put_failure:
4145 nlmsg_free(msg);
4146 return -1;
4147}
4148
4149
93d11400 4150static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
80bc75f1
JM
4151{
4152 struct wiphy_info_data info;
4153 if (wpa_driver_nl80211_get_info(drv, &info))
93d11400 4154 return -1;
e8b5e24e
JB
4155
4156 if (info.error)
4157 return -1;
4158
80bc75f1 4159 drv->has_capability = 1;
1b2a72e8
JM
4160 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4161 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4162 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4163 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
291b6068
JM
4164 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
4165 WPA_DRIVER_AUTH_SHARED |
4166 WPA_DRIVER_AUTH_LEAP;
1b2a72e8 4167
871f4dd0 4168 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
0194fedb 4169 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2fee890a 4170 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
e1bd4e19 4171
354c903f
MB
4172 /*
4173 * As all cfg80211 drivers must support cases where the AP interface is
4174 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
4175 * case that the user space daemon has crashed, they must be able to
4176 * cleanup all stations and key entries in the AP tear down flow. Thus,
4177 * this flag can/should always be set for cfg80211 drivers.
4178 */
4179 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
4180
e5091674 4181 if (!info.device_ap_sme) {
e1bd4e19 4182 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
0194fedb 4183
e5091674
JM
4184 /*
4185 * No AP SME is currently assumed to also indicate no AP MLME
4186 * in the driver/firmware.
4187 */
4188 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
4189 }
4190
61cbe2ff 4191 drv->device_ap_sme = info.device_ap_sme;
39718852 4192 drv->poll_command_supported = info.poll_command_supported;
32ab4855 4193 drv->data_tx_status = info.data_tx_status;
429dd9af
JM
4194 if (info.set_qos_map_supported)
4195 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
57a8f8af 4196 drv->have_low_prio_scan = info.have_low_prio_scan;
61cbe2ff 4197
a11241fa 4198 /*
73a3c6ff
FF
4199 * If poll command and tx status are supported, mac80211 is new enough
4200 * to have everything we need to not need monitor interfaces.
a11241fa 4201 */
73a3c6ff 4202 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
a11241fa 4203
536062f2
JM
4204 if (drv->device_ap_sme && drv->use_monitor) {
4205 /*
4206 * Non-mac80211 drivers may not support monitor interface.
4207 * Make sure we do not get stuck with incorrect capability here
4208 * by explicitly testing this.
4209 */
4210 if (!info.monitor_supported) {
4211 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
4212 "with device_ap_sme since no monitor mode "
4213 "support detected");
4214 drv->use_monitor = 0;
4215 }
4216 }
4217
a11241fa
JB
4218 /*
4219 * If we aren't going to use monitor interfaces, but the
4220 * driver doesn't support data TX status, we won't get TX
4221 * status for EAPOL frames.
4222 */
4223 if (!drv->use_monitor && !info.data_tx_status)
4224 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4225
93d11400 4226 return 0;
80bc75f1
JM
4227}
4228
4229
b088cf82
JM
4230#ifdef ANDROID
4231static int android_genl_ctrl_resolve(struct nl_handle *handle,
4232 const char *name)
4233{
4234 /*
4235 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4236 * need to work around that.
4237 */
4238 struct nl_cache *cache = NULL;
4239 struct genl_family *nl80211 = NULL;
4240 int id = -1;
4241
4242 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4243 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4244 "netlink cache");
4245 goto fail;
4246 }
4247
4248 nl80211 = genl_ctrl_search_by_name(cache, name);
4249 if (nl80211 == NULL)
4250 goto fail;
4251
4252 id = genl_family_get_id(nl80211);
4253
4254fail:
4255 if (nl80211)
4256 genl_family_put(nl80211);
4257 if (cache)
4258 nl_cache_free(cache);
4259
4260 return id;
4261}
4262#define genl_ctrl_resolve android_genl_ctrl_resolve
4263#endif /* ANDROID */
4264
4265
2a7b66f5
BG
4266static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
4267{
d6c9aab8
JB
4268 int ret;
4269
2a7b66f5
BG
4270 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4271 if (global->nl_cb == NULL) {
4272 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4273 "callbacks");
4274 return -1;
4275 }
4276
481234cf
JM
4277 global->nl = nl_create_handle(global->nl_cb, "nl");
4278 if (global->nl == NULL)
d6c9aab8 4279 goto err;
276e2d67 4280
481234cf 4281 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
335d42b1 4282 if (global->nl80211_id < 0) {
276e2d67
BG
4283 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4284 "found");
d6c9aab8 4285 goto err;
276e2d67
BG
4286 }
4287
481234cf
JM
4288 global->nl_event = nl_create_handle(global->nl_cb, "event");
4289 if (global->nl_event == NULL)
d6c9aab8 4290 goto err;
9fff9fdc 4291
d6c9aab8 4292 ret = nl_get_multicast_id(global, "nl80211", "scan");
97865538 4293 if (ret >= 0)
481234cf 4294 ret = nl_socket_add_membership(global->nl_event, ret);
97865538
JM
4295 if (ret < 0) {
4296 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4297 "membership for scan events: %d (%s)",
4298 ret, strerror(-ret));
d6c9aab8 4299 goto err;
97865538 4300 }
c2a04078 4301
d6c9aab8 4302 ret = nl_get_multicast_id(global, "nl80211", "mlme");
c2a04078 4303 if (ret >= 0)
481234cf 4304 ret = nl_socket_add_membership(global->nl_event, ret);
c2a04078
JM
4305 if (ret < 0) {
4306 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4307 "membership for mlme events: %d (%s)",
4308 ret, strerror(-ret));
d6c9aab8 4309 goto err;
c2a04078 4310 }
c2a04078 4311
d6c9aab8 4312 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
33c5deb8 4313 if (ret >= 0)
481234cf 4314 ret = nl_socket_add_membership(global->nl_event, ret);
33c5deb8
JM
4315 if (ret < 0) {
4316 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4317 "membership for regulatory events: %d (%s)",
4318 ret, strerror(-ret));
4319 /* Continue without regulatory events */
4320 }
4321
17b79e65
JM
4322 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4323 if (ret >= 0)
4324 ret = nl_socket_add_membership(global->nl_event, ret);
4325 if (ret < 0) {
4326 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4327 "membership for vendor events: %d (%s)",
4328 ret, strerror(-ret));
4329 /* Continue without vendor events */
4330 }
4331
d6c9aab8
JB
4332 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4333 no_seq_check, NULL);
4334 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4335 process_global_event, global);
4336
5f65e9f7
JB
4337 nl80211_register_eloop_read(&global->nl_event,
4338 wpa_driver_nl80211_event_receive,
4339 global->nl_cb);
d6c9aab8
JB
4340
4341 return 0;
4342
4343err:
4344 nl_destroy_handles(&global->nl_event);
4345 nl_destroy_handles(&global->nl);
4346 nl_cb_put(global->nl_cb);
671a5039 4347 global->nl_cb = NULL;
d6c9aab8
JB
4348 return -1;
4349}
4350
4351
4352static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4353{
1afc986d
JB
4354 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4355 if (!drv->nl_cb) {
4356 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
d6c9aab8 4357 return -1;
1afc986d
JB
4358 }
4359
4360 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4361 no_seq_check, NULL);
f06aedd9
JB
4362 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4363 process_drv_event, drv);
1afc986d 4364
9fff9fdc 4365 return 0;
9fff9fdc
JM
4366}
4367
4368
8401a6b0
JM
4369static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4370{
8401a6b0 4371 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
a63063b4
JM
4372 /*
4373 * This may be for any interface; use ifdown event to disable
4374 * interface.
4375 */
8401a6b0
JM
4376}
4377
4378
4379static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4380{
4381 struct wpa_driver_nl80211_data *drv = ctx;
4382 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
834ee56f 4383 if (i802_set_iface_flags(drv->first_bss, 1)) {
8401a6b0
JM
4384 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4385 "after rfkill unblock");
4386 return;
4387 }
a63063b4 4388 /* rtnetlink ifup handler will report interface as enabled */
8401a6b0
JM
4389}
4390
4391
32ab4855
JB
4392static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4393 void *eloop_ctx,
4394 void *handle)
4395{
4396 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4397 u8 data[2048];
4398 struct msghdr msg;
4399 struct iovec entry;
cad0f50e 4400 u8 control[512];
32ab4855
JB
4401 struct cmsghdr *cmsg;
4402 int res, found_ee = 0, found_wifi = 0, acked = 0;
4403 union wpa_event_data event;
4404
4405 memset(&msg, 0, sizeof(msg));
4406 msg.msg_iov = &entry;
4407 msg.msg_iovlen = 1;
4408 entry.iov_base = data;
4409 entry.iov_len = sizeof(data);
4410 msg.msg_control = &control;
4411 msg.msg_controllen = sizeof(control);
4412
4413 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4414 /* if error or not fitting 802.3 header, return */
4415 if (res < 14)
4416 return;
4417
4418 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4419 {
4420 if (cmsg->cmsg_level == SOL_SOCKET &&
4421 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4422 int *ack;
4423
4424 found_wifi = 1;
4425 ack = (void *)CMSG_DATA(cmsg);
4426 acked = *ack;
4427 }
4428
4429 if (cmsg->cmsg_level == SOL_PACKET &&
4430 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4431 struct sock_extended_err *err =
4432 (struct sock_extended_err *)CMSG_DATA(cmsg);
4433
4434 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4435 found_ee = 1;
4436 }
4437 }
4438
4439 if (!found_ee || !found_wifi)
4440 return;
4441
4442 memset(&event, 0, sizeof(event));
4443 event.eapol_tx_status.dst = data;
4444 event.eapol_tx_status.data = data + 14;
4445 event.eapol_tx_status.data_len = res - 14;
4446 event.eapol_tx_status.ack = acked;
4447 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4448}
4449
4450
cc7a48d1
JB
4451static int nl80211_init_bss(struct i802_bss *bss)
4452{
4453 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4454 if (!bss->nl_cb)
4455 return -1;
4456
4457 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4458 no_seq_check, NULL);
4459 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4460 process_bss_event, bss);
4461
4462 return 0;
4463}
4464
4465
4466static void nl80211_destroy_bss(struct i802_bss *bss)
4467{
4468 nl_cb_put(bss->nl_cb);
4469 bss->nl_cb = NULL;
4470}
4471
4472
0d547d5f
JM
4473static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4474 void *global_priv, int hostapd,
4475 const u8 *set_addr)
9fff9fdc 4476{
9fff9fdc 4477 struct wpa_driver_nl80211_data *drv;
8401a6b0 4478 struct rfkill_config *rcfg;
a2e40bb6 4479 struct i802_bss *bss;
9fff9fdc 4480
a5c696ad
JM
4481 if (global_priv == NULL)
4482 return NULL;
9fff9fdc
JM
4483 drv = os_zalloc(sizeof(*drv));
4484 if (drv == NULL)
4485 return NULL;
f2ed8023 4486 drv->global = global_priv;
9fff9fdc 4487 drv->ctx = ctx;
0d547d5f
JM
4488 drv->hostapd = !!hostapd;
4489 drv->eapol_sock = -1;
4490 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4491 drv->if_indices = drv->default_if_indices;
834ee56f
KP
4492
4493 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4494 if (!drv->first_bss) {
4495 os_free(drv);
4496 return NULL;
4497 }
4498 bss = drv->first_bss;
a2e40bb6 4499 bss->drv = drv;
a5e1eb20
SE
4500 bss->ctx = ctx;
4501
a2e40bb6 4502 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
9fff9fdc
JM
4503 drv->monitor_ifidx = -1;
4504 drv->monitor_sock = -1;
d12dab4c 4505 drv->eapol_tx_sock = -1;
b1f625e0 4506 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
9fff9fdc 4507
ba2d0d7d 4508 if (wpa_driver_nl80211_init_nl(drv)) {
bbaf0837
JM
4509 os_free(drv);
4510 return NULL;
4511 }
9fff9fdc 4512
cc7a48d1
JB
4513 if (nl80211_init_bss(bss))
4514 goto failed;
4515
8401a6b0
JM
4516 rcfg = os_zalloc(sizeof(*rcfg));
4517 if (rcfg == NULL)
4518 goto failed;
4519 rcfg->ctx = drv;
4520 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4521 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4522 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4523 drv->rfkill = rfkill_init(rcfg);
52169389 4524 if (drv->rfkill == NULL) {
8401a6b0 4525 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
52169389
JM
4526 os_free(rcfg);
4527 }
8401a6b0 4528
146fa9b3
JM
4529 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4530 drv->start_iface_up = 1;
4531
49b4b205 4532 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
bbaf0837 4533 goto failed;
7524cfb1 4534
d12dab4c 4535 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
32ab4855
JB
4536 if (drv->eapol_tx_sock < 0)
4537 goto failed;
4538
4539 if (drv->data_tx_status) {
4540 int enabled = 1;
4541
4542 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4543 &enabled, sizeof(enabled)) < 0) {
4544 wpa_printf(MSG_DEBUG,
4545 "nl80211: wifi status sockopt failed\n");
4546 drv->data_tx_status = 0;
a11241fa
JB
4547 if (!drv->use_monitor)
4548 drv->capa.flags &=
4549 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
32ab4855
JB
4550 } else {
4551 eloop_register_read_sock(drv->eapol_tx_sock,
4552 wpa_driver_nl80211_handle_eapol_tx_status,
4553 drv, NULL);
4554 }
4555 }
f10bfc9a 4556
dac12351 4557 if (drv->global) {
c4bb8817 4558 dl_list_add(&drv->global->interfaces, &drv->list);
dac12351
BG
4559 drv->in_interface_list = 1;
4560 }
c4bb8817 4561
a2e40bb6 4562 return bss;
7524cfb1 4563
bbaf0837 4564failed:
dac12351 4565 wpa_driver_nl80211_deinit(bss);
7524cfb1
JM
4566 return NULL;
4567}
4568
4569
0d547d5f
JM
4570/**
4571 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4572 * @ctx: context to be used when calling wpa_supplicant functions,
4573 * e.g., wpa_supplicant_event()
4574 * @ifname: interface name, e.g., wlan0
4575 * @global_priv: private driver global data from global_init()
4576 * Returns: Pointer to private data, %NULL on failure
4577 */
4578static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4579 void *global_priv)
4580{
4581 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4582}
4583
4584
a11241fa 4585static int nl80211_register_frame(struct i802_bss *bss,
5582a5d1 4586 struct nl_handle *nl_handle,
bd94971e 4587 u16 type, const u8 *match, size_t match_len)
58f6fbe0 4588{
a11241fa 4589 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0
JM
4590 struct nl_msg *msg;
4591 int ret = -1;
880de885 4592 char buf[30];
58f6fbe0
JM
4593
4594 msg = nlmsg_alloc();
4595 if (!msg)
4596 return -1;
4597
880de885
JM
4598 buf[0] = '\0';
4599 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
dedfa440
PF
4600 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
4601 type, fc2str(type), nl_handle, buf);
36488c05 4602
9fb04070 4603 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
58f6fbe0 4604
f632e483 4605 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80
DS
4606 goto nla_put_failure;
4607
bd94971e 4608 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
58f6fbe0
JM
4609 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4610
d6c9aab8 4611 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
58f6fbe0
JM
4612 msg = NULL;
4613 if (ret) {
5582a5d1
JB
4614 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4615 "failed (type=%u): ret=%d (%s)",
4616 type, ret, strerror(-ret));
4617 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
58f6fbe0
JM
4618 match, match_len);
4619 goto nla_put_failure;
4620 }
4621 ret = 0;
4622nla_put_failure:
4623 nlmsg_free(msg);
4624 return ret;
4625}
4626
4627
a11241fa
JB
4628static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4629{
4630 struct wpa_driver_nl80211_data *drv = bss->drv;
4631
481234cf 4632 if (bss->nl_mgmt) {
a11241fa 4633 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
36488c05 4634 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
a11241fa
JB
4635 return -1;
4636 }
4637
481234cf
JM
4638 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4639 if (bss->nl_mgmt == NULL)
a11241fa
JB
4640 return -1;
4641
a11241fa
JB
4642 return 0;
4643}
4644
4645
5f65e9f7
JB
4646static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4647{
4648 nl80211_register_eloop_read(&bss->nl_mgmt,
4649 wpa_driver_nl80211_event_receive,
4650 bss->nl_cb);
4651}
4652
4653
a11241fa 4654static int nl80211_register_action_frame(struct i802_bss *bss,
bd94971e
JB
4655 const u8 *match, size_t match_len)
4656{
4657 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
481234cf 4658 return nl80211_register_frame(bss, bss->nl_mgmt,
5582a5d1 4659 type, match, match_len);
bd94971e
JB
4660}
4661
4662
a11241fa 4663static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
58f6fbe0 4664{
a11241fa 4665 struct wpa_driver_nl80211_data *drv = bss->drv;
6f06766e 4666 int ret = 0;
a11241fa
JB
4667
4668 if (nl80211_alloc_mgmt_handle(bss))
4669 return -1;
36488c05
JM
4670 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4671 "handle %p", bss->nl_mgmt);
a11241fa 4672
e8d1168b
JB
4673 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4674 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4675
4676 /* register for any AUTH message */
4677 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4678 }
4679
56f5af48
JM
4680#ifdef CONFIG_INTERWORKING
4681 /* QoS Map Configure */
4682 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
6f06766e 4683 ret = -1;
56f5af48 4684#endif /* CONFIG_INTERWORKING */
4fe9fa0d 4685#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
046b26a2 4686 /* GAS Initial Request */
a11241fa 4687 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
6f06766e 4688 ret = -1;
046b26a2 4689 /* GAS Initial Response */
a11241fa 4690 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
6f06766e 4691 ret = -1;
18708aad 4692 /* GAS Comeback Request */
a11241fa 4693 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
6f06766e 4694 ret = -1;
18708aad 4695 /* GAS Comeback Response */
a11241fa 4696 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
6f06766e 4697 ret = -1;
c5a64e2d
JM
4698 /* Protected GAS Initial Request */
4699 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4700 ret = -1;
4701 /* Protected GAS Initial Response */
4702 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4703 ret = -1;
4704 /* Protected GAS Comeback Request */
4705 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4706 ret = -1;
4707 /* Protected GAS Comeback Response */
4708 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4709 ret = -1;
4fe9fa0d
JM
4710#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4711#ifdef CONFIG_P2P
046b26a2 4712 /* P2P Public Action */
a11241fa 4713 if (nl80211_register_action_frame(bss,
046b26a2
JM
4714 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4715 6) < 0)
6f06766e 4716 ret = -1;
046b26a2 4717 /* P2P Action */
a11241fa 4718 if (nl80211_register_action_frame(bss,
046b26a2
JM
4719 (u8 *) "\x7f\x50\x6f\x9a\x09",
4720 5) < 0)
6f06766e 4721 ret = -1;
046b26a2 4722#endif /* CONFIG_P2P */
7d878ca7
JM
4723#ifdef CONFIG_IEEE80211W
4724 /* SA Query Response */
a11241fa 4725 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
6f06766e 4726 ret = -1;
7d878ca7 4727#endif /* CONFIG_IEEE80211W */
35287637
AN
4728#ifdef CONFIG_TDLS
4729 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4730 /* TDLS Discovery Response */
aa543c0c 4731 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
35287637 4732 0)
6f06766e 4733 ret = -1;
35287637
AN
4734 }
4735#endif /* CONFIG_TDLS */
046b26a2 4736
7b90c16a 4737 /* FT Action frames */
a11241fa 4738 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
6f06766e 4739 ret = -1;
7b90c16a
JM
4740 else
4741 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4742 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4743
71269b37 4744 /* WNM - BSS Transition Management Request */
a11241fa 4745 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
6f06766e 4746 ret = -1;
bd896433
JM
4747 /* WNM-Sleep Mode Response */
4748 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
6f06766e 4749 ret = -1;
a11241fa 4750
95a3ea94
JM
4751#ifdef CONFIG_HS20
4752 /* WNM-Notification */
4753 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
3ee18569 4754 ret = -1;
95a3ea94
JM
4755#endif /* CONFIG_HS20 */
4756
5f65e9f7
JB
4757 nl80211_mgmt_handle_register_eloop(bss);
4758
6f06766e 4759 return ret;
a11241fa
JB
4760}
4761
4762
02bb32c3
JB
4763static int nl80211_register_spurious_class3(struct i802_bss *bss)
4764{
4765 struct wpa_driver_nl80211_data *drv = bss->drv;
4766 struct nl_msg *msg;
4767 int ret = -1;
4768
4769 msg = nlmsg_alloc();
4770 if (!msg)
4771 return -1;
4772
4773 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4774
4775 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4776
481234cf 4777 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
4778 msg = NULL;
4779 if (ret) {
4780 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4781 "failed: ret=%d (%s)",
4782 ret, strerror(-ret));
4783 goto nla_put_failure;
4784 }
4785 ret = 0;
4786nla_put_failure:
4787 nlmsg_free(msg);
4788 return ret;
4789}
4790
4791
a11241fa
JB
4792static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4793{
4794 static const int stypes[] = {
4795 WLAN_FC_STYPE_AUTH,
4796 WLAN_FC_STYPE_ASSOC_REQ,
4797 WLAN_FC_STYPE_REASSOC_REQ,
4798 WLAN_FC_STYPE_DISASSOC,
4799 WLAN_FC_STYPE_DEAUTH,
4800 WLAN_FC_STYPE_ACTION,
4801 WLAN_FC_STYPE_PROBE_REQ,
4802/* Beacon doesn't work as mac80211 doesn't currently allow
4803 * it, but it wouldn't really be the right thing anyway as
4804 * it isn't per interface ... maybe just dump the scan
4805 * results periodically for OLBC?
4806 */
0e80ea2c 4807 /* WLAN_FC_STYPE_BEACON, */
a11241fa
JB
4808 };
4809 unsigned int i;
4810
4811 if (nl80211_alloc_mgmt_handle(bss))
71269b37 4812 return -1;
36488c05
JM
4813 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4814 "handle %p", bss->nl_mgmt);
71269b37 4815
e7ecab4a 4816 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
481234cf 4817 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
4818 (WLAN_FC_TYPE_MGMT << 2) |
4819 (stypes[i] << 4),
4820 NULL, 0) < 0) {
4821 goto out_err;
4822 }
4823 }
4824
02bb32c3
JB
4825 if (nl80211_register_spurious_class3(bss))
4826 goto out_err;
4827
e32ad281
JB
4828 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4829 goto out_err;
4830
5f65e9f7 4831 nl80211_mgmt_handle_register_eloop(bss);
58f6fbe0 4832 return 0;
a11241fa
JB
4833
4834out_err:
a11241fa
JB
4835 nl_destroy_handles(&bss->nl_mgmt);
4836 return -1;
4837}
4838
4839
a6cc0602
JM
4840static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4841{
4842 if (nl80211_alloc_mgmt_handle(bss))
4843 return -1;
4844 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4845 "handle %p (device SME)", bss->nl_mgmt);
4846
4847 if (nl80211_register_frame(bss, bss->nl_mgmt,
4848 (WLAN_FC_TYPE_MGMT << 2) |
4849 (WLAN_FC_STYPE_ACTION << 4),
4850 NULL, 0) < 0)
4851 goto out_err;
4852
5f65e9f7 4853 nl80211_mgmt_handle_register_eloop(bss);
a6cc0602
JM
4854 return 0;
4855
4856out_err:
a6cc0602
JM
4857 nl_destroy_handles(&bss->nl_mgmt);
4858 return -1;
4859}
4860
4861
36488c05 4862static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 4863{
481234cf 4864 if (bss->nl_mgmt == NULL)
a11241fa 4865 return;
36488c05
JM
4866 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4867 "(%s)", bss->nl_mgmt, reason);
5f65e9f7 4868 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
e32ad281
JB
4869
4870 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
4871}
4872
4873
8401a6b0
JM
4874static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4875{
4876 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4877}
4878
4879
eb4582f2
AS
4880static void nl80211_del_p2pdev(struct i802_bss *bss)
4881{
4882 struct wpa_driver_nl80211_data *drv = bss->drv;
4883 struct nl_msg *msg;
4884 int ret;
4885
4886 msg = nlmsg_alloc();
4887 if (!msg)
4888 return;
4889
4890 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4891 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4892
4893 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4894 msg = NULL;
4895
4896 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4897 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 4898 strerror(-ret));
eb4582f2
AS
4899
4900nla_put_failure:
4901 nlmsg_free(msg);
4902}
4903
4904
eb4582f2 4905static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
f632e483
AS
4906{
4907 struct wpa_driver_nl80211_data *drv = bss->drv;
4908 struct nl_msg *msg;
4909 int ret = -1;
4910
f632e483
AS
4911 msg = nlmsg_alloc();
4912 if (!msg)
4913 return -1;
4914
eb4582f2
AS
4915 if (start)
4916 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4917 else
4918 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4919
f632e483
AS
4920 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4921
4922 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4923 msg = NULL;
4924
eb4582f2
AS
4925 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4926 start ? "Start" : "Stop",
4927 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 4928 strerror(-ret));
eb4582f2 4929
f632e483
AS
4930nla_put_failure:
4931 nlmsg_free(msg);
4932 return ret;
4933}
f632e483
AS
4934
4935
91724d6f
AS
4936static int i802_set_iface_flags(struct i802_bss *bss, int up)
4937{
4938 enum nl80211_iftype nlmode;
4939
4940 nlmode = nl80211_get_ifmode(bss);
4941 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4942 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4943 bss->ifname, up);
4944 }
4945
4946 /* P2P Device has start/stop which is equivalent */
4947 return nl80211_set_p2pdev(bss, up);
4948}
4949
4950
362f781e 4951static int
0d547d5f 4952wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
49b4b205 4953 const u8 *set_addr, int first)
7524cfb1 4954{
834ee56f 4955 struct i802_bss *bss = drv->first_bss;
8401a6b0 4956 int send_rfkill_event = 0;
0d547d5f 4957 enum nl80211_iftype nlmode;
a2e40bb6
FF
4958
4959 drv->ifindex = if_nametoindex(bss->ifname);
f632e483
AS
4960 bss->ifindex = drv->ifindex;
4961 bss->wdev_id = drv->global->if_add_wdevid;
4962 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4963
60b13c20
IP
4964 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4965 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
f632e483
AS
4966 drv->global->if_add_wdevid_set = 0;
4967
bf144cf6
AP
4968 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4969 bss->static_ap = 1;
4970
f632e483
AS
4971 if (wpa_driver_nl80211_capa(drv))
4972 return -1;
a87c9d96 4973
5fbcb45d
AS
4974 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4975 bss->ifname, drv->phyname);
4976
0d547d5f
JM
4977 if (set_addr &&
4978 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4979 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4980 set_addr)))
4981 return -1;
4982
49b4b205
JM
4983 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4984 drv->start_mode_ap = 1;
4985
bf144cf6 4986 if (drv->hostapd || bss->static_ap)
0d547d5f
JM
4987 nlmode = NL80211_IFTYPE_AP;
4988 else if (bss->if_dynamic)
8e12685c 4989 nlmode = nl80211_get_ifmode(bss);
0d547d5f
JM
4990 else
4991 nlmode = NL80211_IFTYPE_STATION;
f632e483 4992
8e12685c 4993 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
0d547d5f 4994 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
8e12685c 4995 return -1;
a87c9d96
JM
4996 }
4997
8c06db70 4998 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
597b94f5 4999 nl80211_get_macaddr(bss);
f632e483 5000
8c06db70
MB
5001 if (!rfkill_is_blocked(drv->rfkill)) {
5002 int ret = i802_set_iface_flags(bss, 1);
5003 if (ret) {
8401a6b0
JM
5004 wpa_printf(MSG_ERROR, "nl80211: Could not set "
5005 "interface '%s' UP", bss->ifname);
8c06db70 5006 return ret;
8401a6b0 5007 }
8c06db70
MB
5008 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
5009 return ret;
5010 } else {
5011 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
5012 "interface '%s' due to rfkill", bss->ifname);
5013 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
5014 return 0;
5015 drv->if_disabled = 1;
5016 send_rfkill_event = 1;
362f781e 5017 }
3f5285e8 5018
0d547d5f
JM
5019 if (!drv->hostapd)
5020 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
5021 1, IF_OPER_DORMANT);
362f781e 5022
c81eff1a 5023 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
341eebee 5024 bss->addr))
2136f480 5025 return -1;
fee354c7 5026 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
f2ed8023 5027
8401a6b0
JM
5028 if (send_rfkill_event) {
5029 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
5030 drv, drv->ctx);
5031 }
5032
362f781e 5033 return 0;
3f5285e8
JM
5034}
5035
5036
8a27af5c
JM
5037static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
5038{
5039 struct nl_msg *msg;
5040
5041 msg = nlmsg_alloc();
5042 if (!msg)
5043 return -ENOMEM;
5044
08e55ebb
JM
5045 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
5046 drv->ifindex);
9fb04070 5047 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
8a27af5c
JM
5048 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5049
5050 return send_and_recv_msgs(drv, msg, NULL, NULL);
5051 nla_put_failure:
5883168a 5052 nlmsg_free(msg);
8a27af5c
JM
5053 return -ENOBUFS;
5054}
8a27af5c
JM
5055
5056
3f5285e8 5057/**
7e5ba1b9 5058 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
9ebce9c5 5059 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
5060 *
5061 * Shut down driver interface and processing of driver events. Free
5062 * private data buffer if one was allocated in wpa_driver_nl80211_init().
5063 */
9ebce9c5 5064static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
3f5285e8 5065{
a2e40bb6 5066 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 5067
873d0fcf 5068 bss->in_deinit = 1;
32ab4855
JB
5069 if (drv->data_tx_status)
5070 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
5071 if (drv->eapol_tx_sock >= 0)
5072 close(drv->eapol_tx_sock);
f10bfc9a 5073
481234cf 5074 if (bss->nl_preq)
5582a5d1 5075 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 5076 if (bss->added_if_into_bridge) {
c81eff1a
BG
5077 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
5078 bss->ifname) < 0)
94627f6c
JM
5079 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5080 "interface %s from bridge %s: %s",
e17a2477 5081 bss->ifname, bss->brname, strerror(errno));
97ed9a06 5082 if (drv->rtnl_sk)
ca3c6b4d 5083 nl80211_handle_destroy(drv->rtnl_sk);
94627f6c 5084 }
e17a2477 5085 if (bss->added_bridge) {
c81eff1a 5086 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
5087 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5088 "bridge %s: %s",
e17a2477 5089 bss->brname, strerror(errno));
94627f6c
JM
5090 }
5091
460456f8 5092 nl80211_remove_monitor_interface(drv);
8a27af5c 5093
b1f625e0 5094 if (is_ap_interface(drv->nlmode))
8a27af5c 5095 wpa_driver_nl80211_del_beacon(drv);
0915d02c 5096
bbaf0837
JM
5097 if (drv->eapol_sock >= 0) {
5098 eloop_unregister_read_sock(drv->eapol_sock);
5099 close(drv->eapol_sock);
5100 }
5101
5102 if (drv->if_indices != drv->default_if_indices)
5103 os_free(drv->if_indices);
3f5285e8 5104
b3af99d2 5105 if (drv->disabled_11b_rates)
4e5cb1a3
JM
5106 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
5107
36d84860
BG
5108 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
5109 IF_OPER_UP);
e390df05 5110 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
8401a6b0 5111 rfkill_deinit(drv->rfkill);
3f5285e8 5112
bbaf0837
JM
5113 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5114
146fa9b3
JM
5115 if (!drv->start_iface_up)
5116 (void) i802_set_iface_flags(bss, 0);
fee354c7
JM
5117
5118 if (drv->addr_changed) {
93da0498
JM
5119 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
5120 0) < 0) {
5121 wpa_printf(MSG_DEBUG,
5122 "nl80211: Could not set interface down to restore permanent MAC address");
5123 }
fee354c7
JM
5124 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5125 drv->perm_addr) < 0) {
5126 wpa_printf(MSG_DEBUG,
5127 "nl80211: Could not restore permanent MAC address");
5128 }
5129 }
5130
8e12685c 5131 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
49b4b205
JM
5132 if (!drv->hostapd || !drv->start_mode_ap)
5133 wpa_driver_nl80211_set_mode(bss,
5134 NL80211_IFTYPE_STATION);
b378c41f 5135 nl80211_mgmt_unsubscribe(bss, "deinit");
8e12685c
AS
5136 } else {
5137 nl80211_mgmt_unsubscribe(bss, "deinit");
eb4582f2 5138 nl80211_del_p2pdev(bss);
8e12685c 5139 }
1afc986d 5140 nl_cb_put(drv->nl_cb);
3f5285e8 5141
834ee56f 5142 nl80211_destroy_bss(drv->first_bss);
cc7a48d1 5143
3812464c
JM
5144 os_free(drv->filter_ssids);
5145
536fd62d
JM
5146 os_free(drv->auth_ie);
5147
dac12351 5148 if (drv->in_interface_list)
f2ed8023
JM
5149 dl_list_del(&drv->list);
5150
8cd6b7bc
JB
5151 os_free(drv->extended_capa);
5152 os_free(drv->extended_capa_mask);
834ee56f 5153 os_free(drv->first_bss);
3f5285e8
JM
5154 os_free(drv);
5155}
5156
5157
5158/**
5159 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
ad1e68e6 5160 * @eloop_ctx: Driver private data
3f5285e8
JM
5161 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
5162 *
5163 * This function can be used as registered timeout when starting a scan to
5164 * generate a scan completed event if the driver does not report this.
5165 */
5166static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
5167{
ad1e68e6 5168 struct wpa_driver_nl80211_data *drv = eloop_ctx;
b1f625e0 5169 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
834ee56f 5170 wpa_driver_nl80211_set_mode(drv->first_bss,
b1f625e0
EP
5171 drv->ap_scan_as_station);
5172 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6 5173 }
3f5285e8
JM
5174 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
5175 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
5176}
5177
5178
95ac3bf4
JM
5179static struct nl_msg *
5180nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
d3aaef80 5181 struct wpa_driver_scan_params *params, u64 *wdev_id)
3f5285e8 5182{
95ac3bf4 5183 struct nl_msg *msg;
6a1063e0 5184 size_t i;
57a8f8af 5185 u32 scan_flags = 0;
3f5285e8 5186
0e75527f 5187 msg = nlmsg_alloc();
f0494d0f 5188 if (!msg)
95ac3bf4 5189 return NULL;
3812464c 5190
95ac3bf4 5191 nl80211_cmd(drv, msg, 0, cmd);
0e75527f 5192
d3aaef80
DS
5193 if (!wdev_id)
5194 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5195 else
5196 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
3f5285e8 5197
f0494d0f 5198 if (params->num_ssids) {
8970bae8
JB
5199 struct nlattr *ssids;
5200
5201 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
f0494d0f 5202 if (ssids == NULL)
95ac3bf4 5203 goto fail;
f0494d0f
JM
5204 for (i = 0; i < params->num_ssids; i++) {
5205 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
5206 params->ssids[i].ssid,
5207 params->ssids[i].ssid_len);
8970bae8
JB
5208 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
5209 params->ssids[i].ssid) < 0)
95ac3bf4 5210 goto fail;
f0494d0f 5211 }
8970bae8 5212 nla_nest_end(msg, ssids);
3f5285e8
JM
5213 }
5214
d173df52 5215 if (params->extra_ies) {
3b1c7bfd
JM
5216 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
5217 params->extra_ies, params->extra_ies_len);
95ac3bf4
JM
5218 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
5219 params->extra_ies) < 0)
5220 goto fail;
d173df52
JM
5221 }
5222
d3a98225 5223 if (params->freqs) {
8970bae8
JB
5224 struct nlattr *freqs;
5225 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
f0494d0f 5226 if (freqs == NULL)
95ac3bf4 5227 goto fail;
9fad706c
JM
5228 for (i = 0; params->freqs[i]; i++) {
5229 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
5230 "MHz", params->freqs[i]);
8970bae8 5231 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
95ac3bf4 5232 goto fail;
9fad706c 5233 }
8970bae8 5234 nla_nest_end(msg, freqs);
d3a98225
JM
5235 }
5236
95ac3bf4
JM
5237 os_free(drv->filter_ssids);
5238 drv->filter_ssids = params->filter_ssids;
5239 params->filter_ssids = NULL;
5240 drv->num_filter_ssids = params->num_filter_ssids;
5241
949938aa
JM
5242 if (params->only_new_results) {
5243 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
57a8f8af 5244 scan_flags |= NL80211_SCAN_FLAG_FLUSH;
949938aa
JM
5245 }
5246
57a8f8af
JB
5247 if (params->low_priority && drv->have_low_prio_scan) {
5248 wpa_printf(MSG_DEBUG,
5249 "nl80211: Add NL80211_SCAN_FLAG_LOW_PRIORITY");
5250 scan_flags |= NL80211_SCAN_FLAG_LOW_PRIORITY;
5251 }
5252
5253 if (scan_flags)
5254 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags);
5255
95ac3bf4
JM
5256 return msg;
5257
5258fail:
d3aaef80 5259nla_put_failure:
95ac3bf4
JM
5260 nlmsg_free(msg);
5261 return NULL;
5262}
5263
5264
5265/**
5266 * wpa_driver_nl80211_scan - Request the driver to initiate scan
9ebce9c5 5267 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
95ac3bf4
JM
5268 * @params: Scan parameters
5269 * Returns: 0 on success, -1 on failure
5270 */
9ebce9c5 5271static int wpa_driver_nl80211_scan(struct i802_bss *bss,
95ac3bf4
JM
5272 struct wpa_driver_scan_params *params)
5273{
95ac3bf4
JM
5274 struct wpa_driver_nl80211_data *drv = bss->drv;
5275 int ret = -1, timeout;
8970bae8 5276 struct nl_msg *msg = NULL;
95ac3bf4 5277
565110cd 5278 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
95ac3bf4
JM
5279 drv->scan_for_auth = 0;
5280
d3aaef80
DS
5281 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5282 bss->wdev_id_set ? &bss->wdev_id : NULL);
95ac3bf4
JM
5283 if (!msg)
5284 return -1;
5285
47185fc7 5286 if (params->p2p_probe) {
8970bae8
JB
5287 struct nlattr *rates;
5288
8a6a1e1b
JM
5289 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5290
8970bae8 5291 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
f0494d0f
JM
5292 if (rates == NULL)
5293 goto nla_put_failure;
5294
47185fc7
RM
5295 /*
5296 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5297 * by masking out everything else apart from the OFDM rates 6,
5298 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5299 * rates are left enabled.
5300 */
8970bae8 5301 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
47185fc7 5302 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8970bae8 5303 nla_nest_end(msg, rates);
970fa12e
RM
5304
5305 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
47185fc7
RM
5306 }
5307
0e75527f
JM
5308 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5309 msg = NULL;
5310 if (ret) {
5311 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5312 "(%s)", ret, strerror(-ret));
04eff7d5 5313 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
72e7fb3f
YP
5314 enum nl80211_iftype old_mode = drv->nlmode;
5315
ad1e68e6
JM
5316 /*
5317 * mac80211 does not allow scan requests in AP mode, so
5318 * try to do this in station mode.
5319 */
b1f625e0
EP
5320 if (wpa_driver_nl80211_set_mode(
5321 bss, NL80211_IFTYPE_STATION))
ad1e68e6
JM
5322 goto nla_put_failure;
5323
085b29f1 5324 if (wpa_driver_nl80211_scan(bss, params)) {
b1f625e0 5325 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
ad1e68e6
JM
5326 goto nla_put_failure;
5327 }
5328
5329 /* Restore AP mode when processing scan results */
72e7fb3f 5330 drv->ap_scan_as_station = old_mode;
ad1e68e6
JM
5331 ret = 0;
5332 } else
5333 goto nla_put_failure;
3f5285e8
JM
5334 }
5335
a771c07d 5336 drv->scan_state = SCAN_REQUESTED;
3f5285e8
JM
5337 /* Not all drivers generate "scan completed" wireless event, so try to
5338 * read results after a timeout. */
0e75527f 5339 timeout = 10;
3f5285e8
JM
5340 if (drv->scan_complete_events) {
5341 /*
d173df52
JM
5342 * The driver seems to deliver events to notify when scan is
5343 * complete, so use longer timeout to avoid race conditions
5344 * with scanning and following association request.
3f5285e8
JM
5345 */
5346 timeout = 30;
5347 }
5348 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5349 "seconds", ret, timeout);
5350 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
0e75527f
JM
5351 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5352 drv, drv->ctx);
3f5285e8 5353
0e75527f 5354nla_put_failure:
0e75527f 5355 nlmsg_free(msg);
3f5285e8
JM
5356 return ret;
5357}
5358
5359
d21c63b9
LC
5360/**
5361 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5362 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5363 * @params: Scan parameters
5364 * @interval: Interval between scan cycles in milliseconds
5365 * Returns: 0 on success, -1 on failure or if not supported
5366 */
5367static int wpa_driver_nl80211_sched_scan(void *priv,
5368 struct wpa_driver_scan_params *params,
5369 u32 interval)
5370{
5371 struct i802_bss *bss = priv;
5372 struct wpa_driver_nl80211_data *drv = bss->drv;
6afbc3d6 5373 int ret = -1;
95ac3bf4 5374 struct nl_msg *msg;
d21c63b9
LC
5375 size_t i;
5376
565110cd
JM
5377 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5378
216eede8
DS
5379#ifdef ANDROID
5380 if (!drv->capa.sched_scan_supported)
5381 return android_pno_start(bss, params);
5382#endif /* ANDROID */
5383
d3aaef80
DS
5384 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5385 bss->wdev_id_set ? &bss->wdev_id : NULL);
6afbc3d6
JM
5386 if (!msg)
5387 goto nla_put_failure;
d21c63b9 5388
d21c63b9
LC
5389 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5390
bf8d6d24
TP
5391 if ((drv->num_filter_ssids &&
5392 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5393 params->filter_rssi) {
8970bae8
JB
5394 struct nlattr *match_sets;
5395 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
6afbc3d6
JM
5396 if (match_sets == NULL)
5397 goto nla_put_failure;
bd525934
LC
5398
5399 for (i = 0; i < drv->num_filter_ssids; i++) {
8970bae8 5400 struct nlattr *match_set_ssid;
bd525934
LC
5401 wpa_hexdump_ascii(MSG_MSGDUMP,
5402 "nl80211: Sched scan filter SSID",
5403 drv->filter_ssids[i].ssid,
5404 drv->filter_ssids[i].ssid_len);
5405
8970bae8 5406 match_set_ssid = nla_nest_start(msg, i + 1);
6afbc3d6
JM
5407 if (match_set_ssid == NULL)
5408 goto nla_put_failure;
8970bae8 5409 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
bd525934
LC
5410 drv->filter_ssids[i].ssid_len,
5411 drv->filter_ssids[i].ssid);
ff5e1d14
JB
5412 if (params->filter_rssi)
5413 NLA_PUT_U32(msg,
5414 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5415 params->filter_rssi);
bd525934 5416
adc96dc2 5417 nla_nest_end(msg, match_set_ssid);
bd525934
LC
5418 }
5419
ff5e1d14
JB
5420 /*
5421 * Due to backward compatibility code, newer kernels treat this
5422 * matchset (with only an RSSI filter) as the default for all
5423 * other matchsets, unless it's the only one, in which case the
5424 * matchset will actually allow all SSIDs above the RSSI.
5425 */
bf8d6d24 5426 if (params->filter_rssi) {
8970bae8
JB
5427 struct nlattr *match_set_rssi;
5428 match_set_rssi = nla_nest_start(msg, 0);
6afbc3d6
JM
5429 if (match_set_rssi == NULL)
5430 goto nla_put_failure;
8970bae8 5431 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
bf8d6d24
TP
5432 params->filter_rssi);
5433 wpa_printf(MSG_MSGDUMP,
5434 "nl80211: Sched scan RSSI filter %d dBm",
5435 params->filter_rssi);
8970bae8 5436 nla_nest_end(msg, match_set_rssi);
bf8d6d24
TP
5437 }
5438
8970bae8 5439 nla_nest_end(msg, match_sets);
bd525934
LC
5440 }
5441
d21c63b9
LC
5442 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5443
5444 /* TODO: if we get an error here, we should fall back to normal scan */
5445
5446 msg = NULL;
5447 if (ret) {
5448 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5449 "ret=%d (%s)", ret, strerror(-ret));
5450 goto nla_put_failure;
5451 }
5452
5453 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5454 "scan interval %d msec", ret, interval);
5455
5456nla_put_failure:
d21c63b9 5457 nlmsg_free(msg);
d21c63b9
LC
5458 return ret;
5459}
5460
5461
5462/**
5463 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5464 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5465 * Returns: 0 on success, -1 on failure or if not supported
5466 */
5467static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5468{
5469 struct i802_bss *bss = priv;
5470 struct wpa_driver_nl80211_data *drv = bss->drv;
5471 int ret = 0;
5472 struct nl_msg *msg;
5473
216eede8
DS
5474#ifdef ANDROID
5475 if (!drv->capa.sched_scan_supported)
5476 return android_pno_stop(bss);
5477#endif /* ANDROID */
5478
d21c63b9
LC
5479 msg = nlmsg_alloc();
5480 if (!msg)
5481 return -1;
5482
9fb04070 5483 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
d21c63b9
LC
5484
5485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5486
5487 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5488 msg = NULL;
5489 if (ret) {
5490 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5491 "ret=%d (%s)", ret, strerror(-ret));
5492 goto nla_put_failure;
5493 }
5494
5495 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5496
5497nla_put_failure:
5498 nlmsg_free(msg);
5499 return ret;
5500}
5501
5502
3812464c
JM
5503static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5504{
5505 const u8 *end, *pos;
5506
5507 if (ies == NULL)
5508 return NULL;
5509
5510 pos = ies;
5511 end = ies + ies_len;
5512
5513 while (pos + 1 < end) {
5514 if (pos + 2 + pos[1] > end)
5515 break;
5516 if (pos[0] == ie)
5517 return pos;
5518 pos += 2 + pos[1];
5519 }
5520
5521 return NULL;
5522}
5523
5524
5525static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5526 const u8 *ie, size_t ie_len)
5527{
5528 const u8 *ssid;
5529 size_t i;
5530
5531 if (drv->filter_ssids == NULL)
5532 return 0;
5533
5534 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5535 if (ssid == NULL)
5536 return 1;
5537
5538 for (i = 0; i < drv->num_filter_ssids; i++) {
5539 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5540 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5541 0)
5542 return 0;
5543 }
5544
5545 return 1;
5546}
5547
5548
b3db1e1c 5549static int bss_info_handler(struct nl_msg *msg, void *arg)
3f5285e8 5550{
b3db1e1c
JM
5551 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5552 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5553 struct nlattr *bss[NL80211_BSS_MAX + 1];
5554 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5555 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5556 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5557 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5558 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5559 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5560 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5561 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5562 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
e6b8efeb 5563 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
b3ad11bb 5564 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
8c090654 5565 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
b3db1e1c 5566 };
3812464c
JM
5567 struct nl80211_bss_info_arg *_arg = arg;
5568 struct wpa_scan_results *res = _arg->res;
3f5285e8
JM
5569 struct wpa_scan_res **tmp;
5570 struct wpa_scan_res *r;
8c090654
JM
5571 const u8 *ie, *beacon_ie;
5572 size_t ie_len, beacon_ie_len;
5573 u8 *pos;
46957a9b 5574 size_t i;
3f5285e8 5575
b3db1e1c
JM
5576 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5577 genlmsg_attrlen(gnlh, 0), NULL);
5578 if (!tb[NL80211_ATTR_BSS])
5579 return NL_SKIP;
5580 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5581 bss_policy))
5582 return NL_SKIP;
f5a8d422
JM
5583 if (bss[NL80211_BSS_STATUS]) {
5584 enum nl80211_bss_status status;
5585 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5586 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5587 bss[NL80211_BSS_FREQUENCY]) {
5588 _arg->assoc_freq =
5589 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5590 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5591 _arg->assoc_freq);
5592 }
c7caac56
JM
5593 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
5594 bss[NL80211_BSS_FREQUENCY]) {
5595 _arg->ibss_freq =
5596 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5597 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
5598 _arg->ibss_freq);
5599 }
20f5a4c2
JM
5600 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5601 bss[NL80211_BSS_BSSID]) {
5602 os_memcpy(_arg->assoc_bssid,
5603 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5604 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5605 MACSTR, MAC2STR(_arg->assoc_bssid));
5606 }
f5a8d422
JM
5607 }
5608 if (!res)
5609 return NL_SKIP;
b3db1e1c
JM
5610 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5611 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5612 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5613 } else {
5614 ie = NULL;
5615 ie_len = 0;
5616 }
8c090654
JM
5617 if (bss[NL80211_BSS_BEACON_IES]) {
5618 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5619 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5620 } else {
5621 beacon_ie = NULL;
5622 beacon_ie_len = 0;
5623 }
3f5285e8 5624
3812464c
JM
5625 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5626 ie ? ie_len : beacon_ie_len))
5627 return NL_SKIP;
5628
8c090654 5629 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3f5285e8 5630 if (r == NULL)
b3db1e1c
JM
5631 return NL_SKIP;
5632 if (bss[NL80211_BSS_BSSID])
5633 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5634 ETH_ALEN);
5635 if (bss[NL80211_BSS_FREQUENCY])
5636 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5637 if (bss[NL80211_BSS_BEACON_INTERVAL])
5638 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5639 if (bss[NL80211_BSS_CAPABILITY])
5640 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
7c2849d2
JM
5641 r->flags |= WPA_SCAN_NOISE_INVALID;
5642 if (bss[NL80211_BSS_SIGNAL_MBM]) {
b3db1e1c 5643 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
7c2849d2
JM
5644 r->level /= 100; /* mBm to dBm */
5645 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5646 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5647 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
98ac6763 5648 r->flags |= WPA_SCAN_QUAL_INVALID;
7c2849d2
JM
5649 } else
5650 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
b3db1e1c
JM
5651 if (bss[NL80211_BSS_TSF])
5652 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
b3ad11bb
JM
5653 if (bss[NL80211_BSS_SEEN_MS_AGO])
5654 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
b3db1e1c 5655 r->ie_len = ie_len;
8c090654
JM
5656 pos = (u8 *) (r + 1);
5657 if (ie) {
5658 os_memcpy(pos, ie, ie_len);
5659 pos += ie_len;
5660 }
5661 r->beacon_ie_len = beacon_ie_len;
5662 if (beacon_ie)
5663 os_memcpy(pos, beacon_ie, beacon_ie_len);
3f5285e8 5664
e6b8efeb
JM
5665 if (bss[NL80211_BSS_STATUS]) {
5666 enum nl80211_bss_status status;
5667 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5668 switch (status) {
5669 case NL80211_BSS_STATUS_AUTHENTICATED:
5670 r->flags |= WPA_SCAN_AUTHENTICATED;
5671 break;
5672 case NL80211_BSS_STATUS_ASSOCIATED:
5673 r->flags |= WPA_SCAN_ASSOCIATED;
5674 break;
5675 default:
5676 break;
5677 }
5678 }
5679
46957a9b
JM
5680 /*
5681 * cfg80211 maintains separate BSS table entries for APs if the same
5682 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5683 * not use frequency as a separate key in the BSS table, so filter out
5684 * duplicated entries. Prefer associated BSS entry in such a case in
4ea6a471
JM
5685 * order to get the correct frequency into the BSS table. Similarly,
5686 * prefer newer entries over older.
46957a9b
JM
5687 */
5688 for (i = 0; i < res->num; i++) {
5689 const u8 *s1, *s2;
5690 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5691 continue;
5692
5693 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5694 res->res[i]->ie_len, WLAN_EID_SSID);
5695 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5696 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5697 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5698 continue;
5699
5700 /* Same BSSID,SSID was already included in scan results */
5701 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5702 "for " MACSTR, MAC2STR(r->bssid));
5703
4ea6a471
JM
5704 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5705 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5706 r->age < res->res[i]->age) {
46957a9b
JM
5707 os_free(res->res[i]);
5708 res->res[i] = r;
5709 } else
5710 os_free(r);
5711 return NL_SKIP;
5712 }
5713
067ffa26
JM
5714 tmp = os_realloc_array(res->res, res->num + 1,
5715 sizeof(struct wpa_scan_res *));
3f5285e8
JM
5716 if (tmp == NULL) {
5717 os_free(r);
b3db1e1c 5718 return NL_SKIP;
3f5285e8
JM
5719 }
5720 tmp[res->num++] = r;
5721 res->res = tmp;
b3db1e1c
JM
5722
5723 return NL_SKIP;
3f5285e8 5724}
b3db1e1c 5725
3f5285e8 5726
d72aad94
JM
5727static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5728 const u8 *addr)
5729{
5730 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5731 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
582507be 5732 "mismatch (" MACSTR ")", MAC2STR(addr));
d72aad94
JM
5733 wpa_driver_nl80211_mlme(drv, addr,
5734 NL80211_CMD_DEAUTHENTICATE,
77339912 5735 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
d72aad94
JM
5736 }
5737}
5738
5739
e6b8efeb
JM
5740static void wpa_driver_nl80211_check_bss_status(
5741 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5742{
5743 size_t i;
5744
5745 for (i = 0; i < res->num; i++) {
5746 struct wpa_scan_res *r = res->res[i];
5747 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5748 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5749 "indicates BSS status with " MACSTR
5750 " as authenticated",
5751 MAC2STR(r->bssid));
b1f625e0 5752 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
5753 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5754 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5755 0) {
5756 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5757 " in local state (auth=" MACSTR
5758 " assoc=" MACSTR ")",
5759 MAC2STR(drv->auth_bssid),
5760 MAC2STR(drv->bssid));
582507be 5761 clear_state_mismatch(drv, r->bssid);
e6b8efeb
JM
5762 }
5763 }
5764
5765 if (r->flags & WPA_SCAN_ASSOCIATED) {
5766 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5767 "indicate BSS status with " MACSTR
5768 " as associated",
5769 MAC2STR(r->bssid));
b1f625e0 5770 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
5771 !drv->associated) {
5772 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5773 "(not associated) does not match "
5774 "with BSS state");
d72aad94 5775 clear_state_mismatch(drv, r->bssid);
b1f625e0 5776 } else if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
5777 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5778 0) {
5779 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5780 "(associated with " MACSTR ") does "
5781 "not match with BSS state",
d72aad94
JM
5782 MAC2STR(drv->bssid));
5783 clear_state_mismatch(drv, r->bssid);
5784 clear_state_mismatch(drv, drv->bssid);
e6b8efeb
JM
5785 }
5786 }
5787 }
5788}
5789
5790
7e5ba1b9 5791static struct wpa_scan_results *
8856462d 5792nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3f5285e8 5793{
b3db1e1c 5794 struct nl_msg *msg;
3f5285e8 5795 struct wpa_scan_results *res;
b3db1e1c 5796 int ret;
3812464c 5797 struct nl80211_bss_info_arg arg;
3f5285e8
JM
5798
5799 res = os_zalloc(sizeof(*res));
b3db1e1c 5800 if (res == NULL)
8e2c104f 5801 return NULL;
b3db1e1c
JM
5802 msg = nlmsg_alloc();
5803 if (!msg)
5804 goto nla_put_failure;
3f5285e8 5805
9fb04070 5806 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
834ee56f 5807 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
fdc554b8 5808 goto nla_put_failure;
3f5285e8 5809
3812464c
JM
5810 arg.drv = drv;
5811 arg.res = res;
5812 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
b3db1e1c
JM
5813 msg = NULL;
5814 if (ret == 0) {
577db0ae
GM
5815 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5816 "BSSes)", (unsigned long) res->num);
5817 nl80211_get_noise_for_scan_results(drv, res);
b3db1e1c 5818 return res;
3f5285e8 5819 }
b3db1e1c
JM
5820 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5821 "(%s)", ret, strerror(-ret));
5822nla_put_failure:
5823 nlmsg_free(msg);
5824 wpa_scan_results_free(res);
5825 return NULL;
3f5285e8
JM
5826}
5827
5828
8856462d
JM
5829/**
5830 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5831 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5832 * Returns: Scan results on success, -1 on failure
5833 */
5834static struct wpa_scan_results *
5835wpa_driver_nl80211_get_scan_results(void *priv)
5836{
a2e40bb6
FF
5837 struct i802_bss *bss = priv;
5838 struct wpa_driver_nl80211_data *drv = bss->drv;
8856462d
JM
5839 struct wpa_scan_results *res;
5840
5841 res = nl80211_get_scan_results(drv);
5842 if (res)
5843 wpa_driver_nl80211_check_bss_status(drv, res);
5844 return res;
5845}
5846
5847
5848static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5849{
5850 struct wpa_scan_results *res;
5851 size_t i;
5852
5853 res = nl80211_get_scan_results(drv);
5854 if (res == NULL) {
5855 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5856 return;
5857 }
5858
5859 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5860 for (i = 0; i < res->num; i++) {
5861 struct wpa_scan_res *r = res->res[i];
5862 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5863 (int) i, (int) res->num, MAC2STR(r->bssid),
5864 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5865 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5866 }
5867
5868 wpa_scan_results_free(res);
5869}
5870
5871
de4ed4a8
JM
5872static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5873{
5874 switch (alg) {
5875 case WPA_ALG_WEP:
5876 if (key_len == 5)
5877 return WLAN_CIPHER_SUITE_WEP40;
5878 return WLAN_CIPHER_SUITE_WEP104;
5879 case WPA_ALG_TKIP:
5880 return WLAN_CIPHER_SUITE_TKIP;
5881 case WPA_ALG_CCMP:
5882 return WLAN_CIPHER_SUITE_CCMP;
5883 case WPA_ALG_GCMP:
5884 return WLAN_CIPHER_SUITE_GCMP;
5885 case WPA_ALG_CCMP_256:
5886 return WLAN_CIPHER_SUITE_CCMP_256;
5887 case WPA_ALG_GCMP_256:
5888 return WLAN_CIPHER_SUITE_GCMP_256;
5889 case WPA_ALG_IGTK:
5890 return WLAN_CIPHER_SUITE_AES_CMAC;
5891 case WPA_ALG_BIP_GMAC_128:
5892 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5893 case WPA_ALG_BIP_GMAC_256:
5894 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5895 case WPA_ALG_BIP_CMAC_256:
5896 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5897 case WPA_ALG_SMS4:
5898 return WLAN_CIPHER_SUITE_SMS4;
5899 case WPA_ALG_KRK:
5900 return WLAN_CIPHER_SUITE_KRK;
5901 case WPA_ALG_NONE:
5902 case WPA_ALG_PMK:
5903 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5904 alg);
5905 return 0;
5906 }
5907
5908 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5909 alg);
5910 return 0;
5911}
5912
5913
5914static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5915{
5916 switch (cipher) {
5917 case WPA_CIPHER_CCMP_256:
5918 return WLAN_CIPHER_SUITE_CCMP_256;
5919 case WPA_CIPHER_GCMP_256:
5920 return WLAN_CIPHER_SUITE_GCMP_256;
5921 case WPA_CIPHER_CCMP:
5922 return WLAN_CIPHER_SUITE_CCMP;
5923 case WPA_CIPHER_GCMP:
5924 return WLAN_CIPHER_SUITE_GCMP;
5925 case WPA_CIPHER_TKIP:
5926 return WLAN_CIPHER_SUITE_TKIP;
5927 case WPA_CIPHER_WEP104:
5928 return WLAN_CIPHER_SUITE_WEP104;
5929 case WPA_CIPHER_WEP40:
5930 return WLAN_CIPHER_SUITE_WEP40;
ae6f9272
JM
5931 case WPA_CIPHER_GTK_NOT_USED:
5932 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
de4ed4a8
JM
5933 }
5934
5935 return 0;
5936}
5937
5938
5939static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5940 int max_suites)
5941{
5942 int num_suites = 0;
5943
5944 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5945 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5946 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5947 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5948 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5949 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5950 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5951 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5952 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5953 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5954 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5955 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5956 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5957 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5958
5959 return num_suites;
5960}
5961
5962
b41f2684
CL
5963static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
5964 const u8 *key, size_t key_len)
5965{
5966 struct nl_msg *msg;
5967 int ret = 0;
5968
5969 if (!drv->key_mgmt_set_key_vendor_cmd_avail)
5970 return 0;
5971
5972 msg = nlmsg_alloc();
5973 if (!msg)
5974 return -1;
5975
5976 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
5977 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5978 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
5979 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
5980 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY);
5981 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, key_len, key);
5982 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5983 msg = NULL;
5984 if (ret) {
5985 wpa_printf(MSG_DEBUG,
5986 "nl80211: Key management set key failed: ret=%d (%s)",
5987 ret, strerror(-ret));
5988 }
5989
5990nla_put_failure:
5991 nlmsg_free(msg);
5992 return ret;
5993}
5994
5995
9ebce9c5 5996static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
71934751
JM
5997 enum wpa_alg alg, const u8 *addr,
5998 int key_idx, int set_tx,
642187d6
JM
5999 const u8 *seq, size_t seq_len,
6000 const u8 *key, size_t key_len)
3f5285e8 6001{
a2e40bb6 6002 struct wpa_driver_nl80211_data *drv = bss->drv;
e472e1b4 6003 int ifindex;
3f5285e8 6004 struct nl_msg *msg;
1ad1cdc2 6005 int ret;
dc01de8a 6006 int tdls = 0;
3f5285e8 6007
e472e1b4
AS
6008 /* Ignore for P2P Device */
6009 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
6010 return 0;
6011
6012 ifindex = if_nametoindex(ifname);
8393e1a0 6013 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
1ad1cdc2 6014 "set_tx=%d seq_len=%lu key_len=%lu",
8393e1a0 6015 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
3f5285e8 6016 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185 6017#ifdef CONFIG_TDLS
dc01de8a 6018 if (key_idx == -1) {
8c66e185 6019 key_idx = 0;
dc01de8a
JM
6020 tdls = 1;
6021 }
8c66e185 6022#endif /* CONFIG_TDLS */
3f5285e8 6023
b41f2684
CL
6024 if (alg == WPA_ALG_PMK && drv->key_mgmt_set_key_vendor_cmd_avail) {
6025 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
6026 __func__);
6027 ret = issue_key_mgmt_set_key(drv, key, key_len);
6028 return ret;
6029 }
6030
3f5285e8 6031 msg = nlmsg_alloc();
1ad1cdc2
JM
6032 if (!msg)
6033 return -ENOMEM;
3f5285e8
JM
6034
6035 if (alg == WPA_ALG_NONE) {
9fb04070 6036 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3f5285e8 6037 } else {
9fb04070 6038 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3f5285e8 6039 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
e6ef73f1 6040 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
de4ed4a8
JM
6041 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
6042 wpa_alg_to_cipher_suite(alg, key_len));
3f5285e8
JM
6043 }
6044
e6ef73f1 6045 if (seq && seq_len) {
1ad1cdc2 6046 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
e6ef73f1
JM
6047 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
6048 }
1ad1cdc2 6049
0382097e 6050 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8
JM
6051 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
6052 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
89c38e32
JM
6053
6054 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
6055 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
6056 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
6057 NL80211_KEYTYPE_GROUP);
6058 }
60ea8187 6059 } else if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
6060 struct nlattr *types;
6061
60ea8187 6062 wpa_printf(MSG_DEBUG, " broadcast key");
8970bae8
JB
6063
6064 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
6065 if (!types)
6066 goto nla_put_failure;
8970bae8
JB
6067 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
6068 nla_nest_end(msg, types);
3f5285e8
JM
6069 }
6070 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1ad1cdc2 6071 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3f5285e8 6072
1ad1cdc2 6073 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
15664ad0 6074 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
6075 ret = 0;
6076 if (ret)
6077 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
6078 ret, strerror(-ret));
3f5285e8 6079
1ad1cdc2
JM
6080 /*
6081 * If we failed or don't need to set the default TX key (below),
6082 * we're done here.
6083 */
dc01de8a 6084 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
1ad1cdc2 6085 return ret;
b1f625e0 6086 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 6087 !is_broadcast_ether_addr(addr))
de12717a 6088 return ret;
3f5285e8 6089
1ad1cdc2
JM
6090 msg = nlmsg_alloc();
6091 if (!msg)
6092 return -ENOMEM;
3f5285e8 6093
9fb04070 6094 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
1ad1cdc2
JM
6095 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
6096 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6097 if (alg == WPA_ALG_IGTK)
6098 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
6099 else
6100 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
60ea8187 6101 if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
6102 struct nlattr *types;
6103
6104 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
6105 if (!types)
6106 goto nla_put_failure;
8970bae8
JB
6107 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
6108 nla_nest_end(msg, types);
60ea8187 6109 } else if (addr) {
8970bae8
JB
6110 struct nlattr *types;
6111
6112 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
6113 if (!types)
6114 goto nla_put_failure;
8970bae8
JB
6115 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
6116 nla_nest_end(msg, types);
60ea8187 6117 }
3f5285e8 6118
1ad1cdc2
JM
6119 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6120 if (ret == -ENOENT)
6121 ret = 0;
6122 if (ret)
6123 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
6124 "err=%d %s)", ret, strerror(-ret));
6125 return ret;
3f5285e8
JM
6126
6127nla_put_failure:
5883168a 6128 nlmsg_free(msg);
6241fcb1 6129 return -ENOBUFS;
3f5285e8
JM
6130}
6131
6132
71934751 6133static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
6134 int key_idx, int defkey,
6135 const u8 *seq, size_t seq_len,
6136 const u8 *key, size_t key_len)
6137{
6138 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
6139 if (!key_attr)
6140 return -1;
6141
6142 if (defkey && alg == WPA_ALG_IGTK)
6143 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
6144 else if (defkey)
6145 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
6146
6147 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
6148
de4ed4a8
JM
6149 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6150 wpa_alg_to_cipher_suite(alg, key_len));
0194fedb
JB
6151
6152 if (seq && seq_len)
6153 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
6154
6155 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
6156
6157 nla_nest_end(msg, key_attr);
6158
6159 return 0;
6160 nla_put_failure:
6161 return -1;
6162}
6163
c811d5bc 6164
cfaab580
ZY
6165static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
6166 struct nl_msg *msg)
6167{
6168 int i, privacy = 0;
6169 struct nlattr *nl_keys, *nl_key;
6170
6171 for (i = 0; i < 4; i++) {
6172 if (!params->wep_key[i])
6173 continue;
6174 privacy = 1;
6175 break;
6176 }
ce04af5a
JM
6177 if (params->wps == WPS_MODE_PRIVACY)
6178 privacy = 1;
6179 if (params->pairwise_suite &&
6180 params->pairwise_suite != WPA_CIPHER_NONE)
6181 privacy = 1;
6182
cfaab580
ZY
6183 if (!privacy)
6184 return 0;
6185
6186 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
6187
6188 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
6189 if (!nl_keys)
6190 goto nla_put_failure;
6191
6192 for (i = 0; i < 4; i++) {
6193 if (!params->wep_key[i])
6194 continue;
6195
6196 nl_key = nla_nest_start(msg, i);
6197 if (!nl_key)
6198 goto nla_put_failure;
6199
6200 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
6201 params->wep_key[i]);
6202 if (params->wep_key_len[i] == 5)
6203 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6204 WLAN_CIPHER_SUITE_WEP40);
6205 else
6206 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6207 WLAN_CIPHER_SUITE_WEP104);
6208
6209 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
6210
6211 if (i == params->wep_tx_keyidx)
6212 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
6213
6214 nla_nest_end(msg, nl_key);
6215 }
6216 nla_nest_end(msg, nl_keys);
6217
6218 return 0;
6219
6220nla_put_failure:
6221 return -ENOBUFS;
6222}
6223
6224
c2a04078 6225static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
6226 const u8 *addr, int cmd, u16 reason_code,
6227 int local_state_change)
c2a04078
JM
6228{
6229 int ret = -1;
6230 struct nl_msg *msg;
6231
6232 msg = nlmsg_alloc();
6233 if (!msg)
6234 return -1;
6235
9fb04070 6236 nl80211_cmd(drv, msg, 0, cmd);
c2a04078
JM
6237
6238 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6239 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
817762d9
MI
6240 if (addr)
6241 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
77339912
JM
6242 if (local_state_change)
6243 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
c2a04078
JM
6244
6245 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6246 msg = NULL;
6247 if (ret) {
3b7ea880
BG
6248 wpa_dbg(drv->ctx, MSG_DEBUG,
6249 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
6250 reason_code, ret, strerror(-ret));
c2a04078
JM
6251 goto nla_put_failure;
6252 }
6253 ret = 0;
6254
6255nla_put_failure:
6256 nlmsg_free(msg);
6257 return ret;
6258}
3f5285e8
JM
6259
6260
cfaab580 6261static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
817762d9 6262 int reason_code)
cfaab580 6263{
3f53c006
JJ
6264 int ret;
6265
817762d9 6266 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
add9b7a4 6267 nl80211_mark_disconnected(drv);
817762d9 6268 /* Disconnect command doesn't need BSSID - it uses cached value */
3f53c006
JJ
6269 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
6270 reason_code, 0);
6271 /*
6272 * For locally generated disconnect, supplicant already generates a
6273 * DEAUTH event, so ignore the event from NL80211.
6274 */
6275 drv->ignore_next_local_disconnect = ret == 0;
6276
6277 return ret;
cfaab580
ZY
6278}
6279
6280
9ebce9c5
JM
6281static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
6282 const u8 *addr, int reason_code)
3f5285e8 6283{
a2e40bb6 6284 struct wpa_driver_nl80211_data *drv = bss->drv;
d6a36f39 6285 int ret;
9392c9be
AS
6286
6287 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
6288 nl80211_mark_disconnected(drv);
6289 return nl80211_leave_ibss(drv);
6290 }
cfaab580 6291 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
817762d9 6292 return wpa_driver_nl80211_disconnect(drv, reason_code);
2e75a2b3
JM
6293 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
6294 __func__, MAC2STR(addr), reason_code);
add9b7a4 6295 nl80211_mark_disconnected(drv);
d6a36f39
JM
6296 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
6297 reason_code, 0);
6298 /*
6299 * For locally generated deauthenticate, supplicant already generates a
6300 * DEAUTH event, so ignore the event from NL80211.
6301 */
6302 drv->ignore_next_local_deauth = ret == 0;
6303 return ret;
3f5285e8
JM
6304}
6305
6306
536fd62d
JM
6307static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6308 struct wpa_driver_auth_params *params)
6309{
6310 int i;
6311
6312 drv->auth_freq = params->freq;
6313 drv->auth_alg = params->auth_alg;
6314 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6315 drv->auth_local_state_change = params->local_state_change;
6316 drv->auth_p2p = params->p2p;
6317
6318 if (params->bssid)
6319 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6320 else
6321 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6322
6323 if (params->ssid) {
6324 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6325 drv->auth_ssid_len = params->ssid_len;
6326 } else
6327 drv->auth_ssid_len = 0;
6328
6329
6330 os_free(drv->auth_ie);
6331 drv->auth_ie = NULL;
6332 drv->auth_ie_len = 0;
6333 if (params->ie) {
6334 drv->auth_ie = os_malloc(params->ie_len);
6335 if (drv->auth_ie) {
6336 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6337 drv->auth_ie_len = params->ie_len;
6338 }
6339 }
6340
6341 for (i = 0; i < 4; i++) {
6342 if (params->wep_key[i] && params->wep_key_len[i] &&
6343 params->wep_key_len[i] <= 16) {
6344 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6345 params->wep_key_len[i]);
6346 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6347 } else
6348 drv->auth_wep_key_len[i] = 0;
6349 }
6350}
6351
6352
c2a04078 6353static int wpa_driver_nl80211_authenticate(
9ebce9c5 6354 struct i802_bss *bss, struct wpa_driver_auth_params *params)
c2a04078 6355{
a2e40bb6 6356 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 6357 int ret = -1, i;
c2a04078
JM
6358 struct nl_msg *msg;
6359 enum nl80211_auth_type type;
2f4f73b1 6360 enum nl80211_iftype nlmode;
6d6f4bb8 6361 int count = 0;
536fd62d
JM
6362 int is_retry;
6363
6364 is_retry = drv->retry_auth;
6365 drv->retry_auth = 0;
e8d70a73 6366 drv->ignore_deauth_event = 0;
c2a04078 6367
add9b7a4 6368 nl80211_mark_disconnected(drv);
e6b8efeb 6369 os_memset(drv->auth_bssid, 0, ETH_ALEN);
add9b7a4
JM
6370 if (params->bssid)
6371 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6372 else
6373 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
af473088 6374 /* FIX: IBSS mode */
2f4f73b1
EP
6375 nlmode = params->p2p ?
6376 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6377 if (drv->nlmode != nlmode &&
9ebce9c5 6378 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
4a867032
JM
6379 return -1;
6380
6d6f4bb8 6381retry:
c2a04078
JM
6382 msg = nlmsg_alloc();
6383 if (!msg)
6384 return -1;
6385
6386 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6387 drv->ifindex);
a0b2f99b 6388
9fb04070 6389 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
0194fedb 6390
a0b2f99b
JM
6391 for (i = 0; i < 4; i++) {
6392 if (!params->wep_key[i])
6393 continue;
9ebce9c5 6394 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2ea2fcc7 6395 NULL, i,
a0b2f99b
JM
6396 i == params->wep_tx_keyidx, NULL, 0,
6397 params->wep_key[i],
6398 params->wep_key_len[i]);
0194fedb
JB
6399 if (params->wep_tx_keyidx != i)
6400 continue;
6401 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6402 params->wep_key[i], params->wep_key_len[i])) {
6403 nlmsg_free(msg);
6404 return -1;
6405 }
a0b2f99b
JM
6406 }
6407
c2a04078
JM
6408 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6409 if (params->bssid) {
6410 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6411 MAC2STR(params->bssid));
6412 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6413 }
6414 if (params->freq) {
6415 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6416 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6417 }
6418 if (params->ssid) {
6419 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6420 params->ssid, params->ssid_len);
6421 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6422 params->ssid);
6423 }
6424 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6425 if (params->ie)
6426 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
569fed90
JM
6427 if (params->sae_data) {
6428 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6429 params->sae_data_len);
6430 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6431 params->sae_data);
6432 }
abd9fafa 6433 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
c2a04078 6434 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 6435 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
c2a04078 6436 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 6437 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
c2a04078 6438 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 6439 else if (params->auth_alg & WPA_AUTH_ALG_FT)
c2a04078 6440 type = NL80211_AUTHTYPE_FT;
569fed90
JM
6441 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6442 type = NL80211_AUTHTYPE_SAE;
c2a04078
JM
6443 else
6444 goto nla_put_failure;
6445 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6446 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
77339912
JM
6447 if (params->local_state_change) {
6448 wpa_printf(MSG_DEBUG, " * Local state change only");
6449 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6450 }
c2a04078
JM
6451
6452 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6453 msg = NULL;
6454 if (ret) {
3b7ea880
BG
6455 wpa_dbg(drv->ctx, MSG_DEBUG,
6456 "nl80211: MLME command failed (auth): ret=%d (%s)",
6457 ret, strerror(-ret));
6d6f4bb8 6458 count++;
77339912
JM
6459 if (ret == -EALREADY && count == 1 && params->bssid &&
6460 !params->local_state_change) {
6d6f4bb8
JM
6461 /*
6462 * mac80211 does not currently accept new
6463 * authentication if we are already authenticated. As a
6464 * workaround, force deauthentication and try again.
6465 */
6466 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6467 "after forced deauthentication");
e8d70a73 6468 drv->ignore_deauth_event = 1;
6d6f4bb8 6469 wpa_driver_nl80211_deauthenticate(
5205c4f9 6470 bss, params->bssid,
6d6f4bb8
JM
6471 WLAN_REASON_PREV_AUTH_NOT_VALID);
6472 nlmsg_free(msg);
6473 goto retry;
6474 }
536fd62d
JM
6475
6476 if (ret == -ENOENT && params->freq && !is_retry) {
6477 /*
6478 * cfg80211 has likely expired the BSS entry even
6479 * though it was previously available in our internal
6480 * BSS table. To recover quickly, start a single
6481 * channel scan on the specified channel.
6482 */
6483 struct wpa_driver_scan_params scan;
6484 int freqs[2];
6485
6486 os_memset(&scan, 0, sizeof(scan));
6487 scan.num_ssids = 1;
6488 if (params->ssid) {
6489 scan.ssids[0].ssid = params->ssid;
6490 scan.ssids[0].ssid_len = params->ssid_len;
6491 }
6492 freqs[0] = params->freq;
6493 freqs[1] = 0;
6494 scan.freqs = freqs;
6495 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6496 "channel scan to refresh cfg80211 BSS "
6497 "entry");
6498 ret = wpa_driver_nl80211_scan(bss, &scan);
6499 if (ret == 0) {
6500 nl80211_copy_auth_params(drv, params);
6501 drv->scan_for_auth = 1;
6502 }
8c3ba078
JM
6503 } else if (is_retry) {
6504 /*
6505 * Need to indicate this with an event since the return
6506 * value from the retry is not delivered to core code.
6507 */
6508 union wpa_event_data event;
6509 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6510 "failed");
6511 os_memset(&event, 0, sizeof(event));
6512 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6513 ETH_ALEN);
6514 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6515 &event);
536fd62d
JM
6516 }
6517
c2a04078
JM
6518 goto nla_put_failure;
6519 }
6520 ret = 0;
6521 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6522 "successfully");
6523
6524nla_put_failure:
6525 nlmsg_free(msg);
6526 return ret;
6527}
6528
6529
536fd62d
JM
6530static int wpa_driver_nl80211_authenticate_retry(
6531 struct wpa_driver_nl80211_data *drv)
6532{
6533 struct wpa_driver_auth_params params;
834ee56f 6534 struct i802_bss *bss = drv->first_bss;
536fd62d
JM
6535 int i;
6536
6537 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6538
6539 os_memset(&params, 0, sizeof(params));
6540 params.freq = drv->auth_freq;
6541 params.auth_alg = drv->auth_alg;
6542 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6543 params.local_state_change = drv->auth_local_state_change;
6544 params.p2p = drv->auth_p2p;
6545
6546 if (!is_zero_ether_addr(drv->auth_bssid_))
6547 params.bssid = drv->auth_bssid_;
6548
6549 if (drv->auth_ssid_len) {
6550 params.ssid = drv->auth_ssid;
6551 params.ssid_len = drv->auth_ssid_len;
6552 }
6553
6554 params.ie = drv->auth_ie;
6555 params.ie_len = drv->auth_ie_len;
6556
6557 for (i = 0; i < 4; i++) {
6558 if (drv->auth_wep_key_len[i]) {
6559 params.wep_key[i] = drv->auth_wep_key[i];
6560 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6561 }
6562 }
6563
6564 drv->retry_auth = 1;
6565 return wpa_driver_nl80211_authenticate(bss, &params);
6566}
6567
6568
282d5590
JM
6569struct phy_info_arg {
6570 u16 *num_modes;
6571 struct hostapd_hw_modes *modes;
43245552 6572 int last_mode, last_chan_idx;
282d5590
JM
6573};
6574
e62a1d43
JM
6575static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6576 struct nlattr *ampdu_factor,
6577 struct nlattr *ampdu_density,
6578 struct nlattr *mcs_set)
6579{
6580 if (capa)
6581 mode->ht_capab = nla_get_u16(capa);
6582
6583 if (ampdu_factor)
6584 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
6585
6586 if (ampdu_density)
6587 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6588
6589 if (mcs_set && nla_len(mcs_set) >= 16) {
6590 u8 *mcs;
6591 mcs = nla_data(mcs_set);
6592 os_memcpy(mode->mcs_set, mcs, 16);
6593 }
6594}
6595
6596
6597static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6598 struct nlattr *capa,
6599 struct nlattr *mcs_set)
6600{
6601 if (capa)
6602 mode->vht_capab = nla_get_u32(capa);
6603
6604 if (mcs_set && nla_len(mcs_set) >= 8) {
6605 u8 *mcs;
6606 mcs = nla_data(mcs_set);
6607 os_memcpy(mode->vht_mcs_set, mcs, 8);
6608 }
6609}
6610
6611
214a77b0
JM
6612static void phy_info_freq(struct hostapd_hw_modes *mode,
6613 struct hostapd_channel_data *chan,
6614 struct nlattr *tb_freq[])
6615{
e864c0ae 6616 u8 channel;
214a77b0
JM
6617 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6618 chan->flag = 0;
bbbacbf2 6619 chan->dfs_cac_ms = 0;
e864c0ae
JM
6620 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6621 chan->chan = channel;
214a77b0
JM
6622
6623 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6624 chan->flag |= HOSTAPD_CHAN_DISABLED;
9fcd300d
JM
6625 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6626 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
214a77b0
JM
6627 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6628 chan->flag |= HOSTAPD_CHAN_RADAR;
6629
fc96522e
SW
6630 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6631 enum nl80211_dfs_state state =
6632 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6633
6634 switch (state) {
6635 case NL80211_DFS_USABLE:
6636 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6637 break;
6638 case NL80211_DFS_AVAILABLE:
6639 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6640 break;
6641 case NL80211_DFS_UNAVAILABLE:
6642 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6643 break;
6644 }
6645 }
bbbacbf2
JD
6646
6647 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6648 chan->dfs_cac_ms = nla_get_u32(
6649 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6650 }
214a77b0
JM
6651}
6652
6653
e62a1d43
JM
6654static int phy_info_freqs(struct phy_info_arg *phy_info,
6655 struct hostapd_hw_modes *mode, struct nlattr *tb)
282d5590 6656{
282d5590
JM
6657 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6658 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6659 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
9fcd300d 6660 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
282d5590
JM
6661 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6662 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
fc96522e 6663 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
282d5590 6664 };
e62a1d43
JM
6665 int new_channels = 0;
6666 struct hostapd_channel_data *channel;
6667 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
6668 struct nlattr *nl_freq;
6669 int rem_freq, idx;
6670
6671 if (tb == NULL)
6672 return NL_OK;
6673
6674 nla_for_each_nested(nl_freq, tb, rem_freq) {
6675 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6676 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6677 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6678 continue;
6679 new_channels++;
6680 }
6681
6682 channel = os_realloc_array(mode->channels,
6683 mode->num_channels + new_channels,
6684 sizeof(struct hostapd_channel_data));
6685 if (!channel)
6686 return NL_SKIP;
6687
6688 mode->channels = channel;
6689 mode->num_channels += new_channels;
6690
6691 idx = phy_info->last_chan_idx;
6692
6693 nla_for_each_nested(nl_freq, tb, rem_freq) {
6694 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6695 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6696 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6697 continue;
214a77b0 6698 phy_info_freq(mode, &mode->channels[idx], tb_freq);
e62a1d43
JM
6699 idx++;
6700 }
6701 phy_info->last_chan_idx = idx;
6702
6703 return NL_OK;
6704}
6705
6706
6707static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6708{
282d5590
JM
6709 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6710 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
3cfcad1b
JM
6711 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6712 { .type = NLA_FLAG },
282d5590 6713 };
e62a1d43 6714 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
282d5590 6715 struct nlattr *nl_rate;
e62a1d43
JM
6716 int rem_rate, idx;
6717
6718 if (tb == NULL)
6719 return NL_OK;
6720
6721 nla_for_each_nested(nl_rate, tb, rem_rate) {
6722 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6723 nla_data(nl_rate), nla_len(nl_rate),
6724 rate_policy);
6725 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6726 continue;
6727 mode->num_rates++;
6728 }
6729
6730 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6731 if (!mode->rates)
6732 return NL_SKIP;
6733
6734 idx = 0;
6735
6736 nla_for_each_nested(nl_rate, tb, rem_rate) {
6737 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6738 nla_data(nl_rate), nla_len(nl_rate),
6739 rate_policy);
6740 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6741 continue;
6742 mode->rates[idx] = nla_get_u32(
6743 tb_rate[NL80211_BITRATE_ATTR_RATE]);
e62a1d43
JM
6744 idx++;
6745 }
6746
6747 return NL_OK;
6748}
6749
6750
6751static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6752{
6753 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
282d5590 6754 struct hostapd_hw_modes *mode;
e62a1d43 6755 int ret;
282d5590 6756
3cfcad1b
JM
6757 if (phy_info->last_mode != nl_band->nla_type) {
6758 mode = os_realloc_array(phy_info->modes,
6759 *phy_info->num_modes + 1,
6760 sizeof(*mode));
6761 if (!mode)
6762 return NL_SKIP;
6763 phy_info->modes = mode;
6764
6765 mode = &phy_info->modes[*(phy_info->num_modes)];
6766 os_memset(mode, 0, sizeof(*mode));
6767 mode->mode = NUM_HOSTAPD_MODES;
c8ebeda4
MK
6768 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6769 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6770
6771 /*
6772 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6773 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6774 * possible streams as unsupported. This will be overridden if
6775 * driver advertises VHT support.
6776 */
6777 mode->vht_mcs_set[0] = 0xff;
6778 mode->vht_mcs_set[1] = 0xff;
6779 mode->vht_mcs_set[4] = 0xff;
6780 mode->vht_mcs_set[5] = 0xff;
6781
3cfcad1b
JM
6782 *(phy_info->num_modes) += 1;
6783 phy_info->last_mode = nl_band->nla_type;
6784 phy_info->last_chan_idx = 0;
6785 } else
6786 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
282d5590 6787
3cfcad1b
JM
6788 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6789 nla_len(nl_band), NULL);
282d5590 6790
e62a1d43
JM
6791 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6792 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6793 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6794 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6795 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6796 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6797 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6798 if (ret != NL_OK)
6799 return ret;
6800 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6801 if (ret != NL_OK)
6802 return ret;
282d5590 6803
3cfcad1b
JM
6804 return NL_OK;
6805}
6806
6807
6808static int phy_info_handler(struct nl_msg *msg, void *arg)
6809{
6810 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6811 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6812 struct phy_info_arg *phy_info = arg;
6813 struct nlattr *nl_band;
6814 int rem_band;
6815
6816 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6817 genlmsg_attrlen(gnlh, 0), NULL);
6818
6819 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6820 return NL_SKIP;
6821
6822 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6823 {
6824 int res = phy_info_band(phy_info, nl_band);
6825 if (res != NL_OK)
6826 return res;
6827 }
6828
282d5590
JM
6829 return NL_SKIP;
6830}
6831
3cfcad1b 6832
282d5590 6833static struct hostapd_hw_modes *
c30a4ab0
JB
6834wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6835 u16 *num_modes)
282d5590
JM
6836{
6837 u16 m;
6838 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6839 int i, mode11g_idx = -1;
6840
c30a4ab0
JB
6841 /* heuristic to set up modes */
6842 for (m = 0; m < *num_modes; m++) {
6843 if (!modes[m].num_channels)
6844 continue;
6845 if (modes[m].channels[0].freq < 4000) {
6846 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6847 for (i = 0; i < modes[m].num_rates; i++) {
6848 if (modes[m].rates[i] > 200) {
6849 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6850 break;
6851 }
6852 }
6853 } else if (modes[m].channels[0].freq > 50000)
6854 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6855 else
6856 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6857 }
6858
282d5590
JM
6859 /* If only 802.11g mode is included, use it to construct matching
6860 * 802.11b mode data. */
6861
6862 for (m = 0; m < *num_modes; m++) {
6863 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6864 return modes; /* 802.11b already included */
6865 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6866 mode11g_idx = m;
6867 }
6868
6869 if (mode11g_idx < 0)
6870 return modes; /* 2.4 GHz band not supported at all */
6871
067ffa26 6872 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
282d5590
JM
6873 if (nmodes == NULL)
6874 return modes; /* Could not add 802.11b mode */
6875
6876 mode = &nmodes[*num_modes];
6877 os_memset(mode, 0, sizeof(*mode));
6878 (*num_modes)++;
6879 modes = nmodes;
6880
6881 mode->mode = HOSTAPD_MODE_IEEE80211B;
6882
6883 mode11g = &modes[mode11g_idx];
6884 mode->num_channels = mode11g->num_channels;
6885 mode->channels = os_malloc(mode11g->num_channels *
6886 sizeof(struct hostapd_channel_data));
6887 if (mode->channels == NULL) {
6888 (*num_modes)--;
6889 return modes; /* Could not add 802.11b mode */
6890 }
6891 os_memcpy(mode->channels, mode11g->channels,
6892 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6893
6894 mode->num_rates = 0;
fb7842aa 6895 mode->rates = os_malloc(4 * sizeof(int));
282d5590
JM
6896 if (mode->rates == NULL) {
6897 os_free(mode->channels);
6898 (*num_modes)--;
6899 return modes; /* Could not add 802.11b mode */
6900 }
6901
6902 for (i = 0; i < mode11g->num_rates; i++) {
fb7842aa
JM
6903 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6904 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
282d5590
JM
6905 continue;
6906 mode->rates[mode->num_rates] = mode11g->rates[i];
6907 mode->num_rates++;
6908 if (mode->num_rates == 4)
6909 break;
6910 }
6911
6912 if (mode->num_rates == 0) {
6913 os_free(mode->channels);
6914 os_free(mode->rates);
6915 (*num_modes)--;
6916 return modes; /* No 802.11b rates */
6917 }
6918
6919 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6920 "information");
6921
6922 return modes;
6923}
6924
6925
d8e66e80
JM
6926static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6927 int end)
6928{
6929 int c;
6930
6931 for (c = 0; c < mode->num_channels; c++) {
6932 struct hostapd_channel_data *chan = &mode->channels[c];
6933 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6934 chan->flag |= HOSTAPD_CHAN_HT40;
6935 }
6936}
6937
6938
6939static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6940 int end)
6941{
6942 int c;
6943
6944 for (c = 0; c < mode->num_channels; c++) {
6945 struct hostapd_channel_data *chan = &mode->channels[c];
6946 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6947 continue;
6948 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6949 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6950 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6951 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6952 }
6953}
6954
6955
35f3d3ed 6956static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
6651f1f9
HS
6957 struct phy_info_arg *results)
6958{
6651f1f9
HS
6959 u16 m;
6960
6651f1f9
HS
6961 for (m = 0; m < *results->num_modes; m++) {
6962 int c;
6963 struct hostapd_hw_modes *mode = &results->modes[m];
6964
6965 for (c = 0; c < mode->num_channels; c++) {
6966 struct hostapd_channel_data *chan = &mode->channels[c];
6967 if ((u32) chan->freq - 10 >= start &&
6968 (u32) chan->freq + 10 <= end)
6969 chan->max_tx_power = max_eirp;
6970 }
6971 }
6972}
6973
6974
35f3d3ed 6975static void nl80211_reg_rule_ht40(u32 start, u32 end,
d8e66e80
JM
6976 struct phy_info_arg *results)
6977{
d8e66e80
JM
6978 u16 m;
6979
d8e66e80
JM
6980 for (m = 0; m < *results->num_modes; m++) {
6981 if (!(results->modes[m].ht_capab &
6982 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6983 continue;
6984 nl80211_set_ht40_mode(&results->modes[m], start, end);
6985 }
6986}
6987
6988
6989static void nl80211_reg_rule_sec(struct nlattr *tb[],
6990 struct phy_info_arg *results)
6991{
6992 u32 start, end, max_bw;
6993 u16 m;
6994
6995 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6996 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6997 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6998 return;
6999
7000 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
7001 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
7002 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
7003
7004 if (max_bw < 20)
7005 return;
7006
7007 for (m = 0; m < *results->num_modes; m++) {
7008 if (!(results->modes[m].ht_capab &
7009 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
7010 continue;
7011 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
7012 }
7013}
7014
7015
53cfad46
EP
7016static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
7017 int end)
7018{
7019 int c;
7020
7021 for (c = 0; c < mode->num_channels; c++) {
7022 struct hostapd_channel_data *chan = &mode->channels[c];
7023 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
7024 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
7025
7026 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
7027 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
7028
7029 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
7030 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
7031
7032 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
7033 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
7034 }
7035}
7036
7037
7038static void nl80211_reg_rule_vht(struct nlattr *tb[],
7039 struct phy_info_arg *results)
7040{
7041 u32 start, end, max_bw;
7042 u16 m;
7043
7044 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
7045 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
7046 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
7047 return;
7048
7049 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
7050 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
7051 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
7052
7053 if (max_bw < 80)
7054 return;
7055
7056 for (m = 0; m < *results->num_modes; m++) {
7057 if (!(results->modes[m].ht_capab &
7058 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
7059 continue;
7060 /* TODO: use a real VHT support indication */
7061 if (!results->modes[m].vht_capab)
7062 continue;
7063
7064 nl80211_set_vht_mode(&results->modes[m], start, end);
7065 }
7066}
7067
7068
1412beee
JD
7069static const char * dfs_domain_name(enum nl80211_dfs_regions region)
7070{
7071 switch (region) {
7072 case NL80211_DFS_UNSET:
7073 return "DFS-UNSET";
7074 case NL80211_DFS_FCC:
7075 return "DFS-FCC";
7076 case NL80211_DFS_ETSI:
7077 return "DFS-ETSI";
7078 case NL80211_DFS_JP:
7079 return "DFS-JP";
7080 default:
7081 return "DFS-invalid";
7082 }
7083}
7084
7085
d8e66e80
JM
7086static int nl80211_get_reg(struct nl_msg *msg, void *arg)
7087{
7088 struct phy_info_arg *results = arg;
7089 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
7090 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7091 struct nlattr *nl_rule;
7092 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
7093 int rem_rule;
7094 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
7095 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
7096 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
7097 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
7098 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
7099 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
7100 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
7101 };
7102
7103 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7104 genlmsg_attrlen(gnlh, 0), NULL);
7105 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
7106 !tb_msg[NL80211_ATTR_REG_RULES]) {
7107 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
7108 "available");
7109 return NL_SKIP;
7110 }
7111
1412beee
JD
7112 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
7113 enum nl80211_dfs_regions dfs_domain;
7114 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
7115 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
7116 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
7117 dfs_domain_name(dfs_domain));
7118 } else {
7119 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
7120 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
7121 }
d8e66e80
JM
7122
7123 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7124 {
bfb79dde 7125 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
d8e66e80
JM
7126 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7127 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
35f3d3ed
JM
7128 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
7129 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
7130 continue;
7131 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
7132 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
7133 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7134 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
7135 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7136 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
bfb79dde
JM
7137 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
7138 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
7139
7140 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
7141 start, end, max_bw, max_eirp,
7142 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
7143 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
7144 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
7145 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
7146 "",
7147 flags & NL80211_RRF_DFS ? " (DFS)" : "",
7148 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
7149 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
7150 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
35f3d3ed
JM
7151 if (max_bw >= 40)
7152 nl80211_reg_rule_ht40(start, end, results);
7153 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7154 nl80211_reg_rule_max_eirp(start, end, max_eirp,
7155 results);
d8e66e80
JM
7156 }
7157
7158 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7159 {
7160 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7161 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
7162 nl80211_reg_rule_sec(tb_rule, results);
7163 }
7164
53cfad46
EP
7165 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7166 {
7167 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7168 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
7169 nl80211_reg_rule_vht(tb_rule, results);
7170 }
7171
d8e66e80
JM
7172 return NL_SKIP;
7173}
7174
7175
53cfad46
EP
7176static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
7177 struct phy_info_arg *results)
d8e66e80
JM
7178{
7179 struct nl_msg *msg;
7180
7181 msg = nlmsg_alloc();
7182 if (!msg)
7183 return -ENOMEM;
7184
9fb04070 7185 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
d8e66e80
JM
7186 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
7187}
7188
7189
282d5590
JM
7190static struct hostapd_hw_modes *
7191wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
7192{
43245552 7193 u32 feat;
a2e40bb6
FF
7194 struct i802_bss *bss = priv;
7195 struct wpa_driver_nl80211_data *drv = bss->drv;
282d5590
JM
7196 struct nl_msg *msg;
7197 struct phy_info_arg result = {
7198 .num_modes = num_modes,
7199 .modes = NULL,
43245552 7200 .last_mode = -1,
282d5590
JM
7201 };
7202
7203 *num_modes = 0;
7204 *flags = 0;
7205
7206 msg = nlmsg_alloc();
7207 if (!msg)
7208 return NULL;
7209
43245552
DJ
7210 feat = get_nl80211_protocol_features(drv);
7211 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
7212 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
7213 else
7214 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
282d5590 7215
43245552 7216 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
be24917d
IP
7217 if (nl80211_set_iface_id(msg, bss) < 0)
7218 goto nla_put_failure;
282d5590 7219
d8e66e80 7220 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
53cfad46 7221 nl80211_set_regulatory_flags(drv, &result);
c30a4ab0
JB
7222 return wpa_driver_nl80211_postprocess_modes(result.modes,
7223 num_modes);
d8e66e80 7224 }
5883168a 7225 msg = NULL;
282d5590 7226 nla_put_failure:
5883168a 7227 nlmsg_free(msg);
282d5590
JM
7228 return NULL;
7229}
7230
7231
a11241fa
JB
7232static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
7233 const void *data, size_t len,
7234 int encrypt, int noack)
2c2010ac
JM
7235{
7236 __u8 rtap_hdr[] = {
7237 0x00, 0x00, /* radiotap version */
7238 0x0e, 0x00, /* radiotap length */
7239 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
7240 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
7241 0x00, /* padding */
7242 0x00, 0x00, /* RX and TX flags to indicate that */
7243 0x00, 0x00, /* this is the injected frame directly */
7244 };
7245 struct iovec iov[2] = {
7246 {
7247 .iov_base = &rtap_hdr,
7248 .iov_len = sizeof(rtap_hdr),
7249 },
7250 {
7251 .iov_base = (void *) data,
7252 .iov_len = len,
7253 }
7254 };
7255 struct msghdr msg = {
7256 .msg_name = NULL,
7257 .msg_namelen = 0,
7258 .msg_iov = iov,
7259 .msg_iovlen = 2,
7260 .msg_control = NULL,
7261 .msg_controllen = 0,
7262 .msg_flags = 0,
7263 };
ebbec8b2 7264 int res;
fab25336 7265 u16 txflags = 0;
2c2010ac
JM
7266
7267 if (encrypt)
7268 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
7269
866af8b6
JM
7270 if (drv->monitor_sock < 0) {
7271 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
7272 "for %s", __func__);
7273 return -1;
7274 }
7275
fab25336
HS
7276 if (noack)
7277 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
9d7a63dc 7278 WPA_PUT_LE16(&rtap_hdr[12], txflags);
fab25336 7279
ebbec8b2
JM
7280 res = sendmsg(drv->monitor_sock, &msg, 0);
7281 if (res < 0) {
7282 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
7283 return -1;
7284 }
7285 return 0;
2c2010ac
JM
7286}
7287
7288
a11241fa
JB
7289static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
7290 const void *data, size_t len,
55231068
JM
7291 int encrypt, int noack,
7292 unsigned int freq, int no_cck,
7293 int offchanok, unsigned int wait_time)
a11241fa
JB
7294{
7295 struct wpa_driver_nl80211_data *drv = bss->drv;
7296 u64 cookie;
41cc50d1 7297 int res;
a11241fa 7298
c7caac56
JM
7299 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
7300 freq = nl80211_get_assoc_freq(drv);
7301 wpa_printf(MSG_DEBUG,
7302 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
7303 freq);
7304 }
af964484
JM
7305 if (freq == 0) {
7306 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
7307 bss->freq);
55231068 7308 freq = bss->freq;
af964484 7309 }
55231068 7310
739faee2
JM
7311 if (drv->use_monitor) {
7312 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7313 freq, bss->freq);
a11241fa
JB
7314 return wpa_driver_nl80211_send_mntr(drv, data, len,
7315 encrypt, noack);
739faee2 7316 }
a11241fa 7317
739faee2 7318 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
41cc50d1
JM
7319 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7320 &cookie, no_cck, noack, offchanok);
7321 if (res == 0 && !noack) {
7322 const struct ieee80211_mgmt *mgmt;
7323 u16 fc;
7324
7325 mgmt = (const struct ieee80211_mgmt *) data;
7326 fc = le_to_host16(mgmt->frame_control);
7327 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7328 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7329 wpa_printf(MSG_MSGDUMP,
7330 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7331 (long long unsigned int)
7332 drv->send_action_cookie,
7333 (long long unsigned int) cookie);
7334 drv->send_action_cookie = cookie;
7335 }
7336 }
7337
7338 return res;
a11241fa
JB
7339}
7340
7341
9ebce9c5
JM
7342static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7343 size_t data_len, int noack,
7344 unsigned int freq, int no_cck,
7345 int offchanok,
7346 unsigned int wait_time)
2c2010ac 7347{
a2e40bb6 7348 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 7349 struct ieee80211_mgmt *mgmt;
7a47d567 7350 int encrypt = 1;
2c2010ac
JM
7351 u16 fc;
7352
7353 mgmt = (struct ieee80211_mgmt *) data;
7354 fc = le_to_host16(mgmt->frame_control);
f95a4524
PF
7355 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
7356 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
7357 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
7358 fc, fc2str(fc), drv->nlmode);
2c2010ac 7359
8e12685c
AS
7360 if ((is_sta_interface(drv->nlmode) ||
7361 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
5582a5d1
JB
7362 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7363 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7364 /*
7365 * The use of last_mgmt_freq is a bit of a hack,
7366 * but it works due to the single-threaded nature
7367 * of wpa_supplicant.
7368 */
af964484
JM
7369 if (freq == 0) {
7370 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7371 drv->last_mgmt_freq);
55231068 7372 freq = drv->last_mgmt_freq;
af964484 7373 }
55231068 7374 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7
JB
7375 data, data_len, NULL, 1, noack,
7376 1);
5582a5d1
JB
7377 }
7378
61cbe2ff 7379 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
af964484
JM
7380 if (freq == 0) {
7381 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7382 bss->freq);
55231068 7383 freq = bss->freq;
af964484 7384 }
b5671498
JM
7385 return nl80211_send_frame_cmd(bss, freq,
7386 (int) freq == bss->freq ? 0 :
7387 wait_time,
d8d6b32e
DG
7388 data, data_len,
7389 &drv->send_action_cookie,
55231068 7390 no_cck, noack, offchanok);
86957e62
JM
7391 }
7392
2c2010ac
JM
7393 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7394 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7395 /*
7396 * Only one of the authentication frame types is encrypted.
7397 * In order for static WEP encryption to work properly (i.e.,
7398 * to not encrypt the frame), we need to tell mac80211 about
7399 * the frames that must not be encrypted.
7400 */
7401 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7402 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
7403 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7404 encrypt = 0;
2c2010ac
JM
7405 }
7406
739faee2 7407 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
a11241fa 7408 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068
JM
7409 noack, freq, no_cck, offchanok,
7410 wait_time);
7411}
7412
7413
31357268 7414static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47
JM
7415 int slot, int ht_opmode, int ap_isolate,
7416 int *basic_rates)
31357268
JM
7417{
7418 struct wpa_driver_nl80211_data *drv = bss->drv;
7419 struct nl_msg *msg;
7420
7421 msg = nlmsg_alloc();
7422 if (!msg)
7423 return -ENOMEM;
7424
9fb04070 7425 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
31357268
JM
7426
7427 if (cts >= 0)
7428 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7429 if (preamble >= 0)
7430 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7431 if (slot >= 0)
7432 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7433 if (ht_opmode >= 0)
7434 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
d03e8d11
JM
7435 if (ap_isolate >= 0)
7436 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
e5693c47
JM
7437
7438 if (basic_rates) {
7439 u8 rates[NL80211_MAX_SUPP_RATES];
7440 u8 rates_len = 0;
7441 int i;
7442
7443 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7444 i++)
7445 rates[rates_len++] = basic_rates[i] / 5;
7446
7447 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7448 }
7449
31357268
JM
7450 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7451
7452 return send_and_recv_msgs(drv, msg, NULL, NULL);
7453 nla_put_failure:
5883168a 7454 nlmsg_free(msg);
31357268
JM
7455 return -ENOBUFS;
7456}
7457
7458
3c4ca363
VN
7459static int wpa_driver_nl80211_set_acl(void *priv,
7460 struct hostapd_acl_params *params)
7461{
7462 struct i802_bss *bss = priv;
7463 struct wpa_driver_nl80211_data *drv = bss->drv;
7464 struct nl_msg *msg;
7465 struct nlattr *acl;
7466 unsigned int i;
7467 int ret = 0;
7468
7469 if (!(drv->capa.max_acl_mac_addrs))
7470 return -ENOTSUP;
7471
7472 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7473 return -ENOTSUP;
7474
7475 msg = nlmsg_alloc();
7476 if (!msg)
7477 return -ENOMEM;
7478
7479 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7480 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7481
7482 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7483
7484 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7485
7486 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7487 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7488 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7489
7490 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7491 if (acl == NULL)
7492 goto nla_put_failure;
7493
7494 for (i = 0; i < params->num_mac_acl; i++)
7495 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7496
7497 nla_nest_end(msg, acl);
7498
7499 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7500 msg = NULL;
7501 if (ret) {
7502 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7503 ret, strerror(-ret));
7504 }
7505
7506nla_put_failure:
7507 nlmsg_free(msg);
7508
7509 return ret;
7510}
7511
7512
19c3b566
JM
7513static int wpa_driver_nl80211_set_ap(void *priv,
7514 struct wpa_driver_ap_params *params)
d2440ba0 7515{
a2e40bb6
FF
7516 struct i802_bss *bss = priv;
7517 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
7518 struct nl_msg *msg;
7519 u8 cmd = NL80211_CMD_NEW_BEACON;
7520 int ret;
b4fd6fab 7521 int beacon_set;
8b897f5a 7522 int ifindex = if_nametoindex(bss->ifname);
b11d1d64 7523 int num_suites;
de4ed4a8 7524 u32 suites[10], suite;
b11d1d64 7525 u32 ver;
b4fd6fab 7526
b4fd6fab 7527 beacon_set = bss->beacon_set;
d2440ba0
JM
7528
7529 msg = nlmsg_alloc();
7530 if (!msg)
7531 return -ENOMEM;
7532
7533 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
7534 beacon_set);
7535 if (beacon_set)
d2440ba0
JM
7536 cmd = NL80211_CMD_SET_BEACON;
7537
9fb04070 7538 nl80211_cmd(drv, msg, 0, cmd);
b92e08fc
JM
7539 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7540 params->head, params->head_len);
19c3b566 7541 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
b92e08fc
JM
7542 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7543 params->tail, params->tail_len);
19c3b566 7544 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
b92e08fc 7545 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
b4fd6fab 7546 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
b92e08fc 7547 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
19c3b566 7548 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
b92e08fc 7549 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
19c3b566 7550 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
b92e08fc
JM
7551 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7552 params->ssid, params->ssid_len);
ccb941e6
JM
7553 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7554 params->ssid);
b92e08fc
JM
7555 if (params->proberesp && params->proberesp_len) {
7556 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7557 params->proberesp, params->proberesp_len);
5ed33546
AN
7558 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7559 params->proberesp);
b92e08fc 7560 }
97a7a0b5
JM
7561 switch (params->hide_ssid) {
7562 case NO_SSID_HIDING:
b92e08fc 7563 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
97a7a0b5
JM
7564 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7565 NL80211_HIDDEN_SSID_NOT_IN_USE);
7566 break;
7567 case HIDDEN_SSID_ZERO_LEN:
b92e08fc 7568 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
97a7a0b5
JM
7569 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7570 NL80211_HIDDEN_SSID_ZERO_LEN);
7571 break;
7572 case HIDDEN_SSID_ZERO_CONTENTS:
b92e08fc 7573 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
97a7a0b5
JM
7574 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7575 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7576 break;
7577 }
b92e08fc 7578 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
b11d1d64
JM
7579 if (params->privacy)
7580 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
b92e08fc 7581 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
b11d1d64
JM
7582 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7583 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7584 /* Leave out the attribute */
7585 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7586 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7587 NL80211_AUTHTYPE_SHARED_KEY);
7588 else
7589 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7590 NL80211_AUTHTYPE_OPEN_SYSTEM);
7591
b92e08fc 7592 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
b11d1d64
JM
7593 ver = 0;
7594 if (params->wpa_version & WPA_PROTO_WPA)
7595 ver |= NL80211_WPA_VERSION_1;
7596 if (params->wpa_version & WPA_PROTO_RSN)
7597 ver |= NL80211_WPA_VERSION_2;
7598 if (ver)
7599 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7600
b92e08fc
JM
7601 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7602 params->key_mgmt_suites);
b11d1d64
JM
7603 num_suites = 0;
7604 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7605 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7606 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7607 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7608 if (num_suites) {
7609 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7610 num_suites * sizeof(u32), suites);
7611 }
7612
9f12614b
JB
7613 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7614 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7615 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7616
b92e08fc
JM
7617 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7618 params->pairwise_ciphers);
de4ed4a8
JM
7619 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7620 suites, ARRAY_SIZE(suites));
b11d1d64
JM
7621 if (num_suites) {
7622 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7623 num_suites * sizeof(u32), suites);
7624 }
7625
b92e08fc
JM
7626 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7627 params->group_cipher);
de4ed4a8
JM
7628 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7629 if (suite)
7630 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
d2440ba0 7631
fb91db56 7632 if (params->beacon_ies) {
b92e08fc
JM
7633 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7634 params->beacon_ies);
fb91db56
JM
7635 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7636 wpabuf_head(params->beacon_ies));
7637 }
7638 if (params->proberesp_ies) {
b92e08fc
JM
7639 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7640 params->proberesp_ies);
fb91db56
JM
7641 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7642 wpabuf_len(params->proberesp_ies),
7643 wpabuf_head(params->proberesp_ies));
7644 }
7645 if (params->assocresp_ies) {
b92e08fc
JM
7646 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7647 params->assocresp_ies);
fb91db56
JM
7648 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7649 wpabuf_len(params->assocresp_ies),
7650 wpabuf_head(params->assocresp_ies));
7651 }
7652
a0133ee1 7653 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
b92e08fc
JM
7654 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7655 params->ap_max_inactivity);
a0133ee1
VT
7656 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7657 params->ap_max_inactivity);
7658 }
7659
d2440ba0
JM
7660 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7661 if (ret) {
7662 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7663 ret, strerror(-ret));
b4fd6fab 7664 } else {
b4fd6fab 7665 bss->beacon_set = 1;
31357268 7666 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 7667 params->short_slot_time, params->ht_opmode,
e5693c47 7668 params->isolate, params->basic_rates);
e87ef751
PX
7669 if (beacon_set && params->freq &&
7670 params->freq->bandwidth != bss->bandwidth) {
7671 wpa_printf(MSG_DEBUG,
7672 "nl80211: Update BSS %s bandwidth: %d -> %d",
7673 bss->ifname, bss->bandwidth,
7674 params->freq->bandwidth);
7675 ret = nl80211_set_channel(bss, params->freq, 1);
7676 if (ret) {
7677 wpa_printf(MSG_DEBUG,
7678 "nl80211: Frequency set failed: %d (%s)",
7679 ret, strerror(-ret));
7680 } else {
7681 wpa_printf(MSG_DEBUG,
7682 "nl80211: Frequency set succeeded for ht2040 coex");
7683 bss->bandwidth = params->freq->bandwidth;
7684 }
7685 } else if (!beacon_set) {
7686 /*
7687 * cfg80211 updates the driver on frequence change in AP
7688 * mode only at the point when beaconing is started, so
7689 * set the initial value here.
7690 */
7691 bss->bandwidth = params->freq->bandwidth;
7692 }
b4fd6fab 7693 }
d2440ba0
JM
7694 return ret;
7695 nla_put_failure:
5883168a 7696 nlmsg_free(msg);
d2440ba0
JM
7697 return -ENOBUFS;
7698}
7699
7700
1c4ffa87
AO
7701static int nl80211_put_freq_params(struct nl_msg *msg,
7702 struct hostapd_freq_params *freq)
1581b38b 7703{
89b800d7
JB
7704 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7705 if (freq->vht_enabled) {
7706 switch (freq->bandwidth) {
7707 case 20:
7708 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7709 NL80211_CHAN_WIDTH_20);
7710 break;
7711 case 40:
7712 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7713 NL80211_CHAN_WIDTH_40);
7714 break;
7715 case 80:
7716 if (freq->center_freq2)
7717 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7718 NL80211_CHAN_WIDTH_80P80);
7719 else
7720 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7721 NL80211_CHAN_WIDTH_80);
7722 break;
7723 case 160:
7724 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7725 NL80211_CHAN_WIDTH_160);
7726 break;
7727 default:
1c4ffa87 7728 return -EINVAL;
89b800d7
JB
7729 }
7730 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7731 if (freq->center_freq2)
7732 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7733 freq->center_freq2);
7734 } else if (freq->ht_enabled) {
7735 switch (freq->sec_channel_offset) {
f019981a
JM
7736 case -1:
7737 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7738 NL80211_CHAN_HT40MINUS);
7739 break;
7740 case 1:
7741 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7742 NL80211_CHAN_HT40PLUS);
7743 break;
7744 default:
7745 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7746 NL80211_CHAN_HT20);
7747 break;
7748 }
7749 }
1c4ffa87
AO
7750 return 0;
7751
7752nla_put_failure:
7753 return -ENOBUFS;
7754}
7755
7756
e87ef751
PX
7757static int nl80211_set_channel(struct i802_bss *bss,
7758 struct hostapd_freq_params *freq, int set_chan)
1c4ffa87
AO
7759{
7760 struct wpa_driver_nl80211_data *drv = bss->drv;
7761 struct nl_msg *msg;
7762 int ret;
7763
7764 wpa_printf(MSG_DEBUG,
7765 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7766 freq->freq, freq->ht_enabled, freq->vht_enabled,
7767 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7768 msg = nlmsg_alloc();
7769 if (!msg)
7770 return -1;
7771
e87ef751
PX
7772 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7773 NL80211_CMD_SET_WIPHY);
1c4ffa87
AO
7774
7775 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7776 if (nl80211_put_freq_params(msg, freq) < 0)
7777 goto nla_put_failure;
1581b38b 7778
d2440ba0 7779 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7780 msg = NULL;
e4fb2167 7781 if (ret == 0) {
89b800d7 7782 bss->freq = freq->freq;
1581b38b 7783 return 0;
e4fb2167 7784 }
f019981a 7785 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 7786 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b 7787nla_put_failure:
5883168a 7788 nlmsg_free(msg);
1581b38b
JM
7789 return -1;
7790}
7791
0f4e8b4f 7792
95ab6063
AN
7793static u32 sta_flags_nl80211(int flags)
7794{
7795 u32 f = 0;
7796
7797 if (flags & WPA_STA_AUTHORIZED)
7798 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7799 if (flags & WPA_STA_WMM)
7800 f |= BIT(NL80211_STA_FLAG_WME);
7801 if (flags & WPA_STA_SHORT_PREAMBLE)
7802 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7803 if (flags & WPA_STA_MFP)
7804 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
7805 if (flags & WPA_STA_TDLS_PEER)
7806 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
95ab6063
AN
7807
7808 return f;
7809}
7810
7811
62847751 7812static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
7813 struct hostapd_sta_add_params *params)
7814{
a2e40bb6
FF
7815 struct i802_bss *bss = priv;
7816 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8 7817 struct nl_msg *msg;
95ab6063 7818 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
7819 int ret = -ENOBUFS;
7820
45b722f1
AN
7821 if ((params->flags & WPA_STA_TDLS_PEER) &&
7822 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7823 return -EOPNOTSUPP;
7824
0f4e8b4f
JM
7825 msg = nlmsg_alloc();
7826 if (!msg)
7827 return -ENOMEM;
7828
e4dea253
JM
7829 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7830 params->set ? "Set" : "Add", MAC2STR(params->addr));
45b722f1
AN
7831 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7832 NL80211_CMD_NEW_STATION);
0f4e8b4f 7833
62847751 7834 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
0f4e8b4f 7835 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
0f4e8b4f
JM
7836 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7837 params->supp_rates);
e4dea253
JM
7838 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7839 params->supp_rates_len);
45b722f1 7840 if (!params->set) {
f11b72c3
JM
7841 if (params->aid) {
7842 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7843 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7844 } else {
7845 /*
7846 * cfg80211 validates that AID is non-zero, so we have
7847 * to make this a non-zero value for the TDLS case where
7848 * a dummy STA entry is used for now.
7849 */
7850 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7851 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7852 }
e4dea253
JM
7853 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7854 params->listen_interval);
45b722f1
AN
7855 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7856 params->listen_interval);
e112764e
JM
7857 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7858 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7859 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
45b722f1 7860 }
0f4e8b4f 7861 if (params->ht_capabilities) {
e4dea253
JM
7862 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7863 (u8 *) params->ht_capabilities,
7864 sizeof(*params->ht_capabilities));
0f4e8b4f 7865 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
fc4e2d95
JM
7866 sizeof(*params->ht_capabilities),
7867 params->ht_capabilities);
0f4e8b4f 7868 }
0f4e8b4f 7869
05a8d422 7870 if (params->vht_capabilities) {
e4dea253
JM
7871 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7872 (u8 *) params->vht_capabilities,
7873 sizeof(*params->vht_capabilities));
05a8d422
JB
7874 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7875 sizeof(*params->vht_capabilities),
7876 params->vht_capabilities);
7877 }
7878
8a458116
MK
7879 if (params->vht_opmode_enabled) {
7880 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7881 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7882 params->vht_opmode);
7883 }
7884
122d16f2
SD
7885 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7886 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7887
7888 if (params->ext_capab) {
7889 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7890 params->ext_capab, params->ext_capab_len);
7891 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7892 params->ext_capab_len, params->ext_capab);
7893 }
7894
efc64886
SD
7895 if (params->supp_channels) {
7896 wpa_hexdump(MSG_DEBUG, " * supported channels",
7897 params->supp_channels, params->supp_channels_len);
7898 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7899 params->supp_channels_len, params->supp_channels);
7900 }
7901
7902 if (params->supp_oper_classes) {
7903 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7904 params->supp_oper_classes,
7905 params->supp_oper_classes_len);
7906 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7907 params->supp_oper_classes_len,
7908 params->supp_oper_classes);
7909 }
7910
95ab6063 7911 os_memset(&upd, 0, sizeof(upd));
7e31703e
BC
7912 upd.set = sta_flags_nl80211(params->flags);
7913 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
e4dea253
JM
7914 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7915 upd.set, upd.mask);
95ab6063
AN
7916 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7917
774bfa62 7918 if (params->flags & WPA_STA_WMM) {
8970bae8
JB
7919 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7920
774bfa62
EP
7921 if (!wme)
7922 goto nla_put_failure;
7923
e4dea253 7924 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
8970bae8 7925 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
5d061637 7926 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
8970bae8 7927 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
bdaf1748 7928 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5d061637 7929 WMM_QOSINFO_STA_SP_MASK);
8970bae8 7930 nla_nest_end(msg, wme);
774bfa62
EP
7931 }
7932
0f4e8b4f 7933 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7934 msg = NULL;
0f4e8b4f 7935 if (ret)
45b722f1
AN
7936 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7937 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7938 strerror(-ret));
0f4e8b4f
JM
7939 if (ret == -EEXIST)
7940 ret = 0;
7941 nla_put_failure:
5883168a 7942 nlmsg_free(msg);
0f4e8b4f
JM
7943 return ret;
7944}
7945
7946
97ed9a06
KP
7947static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
7948{
7949#ifdef CONFIG_LIBNL3_ROUTE
7950 struct wpa_driver_nl80211_data *drv = bss->drv;
7951 struct rtnl_neigh *rn;
7952 struct nl_addr *nl_addr;
7953 int err;
7954
7955 rn = rtnl_neigh_alloc();
7956 if (!rn)
7957 return;
7958
7959 rtnl_neigh_set_family(rn, AF_BRIDGE);
7960 rtnl_neigh_set_ifindex(rn, bss->ifindex);
7961 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
7962 if (!nl_addr) {
7963 rtnl_neigh_put(rn);
7964 return;
7965 }
7966 rtnl_neigh_set_lladdr(rn, nl_addr);
7967
7968 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
7969 if (err < 0) {
7970 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
7971 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
7972 bss->ifindex, nl_geterror(err));
7973 } else {
7974 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
7975 MACSTR, MAC2STR(addr));
7976 }
7977
7978 nl_addr_put(nl_addr);
7979 rtnl_neigh_put(rn);
7980#endif /* CONFIG_LIBNL3_ROUTE */
7981}
7982
7983
59d7148a
JM
7984static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
7985 int deauth, u16 reason_code)
0f4e8b4f 7986{
a2e40bb6 7987 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
7988 struct nl_msg *msg;
7989 int ret;
7990
7991 msg = nlmsg_alloc();
7992 if (!msg)
7993 return -ENOMEM;
7994
9fb04070 7995 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
0f4e8b4f
JM
7996
7997 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7998 if_nametoindex(bss->ifname));
0f4e8b4f 7999 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
59d7148a
JM
8000 if (deauth == 0)
8001 NLA_PUT_U8(msg, NL80211_ATTR_MGMT_SUBTYPE,
8002 WLAN_FC_STYPE_DISASSOC);
8003 else if (deauth == 1)
8004 NLA_PUT_U8(msg, NL80211_ATTR_MGMT_SUBTYPE,
8005 WLAN_FC_STYPE_DEAUTH);
8006 if (reason_code)
8007 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
0f4e8b4f
JM
8008
8009 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83e7bb0e
JM
8010 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
8011 " --> %d (%s)",
8012 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
97ed9a06
KP
8013
8014 if (drv->rtnl_sk)
8015 rtnl_neigh_delete_fdb_entry(bss, addr);
8016
0f4e8b4f
JM
8017 if (ret == -ENOENT)
8018 return 0;
8019 return ret;
8020 nla_put_failure:
5883168a 8021 nlmsg_free(msg);
0f4e8b4f
JM
8022 return -ENOBUFS;
8023}
8024
1581b38b 8025
0915d02c
JM
8026static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
8027 int ifidx)
8028{
8029 struct nl_msg *msg;
de884303 8030 struct wpa_driver_nl80211_data *drv2;
0915d02c 8031
c6e8e8e4
JM
8032 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
8033
2135f224 8034 /* stop listening for EAPOL on this interface */
de884303
JM
8035 dl_list_for_each(drv2, &drv->global->interfaces,
8036 struct wpa_driver_nl80211_data, list)
8037 del_ifidx(drv2, ifidx);
2135f224 8038
0915d02c
JM
8039 msg = nlmsg_alloc();
8040 if (!msg)
8041 goto nla_put_failure;
8042
9fb04070 8043 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
8044 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
8045
8046 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8047 return;
5883168a 8048 msg = NULL;
0915d02c 8049 nla_put_failure:
5883168a 8050 nlmsg_free(msg);
e748062b 8051 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
8052}
8053
8054
a1922f93
JM
8055static const char * nl80211_iftype_str(enum nl80211_iftype mode)
8056{
8057 switch (mode) {
8058 case NL80211_IFTYPE_ADHOC:
8059 return "ADHOC";
8060 case NL80211_IFTYPE_STATION:
8061 return "STATION";
8062 case NL80211_IFTYPE_AP:
8063 return "AP";
1045ec36
JM
8064 case NL80211_IFTYPE_AP_VLAN:
8065 return "AP_VLAN";
8066 case NL80211_IFTYPE_WDS:
8067 return "WDS";
a1922f93
JM
8068 case NL80211_IFTYPE_MONITOR:
8069 return "MONITOR";
1045ec36
JM
8070 case NL80211_IFTYPE_MESH_POINT:
8071 return "MESH_POINT";
a1922f93
JM
8072 case NL80211_IFTYPE_P2P_CLIENT:
8073 return "P2P_CLIENT";
8074 case NL80211_IFTYPE_P2P_GO:
8075 return "P2P_GO";
7aad838c
NS
8076 case NL80211_IFTYPE_P2P_DEVICE:
8077 return "P2P_DEVICE";
a1922f93
JM
8078 default:
8079 return "unknown";
8080 }
8081}
8082
8083
a35187e7
KH
8084static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
8085 const char *ifname,
8086 enum nl80211_iftype iftype,
d6dcfcda
DS
8087 const u8 *addr, int wds,
8088 int (*handler)(struct nl_msg *, void *),
8089 void *arg)
0915d02c 8090{
8970bae8 8091 struct nl_msg *msg;
0915d02c
JM
8092 int ifidx;
8093 int ret = -ENOBUFS;
8094
a1922f93
JM
8095 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
8096 iftype, nl80211_iftype_str(iftype));
8097
0915d02c
JM
8098 msg = nlmsg_alloc();
8099 if (!msg)
8100 return -1;
8101
9fb04070 8102 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
834ee56f 8103 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
fa93de40 8104 goto nla_put_failure;
0915d02c
JM
8105 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
8106 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
8107
8108 if (iftype == NL80211_IFTYPE_MONITOR) {
8970bae8 8109 struct nlattr *flags;
0915d02c 8110
8970bae8 8111 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
0915d02c
JM
8112 if (!flags)
8113 goto nla_put_failure;
8114
8970bae8 8115 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
0915d02c 8116
8970bae8 8117 nla_nest_end(msg, flags);
fbbfcbac
FF
8118 } else if (wds) {
8119 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
0915d02c
JM
8120 }
8121
52f5877a
IP
8122 /*
8123 * Tell cfg80211 that the interface belongs to the socket that created
8124 * it, and the interface should be deleted when the socket is closed.
8125 */
8126 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
8127
d6dcfcda 8128 ret = send_and_recv_msgs(drv, msg, handler, arg);
5883168a 8129 msg = NULL;
0915d02c
JM
8130 if (ret) {
8131 nla_put_failure:
5883168a 8132 nlmsg_free(msg);
a35187e7
KH
8133 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
8134 ifname, ret, strerror(-ret));
0915d02c
JM
8135 return ret;
8136 }
8137
f632e483 8138 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6bae92e0 8139 return 0;
6bae92e0 8140
0915d02c 8141 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
8142 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
8143 ifname, ifidx);
0915d02c
JM
8144
8145 if (ifidx <= 0)
8146 return -1;
8147
147848ec
JM
8148 /*
8149 * Some virtual interfaces need to process EAPOL packets and events on
8150 * the parent interface. This is used mainly with hostapd.
8151 */
8152 if (drv->hostapd ||
8153 iftype == NL80211_IFTYPE_AP_VLAN ||
8154 iftype == NL80211_IFTYPE_WDS ||
8155 iftype == NL80211_IFTYPE_MONITOR) {
8156 /* start listening for EAPOL on this interface */
8157 add_ifidx(drv, ifidx);
8158 }
2135f224 8159
7bfc47c3 8160 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 8161 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
8162 nl80211_remove_iface(drv, ifidx);
8163 return -1;
2135f224 8164 }
2135f224 8165
0915d02c
JM
8166 return ifidx;
8167}
22a7c9d7
JM
8168
8169
a35187e7
KH
8170static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
8171 const char *ifname, enum nl80211_iftype iftype,
d6dcfcda
DS
8172 const u8 *addr, int wds,
8173 int (*handler)(struct nl_msg *, void *),
2aec4f3c 8174 void *arg, int use_existing)
a35187e7
KH
8175{
8176 int ret;
8177
d6dcfcda
DS
8178 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
8179 arg);
a35187e7 8180
ffbf1eaa 8181 /* if error occurred and interface exists already */
a35187e7 8182 if (ret == -ENFILE && if_nametoindex(ifname)) {
2aec4f3c
JM
8183 if (use_existing) {
8184 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
8185 ifname);
6997f8ba
JM
8186 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
8187 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8188 addr) < 0 &&
8189 (linux_set_iface_flags(drv->global->ioctl_sock,
8190 ifname, 0) < 0 ||
8191 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8192 addr) < 0 ||
8193 linux_set_iface_flags(drv->global->ioctl_sock,
8194 ifname, 1) < 0))
8195 return -1;
2aec4f3c
JM
8196 return -ENFILE;
8197 }
a35187e7
KH
8198 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
8199
8200 /* Try to remove the interface that was already there. */
8201 nl80211_remove_iface(drv, if_nametoindex(ifname));
8202
8203 /* Try to create the interface again */
fbbfcbac 8204 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
d6dcfcda 8205 wds, handler, arg);
a35187e7
KH
8206 }
8207
6a71413e 8208 if (ret >= 0 && is_p2p_net_interface(iftype))
4e5cb1a3
JM
8209 nl80211_disable_11b_rates(drv, ret, 1);
8210
a35187e7
KH
8211 return ret;
8212}
0915d02c 8213
2135f224 8214
0915d02c
JM
8215static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
8216{
8217 struct ieee80211_hdr *hdr;
f8b1f695
JM
8218 u16 fc;
8219 union wpa_event_data event;
0915d02c
JM
8220
8221 hdr = (struct ieee80211_hdr *) buf;
8222 fc = le_to_host16(hdr->frame_control);
8223
f8b1f695
JM
8224 os_memset(&event, 0, sizeof(event));
8225 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
8226 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
8227 event.tx_status.dst = hdr->addr1;
8228 event.tx_status.data = buf;
8229 event.tx_status.data_len = len;
8230 event.tx_status.ack = ok;
8231 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
0915d02c
JM
8232}
8233
8234
4b9841d3 8235static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
0d9fc3d8 8236 u8 *buf, size_t len)
0915d02c 8237{
9b90955e
JB
8238 struct ieee80211_hdr *hdr = (void *)buf;
8239 u16 fc;
f8b1f695 8240 union wpa_event_data event;
9b90955e
JB
8241
8242 if (len < sizeof(*hdr))
8243 return;
8244
8245 fc = le_to_host16(hdr->frame_control);
8246
f8b1f695 8247 os_memset(&event, 0, sizeof(event));
9b90955e
JB
8248 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
8249 event.rx_from_unknown.addr = hdr->addr2;
8250 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
8251 (WLAN_FC_FROMDS | WLAN_FC_TODS);
f8b1f695 8252 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4b9841d3 8253}
0915d02c 8254
4b9841d3
JM
8255
8256static void handle_frame(struct wpa_driver_nl80211_data *drv,
2a8b7416 8257 u8 *buf, size_t len, int datarate, int ssi_signal)
4b9841d3
JM
8258{
8259 struct ieee80211_hdr *hdr;
f8b1f695
JM
8260 u16 fc;
8261 union wpa_event_data event;
0915d02c
JM
8262
8263 hdr = (struct ieee80211_hdr *) buf;
8264 fc = le_to_host16(hdr->frame_control);
0915d02c 8265
4b9841d3 8266 switch (WLAN_FC_GET_TYPE(fc)) {
0915d02c 8267 case WLAN_FC_TYPE_MGMT:
f8b1f695
JM
8268 os_memset(&event, 0, sizeof(event));
8269 event.rx_mgmt.frame = buf;
8270 event.rx_mgmt.frame_len = len;
2a8b7416
JM
8271 event.rx_mgmt.datarate = datarate;
8272 event.rx_mgmt.ssi_signal = ssi_signal;
f8b1f695 8273 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
0915d02c
JM
8274 break;
8275 case WLAN_FC_TYPE_CTRL:
8276 /* can only get here with PS-Poll frames */
8277 wpa_printf(MSG_DEBUG, "CTRL");
0d9fc3d8 8278 from_unknown_sta(drv, buf, len);
0915d02c
JM
8279 break;
8280 case WLAN_FC_TYPE_DATA:
0d9fc3d8 8281 from_unknown_sta(drv, buf, len);
0915d02c
JM
8282 break;
8283 }
8284}
8285
8286
8287static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
8288{
8289 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8290 int len;
8291 unsigned char buf[3000];
8292 struct ieee80211_radiotap_iterator iter;
8293 int ret;
2a8b7416 8294 int datarate = 0, ssi_signal = 0;
4b9841d3 8295 int injected = 0, failed = 0, rxflags = 0;
0915d02c
JM
8296
8297 len = recv(sock, buf, sizeof(buf), 0);
8298 if (len < 0) {
7ac3616d
JM
8299 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
8300 strerror(errno));
0915d02c
JM
8301 return;
8302 }
8303
0e80ea2c 8304 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
7ac3616d 8305 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
0915d02c
JM
8306 return;
8307 }
8308
0915d02c
JM
8309 while (1) {
8310 ret = ieee80211_radiotap_iterator_next(&iter);
8311 if (ret == -ENOENT)
8312 break;
8313 if (ret) {
7ac3616d
JM
8314 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
8315 ret);
0915d02c
JM
8316 return;
8317 }
8318 switch (iter.this_arg_index) {
8319 case IEEE80211_RADIOTAP_FLAGS:
8320 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
8321 len -= 4;
8322 break;
8323 case IEEE80211_RADIOTAP_RX_FLAGS:
8324 rxflags = 1;
8325 break;
8326 case IEEE80211_RADIOTAP_TX_FLAGS:
8327 injected = 1;
8328 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
8329 IEEE80211_RADIOTAP_F_TX_FAIL;
8330 break;
8331 case IEEE80211_RADIOTAP_DATA_RETRIES:
8332 break;
8333 case IEEE80211_RADIOTAP_CHANNEL:
2a8b7416 8334 /* TODO: convert from freq/flags to channel number */
0915d02c
JM
8335 break;
8336 case IEEE80211_RADIOTAP_RATE:
2a8b7416 8337 datarate = *iter.this_arg * 5;
0915d02c 8338 break;
baf513d6
JB
8339 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
8340 ssi_signal = (s8) *iter.this_arg;
0915d02c
JM
8341 break;
8342 }
8343 }
8344
8345 if (rxflags && injected)
8346 return;
8347
8348 if (!injected)
bacb984b
JB
8349 handle_frame(drv, buf + iter._max_length,
8350 len - iter._max_length, datarate, ssi_signal);
0915d02c 8351 else
bacb984b
JB
8352 handle_tx_callback(drv->ctx, buf + iter._max_length,
8353 len - iter._max_length, !failed);
0915d02c
JM
8354}
8355
8356
8357/*
8358 * we post-process the filter code later and rewrite
8359 * this to the offset to the last instruction
8360 */
8361#define PASS 0xFF
8362#define FAIL 0xFE
8363
8364static struct sock_filter msock_filter_insns[] = {
8365 /*
8366 * do a little-endian load of the radiotap length field
8367 */
8368 /* load lower byte into A */
8369 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
8370 /* put it into X (== index register) */
8371 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8372 /* load upper byte into A */
8373 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8374 /* left-shift it by 8 */
8375 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8376 /* or with X */
8377 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8378 /* put result into X */
8379 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8380
8381 /*
8382 * Allow management frames through, this also gives us those
8383 * management frames that we sent ourselves with status
8384 */
8385 /* load the lower byte of the IEEE 802.11 frame control field */
8386 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8387 /* mask off frame type and version */
8388 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8389 /* accept frame if it's both 0, fall through otherwise */
8390 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8391
8392 /*
8393 * TODO: add a bit to radiotap RX flags that indicates
8394 * that the sending station is not associated, then
8395 * add a filter here that filters on our DA and that flag
8396 * to allow us to deauth frames to that bad station.
8397 *
65ae1afd 8398 * For now allow all To DS data frames through.
0915d02c 8399 */
65ae1afd
HS
8400 /* load the IEEE 802.11 frame control field */
8401 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8402 /* mask off frame type, version and DS status */
8403 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8404 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8405 */
8406 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
0915d02c
JM
8407
8408#if 0
8409 /*
fbbfcbac 8410 * drop non-data frames
0915d02c
JM
8411 */
8412 /* load the lower byte of the frame control field */
8413 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8414 /* mask off QoS bit */
8415 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8416 /* drop non-data frames */
8417 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
fbbfcbac 8418#endif
0915d02c 8419 /* load the upper byte of the frame control field */
fbbfcbac 8420 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
0915d02c
JM
8421 /* mask off toDS/fromDS */
8422 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
fbbfcbac
FF
8423 /* accept WDS frames */
8424 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
0915d02c
JM
8425
8426 /*
8427 * add header length to index
8428 */
8429 /* load the lower byte of the frame control field */
8430 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8431 /* mask off QoS bit */
8432 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8433 /* right shift it by 6 to give 0 or 2 */
8434 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8435 /* add data frame header length */
8436 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8437 /* add index, was start of 802.11 header */
8438 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8439 /* move to index, now start of LL header */
8440 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8441
8442 /*
8443 * Accept empty data frames, we use those for
8444 * polling activity.
8445 */
8446 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8447 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8448
8449 /*
8450 * Accept EAPOL frames
8451 */
8452 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8453 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8454 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8455 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8456
8457 /* keep these last two statements or change the code below */
8458 /* return 0 == "DROP" */
8459 BPF_STMT(BPF_RET | BPF_K, 0),
8460 /* return ~0 == "keep all" */
8461 BPF_STMT(BPF_RET | BPF_K, ~0),
8462};
8463
8464static struct sock_fprog msock_filter = {
e7ecab4a 8465 .len = ARRAY_SIZE(msock_filter_insns),
0915d02c
JM
8466 .filter = msock_filter_insns,
8467};
8468
8469
8470static int add_monitor_filter(int s)
8471{
8472 int idx;
8473
8474 /* rewrite all PASS/FAIL jump offsets */
8475 for (idx = 0; idx < msock_filter.len; idx++) {
8476 struct sock_filter *insn = &msock_filter_insns[idx];
8477
8478 if (BPF_CLASS(insn->code) == BPF_JMP) {
8479 if (insn->code == (BPF_JMP|BPF_JA)) {
8480 if (insn->k == PASS)
8481 insn->k = msock_filter.len - idx - 2;
8482 else if (insn->k == FAIL)
8483 insn->k = msock_filter.len - idx - 3;
8484 }
8485
8486 if (insn->jt == PASS)
8487 insn->jt = msock_filter.len - idx - 2;
8488 else if (insn->jt == FAIL)
8489 insn->jt = msock_filter.len - idx - 3;
8490
8491 if (insn->jf == PASS)
8492 insn->jf = msock_filter.len - idx - 2;
8493 else if (insn->jf == FAIL)
8494 insn->jf = msock_filter.len - idx - 3;
8495 }
8496 }
8497
8498 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8499 &msock_filter, sizeof(msock_filter))) {
7ac3616d
JM
8500 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8501 strerror(errno));
0915d02c
JM
8502 return -1;
8503 }
8504
8505 return 0;
8506}
8507
8508
460456f8
JM
8509static void nl80211_remove_monitor_interface(
8510 struct wpa_driver_nl80211_data *drv)
8511{
748c0ac0
JM
8512 if (drv->monitor_refcount > 0)
8513 drv->monitor_refcount--;
8514 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8515 drv->monitor_refcount);
3fd1cefb
JB
8516 if (drv->monitor_refcount > 0)
8517 return;
8518
460456f8
JM
8519 if (drv->monitor_ifidx >= 0) {
8520 nl80211_remove_iface(drv, drv->monitor_ifidx);
8521 drv->monitor_ifidx = -1;
8522 }
504e905c
JM
8523 if (drv->monitor_sock >= 0) {
8524 eloop_unregister_read_sock(drv->monitor_sock);
8525 close(drv->monitor_sock);
8526 drv->monitor_sock = -1;
8527 }
460456f8
JM
8528}
8529
8530
0915d02c
JM
8531static int
8532nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8533{
8534 char buf[IFNAMSIZ];
8535 struct sockaddr_ll ll;
8536 int optval;
8537 socklen_t optlen;
0915d02c 8538
3fd1cefb
JB
8539 if (drv->monitor_ifidx >= 0) {
8540 drv->monitor_refcount++;
748c0ac0
JM
8541 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8542 drv->monitor_refcount);
3fd1cefb
JB
8543 return 0;
8544 }
8545
834ee56f 8546 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
6758b167
JJ
8547 /*
8548 * P2P interface name is of the format p2p-%s-%d. For monitor
8549 * interface name corresponding to P2P GO, replace "p2p-" with
8550 * "mon-" to retain the same interface name length and to
8551 * indicate that it is a monitor interface.
8552 */
834ee56f 8553 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
6758b167
JJ
8554 } else {
8555 /* Non-P2P interface with AP functionality. */
834ee56f 8556 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
6758b167
JJ
8557 }
8558
0915d02c
JM
8559 buf[IFNAMSIZ - 1] = '\0';
8560
8561 drv->monitor_ifidx =
fbbfcbac 8562 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
2aec4f3c 8563 0, NULL, NULL, 0);
0915d02c 8564
866af8b6 8565 if (drv->monitor_ifidx == -EOPNOTSUPP) {
61cbe2ff
JB
8566 /*
8567 * This is backward compatibility for a few versions of
8568 * the kernel only that didn't advertise the right
8569 * attributes for the only driver that then supported
8570 * AP mode w/o monitor -- ath6kl.
8571 */
866af8b6
JM
8572 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8573 "monitor interface type - try to run without it");
61cbe2ff 8574 drv->device_ap_sme = 1;
866af8b6
JM
8575 }
8576
0915d02c
JM
8577 if (drv->monitor_ifidx < 0)
8578 return -1;
8579
c81eff1a 8580 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
0915d02c 8581 goto error;
0915d02c
JM
8582
8583 memset(&ll, 0, sizeof(ll));
8584 ll.sll_family = AF_PACKET;
8585 ll.sll_ifindex = drv->monitor_ifidx;
8586 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8587 if (drv->monitor_sock < 0) {
7ac3616d
JM
8588 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8589 strerror(errno));
0915d02c
JM
8590 goto error;
8591 }
8592
8593 if (add_monitor_filter(drv->monitor_sock)) {
8594 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8595 "interface; do filtering in user space");
8596 /* This works, but will cost in performance. */
8597 }
8598
2135f224 8599 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
7ac3616d
JM
8600 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8601 strerror(errno));
0915d02c
JM
8602 goto error;
8603 }
8604
8605 optlen = sizeof(optval);
8606 optval = 20;
8607 if (setsockopt
8608 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
7ac3616d
JM
8609 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8610 strerror(errno));
0915d02c
JM
8611 goto error;
8612 }
8613
8614 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8615 drv, NULL)) {
7ac3616d 8616 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
0915d02c
JM
8617 goto error;
8618 }
8619
748c0ac0 8620 drv->monitor_refcount++;
0915d02c
JM
8621 return 0;
8622 error:
460456f8 8623 nl80211_remove_monitor_interface(drv);
0915d02c
JM
8624 return -1;
8625}
8626
db149ac9 8627
3fd1cefb
JB
8628static int nl80211_setup_ap(struct i802_bss *bss)
8629{
8630 struct wpa_driver_nl80211_data *drv = bss->drv;
8631
748c0ac0
JM
8632 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8633 bss->ifname, drv->device_ap_sme, drv->use_monitor);
36488c05 8634
a11241fa
JB
8635 /*
8636 * Disable Probe Request reporting unless we need it in this way for
8637 * devices that include the AP SME, in the other case (unless using
8638 * monitor iface) we'll get it through the nl_mgmt socket instead.
8639 */
8640 if (!drv->device_ap_sme)
8641 wpa_driver_nl80211_probe_req_report(bss, 0);
8642
8643 if (!drv->device_ap_sme && !drv->use_monitor)
8644 if (nl80211_mgmt_subscribe_ap(bss))
8645 return -1;
8646
a6cc0602
JM
8647 if (drv->device_ap_sme && !drv->use_monitor)
8648 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8649 return -1;
8650
a11241fa 8651 if (!drv->device_ap_sme && drv->use_monitor &&
3fd1cefb
JB
8652 nl80211_create_monitor_interface(drv) &&
8653 !drv->device_ap_sme)
8654 return -1;
8655
8656 if (drv->device_ap_sme &&
8657 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8658 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8659 "Probe Request frame reporting in AP mode");
8660 /* Try to survive without this */
8661 }
8662
8663 return 0;
8664}
8665
8666
8667static void nl80211_teardown_ap(struct i802_bss *bss)
8668{
8669 struct wpa_driver_nl80211_data *drv = bss->drv;
8670
748c0ac0
JM
8671 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8672 bss->ifname, drv->device_ap_sme, drv->use_monitor);
a6cc0602 8673 if (drv->device_ap_sme) {
3fd1cefb 8674 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
8675 if (!drv->use_monitor)
8676 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8677 } else if (drv->use_monitor)
3fd1cefb 8678 nl80211_remove_monitor_interface(drv);
a11241fa 8679 else
36488c05 8680 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 8681
3fd1cefb
JB
8682 bss->beacon_set = 0;
8683}
8684
8685
f10bfc9a
JM
8686static int nl80211_send_eapol_data(struct i802_bss *bss,
8687 const u8 *addr, const u8 *data,
d12dab4c 8688 size_t data_len)
f10bfc9a 8689{
d12dab4c
JB
8690 struct sockaddr_ll ll;
8691 int ret;
8692
8693 if (bss->drv->eapol_tx_sock < 0) {
8694 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
8695 return -1;
8696 }
8697
d12dab4c
JB
8698 os_memset(&ll, 0, sizeof(ll));
8699 ll.sll_family = AF_PACKET;
8700 ll.sll_ifindex = bss->ifindex;
8701 ll.sll_protocol = htons(ETH_P_PAE);
8702 ll.sll_halen = ETH_ALEN;
8703 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8704 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8705 (struct sockaddr *) &ll, sizeof(ll));
8706 if (ret < 0)
8707 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8708 strerror(errno));
8709
8710 return ret;
f10bfc9a 8711}
5fb1a232 8712
f10bfc9a 8713
db149ac9
JM
8714static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8715
8716static int wpa_driver_nl80211_hapd_send_eapol(
8717 void *priv, const u8 *addr, const u8 *data,
4378fc14 8718 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 8719{
a2e40bb6
FF
8720 struct i802_bss *bss = priv;
8721 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
8722 struct ieee80211_hdr *hdr;
8723 size_t len;
8724 u8 *pos;
8725 int res;
4378fc14 8726 int qos = flags & WPA_STA_WMM;
db149ac9 8727
a11241fa 8728 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 8729 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 8730
db149ac9
JM
8731 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8732 data_len;
8733 hdr = os_zalloc(len);
8734 if (hdr == NULL) {
7ac3616d
JM
8735 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8736 (unsigned long) len);
db149ac9
JM
8737 return -1;
8738 }
8739
8740 hdr->frame_control =
8741 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8742 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8743 if (encrypt)
8744 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
8745 if (qos) {
8746 hdr->frame_control |=
8747 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8748 }
db149ac9
JM
8749
8750 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8751 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8752 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8753 pos = (u8 *) (hdr + 1);
8754
db149ac9 8755 if (qos) {
92d521d8
FF
8756 /* Set highest priority in QoS header */
8757 pos[0] = 7;
db149ac9
JM
8758 pos[1] = 0;
8759 pos += 2;
8760 }
db149ac9
JM
8761
8762 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8763 pos += sizeof(rfc1042_header);
8764 WPA_PUT_BE16(pos, ETH_P_PAE);
8765 pos += 2;
8766 memcpy(pos, data, data_len);
8767
55231068
JM
8768 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8769 0, 0, 0, 0);
db149ac9
JM
8770 if (res < 0) {
8771 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8772 "failed: %d (%s)",
8773 (unsigned long) len, errno, strerror(errno));
8774 }
7bf12757 8775 os_free(hdr);
db149ac9
JM
8776
8777 return res;
8778}
8779
a8d6ffa4 8780
3234cba4
JM
8781static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8782 int total_flags,
4c32757d 8783 int flags_or, int flags_and)
a8d6ffa4 8784{
a2e40bb6
FF
8785 struct i802_bss *bss = priv;
8786 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
8787 struct nl_msg *msg;
8788 struct nlattr *flags;
7e76ee9c 8789 struct nl80211_sta_flag_update upd;
a8d6ffa4 8790
0cd9846c
JM
8791 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
8792 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
8793 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
8794 !!(total_flags & WPA_STA_AUTHORIZED));
8795
a8d6ffa4
JM
8796 msg = nlmsg_alloc();
8797 if (!msg)
8798 return -ENOMEM;
8799
9fb04070 8800 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
a8d6ffa4
JM
8801
8802 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3234cba4 8803 if_nametoindex(bss->ifname));
a8d6ffa4
JM
8804 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8805
7e76ee9c
JM
8806 /*
8807 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8808 * can be removed eventually.
8809 */
8970bae8
JB
8810 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8811 if (!flags)
8812 goto nla_put_failure;
0de39516 8813 if (total_flags & WPA_STA_AUTHORIZED)
8970bae8 8814 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
a8d6ffa4 8815
0de39516 8816 if (total_flags & WPA_STA_WMM)
8970bae8 8817 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
a8d6ffa4 8818
0de39516 8819 if (total_flags & WPA_STA_SHORT_PREAMBLE)
8970bae8 8820 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
a8d6ffa4 8821
0de39516 8822 if (total_flags & WPA_STA_MFP)
8970bae8 8823 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
a8d6ffa4 8824
45b722f1 8825 if (total_flags & WPA_STA_TDLS_PEER)
8970bae8 8826 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
45b722f1 8827
8970bae8 8828 nla_nest_end(msg, flags);
a8d6ffa4 8829
7e76ee9c
JM
8830 os_memset(&upd, 0, sizeof(upd));
8831 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8832 upd.set = sta_flags_nl80211(flags_or);
8833 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8834
a8d6ffa4
JM
8835 return send_and_recv_msgs(drv, msg, NULL, NULL);
8836 nla_put_failure:
5883168a 8837 nlmsg_free(msg);
a8d6ffa4
JM
8838 return -ENOBUFS;
8839}
8840
0915d02c 8841
1581b38b
JM
8842static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8843 struct wpa_driver_associate_params *params)
8844{
708bc8e0 8845 enum nl80211_iftype nlmode, old_mode;
b1f625e0
EP
8846
8847 if (params->p2p) {
046b26a2
JM
8848 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8849 "group (GO)");
b1f625e0
EP
8850 nlmode = NL80211_IFTYPE_P2P_GO;
8851 } else
8852 nlmode = NL80211_IFTYPE_AP;
8853
708bc8e0 8854 old_mode = drv->nlmode;
834ee56f 8855 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
708bc8e0
JM
8856 nl80211_remove_monitor_interface(drv);
8857 return -1;
8858 }
8859
4ec68377 8860 if (nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
708bc8e0 8861 if (old_mode != nlmode)
834ee56f 8862 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
460456f8 8863 nl80211_remove_monitor_interface(drv);
1581b38b 8864 return -1;
0915d02c 8865 }
1581b38b 8866
1581b38b
JM
8867 return 0;
8868}
1581b38b
JM
8869
8870
5cc4d64b
JM
8871static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8872{
8873 struct nl_msg *msg;
8874 int ret = -1;
8875
8876 msg = nlmsg_alloc();
8877 if (!msg)
8878 return -1;
8879
9fb04070 8880 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b
JM
8881 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8882 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8883 msg = NULL;
8884 if (ret) {
8885 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8886 "(%s)", ret, strerror(-ret));
8887 goto nla_put_failure;
8888 }
8889
8890 ret = 0;
8891 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8892
8893nla_put_failure:
834ee56f 8894 if (wpa_driver_nl80211_set_mode(drv->first_bss,
5d4c78fb
JM
8895 NL80211_IFTYPE_STATION)) {
8896 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8897 "station mode");
8898 }
8899
5cc4d64b
JM
8900 nlmsg_free(msg);
8901 return ret;
8902}
8903
8904
8905static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8906 struct wpa_driver_associate_params *params)
8907{
8908 struct nl_msg *msg;
8909 int ret = -1;
8910 int count = 0;
8911
8912 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8913
4ec68377 8914 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
5cc4d64b
JM
8915 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8916 "IBSS mode");
8917 return -1;
8918 }
8919
8920retry:
8921 msg = nlmsg_alloc();
8922 if (!msg)
8923 return -1;
8924
9fb04070 8925 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5cc4d64b
JM
8926 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8927
8928 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8929 goto nla_put_failure;
8930
8931 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8932 params->ssid, params->ssid_len);
8933 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8934 params->ssid);
8935 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8936 drv->ssid_len = params->ssid_len;
8937
4ec68377
JD
8938 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
8939 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", params->freq.ht_enabled);
8940 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
8941 params->freq.sec_channel_offset);
8942 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", params->freq.vht_enabled);
8943 wpa_printf(MSG_DEBUG, " * center_freq1=%d", params->freq.center_freq1);
8944 wpa_printf(MSG_DEBUG, " * center_freq2=%d", params->freq.center_freq2);
8945 wpa_printf(MSG_DEBUG, " * bandwidth=%d", params->freq.bandwidth);
8946 if (nl80211_put_freq_params(msg, &params->freq) < 0)
8947 goto nla_put_failure;
5cc4d64b 8948
8f05577d
JM
8949 if (params->beacon_int > 0) {
8950 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8951 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8952 params->beacon_int);
8953 }
8954
5cc4d64b
JM
8955 ret = nl80211_set_conn_keys(params, msg);
8956 if (ret)
8957 goto nla_put_failure;
8958
913e3cf7
NC
8959 if (params->bssid && params->fixed_bssid) {
8960 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8961 MAC2STR(params->bssid));
8962 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8963 }
8964
4848a38d
JM
8965 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8966 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8967 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8968 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
e640888c
AQ
8969 wpa_printf(MSG_DEBUG, " * control port");
8970 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8971 }
8972
a95795ad
JM
8973 if (params->wpa_ie) {
8974 wpa_hexdump(MSG_DEBUG,
8975 " * Extra IEs for Beacon/Probe Response frames",
8976 params->wpa_ie, params->wpa_ie_len);
8977 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8978 params->wpa_ie);
8979 }
8980
5cc4d64b
JM
8981 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8982 msg = NULL;
8983 if (ret) {
8984 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8985 ret, strerror(-ret));
8986 count++;
8987 if (ret == -EALREADY && count == 1) {
8988 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8989 "forced leave");
8990 nl80211_leave_ibss(drv);
8991 nlmsg_free(msg);
8992 goto retry;
8993 }
8994
8995 goto nla_put_failure;
8996 }
8997 ret = 0;
8998 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8999
9000nla_put_failure:
9001 nlmsg_free(msg);
9002 return ret;
9003}
9004
9005
a0bdd191
JM
9006static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
9007 struct wpa_driver_associate_params *params,
9008 struct nl_msg *msg)
cfaab580 9009{
cfaab580 9010 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
a0bdd191 9011
cfaab580
ZY
9012 if (params->bssid) {
9013 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
9014 MAC2STR(params->bssid));
9015 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
9016 }
a0bdd191 9017
7ac7fd43
DS
9018 if (params->bssid_hint) {
9019 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
9020 MAC2STR(params->bssid_hint));
9021 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
9022 params->bssid_hint);
9023 }
9024
4ec68377
JD
9025 if (params->freq.freq) {
9026 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
9027 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq.freq);
9028 drv->assoc_freq = params->freq.freq;
30158a0d
SD
9029 } else
9030 drv->assoc_freq = 0;
a0bdd191 9031
7ac7fd43
DS
9032 if (params->freq_hint) {
9033 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
9034 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
9035 params->freq_hint);
9036 }
9037
1f6c0ab8
BS
9038 if (params->bg_scan_period >= 0) {
9039 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
9040 params->bg_scan_period);
9041 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
9042 params->bg_scan_period);
9043 }
a0bdd191 9044
cfaab580
ZY
9045 if (params->ssid) {
9046 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
9047 params->ssid, params->ssid_len);
9048 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
9049 params->ssid);
9050 if (params->ssid_len > sizeof(drv->ssid))
9051 goto nla_put_failure;
9052 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
9053 drv->ssid_len = params->ssid_len;
9054 }
a0bdd191 9055
cfaab580
ZY
9056 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
9057 if (params->wpa_ie)
9058 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
9059 params->wpa_ie);
9060
64fa840a
JM
9061 if (params->wpa_proto) {
9062 enum nl80211_wpa_versions ver = 0;
cfaab580 9063
64fa840a
JM
9064 if (params->wpa_proto & WPA_PROTO_WPA)
9065 ver |= NL80211_WPA_VERSION_1;
9066 if (params->wpa_proto & WPA_PROTO_RSN)
9067 ver |= NL80211_WPA_VERSION_2;
cfaab580 9068
64fa840a 9069 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
cfaab580
ZY
9070 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
9071 }
9072
4848a38d 9073 if (params->pairwise_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
9074 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
9075 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
9076 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
cfaab580
ZY
9077 }
9078
ae6f9272
JM
9079 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
9080 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
9081 /*
9082 * This is likely to work even though many drivers do not
9083 * advertise support for operations without GTK.
9084 */
9085 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
9086 } else if (params->group_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
9087 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
9088 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
9089 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
cfaab580
ZY
9090 }
9091
4848a38d
JM
9092 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
9093 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
9094 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
9095 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
163f801e 9096 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8802326f
JJ
9097 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
9098 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
9099 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
cfaab580
ZY
9100 int mgmt = WLAN_AKM_SUITE_PSK;
9101
9102 switch (params->key_mgmt_suite) {
4848a38d 9103 case WPA_KEY_MGMT_CCKM:
369c8d7b
JM
9104 mgmt = WLAN_AKM_SUITE_CCKM;
9105 break;
4848a38d 9106 case WPA_KEY_MGMT_IEEE8021X:
cfaab580
ZY
9107 mgmt = WLAN_AKM_SUITE_8021X;
9108 break;
4848a38d 9109 case WPA_KEY_MGMT_FT_IEEE8021X:
6a1ce395
DG
9110 mgmt = WLAN_AKM_SUITE_FT_8021X;
9111 break;
4848a38d 9112 case WPA_KEY_MGMT_FT_PSK:
6a1ce395
DG
9113 mgmt = WLAN_AKM_SUITE_FT_PSK;
9114 break;
8802326f
JJ
9115 case WPA_KEY_MGMT_IEEE8021X_SHA256:
9116 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
9117 break;
9118 case WPA_KEY_MGMT_PSK_SHA256:
9119 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
9120 break;
163f801e
JM
9121 case WPA_KEY_MGMT_OSEN:
9122 mgmt = WLAN_AKM_SUITE_OSEN;
9123 break;
4848a38d 9124 case WPA_KEY_MGMT_PSK:
cfaab580
ZY
9125 default:
9126 mgmt = WLAN_AKM_SUITE_PSK;
9127 break;
9128 }
163f801e 9129 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
cfaab580
ZY
9130 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
9131 }
9132
a0bdd191
JM
9133 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
9134
8b706a99
JM
9135 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
9136 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
a565084f 9137
80e8a5ee
BG
9138 if (params->disable_ht)
9139 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
9140
9141 if (params->htcaps && params->htcaps_mask) {
9142 int sz = sizeof(struct ieee80211_ht_capabilities);
6d99bd80 9143 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
80e8a5ee 9144 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6d99bd80
JM
9145 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
9146 params->htcaps_mask, sz);
80e8a5ee
BG
9147 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
9148 params->htcaps_mask);
9149 }
9150
e9ee8dc3
JB
9151#ifdef CONFIG_VHT_OVERRIDES
9152 if (params->disable_vht) {
9153 wpa_printf(MSG_DEBUG, " * VHT disabled");
9154 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
9155 }
9156
9157 if (params->vhtcaps && params->vhtcaps_mask) {
9158 int sz = sizeof(struct ieee80211_vht_capabilities);
6d99bd80 9159 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
e9ee8dc3 9160 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
6d99bd80
JM
9161 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
9162 params->vhtcaps_mask, sz);
e9ee8dc3
JB
9163 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
9164 params->vhtcaps_mask);
9165 }
9166#endif /* CONFIG_VHT_OVERRIDES */
9167
a0bdd191
JM
9168 if (params->p2p)
9169 wpa_printf(MSG_DEBUG, " * P2P group");
9170
9171 return 0;
9172nla_put_failure:
9173 return -1;
9174}
9175
9176
9177static int wpa_driver_nl80211_try_connect(
9178 struct wpa_driver_nl80211_data *drv,
9179 struct wpa_driver_associate_params *params)
9180{
9181 struct nl_msg *msg;
9182 enum nl80211_auth_type type;
9183 int ret;
9184 int algs;
9185
b41f2684
CL
9186 if (params->req_key_mgmt_offload && params->psk &&
9187 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
9188 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
9189 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
9190 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
9191 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
9192 if (ret)
9193 return ret;
9194 }
9195
a0bdd191
JM
9196 msg = nlmsg_alloc();
9197 if (!msg)
9198 return -1;
9199
9200 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9201 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
9202
9203 ret = nl80211_connect_common(drv, params, msg);
9204 if (ret)
9205 goto nla_put_failure;
9206
9207 algs = 0;
9208 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9209 algs++;
9210 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9211 algs++;
9212 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9213 algs++;
9214 if (algs > 1) {
9215 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
9216 "selection");
9217 goto skip_auth_type;
9218 }
9219
9220 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9221 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
9222 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9223 type = NL80211_AUTHTYPE_SHARED_KEY;
9224 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9225 type = NL80211_AUTHTYPE_NETWORK_EAP;
9226 else if (params->auth_alg & WPA_AUTH_ALG_FT)
9227 type = NL80211_AUTHTYPE_FT;
9228 else
9229 goto nla_put_failure;
9230
9231 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
9232 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
9233
9234skip_auth_type:
cfaab580
ZY
9235 ret = nl80211_set_conn_keys(params, msg);
9236 if (ret)
9237 goto nla_put_failure;
9238
9239 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9240 msg = NULL;
9241 if (ret) {
9242 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
9243 "(%s)", ret, strerror(-ret));
9244 goto nla_put_failure;
9245 }
9246 ret = 0;
9247 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
9248
9249nla_put_failure:
9250 nlmsg_free(msg);
9251 return ret;
9252
9253}
9254
9255
a8c5b43a
CW
9256static int wpa_driver_nl80211_connect(
9257 struct wpa_driver_nl80211_data *drv,
9258 struct wpa_driver_associate_params *params)
9259{
9260 int ret = wpa_driver_nl80211_try_connect(drv, params);
9261 if (ret == -EALREADY) {
9262 /*
9263 * cfg80211 does not currently accept new connections if
9264 * we are already connected. As a workaround, force
9265 * disconnection and try again.
9266 */
9267 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
9268 "disconnecting before reassociation "
9269 "attempt");
9270 if (wpa_driver_nl80211_disconnect(
9271 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
9272 return -1;
a8c5b43a
CW
9273 ret = wpa_driver_nl80211_try_connect(drv, params);
9274 }
9275 return ret;
9276}
9277
9278
c2a04078
JM
9279static int wpa_driver_nl80211_associate(
9280 void *priv, struct wpa_driver_associate_params *params)
9281{
a2e40bb6
FF
9282 struct i802_bss *bss = priv;
9283 struct wpa_driver_nl80211_data *drv = bss->drv;
a0bdd191 9284 int ret;
c2a04078
JM
9285 struct nl_msg *msg;
9286
5cc4d64b 9287 if (params->mode == IEEE80211_MODE_AP)
1581b38b 9288 return wpa_driver_nl80211_ap(drv, params);
1581b38b 9289
5cc4d64b
JM
9290 if (params->mode == IEEE80211_MODE_IBSS)
9291 return wpa_driver_nl80211_ibss(drv, params);
9292
4a867032 9293 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
9294 enum nl80211_iftype nlmode = params->p2p ?
9295 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
9296
9297 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 9298 return -1;
cfaab580 9299 return wpa_driver_nl80211_connect(drv, params);
4a867032 9300 }
cfaab580 9301
add9b7a4 9302 nl80211_mark_disconnected(drv);
c2a04078
JM
9303
9304 msg = nlmsg_alloc();
9305 if (!msg)
9306 return -1;
9307
9308 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
9309 drv->ifindex);
9fb04070 9310 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
c2a04078 9311
a0bdd191
JM
9312 ret = nl80211_connect_common(drv, params, msg);
9313 if (ret)
9314 goto nla_put_failure;
01652550 9315
62fa124c
JM
9316 if (params->prev_bssid) {
9317 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
9318 MAC2STR(params->prev_bssid));
9319 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
9320 params->prev_bssid);
9321 }
9322
c2a04078
JM
9323 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9324 msg = NULL;
9325 if (ret) {
3b7ea880
BG
9326 wpa_dbg(drv->ctx, MSG_DEBUG,
9327 "nl80211: MLME command failed (assoc): ret=%d (%s)",
9328 ret, strerror(-ret));
8856462d 9329 nl80211_dump_scan(drv);
c2a04078
JM
9330 goto nla_put_failure;
9331 }
9332 ret = 0;
9333 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
9334 "successfully");
9335
9336nla_put_failure:
9337 nlmsg_free(msg);
9338 return ret;
9339}
3f5285e8
JM
9340
9341
ad1e68e6 9342static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 9343 int ifindex, enum nl80211_iftype mode)
3f5285e8 9344{
3f5285e8 9345 struct nl_msg *msg;
ad1e68e6
JM
9346 int ret = -ENOBUFS;
9347
a1922f93
JM
9348 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
9349 ifindex, mode, nl80211_iftype_str(mode));
9350
ad1e68e6
JM
9351 msg = nlmsg_alloc();
9352 if (!msg)
9353 return -ENOMEM;
9354
9fb04070 9355 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
834ee56f 9356 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
f632e483 9357 goto nla_put_failure;
ad1e68e6
JM
9358 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
9359
9360 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 9361 msg = NULL;
ad1e68e6
JM
9362 if (!ret)
9363 return 0;
9364nla_put_failure:
5883168a 9365 nlmsg_free(msg);
ad1e68e6
JM
9366 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
9367 " %d (%s)", ifindex, mode, ret, strerror(-ret));
9368 return ret;
9369}
9370
9371
3c5d34e3
CW
9372static int wpa_driver_nl80211_set_mode_impl(
9373 struct i802_bss *bss,
9374 enum nl80211_iftype nlmode,
9375 struct hostapd_freq_params *desired_freq_params)
ad1e68e6 9376{
a2e40bb6 9377 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 9378 int ret = -1;
26af9dca 9379 int i;
86957e62 9380 int was_ap = is_ap_interface(drv->nlmode);
671a5039 9381 int res;
ebffdbc4 9382 int mode_switch_res;
1581b38b 9383
ebffdbc4
CW
9384 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9385 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
9386 mode_switch_res = 0;
8e12685c 9387
ebffdbc4 9388 if (mode_switch_res == 0) {
ad1e68e6 9389 drv->nlmode = nlmode;
460456f8
JM
9390 ret = 0;
9391 goto done;
ad1e68e6 9392 }
3f5285e8 9393
ebffdbc4 9394 if (mode_switch_res == -ENODEV)
671a5039
JM
9395 return -1;
9396
460456f8 9397 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
9398 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
9399 "requested mode - ignore error");
460456f8
JM
9400 ret = 0;
9401 goto done; /* Already in the requested mode */
9402 }
3f5285e8 9403
3f5285e8
JM
9404 /* mac80211 doesn't allow mode changes while the device is up, so
9405 * take the device down, try to set the mode again, and bring the
9406 * device back up.
9407 */
26af9dca
JM
9408 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9409 "interface down");
9410 for (i = 0; i < 10; i++) {
91724d6f 9411 res = i802_set_iface_flags(bss, 0);
6e8183d7
JM
9412 if (res == -EACCES || res == -ENODEV)
9413 break;
ebffdbc4 9414 if (res != 0) {
26af9dca
JM
9415 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9416 "interface down");
ebffdbc4
CW
9417 os_sleep(0, 100000);
9418 continue;
9419 }
3c5d34e3
CW
9420
9421 /*
9422 * Setting the mode will fail for some drivers if the phy is
9423 * on a frequency that the mode is disallowed in.
9424 */
9425 if (desired_freq_params) {
9426 res = i802_set_freq(bss, desired_freq_params);
9427 if (res) {
9428 wpa_printf(MSG_DEBUG,
9429 "nl80211: Failed to set frequency on interface");
9430 }
9431 }
9432
ebffdbc4
CW
9433 /* Try to set the mode again while the interface is down */
9434 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9435 if (mode_switch_res == -EBUSY) {
9436 wpa_printf(MSG_DEBUG,
9437 "nl80211: Delaying mode set while interface going down");
9438 os_sleep(0, 100000);
9439 continue;
9440 }
9441 ret = mode_switch_res;
9442 break;
3f5285e8
JM
9443 }
9444
c6e8e8e4
JM
9445 if (!ret) {
9446 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9447 "interface is down");
ad1e68e6 9448 drv->nlmode = nlmode;
7d9c3698 9449 drv->ignore_if_down_event = 1;
c6e8e8e4 9450 }
ad1e68e6 9451
ebffdbc4
CW
9452 /* Bring the interface back up */
9453 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
9454 if (res != 0) {
9455 wpa_printf(MSG_DEBUG,
9456 "nl80211: Failed to set interface up after switching mode");
9457 ret = -1;
9458 }
9459
460456f8 9460done:
3fd1cefb
JB
9461 if (ret) {
9462 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9463 "from %d failed", nlmode, drv->nlmode);
9464 return ret;
9465 }
9466
6a71413e 9467 if (is_p2p_net_interface(nlmode))
edb9bfba 9468 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1d0c6fb1
JM
9469 else if (drv->disabled_11b_rates)
9470 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
edb9bfba 9471
3fd1cefb 9472 if (is_ap_interface(nlmode)) {
36488c05 9473 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 9474 /* Setup additional AP mode functionality if needed */
3fd1cefb 9475 if (nl80211_setup_ap(bss))
460456f8 9476 return -1;
3fd1cefb 9477 } else if (was_ap) {
460456f8 9478 /* Remove additional AP mode functionality */
3fd1cefb 9479 nl80211_teardown_ap(bss);
a11241fa 9480 } else {
36488c05 9481 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 9482 }
460456f8 9483
873d0fcf 9484 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
a11241fa
JB
9485 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9486 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9487 "frame processing - ignore for now");
08359050 9488
3fd1cefb 9489 return 0;
3f5285e8
JM
9490}
9491
9492
65d645ce
AS
9493static int dfs_info_handler(struct nl_msg *msg, void *arg)
9494{
9495 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9496 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9497 int *dfs_capability_ptr = arg;
9498
9499 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9500 genlmsg_attrlen(gnlh, 0), NULL);
9501
9502 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9503 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9504 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9505
9506 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9507 nla_data(nl_vend), nla_len(nl_vend), NULL);
9508
9509 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9510 u32 val;
9511 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9512 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9513 val);
9514 *dfs_capability_ptr = val;
9515 }
9516 }
9517
9518 return NL_SKIP;
9519}
3c5d34e3
CW
9520
9521
9522static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
9523 enum nl80211_iftype nlmode)
9524{
9525 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
9526}
9527
9528
4ec68377
JD
9529static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
9530 struct hostapd_freq_params *freq)
3c5d34e3 9531{
3c5d34e3 9532 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
4ec68377 9533 freq);
3c5d34e3 9534}
65d645ce
AS
9535
9536
3f5285e8
JM
9537static int wpa_driver_nl80211_get_capa(void *priv,
9538 struct wpa_driver_capa *capa)
9539{
a2e40bb6
FF
9540 struct i802_bss *bss = priv;
9541 struct wpa_driver_nl80211_data *drv = bss->drv;
65d645ce
AS
9542 struct nl_msg *msg;
9543 int dfs_capability = 0;
9544 int ret = 0;
9545
3f5285e8
JM
9546 if (!drv->has_capability)
9547 return -1;
9548 os_memcpy(capa, &drv->capa, sizeof(*capa));
8cd6b7bc
JB
9549 if (drv->extended_capa && drv->extended_capa_mask) {
9550 capa->extended_capa = drv->extended_capa;
9551 capa->extended_capa_mask = drv->extended_capa_mask;
9552 capa->extended_capa_len = drv->extended_capa_len;
9553 }
851b0c55 9554
65d645ce
AS
9555 if (drv->dfs_vendor_cmd_avail == 1) {
9556 msg = nlmsg_alloc();
9557 if (!msg)
9558 return -ENOMEM;
9559
9560 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9561
9562 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9563 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9564 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9565 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9566
9567 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9568 &dfs_capability);
9569 if (!ret) {
9570 if (dfs_capability)
9571 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9572 }
9573 }
9574
9575 return ret;
9576
9577 nla_put_failure:
9578 nlmsg_free(msg);
9579 return -ENOBUFS;
3f5285e8
JM
9580}
9581
9582
9583static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9584{
a2e40bb6
FF
9585 struct i802_bss *bss = priv;
9586 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 9587
90a545cc
JM
9588 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9589 bss->ifname, drv->operstate, state,
9590 state ? "UP" : "DORMANT");
3f5285e8 9591 drv->operstate = state;
36d84860 9592 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 9593 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
9594}
9595
01652550
JM
9596
9597static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9598{
a2e40bb6
FF
9599 struct i802_bss *bss = priv;
9600 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
9601 struct nl_msg *msg;
9602 struct nl80211_sta_flag_update upd;
2eef5177
JM
9603 int ret = -ENOBUFS;
9604
9605 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9606 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9607 return 0;
9608 }
01652550 9609
1ba51ec0
JM
9610 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9611 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9612
01652550
JM
9613 msg = nlmsg_alloc();
9614 if (!msg)
9615 return -ENOMEM;
9616
9fb04070 9617 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
01652550
JM
9618
9619 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 9620 if_nametoindex(bss->ifname));
01652550
JM
9621 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9622
9623 os_memset(&upd, 0, sizeof(upd));
9624 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9625 if (authorized)
9626 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9627 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9628
2eef5177
JM
9629 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9630 msg = NULL;
9631 if (!ret)
9632 return 0;
01652550 9633 nla_put_failure:
5883168a 9634 nlmsg_free(msg);
2eef5177
JM
9635 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9636 ret, strerror(-ret));
9637 return ret;
01652550
JM
9638}
9639
3f5285e8 9640
f07ead6a
JM
9641/* Set kernel driver on given frequency (MHz) */
9642static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 9643{
f07ead6a 9644 struct i802_bss *bss = priv;
13a524a3 9645 return nl80211_set_channel(bss, freq, 0);
c5121837
JM
9646}
9647
f7b3920c 9648
c5121837
JM
9649static inline int min_int(int a, int b)
9650{
9651 if (a < b)
9652 return a;
9653 return b;
9654}
9655
9656
9657static int get_key_handler(struct nl_msg *msg, void *arg)
9658{
9659 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9660 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9661
9662 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9663 genlmsg_attrlen(gnlh, 0), NULL);
9664
9665 /*
9666 * TODO: validate the key index and mac address!
9667 * Otherwise, there's a race condition as soon as
9668 * the kernel starts sending key notifications.
9669 */
9670
9671 if (tb[NL80211_ATTR_KEY_SEQ])
9672 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9673 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9674 return NL_SKIP;
9675}
9676
9677
9678static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9679 int idx, u8 *seq)
9680{
a2e40bb6
FF
9681 struct i802_bss *bss = priv;
9682 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
9683 struct nl_msg *msg;
9684
9685 msg = nlmsg_alloc();
9686 if (!msg)
9687 return -ENOMEM;
9688
9fb04070 9689 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
c5121837
JM
9690
9691 if (addr)
9692 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9693 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9694 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9695
9696 memset(seq, 0, 6);
9697
9698 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9699 nla_put_failure:
9e088e74 9700 nlmsg_free(msg);
c5121837
JM
9701 return -ENOBUFS;
9702}
9703
9704
c5121837
JM
9705static int i802_set_rts(void *priv, int rts)
9706{
a2e40bb6
FF
9707 struct i802_bss *bss = priv;
9708 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
9709 struct nl_msg *msg;
9710 int ret = -ENOBUFS;
9711 u32 val;
c5121837 9712
ad649451
JM
9713 msg = nlmsg_alloc();
9714 if (!msg)
9715 return -ENOMEM;
c5121837 9716
ad649451
JM
9717 if (rts >= 2347)
9718 val = (u32) -1;
9719 else
9720 val = rts;
c5121837 9721
9fb04070 9722 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
9723 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9724 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9725
9726 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 9727 msg = NULL;
ad649451
JM
9728 if (!ret)
9729 return 0;
9730nla_put_failure:
5883168a 9731 nlmsg_free(msg);
ad649451
JM
9732 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9733 "%d (%s)", rts, ret, strerror(-ret));
9734 return ret;
c5121837
JM
9735}
9736
9737
9738static int i802_set_frag(void *priv, int frag)
9739{
a2e40bb6
FF
9740 struct i802_bss *bss = priv;
9741 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
9742 struct nl_msg *msg;
9743 int ret = -ENOBUFS;
9744 u32 val;
c5121837 9745
ad649451
JM
9746 msg = nlmsg_alloc();
9747 if (!msg)
9748 return -ENOMEM;
c5121837 9749
ad649451
JM
9750 if (frag >= 2346)
9751 val = (u32) -1;
9752 else
9753 val = frag;
c5121837 9754
9fb04070 9755 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
9756 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9757 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9758
9759 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 9760 msg = NULL;
ad649451
JM
9761 if (!ret)
9762 return 0;
9763nla_put_failure:
5883168a 9764 nlmsg_free(msg);
ad649451
JM
9765 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9766 "%d: %d (%s)", frag, ret, strerror(-ret));
9767 return ret;
c5121837
JM
9768}
9769
9770
c5121837
JM
9771static int i802_flush(void *priv)
9772{
a2e40bb6
FF
9773 struct i802_bss *bss = priv;
9774 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 9775 struct nl_msg *msg;
82554b10 9776 int res;
c5121837
JM
9777
9778 msg = nlmsg_alloc();
9779 if (!msg)
9780 return -1;
9781
83e7bb0e
JM
9782 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9783 bss->ifname);
9fb04070 9784 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
c5121837
JM
9785
9786 /*
9787 * XXX: FIX! this needs to flush all VLANs too
9788 */
9789 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 9790 if_nametoindex(bss->ifname));
c5121837 9791
82554b10
JM
9792 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9793 if (res) {
9794 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9795 "(%s)", res, strerror(-res));
9796 }
9797 return res;
c5121837 9798 nla_put_failure:
9e088e74 9799 nlmsg_free(msg);
c5121837
JM
9800 return -ENOBUFS;
9801}
9802
9803
9804static int get_sta_handler(struct nl_msg *msg, void *arg)
9805{
9806 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9807 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9808 struct hostap_sta_driver_data *data = arg;
9809 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9810 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9811 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9812 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9813 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9814 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9815 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 9816 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
c5121837
JM
9817 };
9818
9819 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9820 genlmsg_attrlen(gnlh, 0), NULL);
9821
9822 /*
9823 * TODO: validate the interface and mac address!
9824 * Otherwise, there's a race condition as soon as
9825 * the kernel starts sending station notifications.
9826 */
9827
9828 if (!tb[NL80211_ATTR_STA_INFO]) {
9829 wpa_printf(MSG_DEBUG, "sta stats missing!");
9830 return NL_SKIP;
9831 }
9832 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9833 tb[NL80211_ATTR_STA_INFO],
9834 stats_policy)) {
9835 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9836 return NL_SKIP;
9837 }
9838
9839 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9840 data->inactive_msec =
9841 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9842 if (stats[NL80211_STA_INFO_RX_BYTES])
9843 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9844 if (stats[NL80211_STA_INFO_TX_BYTES])
9845 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9846 if (stats[NL80211_STA_INFO_RX_PACKETS])
9847 data->rx_packets =
9848 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9849 if (stats[NL80211_STA_INFO_TX_PACKETS])
9850 data->tx_packets =
9851 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
9852 if (stats[NL80211_STA_INFO_TX_FAILED])
9853 data->tx_retry_failed =
9854 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
c5121837
JM
9855
9856 return NL_SKIP;
9857}
9858
9ebce9c5
JM
9859static int i802_read_sta_data(struct i802_bss *bss,
9860 struct hostap_sta_driver_data *data,
c5121837
JM
9861 const u8 *addr)
9862{
a2e40bb6 9863 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
9864 struct nl_msg *msg;
9865
9866 os_memset(data, 0, sizeof(*data));
9867 msg = nlmsg_alloc();
9868 if (!msg)
9869 return -ENOMEM;
9870
9fb04070 9871 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
c5121837
JM
9872
9873 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
a2e40bb6 9874 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
9875
9876 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9877 nla_put_failure:
9e088e74 9878 nlmsg_free(msg);
c5121837
JM
9879 return -ENOBUFS;
9880}
9881
9882
c5121837
JM
9883static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9884 int cw_min, int cw_max, int burst_time)
9885{
a2e40bb6
FF
9886 struct i802_bss *bss = priv;
9887 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
9888 struct nl_msg *msg;
9889 struct nlattr *txq, *params;
9890
9891 msg = nlmsg_alloc();
9892 if (!msg)
9893 return -1;
9894
9fb04070 9895 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
c5121837 9896
a2e40bb6 9897 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
9898
9899 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9900 if (!txq)
9901 goto nla_put_failure;
9902
9903 /* We are only sending parameters for a single TXQ at a time */
9904 params = nla_nest_start(msg, 1);
9905 if (!params)
9906 goto nla_put_failure;
9907
7e3c1781
JM
9908 switch (queue) {
9909 case 0:
9910 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9911 break;
9912 case 1:
9913 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9914 break;
9915 case 2:
9916 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9917 break;
9918 case 3:
9919 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9920 break;
9921 }
c5121837
JM
9922 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9923 * 32 usec, so need to convert the value here. */
9924 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9925 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9926 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9927 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9928
9929 nla_nest_end(msg, params);
9930
9931 nla_nest_end(msg, txq);
9932
9933 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9934 return 0;
9e088e74 9935 msg = NULL;
c5121837 9936 nla_put_failure:
9e088e74 9937 nlmsg_free(msg);
c5121837
JM
9938 return -1;
9939}
9940
9941
9ebce9c5 9942static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
9943 const char *ifname, int vlan_id)
9944{
a2e40bb6 9945 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 9946 struct nl_msg *msg;
cd1d72c1 9947 int ret = -ENOBUFS;
c5121837
JM
9948
9949 msg = nlmsg_alloc();
9950 if (!msg)
9951 return -ENOMEM;
9952
bbc706a3
JM
9953 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9954 ", ifname=%s[%d], vlan_id=%d)",
9955 bss->ifname, if_nametoindex(bss->ifname),
9956 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
9fb04070 9957 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
c5121837
JM
9958
9959 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 9960 if_nametoindex(bss->ifname));
c5121837 9961 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1c766b09 9962 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
c5121837
JM
9963 if_nametoindex(ifname));
9964
cd1d72c1 9965 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9e088e74 9966 msg = NULL;
cd1d72c1
JM
9967 if (ret < 0) {
9968 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9969 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9970 MAC2STR(addr), ifname, vlan_id, ret,
9971 strerror(-ret));
9972 }
c5121837 9973 nla_put_failure:
9e088e74 9974 nlmsg_free(msg);
cd1d72c1 9975 return ret;
c5121837
JM
9976}
9977
fbbfcbac 9978
c5121837
JM
9979static int i802_get_inact_sec(void *priv, const u8 *addr)
9980{
9981 struct hostap_sta_driver_data data;
9982 int ret;
9983
9984 data.inactive_msec = (unsigned long) -1;
9985 ret = i802_read_sta_data(priv, &data, addr);
9986 if (ret || data.inactive_msec == (unsigned long) -1)
9987 return -1;
9988 return data.inactive_msec / 1000;
9989}
9990
9991
9992static int i802_sta_clear_stats(void *priv, const u8 *addr)
9993{
9994#if 0
9995 /* TODO */
9996#endif
9997 return 0;
9998}
9999
10000
731723a5
JM
10001static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
10002 int reason)
c5121837 10003{
a2e40bb6 10004 struct i802_bss *bss = priv;
e1bd4e19 10005 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
10006 struct ieee80211_mgmt mgmt;
10007
e1bd4e19 10008 if (drv->device_ap_sme)
59d7148a 10009 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
e1bd4e19 10010
c5121837
JM
10011 memset(&mgmt, 0, sizeof(mgmt));
10012 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
10013 WLAN_FC_STYPE_DEAUTH);
10014 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
10015 memcpy(mgmt.sa, own_addr, ETH_ALEN);
10016 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 10017 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 10018 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 10019 IEEE80211_HDRLEN +
9ebce9c5
JM
10020 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
10021 0);
c5121837
JM
10022}
10023
10024
731723a5
JM
10025static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
10026 int reason)
c5121837 10027{
a2e40bb6 10028 struct i802_bss *bss = priv;
e1bd4e19 10029 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
10030 struct ieee80211_mgmt mgmt;
10031
e1bd4e19 10032 if (drv->device_ap_sme)
59d7148a 10033 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
e1bd4e19 10034
c5121837
JM
10035 memset(&mgmt, 0, sizeof(mgmt));
10036 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
10037 WLAN_FC_STYPE_DISASSOC);
10038 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
10039 memcpy(mgmt.sa, own_addr, ETH_ALEN);
10040 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 10041 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 10042 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 10043 IEEE80211_HDRLEN +
9ebce9c5
JM
10044 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
10045 0);
c5121837
JM
10046}
10047
10048
9b4d9c8b
JM
10049static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
10050{
10051 char buf[200], *pos, *end;
10052 int i, res;
10053
10054 pos = buf;
10055 end = pos + sizeof(buf);
10056
10057 for (i = 0; i < drv->num_if_indices; i++) {
10058 if (!drv->if_indices[i])
10059 continue;
10060 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
10061 if (res < 0 || res >= end - pos)
10062 break;
10063 pos += res;
10064 }
10065 *pos = '\0';
10066
10067 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
10068 drv->num_if_indices, buf);
10069}
10070
10071
f07ead6a
JM
10072static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
10073{
10074 int i;
10075 int *old;
10076
10077 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
10078 ifidx);
b36935be
MB
10079 if (have_ifidx(drv, ifidx)) {
10080 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
10081 ifidx);
10082 return;
10083 }
f07ead6a
JM
10084 for (i = 0; i < drv->num_if_indices; i++) {
10085 if (drv->if_indices[i] == 0) {
10086 drv->if_indices[i] = ifidx;
9b4d9c8b 10087 dump_ifidx(drv);
f07ead6a
JM
10088 return;
10089 }
10090 }
10091
10092 if (drv->if_indices != drv->default_if_indices)
10093 old = drv->if_indices;
10094 else
10095 old = NULL;
10096
067ffa26
JM
10097 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
10098 sizeof(int));
f07ead6a
JM
10099 if (!drv->if_indices) {
10100 if (!old)
10101 drv->if_indices = drv->default_if_indices;
10102 else
10103 drv->if_indices = old;
10104 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
10105 "interfaces");
10106 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
10107 return;
10108 } else if (!old)
10109 os_memcpy(drv->if_indices, drv->default_if_indices,
10110 sizeof(drv->default_if_indices));
10111 drv->if_indices[drv->num_if_indices] = ifidx;
10112 drv->num_if_indices++;
9b4d9c8b 10113 dump_ifidx(drv);
f07ead6a
JM
10114}
10115
10116
10117static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
10118{
10119 int i;
10120
10121 for (i = 0; i < drv->num_if_indices; i++) {
10122 if (drv->if_indices[i] == ifidx) {
10123 drv->if_indices[i] = 0;
10124 break;
10125 }
10126 }
9b4d9c8b 10127 dump_ifidx(drv);
f07ead6a
JM
10128}
10129
10130
10131static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
10132{
10133 int i;
10134
10135 for (i = 0; i < drv->num_if_indices; i++)
10136 if (drv->if_indices[i] == ifidx)
10137 return 1;
10138
10139 return 0;
10140}
10141
10142
10143static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
0e80ea2c 10144 const char *bridge_ifname, char *ifname_wds)
f07ead6a
JM
10145{
10146 struct i802_bss *bss = priv;
10147 struct wpa_driver_nl80211_data *drv = bss->drv;
10148 char name[IFNAMSIZ + 1];
10149
10150 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
69dd2967
SM
10151 if (ifname_wds)
10152 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
10153
f07ead6a
JM
10154 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
10155 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
10156 if (val) {
10157 if (!if_nametoindex(name)) {
10158 if (nl80211_create_iface(drv, name,
10159 NL80211_IFTYPE_AP_VLAN,
2aec4f3c
JM
10160 bss->addr, 1, NULL, NULL, 0) <
10161 0)
f07ead6a
JM
10162 return -1;
10163 if (bridge_ifname &&
c81eff1a
BG
10164 linux_br_add_if(drv->global->ioctl_sock,
10165 bridge_ifname, name) < 0)
f07ead6a
JM
10166 return -1;
10167 }
75227f3a
JM
10168 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
10169 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
10170 "interface %s up", name);
10171 }
f07ead6a
JM
10172 return i802_set_sta_vlan(priv, addr, name, 0);
10173 } else {
c34e618d
FF
10174 if (bridge_ifname)
10175 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
10176 name);
10177
f07ead6a 10178 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
d0595b25
FF
10179 nl80211_remove_iface(drv, if_nametoindex(name));
10180 return 0;
f07ead6a
JM
10181 }
10182}
10183
10184
10185static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
10186{
10187 struct wpa_driver_nl80211_data *drv = eloop_ctx;
10188 struct sockaddr_ll lladdr;
10189 unsigned char buf[3000];
10190 int len;
10191 socklen_t fromlen = sizeof(lladdr);
10192
10193 len = recvfrom(sock, buf, sizeof(buf), 0,
10194 (struct sockaddr *)&lladdr, &fromlen);
10195 if (len < 0) {
7ac3616d
JM
10196 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
10197 strerror(errno));
f07ead6a
JM
10198 return;
10199 }
10200
10201 if (have_ifidx(drv, lladdr.sll_ifindex))
10202 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
10203}
10204
10205
94627f6c 10206static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 10207 struct i802_bss *bss,
94627f6c
JM
10208 const char *brname, const char *ifname)
10209{
10210 int ifindex;
10211 char in_br[IFNAMSIZ];
10212
e17a2477 10213 os_strlcpy(bss->brname, brname, IFNAMSIZ);
94627f6c
JM
10214 ifindex = if_nametoindex(brname);
10215 if (ifindex == 0) {
10216 /*
10217 * Bridge was configured, but the bridge device does
10218 * not exist. Try to add it now.
10219 */
c81eff1a 10220 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
10221 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
10222 "bridge interface %s: %s",
10223 brname, strerror(errno));
10224 return -1;
10225 }
e17a2477 10226 bss->added_bridge = 1;
94627f6c
JM
10227 add_ifidx(drv, if_nametoindex(brname));
10228 }
10229
10230 if (linux_br_get(in_br, ifname) == 0) {
10231 if (os_strcmp(in_br, brname) == 0)
10232 return 0; /* already in the bridge */
10233
10234 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
10235 "bridge %s", ifname, in_br);
c81eff1a
BG
10236 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
10237 0) {
94627f6c
JM
10238 wpa_printf(MSG_ERROR, "nl80211: Failed to "
10239 "remove interface %s from bridge "
10240 "%s: %s",
10241 ifname, brname, strerror(errno));
10242 return -1;
10243 }
10244 }
10245
10246 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
10247 ifname, brname);
c81eff1a 10248 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
10249 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
10250 "into bridge %s: %s",
10251 ifname, brname, strerror(errno));
10252 return -1;
10253 }
e17a2477 10254 bss->added_if_into_bridge = 1;
94627f6c
JM
10255
10256 return 0;
10257}
10258
10259
92f475b4
JM
10260static void *i802_init(struct hostapd_data *hapd,
10261 struct wpa_init_params *params)
c5121837
JM
10262{
10263 struct wpa_driver_nl80211_data *drv;
a2e40bb6 10264 struct i802_bss *bss;
c5121837 10265 size_t i;
94627f6c
JM
10266 char brname[IFNAMSIZ];
10267 int ifindex, br_ifindex;
10268 int br_added = 0;
c5121837 10269
0d547d5f
JM
10270 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
10271 params->global_priv, 1,
10272 params->bssid);
a2e40bb6 10273 if (bss == NULL)
c5121837 10274 return NULL;
c5121837 10275
a2e40bb6 10276 drv = bss->drv;
7635bfb0 10277
94627f6c
JM
10278 if (linux_br_get(brname, params->ifname) == 0) {
10279 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
10280 params->ifname, brname);
10281 br_ifindex = if_nametoindex(brname);
10282 } else {
10283 brname[0] = '\0';
10284 br_ifindex = 0;
10285 }
10286
92f475b4 10287 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
10288 if (params->bridge[i]) {
10289 ifindex = if_nametoindex(params->bridge[i]);
10290 if (ifindex)
10291 add_ifidx(drv, ifindex);
10292 if (ifindex == br_ifindex)
10293 br_added = 1;
10294 }
c5121837 10295 }
94627f6c
JM
10296 if (!br_added && br_ifindex &&
10297 (params->num_bridge == 0 || !params->bridge[0]))
10298 add_ifidx(drv, br_ifindex);
c5121837 10299
ad1e68e6
JM
10300 /* start listening for EAPOL on the default AP interface */
10301 add_ifidx(drv, drv->ifindex);
10302
94627f6c 10303 if (params->num_bridge && params->bridge[0] &&
e17a2477 10304 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
94627f6c
JM
10305 goto failed;
10306
97ed9a06
KP
10307#ifdef CONFIG_LIBNL3_ROUTE
10308 if (bss->added_if_into_bridge) {
10309 drv->rtnl_sk = nl_socket_alloc();
10310 if (drv->rtnl_sk == NULL) {
10311 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
10312 goto failed;
10313 }
10314
10315 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
10316 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
10317 strerror(errno));
10318 goto failed;
10319 }
10320 }
10321#endif /* CONFIG_LIBNL3_ROUTE */
10322
ad1e68e6
JM
10323 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
10324 if (drv->eapol_sock < 0) {
7ac3616d
JM
10325 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
10326 strerror(errno));
bbaf0837 10327 goto failed;
ad1e68e6
JM
10328 }
10329
10330 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
10331 {
7ac3616d 10332 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
c5121837 10333 goto failed;
ad1e68e6
JM
10334 }
10335
c81eff1a
BG
10336 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
10337 params->own_addr))
bbaf0837 10338 goto failed;
fee354c7 10339 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
c5121837 10340
341eebee
JB
10341 memcpy(bss->addr, params->own_addr, ETH_ALEN);
10342
a2e40bb6 10343 return bss;
c5121837
JM
10344
10345failed:
7635bfb0 10346 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
10347 return NULL;
10348}
c5121837 10349
c5121837 10350
bbaf0837
JM
10351static void i802_deinit(void *priv)
10352{
9ebce9c5
JM
10353 struct i802_bss *bss = priv;
10354 wpa_driver_nl80211_deinit(bss);
c5121837
JM
10355}
10356
c5121837 10357
22a7c9d7
JM
10358static enum nl80211_iftype wpa_driver_nl80211_if_type(
10359 enum wpa_driver_if_type type)
10360{
10361 switch (type) {
10362 case WPA_IF_STATION:
9f51b113 10363 return NL80211_IFTYPE_STATION;
75bde05d
JM
10364 case WPA_IF_P2P_CLIENT:
10365 case WPA_IF_P2P_GROUP:
9f51b113 10366 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
10367 case WPA_IF_AP_VLAN:
10368 return NL80211_IFTYPE_AP_VLAN;
10369 case WPA_IF_AP_BSS:
10370 return NL80211_IFTYPE_AP;
9f51b113
JB
10371 case WPA_IF_P2P_GO:
10372 return NL80211_IFTYPE_P2P_GO;
7aad838c
NS
10373 case WPA_IF_P2P_DEVICE:
10374 return NL80211_IFTYPE_P2P_DEVICE;
22a7c9d7
JM
10375 }
10376 return -1;
10377}
10378
10379
482856c8
JM
10380#ifdef CONFIG_P2P
10381
f2ed8023
JM
10382static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
10383{
10384 struct wpa_driver_nl80211_data *drv;
10385 dl_list_for_each(drv, &global->interfaces,
10386 struct wpa_driver_nl80211_data, list) {
834ee56f 10387 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
f2ed8023
JM
10388 return 1;
10389 }
10390 return 0;
10391}
10392
10393
10394static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
10395 u8 *new_addr)
10396{
10397 unsigned int idx;
10398
10399 if (!drv->global)
10400 return -1;
10401
834ee56f 10402 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
f2ed8023 10403 for (idx = 0; idx < 64; idx++) {
834ee56f 10404 new_addr[0] = drv->first_bss->addr[0] | 0x02;
f2ed8023
JM
10405 new_addr[0] ^= idx << 2;
10406 if (!nl80211_addr_in_use(drv->global, new_addr))
10407 break;
10408 }
10409 if (idx == 64)
10410 return -1;
10411
10412 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
10413 MACSTR, MAC2STR(new_addr));
10414
10415 return 0;
10416}
10417
482856c8
JM
10418#endif /* CONFIG_P2P */
10419
f2ed8023 10420
f632e483
AS
10421struct wdev_info {
10422 u64 wdev_id;
10423 int wdev_id_set;
10424 u8 macaddr[ETH_ALEN];
10425};
10426
10427static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
e472e1b4
AS
10428{
10429 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10430 struct nlattr *tb[NL80211_ATTR_MAX + 1];
f632e483 10431 struct wdev_info *wi = arg;
e472e1b4
AS
10432
10433 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10434 genlmsg_attrlen(gnlh, 0), NULL);
10435 if (tb[NL80211_ATTR_WDEV]) {
f632e483
AS
10436 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
10437 wi->wdev_id_set = 1;
e472e1b4
AS
10438 }
10439
10440 if (tb[NL80211_ATTR_MAC])
f632e483 10441 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
e472e1b4
AS
10442 ETH_ALEN);
10443
10444 return NL_SKIP;
10445}
10446
10447
7ab68865 10448static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 10449 const char *ifname, const u8 *addr,
f3585c8a 10450 void *bss_ctx, void **drv_priv,
e17a2477 10451 char *force_ifname, u8 *if_addr,
2aec4f3c 10452 const char *bridge, int use_existing)
22a7c9d7 10453{
e472e1b4 10454 enum nl80211_iftype nlmode;
a2e40bb6
FF
10455 struct i802_bss *bss = priv;
10456 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7 10457 int ifidx;
2aec4f3c 10458 int added = 1;
22a7c9d7 10459
f3585c8a
JM
10460 if (addr)
10461 os_memcpy(if_addr, addr, ETH_ALEN);
e472e1b4
AS
10462 nlmode = wpa_driver_nl80211_if_type(type);
10463 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
f632e483
AS
10464 struct wdev_info p2pdev_info;
10465
10466 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
e472e1b4 10467 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
f632e483 10468 0, nl80211_wdev_handler,
2aec4f3c 10469 &p2pdev_info, use_existing);
f632e483 10470 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
e472e1b4
AS
10471 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10472 ifname);
e472e1b4
AS
10473 return -1;
10474 }
f632e483
AS
10475
10476 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10477 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10478 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10479 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10480 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10481 ifname,
10482 (long long unsigned int) p2pdev_info.wdev_id);
e472e1b4
AS
10483 } else {
10484 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
2aec4f3c
JM
10485 0, NULL, NULL, use_existing);
10486 if (use_existing && ifidx == -ENFILE) {
10487 added = 0;
10488 ifidx = if_nametoindex(ifname);
10489 } else if (ifidx < 0) {
e472e1b4
AS
10490 return -1;
10491 }
22a7c9d7
JM
10492 }
10493
ab7a1add
AS
10494 if (!addr) {
10495 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10496 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10497 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10498 bss->ifname, if_addr) < 0) {
2aec4f3c
JM
10499 if (added)
10500 nl80211_remove_iface(drv, ifidx);
ab7a1add
AS
10501 return -1;
10502 }
c55f774d
JM
10503 }
10504
10505#ifdef CONFIG_P2P
10506 if (!addr &&
10507 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10508 type == WPA_IF_P2P_GO)) {
10509 /* Enforce unique P2P Interface Address */
ab7a1add 10510 u8 new_addr[ETH_ALEN];
c55f774d 10511
ab7a1add 10512 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
c81eff1a 10513 new_addr) < 0) {
ea39367c
MK
10514 if (added)
10515 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
10516 return -1;
10517 }
f608081c 10518 if (nl80211_addr_in_use(drv->global, new_addr)) {
c55f774d
JM
10519 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10520 "for P2P group interface");
f2ed8023 10521 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
ea39367c
MK
10522 if (added)
10523 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
10524 return -1;
10525 }
c81eff1a 10526 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d 10527 new_addr) < 0) {
ea39367c
MK
10528 if (added)
10529 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
10530 return -1;
10531 }
c55f774d 10532 }
f67eeb5c 10533 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d
JM
10534 }
10535#endif /* CONFIG_P2P */
f3585c8a 10536
22a7c9d7 10537 if (type == WPA_IF_AP_BSS) {
f5eb9da3
JM
10538 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10539 if (new_bss == NULL) {
2aec4f3c
JM
10540 if (added)
10541 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
10542 return -1;
10543 }
10544
10545 if (bridge &&
10546 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10547 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10548 "interface %s to a bridge %s",
10549 ifname, bridge);
2aec4f3c
JM
10550 if (added)
10551 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
10552 os_free(new_bss);
10553 return -1;
10554 }
10555
c81eff1a
BG
10556 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10557 {
ea39367c
MK
10558 if (added)
10559 nl80211_remove_iface(drv, ifidx);
07179987 10560 os_free(new_bss);
22a7c9d7
JM
10561 return -1;
10562 }
a2e40bb6 10563 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 10564 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
10565 new_bss->ifindex = ifidx;
10566 new_bss->drv = drv;
834ee56f
KP
10567 new_bss->next = drv->first_bss->next;
10568 new_bss->freq = drv->first_bss->freq;
a5e1eb20 10569 new_bss->ctx = bss_ctx;
2aec4f3c 10570 new_bss->added_if = added;
834ee56f 10571 drv->first_bss->next = new_bss;
a2e40bb6
FF
10572 if (drv_priv)
10573 *drv_priv = new_bss;
cc7a48d1 10574 nl80211_init_bss(new_bss);
3dd1d890
YAP
10575
10576 /* Subscribe management frames for this WPA_IF_AP_BSS */
10577 if (nl80211_setup_ap(new_bss))
10578 return -1;
22a7c9d7 10579 }
22a7c9d7 10580
ff6a158b
JM
10581 if (drv->global)
10582 drv->global->if_add_ifindex = ifidx;
10583
d1bb7aed
JJ
10584 /*
10585 * Some virtual interfaces need to process EAPOL packets and events on
10586 * the parent interface. This is used mainly with hostapd.
10587 */
10588 if (ifidx > 0 &&
10589 (drv->hostapd ||
10590 nlmode == NL80211_IFTYPE_AP_VLAN ||
10591 nlmode == NL80211_IFTYPE_WDS ||
10592 nlmode == NL80211_IFTYPE_MONITOR))
b36935be
MB
10593 add_ifidx(drv, ifidx);
10594
22a7c9d7
JM
10595 return 0;
10596}
10597
10598
9ebce9c5 10599static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
10600 enum wpa_driver_if_type type,
10601 const char *ifname)
10602{
a2e40bb6 10603 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
10604 int ifindex = if_nametoindex(ifname);
10605
2aec4f3c
JM
10606 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10607 __func__, type, ifname, ifindex, bss->added_if);
158b090c 10608 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
2b72df63 10609 nl80211_remove_iface(drv, ifindex);
de884303
JM
10610 else if (ifindex > 0 && !bss->added_if) {
10611 struct wpa_driver_nl80211_data *drv2;
10612 dl_list_for_each(drv2, &drv->global->interfaces,
10613 struct wpa_driver_nl80211_data, list)
10614 del_ifidx(drv2, ifindex);
10615 }
c34e618d 10616
c34e618d
FF
10617 if (type != WPA_IF_AP_BSS)
10618 return 0;
10619
e17a2477 10620 if (bss->added_if_into_bridge) {
c81eff1a
BG
10621 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10622 bss->ifname) < 0)
e17a2477
JM
10623 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10624 "interface %s from bridge %s: %s",
10625 bss->ifname, bss->brname, strerror(errno));
10626 }
10627 if (bss->added_bridge) {
c81eff1a 10628 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
10629 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10630 "bridge %s: %s",
10631 bss->brname, strerror(errno));
10632 }
a2e40bb6 10633
834ee56f 10634 if (bss != drv->first_bss) {
8546ea19 10635 struct i802_bss *tbss;
a2e40bb6 10636
2aec4f3c 10637 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
834ee56f 10638 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
8546ea19
JM
10639 if (tbss->next == bss) {
10640 tbss->next = bss->next;
3dd1d890
YAP
10641 /* Unsubscribe management frames */
10642 nl80211_teardown_ap(bss);
cc7a48d1 10643 nl80211_destroy_bss(bss);
5c9da160
MB
10644 if (!bss->added_if)
10645 i802_set_iface_flags(bss, 0);
8546ea19
JM
10646 os_free(bss);
10647 bss = NULL;
10648 break;
10649 }
22a7c9d7 10650 }
8546ea19
JM
10651 if (bss)
10652 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10653 "BSS %p in the list", __func__, bss);
390e489c 10654 } else {
2aec4f3c 10655 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
390e489c 10656 nl80211_teardown_ap(bss);
2aec4f3c
JM
10657 if (!bss->added_if && !drv->first_bss->next)
10658 wpa_driver_nl80211_del_beacon(drv);
390e489c 10659 nl80211_destroy_bss(bss);
2aec4f3c
JM
10660 if (!bss->added_if)
10661 i802_set_iface_flags(bss, 0);
390e489c
KP
10662 if (drv->first_bss->next) {
10663 drv->first_bss = drv->first_bss->next;
10664 drv->ctx = drv->first_bss->ctx;
10665 os_free(bss);
10666 } else {
10667 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10668 }
22a7c9d7 10669 }
22a7c9d7
JM
10670
10671 return 0;
10672}
10673
10674
55777702
JM
10675static int cookie_handler(struct nl_msg *msg, void *arg)
10676{
10677 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10678 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10679 u64 *cookie = arg;
10680 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10681 genlmsg_attrlen(gnlh, 0), NULL);
10682 if (tb[NL80211_ATTR_COOKIE])
10683 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10684 return NL_SKIP;
10685}
10686
10687
88df0ef7 10688static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
10689 unsigned int freq, unsigned int wait,
10690 const u8 *buf, size_t buf_len,
88df0ef7
JB
10691 u64 *cookie_out, int no_cck, int no_ack,
10692 int offchanok)
9884f9cc 10693{
88df0ef7 10694 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
10695 struct nl_msg *msg;
10696 u64 cookie;
10697 int ret = -1;
10698
10699 msg = nlmsg_alloc();
10700 if (!msg)
10701 return -1;
10702
cc2ada86 10703 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
2e3e4566
JM
10704 "no_ack=%d offchanok=%d",
10705 freq, wait, no_cck, no_ack, offchanok);
c91f796f 10706 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
9fb04070 10707 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9884f9cc 10708
f632e483 10709 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80 10710 goto nla_put_failure;
c91f796f
NC
10711 if (freq)
10712 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9db931ed
JM
10713 if (wait)
10714 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
64abb725
JM
10715 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10716 drv->test_use_roc_tx))
88df0ef7 10717 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
b106173a
JM
10718 if (no_cck)
10719 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
ddc53271
JM
10720 if (no_ack)
10721 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
970fa12e 10722
9884f9cc
JB
10723 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10724
10725 cookie = 0;
10726 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10727 msg = NULL;
10728 if (ret) {
10729 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
10730 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10731 freq, wait);
9884f9cc
JB
10732 goto nla_put_failure;
10733 }
cc2ada86 10734 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
ddc53271
JM
10735 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10736 (long long unsigned int) cookie);
9884f9cc
JB
10737
10738 if (cookie_out)
ddc53271 10739 *cookie_out = no_ack ? (u64) -1 : cookie;
9884f9cc
JB
10740
10741nla_put_failure:
10742 nlmsg_free(msg);
10743 return ret;
10744}
10745
10746
9ebce9c5
JM
10747static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10748 unsigned int freq,
190b9062 10749 unsigned int wait_time,
58f6fbe0
JM
10750 const u8 *dst, const u8 *src,
10751 const u8 *bssid,
b106173a
JM
10752 const u8 *data, size_t data_len,
10753 int no_cck)
58f6fbe0 10754{
a2e40bb6 10755 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 10756 int ret = -1;
58f6fbe0
JM
10757 u8 *buf;
10758 struct ieee80211_hdr *hdr;
58f6fbe0 10759
5dfca53f 10760 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
10761 "freq=%u MHz wait=%d ms no_cck=%d)",
10762 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
10763
10764 buf = os_zalloc(24 + data_len);
10765 if (buf == NULL)
10766 return ret;
10767 os_memcpy(buf + 24, data, data_len);
10768 hdr = (struct ieee80211_hdr *) buf;
10769 hdr->frame_control =
10770 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10771 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10772 os_memcpy(hdr->addr2, src, ETH_ALEN);
10773 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10774
f78f2785
JM
10775 if (is_ap_interface(drv->nlmode) &&
10776 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10777 (int) freq == bss->freq || drv->device_ap_sme ||
10778 !drv->use_monitor))
9ebce9c5
JM
10779 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10780 0, freq, no_cck, 1,
10781 wait_time);
9884f9cc 10782 else
88df0ef7 10783 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 10784 24 + data_len,
b106173a 10785 &drv->send_action_cookie,
88df0ef7 10786 no_cck, 0, 1);
58f6fbe0 10787
f8bf1421 10788 os_free(buf);
58f6fbe0
JM
10789 return ret;
10790}
10791
10792
5dfca53f
JB
10793static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10794{
10795 struct i802_bss *bss = priv;
10796 struct wpa_driver_nl80211_data *drv = bss->drv;
10797 struct nl_msg *msg;
10798 int ret;
10799
10800 msg = nlmsg_alloc();
10801 if (!msg)
10802 return;
10803
316a9e4d
JM
10804 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10805 (long long unsigned int) drv->send_action_cookie);
9fb04070 10806 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
5dfca53f 10807
7940c790
AS
10808 if (nl80211_set_iface_id(msg, bss) < 0)
10809 goto nla_put_failure;
5dfca53f
JB
10810 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10811
10812 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10813 msg = NULL;
10814 if (ret)
10815 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10816 "(%s)", ret, strerror(-ret));
10817
10818 nla_put_failure:
10819 nlmsg_free(msg);
10820}
10821
10822
55777702
JM
10823static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10824 unsigned int duration)
10825{
a2e40bb6
FF
10826 struct i802_bss *bss = priv;
10827 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
10828 struct nl_msg *msg;
10829 int ret;
10830 u64 cookie;
10831
10832 msg = nlmsg_alloc();
10833 if (!msg)
10834 return -1;
10835
9fb04070 10836 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
55777702 10837
f632e483 10838 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80
DS
10839 goto nla_put_failure;
10840
55777702
JM
10841 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10842 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10843
10844 cookie = 0;
10845 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5883168a 10846 msg = NULL;
55777702
JM
10847 if (ret == 0) {
10848 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10849 "0x%llx for freq=%u MHz duration=%u",
10850 (long long unsigned int) cookie, freq, duration);
10851 drv->remain_on_chan_cookie = cookie;
531f0331 10852 drv->pending_remain_on_chan = 1;
55777702
JM
10853 return 0;
10854 }
10855 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
10856 "(freq=%d duration=%u): %d (%s)",
10857 freq, duration, ret, strerror(-ret));
55777702 10858nla_put_failure:
5883168a 10859 nlmsg_free(msg);
55777702
JM
10860 return -1;
10861}
10862
10863
10864static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10865{
a2e40bb6
FF
10866 struct i802_bss *bss = priv;
10867 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
10868 struct nl_msg *msg;
10869 int ret;
10870
10871 if (!drv->pending_remain_on_chan) {
10872 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10873 "to cancel");
10874 return -1;
10875 }
10876
10877 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10878 "0x%llx",
10879 (long long unsigned int) drv->remain_on_chan_cookie);
10880
10881 msg = nlmsg_alloc();
10882 if (!msg)
10883 return -1;
10884
9fb04070 10885 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
55777702 10886
f632e483 10887 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80
DS
10888 goto nla_put_failure;
10889
55777702
JM
10890 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10891
10892 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 10893 msg = NULL;
55777702
JM
10894 if (ret == 0)
10895 return 0;
10896 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10897 "%d (%s)", ret, strerror(-ret));
10898nla_put_failure:
5883168a 10899 nlmsg_free(msg);
55777702
JM
10900 return -1;
10901}
10902
10903
9ebce9c5 10904static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 10905{
a2e40bb6 10906 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 10907
5582a5d1 10908 if (!report) {
0d891981 10909 if (bss->nl_preq && drv->device_ap_sme &&
b8d87ed2
AP
10910 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
10911 !bss->static_ap) {
0d891981
JM
10912 /*
10913 * Do not disable Probe Request reporting that was
10914 * enabled in nl80211_setup_ap().
10915 */
10916 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10917 "Probe Request reporting nl_preq=%p while "
10918 "in AP mode", bss->nl_preq);
10919 } else if (bss->nl_preq) {
36488c05
JM
10920 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10921 "reporting nl_preq=%p", bss->nl_preq);
5f65e9f7 10922 nl80211_destroy_eloop_handle(&bss->nl_preq);
5582a5d1
JB
10923 }
10924 return 0;
10925 }
10926
481234cf 10927 if (bss->nl_preq) {
5582a5d1 10928 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 10929 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
10930 return 0;
10931 }
10932
481234cf
JM
10933 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10934 if (bss->nl_preq == NULL)
5582a5d1 10935 return -1;
36488c05
JM
10936 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10937 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 10938
481234cf 10939 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
10940 (WLAN_FC_TYPE_MGMT << 2) |
10941 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
10942 NULL, 0) < 0)
10943 goto out_err;
5582a5d1 10944
5f65e9f7
JB
10945 nl80211_register_eloop_read(&bss->nl_preq,
10946 wpa_driver_nl80211_event_receive,
10947 bss->nl_cb);
5582a5d1 10948
504e905c 10949 return 0;
5582a5d1 10950
a92dfde8 10951 out_err:
221a59c9 10952 nl_destroy_handles(&bss->nl_preq);
5582a5d1 10953 return -1;
504e905c
JM
10954}
10955
10956
4e5cb1a3
JM
10957static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10958 int ifindex, int disabled)
10959{
10960 struct nl_msg *msg;
10961 struct nlattr *bands, *band;
10962 int ret;
10963
10964 msg = nlmsg_alloc();
10965 if (!msg)
10966 return -1;
10967
9fb04070 10968 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
10969 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10970
10971 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10972 if (!bands)
10973 goto nla_put_failure;
10974
10975 /*
10976 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10977 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10978 * rates. All 5 GHz rates are left enabled.
10979 */
10980 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10981 if (!band)
10982 goto nla_put_failure;
1dea5882
JM
10983 if (disabled) {
10984 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10985 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10986 }
4e5cb1a3
JM
10987 nla_nest_end(msg, band);
10988
10989 nla_nest_end(msg, bands);
10990
10991 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10992 msg = NULL;
10993 if (ret) {
10994 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10995 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
10996 } else
10997 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
10998
10999 return ret;
11000
11001nla_put_failure:
11002 nlmsg_free(msg);
11003 return -1;
11004}
11005
11006
af473088
JM
11007static int wpa_driver_nl80211_deinit_ap(void *priv)
11008{
a2e40bb6
FF
11009 struct i802_bss *bss = priv;
11010 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 11011 if (!is_ap_interface(drv->nlmode))
af473088
JM
11012 return -1;
11013 wpa_driver_nl80211_del_beacon(drv);
60b13c20
IP
11014
11015 /*
11016 * If the P2P GO interface was dynamically added, then it is
11017 * possible that the interface change to station is not possible.
11018 */
11019 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
11020 return 0;
11021
b1f625e0 11022 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
11023}
11024
11025
695c7038
SW
11026static int wpa_driver_nl80211_stop_ap(void *priv)
11027{
11028 struct i802_bss *bss = priv;
11029 struct wpa_driver_nl80211_data *drv = bss->drv;
11030 if (!is_ap_interface(drv->nlmode))
11031 return -1;
11032 wpa_driver_nl80211_del_beacon(drv);
11033 bss->beacon_set = 0;
11034 return 0;
11035}
11036
11037
3c29244e
EP
11038static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
11039{
11040 struct i802_bss *bss = priv;
11041 struct wpa_driver_nl80211_data *drv = bss->drv;
11042 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
11043 return -1;
60b13c20
IP
11044
11045 /*
11046 * If the P2P Client interface was dynamically added, then it is
11047 * possible that the interface change to station is not possible.
11048 */
11049 if (bss->if_dynamic)
11050 return 0;
11051
3c29244e
EP
11052 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
11053}
11054
11055
207ef3fb
JM
11056static void wpa_driver_nl80211_resume(void *priv)
11057{
a2e40bb6 11058 struct i802_bss *bss = priv;
91724d6f
AS
11059
11060 if (i802_set_iface_flags(bss, 1))
11061 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
207ef3fb
JM
11062}
11063
11064
7b90c16a
JM
11065static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
11066 const u8 *ies, size_t ies_len)
11067{
11068 struct i802_bss *bss = priv;
11069 struct wpa_driver_nl80211_data *drv = bss->drv;
11070 int ret;
11071 u8 *data, *pos;
11072 size_t data_len;
341eebee 11073 const u8 *own_addr = bss->addr;
7b90c16a
JM
11074
11075 if (action != 1) {
11076 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
11077 "action %d", action);
11078 return -1;
11079 }
11080
11081 /*
11082 * Action frame payload:
11083 * Category[1] = 6 (Fast BSS Transition)
11084 * Action[1] = 1 (Fast BSS Transition Request)
11085 * STA Address
11086 * Target AP Address
11087 * FT IEs
11088 */
11089
73fc617d
JM
11090 data_len = 2 + 2 * ETH_ALEN + ies_len;
11091 data = os_malloc(data_len);
7b90c16a
JM
11092 if (data == NULL)
11093 return -1;
11094 pos = data;
11095 *pos++ = 0x06; /* FT Action category */
11096 *pos++ = action;
11097 os_memcpy(pos, own_addr, ETH_ALEN);
11098 pos += ETH_ALEN;
11099 os_memcpy(pos, target_ap, ETH_ALEN);
11100 pos += ETH_ALEN;
11101 os_memcpy(pos, ies, ies_len);
11102
190b9062
JB
11103 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
11104 drv->bssid, own_addr, drv->bssid,
b106173a 11105 data, data_len, 0);
7b90c16a
JM
11106 os_free(data);
11107
11108 return ret;
11109}
11110
11111
b625473c
JM
11112static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
11113{
11114 struct i802_bss *bss = priv;
11115 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
11116 struct nl_msg *msg;
11117 struct nlattr *cqm;
f0494d0f 11118 int ret = -1;
b625473c
JM
11119
11120 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
11121 "hysteresis=%d", threshold, hysteresis);
11122
11123 msg = nlmsg_alloc();
11124 if (!msg)
11125 return -1;
11126
9fb04070 11127 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
b625473c
JM
11128
11129 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11130
8970bae8 11131 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
b625473c 11132 if (cqm == NULL)
21270bb4 11133 goto nla_put_failure;
b625473c 11134
8970bae8
JB
11135 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
11136 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
11137 nla_nest_end(msg, cqm);
21270bb4 11138
f0494d0f 11139 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
11140 msg = NULL;
11141
11142nla_put_failure:
b625473c 11143 nlmsg_free(msg);
f0494d0f 11144 return ret;
b625473c
JM
11145}
11146
11147
2cc8d8f4
AO
11148static int get_channel_width(struct nl_msg *msg, void *arg)
11149{
11150 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11151 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11152 struct wpa_signal_info *sig_change = arg;
11153
11154 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11155 genlmsg_attrlen(gnlh, 0), NULL);
11156
11157 sig_change->center_frq1 = -1;
11158 sig_change->center_frq2 = -1;
11159 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
11160
11161 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
11162 sig_change->chanwidth = convert2width(
11163 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
11164 if (tb[NL80211_ATTR_CENTER_FREQ1])
11165 sig_change->center_frq1 =
11166 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
11167 if (tb[NL80211_ATTR_CENTER_FREQ2])
11168 sig_change->center_frq2 =
11169 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
11170 }
11171
11172 return NL_SKIP;
11173}
11174
11175
11176static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
11177 struct wpa_signal_info *sig)
11178{
11179 struct nl_msg *msg;
11180
11181 msg = nlmsg_alloc();
11182 if (!msg)
11183 return -ENOMEM;
11184
11185 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
11186 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11187
11188 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
11189
11190nla_put_failure:
11191 nlmsg_free(msg);
11192 return -ENOBUFS;
11193}
11194
11195
1c5c7273
PS
11196static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
11197{
11198 struct i802_bss *bss = priv;
11199 struct wpa_driver_nl80211_data *drv = bss->drv;
11200 int res;
11201
11202 os_memset(si, 0, sizeof(*si));
11203 res = nl80211_get_link_signal(drv, si);
11204 if (res != 0)
11205 return res;
11206
2cc8d8f4
AO
11207 res = nl80211_get_channel_width(drv, si);
11208 if (res != 0)
11209 return res;
11210
1c5c7273
PS
11211 return nl80211_get_link_noise(drv, si);
11212}
11213
11214
57ebba59
JJ
11215static int wpa_driver_nl80211_shared_freq(void *priv)
11216{
11217 struct i802_bss *bss = priv;
11218 struct wpa_driver_nl80211_data *drv = bss->drv;
11219 struct wpa_driver_nl80211_data *driver;
11220 int freq = 0;
11221
11222 /*
11223 * If the same PHY is in connected state with some other interface,
11224 * then retrieve the assoc freq.
11225 */
11226 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
11227 drv->phyname);
11228
11229 dl_list_for_each(driver, &drv->global->interfaces,
11230 struct wpa_driver_nl80211_data, list) {
11231 if (drv == driver ||
11232 os_strcmp(drv->phyname, driver->phyname) != 0 ||
11233 !driver->associated)
11234 continue;
11235
11236 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
11237 MACSTR,
834ee56f
KP
11238 driver->phyname, driver->first_bss->ifname,
11239 MAC2STR(driver->first_bss->addr));
d3bd0f05 11240 if (is_ap_interface(driver->nlmode))
834ee56f 11241 freq = driver->first_bss->freq;
d3bd0f05
JJ
11242 else
11243 freq = nl80211_get_assoc_freq(driver);
57ebba59
JJ
11244 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
11245 drv->phyname, freq);
11246 }
11247
11248 if (!freq)
11249 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
11250 "PHY (%s) in associated state", drv->phyname);
11251
11252 return freq;
11253}
11254
11255
b91ab76e
JM
11256static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
11257 int encrypt)
11258{
11259 struct i802_bss *bss = priv;
55231068
JM
11260 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
11261 0, 0, 0, 0);
b91ab76e
JM
11262}
11263
11264
c55f774d
JM
11265static int nl80211_set_param(void *priv, const char *param)
11266{
c55f774d
JM
11267 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
11268 if (param == NULL)
11269 return 0;
11270
11271#ifdef CONFIG_P2P
11272 if (os_strstr(param, "use_p2p_group_interface=1")) {
482856c8
JM
11273 struct i802_bss *bss = priv;
11274 struct wpa_driver_nl80211_data *drv = bss->drv;
11275
c55f774d
JM
11276 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
11277 "interface");
11278 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
11279 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
11280 }
11281#endif /* CONFIG_P2P */
11282
327b01d3
JM
11283 if (os_strstr(param, "use_monitor=1")) {
11284 struct i802_bss *bss = priv;
11285 struct wpa_driver_nl80211_data *drv = bss->drv;
11286 drv->use_monitor = 1;
11287 }
11288
11289 if (os_strstr(param, "force_connect_cmd=1")) {
11290 struct i802_bss *bss = priv;
11291 struct wpa_driver_nl80211_data *drv = bss->drv;
11292 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
b497a212 11293 drv->force_connect_cmd = 1;
327b01d3
JM
11294 }
11295
64abb725
JM
11296 if (os_strstr(param, "no_offchannel_tx=1")) {
11297 struct i802_bss *bss = priv;
11298 struct wpa_driver_nl80211_data *drv = bss->drv;
11299 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
11300 drv->test_use_roc_tx = 1;
11301 }
11302
c55f774d
JM
11303 return 0;
11304}
11305
11306
f2ed8023
JM
11307static void * nl80211_global_init(void)
11308{
11309 struct nl80211_global *global;
36d84860
BG
11310 struct netlink_config *cfg;
11311
f2ed8023
JM
11312 global = os_zalloc(sizeof(*global));
11313 if (global == NULL)
11314 return NULL;
c81eff1a 11315 global->ioctl_sock = -1;
f2ed8023 11316 dl_list_init(&global->interfaces);
ff6a158b 11317 global->if_add_ifindex = -1;
36d84860
BG
11318
11319 cfg = os_zalloc(sizeof(*cfg));
11320 if (cfg == NULL)
11321 goto err;
11322
11323 cfg->ctx = global;
11324 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
11325 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
11326 global->netlink = netlink_init(cfg);
11327 if (global->netlink == NULL) {
11328 os_free(cfg);
11329 goto err;
11330 }
11331
2a7b66f5
BG
11332 if (wpa_driver_nl80211_init_nl_global(global) < 0)
11333 goto err;
11334
c81eff1a
BG
11335 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
11336 if (global->ioctl_sock < 0) {
7ac3616d
JM
11337 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
11338 strerror(errno));
c81eff1a
BG
11339 goto err;
11340 }
11341
f2ed8023 11342 return global;
36d84860
BG
11343
11344err:
11345 nl80211_global_deinit(global);
11346 return NULL;
f2ed8023
JM
11347}
11348
11349
11350static void nl80211_global_deinit(void *priv)
11351{
11352 struct nl80211_global *global = priv;
11353 if (global == NULL)
11354 return;
11355 if (!dl_list_empty(&global->interfaces)) {
11356 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
11357 "nl80211_global_deinit",
11358 dl_list_len(&global->interfaces));
11359 }
36d84860
BG
11360
11361 if (global->netlink)
11362 netlink_deinit(global->netlink);
11363
276e2d67
BG
11364 nl_destroy_handles(&global->nl);
11365
5f65e9f7
JB
11366 if (global->nl_event)
11367 nl80211_destroy_eloop_handle(&global->nl_event);
d6c9aab8
JB
11368
11369 nl_cb_put(global->nl_cb);
2a7b66f5 11370
c81eff1a
BG
11371 if (global->ioctl_sock >= 0)
11372 close(global->ioctl_sock);
11373
f2ed8023
JM
11374 os_free(global);
11375}
11376
11377
6859f1cb
BG
11378static const char * nl80211_get_radio_name(void *priv)
11379{
11380 struct i802_bss *bss = priv;
11381 struct wpa_driver_nl80211_data *drv = bss->drv;
11382 return drv->phyname;
11383}
11384
11385
a6efc65d
JM
11386static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
11387 const u8 *pmkid)
11388{
11389 struct nl_msg *msg;
11390
11391 msg = nlmsg_alloc();
11392 if (!msg)
11393 return -ENOMEM;
11394
9fb04070 11395 nl80211_cmd(bss->drv, msg, 0, cmd);
a6efc65d
JM
11396
11397 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
11398 if (pmkid)
11399 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
11400 if (bssid)
11401 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
11402
11403 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11404 nla_put_failure:
9e088e74 11405 nlmsg_free(msg);
a6efc65d
JM
11406 return -ENOBUFS;
11407}
11408
11409
11410static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11411{
11412 struct i802_bss *bss = priv;
11413 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
11414 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
11415}
11416
11417
11418static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11419{
11420 struct i802_bss *bss = priv;
11421 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
11422 MAC2STR(bssid));
11423 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
11424}
11425
11426
11427static int nl80211_flush_pmkid(void *priv)
11428{
11429 struct i802_bss *bss = priv;
11430 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
11431 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
11432}
11433
11434
0185007c
MK
11435static void clean_survey_results(struct survey_results *survey_results)
11436{
11437 struct freq_survey *survey, *tmp;
11438
11439 if (dl_list_empty(&survey_results->survey_list))
11440 return;
11441
11442 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
11443 struct freq_survey, list) {
11444 dl_list_del(&survey->list);
11445 os_free(survey);
11446 }
11447}
11448
11449
11450static void add_survey(struct nlattr **sinfo, u32 ifidx,
11451 struct dl_list *survey_list)
11452{
11453 struct freq_survey *survey;
11454
11455 survey = os_zalloc(sizeof(struct freq_survey));
11456 if (!survey)
11457 return;
11458
11459 survey->ifidx = ifidx;
11460 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11461 survey->filled = 0;
11462
11463 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11464 survey->nf = (int8_t)
11465 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11466 survey->filled |= SURVEY_HAS_NF;
11467 }
11468
11469 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11470 survey->channel_time =
11471 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11472 survey->filled |= SURVEY_HAS_CHAN_TIME;
11473 }
11474
11475 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11476 survey->channel_time_busy =
11477 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11478 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11479 }
11480
11481 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11482 survey->channel_time_rx =
11483 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11484 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11485 }
11486
11487 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11488 survey->channel_time_tx =
11489 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11490 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11491 }
11492
11493 wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
11494 survey->freq,
11495 survey->nf,
11496 (unsigned long int) survey->channel_time,
11497 (unsigned long int) survey->channel_time_busy,
11498 (unsigned long int) survey->channel_time_tx,
11499 (unsigned long int) survey->channel_time_rx,
11500 survey->filled);
11501
11502 dl_list_add_tail(survey_list, &survey->list);
11503}
11504
11505
11506static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11507 unsigned int freq_filter)
11508{
11509 if (!freq_filter)
11510 return 1;
11511
11512 return freq_filter == surveyed_freq;
11513}
11514
11515
11516static int survey_handler(struct nl_msg *msg, void *arg)
11517{
11518 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11519 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11520 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11521 struct survey_results *survey_results;
11522 u32 surveyed_freq = 0;
11523 u32 ifidx;
11524
11525 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11526 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11527 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11528 };
11529
11530 survey_results = (struct survey_results *) arg;
11531
11532 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11533 genlmsg_attrlen(gnlh, 0), NULL);
11534
e28f39b7
SJ
11535 if (!tb[NL80211_ATTR_IFINDEX])
11536 return NL_SKIP;
11537
0185007c
MK
11538 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11539
11540 if (!tb[NL80211_ATTR_SURVEY_INFO])
11541 return NL_SKIP;
11542
11543 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11544 tb[NL80211_ATTR_SURVEY_INFO],
11545 survey_policy))
11546 return NL_SKIP;
11547
11548 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11549 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11550 return NL_SKIP;
11551 }
11552
11553 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11554
11555 if (!check_survey_ok(sinfo, surveyed_freq,
11556 survey_results->freq_filter))
11557 return NL_SKIP;
11558
11559 if (survey_results->freq_filter &&
11560 survey_results->freq_filter != surveyed_freq) {
11561 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11562 surveyed_freq);
11563 return NL_SKIP;
11564 }
11565
11566 add_survey(sinfo, ifidx, &survey_results->survey_list);
11567
11568 return NL_SKIP;
11569}
11570
11571
11572static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11573{
11574 struct i802_bss *bss = priv;
11575 struct wpa_driver_nl80211_data *drv = bss->drv;
11576 struct nl_msg *msg;
11577 int err = -ENOBUFS;
11578 union wpa_event_data data;
11579 struct survey_results *survey_results;
11580
11581 os_memset(&data, 0, sizeof(data));
11582 survey_results = &data.survey_results;
11583
11584 dl_list_init(&survey_results->survey_list);
11585
11586 msg = nlmsg_alloc();
11587 if (!msg)
11588 goto nla_put_failure;
11589
11590 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11591
11592 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11593
11594 if (freq)
11595 data.survey_results.freq_filter = freq;
11596
11597 do {
11598 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11599 err = send_and_recv_msgs(drv, msg, survey_handler,
11600 survey_results);
11601 } while (err > 0);
11602
11603 if (err) {
11604 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11605 goto out_clean;
11606 }
11607
11608 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11609
11610out_clean:
11611 clean_survey_results(survey_results);
11612nla_put_failure:
11613 return err;
11614}
11615
11616
b14a210c
JB
11617static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11618 const u8 *replay_ctr)
11619{
11620 struct i802_bss *bss = priv;
11621 struct wpa_driver_nl80211_data *drv = bss->drv;
11622 struct nlattr *replay_nested;
11623 struct nl_msg *msg;
11624
11625 msg = nlmsg_alloc();
11626 if (!msg)
11627 return;
11628
9fb04070 11629 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
b14a210c
JB
11630
11631 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11632
11633 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11634 if (!replay_nested)
11635 goto nla_put_failure;
11636
11637 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11638 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11639 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11640 replay_ctr);
11641
11642 nla_nest_end(msg, replay_nested);
11643
11644 send_and_recv_msgs(drv, msg, NULL, NULL);
11645 return;
11646 nla_put_failure:
11647 nlmsg_free(msg);
11648}
11649
11650
39718852
JB
11651static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11652 const u8 *addr, int qos)
bcf24348 11653{
39718852
JB
11654 /* send data frame to poll STA and check whether
11655 * this frame is ACKed */
bcf24348
JB
11656 struct {
11657 struct ieee80211_hdr hdr;
11658 u16 qos_ctl;
11659 } STRUCT_PACKED nulldata;
11660 size_t size;
11661
11662 /* Send data frame to poll STA and check whether this frame is ACKed */
11663
11664 os_memset(&nulldata, 0, sizeof(nulldata));
11665
11666 if (qos) {
11667 nulldata.hdr.frame_control =
11668 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11669 WLAN_FC_STYPE_QOS_NULL);
11670 size = sizeof(nulldata);
11671 } else {
11672 nulldata.hdr.frame_control =
11673 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11674 WLAN_FC_STYPE_NULLFUNC);
11675 size = sizeof(struct ieee80211_hdr);
11676 }
11677
11678 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11679 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11680 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11681 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11682
9ebce9c5
JM
11683 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11684 0, 0) < 0)
bcf24348
JB
11685 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11686 "send poll frame");
11687}
11688
39718852
JB
11689static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11690 int qos)
11691{
11692 struct i802_bss *bss = priv;
11693 struct wpa_driver_nl80211_data *drv = bss->drv;
11694 struct nl_msg *msg;
11695
11696 if (!drv->poll_command_supported) {
11697 nl80211_send_null_frame(bss, own_addr, addr, qos);
11698 return;
11699 }
11700
11701 msg = nlmsg_alloc();
11702 if (!msg)
11703 return;
11704
11705 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11706
11707 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11708 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11709
11710 send_and_recv_msgs(drv, msg, NULL, NULL);
11711 return;
11712 nla_put_failure:
11713 nlmsg_free(msg);
11714}
11715
bcf24348 11716
29f338af
JM
11717static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11718{
11719 struct nl_msg *msg;
11720
11721 msg = nlmsg_alloc();
11722 if (!msg)
11723 return -ENOMEM;
11724
11725 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11726 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11727 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11728 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11729 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11730nla_put_failure:
11731 nlmsg_free(msg);
11732 return -ENOBUFS;
11733}
11734
11735
11736static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11737 int ctwindow)
11738{
11739 struct i802_bss *bss = priv;
11740
11741 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11742 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11743
0de38036
JM
11744 if (opp_ps != -1 || ctwindow != -1) {
11745#ifdef ANDROID_P2P
11746 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
11747#else /* ANDROID_P2P */
29f338af 11748 return -1; /* Not yet supported */
0de38036
JM
11749#endif /* ANDROID_P2P */
11750 }
29f338af
JM
11751
11752 if (legacy_ps == -1)
11753 return 0;
11754 if (legacy_ps != 0 && legacy_ps != 1)
11755 return -1; /* Not yet supported */
11756
11757 return nl80211_set_power_save(bss, legacy_ps);
11758}
11759
11760
04e8003c
JD
11761static int nl80211_start_radar_detection(void *priv,
11762 struct hostapd_freq_params *freq)
f90e9c1c
SW
11763{
11764 struct i802_bss *bss = priv;
11765 struct wpa_driver_nl80211_data *drv = bss->drv;
11766 struct nl_msg *msg;
11767 int ret;
11768
04e8003c
JD
11769 wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
11770 freq->freq, freq->ht_enabled, freq->vht_enabled,
11771 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11772
f90e9c1c
SW
11773 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11774 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11775 "detection");
11776 return -1;
11777 }
11778
11779 msg = nlmsg_alloc();
11780 if (!msg)
11781 return -1;
11782
11783 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11784 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
f90e9c1c 11785
ed8e0059
JD
11786 if (nl80211_put_freq_params(msg, freq) < 0)
11787 goto nla_put_failure;
f90e9c1c
SW
11788
11789 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
dbdc9a1d 11790 msg = NULL;
f90e9c1c
SW
11791 if (ret == 0)
11792 return 0;
11793 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11794 "%d (%s)", ret, strerror(-ret));
11795nla_put_failure:
dbdc9a1d 11796 nlmsg_free(msg);
f90e9c1c
SW
11797 return -1;
11798}
11799
03ea1786
AN
11800#ifdef CONFIG_TDLS
11801
11802static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11803 u8 dialog_token, u16 status_code,
984dadc2
AN
11804 u32 peer_capab, int initiator, const u8 *buf,
11805 size_t len)
03ea1786
AN
11806{
11807 struct i802_bss *bss = priv;
11808 struct wpa_driver_nl80211_data *drv = bss->drv;
11809 struct nl_msg *msg;
11810
11811 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11812 return -EOPNOTSUPP;
11813
11814 if (!dst)
11815 return -EINVAL;
11816
11817 msg = nlmsg_alloc();
11818 if (!msg)
11819 return -ENOMEM;
11820
11821 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11822 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11823 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11824 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11825 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11826 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
96ecea5e
SD
11827 if (peer_capab) {
11828 /*
11829 * The internal enum tdls_peer_capability definition is
11830 * currently identical with the nl80211 enum
11831 * nl80211_tdls_peer_capability, so no conversion is needed
11832 * here.
11833 */
11834 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11835 }
984dadc2
AN
11836 if (initiator)
11837 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_INITIATOR);
03ea1786
AN
11838 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11839
11840 return send_and_recv_msgs(drv, msg, NULL, NULL);
11841
11842nla_put_failure:
11843 nlmsg_free(msg);
11844 return -ENOBUFS;
11845}
11846
11847
11848static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11849{
11850 struct i802_bss *bss = priv;
11851 struct wpa_driver_nl80211_data *drv = bss->drv;
11852 struct nl_msg *msg;
11853 enum nl80211_tdls_operation nl80211_oper;
11854
11855 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11856 return -EOPNOTSUPP;
11857
11858 switch (oper) {
11859 case TDLS_DISCOVERY_REQ:
11860 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11861 break;
11862 case TDLS_SETUP:
11863 nl80211_oper = NL80211_TDLS_SETUP;
11864 break;
11865 case TDLS_TEARDOWN:
11866 nl80211_oper = NL80211_TDLS_TEARDOWN;
11867 break;
11868 case TDLS_ENABLE_LINK:
11869 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11870 break;
11871 case TDLS_DISABLE_LINK:
11872 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11873 break;
11874 case TDLS_ENABLE:
11875 return 0;
11876 case TDLS_DISABLE:
11877 return 0;
11878 default:
11879 return -EINVAL;
11880 }
11881
11882 msg = nlmsg_alloc();
11883 if (!msg)
11884 return -ENOMEM;
11885
11886 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11887 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11888 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11889 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11890
11891 return send_and_recv_msgs(drv, msg, NULL, NULL);
11892
11893nla_put_failure:
11894 nlmsg_free(msg);
11895 return -ENOBUFS;
11896}
11897
11898#endif /* CONFIG TDLS */
11899
11900
216eede8
DS
11901#ifdef ANDROID
11902
11903typedef struct android_wifi_priv_cmd {
11904 char *buf;
11905 int used_len;
11906 int total_len;
11907} android_wifi_priv_cmd;
11908
11909static int drv_errors = 0;
11910
11911static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11912{
11913 drv_errors++;
11914 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11915 drv_errors = 0;
11916 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11917 }
11918}
11919
11920
11921static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11922{
11923 struct wpa_driver_nl80211_data *drv = bss->drv;
11924 struct ifreq ifr;
11925 android_wifi_priv_cmd priv_cmd;
11926 char buf[MAX_DRV_CMD_SIZE];
11927 int ret;
11928
11929 os_memset(&ifr, 0, sizeof(ifr));
11930 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11931 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11932
11933 os_memset(buf, 0, sizeof(buf));
11934 os_strlcpy(buf, cmd, sizeof(buf));
11935
11936 priv_cmd.buf = buf;
11937 priv_cmd.used_len = sizeof(buf);
11938 priv_cmd.total_len = sizeof(buf);
11939 ifr.ifr_data = &priv_cmd;
11940
11941 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11942 if (ret < 0) {
11943 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11944 __func__);
11945 wpa_driver_send_hang_msg(drv);
11946 return ret;
11947 }
11948
11949 drv_errors = 0;
11950 return 0;
11951}
11952
11953
11954static int android_pno_start(struct i802_bss *bss,
11955 struct wpa_driver_scan_params *params)
11956{
11957 struct wpa_driver_nl80211_data *drv = bss->drv;
11958 struct ifreq ifr;
11959 android_wifi_priv_cmd priv_cmd;
11960 int ret = 0, i = 0, bp;
11961 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11962
11963 bp = WEXT_PNOSETUP_HEADER_SIZE;
11964 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11965 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11966 buf[bp++] = WEXT_PNO_TLV_VERSION;
11967 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11968 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11969
11970 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11971 /* Check that there is enough space needed for 1 more SSID, the
11972 * other sections and null termination */
11973 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11974 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11975 break;
11976 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
a97bde0a
JM
11977 params->ssids[i].ssid,
11978 params->ssids[i].ssid_len);
216eede8
DS
11979 buf[bp++] = WEXT_PNO_SSID_SECTION;
11980 buf[bp++] = params->ssids[i].ssid_len;
11981 os_memcpy(&buf[bp], params->ssids[i].ssid,
11982 params->ssids[i].ssid_len);
11983 bp += params->ssids[i].ssid_len;
11984 i++;
11985 }
11986
11987 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11988 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11989 WEXT_PNO_SCAN_INTERVAL);
11990 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11991
11992 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11993 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11994 WEXT_PNO_REPEAT);
11995 bp += WEXT_PNO_REPEAT_LENGTH;
11996
11997 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11998 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11999 WEXT_PNO_MAX_REPEAT);
12000 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
12001
12002 memset(&ifr, 0, sizeof(ifr));
12003 memset(&priv_cmd, 0, sizeof(priv_cmd));
24f051eb 12004 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
216eede8
DS
12005
12006 priv_cmd.buf = buf;
12007 priv_cmd.used_len = bp;
12008 priv_cmd.total_len = bp;
12009 ifr.ifr_data = &priv_cmd;
12010
12011 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
12012
12013 if (ret < 0) {
12014 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
12015 ret);
12016 wpa_driver_send_hang_msg(drv);
12017 return ret;
12018 }
12019
12020 drv_errors = 0;
12021
12022 return android_priv_cmd(bss, "PNOFORCE 1");
12023}
12024
12025
12026static int android_pno_stop(struct i802_bss *bss)
12027{
12028 return android_priv_cmd(bss, "PNOFORCE 0");
12029}
12030
12031#endif /* ANDROID */
12032
12033
9ebce9c5
JM
12034static int driver_nl80211_set_key(const char *ifname, void *priv,
12035 enum wpa_alg alg, const u8 *addr,
12036 int key_idx, int set_tx,
12037 const u8 *seq, size_t seq_len,
12038 const u8 *key, size_t key_len)
12039{
12040 struct i802_bss *bss = priv;
12041 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
12042 set_tx, seq, seq_len, key, key_len);
12043}
12044
12045
12046static int driver_nl80211_scan2(void *priv,
12047 struct wpa_driver_scan_params *params)
12048{
12049 struct i802_bss *bss = priv;
12050 return wpa_driver_nl80211_scan(bss, params);
12051}
12052
12053
12054static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
12055 int reason_code)
12056{
12057 struct i802_bss *bss = priv;
12058 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
12059}
12060
12061
12062static int driver_nl80211_authenticate(void *priv,
12063 struct wpa_driver_auth_params *params)
12064{
12065 struct i802_bss *bss = priv;
12066 return wpa_driver_nl80211_authenticate(bss, params);
12067}
12068
12069
12070static void driver_nl80211_deinit(void *priv)
12071{
12072 struct i802_bss *bss = priv;
12073 wpa_driver_nl80211_deinit(bss);
12074}
12075
12076
12077static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
12078 const char *ifname)
12079{
12080 struct i802_bss *bss = priv;
12081 return wpa_driver_nl80211_if_remove(bss, type, ifname);
12082}
12083
12084
12085static int driver_nl80211_send_mlme(void *priv, const u8 *data,
12086 size_t data_len, int noack)
12087{
12088 struct i802_bss *bss = priv;
12089 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
12090 0, 0, 0, 0);
12091}
12092
12093
12094static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
12095{
12096 struct i802_bss *bss = priv;
59d7148a 12097 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
9ebce9c5
JM
12098}
12099
12100
9ebce9c5
JM
12101static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
12102 const char *ifname, int vlan_id)
12103{
12104 struct i802_bss *bss = priv;
12105 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
12106}
9ebce9c5
JM
12107
12108
12109static int driver_nl80211_read_sta_data(void *priv,
12110 struct hostap_sta_driver_data *data,
12111 const u8 *addr)
12112{
12113 struct i802_bss *bss = priv;
12114 return i802_read_sta_data(bss, data, addr);
12115}
12116
12117
12118static int driver_nl80211_send_action(void *priv, unsigned int freq,
12119 unsigned int wait_time,
12120 const u8 *dst, const u8 *src,
12121 const u8 *bssid,
12122 const u8 *data, size_t data_len,
12123 int no_cck)
12124{
12125 struct i802_bss *bss = priv;
12126 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
12127 bssid, data, data_len, no_cck);
12128}
12129
12130
12131static int driver_nl80211_probe_req_report(void *priv, int report)
12132{
12133 struct i802_bss *bss = priv;
12134 return wpa_driver_nl80211_probe_req_report(bss, report);
12135}
12136
12137
6a1ce395
DG
12138static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
12139 const u8 *ies, size_t ies_len)
12140{
12141 int ret;
12142 struct nl_msg *msg;
12143 struct i802_bss *bss = priv;
12144 struct wpa_driver_nl80211_data *drv = bss->drv;
12145 u16 mdid = WPA_GET_LE16(md);
12146
12147 msg = nlmsg_alloc();
12148 if (!msg)
12149 return -ENOMEM;
12150
12151 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
12152 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
12153 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12154 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
12155 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
12156
12157 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12158 if (ret) {
12159 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
12160 "err=%d (%s)", ret, strerror(-ret));
12161 }
12162
12163 return ret;
12164
12165nla_put_failure:
12166 nlmsg_free(msg);
12167 return -ENOBUFS;
12168}
12169
12170
597b94f5
AS
12171const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
12172{
12173 struct i802_bss *bss = priv;
12174 struct wpa_driver_nl80211_data *drv = bss->drv;
12175
12176 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
12177 return NULL;
12178
12179 return bss->addr;
12180}
12181
12182
a771c07d
JM
12183static const char * scan_state_str(enum scan_states scan_state)
12184{
12185 switch (scan_state) {
12186 case NO_SCAN:
12187 return "NO_SCAN";
12188 case SCAN_REQUESTED:
12189 return "SCAN_REQUESTED";
12190 case SCAN_STARTED:
12191 return "SCAN_STARTED";
12192 case SCAN_COMPLETED:
12193 return "SCAN_COMPLETED";
12194 case SCAN_ABORTED:
12195 return "SCAN_ABORTED";
12196 case SCHED_SCAN_STARTED:
12197 return "SCHED_SCAN_STARTED";
12198 case SCHED_SCAN_STOPPED:
12199 return "SCHED_SCAN_STOPPED";
12200 case SCHED_SCAN_RESULTS:
12201 return "SCHED_SCAN_RESULTS";
12202 }
12203
12204 return "??";
12205}
12206
12207
12208static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
12209{
12210 struct i802_bss *bss = priv;
12211 struct wpa_driver_nl80211_data *drv = bss->drv;
12212 int res;
12213 char *pos, *end;
12214
12215 pos = buf;
12216 end = buf + buflen;
12217
12218 res = os_snprintf(pos, end - pos,
12219 "ifindex=%d\n"
12220 "ifname=%s\n"
12221 "brname=%s\n"
12222 "addr=" MACSTR "\n"
12223 "freq=%d\n"
12224 "%s%s%s%s%s",
12225 bss->ifindex,
12226 bss->ifname,
12227 bss->brname,
12228 MAC2STR(bss->addr),
12229 bss->freq,
12230 bss->beacon_set ? "beacon_set=1\n" : "",
12231 bss->added_if_into_bridge ?
12232 "added_if_into_bridge=1\n" : "",
12233 bss->added_bridge ? "added_bridge=1\n" : "",
12234 bss->in_deinit ? "in_deinit=1\n" : "",
12235 bss->if_dynamic ? "if_dynamic=1\n" : "");
12236 if (res < 0 || res >= end - pos)
12237 return pos - buf;
12238 pos += res;
12239
12240 if (bss->wdev_id_set) {
12241 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
12242 (unsigned long long) bss->wdev_id);
12243 if (res < 0 || res >= end - pos)
12244 return pos - buf;
12245 pos += res;
12246 }
12247
12248 res = os_snprintf(pos, end - pos,
12249 "phyname=%s\n"
fee354c7 12250 "perm_addr=" MACSTR "\n"
a771c07d
JM
12251 "drv_ifindex=%d\n"
12252 "operstate=%d\n"
12253 "scan_state=%s\n"
12254 "auth_bssid=" MACSTR "\n"
12255 "auth_attempt_bssid=" MACSTR "\n"
12256 "bssid=" MACSTR "\n"
12257 "prev_bssid=" MACSTR "\n"
12258 "associated=%d\n"
12259 "assoc_freq=%u\n"
12260 "monitor_sock=%d\n"
12261 "monitor_ifidx=%d\n"
12262 "monitor_refcount=%d\n"
12263 "last_mgmt_freq=%u\n"
12264 "eapol_tx_sock=%d\n"
97752f79 12265 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
a771c07d 12266 drv->phyname,
fee354c7 12267 MAC2STR(drv->perm_addr),
a771c07d
JM
12268 drv->ifindex,
12269 drv->operstate,
12270 scan_state_str(drv->scan_state),
12271 MAC2STR(drv->auth_bssid),
12272 MAC2STR(drv->auth_attempt_bssid),
12273 MAC2STR(drv->bssid),
12274 MAC2STR(drv->prev_bssid),
12275 drv->associated,
12276 drv->assoc_freq,
12277 drv->monitor_sock,
12278 drv->monitor_ifidx,
12279 drv->monitor_refcount,
12280 drv->last_mgmt_freq,
12281 drv->eapol_tx_sock,
12282 drv->ignore_if_down_event ?
12283 "ignore_if_down_event=1\n" : "",
12284 drv->scan_complete_events ?
12285 "scan_complete_events=1\n" : "",
12286 drv->disabled_11b_rates ?
12287 "disabled_11b_rates=1\n" : "",
12288 drv->pending_remain_on_chan ?
12289 "pending_remain_on_chan=1\n" : "",
12290 drv->in_interface_list ? "in_interface_list=1\n" : "",
12291 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
12292 drv->poll_command_supported ?
12293 "poll_command_supported=1\n" : "",
12294 drv->data_tx_status ? "data_tx_status=1\n" : "",
12295 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
12296 drv->retry_auth ? "retry_auth=1\n" : "",
12297 drv->use_monitor ? "use_monitor=1\n" : "",
12298 drv->ignore_next_local_disconnect ?
12299 "ignore_next_local_disconnect=1\n" : "",
d6a36f39 12300 drv->ignore_next_local_deauth ?
97752f79 12301 "ignore_next_local_deauth=1\n" : "");
a771c07d
JM
12302 if (res < 0 || res >= end - pos)
12303 return pos - buf;
12304 pos += res;
12305
12306 if (drv->has_capability) {
12307 res = os_snprintf(pos, end - pos,
12308 "capa.key_mgmt=0x%x\n"
12309 "capa.enc=0x%x\n"
12310 "capa.auth=0x%x\n"
24bd4e0b 12311 "capa.flags=0x%llx\n"
a771c07d
JM
12312 "capa.max_scan_ssids=%d\n"
12313 "capa.max_sched_scan_ssids=%d\n"
12314 "capa.sched_scan_supported=%d\n"
12315 "capa.max_match_sets=%d\n"
12316 "capa.max_remain_on_chan=%u\n"
12317 "capa.max_stations=%u\n"
12318 "capa.probe_resp_offloads=0x%x\n"
12319 "capa.max_acl_mac_addrs=%u\n"
12320 "capa.num_multichan_concurrent=%u\n",
12321 drv->capa.key_mgmt,
12322 drv->capa.enc,
12323 drv->capa.auth,
24bd4e0b 12324 (unsigned long long) drv->capa.flags,
a771c07d
JM
12325 drv->capa.max_scan_ssids,
12326 drv->capa.max_sched_scan_ssids,
12327 drv->capa.sched_scan_supported,
12328 drv->capa.max_match_sets,
12329 drv->capa.max_remain_on_chan,
12330 drv->capa.max_stations,
12331 drv->capa.probe_resp_offloads,
12332 drv->capa.max_acl_mac_addrs,
12333 drv->capa.num_multichan_concurrent);
12334 if (res < 0 || res >= end - pos)
12335 return pos - buf;
12336 pos += res;
12337 }
12338
12339 return pos - buf;
12340}
12341
12342
1c4ffa87
AO
12343static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
12344{
12345 if (settings->head)
12346 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
12347 settings->head_len, settings->head);
12348
12349 if (settings->tail)
12350 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
12351 settings->tail_len, settings->tail);
12352
12353 if (settings->beacon_ies)
12354 NLA_PUT(msg, NL80211_ATTR_IE,
12355 settings->beacon_ies_len, settings->beacon_ies);
12356
12357 if (settings->proberesp_ies)
12358 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
12359 settings->proberesp_ies_len, settings->proberesp_ies);
12360
12361 if (settings->assocresp_ies)
12362 NLA_PUT(msg,
12363 NL80211_ATTR_IE_ASSOC_RESP,
12364 settings->assocresp_ies_len, settings->assocresp_ies);
12365
12366 if (settings->probe_resp)
12367 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
12368 settings->probe_resp_len, settings->probe_resp);
12369
12370 return 0;
12371
12372nla_put_failure:
12373 return -ENOBUFS;
12374}
12375
12376
12377static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
12378{
12379 struct nl_msg *msg;
12380 struct i802_bss *bss = priv;
12381 struct wpa_driver_nl80211_data *drv = bss->drv;
12382 struct nlattr *beacon_csa;
12383 int ret = -ENOBUFS;
12384
8d1fdde7 12385 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
1c4ffa87 12386 settings->cs_count, settings->block_tx,
8d1fdde7
JD
12387 settings->freq_params.freq, settings->freq_params.bandwidth,
12388 settings->freq_params.center_freq1,
12389 settings->freq_params.center_freq2);
1c4ffa87 12390
991aa9c7 12391 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
1c4ffa87
AO
12392 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
12393 return -EOPNOTSUPP;
12394 }
12395
12396 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
12397 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
12398 return -EOPNOTSUPP;
12399
12400 /* check settings validity */
12401 if (!settings->beacon_csa.tail ||
12402 ((settings->beacon_csa.tail_len <=
12403 settings->counter_offset_beacon) ||
12404 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
12405 settings->cs_count)))
12406 return -EINVAL;
12407
12408 if (settings->beacon_csa.probe_resp &&
12409 ((settings->beacon_csa.probe_resp_len <=
12410 settings->counter_offset_presp) ||
12411 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
12412 settings->cs_count)))
12413 return -EINVAL;
12414
12415 msg = nlmsg_alloc();
12416 if (!msg)
12417 return -ENOMEM;
12418
12419 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
6782b684 12420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
1c4ffa87
AO
12421 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
12422 ret = nl80211_put_freq_params(msg, &settings->freq_params);
12423 if (ret)
12424 goto error;
12425
12426 if (settings->block_tx)
12427 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12428
12429 /* beacon_after params */
12430 ret = set_beacon_data(msg, &settings->beacon_after);
12431 if (ret)
12432 goto error;
12433
12434 /* beacon_csa params */
12435 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12436 if (!beacon_csa)
12437 goto nla_put_failure;
12438
12439 ret = set_beacon_data(msg, &settings->beacon_csa);
12440 if (ret)
12441 goto error;
12442
12443 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12444 settings->counter_offset_beacon);
12445
12446 if (settings->beacon_csa.probe_resp)
12447 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12448 settings->counter_offset_presp);
12449
12450 nla_nest_end(msg, beacon_csa);
12451 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12452 if (ret) {
12453 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12454 ret, strerror(-ret));
12455 }
12456 return ret;
12457
12458nla_put_failure:
12459 ret = -ENOBUFS;
12460error:
12461 nlmsg_free(msg);
12462 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12463 return ret;
12464}
12465
12466
6b9f7af6
JM
12467#ifdef CONFIG_TESTING_OPTIONS
12468static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12469{
12470 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12471 struct wpabuf *buf = arg;
12472
12473 if (!buf)
12474 return NL_SKIP;
12475
12476 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12477 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12478 return NL_SKIP;
12479 }
12480
12481 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12482 genlmsg_attrlen(gnlh, 0));
12483
12484 return NL_SKIP;
12485}
12486#endif /* CONFIG_TESTING_OPTIONS */
12487
12488
adef8948
BL
12489static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12490{
12491 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12492 struct nlattr *nl_vendor_reply, *nl;
12493 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12494 struct wpabuf *buf = arg;
12495 int rem;
12496
12497 if (!buf)
12498 return NL_SKIP;
12499
12500 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12501 genlmsg_attrlen(gnlh, 0), NULL);
12502 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12503
12504 if (!nl_vendor_reply)
12505 return NL_SKIP;
12506
12507 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12508 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12509 return NL_SKIP;
12510 }
12511
12512 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12513 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12514 }
12515
12516 return NL_SKIP;
12517}
12518
12519
12520static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12521 unsigned int subcmd, const u8 *data,
12522 size_t data_len, struct wpabuf *buf)
12523{
12524 struct i802_bss *bss = priv;
12525 struct wpa_driver_nl80211_data *drv = bss->drv;
12526 struct nl_msg *msg;
12527 int ret;
12528
12529 msg = nlmsg_alloc();
12530 if (!msg)
12531 return -ENOMEM;
12532
6b9f7af6
JM
12533#ifdef CONFIG_TESTING_OPTIONS
12534 if (vendor_id == 0xffffffff) {
12535 nl80211_cmd(drv, msg, 0, subcmd);
12536 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12537 0)
12538 goto nla_put_failure;
12539 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12540 if (ret)
12541 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12542 ret);
12543 return ret;
12544 }
12545#endif /* CONFIG_TESTING_OPTIONS */
12546
adef8948
BL
12547 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12548 if (nl80211_set_iface_id(msg, bss) < 0)
12549 goto nla_put_failure;
12550 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12551 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12552 if (data)
12553 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12554
12555 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12556 if (ret)
12557 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12558 ret);
12559 return ret;
12560
12561nla_put_failure:
12562 nlmsg_free(msg);
12563 return -ENOBUFS;
12564}
12565
12566
049105b4
KP
12567static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12568 u8 qos_map_set_len)
12569{
12570 struct i802_bss *bss = priv;
12571 struct wpa_driver_nl80211_data *drv = bss->drv;
12572 struct nl_msg *msg;
12573 int ret;
12574
12575 msg = nlmsg_alloc();
12576 if (!msg)
12577 return -ENOMEM;
12578
12579 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12580 qos_map_set, qos_map_set_len);
12581
12582 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12583 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12584 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12585
12586 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12587 if (ret)
12588 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12589
12590 return ret;
12591
12592nla_put_failure:
12593 nlmsg_free(msg);
12594 return -ENOBUFS;
12595}
12596
12597
e4fa8b12
EP
12598static int nl80211_set_wowlan(void *priv,
12599 const struct wowlan_triggers *triggers)
12600{
12601 struct i802_bss *bss = priv;
12602 struct wpa_driver_nl80211_data *drv = bss->drv;
12603 struct nl_msg *msg;
12604 struct nlattr *wowlan_triggers;
12605 int ret;
12606
12607 msg = nlmsg_alloc();
12608 if (!msg)
12609 return -ENOMEM;
12610
12611 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12612
12613 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12614 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12615
12616 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12617 if (!wowlan_triggers)
12618 goto nla_put_failure;
12619
12620 if (triggers->any)
12621 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12622 if (triggers->disconnect)
12623 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12624 if (triggers->magic_pkt)
12625 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12626 if (triggers->gtk_rekey_failure)
12627 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12628 if (triggers->eap_identity_req)
12629 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12630 if (triggers->four_way_handshake)
12631 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12632 if (triggers->rfkill_release)
12633 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12634
12635 nla_nest_end(msg, wowlan_triggers);
12636
12637 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12638 if (ret)
12639 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12640
12641 return ret;
12642
12643nla_put_failure:
12644 nlmsg_free(msg);
12645 return -ENOBUFS;
12646}
12647
12648
0800f9ee
JM
12649static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
12650{
12651 struct i802_bss *bss = priv;
12652 struct wpa_driver_nl80211_data *drv = bss->drv;
12653 struct nl_msg *msg;
12654 struct nlattr *params;
12655
12656 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
12657
12658 if (!drv->roaming_vendor_cmd_avail) {
12659 wpa_printf(MSG_DEBUG,
12660 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
12661 return -1;
12662 }
12663
12664 msg = nlmsg_alloc();
12665 if (!msg)
12666 return -ENOMEM;
12667
12668 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12669
12670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12671 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
12672 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12673 QCA_NL80211_VENDOR_SUBCMD_ROAMING);
12674
12675 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
12676 if (!params)
12677 goto nla_put_failure;
12678 NLA_PUT_U32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
12679 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
12680 QCA_ROAMING_NOT_ALLOWED);
12681 if (bssid)
12682 NLA_PUT(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid);
12683 nla_nest_end(msg, params);
12684
12685 return send_and_recv_msgs(drv, msg, NULL, NULL);
12686
12687 nla_put_failure:
12688 nlmsg_free(msg);
12689 return -1;
12690}
12691
12692
fee354c7
JM
12693static int nl80211_set_mac_addr(void *priv, const u8 *addr)
12694{
12695 struct i802_bss *bss = priv;
12696 struct wpa_driver_nl80211_data *drv = bss->drv;
12697 int new_addr = addr != NULL;
12698
12699 if (!addr)
12700 addr = drv->perm_addr;
12701
12702 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
12703 return -1;
12704
12705 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
12706 {
12707 wpa_printf(MSG_DEBUG,
12708 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12709 bss->ifname, MAC2STR(addr));
12710 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
12711 1) < 0) {
12712 wpa_printf(MSG_DEBUG,
12713 "nl80211: Could not restore interface UP after failed set_mac_addr");
12714 }
12715 return -1;
12716 }
12717
12718 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
12719 bss->ifname, MAC2STR(addr));
12720 drv->addr_changed = new_addr;
12721 os_memcpy(bss->addr, addr, ETH_ALEN);
12722
12723 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12724 {
12725 wpa_printf(MSG_DEBUG,
12726 "nl80211: Could not restore interface UP after set_mac_addr");
12727 }
12728
12729 return 0;
12730}
12731
12732
6c1664f6
BC
12733#ifdef CONFIG_MESH
12734
12735static int wpa_driver_nl80211_init_mesh(void *priv)
12736{
12737 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
12738 wpa_printf(MSG_INFO,
12739 "nl80211: Failed to set interface into mesh mode");
12740 return -1;
12741 }
12742 return 0;
12743}
12744
12745
12746static int
12747wpa_driver_nl80211_join_mesh(void *priv,
12748 struct wpa_driver_mesh_join_params *params)
12749{
12750 struct i802_bss *bss = priv;
12751 struct wpa_driver_nl80211_data *drv = bss->drv;
12752 struct nl_msg *msg;
12753 struct nlattr *container;
12754 int ret = 0;
12755
12756 msg = nlmsg_alloc();
12757 if (!msg)
12758 return -1;
12759
12760 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
12761 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_MESH);
12762
12763 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12764 /* XXX: need chtype too in case we want HT */
12765 if (params->freq) {
12766 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
12767 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
12768 }
12769
12770 if (params->basic_rates) {
12771 u8 rates[NL80211_MAX_SUPP_RATES];
12772 u8 rates_len = 0;
12773 int i;
12774
12775 for (i = 0; i < NL80211_MAX_SUPP_RATES; i++) {
12776 if (params->basic_rates[i] < 0)
12777 break;
12778 rates[rates_len++] = params->basic_rates[i] / 5;
12779 }
12780
12781 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
12782 }
12783
12784 if (params->meshid) {
12785 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
12786 params->meshid, params->meshid_len);
12787 NLA_PUT(msg, NL80211_ATTR_MESH_ID, params->meshid_len,
12788 params->meshid);
12789 }
12790
12791 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
12792
12793 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
12794 if (!container)
12795 goto nla_put_failure;
12796
12797 if (params->ies) {
12798 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
12799 NLA_PUT(msg, NL80211_MESH_SETUP_IE, params->ie_len,
12800 params->ies);
12801 }
12802 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
12803 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
12804 NLA_PUT_U8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1);
12805 NLA_PUT_FLAG(msg, NL80211_MESH_SETUP_USERSPACE_AUTH);
12806 }
12807 if (params->flags & WPA_DRIVER_MESH_FLAG_AMPE)
12808 NLA_PUT_FLAG(msg, NL80211_MESH_SETUP_USERSPACE_AMPE);
12809 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
12810 NLA_PUT_FLAG(msg, NL80211_MESH_SETUP_USERSPACE_MPM);
12811 nla_nest_end(msg, container);
12812
12813 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
12814 if (!container)
12815 goto nla_put_failure;
12816
12817 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS))
12818 NLA_PUT_U32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0);
12819 nla_nest_end(msg, container);
12820
12821 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12822 msg = NULL;
12823 if (ret) {
12824 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
12825 ret, strerror(-ret));
12826 goto nla_put_failure;
12827 }
12828 ret = 0;
12829 bss->freq = params->freq;
12830 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
12831
12832
12833nla_put_failure:
12834 nlmsg_free(msg);
12835 return ret;
12836}
12837
12838
12839static int wpa_driver_nl80211_leave_mesh(void *priv)
12840{
12841 struct i802_bss *bss = priv;
12842 struct wpa_driver_nl80211_data *drv = bss->drv;
12843 struct nl_msg *msg;
12844 int ret = 0;
12845
12846 msg = nlmsg_alloc();
12847 if (!msg)
12848 return -1;
12849
12850 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
12851 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_MESH);
12852 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12853
12854 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12855 msg = NULL;
12856 if (ret) {
12857 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
12858 ret, strerror(-ret));
12859 goto nla_put_failure;
12860 }
12861 ret = 0;
12862 wpa_printf(MSG_DEBUG, "nl80211: mesh leave request send successfully");
12863
12864nla_put_failure:
12865 nlmsg_free(msg);
12866 return ret;
12867}
12868
12869#endif /* CONFIG_MESH */
12870
12871
3f5285e8
JM
12872const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12873 .name = "nl80211",
12874 .desc = "Linux nl80211/cfg80211",
12875 .get_bssid = wpa_driver_nl80211_get_bssid,
12876 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
12877 .set_key = driver_nl80211_set_key,
12878 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
12879 .sched_scan = wpa_driver_nl80211_sched_scan,
12880 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 12881 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9ebce9c5
JM
12882 .deauthenticate = driver_nl80211_deauthenticate,
12883 .authenticate = driver_nl80211_authenticate,
3f5285e8 12884 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
12885 .global_init = nl80211_global_init,
12886 .global_deinit = nl80211_global_deinit,
12887 .init2 = wpa_driver_nl80211_init,
9ebce9c5 12888 .deinit = driver_nl80211_deinit,
3f5285e8
JM
12889 .get_capa = wpa_driver_nl80211_get_capa,
12890 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 12891 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 12892 .set_country = wpa_driver_nl80211_set_country,
f0793bf1 12893 .get_country = wpa_driver_nl80211_get_country,
19c3b566 12894 .set_ap = wpa_driver_nl80211_set_ap,
3c4ca363 12895 .set_acl = wpa_driver_nl80211_set_acl,
22a7c9d7 12896 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
12897 .if_remove = driver_nl80211_if_remove,
12898 .send_mlme = driver_nl80211_send_mlme,
c3965310 12899 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
0f4e8b4f 12900 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 12901 .sta_remove = driver_nl80211_sta_remove,
db149ac9 12902 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 12903 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837 12904 .hapd_init = i802_init,
c5121837 12905 .hapd_deinit = i802_deinit,
f7b3920c 12906 .set_wds_sta = i802_set_wds_sta,
c5121837
JM
12907 .get_seqnum = i802_get_seqnum,
12908 .flush = i802_flush,
c5121837
JM
12909 .get_inact_sec = i802_get_inact_sec,
12910 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
12911 .set_rts = i802_set_rts,
12912 .set_frag = i802_set_frag,
c5121837 12913 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 12914 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
12915 .sta_deauth = i802_sta_deauth,
12916 .sta_disassoc = i802_sta_disassoc,
9ebce9c5 12917 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 12918 .set_freq = i802_set_freq,
9ebce9c5 12919 .send_action = driver_nl80211_send_action,
5dfca53f 12920 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
12921 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12922 .cancel_remain_on_channel =
12923 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 12924 .probe_req_report = driver_nl80211_probe_req_report,
af473088 12925 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 12926 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 12927 .resume = wpa_driver_nl80211_resume,
7b90c16a 12928 .send_ft_action = nl80211_send_ft_action,
b625473c 12929 .signal_monitor = nl80211_signal_monitor,
1c5c7273 12930 .signal_poll = nl80211_signal_poll,
b91ab76e 12931 .send_frame = nl80211_send_frame,
57ebba59 12932 .shared_freq = wpa_driver_nl80211_shared_freq,
c55f774d 12933 .set_param = nl80211_set_param,
6859f1cb 12934 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
12935 .add_pmkid = nl80211_add_pmkid,
12936 .remove_pmkid = nl80211_remove_pmkid,
12937 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 12938 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 12939 .poll_client = nl80211_poll_client,
29f338af 12940 .set_p2p_powersave = nl80211_set_p2p_powersave,
f90e9c1c 12941 .start_dfs_cac = nl80211_start_radar_detection,
695c7038 12942 .stop_ap = wpa_driver_nl80211_stop_ap,
03ea1786
AN
12943#ifdef CONFIG_TDLS
12944 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12945 .tdls_oper = nl80211_tdls_oper,
12946#endif /* CONFIG_TDLS */
6a1ce395 12947 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
597b94f5 12948 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
0185007c 12949 .get_survey = wpa_driver_nl80211_get_survey,
a771c07d 12950 .status = wpa_driver_nl80211_status,
1c4ffa87 12951 .switch_channel = nl80211_switch_channel,
0de38036
JM
12952#ifdef ANDROID_P2P
12953 .set_noa = wpa_driver_set_p2p_noa,
12954 .get_noa = wpa_driver_get_p2p_noa,
12955 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
12956#endif /* ANDROID_P2P */
5e2c3490
JM
12957#ifdef ANDROID
12958 .driver_cmd = wpa_driver_nl80211_driver_cmd,
12959#endif /* ANDROID */
adef8948 12960 .vendor_cmd = nl80211_vendor_cmd,
049105b4 12961 .set_qos_map = nl80211_set_qos_map,
e4fa8b12 12962 .set_wowlan = nl80211_set_wowlan,
0800f9ee 12963 .roaming = nl80211_roaming,
fee354c7 12964 .set_mac_addr = nl80211_set_mac_addr,
6c1664f6
BC
12965#ifdef CONFIG_MESH
12966 .init_mesh = wpa_driver_nl80211_init_mesh,
12967 .join_mesh = wpa_driver_nl80211_join_mesh,
12968 .leave_mesh = wpa_driver_nl80211_leave_mesh,
12969#endif /* CONFIG_MESH */
3f5285e8 12970};