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