blob: 9cbe6f35b4d2f6d53835dbbbca5530a192ef6f02 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <net/if.h>
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
22#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
24#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "nl80211_copy.h"
27
28#include "common.h"
29#include "eloop.h"
30#include "utils/list.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080031#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070032#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080034#include "common/ieee802_11_common.h"
35#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "netlink.h"
37#include "linux_ioctl.h"
38#include "radiotap.h"
39#include "radiotap_iter.h"
40#include "rfkill.h"
41#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080042
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043#ifndef SO_WIFI_STATUS
44# if defined(__sparc__)
45# define SO_WIFI_STATUS 0x0025
46# elif defined(__parisc__)
47# define SO_WIFI_STATUS 0x4022
48# else
49# define SO_WIFI_STATUS 41
50# endif
51
52# define SCM_WIFI_STATUS SO_WIFI_STATUS
53#endif
54
55#ifndef SO_EE_ORIGIN_TXSTATUS
56#define SO_EE_ORIGIN_TXSTATUS 4
57#endif
58
59#ifndef PACKET_TX_TIMESTAMP
60#define PACKET_TX_TIMESTAMP 16
61#endif
62
63#ifdef ANDROID
64#include "android_drv.h"
65#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066#ifdef CONFIG_LIBNL20
67/* libnl 2.0 compatibility code */
68#define nl_handle nl_sock
69#define nl80211_handle_alloc nl_socket_alloc_cb
70#define nl80211_handle_destroy nl_socket_free
71#else
72/*
73 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
74 * but when you free a socket again it will mess up its bitmap and
75 * and use the wrong number the next time it needs a socket ID.
76 * Therefore, we wrap the handle alloc/destroy and add our own pid
77 * accounting.
78 */
79static uint32_t port_bitmap[32] = { 0 };
80
81static struct nl_handle *nl80211_handle_alloc(void *cb)
82{
83 struct nl_handle *handle;
84 uint32_t pid = getpid() & 0x3FFFFF;
85 int i;
86
87 handle = nl_handle_alloc_cb(cb);
88
89 for (i = 0; i < 1024; i++) {
90 if (port_bitmap[i / 32] & (1 << (i % 32)))
91 continue;
92 port_bitmap[i / 32] |= 1 << (i % 32);
93 pid += i << 22;
94 break;
95 }
96
97 nl_socket_set_local_port(handle, pid);
98
99 return handle;
100}
101
102static void nl80211_handle_destroy(struct nl_handle *handle)
103{
104 uint32_t port = nl_socket_get_local_port(handle);
105
106 port >>= 22;
107 port_bitmap[port / 32] &= ~(1 << (port % 32));
108
109 nl_handle_destroy(handle);
110}
111#endif /* CONFIG_LIBNL20 */
112
113
Dmitry Shmidt54605472013-11-08 11:10:19 -0800114#ifdef ANDROID
115/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
116static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
117{
118 return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
119}
120#undef nl_socket_set_nonblocking
121#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
122#endif /* ANDROID */
123
124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800125static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
126{
127 struct nl_handle *handle;
128
129 handle = nl80211_handle_alloc(cb);
130 if (handle == NULL) {
131 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
132 "callbacks (%s)", dbg);
133 return NULL;
134 }
135
136 if (genl_connect(handle)) {
137 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
138 "netlink (%s)", dbg);
139 nl80211_handle_destroy(handle);
140 return NULL;
141 }
142
143 return handle;
144}
145
146
147static void nl_destroy_handles(struct nl_handle **handle)
148{
149 if (*handle == NULL)
150 return;
151 nl80211_handle_destroy(*handle);
152 *handle = NULL;
153}
154
155
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700156#if __WORDSIZE == 64
157#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
158#else
159#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
160#endif
161
162static void nl80211_register_eloop_read(struct nl_handle **handle,
163 eloop_sock_handler handler,
164 void *eloop_data)
165{
166 nl_socket_set_nonblocking(*handle);
167 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
168 eloop_data, *handle);
169 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
170}
171
172
173static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
174{
175 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
176 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
177 nl_destroy_handles(handle);
178}
179
180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181#ifndef IFF_LOWER_UP
182#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
183#endif
184#ifndef IFF_DORMANT
185#define IFF_DORMANT 0x20000 /* driver signals dormant */
186#endif
187
188#ifndef IF_OPER_DORMANT
189#define IF_OPER_DORMANT 5
190#endif
191#ifndef IF_OPER_UP
192#define IF_OPER_UP 6
193#endif
194
195struct nl80211_global {
196 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197 int if_add_ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700198 u64 if_add_wdevid;
199 int if_add_wdevid_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200 struct netlink_data *netlink;
201 struct nl_cb *nl_cb;
202 struct nl_handle *nl;
203 int nl80211_id;
204 int ioctl_sock; /* socket for ioctl() use */
205
206 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207};
208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209struct nl80211_wiphy_data {
210 struct dl_list list;
211 struct dl_list bsss;
212 struct dl_list drvs;
213
214 struct nl_handle *nl_beacons;
215 struct nl_cb *nl_cb;
216
217 int wiphy_idx;
218};
219
220static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222struct i802_bss {
223 struct wpa_driver_nl80211_data *drv;
224 struct i802_bss *next;
225 int ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700226 u64 wdev_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 char ifname[IFNAMSIZ + 1];
228 char brname[IFNAMSIZ];
229 unsigned int beacon_set:1;
230 unsigned int added_if_into_bridge:1;
231 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232 unsigned int in_deinit:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700233 unsigned int wdev_id_set:1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800234 unsigned int added_if:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800235
236 u8 addr[ETH_ALEN];
237
238 int freq;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700239 int bandwidth;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700240 int if_dynamic;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800242 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800243 struct nl_handle *nl_preq, *nl_mgmt;
244 struct nl_cb *nl_cb;
245
246 struct nl80211_wiphy_data *wiphy_data;
247 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248};
249
250struct wpa_driver_nl80211_data {
251 struct nl80211_global *global;
252 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 char phyname[32];
255 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 int ifindex;
257 int if_removed;
258 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800259 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 struct rfkill_data *rfkill;
261 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700262 u8 *extended_capa, *extended_capa_mask;
263 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 int has_capability;
265
266 int operstate;
267
268 int scan_complete_events;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700269 enum scan_states {
270 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
271 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
272 SCHED_SCAN_RESULTS
273 } scan_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276
277 u8 auth_bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700278 u8 auth_attempt_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 u8 bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700280 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 int associated;
282 u8 ssid[32];
283 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284 enum nl80211_iftype nlmode;
285 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 unsigned int assoc_freq;
287
288 int monitor_sock;
289 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800292 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800294 unsigned int in_interface_list:1;
295 unsigned int device_ap_sme:1;
296 unsigned int poll_command_supported:1;
297 unsigned int data_tx_status:1;
298 unsigned int scan_for_auth:1;
299 unsigned int retry_auth:1;
300 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800301 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700302 unsigned int ignore_next_local_deauth:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700303 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800304 unsigned int hostapd:1;
305 unsigned int start_mode_ap:1;
306 unsigned int start_iface_up:1;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800307 unsigned int test_use_roc_tx:1;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700308 unsigned int ignore_deauth_event:1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700309 unsigned int dfs_vendor_cmd_avail:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310
311 u64 remain_on_chan_cookie;
312 u64 send_action_cookie;
313
314 unsigned int last_mgmt_freq;
315
316 struct wpa_driver_scan_filter *filter_ssids;
317 size_t num_filter_ssids;
318
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800319 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321 int eapol_tx_sock;
322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 int eapol_sock; /* socket for EAPOL frames */
324
325 int default_if_indices[16];
326 int *if_indices;
327 int num_if_indices;
328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800329 /* From failed authentication command */
330 int auth_freq;
331 u8 auth_bssid_[ETH_ALEN];
332 u8 auth_ssid[32];
333 size_t auth_ssid_len;
334 int auth_alg;
335 u8 *auth_ie;
336 size_t auth_ie_len;
337 u8 auth_wep_key[4][16];
338 size_t auth_wep_key_len[4];
339 int auth_wep_tx_keyidx;
340 int auth_local_state_change;
341 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342};
343
344
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800345static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
347 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
349 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800351wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
352 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
354 const u8 *addr, int cmd, u16 reason_code,
355 int local_state_change);
356static void nl80211_remove_monitor_interface(
357 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800358static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 const u8 *buf, size_t buf_len, u64 *cookie,
361 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700362static int nl80211_register_frame(struct i802_bss *bss,
363 struct nl_handle *hl_handle,
364 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800365static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
366 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367#ifdef ANDROID
368static int android_pno_start(struct i802_bss *bss,
369 struct wpa_driver_scan_params *params);
370static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800371extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
372 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373#endif /* ANDROID */
374#ifdef ANDROID_P2P
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700375#ifdef ANDROID_P2P_STUB
376int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration) {
377 return 0;
378}
379int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len) {
380 return 0;
381}
382int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow) {
383 return -1;
384}
385int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
386 const struct wpabuf *proberesp,
387 const struct wpabuf *assocresp) {
388 return 0;
389}
390#else /* ANDROID_P2P_STUB */
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700391int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700393int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
394int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800395 const struct wpabuf *proberesp,
396 const struct wpabuf *assocresp);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700397#endif /* ANDROID_P2P_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800398#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
401static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
402static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800403static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 enum wpa_driver_if_type type,
405 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700406
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700407static int nl80211_set_channel(struct i802_bss *bss,
408 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
410 int ifindex, int disabled);
411
412static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800413static int wpa_driver_nl80211_authenticate_retry(
414 struct wpa_driver_nl80211_data *drv);
415
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700416static int i802_set_iface_flags(struct i802_bss *bss, int up);
417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700419static const char * nl80211_command_to_string(enum nl80211_commands cmd)
420{
421#define C2S(x) case x: return #x;
422 switch (cmd) {
423 C2S(NL80211_CMD_UNSPEC)
424 C2S(NL80211_CMD_GET_WIPHY)
425 C2S(NL80211_CMD_SET_WIPHY)
426 C2S(NL80211_CMD_NEW_WIPHY)
427 C2S(NL80211_CMD_DEL_WIPHY)
428 C2S(NL80211_CMD_GET_INTERFACE)
429 C2S(NL80211_CMD_SET_INTERFACE)
430 C2S(NL80211_CMD_NEW_INTERFACE)
431 C2S(NL80211_CMD_DEL_INTERFACE)
432 C2S(NL80211_CMD_GET_KEY)
433 C2S(NL80211_CMD_SET_KEY)
434 C2S(NL80211_CMD_NEW_KEY)
435 C2S(NL80211_CMD_DEL_KEY)
436 C2S(NL80211_CMD_GET_BEACON)
437 C2S(NL80211_CMD_SET_BEACON)
438 C2S(NL80211_CMD_START_AP)
439 C2S(NL80211_CMD_STOP_AP)
440 C2S(NL80211_CMD_GET_STATION)
441 C2S(NL80211_CMD_SET_STATION)
442 C2S(NL80211_CMD_NEW_STATION)
443 C2S(NL80211_CMD_DEL_STATION)
444 C2S(NL80211_CMD_GET_MPATH)
445 C2S(NL80211_CMD_SET_MPATH)
446 C2S(NL80211_CMD_NEW_MPATH)
447 C2S(NL80211_CMD_DEL_MPATH)
448 C2S(NL80211_CMD_SET_BSS)
449 C2S(NL80211_CMD_SET_REG)
450 C2S(NL80211_CMD_REQ_SET_REG)
451 C2S(NL80211_CMD_GET_MESH_CONFIG)
452 C2S(NL80211_CMD_SET_MESH_CONFIG)
453 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
454 C2S(NL80211_CMD_GET_REG)
455 C2S(NL80211_CMD_GET_SCAN)
456 C2S(NL80211_CMD_TRIGGER_SCAN)
457 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
458 C2S(NL80211_CMD_SCAN_ABORTED)
459 C2S(NL80211_CMD_REG_CHANGE)
460 C2S(NL80211_CMD_AUTHENTICATE)
461 C2S(NL80211_CMD_ASSOCIATE)
462 C2S(NL80211_CMD_DEAUTHENTICATE)
463 C2S(NL80211_CMD_DISASSOCIATE)
464 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
465 C2S(NL80211_CMD_REG_BEACON_HINT)
466 C2S(NL80211_CMD_JOIN_IBSS)
467 C2S(NL80211_CMD_LEAVE_IBSS)
468 C2S(NL80211_CMD_TESTMODE)
469 C2S(NL80211_CMD_CONNECT)
470 C2S(NL80211_CMD_ROAM)
471 C2S(NL80211_CMD_DISCONNECT)
472 C2S(NL80211_CMD_SET_WIPHY_NETNS)
473 C2S(NL80211_CMD_GET_SURVEY)
474 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
475 C2S(NL80211_CMD_SET_PMKSA)
476 C2S(NL80211_CMD_DEL_PMKSA)
477 C2S(NL80211_CMD_FLUSH_PMKSA)
478 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
479 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
480 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
481 C2S(NL80211_CMD_REGISTER_FRAME)
482 C2S(NL80211_CMD_FRAME)
483 C2S(NL80211_CMD_FRAME_TX_STATUS)
484 C2S(NL80211_CMD_SET_POWER_SAVE)
485 C2S(NL80211_CMD_GET_POWER_SAVE)
486 C2S(NL80211_CMD_SET_CQM)
487 C2S(NL80211_CMD_NOTIFY_CQM)
488 C2S(NL80211_CMD_SET_CHANNEL)
489 C2S(NL80211_CMD_SET_WDS_PEER)
490 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
491 C2S(NL80211_CMD_JOIN_MESH)
492 C2S(NL80211_CMD_LEAVE_MESH)
493 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
494 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
495 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
496 C2S(NL80211_CMD_GET_WOWLAN)
497 C2S(NL80211_CMD_SET_WOWLAN)
498 C2S(NL80211_CMD_START_SCHED_SCAN)
499 C2S(NL80211_CMD_STOP_SCHED_SCAN)
500 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
501 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
502 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
503 C2S(NL80211_CMD_PMKSA_CANDIDATE)
504 C2S(NL80211_CMD_TDLS_OPER)
505 C2S(NL80211_CMD_TDLS_MGMT)
506 C2S(NL80211_CMD_UNEXPECTED_FRAME)
507 C2S(NL80211_CMD_PROBE_CLIENT)
508 C2S(NL80211_CMD_REGISTER_BEACONS)
509 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
510 C2S(NL80211_CMD_SET_NOACK_MAP)
511 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
512 C2S(NL80211_CMD_START_P2P_DEVICE)
513 C2S(NL80211_CMD_STOP_P2P_DEVICE)
514 C2S(NL80211_CMD_CONN_FAILED)
515 C2S(NL80211_CMD_SET_MCAST_RATE)
516 C2S(NL80211_CMD_SET_MAC_ACL)
517 C2S(NL80211_CMD_RADAR_DETECT)
518 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
519 C2S(NL80211_CMD_UPDATE_FT_IES)
520 C2S(NL80211_CMD_FT_EVENT)
521 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
522 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800523 C2S(NL80211_CMD_GET_COALESCE)
524 C2S(NL80211_CMD_SET_COALESCE)
525 C2S(NL80211_CMD_CHANNEL_SWITCH)
526 C2S(NL80211_CMD_VENDOR)
527 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700528 default:
529 return "NL80211_CMD_UNKNOWN";
530 }
531#undef C2S
532}
533
534
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800535/* Converts nl80211_chan_width to a common format */
536static enum chan_width convert2width(int width)
537{
538 switch (width) {
539 case NL80211_CHAN_WIDTH_20_NOHT:
540 return CHAN_WIDTH_20_NOHT;
541 case NL80211_CHAN_WIDTH_20:
542 return CHAN_WIDTH_20;
543 case NL80211_CHAN_WIDTH_40:
544 return CHAN_WIDTH_40;
545 case NL80211_CHAN_WIDTH_80:
546 return CHAN_WIDTH_80;
547 case NL80211_CHAN_WIDTH_80P80:
548 return CHAN_WIDTH_80P80;
549 case NL80211_CHAN_WIDTH_160:
550 return CHAN_WIDTH_160;
551 }
552 return CHAN_WIDTH_UNKNOWN;
553}
554
555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800556static int is_ap_interface(enum nl80211_iftype nlmode)
557{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700558 return nlmode == NL80211_IFTYPE_AP ||
559 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800560}
561
562
563static int is_sta_interface(enum nl80211_iftype nlmode)
564{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700565 return nlmode == NL80211_IFTYPE_STATION ||
566 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800567}
568
569
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700570static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700572 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
573 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575
576
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700577static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
578{
579 if (drv->associated)
580 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
581 drv->associated = 0;
582 os_memset(drv->bssid, 0, ETH_ALEN);
583}
584
585
Jouni Malinen87fd2792011-05-16 18:35:42 +0300586struct nl80211_bss_info_arg {
587 struct wpa_driver_nl80211_data *drv;
588 struct wpa_scan_results *res;
589 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300591};
592
593static int bss_info_handler(struct nl_msg *msg, void *arg);
594
595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596/* nl80211 code */
597static int ack_handler(struct nl_msg *msg, void *arg)
598{
599 int *err = arg;
600 *err = 0;
601 return NL_STOP;
602}
603
604static int finish_handler(struct nl_msg *msg, void *arg)
605{
606 int *ret = arg;
607 *ret = 0;
608 return NL_SKIP;
609}
610
611static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
612 void *arg)
613{
614 int *ret = arg;
615 *ret = err->error;
616 return NL_SKIP;
617}
618
619
620static int no_seq_check(struct nl_msg *msg, void *arg)
621{
622 return NL_OK;
623}
624
625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 struct nl_handle *nl_handle, struct nl_msg *msg,
628 int (*valid_handler)(struct nl_msg *, void *),
629 void *valid_data)
630{
631 struct nl_cb *cb;
632 int err = -ENOMEM;
633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800634 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 if (!cb)
636 goto out;
637
638 err = nl_send_auto_complete(nl_handle, msg);
639 if (err < 0)
640 goto out;
641
642 err = 1;
643
644 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
645 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
646 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
647
648 if (valid_handler)
649 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
650 valid_handler, valid_data);
651
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700652 while (err > 0) {
653 int res = nl_recvmsgs(nl_handle, cb);
654 if (res) {
655 wpa_printf(MSG_INFO,
656 "nl80211: %s->nl_recvmsgs failed: %d",
657 __func__, res);
658 }
659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660 out:
661 nl_cb_put(cb);
662 nlmsg_free(msg);
663 return err;
664}
665
666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667static int send_and_recv_msgs_global(struct nl80211_global *global,
668 struct nl_msg *msg,
669 int (*valid_handler)(struct nl_msg *, void *),
670 void *valid_data)
671{
672 return send_and_recv(global, global->nl, msg, valid_handler,
673 valid_data);
674}
675
Dmitry Shmidt04949592012-07-19 12:16:46 -0700676
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800677static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 struct nl_msg *msg,
679 int (*valid_handler)(struct nl_msg *, void *),
680 void *valid_data)
681{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682 return send_and_recv(drv->global, drv->global->nl, msg,
683 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684}
685
686
687struct family_data {
688 const char *group;
689 int id;
690};
691
692
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700693static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
694{
695 if (bss->wdev_id_set)
696 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
697 else
698 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
699 return 0;
700
701nla_put_failure:
702 return -1;
703}
704
705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706static int family_handler(struct nl_msg *msg, void *arg)
707{
708 struct family_data *res = arg;
709 struct nlattr *tb[CTRL_ATTR_MAX + 1];
710 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
711 struct nlattr *mcgrp;
712 int i;
713
714 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
715 genlmsg_attrlen(gnlh, 0), NULL);
716 if (!tb[CTRL_ATTR_MCAST_GROUPS])
717 return NL_SKIP;
718
719 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
720 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
721 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
722 nla_len(mcgrp), NULL);
723 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
724 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
725 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
726 res->group,
727 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
728 continue;
729 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
730 break;
731 };
732
733 return NL_SKIP;
734}
735
736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 const char *family, const char *group)
739{
740 struct nl_msg *msg;
741 int ret = -1;
742 struct family_data res = { group, -ENOENT };
743
744 msg = nlmsg_alloc();
745 if (!msg)
746 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 0, 0, CTRL_CMD_GETFAMILY, 0);
749 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 msg = NULL;
753 if (ret == 0)
754 ret = res.id;
755
756nla_put_failure:
757 nlmsg_free(msg);
758 return ret;
759}
760
761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
763 struct nl_msg *msg, int flags, uint8_t cmd)
764{
765 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
766 0, flags, cmd, 0);
767}
768
769
770struct wiphy_idx_data {
771 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700772 enum nl80211_iftype nlmode;
773 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774};
775
776
777static int netdev_info_handler(struct nl_msg *msg, void *arg)
778{
779 struct nlattr *tb[NL80211_ATTR_MAX + 1];
780 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
781 struct wiphy_idx_data *info = arg;
782
783 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
784 genlmsg_attrlen(gnlh, 0), NULL);
785
786 if (tb[NL80211_ATTR_WIPHY])
787 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
788
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700789 if (tb[NL80211_ATTR_IFTYPE])
790 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
791
792 if (tb[NL80211_ATTR_MAC] && info->macaddr)
793 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
794 ETH_ALEN);
795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 return NL_SKIP;
797}
798
799
800static int nl80211_get_wiphy_index(struct i802_bss *bss)
801{
802 struct nl_msg *msg;
803 struct wiphy_idx_data data = {
804 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700805 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800806 };
807
808 msg = nlmsg_alloc();
809 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700810 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811
812 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
813
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700814 if (nl80211_set_iface_id(msg, bss) < 0)
815 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816
817 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
818 return data.wiphy_idx;
819 msg = NULL;
820nla_put_failure:
821 nlmsg_free(msg);
822 return -1;
823}
824
825
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700826static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
827{
828 struct nl_msg *msg;
829 struct wiphy_idx_data data = {
830 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
831 .macaddr = NULL,
832 };
833
834 msg = nlmsg_alloc();
835 if (!msg)
836 return -1;
837
838 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
839
840 if (nl80211_set_iface_id(msg, bss) < 0)
841 goto nla_put_failure;
842
843 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
844 return data.nlmode;
845 msg = NULL;
846nla_put_failure:
847 nlmsg_free(msg);
848 return NL80211_IFTYPE_UNSPECIFIED;
849}
850
851
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700852static int nl80211_get_macaddr(struct i802_bss *bss)
853{
854 struct nl_msg *msg;
855 struct wiphy_idx_data data = {
856 .macaddr = bss->addr,
857 };
858
859 msg = nlmsg_alloc();
860 if (!msg)
861 return NL80211_IFTYPE_UNSPECIFIED;
862
863 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
864 if (nl80211_set_iface_id(msg, bss) < 0)
865 goto nla_put_failure;
866
867 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
868
869nla_put_failure:
870 nlmsg_free(msg);
871 return NL80211_IFTYPE_UNSPECIFIED;
872}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700873
874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
876 struct nl80211_wiphy_data *w)
877{
878 struct nl_msg *msg;
879 int ret = -1;
880
881 msg = nlmsg_alloc();
882 if (!msg)
883 return -1;
884
885 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
886
887 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
888
889 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
890 msg = NULL;
891 if (ret) {
892 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
893 "failed: ret=%d (%s)",
894 ret, strerror(-ret));
895 goto nla_put_failure;
896 }
897 ret = 0;
898nla_put_failure:
899 nlmsg_free(msg);
900 return ret;
901}
902
903
904static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
905{
906 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700907 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800908
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800909 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800910
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700911 res = nl_recvmsgs(handle, w->nl_cb);
912 if (res) {
913 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
914 __func__, res);
915 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800916}
917
918
919static int process_beacon_event(struct nl_msg *msg, void *arg)
920{
921 struct nl80211_wiphy_data *w = arg;
922 struct wpa_driver_nl80211_data *drv;
923 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
924 struct nlattr *tb[NL80211_ATTR_MAX + 1];
925 union wpa_event_data event;
926
927 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
928 genlmsg_attrlen(gnlh, 0), NULL);
929
930 if (gnlh->cmd != NL80211_CMD_FRAME) {
931 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
932 gnlh->cmd);
933 return NL_SKIP;
934 }
935
936 if (!tb[NL80211_ATTR_FRAME])
937 return NL_SKIP;
938
939 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
940 wiphy_list) {
941 os_memset(&event, 0, sizeof(event));
942 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
943 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
944 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
945 }
946
947 return NL_SKIP;
948}
949
950
951static struct nl80211_wiphy_data *
952nl80211_get_wiphy_data_ap(struct i802_bss *bss)
953{
954 static DEFINE_DL_LIST(nl80211_wiphys);
955 struct nl80211_wiphy_data *w;
956 int wiphy_idx, found = 0;
957 struct i802_bss *tmp_bss;
958
959 if (bss->wiphy_data != NULL)
960 return bss->wiphy_data;
961
962 wiphy_idx = nl80211_get_wiphy_index(bss);
963
964 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
965 if (w->wiphy_idx == wiphy_idx)
966 goto add;
967 }
968
969 /* alloc new one */
970 w = os_zalloc(sizeof(*w));
971 if (w == NULL)
972 return NULL;
973 w->wiphy_idx = wiphy_idx;
974 dl_list_init(&w->bsss);
975 dl_list_init(&w->drvs);
976
977 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
978 if (!w->nl_cb) {
979 os_free(w);
980 return NULL;
981 }
982 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
983 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
984 w);
985
986 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
987 "wiphy beacons");
988 if (w->nl_beacons == NULL) {
989 os_free(w);
990 return NULL;
991 }
992
993 if (nl80211_register_beacons(bss->drv, w)) {
994 nl_destroy_handles(&w->nl_beacons);
995 os_free(w);
996 return NULL;
997 }
998
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700999 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000
1001 dl_list_add(&nl80211_wiphys, &w->list);
1002
1003add:
1004 /* drv entry for this bss already there? */
1005 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1006 if (tmp_bss->drv == bss->drv) {
1007 found = 1;
1008 break;
1009 }
1010 }
1011 /* if not add it */
1012 if (!found)
1013 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
1014
1015 dl_list_add(&w->bsss, &bss->wiphy_list);
1016 bss->wiphy_data = w;
1017 return w;
1018}
1019
1020
1021static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1022{
1023 struct nl80211_wiphy_data *w = bss->wiphy_data;
1024 struct i802_bss *tmp_bss;
1025 int found = 0;
1026
1027 if (w == NULL)
1028 return;
1029 bss->wiphy_data = NULL;
1030 dl_list_del(&bss->wiphy_list);
1031
1032 /* still any for this drv present? */
1033 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1034 if (tmp_bss->drv == bss->drv) {
1035 found = 1;
1036 break;
1037 }
1038 }
1039 /* if not remove it */
1040 if (!found)
1041 dl_list_del(&bss->drv->wiphy_list);
1042
1043 if (!dl_list_empty(&w->bsss))
1044 return;
1045
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001046 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047
1048 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 dl_list_del(&w->list);
1050 os_free(w);
1051}
1052
1053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1055{
1056 struct i802_bss *bss = priv;
1057 struct wpa_driver_nl80211_data *drv = bss->drv;
1058 if (!drv->associated)
1059 return -1;
1060 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1061 return 0;
1062}
1063
1064
1065static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1066{
1067 struct i802_bss *bss = priv;
1068 struct wpa_driver_nl80211_data *drv = bss->drv;
1069 if (!drv->associated)
1070 return -1;
1071 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1072 return drv->ssid_len;
1073}
1074
1075
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001076static void wpa_driver_nl80211_event_newlink(
1077 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078{
1079 union wpa_event_data event;
1080
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001081 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1082 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1083 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1084 drv->first_bss->ifname);
1085 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001087 if (!drv->if_removed)
1088 return;
1089 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1090 drv->first_bss->ifname);
1091 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 }
1093
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094 os_memset(&event, 0, sizeof(event));
1095 os_strlcpy(event.interface_status.ifname, ifname,
1096 sizeof(event.interface_status.ifname));
1097 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1098 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1099}
1100
1101
1102static void wpa_driver_nl80211_event_dellink(
1103 struct wpa_driver_nl80211_data *drv, char *ifname)
1104{
1105 union wpa_event_data event;
1106
1107 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1108 if (drv->if_removed) {
1109 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1110 ifname);
1111 return;
1112 }
1113 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1114 ifname);
1115 drv->if_removed = 1;
1116 } else {
1117 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1118 ifname);
1119 }
1120
1121 os_memset(&event, 0, sizeof(event));
1122 os_strlcpy(event.interface_status.ifname, ifname,
1123 sizeof(event.interface_status.ifname));
1124 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1126}
1127
1128
1129static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1130 u8 *buf, size_t len)
1131{
1132 int attrlen, rta_len;
1133 struct rtattr *attr;
1134
1135 attrlen = len;
1136 attr = (struct rtattr *) buf;
1137
1138 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1139 while (RTA_OK(attr, attrlen)) {
1140 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001141 if (os_strcmp(((char *) attr) + rta_len,
1142 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return 1;
1144 else
1145 break;
1146 }
1147 attr = RTA_NEXT(attr, attrlen);
1148 }
1149
1150 return 0;
1151}
1152
1153
1154static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1155 int ifindex, u8 *buf, size_t len)
1156{
1157 if (drv->ifindex == ifindex)
1158 return 1;
1159
1160 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1162 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001163 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 return 1;
1165 }
1166
1167 return 0;
1168}
1169
1170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171static struct wpa_driver_nl80211_data *
1172nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1173{
1174 struct wpa_driver_nl80211_data *drv;
1175 dl_list_for_each(drv, &global->interfaces,
1176 struct wpa_driver_nl80211_data, list) {
1177 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1178 have_ifidx(drv, idx))
1179 return drv;
1180 }
1181 return NULL;
1182}
1183
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1186 struct ifinfomsg *ifi,
1187 u8 *buf, size_t len)
1188{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001189 struct nl80211_global *global = ctx;
1190 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001191 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 struct rtattr *attr;
1193 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001194 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001195 char ifname[IFNAMSIZ + 1];
1196 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1199 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001200 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1201 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 return;
1203 }
1204
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001205 extra[0] = '\0';
1206 pos = extra;
1207 end = pos + sizeof(extra);
1208 ifname[0] = '\0';
1209
1210 attrlen = len;
1211 attr = (struct rtattr *) buf;
1212 while (RTA_OK(attr, attrlen)) {
1213 switch (attr->rta_type) {
1214 case IFLA_IFNAME:
1215 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1216 break;
1217 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1218 ifname[RTA_PAYLOAD(attr)] = '\0';
1219 break;
1220 case IFLA_MASTER:
1221 brid = nla_get_u32((struct nlattr *) attr);
1222 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1223 break;
1224 case IFLA_WIRELESS:
1225 pos += os_snprintf(pos, end - pos, " wext");
1226 break;
1227 case IFLA_OPERSTATE:
1228 pos += os_snprintf(pos, end - pos, " operstate=%u",
1229 nla_get_u32((struct nlattr *) attr));
1230 break;
1231 case IFLA_LINKMODE:
1232 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1233 nla_get_u32((struct nlattr *) attr));
1234 break;
1235 }
1236 attr = RTA_NEXT(attr, attrlen);
1237 }
1238 extra[sizeof(extra) - 1] = '\0';
1239
1240 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1241 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1243 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1244 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1245 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1246
1247 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001248 if (if_indextoname(ifi->ifi_index, namebuf) &&
1249 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001250 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1252 "event since interface %s is up", namebuf);
1253 return;
1254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001256 if (drv->ignore_if_down_event) {
1257 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1258 "event generated by mode change");
1259 drv->ignore_if_down_event = 0;
1260 } else {
1261 drv->if_disabled = 1;
1262 wpa_supplicant_event(drv->ctx,
1263 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001264
1265 /*
1266 * Try to get drv again, since it may be removed as
1267 * part of the EVENT_INTERFACE_DISABLED handling for
1268 * dynamic interfaces
1269 */
1270 drv = nl80211_find_drv(global, ifi->ifi_index,
1271 buf, len);
1272 if (!drv)
1273 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 }
1276
1277 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001278 if (if_indextoname(ifi->ifi_index, namebuf) &&
1279 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001280 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001281 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1282 "event since interface %s is down",
1283 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001284 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001285 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1286 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001287 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001288 } else if (drv->if_removed) {
1289 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1290 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001291 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292 } else {
1293 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1294 drv->if_disabled = 0;
1295 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1296 NULL);
1297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 }
1299
1300 /*
1301 * Some drivers send the association event before the operup event--in
1302 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1303 * fails. This will hit us when wpa_supplicant does not need to do
1304 * IEEE 802.1X authentication
1305 */
1306 if (drv->operstate == 1 &&
1307 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001308 !(ifi->ifi_flags & IFF_RUNNING)) {
1309 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 }
1313
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001314 if (ifname[0])
1315 wpa_driver_nl80211_event_newlink(drv, ifname);
1316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 if (ifi->ifi_family == AF_BRIDGE && brid) {
1318 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 if_indextoname(brid, namebuf);
1320 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1321 brid, namebuf);
1322 add_ifidx(drv, brid);
1323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324}
1325
1326
1327static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1328 struct ifinfomsg *ifi,
1329 u8 *buf, size_t len)
1330{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001331 struct nl80211_global *global = ctx;
1332 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001333 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 struct rtattr *attr;
1335 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001336 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001338 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1339 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001340 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1341 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 return;
1343 }
1344
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001345 ifname[0] = '\0';
1346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 attrlen = len;
1348 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001350 switch (attr->rta_type) {
1351 case IFLA_IFNAME:
1352 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1353 break;
1354 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1355 ifname[RTA_PAYLOAD(attr)] = '\0';
1356 break;
1357 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001359 break;
1360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 attr = RTA_NEXT(attr, attrlen);
1362 }
1363
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001364 if (ifname[0])
1365 wpa_driver_nl80211_event_dellink(drv, ifname);
1366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 if (ifi->ifi_family == AF_BRIDGE && brid) {
1368 /* device has been removed from bridge */
1369 char namebuf[IFNAMSIZ];
1370 if_indextoname(brid, namebuf);
1371 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1372 "%s", brid, namebuf);
1373 del_ifidx(drv, brid);
1374 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375}
1376
1377
1378static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1379 const u8 *frame, size_t len)
1380{
1381 const struct ieee80211_mgmt *mgmt;
1382 union wpa_event_data event;
1383
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001384 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 mgmt = (const struct ieee80211_mgmt *) frame;
1386 if (len < 24 + sizeof(mgmt->u.auth)) {
1387 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1388 "frame");
1389 return;
1390 }
1391
1392 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001393 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 os_memset(&event, 0, sizeof(event));
1395 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1396 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001397 event.auth.auth_transaction =
1398 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1400 if (len > 24 + sizeof(mgmt->u.auth)) {
1401 event.auth.ies = mgmt->u.auth.variable;
1402 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1403 }
1404
1405 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1406}
1407
1408
Jouni Malinen87fd2792011-05-16 18:35:42 +03001409static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1410{
1411 struct nl_msg *msg;
1412 int ret;
1413 struct nl80211_bss_info_arg arg;
1414
1415 os_memset(&arg, 0, sizeof(arg));
1416 msg = nlmsg_alloc();
1417 if (!msg)
1418 goto nla_put_failure;
1419
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001420 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001421 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1422
1423 arg.drv = drv;
1424 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1425 msg = NULL;
1426 if (ret == 0) {
1427 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1428 "associated BSS from scan results: %u MHz",
1429 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001430 if (arg.assoc_freq)
1431 drv->assoc_freq = arg.assoc_freq;
1432 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001433 }
1434 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1435 "(%s)", ret, strerror(-ret));
1436nla_put_failure:
1437 nlmsg_free(msg);
1438 return drv->assoc_freq;
1439}
1440
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1443 const u8 *frame, size_t len)
1444{
1445 const struct ieee80211_mgmt *mgmt;
1446 union wpa_event_data event;
1447 u16 status;
1448
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001449 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 mgmt = (const struct ieee80211_mgmt *) frame;
1451 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1452 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1453 "frame");
1454 return;
1455 }
1456
1457 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1458 if (status != WLAN_STATUS_SUCCESS) {
1459 os_memset(&event, 0, sizeof(event));
1460 event.assoc_reject.bssid = mgmt->bssid;
1461 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1462 event.assoc_reject.resp_ies =
1463 (u8 *) mgmt->u.assoc_resp.variable;
1464 event.assoc_reject.resp_ies_len =
1465 len - 24 - sizeof(mgmt->u.assoc_resp);
1466 }
1467 event.assoc_reject.status_code = status;
1468
1469 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1470 return;
1471 }
1472
1473 drv->associated = 1;
1474 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001475 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476
1477 os_memset(&event, 0, sizeof(event));
1478 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1479 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1480 event.assoc_info.resp_ies_len =
1481 len - 24 - sizeof(mgmt->u.assoc_resp);
1482 }
1483
1484 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1487}
1488
1489
1490static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1491 enum nl80211_commands cmd, struct nlattr *status,
1492 struct nlattr *addr, struct nlattr *req_ie,
1493 struct nlattr *resp_ie)
1494{
1495 union wpa_event_data event;
1496
1497 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1498 /*
1499 * Avoid reporting two association events that would confuse
1500 * the core code.
1501 */
1502 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1503 "when using userspace SME", cmd);
1504 return;
1505 }
1506
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001507 if (cmd == NL80211_CMD_CONNECT)
1508 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1509 else if (cmd == NL80211_CMD_ROAM)
1510 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 os_memset(&event, 0, sizeof(event));
1513 if (cmd == NL80211_CMD_CONNECT &&
1514 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1515 if (addr)
1516 event.assoc_reject.bssid = nla_data(addr);
1517 if (resp_ie) {
1518 event.assoc_reject.resp_ies = nla_data(resp_ie);
1519 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1520 }
1521 event.assoc_reject.status_code = nla_get_u16(status);
1522 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1523 return;
1524 }
1525
1526 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001527 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001529 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1530 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531
1532 if (req_ie) {
1533 event.assoc_info.req_ies = nla_data(req_ie);
1534 event.assoc_info.req_ies_len = nla_len(req_ie);
1535 }
1536 if (resp_ie) {
1537 event.assoc_info.resp_ies = nla_data(resp_ie);
1538 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1539 }
1540
Jouni Malinen87fd2792011-05-16 18:35:42 +03001541 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1544}
1545
1546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001547static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001548 struct nlattr *reason, struct nlattr *addr,
1549 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001550{
1551 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001552 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553
1554 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1555 /*
1556 * Avoid reporting two disassociation events that could
1557 * confuse the core code.
1558 */
1559 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1560 "event when using userspace SME");
1561 return;
1562 }
1563
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001564 if (drv->ignore_next_local_disconnect) {
1565 drv->ignore_next_local_disconnect = 0;
1566 if (locally_generated) {
1567 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1568 "event triggered during reassociation");
1569 return;
1570 }
1571 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1572 "disconnect but got another disconnect "
1573 "event first");
1574 }
1575
1576 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001577 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001578 os_memset(&data, 0, sizeof(data));
1579 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001580 data.deauth_info.reason_code = nla_get_u16(reason);
1581 data.deauth_info.locally_generated = by_ap == NULL;
1582 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1583}
1584
1585
Dmitry Shmidt97672262014-02-03 13:02:54 -08001586static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1587{
1588 int freq1 = 0;
1589
1590 switch (convert2width(width)) {
1591 case CHAN_WIDTH_20_NOHT:
1592 case CHAN_WIDTH_20:
1593 return 0;
1594 case CHAN_WIDTH_40:
1595 freq1 = cf1 - 10;
1596 break;
1597 case CHAN_WIDTH_80:
1598 freq1 = cf1 - 30;
1599 break;
1600 case CHAN_WIDTH_160:
1601 freq1 = cf1 - 70;
1602 break;
1603 case CHAN_WIDTH_UNKNOWN:
1604 case CHAN_WIDTH_80P80:
1605 /* FIXME: implement this */
1606 return 0;
1607 }
1608
1609 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1610}
1611
1612
Dmitry Shmidt04949592012-07-19 12:16:46 -07001613static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001614 struct nlattr *ifindex, struct nlattr *freq,
1615 struct nlattr *type, struct nlattr *bw,
1616 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001617{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001618 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001619 union wpa_event_data data;
1620 int ht_enabled = 1;
1621 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001622 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001623
1624 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1625
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001626 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001627 return;
1628
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001629 ifidx = nla_get_u32(ifindex);
1630 for (bss = drv->first_bss; bss; bss = bss->next)
1631 if (bss->ifindex == ifidx)
1632 break;
1633
1634 if (bss == NULL) {
1635 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1636 ifidx);
1637 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001638 }
1639
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001640 if (type) {
1641 switch (nla_get_u32(type)) {
1642 case NL80211_CHAN_NO_HT:
1643 ht_enabled = 0;
1644 break;
1645 case NL80211_CHAN_HT20:
1646 break;
1647 case NL80211_CHAN_HT40PLUS:
1648 chan_offset = 1;
1649 break;
1650 case NL80211_CHAN_HT40MINUS:
1651 chan_offset = -1;
1652 break;
1653 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001654 } else if (bw && cf1) {
1655 /* This can happen for example with VHT80 ch switch */
1656 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1657 nla_get_u32(freq),
1658 nla_get_u32(cf1),
1659 cf2 ? nla_get_u32(cf2) : 0);
1660 } else {
1661 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001662 }
1663
1664 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001665 data.ch_switch.freq = nla_get_u32(freq);
1666 data.ch_switch.ht_enabled = ht_enabled;
1667 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001668 if (bw)
1669 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1670 if (cf1)
1671 data.ch_switch.cf1 = nla_get_u32(cf1);
1672 if (cf2)
1673 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001675 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001676
Dmitry Shmidt04949592012-07-19 12:16:46 -07001677 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001678}
1679
1680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1682 enum nl80211_commands cmd, struct nlattr *addr)
1683{
1684 union wpa_event_data event;
1685 enum wpa_event_type ev;
1686
1687 if (nla_len(addr) != ETH_ALEN)
1688 return;
1689
1690 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1691 cmd, MAC2STR((u8 *) nla_data(addr)));
1692
1693 if (cmd == NL80211_CMD_AUTHENTICATE)
1694 ev = EVENT_AUTH_TIMED_OUT;
1695 else if (cmd == NL80211_CMD_ASSOCIATE)
1696 ev = EVENT_ASSOC_TIMED_OUT;
1697 else
1698 return;
1699
1700 os_memset(&event, 0, sizeof(event));
1701 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1702 wpa_supplicant_event(drv->ctx, ev, &event);
1703}
1704
1705
Dmitry Shmidt98660862014-03-11 17:26:21 -07001706static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001707 struct nlattr *freq, struct nlattr *sig,
1708 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001710 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 const struct ieee80211_mgmt *mgmt;
1712 union wpa_event_data event;
1713 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001714 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001715 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001717 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 mgmt = (const struct ieee80211_mgmt *) frame;
1719 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001720 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 return;
1722 }
1723
1724 fc = le_to_host16(mgmt->frame_control);
1725 stype = WLAN_FC_GET_STYPE(fc);
1726
Dmitry Shmidt04949592012-07-19 12:16:46 -07001727 if (sig)
1728 ssi_signal = (s32) nla_get_u32(sig);
1729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 os_memset(&event, 0, sizeof(event));
1731 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001732 event.rx_mgmt.freq = nla_get_u32(freq);
1733 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001735 wpa_printf(MSG_DEBUG,
1736 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1737 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001738 event.rx_mgmt.frame = frame;
1739 event.rx_mgmt.frame_len = len;
1740 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001741 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001742 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743}
1744
1745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1747 struct nlattr *cookie, const u8 *frame,
1748 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749{
1750 union wpa_event_data event;
1751 const struct ieee80211_hdr *hdr;
1752 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001754 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755 if (!is_ap_interface(drv->nlmode)) {
1756 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758 if (!cookie)
1759 return;
1760
1761 cookie_val = nla_get_u64(cookie);
1762 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1763 " cookie=0%llx%s (ack=%d)",
1764 (long long unsigned int) cookie_val,
1765 cookie_val == drv->send_action_cookie ?
1766 " (match)" : " (unknown)", ack != NULL);
1767 if (cookie_val != drv->send_action_cookie)
1768 return;
1769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770
1771 hdr = (const struct ieee80211_hdr *) frame;
1772 fc = le_to_host16(hdr->frame_control);
1773
1774 os_memset(&event, 0, sizeof(event));
1775 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1776 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1777 event.tx_status.dst = hdr->addr1;
1778 event.tx_status.data = frame;
1779 event.tx_status.data_len = len;
1780 event.tx_status.ack = ack != NULL;
1781 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1782}
1783
1784
1785static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1786 enum wpa_event_type type,
1787 const u8 *frame, size_t len)
1788{
1789 const struct ieee80211_mgmt *mgmt;
1790 union wpa_event_data event;
1791 const u8 *bssid = NULL;
1792 u16 reason_code = 0;
1793
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001794 if (type == EVENT_DEAUTH)
1795 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1796 else
1797 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 mgmt = (const struct ieee80211_mgmt *) frame;
1800 if (len >= 24) {
1801 bssid = mgmt->bssid;
1802
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001803 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1804 !drv->associated &&
1805 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1806 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1807 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1808 /*
1809 * Avoid issues with some roaming cases where
1810 * disconnection event for the old AP may show up after
1811 * we have started connection with the new AP.
1812 */
1813 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1814 MAC2STR(bssid),
1815 MAC2STR(drv->auth_attempt_bssid));
1816 return;
1817 }
1818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 if (drv->associated != 0 &&
1820 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1821 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1822 /*
1823 * We have presumably received this deauth as a
1824 * response to a clear_state_mismatch() outgoing
1825 * deauth. Don't let it take us offline!
1826 */
1827 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1828 "from Unknown BSSID " MACSTR " -- ignoring",
1829 MAC2STR(bssid));
1830 return;
1831 }
1832 }
1833
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001834 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 os_memset(&event, 0, sizeof(event));
1836
1837 /* Note: Same offset for Reason Code in both frame subtypes */
1838 if (len >= 24 + sizeof(mgmt->u.deauth))
1839 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1840
1841 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001842 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001843 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 event.disassoc_info.addr = bssid;
1845 event.disassoc_info.reason_code = reason_code;
1846 if (frame + len > mgmt->u.disassoc.variable) {
1847 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1848 event.disassoc_info.ie_len = frame + len -
1849 mgmt->u.disassoc.variable;
1850 }
1851 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001852 if (drv->ignore_deauth_event) {
1853 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1854 drv->ignore_deauth_event = 0;
1855 return;
1856 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001857 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001858 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001859 if (drv->ignore_next_local_deauth) {
1860 drv->ignore_next_local_deauth = 0;
1861 if (event.deauth_info.locally_generated) {
1862 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1863 return;
1864 }
1865 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1866 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 event.deauth_info.addr = bssid;
1868 event.deauth_info.reason_code = reason_code;
1869 if (frame + len > mgmt->u.deauth.variable) {
1870 event.deauth_info.ie = mgmt->u.deauth.variable;
1871 event.deauth_info.ie_len = frame + len -
1872 mgmt->u.deauth.variable;
1873 }
1874 }
1875
1876 wpa_supplicant_event(drv->ctx, type, &event);
1877}
1878
1879
1880static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1881 enum wpa_event_type type,
1882 const u8 *frame, size_t len)
1883{
1884 const struct ieee80211_mgmt *mgmt;
1885 union wpa_event_data event;
1886 u16 reason_code = 0;
1887
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001888 if (type == EVENT_UNPROT_DEAUTH)
1889 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1890 else
1891 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 if (len < 24)
1894 return;
1895
1896 mgmt = (const struct ieee80211_mgmt *) frame;
1897
1898 os_memset(&event, 0, sizeof(event));
1899 /* Note: Same offset for Reason Code in both frame subtypes */
1900 if (len >= 24 + sizeof(mgmt->u.deauth))
1901 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1902
1903 if (type == EVENT_UNPROT_DISASSOC) {
1904 event.unprot_disassoc.sa = mgmt->sa;
1905 event.unprot_disassoc.da = mgmt->da;
1906 event.unprot_disassoc.reason_code = reason_code;
1907 } else {
1908 event.unprot_deauth.sa = mgmt->sa;
1909 event.unprot_deauth.da = mgmt->da;
1910 event.unprot_deauth.reason_code = reason_code;
1911 }
1912
1913 wpa_supplicant_event(drv->ctx, type, &event);
1914}
1915
1916
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001917static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 enum nl80211_commands cmd, struct nlattr *frame,
1919 struct nlattr *addr, struct nlattr *timed_out,
1920 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001921 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001923 struct wpa_driver_nl80211_data *drv = bss->drv;
1924 const u8 *data;
1925 size_t len;
1926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 if (timed_out && addr) {
1928 mlme_timeout_event(drv, cmd, addr);
1929 return;
1930 }
1931
1932 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001933 wpa_printf(MSG_DEBUG,
1934 "nl80211: MLME event %d (%s) without frame data",
1935 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 return;
1937 }
1938
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001939 data = nla_data(frame);
1940 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001941 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001942 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1943 MACSTR ") - too short",
1944 cmd, nl80211_command_to_string(cmd), bss->ifname,
1945 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001946 return;
1947 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001948 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1949 ") A1=" MACSTR " A2=" MACSTR, cmd,
1950 nl80211_command_to_string(cmd), bss->ifname,
1951 MAC2STR(bss->addr), MAC2STR(data + 4),
1952 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001953 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001954 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1955 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001956 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1957 "for foreign address", bss->ifname);
1958 return;
1959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1961 nla_data(frame), nla_len(frame));
1962
1963 switch (cmd) {
1964 case NL80211_CMD_AUTHENTICATE:
1965 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1966 break;
1967 case NL80211_CMD_ASSOCIATE:
1968 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1969 break;
1970 case NL80211_CMD_DEAUTHENTICATE:
1971 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1972 nla_data(frame), nla_len(frame));
1973 break;
1974 case NL80211_CMD_DISASSOCIATE:
1975 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1976 nla_data(frame), nla_len(frame));
1977 break;
1978 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07001979 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001980 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 break;
1982 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1984 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 break;
1986 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1987 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1988 nla_data(frame), nla_len(frame));
1989 break;
1990 case NL80211_CMD_UNPROT_DISASSOCIATE:
1991 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1992 nla_data(frame), nla_len(frame));
1993 break;
1994 default:
1995 break;
1996 }
1997}
1998
1999
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002000static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 struct nlattr *tb[])
2002{
2003 union wpa_event_data data;
2004
2005 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2006 os_memset(&data, 0, sizeof(data));
2007 if (tb[NL80211_ATTR_MAC]) {
2008 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2009 nla_data(tb[NL80211_ATTR_MAC]),
2010 nla_len(tb[NL80211_ATTR_MAC]));
2011 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
2012 }
2013 if (tb[NL80211_ATTR_KEY_SEQ]) {
2014 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2015 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2016 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2017 }
2018 if (tb[NL80211_ATTR_KEY_TYPE]) {
2019 enum nl80211_key_type key_type =
2020 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2021 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2022 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2023 data.michael_mic_failure.unicast = 1;
2024 } else
2025 data.michael_mic_failure.unicast = 1;
2026
2027 if (tb[NL80211_ATTR_KEY_IDX]) {
2028 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2029 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2030 }
2031
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002032 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033}
2034
2035
2036static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2037 struct nlattr *tb[])
2038{
2039 if (tb[NL80211_ATTR_MAC] == NULL) {
2040 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2041 "event");
2042 return;
2043 }
2044 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 drv->associated = 1;
2047 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2048 MAC2STR(drv->bssid));
2049
2050 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2051}
2052
2053
2054static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2055 int cancel_event, struct nlattr *tb[])
2056{
2057 unsigned int freq, chan_type, duration;
2058 union wpa_event_data data;
2059 u64 cookie;
2060
2061 if (tb[NL80211_ATTR_WIPHY_FREQ])
2062 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2063 else
2064 freq = 0;
2065
2066 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2067 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2068 else
2069 chan_type = 0;
2070
2071 if (tb[NL80211_ATTR_DURATION])
2072 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2073 else
2074 duration = 0;
2075
2076 if (tb[NL80211_ATTR_COOKIE])
2077 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2078 else
2079 cookie = 0;
2080
2081 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2082 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2083 cancel_event, freq, chan_type, duration,
2084 (long long unsigned int) cookie,
2085 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2086
2087 if (cookie != drv->remain_on_chan_cookie)
2088 return; /* not for us */
2089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 if (cancel_event)
2091 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092
2093 os_memset(&data, 0, sizeof(data));
2094 data.remain_on_channel.freq = freq;
2095 data.remain_on_channel.duration = duration;
2096 wpa_supplicant_event(drv->ctx, cancel_event ?
2097 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2098 EVENT_REMAIN_ON_CHANNEL, &data);
2099}
2100
2101
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002102static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2103 struct nlattr *tb[])
2104{
2105 union wpa_event_data data;
2106
2107 os_memset(&data, 0, sizeof(data));
2108
2109 if (tb[NL80211_ATTR_IE]) {
2110 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2111 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2112 }
2113
2114 if (tb[NL80211_ATTR_IE_RIC]) {
2115 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2116 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2117 }
2118
2119 if (tb[NL80211_ATTR_MAC])
2120 os_memcpy(data.ft_ies.target_ap,
2121 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2122
2123 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2124 MAC2STR(data.ft_ies.target_ap));
2125
2126 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2127}
2128
2129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2131 struct nlattr *tb[])
2132{
2133 union wpa_event_data event;
2134 struct nlattr *nl;
2135 int rem;
2136 struct scan_info *info;
2137#define MAX_REPORT_FREQS 50
2138 int freqs[MAX_REPORT_FREQS];
2139 int num_freqs = 0;
2140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002141 if (drv->scan_for_auth) {
2142 drv->scan_for_auth = 0;
2143 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2144 "cfg80211 BSS entry");
2145 wpa_driver_nl80211_authenticate_retry(drv);
2146 return;
2147 }
2148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 os_memset(&event, 0, sizeof(event));
2150 info = &event.scan_info;
2151 info->aborted = aborted;
2152
2153 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2154 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2155 struct wpa_driver_scan_ssid *s =
2156 &info->ssids[info->num_ssids];
2157 s->ssid = nla_data(nl);
2158 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002159 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2160 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 info->num_ssids++;
2162 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2163 break;
2164 }
2165 }
2166 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002167 char msg[200], *pos, *end;
2168 int res;
2169
2170 pos = msg;
2171 end = pos + sizeof(msg);
2172 *pos = '\0';
2173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2175 {
2176 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002177 res = os_snprintf(pos, end - pos, " %d",
2178 freqs[num_freqs]);
2179 if (res > 0 && end - pos > res)
2180 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 num_freqs++;
2182 if (num_freqs == MAX_REPORT_FREQS - 1)
2183 break;
2184 }
2185 info->freqs = freqs;
2186 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002187 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2188 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 }
2190 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2191}
2192
2193
2194static int get_link_signal(struct nl_msg *msg, void *arg)
2195{
2196 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2197 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2198 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2199 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2200 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002201 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 };
2203 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2204 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2205 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2206 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2207 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2208 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2209 };
2210 struct wpa_signal_info *sig_change = arg;
2211
2212 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2213 genlmsg_attrlen(gnlh, 0), NULL);
2214 if (!tb[NL80211_ATTR_STA_INFO] ||
2215 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2216 tb[NL80211_ATTR_STA_INFO], policy))
2217 return NL_SKIP;
2218 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2219 return NL_SKIP;
2220
2221 sig_change->current_signal =
2222 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2223
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002224 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2225 sig_change->avg_signal =
2226 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2227 else
2228 sig_change->avg_signal = 0;
2229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2231 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2232 sinfo[NL80211_STA_INFO_TX_BITRATE],
2233 rate_policy)) {
2234 sig_change->current_txrate = 0;
2235 } else {
2236 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2237 sig_change->current_txrate =
2238 nla_get_u16(rinfo[
2239 NL80211_RATE_INFO_BITRATE]) * 100;
2240 }
2241 }
2242 }
2243
2244 return NL_SKIP;
2245}
2246
2247
2248static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2249 struct wpa_signal_info *sig)
2250{
2251 struct nl_msg *msg;
2252
2253 sig->current_signal = -9999;
2254 sig->current_txrate = 0;
2255
2256 msg = nlmsg_alloc();
2257 if (!msg)
2258 return -ENOMEM;
2259
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002260 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261
2262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2263 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2264
2265 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2266 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 return -ENOBUFS;
2269}
2270
2271
2272static int get_link_noise(struct nl_msg *msg, void *arg)
2273{
2274 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2275 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2276 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2277 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2278 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2279 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2280 };
2281 struct wpa_signal_info *sig_change = arg;
2282
2283 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2284 genlmsg_attrlen(gnlh, 0), NULL);
2285
2286 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2287 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2288 return NL_SKIP;
2289 }
2290
2291 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2292 tb[NL80211_ATTR_SURVEY_INFO],
2293 survey_policy)) {
2294 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2295 "attributes!");
2296 return NL_SKIP;
2297 }
2298
2299 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2300 return NL_SKIP;
2301
2302 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2303 sig_change->frequency)
2304 return NL_SKIP;
2305
2306 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2307 return NL_SKIP;
2308
2309 sig_change->current_noise =
2310 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2311
2312 return NL_SKIP;
2313}
2314
2315
2316static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2317 struct wpa_signal_info *sig_change)
2318{
2319 struct nl_msg *msg;
2320
2321 sig_change->current_noise = 9999;
2322 sig_change->frequency = drv->assoc_freq;
2323
2324 msg = nlmsg_alloc();
2325 if (!msg)
2326 return -ENOMEM;
2327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002328 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329
2330 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2331
2332 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2333 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002334 nlmsg_free(msg);
2335 return -ENOBUFS;
2336}
2337
2338
2339static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2340{
2341 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2342 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2343 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2344 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2345 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2346 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2347 };
2348 struct wpa_scan_results *scan_results = arg;
2349 struct wpa_scan_res *scan_res;
2350 size_t i;
2351
2352 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2353 genlmsg_attrlen(gnlh, 0), NULL);
2354
2355 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2356 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2357 return NL_SKIP;
2358 }
2359
2360 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2361 tb[NL80211_ATTR_SURVEY_INFO],
2362 survey_policy)) {
2363 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2364 "attributes");
2365 return NL_SKIP;
2366 }
2367
2368 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2369 return NL_SKIP;
2370
2371 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2372 return NL_SKIP;
2373
2374 for (i = 0; i < scan_results->num; ++i) {
2375 scan_res = scan_results->res[i];
2376 if (!scan_res)
2377 continue;
2378 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2379 scan_res->freq)
2380 continue;
2381 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2382 continue;
2383 scan_res->noise = (s8)
2384 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2385 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2386 }
2387
2388 return NL_SKIP;
2389}
2390
2391
2392static int nl80211_get_noise_for_scan_results(
2393 struct wpa_driver_nl80211_data *drv,
2394 struct wpa_scan_results *scan_res)
2395{
2396 struct nl_msg *msg;
2397
2398 msg = nlmsg_alloc();
2399 if (!msg)
2400 return -ENOMEM;
2401
2402 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2403
2404 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2405
2406 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2407 scan_res);
2408 nla_put_failure:
2409 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410 return -ENOBUFS;
2411}
2412
2413
2414static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2415 struct nlattr *tb[])
2416{
2417 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2418 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2419 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2420 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2421 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2422 };
2423 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2424 enum nl80211_cqm_rssi_threshold_event event;
2425 union wpa_event_data ed;
2426 struct wpa_signal_info sig;
2427 int res;
2428
2429 if (tb[NL80211_ATTR_CQM] == NULL ||
2430 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2431 cqm_policy)) {
2432 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2433 return;
2434 }
2435
2436 os_memset(&ed, 0, sizeof(ed));
2437
2438 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2439 if (!tb[NL80211_ATTR_MAC])
2440 return;
2441 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2442 ETH_ALEN);
2443 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2444 return;
2445 }
2446
2447 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2448 return;
2449 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2450
2451 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2452 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2453 "event: RSSI high");
2454 ed.signal_change.above_threshold = 1;
2455 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2456 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2457 "event: RSSI low");
2458 ed.signal_change.above_threshold = 0;
2459 } else
2460 return;
2461
2462 res = nl80211_get_link_signal(drv, &sig);
2463 if (res == 0) {
2464 ed.signal_change.current_signal = sig.current_signal;
2465 ed.signal_change.current_txrate = sig.current_txrate;
2466 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2467 sig.current_signal, sig.current_txrate);
2468 }
2469
2470 res = nl80211_get_link_noise(drv, &sig);
2471 if (res == 0) {
2472 ed.signal_change.current_noise = sig.current_noise;
2473 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2474 sig.current_noise);
2475 }
2476
2477 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2478}
2479
2480
2481static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2482 struct nlattr **tb)
2483{
2484 u8 *addr;
2485 union wpa_event_data data;
2486
2487 if (tb[NL80211_ATTR_MAC] == NULL)
2488 return;
2489 addr = nla_data(tb[NL80211_ATTR_MAC]);
2490 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002492 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002493 u8 *ies = NULL;
2494 size_t ies_len = 0;
2495 if (tb[NL80211_ATTR_IE]) {
2496 ies = nla_data(tb[NL80211_ATTR_IE]);
2497 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2498 }
2499 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2500 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2501 return;
2502 }
2503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2505 return;
2506
2507 os_memset(&data, 0, sizeof(data));
2508 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2509 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2510}
2511
2512
2513static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2514 struct nlattr **tb)
2515{
2516 u8 *addr;
2517 union wpa_event_data data;
2518
2519 if (tb[NL80211_ATTR_MAC] == NULL)
2520 return;
2521 addr = nla_data(tb[NL80211_ATTR_MAC]);
2522 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2523 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002525 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002526 drv_event_disassoc(drv->ctx, addr);
2527 return;
2528 }
2529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2531 return;
2532
2533 os_memset(&data, 0, sizeof(data));
2534 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2535 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2536}
2537
2538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002539static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2540 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2543 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2544 [NL80211_REKEY_DATA_KEK] = {
2545 .minlen = NL80211_KEK_LEN,
2546 .maxlen = NL80211_KEK_LEN,
2547 },
2548 [NL80211_REKEY_DATA_KCK] = {
2549 .minlen = NL80211_KCK_LEN,
2550 .maxlen = NL80211_KCK_LEN,
2551 },
2552 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2553 .minlen = NL80211_REPLAY_CTR_LEN,
2554 .maxlen = NL80211_REPLAY_CTR_LEN,
2555 },
2556 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 union wpa_event_data data;
2558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002559 if (!tb[NL80211_ATTR_MAC])
2560 return;
2561 if (!tb[NL80211_ATTR_REKEY_DATA])
2562 return;
2563 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2564 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2565 return;
2566 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2567 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002569 os_memset(&data, 0, sizeof(data));
2570 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2571 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2572 MAC2STR(data.driver_gtk_rekey.bssid));
2573 data.driver_gtk_rekey.replay_ctr =
2574 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2575 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2576 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2577 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2578}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580
2581static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2582 struct nlattr **tb)
2583{
2584 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2585 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2586 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2587 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2588 .minlen = ETH_ALEN,
2589 .maxlen = ETH_ALEN,
2590 },
2591 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2592 };
2593 union wpa_event_data data;
2594
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002595 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2598 return;
2599 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2600 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2601 return;
2602 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2603 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2604 return;
2605
2606 os_memset(&data, 0, sizeof(data));
2607 os_memcpy(data.pmkid_candidate.bssid,
2608 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2609 data.pmkid_candidate.index =
2610 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2611 data.pmkid_candidate.preauth =
2612 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2613 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2614}
2615
2616
2617static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2618 struct nlattr **tb)
2619{
2620 union wpa_event_data data;
2621
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002622 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002624 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2625 return;
2626
2627 os_memset(&data, 0, sizeof(data));
2628 os_memcpy(data.client_poll.addr,
2629 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2630
2631 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2632}
2633
2634
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002635static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2636 struct nlattr **tb)
2637{
2638 union wpa_event_data data;
2639
2640 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2641
2642 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2643 return;
2644
2645 os_memset(&data, 0, sizeof(data));
2646 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2647 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2648 case NL80211_TDLS_SETUP:
2649 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2650 MACSTR, MAC2STR(data.tdls.peer));
2651 data.tdls.oper = TDLS_REQUEST_SETUP;
2652 break;
2653 case NL80211_TDLS_TEARDOWN:
2654 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2655 MACSTR, MAC2STR(data.tdls.peer));
2656 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2657 break;
2658 default:
2659 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2660 "event");
2661 return;
2662 }
2663 if (tb[NL80211_ATTR_REASON_CODE]) {
2664 data.tdls.reason_code =
2665 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2666 }
2667
2668 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2669}
2670
2671
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002672static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2673 struct nlattr **tb)
2674{
2675 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2676}
2677
2678
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002679static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2680 struct nlattr **tb)
2681{
2682 union wpa_event_data data;
2683 u32 reason;
2684
2685 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2686
2687 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2688 return;
2689
2690 os_memset(&data, 0, sizeof(data));
2691 os_memcpy(data.connect_failed_reason.addr,
2692 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2693
2694 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2695 switch (reason) {
2696 case NL80211_CONN_FAIL_MAX_CLIENTS:
2697 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2698 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2699 break;
2700 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2701 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2702 " tried to connect",
2703 MAC2STR(data.connect_failed_reason.addr));
2704 data.connect_failed_reason.code = BLOCKED_CLIENT;
2705 break;
2706 default:
2707 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2708 "%u", reason);
2709 return;
2710 }
2711
2712 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2713}
2714
2715
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002716static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2717 struct nlattr **tb)
2718{
2719 union wpa_event_data data;
2720 enum nl80211_radar_event event_type;
2721
2722 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2723 return;
2724
2725 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002726 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2727 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002728
Dmitry Shmidt051af732013-10-22 13:52:46 -07002729 /* Check HT params */
2730 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2731 data.dfs_event.ht_enabled = 1;
2732 data.dfs_event.chan_offset = 0;
2733
2734 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2735 case NL80211_CHAN_NO_HT:
2736 data.dfs_event.ht_enabled = 0;
2737 break;
2738 case NL80211_CHAN_HT20:
2739 break;
2740 case NL80211_CHAN_HT40PLUS:
2741 data.dfs_event.chan_offset = 1;
2742 break;
2743 case NL80211_CHAN_HT40MINUS:
2744 data.dfs_event.chan_offset = -1;
2745 break;
2746 }
2747 }
2748
2749 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002750 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2751 data.dfs_event.chan_width =
2752 convert2width(nla_get_u32(
2753 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2754 if (tb[NL80211_ATTR_CENTER_FREQ1])
2755 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002756 if (tb[NL80211_ATTR_CENTER_FREQ2])
2757 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2758
2759 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2760 data.dfs_event.freq, data.dfs_event.ht_enabled,
2761 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2762 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002763
2764 switch (event_type) {
2765 case NL80211_RADAR_DETECTED:
2766 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2767 break;
2768 case NL80211_RADAR_CAC_FINISHED:
2769 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2770 break;
2771 case NL80211_RADAR_CAC_ABORTED:
2772 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2773 break;
2774 case NL80211_RADAR_NOP_FINISHED:
2775 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2776 break;
2777 default:
2778 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2779 "received", event_type);
2780 break;
2781 }
2782}
2783
2784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002785static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2786 int wds)
2787{
2788 struct wpa_driver_nl80211_data *drv = bss->drv;
2789 union wpa_event_data event;
2790
2791 if (!tb[NL80211_ATTR_MAC])
2792 return;
2793
2794 os_memset(&event, 0, sizeof(event));
2795 event.rx_from_unknown.bssid = bss->addr;
2796 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2797 event.rx_from_unknown.wds = wds;
2798
2799 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2800}
2801
2802
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002803static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2804 const u8 *data, size_t len)
2805{
2806 u32 i, count;
2807 union wpa_event_data event;
2808 struct wpa_freq_range *range = NULL;
2809 const struct qca_avoid_freq_list *freq_range;
2810
2811 freq_range = (const struct qca_avoid_freq_list *) data;
2812 if (len < sizeof(freq_range->count))
2813 return;
2814
2815 count = freq_range->count;
2816 if (len < sizeof(freq_range->count) +
2817 count * sizeof(struct qca_avoid_freq_range)) {
2818 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2819 (unsigned int) len);
2820 return;
2821 }
2822
2823 if (count > 0) {
2824 range = os_calloc(count, sizeof(struct wpa_freq_range));
2825 if (range == NULL)
2826 return;
2827 }
2828
2829 os_memset(&event, 0, sizeof(event));
2830 for (i = 0; i < count; i++) {
2831 unsigned int idx = event.freq_range.num;
2832 range[idx].min = freq_range->range[i].start_freq;
2833 range[idx].max = freq_range->range[i].end_freq;
2834 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2835 range[idx].min, range[idx].max);
2836 if (range[idx].min > range[idx].max) {
2837 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2838 continue;
2839 }
2840 event.freq_range.num++;
2841 }
2842 event.freq_range.range = range;
2843
2844 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2845
2846 os_free(range);
2847}
2848
2849
2850static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2851 u32 subcmd, u8 *data, size_t len)
2852{
2853 switch (subcmd) {
2854 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2855 qca_nl80211_avoid_freq(drv, data, len);
2856 break;
2857 default:
2858 wpa_printf(MSG_DEBUG,
2859 "nl80211: Ignore unsupported QCA vendor event %u",
2860 subcmd);
2861 break;
2862 }
2863}
2864
2865
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002866static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2867 struct nlattr **tb)
2868{
2869 u32 vendor_id, subcmd, wiphy = 0;
2870 int wiphy_idx;
2871 u8 *data = NULL;
2872 size_t len = 0;
2873
2874 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2875 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2876 return;
2877
2878 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2879 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2880
2881 if (tb[NL80211_ATTR_WIPHY])
2882 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2883
2884 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2885 wiphy, vendor_id, subcmd);
2886
2887 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2888 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2889 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2890 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2891 }
2892
2893 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2894 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2895 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2896 wiphy, wiphy_idx);
2897 return;
2898 }
2899
2900 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002901 case OUI_QCA:
2902 nl80211_vendor_event_qca(drv, subcmd, data, len);
2903 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002904 default:
2905 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2906 break;
2907 }
2908}
2909
2910
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002911static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
2912 struct nlattr *tb[])
2913{
2914 union wpa_event_data data;
2915 enum nl80211_reg_initiator init;
2916
2917 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2918
2919 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2920 return;
2921
2922 os_memset(&data, 0, sizeof(data));
2923 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
2924 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
2925 switch (init) {
2926 case NL80211_REGDOM_SET_BY_CORE:
2927 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
2928 break;
2929 case NL80211_REGDOM_SET_BY_USER:
2930 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
2931 break;
2932 case NL80211_REGDOM_SET_BY_DRIVER:
2933 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
2934 break;
2935 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2936 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
2937 break;
2938 }
2939
2940 if (tb[NL80211_ATTR_REG_TYPE]) {
2941 enum nl80211_reg_type type;
2942 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
2943 wpa_printf(MSG_DEBUG, " * type=%d", type);
2944 switch (type) {
2945 case NL80211_REGDOM_TYPE_COUNTRY:
2946 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
2947 break;
2948 case NL80211_REGDOM_TYPE_WORLD:
2949 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
2950 break;
2951 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
2952 data.channel_list_changed.type =
2953 REGDOM_TYPE_CUSTOM_WORLD;
2954 break;
2955 case NL80211_REGDOM_TYPE_INTERSECTION:
2956 data.channel_list_changed.type =
2957 REGDOM_TYPE_INTERSECTION;
2958 break;
2959 }
2960 }
2961
2962 if (tb[NL80211_ATTR_REG_ALPHA2]) {
2963 os_strlcpy(data.channel_list_changed.alpha2,
2964 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
2965 sizeof(data.channel_list_changed.alpha2));
2966 wpa_printf(MSG_DEBUG, " * alpha2=%s",
2967 data.channel_list_changed.alpha2);
2968 }
2969
2970 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
2971}
2972
2973
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002974static void do_process_drv_event(struct i802_bss *bss, int cmd,
2975 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002976{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002977 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002978 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002979
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002980 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2981 cmd, nl80211_command_to_string(cmd), bss->ifname);
2982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002983 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2984 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2985 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002986 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002987 drv->ap_scan_as_station);
2988 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989 }
2990
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002991 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002993 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002994 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002995 if (drv->scan_for_auth) {
2996 /*
2997 * Cannot indicate EVENT_SCAN_STARTED here since we skip
2998 * EVENT_SCAN_RESULTS in scan_for_auth case and the
2999 * upper layer implementation could get confused about
3000 * scanning state.
3001 */
3002 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3003 break;
3004 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003005 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003007 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003008 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003009 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003010 break;
3011 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003012 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003013 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003014 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3015 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003017 wpa_dbg(drv->ctx, MSG_DEBUG,
3018 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003019 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 drv->scan_complete_events = 1;
3021 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3022 drv->ctx);
3023 send_scan_event(drv, 0, tb);
3024 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003026 wpa_dbg(drv->ctx, MSG_DEBUG,
3027 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003028 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003029 send_scan_event(drv, 0, tb);
3030 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003032 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003033 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 /*
3035 * Need to indicate that scan results are available in order
3036 * not to make wpa_supplicant stop its scanning.
3037 */
3038 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3039 drv->ctx);
3040 send_scan_event(drv, 1, tb);
3041 break;
3042 case NL80211_CMD_AUTHENTICATE:
3043 case NL80211_CMD_ASSOCIATE:
3044 case NL80211_CMD_DEAUTHENTICATE:
3045 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 case NL80211_CMD_FRAME_TX_STATUS:
3047 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3048 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003049 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3051 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003052 tb[NL80211_ATTR_COOKIE],
3053 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 break;
3055 case NL80211_CMD_CONNECT:
3056 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003057 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 tb[NL80211_ATTR_STATUS_CODE],
3059 tb[NL80211_ATTR_MAC],
3060 tb[NL80211_ATTR_REQ_IE],
3061 tb[NL80211_ATTR_RESP_IE]);
3062 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003064 mlme_event_ch_switch(drv,
3065 tb[NL80211_ATTR_IFINDEX],
3066 tb[NL80211_ATTR_WIPHY_FREQ],
3067 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3068 tb[NL80211_ATTR_CHANNEL_WIDTH],
3069 tb[NL80211_ATTR_CENTER_FREQ1],
3070 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003071 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003073 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003074 tb[NL80211_ATTR_MAC],
3075 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 break;
3077 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003078 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 break;
3080 case NL80211_CMD_JOIN_IBSS:
3081 mlme_event_join_ibss(drv, tb);
3082 break;
3083 case NL80211_CMD_REMAIN_ON_CHANNEL:
3084 mlme_event_remain_on_channel(drv, 0, tb);
3085 break;
3086 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3087 mlme_event_remain_on_channel(drv, 1, tb);
3088 break;
3089 case NL80211_CMD_NOTIFY_CQM:
3090 nl80211_cqm_event(drv, tb);
3091 break;
3092 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003093 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 break;
3095 case NL80211_CMD_REG_BEACON_HINT:
3096 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003097 os_memset(&data, 0, sizeof(data));
3098 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003100 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 break;
3102 case NL80211_CMD_NEW_STATION:
3103 nl80211_new_station_event(drv, tb);
3104 break;
3105 case NL80211_CMD_DEL_STATION:
3106 nl80211_del_station_event(drv, tb);
3107 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003108 case NL80211_CMD_SET_REKEY_OFFLOAD:
3109 nl80211_rekey_offload_event(drv, tb);
3110 break;
3111 case NL80211_CMD_PMKSA_CANDIDATE:
3112 nl80211_pmksa_candidate_event(drv, tb);
3113 break;
3114 case NL80211_CMD_PROBE_CLIENT:
3115 nl80211_client_probe_event(drv, tb);
3116 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003117 case NL80211_CMD_TDLS_OPER:
3118 nl80211_tdls_oper_event(drv, tb);
3119 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003120 case NL80211_CMD_CONN_FAILED:
3121 nl80211_connect_failed_event(drv, tb);
3122 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003123 case NL80211_CMD_FT_EVENT:
3124 mlme_event_ft_event(drv, tb);
3125 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003126 case NL80211_CMD_RADAR_DETECT:
3127 nl80211_radar_event(drv, tb);
3128 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003129 case NL80211_CMD_STOP_AP:
3130 nl80211_stop_ap(drv, tb);
3131 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003132 case NL80211_CMD_VENDOR:
3133 nl80211_vendor_event(drv, tb);
3134 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003135 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003136 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3137 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003138 break;
3139 }
3140}
3141
3142
3143static int process_drv_event(struct nl_msg *msg, void *arg)
3144{
3145 struct wpa_driver_nl80211_data *drv = arg;
3146 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3147 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003148 struct i802_bss *bss;
3149 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003150
3151 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3152 genlmsg_attrlen(gnlh, 0), NULL);
3153
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003154 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003155 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3156
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003157 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003158 if (ifidx == -1 || ifidx == bss->ifindex) {
3159 do_process_drv_event(bss, gnlh->cmd, tb);
3160 return NL_SKIP;
3161 }
3162 wpa_printf(MSG_DEBUG,
3163 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3164 gnlh->cmd, ifidx);
3165 } else if (tb[NL80211_ATTR_WDEV]) {
3166 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3167 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003168 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003169 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3170 do_process_drv_event(bss, gnlh->cmd, tb);
3171 return NL_SKIP;
3172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003173 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003174 wpa_printf(MSG_DEBUG,
3175 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3176 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003177 }
3178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179 return NL_SKIP;
3180}
3181
3182
3183static int process_global_event(struct nl_msg *msg, void *arg)
3184{
3185 struct nl80211_global *global = arg;
3186 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3187 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003188 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003189 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003190 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003191 u64 wdev_id = 0;
3192 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003193
3194 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3195 genlmsg_attrlen(gnlh, 0), NULL);
3196
3197 if (tb[NL80211_ATTR_IFINDEX])
3198 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003199 else if (tb[NL80211_ATTR_WDEV]) {
3200 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3201 wdev_id_set = 1;
3202 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003203
Dmitry Shmidt04949592012-07-19 12:16:46 -07003204 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3205 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003206 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003207 if ((ifidx == -1 && !wdev_id_set) ||
3208 ifidx == bss->ifindex ||
3209 (wdev_id_set && bss->wdev_id_set &&
3210 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003211 do_process_drv_event(bss, gnlh->cmd, tb);
3212 return NL_SKIP;
3213 }
3214 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003215 }
3216
3217 return NL_SKIP;
3218}
3219
3220
3221static int process_bss_event(struct nl_msg *msg, void *arg)
3222{
3223 struct i802_bss *bss = arg;
3224 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3225 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3226
3227 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3228 genlmsg_attrlen(gnlh, 0), NULL);
3229
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003230 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3231 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3232 bss->ifname);
3233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234 switch (gnlh->cmd) {
3235 case NL80211_CMD_FRAME:
3236 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003237 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003238 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3239 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003240 tb[NL80211_ATTR_COOKIE],
3241 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003242 break;
3243 case NL80211_CMD_UNEXPECTED_FRAME:
3244 nl80211_spurious_frame(bss, tb, 0);
3245 break;
3246 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3247 nl80211_spurious_frame(bss, tb, 1);
3248 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 default:
3250 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3251 "(cmd=%d)", gnlh->cmd);
3252 break;
3253 }
3254
3255 return NL_SKIP;
3256}
3257
3258
3259static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3260 void *handle)
3261{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003262 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003263 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003265 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003267 res = nl_recvmsgs(handle, cb);
3268 if (res) {
3269 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3270 __func__, res);
3271 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272}
3273
3274
3275/**
3276 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3277 * @priv: driver_nl80211 private data
3278 * @alpha2_arg: country to which to switch to
3279 * Returns: 0 on success, -1 on failure
3280 *
3281 * This asks nl80211 to set the regulatory domain for given
3282 * country ISO / IEC alpha2.
3283 */
3284static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3285{
3286 struct i802_bss *bss = priv;
3287 struct wpa_driver_nl80211_data *drv = bss->drv;
3288 char alpha2[3];
3289 struct nl_msg *msg;
3290
3291 msg = nlmsg_alloc();
3292 if (!msg)
3293 return -ENOMEM;
3294
3295 alpha2[0] = alpha2_arg[0];
3296 alpha2[1] = alpha2_arg[1];
3297 alpha2[2] = '\0';
3298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003299 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300
3301 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3302 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3303 return -EINVAL;
3304 return 0;
3305nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003306 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 return -EINVAL;
3308}
3309
3310
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003311static int nl80211_get_country(struct nl_msg *msg, void *arg)
3312{
3313 char *alpha2 = arg;
3314 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3315 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3316
3317 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3318 genlmsg_attrlen(gnlh, 0), NULL);
3319 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3320 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3321 return NL_SKIP;
3322 }
3323 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3324 return NL_SKIP;
3325}
3326
3327
3328static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3329{
3330 struct i802_bss *bss = priv;
3331 struct wpa_driver_nl80211_data *drv = bss->drv;
3332 struct nl_msg *msg;
3333 int ret;
3334
3335 msg = nlmsg_alloc();
3336 if (!msg)
3337 return -ENOMEM;
3338
3339 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3340 alpha2[0] = '\0';
3341 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3342 if (!alpha2[0])
3343 ret = -1;
3344
3345 return ret;
3346}
3347
3348
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003349static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3350{
3351 u32 *feat = arg;
3352 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3353 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3354
3355 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3356 genlmsg_attrlen(gnlh, 0), NULL);
3357
3358 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3359 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3360
3361 return NL_SKIP;
3362}
3363
3364
3365static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3366{
3367 u32 feat = 0;
3368 struct nl_msg *msg;
3369
3370 msg = nlmsg_alloc();
3371 if (!msg)
3372 goto nla_put_failure;
3373
3374 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3375 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3376 return feat;
3377
3378 msg = NULL;
3379nla_put_failure:
3380 nlmsg_free(msg);
3381 return 0;
3382}
3383
3384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003386 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003387 struct wpa_driver_capa *capa;
3388
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003389 unsigned int num_multichan_concurrent;
3390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003391 unsigned int error:1;
3392 unsigned int device_ap_sme:1;
3393 unsigned int poll_command_supported:1;
3394 unsigned int data_tx_status:1;
3395 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003396 unsigned int auth_supported:1;
3397 unsigned int connect_supported:1;
3398 unsigned int p2p_go_supported:1;
3399 unsigned int p2p_client_supported:1;
3400 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003401 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003402 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403};
3404
3405
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003406static unsigned int probe_resp_offload_support(int supp_protocols)
3407{
3408 unsigned int prot = 0;
3409
3410 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3411 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3412 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3413 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3414 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3415 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3416 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3417 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3418
3419 return prot;
3420}
3421
3422
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003423static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3424 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003426 struct nlattr *nl_mode;
3427 int i;
3428
3429 if (tb == NULL)
3430 return;
3431
3432 nla_for_each_nested(nl_mode, tb, i) {
3433 switch (nla_type(nl_mode)) {
3434 case NL80211_IFTYPE_AP:
3435 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3436 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003437 case NL80211_IFTYPE_ADHOC:
3438 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3439 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003440 case NL80211_IFTYPE_P2P_DEVICE:
3441 info->capa->flags |=
3442 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3443 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003444 case NL80211_IFTYPE_P2P_GO:
3445 info->p2p_go_supported = 1;
3446 break;
3447 case NL80211_IFTYPE_P2P_CLIENT:
3448 info->p2p_client_supported = 1;
3449 break;
3450 case NL80211_IFTYPE_MONITOR:
3451 info->monitor_supported = 1;
3452 break;
3453 }
3454 }
3455}
3456
3457
3458static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3459 struct nlattr *nl_combi)
3460{
3461 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3462 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3463 struct nlattr *nl_limit, *nl_mode;
3464 int err, rem_limit, rem_mode;
3465 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 static struct nla_policy
3467 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3468 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3469 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3470 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3471 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003472 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003473 },
3474 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3475 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3476 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3477 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003479 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3480 nl_combi, iface_combination_policy);
3481 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3482 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3483 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3484 return 0; /* broken combination */
3485
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003486 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3487 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3488
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003489 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3490 rem_limit) {
3491 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3492 nl_limit, iface_limit_policy);
3493 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3494 return 0; /* broken combination */
3495
3496 nla_for_each_nested(nl_mode,
3497 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3498 rem_mode) {
3499 int ift = nla_type(nl_mode);
3500 if (ift == NL80211_IFTYPE_P2P_GO ||
3501 ift == NL80211_IFTYPE_P2P_CLIENT)
3502 combination_has_p2p = 1;
3503 if (ift == NL80211_IFTYPE_STATION)
3504 combination_has_mgd = 1;
3505 }
3506 if (combination_has_p2p && combination_has_mgd)
3507 break;
3508 }
3509
3510 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003511 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003512 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003513
3514 info->p2p_concurrent = 1;
3515 if (info->num_multichan_concurrent < num_channels)
3516 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003517 }
3518
3519 return 0;
3520}
3521
3522
3523static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3524 struct nlattr *tb)
3525{
3526 struct nlattr *nl_combi;
3527 int rem_combi;
3528
3529 if (tb == NULL)
3530 return;
3531
3532 nla_for_each_nested(nl_combi, tb, rem_combi) {
3533 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3534 break;
3535 }
3536}
3537
3538
3539static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3540 struct nlattr *tb)
3541{
3542 struct nlattr *nl_cmd;
3543 int i;
3544
3545 if (tb == NULL)
3546 return;
3547
3548 nla_for_each_nested(nl_cmd, tb, i) {
3549 switch (nla_get_u32(nl_cmd)) {
3550 case NL80211_CMD_AUTHENTICATE:
3551 info->auth_supported = 1;
3552 break;
3553 case NL80211_CMD_CONNECT:
3554 info->connect_supported = 1;
3555 break;
3556 case NL80211_CMD_START_SCHED_SCAN:
3557 info->capa->sched_scan_supported = 1;
3558 break;
3559 case NL80211_CMD_PROBE_CLIENT:
3560 info->poll_command_supported = 1;
3561 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003562 case NL80211_CMD_CHANNEL_SWITCH:
3563 info->channel_switch_supported = 1;
3564 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003565 case NL80211_CMD_SET_QOS_MAP:
3566 info->set_qos_map_supported = 1;
3567 break;
3568 }
3569 }
3570}
3571
3572
3573static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3574 struct nlattr *tb)
3575{
3576 int i, num;
3577 u32 *ciphers;
3578
3579 if (tb == NULL)
3580 return;
3581
3582 num = nla_len(tb) / sizeof(u32);
3583 ciphers = nla_data(tb);
3584 for (i = 0; i < num; i++) {
3585 u32 c = ciphers[i];
3586
3587 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3588 c >> 24, (c >> 16) & 0xff,
3589 (c >> 8) & 0xff, c & 0xff);
3590 switch (c) {
3591 case WLAN_CIPHER_SUITE_CCMP_256:
3592 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3593 break;
3594 case WLAN_CIPHER_SUITE_GCMP_256:
3595 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3596 break;
3597 case WLAN_CIPHER_SUITE_CCMP:
3598 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3599 break;
3600 case WLAN_CIPHER_SUITE_GCMP:
3601 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3602 break;
3603 case WLAN_CIPHER_SUITE_TKIP:
3604 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3605 break;
3606 case WLAN_CIPHER_SUITE_WEP104:
3607 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3608 break;
3609 case WLAN_CIPHER_SUITE_WEP40:
3610 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3611 break;
3612 case WLAN_CIPHER_SUITE_AES_CMAC:
3613 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3614 break;
3615 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3616 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3617 break;
3618 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3619 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3620 break;
3621 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3622 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3623 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003624 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3625 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3626 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003627 }
3628 }
3629}
3630
3631
3632static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3633 struct nlattr *tb)
3634{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003635 if (tb)
3636 capa->max_remain_on_chan = nla_get_u32(tb);
3637}
3638
3639
3640static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3641 struct nlattr *ext_setup)
3642{
3643 if (tdls == NULL)
3644 return;
3645
3646 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3647 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3648
3649 if (ext_setup) {
3650 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3651 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3652 }
3653}
3654
3655
3656static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3657 struct nlattr *tb)
3658{
3659 u32 flags;
3660 struct wpa_driver_capa *capa = info->capa;
3661
3662 if (tb == NULL)
3663 return;
3664
3665 flags = nla_get_u32(tb);
3666
3667 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3668 info->data_tx_status = 1;
3669
3670 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3671 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3672
3673 if (flags & NL80211_FEATURE_SAE)
3674 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3675
3676 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3677 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003678
3679 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3680 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003681}
3682
3683
3684static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3685 struct nlattr *tb)
3686{
3687 u32 protocols;
3688
3689 if (tb == NULL)
3690 return;
3691
3692 protocols = nla_get_u32(tb);
3693 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3694 "mode");
3695 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3696 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3697}
3698
3699
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003700static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3701 struct nlattr *tb)
3702{
3703 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3704
3705 if (tb == NULL)
3706 return;
3707
3708 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3709 tb, NULL))
3710 return;
3711
3712 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3713 capa->wowlan_triggers.any = 1;
3714 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3715 capa->wowlan_triggers.disconnect = 1;
3716 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3717 capa->wowlan_triggers.magic_pkt = 1;
3718 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3719 capa->wowlan_triggers.gtk_rekey_failure = 1;
3720 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3721 capa->wowlan_triggers.eap_identity_req = 1;
3722 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3723 capa->wowlan_triggers.four_way_handshake = 1;
3724 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3725 capa->wowlan_triggers.rfkill_release = 1;
3726}
3727
3728
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003729static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3730{
3731 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3732 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3733 struct wiphy_info_data *info = arg;
3734 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003735 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3738 genlmsg_attrlen(gnlh, 0), NULL);
3739
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003740 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003741 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003742 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3743 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003745 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003748 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3749 capa->max_sched_scan_ssids =
3750 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3751
3752 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3753 capa->max_match_sets =
3754 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3755
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003756 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3757 capa->max_acl_mac_addrs =
3758 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3759
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003760 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3761 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3762 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003763 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003765 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3766 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3767 "off-channel TX");
3768 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3769 }
3770
3771 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3772 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3773 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3774 }
3775
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003776 wiphy_info_max_roc(capa,
3777 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003778
3779 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3780 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003782 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3783 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003785 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3786 info->device_ap_sme = 1;
3787
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003788 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3789 wiphy_info_probe_resp_offload(capa,
3790 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003791
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003792 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3793 drv->extended_capa == NULL) {
3794 drv->extended_capa =
3795 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3796 if (drv->extended_capa) {
3797 os_memcpy(drv->extended_capa,
3798 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3799 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3800 drv->extended_capa_len =
3801 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3802 }
3803 drv->extended_capa_mask =
3804 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3805 if (drv->extended_capa_mask) {
3806 os_memcpy(drv->extended_capa_mask,
3807 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3808 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3809 } else {
3810 os_free(drv->extended_capa);
3811 drv->extended_capa = NULL;
3812 drv->extended_capa_len = 0;
3813 }
3814 }
3815
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003816 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3817 struct nlattr *nl;
3818 int rem;
3819
3820 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3821 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003822 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003823 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3824 continue;
3825 }
3826 vinfo = nla_data(nl);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003827 if (vinfo->subcmd ==
3828 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)
3829 drv->dfs_vendor_cmd_avail = 1;
3830
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003831 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3832 vinfo->vendor_id, vinfo->subcmd);
3833 }
3834 }
3835
3836 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3837 struct nlattr *nl;
3838 int rem;
3839
3840 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3841 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003842 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003843 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3844 continue;
3845 }
3846 vinfo = nla_data(nl);
3847 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3848 vinfo->vendor_id, vinfo->subcmd);
3849 }
3850 }
3851
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003852 wiphy_info_wowlan_triggers(capa,
3853 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
3854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 return NL_SKIP;
3856}
3857
3858
3859static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3860 struct wiphy_info_data *info)
3861{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003862 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 struct nl_msg *msg;
3864
3865 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003866 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003867 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868
3869 msg = nlmsg_alloc();
3870 if (!msg)
3871 return -1;
3872
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003873 feat = get_nl80211_protocol_features(drv);
3874 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3875 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3876 else
3877 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003879 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003880 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003881 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003883 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3884 return -1;
3885
3886 if (info->auth_supported)
3887 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3888 else if (!info->connect_supported) {
3889 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3890 "authentication/association or connect commands");
3891 info->error = 1;
3892 }
3893
3894 if (info->p2p_go_supported && info->p2p_client_supported)
3895 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3896 if (info->p2p_concurrent) {
3897 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3898 "interface (driver advertised support)");
3899 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3900 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3901 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003902 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003903 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3904 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003905 drv->capa.num_multichan_concurrent =
3906 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003907 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003908
3909 /* default to 5000 since early versions of mac80211 don't set it */
3910 if (!drv->capa.max_remain_on_chan)
3911 drv->capa.max_remain_on_chan = 5000;
3912
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003913 if (info->channel_switch_supported)
3914 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3915
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003916 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917nla_put_failure:
3918 nlmsg_free(msg);
3919 return -1;
3920}
3921
3922
3923static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3924{
3925 struct wiphy_info_data info;
3926 if (wpa_driver_nl80211_get_info(drv, &info))
3927 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003928
3929 if (info.error)
3930 return -1;
3931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3934 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3935 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3936 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3938 WPA_DRIVER_AUTH_SHARED |
3939 WPA_DRIVER_AUTH_LEAP;
3940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3942 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003944
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003945 /*
3946 * As all cfg80211 drivers must support cases where the AP interface is
3947 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
3948 * case that the user space daemon has crashed, they must be able to
3949 * cleanup all stations and key entries in the AP tear down flow. Thus,
3950 * this flag can/should always be set for cfg80211 drivers.
3951 */
3952 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
3953
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003954 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003955 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003956
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003957 /*
3958 * No AP SME is currently assumed to also indicate no AP MLME
3959 * in the driver/firmware.
3960 */
3961 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3962 }
3963
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003964 drv->device_ap_sme = info.device_ap_sme;
3965 drv->poll_command_supported = info.poll_command_supported;
3966 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003967 if (info.set_qos_map_supported)
3968 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003969
3970 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003971 * If poll command and tx status are supported, mac80211 is new enough
3972 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003973 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003974 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003975
3976 if (drv->device_ap_sme && drv->use_monitor) {
3977 /*
3978 * Non-mac80211 drivers may not support monitor interface.
3979 * Make sure we do not get stuck with incorrect capability here
3980 * by explicitly testing this.
3981 */
3982 if (!info.monitor_supported) {
3983 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3984 "with device_ap_sme since no monitor mode "
3985 "support detected");
3986 drv->use_monitor = 0;
3987 }
3988 }
3989
3990 /*
3991 * If we aren't going to use monitor interfaces, but the
3992 * driver doesn't support data TX status, we won't get TX
3993 * status for EAPOL frames.
3994 */
3995 if (!drv->use_monitor && !info.data_tx_status)
3996 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997
3998 return 0;
3999}
4000
4001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004002#ifdef ANDROID
4003static int android_genl_ctrl_resolve(struct nl_handle *handle,
4004 const char *name)
4005{
4006 /*
4007 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4008 * need to work around that.
4009 */
4010 struct nl_cache *cache = NULL;
4011 struct genl_family *nl80211 = NULL;
4012 int id = -1;
4013
4014 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4015 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4016 "netlink cache");
4017 goto fail;
4018 }
4019
4020 nl80211 = genl_ctrl_search_by_name(cache, name);
4021 if (nl80211 == NULL)
4022 goto fail;
4023
4024 id = genl_family_get_id(nl80211);
4025
4026fail:
4027 if (nl80211)
4028 genl_family_put(nl80211);
4029 if (cache)
4030 nl_cache_free(cache);
4031
4032 return id;
4033}
4034#define genl_ctrl_resolve android_genl_ctrl_resolve
4035#endif /* ANDROID */
4036
4037
4038static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039{
4040 int ret;
4041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004042 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4043 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4045 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 }
4048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004049 global->nl = nl_create_handle(global->nl_cb, "nl");
4050 if (global->nl == NULL)
4051 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004053 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4054 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4056 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 }
4059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004060 global->nl_event = nl_create_handle(global->nl_cb, "event");
4061 if (global->nl_event == NULL)
4062 goto err;
4063
4064 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004066 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 if (ret < 0) {
4068 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4069 "membership for scan events: %d (%s)",
4070 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004071 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072 }
4073
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004074 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 if (ret < 0) {
4078 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4079 "membership for mlme events: %d (%s)",
4080 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004081 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 }
4083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004084 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004086 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 if (ret < 0) {
4088 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4089 "membership for regulatory events: %d (%s)",
4090 ret, strerror(-ret));
4091 /* Continue without regulatory events */
4092 }
4093
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004094 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4095 if (ret >= 0)
4096 ret = nl_socket_add_membership(global->nl_event, ret);
4097 if (ret < 0) {
4098 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4099 "membership for vendor events: %d (%s)",
4100 ret, strerror(-ret));
4101 /* Continue without vendor events */
4102 }
4103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004104 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4105 no_seq_check, NULL);
4106 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4107 process_global_event, global);
4108
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004109 nl80211_register_eloop_read(&global->nl_event,
4110 wpa_driver_nl80211_event_receive,
4111 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004112
4113 return 0;
4114
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004115err:
4116 nl_destroy_handles(&global->nl_event);
4117 nl_destroy_handles(&global->nl);
4118 nl_cb_put(global->nl_cb);
4119 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 return -1;
4121}
4122
4123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004124static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4125{
4126 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4127 if (!drv->nl_cb) {
4128 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4129 return -1;
4130 }
4131
4132 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4133 no_seq_check, NULL);
4134 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4135 process_drv_event, drv);
4136
4137 return 0;
4138}
4139
4140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4142{
4143 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4144 /*
4145 * This may be for any interface; use ifdown event to disable
4146 * interface.
4147 */
4148}
4149
4150
4151static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4152{
4153 struct wpa_driver_nl80211_data *drv = ctx;
4154 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004155 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4157 "after rfkill unblock");
4158 return;
4159 }
4160 /* rtnetlink ifup handler will report interface as enabled */
4161}
4162
4163
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004164static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4165 void *eloop_ctx,
4166 void *handle)
4167{
4168 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4169 u8 data[2048];
4170 struct msghdr msg;
4171 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004172 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004173 struct cmsghdr *cmsg;
4174 int res, found_ee = 0, found_wifi = 0, acked = 0;
4175 union wpa_event_data event;
4176
4177 memset(&msg, 0, sizeof(msg));
4178 msg.msg_iov = &entry;
4179 msg.msg_iovlen = 1;
4180 entry.iov_base = data;
4181 entry.iov_len = sizeof(data);
4182 msg.msg_control = &control;
4183 msg.msg_controllen = sizeof(control);
4184
4185 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4186 /* if error or not fitting 802.3 header, return */
4187 if (res < 14)
4188 return;
4189
4190 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4191 {
4192 if (cmsg->cmsg_level == SOL_SOCKET &&
4193 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4194 int *ack;
4195
4196 found_wifi = 1;
4197 ack = (void *)CMSG_DATA(cmsg);
4198 acked = *ack;
4199 }
4200
4201 if (cmsg->cmsg_level == SOL_PACKET &&
4202 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4203 struct sock_extended_err *err =
4204 (struct sock_extended_err *)CMSG_DATA(cmsg);
4205
4206 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4207 found_ee = 1;
4208 }
4209 }
4210
4211 if (!found_ee || !found_wifi)
4212 return;
4213
4214 memset(&event, 0, sizeof(event));
4215 event.eapol_tx_status.dst = data;
4216 event.eapol_tx_status.data = data + 14;
4217 event.eapol_tx_status.data_len = res - 14;
4218 event.eapol_tx_status.ack = acked;
4219 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4220}
4221
4222
4223static int nl80211_init_bss(struct i802_bss *bss)
4224{
4225 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4226 if (!bss->nl_cb)
4227 return -1;
4228
4229 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4230 no_seq_check, NULL);
4231 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4232 process_bss_event, bss);
4233
4234 return 0;
4235}
4236
4237
4238static void nl80211_destroy_bss(struct i802_bss *bss)
4239{
4240 nl_cb_put(bss->nl_cb);
4241 bss->nl_cb = NULL;
4242}
4243
4244
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004245static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4246 void *global_priv, int hostapd,
4247 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248{
4249 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 struct rfkill_config *rcfg;
4251 struct i802_bss *bss;
4252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004253 if (global_priv == NULL)
4254 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255 drv = os_zalloc(sizeof(*drv));
4256 if (drv == NULL)
4257 return NULL;
4258 drv->global = global_priv;
4259 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004260 drv->hostapd = !!hostapd;
4261 drv->eapol_sock = -1;
4262 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4263 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004264
4265 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4266 if (!drv->first_bss) {
4267 os_free(drv);
4268 return NULL;
4269 }
4270 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004272 bss->ctx = ctx;
4273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4275 drv->monitor_ifidx = -1;
4276 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004277 drv->eapol_tx_sock = -1;
4278 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279
4280 if (wpa_driver_nl80211_init_nl(drv)) {
4281 os_free(drv);
4282 return NULL;
4283 }
4284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004285 if (nl80211_init_bss(bss))
4286 goto failed;
4287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 rcfg = os_zalloc(sizeof(*rcfg));
4289 if (rcfg == NULL)
4290 goto failed;
4291 rcfg->ctx = drv;
4292 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4293 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4294 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4295 drv->rfkill = rfkill_init(rcfg);
4296 if (drv->rfkill == NULL) {
4297 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4298 os_free(rcfg);
4299 }
4300
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004301 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4302 drv->start_iface_up = 1;
4303
4304 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305 goto failed;
4306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004307 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4308 if (drv->eapol_tx_sock < 0)
4309 goto failed;
4310
4311 if (drv->data_tx_status) {
4312 int enabled = 1;
4313
4314 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4315 &enabled, sizeof(enabled)) < 0) {
4316 wpa_printf(MSG_DEBUG,
4317 "nl80211: wifi status sockopt failed\n");
4318 drv->data_tx_status = 0;
4319 if (!drv->use_monitor)
4320 drv->capa.flags &=
4321 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4322 } else {
4323 eloop_register_read_sock(drv->eapol_tx_sock,
4324 wpa_driver_nl80211_handle_eapol_tx_status,
4325 drv, NULL);
4326 }
4327 }
4328
4329 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004331 drv->in_interface_list = 1;
4332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333
4334 return bss;
4335
4336failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004337 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 return NULL;
4339}
4340
4341
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004342/**
4343 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4344 * @ctx: context to be used when calling wpa_supplicant functions,
4345 * e.g., wpa_supplicant_event()
4346 * @ifname: interface name, e.g., wlan0
4347 * @global_priv: private driver global data from global_init()
4348 * Returns: Pointer to private data, %NULL on failure
4349 */
4350static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4351 void *global_priv)
4352{
4353 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4354}
4355
4356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 struct nl_handle *nl_handle,
4359 u16 type, const u8 *match, size_t match_len)
4360{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004361 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 struct nl_msg *msg;
4363 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004364 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365
4366 msg = nlmsg_alloc();
4367 if (!msg)
4368 return -1;
4369
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004370 buf[0] = '\0';
4371 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4372 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4373 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004374
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004375 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4376
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004377 if (nl80211_set_iface_id(msg, bss) < 0)
4378 goto nla_put_failure;
4379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004380 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4381 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4382
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 msg = NULL;
4385 if (ret) {
4386 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4387 "failed (type=%u): ret=%d (%s)",
4388 type, ret, strerror(-ret));
4389 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4390 match, match_len);
4391 goto nla_put_failure;
4392 }
4393 ret = 0;
4394nla_put_failure:
4395 nlmsg_free(msg);
4396 return ret;
4397}
4398
4399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004400static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4401{
4402 struct wpa_driver_nl80211_data *drv = bss->drv;
4403
4404 if (bss->nl_mgmt) {
4405 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4406 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4407 return -1;
4408 }
4409
4410 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4411 if (bss->nl_mgmt == NULL)
4412 return -1;
4413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004414 return 0;
4415}
4416
4417
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004418static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4419{
4420 nl80211_register_eloop_read(&bss->nl_mgmt,
4421 wpa_driver_nl80211_event_receive,
4422 bss->nl_cb);
4423}
4424
4425
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004426static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427 const u8 *match, size_t match_len)
4428{
4429 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004430 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 type, match, match_len);
4432}
4433
4434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004435static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004437 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004438 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004439
4440 if (nl80211_alloc_mgmt_handle(bss))
4441 return -1;
4442 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4443 "handle %p", bss->nl_mgmt);
4444
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004445 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4446 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4447
4448 /* register for any AUTH message */
4449 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4450 }
4451
Dmitry Shmidt051af732013-10-22 13:52:46 -07004452#ifdef CONFIG_INTERWORKING
4453 /* QoS Map Configure */
4454 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004455 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004456#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004457#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004459 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004460 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004462 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004463 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004465 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004466 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004467 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004468 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004469 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004470 /* Protected GAS Initial Request */
4471 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4472 ret = -1;
4473 /* Protected GAS Initial Response */
4474 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4475 ret = -1;
4476 /* Protected GAS Comeback Request */
4477 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4478 ret = -1;
4479 /* Protected GAS Comeback Response */
4480 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4481 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004482#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4483#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004485 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4487 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004488 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004490 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 (u8 *) "\x7f\x50\x6f\x9a\x09",
4492 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004493 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494#endif /* CONFIG_P2P */
4495#ifdef CONFIG_IEEE80211W
4496 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004498 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004500#ifdef CONFIG_TDLS
4501 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4502 /* TDLS Discovery Response */
4503 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4504 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004505 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004506 }
4507#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508
4509 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004510 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004511 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 else
4513 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4514 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004516 /* WNM - BSS Transition Management Request */
4517 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004518 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004519 /* WNM-Sleep Mode Response */
4520 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004521 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004522
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004523#ifdef CONFIG_HS20
4524 /* WNM-Notification */
4525 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4526 return -1;
4527#endif /* CONFIG_HS20 */
4528
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004529 nl80211_mgmt_handle_register_eloop(bss);
4530
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004531 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532}
4533
4534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004535static int nl80211_register_spurious_class3(struct i802_bss *bss)
4536{
4537 struct wpa_driver_nl80211_data *drv = bss->drv;
4538 struct nl_msg *msg;
4539 int ret = -1;
4540
4541 msg = nlmsg_alloc();
4542 if (!msg)
4543 return -1;
4544
4545 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4546
4547 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4548
4549 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4550 msg = NULL;
4551 if (ret) {
4552 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4553 "failed: ret=%d (%s)",
4554 ret, strerror(-ret));
4555 goto nla_put_failure;
4556 }
4557 ret = 0;
4558nla_put_failure:
4559 nlmsg_free(msg);
4560 return ret;
4561}
4562
4563
4564static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4565{
4566 static const int stypes[] = {
4567 WLAN_FC_STYPE_AUTH,
4568 WLAN_FC_STYPE_ASSOC_REQ,
4569 WLAN_FC_STYPE_REASSOC_REQ,
4570 WLAN_FC_STYPE_DISASSOC,
4571 WLAN_FC_STYPE_DEAUTH,
4572 WLAN_FC_STYPE_ACTION,
4573 WLAN_FC_STYPE_PROBE_REQ,
4574/* Beacon doesn't work as mac80211 doesn't currently allow
4575 * it, but it wouldn't really be the right thing anyway as
4576 * it isn't per interface ... maybe just dump the scan
4577 * results periodically for OLBC?
4578 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004579 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004580 };
4581 unsigned int i;
4582
4583 if (nl80211_alloc_mgmt_handle(bss))
4584 return -1;
4585 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4586 "handle %p", bss->nl_mgmt);
4587
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004588 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004589 if (nl80211_register_frame(bss, bss->nl_mgmt,
4590 (WLAN_FC_TYPE_MGMT << 2) |
4591 (stypes[i] << 4),
4592 NULL, 0) < 0) {
4593 goto out_err;
4594 }
4595 }
4596
4597 if (nl80211_register_spurious_class3(bss))
4598 goto out_err;
4599
4600 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4601 goto out_err;
4602
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004603 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004604 return 0;
4605
4606out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004607 nl_destroy_handles(&bss->nl_mgmt);
4608 return -1;
4609}
4610
4611
4612static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4613{
4614 if (nl80211_alloc_mgmt_handle(bss))
4615 return -1;
4616 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4617 "handle %p (device SME)", bss->nl_mgmt);
4618
4619 if (nl80211_register_frame(bss, bss->nl_mgmt,
4620 (WLAN_FC_TYPE_MGMT << 2) |
4621 (WLAN_FC_STYPE_ACTION << 4),
4622 NULL, 0) < 0)
4623 goto out_err;
4624
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004625 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004626 return 0;
4627
4628out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004629 nl_destroy_handles(&bss->nl_mgmt);
4630 return -1;
4631}
4632
4633
4634static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4635{
4636 if (bss->nl_mgmt == NULL)
4637 return;
4638 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4639 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004640 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004641
4642 nl80211_put_wiphy_data_ap(bss);
4643}
4644
4645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4647{
4648 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4649}
4650
4651
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004652static void nl80211_del_p2pdev(struct i802_bss *bss)
4653{
4654 struct wpa_driver_nl80211_data *drv = bss->drv;
4655 struct nl_msg *msg;
4656 int ret;
4657
4658 msg = nlmsg_alloc();
4659 if (!msg)
4660 return;
4661
4662 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4663 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4664
4665 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4666 msg = NULL;
4667
4668 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4669 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004670 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004671
4672nla_put_failure:
4673 nlmsg_free(msg);
4674}
4675
4676
4677static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4678{
4679 struct wpa_driver_nl80211_data *drv = bss->drv;
4680 struct nl_msg *msg;
4681 int ret = -1;
4682
4683 msg = nlmsg_alloc();
4684 if (!msg)
4685 return -1;
4686
4687 if (start)
4688 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4689 else
4690 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4691
4692 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4693
4694 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4695 msg = NULL;
4696
4697 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4698 start ? "Start" : "Stop",
4699 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004700 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004701
4702nla_put_failure:
4703 nlmsg_free(msg);
4704 return ret;
4705}
4706
4707
4708static int i802_set_iface_flags(struct i802_bss *bss, int up)
4709{
4710 enum nl80211_iftype nlmode;
4711
4712 nlmode = nl80211_get_ifmode(bss);
4713 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4714 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4715 bss->ifname, up);
4716 }
4717
4718 /* P2P Device has start/stop which is equivalent */
4719 return nl80211_set_p2pdev(bss, up);
4720}
4721
4722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004724wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4725 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004727 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004729 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730
4731 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004732 bss->ifindex = drv->ifindex;
4733 bss->wdev_id = drv->global->if_add_wdevid;
4734 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4735
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004736 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4737 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004738 drv->global->if_add_wdevid_set = 0;
4739
4740 if (wpa_driver_nl80211_capa(drv))
4741 return -1;
4742
4743 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4744 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004746 if (set_addr &&
4747 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4748 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4749 set_addr)))
4750 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004751
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004752 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4753 drv->start_mode_ap = 1;
4754
4755 if (drv->hostapd)
4756 nlmode = NL80211_IFTYPE_AP;
4757 else if (bss->if_dynamic)
4758 nlmode = nl80211_get_ifmode(bss);
4759 else
4760 nlmode = NL80211_IFTYPE_STATION;
4761
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004762 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004763 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004764 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004766
Dmitry Shmidt98660862014-03-11 17:26:21 -07004767 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004768 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769
Dmitry Shmidt98660862014-03-11 17:26:21 -07004770 if (!rfkill_is_blocked(drv->rfkill)) {
4771 int ret = i802_set_iface_flags(bss, 1);
4772 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4774 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004775 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004777 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4778 return ret;
4779 } else {
4780 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4781 "interface '%s' due to rfkill", bss->ifname);
4782 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4783 return 0;
4784 drv->if_disabled = 1;
4785 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786 }
4787
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004788 if (!drv->hostapd)
4789 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4790 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004792 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4793 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794 return -1;
4795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796 if (send_rfkill_event) {
4797 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4798 drv, drv->ctx);
4799 }
4800
4801 return 0;
4802}
4803
4804
4805static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4806{
4807 struct nl_msg *msg;
4808
4809 msg = nlmsg_alloc();
4810 if (!msg)
4811 return -ENOMEM;
4812
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004813 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4814 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004815 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4817
4818 return send_and_recv_msgs(drv, msg, NULL, NULL);
4819 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004820 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821 return -ENOBUFS;
4822}
4823
4824
4825/**
4826 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004827 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 *
4829 * Shut down driver interface and processing of driver events. Free
4830 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4831 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004832static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 struct wpa_driver_nl80211_data *drv = bss->drv;
4835
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004837 if (drv->data_tx_status)
4838 eloop_unregister_read_sock(drv->eapol_tx_sock);
4839 if (drv->eapol_tx_sock >= 0)
4840 close(drv->eapol_tx_sock);
4841
4842 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 wpa_driver_nl80211_probe_req_report(bss, 0);
4844 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004845 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4846 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4848 "interface %s from bridge %s: %s",
4849 bss->ifname, bss->brname, strerror(errno));
4850 }
4851 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004852 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4854 "bridge %s: %s",
4855 bss->brname, strerror(errno));
4856 }
4857
4858 nl80211_remove_monitor_interface(drv);
4859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004860 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004861 wpa_driver_nl80211_del_beacon(drv);
4862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863 if (drv->eapol_sock >= 0) {
4864 eloop_unregister_read_sock(drv->eapol_sock);
4865 close(drv->eapol_sock);
4866 }
4867
4868 if (drv->if_indices != drv->default_if_indices)
4869 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004871 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4873
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004874 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4875 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004876 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004877 rfkill_deinit(drv->rfkill);
4878
4879 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4880
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004881 if (!drv->start_iface_up)
4882 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004883 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004884 if (!drv->hostapd || !drv->start_mode_ap)
4885 wpa_driver_nl80211_set_mode(bss,
4886 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004887 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004888 } else {
4889 nl80211_mgmt_unsubscribe(bss, "deinit");
4890 nl80211_del_p2pdev(bss);
4891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 nl_cb_put(drv->nl_cb);
4893
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004894 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 os_free(drv->filter_ssids);
4897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004898 os_free(drv->auth_ie);
4899
4900 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901 dl_list_del(&drv->list);
4902
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004903 os_free(drv->extended_capa);
4904 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004905 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906 os_free(drv);
4907}
4908
4909
4910/**
4911 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4912 * @eloop_ctx: Driver private data
4913 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4914 *
4915 * This function can be used as registered timeout when starting a scan to
4916 * generate a scan completed event if the driver does not report this.
4917 */
4918static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4919{
4920 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004921 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004922 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004923 drv->ap_scan_as_station);
4924 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 }
4926 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4927 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4928}
4929
4930
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004931static struct nl_msg *
4932nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004933 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004934{
4935 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004936 size_t i;
4937
4938 msg = nlmsg_alloc();
4939 if (!msg)
4940 return NULL;
4941
4942 nl80211_cmd(drv, msg, 0, cmd);
4943
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004944 if (!wdev_id)
4945 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4946 else
4947 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004948
4949 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004950 struct nlattr *ssids;
4951
4952 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004953 if (ssids == NULL)
4954 goto fail;
4955 for (i = 0; i < params->num_ssids; i++) {
4956 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4957 params->ssids[i].ssid,
4958 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004959 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4960 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004961 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004962 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004963 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004964 }
4965
4966 if (params->extra_ies) {
4967 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4968 params->extra_ies, params->extra_ies_len);
4969 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4970 params->extra_ies) < 0)
4971 goto fail;
4972 }
4973
4974 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004975 struct nlattr *freqs;
4976 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004977 if (freqs == NULL)
4978 goto fail;
4979 for (i = 0; params->freqs[i]; i++) {
4980 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4981 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004982 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004983 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004984 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004985 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004986 }
4987
4988 os_free(drv->filter_ssids);
4989 drv->filter_ssids = params->filter_ssids;
4990 params->filter_ssids = NULL;
4991 drv->num_filter_ssids = params->num_filter_ssids;
4992
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004993 if (params->only_new_results) {
4994 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4995 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
4996 NL80211_SCAN_FLAG_FLUSH);
4997 }
4998
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004999 return msg;
5000
5001fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005002nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005003 nlmsg_free(msg);
5004 return NULL;
5005}
5006
5007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008/**
5009 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005010 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 * @params: Scan parameters
5012 * Returns: 0 on success, -1 on failure
5013 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005014static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015 struct wpa_driver_scan_params *params)
5016{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005018 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005019 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005021 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005022 drv->scan_for_auth = 0;
5023
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005024 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5025 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005026 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005029 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005030 struct nlattr *rates;
5031
Dmitry Shmidt04949592012-07-19 12:16:46 -07005032 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5033
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005034 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005035 if (rates == NULL)
5036 goto nla_put_failure;
5037
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005038 /*
5039 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5040 * by masking out everything else apart from the OFDM rates 6,
5041 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5042 * rates are left enabled.
5043 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005044 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005045 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005046 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047
5048 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
5049 }
5050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5052 msg = NULL;
5053 if (ret) {
5054 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5055 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005056 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005057 enum nl80211_iftype old_mode = drv->nlmode;
5058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 /*
5060 * mac80211 does not allow scan requests in AP mode, so
5061 * try to do this in station mode.
5062 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005063 if (wpa_driver_nl80211_set_mode(
5064 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065 goto nla_put_failure;
5066
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005067 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005068 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069 goto nla_put_failure;
5070 }
5071
5072 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005073 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 ret = 0;
5075 } else
5076 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077 }
5078
Dmitry Shmidt56052862013-10-04 10:23:25 -07005079 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 /* Not all drivers generate "scan completed" wireless event, so try to
5081 * read results after a timeout. */
5082 timeout = 10;
5083 if (drv->scan_complete_events) {
5084 /*
5085 * The driver seems to deliver events to notify when scan is
5086 * complete, so use longer timeout to avoid race conditions
5087 * with scanning and following association request.
5088 */
5089 timeout = 30;
5090 }
5091 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5092 "seconds", ret, timeout);
5093 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5094 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5095 drv, drv->ctx);
5096
5097nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005099 return ret;
5100}
5101
5102
5103/**
5104 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5105 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5106 * @params: Scan parameters
5107 * @interval: Interval between scan cycles in milliseconds
5108 * Returns: 0 on success, -1 on failure or if not supported
5109 */
5110static int wpa_driver_nl80211_sched_scan(void *priv,
5111 struct wpa_driver_scan_params *params,
5112 u32 interval)
5113{
5114 struct i802_bss *bss = priv;
5115 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005116 int ret = -1;
5117 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005118 size_t i;
5119
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005120 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5121
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005122#ifdef ANDROID
5123 if (!drv->capa.sched_scan_supported)
5124 return android_pno_start(bss, params);
5125#endif /* ANDROID */
5126
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005127 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5128 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005129 if (!msg)
5130 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005131
5132 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5133
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134 if ((drv->num_filter_ssids &&
5135 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5136 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005137 struct nlattr *match_sets;
5138 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005139 if (match_sets == NULL)
5140 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005141
5142 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005143 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005144 wpa_hexdump_ascii(MSG_MSGDUMP,
5145 "nl80211: Sched scan filter SSID",
5146 drv->filter_ssids[i].ssid,
5147 drv->filter_ssids[i].ssid_len);
5148
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005149 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005150 if (match_set_ssid == NULL)
5151 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005152 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005153 drv->filter_ssids[i].ssid_len,
5154 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005155 if (params->filter_rssi)
5156 NLA_PUT_U32(msg,
5157 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5158 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005159
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005160 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005161 }
5162
Dmitry Shmidt97672262014-02-03 13:02:54 -08005163 /*
5164 * Due to backward compatibility code, newer kernels treat this
5165 * matchset (with only an RSSI filter) as the default for all
5166 * other matchsets, unless it's the only one, in which case the
5167 * matchset will actually allow all SSIDs above the RSSI.
5168 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005169 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005170 struct nlattr *match_set_rssi;
5171 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005172 if (match_set_rssi == NULL)
5173 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005174 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005175 params->filter_rssi);
5176 wpa_printf(MSG_MSGDUMP,
5177 "nl80211: Sched scan RSSI filter %d dBm",
5178 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005179 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005180 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005181
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005182 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005183 }
5184
5185 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5186
5187 /* TODO: if we get an error here, we should fall back to normal scan */
5188
5189 msg = NULL;
5190 if (ret) {
5191 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5192 "ret=%d (%s)", ret, strerror(-ret));
5193 goto nla_put_failure;
5194 }
5195
5196 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5197 "scan interval %d msec", ret, interval);
5198
5199nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005200 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005201 return ret;
5202}
5203
5204
5205/**
5206 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5207 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5208 * Returns: 0 on success, -1 on failure or if not supported
5209 */
5210static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5211{
5212 struct i802_bss *bss = priv;
5213 struct wpa_driver_nl80211_data *drv = bss->drv;
5214 int ret = 0;
5215 struct nl_msg *msg;
5216
5217#ifdef ANDROID
5218 if (!drv->capa.sched_scan_supported)
5219 return android_pno_stop(bss);
5220#endif /* ANDROID */
5221
5222 msg = nlmsg_alloc();
5223 if (!msg)
5224 return -1;
5225
5226 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5227
5228 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5229
5230 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5231 msg = NULL;
5232 if (ret) {
5233 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5234 "ret=%d (%s)", ret, strerror(-ret));
5235 goto nla_put_failure;
5236 }
5237
5238 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5239
5240nla_put_failure:
5241 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 return ret;
5243}
5244
5245
5246static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5247{
5248 const u8 *end, *pos;
5249
5250 if (ies == NULL)
5251 return NULL;
5252
5253 pos = ies;
5254 end = ies + ies_len;
5255
5256 while (pos + 1 < end) {
5257 if (pos + 2 + pos[1] > end)
5258 break;
5259 if (pos[0] == ie)
5260 return pos;
5261 pos += 2 + pos[1];
5262 }
5263
5264 return NULL;
5265}
5266
5267
5268static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5269 const u8 *ie, size_t ie_len)
5270{
5271 const u8 *ssid;
5272 size_t i;
5273
5274 if (drv->filter_ssids == NULL)
5275 return 0;
5276
5277 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5278 if (ssid == NULL)
5279 return 1;
5280
5281 for (i = 0; i < drv->num_filter_ssids; i++) {
5282 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5283 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5284 0)
5285 return 0;
5286 }
5287
5288 return 1;
5289}
5290
5291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292static int bss_info_handler(struct nl_msg *msg, void *arg)
5293{
5294 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5295 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5296 struct nlattr *bss[NL80211_BSS_MAX + 1];
5297 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5298 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5299 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5300 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5301 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5302 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5303 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5304 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5305 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5306 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5307 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5308 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5309 };
5310 struct nl80211_bss_info_arg *_arg = arg;
5311 struct wpa_scan_results *res = _arg->res;
5312 struct wpa_scan_res **tmp;
5313 struct wpa_scan_res *r;
5314 const u8 *ie, *beacon_ie;
5315 size_t ie_len, beacon_ie_len;
5316 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005317 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318
5319 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5320 genlmsg_attrlen(gnlh, 0), NULL);
5321 if (!tb[NL80211_ATTR_BSS])
5322 return NL_SKIP;
5323 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5324 bss_policy))
5325 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005326 if (bss[NL80211_BSS_STATUS]) {
5327 enum nl80211_bss_status status;
5328 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5329 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5330 bss[NL80211_BSS_FREQUENCY]) {
5331 _arg->assoc_freq =
5332 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5333 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5334 _arg->assoc_freq);
5335 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005336 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5337 bss[NL80211_BSS_BSSID]) {
5338 os_memcpy(_arg->assoc_bssid,
5339 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5340 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5341 MACSTR, MAC2STR(_arg->assoc_bssid));
5342 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005343 }
5344 if (!res)
5345 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5347 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5348 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5349 } else {
5350 ie = NULL;
5351 ie_len = 0;
5352 }
5353 if (bss[NL80211_BSS_BEACON_IES]) {
5354 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5355 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5356 } else {
5357 beacon_ie = NULL;
5358 beacon_ie_len = 0;
5359 }
5360
5361 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5362 ie ? ie_len : beacon_ie_len))
5363 return NL_SKIP;
5364
5365 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5366 if (r == NULL)
5367 return NL_SKIP;
5368 if (bss[NL80211_BSS_BSSID])
5369 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5370 ETH_ALEN);
5371 if (bss[NL80211_BSS_FREQUENCY])
5372 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5373 if (bss[NL80211_BSS_BEACON_INTERVAL])
5374 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5375 if (bss[NL80211_BSS_CAPABILITY])
5376 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5377 r->flags |= WPA_SCAN_NOISE_INVALID;
5378 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5379 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5380 r->level /= 100; /* mBm to dBm */
5381 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5382 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5383 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005384 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385 } else
5386 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5387 if (bss[NL80211_BSS_TSF])
5388 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5389 if (bss[NL80211_BSS_SEEN_MS_AGO])
5390 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5391 r->ie_len = ie_len;
5392 pos = (u8 *) (r + 1);
5393 if (ie) {
5394 os_memcpy(pos, ie, ie_len);
5395 pos += ie_len;
5396 }
5397 r->beacon_ie_len = beacon_ie_len;
5398 if (beacon_ie)
5399 os_memcpy(pos, beacon_ie, beacon_ie_len);
5400
5401 if (bss[NL80211_BSS_STATUS]) {
5402 enum nl80211_bss_status status;
5403 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5404 switch (status) {
5405 case NL80211_BSS_STATUS_AUTHENTICATED:
5406 r->flags |= WPA_SCAN_AUTHENTICATED;
5407 break;
5408 case NL80211_BSS_STATUS_ASSOCIATED:
5409 r->flags |= WPA_SCAN_ASSOCIATED;
5410 break;
5411 default:
5412 break;
5413 }
5414 }
5415
Jouni Malinen87fd2792011-05-16 18:35:42 +03005416 /*
5417 * cfg80211 maintains separate BSS table entries for APs if the same
5418 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5419 * not use frequency as a separate key in the BSS table, so filter out
5420 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005421 * order to get the correct frequency into the BSS table. Similarly,
5422 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005423 */
5424 for (i = 0; i < res->num; i++) {
5425 const u8 *s1, *s2;
5426 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5427 continue;
5428
5429 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5430 res->res[i]->ie_len, WLAN_EID_SSID);
5431 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5432 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5433 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5434 continue;
5435
5436 /* Same BSSID,SSID was already included in scan results */
5437 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5438 "for " MACSTR, MAC2STR(r->bssid));
5439
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005440 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5441 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5442 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005443 os_free(res->res[i]);
5444 res->res[i] = r;
5445 } else
5446 os_free(r);
5447 return NL_SKIP;
5448 }
5449
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005450 tmp = os_realloc_array(res->res, res->num + 1,
5451 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 if (tmp == NULL) {
5453 os_free(r);
5454 return NL_SKIP;
5455 }
5456 tmp[res->num++] = r;
5457 res->res = tmp;
5458
5459 return NL_SKIP;
5460}
5461
5462
5463static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5464 const u8 *addr)
5465{
5466 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5467 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5468 "mismatch (" MACSTR ")", MAC2STR(addr));
5469 wpa_driver_nl80211_mlme(drv, addr,
5470 NL80211_CMD_DEAUTHENTICATE,
5471 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5472 }
5473}
5474
5475
5476static void wpa_driver_nl80211_check_bss_status(
5477 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5478{
5479 size_t i;
5480
5481 for (i = 0; i < res->num; i++) {
5482 struct wpa_scan_res *r = res->res[i];
5483 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5484 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5485 "indicates BSS status with " MACSTR
5486 " as authenticated",
5487 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005488 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005489 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5490 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5491 0) {
5492 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5493 " in local state (auth=" MACSTR
5494 " assoc=" MACSTR ")",
5495 MAC2STR(drv->auth_bssid),
5496 MAC2STR(drv->bssid));
5497 clear_state_mismatch(drv, r->bssid);
5498 }
5499 }
5500
5501 if (r->flags & WPA_SCAN_ASSOCIATED) {
5502 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5503 "indicate BSS status with " MACSTR
5504 " as associated",
5505 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005506 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 !drv->associated) {
5508 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5509 "(not associated) does not match "
5510 "with BSS state");
5511 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005512 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5514 0) {
5515 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5516 "(associated with " MACSTR ") does "
5517 "not match with BSS state",
5518 MAC2STR(drv->bssid));
5519 clear_state_mismatch(drv, r->bssid);
5520 clear_state_mismatch(drv, drv->bssid);
5521 }
5522 }
5523 }
5524}
5525
5526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527static struct wpa_scan_results *
5528nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5529{
5530 struct nl_msg *msg;
5531 struct wpa_scan_results *res;
5532 int ret;
5533 struct nl80211_bss_info_arg arg;
5534
5535 res = os_zalloc(sizeof(*res));
5536 if (res == NULL)
5537 return NULL;
5538 msg = nlmsg_alloc();
5539 if (!msg)
5540 goto nla_put_failure;
5541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005542 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005543 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005544 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545
5546 arg.drv = drv;
5547 arg.res = res;
5548 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5549 msg = NULL;
5550 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005551 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5552 "BSSes)", (unsigned long) res->num);
5553 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005554 return res;
5555 }
5556 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5557 "(%s)", ret, strerror(-ret));
5558nla_put_failure:
5559 nlmsg_free(msg);
5560 wpa_scan_results_free(res);
5561 return NULL;
5562}
5563
5564
5565/**
5566 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5567 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5568 * Returns: Scan results on success, -1 on failure
5569 */
5570static struct wpa_scan_results *
5571wpa_driver_nl80211_get_scan_results(void *priv)
5572{
5573 struct i802_bss *bss = priv;
5574 struct wpa_driver_nl80211_data *drv = bss->drv;
5575 struct wpa_scan_results *res;
5576
5577 res = nl80211_get_scan_results(drv);
5578 if (res)
5579 wpa_driver_nl80211_check_bss_status(drv, res);
5580 return res;
5581}
5582
5583
5584static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5585{
5586 struct wpa_scan_results *res;
5587 size_t i;
5588
5589 res = nl80211_get_scan_results(drv);
5590 if (res == NULL) {
5591 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5592 return;
5593 }
5594
5595 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5596 for (i = 0; i < res->num; i++) {
5597 struct wpa_scan_res *r = res->res[i];
5598 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5599 (int) i, (int) res->num, MAC2STR(r->bssid),
5600 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5601 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5602 }
5603
5604 wpa_scan_results_free(res);
5605}
5606
5607
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005608static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5609{
5610 switch (alg) {
5611 case WPA_ALG_WEP:
5612 if (key_len == 5)
5613 return WLAN_CIPHER_SUITE_WEP40;
5614 return WLAN_CIPHER_SUITE_WEP104;
5615 case WPA_ALG_TKIP:
5616 return WLAN_CIPHER_SUITE_TKIP;
5617 case WPA_ALG_CCMP:
5618 return WLAN_CIPHER_SUITE_CCMP;
5619 case WPA_ALG_GCMP:
5620 return WLAN_CIPHER_SUITE_GCMP;
5621 case WPA_ALG_CCMP_256:
5622 return WLAN_CIPHER_SUITE_CCMP_256;
5623 case WPA_ALG_GCMP_256:
5624 return WLAN_CIPHER_SUITE_GCMP_256;
5625 case WPA_ALG_IGTK:
5626 return WLAN_CIPHER_SUITE_AES_CMAC;
5627 case WPA_ALG_BIP_GMAC_128:
5628 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5629 case WPA_ALG_BIP_GMAC_256:
5630 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5631 case WPA_ALG_BIP_CMAC_256:
5632 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5633 case WPA_ALG_SMS4:
5634 return WLAN_CIPHER_SUITE_SMS4;
5635 case WPA_ALG_KRK:
5636 return WLAN_CIPHER_SUITE_KRK;
5637 case WPA_ALG_NONE:
5638 case WPA_ALG_PMK:
5639 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5640 alg);
5641 return 0;
5642 }
5643
5644 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5645 alg);
5646 return 0;
5647}
5648
5649
5650static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5651{
5652 switch (cipher) {
5653 case WPA_CIPHER_CCMP_256:
5654 return WLAN_CIPHER_SUITE_CCMP_256;
5655 case WPA_CIPHER_GCMP_256:
5656 return WLAN_CIPHER_SUITE_GCMP_256;
5657 case WPA_CIPHER_CCMP:
5658 return WLAN_CIPHER_SUITE_CCMP;
5659 case WPA_CIPHER_GCMP:
5660 return WLAN_CIPHER_SUITE_GCMP;
5661 case WPA_CIPHER_TKIP:
5662 return WLAN_CIPHER_SUITE_TKIP;
5663 case WPA_CIPHER_WEP104:
5664 return WLAN_CIPHER_SUITE_WEP104;
5665 case WPA_CIPHER_WEP40:
5666 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005667 case WPA_CIPHER_GTK_NOT_USED:
5668 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005669 }
5670
5671 return 0;
5672}
5673
5674
5675static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5676 int max_suites)
5677{
5678 int num_suites = 0;
5679
5680 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5681 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5682 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5683 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5684 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5685 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5686 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5687 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5688 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5689 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5690 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5691 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5692 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5693 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5694
5695 return num_suites;
5696}
5697
5698
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005699static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700 enum wpa_alg alg, const u8 *addr,
5701 int key_idx, int set_tx,
5702 const u8 *seq, size_t seq_len,
5703 const u8 *key, size_t key_len)
5704{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005706 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707 struct nl_msg *msg;
5708 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005709 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005711 /* Ignore for P2P Device */
5712 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5713 return 0;
5714
5715 ifindex = if_nametoindex(ifname);
5716 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005718 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005719 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005720#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005721 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005722 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005723 tdls = 1;
5724 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005725#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726
5727 msg = nlmsg_alloc();
5728 if (!msg)
5729 return -ENOMEM;
5730
5731 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005732 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005734 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005735 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005736 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005737 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5738 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739 }
5740
Dmitry Shmidt98660862014-03-11 17:26:21 -07005741 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005743 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5744 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745
5746 if (addr && !is_broadcast_ether_addr(addr)) {
5747 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5748 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5749
5750 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5751 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5752 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5753 NL80211_KEYTYPE_GROUP);
5754 }
5755 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005756 struct nlattr *types;
5757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005759
5760 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 if (!types)
5762 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005763 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5764 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005765 }
5766 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5767 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5768
5769 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5770 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5771 ret = 0;
5772 if (ret)
5773 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5774 ret, strerror(-ret));
5775
5776 /*
5777 * If we failed or don't need to set the default TX key (below),
5778 * we're done here.
5779 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005780 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005782 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783 !is_broadcast_ether_addr(addr))
5784 return ret;
5785
5786 msg = nlmsg_alloc();
5787 if (!msg)
5788 return -ENOMEM;
5789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005790 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5793 if (alg == WPA_ALG_IGTK)
5794 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5795 else
5796 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5797 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005798 struct nlattr *types;
5799
5800 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005801 if (!types)
5802 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005803 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5804 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005806 struct nlattr *types;
5807
5808 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809 if (!types)
5810 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005811 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5812 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813 }
5814
5815 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5816 if (ret == -ENOENT)
5817 ret = 0;
5818 if (ret)
5819 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5820 "err=%d %s)", ret, strerror(-ret));
5821 return ret;
5822
5823nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 return -ENOBUFS;
5826}
5827
5828
5829static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5830 int key_idx, int defkey,
5831 const u8 *seq, size_t seq_len,
5832 const u8 *key, size_t key_len)
5833{
5834 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5835 if (!key_attr)
5836 return -1;
5837
5838 if (defkey && alg == WPA_ALG_IGTK)
5839 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5840 else if (defkey)
5841 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5842
5843 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5844
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005845 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5846 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847
5848 if (seq && seq_len)
5849 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5850
5851 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5852
5853 nla_nest_end(msg, key_attr);
5854
5855 return 0;
5856 nla_put_failure:
5857 return -1;
5858}
5859
5860
5861static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5862 struct nl_msg *msg)
5863{
5864 int i, privacy = 0;
5865 struct nlattr *nl_keys, *nl_key;
5866
5867 for (i = 0; i < 4; i++) {
5868 if (!params->wep_key[i])
5869 continue;
5870 privacy = 1;
5871 break;
5872 }
5873 if (params->wps == WPS_MODE_PRIVACY)
5874 privacy = 1;
5875 if (params->pairwise_suite &&
5876 params->pairwise_suite != WPA_CIPHER_NONE)
5877 privacy = 1;
5878
5879 if (!privacy)
5880 return 0;
5881
5882 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5883
5884 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5885 if (!nl_keys)
5886 goto nla_put_failure;
5887
5888 for (i = 0; i < 4; i++) {
5889 if (!params->wep_key[i])
5890 continue;
5891
5892 nl_key = nla_nest_start(msg, i);
5893 if (!nl_key)
5894 goto nla_put_failure;
5895
5896 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5897 params->wep_key[i]);
5898 if (params->wep_key_len[i] == 5)
5899 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5900 WLAN_CIPHER_SUITE_WEP40);
5901 else
5902 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5903 WLAN_CIPHER_SUITE_WEP104);
5904
5905 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5906
5907 if (i == params->wep_tx_keyidx)
5908 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5909
5910 nla_nest_end(msg, nl_key);
5911 }
5912 nla_nest_end(msg, nl_keys);
5913
5914 return 0;
5915
5916nla_put_failure:
5917 return -ENOBUFS;
5918}
5919
5920
5921static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5922 const u8 *addr, int cmd, u16 reason_code,
5923 int local_state_change)
5924{
5925 int ret = -1;
5926 struct nl_msg *msg;
5927
5928 msg = nlmsg_alloc();
5929 if (!msg)
5930 return -1;
5931
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005932 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933
5934 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5935 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005936 if (addr)
5937 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005938 if (local_state_change)
5939 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5940
5941 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5942 msg = NULL;
5943 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005944 wpa_dbg(drv->ctx, MSG_DEBUG,
5945 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5946 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947 goto nla_put_failure;
5948 }
5949 ret = 0;
5950
5951nla_put_failure:
5952 nlmsg_free(msg);
5953 return ret;
5954}
5955
5956
5957static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005958 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005960 int ret;
5961
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005962 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005963 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005964 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005965 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5966 reason_code, 0);
5967 /*
5968 * For locally generated disconnect, supplicant already generates a
5969 * DEAUTH event, so ignore the event from NL80211.
5970 */
5971 drv->ignore_next_local_disconnect = ret == 0;
5972
5973 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974}
5975
5976
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005977static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5978 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005981 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07005982
5983 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
5984 nl80211_mark_disconnected(drv);
5985 return nl80211_leave_ibss(drv);
5986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005988 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005989 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5990 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005991 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005992 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5993 reason_code, 0);
5994 /*
5995 * For locally generated deauthenticate, supplicant already generates a
5996 * DEAUTH event, so ignore the event from NL80211.
5997 */
5998 drv->ignore_next_local_deauth = ret == 0;
5999 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000}
6001
6002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006003static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6004 struct wpa_driver_auth_params *params)
6005{
6006 int i;
6007
6008 drv->auth_freq = params->freq;
6009 drv->auth_alg = params->auth_alg;
6010 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6011 drv->auth_local_state_change = params->local_state_change;
6012 drv->auth_p2p = params->p2p;
6013
6014 if (params->bssid)
6015 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6016 else
6017 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6018
6019 if (params->ssid) {
6020 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6021 drv->auth_ssid_len = params->ssid_len;
6022 } else
6023 drv->auth_ssid_len = 0;
6024
6025
6026 os_free(drv->auth_ie);
6027 drv->auth_ie = NULL;
6028 drv->auth_ie_len = 0;
6029 if (params->ie) {
6030 drv->auth_ie = os_malloc(params->ie_len);
6031 if (drv->auth_ie) {
6032 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6033 drv->auth_ie_len = params->ie_len;
6034 }
6035 }
6036
6037 for (i = 0; i < 4; i++) {
6038 if (params->wep_key[i] && params->wep_key_len[i] &&
6039 params->wep_key_len[i] <= 16) {
6040 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6041 params->wep_key_len[i]);
6042 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6043 } else
6044 drv->auth_wep_key_len[i] = 0;
6045 }
6046}
6047
6048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006049static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006050 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006051{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006052 struct wpa_driver_nl80211_data *drv = bss->drv;
6053 int ret = -1, i;
6054 struct nl_msg *msg;
6055 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006056 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006057 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006058 int is_retry;
6059
6060 is_retry = drv->retry_auth;
6061 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006062 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006064 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006066 if (params->bssid)
6067 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6068 else
6069 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006071 nlmode = params->p2p ?
6072 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6073 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006074 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006075 return -1;
6076
6077retry:
6078 msg = nlmsg_alloc();
6079 if (!msg)
6080 return -1;
6081
6082 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6083 drv->ifindex);
6084
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006085 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086
6087 for (i = 0; i < 4; i++) {
6088 if (!params->wep_key[i])
6089 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006090 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006091 NULL, i,
6092 i == params->wep_tx_keyidx, NULL, 0,
6093 params->wep_key[i],
6094 params->wep_key_len[i]);
6095 if (params->wep_tx_keyidx != i)
6096 continue;
6097 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6098 params->wep_key[i], params->wep_key_len[i])) {
6099 nlmsg_free(msg);
6100 return -1;
6101 }
6102 }
6103
6104 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6105 if (params->bssid) {
6106 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6107 MAC2STR(params->bssid));
6108 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6109 }
6110 if (params->freq) {
6111 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6112 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6113 }
6114 if (params->ssid) {
6115 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6116 params->ssid, params->ssid_len);
6117 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6118 params->ssid);
6119 }
6120 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6121 if (params->ie)
6122 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006123 if (params->sae_data) {
6124 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6125 params->sae_data_len);
6126 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6127 params->sae_data);
6128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6130 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6131 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6132 type = NL80211_AUTHTYPE_SHARED_KEY;
6133 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6134 type = NL80211_AUTHTYPE_NETWORK_EAP;
6135 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6136 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006137 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6138 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139 else
6140 goto nla_put_failure;
6141 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6142 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6143 if (params->local_state_change) {
6144 wpa_printf(MSG_DEBUG, " * Local state change only");
6145 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6146 }
6147
6148 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6149 msg = NULL;
6150 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006151 wpa_dbg(drv->ctx, MSG_DEBUG,
6152 "nl80211: MLME command failed (auth): ret=%d (%s)",
6153 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 count++;
6155 if (ret == -EALREADY && count == 1 && params->bssid &&
6156 !params->local_state_change) {
6157 /*
6158 * mac80211 does not currently accept new
6159 * authentication if we are already authenticated. As a
6160 * workaround, force deauthentication and try again.
6161 */
6162 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6163 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006164 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165 wpa_driver_nl80211_deauthenticate(
6166 bss, params->bssid,
6167 WLAN_REASON_PREV_AUTH_NOT_VALID);
6168 nlmsg_free(msg);
6169 goto retry;
6170 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006171
6172 if (ret == -ENOENT && params->freq && !is_retry) {
6173 /*
6174 * cfg80211 has likely expired the BSS entry even
6175 * though it was previously available in our internal
6176 * BSS table. To recover quickly, start a single
6177 * channel scan on the specified channel.
6178 */
6179 struct wpa_driver_scan_params scan;
6180 int freqs[2];
6181
6182 os_memset(&scan, 0, sizeof(scan));
6183 scan.num_ssids = 1;
6184 if (params->ssid) {
6185 scan.ssids[0].ssid = params->ssid;
6186 scan.ssids[0].ssid_len = params->ssid_len;
6187 }
6188 freqs[0] = params->freq;
6189 freqs[1] = 0;
6190 scan.freqs = freqs;
6191 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6192 "channel scan to refresh cfg80211 BSS "
6193 "entry");
6194 ret = wpa_driver_nl80211_scan(bss, &scan);
6195 if (ret == 0) {
6196 nl80211_copy_auth_params(drv, params);
6197 drv->scan_for_auth = 1;
6198 }
6199 } else if (is_retry) {
6200 /*
6201 * Need to indicate this with an event since the return
6202 * value from the retry is not delivered to core code.
6203 */
6204 union wpa_event_data event;
6205 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6206 "failed");
6207 os_memset(&event, 0, sizeof(event));
6208 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6209 ETH_ALEN);
6210 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6211 &event);
6212 }
6213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214 goto nla_put_failure;
6215 }
6216 ret = 0;
6217 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6218 "successfully");
6219
6220nla_put_failure:
6221 nlmsg_free(msg);
6222 return ret;
6223}
6224
6225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006226static int wpa_driver_nl80211_authenticate_retry(
6227 struct wpa_driver_nl80211_data *drv)
6228{
6229 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006230 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006231 int i;
6232
6233 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6234
6235 os_memset(&params, 0, sizeof(params));
6236 params.freq = drv->auth_freq;
6237 params.auth_alg = drv->auth_alg;
6238 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6239 params.local_state_change = drv->auth_local_state_change;
6240 params.p2p = drv->auth_p2p;
6241
6242 if (!is_zero_ether_addr(drv->auth_bssid_))
6243 params.bssid = drv->auth_bssid_;
6244
6245 if (drv->auth_ssid_len) {
6246 params.ssid = drv->auth_ssid;
6247 params.ssid_len = drv->auth_ssid_len;
6248 }
6249
6250 params.ie = drv->auth_ie;
6251 params.ie_len = drv->auth_ie_len;
6252
6253 for (i = 0; i < 4; i++) {
6254 if (drv->auth_wep_key_len[i]) {
6255 params.wep_key[i] = drv->auth_wep_key[i];
6256 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6257 }
6258 }
6259
6260 drv->retry_auth = 1;
6261 return wpa_driver_nl80211_authenticate(bss, &params);
6262}
6263
6264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265struct phy_info_arg {
6266 u16 *num_modes;
6267 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006268 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269};
6270
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006271static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6272 struct nlattr *ampdu_factor,
6273 struct nlattr *ampdu_density,
6274 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006276 if (capa)
6277 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006278
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006279 if (ampdu_factor)
6280 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006282 if (ampdu_density)
6283 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6284
6285 if (mcs_set && nla_len(mcs_set) >= 16) {
6286 u8 *mcs;
6287 mcs = nla_data(mcs_set);
6288 os_memcpy(mode->mcs_set, mcs, 16);
6289 }
6290}
6291
6292
6293static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6294 struct nlattr *capa,
6295 struct nlattr *mcs_set)
6296{
6297 if (capa)
6298 mode->vht_capab = nla_get_u32(capa);
6299
6300 if (mcs_set && nla_len(mcs_set) >= 8) {
6301 u8 *mcs;
6302 mcs = nla_data(mcs_set);
6303 os_memcpy(mode->vht_mcs_set, mcs, 8);
6304 }
6305}
6306
6307
6308static void phy_info_freq(struct hostapd_hw_modes *mode,
6309 struct hostapd_channel_data *chan,
6310 struct nlattr *tb_freq[])
6311{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006312 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006313 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6314 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006315 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006316 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6317 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006318
6319 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6320 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006321 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6322 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006323 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6324 chan->flag |= HOSTAPD_CHAN_RADAR;
6325
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006326 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6327 enum nl80211_dfs_state state =
6328 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6329
6330 switch (state) {
6331 case NL80211_DFS_USABLE:
6332 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6333 break;
6334 case NL80211_DFS_AVAILABLE:
6335 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6336 break;
6337 case NL80211_DFS_UNAVAILABLE:
6338 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6339 break;
6340 }
6341 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006342
6343 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6344 chan->dfs_cac_ms = nla_get_u32(
6345 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6346 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006347}
6348
6349
6350static int phy_info_freqs(struct phy_info_arg *phy_info,
6351 struct hostapd_hw_modes *mode, struct nlattr *tb)
6352{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6354 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6355 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006356 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6358 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006359 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006361 int new_channels = 0;
6362 struct hostapd_channel_data *channel;
6363 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006365 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006366
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006367 if (tb == NULL)
6368 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006369
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006370 nla_for_each_nested(nl_freq, tb, rem_freq) {
6371 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6372 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6373 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6374 continue;
6375 new_channels++;
6376 }
6377
6378 channel = os_realloc_array(mode->channels,
6379 mode->num_channels + new_channels,
6380 sizeof(struct hostapd_channel_data));
6381 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006382 return NL_SKIP;
6383
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006384 mode->channels = channel;
6385 mode->num_channels += new_channels;
6386
6387 idx = phy_info->last_chan_idx;
6388
6389 nla_for_each_nested(nl_freq, tb, rem_freq) {
6390 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6391 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6392 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6393 continue;
6394 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6395 idx++;
6396 }
6397 phy_info->last_chan_idx = idx;
6398
6399 return NL_OK;
6400}
6401
6402
6403static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6404{
6405 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6406 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6407 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6408 { .type = NLA_FLAG },
6409 };
6410 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6411 struct nlattr *nl_rate;
6412 int rem_rate, idx;
6413
6414 if (tb == NULL)
6415 return NL_OK;
6416
6417 nla_for_each_nested(nl_rate, tb, rem_rate) {
6418 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6419 nla_data(nl_rate), nla_len(nl_rate),
6420 rate_policy);
6421 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6422 continue;
6423 mode->num_rates++;
6424 }
6425
6426 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6427 if (!mode->rates)
6428 return NL_SKIP;
6429
6430 idx = 0;
6431
6432 nla_for_each_nested(nl_rate, tb, rem_rate) {
6433 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6434 nla_data(nl_rate), nla_len(nl_rate),
6435 rate_policy);
6436 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6437 continue;
6438 mode->rates[idx] = nla_get_u32(
6439 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006440 idx++;
6441 }
6442
6443 return NL_OK;
6444}
6445
6446
6447static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6448{
6449 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6450 struct hostapd_hw_modes *mode;
6451 int ret;
6452
6453 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006454 mode = os_realloc_array(phy_info->modes,
6455 *phy_info->num_modes + 1,
6456 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457 if (!mode)
6458 return NL_SKIP;
6459 phy_info->modes = mode;
6460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006461 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006462 os_memset(mode, 0, sizeof(*mode));
6463 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006464 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6465 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6466
6467 /*
6468 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6469 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6470 * possible streams as unsupported. This will be overridden if
6471 * driver advertises VHT support.
6472 */
6473 mode->vht_mcs_set[0] = 0xff;
6474 mode->vht_mcs_set[1] = 0xff;
6475 mode->vht_mcs_set[4] = 0xff;
6476 mode->vht_mcs_set[5] = 0xff;
6477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006479 phy_info->last_mode = nl_band->nla_type;
6480 phy_info->last_chan_idx = 0;
6481 } else
6482 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006484 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6485 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006486
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006487 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6488 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6489 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6490 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6491 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6492 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6493 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6494 if (ret != NL_OK)
6495 return ret;
6496 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6497 if (ret != NL_OK)
6498 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006500 return NL_OK;
6501}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006504static int phy_info_handler(struct nl_msg *msg, void *arg)
6505{
6506 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6507 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6508 struct phy_info_arg *phy_info = arg;
6509 struct nlattr *nl_band;
6510 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006511
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006512 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6513 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006514
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006515 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6516 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006517
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006518 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6519 {
6520 int res = phy_info_band(phy_info, nl_band);
6521 if (res != NL_OK)
6522 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 }
6524
6525 return NL_SKIP;
6526}
6527
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006530wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6531 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532{
6533 u16 m;
6534 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6535 int i, mode11g_idx = -1;
6536
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006537 /* heuristic to set up modes */
6538 for (m = 0; m < *num_modes; m++) {
6539 if (!modes[m].num_channels)
6540 continue;
6541 if (modes[m].channels[0].freq < 4000) {
6542 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6543 for (i = 0; i < modes[m].num_rates; i++) {
6544 if (modes[m].rates[i] > 200) {
6545 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6546 break;
6547 }
6548 }
6549 } else if (modes[m].channels[0].freq > 50000)
6550 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6551 else
6552 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6553 }
6554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555 /* If only 802.11g mode is included, use it to construct matching
6556 * 802.11b mode data. */
6557
6558 for (m = 0; m < *num_modes; m++) {
6559 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6560 return modes; /* 802.11b already included */
6561 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6562 mode11g_idx = m;
6563 }
6564
6565 if (mode11g_idx < 0)
6566 return modes; /* 2.4 GHz band not supported at all */
6567
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006568 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006569 if (nmodes == NULL)
6570 return modes; /* Could not add 802.11b mode */
6571
6572 mode = &nmodes[*num_modes];
6573 os_memset(mode, 0, sizeof(*mode));
6574 (*num_modes)++;
6575 modes = nmodes;
6576
6577 mode->mode = HOSTAPD_MODE_IEEE80211B;
6578
6579 mode11g = &modes[mode11g_idx];
6580 mode->num_channels = mode11g->num_channels;
6581 mode->channels = os_malloc(mode11g->num_channels *
6582 sizeof(struct hostapd_channel_data));
6583 if (mode->channels == NULL) {
6584 (*num_modes)--;
6585 return modes; /* Could not add 802.11b mode */
6586 }
6587 os_memcpy(mode->channels, mode11g->channels,
6588 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6589
6590 mode->num_rates = 0;
6591 mode->rates = os_malloc(4 * sizeof(int));
6592 if (mode->rates == NULL) {
6593 os_free(mode->channels);
6594 (*num_modes)--;
6595 return modes; /* Could not add 802.11b mode */
6596 }
6597
6598 for (i = 0; i < mode11g->num_rates; i++) {
6599 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6600 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6601 continue;
6602 mode->rates[mode->num_rates] = mode11g->rates[i];
6603 mode->num_rates++;
6604 if (mode->num_rates == 4)
6605 break;
6606 }
6607
6608 if (mode->num_rates == 0) {
6609 os_free(mode->channels);
6610 os_free(mode->rates);
6611 (*num_modes)--;
6612 return modes; /* No 802.11b rates */
6613 }
6614
6615 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6616 "information");
6617
6618 return modes;
6619}
6620
6621
6622static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6623 int end)
6624{
6625 int c;
6626
6627 for (c = 0; c < mode->num_channels; c++) {
6628 struct hostapd_channel_data *chan = &mode->channels[c];
6629 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6630 chan->flag |= HOSTAPD_CHAN_HT40;
6631 }
6632}
6633
6634
6635static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6636 int end)
6637{
6638 int c;
6639
6640 for (c = 0; c < mode->num_channels; c++) {
6641 struct hostapd_channel_data *chan = &mode->channels[c];
6642 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6643 continue;
6644 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6645 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6646 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6647 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6648 }
6649}
6650
6651
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006652static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006653 struct phy_info_arg *results)
6654{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006655 u16 m;
6656
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006657 for (m = 0; m < *results->num_modes; m++) {
6658 int c;
6659 struct hostapd_hw_modes *mode = &results->modes[m];
6660
6661 for (c = 0; c < mode->num_channels; c++) {
6662 struct hostapd_channel_data *chan = &mode->channels[c];
6663 if ((u32) chan->freq - 10 >= start &&
6664 (u32) chan->freq + 10 <= end)
6665 chan->max_tx_power = max_eirp;
6666 }
6667 }
6668}
6669
6670
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006671static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006672 struct phy_info_arg *results)
6673{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674 u16 m;
6675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676 for (m = 0; m < *results->num_modes; m++) {
6677 if (!(results->modes[m].ht_capab &
6678 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6679 continue;
6680 nl80211_set_ht40_mode(&results->modes[m], start, end);
6681 }
6682}
6683
6684
6685static void nl80211_reg_rule_sec(struct nlattr *tb[],
6686 struct phy_info_arg *results)
6687{
6688 u32 start, end, max_bw;
6689 u16 m;
6690
6691 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6692 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6693 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6694 return;
6695
6696 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6697 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6698 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6699
6700 if (max_bw < 20)
6701 return;
6702
6703 for (m = 0; m < *results->num_modes; m++) {
6704 if (!(results->modes[m].ht_capab &
6705 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6706 continue;
6707 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6708 }
6709}
6710
6711
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006712static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6713 int end)
6714{
6715 int c;
6716
6717 for (c = 0; c < mode->num_channels; c++) {
6718 struct hostapd_channel_data *chan = &mode->channels[c];
6719 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6720 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6721
6722 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6723 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6724
6725 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6726 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6727
6728 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6729 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6730 }
6731}
6732
6733
6734static void nl80211_reg_rule_vht(struct nlattr *tb[],
6735 struct phy_info_arg *results)
6736{
6737 u32 start, end, max_bw;
6738 u16 m;
6739
6740 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6741 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6742 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6743 return;
6744
6745 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6746 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6747 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6748
6749 if (max_bw < 80)
6750 return;
6751
6752 for (m = 0; m < *results->num_modes; m++) {
6753 if (!(results->modes[m].ht_capab &
6754 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6755 continue;
6756 /* TODO: use a real VHT support indication */
6757 if (!results->modes[m].vht_capab)
6758 continue;
6759
6760 nl80211_set_vht_mode(&results->modes[m], start, end);
6761 }
6762}
6763
6764
Dmitry Shmidt97672262014-02-03 13:02:54 -08006765static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6766{
6767 switch (region) {
6768 case NL80211_DFS_UNSET:
6769 return "DFS-UNSET";
6770 case NL80211_DFS_FCC:
6771 return "DFS-FCC";
6772 case NL80211_DFS_ETSI:
6773 return "DFS-ETSI";
6774 case NL80211_DFS_JP:
6775 return "DFS-JP";
6776 default:
6777 return "DFS-invalid";
6778 }
6779}
6780
6781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006782static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6783{
6784 struct phy_info_arg *results = arg;
6785 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6786 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6787 struct nlattr *nl_rule;
6788 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6789 int rem_rule;
6790 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6791 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6792 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6793 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6794 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6795 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6796 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6797 };
6798
6799 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6800 genlmsg_attrlen(gnlh, 0), NULL);
6801 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6802 !tb_msg[NL80211_ATTR_REG_RULES]) {
6803 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6804 "available");
6805 return NL_SKIP;
6806 }
6807
Dmitry Shmidt97672262014-02-03 13:02:54 -08006808 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6809 enum nl80211_dfs_regions dfs_domain;
6810 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6811 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6812 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6813 dfs_domain_name(dfs_domain));
6814 } else {
6815 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6816 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006818
6819 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6820 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006821 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006822 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6823 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006824 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6825 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6826 continue;
6827 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6828 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6829 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6830 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6831 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6832 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006833 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6834 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006835
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006836 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6837 start, end, max_bw, max_eirp,
6838 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6839 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6840 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6841 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6842 "",
6843 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6844 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6845 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6846 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006847 if (max_bw >= 40)
6848 nl80211_reg_rule_ht40(start, end, results);
6849 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6850 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6851 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006852 }
6853
6854 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6855 {
6856 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6857 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6858 nl80211_reg_rule_sec(tb_rule, results);
6859 }
6860
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006861 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6862 {
6863 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6864 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6865 nl80211_reg_rule_vht(tb_rule, results);
6866 }
6867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868 return NL_SKIP;
6869}
6870
6871
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006872static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6873 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006874{
6875 struct nl_msg *msg;
6876
6877 msg = nlmsg_alloc();
6878 if (!msg)
6879 return -ENOMEM;
6880
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006881 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006882 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6883}
6884
6885
6886static struct hostapd_hw_modes *
6887wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6888{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006889 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006890 struct i802_bss *bss = priv;
6891 struct wpa_driver_nl80211_data *drv = bss->drv;
6892 struct nl_msg *msg;
6893 struct phy_info_arg result = {
6894 .num_modes = num_modes,
6895 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006896 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897 };
6898
6899 *num_modes = 0;
6900 *flags = 0;
6901
6902 msg = nlmsg_alloc();
6903 if (!msg)
6904 return NULL;
6905
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006906 feat = get_nl80211_protocol_features(drv);
6907 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6908 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6909 else
6910 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006912 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006913 if (nl80211_set_iface_id(msg, bss) < 0)
6914 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915
6916 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006917 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006918 return wpa_driver_nl80211_postprocess_modes(result.modes,
6919 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006920 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006921 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006923 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 return NULL;
6925}
6926
6927
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006928static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6929 const void *data, size_t len,
6930 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931{
6932 __u8 rtap_hdr[] = {
6933 0x00, 0x00, /* radiotap version */
6934 0x0e, 0x00, /* radiotap length */
6935 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6936 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6937 0x00, /* padding */
6938 0x00, 0x00, /* RX and TX flags to indicate that */
6939 0x00, 0x00, /* this is the injected frame directly */
6940 };
6941 struct iovec iov[2] = {
6942 {
6943 .iov_base = &rtap_hdr,
6944 .iov_len = sizeof(rtap_hdr),
6945 },
6946 {
6947 .iov_base = (void *) data,
6948 .iov_len = len,
6949 }
6950 };
6951 struct msghdr msg = {
6952 .msg_name = NULL,
6953 .msg_namelen = 0,
6954 .msg_iov = iov,
6955 .msg_iovlen = 2,
6956 .msg_control = NULL,
6957 .msg_controllen = 0,
6958 .msg_flags = 0,
6959 };
6960 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006961 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962
6963 if (encrypt)
6964 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6965
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006966 if (drv->monitor_sock < 0) {
6967 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6968 "for %s", __func__);
6969 return -1;
6970 }
6971
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006972 if (noack)
6973 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006974 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 res = sendmsg(drv->monitor_sock, &msg, 0);
6977 if (res < 0) {
6978 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6979 return -1;
6980 }
6981 return 0;
6982}
6983
6984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006985static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6986 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006987 int encrypt, int noack,
6988 unsigned int freq, int no_cck,
6989 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990{
6991 struct wpa_driver_nl80211_data *drv = bss->drv;
6992 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006993 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006994
Dmitry Shmidt56052862013-10-04 10:23:25 -07006995 if (freq == 0) {
6996 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
6997 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006998 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006999 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007000
Dmitry Shmidt56052862013-10-04 10:23:25 -07007001 if (drv->use_monitor) {
7002 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7003 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007004 return wpa_driver_nl80211_send_mntr(drv, data, len,
7005 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007006 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007007
Dmitry Shmidt56052862013-10-04 10:23:25 -07007008 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07007009 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7010 &cookie, no_cck, noack, offchanok);
7011 if (res == 0 && !noack) {
7012 const struct ieee80211_mgmt *mgmt;
7013 u16 fc;
7014
7015 mgmt = (const struct ieee80211_mgmt *) data;
7016 fc = le_to_host16(mgmt->frame_control);
7017 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7018 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7019 wpa_printf(MSG_MSGDUMP,
7020 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7021 (long long unsigned int)
7022 drv->send_action_cookie,
7023 (long long unsigned int) cookie);
7024 drv->send_action_cookie = cookie;
7025 }
7026 }
7027
7028 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007029}
7030
7031
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007032static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7033 size_t data_len, int noack,
7034 unsigned int freq, int no_cck,
7035 int offchanok,
7036 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 struct wpa_driver_nl80211_data *drv = bss->drv;
7039 struct ieee80211_mgmt *mgmt;
7040 int encrypt = 1;
7041 u16 fc;
7042
7043 mgmt = (struct ieee80211_mgmt *) data;
7044 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007045 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
7046 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007047
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007048 if ((is_sta_interface(drv->nlmode) ||
7049 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7051 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7052 /*
7053 * The use of last_mgmt_freq is a bit of a hack,
7054 * but it works due to the single-threaded nature
7055 * of wpa_supplicant.
7056 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007057 if (freq == 0) {
7058 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7059 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007060 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007061 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007062 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007063 data, data_len, NULL, 1, noack,
7064 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007066
7067 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007068 if (freq == 0) {
7069 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7070 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007071 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007072 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007073 return nl80211_send_frame_cmd(bss, freq,
7074 (int) freq == bss->freq ? 0 :
7075 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007076 data, data_len,
7077 &drv->send_action_cookie,
7078 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007079 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007081 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7082 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7083 /*
7084 * Only one of the authentication frame types is encrypted.
7085 * In order for static WEP encryption to work properly (i.e.,
7086 * to not encrypt the frame), we need to tell mac80211 about
7087 * the frames that must not be encrypted.
7088 */
7089 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7090 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7091 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7092 encrypt = 0;
7093 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007094
Dmitry Shmidt56052862013-10-04 10:23:25 -07007095 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007096 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007097 noack, freq, no_cck, offchanok,
7098 wait_time);
7099}
7100
7101
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007102static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7103 int slot, int ht_opmode, int ap_isolate,
7104 int *basic_rates)
7105{
7106 struct wpa_driver_nl80211_data *drv = bss->drv;
7107 struct nl_msg *msg;
7108
7109 msg = nlmsg_alloc();
7110 if (!msg)
7111 return -ENOMEM;
7112
7113 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7114
7115 if (cts >= 0)
7116 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7117 if (preamble >= 0)
7118 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7119 if (slot >= 0)
7120 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7121 if (ht_opmode >= 0)
7122 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7123 if (ap_isolate >= 0)
7124 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7125
7126 if (basic_rates) {
7127 u8 rates[NL80211_MAX_SUPP_RATES];
7128 u8 rates_len = 0;
7129 int i;
7130
7131 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7132 i++)
7133 rates[rates_len++] = basic_rates[i] / 5;
7134
7135 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7136 }
7137
7138 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7139
7140 return send_and_recv_msgs(drv, msg, NULL, NULL);
7141 nla_put_failure:
7142 nlmsg_free(msg);
7143 return -ENOBUFS;
7144}
7145
7146
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007147static int wpa_driver_nl80211_set_acl(void *priv,
7148 struct hostapd_acl_params *params)
7149{
7150 struct i802_bss *bss = priv;
7151 struct wpa_driver_nl80211_data *drv = bss->drv;
7152 struct nl_msg *msg;
7153 struct nlattr *acl;
7154 unsigned int i;
7155 int ret = 0;
7156
7157 if (!(drv->capa.max_acl_mac_addrs))
7158 return -ENOTSUP;
7159
7160 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7161 return -ENOTSUP;
7162
7163 msg = nlmsg_alloc();
7164 if (!msg)
7165 return -ENOMEM;
7166
7167 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7168 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7169
7170 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7171
7172 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7173
7174 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7175 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7176 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7177
7178 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7179 if (acl == NULL)
7180 goto nla_put_failure;
7181
7182 for (i = 0; i < params->num_mac_acl; i++)
7183 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7184
7185 nla_nest_end(msg, acl);
7186
7187 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7188 msg = NULL;
7189 if (ret) {
7190 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7191 ret, strerror(-ret));
7192 }
7193
7194nla_put_failure:
7195 nlmsg_free(msg);
7196
7197 return ret;
7198}
7199
7200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007201static int wpa_driver_nl80211_set_ap(void *priv,
7202 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007203{
7204 struct i802_bss *bss = priv;
7205 struct wpa_driver_nl80211_data *drv = bss->drv;
7206 struct nl_msg *msg;
7207 u8 cmd = NL80211_CMD_NEW_BEACON;
7208 int ret;
7209 int beacon_set;
7210 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007211 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007212 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007213 u32 ver;
7214
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007215 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007216
7217 msg = nlmsg_alloc();
7218 if (!msg)
7219 return -ENOMEM;
7220
7221 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7222 beacon_set);
7223 if (beacon_set)
7224 cmd = NL80211_CMD_SET_BEACON;
7225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007226 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007227 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7228 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007229 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007230 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7231 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007232 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007233 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007235 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007236 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007237 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007238 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007239 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7240 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007241 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7242 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007243 if (params->proberesp && params->proberesp_len) {
7244 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7245 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007246 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7247 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007248 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007249 switch (params->hide_ssid) {
7250 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007251 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007252 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7253 NL80211_HIDDEN_SSID_NOT_IN_USE);
7254 break;
7255 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007256 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007257 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7258 NL80211_HIDDEN_SSID_ZERO_LEN);
7259 break;
7260 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007261 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007262 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7263 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7264 break;
7265 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007266 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007267 if (params->privacy)
7268 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007269 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007270 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7271 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7272 /* Leave out the attribute */
7273 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7274 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7275 NL80211_AUTHTYPE_SHARED_KEY);
7276 else
7277 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7278 NL80211_AUTHTYPE_OPEN_SYSTEM);
7279
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007280 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007281 ver = 0;
7282 if (params->wpa_version & WPA_PROTO_WPA)
7283 ver |= NL80211_WPA_VERSION_1;
7284 if (params->wpa_version & WPA_PROTO_RSN)
7285 ver |= NL80211_WPA_VERSION_2;
7286 if (ver)
7287 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7288
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007289 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7290 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007291 num_suites = 0;
7292 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7293 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7294 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7295 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7296 if (num_suites) {
7297 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7298 num_suites * sizeof(u32), suites);
7299 }
7300
7301 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7302 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7303 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7304
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007305 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7306 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007307 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7308 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007309 if (num_suites) {
7310 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7311 num_suites * sizeof(u32), suites);
7312 }
7313
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007314 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7315 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007316 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7317 if (suite)
7318 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007319
7320 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007321 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7322 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007323 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7324 wpabuf_head(params->beacon_ies));
7325 }
7326 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007327 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7328 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007329 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7330 wpabuf_len(params->proberesp_ies),
7331 wpabuf_head(params->proberesp_ies));
7332 }
7333 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007334 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7335 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007336 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7337 wpabuf_len(params->assocresp_ies),
7338 wpabuf_head(params->assocresp_ies));
7339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007340
Dmitry Shmidt04949592012-07-19 12:16:46 -07007341 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007342 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7343 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007344 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7345 params->ap_max_inactivity);
7346 }
7347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007348 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7349 if (ret) {
7350 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7351 ret, strerror(-ret));
7352 } else {
7353 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007354 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7355 params->short_slot_time, params->ht_opmode,
7356 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007357 if (beacon_set && params->freq &&
7358 params->freq->bandwidth != bss->bandwidth) {
7359 wpa_printf(MSG_DEBUG,
7360 "nl80211: Update BSS %s bandwidth: %d -> %d",
7361 bss->ifname, bss->bandwidth,
7362 params->freq->bandwidth);
7363 ret = nl80211_set_channel(bss, params->freq, 1);
7364 if (ret) {
7365 wpa_printf(MSG_DEBUG,
7366 "nl80211: Frequency set failed: %d (%s)",
7367 ret, strerror(-ret));
7368 } else {
7369 wpa_printf(MSG_DEBUG,
7370 "nl80211: Frequency set succeeded for ht2040 coex");
7371 bss->bandwidth = params->freq->bandwidth;
7372 }
7373 } else if (!beacon_set) {
7374 /*
7375 * cfg80211 updates the driver on frequence change in AP
7376 * mode only at the point when beaconing is started, so
7377 * set the initial value here.
7378 */
7379 bss->bandwidth = params->freq->bandwidth;
7380 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381 }
7382 return ret;
7383 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007384 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385 return -ENOBUFS;
7386}
7387
7388
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007389static int nl80211_put_freq_params(struct nl_msg *msg,
7390 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007391{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007392 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7393 if (freq->vht_enabled) {
7394 switch (freq->bandwidth) {
7395 case 20:
7396 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7397 NL80211_CHAN_WIDTH_20);
7398 break;
7399 case 40:
7400 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7401 NL80211_CHAN_WIDTH_40);
7402 break;
7403 case 80:
7404 if (freq->center_freq2)
7405 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7406 NL80211_CHAN_WIDTH_80P80);
7407 else
7408 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7409 NL80211_CHAN_WIDTH_80);
7410 break;
7411 case 160:
7412 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7413 NL80211_CHAN_WIDTH_160);
7414 break;
7415 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007416 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007417 }
7418 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7419 if (freq->center_freq2)
7420 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7421 freq->center_freq2);
7422 } else if (freq->ht_enabled) {
7423 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007424 case -1:
7425 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7426 NL80211_CHAN_HT40MINUS);
7427 break;
7428 case 1:
7429 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7430 NL80211_CHAN_HT40PLUS);
7431 break;
7432 default:
7433 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7434 NL80211_CHAN_HT20);
7435 break;
7436 }
7437 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007438 return 0;
7439
7440nla_put_failure:
7441 return -ENOBUFS;
7442}
7443
7444
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007445static int nl80211_set_channel(struct i802_bss *bss,
7446 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007447{
7448 struct wpa_driver_nl80211_data *drv = bss->drv;
7449 struct nl_msg *msg;
7450 int ret;
7451
7452 wpa_printf(MSG_DEBUG,
7453 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7454 freq->freq, freq->ht_enabled, freq->vht_enabled,
7455 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7456 msg = nlmsg_alloc();
7457 if (!msg)
7458 return -1;
7459
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007460 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7461 NL80211_CMD_SET_WIPHY);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007462
7463 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7464 if (nl80211_put_freq_params(msg, freq) < 0)
7465 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466
7467 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007468 msg = NULL;
7469 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007470 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007473 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007474 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007475nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007476 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007477 return -1;
7478}
7479
7480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007481static u32 sta_flags_nl80211(int flags)
7482{
7483 u32 f = 0;
7484
7485 if (flags & WPA_STA_AUTHORIZED)
7486 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7487 if (flags & WPA_STA_WMM)
7488 f |= BIT(NL80211_STA_FLAG_WME);
7489 if (flags & WPA_STA_SHORT_PREAMBLE)
7490 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7491 if (flags & WPA_STA_MFP)
7492 f |= BIT(NL80211_STA_FLAG_MFP);
7493 if (flags & WPA_STA_TDLS_PEER)
7494 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7495
7496 return f;
7497}
7498
7499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007500static int wpa_driver_nl80211_sta_add(void *priv,
7501 struct hostapd_sta_add_params *params)
7502{
7503 struct i802_bss *bss = priv;
7504 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007505 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007506 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507 int ret = -ENOBUFS;
7508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007509 if ((params->flags & WPA_STA_TDLS_PEER) &&
7510 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7511 return -EOPNOTSUPP;
7512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007513 msg = nlmsg_alloc();
7514 if (!msg)
7515 return -ENOMEM;
7516
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007517 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7518 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007519 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7520 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521
7522 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7523 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7525 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007526 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7527 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007528 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007529 if (params->aid) {
7530 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7531 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7532 } else {
7533 /*
7534 * cfg80211 validates that AID is non-zero, so we have
7535 * to make this a non-zero value for the TDLS case where
7536 * a dummy STA entry is used for now.
7537 */
7538 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7539 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7540 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007541 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7542 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007543 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7544 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007545 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7546 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7547 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007550 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7551 (u8 *) params->ht_capabilities,
7552 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7554 sizeof(*params->ht_capabilities),
7555 params->ht_capabilities);
7556 }
7557
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007558 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007559 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7560 (u8 *) params->vht_capabilities,
7561 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007562 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7563 sizeof(*params->vht_capabilities),
7564 params->vht_capabilities);
7565 }
7566
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007567 if (params->vht_opmode_enabled) {
7568 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7569 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7570 params->vht_opmode);
7571 }
7572
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007573 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7574 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7575
7576 if (params->ext_capab) {
7577 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7578 params->ext_capab, params->ext_capab_len);
7579 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7580 params->ext_capab_len, params->ext_capab);
7581 }
7582
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007583 if (params->supp_channels) {
7584 wpa_hexdump(MSG_DEBUG, " * supported channels",
7585 params->supp_channels, params->supp_channels_len);
7586 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7587 params->supp_channels_len, params->supp_channels);
7588 }
7589
7590 if (params->supp_oper_classes) {
7591 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7592 params->supp_oper_classes,
7593 params->supp_oper_classes_len);
7594 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7595 params->supp_oper_classes_len,
7596 params->supp_oper_classes);
7597 }
7598
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007599 os_memset(&upd, 0, sizeof(upd));
7600 upd.mask = sta_flags_nl80211(params->flags);
7601 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007602 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7603 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007604 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7605
7606 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007607 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7608
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007609 if (!wme)
7610 goto nla_put_failure;
7611
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007612 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007613 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007614 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007615 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007616 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007617 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007618 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007619 }
7620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007622 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007623 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007624 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7625 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7626 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627 if (ret == -EEXIST)
7628 ret = 0;
7629 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007630 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631 return ret;
7632}
7633
7634
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007635static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007636{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637 struct wpa_driver_nl80211_data *drv = bss->drv;
7638 struct nl_msg *msg;
7639 int ret;
7640
7641 msg = nlmsg_alloc();
7642 if (!msg)
7643 return -ENOMEM;
7644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007645 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007646
7647 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7648 if_nametoindex(bss->ifname));
7649 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7650
7651 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007652 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7653 " --> %d (%s)",
7654 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007655 if (ret == -ENOENT)
7656 return 0;
7657 return ret;
7658 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007659 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660 return -ENOBUFS;
7661}
7662
7663
7664static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7665 int ifidx)
7666{
7667 struct nl_msg *msg;
7668
7669 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671 /* stop listening for EAPOL on this interface */
7672 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673
7674 msg = nlmsg_alloc();
7675 if (!msg)
7676 goto nla_put_failure;
7677
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007678 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007679 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7680
7681 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7682 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007683 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007684 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007685 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007686 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7687}
7688
7689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007690static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7691{
7692 switch (mode) {
7693 case NL80211_IFTYPE_ADHOC:
7694 return "ADHOC";
7695 case NL80211_IFTYPE_STATION:
7696 return "STATION";
7697 case NL80211_IFTYPE_AP:
7698 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007699 case NL80211_IFTYPE_AP_VLAN:
7700 return "AP_VLAN";
7701 case NL80211_IFTYPE_WDS:
7702 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007703 case NL80211_IFTYPE_MONITOR:
7704 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007705 case NL80211_IFTYPE_MESH_POINT:
7706 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007707 case NL80211_IFTYPE_P2P_CLIENT:
7708 return "P2P_CLIENT";
7709 case NL80211_IFTYPE_P2P_GO:
7710 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007711 case NL80211_IFTYPE_P2P_DEVICE:
7712 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007713 default:
7714 return "unknown";
7715 }
7716}
7717
7718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007719static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7720 const char *ifname,
7721 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007722 const u8 *addr, int wds,
7723 int (*handler)(struct nl_msg *, void *),
7724 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007725{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007726 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007727 int ifidx;
7728 int ret = -ENOBUFS;
7729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007730 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7731 iftype, nl80211_iftype_str(iftype));
7732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007733 msg = nlmsg_alloc();
7734 if (!msg)
7735 return -1;
7736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007737 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007738 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007739 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7741 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7742
7743 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007744 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007745
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007746 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747 if (!flags)
7748 goto nla_put_failure;
7749
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007750 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007751
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007752 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007753 } else if (wds) {
7754 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7755 }
7756
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007757 /*
7758 * Tell cfg80211 that the interface belongs to the socket that created
7759 * it, and the interface should be deleted when the socket is closed.
7760 */
7761 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
7762
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007763 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007764 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007765 if (ret) {
7766 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007767 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7769 ifname, ret, strerror(-ret));
7770 return ret;
7771 }
7772
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007773 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7774 return 0;
7775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007776 ifidx = if_nametoindex(ifname);
7777 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7778 ifname, ifidx);
7779
7780 if (ifidx <= 0)
7781 return -1;
7782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007783 /* start listening for EAPOL on this interface */
7784 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785
7786 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007787 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007788 nl80211_remove_iface(drv, ifidx);
7789 return -1;
7790 }
7791
7792 return ifidx;
7793}
7794
7795
7796static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7797 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007798 const u8 *addr, int wds,
7799 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007800 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007801{
7802 int ret;
7803
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007804 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7805 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007807 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007808 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007809 if (use_existing) {
7810 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7811 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007812 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
7813 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7814 addr) < 0 &&
7815 (linux_set_iface_flags(drv->global->ioctl_sock,
7816 ifname, 0) < 0 ||
7817 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7818 addr) < 0 ||
7819 linux_set_iface_flags(drv->global->ioctl_sock,
7820 ifname, 1) < 0))
7821 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007822 return -ENFILE;
7823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7825
7826 /* Try to remove the interface that was already there. */
7827 nl80211_remove_iface(drv, if_nametoindex(ifname));
7828
7829 /* Try to create the interface again */
7830 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007831 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007832 }
7833
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007834 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007835 nl80211_disable_11b_rates(drv, ret, 1);
7836
7837 return ret;
7838}
7839
7840
7841static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7842{
7843 struct ieee80211_hdr *hdr;
7844 u16 fc;
7845 union wpa_event_data event;
7846
7847 hdr = (struct ieee80211_hdr *) buf;
7848 fc = le_to_host16(hdr->frame_control);
7849
7850 os_memset(&event, 0, sizeof(event));
7851 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7852 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7853 event.tx_status.dst = hdr->addr1;
7854 event.tx_status.data = buf;
7855 event.tx_status.data_len = len;
7856 event.tx_status.ack = ok;
7857 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7858}
7859
7860
7861static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7862 u8 *buf, size_t len)
7863{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007864 struct ieee80211_hdr *hdr = (void *)buf;
7865 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007866 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007867
7868 if (len < sizeof(*hdr))
7869 return;
7870
7871 fc = le_to_host16(hdr->frame_control);
7872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007873 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007874 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7875 event.rx_from_unknown.addr = hdr->addr2;
7876 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7877 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007878 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7879}
7880
7881
7882static void handle_frame(struct wpa_driver_nl80211_data *drv,
7883 u8 *buf, size_t len, int datarate, int ssi_signal)
7884{
7885 struct ieee80211_hdr *hdr;
7886 u16 fc;
7887 union wpa_event_data event;
7888
7889 hdr = (struct ieee80211_hdr *) buf;
7890 fc = le_to_host16(hdr->frame_control);
7891
7892 switch (WLAN_FC_GET_TYPE(fc)) {
7893 case WLAN_FC_TYPE_MGMT:
7894 os_memset(&event, 0, sizeof(event));
7895 event.rx_mgmt.frame = buf;
7896 event.rx_mgmt.frame_len = len;
7897 event.rx_mgmt.datarate = datarate;
7898 event.rx_mgmt.ssi_signal = ssi_signal;
7899 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7900 break;
7901 case WLAN_FC_TYPE_CTRL:
7902 /* can only get here with PS-Poll frames */
7903 wpa_printf(MSG_DEBUG, "CTRL");
7904 from_unknown_sta(drv, buf, len);
7905 break;
7906 case WLAN_FC_TYPE_DATA:
7907 from_unknown_sta(drv, buf, len);
7908 break;
7909 }
7910}
7911
7912
7913static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7914{
7915 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7916 int len;
7917 unsigned char buf[3000];
7918 struct ieee80211_radiotap_iterator iter;
7919 int ret;
7920 int datarate = 0, ssi_signal = 0;
7921 int injected = 0, failed = 0, rxflags = 0;
7922
7923 len = recv(sock, buf, sizeof(buf), 0);
7924 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007925 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7926 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007927 return;
7928 }
7929
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007930 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007931 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932 return;
7933 }
7934
7935 while (1) {
7936 ret = ieee80211_radiotap_iterator_next(&iter);
7937 if (ret == -ENOENT)
7938 break;
7939 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007940 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7941 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007942 return;
7943 }
7944 switch (iter.this_arg_index) {
7945 case IEEE80211_RADIOTAP_FLAGS:
7946 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7947 len -= 4;
7948 break;
7949 case IEEE80211_RADIOTAP_RX_FLAGS:
7950 rxflags = 1;
7951 break;
7952 case IEEE80211_RADIOTAP_TX_FLAGS:
7953 injected = 1;
7954 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7955 IEEE80211_RADIOTAP_F_TX_FAIL;
7956 break;
7957 case IEEE80211_RADIOTAP_DATA_RETRIES:
7958 break;
7959 case IEEE80211_RADIOTAP_CHANNEL:
7960 /* TODO: convert from freq/flags to channel number */
7961 break;
7962 case IEEE80211_RADIOTAP_RATE:
7963 datarate = *iter.this_arg * 5;
7964 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007965 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7966 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967 break;
7968 }
7969 }
7970
7971 if (rxflags && injected)
7972 return;
7973
7974 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007975 handle_frame(drv, buf + iter._max_length,
7976 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007977 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007978 handle_tx_callback(drv->ctx, buf + iter._max_length,
7979 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980}
7981
7982
7983/*
7984 * we post-process the filter code later and rewrite
7985 * this to the offset to the last instruction
7986 */
7987#define PASS 0xFF
7988#define FAIL 0xFE
7989
7990static struct sock_filter msock_filter_insns[] = {
7991 /*
7992 * do a little-endian load of the radiotap length field
7993 */
7994 /* load lower byte into A */
7995 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
7996 /* put it into X (== index register) */
7997 BPF_STMT(BPF_MISC| BPF_TAX, 0),
7998 /* load upper byte into A */
7999 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8000 /* left-shift it by 8 */
8001 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8002 /* or with X */
8003 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8004 /* put result into X */
8005 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8006
8007 /*
8008 * Allow management frames through, this also gives us those
8009 * management frames that we sent ourselves with status
8010 */
8011 /* load the lower byte of the IEEE 802.11 frame control field */
8012 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8013 /* mask off frame type and version */
8014 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8015 /* accept frame if it's both 0, fall through otherwise */
8016 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8017
8018 /*
8019 * TODO: add a bit to radiotap RX flags that indicates
8020 * that the sending station is not associated, then
8021 * add a filter here that filters on our DA and that flag
8022 * to allow us to deauth frames to that bad station.
8023 *
8024 * For now allow all To DS data frames through.
8025 */
8026 /* load the IEEE 802.11 frame control field */
8027 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8028 /* mask off frame type, version and DS status */
8029 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8030 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8031 */
8032 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
8033
8034#if 0
8035 /*
8036 * drop non-data frames
8037 */
8038 /* load the lower byte of the frame control field */
8039 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8040 /* mask off QoS bit */
8041 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8042 /* drop non-data frames */
8043 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
8044#endif
8045 /* load the upper byte of the frame control field */
8046 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
8047 /* mask off toDS/fromDS */
8048 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
8049 /* accept WDS frames */
8050 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
8051
8052 /*
8053 * add header length to index
8054 */
8055 /* load the lower byte of the frame control field */
8056 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8057 /* mask off QoS bit */
8058 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8059 /* right shift it by 6 to give 0 or 2 */
8060 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8061 /* add data frame header length */
8062 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8063 /* add index, was start of 802.11 header */
8064 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8065 /* move to index, now start of LL header */
8066 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8067
8068 /*
8069 * Accept empty data frames, we use those for
8070 * polling activity.
8071 */
8072 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8073 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8074
8075 /*
8076 * Accept EAPOL frames
8077 */
8078 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8079 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8080 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8081 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8082
8083 /* keep these last two statements or change the code below */
8084 /* return 0 == "DROP" */
8085 BPF_STMT(BPF_RET | BPF_K, 0),
8086 /* return ~0 == "keep all" */
8087 BPF_STMT(BPF_RET | BPF_K, ~0),
8088};
8089
8090static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008091 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008092 .filter = msock_filter_insns,
8093};
8094
8095
8096static int add_monitor_filter(int s)
8097{
8098 int idx;
8099
8100 /* rewrite all PASS/FAIL jump offsets */
8101 for (idx = 0; idx < msock_filter.len; idx++) {
8102 struct sock_filter *insn = &msock_filter_insns[idx];
8103
8104 if (BPF_CLASS(insn->code) == BPF_JMP) {
8105 if (insn->code == (BPF_JMP|BPF_JA)) {
8106 if (insn->k == PASS)
8107 insn->k = msock_filter.len - idx - 2;
8108 else if (insn->k == FAIL)
8109 insn->k = msock_filter.len - idx - 3;
8110 }
8111
8112 if (insn->jt == PASS)
8113 insn->jt = msock_filter.len - idx - 2;
8114 else if (insn->jt == FAIL)
8115 insn->jt = msock_filter.len - idx - 3;
8116
8117 if (insn->jf == PASS)
8118 insn->jf = msock_filter.len - idx - 2;
8119 else if (insn->jf == FAIL)
8120 insn->jf = msock_filter.len - idx - 3;
8121 }
8122 }
8123
8124 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8125 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008126 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8127 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008128 return -1;
8129 }
8130
8131 return 0;
8132}
8133
8134
8135static void nl80211_remove_monitor_interface(
8136 struct wpa_driver_nl80211_data *drv)
8137{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008138 if (drv->monitor_refcount > 0)
8139 drv->monitor_refcount--;
8140 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8141 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008142 if (drv->monitor_refcount > 0)
8143 return;
8144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008145 if (drv->monitor_ifidx >= 0) {
8146 nl80211_remove_iface(drv, drv->monitor_ifidx);
8147 drv->monitor_ifidx = -1;
8148 }
8149 if (drv->monitor_sock >= 0) {
8150 eloop_unregister_read_sock(drv->monitor_sock);
8151 close(drv->monitor_sock);
8152 drv->monitor_sock = -1;
8153 }
8154}
8155
8156
8157static int
8158nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8159{
8160 char buf[IFNAMSIZ];
8161 struct sockaddr_ll ll;
8162 int optval;
8163 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008164
8165 if (drv->monitor_ifidx >= 0) {
8166 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008167 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8168 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008169 return 0;
8170 }
8171
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008172 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008173 /*
8174 * P2P interface name is of the format p2p-%s-%d. For monitor
8175 * interface name corresponding to P2P GO, replace "p2p-" with
8176 * "mon-" to retain the same interface name length and to
8177 * indicate that it is a monitor interface.
8178 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008179 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008180 } else {
8181 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008182 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008183 }
8184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008185 buf[IFNAMSIZ - 1] = '\0';
8186
8187 drv->monitor_ifidx =
8188 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008189 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008190
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008191 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008192 /*
8193 * This is backward compatibility for a few versions of
8194 * the kernel only that didn't advertise the right
8195 * attributes for the only driver that then supported
8196 * AP mode w/o monitor -- ath6kl.
8197 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008198 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8199 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008200 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008201 }
8202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008203 if (drv->monitor_ifidx < 0)
8204 return -1;
8205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008206 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008207 goto error;
8208
8209 memset(&ll, 0, sizeof(ll));
8210 ll.sll_family = AF_PACKET;
8211 ll.sll_ifindex = drv->monitor_ifidx;
8212 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8213 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008214 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8215 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008216 goto error;
8217 }
8218
8219 if (add_monitor_filter(drv->monitor_sock)) {
8220 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8221 "interface; do filtering in user space");
8222 /* This works, but will cost in performance. */
8223 }
8224
8225 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008226 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8227 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008228 goto error;
8229 }
8230
8231 optlen = sizeof(optval);
8232 optval = 20;
8233 if (setsockopt
8234 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008235 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8236 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008237 goto error;
8238 }
8239
8240 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8241 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008242 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008243 goto error;
8244 }
8245
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008246 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008247 return 0;
8248 error:
8249 nl80211_remove_monitor_interface(drv);
8250 return -1;
8251}
8252
8253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008254static int nl80211_setup_ap(struct i802_bss *bss)
8255{
8256 struct wpa_driver_nl80211_data *drv = bss->drv;
8257
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008258 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8259 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008260
8261 /*
8262 * Disable Probe Request reporting unless we need it in this way for
8263 * devices that include the AP SME, in the other case (unless using
8264 * monitor iface) we'll get it through the nl_mgmt socket instead.
8265 */
8266 if (!drv->device_ap_sme)
8267 wpa_driver_nl80211_probe_req_report(bss, 0);
8268
8269 if (!drv->device_ap_sme && !drv->use_monitor)
8270 if (nl80211_mgmt_subscribe_ap(bss))
8271 return -1;
8272
8273 if (drv->device_ap_sme && !drv->use_monitor)
8274 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8275 return -1;
8276
8277 if (!drv->device_ap_sme && drv->use_monitor &&
8278 nl80211_create_monitor_interface(drv) &&
8279 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008280 return -1;
8281
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008282 if (drv->device_ap_sme &&
8283 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8284 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8285 "Probe Request frame reporting in AP mode");
8286 /* Try to survive without this */
8287 }
8288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008289 return 0;
8290}
8291
8292
8293static void nl80211_teardown_ap(struct i802_bss *bss)
8294{
8295 struct wpa_driver_nl80211_data *drv = bss->drv;
8296
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008297 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8298 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008299 if (drv->device_ap_sme) {
8300 wpa_driver_nl80211_probe_req_report(bss, 0);
8301 if (!drv->use_monitor)
8302 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8303 } else if (drv->use_monitor)
8304 nl80211_remove_monitor_interface(drv);
8305 else
8306 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8307
8308 bss->beacon_set = 0;
8309}
8310
8311
8312static int nl80211_send_eapol_data(struct i802_bss *bss,
8313 const u8 *addr, const u8 *data,
8314 size_t data_len)
8315{
8316 struct sockaddr_ll ll;
8317 int ret;
8318
8319 if (bss->drv->eapol_tx_sock < 0) {
8320 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8321 return -1;
8322 }
8323
8324 os_memset(&ll, 0, sizeof(ll));
8325 ll.sll_family = AF_PACKET;
8326 ll.sll_ifindex = bss->ifindex;
8327 ll.sll_protocol = htons(ETH_P_PAE);
8328 ll.sll_halen = ETH_ALEN;
8329 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8330 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8331 (struct sockaddr *) &ll, sizeof(ll));
8332 if (ret < 0)
8333 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8334 strerror(errno));
8335
8336 return ret;
8337}
8338
8339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008340static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8341
8342static int wpa_driver_nl80211_hapd_send_eapol(
8343 void *priv, const u8 *addr, const u8 *data,
8344 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8345{
8346 struct i802_bss *bss = priv;
8347 struct wpa_driver_nl80211_data *drv = bss->drv;
8348 struct ieee80211_hdr *hdr;
8349 size_t len;
8350 u8 *pos;
8351 int res;
8352 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008354 if (drv->device_ap_sme || !drv->use_monitor)
8355 return nl80211_send_eapol_data(bss, addr, data, data_len);
8356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008357 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8358 data_len;
8359 hdr = os_zalloc(len);
8360 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008361 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8362 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008363 return -1;
8364 }
8365
8366 hdr->frame_control =
8367 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8368 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8369 if (encrypt)
8370 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8371 if (qos) {
8372 hdr->frame_control |=
8373 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8374 }
8375
8376 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8377 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8378 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8379 pos = (u8 *) (hdr + 1);
8380
8381 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008382 /* Set highest priority in QoS header */
8383 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008384 pos[1] = 0;
8385 pos += 2;
8386 }
8387
8388 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8389 pos += sizeof(rfc1042_header);
8390 WPA_PUT_BE16(pos, ETH_P_PAE);
8391 pos += 2;
8392 memcpy(pos, data, data_len);
8393
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008394 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8395 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008396 if (res < 0) {
8397 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8398 "failed: %d (%s)",
8399 (unsigned long) len, errno, strerror(errno));
8400 }
8401 os_free(hdr);
8402
8403 return res;
8404}
8405
8406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008407static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8408 int total_flags,
8409 int flags_or, int flags_and)
8410{
8411 struct i802_bss *bss = priv;
8412 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008413 struct nl_msg *msg;
8414 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008415 struct nl80211_sta_flag_update upd;
8416
8417 msg = nlmsg_alloc();
8418 if (!msg)
8419 return -ENOMEM;
8420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008421 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008422
8423 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8424 if_nametoindex(bss->ifname));
8425 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8426
8427 /*
8428 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8429 * can be removed eventually.
8430 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008431 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8432 if (!flags)
8433 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008434 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008435 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008436
8437 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008438 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008439
8440 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008441 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008442
8443 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008444 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008445
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008446 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008447 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008448
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008449 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008450
8451 os_memset(&upd, 0, sizeof(upd));
8452 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8453 upd.set = sta_flags_nl80211(flags_or);
8454 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008456 return send_and_recv_msgs(drv, msg, NULL, NULL);
8457 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008458 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008459 return -ENOBUFS;
8460}
8461
8462
8463static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8464 struct wpa_driver_associate_params *params)
8465{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008466 enum nl80211_iftype nlmode, old_mode;
8467 struct hostapd_freq_params freq = {
8468 .freq = params->freq,
8469 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008470
8471 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008472 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8473 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008474 nlmode = NL80211_IFTYPE_P2P_GO;
8475 } else
8476 nlmode = NL80211_IFTYPE_AP;
8477
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008478 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008479 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008480 nl80211_remove_monitor_interface(drv);
8481 return -1;
8482 }
8483
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008484 if (nl80211_set_channel(drv->first_bss, &freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008485 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008486 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008487 nl80211_remove_monitor_interface(drv);
8488 return -1;
8489 }
8490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008491 return 0;
8492}
8493
8494
8495static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8496{
8497 struct nl_msg *msg;
8498 int ret = -1;
8499
8500 msg = nlmsg_alloc();
8501 if (!msg)
8502 return -1;
8503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008504 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008505 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8506 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8507 msg = NULL;
8508 if (ret) {
8509 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8510 "(%s)", ret, strerror(-ret));
8511 goto nla_put_failure;
8512 }
8513
8514 ret = 0;
8515 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8516
8517nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008518 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008519 NL80211_IFTYPE_STATION)) {
8520 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8521 "station mode");
8522 }
8523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008524 nlmsg_free(msg);
8525 return ret;
8526}
8527
8528
8529static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8530 struct wpa_driver_associate_params *params)
8531{
8532 struct nl_msg *msg;
8533 int ret = -1;
8534 int count = 0;
8535
8536 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8537
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008538 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008539 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008540 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8541 "IBSS mode");
8542 return -1;
8543 }
8544
8545retry:
8546 msg = nlmsg_alloc();
8547 if (!msg)
8548 return -1;
8549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008550 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008551 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8552
8553 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8554 goto nla_put_failure;
8555
8556 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8557 params->ssid, params->ssid_len);
8558 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8559 params->ssid);
8560 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8561 drv->ssid_len = params->ssid_len;
8562
8563 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8564 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8565
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008566 if (params->beacon_int > 0) {
8567 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8568 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8569 params->beacon_int);
8570 }
8571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008572 ret = nl80211_set_conn_keys(params, msg);
8573 if (ret)
8574 goto nla_put_failure;
8575
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008576 if (params->bssid && params->fixed_bssid) {
8577 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8578 MAC2STR(params->bssid));
8579 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8580 }
8581
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008582 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8583 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8584 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8585 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008586 wpa_printf(MSG_DEBUG, " * control port");
8587 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8588 }
8589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008590 if (params->wpa_ie) {
8591 wpa_hexdump(MSG_DEBUG,
8592 " * Extra IEs for Beacon/Probe Response frames",
8593 params->wpa_ie, params->wpa_ie_len);
8594 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8595 params->wpa_ie);
8596 }
8597
8598 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8599 msg = NULL;
8600 if (ret) {
8601 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8602 ret, strerror(-ret));
8603 count++;
8604 if (ret == -EALREADY && count == 1) {
8605 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8606 "forced leave");
8607 nl80211_leave_ibss(drv);
8608 nlmsg_free(msg);
8609 goto retry;
8610 }
8611
8612 goto nla_put_failure;
8613 }
8614 ret = 0;
8615 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8616
8617nla_put_failure:
8618 nlmsg_free(msg);
8619 return ret;
8620}
8621
8622
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008623static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8624 struct wpa_driver_associate_params *params,
8625 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008626{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008627 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008629 if (params->bssid) {
8630 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8631 MAC2STR(params->bssid));
8632 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8633 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008634
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008635 if (params->bssid_hint) {
8636 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8637 MAC2STR(params->bssid_hint));
8638 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8639 params->bssid_hint);
8640 }
8641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008642 if (params->freq) {
8643 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8644 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008645 drv->assoc_freq = params->freq;
8646 } else
8647 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008648
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008649 if (params->freq_hint) {
8650 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8651 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8652 params->freq_hint);
8653 }
8654
Dmitry Shmidt04949592012-07-19 12:16:46 -07008655 if (params->bg_scan_period >= 0) {
8656 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8657 params->bg_scan_period);
8658 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8659 params->bg_scan_period);
8660 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008662 if (params->ssid) {
8663 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8664 params->ssid, params->ssid_len);
8665 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8666 params->ssid);
8667 if (params->ssid_len > sizeof(drv->ssid))
8668 goto nla_put_failure;
8669 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8670 drv->ssid_len = params->ssid_len;
8671 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008673 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8674 if (params->wpa_ie)
8675 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8676 params->wpa_ie);
8677
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008678 if (params->wpa_proto) {
8679 enum nl80211_wpa_versions ver = 0;
8680
8681 if (params->wpa_proto & WPA_PROTO_WPA)
8682 ver |= NL80211_WPA_VERSION_1;
8683 if (params->wpa_proto & WPA_PROTO_RSN)
8684 ver |= NL80211_WPA_VERSION_2;
8685
8686 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8687 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8688 }
8689
8690 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8691 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8692 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8693 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8694 }
8695
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008696 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8697 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8698 /*
8699 * This is likely to work even though many drivers do not
8700 * advertise support for operations without GTK.
8701 */
8702 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8703 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008704 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8705 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8706 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8707 }
8708
8709 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8710 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8711 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8712 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008713 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8714 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008715 int mgmt = WLAN_AKM_SUITE_PSK;
8716
8717 switch (params->key_mgmt_suite) {
8718 case WPA_KEY_MGMT_CCKM:
8719 mgmt = WLAN_AKM_SUITE_CCKM;
8720 break;
8721 case WPA_KEY_MGMT_IEEE8021X:
8722 mgmt = WLAN_AKM_SUITE_8021X;
8723 break;
8724 case WPA_KEY_MGMT_FT_IEEE8021X:
8725 mgmt = WLAN_AKM_SUITE_FT_8021X;
8726 break;
8727 case WPA_KEY_MGMT_FT_PSK:
8728 mgmt = WLAN_AKM_SUITE_FT_PSK;
8729 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008730 case WPA_KEY_MGMT_OSEN:
8731 mgmt = WLAN_AKM_SUITE_OSEN;
8732 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008733 case WPA_KEY_MGMT_PSK:
8734 default:
8735 mgmt = WLAN_AKM_SUITE_PSK;
8736 break;
8737 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008738 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008739 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8740 }
8741
8742 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8743
8744 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8745 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8746
8747 if (params->disable_ht)
8748 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8749
8750 if (params->htcaps && params->htcaps_mask) {
8751 int sz = sizeof(struct ieee80211_ht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008752 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008753 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008754 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
8755 params->htcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008756 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8757 params->htcaps_mask);
8758 }
8759
8760#ifdef CONFIG_VHT_OVERRIDES
8761 if (params->disable_vht) {
8762 wpa_printf(MSG_DEBUG, " * VHT disabled");
8763 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8764 }
8765
8766 if (params->vhtcaps && params->vhtcaps_mask) {
8767 int sz = sizeof(struct ieee80211_vht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008768 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008769 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008770 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
8771 params->vhtcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008772 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8773 params->vhtcaps_mask);
8774 }
8775#endif /* CONFIG_VHT_OVERRIDES */
8776
8777 if (params->p2p)
8778 wpa_printf(MSG_DEBUG, " * P2P group");
8779
8780 return 0;
8781nla_put_failure:
8782 return -1;
8783}
8784
8785
8786static int wpa_driver_nl80211_try_connect(
8787 struct wpa_driver_nl80211_data *drv,
8788 struct wpa_driver_associate_params *params)
8789{
8790 struct nl_msg *msg;
8791 enum nl80211_auth_type type;
8792 int ret;
8793 int algs;
8794
8795 msg = nlmsg_alloc();
8796 if (!msg)
8797 return -1;
8798
8799 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8800 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8801
8802 ret = nl80211_connect_common(drv, params, msg);
8803 if (ret)
8804 goto nla_put_failure;
8805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008806 algs = 0;
8807 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8808 algs++;
8809 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8810 algs++;
8811 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8812 algs++;
8813 if (algs > 1) {
8814 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8815 "selection");
8816 goto skip_auth_type;
8817 }
8818
8819 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8820 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8821 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8822 type = NL80211_AUTHTYPE_SHARED_KEY;
8823 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8824 type = NL80211_AUTHTYPE_NETWORK_EAP;
8825 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8826 type = NL80211_AUTHTYPE_FT;
8827 else
8828 goto nla_put_failure;
8829
8830 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8831 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8832
8833skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008834 ret = nl80211_set_conn_keys(params, msg);
8835 if (ret)
8836 goto nla_put_failure;
8837
8838 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8839 msg = NULL;
8840 if (ret) {
8841 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8842 "(%s)", ret, strerror(-ret));
8843 goto nla_put_failure;
8844 }
8845 ret = 0;
8846 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8847
8848nla_put_failure:
8849 nlmsg_free(msg);
8850 return ret;
8851
8852}
8853
8854
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008855static int wpa_driver_nl80211_connect(
8856 struct wpa_driver_nl80211_data *drv,
8857 struct wpa_driver_associate_params *params)
8858{
8859 int ret = wpa_driver_nl80211_try_connect(drv, params);
8860 if (ret == -EALREADY) {
8861 /*
8862 * cfg80211 does not currently accept new connections if
8863 * we are already connected. As a workaround, force
8864 * disconnection and try again.
8865 */
8866 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8867 "disconnecting before reassociation "
8868 "attempt");
8869 if (wpa_driver_nl80211_disconnect(
8870 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8871 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008872 ret = wpa_driver_nl80211_try_connect(drv, params);
8873 }
8874 return ret;
8875}
8876
8877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008878static int wpa_driver_nl80211_associate(
8879 void *priv, struct wpa_driver_associate_params *params)
8880{
8881 struct i802_bss *bss = priv;
8882 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008883 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008884 struct nl_msg *msg;
8885
8886 if (params->mode == IEEE80211_MODE_AP)
8887 return wpa_driver_nl80211_ap(drv, params);
8888
8889 if (params->mode == IEEE80211_MODE_IBSS)
8890 return wpa_driver_nl80211_ibss(drv, params);
8891
8892 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008893 enum nl80211_iftype nlmode = params->p2p ?
8894 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8895
8896 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008897 return -1;
8898 return wpa_driver_nl80211_connect(drv, params);
8899 }
8900
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008901 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008902
8903 msg = nlmsg_alloc();
8904 if (!msg)
8905 return -1;
8906
8907 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8908 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008909 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008910
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008911 ret = nl80211_connect_common(drv, params, msg);
8912 if (ret)
8913 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008914
8915 if (params->prev_bssid) {
8916 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8917 MAC2STR(params->prev_bssid));
8918 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8919 params->prev_bssid);
8920 }
8921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008922 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8923 msg = NULL;
8924 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008925 wpa_dbg(drv->ctx, MSG_DEBUG,
8926 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8927 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008928 nl80211_dump_scan(drv);
8929 goto nla_put_failure;
8930 }
8931 ret = 0;
8932 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8933 "successfully");
8934
8935nla_put_failure:
8936 nlmsg_free(msg);
8937 return ret;
8938}
8939
8940
8941static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008942 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008943{
8944 struct nl_msg *msg;
8945 int ret = -ENOBUFS;
8946
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008947 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8948 ifindex, mode, nl80211_iftype_str(mode));
8949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008950 msg = nlmsg_alloc();
8951 if (!msg)
8952 return -ENOMEM;
8953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008954 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008955 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008956 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008957 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8958
8959 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008960 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008961 if (!ret)
8962 return 0;
8963nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008964 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008965 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8966 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8967 return ret;
8968}
8969
8970
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008971static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8972 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008973{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008974 struct wpa_driver_nl80211_data *drv = bss->drv;
8975 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008976 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008977 int was_ap = is_ap_interface(drv->nlmode);
8978 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008980 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008981 if (res && nlmode == nl80211_get_ifmode(bss))
8982 res = 0;
8983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008984 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008985 drv->nlmode = nlmode;
8986 ret = 0;
8987 goto done;
8988 }
8989
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008990 if (res == -ENODEV)
8991 return -1;
8992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008993 if (nlmode == drv->nlmode) {
8994 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8995 "requested mode - ignore error");
8996 ret = 0;
8997 goto done; /* Already in the requested mode */
8998 }
8999
9000 /* mac80211 doesn't allow mode changes while the device is up, so
9001 * take the device down, try to set the mode again, and bring the
9002 * device back up.
9003 */
9004 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9005 "interface down");
9006 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009007 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009008 if (res == -EACCES || res == -ENODEV)
9009 break;
9010 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009011 /* Try to set the mode again while the interface is
9012 * down */
9013 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009014 if (ret == -EACCES)
9015 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009016 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009017 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009018 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009019 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009020 break;
9021 } else
9022 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9023 "interface down");
9024 os_sleep(0, 100000);
9025 }
9026
9027 if (!ret) {
9028 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9029 "interface is down");
9030 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009031 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009032 }
9033
9034done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009035 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009036 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9037 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009038 return ret;
9039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009040
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009041 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009042 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
9043 else if (drv->disabled_11b_rates)
9044 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
9045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009046 if (is_ap_interface(nlmode)) {
9047 nl80211_mgmt_unsubscribe(bss, "start AP");
9048 /* Setup additional AP mode functionality if needed */
9049 if (nl80211_setup_ap(bss))
9050 return -1;
9051 } else if (was_ap) {
9052 /* Remove additional AP mode functionality */
9053 nl80211_teardown_ap(bss);
9054 } else {
9055 nl80211_mgmt_unsubscribe(bss, "mode change");
9056 }
9057
Dmitry Shmidt04949592012-07-19 12:16:46 -07009058 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009059 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9060 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9061 "frame processing - ignore for now");
9062
9063 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009064}
9065
9066
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009067static int dfs_info_handler(struct nl_msg *msg, void *arg)
9068{
9069 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9070 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9071 int *dfs_capability_ptr = arg;
9072
9073 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9074 genlmsg_attrlen(gnlh, 0), NULL);
9075
9076 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9077 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9078 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9079
9080 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9081 nla_data(nl_vend), nla_len(nl_vend), NULL);
9082
9083 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9084 u32 val;
9085 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9086 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9087 val);
9088 *dfs_capability_ptr = val;
9089 }
9090 }
9091
9092 return NL_SKIP;
9093}
9094
9095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009096static int wpa_driver_nl80211_get_capa(void *priv,
9097 struct wpa_driver_capa *capa)
9098{
9099 struct i802_bss *bss = priv;
9100 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009101 struct nl_msg *msg;
9102 int dfs_capability = 0;
9103 int ret = 0;
9104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009105 if (!drv->has_capability)
9106 return -1;
9107 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009108 if (drv->extended_capa && drv->extended_capa_mask) {
9109 capa->extended_capa = drv->extended_capa;
9110 capa->extended_capa_mask = drv->extended_capa_mask;
9111 capa->extended_capa_len = drv->extended_capa_len;
9112 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009113
9114 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9115 !drv->allow_p2p_device) {
9116 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9117 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9118 }
9119
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009120 if (drv->dfs_vendor_cmd_avail == 1) {
9121 msg = nlmsg_alloc();
9122 if (!msg)
9123 return -ENOMEM;
9124
9125 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9126
9127 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9128 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9129 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9130 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9131
9132 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9133 &dfs_capability);
9134 if (!ret) {
9135 if (dfs_capability)
9136 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9137 }
9138 }
9139
9140 return ret;
9141
9142 nla_put_failure:
9143 nlmsg_free(msg);
9144 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009145}
9146
9147
9148static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9149{
9150 struct i802_bss *bss = priv;
9151 struct wpa_driver_nl80211_data *drv = bss->drv;
9152
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009153 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9154 bss->ifname, drv->operstate, state,
9155 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009156 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009157 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009158 state ? IF_OPER_UP : IF_OPER_DORMANT);
9159}
9160
9161
9162static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9163{
9164 struct i802_bss *bss = priv;
9165 struct wpa_driver_nl80211_data *drv = bss->drv;
9166 struct nl_msg *msg;
9167 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009168 int ret = -ENOBUFS;
9169
9170 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9171 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9172 return 0;
9173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009174
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009175 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9176 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009178 msg = nlmsg_alloc();
9179 if (!msg)
9180 return -ENOMEM;
9181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009182 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009183
9184 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9185 if_nametoindex(bss->ifname));
9186 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9187
9188 os_memset(&upd, 0, sizeof(upd));
9189 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9190 if (authorized)
9191 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9192 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9193
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009194 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9195 msg = NULL;
9196 if (!ret)
9197 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009198 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009199 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009200 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9201 ret, strerror(-ret));
9202 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009203}
9204
9205
Jouni Malinen75ecf522011-06-27 15:19:46 -07009206/* Set kernel driver on given frequency (MHz) */
9207static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009208{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009209 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009210 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009211}
9212
9213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009214static inline int min_int(int a, int b)
9215{
9216 if (a < b)
9217 return a;
9218 return b;
9219}
9220
9221
9222static int get_key_handler(struct nl_msg *msg, void *arg)
9223{
9224 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9225 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9226
9227 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9228 genlmsg_attrlen(gnlh, 0), NULL);
9229
9230 /*
9231 * TODO: validate the key index and mac address!
9232 * Otherwise, there's a race condition as soon as
9233 * the kernel starts sending key notifications.
9234 */
9235
9236 if (tb[NL80211_ATTR_KEY_SEQ])
9237 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9238 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9239 return NL_SKIP;
9240}
9241
9242
9243static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9244 int idx, u8 *seq)
9245{
9246 struct i802_bss *bss = priv;
9247 struct wpa_driver_nl80211_data *drv = bss->drv;
9248 struct nl_msg *msg;
9249
9250 msg = nlmsg_alloc();
9251 if (!msg)
9252 return -ENOMEM;
9253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009254 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009255
9256 if (addr)
9257 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9258 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9260
9261 memset(seq, 0, 6);
9262
9263 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9264 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009265 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009266 return -ENOBUFS;
9267}
9268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009269
9270static int i802_set_rts(void *priv, int rts)
9271{
9272 struct i802_bss *bss = priv;
9273 struct wpa_driver_nl80211_data *drv = bss->drv;
9274 struct nl_msg *msg;
9275 int ret = -ENOBUFS;
9276 u32 val;
9277
9278 msg = nlmsg_alloc();
9279 if (!msg)
9280 return -ENOMEM;
9281
9282 if (rts >= 2347)
9283 val = (u32) -1;
9284 else
9285 val = rts;
9286
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009287 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009288 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9289 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9290
9291 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009292 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009293 if (!ret)
9294 return 0;
9295nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009296 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009297 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9298 "%d (%s)", rts, ret, strerror(-ret));
9299 return ret;
9300}
9301
9302
9303static int i802_set_frag(void *priv, int frag)
9304{
9305 struct i802_bss *bss = priv;
9306 struct wpa_driver_nl80211_data *drv = bss->drv;
9307 struct nl_msg *msg;
9308 int ret = -ENOBUFS;
9309 u32 val;
9310
9311 msg = nlmsg_alloc();
9312 if (!msg)
9313 return -ENOMEM;
9314
9315 if (frag >= 2346)
9316 val = (u32) -1;
9317 else
9318 val = frag;
9319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009320 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009321 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9322 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9323
9324 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009325 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009326 if (!ret)
9327 return 0;
9328nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009329 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009330 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9331 "%d: %d (%s)", frag, ret, strerror(-ret));
9332 return ret;
9333}
9334
9335
9336static int i802_flush(void *priv)
9337{
9338 struct i802_bss *bss = priv;
9339 struct wpa_driver_nl80211_data *drv = bss->drv;
9340 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009341 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009342
9343 msg = nlmsg_alloc();
9344 if (!msg)
9345 return -1;
9346
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009347 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9348 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009349 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009350
9351 /*
9352 * XXX: FIX! this needs to flush all VLANs too
9353 */
9354 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9355 if_nametoindex(bss->ifname));
9356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009357 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9358 if (res) {
9359 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9360 "(%s)", res, strerror(-res));
9361 }
9362 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009363 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009364 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009365 return -ENOBUFS;
9366}
9367
9368
9369static int get_sta_handler(struct nl_msg *msg, void *arg)
9370{
9371 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9372 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9373 struct hostap_sta_driver_data *data = arg;
9374 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9375 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9376 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9377 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9378 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9379 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9380 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009381 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009382 };
9383
9384 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9385 genlmsg_attrlen(gnlh, 0), NULL);
9386
9387 /*
9388 * TODO: validate the interface and mac address!
9389 * Otherwise, there's a race condition as soon as
9390 * the kernel starts sending station notifications.
9391 */
9392
9393 if (!tb[NL80211_ATTR_STA_INFO]) {
9394 wpa_printf(MSG_DEBUG, "sta stats missing!");
9395 return NL_SKIP;
9396 }
9397 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9398 tb[NL80211_ATTR_STA_INFO],
9399 stats_policy)) {
9400 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9401 return NL_SKIP;
9402 }
9403
9404 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9405 data->inactive_msec =
9406 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9407 if (stats[NL80211_STA_INFO_RX_BYTES])
9408 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9409 if (stats[NL80211_STA_INFO_TX_BYTES])
9410 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9411 if (stats[NL80211_STA_INFO_RX_PACKETS])
9412 data->rx_packets =
9413 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9414 if (stats[NL80211_STA_INFO_TX_PACKETS])
9415 data->tx_packets =
9416 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009417 if (stats[NL80211_STA_INFO_TX_FAILED])
9418 data->tx_retry_failed =
9419 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009420
9421 return NL_SKIP;
9422}
9423
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009424static int i802_read_sta_data(struct i802_bss *bss,
9425 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009426 const u8 *addr)
9427{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009428 struct wpa_driver_nl80211_data *drv = bss->drv;
9429 struct nl_msg *msg;
9430
9431 os_memset(data, 0, sizeof(*data));
9432 msg = nlmsg_alloc();
9433 if (!msg)
9434 return -ENOMEM;
9435
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009436 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009437
9438 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9440
9441 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9442 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009443 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009444 return -ENOBUFS;
9445}
9446
9447
9448static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9449 int cw_min, int cw_max, int burst_time)
9450{
9451 struct i802_bss *bss = priv;
9452 struct wpa_driver_nl80211_data *drv = bss->drv;
9453 struct nl_msg *msg;
9454 struct nlattr *txq, *params;
9455
9456 msg = nlmsg_alloc();
9457 if (!msg)
9458 return -1;
9459
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009460 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009461
9462 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9463
9464 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9465 if (!txq)
9466 goto nla_put_failure;
9467
9468 /* We are only sending parameters for a single TXQ at a time */
9469 params = nla_nest_start(msg, 1);
9470 if (!params)
9471 goto nla_put_failure;
9472
9473 switch (queue) {
9474 case 0:
9475 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9476 break;
9477 case 1:
9478 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9479 break;
9480 case 2:
9481 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9482 break;
9483 case 3:
9484 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9485 break;
9486 }
9487 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9488 * 32 usec, so need to convert the value here. */
9489 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9490 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9491 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9492 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9493
9494 nla_nest_end(msg, params);
9495
9496 nla_nest_end(msg, txq);
9497
9498 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9499 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009500 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009501 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009502 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009503 return -1;
9504}
9505
9506
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009507static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009508 const char *ifname, int vlan_id)
9509{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009510 struct wpa_driver_nl80211_data *drv = bss->drv;
9511 struct nl_msg *msg;
9512 int ret = -ENOBUFS;
9513
9514 msg = nlmsg_alloc();
9515 if (!msg)
9516 return -ENOMEM;
9517
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009518 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9519 ", ifname=%s[%d], vlan_id=%d)",
9520 bss->ifname, if_nametoindex(bss->ifname),
9521 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009522 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009523
9524 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9525 if_nametoindex(bss->ifname));
9526 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9527 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9528 if_nametoindex(ifname));
9529
9530 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009531 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009532 if (ret < 0) {
9533 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9534 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9535 MAC2STR(addr), ifname, vlan_id, ret,
9536 strerror(-ret));
9537 }
9538 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009539 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009540 return ret;
9541}
9542
9543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009544static int i802_get_inact_sec(void *priv, const u8 *addr)
9545{
9546 struct hostap_sta_driver_data data;
9547 int ret;
9548
9549 data.inactive_msec = (unsigned long) -1;
9550 ret = i802_read_sta_data(priv, &data, addr);
9551 if (ret || data.inactive_msec == (unsigned long) -1)
9552 return -1;
9553 return data.inactive_msec / 1000;
9554}
9555
9556
9557static int i802_sta_clear_stats(void *priv, const u8 *addr)
9558{
9559#if 0
9560 /* TODO */
9561#endif
9562 return 0;
9563}
9564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009565
9566static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9567 int reason)
9568{
9569 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009570 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009571 struct ieee80211_mgmt mgmt;
9572
Dmitry Shmidt04949592012-07-19 12:16:46 -07009573 if (drv->device_ap_sme)
9574 return wpa_driver_nl80211_sta_remove(bss, addr);
9575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009576 memset(&mgmt, 0, sizeof(mgmt));
9577 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9578 WLAN_FC_STYPE_DEAUTH);
9579 memcpy(mgmt.da, addr, ETH_ALEN);
9580 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9581 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9582 mgmt.u.deauth.reason_code = host_to_le16(reason);
9583 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9584 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009585 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9586 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009587}
9588
9589
9590static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9591 int reason)
9592{
9593 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009594 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009595 struct ieee80211_mgmt mgmt;
9596
Dmitry Shmidt04949592012-07-19 12:16:46 -07009597 if (drv->device_ap_sme)
9598 return wpa_driver_nl80211_sta_remove(bss, addr);
9599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009600 memset(&mgmt, 0, sizeof(mgmt));
9601 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9602 WLAN_FC_STYPE_DISASSOC);
9603 memcpy(mgmt.da, addr, ETH_ALEN);
9604 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9605 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9606 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9607 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9608 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009609 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9610 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009611}
9612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009613
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009614static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9615{
9616 char buf[200], *pos, *end;
9617 int i, res;
9618
9619 pos = buf;
9620 end = pos + sizeof(buf);
9621
9622 for (i = 0; i < drv->num_if_indices; i++) {
9623 if (!drv->if_indices[i])
9624 continue;
9625 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9626 if (res < 0 || res >= end - pos)
9627 break;
9628 pos += res;
9629 }
9630 *pos = '\0';
9631
9632 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9633 drv->num_if_indices, buf);
9634}
9635
9636
Jouni Malinen75ecf522011-06-27 15:19:46 -07009637static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9638{
9639 int i;
9640 int *old;
9641
9642 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9643 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009644 if (have_ifidx(drv, ifidx)) {
9645 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9646 ifidx);
9647 return;
9648 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009649 for (i = 0; i < drv->num_if_indices; i++) {
9650 if (drv->if_indices[i] == 0) {
9651 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009652 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009653 return;
9654 }
9655 }
9656
9657 if (drv->if_indices != drv->default_if_indices)
9658 old = drv->if_indices;
9659 else
9660 old = NULL;
9661
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009662 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9663 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009664 if (!drv->if_indices) {
9665 if (!old)
9666 drv->if_indices = drv->default_if_indices;
9667 else
9668 drv->if_indices = old;
9669 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9670 "interfaces");
9671 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9672 return;
9673 } else if (!old)
9674 os_memcpy(drv->if_indices, drv->default_if_indices,
9675 sizeof(drv->default_if_indices));
9676 drv->if_indices[drv->num_if_indices] = ifidx;
9677 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009678 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009679}
9680
9681
9682static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9683{
9684 int i;
9685
9686 for (i = 0; i < drv->num_if_indices; i++) {
9687 if (drv->if_indices[i] == ifidx) {
9688 drv->if_indices[i] = 0;
9689 break;
9690 }
9691 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009692 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009693}
9694
9695
9696static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9697{
9698 int i;
9699
9700 for (i = 0; i < drv->num_if_indices; i++)
9701 if (drv->if_indices[i] == ifidx)
9702 return 1;
9703
9704 return 0;
9705}
9706
9707
9708static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009709 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009710{
9711 struct i802_bss *bss = priv;
9712 struct wpa_driver_nl80211_data *drv = bss->drv;
9713 char name[IFNAMSIZ + 1];
9714
9715 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009716 if (ifname_wds)
9717 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9718
Jouni Malinen75ecf522011-06-27 15:19:46 -07009719 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9720 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9721 if (val) {
9722 if (!if_nametoindex(name)) {
9723 if (nl80211_create_iface(drv, name,
9724 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009725 bss->addr, 1, NULL, NULL, 0) <
9726 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009727 return -1;
9728 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009729 linux_br_add_if(drv->global->ioctl_sock,
9730 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009731 return -1;
9732 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009733 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9734 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9735 "interface %s up", name);
9736 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009737 return i802_set_sta_vlan(priv, addr, name, 0);
9738 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009739 if (bridge_ifname)
9740 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9741 name);
9742
Jouni Malinen75ecf522011-06-27 15:19:46 -07009743 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009744 nl80211_remove_iface(drv, if_nametoindex(name));
9745 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009746 }
9747}
9748
9749
9750static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9751{
9752 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9753 struct sockaddr_ll lladdr;
9754 unsigned char buf[3000];
9755 int len;
9756 socklen_t fromlen = sizeof(lladdr);
9757
9758 len = recvfrom(sock, buf, sizeof(buf), 0,
9759 (struct sockaddr *)&lladdr, &fromlen);
9760 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009761 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9762 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009763 return;
9764 }
9765
9766 if (have_ifidx(drv, lladdr.sll_ifindex))
9767 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9768}
9769
9770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009771static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9772 struct i802_bss *bss,
9773 const char *brname, const char *ifname)
9774{
9775 int ifindex;
9776 char in_br[IFNAMSIZ];
9777
9778 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9779 ifindex = if_nametoindex(brname);
9780 if (ifindex == 0) {
9781 /*
9782 * Bridge was configured, but the bridge device does
9783 * not exist. Try to add it now.
9784 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009785 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009786 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9787 "bridge interface %s: %s",
9788 brname, strerror(errno));
9789 return -1;
9790 }
9791 bss->added_bridge = 1;
9792 add_ifidx(drv, if_nametoindex(brname));
9793 }
9794
9795 if (linux_br_get(in_br, ifname) == 0) {
9796 if (os_strcmp(in_br, brname) == 0)
9797 return 0; /* already in the bridge */
9798
9799 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9800 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009801 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9802 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009803 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9804 "remove interface %s from bridge "
9805 "%s: %s",
9806 ifname, brname, strerror(errno));
9807 return -1;
9808 }
9809 }
9810
9811 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9812 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009813 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9815 "into bridge %s: %s",
9816 ifname, brname, strerror(errno));
9817 return -1;
9818 }
9819 bss->added_if_into_bridge = 1;
9820
9821 return 0;
9822}
9823
9824
9825static void *i802_init(struct hostapd_data *hapd,
9826 struct wpa_init_params *params)
9827{
9828 struct wpa_driver_nl80211_data *drv;
9829 struct i802_bss *bss;
9830 size_t i;
9831 char brname[IFNAMSIZ];
9832 int ifindex, br_ifindex;
9833 int br_added = 0;
9834
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009835 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9836 params->global_priv, 1,
9837 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009838 if (bss == NULL)
9839 return NULL;
9840
9841 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009843 if (linux_br_get(brname, params->ifname) == 0) {
9844 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9845 params->ifname, brname);
9846 br_ifindex = if_nametoindex(brname);
9847 } else {
9848 brname[0] = '\0';
9849 br_ifindex = 0;
9850 }
9851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009852 for (i = 0; i < params->num_bridge; i++) {
9853 if (params->bridge[i]) {
9854 ifindex = if_nametoindex(params->bridge[i]);
9855 if (ifindex)
9856 add_ifidx(drv, ifindex);
9857 if (ifindex == br_ifindex)
9858 br_added = 1;
9859 }
9860 }
9861 if (!br_added && br_ifindex &&
9862 (params->num_bridge == 0 || !params->bridge[0]))
9863 add_ifidx(drv, br_ifindex);
9864
9865 /* start listening for EAPOL on the default AP interface */
9866 add_ifidx(drv, drv->ifindex);
9867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009868 if (params->num_bridge && params->bridge[0] &&
9869 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9870 goto failed;
9871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009872 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9873 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009874 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9875 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009876 goto failed;
9877 }
9878
9879 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9880 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009881 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009882 goto failed;
9883 }
9884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009885 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9886 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009887 goto failed;
9888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009889 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009891 return bss;
9892
9893failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009894 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009895 return NULL;
9896}
9897
9898
9899static void i802_deinit(void *priv)
9900{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009901 struct i802_bss *bss = priv;
9902 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009903}
9904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009905
9906static enum nl80211_iftype wpa_driver_nl80211_if_type(
9907 enum wpa_driver_if_type type)
9908{
9909 switch (type) {
9910 case WPA_IF_STATION:
9911 return NL80211_IFTYPE_STATION;
9912 case WPA_IF_P2P_CLIENT:
9913 case WPA_IF_P2P_GROUP:
9914 return NL80211_IFTYPE_P2P_CLIENT;
9915 case WPA_IF_AP_VLAN:
9916 return NL80211_IFTYPE_AP_VLAN;
9917 case WPA_IF_AP_BSS:
9918 return NL80211_IFTYPE_AP;
9919 case WPA_IF_P2P_GO:
9920 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009921 case WPA_IF_P2P_DEVICE:
9922 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009923 }
9924 return -1;
9925}
9926
9927
9928#ifdef CONFIG_P2P
9929
9930static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9931{
9932 struct wpa_driver_nl80211_data *drv;
9933 dl_list_for_each(drv, &global->interfaces,
9934 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009935 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009936 return 1;
9937 }
9938 return 0;
9939}
9940
9941
9942static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9943 u8 *new_addr)
9944{
9945 unsigned int idx;
9946
9947 if (!drv->global)
9948 return -1;
9949
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009950 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009951 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009952 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009953 new_addr[0] ^= idx << 2;
9954 if (!nl80211_addr_in_use(drv->global, new_addr))
9955 break;
9956 }
9957 if (idx == 64)
9958 return -1;
9959
9960 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9961 MACSTR, MAC2STR(new_addr));
9962
9963 return 0;
9964}
9965
9966#endif /* CONFIG_P2P */
9967
9968
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009969struct wdev_info {
9970 u64 wdev_id;
9971 int wdev_id_set;
9972 u8 macaddr[ETH_ALEN];
9973};
9974
9975static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9976{
9977 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9978 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9979 struct wdev_info *wi = arg;
9980
9981 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9982 genlmsg_attrlen(gnlh, 0), NULL);
9983 if (tb[NL80211_ATTR_WDEV]) {
9984 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9985 wi->wdev_id_set = 1;
9986 }
9987
9988 if (tb[NL80211_ATTR_MAC])
9989 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9990 ETH_ALEN);
9991
9992 return NL_SKIP;
9993}
9994
9995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009996static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
9997 const char *ifname, const u8 *addr,
9998 void *bss_ctx, void **drv_priv,
9999 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010000 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010001{
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010002 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010003 struct i802_bss *bss = priv;
10004 struct wpa_driver_nl80211_data *drv = bss->drv;
10005 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010006 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010007
10008 if (addr)
10009 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010010 nlmode = wpa_driver_nl80211_if_type(type);
10011 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
10012 struct wdev_info p2pdev_info;
10013
10014 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
10015 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
10016 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010017 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010018 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
10019 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10020 ifname);
10021 return -1;
10022 }
10023
10024 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10025 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10026 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10027 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10028 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10029 ifname,
10030 (long long unsigned int) p2pdev_info.wdev_id);
10031 } else {
10032 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010033 0, NULL, NULL, use_existing);
10034 if (use_existing && ifidx == -ENFILE) {
10035 added = 0;
10036 ifidx = if_nametoindex(ifname);
10037 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010038 return -1;
10039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010040 }
10041
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010042 if (!addr) {
10043 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10044 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10045 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10046 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010047 if (added)
10048 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010049 return -1;
10050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010051 }
10052
10053#ifdef CONFIG_P2P
10054 if (!addr &&
10055 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10056 type == WPA_IF_P2P_GO)) {
10057 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010058 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010059
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010060 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010061 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010062 nl80211_remove_iface(drv, ifidx);
10063 return -1;
10064 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010065 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010066 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10067 "for P2P group interface");
10068 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
10069 nl80211_remove_iface(drv, ifidx);
10070 return -1;
10071 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010072 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010073 new_addr) < 0) {
10074 nl80211_remove_iface(drv, ifidx);
10075 return -1;
10076 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010077 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010078 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010079 }
10080#endif /* CONFIG_P2P */
10081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010082 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010083 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10084 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010085 if (added)
10086 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010087 return -1;
10088 }
10089
10090 if (bridge &&
10091 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10092 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10093 "interface %s to a bridge %s",
10094 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010095 if (added)
10096 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010097 os_free(new_bss);
10098 return -1;
10099 }
10100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010101 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10102 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010103 nl80211_remove_iface(drv, ifidx);
10104 os_free(new_bss);
10105 return -1;
10106 }
10107 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010108 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010109 new_bss->ifindex = ifidx;
10110 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010111 new_bss->next = drv->first_bss->next;
10112 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010113 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010114 new_bss->added_if = added;
10115 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010116 if (drv_priv)
10117 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010118 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010119
10120 /* Subscribe management frames for this WPA_IF_AP_BSS */
10121 if (nl80211_setup_ap(new_bss))
10122 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010125 if (drv->global)
10126 drv->global->if_add_ifindex = ifidx;
10127
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010128 if (ifidx > 0)
10129 add_ifidx(drv, ifidx);
10130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010131 return 0;
10132}
10133
10134
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010135static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010136 enum wpa_driver_if_type type,
10137 const char *ifname)
10138{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010139 struct wpa_driver_nl80211_data *drv = bss->drv;
10140 int ifindex = if_nametoindex(ifname);
10141
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010142 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10143 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010144 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010145 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010146 else if (ifindex > 0 && !bss->added_if)
10147 del_ifidx(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010148
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010149 if (type != WPA_IF_AP_BSS)
10150 return 0;
10151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010152 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010153 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10154 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010155 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10156 "interface %s from bridge %s: %s",
10157 bss->ifname, bss->brname, strerror(errno));
10158 }
10159 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010160 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010161 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10162 "bridge %s: %s",
10163 bss->brname, strerror(errno));
10164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010165
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010166 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010167 struct i802_bss *tbss;
10168
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010169 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10170 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010171 if (tbss->next == bss) {
10172 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010173 /* Unsubscribe management frames */
10174 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010175 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010176 if (!bss->added_if)
10177 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010178 os_free(bss);
10179 bss = NULL;
10180 break;
10181 }
10182 }
10183 if (bss)
10184 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10185 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010186 } else {
10187 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10188 nl80211_teardown_ap(bss);
10189 if (!bss->added_if && !drv->first_bss->next)
10190 wpa_driver_nl80211_del_beacon(drv);
10191 nl80211_destroy_bss(bss);
10192 if (!bss->added_if)
10193 i802_set_iface_flags(bss, 0);
10194 if (drv->first_bss->next) {
10195 drv->first_bss = drv->first_bss->next;
10196 drv->ctx = drv->first_bss->ctx;
10197 os_free(bss);
10198 } else {
10199 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010202
10203 return 0;
10204}
10205
10206
10207static int cookie_handler(struct nl_msg *msg, void *arg)
10208{
10209 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10210 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10211 u64 *cookie = arg;
10212 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10213 genlmsg_attrlen(gnlh, 0), NULL);
10214 if (tb[NL80211_ATTR_COOKIE])
10215 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10216 return NL_SKIP;
10217}
10218
10219
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010220static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010221 unsigned int freq, unsigned int wait,
10222 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010223 u64 *cookie_out, int no_cck, int no_ack,
10224 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010225{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010226 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010227 struct nl_msg *msg;
10228 u64 cookie;
10229 int ret = -1;
10230
10231 msg = nlmsg_alloc();
10232 if (!msg)
10233 return -1;
10234
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010235 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010236 "no_ack=%d offchanok=%d",
10237 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010238 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010239 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010240
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010241 if (nl80211_set_iface_id(msg, bss) < 0)
10242 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010243 if (freq)
10244 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010245 if (wait)
10246 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010247 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10248 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010249 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10250 if (no_cck)
10251 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10252 if (no_ack)
10253 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010255 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10256
10257 cookie = 0;
10258 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10259 msg = NULL;
10260 if (ret) {
10261 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010262 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10263 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010264 goto nla_put_failure;
10265 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010266 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010267 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10268 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010269
10270 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010271 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010272
10273nla_put_failure:
10274 nlmsg_free(msg);
10275 return ret;
10276}
10277
10278
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010279static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10280 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010281 unsigned int wait_time,
10282 const u8 *dst, const u8 *src,
10283 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010284 const u8 *data, size_t data_len,
10285 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010286{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010287 struct wpa_driver_nl80211_data *drv = bss->drv;
10288 int ret = -1;
10289 u8 *buf;
10290 struct ieee80211_hdr *hdr;
10291
10292 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010293 "freq=%u MHz wait=%d ms no_cck=%d)",
10294 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010295
10296 buf = os_zalloc(24 + data_len);
10297 if (buf == NULL)
10298 return ret;
10299 os_memcpy(buf + 24, data, data_len);
10300 hdr = (struct ieee80211_hdr *) buf;
10301 hdr->frame_control =
10302 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10303 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10304 os_memcpy(hdr->addr2, src, ETH_ALEN);
10305 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10306
Dmitry Shmidt56052862013-10-04 10:23:25 -070010307 if (is_ap_interface(drv->nlmode) &&
10308 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10309 (int) freq == bss->freq || drv->device_ap_sme ||
10310 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010311 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10312 0, freq, no_cck, 1,
10313 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010314 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010315 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010316 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010317 &drv->send_action_cookie,
10318 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010319
10320 os_free(buf);
10321 return ret;
10322}
10323
10324
10325static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10326{
10327 struct i802_bss *bss = priv;
10328 struct wpa_driver_nl80211_data *drv = bss->drv;
10329 struct nl_msg *msg;
10330 int ret;
10331
10332 msg = nlmsg_alloc();
10333 if (!msg)
10334 return;
10335
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010336 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10337 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010338 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010339
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010340 if (nl80211_set_iface_id(msg, bss) < 0)
10341 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010342 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10343
10344 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10345 msg = NULL;
10346 if (ret)
10347 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10348 "(%s)", ret, strerror(-ret));
10349
10350 nla_put_failure:
10351 nlmsg_free(msg);
10352}
10353
10354
10355static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10356 unsigned int duration)
10357{
10358 struct i802_bss *bss = priv;
10359 struct wpa_driver_nl80211_data *drv = bss->drv;
10360 struct nl_msg *msg;
10361 int ret;
10362 u64 cookie;
10363
10364 msg = nlmsg_alloc();
10365 if (!msg)
10366 return -1;
10367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010368 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010369
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010370 if (nl80211_set_iface_id(msg, bss) < 0)
10371 goto nla_put_failure;
10372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010373 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10374 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10375
10376 cookie = 0;
10377 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010378 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010379 if (ret == 0) {
10380 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10381 "0x%llx for freq=%u MHz duration=%u",
10382 (long long unsigned int) cookie, freq, duration);
10383 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010384 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010385 return 0;
10386 }
10387 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10388 "(freq=%d duration=%u): %d (%s)",
10389 freq, duration, ret, strerror(-ret));
10390nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010391 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010392 return -1;
10393}
10394
10395
10396static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10397{
10398 struct i802_bss *bss = priv;
10399 struct wpa_driver_nl80211_data *drv = bss->drv;
10400 struct nl_msg *msg;
10401 int ret;
10402
10403 if (!drv->pending_remain_on_chan) {
10404 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10405 "to cancel");
10406 return -1;
10407 }
10408
10409 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10410 "0x%llx",
10411 (long long unsigned int) drv->remain_on_chan_cookie);
10412
10413 msg = nlmsg_alloc();
10414 if (!msg)
10415 return -1;
10416
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010417 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010418
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010419 if (nl80211_set_iface_id(msg, bss) < 0)
10420 goto nla_put_failure;
10421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010422 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10423
10424 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010425 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010426 if (ret == 0)
10427 return 0;
10428 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10429 "%d (%s)", ret, strerror(-ret));
10430nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010431 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010432 return -1;
10433}
10434
10435
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010436static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010437{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010438 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010440 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010441 if (bss->nl_preq && drv->device_ap_sme &&
10442 is_ap_interface(drv->nlmode)) {
10443 /*
10444 * Do not disable Probe Request reporting that was
10445 * enabled in nl80211_setup_ap().
10446 */
10447 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10448 "Probe Request reporting nl_preq=%p while "
10449 "in AP mode", bss->nl_preq);
10450 } else if (bss->nl_preq) {
10451 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10452 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010453 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010454 }
10455 return 0;
10456 }
10457
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010458 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010459 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010460 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010461 return 0;
10462 }
10463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010464 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10465 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010466 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010467 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10468 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010469
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010470 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010471 (WLAN_FC_TYPE_MGMT << 2) |
10472 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010473 NULL, 0) < 0)
10474 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010475
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010476 nl80211_register_eloop_read(&bss->nl_preq,
10477 wpa_driver_nl80211_event_receive,
10478 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010479
10480 return 0;
10481
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010482 out_err:
10483 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010484 return -1;
10485}
10486
10487
10488static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10489 int ifindex, int disabled)
10490{
10491 struct nl_msg *msg;
10492 struct nlattr *bands, *band;
10493 int ret;
10494
10495 msg = nlmsg_alloc();
10496 if (!msg)
10497 return -1;
10498
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010499 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010500 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10501
10502 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10503 if (!bands)
10504 goto nla_put_failure;
10505
10506 /*
10507 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10508 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10509 * rates. All 5 GHz rates are left enabled.
10510 */
10511 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10512 if (!band)
10513 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010514 if (disabled) {
10515 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10516 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010518 nla_nest_end(msg, band);
10519
10520 nla_nest_end(msg, bands);
10521
10522 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10523 msg = NULL;
10524 if (ret) {
10525 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10526 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010527 } else
10528 drv->disabled_11b_rates = disabled;
10529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010530 return ret;
10531
10532nla_put_failure:
10533 nlmsg_free(msg);
10534 return -1;
10535}
10536
10537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010538static int wpa_driver_nl80211_deinit_ap(void *priv)
10539{
10540 struct i802_bss *bss = priv;
10541 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010542 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010543 return -1;
10544 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010545
10546 /*
10547 * If the P2P GO interface was dynamically added, then it is
10548 * possible that the interface change to station is not possible.
10549 */
10550 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10551 return 0;
10552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010553 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010554}
10555
10556
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010557static int wpa_driver_nl80211_stop_ap(void *priv)
10558{
10559 struct i802_bss *bss = priv;
10560 struct wpa_driver_nl80211_data *drv = bss->drv;
10561 if (!is_ap_interface(drv->nlmode))
10562 return -1;
10563 wpa_driver_nl80211_del_beacon(drv);
10564 bss->beacon_set = 0;
10565 return 0;
10566}
10567
10568
Dmitry Shmidt04949592012-07-19 12:16:46 -070010569static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10570{
10571 struct i802_bss *bss = priv;
10572 struct wpa_driver_nl80211_data *drv = bss->drv;
10573 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10574 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010575
10576 /*
10577 * If the P2P Client interface was dynamically added, then it is
10578 * possible that the interface change to station is not possible.
10579 */
10580 if (bss->if_dynamic)
10581 return 0;
10582
Dmitry Shmidt04949592012-07-19 12:16:46 -070010583 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10584}
10585
10586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010587static void wpa_driver_nl80211_resume(void *priv)
10588{
10589 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010590
10591 if (i802_set_iface_flags(bss, 1))
10592 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010593}
10594
10595
10596static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10597 const u8 *ies, size_t ies_len)
10598{
10599 struct i802_bss *bss = priv;
10600 struct wpa_driver_nl80211_data *drv = bss->drv;
10601 int ret;
10602 u8 *data, *pos;
10603 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010604 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010605
10606 if (action != 1) {
10607 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10608 "action %d", action);
10609 return -1;
10610 }
10611
10612 /*
10613 * Action frame payload:
10614 * Category[1] = 6 (Fast BSS Transition)
10615 * Action[1] = 1 (Fast BSS Transition Request)
10616 * STA Address
10617 * Target AP Address
10618 * FT IEs
10619 */
10620
10621 data_len = 2 + 2 * ETH_ALEN + ies_len;
10622 data = os_malloc(data_len);
10623 if (data == NULL)
10624 return -1;
10625 pos = data;
10626 *pos++ = 0x06; /* FT Action category */
10627 *pos++ = action;
10628 os_memcpy(pos, own_addr, ETH_ALEN);
10629 pos += ETH_ALEN;
10630 os_memcpy(pos, target_ap, ETH_ALEN);
10631 pos += ETH_ALEN;
10632 os_memcpy(pos, ies, ies_len);
10633
10634 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10635 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010636 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010637 os_free(data);
10638
10639 return ret;
10640}
10641
10642
10643static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10644{
10645 struct i802_bss *bss = priv;
10646 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010647 struct nl_msg *msg;
10648 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010649 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010650
10651 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10652 "hysteresis=%d", threshold, hysteresis);
10653
10654 msg = nlmsg_alloc();
10655 if (!msg)
10656 return -1;
10657
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010658 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010659
10660 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10661
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010662 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010663 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010664 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010665
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010666 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10667 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10668 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010669
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010670 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010671 msg = NULL;
10672
10673nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010674 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010675 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010676}
10677
10678
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010679static int get_channel_width(struct nl_msg *msg, void *arg)
10680{
10681 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10682 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10683 struct wpa_signal_info *sig_change = arg;
10684
10685 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10686 genlmsg_attrlen(gnlh, 0), NULL);
10687
10688 sig_change->center_frq1 = -1;
10689 sig_change->center_frq2 = -1;
10690 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10691
10692 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10693 sig_change->chanwidth = convert2width(
10694 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10695 if (tb[NL80211_ATTR_CENTER_FREQ1])
10696 sig_change->center_frq1 =
10697 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10698 if (tb[NL80211_ATTR_CENTER_FREQ2])
10699 sig_change->center_frq2 =
10700 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10701 }
10702
10703 return NL_SKIP;
10704}
10705
10706
10707static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10708 struct wpa_signal_info *sig)
10709{
10710 struct nl_msg *msg;
10711
10712 msg = nlmsg_alloc();
10713 if (!msg)
10714 return -ENOMEM;
10715
10716 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10717 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10718
10719 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10720
10721nla_put_failure:
10722 nlmsg_free(msg);
10723 return -ENOBUFS;
10724}
10725
10726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010727static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10728{
10729 struct i802_bss *bss = priv;
10730 struct wpa_driver_nl80211_data *drv = bss->drv;
10731 int res;
10732
10733 os_memset(si, 0, sizeof(*si));
10734 res = nl80211_get_link_signal(drv, si);
10735 if (res != 0)
10736 return res;
10737
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010738 res = nl80211_get_channel_width(drv, si);
10739 if (res != 0)
10740 return res;
10741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010742 return nl80211_get_link_noise(drv, si);
10743}
10744
10745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010746static int wpa_driver_nl80211_shared_freq(void *priv)
10747{
10748 struct i802_bss *bss = priv;
10749 struct wpa_driver_nl80211_data *drv = bss->drv;
10750 struct wpa_driver_nl80211_data *driver;
10751 int freq = 0;
10752
10753 /*
10754 * If the same PHY is in connected state with some other interface,
10755 * then retrieve the assoc freq.
10756 */
10757 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10758 drv->phyname);
10759
10760 dl_list_for_each(driver, &drv->global->interfaces,
10761 struct wpa_driver_nl80211_data, list) {
10762 if (drv == driver ||
10763 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010764 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010765 continue;
10766
10767 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10768 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010769 driver->phyname, driver->first_bss->ifname,
10770 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010771 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010772 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010773 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010774 freq = nl80211_get_assoc_freq(driver);
10775 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10776 drv->phyname, freq);
10777 }
10778
10779 if (!freq)
10780 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10781 "PHY (%s) in associated state", drv->phyname);
10782
10783 return freq;
10784}
10785
10786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010787static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10788 int encrypt)
10789{
10790 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010791 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10792 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010793}
10794
10795
10796static int nl80211_set_param(void *priv, const char *param)
10797{
10798 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10799 if (param == NULL)
10800 return 0;
10801
10802#ifdef CONFIG_P2P
10803 if (os_strstr(param, "use_p2p_group_interface=1")) {
10804 struct i802_bss *bss = priv;
10805 struct wpa_driver_nl80211_data *drv = bss->drv;
10806
10807 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10808 "interface");
10809 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10810 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10811 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010812
10813 if (os_strstr(param, "p2p_device=1")) {
10814 struct i802_bss *bss = priv;
10815 struct wpa_driver_nl80211_data *drv = bss->drv;
10816 drv->allow_p2p_device = 1;
10817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010818#endif /* CONFIG_P2P */
10819
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010820 if (os_strstr(param, "use_monitor=1")) {
10821 struct i802_bss *bss = priv;
10822 struct wpa_driver_nl80211_data *drv = bss->drv;
10823 drv->use_monitor = 1;
10824 }
10825
10826 if (os_strstr(param, "force_connect_cmd=1")) {
10827 struct i802_bss *bss = priv;
10828 struct wpa_driver_nl80211_data *drv = bss->drv;
10829 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10830 }
10831
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010832 if (os_strstr(param, "no_offchannel_tx=1")) {
10833 struct i802_bss *bss = priv;
10834 struct wpa_driver_nl80211_data *drv = bss->drv;
10835 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10836 drv->test_use_roc_tx = 1;
10837 }
10838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010839 return 0;
10840}
10841
10842
10843static void * nl80211_global_init(void)
10844{
10845 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010846 struct netlink_config *cfg;
10847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010848 global = os_zalloc(sizeof(*global));
10849 if (global == NULL)
10850 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010851 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010852 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010853 global->if_add_ifindex = -1;
10854
10855 cfg = os_zalloc(sizeof(*cfg));
10856 if (cfg == NULL)
10857 goto err;
10858
10859 cfg->ctx = global;
10860 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10861 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10862 global->netlink = netlink_init(cfg);
10863 if (global->netlink == NULL) {
10864 os_free(cfg);
10865 goto err;
10866 }
10867
10868 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10869 goto err;
10870
10871 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10872 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010873 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10874 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010875 goto err;
10876 }
10877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010878 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010879
10880err:
10881 nl80211_global_deinit(global);
10882 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010883}
10884
10885
10886static void nl80211_global_deinit(void *priv)
10887{
10888 struct nl80211_global *global = priv;
10889 if (global == NULL)
10890 return;
10891 if (!dl_list_empty(&global->interfaces)) {
10892 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10893 "nl80211_global_deinit",
10894 dl_list_len(&global->interfaces));
10895 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010896
10897 if (global->netlink)
10898 netlink_deinit(global->netlink);
10899
10900 nl_destroy_handles(&global->nl);
10901
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010902 if (global->nl_event)
10903 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010904
10905 nl_cb_put(global->nl_cb);
10906
10907 if (global->ioctl_sock >= 0)
10908 close(global->ioctl_sock);
10909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010910 os_free(global);
10911}
10912
10913
10914static const char * nl80211_get_radio_name(void *priv)
10915{
10916 struct i802_bss *bss = priv;
10917 struct wpa_driver_nl80211_data *drv = bss->drv;
10918 return drv->phyname;
10919}
10920
10921
Jouni Malinen75ecf522011-06-27 15:19:46 -070010922static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10923 const u8 *pmkid)
10924{
10925 struct nl_msg *msg;
10926
10927 msg = nlmsg_alloc();
10928 if (!msg)
10929 return -ENOMEM;
10930
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010931 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010932
10933 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10934 if (pmkid)
10935 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10936 if (bssid)
10937 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10938
10939 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10940 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010941 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010942 return -ENOBUFS;
10943}
10944
10945
10946static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10947{
10948 struct i802_bss *bss = priv;
10949 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10950 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10951}
10952
10953
10954static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10955{
10956 struct i802_bss *bss = priv;
10957 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10958 MAC2STR(bssid));
10959 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10960}
10961
10962
10963static int nl80211_flush_pmkid(void *priv)
10964{
10965 struct i802_bss *bss = priv;
10966 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10967 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10968}
10969
10970
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010971static void clean_survey_results(struct survey_results *survey_results)
10972{
10973 struct freq_survey *survey, *tmp;
10974
10975 if (dl_list_empty(&survey_results->survey_list))
10976 return;
10977
10978 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10979 struct freq_survey, list) {
10980 dl_list_del(&survey->list);
10981 os_free(survey);
10982 }
10983}
10984
10985
10986static void add_survey(struct nlattr **sinfo, u32 ifidx,
10987 struct dl_list *survey_list)
10988{
10989 struct freq_survey *survey;
10990
10991 survey = os_zalloc(sizeof(struct freq_survey));
10992 if (!survey)
10993 return;
10994
10995 survey->ifidx = ifidx;
10996 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
10997 survey->filled = 0;
10998
10999 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11000 survey->nf = (int8_t)
11001 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11002 survey->filled |= SURVEY_HAS_NF;
11003 }
11004
11005 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11006 survey->channel_time =
11007 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11008 survey->filled |= SURVEY_HAS_CHAN_TIME;
11009 }
11010
11011 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11012 survey->channel_time_busy =
11013 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11014 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11015 }
11016
11017 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11018 survey->channel_time_rx =
11019 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11020 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11021 }
11022
11023 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11024 survey->channel_time_tx =
11025 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11026 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11027 }
11028
11029 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)",
11030 survey->freq,
11031 survey->nf,
11032 (unsigned long int) survey->channel_time,
11033 (unsigned long int) survey->channel_time_busy,
11034 (unsigned long int) survey->channel_time_tx,
11035 (unsigned long int) survey->channel_time_rx,
11036 survey->filled);
11037
11038 dl_list_add_tail(survey_list, &survey->list);
11039}
11040
11041
11042static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11043 unsigned int freq_filter)
11044{
11045 if (!freq_filter)
11046 return 1;
11047
11048 return freq_filter == surveyed_freq;
11049}
11050
11051
11052static int survey_handler(struct nl_msg *msg, void *arg)
11053{
11054 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11055 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11056 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11057 struct survey_results *survey_results;
11058 u32 surveyed_freq = 0;
11059 u32 ifidx;
11060
11061 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11062 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11063 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11064 };
11065
11066 survey_results = (struct survey_results *) arg;
11067
11068 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11069 genlmsg_attrlen(gnlh, 0), NULL);
11070
Dmitry Shmidt97672262014-02-03 13:02:54 -080011071 if (!tb[NL80211_ATTR_IFINDEX])
11072 return NL_SKIP;
11073
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011074 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11075
11076 if (!tb[NL80211_ATTR_SURVEY_INFO])
11077 return NL_SKIP;
11078
11079 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11080 tb[NL80211_ATTR_SURVEY_INFO],
11081 survey_policy))
11082 return NL_SKIP;
11083
11084 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11085 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11086 return NL_SKIP;
11087 }
11088
11089 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11090
11091 if (!check_survey_ok(sinfo, surveyed_freq,
11092 survey_results->freq_filter))
11093 return NL_SKIP;
11094
11095 if (survey_results->freq_filter &&
11096 survey_results->freq_filter != surveyed_freq) {
11097 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11098 surveyed_freq);
11099 return NL_SKIP;
11100 }
11101
11102 add_survey(sinfo, ifidx, &survey_results->survey_list);
11103
11104 return NL_SKIP;
11105}
11106
11107
11108static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11109{
11110 struct i802_bss *bss = priv;
11111 struct wpa_driver_nl80211_data *drv = bss->drv;
11112 struct nl_msg *msg;
11113 int err = -ENOBUFS;
11114 union wpa_event_data data;
11115 struct survey_results *survey_results;
11116
11117 os_memset(&data, 0, sizeof(data));
11118 survey_results = &data.survey_results;
11119
11120 dl_list_init(&survey_results->survey_list);
11121
11122 msg = nlmsg_alloc();
11123 if (!msg)
11124 goto nla_put_failure;
11125
11126 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11127
11128 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11129
11130 if (freq)
11131 data.survey_results.freq_filter = freq;
11132
11133 do {
11134 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11135 err = send_and_recv_msgs(drv, msg, survey_handler,
11136 survey_results);
11137 } while (err > 0);
11138
11139 if (err) {
11140 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11141 goto out_clean;
11142 }
11143
11144 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11145
11146out_clean:
11147 clean_survey_results(survey_results);
11148nla_put_failure:
11149 return err;
11150}
11151
11152
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011153static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11154 const u8 *replay_ctr)
11155{
11156 struct i802_bss *bss = priv;
11157 struct wpa_driver_nl80211_data *drv = bss->drv;
11158 struct nlattr *replay_nested;
11159 struct nl_msg *msg;
11160
11161 msg = nlmsg_alloc();
11162 if (!msg)
11163 return;
11164
11165 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11166
11167 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11168
11169 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11170 if (!replay_nested)
11171 goto nla_put_failure;
11172
11173 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11174 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11175 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11176 replay_ctr);
11177
11178 nla_nest_end(msg, replay_nested);
11179
11180 send_and_recv_msgs(drv, msg, NULL, NULL);
11181 return;
11182 nla_put_failure:
11183 nlmsg_free(msg);
11184}
11185
11186
11187static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11188 const u8 *addr, int qos)
11189{
11190 /* send data frame to poll STA and check whether
11191 * this frame is ACKed */
11192 struct {
11193 struct ieee80211_hdr hdr;
11194 u16 qos_ctl;
11195 } STRUCT_PACKED nulldata;
11196 size_t size;
11197
11198 /* Send data frame to poll STA and check whether this frame is ACKed */
11199
11200 os_memset(&nulldata, 0, sizeof(nulldata));
11201
11202 if (qos) {
11203 nulldata.hdr.frame_control =
11204 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11205 WLAN_FC_STYPE_QOS_NULL);
11206 size = sizeof(nulldata);
11207 } else {
11208 nulldata.hdr.frame_control =
11209 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11210 WLAN_FC_STYPE_NULLFUNC);
11211 size = sizeof(struct ieee80211_hdr);
11212 }
11213
11214 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11215 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11216 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11217 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11218
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011219 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11220 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011221 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11222 "send poll frame");
11223}
11224
11225static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11226 int qos)
11227{
11228 struct i802_bss *bss = priv;
11229 struct wpa_driver_nl80211_data *drv = bss->drv;
11230 struct nl_msg *msg;
11231
11232 if (!drv->poll_command_supported) {
11233 nl80211_send_null_frame(bss, own_addr, addr, qos);
11234 return;
11235 }
11236
11237 msg = nlmsg_alloc();
11238 if (!msg)
11239 return;
11240
11241 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11242
11243 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11244 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11245
11246 send_and_recv_msgs(drv, msg, NULL, NULL);
11247 return;
11248 nla_put_failure:
11249 nlmsg_free(msg);
11250}
11251
11252
11253static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11254{
11255 struct nl_msg *msg;
11256
11257 msg = nlmsg_alloc();
11258 if (!msg)
11259 return -ENOMEM;
11260
11261 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11263 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11264 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11265 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11266nla_put_failure:
11267 nlmsg_free(msg);
11268 return -ENOBUFS;
11269}
11270
11271
11272static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11273 int ctwindow)
11274{
11275 struct i802_bss *bss = priv;
11276
11277 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11278 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11279
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011280 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011281#ifdef ANDROID_P2P
11282 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011283#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011284 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011285#endif /* ANDROID_P2P */
11286 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011287
11288 if (legacy_ps == -1)
11289 return 0;
11290 if (legacy_ps != 0 && legacy_ps != 1)
11291 return -1; /* Not yet supported */
11292
11293 return nl80211_set_power_save(bss, legacy_ps);
11294}
11295
11296
Dmitry Shmidt051af732013-10-22 13:52:46 -070011297static int nl80211_start_radar_detection(void *priv,
11298 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011299{
11300 struct i802_bss *bss = priv;
11301 struct wpa_driver_nl80211_data *drv = bss->drv;
11302 struct nl_msg *msg;
11303 int ret;
11304
Dmitry Shmidt051af732013-10-22 13:52:46 -070011305 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)",
11306 freq->freq, freq->ht_enabled, freq->vht_enabled,
11307 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11308
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011309 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11310 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11311 "detection");
11312 return -1;
11313 }
11314
11315 msg = nlmsg_alloc();
11316 if (!msg)
11317 return -1;
11318
11319 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11320 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070011321 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011322
Dmitry Shmidt051af732013-10-22 13:52:46 -070011323 if (freq->vht_enabled) {
11324 switch (freq->bandwidth) {
11325 case 20:
11326 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11327 NL80211_CHAN_WIDTH_20);
11328 break;
11329 case 40:
11330 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11331 NL80211_CHAN_WIDTH_40);
11332 break;
11333 case 80:
11334 if (freq->center_freq2)
11335 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11336 NL80211_CHAN_WIDTH_80P80);
11337 else
11338 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11339 NL80211_CHAN_WIDTH_80);
11340 break;
11341 case 160:
11342 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11343 NL80211_CHAN_WIDTH_160);
11344 break;
11345 default:
11346 return -1;
11347 }
11348 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11349 if (freq->center_freq2)
11350 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11351 freq->center_freq2);
11352 } else if (freq->ht_enabled) {
11353 switch (freq->sec_channel_offset) {
11354 case -1:
11355 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11356 NL80211_CHAN_HT40MINUS);
11357 break;
11358 case 1:
11359 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11360 NL80211_CHAN_HT40PLUS);
11361 break;
11362 default:
11363 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11364 NL80211_CHAN_HT20);
11365 break;
11366 }
11367 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011368
11369 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11370 if (ret == 0)
11371 return 0;
11372 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11373 "%d (%s)", ret, strerror(-ret));
11374nla_put_failure:
11375 return -1;
11376}
11377
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011378#ifdef CONFIG_TDLS
11379
11380static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11381 u8 dialog_token, u16 status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011382 u32 peer_capab, const u8 *buf, size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011383{
11384 struct i802_bss *bss = priv;
11385 struct wpa_driver_nl80211_data *drv = bss->drv;
11386 struct nl_msg *msg;
11387
11388 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11389 return -EOPNOTSUPP;
11390
11391 if (!dst)
11392 return -EINVAL;
11393
11394 msg = nlmsg_alloc();
11395 if (!msg)
11396 return -ENOMEM;
11397
11398 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11399 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11400 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11401 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11402 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11403 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011404 if (peer_capab) {
11405 /*
11406 * The internal enum tdls_peer_capability definition is
11407 * currently identical with the nl80211 enum
11408 * nl80211_tdls_peer_capability, so no conversion is needed
11409 * here.
11410 */
11411 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11412 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011413 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11414
11415 return send_and_recv_msgs(drv, msg, NULL, NULL);
11416
11417nla_put_failure:
11418 nlmsg_free(msg);
11419 return -ENOBUFS;
11420}
11421
11422
11423static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11424{
11425 struct i802_bss *bss = priv;
11426 struct wpa_driver_nl80211_data *drv = bss->drv;
11427 struct nl_msg *msg;
11428 enum nl80211_tdls_operation nl80211_oper;
11429
11430 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11431 return -EOPNOTSUPP;
11432
11433 switch (oper) {
11434 case TDLS_DISCOVERY_REQ:
11435 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11436 break;
11437 case TDLS_SETUP:
11438 nl80211_oper = NL80211_TDLS_SETUP;
11439 break;
11440 case TDLS_TEARDOWN:
11441 nl80211_oper = NL80211_TDLS_TEARDOWN;
11442 break;
11443 case TDLS_ENABLE_LINK:
11444 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11445 break;
11446 case TDLS_DISABLE_LINK:
11447 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11448 break;
11449 case TDLS_ENABLE:
11450 return 0;
11451 case TDLS_DISABLE:
11452 return 0;
11453 default:
11454 return -EINVAL;
11455 }
11456
11457 msg = nlmsg_alloc();
11458 if (!msg)
11459 return -ENOMEM;
11460
11461 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11462 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11463 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11464 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11465
11466 return send_and_recv_msgs(drv, msg, NULL, NULL);
11467
11468nla_put_failure:
11469 nlmsg_free(msg);
11470 return -ENOBUFS;
11471}
11472
11473#endif /* CONFIG TDLS */
11474
11475
11476#ifdef ANDROID
11477
11478typedef struct android_wifi_priv_cmd {
11479 char *buf;
11480 int used_len;
11481 int total_len;
11482} android_wifi_priv_cmd;
11483
11484static int drv_errors = 0;
11485
11486static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11487{
11488 drv_errors++;
11489 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11490 drv_errors = 0;
11491 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11492 }
11493}
11494
11495
11496static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11497{
11498 struct wpa_driver_nl80211_data *drv = bss->drv;
11499 struct ifreq ifr;
11500 android_wifi_priv_cmd priv_cmd;
11501 char buf[MAX_DRV_CMD_SIZE];
11502 int ret;
11503
11504 os_memset(&ifr, 0, sizeof(ifr));
11505 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11506 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11507
11508 os_memset(buf, 0, sizeof(buf));
11509 os_strlcpy(buf, cmd, sizeof(buf));
11510
11511 priv_cmd.buf = buf;
11512 priv_cmd.used_len = sizeof(buf);
11513 priv_cmd.total_len = sizeof(buf);
11514 ifr.ifr_data = &priv_cmd;
11515
11516 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11517 if (ret < 0) {
11518 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11519 __func__);
11520 wpa_driver_send_hang_msg(drv);
11521 return ret;
11522 }
11523
11524 drv_errors = 0;
11525 return 0;
11526}
11527
11528
11529static int android_pno_start(struct i802_bss *bss,
11530 struct wpa_driver_scan_params *params)
11531{
11532 struct wpa_driver_nl80211_data *drv = bss->drv;
11533 struct ifreq ifr;
11534 android_wifi_priv_cmd priv_cmd;
11535 int ret = 0, i = 0, bp;
11536 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11537
11538 bp = WEXT_PNOSETUP_HEADER_SIZE;
11539 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11540 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11541 buf[bp++] = WEXT_PNO_TLV_VERSION;
11542 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11543 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11544
11545 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11546 /* Check that there is enough space needed for 1 more SSID, the
11547 * other sections and null termination */
11548 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11549 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11550 break;
11551 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11552 params->ssids[i].ssid,
11553 params->ssids[i].ssid_len);
11554 buf[bp++] = WEXT_PNO_SSID_SECTION;
11555 buf[bp++] = params->ssids[i].ssid_len;
11556 os_memcpy(&buf[bp], params->ssids[i].ssid,
11557 params->ssids[i].ssid_len);
11558 bp += params->ssids[i].ssid_len;
11559 i++;
11560 }
11561
11562 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11563 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11564 WEXT_PNO_SCAN_INTERVAL);
11565 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11566
11567 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11568 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11569 WEXT_PNO_REPEAT);
11570 bp += WEXT_PNO_REPEAT_LENGTH;
11571
11572 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11573 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11574 WEXT_PNO_MAX_REPEAT);
11575 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11576
11577 memset(&ifr, 0, sizeof(ifr));
11578 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011579 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011580
11581 priv_cmd.buf = buf;
11582 priv_cmd.used_len = bp;
11583 priv_cmd.total_len = bp;
11584 ifr.ifr_data = &priv_cmd;
11585
11586 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11587
11588 if (ret < 0) {
11589 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11590 ret);
11591 wpa_driver_send_hang_msg(drv);
11592 return ret;
11593 }
11594
11595 drv_errors = 0;
11596
11597 return android_priv_cmd(bss, "PNOFORCE 1");
11598}
11599
11600
11601static int android_pno_stop(struct i802_bss *bss)
11602{
11603 return android_priv_cmd(bss, "PNOFORCE 0");
11604}
11605
11606#endif /* ANDROID */
11607
11608
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011609static int driver_nl80211_set_key(const char *ifname, void *priv,
11610 enum wpa_alg alg, const u8 *addr,
11611 int key_idx, int set_tx,
11612 const u8 *seq, size_t seq_len,
11613 const u8 *key, size_t key_len)
11614{
11615 struct i802_bss *bss = priv;
11616 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11617 set_tx, seq, seq_len, key, key_len);
11618}
11619
11620
11621static int driver_nl80211_scan2(void *priv,
11622 struct wpa_driver_scan_params *params)
11623{
11624 struct i802_bss *bss = priv;
11625 return wpa_driver_nl80211_scan(bss, params);
11626}
11627
11628
11629static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11630 int reason_code)
11631{
11632 struct i802_bss *bss = priv;
11633 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11634}
11635
11636
11637static int driver_nl80211_authenticate(void *priv,
11638 struct wpa_driver_auth_params *params)
11639{
11640 struct i802_bss *bss = priv;
11641 return wpa_driver_nl80211_authenticate(bss, params);
11642}
11643
11644
11645static void driver_nl80211_deinit(void *priv)
11646{
11647 struct i802_bss *bss = priv;
11648 wpa_driver_nl80211_deinit(bss);
11649}
11650
11651
11652static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11653 const char *ifname)
11654{
11655 struct i802_bss *bss = priv;
11656 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11657}
11658
11659
11660static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11661 size_t data_len, int noack)
11662{
11663 struct i802_bss *bss = priv;
11664 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11665 0, 0, 0, 0);
11666}
11667
11668
11669static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11670{
11671 struct i802_bss *bss = priv;
11672 return wpa_driver_nl80211_sta_remove(bss, addr);
11673}
11674
11675
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011676static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11677 const char *ifname, int vlan_id)
11678{
11679 struct i802_bss *bss = priv;
11680 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11681}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011682
11683
11684static int driver_nl80211_read_sta_data(void *priv,
11685 struct hostap_sta_driver_data *data,
11686 const u8 *addr)
11687{
11688 struct i802_bss *bss = priv;
11689 return i802_read_sta_data(bss, data, addr);
11690}
11691
11692
11693static int driver_nl80211_send_action(void *priv, unsigned int freq,
11694 unsigned int wait_time,
11695 const u8 *dst, const u8 *src,
11696 const u8 *bssid,
11697 const u8 *data, size_t data_len,
11698 int no_cck)
11699{
11700 struct i802_bss *bss = priv;
11701 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11702 bssid, data, data_len, no_cck);
11703}
11704
11705
11706static int driver_nl80211_probe_req_report(void *priv, int report)
11707{
11708 struct i802_bss *bss = priv;
11709 return wpa_driver_nl80211_probe_req_report(bss, report);
11710}
11711
11712
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011713static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11714 const u8 *ies, size_t ies_len)
11715{
11716 int ret;
11717 struct nl_msg *msg;
11718 struct i802_bss *bss = priv;
11719 struct wpa_driver_nl80211_data *drv = bss->drv;
11720 u16 mdid = WPA_GET_LE16(md);
11721
11722 msg = nlmsg_alloc();
11723 if (!msg)
11724 return -ENOMEM;
11725
11726 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11727 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11728 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11729 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11730 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11731
11732 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11733 if (ret) {
11734 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11735 "err=%d (%s)", ret, strerror(-ret));
11736 }
11737
11738 return ret;
11739
11740nla_put_failure:
11741 nlmsg_free(msg);
11742 return -ENOBUFS;
11743}
11744
11745
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011746const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11747{
11748 struct i802_bss *bss = priv;
11749 struct wpa_driver_nl80211_data *drv = bss->drv;
11750
11751 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11752 return NULL;
11753
11754 return bss->addr;
11755}
11756
11757
Dmitry Shmidt56052862013-10-04 10:23:25 -070011758static const char * scan_state_str(enum scan_states scan_state)
11759{
11760 switch (scan_state) {
11761 case NO_SCAN:
11762 return "NO_SCAN";
11763 case SCAN_REQUESTED:
11764 return "SCAN_REQUESTED";
11765 case SCAN_STARTED:
11766 return "SCAN_STARTED";
11767 case SCAN_COMPLETED:
11768 return "SCAN_COMPLETED";
11769 case SCAN_ABORTED:
11770 return "SCAN_ABORTED";
11771 case SCHED_SCAN_STARTED:
11772 return "SCHED_SCAN_STARTED";
11773 case SCHED_SCAN_STOPPED:
11774 return "SCHED_SCAN_STOPPED";
11775 case SCHED_SCAN_RESULTS:
11776 return "SCHED_SCAN_RESULTS";
11777 }
11778
11779 return "??";
11780}
11781
11782
11783static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11784{
11785 struct i802_bss *bss = priv;
11786 struct wpa_driver_nl80211_data *drv = bss->drv;
11787 int res;
11788 char *pos, *end;
11789
11790 pos = buf;
11791 end = buf + buflen;
11792
11793 res = os_snprintf(pos, end - pos,
11794 "ifindex=%d\n"
11795 "ifname=%s\n"
11796 "brname=%s\n"
11797 "addr=" MACSTR "\n"
11798 "freq=%d\n"
11799 "%s%s%s%s%s",
11800 bss->ifindex,
11801 bss->ifname,
11802 bss->brname,
11803 MAC2STR(bss->addr),
11804 bss->freq,
11805 bss->beacon_set ? "beacon_set=1\n" : "",
11806 bss->added_if_into_bridge ?
11807 "added_if_into_bridge=1\n" : "",
11808 bss->added_bridge ? "added_bridge=1\n" : "",
11809 bss->in_deinit ? "in_deinit=1\n" : "",
11810 bss->if_dynamic ? "if_dynamic=1\n" : "");
11811 if (res < 0 || res >= end - pos)
11812 return pos - buf;
11813 pos += res;
11814
11815 if (bss->wdev_id_set) {
11816 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11817 (unsigned long long) bss->wdev_id);
11818 if (res < 0 || res >= end - pos)
11819 return pos - buf;
11820 pos += res;
11821 }
11822
11823 res = os_snprintf(pos, end - pos,
11824 "phyname=%s\n"
11825 "drv_ifindex=%d\n"
11826 "operstate=%d\n"
11827 "scan_state=%s\n"
11828 "auth_bssid=" MACSTR "\n"
11829 "auth_attempt_bssid=" MACSTR "\n"
11830 "bssid=" MACSTR "\n"
11831 "prev_bssid=" MACSTR "\n"
11832 "associated=%d\n"
11833 "assoc_freq=%u\n"
11834 "monitor_sock=%d\n"
11835 "monitor_ifidx=%d\n"
11836 "monitor_refcount=%d\n"
11837 "last_mgmt_freq=%u\n"
11838 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011839 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011840 drv->phyname,
11841 drv->ifindex,
11842 drv->operstate,
11843 scan_state_str(drv->scan_state),
11844 MAC2STR(drv->auth_bssid),
11845 MAC2STR(drv->auth_attempt_bssid),
11846 MAC2STR(drv->bssid),
11847 MAC2STR(drv->prev_bssid),
11848 drv->associated,
11849 drv->assoc_freq,
11850 drv->monitor_sock,
11851 drv->monitor_ifidx,
11852 drv->monitor_refcount,
11853 drv->last_mgmt_freq,
11854 drv->eapol_tx_sock,
11855 drv->ignore_if_down_event ?
11856 "ignore_if_down_event=1\n" : "",
11857 drv->scan_complete_events ?
11858 "scan_complete_events=1\n" : "",
11859 drv->disabled_11b_rates ?
11860 "disabled_11b_rates=1\n" : "",
11861 drv->pending_remain_on_chan ?
11862 "pending_remain_on_chan=1\n" : "",
11863 drv->in_interface_list ? "in_interface_list=1\n" : "",
11864 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11865 drv->poll_command_supported ?
11866 "poll_command_supported=1\n" : "",
11867 drv->data_tx_status ? "data_tx_status=1\n" : "",
11868 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11869 drv->retry_auth ? "retry_auth=1\n" : "",
11870 drv->use_monitor ? "use_monitor=1\n" : "",
11871 drv->ignore_next_local_disconnect ?
11872 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011873 drv->ignore_next_local_deauth ?
11874 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011875 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11876 if (res < 0 || res >= end - pos)
11877 return pos - buf;
11878 pos += res;
11879
11880 if (drv->has_capability) {
11881 res = os_snprintf(pos, end - pos,
11882 "capa.key_mgmt=0x%x\n"
11883 "capa.enc=0x%x\n"
11884 "capa.auth=0x%x\n"
11885 "capa.flags=0x%x\n"
11886 "capa.max_scan_ssids=%d\n"
11887 "capa.max_sched_scan_ssids=%d\n"
11888 "capa.sched_scan_supported=%d\n"
11889 "capa.max_match_sets=%d\n"
11890 "capa.max_remain_on_chan=%u\n"
11891 "capa.max_stations=%u\n"
11892 "capa.probe_resp_offloads=0x%x\n"
11893 "capa.max_acl_mac_addrs=%u\n"
11894 "capa.num_multichan_concurrent=%u\n",
11895 drv->capa.key_mgmt,
11896 drv->capa.enc,
11897 drv->capa.auth,
11898 drv->capa.flags,
11899 drv->capa.max_scan_ssids,
11900 drv->capa.max_sched_scan_ssids,
11901 drv->capa.sched_scan_supported,
11902 drv->capa.max_match_sets,
11903 drv->capa.max_remain_on_chan,
11904 drv->capa.max_stations,
11905 drv->capa.probe_resp_offloads,
11906 drv->capa.max_acl_mac_addrs,
11907 drv->capa.num_multichan_concurrent);
11908 if (res < 0 || res >= end - pos)
11909 return pos - buf;
11910 pos += res;
11911 }
11912
11913 return pos - buf;
11914}
11915
11916
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011917static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11918{
11919 if (settings->head)
11920 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11921 settings->head_len, settings->head);
11922
11923 if (settings->tail)
11924 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11925 settings->tail_len, settings->tail);
11926
11927 if (settings->beacon_ies)
11928 NLA_PUT(msg, NL80211_ATTR_IE,
11929 settings->beacon_ies_len, settings->beacon_ies);
11930
11931 if (settings->proberesp_ies)
11932 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11933 settings->proberesp_ies_len, settings->proberesp_ies);
11934
11935 if (settings->assocresp_ies)
11936 NLA_PUT(msg,
11937 NL80211_ATTR_IE_ASSOC_RESP,
11938 settings->assocresp_ies_len, settings->assocresp_ies);
11939
11940 if (settings->probe_resp)
11941 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11942 settings->probe_resp_len, settings->probe_resp);
11943
11944 return 0;
11945
11946nla_put_failure:
11947 return -ENOBUFS;
11948}
11949
11950
11951static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11952{
11953 struct nl_msg *msg;
11954 struct i802_bss *bss = priv;
11955 struct wpa_driver_nl80211_data *drv = bss->drv;
11956 struct nlattr *beacon_csa;
11957 int ret = -ENOBUFS;
11958
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011959 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011960 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011961 settings->freq_params.freq, settings->freq_params.bandwidth,
11962 settings->freq_params.center_freq1,
11963 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011964
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011965 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011966 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11967 return -EOPNOTSUPP;
11968 }
11969
11970 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11971 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11972 return -EOPNOTSUPP;
11973
11974 /* check settings validity */
11975 if (!settings->beacon_csa.tail ||
11976 ((settings->beacon_csa.tail_len <=
11977 settings->counter_offset_beacon) ||
11978 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11979 settings->cs_count)))
11980 return -EINVAL;
11981
11982 if (settings->beacon_csa.probe_resp &&
11983 ((settings->beacon_csa.probe_resp_len <=
11984 settings->counter_offset_presp) ||
11985 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11986 settings->cs_count)))
11987 return -EINVAL;
11988
11989 msg = nlmsg_alloc();
11990 if (!msg)
11991 return -ENOMEM;
11992
11993 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11994 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11995 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
11996 ret = nl80211_put_freq_params(msg, &settings->freq_params);
11997 if (ret)
11998 goto error;
11999
12000 if (settings->block_tx)
12001 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12002
12003 /* beacon_after params */
12004 ret = set_beacon_data(msg, &settings->beacon_after);
12005 if (ret)
12006 goto error;
12007
12008 /* beacon_csa params */
12009 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12010 if (!beacon_csa)
12011 goto nla_put_failure;
12012
12013 ret = set_beacon_data(msg, &settings->beacon_csa);
12014 if (ret)
12015 goto error;
12016
12017 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12018 settings->counter_offset_beacon);
12019
12020 if (settings->beacon_csa.probe_resp)
12021 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12022 settings->counter_offset_presp);
12023
12024 nla_nest_end(msg, beacon_csa);
12025 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12026 if (ret) {
12027 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12028 ret, strerror(-ret));
12029 }
12030 return ret;
12031
12032nla_put_failure:
12033 ret = -ENOBUFS;
12034error:
12035 nlmsg_free(msg);
12036 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12037 return ret;
12038}
12039
12040
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012041#ifdef CONFIG_TESTING_OPTIONS
12042static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12043{
12044 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12045 struct wpabuf *buf = arg;
12046
12047 if (!buf)
12048 return NL_SKIP;
12049
12050 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12051 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12052 return NL_SKIP;
12053 }
12054
12055 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12056 genlmsg_attrlen(gnlh, 0));
12057
12058 return NL_SKIP;
12059}
12060#endif /* CONFIG_TESTING_OPTIONS */
12061
12062
12063static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12064{
12065 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12066 struct nlattr *nl_vendor_reply, *nl;
12067 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12068 struct wpabuf *buf = arg;
12069 int rem;
12070
12071 if (!buf)
12072 return NL_SKIP;
12073
12074 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12075 genlmsg_attrlen(gnlh, 0), NULL);
12076 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12077
12078 if (!nl_vendor_reply)
12079 return NL_SKIP;
12080
12081 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12082 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12083 return NL_SKIP;
12084 }
12085
12086 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12087 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12088 }
12089
12090 return NL_SKIP;
12091}
12092
12093
12094static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12095 unsigned int subcmd, const u8 *data,
12096 size_t data_len, struct wpabuf *buf)
12097{
12098 struct i802_bss *bss = priv;
12099 struct wpa_driver_nl80211_data *drv = bss->drv;
12100 struct nl_msg *msg;
12101 int ret;
12102
12103 msg = nlmsg_alloc();
12104 if (!msg)
12105 return -ENOMEM;
12106
12107#ifdef CONFIG_TESTING_OPTIONS
12108 if (vendor_id == 0xffffffff) {
12109 nl80211_cmd(drv, msg, 0, subcmd);
12110 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12111 0)
12112 goto nla_put_failure;
12113 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12114 if (ret)
12115 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12116 ret);
12117 return ret;
12118 }
12119#endif /* CONFIG_TESTING_OPTIONS */
12120
12121 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12122 if (nl80211_set_iface_id(msg, bss) < 0)
12123 goto nla_put_failure;
12124 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12125 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12126 if (data)
12127 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12128
12129 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12130 if (ret)
12131 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12132 ret);
12133 return ret;
12134
12135nla_put_failure:
12136 nlmsg_free(msg);
12137 return -ENOBUFS;
12138}
12139
12140
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012141static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12142 u8 qos_map_set_len)
12143{
12144 struct i802_bss *bss = priv;
12145 struct wpa_driver_nl80211_data *drv = bss->drv;
12146 struct nl_msg *msg;
12147 int ret;
12148
12149 msg = nlmsg_alloc();
12150 if (!msg)
12151 return -ENOMEM;
12152
12153 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12154 qos_map_set, qos_map_set_len);
12155
12156 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12157 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12158 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12159
12160 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12161 if (ret)
12162 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12163
12164 return ret;
12165
12166nla_put_failure:
12167 nlmsg_free(msg);
12168 return -ENOBUFS;
12169}
12170
12171
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012172static int nl80211_set_wowlan(void *priv,
12173 const struct wowlan_triggers *triggers)
12174{
12175 struct i802_bss *bss = priv;
12176 struct wpa_driver_nl80211_data *drv = bss->drv;
12177 struct nl_msg *msg;
12178 struct nlattr *wowlan_triggers;
12179 int ret;
12180
12181 msg = nlmsg_alloc();
12182 if (!msg)
12183 return -ENOMEM;
12184
12185 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12186
12187 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12188 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12189
12190 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12191 if (!wowlan_triggers)
12192 goto nla_put_failure;
12193
12194 if (triggers->any)
12195 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12196 if (triggers->disconnect)
12197 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12198 if (triggers->magic_pkt)
12199 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12200 if (triggers->gtk_rekey_failure)
12201 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12202 if (triggers->eap_identity_req)
12203 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12204 if (triggers->four_way_handshake)
12205 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12206 if (triggers->rfkill_release)
12207 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12208
12209 nla_nest_end(msg, wowlan_triggers);
12210
12211 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12212 if (ret)
12213 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12214
12215 return ret;
12216
12217nla_put_failure:
12218 nlmsg_free(msg);
12219 return -ENOBUFS;
12220}
12221
12222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012223const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12224 .name = "nl80211",
12225 .desc = "Linux nl80211/cfg80211",
12226 .get_bssid = wpa_driver_nl80211_get_bssid,
12227 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012228 .set_key = driver_nl80211_set_key,
12229 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012230 .sched_scan = wpa_driver_nl80211_sched_scan,
12231 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012232 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012233 .deauthenticate = driver_nl80211_deauthenticate,
12234 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012235 .associate = wpa_driver_nl80211_associate,
12236 .global_init = nl80211_global_init,
12237 .global_deinit = nl80211_global_deinit,
12238 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012239 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012240 .get_capa = wpa_driver_nl80211_get_capa,
12241 .set_operstate = wpa_driver_nl80211_set_operstate,
12242 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12243 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012244 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012245 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012246 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012247 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012248 .if_remove = driver_nl80211_if_remove,
12249 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012250 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12251 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012252 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012253 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12254 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012255 .hapd_init = i802_init,
12256 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012257 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012258 .get_seqnum = i802_get_seqnum,
12259 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012260 .get_inact_sec = i802_get_inact_sec,
12261 .sta_clear_stats = i802_sta_clear_stats,
12262 .set_rts = i802_set_rts,
12263 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012264 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012265 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012266 .sta_deauth = i802_sta_deauth,
12267 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012268 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012269 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012270 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012271 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12272 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12273 .cancel_remain_on_channel =
12274 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012275 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012276 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012277 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012278 .resume = wpa_driver_nl80211_resume,
12279 .send_ft_action = nl80211_send_ft_action,
12280 .signal_monitor = nl80211_signal_monitor,
12281 .signal_poll = nl80211_signal_poll,
12282 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012283 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012284 .set_param = nl80211_set_param,
12285 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012286 .add_pmkid = nl80211_add_pmkid,
12287 .remove_pmkid = nl80211_remove_pmkid,
12288 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012289 .set_rekey_info = nl80211_set_rekey_info,
12290 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012291 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012292 .start_dfs_cac = nl80211_start_radar_detection,
12293 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012294#ifdef CONFIG_TDLS
12295 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12296 .tdls_oper = nl80211_tdls_oper,
12297#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012298 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012299 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012300 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012301 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012302 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012303#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012304 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012305 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012306 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012307#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012308#ifdef ANDROID
12309 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012310#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012311 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012312 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012313 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012314};