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