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