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