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