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