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