]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
Remove unnecessary build #ifdef from definitions
[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
JB
4052 struct wpa_driver_nl80211_data *drv = bss->drv;
4053
4054 if (nl80211_alloc_mgmt_handle(bss))
4055 return -1;
36488c05
JM
4056 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4057 "handle %p", bss->nl_mgmt);
a11241fa 4058
e8d1168b
JB
4059 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4060 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4061
4062 /* register for any AUTH message */
4063 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4064 }
4065
56f5af48
JM
4066#ifdef CONFIG_INTERWORKING
4067 /* QoS Map Configure */
4068 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
4069 return -1;
4070#endif /* CONFIG_INTERWORKING */
4fe9fa0d 4071#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
046b26a2 4072 /* GAS Initial Request */
a11241fa 4073 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
046b26a2
JM
4074 return -1;
4075 /* GAS Initial Response */
a11241fa 4076 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
046b26a2 4077 return -1;
18708aad 4078 /* GAS Comeback Request */
a11241fa 4079 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
18708aad
JM
4080 return -1;
4081 /* GAS Comeback Response */
a11241fa 4082 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
18708aad 4083 return -1;
4fe9fa0d
JM
4084#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4085#ifdef CONFIG_P2P
046b26a2 4086 /* P2P Public Action */
a11241fa 4087 if (nl80211_register_action_frame(bss,
046b26a2
JM
4088 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4089 6) < 0)
4090 return -1;
4091 /* P2P Action */
a11241fa 4092 if (nl80211_register_action_frame(bss,
046b26a2
JM
4093 (u8 *) "\x7f\x50\x6f\x9a\x09",
4094 5) < 0)
4095 return -1;
4096#endif /* CONFIG_P2P */
7d878ca7
JM
4097#ifdef CONFIG_IEEE80211W
4098 /* SA Query Response */
a11241fa 4099 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
7d878ca7
JM
4100 return -1;
4101#endif /* CONFIG_IEEE80211W */
35287637
AN
4102#ifdef CONFIG_TDLS
4103 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4104 /* TDLS Discovery Response */
aa543c0c 4105 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
35287637
AN
4106 0)
4107 return -1;
4108 }
4109#endif /* CONFIG_TDLS */
046b26a2 4110
7b90c16a 4111 /* FT Action frames */
a11241fa 4112 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
7b90c16a
JM
4113 return -1;
4114 else
4115 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4116 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4117
71269b37 4118 /* WNM - BSS Transition Management Request */
a11241fa
JB
4119 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
4120 return -1;
bd896433
JM
4121 /* WNM-Sleep Mode Response */
4122 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
4123 return -1;
a11241fa 4124
5f65e9f7
JB
4125 nl80211_mgmt_handle_register_eloop(bss);
4126
a11241fa
JB
4127 return 0;
4128}
4129
4130
02bb32c3
JB
4131static int nl80211_register_spurious_class3(struct i802_bss *bss)
4132{
4133 struct wpa_driver_nl80211_data *drv = bss->drv;
4134 struct nl_msg *msg;
4135 int ret = -1;
4136
4137 msg = nlmsg_alloc();
4138 if (!msg)
4139 return -1;
4140
4141 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4142
4143 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4144
481234cf 4145 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
4146 msg = NULL;
4147 if (ret) {
4148 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4149 "failed: ret=%d (%s)",
4150 ret, strerror(-ret));
4151 goto nla_put_failure;
4152 }
4153 ret = 0;
4154nla_put_failure:
4155 nlmsg_free(msg);
4156 return ret;
4157}
4158
4159
a11241fa
JB
4160static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4161{
4162 static const int stypes[] = {
4163 WLAN_FC_STYPE_AUTH,
4164 WLAN_FC_STYPE_ASSOC_REQ,
4165 WLAN_FC_STYPE_REASSOC_REQ,
4166 WLAN_FC_STYPE_DISASSOC,
4167 WLAN_FC_STYPE_DEAUTH,
4168 WLAN_FC_STYPE_ACTION,
4169 WLAN_FC_STYPE_PROBE_REQ,
4170/* Beacon doesn't work as mac80211 doesn't currently allow
4171 * it, but it wouldn't really be the right thing anyway as
4172 * it isn't per interface ... maybe just dump the scan
4173 * results periodically for OLBC?
4174 */
4175// WLAN_FC_STYPE_BEACON,
4176 };
4177 unsigned int i;
4178
4179 if (nl80211_alloc_mgmt_handle(bss))
71269b37 4180 return -1;
36488c05
JM
4181 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4182 "handle %p", bss->nl_mgmt);
71269b37 4183
e7ecab4a 4184 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
481234cf 4185 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
4186 (WLAN_FC_TYPE_MGMT << 2) |
4187 (stypes[i] << 4),
4188 NULL, 0) < 0) {
4189 goto out_err;
4190 }
4191 }
4192
02bb32c3
JB
4193 if (nl80211_register_spurious_class3(bss))
4194 goto out_err;
4195
e32ad281
JB
4196 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4197 goto out_err;
4198
5f65e9f7 4199 nl80211_mgmt_handle_register_eloop(bss);
58f6fbe0 4200 return 0;
a11241fa
JB
4201
4202out_err:
a11241fa
JB
4203 nl_destroy_handles(&bss->nl_mgmt);
4204 return -1;
4205}
4206
4207
a6cc0602
JM
4208static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4209{
4210 if (nl80211_alloc_mgmt_handle(bss))
4211 return -1;
4212 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4213 "handle %p (device SME)", bss->nl_mgmt);
4214
4215 if (nl80211_register_frame(bss, bss->nl_mgmt,
4216 (WLAN_FC_TYPE_MGMT << 2) |
4217 (WLAN_FC_STYPE_ACTION << 4),
4218 NULL, 0) < 0)
4219 goto out_err;
4220
5f65e9f7 4221 nl80211_mgmt_handle_register_eloop(bss);
a6cc0602
JM
4222 return 0;
4223
4224out_err:
a6cc0602
JM
4225 nl_destroy_handles(&bss->nl_mgmt);
4226 return -1;
4227}
4228
4229
36488c05 4230static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 4231{
481234cf 4232 if (bss->nl_mgmt == NULL)
a11241fa 4233 return;
36488c05
JM
4234 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4235 "(%s)", bss->nl_mgmt, reason);
5f65e9f7 4236 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
e32ad281
JB
4237
4238 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
4239}
4240
4241
8401a6b0
JM
4242static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4243{
4244 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4245}
4246
4247
eb4582f2
AS
4248static void nl80211_del_p2pdev(struct i802_bss *bss)
4249{
4250 struct wpa_driver_nl80211_data *drv = bss->drv;
4251 struct nl_msg *msg;
4252 int ret;
4253
4254 msg = nlmsg_alloc();
4255 if (!msg)
4256 return;
4257
4258 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4259 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4260
4261 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4262 msg = NULL;
4263
4264 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4265 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 4266 strerror(-ret));
eb4582f2
AS
4267
4268nla_put_failure:
4269 nlmsg_free(msg);
4270}
4271
4272
eb4582f2 4273static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
f632e483
AS
4274{
4275 struct wpa_driver_nl80211_data *drv = bss->drv;
4276 struct nl_msg *msg;
4277 int ret = -1;
4278
f632e483
AS
4279 msg = nlmsg_alloc();
4280 if (!msg)
4281 return -1;
4282
eb4582f2
AS
4283 if (start)
4284 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4285 else
4286 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4287
f632e483
AS
4288 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4289
4290 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4291 msg = NULL;
4292
eb4582f2
AS
4293 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4294 start ? "Start" : "Stop",
4295 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 4296 strerror(-ret));
eb4582f2 4297
f632e483
AS
4298nla_put_failure:
4299 nlmsg_free(msg);
4300 return ret;
4301}
f632e483
AS
4302
4303
91724d6f
AS
4304static int i802_set_iface_flags(struct i802_bss *bss, int up)
4305{
4306 enum nl80211_iftype nlmode;
4307
4308 nlmode = nl80211_get_ifmode(bss);
4309 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4310 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4311 bss->ifname, up);
4312 }
4313
4314 /* P2P Device has start/stop which is equivalent */
4315 return nl80211_set_p2pdev(bss, up);
4316}
4317
4318
362f781e 4319static int
0d547d5f 4320wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
49b4b205 4321 const u8 *set_addr, int first)
7524cfb1 4322{
834ee56f 4323 struct i802_bss *bss = drv->first_bss;
8401a6b0 4324 int send_rfkill_event = 0;
0d547d5f 4325 enum nl80211_iftype nlmode;
a2e40bb6
FF
4326
4327 drv->ifindex = if_nametoindex(bss->ifname);
f632e483
AS
4328 bss->ifindex = drv->ifindex;
4329 bss->wdev_id = drv->global->if_add_wdevid;
4330 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4331
60b13c20
IP
4332 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4333 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
f632e483
AS
4334 drv->global->if_add_wdevid_set = 0;
4335
4336 if (wpa_driver_nl80211_capa(drv))
4337 return -1;
a87c9d96 4338
5fbcb45d
AS
4339 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4340 bss->ifname, drv->phyname);
4341
0d547d5f
JM
4342 if (set_addr &&
4343 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4344 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4345 set_addr)))
4346 return -1;
4347
49b4b205
JM
4348 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4349 drv->start_mode_ap = 1;
4350
0d547d5f
JM
4351 if (drv->hostapd)
4352 nlmode = NL80211_IFTYPE_AP;
4353 else if (bss->if_dynamic)
8e12685c 4354 nlmode = nl80211_get_ifmode(bss);
0d547d5f
JM
4355 else
4356 nlmode = NL80211_IFTYPE_STATION;
f632e483 4357
8e12685c 4358 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
0d547d5f 4359 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
8e12685c 4360 return -1;
a87c9d96
JM
4361 }
4362
f632e483 4363 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
eb4582f2 4364 int ret = nl80211_set_p2pdev(bss, 1);
f632e483
AS
4365 if (ret < 0)
4366 wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
597b94f5 4367 nl80211_get_macaddr(bss);
f632e483
AS
4368 return ret;
4369 }
4370
c81eff1a 4371 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
8401a6b0
JM
4372 if (rfkill_is_blocked(drv->rfkill)) {
4373 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4374 "interface '%s' due to rfkill",
4375 bss->ifname);
a63063b4 4376 drv->if_disabled = 1;
8401a6b0
JM
4377 send_rfkill_event = 1;
4378 } else {
4379 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4380 "interface '%s' UP", bss->ifname);
4381 return -1;
4382 }
362f781e 4383 }
3f5285e8 4384
0d547d5f
JM
4385 if (!drv->hostapd)
4386 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4387 1, IF_OPER_DORMANT);
362f781e 4388
c81eff1a 4389 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
341eebee 4390 bss->addr))
2136f480 4391 return -1;
f2ed8023 4392
8401a6b0
JM
4393 if (send_rfkill_event) {
4394 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4395 drv, drv->ctx);
4396 }
4397
362f781e 4398 return 0;
3f5285e8
JM
4399}
4400
4401
8a27af5c
JM
4402static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4403{
4404 struct nl_msg *msg;
4405
4406 msg = nlmsg_alloc();
4407 if (!msg)
4408 return -ENOMEM;
4409
08e55ebb
JM
4410 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4411 drv->ifindex);
9fb04070 4412 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
8a27af5c
JM
4413 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4414
4415 return send_and_recv_msgs(drv, msg, NULL, NULL);
4416 nla_put_failure:
5883168a 4417 nlmsg_free(msg);
8a27af5c
JM
4418 return -ENOBUFS;
4419}
8a27af5c
JM
4420
4421
3f5285e8 4422/**
7e5ba1b9 4423 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
9ebce9c5 4424 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
4425 *
4426 * Shut down driver interface and processing of driver events. Free
4427 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4428 */
9ebce9c5 4429static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
3f5285e8 4430{
a2e40bb6 4431 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 4432
873d0fcf 4433 bss->in_deinit = 1;
32ab4855
JB
4434 if (drv->data_tx_status)
4435 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
4436 if (drv->eapol_tx_sock >= 0)
4437 close(drv->eapol_tx_sock);
f10bfc9a 4438
481234cf 4439 if (bss->nl_preq)
5582a5d1 4440 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 4441 if (bss->added_if_into_bridge) {
c81eff1a
BG
4442 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4443 bss->ifname) < 0)
94627f6c
JM
4444 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4445 "interface %s from bridge %s: %s",
e17a2477 4446 bss->ifname, bss->brname, strerror(errno));
94627f6c 4447 }
e17a2477 4448 if (bss->added_bridge) {
c81eff1a 4449 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
4450 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4451 "bridge %s: %s",
e17a2477 4452 bss->brname, strerror(errno));
94627f6c
JM
4453 }
4454
460456f8 4455 nl80211_remove_monitor_interface(drv);
8a27af5c 4456
b1f625e0 4457 if (is_ap_interface(drv->nlmode))
8a27af5c 4458 wpa_driver_nl80211_del_beacon(drv);
0915d02c 4459
bbaf0837
JM
4460 if (drv->eapol_sock >= 0) {
4461 eloop_unregister_read_sock(drv->eapol_sock);
4462 close(drv->eapol_sock);
4463 }
4464
4465 if (drv->if_indices != drv->default_if_indices)
4466 os_free(drv->if_indices);
3f5285e8 4467
b3af99d2 4468 if (drv->disabled_11b_rates)
4e5cb1a3
JM
4469 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4470
36d84860
BG
4471 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4472 IF_OPER_UP);
8401a6b0 4473 rfkill_deinit(drv->rfkill);
3f5285e8 4474
bbaf0837
JM
4475 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4476
146fa9b3
JM
4477 if (!drv->start_iface_up)
4478 (void) i802_set_iface_flags(bss, 0);
8e12685c 4479 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
49b4b205
JM
4480 if (!drv->hostapd || !drv->start_mode_ap)
4481 wpa_driver_nl80211_set_mode(bss,
4482 NL80211_IFTYPE_STATION);
b378c41f 4483 nl80211_mgmt_unsubscribe(bss, "deinit");
8e12685c
AS
4484 } else {
4485 nl80211_mgmt_unsubscribe(bss, "deinit");
eb4582f2 4486 nl80211_del_p2pdev(bss);
8e12685c 4487 }
1afc986d 4488 nl_cb_put(drv->nl_cb);
3f5285e8 4489
834ee56f 4490 nl80211_destroy_bss(drv->first_bss);
cc7a48d1 4491
3812464c
JM
4492 os_free(drv->filter_ssids);
4493
536fd62d
JM
4494 os_free(drv->auth_ie);
4495
dac12351 4496 if (drv->in_interface_list)
f2ed8023
JM
4497 dl_list_del(&drv->list);
4498
8cd6b7bc
JB
4499 os_free(drv->extended_capa);
4500 os_free(drv->extended_capa_mask);
834ee56f 4501 os_free(drv->first_bss);
3f5285e8
JM
4502 os_free(drv);
4503}
4504
4505
4506/**
4507 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
ad1e68e6 4508 * @eloop_ctx: Driver private data
3f5285e8
JM
4509 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4510 *
4511 * This function can be used as registered timeout when starting a scan to
4512 * generate a scan completed event if the driver does not report this.
4513 */
4514static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4515{
ad1e68e6 4516 struct wpa_driver_nl80211_data *drv = eloop_ctx;
b1f625e0 4517 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
834ee56f 4518 wpa_driver_nl80211_set_mode(drv->first_bss,
b1f625e0
EP
4519 drv->ap_scan_as_station);
4520 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6 4521 }
3f5285e8
JM
4522 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4523 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4524}
4525
4526
95ac3bf4
JM
4527static struct nl_msg *
4528nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
d3aaef80 4529 struct wpa_driver_scan_params *params, u64 *wdev_id)
3f5285e8 4530{
95ac3bf4 4531 struct nl_msg *msg;
6a1063e0 4532 size_t i;
3f5285e8 4533
0e75527f 4534 msg = nlmsg_alloc();
f0494d0f 4535 if (!msg)
95ac3bf4 4536 return NULL;
3812464c 4537
95ac3bf4 4538 nl80211_cmd(drv, msg, 0, cmd);
0e75527f 4539
d3aaef80
DS
4540 if (!wdev_id)
4541 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4542 else
4543 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
3f5285e8 4544
f0494d0f 4545 if (params->num_ssids) {
8970bae8
JB
4546 struct nlattr *ssids;
4547
4548 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
f0494d0f 4549 if (ssids == NULL)
95ac3bf4 4550 goto fail;
f0494d0f
JM
4551 for (i = 0; i < params->num_ssids; i++) {
4552 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4553 params->ssids[i].ssid,
4554 params->ssids[i].ssid_len);
8970bae8
JB
4555 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4556 params->ssids[i].ssid) < 0)
95ac3bf4 4557 goto fail;
f0494d0f 4558 }
8970bae8 4559 nla_nest_end(msg, ssids);
3f5285e8
JM
4560 }
4561
d173df52 4562 if (params->extra_ies) {
3b1c7bfd
JM
4563 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4564 params->extra_ies, params->extra_ies_len);
95ac3bf4
JM
4565 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4566 params->extra_ies) < 0)
4567 goto fail;
d173df52
JM
4568 }
4569
d3a98225 4570 if (params->freqs) {
8970bae8
JB
4571 struct nlattr *freqs;
4572 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
f0494d0f 4573 if (freqs == NULL)
95ac3bf4 4574 goto fail;
9fad706c
JM
4575 for (i = 0; params->freqs[i]; i++) {
4576 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4577 "MHz", params->freqs[i]);
8970bae8 4578 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
95ac3bf4 4579 goto fail;
9fad706c 4580 }
8970bae8 4581 nla_nest_end(msg, freqs);
d3a98225
JM
4582 }
4583
95ac3bf4
JM
4584 os_free(drv->filter_ssids);
4585 drv->filter_ssids = params->filter_ssids;
4586 params->filter_ssids = NULL;
4587 drv->num_filter_ssids = params->num_filter_ssids;
4588
4589 return msg;
4590
4591fail:
d3aaef80 4592nla_put_failure:
95ac3bf4
JM
4593 nlmsg_free(msg);
4594 return NULL;
4595}
4596
4597
4598/**
4599 * wpa_driver_nl80211_scan - Request the driver to initiate scan
9ebce9c5 4600 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
95ac3bf4
JM
4601 * @params: Scan parameters
4602 * Returns: 0 on success, -1 on failure
4603 */
9ebce9c5 4604static int wpa_driver_nl80211_scan(struct i802_bss *bss,
95ac3bf4
JM
4605 struct wpa_driver_scan_params *params)
4606{
95ac3bf4
JM
4607 struct wpa_driver_nl80211_data *drv = bss->drv;
4608 int ret = -1, timeout;
8970bae8 4609 struct nl_msg *msg = NULL;
95ac3bf4 4610
565110cd 4611 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
95ac3bf4
JM
4612 drv->scan_for_auth = 0;
4613
d3aaef80
DS
4614 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4615 bss->wdev_id_set ? &bss->wdev_id : NULL);
95ac3bf4
JM
4616 if (!msg)
4617 return -1;
4618
47185fc7 4619 if (params->p2p_probe) {
8970bae8
JB
4620 struct nlattr *rates;
4621
8a6a1e1b
JM
4622 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4623
8970bae8 4624 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
f0494d0f
JM
4625 if (rates == NULL)
4626 goto nla_put_failure;
4627
47185fc7
RM
4628 /*
4629 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4630 * by masking out everything else apart from the OFDM rates 6,
4631 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4632 * rates are left enabled.
4633 */
8970bae8 4634 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
47185fc7 4635 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8970bae8 4636 nla_nest_end(msg, rates);
970fa12e
RM
4637
4638 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
47185fc7
RM
4639 }
4640
0e75527f
JM
4641 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4642 msg = NULL;
4643 if (ret) {
4644 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4645 "(%s)", ret, strerror(-ret));
04eff7d5 4646 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
ad1e68e6
JM
4647 /*
4648 * mac80211 does not allow scan requests in AP mode, so
4649 * try to do this in station mode.
4650 */
b1f625e0
EP
4651 if (wpa_driver_nl80211_set_mode(
4652 bss, NL80211_IFTYPE_STATION))
ad1e68e6
JM
4653 goto nla_put_failure;
4654
085b29f1 4655 if (wpa_driver_nl80211_scan(bss, params)) {
b1f625e0 4656 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
ad1e68e6
JM
4657 goto nla_put_failure;
4658 }
4659
4660 /* Restore AP mode when processing scan results */
b1f625e0 4661 drv->ap_scan_as_station = drv->nlmode;
ad1e68e6
JM
4662 ret = 0;
4663 } else
4664 goto nla_put_failure;
3f5285e8
JM
4665 }
4666
a771c07d 4667 drv->scan_state = SCAN_REQUESTED;
3f5285e8
JM
4668 /* Not all drivers generate "scan completed" wireless event, so try to
4669 * read results after a timeout. */
0e75527f 4670 timeout = 10;
3f5285e8
JM
4671 if (drv->scan_complete_events) {
4672 /*
d173df52
JM
4673 * The driver seems to deliver events to notify when scan is
4674 * complete, so use longer timeout to avoid race conditions
4675 * with scanning and following association request.
3f5285e8
JM
4676 */
4677 timeout = 30;
4678 }
4679 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4680 "seconds", ret, timeout);
4681 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
0e75527f
JM
4682 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4683 drv, drv->ctx);
3f5285e8 4684
0e75527f 4685nla_put_failure:
0e75527f 4686 nlmsg_free(msg);
3f5285e8
JM
4687 return ret;
4688}
4689
4690
d21c63b9
LC
4691/**
4692 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4693 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4694 * @params: Scan parameters
4695 * @interval: Interval between scan cycles in milliseconds
4696 * Returns: 0 on success, -1 on failure or if not supported
4697 */
4698static int wpa_driver_nl80211_sched_scan(void *priv,
4699 struct wpa_driver_scan_params *params,
4700 u32 interval)
4701{
4702 struct i802_bss *bss = priv;
4703 struct wpa_driver_nl80211_data *drv = bss->drv;
6afbc3d6 4704 int ret = -1;
95ac3bf4 4705 struct nl_msg *msg;
d21c63b9
LC
4706 size_t i;
4707
565110cd
JM
4708 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4709
216eede8
DS
4710#ifdef ANDROID
4711 if (!drv->capa.sched_scan_supported)
4712 return android_pno_start(bss, params);
4713#endif /* ANDROID */
4714
d3aaef80
DS
4715 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4716 bss->wdev_id_set ? &bss->wdev_id : NULL);
6afbc3d6
JM
4717 if (!msg)
4718 goto nla_put_failure;
d21c63b9 4719
d21c63b9
LC
4720 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4721
bf8d6d24
TP
4722 if ((drv->num_filter_ssids &&
4723 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4724 params->filter_rssi) {
8970bae8
JB
4725 struct nlattr *match_sets;
4726 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
6afbc3d6
JM
4727 if (match_sets == NULL)
4728 goto nla_put_failure;
bd525934
LC
4729
4730 for (i = 0; i < drv->num_filter_ssids; i++) {
8970bae8 4731 struct nlattr *match_set_ssid;
bd525934
LC
4732 wpa_hexdump_ascii(MSG_MSGDUMP,
4733 "nl80211: Sched scan filter SSID",
4734 drv->filter_ssids[i].ssid,
4735 drv->filter_ssids[i].ssid_len);
4736
8970bae8 4737 match_set_ssid = nla_nest_start(msg, i + 1);
6afbc3d6
JM
4738 if (match_set_ssid == NULL)
4739 goto nla_put_failure;
8970bae8 4740 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
bd525934
LC
4741 drv->filter_ssids[i].ssid_len,
4742 drv->filter_ssids[i].ssid);
4743
adc96dc2 4744 nla_nest_end(msg, match_set_ssid);
bd525934
LC
4745 }
4746
bf8d6d24 4747 if (params->filter_rssi) {
8970bae8
JB
4748 struct nlattr *match_set_rssi;
4749 match_set_rssi = nla_nest_start(msg, 0);
6afbc3d6
JM
4750 if (match_set_rssi == NULL)
4751 goto nla_put_failure;
8970bae8 4752 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
bf8d6d24
TP
4753 params->filter_rssi);
4754 wpa_printf(MSG_MSGDUMP,
4755 "nl80211: Sched scan RSSI filter %d dBm",
4756 params->filter_rssi);
8970bae8 4757 nla_nest_end(msg, match_set_rssi);
bf8d6d24
TP
4758 }
4759
8970bae8 4760 nla_nest_end(msg, match_sets);
bd525934
LC
4761 }
4762
d21c63b9
LC
4763 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4764
4765 /* TODO: if we get an error here, we should fall back to normal scan */
4766
4767 msg = NULL;
4768 if (ret) {
4769 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4770 "ret=%d (%s)", ret, strerror(-ret));
4771 goto nla_put_failure;
4772 }
4773
4774 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4775 "scan interval %d msec", ret, interval);
4776
4777nla_put_failure:
d21c63b9 4778 nlmsg_free(msg);
d21c63b9
LC
4779 return ret;
4780}
4781
4782
4783/**
4784 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4785 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4786 * Returns: 0 on success, -1 on failure or if not supported
4787 */
4788static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4789{
4790 struct i802_bss *bss = priv;
4791 struct wpa_driver_nl80211_data *drv = bss->drv;
4792 int ret = 0;
4793 struct nl_msg *msg;
4794
216eede8
DS
4795#ifdef ANDROID
4796 if (!drv->capa.sched_scan_supported)
4797 return android_pno_stop(bss);
4798#endif /* ANDROID */
4799
d21c63b9
LC
4800 msg = nlmsg_alloc();
4801 if (!msg)
4802 return -1;
4803
9fb04070 4804 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
d21c63b9
LC
4805
4806 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4807
4808 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4809 msg = NULL;
4810 if (ret) {
4811 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4812 "ret=%d (%s)", ret, strerror(-ret));
4813 goto nla_put_failure;
4814 }
4815
4816 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4817
4818nla_put_failure:
4819 nlmsg_free(msg);
4820 return ret;
4821}
4822
4823
3812464c
JM
4824static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4825{
4826 const u8 *end, *pos;
4827
4828 if (ies == NULL)
4829 return NULL;
4830
4831 pos = ies;
4832 end = ies + ies_len;
4833
4834 while (pos + 1 < end) {
4835 if (pos + 2 + pos[1] > end)
4836 break;
4837 if (pos[0] == ie)
4838 return pos;
4839 pos += 2 + pos[1];
4840 }
4841
4842 return NULL;
4843}
4844
4845
4846static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4847 const u8 *ie, size_t ie_len)
4848{
4849 const u8 *ssid;
4850 size_t i;
4851
4852 if (drv->filter_ssids == NULL)
4853 return 0;
4854
4855 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4856 if (ssid == NULL)
4857 return 1;
4858
4859 for (i = 0; i < drv->num_filter_ssids; i++) {
4860 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4861 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4862 0)
4863 return 0;
4864 }
4865
4866 return 1;
4867}
4868
4869
b3db1e1c 4870static int bss_info_handler(struct nl_msg *msg, void *arg)
3f5285e8 4871{
b3db1e1c
JM
4872 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4873 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4874 struct nlattr *bss[NL80211_BSS_MAX + 1];
4875 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4876 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4877 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4878 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4879 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4880 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4881 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4882 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4883 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
e6b8efeb 4884 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
b3ad11bb 4885 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
8c090654 4886 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
b3db1e1c 4887 };
3812464c
JM
4888 struct nl80211_bss_info_arg *_arg = arg;
4889 struct wpa_scan_results *res = _arg->res;
3f5285e8
JM
4890 struct wpa_scan_res **tmp;
4891 struct wpa_scan_res *r;
8c090654
JM
4892 const u8 *ie, *beacon_ie;
4893 size_t ie_len, beacon_ie_len;
4894 u8 *pos;
46957a9b 4895 size_t i;
3f5285e8 4896
b3db1e1c
JM
4897 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4898 genlmsg_attrlen(gnlh, 0), NULL);
4899 if (!tb[NL80211_ATTR_BSS])
4900 return NL_SKIP;
4901 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4902 bss_policy))
4903 return NL_SKIP;
f5a8d422
JM
4904 if (bss[NL80211_BSS_STATUS]) {
4905 enum nl80211_bss_status status;
4906 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4907 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4908 bss[NL80211_BSS_FREQUENCY]) {
4909 _arg->assoc_freq =
4910 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4911 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4912 _arg->assoc_freq);
4913 }
20f5a4c2
JM
4914 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4915 bss[NL80211_BSS_BSSID]) {
4916 os_memcpy(_arg->assoc_bssid,
4917 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4918 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4919 MACSTR, MAC2STR(_arg->assoc_bssid));
4920 }
f5a8d422
JM
4921 }
4922 if (!res)
4923 return NL_SKIP;
b3db1e1c
JM
4924 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4925 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4926 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4927 } else {
4928 ie = NULL;
4929 ie_len = 0;
4930 }
8c090654
JM
4931 if (bss[NL80211_BSS_BEACON_IES]) {
4932 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4933 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4934 } else {
4935 beacon_ie = NULL;
4936 beacon_ie_len = 0;
4937 }
3f5285e8 4938
3812464c
JM
4939 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4940 ie ? ie_len : beacon_ie_len))
4941 return NL_SKIP;
4942
8c090654 4943 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3f5285e8 4944 if (r == NULL)
b3db1e1c
JM
4945 return NL_SKIP;
4946 if (bss[NL80211_BSS_BSSID])
4947 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4948 ETH_ALEN);
4949 if (bss[NL80211_BSS_FREQUENCY])
4950 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4951 if (bss[NL80211_BSS_BEACON_INTERVAL])
4952 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4953 if (bss[NL80211_BSS_CAPABILITY])
4954 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
7c2849d2
JM
4955 r->flags |= WPA_SCAN_NOISE_INVALID;
4956 if (bss[NL80211_BSS_SIGNAL_MBM]) {
b3db1e1c 4957 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
7c2849d2
JM
4958 r->level /= 100; /* mBm to dBm */
4959 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4960 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4961 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
98ac6763 4962 r->flags |= WPA_SCAN_QUAL_INVALID;
7c2849d2
JM
4963 } else
4964 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
b3db1e1c
JM
4965 if (bss[NL80211_BSS_TSF])
4966 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
b3ad11bb
JM
4967 if (bss[NL80211_BSS_SEEN_MS_AGO])
4968 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
b3db1e1c 4969 r->ie_len = ie_len;
8c090654
JM
4970 pos = (u8 *) (r + 1);
4971 if (ie) {
4972 os_memcpy(pos, ie, ie_len);
4973 pos += ie_len;
4974 }
4975 r->beacon_ie_len = beacon_ie_len;
4976 if (beacon_ie)
4977 os_memcpy(pos, beacon_ie, beacon_ie_len);
3f5285e8 4978
e6b8efeb
JM
4979 if (bss[NL80211_BSS_STATUS]) {
4980 enum nl80211_bss_status status;
4981 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4982 switch (status) {
4983 case NL80211_BSS_STATUS_AUTHENTICATED:
4984 r->flags |= WPA_SCAN_AUTHENTICATED;
4985 break;
4986 case NL80211_BSS_STATUS_ASSOCIATED:
4987 r->flags |= WPA_SCAN_ASSOCIATED;
4988 break;
4989 default:
4990 break;
4991 }
4992 }
4993
46957a9b
JM
4994 /*
4995 * cfg80211 maintains separate BSS table entries for APs if the same
4996 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4997 * not use frequency as a separate key in the BSS table, so filter out
4998 * duplicated entries. Prefer associated BSS entry in such a case in
4999 * order to get the correct frequency into the BSS table.
5000 */
5001 for (i = 0; i < res->num; i++) {
5002 const u8 *s1, *s2;
5003 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5004 continue;
5005
5006 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5007 res->res[i]->ie_len, WLAN_EID_SSID);
5008 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5009 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5010 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5011 continue;
5012
5013 /* Same BSSID,SSID was already included in scan results */
5014 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5015 "for " MACSTR, MAC2STR(r->bssid));
5016
5017 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
5018 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
5019 os_free(res->res[i]);
5020 res->res[i] = r;
5021 } else
5022 os_free(r);
5023 return NL_SKIP;
5024 }
5025
067ffa26
JM
5026 tmp = os_realloc_array(res->res, res->num + 1,
5027 sizeof(struct wpa_scan_res *));
3f5285e8
JM
5028 if (tmp == NULL) {
5029 os_free(r);
b3db1e1c 5030 return NL_SKIP;
3f5285e8
JM
5031 }
5032 tmp[res->num++] = r;
5033 res->res = tmp;
b3db1e1c
JM
5034
5035 return NL_SKIP;
3f5285e8 5036}
b3db1e1c 5037
3f5285e8 5038
d72aad94
JM
5039static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5040 const u8 *addr)
5041{
5042 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5043 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
582507be 5044 "mismatch (" MACSTR ")", MAC2STR(addr));
d72aad94
JM
5045 wpa_driver_nl80211_mlme(drv, addr,
5046 NL80211_CMD_DEAUTHENTICATE,
77339912 5047 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
d72aad94
JM
5048 }
5049}
5050
5051
e6b8efeb
JM
5052static void wpa_driver_nl80211_check_bss_status(
5053 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5054{
5055 size_t i;
5056
5057 for (i = 0; i < res->num; i++) {
5058 struct wpa_scan_res *r = res->res[i];
5059 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5060 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5061 "indicates BSS status with " MACSTR
5062 " as authenticated",
5063 MAC2STR(r->bssid));
b1f625e0 5064 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
5065 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5066 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5067 0) {
5068 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5069 " in local state (auth=" MACSTR
5070 " assoc=" MACSTR ")",
5071 MAC2STR(drv->auth_bssid),
5072 MAC2STR(drv->bssid));
582507be 5073 clear_state_mismatch(drv, r->bssid);
e6b8efeb
JM
5074 }
5075 }
5076
5077 if (r->flags & WPA_SCAN_ASSOCIATED) {
5078 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5079 "indicate BSS status with " MACSTR
5080 " as associated",
5081 MAC2STR(r->bssid));
b1f625e0 5082 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
5083 !drv->associated) {
5084 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5085 "(not associated) does not match "
5086 "with BSS state");
d72aad94 5087 clear_state_mismatch(drv, r->bssid);
b1f625e0 5088 } else if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
5089 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5090 0) {
5091 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5092 "(associated with " MACSTR ") does "
5093 "not match with BSS state",
d72aad94
JM
5094 MAC2STR(drv->bssid));
5095 clear_state_mismatch(drv, r->bssid);
5096 clear_state_mismatch(drv, drv->bssid);
e6b8efeb
JM
5097 }
5098 }
5099 }
5100}
5101
5102
7e5ba1b9 5103static struct wpa_scan_results *
8856462d 5104nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3f5285e8 5105{
b3db1e1c 5106 struct nl_msg *msg;
3f5285e8 5107 struct wpa_scan_results *res;
b3db1e1c 5108 int ret;
3812464c 5109 struct nl80211_bss_info_arg arg;
3f5285e8
JM
5110
5111 res = os_zalloc(sizeof(*res));
b3db1e1c 5112 if (res == NULL)
8e2c104f 5113 return NULL;
b3db1e1c
JM
5114 msg = nlmsg_alloc();
5115 if (!msg)
5116 goto nla_put_failure;
3f5285e8 5117
9fb04070 5118 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
834ee56f 5119 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
fdc554b8 5120 goto nla_put_failure;
3f5285e8 5121
3812464c
JM
5122 arg.drv = drv;
5123 arg.res = res;
5124 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
b3db1e1c
JM
5125 msg = NULL;
5126 if (ret == 0) {
577db0ae
GM
5127 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5128 "BSSes)", (unsigned long) res->num);
5129 nl80211_get_noise_for_scan_results(drv, res);
b3db1e1c 5130 return res;
3f5285e8 5131 }
b3db1e1c
JM
5132 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5133 "(%s)", ret, strerror(-ret));
5134nla_put_failure:
5135 nlmsg_free(msg);
5136 wpa_scan_results_free(res);
5137 return NULL;
3f5285e8
JM
5138}
5139
5140
8856462d
JM
5141/**
5142 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5143 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5144 * Returns: Scan results on success, -1 on failure
5145 */
5146static struct wpa_scan_results *
5147wpa_driver_nl80211_get_scan_results(void *priv)
5148{
a2e40bb6
FF
5149 struct i802_bss *bss = priv;
5150 struct wpa_driver_nl80211_data *drv = bss->drv;
8856462d
JM
5151 struct wpa_scan_results *res;
5152
5153 res = nl80211_get_scan_results(drv);
5154 if (res)
5155 wpa_driver_nl80211_check_bss_status(drv, res);
5156 return res;
5157}
5158
5159
5160static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5161{
5162 struct wpa_scan_results *res;
5163 size_t i;
5164
5165 res = nl80211_get_scan_results(drv);
5166 if (res == NULL) {
5167 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5168 return;
5169 }
5170
5171 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5172 for (i = 0; i < res->num; i++) {
5173 struct wpa_scan_res *r = res->res[i];
5174 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5175 (int) i, (int) res->num, MAC2STR(r->bssid),
5176 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5177 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5178 }
5179
5180 wpa_scan_results_free(res);
5181}
5182
5183
de4ed4a8
JM
5184static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5185{
5186 switch (alg) {
5187 case WPA_ALG_WEP:
5188 if (key_len == 5)
5189 return WLAN_CIPHER_SUITE_WEP40;
5190 return WLAN_CIPHER_SUITE_WEP104;
5191 case WPA_ALG_TKIP:
5192 return WLAN_CIPHER_SUITE_TKIP;
5193 case WPA_ALG_CCMP:
5194 return WLAN_CIPHER_SUITE_CCMP;
5195 case WPA_ALG_GCMP:
5196 return WLAN_CIPHER_SUITE_GCMP;
5197 case WPA_ALG_CCMP_256:
5198 return WLAN_CIPHER_SUITE_CCMP_256;
5199 case WPA_ALG_GCMP_256:
5200 return WLAN_CIPHER_SUITE_GCMP_256;
5201 case WPA_ALG_IGTK:
5202 return WLAN_CIPHER_SUITE_AES_CMAC;
5203 case WPA_ALG_BIP_GMAC_128:
5204 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5205 case WPA_ALG_BIP_GMAC_256:
5206 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5207 case WPA_ALG_BIP_CMAC_256:
5208 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5209 case WPA_ALG_SMS4:
5210 return WLAN_CIPHER_SUITE_SMS4;
5211 case WPA_ALG_KRK:
5212 return WLAN_CIPHER_SUITE_KRK;
5213 case WPA_ALG_NONE:
5214 case WPA_ALG_PMK:
5215 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5216 alg);
5217 return 0;
5218 }
5219
5220 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5221 alg);
5222 return 0;
5223}
5224
5225
5226static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5227{
5228 switch (cipher) {
5229 case WPA_CIPHER_CCMP_256:
5230 return WLAN_CIPHER_SUITE_CCMP_256;
5231 case WPA_CIPHER_GCMP_256:
5232 return WLAN_CIPHER_SUITE_GCMP_256;
5233 case WPA_CIPHER_CCMP:
5234 return WLAN_CIPHER_SUITE_CCMP;
5235 case WPA_CIPHER_GCMP:
5236 return WLAN_CIPHER_SUITE_GCMP;
5237 case WPA_CIPHER_TKIP:
5238 return WLAN_CIPHER_SUITE_TKIP;
5239 case WPA_CIPHER_WEP104:
5240 return WLAN_CIPHER_SUITE_WEP104;
5241 case WPA_CIPHER_WEP40:
5242 return WLAN_CIPHER_SUITE_WEP40;
5243 }
5244
5245 return 0;
5246}
5247
5248
5249static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5250 int max_suites)
5251{
5252 int num_suites = 0;
5253
5254 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5255 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5256 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5257 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5258 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5259 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5260 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5261 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5262 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5263 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5264 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5265 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5266 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5267 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5268
5269 return num_suites;
5270}
5271
5272
9ebce9c5 5273static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
71934751
JM
5274 enum wpa_alg alg, const u8 *addr,
5275 int key_idx, int set_tx,
642187d6
JM
5276 const u8 *seq, size_t seq_len,
5277 const u8 *key, size_t key_len)
3f5285e8 5278{
a2e40bb6 5279 struct wpa_driver_nl80211_data *drv = bss->drv;
e472e1b4 5280 int ifindex;
3f5285e8 5281 struct nl_msg *msg;
1ad1cdc2 5282 int ret;
dc01de8a 5283 int tdls = 0;
3f5285e8 5284
e472e1b4
AS
5285 /* Ignore for P2P Device */
5286 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5287 return 0;
5288
5289 ifindex = if_nametoindex(ifname);
8393e1a0 5290 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
1ad1cdc2 5291 "set_tx=%d seq_len=%lu key_len=%lu",
8393e1a0 5292 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
3f5285e8 5293 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185 5294#ifdef CONFIG_TDLS
dc01de8a 5295 if (key_idx == -1) {
8c66e185 5296 key_idx = 0;
dc01de8a
JM
5297 tdls = 1;
5298 }
8c66e185 5299#endif /* CONFIG_TDLS */
3f5285e8
JM
5300
5301 msg = nlmsg_alloc();
1ad1cdc2
JM
5302 if (!msg)
5303 return -ENOMEM;
3f5285e8
JM
5304
5305 if (alg == WPA_ALG_NONE) {
9fb04070 5306 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3f5285e8 5307 } else {
9fb04070 5308 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3f5285e8 5309 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
de4ed4a8
JM
5310 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5311 wpa_alg_to_cipher_suite(alg, key_len));
3f5285e8
JM
5312 }
5313
849ef835 5314 if (seq && seq_len)
1ad1cdc2
JM
5315 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
5316
0382097e 5317 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8
JM
5318 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5319 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
89c38e32
JM
5320
5321 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5322 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5323 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5324 NL80211_KEYTYPE_GROUP);
5325 }
60ea8187 5326 } else if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
5327 struct nlattr *types;
5328
60ea8187 5329 wpa_printf(MSG_DEBUG, " broadcast key");
8970bae8
JB
5330
5331 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
5332 if (!types)
5333 goto nla_put_failure;
8970bae8
JB
5334 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5335 nla_nest_end(msg, types);
3f5285e8
JM
5336 }
5337 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1ad1cdc2 5338 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3f5285e8 5339
1ad1cdc2 5340 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
15664ad0 5341 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
5342 ret = 0;
5343 if (ret)
5344 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5345 ret, strerror(-ret));
3f5285e8 5346
1ad1cdc2
JM
5347 /*
5348 * If we failed or don't need to set the default TX key (below),
5349 * we're done here.
5350 */
dc01de8a 5351 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
1ad1cdc2 5352 return ret;
b1f625e0 5353 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 5354 !is_broadcast_ether_addr(addr))
de12717a 5355 return ret;
3f5285e8 5356
1ad1cdc2
JM
5357 msg = nlmsg_alloc();
5358 if (!msg)
5359 return -ENOMEM;
3f5285e8 5360
9fb04070 5361 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
1ad1cdc2
JM
5362 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5363 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5364 if (alg == WPA_ALG_IGTK)
5365 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5366 else
5367 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
60ea8187 5368 if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
5369 struct nlattr *types;
5370
5371 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
5372 if (!types)
5373 goto nla_put_failure;
8970bae8
JB
5374 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5375 nla_nest_end(msg, types);
60ea8187 5376 } else if (addr) {
8970bae8
JB
5377 struct nlattr *types;
5378
5379 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
60ea8187
JM
5380 if (!types)
5381 goto nla_put_failure;
8970bae8
JB
5382 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5383 nla_nest_end(msg, types);
60ea8187 5384 }
3f5285e8 5385
1ad1cdc2
JM
5386 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5387 if (ret == -ENOENT)
5388 ret = 0;
5389 if (ret)
5390 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5391 "err=%d %s)", ret, strerror(-ret));
5392 return ret;
3f5285e8
JM
5393
5394nla_put_failure:
5883168a 5395 nlmsg_free(msg);
6241fcb1 5396 return -ENOBUFS;
3f5285e8
JM
5397}
5398
5399
71934751 5400static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
5401 int key_idx, int defkey,
5402 const u8 *seq, size_t seq_len,
5403 const u8 *key, size_t key_len)
5404{
5405 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5406 if (!key_attr)
5407 return -1;
5408
5409 if (defkey && alg == WPA_ALG_IGTK)
5410 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5411 else if (defkey)
5412 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5413
5414 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5415
de4ed4a8
JM
5416 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5417 wpa_alg_to_cipher_suite(alg, key_len));
0194fedb
JB
5418
5419 if (seq && seq_len)
5420 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5421
5422 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5423
5424 nla_nest_end(msg, key_attr);
5425
5426 return 0;
5427 nla_put_failure:
5428 return -1;
5429}
5430
c811d5bc 5431
cfaab580
ZY
5432static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5433 struct nl_msg *msg)
5434{
5435 int i, privacy = 0;
5436 struct nlattr *nl_keys, *nl_key;
5437
5438 for (i = 0; i < 4; i++) {
5439 if (!params->wep_key[i])
5440 continue;
5441 privacy = 1;
5442 break;
5443 }
ce04af5a
JM
5444 if (params->wps == WPS_MODE_PRIVACY)
5445 privacy = 1;
5446 if (params->pairwise_suite &&
5447 params->pairwise_suite != WPA_CIPHER_NONE)
5448 privacy = 1;
5449
cfaab580
ZY
5450 if (!privacy)
5451 return 0;
5452
5453 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5454
5455 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5456 if (!nl_keys)
5457 goto nla_put_failure;
5458
5459 for (i = 0; i < 4; i++) {
5460 if (!params->wep_key[i])
5461 continue;
5462
5463 nl_key = nla_nest_start(msg, i);
5464 if (!nl_key)
5465 goto nla_put_failure;
5466
5467 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5468 params->wep_key[i]);
5469 if (params->wep_key_len[i] == 5)
5470 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5471 WLAN_CIPHER_SUITE_WEP40);
5472 else
5473 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5474 WLAN_CIPHER_SUITE_WEP104);
5475
5476 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5477
5478 if (i == params->wep_tx_keyidx)
5479 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5480
5481 nla_nest_end(msg, nl_key);
5482 }
5483 nla_nest_end(msg, nl_keys);
5484
5485 return 0;
5486
5487nla_put_failure:
5488 return -ENOBUFS;
5489}
5490
5491
c2a04078 5492static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
5493 const u8 *addr, int cmd, u16 reason_code,
5494 int local_state_change)
c2a04078
JM
5495{
5496 int ret = -1;
5497 struct nl_msg *msg;
5498
5499 msg = nlmsg_alloc();
5500 if (!msg)
5501 return -1;
5502
9fb04070 5503 nl80211_cmd(drv, msg, 0, cmd);
c2a04078
JM
5504
5505 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5506 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
817762d9
MI
5507 if (addr)
5508 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
77339912
JM
5509 if (local_state_change)
5510 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
c2a04078
JM
5511
5512 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5513 msg = NULL;
5514 if (ret) {
3b7ea880
BG
5515 wpa_dbg(drv->ctx, MSG_DEBUG,
5516 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5517 reason_code, ret, strerror(-ret));
c2a04078
JM
5518 goto nla_put_failure;
5519 }
5520 ret = 0;
5521
5522nla_put_failure:
5523 nlmsg_free(msg);
5524 return ret;
5525}
3f5285e8
JM
5526
5527
cfaab580 5528static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
817762d9 5529 int reason_code)
cfaab580 5530{
3f53c006
JJ
5531 int ret;
5532
817762d9 5533 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
add9b7a4 5534 nl80211_mark_disconnected(drv);
817762d9 5535 /* Disconnect command doesn't need BSSID - it uses cached value */
3f53c006
JJ
5536 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5537 reason_code, 0);
5538 /*
5539 * For locally generated disconnect, supplicant already generates a
5540 * DEAUTH event, so ignore the event from NL80211.
5541 */
5542 drv->ignore_next_local_disconnect = ret == 0;
5543
5544 return ret;
cfaab580
ZY
5545}
5546
5547
9ebce9c5
JM
5548static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5549 const u8 *addr, int reason_code)
3f5285e8 5550{
a2e40bb6 5551 struct wpa_driver_nl80211_data *drv = bss->drv;
cfaab580 5552 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
817762d9 5553 return wpa_driver_nl80211_disconnect(drv, reason_code);
2e75a2b3
JM
5554 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5555 __func__, MAC2STR(addr), reason_code);
add9b7a4 5556 nl80211_mark_disconnected(drv);
21bdbe38
JM
5557 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5558 return nl80211_leave_ibss(drv);
c2a04078 5559 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
77339912 5560 reason_code, 0);
3f5285e8
JM
5561}
5562
5563
536fd62d
JM
5564static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5565 struct wpa_driver_auth_params *params)
5566{
5567 int i;
5568
5569 drv->auth_freq = params->freq;
5570 drv->auth_alg = params->auth_alg;
5571 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5572 drv->auth_local_state_change = params->local_state_change;
5573 drv->auth_p2p = params->p2p;
5574
5575 if (params->bssid)
5576 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5577 else
5578 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5579
5580 if (params->ssid) {
5581 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5582 drv->auth_ssid_len = params->ssid_len;
5583 } else
5584 drv->auth_ssid_len = 0;
5585
5586
5587 os_free(drv->auth_ie);
5588 drv->auth_ie = NULL;
5589 drv->auth_ie_len = 0;
5590 if (params->ie) {
5591 drv->auth_ie = os_malloc(params->ie_len);
5592 if (drv->auth_ie) {
5593 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5594 drv->auth_ie_len = params->ie_len;
5595 }
5596 }
5597
5598 for (i = 0; i < 4; i++) {
5599 if (params->wep_key[i] && params->wep_key_len[i] &&
5600 params->wep_key_len[i] <= 16) {
5601 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5602 params->wep_key_len[i]);
5603 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5604 } else
5605 drv->auth_wep_key_len[i] = 0;
5606 }
5607}
5608
5609
c2a04078 5610static int wpa_driver_nl80211_authenticate(
9ebce9c5 5611 struct i802_bss *bss, struct wpa_driver_auth_params *params)
c2a04078 5612{
a2e40bb6 5613 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 5614 int ret = -1, i;
c2a04078
JM
5615 struct nl_msg *msg;
5616 enum nl80211_auth_type type;
2f4f73b1 5617 enum nl80211_iftype nlmode;
6d6f4bb8 5618 int count = 0;
536fd62d
JM
5619 int is_retry;
5620
5621 is_retry = drv->retry_auth;
5622 drv->retry_auth = 0;
c2a04078 5623
add9b7a4 5624 nl80211_mark_disconnected(drv);
e6b8efeb 5625 os_memset(drv->auth_bssid, 0, ETH_ALEN);
add9b7a4
JM
5626 if (params->bssid)
5627 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5628 else
5629 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
af473088 5630 /* FIX: IBSS mode */
2f4f73b1
EP
5631 nlmode = params->p2p ?
5632 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5633 if (drv->nlmode != nlmode &&
9ebce9c5 5634 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
4a867032
JM
5635 return -1;
5636
6d6f4bb8 5637retry:
c2a04078
JM
5638 msg = nlmsg_alloc();
5639 if (!msg)
5640 return -1;
5641
5642 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5643 drv->ifindex);
a0b2f99b 5644
9fb04070 5645 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
0194fedb 5646
a0b2f99b
JM
5647 for (i = 0; i < 4; i++) {
5648 if (!params->wep_key[i])
5649 continue;
9ebce9c5 5650 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2ea2fcc7 5651 NULL, i,
a0b2f99b
JM
5652 i == params->wep_tx_keyidx, NULL, 0,
5653 params->wep_key[i],
5654 params->wep_key_len[i]);
0194fedb
JB
5655 if (params->wep_tx_keyidx != i)
5656 continue;
5657 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5658 params->wep_key[i], params->wep_key_len[i])) {
5659 nlmsg_free(msg);
5660 return -1;
5661 }
a0b2f99b
JM
5662 }
5663
c2a04078
JM
5664 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5665 if (params->bssid) {
5666 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5667 MAC2STR(params->bssid));
5668 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5669 }
5670 if (params->freq) {
5671 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5672 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5673 }
5674 if (params->ssid) {
5675 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5676 params->ssid, params->ssid_len);
5677 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5678 params->ssid);
5679 }
5680 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
5681 if (params->ie)
5682 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
569fed90
JM
5683 if (params->sae_data) {
5684 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
5685 params->sae_data_len);
5686 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5687 params->sae_data);
5688 }
abd9fafa 5689 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
c2a04078 5690 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 5691 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
c2a04078 5692 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 5693 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
c2a04078 5694 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 5695 else if (params->auth_alg & WPA_AUTH_ALG_FT)
c2a04078 5696 type = NL80211_AUTHTYPE_FT;
569fed90
JM
5697 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5698 type = NL80211_AUTHTYPE_SAE;
c2a04078
JM
5699 else
5700 goto nla_put_failure;
5701 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5702 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
77339912
JM
5703 if (params->local_state_change) {
5704 wpa_printf(MSG_DEBUG, " * Local state change only");
5705 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5706 }
c2a04078
JM
5707
5708 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5709 msg = NULL;
5710 if (ret) {
3b7ea880
BG
5711 wpa_dbg(drv->ctx, MSG_DEBUG,
5712 "nl80211: MLME command failed (auth): ret=%d (%s)",
5713 ret, strerror(-ret));
6d6f4bb8 5714 count++;
77339912
JM
5715 if (ret == -EALREADY && count == 1 && params->bssid &&
5716 !params->local_state_change) {
6d6f4bb8
JM
5717 /*
5718 * mac80211 does not currently accept new
5719 * authentication if we are already authenticated. As a
5720 * workaround, force deauthentication and try again.
5721 */
5722 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
5723 "after forced deauthentication");
5724 wpa_driver_nl80211_deauthenticate(
5205c4f9 5725 bss, params->bssid,
6d6f4bb8
JM
5726 WLAN_REASON_PREV_AUTH_NOT_VALID);
5727 nlmsg_free(msg);
5728 goto retry;
5729 }
536fd62d
JM
5730
5731 if (ret == -ENOENT && params->freq && !is_retry) {
5732 /*
5733 * cfg80211 has likely expired the BSS entry even
5734 * though it was previously available in our internal
5735 * BSS table. To recover quickly, start a single
5736 * channel scan on the specified channel.
5737 */
5738 struct wpa_driver_scan_params scan;
5739 int freqs[2];
5740
5741 os_memset(&scan, 0, sizeof(scan));
5742 scan.num_ssids = 1;
5743 if (params->ssid) {
5744 scan.ssids[0].ssid = params->ssid;
5745 scan.ssids[0].ssid_len = params->ssid_len;
5746 }
5747 freqs[0] = params->freq;
5748 freqs[1] = 0;
5749 scan.freqs = freqs;
5750 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5751 "channel scan to refresh cfg80211 BSS "
5752 "entry");
5753 ret = wpa_driver_nl80211_scan(bss, &scan);
5754 if (ret == 0) {
5755 nl80211_copy_auth_params(drv, params);
5756 drv->scan_for_auth = 1;
5757 }
8c3ba078
JM
5758 } else if (is_retry) {
5759 /*
5760 * Need to indicate this with an event since the return
5761 * value from the retry is not delivered to core code.
5762 */
5763 union wpa_event_data event;
5764 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5765 "failed");
5766 os_memset(&event, 0, sizeof(event));
5767 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5768 ETH_ALEN);
5769 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5770 &event);
536fd62d
JM
5771 }
5772
c2a04078
JM
5773 goto nla_put_failure;
5774 }
5775 ret = 0;
5776 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5777 "successfully");
5778
5779nla_put_failure:
5780 nlmsg_free(msg);
5781 return ret;
5782}
5783
5784
536fd62d
JM
5785static int wpa_driver_nl80211_authenticate_retry(
5786 struct wpa_driver_nl80211_data *drv)
5787{
5788 struct wpa_driver_auth_params params;
834ee56f 5789 struct i802_bss *bss = drv->first_bss;
536fd62d
JM
5790 int i;
5791
5792 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5793
5794 os_memset(&params, 0, sizeof(params));
5795 params.freq = drv->auth_freq;
5796 params.auth_alg = drv->auth_alg;
5797 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5798 params.local_state_change = drv->auth_local_state_change;
5799 params.p2p = drv->auth_p2p;
5800
5801 if (!is_zero_ether_addr(drv->auth_bssid_))
5802 params.bssid = drv->auth_bssid_;
5803
5804 if (drv->auth_ssid_len) {
5805 params.ssid = drv->auth_ssid;
5806 params.ssid_len = drv->auth_ssid_len;
5807 }
5808
5809 params.ie = drv->auth_ie;
5810 params.ie_len = drv->auth_ie_len;
5811
5812 for (i = 0; i < 4; i++) {
5813 if (drv->auth_wep_key_len[i]) {
5814 params.wep_key[i] = drv->auth_wep_key[i];
5815 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5816 }
5817 }
5818
5819 drv->retry_auth = 1;
5820 return wpa_driver_nl80211_authenticate(bss, &params);
5821}
5822
5823
282d5590
JM
5824struct phy_info_arg {
5825 u16 *num_modes;
5826 struct hostapd_hw_modes *modes;
43245552 5827 int last_mode, last_chan_idx;
282d5590
JM
5828};
5829
e62a1d43
JM
5830static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5831 struct nlattr *ampdu_factor,
5832 struct nlattr *ampdu_density,
5833 struct nlattr *mcs_set)
5834{
5835 if (capa)
5836 mode->ht_capab = nla_get_u16(capa);
5837
5838 if (ampdu_factor)
5839 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
5840
5841 if (ampdu_density)
5842 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5843
5844 if (mcs_set && nla_len(mcs_set) >= 16) {
5845 u8 *mcs;
5846 mcs = nla_data(mcs_set);
5847 os_memcpy(mode->mcs_set, mcs, 16);
5848 }
5849}
5850
5851
5852static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5853 struct nlattr *capa,
5854 struct nlattr *mcs_set)
5855{
5856 if (capa)
5857 mode->vht_capab = nla_get_u32(capa);
5858
5859 if (mcs_set && nla_len(mcs_set) >= 8) {
5860 u8 *mcs;
5861 mcs = nla_data(mcs_set);
5862 os_memcpy(mode->vht_mcs_set, mcs, 8);
5863 }
5864}
5865
5866
214a77b0
JM
5867static void phy_info_freq(struct hostapd_hw_modes *mode,
5868 struct hostapd_channel_data *chan,
5869 struct nlattr *tb_freq[])
5870{
e864c0ae 5871 u8 channel;
214a77b0
JM
5872 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5873 chan->flag = 0;
e864c0ae
JM
5874 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
5875 chan->chan = channel;
214a77b0
JM
5876
5877 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5878 chan->flag |= HOSTAPD_CHAN_DISABLED;
9fcd300d
JM
5879 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
5880 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
214a77b0
JM
5881 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5882 chan->flag |= HOSTAPD_CHAN_RADAR;
5883
fc96522e
SW
5884 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
5885 enum nl80211_dfs_state state =
5886 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
5887
5888 switch (state) {
5889 case NL80211_DFS_USABLE:
5890 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
5891 break;
5892 case NL80211_DFS_AVAILABLE:
5893 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
5894 break;
5895 case NL80211_DFS_UNAVAILABLE:
5896 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
5897 break;
5898 }
5899 }
214a77b0
JM
5900}
5901
5902
e62a1d43
JM
5903static int phy_info_freqs(struct phy_info_arg *phy_info,
5904 struct hostapd_hw_modes *mode, struct nlattr *tb)
282d5590 5905{
282d5590
JM
5906 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5907 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5908 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
9fcd300d 5909 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
282d5590
JM
5910 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5911 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
fc96522e 5912 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
282d5590 5913 };
e62a1d43
JM
5914 int new_channels = 0;
5915 struct hostapd_channel_data *channel;
5916 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
5917 struct nlattr *nl_freq;
5918 int rem_freq, idx;
5919
5920 if (tb == NULL)
5921 return NL_OK;
5922
5923 nla_for_each_nested(nl_freq, tb, rem_freq) {
5924 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5925 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5926 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5927 continue;
5928 new_channels++;
5929 }
5930
5931 channel = os_realloc_array(mode->channels,
5932 mode->num_channels + new_channels,
5933 sizeof(struct hostapd_channel_data));
5934 if (!channel)
5935 return NL_SKIP;
5936
5937 mode->channels = channel;
5938 mode->num_channels += new_channels;
5939
5940 idx = phy_info->last_chan_idx;
5941
5942 nla_for_each_nested(nl_freq, tb, rem_freq) {
5943 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5944 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5945 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5946 continue;
214a77b0 5947 phy_info_freq(mode, &mode->channels[idx], tb_freq);
e62a1d43
JM
5948 idx++;
5949 }
5950 phy_info->last_chan_idx = idx;
5951
5952 return NL_OK;
5953}
5954
5955
5956static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5957{
282d5590
JM
5958 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5959 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
3cfcad1b
JM
5960 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5961 { .type = NLA_FLAG },
282d5590 5962 };
e62a1d43 5963 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
282d5590 5964 struct nlattr *nl_rate;
e62a1d43
JM
5965 int rem_rate, idx;
5966
5967 if (tb == NULL)
5968 return NL_OK;
5969
5970 nla_for_each_nested(nl_rate, tb, rem_rate) {
5971 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5972 nla_data(nl_rate), nla_len(nl_rate),
5973 rate_policy);
5974 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5975 continue;
5976 mode->num_rates++;
5977 }
5978
5979 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5980 if (!mode->rates)
5981 return NL_SKIP;
5982
5983 idx = 0;
5984
5985 nla_for_each_nested(nl_rate, tb, rem_rate) {
5986 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5987 nla_data(nl_rate), nla_len(nl_rate),
5988 rate_policy);
5989 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5990 continue;
5991 mode->rates[idx] = nla_get_u32(
5992 tb_rate[NL80211_BITRATE_ATTR_RATE]);
e62a1d43
JM
5993 idx++;
5994 }
5995
5996 return NL_OK;
5997}
5998
5999
6000static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6001{
6002 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
282d5590 6003 struct hostapd_hw_modes *mode;
e62a1d43 6004 int ret;
282d5590 6005
3cfcad1b
JM
6006 if (phy_info->last_mode != nl_band->nla_type) {
6007 mode = os_realloc_array(phy_info->modes,
6008 *phy_info->num_modes + 1,
6009 sizeof(*mode));
6010 if (!mode)
6011 return NL_SKIP;
6012 phy_info->modes = mode;
6013
6014 mode = &phy_info->modes[*(phy_info->num_modes)];
6015 os_memset(mode, 0, sizeof(*mode));
6016 mode->mode = NUM_HOSTAPD_MODES;
c8ebeda4
MK
6017 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6018 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6019
6020 /*
6021 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6022 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6023 * possible streams as unsupported. This will be overridden if
6024 * driver advertises VHT support.
6025 */
6026 mode->vht_mcs_set[0] = 0xff;
6027 mode->vht_mcs_set[1] = 0xff;
6028 mode->vht_mcs_set[4] = 0xff;
6029 mode->vht_mcs_set[5] = 0xff;
6030
3cfcad1b
JM
6031 *(phy_info->num_modes) += 1;
6032 phy_info->last_mode = nl_band->nla_type;
6033 phy_info->last_chan_idx = 0;
6034 } else
6035 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
282d5590 6036
3cfcad1b
JM
6037 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6038 nla_len(nl_band), NULL);
282d5590 6039
e62a1d43
JM
6040 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6041 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6042 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6043 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6044 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6045 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6046 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6047 if (ret != NL_OK)
6048 return ret;
6049 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6050 if (ret != NL_OK)
6051 return ret;
282d5590 6052
3cfcad1b
JM
6053 return NL_OK;
6054}
6055
6056
6057static int phy_info_handler(struct nl_msg *msg, void *arg)
6058{
6059 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6060 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6061 struct phy_info_arg *phy_info = arg;
6062 struct nlattr *nl_band;
6063 int rem_band;
6064
6065 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6066 genlmsg_attrlen(gnlh, 0), NULL);
6067
6068 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6069 return NL_SKIP;
6070
6071 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6072 {
6073 int res = phy_info_band(phy_info, nl_band);
6074 if (res != NL_OK)
6075 return res;
6076 }
6077
282d5590
JM
6078 return NL_SKIP;
6079}
6080
3cfcad1b 6081
282d5590 6082static struct hostapd_hw_modes *
c30a4ab0
JB
6083wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6084 u16 *num_modes)
282d5590
JM
6085{
6086 u16 m;
6087 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6088 int i, mode11g_idx = -1;
6089
c30a4ab0
JB
6090 /* heuristic to set up modes */
6091 for (m = 0; m < *num_modes; m++) {
6092 if (!modes[m].num_channels)
6093 continue;
6094 if (modes[m].channels[0].freq < 4000) {
6095 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6096 for (i = 0; i < modes[m].num_rates; i++) {
6097 if (modes[m].rates[i] > 200) {
6098 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6099 break;
6100 }
6101 }
6102 } else if (modes[m].channels[0].freq > 50000)
6103 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6104 else
6105 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6106 }
6107
282d5590
JM
6108 /* If only 802.11g mode is included, use it to construct matching
6109 * 802.11b mode data. */
6110
6111 for (m = 0; m < *num_modes; m++) {
6112 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6113 return modes; /* 802.11b already included */
6114 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6115 mode11g_idx = m;
6116 }
6117
6118 if (mode11g_idx < 0)
6119 return modes; /* 2.4 GHz band not supported at all */
6120
067ffa26 6121 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
282d5590
JM
6122 if (nmodes == NULL)
6123 return modes; /* Could not add 802.11b mode */
6124
6125 mode = &nmodes[*num_modes];
6126 os_memset(mode, 0, sizeof(*mode));
6127 (*num_modes)++;
6128 modes = nmodes;
6129
6130 mode->mode = HOSTAPD_MODE_IEEE80211B;
6131
6132 mode11g = &modes[mode11g_idx];
6133 mode->num_channels = mode11g->num_channels;
6134 mode->channels = os_malloc(mode11g->num_channels *
6135 sizeof(struct hostapd_channel_data));
6136 if (mode->channels == NULL) {
6137 (*num_modes)--;
6138 return modes; /* Could not add 802.11b mode */
6139 }
6140 os_memcpy(mode->channels, mode11g->channels,
6141 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6142
6143 mode->num_rates = 0;
fb7842aa 6144 mode->rates = os_malloc(4 * sizeof(int));
282d5590
JM
6145 if (mode->rates == NULL) {
6146 os_free(mode->channels);
6147 (*num_modes)--;
6148 return modes; /* Could not add 802.11b mode */
6149 }
6150
6151 for (i = 0; i < mode11g->num_rates; i++) {
fb7842aa
JM
6152 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6153 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
282d5590
JM
6154 continue;
6155 mode->rates[mode->num_rates] = mode11g->rates[i];
6156 mode->num_rates++;
6157 if (mode->num_rates == 4)
6158 break;
6159 }
6160
6161 if (mode->num_rates == 0) {
6162 os_free(mode->channels);
6163 os_free(mode->rates);
6164 (*num_modes)--;
6165 return modes; /* No 802.11b rates */
6166 }
6167
6168 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6169 "information");
6170
6171 return modes;
6172}
6173
6174
d8e66e80
JM
6175static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6176 int end)
6177{
6178 int c;
6179
6180 for (c = 0; c < mode->num_channels; c++) {
6181 struct hostapd_channel_data *chan = &mode->channels[c];
6182 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6183 chan->flag |= HOSTAPD_CHAN_HT40;
6184 }
6185}
6186
6187
6188static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6189 int end)
6190{
6191 int c;
6192
6193 for (c = 0; c < mode->num_channels; c++) {
6194 struct hostapd_channel_data *chan = &mode->channels[c];
6195 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6196 continue;
6197 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6198 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6199 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6200 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6201 }
6202}
6203
6204
35f3d3ed 6205static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
6651f1f9
HS
6206 struct phy_info_arg *results)
6207{
6651f1f9
HS
6208 u16 m;
6209
6651f1f9
HS
6210 for (m = 0; m < *results->num_modes; m++) {
6211 int c;
6212 struct hostapd_hw_modes *mode = &results->modes[m];
6213
6214 for (c = 0; c < mode->num_channels; c++) {
6215 struct hostapd_channel_data *chan = &mode->channels[c];
6216 if ((u32) chan->freq - 10 >= start &&
6217 (u32) chan->freq + 10 <= end)
6218 chan->max_tx_power = max_eirp;
6219 }
6220 }
6221}
6222
6223
35f3d3ed 6224static void nl80211_reg_rule_ht40(u32 start, u32 end,
d8e66e80
JM
6225 struct phy_info_arg *results)
6226{
d8e66e80
JM
6227 u16 m;
6228
d8e66e80
JM
6229 for (m = 0; m < *results->num_modes; m++) {
6230 if (!(results->modes[m].ht_capab &
6231 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6232 continue;
6233 nl80211_set_ht40_mode(&results->modes[m], start, end);
6234 }
6235}
6236
6237
6238static void nl80211_reg_rule_sec(struct nlattr *tb[],
6239 struct phy_info_arg *results)
6240{
6241 u32 start, end, max_bw;
6242 u16 m;
6243
6244 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6245 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6246 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6247 return;
6248
6249 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6250 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6251 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6252
6253 if (max_bw < 20)
6254 return;
6255
6256 for (m = 0; m < *results->num_modes; m++) {
6257 if (!(results->modes[m].ht_capab &
6258 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6259 continue;
6260 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6261 }
6262}
6263
6264
53cfad46
EP
6265static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6266 int end)
6267{
6268 int c;
6269
6270 for (c = 0; c < mode->num_channels; c++) {
6271 struct hostapd_channel_data *chan = &mode->channels[c];
6272 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6273 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6274
6275 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6276 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6277
6278 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6279 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6280
6281 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6282 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6283 }
6284}
6285
6286
6287static void nl80211_reg_rule_vht(struct nlattr *tb[],
6288 struct phy_info_arg *results)
6289{
6290 u32 start, end, max_bw;
6291 u16 m;
6292
6293 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6294 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6295 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6296 return;
6297
6298 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6299 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6300 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6301
6302 if (max_bw < 80)
6303 return;
6304
6305 for (m = 0; m < *results->num_modes; m++) {
6306 if (!(results->modes[m].ht_capab &
6307 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6308 continue;
6309 /* TODO: use a real VHT support indication */
6310 if (!results->modes[m].vht_capab)
6311 continue;
6312
6313 nl80211_set_vht_mode(&results->modes[m], start, end);
6314 }
6315}
6316
6317
d8e66e80
JM
6318static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6319{
6320 struct phy_info_arg *results = arg;
6321 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6322 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6323 struct nlattr *nl_rule;
6324 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6325 int rem_rule;
6326 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6327 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6328 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6329 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6330 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6331 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6332 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6333 };
6334
6335 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6336 genlmsg_attrlen(gnlh, 0), NULL);
6337 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6338 !tb_msg[NL80211_ATTR_REG_RULES]) {
6339 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6340 "available");
6341 return NL_SKIP;
6342 }
6343
6344 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6345 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6346
6347 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6348 {
35f3d3ed 6349 u32 start, end, max_eirp = 0, max_bw = 0;
d8e66e80
JM
6350 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6351 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
35f3d3ed
JM
6352 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6353 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6354 continue;
6355 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6356 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6357 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6358 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6359 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6360 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6361
6362 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm",
6363 start, end, max_bw, max_eirp);
6364 if (max_bw >= 40)
6365 nl80211_reg_rule_ht40(start, end, results);
6366 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6367 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6368 results);
d8e66e80
JM
6369 }
6370
6371 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6372 {
6373 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6374 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6375 nl80211_reg_rule_sec(tb_rule, results);
6376 }
6377
53cfad46
EP
6378 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6379 {
6380 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6381 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6382 nl80211_reg_rule_vht(tb_rule, results);
6383 }
6384
d8e66e80
JM
6385 return NL_SKIP;
6386}
6387
6388
53cfad46
EP
6389static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6390 struct phy_info_arg *results)
d8e66e80
JM
6391{
6392 struct nl_msg *msg;
6393
6394 msg = nlmsg_alloc();
6395 if (!msg)
6396 return -ENOMEM;
6397
9fb04070 6398 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
d8e66e80
JM
6399 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6400}
6401
6402
282d5590
JM
6403static struct hostapd_hw_modes *
6404wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6405{
43245552 6406 u32 feat;
a2e40bb6
FF
6407 struct i802_bss *bss = priv;
6408 struct wpa_driver_nl80211_data *drv = bss->drv;
282d5590
JM
6409 struct nl_msg *msg;
6410 struct phy_info_arg result = {
6411 .num_modes = num_modes,
6412 .modes = NULL,
43245552 6413 .last_mode = -1,
282d5590
JM
6414 };
6415
6416 *num_modes = 0;
6417 *flags = 0;
6418
6419 msg = nlmsg_alloc();
6420 if (!msg)
6421 return NULL;
6422
43245552
DJ
6423 feat = get_nl80211_protocol_features(drv);
6424 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6425 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6426 else
6427 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
282d5590 6428
43245552 6429 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
282d5590
JM
6430 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6431
d8e66e80 6432 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
53cfad46 6433 nl80211_set_regulatory_flags(drv, &result);
c30a4ab0
JB
6434 return wpa_driver_nl80211_postprocess_modes(result.modes,
6435 num_modes);
d8e66e80 6436 }
5883168a 6437 msg = NULL;
282d5590 6438 nla_put_failure:
5883168a 6439 nlmsg_free(msg);
282d5590
JM
6440 return NULL;
6441}
6442
6443
a11241fa
JB
6444static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6445 const void *data, size_t len,
6446 int encrypt, int noack)
2c2010ac
JM
6447{
6448 __u8 rtap_hdr[] = {
6449 0x00, 0x00, /* radiotap version */
6450 0x0e, 0x00, /* radiotap length */
6451 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6452 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6453 0x00, /* padding */
6454 0x00, 0x00, /* RX and TX flags to indicate that */
6455 0x00, 0x00, /* this is the injected frame directly */
6456 };
6457 struct iovec iov[2] = {
6458 {
6459 .iov_base = &rtap_hdr,
6460 .iov_len = sizeof(rtap_hdr),
6461 },
6462 {
6463 .iov_base = (void *) data,
6464 .iov_len = len,
6465 }
6466 };
6467 struct msghdr msg = {
6468 .msg_name = NULL,
6469 .msg_namelen = 0,
6470 .msg_iov = iov,
6471 .msg_iovlen = 2,
6472 .msg_control = NULL,
6473 .msg_controllen = 0,
6474 .msg_flags = 0,
6475 };
ebbec8b2 6476 int res;
fab25336 6477 u16 txflags = 0;
2c2010ac
JM
6478
6479 if (encrypt)
6480 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6481
866af8b6
JM
6482 if (drv->monitor_sock < 0) {
6483 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6484 "for %s", __func__);
6485 return -1;
6486 }
6487
fab25336
HS
6488 if (noack)
6489 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
9d7a63dc 6490 WPA_PUT_LE16(&rtap_hdr[12], txflags);
fab25336 6491
ebbec8b2
JM
6492 res = sendmsg(drv->monitor_sock, &msg, 0);
6493 if (res < 0) {
6494 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6495 return -1;
6496 }
6497 return 0;
2c2010ac
JM
6498}
6499
6500
a11241fa
JB
6501static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6502 const void *data, size_t len,
55231068
JM
6503 int encrypt, int noack,
6504 unsigned int freq, int no_cck,
6505 int offchanok, unsigned int wait_time)
a11241fa
JB
6506{
6507 struct wpa_driver_nl80211_data *drv = bss->drv;
6508 u64 cookie;
41cc50d1 6509 int res;
a11241fa 6510
af964484
JM
6511 if (freq == 0) {
6512 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6513 bss->freq);
55231068 6514 freq = bss->freq;
af964484 6515 }
55231068 6516
739faee2
JM
6517 if (drv->use_monitor) {
6518 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
6519 freq, bss->freq);
a11241fa
JB
6520 return wpa_driver_nl80211_send_mntr(drv, data, len,
6521 encrypt, noack);
739faee2 6522 }
a11241fa 6523
739faee2 6524 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
41cc50d1
JM
6525 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
6526 &cookie, no_cck, noack, offchanok);
6527 if (res == 0 && !noack) {
6528 const struct ieee80211_mgmt *mgmt;
6529 u16 fc;
6530
6531 mgmt = (const struct ieee80211_mgmt *) data;
6532 fc = le_to_host16(mgmt->frame_control);
6533 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6534 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
6535 wpa_printf(MSG_MSGDUMP,
6536 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
6537 (long long unsigned int)
6538 drv->send_action_cookie,
6539 (long long unsigned int) cookie);
6540 drv->send_action_cookie = cookie;
6541 }
6542 }
6543
6544 return res;
a11241fa
JB
6545}
6546
6547
9ebce9c5
JM
6548static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
6549 size_t data_len, int noack,
6550 unsigned int freq, int no_cck,
6551 int offchanok,
6552 unsigned int wait_time)
2c2010ac 6553{
a2e40bb6 6554 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 6555 struct ieee80211_mgmt *mgmt;
7a47d567 6556 int encrypt = 1;
2c2010ac
JM
6557 u16 fc;
6558
6559 mgmt = (struct ieee80211_mgmt *) data;
6560 fc = le_to_host16(mgmt->frame_control);
af964484
JM
6561 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
6562 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
2c2010ac 6563
8e12685c
AS
6564 if ((is_sta_interface(drv->nlmode) ||
6565 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
5582a5d1
JB
6566 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6567 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
6568 /*
6569 * The use of last_mgmt_freq is a bit of a hack,
6570 * but it works due to the single-threaded nature
6571 * of wpa_supplicant.
6572 */
af964484
JM
6573 if (freq == 0) {
6574 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
6575 drv->last_mgmt_freq);
55231068 6576 freq = drv->last_mgmt_freq;
af964484 6577 }
55231068 6578 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7
JB
6579 data, data_len, NULL, 1, noack,
6580 1);
5582a5d1
JB
6581 }
6582
61cbe2ff 6583 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
af964484
JM
6584 if (freq == 0) {
6585 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
6586 bss->freq);
55231068 6587 freq = bss->freq;
af964484 6588 }
b5671498
JM
6589 return nl80211_send_frame_cmd(bss, freq,
6590 (int) freq == bss->freq ? 0 :
6591 wait_time,
d8d6b32e
DG
6592 data, data_len,
6593 &drv->send_action_cookie,
55231068 6594 no_cck, noack, offchanok);
86957e62
JM
6595 }
6596
2c2010ac
JM
6597 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6598 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6599 /*
6600 * Only one of the authentication frame types is encrypted.
6601 * In order for static WEP encryption to work properly (i.e.,
6602 * to not encrypt the frame), we need to tell mac80211 about
6603 * the frames that must not be encrypted.
6604 */
6605 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6606 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
6607 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6608 encrypt = 0;
2c2010ac
JM
6609 }
6610
739faee2 6611 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
a11241fa 6612 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068
JM
6613 noack, freq, no_cck, offchanok,
6614 wait_time);
6615}
6616
6617
31357268 6618static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47
JM
6619 int slot, int ht_opmode, int ap_isolate,
6620 int *basic_rates)
31357268
JM
6621{
6622 struct wpa_driver_nl80211_data *drv = bss->drv;
6623 struct nl_msg *msg;
6624
6625 msg = nlmsg_alloc();
6626 if (!msg)
6627 return -ENOMEM;
6628
9fb04070 6629 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
31357268
JM
6630
6631 if (cts >= 0)
6632 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6633 if (preamble >= 0)
6634 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6635 if (slot >= 0)
6636 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6637 if (ht_opmode >= 0)
6638 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
d03e8d11
JM
6639 if (ap_isolate >= 0)
6640 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
e5693c47
JM
6641
6642 if (basic_rates) {
6643 u8 rates[NL80211_MAX_SUPP_RATES];
6644 u8 rates_len = 0;
6645 int i;
6646
6647 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6648 i++)
6649 rates[rates_len++] = basic_rates[i] / 5;
6650
6651 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6652 }
6653
31357268
JM
6654 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6655
6656 return send_and_recv_msgs(drv, msg, NULL, NULL);
6657 nla_put_failure:
5883168a 6658 nlmsg_free(msg);
31357268
JM
6659 return -ENOBUFS;
6660}
6661
6662
3c4ca363
VN
6663static int wpa_driver_nl80211_set_acl(void *priv,
6664 struct hostapd_acl_params *params)
6665{
6666 struct i802_bss *bss = priv;
6667 struct wpa_driver_nl80211_data *drv = bss->drv;
6668 struct nl_msg *msg;
6669 struct nlattr *acl;
6670 unsigned int i;
6671 int ret = 0;
6672
6673 if (!(drv->capa.max_acl_mac_addrs))
6674 return -ENOTSUP;
6675
6676 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6677 return -ENOTSUP;
6678
6679 msg = nlmsg_alloc();
6680 if (!msg)
6681 return -ENOMEM;
6682
6683 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
6684 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
6685
6686 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
6687
6688 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6689
6690 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
6691 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
6692 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
6693
6694 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
6695 if (acl == NULL)
6696 goto nla_put_failure;
6697
6698 for (i = 0; i < params->num_mac_acl; i++)
6699 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
6700
6701 nla_nest_end(msg, acl);
6702
6703 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6704 msg = NULL;
6705 if (ret) {
6706 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
6707 ret, strerror(-ret));
6708 }
6709
6710nla_put_failure:
6711 nlmsg_free(msg);
6712
6713 return ret;
6714}
6715
6716
19c3b566
JM
6717static int wpa_driver_nl80211_set_ap(void *priv,
6718 struct wpa_driver_ap_params *params)
d2440ba0 6719{
a2e40bb6
FF
6720 struct i802_bss *bss = priv;
6721 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
6722 struct nl_msg *msg;
6723 u8 cmd = NL80211_CMD_NEW_BEACON;
6724 int ret;
b4fd6fab 6725 int beacon_set;
8b897f5a 6726 int ifindex = if_nametoindex(bss->ifname);
b11d1d64 6727 int num_suites;
de4ed4a8 6728 u32 suites[10], suite;
b11d1d64 6729 u32 ver;
b4fd6fab 6730
b4fd6fab 6731 beacon_set = bss->beacon_set;
d2440ba0
JM
6732
6733 msg = nlmsg_alloc();
6734 if (!msg)
6735 return -ENOMEM;
6736
6737 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
6738 beacon_set);
6739 if (beacon_set)
d2440ba0
JM
6740 cmd = NL80211_CMD_SET_BEACON;
6741
9fb04070 6742 nl80211_cmd(drv, msg, 0, cmd);
b92e08fc
JM
6743 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
6744 params->head, params->head_len);
19c3b566 6745 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
b92e08fc
JM
6746 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
6747 params->tail, params->tail_len);
19c3b566 6748 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
b92e08fc 6749 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
b4fd6fab 6750 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
b92e08fc 6751 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
19c3b566 6752 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
b92e08fc 6753 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
19c3b566 6754 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
b92e08fc
JM
6755 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
6756 params->ssid, params->ssid_len);
ccb941e6
JM
6757 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6758 params->ssid);
b92e08fc
JM
6759 if (params->proberesp && params->proberesp_len) {
6760 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
6761 params->proberesp, params->proberesp_len);
5ed33546
AN
6762 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
6763 params->proberesp);
b92e08fc 6764 }
97a7a0b5
JM
6765 switch (params->hide_ssid) {
6766 case NO_SSID_HIDING:
b92e08fc 6767 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
97a7a0b5
JM
6768 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6769 NL80211_HIDDEN_SSID_NOT_IN_USE);
6770 break;
6771 case HIDDEN_SSID_ZERO_LEN:
b92e08fc 6772 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
97a7a0b5
JM
6773 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6774 NL80211_HIDDEN_SSID_ZERO_LEN);
6775 break;
6776 case HIDDEN_SSID_ZERO_CONTENTS:
b92e08fc 6777 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
97a7a0b5
JM
6778 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6779 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
6780 break;
6781 }
b92e08fc 6782 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
b11d1d64
JM
6783 if (params->privacy)
6784 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
b92e08fc 6785 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
b11d1d64
JM
6786 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
6787 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
6788 /* Leave out the attribute */
6789 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
6790 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6791 NL80211_AUTHTYPE_SHARED_KEY);
6792 else
6793 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6794 NL80211_AUTHTYPE_OPEN_SYSTEM);
6795
b92e08fc 6796 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
b11d1d64
JM
6797 ver = 0;
6798 if (params->wpa_version & WPA_PROTO_WPA)
6799 ver |= NL80211_WPA_VERSION_1;
6800 if (params->wpa_version & WPA_PROTO_RSN)
6801 ver |= NL80211_WPA_VERSION_2;
6802 if (ver)
6803 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6804
b92e08fc
JM
6805 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
6806 params->key_mgmt_suites);
b11d1d64
JM
6807 num_suites = 0;
6808 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
6809 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
6810 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
6811 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
6812 if (num_suites) {
6813 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
6814 num_suites * sizeof(u32), suites);
6815 }
6816
9f12614b
JB
6817 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
6818 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
6819 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
6820
b92e08fc
JM
6821 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
6822 params->pairwise_ciphers);
de4ed4a8
JM
6823 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
6824 suites, ARRAY_SIZE(suites));
b11d1d64
JM
6825 if (num_suites) {
6826 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6827 num_suites * sizeof(u32), suites);
6828 }
6829
b92e08fc
JM
6830 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
6831 params->group_cipher);
de4ed4a8
JM
6832 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
6833 if (suite)
6834 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
d2440ba0 6835
fb91db56 6836 if (params->beacon_ies) {
b92e08fc
JM
6837 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
6838 params->beacon_ies);
fb91db56
JM
6839 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
6840 wpabuf_head(params->beacon_ies));
6841 }
6842 if (params->proberesp_ies) {
b92e08fc
JM
6843 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
6844 params->proberesp_ies);
fb91db56
JM
6845 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
6846 wpabuf_len(params->proberesp_ies),
6847 wpabuf_head(params->proberesp_ies));
6848 }
6849 if (params->assocresp_ies) {
b92e08fc
JM
6850 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
6851 params->assocresp_ies);
fb91db56
JM
6852 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
6853 wpabuf_len(params->assocresp_ies),
6854 wpabuf_head(params->assocresp_ies));
6855 }
6856
a0133ee1 6857 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
b92e08fc
JM
6858 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
6859 params->ap_max_inactivity);
a0133ee1
VT
6860 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
6861 params->ap_max_inactivity);
6862 }
6863
d2440ba0
JM
6864 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6865 if (ret) {
6866 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
6867 ret, strerror(-ret));
b4fd6fab 6868 } else {
b4fd6fab 6869 bss->beacon_set = 1;
31357268 6870 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 6871 params->short_slot_time, params->ht_opmode,
e5693c47 6872 params->isolate, params->basic_rates);
b4fd6fab 6873 }
d2440ba0
JM
6874 return ret;
6875 nla_put_failure:
5883168a 6876 nlmsg_free(msg);
d2440ba0
JM
6877 return -ENOBUFS;
6878}
6879
6880
1c4ffa87
AO
6881static int nl80211_put_freq_params(struct nl_msg *msg,
6882 struct hostapd_freq_params *freq)
1581b38b 6883{
89b800d7
JB
6884 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
6885 if (freq->vht_enabled) {
6886 switch (freq->bandwidth) {
6887 case 20:
6888 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6889 NL80211_CHAN_WIDTH_20);
6890 break;
6891 case 40:
6892 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6893 NL80211_CHAN_WIDTH_40);
6894 break;
6895 case 80:
6896 if (freq->center_freq2)
6897 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6898 NL80211_CHAN_WIDTH_80P80);
6899 else
6900 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6901 NL80211_CHAN_WIDTH_80);
6902 break;
6903 case 160:
6904 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6905 NL80211_CHAN_WIDTH_160);
6906 break;
6907 default:
1c4ffa87 6908 return -EINVAL;
89b800d7
JB
6909 }
6910 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6911 if (freq->center_freq2)
6912 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6913 freq->center_freq2);
6914 } else if (freq->ht_enabled) {
6915 switch (freq->sec_channel_offset) {
f019981a
JM
6916 case -1:
6917 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6918 NL80211_CHAN_HT40MINUS);
6919 break;
6920 case 1:
6921 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6922 NL80211_CHAN_HT40PLUS);
6923 break;
6924 default:
6925 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6926 NL80211_CHAN_HT20);
6927 break;
6928 }
6929 }
1c4ffa87
AO
6930 return 0;
6931
6932nla_put_failure:
6933 return -ENOBUFS;
6934}
6935
6936
6937static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
6938 struct hostapd_freq_params *freq)
6939{
6940 struct wpa_driver_nl80211_data *drv = bss->drv;
6941 struct nl_msg *msg;
6942 int ret;
6943
6944 wpa_printf(MSG_DEBUG,
6945 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6946 freq->freq, freq->ht_enabled, freq->vht_enabled,
6947 freq->bandwidth, freq->center_freq1, freq->center_freq2);
6948 msg = nlmsg_alloc();
6949 if (!msg)
6950 return -1;
6951
6952 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6953
6954 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6955 if (nl80211_put_freq_params(msg, freq) < 0)
6956 goto nla_put_failure;
1581b38b 6957
d2440ba0 6958 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 6959 msg = NULL;
e4fb2167 6960 if (ret == 0) {
89b800d7 6961 bss->freq = freq->freq;
1581b38b 6962 return 0;
e4fb2167 6963 }
f019981a 6964 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 6965 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b 6966nla_put_failure:
5883168a 6967 nlmsg_free(msg);
1581b38b
JM
6968 return -1;
6969}
6970
0f4e8b4f 6971
95ab6063
AN
6972static u32 sta_flags_nl80211(int flags)
6973{
6974 u32 f = 0;
6975
6976 if (flags & WPA_STA_AUTHORIZED)
6977 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6978 if (flags & WPA_STA_WMM)
6979 f |= BIT(NL80211_STA_FLAG_WME);
6980 if (flags & WPA_STA_SHORT_PREAMBLE)
6981 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6982 if (flags & WPA_STA_MFP)
6983 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
6984 if (flags & WPA_STA_TDLS_PEER)
6985 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
95ab6063
AN
6986
6987 return f;
6988}
6989
6990
62847751 6991static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
6992 struct hostapd_sta_add_params *params)
6993{
a2e40bb6
FF
6994 struct i802_bss *bss = priv;
6995 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8 6996 struct nl_msg *msg;
95ab6063 6997 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
6998 int ret = -ENOBUFS;
6999
45b722f1
AN
7000 if ((params->flags & WPA_STA_TDLS_PEER) &&
7001 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7002 return -EOPNOTSUPP;
7003
0f4e8b4f
JM
7004 msg = nlmsg_alloc();
7005 if (!msg)
7006 return -ENOMEM;
7007
e4dea253
JM
7008 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7009 params->set ? "Set" : "Add", MAC2STR(params->addr));
45b722f1
AN
7010 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7011 NL80211_CMD_NEW_STATION);
0f4e8b4f 7012
62847751 7013 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
0f4e8b4f 7014 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
0f4e8b4f
JM
7015 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7016 params->supp_rates);
e4dea253
JM
7017 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7018 params->supp_rates_len);
45b722f1 7019 if (!params->set) {
f11b72c3
JM
7020 if (params->aid) {
7021 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7022 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7023 } else {
7024 /*
7025 * cfg80211 validates that AID is non-zero, so we have
7026 * to make this a non-zero value for the TDLS case where
7027 * a dummy STA entry is used for now.
7028 */
7029 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7030 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7031 }
e4dea253
JM
7032 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7033 params->listen_interval);
45b722f1
AN
7034 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7035 params->listen_interval);
e112764e
JM
7036 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7037 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7038 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
45b722f1 7039 }
0f4e8b4f 7040 if (params->ht_capabilities) {
e4dea253
JM
7041 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7042 (u8 *) params->ht_capabilities,
7043 sizeof(*params->ht_capabilities));
0f4e8b4f 7044 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
fc4e2d95
JM
7045 sizeof(*params->ht_capabilities),
7046 params->ht_capabilities);
0f4e8b4f 7047 }
0f4e8b4f 7048
05a8d422 7049 if (params->vht_capabilities) {
e4dea253
JM
7050 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7051 (u8 *) params->vht_capabilities,
7052 sizeof(*params->vht_capabilities));
05a8d422
JB
7053 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7054 sizeof(*params->vht_capabilities),
7055 params->vht_capabilities);
7056 }
7057
122d16f2
SD
7058 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7059 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7060
7061 if (params->ext_capab) {
7062 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7063 params->ext_capab, params->ext_capab_len);
7064 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7065 params->ext_capab_len, params->ext_capab);
7066 }
7067
95ab6063
AN
7068 os_memset(&upd, 0, sizeof(upd));
7069 upd.mask = sta_flags_nl80211(params->flags);
7070 upd.set = upd.mask;
e4dea253
JM
7071 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7072 upd.set, upd.mask);
95ab6063
AN
7073 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7074
774bfa62 7075 if (params->flags & WPA_STA_WMM) {
8970bae8
JB
7076 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7077
774bfa62
EP
7078 if (!wme)
7079 goto nla_put_failure;
7080
e4dea253 7081 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
8970bae8 7082 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
5d061637 7083 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
8970bae8 7084 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
bdaf1748 7085 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5d061637 7086 WMM_QOSINFO_STA_SP_MASK);
8970bae8 7087 nla_nest_end(msg, wme);
774bfa62
EP
7088 }
7089
0f4e8b4f 7090 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7091 msg = NULL;
0f4e8b4f 7092 if (ret)
45b722f1
AN
7093 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7094 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7095 strerror(-ret));
0f4e8b4f
JM
7096 if (ret == -EEXIST)
7097 ret = 0;
7098 nla_put_failure:
5883168a 7099 nlmsg_free(msg);
0f4e8b4f
JM
7100 return ret;
7101}
7102
7103
9ebce9c5 7104static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
0f4e8b4f 7105{
a2e40bb6 7106 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
7107 struct nl_msg *msg;
7108 int ret;
7109
7110 msg = nlmsg_alloc();
7111 if (!msg)
7112 return -ENOMEM;
7113
9fb04070 7114 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
0f4e8b4f
JM
7115
7116 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7117 if_nametoindex(bss->ifname));
0f4e8b4f
JM
7118 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7119
7120 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83e7bb0e
JM
7121 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7122 " --> %d (%s)",
7123 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
0f4e8b4f
JM
7124 if (ret == -ENOENT)
7125 return 0;
7126 return ret;
7127 nla_put_failure:
5883168a 7128 nlmsg_free(msg);
0f4e8b4f
JM
7129 return -ENOBUFS;
7130}
7131
1581b38b 7132
0915d02c
JM
7133static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7134 int ifidx)
7135{
7136 struct nl_msg *msg;
7137
c6e8e8e4
JM
7138 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7139
2135f224
JM
7140 /* stop listening for EAPOL on this interface */
7141 del_ifidx(drv, ifidx);
2135f224 7142
0915d02c
JM
7143 msg = nlmsg_alloc();
7144 if (!msg)
7145 goto nla_put_failure;
7146
9fb04070 7147 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
7148 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7149
7150 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7151 return;
5883168a 7152 msg = NULL;
0915d02c 7153 nla_put_failure:
5883168a 7154 nlmsg_free(msg);
e748062b 7155 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
7156}
7157
7158
a1922f93
JM
7159static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7160{
7161 switch (mode) {
7162 case NL80211_IFTYPE_ADHOC:
7163 return "ADHOC";
7164 case NL80211_IFTYPE_STATION:
7165 return "STATION";
7166 case NL80211_IFTYPE_AP:
7167 return "AP";
1045ec36
JM
7168 case NL80211_IFTYPE_AP_VLAN:
7169 return "AP_VLAN";
7170 case NL80211_IFTYPE_WDS:
7171 return "WDS";
a1922f93
JM
7172 case NL80211_IFTYPE_MONITOR:
7173 return "MONITOR";
1045ec36
JM
7174 case NL80211_IFTYPE_MESH_POINT:
7175 return "MESH_POINT";
a1922f93
JM
7176 case NL80211_IFTYPE_P2P_CLIENT:
7177 return "P2P_CLIENT";
7178 case NL80211_IFTYPE_P2P_GO:
7179 return "P2P_GO";
7aad838c
NS
7180 case NL80211_IFTYPE_P2P_DEVICE:
7181 return "P2P_DEVICE";
a1922f93
JM
7182 default:
7183 return "unknown";
7184 }
7185}
7186
7187
a35187e7
KH
7188static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7189 const char *ifname,
7190 enum nl80211_iftype iftype,
d6dcfcda
DS
7191 const u8 *addr, int wds,
7192 int (*handler)(struct nl_msg *, void *),
7193 void *arg)
0915d02c 7194{
8970bae8 7195 struct nl_msg *msg;
0915d02c
JM
7196 int ifidx;
7197 int ret = -ENOBUFS;
7198
a1922f93
JM
7199 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7200 iftype, nl80211_iftype_str(iftype));
7201
0915d02c
JM
7202 msg = nlmsg_alloc();
7203 if (!msg)
7204 return -1;
7205
9fb04070 7206 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
834ee56f 7207 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
fa93de40 7208 goto nla_put_failure;
0915d02c
JM
7209 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7210 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7211
7212 if (iftype == NL80211_IFTYPE_MONITOR) {
8970bae8 7213 struct nlattr *flags;
0915d02c 7214
8970bae8 7215 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
0915d02c
JM
7216 if (!flags)
7217 goto nla_put_failure;
7218
8970bae8 7219 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
0915d02c 7220
8970bae8 7221 nla_nest_end(msg, flags);
fbbfcbac
FF
7222 } else if (wds) {
7223 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
0915d02c
JM
7224 }
7225
d6dcfcda 7226 ret = send_and_recv_msgs(drv, msg, handler, arg);
5883168a 7227 msg = NULL;
0915d02c
JM
7228 if (ret) {
7229 nla_put_failure:
5883168a 7230 nlmsg_free(msg);
a35187e7
KH
7231 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7232 ifname, ret, strerror(-ret));
0915d02c
JM
7233 return ret;
7234 }
7235
f632e483 7236 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6bae92e0 7237 return 0;
6bae92e0 7238
0915d02c 7239 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
7240 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7241 ifname, ifidx);
0915d02c
JM
7242
7243 if (ifidx <= 0)
7244 return -1;
7245
2135f224
JM
7246 /* start listening for EAPOL on this interface */
7247 add_ifidx(drv, ifidx);
7248
7bfc47c3 7249 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 7250 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
7251 nl80211_remove_iface(drv, ifidx);
7252 return -1;
2135f224 7253 }
2135f224 7254
0915d02c
JM
7255 return ifidx;
7256}
22a7c9d7
JM
7257
7258
a35187e7
KH
7259static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7260 const char *ifname, enum nl80211_iftype iftype,
d6dcfcda
DS
7261 const u8 *addr, int wds,
7262 int (*handler)(struct nl_msg *, void *),
2aec4f3c 7263 void *arg, int use_existing)
a35187e7
KH
7264{
7265 int ret;
7266
d6dcfcda
DS
7267 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7268 arg);
a35187e7 7269
ffbf1eaa 7270 /* if error occurred and interface exists already */
a35187e7 7271 if (ret == -ENFILE && if_nametoindex(ifname)) {
2aec4f3c
JM
7272 if (use_existing) {
7273 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7274 ifname);
7275 return -ENFILE;
7276 }
a35187e7
KH
7277 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7278
7279 /* Try to remove the interface that was already there. */
7280 nl80211_remove_iface(drv, if_nametoindex(ifname));
7281
7282 /* Try to create the interface again */
fbbfcbac 7283 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
d6dcfcda 7284 wds, handler, arg);
a35187e7
KH
7285 }
7286
6a71413e 7287 if (ret >= 0 && is_p2p_net_interface(iftype))
4e5cb1a3
JM
7288 nl80211_disable_11b_rates(drv, ret, 1);
7289
a35187e7
KH
7290 return ret;
7291}
0915d02c 7292
2135f224 7293
0915d02c
JM
7294static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7295{
7296 struct ieee80211_hdr *hdr;
f8b1f695
JM
7297 u16 fc;
7298 union wpa_event_data event;
0915d02c
JM
7299
7300 hdr = (struct ieee80211_hdr *) buf;
7301 fc = le_to_host16(hdr->frame_control);
7302
f8b1f695
JM
7303 os_memset(&event, 0, sizeof(event));
7304 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7305 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7306 event.tx_status.dst = hdr->addr1;
7307 event.tx_status.data = buf;
7308 event.tx_status.data_len = len;
7309 event.tx_status.ack = ok;
7310 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
0915d02c
JM
7311}
7312
7313
4b9841d3 7314static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
0d9fc3d8 7315 u8 *buf, size_t len)
0915d02c 7316{
9b90955e
JB
7317 struct ieee80211_hdr *hdr = (void *)buf;
7318 u16 fc;
f8b1f695 7319 union wpa_event_data event;
9b90955e
JB
7320
7321 if (len < sizeof(*hdr))
7322 return;
7323
7324 fc = le_to_host16(hdr->frame_control);
7325
f8b1f695 7326 os_memset(&event, 0, sizeof(event));
9b90955e
JB
7327 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7328 event.rx_from_unknown.addr = hdr->addr2;
7329 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7330 (WLAN_FC_FROMDS | WLAN_FC_TODS);
f8b1f695 7331 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4b9841d3 7332}
0915d02c 7333
4b9841d3
JM
7334
7335static void handle_frame(struct wpa_driver_nl80211_data *drv,
2a8b7416 7336 u8 *buf, size_t len, int datarate, int ssi_signal)
4b9841d3
JM
7337{
7338 struct ieee80211_hdr *hdr;
f8b1f695
JM
7339 u16 fc;
7340 union wpa_event_data event;
0915d02c
JM
7341
7342 hdr = (struct ieee80211_hdr *) buf;
7343 fc = le_to_host16(hdr->frame_control);
0915d02c 7344
4b9841d3 7345 switch (WLAN_FC_GET_TYPE(fc)) {
0915d02c 7346 case WLAN_FC_TYPE_MGMT:
f8b1f695
JM
7347 os_memset(&event, 0, sizeof(event));
7348 event.rx_mgmt.frame = buf;
7349 event.rx_mgmt.frame_len = len;
2a8b7416
JM
7350 event.rx_mgmt.datarate = datarate;
7351 event.rx_mgmt.ssi_signal = ssi_signal;
f8b1f695 7352 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
0915d02c
JM
7353 break;
7354 case WLAN_FC_TYPE_CTRL:
7355 /* can only get here with PS-Poll frames */
7356 wpa_printf(MSG_DEBUG, "CTRL");
0d9fc3d8 7357 from_unknown_sta(drv, buf, len);
0915d02c
JM
7358 break;
7359 case WLAN_FC_TYPE_DATA:
0d9fc3d8 7360 from_unknown_sta(drv, buf, len);
0915d02c
JM
7361 break;
7362 }
7363}
7364
7365
7366static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7367{
7368 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7369 int len;
7370 unsigned char buf[3000];
7371 struct ieee80211_radiotap_iterator iter;
7372 int ret;
2a8b7416 7373 int datarate = 0, ssi_signal = 0;
4b9841d3 7374 int injected = 0, failed = 0, rxflags = 0;
0915d02c
JM
7375
7376 len = recv(sock, buf, sizeof(buf), 0);
7377 if (len < 0) {
7ac3616d
JM
7378 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7379 strerror(errno));
0915d02c
JM
7380 return;
7381 }
7382
7383 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
7ac3616d 7384 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
0915d02c
JM
7385 return;
7386 }
7387
0915d02c
JM
7388 while (1) {
7389 ret = ieee80211_radiotap_iterator_next(&iter);
7390 if (ret == -ENOENT)
7391 break;
7392 if (ret) {
7ac3616d
JM
7393 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7394 ret);
0915d02c
JM
7395 return;
7396 }
7397 switch (iter.this_arg_index) {
7398 case IEEE80211_RADIOTAP_FLAGS:
7399 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7400 len -= 4;
7401 break;
7402 case IEEE80211_RADIOTAP_RX_FLAGS:
7403 rxflags = 1;
7404 break;
7405 case IEEE80211_RADIOTAP_TX_FLAGS:
7406 injected = 1;
7407 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7408 IEEE80211_RADIOTAP_F_TX_FAIL;
7409 break;
7410 case IEEE80211_RADIOTAP_DATA_RETRIES:
7411 break;
7412 case IEEE80211_RADIOTAP_CHANNEL:
2a8b7416 7413 /* TODO: convert from freq/flags to channel number */
0915d02c
JM
7414 break;
7415 case IEEE80211_RADIOTAP_RATE:
2a8b7416 7416 datarate = *iter.this_arg * 5;
0915d02c 7417 break;
baf513d6
JB
7418 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7419 ssi_signal = (s8) *iter.this_arg;
0915d02c
JM
7420 break;
7421 }
7422 }
7423
7424 if (rxflags && injected)
7425 return;
7426
7427 if (!injected)
4b9841d3 7428 handle_frame(drv, buf + iter.max_length,
2a8b7416 7429 len - iter.max_length, datarate, ssi_signal);
0915d02c 7430 else
4b9841d3
JM
7431 handle_tx_callback(drv->ctx, buf + iter.max_length,
7432 len - iter.max_length, !failed);
0915d02c
JM
7433}
7434
7435
7436/*
7437 * we post-process the filter code later and rewrite
7438 * this to the offset to the last instruction
7439 */
7440#define PASS 0xFF
7441#define FAIL 0xFE
7442
7443static struct sock_filter msock_filter_insns[] = {
7444 /*
7445 * do a little-endian load of the radiotap length field
7446 */
7447 /* load lower byte into A */
7448 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7449 /* put it into X (== index register) */
7450 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7451 /* load upper byte into A */
7452 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
7453 /* left-shift it by 8 */
7454 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
7455 /* or with X */
7456 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
7457 /* put result into X */
7458 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7459
7460 /*
7461 * Allow management frames through, this also gives us those
7462 * management frames that we sent ourselves with status
7463 */
7464 /* load the lower byte of the IEEE 802.11 frame control field */
7465 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7466 /* mask off frame type and version */
7467 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
7468 /* accept frame if it's both 0, fall through otherwise */
7469 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
7470
7471 /*
7472 * TODO: add a bit to radiotap RX flags that indicates
7473 * that the sending station is not associated, then
7474 * add a filter here that filters on our DA and that flag
7475 * to allow us to deauth frames to that bad station.
7476 *
65ae1afd 7477 * For now allow all To DS data frames through.
0915d02c 7478 */
65ae1afd
HS
7479 /* load the IEEE 802.11 frame control field */
7480 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
7481 /* mask off frame type, version and DS status */
7482 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
7483 /* accept frame if version 0, type 2 and To DS, fall through otherwise
7484 */
7485 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
0915d02c
JM
7486
7487#if 0
7488 /*
fbbfcbac 7489 * drop non-data frames
0915d02c
JM
7490 */
7491 /* load the lower byte of the frame control field */
7492 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7493 /* mask off QoS bit */
7494 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
7495 /* drop non-data frames */
7496 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
fbbfcbac 7497#endif
0915d02c 7498 /* load the upper byte of the frame control field */
fbbfcbac 7499 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
0915d02c
JM
7500 /* mask off toDS/fromDS */
7501 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
fbbfcbac
FF
7502 /* accept WDS frames */
7503 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
0915d02c
JM
7504
7505 /*
7506 * add header length to index
7507 */
7508 /* load the lower byte of the frame control field */
7509 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
7510 /* mask off QoS bit */
7511 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
7512 /* right shift it by 6 to give 0 or 2 */
7513 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
7514 /* add data frame header length */
7515 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
7516 /* add index, was start of 802.11 header */
7517 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
7518 /* move to index, now start of LL header */
7519 BPF_STMT(BPF_MISC | BPF_TAX, 0),
7520
7521 /*
7522 * Accept empty data frames, we use those for
7523 * polling activity.
7524 */
7525 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
7526 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
7527
7528 /*
7529 * Accept EAPOL frames
7530 */
7531 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
7532 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
7533 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
7534 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
7535
7536 /* keep these last two statements or change the code below */
7537 /* return 0 == "DROP" */
7538 BPF_STMT(BPF_RET | BPF_K, 0),
7539 /* return ~0 == "keep all" */
7540 BPF_STMT(BPF_RET | BPF_K, ~0),
7541};
7542
7543static struct sock_fprog msock_filter = {
e7ecab4a 7544 .len = ARRAY_SIZE(msock_filter_insns),
0915d02c
JM
7545 .filter = msock_filter_insns,
7546};
7547
7548
7549static int add_monitor_filter(int s)
7550{
7551 int idx;
7552
7553 /* rewrite all PASS/FAIL jump offsets */
7554 for (idx = 0; idx < msock_filter.len; idx++) {
7555 struct sock_filter *insn = &msock_filter_insns[idx];
7556
7557 if (BPF_CLASS(insn->code) == BPF_JMP) {
7558 if (insn->code == (BPF_JMP|BPF_JA)) {
7559 if (insn->k == PASS)
7560 insn->k = msock_filter.len - idx - 2;
7561 else if (insn->k == FAIL)
7562 insn->k = msock_filter.len - idx - 3;
7563 }
7564
7565 if (insn->jt == PASS)
7566 insn->jt = msock_filter.len - idx - 2;
7567 else if (insn->jt == FAIL)
7568 insn->jt = msock_filter.len - idx - 3;
7569
7570 if (insn->jf == PASS)
7571 insn->jf = msock_filter.len - idx - 2;
7572 else if (insn->jf == FAIL)
7573 insn->jf = msock_filter.len - idx - 3;
7574 }
7575 }
7576
7577 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7578 &msock_filter, sizeof(msock_filter))) {
7ac3616d
JM
7579 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
7580 strerror(errno));
0915d02c
JM
7581 return -1;
7582 }
7583
7584 return 0;
7585}
7586
7587
460456f8
JM
7588static void nl80211_remove_monitor_interface(
7589 struct wpa_driver_nl80211_data *drv)
7590{
748c0ac0
JM
7591 if (drv->monitor_refcount > 0)
7592 drv->monitor_refcount--;
7593 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
7594 drv->monitor_refcount);
3fd1cefb
JB
7595 if (drv->monitor_refcount > 0)
7596 return;
7597
460456f8
JM
7598 if (drv->monitor_ifidx >= 0) {
7599 nl80211_remove_iface(drv, drv->monitor_ifidx);
7600 drv->monitor_ifidx = -1;
7601 }
504e905c
JM
7602 if (drv->monitor_sock >= 0) {
7603 eloop_unregister_read_sock(drv->monitor_sock);
7604 close(drv->monitor_sock);
7605 drv->monitor_sock = -1;
7606 }
460456f8
JM
7607}
7608
7609
0915d02c
JM
7610static int
7611nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7612{
7613 char buf[IFNAMSIZ];
7614 struct sockaddr_ll ll;
7615 int optval;
7616 socklen_t optlen;
0915d02c 7617
3fd1cefb
JB
7618 if (drv->monitor_ifidx >= 0) {
7619 drv->monitor_refcount++;
748c0ac0
JM
7620 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
7621 drv->monitor_refcount);
3fd1cefb
JB
7622 return 0;
7623 }
7624
834ee56f 7625 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
6758b167
JJ
7626 /*
7627 * P2P interface name is of the format p2p-%s-%d. For monitor
7628 * interface name corresponding to P2P GO, replace "p2p-" with
7629 * "mon-" to retain the same interface name length and to
7630 * indicate that it is a monitor interface.
7631 */
834ee56f 7632 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
6758b167
JJ
7633 } else {
7634 /* Non-P2P interface with AP functionality. */
834ee56f 7635 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
6758b167
JJ
7636 }
7637
0915d02c
JM
7638 buf[IFNAMSIZ - 1] = '\0';
7639
7640 drv->monitor_ifidx =
fbbfcbac 7641 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
2aec4f3c 7642 0, NULL, NULL, 0);
0915d02c 7643
866af8b6 7644 if (drv->monitor_ifidx == -EOPNOTSUPP) {
61cbe2ff
JB
7645 /*
7646 * This is backward compatibility for a few versions of
7647 * the kernel only that didn't advertise the right
7648 * attributes for the only driver that then supported
7649 * AP mode w/o monitor -- ath6kl.
7650 */
866af8b6
JM
7651 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7652 "monitor interface type - try to run without it");
61cbe2ff 7653 drv->device_ap_sme = 1;
866af8b6
JM
7654 }
7655
0915d02c
JM
7656 if (drv->monitor_ifidx < 0)
7657 return -1;
7658
c81eff1a 7659 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
0915d02c 7660 goto error;
0915d02c
JM
7661
7662 memset(&ll, 0, sizeof(ll));
7663 ll.sll_family = AF_PACKET;
7664 ll.sll_ifindex = drv->monitor_ifidx;
7665 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
7666 if (drv->monitor_sock < 0) {
7ac3616d
JM
7667 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
7668 strerror(errno));
0915d02c
JM
7669 goto error;
7670 }
7671
7672 if (add_monitor_filter(drv->monitor_sock)) {
7673 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
7674 "interface; do filtering in user space");
7675 /* This works, but will cost in performance. */
7676 }
7677
2135f224 7678 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
7ac3616d
JM
7679 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
7680 strerror(errno));
0915d02c
JM
7681 goto error;
7682 }
7683
7684 optlen = sizeof(optval);
7685 optval = 20;
7686 if (setsockopt
7687 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
7ac3616d
JM
7688 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
7689 strerror(errno));
0915d02c
JM
7690 goto error;
7691 }
7692
7693 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
7694 drv, NULL)) {
7ac3616d 7695 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
0915d02c
JM
7696 goto error;
7697 }
7698
748c0ac0 7699 drv->monitor_refcount++;
0915d02c
JM
7700 return 0;
7701 error:
460456f8 7702 nl80211_remove_monitor_interface(drv);
0915d02c
JM
7703 return -1;
7704}
7705
db149ac9 7706
3fd1cefb
JB
7707static int nl80211_setup_ap(struct i802_bss *bss)
7708{
7709 struct wpa_driver_nl80211_data *drv = bss->drv;
7710
748c0ac0
JM
7711 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
7712 bss->ifname, drv->device_ap_sme, drv->use_monitor);
36488c05 7713
a11241fa
JB
7714 /*
7715 * Disable Probe Request reporting unless we need it in this way for
7716 * devices that include the AP SME, in the other case (unless using
7717 * monitor iface) we'll get it through the nl_mgmt socket instead.
7718 */
7719 if (!drv->device_ap_sme)
7720 wpa_driver_nl80211_probe_req_report(bss, 0);
7721
7722 if (!drv->device_ap_sme && !drv->use_monitor)
7723 if (nl80211_mgmt_subscribe_ap(bss))
7724 return -1;
7725
a6cc0602
JM
7726 if (drv->device_ap_sme && !drv->use_monitor)
7727 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
7728 return -1;
7729
a11241fa 7730 if (!drv->device_ap_sme && drv->use_monitor &&
3fd1cefb
JB
7731 nl80211_create_monitor_interface(drv) &&
7732 !drv->device_ap_sme)
7733 return -1;
7734
7735 if (drv->device_ap_sme &&
7736 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
7737 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
7738 "Probe Request frame reporting in AP mode");
7739 /* Try to survive without this */
7740 }
7741
7742 return 0;
7743}
7744
7745
7746static void nl80211_teardown_ap(struct i802_bss *bss)
7747{
7748 struct wpa_driver_nl80211_data *drv = bss->drv;
7749
748c0ac0
JM
7750 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
7751 bss->ifname, drv->device_ap_sme, drv->use_monitor);
a6cc0602 7752 if (drv->device_ap_sme) {
3fd1cefb 7753 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
7754 if (!drv->use_monitor)
7755 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
7756 } else if (drv->use_monitor)
3fd1cefb 7757 nl80211_remove_monitor_interface(drv);
a11241fa 7758 else
36488c05 7759 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 7760
3fd1cefb
JB
7761 bss->beacon_set = 0;
7762}
7763
7764
f10bfc9a
JM
7765static int nl80211_send_eapol_data(struct i802_bss *bss,
7766 const u8 *addr, const u8 *data,
d12dab4c 7767 size_t data_len)
f10bfc9a 7768{
d12dab4c
JB
7769 struct sockaddr_ll ll;
7770 int ret;
7771
7772 if (bss->drv->eapol_tx_sock < 0) {
7773 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
7774 return -1;
7775 }
7776
d12dab4c
JB
7777 os_memset(&ll, 0, sizeof(ll));
7778 ll.sll_family = AF_PACKET;
7779 ll.sll_ifindex = bss->ifindex;
7780 ll.sll_protocol = htons(ETH_P_PAE);
7781 ll.sll_halen = ETH_ALEN;
7782 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
7783 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
7784 (struct sockaddr *) &ll, sizeof(ll));
7785 if (ret < 0)
7786 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
7787 strerror(errno));
7788
7789 return ret;
f10bfc9a 7790}
5fb1a232 7791
f10bfc9a 7792
db149ac9
JM
7793static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
7794
7795static int wpa_driver_nl80211_hapd_send_eapol(
7796 void *priv, const u8 *addr, const u8 *data,
4378fc14 7797 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 7798{
a2e40bb6
FF
7799 struct i802_bss *bss = priv;
7800 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
7801 struct ieee80211_hdr *hdr;
7802 size_t len;
7803 u8 *pos;
7804 int res;
4378fc14 7805 int qos = flags & WPA_STA_WMM;
db149ac9 7806
a11241fa 7807 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 7808 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 7809
db149ac9
JM
7810 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
7811 data_len;
7812 hdr = os_zalloc(len);
7813 if (hdr == NULL) {
7ac3616d
JM
7814 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
7815 (unsigned long) len);
db149ac9
JM
7816 return -1;
7817 }
7818
7819 hdr->frame_control =
7820 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
7821 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
7822 if (encrypt)
7823 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
7824 if (qos) {
7825 hdr->frame_control |=
7826 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
7827 }
db149ac9
JM
7828
7829 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7830 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7831 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7832 pos = (u8 *) (hdr + 1);
7833
db149ac9 7834 if (qos) {
92d521d8
FF
7835 /* Set highest priority in QoS header */
7836 pos[0] = 7;
db149ac9
JM
7837 pos[1] = 0;
7838 pos += 2;
7839 }
db149ac9
JM
7840
7841 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
7842 pos += sizeof(rfc1042_header);
7843 WPA_PUT_BE16(pos, ETH_P_PAE);
7844 pos += 2;
7845 memcpy(pos, data, data_len);
7846
55231068
JM
7847 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
7848 0, 0, 0, 0);
db149ac9
JM
7849 if (res < 0) {
7850 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
7851 "failed: %d (%s)",
7852 (unsigned long) len, errno, strerror(errno));
7853 }
7bf12757 7854 os_free(hdr);
db149ac9
JM
7855
7856 return res;
7857}
7858
a8d6ffa4 7859
3234cba4
JM
7860static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
7861 int total_flags,
4c32757d 7862 int flags_or, int flags_and)
a8d6ffa4 7863{
a2e40bb6
FF
7864 struct i802_bss *bss = priv;
7865 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
7866 struct nl_msg *msg;
7867 struct nlattr *flags;
7e76ee9c 7868 struct nl80211_sta_flag_update upd;
a8d6ffa4
JM
7869
7870 msg = nlmsg_alloc();
7871 if (!msg)
7872 return -ENOMEM;
7873
9fb04070 7874 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
a8d6ffa4
JM
7875
7876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3234cba4 7877 if_nametoindex(bss->ifname));
a8d6ffa4
JM
7878 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7879
7e76ee9c
JM
7880 /*
7881 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
7882 * can be removed eventually.
7883 */
8970bae8
JB
7884 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
7885 if (!flags)
7886 goto nla_put_failure;
0de39516 7887 if (total_flags & WPA_STA_AUTHORIZED)
8970bae8 7888 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
a8d6ffa4 7889
0de39516 7890 if (total_flags & WPA_STA_WMM)
8970bae8 7891 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
a8d6ffa4 7892
0de39516 7893 if (total_flags & WPA_STA_SHORT_PREAMBLE)
8970bae8 7894 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
a8d6ffa4 7895
0de39516 7896 if (total_flags & WPA_STA_MFP)
8970bae8 7897 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
a8d6ffa4 7898
45b722f1 7899 if (total_flags & WPA_STA_TDLS_PEER)
8970bae8 7900 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
45b722f1 7901
8970bae8 7902 nla_nest_end(msg, flags);
a8d6ffa4 7903
7e76ee9c
JM
7904 os_memset(&upd, 0, sizeof(upd));
7905 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
7906 upd.set = sta_flags_nl80211(flags_or);
7907 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7908
a8d6ffa4
JM
7909 return send_and_recv_msgs(drv, msg, NULL, NULL);
7910 nla_put_failure:
5883168a 7911 nlmsg_free(msg);
a8d6ffa4
JM
7912 return -ENOBUFS;
7913}
7914
0915d02c 7915
1581b38b
JM
7916static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
7917 struct wpa_driver_associate_params *params)
7918{
708bc8e0 7919 enum nl80211_iftype nlmode, old_mode;
89b800d7
JB
7920 struct hostapd_freq_params freq = {
7921 .freq = params->freq,
7922 };
b1f625e0
EP
7923
7924 if (params->p2p) {
046b26a2
JM
7925 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
7926 "group (GO)");
b1f625e0
EP
7927 nlmode = NL80211_IFTYPE_P2P_GO;
7928 } else
7929 nlmode = NL80211_IFTYPE_AP;
7930
708bc8e0 7931 old_mode = drv->nlmode;
834ee56f 7932 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
708bc8e0
JM
7933 nl80211_remove_monitor_interface(drv);
7934 return -1;
7935 }
7936
834ee56f 7937 if (wpa_driver_nl80211_set_freq(drv->first_bss, &freq)) {
708bc8e0 7938 if (old_mode != nlmode)
834ee56f 7939 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
460456f8 7940 nl80211_remove_monitor_interface(drv);
1581b38b 7941 return -1;
0915d02c 7942 }
1581b38b 7943
1581b38b
JM
7944 return 0;
7945}
1581b38b
JM
7946
7947
5cc4d64b
JM
7948static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7949{
7950 struct nl_msg *msg;
7951 int ret = -1;
7952
7953 msg = nlmsg_alloc();
7954 if (!msg)
7955 return -1;
7956
9fb04070 7957 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b
JM
7958 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7959 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7960 msg = NULL;
7961 if (ret) {
7962 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7963 "(%s)", ret, strerror(-ret));
7964 goto nla_put_failure;
7965 }
7966
7967 ret = 0;
7968 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7969
7970nla_put_failure:
834ee56f 7971 if (wpa_driver_nl80211_set_mode(drv->first_bss,
5d4c78fb
JM
7972 NL80211_IFTYPE_STATION)) {
7973 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7974 "station mode");
7975 }
7976
5cc4d64b
JM
7977 nlmsg_free(msg);
7978 return ret;
7979}
7980
7981
7982static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7983 struct wpa_driver_associate_params *params)
7984{
7985 struct nl_msg *msg;
7986 int ret = -1;
7987 int count = 0;
7988
7989 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7990
834ee56f 7991 if (wpa_driver_nl80211_set_mode(drv->first_bss,
b1f625e0 7992 NL80211_IFTYPE_ADHOC)) {
5cc4d64b
JM
7993 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7994 "IBSS mode");
7995 return -1;
7996 }
7997
7998retry:
7999 msg = nlmsg_alloc();
8000 if (!msg)
8001 return -1;
8002
9fb04070 8003 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5cc4d64b
JM
8004 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8005
8006 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8007 goto nla_put_failure;
8008
8009 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8010 params->ssid, params->ssid_len);
8011 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8012 params->ssid);
8013 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8014 drv->ssid_len = params->ssid_len;
8015
8016 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8017 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8018
8019 ret = nl80211_set_conn_keys(params, msg);
8020 if (ret)
8021 goto nla_put_failure;
8022
913e3cf7
NC
8023 if (params->bssid && params->fixed_bssid) {
8024 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8025 MAC2STR(params->bssid));
8026 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8027 }
8028
4848a38d
JM
8029 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8030 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8031 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8032 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
e640888c
AQ
8033 wpa_printf(MSG_DEBUG, " * control port");
8034 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8035 }
8036
a95795ad
JM
8037 if (params->wpa_ie) {
8038 wpa_hexdump(MSG_DEBUG,
8039 " * Extra IEs for Beacon/Probe Response frames",
8040 params->wpa_ie, params->wpa_ie_len);
8041 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8042 params->wpa_ie);
8043 }
8044
5cc4d64b
JM
8045 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8046 msg = NULL;
8047 if (ret) {
8048 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8049 ret, strerror(-ret));
8050 count++;
8051 if (ret == -EALREADY && count == 1) {
8052 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8053 "forced leave");
8054 nl80211_leave_ibss(drv);
8055 nlmsg_free(msg);
8056 goto retry;
8057 }
8058
8059 goto nla_put_failure;
8060 }
8061 ret = 0;
8062 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8063
8064nla_put_failure:
8065 nlmsg_free(msg);
8066 return ret;
8067}
8068
8069
a8c5b43a 8070static int wpa_driver_nl80211_try_connect(
cfaab580
ZY
8071 struct wpa_driver_nl80211_data *drv,
8072 struct wpa_driver_associate_params *params)
8073{
8074 struct nl_msg *msg;
8075 enum nl80211_auth_type type;
8076 int ret = 0;
3f360238 8077 int algs;
cfaab580
ZY
8078
8079 msg = nlmsg_alloc();
8080 if (!msg)
8081 return -1;
8082
8083 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9fb04070 8084 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
cfaab580
ZY
8085
8086 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8087 if (params->bssid) {
8088 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8089 MAC2STR(params->bssid));
8090 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8091 }
8092 if (params->freq) {
8093 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8094 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
30158a0d
SD
8095 drv->assoc_freq = params->freq;
8096 } else
8097 drv->assoc_freq = 0;
1f6c0ab8
BS
8098 if (params->bg_scan_period >= 0) {
8099 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8100 params->bg_scan_period);
8101 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8102 params->bg_scan_period);
8103 }
cfaab580
ZY
8104 if (params->ssid) {
8105 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8106 params->ssid, params->ssid_len);
8107 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8108 params->ssid);
8109 if (params->ssid_len > sizeof(drv->ssid))
8110 goto nla_put_failure;
8111 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8112 drv->ssid_len = params->ssid_len;
8113 }
8114 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8115 if (params->wpa_ie)
8116 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8117 params->wpa_ie);
8118
3f360238
JM
8119 algs = 0;
8120 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8121 algs++;
8122 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8123 algs++;
8124 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8125 algs++;
8126 if (algs > 1) {
8127 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8128 "selection");
8129 goto skip_auth_type;
8130 }
8131
abd9fafa 8132 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
cfaab580 8133 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 8134 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
cfaab580 8135 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 8136 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
cfaab580 8137 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 8138 else if (params->auth_alg & WPA_AUTH_ALG_FT)
cfaab580
ZY
8139 type = NL80211_AUTHTYPE_FT;
8140 else
8141 goto nla_put_failure;
8142
8143 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8144 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8145
3f360238 8146skip_auth_type:
64fa840a
JM
8147 if (params->wpa_proto) {
8148 enum nl80211_wpa_versions ver = 0;
cfaab580 8149
64fa840a
JM
8150 if (params->wpa_proto & WPA_PROTO_WPA)
8151 ver |= NL80211_WPA_VERSION_1;
8152 if (params->wpa_proto & WPA_PROTO_RSN)
8153 ver |= NL80211_WPA_VERSION_2;
cfaab580 8154
64fa840a 8155 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
cfaab580
ZY
8156 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8157 }
8158
4848a38d 8159 if (params->pairwise_suite != WPA_CIPHER_NONE) {
de4ed4a8 8160 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4848a38d 8161 wpa_cipher_to_cipher_suite(params->pairwise_suite));
cfaab580
ZY
8162 }
8163
4848a38d 8164 if (params->group_suite != WPA_CIPHER_NONE) {
de4ed4a8 8165 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4848a38d 8166 wpa_cipher_to_cipher_suite(params->group_suite));
cfaab580
ZY
8167 }
8168
4848a38d
JM
8169 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8170 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8171 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8172 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
8173 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM) {
cfaab580
ZY
8174 int mgmt = WLAN_AKM_SUITE_PSK;
8175
8176 switch (params->key_mgmt_suite) {
4848a38d 8177 case WPA_KEY_MGMT_CCKM:
369c8d7b
JM
8178 mgmt = WLAN_AKM_SUITE_CCKM;
8179 break;
4848a38d 8180 case WPA_KEY_MGMT_IEEE8021X:
cfaab580
ZY
8181 mgmt = WLAN_AKM_SUITE_8021X;
8182 break;
4848a38d 8183 case WPA_KEY_MGMT_FT_IEEE8021X:
6a1ce395
DG
8184 mgmt = WLAN_AKM_SUITE_FT_8021X;
8185 break;
4848a38d 8186 case WPA_KEY_MGMT_FT_PSK:
6a1ce395
DG
8187 mgmt = WLAN_AKM_SUITE_FT_PSK;
8188 break;
4848a38d 8189 case WPA_KEY_MGMT_PSK:
cfaab580
ZY
8190 default:
8191 mgmt = WLAN_AKM_SUITE_PSK;
8192 break;
8193 }
8194 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8195 }
8196
8b706a99
JM
8197#ifdef CONFIG_IEEE80211W
8198 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8199 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8200#endif /* CONFIG_IEEE80211W */
8201
a565084f
JM
8202 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8203
80e8a5ee
BG
8204 if (params->disable_ht)
8205 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8206
8207 if (params->htcaps && params->htcaps_mask) {
8208 int sz = sizeof(struct ieee80211_ht_capabilities);
8209 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8210 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8211 params->htcaps_mask);
8212 }
8213
e9ee8dc3
JB
8214#ifdef CONFIG_VHT_OVERRIDES
8215 if (params->disable_vht) {
8216 wpa_printf(MSG_DEBUG, " * VHT disabled");
8217 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8218 }
8219
8220 if (params->vhtcaps && params->vhtcaps_mask) {
8221 int sz = sizeof(struct ieee80211_vht_capabilities);
8222 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8223 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8224 params->vhtcaps_mask);
8225 }
8226#endif /* CONFIG_VHT_OVERRIDES */
8227
cfaab580
ZY
8228 ret = nl80211_set_conn_keys(params, msg);
8229 if (ret)
8230 goto nla_put_failure;
8231
8232 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8233 msg = NULL;
8234 if (ret) {
8235 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8236 "(%s)", ret, strerror(-ret));
8237 goto nla_put_failure;
8238 }
8239 ret = 0;
8240 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8241
8242nla_put_failure:
8243 nlmsg_free(msg);
8244 return ret;
8245
8246}
8247
8248
a8c5b43a
CW
8249static int wpa_driver_nl80211_connect(
8250 struct wpa_driver_nl80211_data *drv,
8251 struct wpa_driver_associate_params *params)
8252{
8253 int ret = wpa_driver_nl80211_try_connect(drv, params);
8254 if (ret == -EALREADY) {
8255 /*
8256 * cfg80211 does not currently accept new connections if
8257 * we are already connected. As a workaround, force
8258 * disconnection and try again.
8259 */
8260 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8261 "disconnecting before reassociation "
8262 "attempt");
8263 if (wpa_driver_nl80211_disconnect(
8264 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8265 return -1;
a8c5b43a
CW
8266 ret = wpa_driver_nl80211_try_connect(drv, params);
8267 }
8268 return ret;
8269}
8270
8271
c2a04078
JM
8272static int wpa_driver_nl80211_associate(
8273 void *priv, struct wpa_driver_associate_params *params)
8274{
a2e40bb6
FF
8275 struct i802_bss *bss = priv;
8276 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
8277 int ret = -1;
8278 struct nl_msg *msg;
8279
5cc4d64b 8280 if (params->mode == IEEE80211_MODE_AP)
1581b38b 8281 return wpa_driver_nl80211_ap(drv, params);
1581b38b 8282
5cc4d64b
JM
8283 if (params->mode == IEEE80211_MODE_IBSS)
8284 return wpa_driver_nl80211_ibss(drv, params);
8285
4a867032 8286 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
8287 enum nl80211_iftype nlmode = params->p2p ?
8288 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8289
8290 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 8291 return -1;
cfaab580 8292 return wpa_driver_nl80211_connect(drv, params);
4a867032 8293 }
cfaab580 8294
add9b7a4 8295 nl80211_mark_disconnected(drv);
c2a04078
JM
8296
8297 msg = nlmsg_alloc();
8298 if (!msg)
8299 return -1;
8300
8301 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8302 drv->ifindex);
9fb04070 8303 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
c2a04078
JM
8304
8305 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8306 if (params->bssid) {
8307 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8308 MAC2STR(params->bssid));
8309 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8310 }
8311 if (params->freq) {
8312 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8313 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4832ecd7
JM
8314 drv->assoc_freq = params->freq;
8315 } else
8316 drv->assoc_freq = 0;
1f6c0ab8
BS
8317 if (params->bg_scan_period >= 0) {
8318 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8319 params->bg_scan_period);
8320 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8321 params->bg_scan_period);
8322 }
c2a04078
JM
8323 if (params->ssid) {
8324 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8325 params->ssid, params->ssid_len);
8326 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8327 params->ssid);
fd05d64e
JM
8328 if (params->ssid_len > sizeof(drv->ssid))
8329 goto nla_put_failure;
8330 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8331 drv->ssid_len = params->ssid_len;
c2a04078
JM
8332 }
8333 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8334 if (params->wpa_ie)
8335 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8336 params->wpa_ie);
8337
4848a38d
JM
8338 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8339 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
79c3124c 8340 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
aca01605
JM
8341 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8342 }
8343
4848a38d
JM
8344 if (params->group_suite != WPA_CIPHER_NONE) {
8345 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
79c3124c 8346 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
aca01605
JM
8347 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8348 }
8349
e572fa33
JM
8350#ifdef CONFIG_IEEE80211W
8351 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8352 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8353#endif /* CONFIG_IEEE80211W */
8354
01652550
JM
8355 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8356
62fa124c
JM
8357 if (params->prev_bssid) {
8358 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8359 MAC2STR(params->prev_bssid));
8360 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8361 params->prev_bssid);
8362 }
8363
80e8a5ee
BG
8364 if (params->disable_ht)
8365 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8366
8367 if (params->htcaps && params->htcaps_mask) {
8368 int sz = sizeof(struct ieee80211_ht_capabilities);
8369 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
8370 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8371 params->htcaps_mask);
8372 }
8373
e9ee8dc3
JB
8374#ifdef CONFIG_VHT_OVERRIDES
8375 if (params->disable_vht) {
8376 wpa_printf(MSG_DEBUG, " * VHT disabled");
8377 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8378 }
8379
8380 if (params->vhtcaps && params->vhtcaps_mask) {
8381 int sz = sizeof(struct ieee80211_vht_capabilities);
8382 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
8383 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8384 params->vhtcaps_mask);
8385 }
8386#endif /* CONFIG_VHT_OVERRIDES */
8387
046b26a2
JM
8388 if (params->p2p)
8389 wpa_printf(MSG_DEBUG, " * P2P group");
8390
c2a04078
JM
8391 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8392 msg = NULL;
8393 if (ret) {
3b7ea880
BG
8394 wpa_dbg(drv->ctx, MSG_DEBUG,
8395 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8396 ret, strerror(-ret));
8856462d 8397 nl80211_dump_scan(drv);
c2a04078
JM
8398 goto nla_put_failure;
8399 }
8400 ret = 0;
8401 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8402 "successfully");
8403
8404nla_put_failure:
8405 nlmsg_free(msg);
8406 return ret;
8407}
3f5285e8
JM
8408
8409
ad1e68e6 8410static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 8411 int ifindex, enum nl80211_iftype mode)
3f5285e8 8412{
3f5285e8 8413 struct nl_msg *msg;
ad1e68e6
JM
8414 int ret = -ENOBUFS;
8415
a1922f93
JM
8416 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8417 ifindex, mode, nl80211_iftype_str(mode));
8418
ad1e68e6
JM
8419 msg = nlmsg_alloc();
8420 if (!msg)
8421 return -ENOMEM;
8422
9fb04070 8423 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
834ee56f 8424 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
f632e483 8425 goto nla_put_failure;
ad1e68e6
JM
8426 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8427
8428 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 8429 msg = NULL;
ad1e68e6
JM
8430 if (!ret)
8431 return 0;
8432nla_put_failure:
5883168a 8433 nlmsg_free(msg);
ad1e68e6
JM
8434 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8435 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8436 return ret;
8437}
8438
8439
b1f625e0
EP
8440static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8441 enum nl80211_iftype nlmode)
ad1e68e6 8442{
a2e40bb6 8443 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 8444 int ret = -1;
26af9dca 8445 int i;
86957e62 8446 int was_ap = is_ap_interface(drv->nlmode);
671a5039 8447 int res;
1581b38b 8448
671a5039 8449 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
8e12685c
AS
8450 if (res && nlmode == nl80211_get_ifmode(bss))
8451 res = 0;
8452
671a5039 8453 if (res == 0) {
ad1e68e6 8454 drv->nlmode = nlmode;
460456f8
JM
8455 ret = 0;
8456 goto done;
ad1e68e6 8457 }
3f5285e8 8458
671a5039
JM
8459 if (res == -ENODEV)
8460 return -1;
8461
460456f8 8462 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
8463 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8464 "requested mode - ignore error");
460456f8
JM
8465 ret = 0;
8466 goto done; /* Already in the requested mode */
8467 }
3f5285e8 8468
3f5285e8
JM
8469 /* mac80211 doesn't allow mode changes while the device is up, so
8470 * take the device down, try to set the mode again, and bring the
8471 * device back up.
8472 */
26af9dca
JM
8473 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
8474 "interface down");
8475 for (i = 0; i < 10; i++) {
91724d6f 8476 res = i802_set_iface_flags(bss, 0);
6e8183d7
JM
8477 if (res == -EACCES || res == -ENODEV)
8478 break;
8479 if (res == 0) {
26af9dca
JM
8480 /* Try to set the mode again while the interface is
8481 * down */
8482 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
6e8183d7
JM
8483 if (ret == -EACCES)
8484 break;
91724d6f 8485 res = i802_set_iface_flags(bss, 1);
6e8183d7 8486 if (res && !ret)
26af9dca 8487 ret = -1;
6e8183d7 8488 else if (ret != -EBUSY)
26af9dca
JM
8489 break;
8490 } else
8491 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
8492 "interface down");
8493 os_sleep(0, 100000);
3f5285e8
JM
8494 }
8495
c6e8e8e4
JM
8496 if (!ret) {
8497 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
8498 "interface is down");
ad1e68e6 8499 drv->nlmode = nlmode;
7d9c3698 8500 drv->ignore_if_down_event = 1;
c6e8e8e4 8501 }
ad1e68e6 8502
460456f8 8503done:
3fd1cefb
JB
8504 if (ret) {
8505 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
8506 "from %d failed", nlmode, drv->nlmode);
8507 return ret;
8508 }
8509
6a71413e 8510 if (is_p2p_net_interface(nlmode))
edb9bfba 8511 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1d0c6fb1
JM
8512 else if (drv->disabled_11b_rates)
8513 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
edb9bfba 8514
3fd1cefb 8515 if (is_ap_interface(nlmode)) {
36488c05 8516 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 8517 /* Setup additional AP mode functionality if needed */
3fd1cefb 8518 if (nl80211_setup_ap(bss))
460456f8 8519 return -1;
3fd1cefb 8520 } else if (was_ap) {
460456f8 8521 /* Remove additional AP mode functionality */
3fd1cefb 8522 nl80211_teardown_ap(bss);
a11241fa 8523 } else {
36488c05 8524 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 8525 }
460456f8 8526
873d0fcf 8527 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
a11241fa
JB
8528 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8529 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8530 "frame processing - ignore for now");
08359050 8531
3fd1cefb 8532 return 0;
3f5285e8
JM
8533}
8534
8535
3f5285e8
JM
8536static int wpa_driver_nl80211_get_capa(void *priv,
8537 struct wpa_driver_capa *capa)
8538{
a2e40bb6
FF
8539 struct i802_bss *bss = priv;
8540 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
8541 if (!drv->has_capability)
8542 return -1;
8543 os_memcpy(capa, &drv->capa, sizeof(*capa));
8cd6b7bc
JB
8544 if (drv->extended_capa && drv->extended_capa_mask) {
8545 capa->extended_capa = drv->extended_capa;
8546 capa->extended_capa_mask = drv->extended_capa_mask;
8547 capa->extended_capa_len = drv->extended_capa_len;
8548 }
851b0c55
JM
8549
8550 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8551 !drv->allow_p2p_device) {
8552 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
8553 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
8554 }
8555
3f5285e8
JM
8556 return 0;
8557}
8558
8559
8560static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8561{
a2e40bb6
FF
8562 struct i802_bss *bss = priv;
8563 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
8564
8565 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
8566 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
8567 drv->operstate = state;
36d84860 8568 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 8569 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
8570}
8571
01652550
JM
8572
8573static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8574{
a2e40bb6
FF
8575 struct i802_bss *bss = priv;
8576 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
8577 struct nl_msg *msg;
8578 struct nl80211_sta_flag_update upd;
8579
1ba51ec0
JM
8580 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8581 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8582
01652550
JM
8583 msg = nlmsg_alloc();
8584 if (!msg)
8585 return -ENOMEM;
8586
9fb04070 8587 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
01652550
JM
8588
8589 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 8590 if_nametoindex(bss->ifname));
01652550
JM
8591 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8592
8593 os_memset(&upd, 0, sizeof(upd));
8594 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8595 if (authorized)
8596 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8597 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8598
8599 return send_and_recv_msgs(drv, msg, NULL, NULL);
8600 nla_put_failure:
5883168a 8601 nlmsg_free(msg);
01652550
JM
8602 return -ENOBUFS;
8603}
8604
3f5285e8 8605
f07ead6a
JM
8606/* Set kernel driver on given frequency (MHz) */
8607static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 8608{
f07ead6a 8609 struct i802_bss *bss = priv;
89b800d7 8610 return wpa_driver_nl80211_set_freq(bss, freq);
c5121837
JM
8611}
8612
f7b3920c 8613
c5121837
JM
8614static inline int min_int(int a, int b)
8615{
8616 if (a < b)
8617 return a;
8618 return b;
8619}
8620
8621
8622static int get_key_handler(struct nl_msg *msg, void *arg)
8623{
8624 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8625 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8626
8627 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8628 genlmsg_attrlen(gnlh, 0), NULL);
8629
8630 /*
8631 * TODO: validate the key index and mac address!
8632 * Otherwise, there's a race condition as soon as
8633 * the kernel starts sending key notifications.
8634 */
8635
8636 if (tb[NL80211_ATTR_KEY_SEQ])
8637 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8638 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8639 return NL_SKIP;
8640}
8641
8642
8643static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8644 int idx, u8 *seq)
8645{
a2e40bb6
FF
8646 struct i802_bss *bss = priv;
8647 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8648 struct nl_msg *msg;
8649
8650 msg = nlmsg_alloc();
8651 if (!msg)
8652 return -ENOMEM;
8653
9fb04070 8654 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
c5121837
JM
8655
8656 if (addr)
8657 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8658 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8659 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8660
8661 memset(seq, 0, 6);
8662
8663 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8664 nla_put_failure:
9e088e74 8665 nlmsg_free(msg);
c5121837
JM
8666 return -ENOBUFS;
8667}
8668
8669
c5121837
JM
8670static int i802_set_rts(void *priv, int rts)
8671{
a2e40bb6
FF
8672 struct i802_bss *bss = priv;
8673 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
8674 struct nl_msg *msg;
8675 int ret = -ENOBUFS;
8676 u32 val;
c5121837 8677
ad649451
JM
8678 msg = nlmsg_alloc();
8679 if (!msg)
8680 return -ENOMEM;
c5121837 8681
ad649451
JM
8682 if (rts >= 2347)
8683 val = (u32) -1;
8684 else
8685 val = rts;
c5121837 8686
9fb04070 8687 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
8688 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8689 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
8690
8691 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 8692 msg = NULL;
ad649451
JM
8693 if (!ret)
8694 return 0;
8695nla_put_failure:
5883168a 8696 nlmsg_free(msg);
ad649451
JM
8697 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
8698 "%d (%s)", rts, ret, strerror(-ret));
8699 return ret;
c5121837
JM
8700}
8701
8702
8703static int i802_set_frag(void *priv, int frag)
8704{
a2e40bb6
FF
8705 struct i802_bss *bss = priv;
8706 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
8707 struct nl_msg *msg;
8708 int ret = -ENOBUFS;
8709 u32 val;
c5121837 8710
ad649451
JM
8711 msg = nlmsg_alloc();
8712 if (!msg)
8713 return -ENOMEM;
c5121837 8714
ad649451
JM
8715 if (frag >= 2346)
8716 val = (u32) -1;
8717 else
8718 val = frag;
c5121837 8719
9fb04070 8720 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
8721 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8722 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
8723
8724 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 8725 msg = NULL;
ad649451
JM
8726 if (!ret)
8727 return 0;
8728nla_put_failure:
5883168a 8729 nlmsg_free(msg);
ad649451
JM
8730 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
8731 "%d: %d (%s)", frag, ret, strerror(-ret));
8732 return ret;
c5121837
JM
8733}
8734
8735
c5121837
JM
8736static int i802_flush(void *priv)
8737{
a2e40bb6
FF
8738 struct i802_bss *bss = priv;
8739 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 8740 struct nl_msg *msg;
82554b10 8741 int res;
c5121837
JM
8742
8743 msg = nlmsg_alloc();
8744 if (!msg)
8745 return -1;
8746
83e7bb0e
JM
8747 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
8748 bss->ifname);
9fb04070 8749 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
c5121837
JM
8750
8751 /*
8752 * XXX: FIX! this needs to flush all VLANs too
8753 */
8754 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 8755 if_nametoindex(bss->ifname));
c5121837 8756
82554b10
JM
8757 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8758 if (res) {
8759 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
8760 "(%s)", res, strerror(-res));
8761 }
8762 return res;
c5121837 8763 nla_put_failure:
9e088e74 8764 nlmsg_free(msg);
c5121837
JM
8765 return -ENOBUFS;
8766}
8767
8768
8769static int get_sta_handler(struct nl_msg *msg, void *arg)
8770{
8771 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8772 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8773 struct hostap_sta_driver_data *data = arg;
8774 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
8775 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
8776 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
8777 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
8778 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
8779 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
8780 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 8781 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
c5121837
JM
8782 };
8783
8784 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8785 genlmsg_attrlen(gnlh, 0), NULL);
8786
8787 /*
8788 * TODO: validate the interface and mac address!
8789 * Otherwise, there's a race condition as soon as
8790 * the kernel starts sending station notifications.
8791 */
8792
8793 if (!tb[NL80211_ATTR_STA_INFO]) {
8794 wpa_printf(MSG_DEBUG, "sta stats missing!");
8795 return NL_SKIP;
8796 }
8797 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
8798 tb[NL80211_ATTR_STA_INFO],
8799 stats_policy)) {
8800 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
8801 return NL_SKIP;
8802 }
8803
8804 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
8805 data->inactive_msec =
8806 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
8807 if (stats[NL80211_STA_INFO_RX_BYTES])
8808 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
8809 if (stats[NL80211_STA_INFO_TX_BYTES])
8810 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
8811 if (stats[NL80211_STA_INFO_RX_PACKETS])
8812 data->rx_packets =
8813 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
8814 if (stats[NL80211_STA_INFO_TX_PACKETS])
8815 data->tx_packets =
8816 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
8817 if (stats[NL80211_STA_INFO_TX_FAILED])
8818 data->tx_retry_failed =
8819 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
c5121837
JM
8820
8821 return NL_SKIP;
8822}
8823
9ebce9c5
JM
8824static int i802_read_sta_data(struct i802_bss *bss,
8825 struct hostap_sta_driver_data *data,
c5121837
JM
8826 const u8 *addr)
8827{
a2e40bb6 8828 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8829 struct nl_msg *msg;
8830
8831 os_memset(data, 0, sizeof(*data));
8832 msg = nlmsg_alloc();
8833 if (!msg)
8834 return -ENOMEM;
8835
9fb04070 8836 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
c5121837
JM
8837
8838 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
a2e40bb6 8839 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
8840
8841 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
8842 nla_put_failure:
9e088e74 8843 nlmsg_free(msg);
c5121837
JM
8844 return -ENOBUFS;
8845}
8846
8847
c5121837
JM
8848static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
8849 int cw_min, int cw_max, int burst_time)
8850{
a2e40bb6
FF
8851 struct i802_bss *bss = priv;
8852 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8853 struct nl_msg *msg;
8854 struct nlattr *txq, *params;
8855
8856 msg = nlmsg_alloc();
8857 if (!msg)
8858 return -1;
8859
9fb04070 8860 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
c5121837 8861
a2e40bb6 8862 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
8863
8864 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8865 if (!txq)
8866 goto nla_put_failure;
8867
8868 /* We are only sending parameters for a single TXQ at a time */
8869 params = nla_nest_start(msg, 1);
8870 if (!params)
8871 goto nla_put_failure;
8872
7e3c1781
JM
8873 switch (queue) {
8874 case 0:
8875 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8876 break;
8877 case 1:
8878 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8879 break;
8880 case 2:
8881 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8882 break;
8883 case 3:
8884 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8885 break;
8886 }
c5121837
JM
8887 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8888 * 32 usec, so need to convert the value here. */
8889 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8890 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8891 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8892 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8893
8894 nla_nest_end(msg, params);
8895
8896 nla_nest_end(msg, txq);
8897
8898 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8899 return 0;
9e088e74 8900 msg = NULL;
c5121837 8901 nla_put_failure:
9e088e74 8902 nlmsg_free(msg);
c5121837
JM
8903 return -1;
8904}
8905
8906
9ebce9c5 8907static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
8908 const char *ifname, int vlan_id)
8909{
a2e40bb6 8910 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 8911 struct nl_msg *msg;
cd1d72c1 8912 int ret = -ENOBUFS;
c5121837
JM
8913
8914 msg = nlmsg_alloc();
8915 if (!msg)
8916 return -ENOMEM;
8917
bbc706a3
JM
8918 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
8919 ", ifname=%s[%d], vlan_id=%d)",
8920 bss->ifname, if_nametoindex(bss->ifname),
8921 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
9fb04070 8922 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
c5121837
JM
8923
8924 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 8925 if_nametoindex(bss->ifname));
c5121837 8926 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1c766b09 8927 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
c5121837
JM
8928 if_nametoindex(ifname));
8929
cd1d72c1 8930 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9e088e74 8931 msg = NULL;
cd1d72c1
JM
8932 if (ret < 0) {
8933 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8934 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8935 MAC2STR(addr), ifname, vlan_id, ret,
8936 strerror(-ret));
8937 }
c5121837 8938 nla_put_failure:
9e088e74 8939 nlmsg_free(msg);
cd1d72c1 8940 return ret;
c5121837
JM
8941}
8942
fbbfcbac 8943
c5121837
JM
8944static int i802_get_inact_sec(void *priv, const u8 *addr)
8945{
8946 struct hostap_sta_driver_data data;
8947 int ret;
8948
8949 data.inactive_msec = (unsigned long) -1;
8950 ret = i802_read_sta_data(priv, &data, addr);
8951 if (ret || data.inactive_msec == (unsigned long) -1)
8952 return -1;
8953 return data.inactive_msec / 1000;
8954}
8955
8956
8957static int i802_sta_clear_stats(void *priv, const u8 *addr)
8958{
8959#if 0
8960 /* TODO */
8961#endif
8962 return 0;
8963}
8964
8965
731723a5
JM
8966static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8967 int reason)
c5121837 8968{
a2e40bb6 8969 struct i802_bss *bss = priv;
e1bd4e19 8970 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8971 struct ieee80211_mgmt mgmt;
8972
e1bd4e19
AT
8973 if (drv->device_ap_sme)
8974 return wpa_driver_nl80211_sta_remove(bss, addr);
8975
c5121837
JM
8976 memset(&mgmt, 0, sizeof(mgmt));
8977 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8978 WLAN_FC_STYPE_DEAUTH);
8979 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
8980 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8981 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 8982 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 8983 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 8984 IEEE80211_HDRLEN +
9ebce9c5
JM
8985 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8986 0);
c5121837
JM
8987}
8988
8989
731723a5
JM
8990static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8991 int reason)
c5121837 8992{
a2e40bb6 8993 struct i802_bss *bss = priv;
e1bd4e19 8994 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
8995 struct ieee80211_mgmt mgmt;
8996
e1bd4e19
AT
8997 if (drv->device_ap_sme)
8998 return wpa_driver_nl80211_sta_remove(bss, addr);
8999
c5121837
JM
9000 memset(&mgmt, 0, sizeof(mgmt));
9001 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9002 WLAN_FC_STYPE_DISASSOC);
9003 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
9004 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9005 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 9006 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 9007 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 9008 IEEE80211_HDRLEN +
9ebce9c5
JM
9009 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9010 0);
c5121837
JM
9011}
9012
9013
f07ead6a
JM
9014static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9015{
9016 int i;
9017 int *old;
9018
9019 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9020 ifidx);
9021 for (i = 0; i < drv->num_if_indices; i++) {
9022 if (drv->if_indices[i] == 0) {
9023 drv->if_indices[i] = ifidx;
9024 return;
9025 }
9026 }
9027
9028 if (drv->if_indices != drv->default_if_indices)
9029 old = drv->if_indices;
9030 else
9031 old = NULL;
9032
067ffa26
JM
9033 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9034 sizeof(int));
f07ead6a
JM
9035 if (!drv->if_indices) {
9036 if (!old)
9037 drv->if_indices = drv->default_if_indices;
9038 else
9039 drv->if_indices = old;
9040 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9041 "interfaces");
9042 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9043 return;
9044 } else if (!old)
9045 os_memcpy(drv->if_indices, drv->default_if_indices,
9046 sizeof(drv->default_if_indices));
9047 drv->if_indices[drv->num_if_indices] = ifidx;
9048 drv->num_if_indices++;
9049}
9050
9051
9052static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9053{
9054 int i;
9055
9056 for (i = 0; i < drv->num_if_indices; i++) {
9057 if (drv->if_indices[i] == ifidx) {
9058 drv->if_indices[i] = 0;
9059 break;
9060 }
9061 }
9062}
9063
9064
9065static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9066{
9067 int i;
9068
9069 for (i = 0; i < drv->num_if_indices; i++)
9070 if (drv->if_indices[i] == ifidx)
9071 return 1;
9072
9073 return 0;
9074}
9075
9076
9077static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
69dd2967 9078 const char *bridge_ifname, char *ifname_wds)
f07ead6a
JM
9079{
9080 struct i802_bss *bss = priv;
9081 struct wpa_driver_nl80211_data *drv = bss->drv;
9082 char name[IFNAMSIZ + 1];
9083
9084 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
69dd2967
SM
9085 if (ifname_wds)
9086 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9087
f07ead6a
JM
9088 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9089 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9090 if (val) {
9091 if (!if_nametoindex(name)) {
9092 if (nl80211_create_iface(drv, name,
9093 NL80211_IFTYPE_AP_VLAN,
2aec4f3c
JM
9094 bss->addr, 1, NULL, NULL, 0) <
9095 0)
f07ead6a
JM
9096 return -1;
9097 if (bridge_ifname &&
c81eff1a
BG
9098 linux_br_add_if(drv->global->ioctl_sock,
9099 bridge_ifname, name) < 0)
f07ead6a
JM
9100 return -1;
9101 }
75227f3a
JM
9102 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9103 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9104 "interface %s up", name);
9105 }
f07ead6a
JM
9106 return i802_set_sta_vlan(priv, addr, name, 0);
9107 } else {
c34e618d
FF
9108 if (bridge_ifname)
9109 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9110 name);
9111
f07ead6a
JM
9112 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
9113 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
9114 name);
9115 }
9116}
9117
9118
9119static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9120{
9121 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9122 struct sockaddr_ll lladdr;
9123 unsigned char buf[3000];
9124 int len;
9125 socklen_t fromlen = sizeof(lladdr);
9126
9127 len = recvfrom(sock, buf, sizeof(buf), 0,
9128 (struct sockaddr *)&lladdr, &fromlen);
9129 if (len < 0) {
7ac3616d
JM
9130 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9131 strerror(errno));
f07ead6a
JM
9132 return;
9133 }
9134
9135 if (have_ifidx(drv, lladdr.sll_ifindex))
9136 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9137}
9138
9139
94627f6c 9140static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 9141 struct i802_bss *bss,
94627f6c
JM
9142 const char *brname, const char *ifname)
9143{
9144 int ifindex;
9145 char in_br[IFNAMSIZ];
9146
e17a2477 9147 os_strlcpy(bss->brname, brname, IFNAMSIZ);
94627f6c
JM
9148 ifindex = if_nametoindex(brname);
9149 if (ifindex == 0) {
9150 /*
9151 * Bridge was configured, but the bridge device does
9152 * not exist. Try to add it now.
9153 */
c81eff1a 9154 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
9155 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9156 "bridge interface %s: %s",
9157 brname, strerror(errno));
9158 return -1;
9159 }
e17a2477 9160 bss->added_bridge = 1;
94627f6c
JM
9161 add_ifidx(drv, if_nametoindex(brname));
9162 }
9163
9164 if (linux_br_get(in_br, ifname) == 0) {
9165 if (os_strcmp(in_br, brname) == 0)
9166 return 0; /* already in the bridge */
9167
9168 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9169 "bridge %s", ifname, in_br);
c81eff1a
BG
9170 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9171 0) {
94627f6c
JM
9172 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9173 "remove interface %s from bridge "
9174 "%s: %s",
9175 ifname, brname, strerror(errno));
9176 return -1;
9177 }
9178 }
9179
9180 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9181 ifname, brname);
c81eff1a 9182 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
9183 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9184 "into bridge %s: %s",
9185 ifname, brname, strerror(errno));
9186 return -1;
9187 }
e17a2477 9188 bss->added_if_into_bridge = 1;
94627f6c
JM
9189
9190 return 0;
9191}
9192
9193
92f475b4
JM
9194static void *i802_init(struct hostapd_data *hapd,
9195 struct wpa_init_params *params)
c5121837
JM
9196{
9197 struct wpa_driver_nl80211_data *drv;
a2e40bb6 9198 struct i802_bss *bss;
c5121837 9199 size_t i;
94627f6c
JM
9200 char brname[IFNAMSIZ];
9201 int ifindex, br_ifindex;
9202 int br_added = 0;
c5121837 9203
0d547d5f
JM
9204 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9205 params->global_priv, 1,
9206 params->bssid);
a2e40bb6 9207 if (bss == NULL)
c5121837 9208 return NULL;
c5121837 9209
a2e40bb6 9210 drv = bss->drv;
7635bfb0 9211
94627f6c
JM
9212 if (linux_br_get(brname, params->ifname) == 0) {
9213 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9214 params->ifname, brname);
9215 br_ifindex = if_nametoindex(brname);
9216 } else {
9217 brname[0] = '\0';
9218 br_ifindex = 0;
9219 }
9220
92f475b4 9221 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
9222 if (params->bridge[i]) {
9223 ifindex = if_nametoindex(params->bridge[i]);
9224 if (ifindex)
9225 add_ifidx(drv, ifindex);
9226 if (ifindex == br_ifindex)
9227 br_added = 1;
9228 }
c5121837 9229 }
94627f6c
JM
9230 if (!br_added && br_ifindex &&
9231 (params->num_bridge == 0 || !params->bridge[0]))
9232 add_ifidx(drv, br_ifindex);
c5121837 9233
ad1e68e6
JM
9234 /* start listening for EAPOL on the default AP interface */
9235 add_ifidx(drv, drv->ifindex);
9236
94627f6c 9237 if (params->num_bridge && params->bridge[0] &&
e17a2477 9238 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
94627f6c
JM
9239 goto failed;
9240
ad1e68e6
JM
9241 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9242 if (drv->eapol_sock < 0) {
7ac3616d
JM
9243 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9244 strerror(errno));
bbaf0837 9245 goto failed;
ad1e68e6
JM
9246 }
9247
9248 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9249 {
7ac3616d 9250 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
c5121837 9251 goto failed;
ad1e68e6
JM
9252 }
9253
c81eff1a
BG
9254 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9255 params->own_addr))
bbaf0837 9256 goto failed;
c5121837 9257
341eebee
JB
9258 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9259
a2e40bb6 9260 return bss;
c5121837
JM
9261
9262failed:
7635bfb0 9263 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
9264 return NULL;
9265}
c5121837 9266
c5121837 9267
bbaf0837
JM
9268static void i802_deinit(void *priv)
9269{
9ebce9c5
JM
9270 struct i802_bss *bss = priv;
9271 wpa_driver_nl80211_deinit(bss);
c5121837
JM
9272}
9273
c5121837 9274
22a7c9d7
JM
9275static enum nl80211_iftype wpa_driver_nl80211_if_type(
9276 enum wpa_driver_if_type type)
9277{
9278 switch (type) {
9279 case WPA_IF_STATION:
9f51b113 9280 return NL80211_IFTYPE_STATION;
75bde05d
JM
9281 case WPA_IF_P2P_CLIENT:
9282 case WPA_IF_P2P_GROUP:
9f51b113 9283 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
9284 case WPA_IF_AP_VLAN:
9285 return NL80211_IFTYPE_AP_VLAN;
9286 case WPA_IF_AP_BSS:
9287 return NL80211_IFTYPE_AP;
9f51b113
JB
9288 case WPA_IF_P2P_GO:
9289 return NL80211_IFTYPE_P2P_GO;
7aad838c
NS
9290 case WPA_IF_P2P_DEVICE:
9291 return NL80211_IFTYPE_P2P_DEVICE;
22a7c9d7
JM
9292 }
9293 return -1;
9294}
9295
9296
482856c8
JM
9297#ifdef CONFIG_P2P
9298
f2ed8023
JM
9299static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9300{
9301 struct wpa_driver_nl80211_data *drv;
9302 dl_list_for_each(drv, &global->interfaces,
9303 struct wpa_driver_nl80211_data, list) {
834ee56f 9304 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
f2ed8023
JM
9305 return 1;
9306 }
9307 return 0;
9308}
9309
9310
9311static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9312 u8 *new_addr)
9313{
9314 unsigned int idx;
9315
9316 if (!drv->global)
9317 return -1;
9318
834ee56f 9319 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
f2ed8023 9320 for (idx = 0; idx < 64; idx++) {
834ee56f 9321 new_addr[0] = drv->first_bss->addr[0] | 0x02;
f2ed8023
JM
9322 new_addr[0] ^= idx << 2;
9323 if (!nl80211_addr_in_use(drv->global, new_addr))
9324 break;
9325 }
9326 if (idx == 64)
9327 return -1;
9328
9329 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9330 MACSTR, MAC2STR(new_addr));
9331
9332 return 0;
9333}
9334
482856c8
JM
9335#endif /* CONFIG_P2P */
9336
f2ed8023 9337
f632e483
AS
9338struct wdev_info {
9339 u64 wdev_id;
9340 int wdev_id_set;
9341 u8 macaddr[ETH_ALEN];
9342};
9343
9344static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
e472e1b4
AS
9345{
9346 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9347 struct nlattr *tb[NL80211_ATTR_MAX + 1];
f632e483 9348 struct wdev_info *wi = arg;
e472e1b4
AS
9349
9350 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9351 genlmsg_attrlen(gnlh, 0), NULL);
9352 if (tb[NL80211_ATTR_WDEV]) {
f632e483
AS
9353 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9354 wi->wdev_id_set = 1;
e472e1b4
AS
9355 }
9356
9357 if (tb[NL80211_ATTR_MAC])
f632e483 9358 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
e472e1b4
AS
9359 ETH_ALEN);
9360
9361 return NL_SKIP;
9362}
9363
9364
7ab68865 9365static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 9366 const char *ifname, const u8 *addr,
f3585c8a 9367 void *bss_ctx, void **drv_priv,
e17a2477 9368 char *force_ifname, u8 *if_addr,
2aec4f3c 9369 const char *bridge, int use_existing)
22a7c9d7 9370{
e472e1b4 9371 enum nl80211_iftype nlmode;
a2e40bb6
FF
9372 struct i802_bss *bss = priv;
9373 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7 9374 int ifidx;
2aec4f3c 9375 int added = 1;
22a7c9d7 9376
f3585c8a
JM
9377 if (addr)
9378 os_memcpy(if_addr, addr, ETH_ALEN);
e472e1b4
AS
9379 nlmode = wpa_driver_nl80211_if_type(type);
9380 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
f632e483
AS
9381 struct wdev_info p2pdev_info;
9382
9383 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
e472e1b4 9384 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
f632e483 9385 0, nl80211_wdev_handler,
2aec4f3c 9386 &p2pdev_info, use_existing);
f632e483 9387 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
e472e1b4
AS
9388 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
9389 ifname);
e472e1b4
AS
9390 return -1;
9391 }
f632e483
AS
9392
9393 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
9394 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
9395 if (!is_zero_ether_addr(p2pdev_info.macaddr))
9396 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
9397 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
9398 ifname,
9399 (long long unsigned int) p2pdev_info.wdev_id);
e472e1b4
AS
9400 } else {
9401 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
2aec4f3c
JM
9402 0, NULL, NULL, use_existing);
9403 if (use_existing && ifidx == -ENFILE) {
9404 added = 0;
9405 ifidx = if_nametoindex(ifname);
9406 } else if (ifidx < 0) {
e472e1b4
AS
9407 return -1;
9408 }
22a7c9d7
JM
9409 }
9410
ab7a1add
AS
9411 if (!addr) {
9412 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
9413 os_memcpy(if_addr, bss->addr, ETH_ALEN);
9414 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
9415 bss->ifname, if_addr) < 0) {
2aec4f3c
JM
9416 if (added)
9417 nl80211_remove_iface(drv, ifidx);
ab7a1add
AS
9418 return -1;
9419 }
c55f774d
JM
9420 }
9421
9422#ifdef CONFIG_P2P
9423 if (!addr &&
9424 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
9425 type == WPA_IF_P2P_GO)) {
9426 /* Enforce unique P2P Interface Address */
ab7a1add 9427 u8 new_addr[ETH_ALEN];
c55f774d 9428
ab7a1add 9429 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
c81eff1a 9430 new_addr) < 0) {
c55f774d
JM
9431 nl80211_remove_iface(drv, ifidx);
9432 return -1;
9433 }
f608081c 9434 if (nl80211_addr_in_use(drv->global, new_addr)) {
c55f774d
JM
9435 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
9436 "for P2P group interface");
f2ed8023 9437 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
c55f774d
JM
9438 nl80211_remove_iface(drv, ifidx);
9439 return -1;
9440 }
c81eff1a 9441 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d
JM
9442 new_addr) < 0) {
9443 nl80211_remove_iface(drv, ifidx);
9444 return -1;
9445 }
c55f774d 9446 }
f67eeb5c 9447 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d
JM
9448 }
9449#endif /* CONFIG_P2P */
f3585c8a 9450
22a7c9d7 9451 if (type == WPA_IF_AP_BSS) {
f5eb9da3
JM
9452 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
9453 if (new_bss == NULL) {
2aec4f3c
JM
9454 if (added)
9455 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
9456 return -1;
9457 }
9458
9459 if (bridge &&
9460 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
9461 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
9462 "interface %s to a bridge %s",
9463 ifname, bridge);
2aec4f3c
JM
9464 if (added)
9465 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
9466 os_free(new_bss);
9467 return -1;
9468 }
9469
c81eff1a
BG
9470 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
9471 {
34f2f814 9472 nl80211_remove_iface(drv, ifidx);
07179987 9473 os_free(new_bss);
22a7c9d7
JM
9474 return -1;
9475 }
a2e40bb6 9476 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 9477 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
9478 new_bss->ifindex = ifidx;
9479 new_bss->drv = drv;
834ee56f
KP
9480 new_bss->next = drv->first_bss->next;
9481 new_bss->freq = drv->first_bss->freq;
a5e1eb20 9482 new_bss->ctx = bss_ctx;
2aec4f3c 9483 new_bss->added_if = added;
834ee56f 9484 drv->first_bss->next = new_bss;
a2e40bb6
FF
9485 if (drv_priv)
9486 *drv_priv = new_bss;
cc7a48d1 9487 nl80211_init_bss(new_bss);
3dd1d890
YAP
9488
9489 /* Subscribe management frames for this WPA_IF_AP_BSS */
9490 if (nl80211_setup_ap(new_bss))
9491 return -1;
22a7c9d7 9492 }
22a7c9d7 9493
ff6a158b
JM
9494 if (drv->global)
9495 drv->global->if_add_ifindex = ifidx;
9496
22a7c9d7
JM
9497 return 0;
9498}
9499
9500
9ebce9c5 9501static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
9502 enum wpa_driver_if_type type,
9503 const char *ifname)
9504{
a2e40bb6 9505 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
9506 int ifindex = if_nametoindex(ifname);
9507
2aec4f3c
JM
9508 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
9509 __func__, type, ifname, ifindex, bss->added_if);
158b090c 9510 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
2b72df63 9511 nl80211_remove_iface(drv, ifindex);
c34e618d 9512
c34e618d
FF
9513 if (type != WPA_IF_AP_BSS)
9514 return 0;
9515
e17a2477 9516 if (bss->added_if_into_bridge) {
c81eff1a
BG
9517 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9518 bss->ifname) < 0)
e17a2477
JM
9519 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9520 "interface %s from bridge %s: %s",
9521 bss->ifname, bss->brname, strerror(errno));
9522 }
9523 if (bss->added_bridge) {
c81eff1a 9524 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
9525 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9526 "bridge %s: %s",
9527 bss->brname, strerror(errno));
9528 }
a2e40bb6 9529
834ee56f 9530 if (bss != drv->first_bss) {
8546ea19 9531 struct i802_bss *tbss;
a2e40bb6 9532
2aec4f3c 9533 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
834ee56f 9534 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
8546ea19
JM
9535 if (tbss->next == bss) {
9536 tbss->next = bss->next;
3dd1d890
YAP
9537 /* Unsubscribe management frames */
9538 nl80211_teardown_ap(bss);
cc7a48d1 9539 nl80211_destroy_bss(bss);
8546ea19
JM
9540 os_free(bss);
9541 bss = NULL;
9542 break;
9543 }
22a7c9d7 9544 }
8546ea19
JM
9545 if (bss)
9546 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9547 "BSS %p in the list", __func__, bss);
390e489c 9548 } else {
2aec4f3c 9549 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
390e489c 9550 nl80211_teardown_ap(bss);
2aec4f3c
JM
9551 if (!bss->added_if && !drv->first_bss->next)
9552 wpa_driver_nl80211_del_beacon(drv);
390e489c 9553 nl80211_destroy_bss(bss);
2aec4f3c
JM
9554 if (!bss->added_if)
9555 i802_set_iface_flags(bss, 0);
390e489c
KP
9556 if (drv->first_bss->next) {
9557 drv->first_bss = drv->first_bss->next;
9558 drv->ctx = drv->first_bss->ctx;
9559 os_free(bss);
9560 } else {
9561 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
9562 }
22a7c9d7 9563 }
22a7c9d7
JM
9564
9565 return 0;
9566}
9567
9568
55777702
JM
9569static int cookie_handler(struct nl_msg *msg, void *arg)
9570{
9571 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9572 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9573 u64 *cookie = arg;
9574 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9575 genlmsg_attrlen(gnlh, 0), NULL);
9576 if (tb[NL80211_ATTR_COOKIE])
9577 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9578 return NL_SKIP;
9579}
9580
9581
88df0ef7 9582static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
9583 unsigned int freq, unsigned int wait,
9584 const u8 *buf, size_t buf_len,
88df0ef7
JB
9585 u64 *cookie_out, int no_cck, int no_ack,
9586 int offchanok)
9884f9cc 9587{
88df0ef7 9588 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
9589 struct nl_msg *msg;
9590 u64 cookie;
9591 int ret = -1;
9592
9593 msg = nlmsg_alloc();
9594 if (!msg)
9595 return -1;
9596
cc2ada86 9597 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
2e3e4566
JM
9598 "no_ack=%d offchanok=%d",
9599 freq, wait, no_cck, no_ack, offchanok);
c91f796f 9600 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
9fb04070 9601 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9884f9cc 9602
f632e483 9603 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80 9604 goto nla_put_failure;
c91f796f
NC
9605 if (freq)
9606 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9db931ed
JM
9607 if (wait)
9608 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
b9fd8ce8 9609 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
88df0ef7 9610 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
b106173a
JM
9611 if (no_cck)
9612 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
ddc53271
JM
9613 if (no_ack)
9614 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
970fa12e 9615
9884f9cc
JB
9616 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9617
9618 cookie = 0;
9619 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9620 msg = NULL;
9621 if (ret) {
9622 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
9623 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9624 freq, wait);
9884f9cc
JB
9625 goto nla_put_failure;
9626 }
cc2ada86 9627 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
ddc53271
JM
9628 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9629 (long long unsigned int) cookie);
9884f9cc
JB
9630
9631 if (cookie_out)
ddc53271 9632 *cookie_out = no_ack ? (u64) -1 : cookie;
9884f9cc
JB
9633
9634nla_put_failure:
9635 nlmsg_free(msg);
9636 return ret;
9637}
9638
9639
9ebce9c5
JM
9640static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9641 unsigned int freq,
190b9062 9642 unsigned int wait_time,
58f6fbe0
JM
9643 const u8 *dst, const u8 *src,
9644 const u8 *bssid,
b106173a
JM
9645 const u8 *data, size_t data_len,
9646 int no_cck)
58f6fbe0 9647{
a2e40bb6 9648 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 9649 int ret = -1;
58f6fbe0
JM
9650 u8 *buf;
9651 struct ieee80211_hdr *hdr;
58f6fbe0 9652
5dfca53f 9653 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
9654 "freq=%u MHz wait=%d ms no_cck=%d)",
9655 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
9656
9657 buf = os_zalloc(24 + data_len);
9658 if (buf == NULL)
9659 return ret;
9660 os_memcpy(buf + 24, data, data_len);
9661 hdr = (struct ieee80211_hdr *) buf;
9662 hdr->frame_control =
9663 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9664 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9665 os_memcpy(hdr->addr2, src, ETH_ALEN);
9666 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9667
f78f2785
JM
9668 if (is_ap_interface(drv->nlmode) &&
9669 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
9670 (int) freq == bss->freq || drv->device_ap_sme ||
9671 !drv->use_monitor))
9ebce9c5
JM
9672 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9673 0, freq, no_cck, 1,
9674 wait_time);
9884f9cc 9675 else
88df0ef7 9676 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 9677 24 + data_len,
b106173a 9678 &drv->send_action_cookie,
88df0ef7 9679 no_cck, 0, 1);
58f6fbe0 9680
f8bf1421 9681 os_free(buf);
58f6fbe0
JM
9682 return ret;
9683}
9684
9685
5dfca53f
JB
9686static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9687{
9688 struct i802_bss *bss = priv;
9689 struct wpa_driver_nl80211_data *drv = bss->drv;
9690 struct nl_msg *msg;
9691 int ret;
9692
9693 msg = nlmsg_alloc();
9694 if (!msg)
9695 return;
9696
316a9e4d
JM
9697 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
9698 (long long unsigned int) drv->send_action_cookie);
9fb04070 9699 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
5dfca53f 9700
7940c790
AS
9701 if (nl80211_set_iface_id(msg, bss) < 0)
9702 goto nla_put_failure;
5dfca53f
JB
9703 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
9704
9705 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9706 msg = NULL;
9707 if (ret)
9708 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9709 "(%s)", ret, strerror(-ret));
9710
9711 nla_put_failure:
9712 nlmsg_free(msg);
9713}
9714
9715
55777702
JM
9716static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9717 unsigned int duration)
9718{
a2e40bb6
FF
9719 struct i802_bss *bss = priv;
9720 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
9721 struct nl_msg *msg;
9722 int ret;
9723 u64 cookie;
9724
9725 msg = nlmsg_alloc();
9726 if (!msg)
9727 return -1;
9728
9fb04070 9729 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
55777702 9730
f632e483 9731 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80
DS
9732 goto nla_put_failure;
9733
55777702
JM
9734 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9735 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
9736
9737 cookie = 0;
9738 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5883168a 9739 msg = NULL;
55777702
JM
9740 if (ret == 0) {
9741 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9742 "0x%llx for freq=%u MHz duration=%u",
9743 (long long unsigned int) cookie, freq, duration);
9744 drv->remain_on_chan_cookie = cookie;
531f0331 9745 drv->pending_remain_on_chan = 1;
55777702
JM
9746 return 0;
9747 }
9748 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
9749 "(freq=%d duration=%u): %d (%s)",
9750 freq, duration, ret, strerror(-ret));
55777702 9751nla_put_failure:
5883168a 9752 nlmsg_free(msg);
55777702
JM
9753 return -1;
9754}
9755
9756
9757static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9758{
a2e40bb6
FF
9759 struct i802_bss *bss = priv;
9760 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
9761 struct nl_msg *msg;
9762 int ret;
9763
9764 if (!drv->pending_remain_on_chan) {
9765 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9766 "to cancel");
9767 return -1;
9768 }
9769
9770 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9771 "0x%llx",
9772 (long long unsigned int) drv->remain_on_chan_cookie);
9773
9774 msg = nlmsg_alloc();
9775 if (!msg)
9776 return -1;
9777
9fb04070 9778 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
55777702 9779
f632e483 9780 if (nl80211_set_iface_id(msg, bss) < 0)
d3aaef80
DS
9781 goto nla_put_failure;
9782
55777702
JM
9783 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
9784
9785 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 9786 msg = NULL;
55777702
JM
9787 if (ret == 0)
9788 return 0;
9789 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9790 "%d (%s)", ret, strerror(-ret));
9791nla_put_failure:
5883168a 9792 nlmsg_free(msg);
55777702
JM
9793 return -1;
9794}
9795
9796
9ebce9c5 9797static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 9798{
a2e40bb6 9799 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 9800
5582a5d1 9801 if (!report) {
0d891981
JM
9802 if (bss->nl_preq && drv->device_ap_sme &&
9803 is_ap_interface(drv->nlmode)) {
9804 /*
9805 * Do not disable Probe Request reporting that was
9806 * enabled in nl80211_setup_ap().
9807 */
9808 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9809 "Probe Request reporting nl_preq=%p while "
9810 "in AP mode", bss->nl_preq);
9811 } else if (bss->nl_preq) {
36488c05
JM
9812 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9813 "reporting nl_preq=%p", bss->nl_preq);
5f65e9f7 9814 nl80211_destroy_eloop_handle(&bss->nl_preq);
5582a5d1
JB
9815 }
9816 return 0;
9817 }
9818
481234cf 9819 if (bss->nl_preq) {
5582a5d1 9820 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 9821 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
9822 return 0;
9823 }
9824
481234cf
JM
9825 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9826 if (bss->nl_preq == NULL)
5582a5d1 9827 return -1;
36488c05
JM
9828 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9829 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 9830
481234cf 9831 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
9832 (WLAN_FC_TYPE_MGMT << 2) |
9833 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
9834 NULL, 0) < 0)
9835 goto out_err;
5582a5d1 9836
5f65e9f7
JB
9837 nl80211_register_eloop_read(&bss->nl_preq,
9838 wpa_driver_nl80211_event_receive,
9839 bss->nl_cb);
5582a5d1 9840
504e905c 9841 return 0;
5582a5d1 9842
a92dfde8 9843 out_err:
221a59c9 9844 nl_destroy_handles(&bss->nl_preq);
5582a5d1 9845 return -1;
504e905c
JM
9846}
9847
9848
4e5cb1a3
JM
9849static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9850 int ifindex, int disabled)
9851{
9852 struct nl_msg *msg;
9853 struct nlattr *bands, *band;
9854 int ret;
9855
9856 msg = nlmsg_alloc();
9857 if (!msg)
9858 return -1;
9859
9fb04070 9860 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
9861 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
9862
9863 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9864 if (!bands)
9865 goto nla_put_failure;
9866
9867 /*
9868 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9869 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9870 * rates. All 5 GHz rates are left enabled.
9871 */
9872 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
9873 if (!band)
9874 goto nla_put_failure;
1dea5882
JM
9875 if (disabled) {
9876 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
9877 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
9878 }
4e5cb1a3
JM
9879 nla_nest_end(msg, band);
9880
9881 nla_nest_end(msg, bands);
9882
9883 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9884 msg = NULL;
9885 if (ret) {
9886 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9887 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
9888 } else
9889 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
9890
9891 return ret;
9892
9893nla_put_failure:
9894 nlmsg_free(msg);
9895 return -1;
9896}
9897
9898
af473088
JM
9899static int wpa_driver_nl80211_deinit_ap(void *priv)
9900{
a2e40bb6
FF
9901 struct i802_bss *bss = priv;
9902 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 9903 if (!is_ap_interface(drv->nlmode))
af473088
JM
9904 return -1;
9905 wpa_driver_nl80211_del_beacon(drv);
60b13c20
IP
9906
9907 /*
9908 * If the P2P GO interface was dynamically added, then it is
9909 * possible that the interface change to station is not possible.
9910 */
9911 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
9912 return 0;
9913
b1f625e0 9914 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
9915}
9916
9917
695c7038
SW
9918static int wpa_driver_nl80211_stop_ap(void *priv)
9919{
9920 struct i802_bss *bss = priv;
9921 struct wpa_driver_nl80211_data *drv = bss->drv;
9922 if (!is_ap_interface(drv->nlmode))
9923 return -1;
9924 wpa_driver_nl80211_del_beacon(drv);
9925 bss->beacon_set = 0;
9926 return 0;
9927}
9928
9929
3c29244e
EP
9930static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9931{
9932 struct i802_bss *bss = priv;
9933 struct wpa_driver_nl80211_data *drv = bss->drv;
9934 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9935 return -1;
60b13c20
IP
9936
9937 /*
9938 * If the P2P Client interface was dynamically added, then it is
9939 * possible that the interface change to station is not possible.
9940 */
9941 if (bss->if_dynamic)
9942 return 0;
9943
3c29244e
EP
9944 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9945}
9946
9947
207ef3fb
JM
9948static void wpa_driver_nl80211_resume(void *priv)
9949{
a2e40bb6 9950 struct i802_bss *bss = priv;
91724d6f
AS
9951
9952 if (i802_set_iface_flags(bss, 1))
9953 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
207ef3fb
JM
9954}
9955
9956
7b90c16a
JM
9957static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
9958 const u8 *ies, size_t ies_len)
9959{
9960 struct i802_bss *bss = priv;
9961 struct wpa_driver_nl80211_data *drv = bss->drv;
9962 int ret;
9963 u8 *data, *pos;
9964 size_t data_len;
341eebee 9965 const u8 *own_addr = bss->addr;
7b90c16a
JM
9966
9967 if (action != 1) {
9968 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9969 "action %d", action);
9970 return -1;
9971 }
9972
9973 /*
9974 * Action frame payload:
9975 * Category[1] = 6 (Fast BSS Transition)
9976 * Action[1] = 1 (Fast BSS Transition Request)
9977 * STA Address
9978 * Target AP Address
9979 * FT IEs
9980 */
9981
73fc617d
JM
9982 data_len = 2 + 2 * ETH_ALEN + ies_len;
9983 data = os_malloc(data_len);
7b90c16a
JM
9984 if (data == NULL)
9985 return -1;
9986 pos = data;
9987 *pos++ = 0x06; /* FT Action category */
9988 *pos++ = action;
9989 os_memcpy(pos, own_addr, ETH_ALEN);
9990 pos += ETH_ALEN;
9991 os_memcpy(pos, target_ap, ETH_ALEN);
9992 pos += ETH_ALEN;
9993 os_memcpy(pos, ies, ies_len);
9994
190b9062
JB
9995 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9996 drv->bssid, own_addr, drv->bssid,
b106173a 9997 data, data_len, 0);
7b90c16a
JM
9998 os_free(data);
9999
10000 return ret;
10001}
10002
10003
b625473c
JM
10004static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10005{
10006 struct i802_bss *bss = priv;
10007 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
10008 struct nl_msg *msg;
10009 struct nlattr *cqm;
f0494d0f 10010 int ret = -1;
b625473c
JM
10011
10012 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10013 "hysteresis=%d", threshold, hysteresis);
10014
10015 msg = nlmsg_alloc();
10016 if (!msg)
10017 return -1;
10018
9fb04070 10019 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
b625473c
JM
10020
10021 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10022
8970bae8 10023 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
b625473c 10024 if (cqm == NULL)
21270bb4 10025 goto nla_put_failure;
b625473c 10026
8970bae8
JB
10027 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10028 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10029 nla_nest_end(msg, cqm);
21270bb4 10030
f0494d0f 10031 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
10032 msg = NULL;
10033
10034nla_put_failure:
b625473c 10035 nlmsg_free(msg);
f0494d0f 10036 return ret;
b625473c
JM
10037}
10038
10039
2cc8d8f4
AO
10040static int get_channel_width(struct nl_msg *msg, void *arg)
10041{
10042 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10043 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10044 struct wpa_signal_info *sig_change = arg;
10045
10046 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10047 genlmsg_attrlen(gnlh, 0), NULL);
10048
10049 sig_change->center_frq1 = -1;
10050 sig_change->center_frq2 = -1;
10051 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10052
10053 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10054 sig_change->chanwidth = convert2width(
10055 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10056 if (tb[NL80211_ATTR_CENTER_FREQ1])
10057 sig_change->center_frq1 =
10058 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10059 if (tb[NL80211_ATTR_CENTER_FREQ2])
10060 sig_change->center_frq2 =
10061 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10062 }
10063
10064 return NL_SKIP;
10065}
10066
10067
10068static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10069 struct wpa_signal_info *sig)
10070{
10071 struct nl_msg *msg;
10072
10073 msg = nlmsg_alloc();
10074 if (!msg)
10075 return -ENOMEM;
10076
10077 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10078 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10079
10080 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10081
10082nla_put_failure:
10083 nlmsg_free(msg);
10084 return -ENOBUFS;
10085}
10086
10087
1c5c7273
PS
10088static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10089{
10090 struct i802_bss *bss = priv;
10091 struct wpa_driver_nl80211_data *drv = bss->drv;
10092 int res;
10093
10094 os_memset(si, 0, sizeof(*si));
10095 res = nl80211_get_link_signal(drv, si);
10096 if (res != 0)
10097 return res;
10098
2cc8d8f4
AO
10099 res = nl80211_get_channel_width(drv, si);
10100 if (res != 0)
10101 return res;
10102
1c5c7273
PS
10103 return nl80211_get_link_noise(drv, si);
10104}
10105
10106
57ebba59
JJ
10107static int wpa_driver_nl80211_shared_freq(void *priv)
10108{
10109 struct i802_bss *bss = priv;
10110 struct wpa_driver_nl80211_data *drv = bss->drv;
10111 struct wpa_driver_nl80211_data *driver;
10112 int freq = 0;
10113
10114 /*
10115 * If the same PHY is in connected state with some other interface,
10116 * then retrieve the assoc freq.
10117 */
10118 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10119 drv->phyname);
10120
10121 dl_list_for_each(driver, &drv->global->interfaces,
10122 struct wpa_driver_nl80211_data, list) {
10123 if (drv == driver ||
10124 os_strcmp(drv->phyname, driver->phyname) != 0 ||
10125 !driver->associated)
10126 continue;
10127
10128 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10129 MACSTR,
834ee56f
KP
10130 driver->phyname, driver->first_bss->ifname,
10131 MAC2STR(driver->first_bss->addr));
d3bd0f05 10132 if (is_ap_interface(driver->nlmode))
834ee56f 10133 freq = driver->first_bss->freq;
d3bd0f05
JJ
10134 else
10135 freq = nl80211_get_assoc_freq(driver);
57ebba59
JJ
10136 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10137 drv->phyname, freq);
10138 }
10139
10140 if (!freq)
10141 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10142 "PHY (%s) in associated state", drv->phyname);
10143
10144 return freq;
10145}
10146
10147
b91ab76e
JM
10148static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10149 int encrypt)
10150{
10151 struct i802_bss *bss = priv;
55231068
JM
10152 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10153 0, 0, 0, 0);
b91ab76e
JM
10154}
10155
10156
c55f774d
JM
10157static int nl80211_set_param(void *priv, const char *param)
10158{
c55f774d
JM
10159 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10160 if (param == NULL)
10161 return 0;
10162
10163#ifdef CONFIG_P2P
10164 if (os_strstr(param, "use_p2p_group_interface=1")) {
482856c8
JM
10165 struct i802_bss *bss = priv;
10166 struct wpa_driver_nl80211_data *drv = bss->drv;
10167
c55f774d
JM
10168 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10169 "interface");
10170 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10171 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10172 }
851b0c55
JM
10173
10174 if (os_strstr(param, "p2p_device=1")) {
10175 struct i802_bss *bss = priv;
10176 struct wpa_driver_nl80211_data *drv = bss->drv;
10177 drv->allow_p2p_device = 1;
10178 }
c55f774d
JM
10179#endif /* CONFIG_P2P */
10180
10181 return 0;
10182}
10183
10184
f2ed8023
JM
10185static void * nl80211_global_init(void)
10186{
10187 struct nl80211_global *global;
36d84860
BG
10188 struct netlink_config *cfg;
10189
f2ed8023
JM
10190 global = os_zalloc(sizeof(*global));
10191 if (global == NULL)
10192 return NULL;
c81eff1a 10193 global->ioctl_sock = -1;
f2ed8023 10194 dl_list_init(&global->interfaces);
ff6a158b 10195 global->if_add_ifindex = -1;
36d84860
BG
10196
10197 cfg = os_zalloc(sizeof(*cfg));
10198 if (cfg == NULL)
10199 goto err;
10200
10201 cfg->ctx = global;
10202 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10203 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10204 global->netlink = netlink_init(cfg);
10205 if (global->netlink == NULL) {
10206 os_free(cfg);
10207 goto err;
10208 }
10209
2a7b66f5
BG
10210 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10211 goto err;
10212
c81eff1a
BG
10213 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10214 if (global->ioctl_sock < 0) {
7ac3616d
JM
10215 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10216 strerror(errno));
c81eff1a
BG
10217 goto err;
10218 }
10219
f2ed8023 10220 return global;
36d84860
BG
10221
10222err:
10223 nl80211_global_deinit(global);
10224 return NULL;
f2ed8023
JM
10225}
10226
10227
10228static void nl80211_global_deinit(void *priv)
10229{
10230 struct nl80211_global *global = priv;
10231 if (global == NULL)
10232 return;
10233 if (!dl_list_empty(&global->interfaces)) {
10234 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10235 "nl80211_global_deinit",
10236 dl_list_len(&global->interfaces));
10237 }
36d84860
BG
10238
10239 if (global->netlink)
10240 netlink_deinit(global->netlink);
10241
276e2d67
BG
10242 nl_destroy_handles(&global->nl);
10243
5f65e9f7
JB
10244 if (global->nl_event)
10245 nl80211_destroy_eloop_handle(&global->nl_event);
d6c9aab8
JB
10246
10247 nl_cb_put(global->nl_cb);
2a7b66f5 10248
c81eff1a
BG
10249 if (global->ioctl_sock >= 0)
10250 close(global->ioctl_sock);
10251
f2ed8023
JM
10252 os_free(global);
10253}
10254
10255
6859f1cb
BG
10256static const char * nl80211_get_radio_name(void *priv)
10257{
10258 struct i802_bss *bss = priv;
10259 struct wpa_driver_nl80211_data *drv = bss->drv;
10260 return drv->phyname;
10261}
10262
10263
a6efc65d
JM
10264static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10265 const u8 *pmkid)
10266{
10267 struct nl_msg *msg;
10268
10269 msg = nlmsg_alloc();
10270 if (!msg)
10271 return -ENOMEM;
10272
9fb04070 10273 nl80211_cmd(bss->drv, msg, 0, cmd);
a6efc65d
JM
10274
10275 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10276 if (pmkid)
10277 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10278 if (bssid)
10279 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10280
10281 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10282 nla_put_failure:
9e088e74 10283 nlmsg_free(msg);
a6efc65d
JM
10284 return -ENOBUFS;
10285}
10286
10287
10288static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10289{
10290 struct i802_bss *bss = priv;
10291 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10292 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10293}
10294
10295
10296static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10297{
10298 struct i802_bss *bss = priv;
10299 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10300 MAC2STR(bssid));
10301 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10302}
10303
10304
10305static int nl80211_flush_pmkid(void *priv)
10306{
10307 struct i802_bss *bss = priv;
10308 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10309 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10310}
10311
10312
0185007c
MK
10313static void clean_survey_results(struct survey_results *survey_results)
10314{
10315 struct freq_survey *survey, *tmp;
10316
10317 if (dl_list_empty(&survey_results->survey_list))
10318 return;
10319
10320 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10321 struct freq_survey, list) {
10322 dl_list_del(&survey->list);
10323 os_free(survey);
10324 }
10325}
10326
10327
10328static void add_survey(struct nlattr **sinfo, u32 ifidx,
10329 struct dl_list *survey_list)
10330{
10331 struct freq_survey *survey;
10332
10333 survey = os_zalloc(sizeof(struct freq_survey));
10334 if (!survey)
10335 return;
10336
10337 survey->ifidx = ifidx;
10338 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10339 survey->filled = 0;
10340
10341 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
10342 survey->nf = (int8_t)
10343 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
10344 survey->filled |= SURVEY_HAS_NF;
10345 }
10346
10347 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
10348 survey->channel_time =
10349 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
10350 survey->filled |= SURVEY_HAS_CHAN_TIME;
10351 }
10352
10353 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
10354 survey->channel_time_busy =
10355 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
10356 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
10357 }
10358
10359 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
10360 survey->channel_time_rx =
10361 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
10362 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
10363 }
10364
10365 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
10366 survey->channel_time_tx =
10367 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
10368 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
10369 }
10370
10371 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)",
10372 survey->freq,
10373 survey->nf,
10374 (unsigned long int) survey->channel_time,
10375 (unsigned long int) survey->channel_time_busy,
10376 (unsigned long int) survey->channel_time_tx,
10377 (unsigned long int) survey->channel_time_rx,
10378 survey->filled);
10379
10380 dl_list_add_tail(survey_list, &survey->list);
10381}
10382
10383
10384static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
10385 unsigned int freq_filter)
10386{
10387 if (!freq_filter)
10388 return 1;
10389
10390 return freq_filter == surveyed_freq;
10391}
10392
10393
10394static int survey_handler(struct nl_msg *msg, void *arg)
10395{
10396 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10397 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10398 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
10399 struct survey_results *survey_results;
10400 u32 surveyed_freq = 0;
10401 u32 ifidx;
10402
10403 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
10404 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
10405 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
10406 };
10407
10408 survey_results = (struct survey_results *) arg;
10409
10410 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10411 genlmsg_attrlen(gnlh, 0), NULL);
10412
10413 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
10414
10415 if (!tb[NL80211_ATTR_SURVEY_INFO])
10416 return NL_SKIP;
10417
10418 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
10419 tb[NL80211_ATTR_SURVEY_INFO],
10420 survey_policy))
10421 return NL_SKIP;
10422
10423 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
10424 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
10425 return NL_SKIP;
10426 }
10427
10428 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10429
10430 if (!check_survey_ok(sinfo, surveyed_freq,
10431 survey_results->freq_filter))
10432 return NL_SKIP;
10433
10434 if (survey_results->freq_filter &&
10435 survey_results->freq_filter != surveyed_freq) {
10436 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
10437 surveyed_freq);
10438 return NL_SKIP;
10439 }
10440
10441 add_survey(sinfo, ifidx, &survey_results->survey_list);
10442
10443 return NL_SKIP;
10444}
10445
10446
10447static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
10448{
10449 struct i802_bss *bss = priv;
10450 struct wpa_driver_nl80211_data *drv = bss->drv;
10451 struct nl_msg *msg;
10452 int err = -ENOBUFS;
10453 union wpa_event_data data;
10454 struct survey_results *survey_results;
10455
10456 os_memset(&data, 0, sizeof(data));
10457 survey_results = &data.survey_results;
10458
10459 dl_list_init(&survey_results->survey_list);
10460
10461 msg = nlmsg_alloc();
10462 if (!msg)
10463 goto nla_put_failure;
10464
10465 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
10466
10467 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10468
10469 if (freq)
10470 data.survey_results.freq_filter = freq;
10471
10472 do {
10473 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
10474 err = send_and_recv_msgs(drv, msg, survey_handler,
10475 survey_results);
10476 } while (err > 0);
10477
10478 if (err) {
10479 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
10480 goto out_clean;
10481 }
10482
10483 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
10484
10485out_clean:
10486 clean_survey_results(survey_results);
10487nla_put_failure:
10488 return err;
10489}
10490
10491
b14a210c
JB
10492static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
10493 const u8 *replay_ctr)
10494{
10495 struct i802_bss *bss = priv;
10496 struct wpa_driver_nl80211_data *drv = bss->drv;
10497 struct nlattr *replay_nested;
10498 struct nl_msg *msg;
10499
10500 msg = nlmsg_alloc();
10501 if (!msg)
10502 return;
10503
9fb04070 10504 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
b14a210c
JB
10505
10506 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10507
10508 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10509 if (!replay_nested)
10510 goto nla_put_failure;
10511
10512 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
10513 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
10514 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
10515 replay_ctr);
10516
10517 nla_nest_end(msg, replay_nested);
10518
10519 send_and_recv_msgs(drv, msg, NULL, NULL);
10520 return;
10521 nla_put_failure:
10522 nlmsg_free(msg);
10523}
10524
10525
39718852
JB
10526static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
10527 const u8 *addr, int qos)
bcf24348 10528{
39718852
JB
10529 /* send data frame to poll STA and check whether
10530 * this frame is ACKed */
bcf24348
JB
10531 struct {
10532 struct ieee80211_hdr hdr;
10533 u16 qos_ctl;
10534 } STRUCT_PACKED nulldata;
10535 size_t size;
10536
10537 /* Send data frame to poll STA and check whether this frame is ACKed */
10538
10539 os_memset(&nulldata, 0, sizeof(nulldata));
10540
10541 if (qos) {
10542 nulldata.hdr.frame_control =
10543 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10544 WLAN_FC_STYPE_QOS_NULL);
10545 size = sizeof(nulldata);
10546 } else {
10547 nulldata.hdr.frame_control =
10548 IEEE80211_FC(WLAN_FC_TYPE_DATA,
10549 WLAN_FC_STYPE_NULLFUNC);
10550 size = sizeof(struct ieee80211_hdr);
10551 }
10552
10553 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
10554 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
10555 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
10556 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
10557
9ebce9c5
JM
10558 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
10559 0, 0) < 0)
bcf24348
JB
10560 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
10561 "send poll frame");
10562}
10563
39718852
JB
10564static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
10565 int qos)
10566{
10567 struct i802_bss *bss = priv;
10568 struct wpa_driver_nl80211_data *drv = bss->drv;
10569 struct nl_msg *msg;
10570
10571 if (!drv->poll_command_supported) {
10572 nl80211_send_null_frame(bss, own_addr, addr, qos);
10573 return;
10574 }
10575
10576 msg = nlmsg_alloc();
10577 if (!msg)
10578 return;
10579
10580 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
10581
10582 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10583 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
10584
10585 send_and_recv_msgs(drv, msg, NULL, NULL);
10586 return;
10587 nla_put_failure:
10588 nlmsg_free(msg);
10589}
10590
bcf24348 10591
29f338af
JM
10592static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
10593{
10594 struct nl_msg *msg;
10595
10596 msg = nlmsg_alloc();
10597 if (!msg)
10598 return -ENOMEM;
10599
10600 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
10601 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10602 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
10603 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
10604 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10605nla_put_failure:
10606 nlmsg_free(msg);
10607 return -ENOBUFS;
10608}
10609
10610
10611static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
10612 int ctwindow)
10613{
10614 struct i802_bss *bss = priv;
10615
10616 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
10617 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
10618
0de38036
JM
10619 if (opp_ps != -1 || ctwindow != -1) {
10620#ifdef ANDROID_P2P
10621 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
10622#else /* ANDROID_P2P */
29f338af 10623 return -1; /* Not yet supported */
0de38036
JM
10624#endif /* ANDROID_P2P */
10625 }
29f338af
JM
10626
10627 if (legacy_ps == -1)
10628 return 0;
10629 if (legacy_ps != 0 && legacy_ps != 1)
10630 return -1; /* Not yet supported */
10631
10632 return nl80211_set_power_save(bss, legacy_ps);
10633}
10634
10635
04e8003c
JD
10636static int nl80211_start_radar_detection(void *priv,
10637 struct hostapd_freq_params *freq)
f90e9c1c
SW
10638{
10639 struct i802_bss *bss = priv;
10640 struct wpa_driver_nl80211_data *drv = bss->drv;
10641 struct nl_msg *msg;
10642 int ret;
10643
04e8003c
JD
10644 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)",
10645 freq->freq, freq->ht_enabled, freq->vht_enabled,
10646 freq->bandwidth, freq->center_freq1, freq->center_freq2);
10647
f90e9c1c
SW
10648 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
10649 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
10650 "detection");
10651 return -1;
10652 }
10653
10654 msg = nlmsg_alloc();
10655 if (!msg)
10656 return -1;
10657
10658 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
10659 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
04e8003c 10660 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
f90e9c1c 10661
04e8003c
JD
10662 if (freq->vht_enabled) {
10663 switch (freq->bandwidth) {
10664 case 20:
10665 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10666 NL80211_CHAN_WIDTH_20);
10667 break;
10668 case 40:
10669 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10670 NL80211_CHAN_WIDTH_40);
10671 break;
10672 case 80:
10673 if (freq->center_freq2)
10674 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10675 NL80211_CHAN_WIDTH_80P80);
10676 else
10677 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10678 NL80211_CHAN_WIDTH_80);
10679 break;
10680 case 160:
10681 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
10682 NL80211_CHAN_WIDTH_160);
10683 break;
10684 default:
10685 return -1;
10686 }
10687 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
10688 if (freq->center_freq2)
10689 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
10690 freq->center_freq2);
10691 } else if (freq->ht_enabled) {
10692 switch (freq->sec_channel_offset) {
10693 case -1:
10694 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10695 NL80211_CHAN_HT40MINUS);
10696 break;
10697 case 1:
10698 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10699 NL80211_CHAN_HT40PLUS);
10700 break;
10701 default:
10702 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10703 NL80211_CHAN_HT20);
10704 break;
10705 }
10706 }
f90e9c1c
SW
10707
10708 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10709 if (ret == 0)
10710 return 0;
10711 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
10712 "%d (%s)", ret, strerror(-ret));
10713nla_put_failure:
10714 return -1;
10715}
10716
03ea1786
AN
10717#ifdef CONFIG_TDLS
10718
10719static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
10720 u8 dialog_token, u16 status_code,
10721 const u8 *buf, size_t len)
10722{
10723 struct i802_bss *bss = priv;
10724 struct wpa_driver_nl80211_data *drv = bss->drv;
10725 struct nl_msg *msg;
10726
10727 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10728 return -EOPNOTSUPP;
10729
10730 if (!dst)
10731 return -EINVAL;
10732
10733 msg = nlmsg_alloc();
10734 if (!msg)
10735 return -ENOMEM;
10736
10737 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
10738 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10739 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
10740 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
10741 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
10742 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
10743 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
10744
10745 return send_and_recv_msgs(drv, msg, NULL, NULL);
10746
10747nla_put_failure:
10748 nlmsg_free(msg);
10749 return -ENOBUFS;
10750}
10751
10752
10753static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
10754{
10755 struct i802_bss *bss = priv;
10756 struct wpa_driver_nl80211_data *drv = bss->drv;
10757 struct nl_msg *msg;
10758 enum nl80211_tdls_operation nl80211_oper;
10759
10760 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10761 return -EOPNOTSUPP;
10762
10763 switch (oper) {
10764 case TDLS_DISCOVERY_REQ:
10765 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10766 break;
10767 case TDLS_SETUP:
10768 nl80211_oper = NL80211_TDLS_SETUP;
10769 break;
10770 case TDLS_TEARDOWN:
10771 nl80211_oper = NL80211_TDLS_TEARDOWN;
10772 break;
10773 case TDLS_ENABLE_LINK:
10774 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10775 break;
10776 case TDLS_DISABLE_LINK:
10777 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10778 break;
10779 case TDLS_ENABLE:
10780 return 0;
10781 case TDLS_DISABLE:
10782 return 0;
10783 default:
10784 return -EINVAL;
10785 }
10786
10787 msg = nlmsg_alloc();
10788 if (!msg)
10789 return -ENOMEM;
10790
10791 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
10792 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
10793 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10794 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
10795
10796 return send_and_recv_msgs(drv, msg, NULL, NULL);
10797
10798nla_put_failure:
10799 nlmsg_free(msg);
10800 return -ENOBUFS;
10801}
10802
10803#endif /* CONFIG TDLS */
10804
10805
216eede8
DS
10806#ifdef ANDROID
10807
10808typedef struct android_wifi_priv_cmd {
10809 char *buf;
10810 int used_len;
10811 int total_len;
10812} android_wifi_priv_cmd;
10813
10814static int drv_errors = 0;
10815
10816static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
10817{
10818 drv_errors++;
10819 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
10820 drv_errors = 0;
10821 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
10822 }
10823}
10824
10825
10826static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
10827{
10828 struct wpa_driver_nl80211_data *drv = bss->drv;
10829 struct ifreq ifr;
10830 android_wifi_priv_cmd priv_cmd;
10831 char buf[MAX_DRV_CMD_SIZE];
10832 int ret;
10833
10834 os_memset(&ifr, 0, sizeof(ifr));
10835 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
10836 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10837
10838 os_memset(buf, 0, sizeof(buf));
10839 os_strlcpy(buf, cmd, sizeof(buf));
10840
10841 priv_cmd.buf = buf;
10842 priv_cmd.used_len = sizeof(buf);
10843 priv_cmd.total_len = sizeof(buf);
10844 ifr.ifr_data = &priv_cmd;
10845
10846 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10847 if (ret < 0) {
10848 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
10849 __func__);
10850 wpa_driver_send_hang_msg(drv);
10851 return ret;
10852 }
10853
10854 drv_errors = 0;
10855 return 0;
10856}
10857
10858
10859static int android_pno_start(struct i802_bss *bss,
10860 struct wpa_driver_scan_params *params)
10861{
10862 struct wpa_driver_nl80211_data *drv = bss->drv;
10863 struct ifreq ifr;
10864 android_wifi_priv_cmd priv_cmd;
10865 int ret = 0, i = 0, bp;
10866 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
10867
10868 bp = WEXT_PNOSETUP_HEADER_SIZE;
10869 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
10870 buf[bp++] = WEXT_PNO_TLV_PREFIX;
10871 buf[bp++] = WEXT_PNO_TLV_VERSION;
10872 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
10873 buf[bp++] = WEXT_PNO_TLV_RESERVED;
10874
10875 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
10876 /* Check that there is enough space needed for 1 more SSID, the
10877 * other sections and null termination */
10878 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
10879 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
10880 break;
10881 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
a97bde0a
JM
10882 params->ssids[i].ssid,
10883 params->ssids[i].ssid_len);
216eede8
DS
10884 buf[bp++] = WEXT_PNO_SSID_SECTION;
10885 buf[bp++] = params->ssids[i].ssid_len;
10886 os_memcpy(&buf[bp], params->ssids[i].ssid,
10887 params->ssids[i].ssid_len);
10888 bp += params->ssids[i].ssid_len;
10889 i++;
10890 }
10891
10892 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
10893 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
10894 WEXT_PNO_SCAN_INTERVAL);
10895 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
10896
10897 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
10898 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
10899 WEXT_PNO_REPEAT);
10900 bp += WEXT_PNO_REPEAT_LENGTH;
10901
10902 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
10903 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
10904 WEXT_PNO_MAX_REPEAT);
10905 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
10906
10907 memset(&ifr, 0, sizeof(ifr));
10908 memset(&priv_cmd, 0, sizeof(priv_cmd));
24f051eb 10909 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
216eede8
DS
10910
10911 priv_cmd.buf = buf;
10912 priv_cmd.used_len = bp;
10913 priv_cmd.total_len = bp;
10914 ifr.ifr_data = &priv_cmd;
10915
10916 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10917
10918 if (ret < 0) {
10919 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
10920 ret);
10921 wpa_driver_send_hang_msg(drv);
10922 return ret;
10923 }
10924
10925 drv_errors = 0;
10926
10927 return android_priv_cmd(bss, "PNOFORCE 1");
10928}
10929
10930
10931static int android_pno_stop(struct i802_bss *bss)
10932{
10933 return android_priv_cmd(bss, "PNOFORCE 0");
10934}
10935
10936#endif /* ANDROID */
10937
10938
9ebce9c5
JM
10939static int driver_nl80211_set_key(const char *ifname, void *priv,
10940 enum wpa_alg alg, const u8 *addr,
10941 int key_idx, int set_tx,
10942 const u8 *seq, size_t seq_len,
10943 const u8 *key, size_t key_len)
10944{
10945 struct i802_bss *bss = priv;
10946 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
10947 set_tx, seq, seq_len, key, key_len);
10948}
10949
10950
10951static int driver_nl80211_scan2(void *priv,
10952 struct wpa_driver_scan_params *params)
10953{
10954 struct i802_bss *bss = priv;
10955 return wpa_driver_nl80211_scan(bss, params);
10956}
10957
10958
10959static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
10960 int reason_code)
10961{
10962 struct i802_bss *bss = priv;
10963 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
10964}
10965
10966
10967static int driver_nl80211_authenticate(void *priv,
10968 struct wpa_driver_auth_params *params)
10969{
10970 struct i802_bss *bss = priv;
10971 return wpa_driver_nl80211_authenticate(bss, params);
10972}
10973
10974
10975static void driver_nl80211_deinit(void *priv)
10976{
10977 struct i802_bss *bss = priv;
10978 wpa_driver_nl80211_deinit(bss);
10979}
10980
10981
10982static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
10983 const char *ifname)
10984{
10985 struct i802_bss *bss = priv;
10986 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10987}
10988
10989
10990static int driver_nl80211_send_mlme(void *priv, const u8 *data,
10991 size_t data_len, int noack)
10992{
10993 struct i802_bss *bss = priv;
10994 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
10995 0, 0, 0, 0);
10996}
10997
10998
10999static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11000{
11001 struct i802_bss *bss = priv;
11002 return wpa_driver_nl80211_sta_remove(bss, addr);
11003}
11004
11005
9ebce9c5
JM
11006static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11007 const char *ifname, int vlan_id)
11008{
11009 struct i802_bss *bss = priv;
11010 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11011}
9ebce9c5
JM
11012
11013
11014static int driver_nl80211_read_sta_data(void *priv,
11015 struct hostap_sta_driver_data *data,
11016 const u8 *addr)
11017{
11018 struct i802_bss *bss = priv;
11019 return i802_read_sta_data(bss, data, addr);
11020}
11021
11022
11023static int driver_nl80211_send_action(void *priv, unsigned int freq,
11024 unsigned int wait_time,
11025 const u8 *dst, const u8 *src,
11026 const u8 *bssid,
11027 const u8 *data, size_t data_len,
11028 int no_cck)
11029{
11030 struct i802_bss *bss = priv;
11031 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11032 bssid, data, data_len, no_cck);
11033}
11034
11035
11036static int driver_nl80211_probe_req_report(void *priv, int report)
11037{
11038 struct i802_bss *bss = priv;
11039 return wpa_driver_nl80211_probe_req_report(bss, report);
11040}
11041
11042
6a1ce395
DG
11043static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11044 const u8 *ies, size_t ies_len)
11045{
11046 int ret;
11047 struct nl_msg *msg;
11048 struct i802_bss *bss = priv;
11049 struct wpa_driver_nl80211_data *drv = bss->drv;
11050 u16 mdid = WPA_GET_LE16(md);
11051
11052 msg = nlmsg_alloc();
11053 if (!msg)
11054 return -ENOMEM;
11055
11056 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11057 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11058 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11059 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11060 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11061
11062 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11063 if (ret) {
11064 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11065 "err=%d (%s)", ret, strerror(-ret));
11066 }
11067
11068 return ret;
11069
11070nla_put_failure:
11071 nlmsg_free(msg);
11072 return -ENOBUFS;
11073}
11074
11075
597b94f5
AS
11076const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11077{
11078 struct i802_bss *bss = priv;
11079 struct wpa_driver_nl80211_data *drv = bss->drv;
11080
11081 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11082 return NULL;
11083
11084 return bss->addr;
11085}
11086
11087
a771c07d
JM
11088static const char * scan_state_str(enum scan_states scan_state)
11089{
11090 switch (scan_state) {
11091 case NO_SCAN:
11092 return "NO_SCAN";
11093 case SCAN_REQUESTED:
11094 return "SCAN_REQUESTED";
11095 case SCAN_STARTED:
11096 return "SCAN_STARTED";
11097 case SCAN_COMPLETED:
11098 return "SCAN_COMPLETED";
11099 case SCAN_ABORTED:
11100 return "SCAN_ABORTED";
11101 case SCHED_SCAN_STARTED:
11102 return "SCHED_SCAN_STARTED";
11103 case SCHED_SCAN_STOPPED:
11104 return "SCHED_SCAN_STOPPED";
11105 case SCHED_SCAN_RESULTS:
11106 return "SCHED_SCAN_RESULTS";
11107 }
11108
11109 return "??";
11110}
11111
11112
11113static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11114{
11115 struct i802_bss *bss = priv;
11116 struct wpa_driver_nl80211_data *drv = bss->drv;
11117 int res;
11118 char *pos, *end;
11119
11120 pos = buf;
11121 end = buf + buflen;
11122
11123 res = os_snprintf(pos, end - pos,
11124 "ifindex=%d\n"
11125 "ifname=%s\n"
11126 "brname=%s\n"
11127 "addr=" MACSTR "\n"
11128 "freq=%d\n"
11129 "%s%s%s%s%s",
11130 bss->ifindex,
11131 bss->ifname,
11132 bss->brname,
11133 MAC2STR(bss->addr),
11134 bss->freq,
11135 bss->beacon_set ? "beacon_set=1\n" : "",
11136 bss->added_if_into_bridge ?
11137 "added_if_into_bridge=1\n" : "",
11138 bss->added_bridge ? "added_bridge=1\n" : "",
11139 bss->in_deinit ? "in_deinit=1\n" : "",
11140 bss->if_dynamic ? "if_dynamic=1\n" : "");
11141 if (res < 0 || res >= end - pos)
11142 return pos - buf;
11143 pos += res;
11144
11145 if (bss->wdev_id_set) {
11146 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11147 (unsigned long long) bss->wdev_id);
11148 if (res < 0 || res >= end - pos)
11149 return pos - buf;
11150 pos += res;
11151 }
11152
11153 res = os_snprintf(pos, end - pos,
11154 "phyname=%s\n"
11155 "drv_ifindex=%d\n"
11156 "operstate=%d\n"
11157 "scan_state=%s\n"
11158 "auth_bssid=" MACSTR "\n"
11159 "auth_attempt_bssid=" MACSTR "\n"
11160 "bssid=" MACSTR "\n"
11161 "prev_bssid=" MACSTR "\n"
11162 "associated=%d\n"
11163 "assoc_freq=%u\n"
11164 "monitor_sock=%d\n"
11165 "monitor_ifidx=%d\n"
11166 "monitor_refcount=%d\n"
11167 "last_mgmt_freq=%u\n"
11168 "eapol_tx_sock=%d\n"
11169 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
11170 drv->phyname,
11171 drv->ifindex,
11172 drv->operstate,
11173 scan_state_str(drv->scan_state),
11174 MAC2STR(drv->auth_bssid),
11175 MAC2STR(drv->auth_attempt_bssid),
11176 MAC2STR(drv->bssid),
11177 MAC2STR(drv->prev_bssid),
11178 drv->associated,
11179 drv->assoc_freq,
11180 drv->monitor_sock,
11181 drv->monitor_ifidx,
11182 drv->monitor_refcount,
11183 drv->last_mgmt_freq,
11184 drv->eapol_tx_sock,
11185 drv->ignore_if_down_event ?
11186 "ignore_if_down_event=1\n" : "",
11187 drv->scan_complete_events ?
11188 "scan_complete_events=1\n" : "",
11189 drv->disabled_11b_rates ?
11190 "disabled_11b_rates=1\n" : "",
11191 drv->pending_remain_on_chan ?
11192 "pending_remain_on_chan=1\n" : "",
11193 drv->in_interface_list ? "in_interface_list=1\n" : "",
11194 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11195 drv->poll_command_supported ?
11196 "poll_command_supported=1\n" : "",
11197 drv->data_tx_status ? "data_tx_status=1\n" : "",
11198 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11199 drv->retry_auth ? "retry_auth=1\n" : "",
11200 drv->use_monitor ? "use_monitor=1\n" : "",
11201 drv->ignore_next_local_disconnect ?
11202 "ignore_next_local_disconnect=1\n" : "",
11203 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11204 if (res < 0 || res >= end - pos)
11205 return pos - buf;
11206 pos += res;
11207
11208 if (drv->has_capability) {
11209 res = os_snprintf(pos, end - pos,
11210 "capa.key_mgmt=0x%x\n"
11211 "capa.enc=0x%x\n"
11212 "capa.auth=0x%x\n"
11213 "capa.flags=0x%x\n"
11214 "capa.max_scan_ssids=%d\n"
11215 "capa.max_sched_scan_ssids=%d\n"
11216 "capa.sched_scan_supported=%d\n"
11217 "capa.max_match_sets=%d\n"
11218 "capa.max_remain_on_chan=%u\n"
11219 "capa.max_stations=%u\n"
11220 "capa.probe_resp_offloads=0x%x\n"
11221 "capa.max_acl_mac_addrs=%u\n"
11222 "capa.num_multichan_concurrent=%u\n",
11223 drv->capa.key_mgmt,
11224 drv->capa.enc,
11225 drv->capa.auth,
11226 drv->capa.flags,
11227 drv->capa.max_scan_ssids,
11228 drv->capa.max_sched_scan_ssids,
11229 drv->capa.sched_scan_supported,
11230 drv->capa.max_match_sets,
11231 drv->capa.max_remain_on_chan,
11232 drv->capa.max_stations,
11233 drv->capa.probe_resp_offloads,
11234 drv->capa.max_acl_mac_addrs,
11235 drv->capa.num_multichan_concurrent);
11236 if (res < 0 || res >= end - pos)
11237 return pos - buf;
11238 pos += res;
11239 }
11240
11241 return pos - buf;
11242}
11243
11244
1c4ffa87
AO
11245static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11246{
11247 if (settings->head)
11248 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11249 settings->head_len, settings->head);
11250
11251 if (settings->tail)
11252 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11253 settings->tail_len, settings->tail);
11254
11255 if (settings->beacon_ies)
11256 NLA_PUT(msg, NL80211_ATTR_IE,
11257 settings->beacon_ies_len, settings->beacon_ies);
11258
11259 if (settings->proberesp_ies)
11260 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11261 settings->proberesp_ies_len, settings->proberesp_ies);
11262
11263 if (settings->assocresp_ies)
11264 NLA_PUT(msg,
11265 NL80211_ATTR_IE_ASSOC_RESP,
11266 settings->assocresp_ies_len, settings->assocresp_ies);
11267
11268 if (settings->probe_resp)
11269 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11270 settings->probe_resp_len, settings->probe_resp);
11271
11272 return 0;
11273
11274nla_put_failure:
11275 return -ENOBUFS;
11276}
11277
11278
11279static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11280{
11281 struct nl_msg *msg;
11282 struct i802_bss *bss = priv;
11283 struct wpa_driver_nl80211_data *drv = bss->drv;
11284 struct nlattr *beacon_csa;
11285 int ret = -ENOBUFS;
11286
8d1fdde7 11287 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
1c4ffa87 11288 settings->cs_count, settings->block_tx,
8d1fdde7
JD
11289 settings->freq_params.freq, settings->freq_params.bandwidth,
11290 settings->freq_params.center_freq1,
11291 settings->freq_params.center_freq2);
1c4ffa87
AO
11292
11293 if (!drv->channel_switch_supported) {
11294 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11295 return -EOPNOTSUPP;
11296 }
11297
11298 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11299 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11300 return -EOPNOTSUPP;
11301
11302 /* check settings validity */
11303 if (!settings->beacon_csa.tail ||
11304 ((settings->beacon_csa.tail_len <=
11305 settings->counter_offset_beacon) ||
11306 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11307 settings->cs_count)))
11308 return -EINVAL;
11309
11310 if (settings->beacon_csa.probe_resp &&
11311 ((settings->beacon_csa.probe_resp_len <=
11312 settings->counter_offset_presp) ||
11313 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11314 settings->cs_count)))
11315 return -EINVAL;
11316
11317 msg = nlmsg_alloc();
11318 if (!msg)
11319 return -ENOMEM;
11320
11321 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11322 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11323 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11324 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11325 if (ret)
11326 goto error;
11327
11328 if (settings->block_tx)
11329 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
11330
11331 /* beacon_after params */
11332 ret = set_beacon_data(msg, &settings->beacon_after);
11333 if (ret)
11334 goto error;
11335
11336 /* beacon_csa params */
11337 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
11338 if (!beacon_csa)
11339 goto nla_put_failure;
11340
11341 ret = set_beacon_data(msg, &settings->beacon_csa);
11342 if (ret)
11343 goto error;
11344
11345 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
11346 settings->counter_offset_beacon);
11347
11348 if (settings->beacon_csa.probe_resp)
11349 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
11350 settings->counter_offset_presp);
11351
11352 nla_nest_end(msg, beacon_csa);
11353 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11354 if (ret) {
11355 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
11356 ret, strerror(-ret));
11357 }
11358 return ret;
11359
11360nla_put_failure:
11361 ret = -ENOBUFS;
11362error:
11363 nlmsg_free(msg);
11364 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
11365 return ret;
11366}
11367
11368
049105b4
KP
11369static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
11370 u8 qos_map_set_len)
11371{
11372 struct i802_bss *bss = priv;
11373 struct wpa_driver_nl80211_data *drv = bss->drv;
11374 struct nl_msg *msg;
11375 int ret;
11376
11377 msg = nlmsg_alloc();
11378 if (!msg)
11379 return -ENOMEM;
11380
11381 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
11382 qos_map_set, qos_map_set_len);
11383
11384 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
11385 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11386 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
11387
11388 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11389 if (ret)
11390 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
11391
11392 return ret;
11393
11394nla_put_failure:
11395 nlmsg_free(msg);
11396 return -ENOBUFS;
11397}
11398
11399
3f5285e8
JM
11400const struct wpa_driver_ops wpa_driver_nl80211_ops = {
11401 .name = "nl80211",
11402 .desc = "Linux nl80211/cfg80211",
11403 .get_bssid = wpa_driver_nl80211_get_bssid,
11404 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
11405 .set_key = driver_nl80211_set_key,
11406 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
11407 .sched_scan = wpa_driver_nl80211_sched_scan,
11408 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 11409 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9ebce9c5
JM
11410 .deauthenticate = driver_nl80211_deauthenticate,
11411 .authenticate = driver_nl80211_authenticate,
3f5285e8 11412 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
11413 .global_init = nl80211_global_init,
11414 .global_deinit = nl80211_global_deinit,
11415 .init2 = wpa_driver_nl80211_init,
9ebce9c5 11416 .deinit = driver_nl80211_deinit,
3f5285e8
JM
11417 .get_capa = wpa_driver_nl80211_get_capa,
11418 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 11419 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 11420 .set_country = wpa_driver_nl80211_set_country,
f0793bf1 11421 .get_country = wpa_driver_nl80211_get_country,
19c3b566 11422 .set_ap = wpa_driver_nl80211_set_ap,
3c4ca363 11423 .set_acl = wpa_driver_nl80211_set_acl,
22a7c9d7 11424 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
11425 .if_remove = driver_nl80211_if_remove,
11426 .send_mlme = driver_nl80211_send_mlme,
c3965310 11427 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
0f4e8b4f 11428 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 11429 .sta_remove = driver_nl80211_sta_remove,
db149ac9 11430 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 11431 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837 11432 .hapd_init = i802_init,
c5121837 11433 .hapd_deinit = i802_deinit,
f7b3920c 11434 .set_wds_sta = i802_set_wds_sta,
c5121837
JM
11435 .get_seqnum = i802_get_seqnum,
11436 .flush = i802_flush,
c5121837
JM
11437 .get_inact_sec = i802_get_inact_sec,
11438 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
11439 .set_rts = i802_set_rts,
11440 .set_frag = i802_set_frag,
c5121837 11441 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 11442 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
11443 .sta_deauth = i802_sta_deauth,
11444 .sta_disassoc = i802_sta_disassoc,
9ebce9c5 11445 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 11446 .set_freq = i802_set_freq,
9ebce9c5 11447 .send_action = driver_nl80211_send_action,
5dfca53f 11448 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
11449 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
11450 .cancel_remain_on_channel =
11451 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 11452 .probe_req_report = driver_nl80211_probe_req_report,
af473088 11453 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 11454 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 11455 .resume = wpa_driver_nl80211_resume,
7b90c16a 11456 .send_ft_action = nl80211_send_ft_action,
b625473c 11457 .signal_monitor = nl80211_signal_monitor,
1c5c7273 11458 .signal_poll = nl80211_signal_poll,
b91ab76e 11459 .send_frame = nl80211_send_frame,
57ebba59 11460 .shared_freq = wpa_driver_nl80211_shared_freq,
c55f774d 11461 .set_param = nl80211_set_param,
6859f1cb 11462 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
11463 .add_pmkid = nl80211_add_pmkid,
11464 .remove_pmkid = nl80211_remove_pmkid,
11465 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 11466 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 11467 .poll_client = nl80211_poll_client,
29f338af 11468 .set_p2p_powersave = nl80211_set_p2p_powersave,
f90e9c1c 11469 .start_dfs_cac = nl80211_start_radar_detection,
695c7038 11470 .stop_ap = wpa_driver_nl80211_stop_ap,
03ea1786
AN
11471#ifdef CONFIG_TDLS
11472 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
11473 .tdls_oper = nl80211_tdls_oper,
11474#endif /* CONFIG_TDLS */
6a1ce395 11475 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
597b94f5 11476 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
0185007c 11477 .get_survey = wpa_driver_nl80211_get_survey,
a771c07d 11478 .status = wpa_driver_nl80211_status,
1c4ffa87 11479 .switch_channel = nl80211_switch_channel,
0de38036
JM
11480#ifdef ANDROID_P2P
11481 .set_noa = wpa_driver_set_p2p_noa,
11482 .get_noa = wpa_driver_get_p2p_noa,
11483 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
11484#endif /* ANDROID_P2P */
5e2c3490
JM
11485#ifdef ANDROID
11486 .driver_cmd = wpa_driver_nl80211_driver_cmd,
11487#endif /* ANDROID */
049105b4 11488 .set_qos_map = nl80211_set_qos_map,
3f5285e8 11489};