blob: aa2cd04afe2e254634cdf13f2f6a692e5a6025aa [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 Shmidt6dc03bd2014-05-16 10:40:13 -0700590 unsigned int ibss_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300592};
593
594static int bss_info_handler(struct nl_msg *msg, void *arg);
595
596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597/* nl80211 code */
598static int ack_handler(struct nl_msg *msg, void *arg)
599{
600 int *err = arg;
601 *err = 0;
602 return NL_STOP;
603}
604
605static int finish_handler(struct nl_msg *msg, void *arg)
606{
607 int *ret = arg;
608 *ret = 0;
609 return NL_SKIP;
610}
611
612static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
613 void *arg)
614{
615 int *ret = arg;
616 *ret = err->error;
617 return NL_SKIP;
618}
619
620
621static int no_seq_check(struct nl_msg *msg, void *arg)
622{
623 return NL_OK;
624}
625
626
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 struct nl_handle *nl_handle, struct nl_msg *msg,
629 int (*valid_handler)(struct nl_msg *, void *),
630 void *valid_data)
631{
632 struct nl_cb *cb;
633 int err = -ENOMEM;
634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800635 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 if (!cb)
637 goto out;
638
639 err = nl_send_auto_complete(nl_handle, msg);
640 if (err < 0)
641 goto out;
642
643 err = 1;
644
645 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
646 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
647 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
648
649 if (valid_handler)
650 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
651 valid_handler, valid_data);
652
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700653 while (err > 0) {
654 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700655 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700656 wpa_printf(MSG_INFO,
657 "nl80211: %s->nl_recvmsgs failed: %d",
658 __func__, res);
659 }
660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661 out:
662 nl_cb_put(cb);
663 nlmsg_free(msg);
664 return err;
665}
666
667
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800668static int send_and_recv_msgs_global(struct nl80211_global *global,
669 struct nl_msg *msg,
670 int (*valid_handler)(struct nl_msg *, void *),
671 void *valid_data)
672{
673 return send_and_recv(global, global->nl, msg, valid_handler,
674 valid_data);
675}
676
Dmitry Shmidt04949592012-07-19 12:16:46 -0700677
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800678static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 struct nl_msg *msg,
680 int (*valid_handler)(struct nl_msg *, void *),
681 void *valid_data)
682{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800683 return send_and_recv(drv->global, drv->global->nl, msg,
684 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685}
686
687
688struct family_data {
689 const char *group;
690 int id;
691};
692
693
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700694static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
695{
696 if (bss->wdev_id_set)
697 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
698 else
699 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
700 return 0;
701
702nla_put_failure:
703 return -1;
704}
705
706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700707static int family_handler(struct nl_msg *msg, void *arg)
708{
709 struct family_data *res = arg;
710 struct nlattr *tb[CTRL_ATTR_MAX + 1];
711 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
712 struct nlattr *mcgrp;
713 int i;
714
715 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
716 genlmsg_attrlen(gnlh, 0), NULL);
717 if (!tb[CTRL_ATTR_MCAST_GROUPS])
718 return NL_SKIP;
719
720 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
721 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
722 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
723 nla_len(mcgrp), NULL);
724 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
725 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
726 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
727 res->group,
728 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
729 continue;
730 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
731 break;
732 };
733
734 return NL_SKIP;
735}
736
737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 const char *family, const char *group)
740{
741 struct nl_msg *msg;
742 int ret = -1;
743 struct family_data res = { group, -ENOENT };
744
745 msg = nlmsg_alloc();
746 if (!msg)
747 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800748 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 0, 0, CTRL_CMD_GETFAMILY, 0);
750 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 msg = NULL;
754 if (ret == 0)
755 ret = res.id;
756
757nla_put_failure:
758 nlmsg_free(msg);
759 return ret;
760}
761
762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
764 struct nl_msg *msg, int flags, uint8_t cmd)
765{
766 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
767 0, flags, cmd, 0);
768}
769
770
771struct wiphy_idx_data {
772 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700773 enum nl80211_iftype nlmode;
774 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800775};
776
777
778static int netdev_info_handler(struct nl_msg *msg, void *arg)
779{
780 struct nlattr *tb[NL80211_ATTR_MAX + 1];
781 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
782 struct wiphy_idx_data *info = arg;
783
784 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
785 genlmsg_attrlen(gnlh, 0), NULL);
786
787 if (tb[NL80211_ATTR_WIPHY])
788 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
789
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700790 if (tb[NL80211_ATTR_IFTYPE])
791 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
792
793 if (tb[NL80211_ATTR_MAC] && info->macaddr)
794 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
795 ETH_ALEN);
796
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800797 return NL_SKIP;
798}
799
800
801static int nl80211_get_wiphy_index(struct i802_bss *bss)
802{
803 struct nl_msg *msg;
804 struct wiphy_idx_data data = {
805 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700806 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 };
808
809 msg = nlmsg_alloc();
810 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700811 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812
813 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
814
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700815 if (nl80211_set_iface_id(msg, bss) < 0)
816 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800817
818 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
819 return data.wiphy_idx;
820 msg = NULL;
821nla_put_failure:
822 nlmsg_free(msg);
823 return -1;
824}
825
826
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700827static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
828{
829 struct nl_msg *msg;
830 struct wiphy_idx_data data = {
831 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
832 .macaddr = NULL,
833 };
834
835 msg = nlmsg_alloc();
836 if (!msg)
837 return -1;
838
839 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
840
841 if (nl80211_set_iface_id(msg, bss) < 0)
842 goto nla_put_failure;
843
844 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
845 return data.nlmode;
846 msg = NULL;
847nla_put_failure:
848 nlmsg_free(msg);
849 return NL80211_IFTYPE_UNSPECIFIED;
850}
851
852
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700853static int nl80211_get_macaddr(struct i802_bss *bss)
854{
855 struct nl_msg *msg;
856 struct wiphy_idx_data data = {
857 .macaddr = bss->addr,
858 };
859
860 msg = nlmsg_alloc();
861 if (!msg)
862 return NL80211_IFTYPE_UNSPECIFIED;
863
864 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
865 if (nl80211_set_iface_id(msg, bss) < 0)
866 goto nla_put_failure;
867
868 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
869
870nla_put_failure:
871 nlmsg_free(msg);
872 return NL80211_IFTYPE_UNSPECIFIED;
873}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700874
875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800876static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
877 struct nl80211_wiphy_data *w)
878{
879 struct nl_msg *msg;
880 int ret = -1;
881
882 msg = nlmsg_alloc();
883 if (!msg)
884 return -1;
885
886 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
887
888 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
889
890 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
891 msg = NULL;
892 if (ret) {
893 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
894 "failed: ret=%d (%s)",
895 ret, strerror(-ret));
896 goto nla_put_failure;
897 }
898 ret = 0;
899nla_put_failure:
900 nlmsg_free(msg);
901 return ret;
902}
903
904
905static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
906{
907 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700908 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800909
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800910 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800911
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700912 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700913 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700914 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
915 __func__, res);
916 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917}
918
919
920static int process_beacon_event(struct nl_msg *msg, void *arg)
921{
922 struct nl80211_wiphy_data *w = arg;
923 struct wpa_driver_nl80211_data *drv;
924 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
925 struct nlattr *tb[NL80211_ATTR_MAX + 1];
926 union wpa_event_data event;
927
928 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
929 genlmsg_attrlen(gnlh, 0), NULL);
930
931 if (gnlh->cmd != NL80211_CMD_FRAME) {
932 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
933 gnlh->cmd);
934 return NL_SKIP;
935 }
936
937 if (!tb[NL80211_ATTR_FRAME])
938 return NL_SKIP;
939
940 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
941 wiphy_list) {
942 os_memset(&event, 0, sizeof(event));
943 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
944 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
945 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
946 }
947
948 return NL_SKIP;
949}
950
951
952static struct nl80211_wiphy_data *
953nl80211_get_wiphy_data_ap(struct i802_bss *bss)
954{
955 static DEFINE_DL_LIST(nl80211_wiphys);
956 struct nl80211_wiphy_data *w;
957 int wiphy_idx, found = 0;
958 struct i802_bss *tmp_bss;
959
960 if (bss->wiphy_data != NULL)
961 return bss->wiphy_data;
962
963 wiphy_idx = nl80211_get_wiphy_index(bss);
964
965 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
966 if (w->wiphy_idx == wiphy_idx)
967 goto add;
968 }
969
970 /* alloc new one */
971 w = os_zalloc(sizeof(*w));
972 if (w == NULL)
973 return NULL;
974 w->wiphy_idx = wiphy_idx;
975 dl_list_init(&w->bsss);
976 dl_list_init(&w->drvs);
977
978 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
979 if (!w->nl_cb) {
980 os_free(w);
981 return NULL;
982 }
983 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
984 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
985 w);
986
987 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
988 "wiphy beacons");
989 if (w->nl_beacons == NULL) {
990 os_free(w);
991 return NULL;
992 }
993
994 if (nl80211_register_beacons(bss->drv, w)) {
995 nl_destroy_handles(&w->nl_beacons);
996 os_free(w);
997 return NULL;
998 }
999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001000 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001001
1002 dl_list_add(&nl80211_wiphys, &w->list);
1003
1004add:
1005 /* drv entry for this bss already there? */
1006 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1007 if (tmp_bss->drv == bss->drv) {
1008 found = 1;
1009 break;
1010 }
1011 }
1012 /* if not add it */
1013 if (!found)
1014 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
1015
1016 dl_list_add(&w->bsss, &bss->wiphy_list);
1017 bss->wiphy_data = w;
1018 return w;
1019}
1020
1021
1022static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1023{
1024 struct nl80211_wiphy_data *w = bss->wiphy_data;
1025 struct i802_bss *tmp_bss;
1026 int found = 0;
1027
1028 if (w == NULL)
1029 return;
1030 bss->wiphy_data = NULL;
1031 dl_list_del(&bss->wiphy_list);
1032
1033 /* still any for this drv present? */
1034 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1035 if (tmp_bss->drv == bss->drv) {
1036 found = 1;
1037 break;
1038 }
1039 }
1040 /* if not remove it */
1041 if (!found)
1042 dl_list_del(&bss->drv->wiphy_list);
1043
1044 if (!dl_list_empty(&w->bsss))
1045 return;
1046
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001047 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001048
1049 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001050 dl_list_del(&w->list);
1051 os_free(w);
1052}
1053
1054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1056{
1057 struct i802_bss *bss = priv;
1058 struct wpa_driver_nl80211_data *drv = bss->drv;
1059 if (!drv->associated)
1060 return -1;
1061 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1062 return 0;
1063}
1064
1065
1066static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1067{
1068 struct i802_bss *bss = priv;
1069 struct wpa_driver_nl80211_data *drv = bss->drv;
1070 if (!drv->associated)
1071 return -1;
1072 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1073 return drv->ssid_len;
1074}
1075
1076
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001077static void wpa_driver_nl80211_event_newlink(
1078 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079{
1080 union wpa_event_data event;
1081
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001082 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1083 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1084 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1085 drv->first_bss->ifname);
1086 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001087 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001088 if (!drv->if_removed)
1089 return;
1090 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1091 drv->first_bss->ifname);
1092 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 }
1094
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001095 os_memset(&event, 0, sizeof(event));
1096 os_strlcpy(event.interface_status.ifname, ifname,
1097 sizeof(event.interface_status.ifname));
1098 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1099 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1100}
1101
1102
1103static void wpa_driver_nl80211_event_dellink(
1104 struct wpa_driver_nl80211_data *drv, char *ifname)
1105{
1106 union wpa_event_data event;
1107
1108 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1109 if (drv->if_removed) {
1110 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1111 ifname);
1112 return;
1113 }
1114 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1115 ifname);
1116 drv->if_removed = 1;
1117 } else {
1118 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1119 ifname);
1120 }
1121
1122 os_memset(&event, 0, sizeof(event));
1123 os_strlcpy(event.interface_status.ifname, ifname,
1124 sizeof(event.interface_status.ifname));
1125 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1127}
1128
1129
1130static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1131 u8 *buf, size_t len)
1132{
1133 int attrlen, rta_len;
1134 struct rtattr *attr;
1135
1136 attrlen = len;
1137 attr = (struct rtattr *) buf;
1138
1139 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1140 while (RTA_OK(attr, attrlen)) {
1141 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001142 if (os_strcmp(((char *) attr) + rta_len,
1143 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 return 1;
1145 else
1146 break;
1147 }
1148 attr = RTA_NEXT(attr, attrlen);
1149 }
1150
1151 return 0;
1152}
1153
1154
1155static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1156 int ifindex, u8 *buf, size_t len)
1157{
1158 if (drv->ifindex == ifindex)
1159 return 1;
1160
1161 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1163 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001164 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 return 1;
1166 }
1167
1168 return 0;
1169}
1170
1171
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172static struct wpa_driver_nl80211_data *
1173nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1174{
1175 struct wpa_driver_nl80211_data *drv;
1176 dl_list_for_each(drv, &global->interfaces,
1177 struct wpa_driver_nl80211_data, list) {
1178 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1179 have_ifidx(drv, idx))
1180 return drv;
1181 }
1182 return NULL;
1183}
1184
1185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1187 struct ifinfomsg *ifi,
1188 u8 *buf, size_t len)
1189{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 struct nl80211_global *global = ctx;
1191 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001192 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 struct rtattr *attr;
1194 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001195 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001196 char ifname[IFNAMSIZ + 1];
1197 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001199 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1200 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001201 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1202 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 return;
1204 }
1205
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001206 extra[0] = '\0';
1207 pos = extra;
1208 end = pos + sizeof(extra);
1209 ifname[0] = '\0';
1210
1211 attrlen = len;
1212 attr = (struct rtattr *) buf;
1213 while (RTA_OK(attr, attrlen)) {
1214 switch (attr->rta_type) {
1215 case IFLA_IFNAME:
1216 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1217 break;
1218 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1219 ifname[RTA_PAYLOAD(attr)] = '\0';
1220 break;
1221 case IFLA_MASTER:
1222 brid = nla_get_u32((struct nlattr *) attr);
1223 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1224 break;
1225 case IFLA_WIRELESS:
1226 pos += os_snprintf(pos, end - pos, " wext");
1227 break;
1228 case IFLA_OPERSTATE:
1229 pos += os_snprintf(pos, end - pos, " operstate=%u",
1230 nla_get_u32((struct nlattr *) attr));
1231 break;
1232 case IFLA_LINKMODE:
1233 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1234 nla_get_u32((struct nlattr *) attr));
1235 break;
1236 }
1237 attr = RTA_NEXT(attr, attrlen);
1238 }
1239 extra[sizeof(extra) - 1] = '\0';
1240
1241 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1242 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1244 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1245 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1246 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1247
1248 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001249 if (if_indextoname(ifi->ifi_index, namebuf) &&
1250 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001251 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1253 "event since interface %s is up", namebuf);
1254 return;
1255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 if (drv->ignore_if_down_event) {
1258 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1259 "event generated by mode change");
1260 drv->ignore_if_down_event = 0;
1261 } else {
1262 drv->if_disabled = 1;
1263 wpa_supplicant_event(drv->ctx,
1264 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001265
1266 /*
1267 * Try to get drv again, since it may be removed as
1268 * part of the EVENT_INTERFACE_DISABLED handling for
1269 * dynamic interfaces
1270 */
1271 drv = nl80211_find_drv(global, ifi->ifi_index,
1272 buf, len);
1273 if (!drv)
1274 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001275 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 }
1277
1278 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279 if (if_indextoname(ifi->ifi_index, namebuf) &&
1280 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001281 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001282 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1283 "event since interface %s is down",
1284 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001285 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001286 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1287 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001288 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001289 } else if (drv->if_removed) {
1290 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1291 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001292 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001293 } else {
1294 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1295 drv->if_disabled = 0;
1296 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1297 NULL);
1298 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 }
1300
1301 /*
1302 * Some drivers send the association event before the operup event--in
1303 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1304 * fails. This will hit us when wpa_supplicant does not need to do
1305 * IEEE 802.1X authentication
1306 */
1307 if (drv->operstate == 1 &&
1308 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001309 !(ifi->ifi_flags & IFF_RUNNING)) {
1310 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001311 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 }
1314
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001315 if (ifname[0])
1316 wpa_driver_nl80211_event_newlink(drv, ifname);
1317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 if (ifi->ifi_family == AF_BRIDGE && brid) {
1319 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 if_indextoname(brid, namebuf);
1321 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1322 brid, namebuf);
1323 add_ifidx(drv, brid);
1324 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325}
1326
1327
1328static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1329 struct ifinfomsg *ifi,
1330 u8 *buf, size_t len)
1331{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001332 struct nl80211_global *global = ctx;
1333 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001334 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 struct rtattr *attr;
1336 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001337 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1340 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001341 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1342 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343 return;
1344 }
1345
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001346 ifname[0] = '\0';
1347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 attrlen = len;
1349 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001351 switch (attr->rta_type) {
1352 case IFLA_IFNAME:
1353 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1354 break;
1355 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1356 ifname[RTA_PAYLOAD(attr)] = '\0';
1357 break;
1358 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001360 break;
1361 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 attr = RTA_NEXT(attr, attrlen);
1363 }
1364
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001365 if (ifname[0])
1366 wpa_driver_nl80211_event_dellink(drv, ifname);
1367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 if (ifi->ifi_family == AF_BRIDGE && brid) {
1369 /* device has been removed from bridge */
1370 char namebuf[IFNAMSIZ];
1371 if_indextoname(brid, namebuf);
1372 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1373 "%s", brid, namebuf);
1374 del_ifidx(drv, brid);
1375 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376}
1377
1378
1379static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1380 const u8 *frame, size_t len)
1381{
1382 const struct ieee80211_mgmt *mgmt;
1383 union wpa_event_data event;
1384
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001385 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 mgmt = (const struct ieee80211_mgmt *) frame;
1387 if (len < 24 + sizeof(mgmt->u.auth)) {
1388 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1389 "frame");
1390 return;
1391 }
1392
1393 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001394 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 os_memset(&event, 0, sizeof(event));
1396 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1397 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001398 event.auth.auth_transaction =
1399 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1401 if (len > 24 + sizeof(mgmt->u.auth)) {
1402 event.auth.ies = mgmt->u.auth.variable;
1403 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1404 }
1405
1406 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1407}
1408
1409
Jouni Malinen87fd2792011-05-16 18:35:42 +03001410static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1411{
1412 struct nl_msg *msg;
1413 int ret;
1414 struct nl80211_bss_info_arg arg;
1415
1416 os_memset(&arg, 0, sizeof(arg));
1417 msg = nlmsg_alloc();
1418 if (!msg)
1419 goto nla_put_failure;
1420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001421 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001422 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1423
1424 arg.drv = drv;
1425 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1426 msg = NULL;
1427 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001428 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1429 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001430 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001431 "associated BSS from scan results: %u MHz", freq);
1432 if (freq)
1433 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001434 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001435 }
1436 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1437 "(%s)", ret, strerror(-ret));
1438nla_put_failure:
1439 nlmsg_free(msg);
1440 return drv->assoc_freq;
1441}
1442
1443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1445 const u8 *frame, size_t len)
1446{
1447 const struct ieee80211_mgmt *mgmt;
1448 union wpa_event_data event;
1449 u16 status;
1450
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001451 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 mgmt = (const struct ieee80211_mgmt *) frame;
1453 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1454 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1455 "frame");
1456 return;
1457 }
1458
1459 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1460 if (status != WLAN_STATUS_SUCCESS) {
1461 os_memset(&event, 0, sizeof(event));
1462 event.assoc_reject.bssid = mgmt->bssid;
1463 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1464 event.assoc_reject.resp_ies =
1465 (u8 *) mgmt->u.assoc_resp.variable;
1466 event.assoc_reject.resp_ies_len =
1467 len - 24 - sizeof(mgmt->u.assoc_resp);
1468 }
1469 event.assoc_reject.status_code = status;
1470
1471 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1472 return;
1473 }
1474
1475 drv->associated = 1;
1476 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001477 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478
1479 os_memset(&event, 0, sizeof(event));
1480 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1481 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1482 event.assoc_info.resp_ies_len =
1483 len - 24 - sizeof(mgmt->u.assoc_resp);
1484 }
1485
1486 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1489}
1490
1491
1492static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1493 enum nl80211_commands cmd, struct nlattr *status,
1494 struct nlattr *addr, struct nlattr *req_ie,
1495 struct nlattr *resp_ie)
1496{
1497 union wpa_event_data event;
1498
1499 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1500 /*
1501 * Avoid reporting two association events that would confuse
1502 * the core code.
1503 */
1504 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1505 "when using userspace SME", cmd);
1506 return;
1507 }
1508
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001509 if (cmd == NL80211_CMD_CONNECT)
1510 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1511 else if (cmd == NL80211_CMD_ROAM)
1512 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 os_memset(&event, 0, sizeof(event));
1515 if (cmd == NL80211_CMD_CONNECT &&
1516 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1517 if (addr)
1518 event.assoc_reject.bssid = nla_data(addr);
1519 if (resp_ie) {
1520 event.assoc_reject.resp_ies = nla_data(resp_ie);
1521 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1522 }
1523 event.assoc_reject.status_code = nla_get_u16(status);
1524 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1525 return;
1526 }
1527
1528 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001529 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001531 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533
1534 if (req_ie) {
1535 event.assoc_info.req_ies = nla_data(req_ie);
1536 event.assoc_info.req_ies_len = nla_len(req_ie);
1537 }
1538 if (resp_ie) {
1539 event.assoc_info.resp_ies = nla_data(resp_ie);
1540 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1541 }
1542
Jouni Malinen87fd2792011-05-16 18:35:42 +03001543 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1546}
1547
1548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001550 struct nlattr *reason, struct nlattr *addr,
1551 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001552{
1553 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001554 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001555
1556 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1557 /*
1558 * Avoid reporting two disassociation events that could
1559 * confuse the core code.
1560 */
1561 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1562 "event when using userspace SME");
1563 return;
1564 }
1565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001566 if (drv->ignore_next_local_disconnect) {
1567 drv->ignore_next_local_disconnect = 0;
1568 if (locally_generated) {
1569 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1570 "event triggered during reassociation");
1571 return;
1572 }
1573 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1574 "disconnect but got another disconnect "
1575 "event first");
1576 }
1577
1578 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001579 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001580 os_memset(&data, 0, sizeof(data));
1581 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001582 data.deauth_info.reason_code = nla_get_u16(reason);
1583 data.deauth_info.locally_generated = by_ap == NULL;
1584 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1585}
1586
1587
Dmitry Shmidt97672262014-02-03 13:02:54 -08001588static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1589{
1590 int freq1 = 0;
1591
1592 switch (convert2width(width)) {
1593 case CHAN_WIDTH_20_NOHT:
1594 case CHAN_WIDTH_20:
1595 return 0;
1596 case CHAN_WIDTH_40:
1597 freq1 = cf1 - 10;
1598 break;
1599 case CHAN_WIDTH_80:
1600 freq1 = cf1 - 30;
1601 break;
1602 case CHAN_WIDTH_160:
1603 freq1 = cf1 - 70;
1604 break;
1605 case CHAN_WIDTH_UNKNOWN:
1606 case CHAN_WIDTH_80P80:
1607 /* FIXME: implement this */
1608 return 0;
1609 }
1610
1611 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1612}
1613
1614
Dmitry Shmidt04949592012-07-19 12:16:46 -07001615static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001616 struct nlattr *ifindex, struct nlattr *freq,
1617 struct nlattr *type, struct nlattr *bw,
1618 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001619{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001620 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001621 union wpa_event_data data;
1622 int ht_enabled = 1;
1623 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001624 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001625
1626 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1627
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001628 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001629 return;
1630
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001631 ifidx = nla_get_u32(ifindex);
1632 for (bss = drv->first_bss; bss; bss = bss->next)
1633 if (bss->ifindex == ifidx)
1634 break;
1635
1636 if (bss == NULL) {
1637 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1638 ifidx);
1639 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001640 }
1641
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001642 if (type) {
1643 switch (nla_get_u32(type)) {
1644 case NL80211_CHAN_NO_HT:
1645 ht_enabled = 0;
1646 break;
1647 case NL80211_CHAN_HT20:
1648 break;
1649 case NL80211_CHAN_HT40PLUS:
1650 chan_offset = 1;
1651 break;
1652 case NL80211_CHAN_HT40MINUS:
1653 chan_offset = -1;
1654 break;
1655 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001656 } else if (bw && cf1) {
1657 /* This can happen for example with VHT80 ch switch */
1658 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1659 nla_get_u32(freq),
1660 nla_get_u32(cf1),
1661 cf2 ? nla_get_u32(cf2) : 0);
1662 } else {
1663 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001664 }
1665
1666 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001667 data.ch_switch.freq = nla_get_u32(freq);
1668 data.ch_switch.ht_enabled = ht_enabled;
1669 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001670 if (bw)
1671 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1672 if (cf1)
1673 data.ch_switch.cf1 = nla_get_u32(cf1);
1674 if (cf2)
1675 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001676
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001677 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001678
Dmitry Shmidt04949592012-07-19 12:16:46 -07001679 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001680}
1681
1682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1684 enum nl80211_commands cmd, struct nlattr *addr)
1685{
1686 union wpa_event_data event;
1687 enum wpa_event_type ev;
1688
1689 if (nla_len(addr) != ETH_ALEN)
1690 return;
1691
1692 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1693 cmd, MAC2STR((u8 *) nla_data(addr)));
1694
1695 if (cmd == NL80211_CMD_AUTHENTICATE)
1696 ev = EVENT_AUTH_TIMED_OUT;
1697 else if (cmd == NL80211_CMD_ASSOCIATE)
1698 ev = EVENT_ASSOC_TIMED_OUT;
1699 else
1700 return;
1701
1702 os_memset(&event, 0, sizeof(event));
1703 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1704 wpa_supplicant_event(drv->ctx, ev, &event);
1705}
1706
1707
Dmitry Shmidt98660862014-03-11 17:26:21 -07001708static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001709 struct nlattr *freq, struct nlattr *sig,
1710 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001712 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 const struct ieee80211_mgmt *mgmt;
1714 union wpa_event_data event;
1715 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001716 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001717 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001719 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 mgmt = (const struct ieee80211_mgmt *) frame;
1721 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001722 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 return;
1724 }
1725
1726 fc = le_to_host16(mgmt->frame_control);
1727 stype = WLAN_FC_GET_STYPE(fc);
1728
Dmitry Shmidt04949592012-07-19 12:16:46 -07001729 if (sig)
1730 ssi_signal = (s32) nla_get_u32(sig);
1731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 os_memset(&event, 0, sizeof(event));
1733 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001734 event.rx_mgmt.freq = nla_get_u32(freq);
1735 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001737 wpa_printf(MSG_DEBUG,
1738 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1739 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001740 event.rx_mgmt.frame = frame;
1741 event.rx_mgmt.frame_len = len;
1742 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001743 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001744 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745}
1746
1747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001748static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1749 struct nlattr *cookie, const u8 *frame,
1750 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751{
1752 union wpa_event_data event;
1753 const struct ieee80211_hdr *hdr;
1754 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001756 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001757 if (!is_ap_interface(drv->nlmode)) {
1758 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001760 if (!cookie)
1761 return;
1762
1763 cookie_val = nla_get_u64(cookie);
1764 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1765 " cookie=0%llx%s (ack=%d)",
1766 (long long unsigned int) cookie_val,
1767 cookie_val == drv->send_action_cookie ?
1768 " (match)" : " (unknown)", ack != NULL);
1769 if (cookie_val != drv->send_action_cookie)
1770 return;
1771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772
1773 hdr = (const struct ieee80211_hdr *) frame;
1774 fc = le_to_host16(hdr->frame_control);
1775
1776 os_memset(&event, 0, sizeof(event));
1777 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1778 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1779 event.tx_status.dst = hdr->addr1;
1780 event.tx_status.data = frame;
1781 event.tx_status.data_len = len;
1782 event.tx_status.ack = ack != NULL;
1783 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1784}
1785
1786
1787static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1788 enum wpa_event_type type,
1789 const u8 *frame, size_t len)
1790{
1791 const struct ieee80211_mgmt *mgmt;
1792 union wpa_event_data event;
1793 const u8 *bssid = NULL;
1794 u16 reason_code = 0;
1795
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001796 if (type == EVENT_DEAUTH)
1797 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1798 else
1799 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 mgmt = (const struct ieee80211_mgmt *) frame;
1802 if (len >= 24) {
1803 bssid = mgmt->bssid;
1804
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001805 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1806 !drv->associated &&
1807 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1808 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1809 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1810 /*
1811 * Avoid issues with some roaming cases where
1812 * disconnection event for the old AP may show up after
1813 * we have started connection with the new AP.
1814 */
1815 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1816 MAC2STR(bssid),
1817 MAC2STR(drv->auth_attempt_bssid));
1818 return;
1819 }
1820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 if (drv->associated != 0 &&
1822 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1823 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1824 /*
1825 * We have presumably received this deauth as a
1826 * response to a clear_state_mismatch() outgoing
1827 * deauth. Don't let it take us offline!
1828 */
1829 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1830 "from Unknown BSSID " MACSTR " -- ignoring",
1831 MAC2STR(bssid));
1832 return;
1833 }
1834 }
1835
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001836 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 os_memset(&event, 0, sizeof(event));
1838
1839 /* Note: Same offset for Reason Code in both frame subtypes */
1840 if (len >= 24 + sizeof(mgmt->u.deauth))
1841 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1842
1843 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001844 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001845 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 event.disassoc_info.addr = bssid;
1847 event.disassoc_info.reason_code = reason_code;
1848 if (frame + len > mgmt->u.disassoc.variable) {
1849 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1850 event.disassoc_info.ie_len = frame + len -
1851 mgmt->u.disassoc.variable;
1852 }
1853 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001854 if (drv->ignore_deauth_event) {
1855 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1856 drv->ignore_deauth_event = 0;
1857 return;
1858 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001859 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001860 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001861 if (drv->ignore_next_local_deauth) {
1862 drv->ignore_next_local_deauth = 0;
1863 if (event.deauth_info.locally_generated) {
1864 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1865 return;
1866 }
1867 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1868 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 event.deauth_info.addr = bssid;
1870 event.deauth_info.reason_code = reason_code;
1871 if (frame + len > mgmt->u.deauth.variable) {
1872 event.deauth_info.ie = mgmt->u.deauth.variable;
1873 event.deauth_info.ie_len = frame + len -
1874 mgmt->u.deauth.variable;
1875 }
1876 }
1877
1878 wpa_supplicant_event(drv->ctx, type, &event);
1879}
1880
1881
1882static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1883 enum wpa_event_type type,
1884 const u8 *frame, size_t len)
1885{
1886 const struct ieee80211_mgmt *mgmt;
1887 union wpa_event_data event;
1888 u16 reason_code = 0;
1889
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001890 if (type == EVENT_UNPROT_DEAUTH)
1891 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1892 else
1893 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 if (len < 24)
1896 return;
1897
1898 mgmt = (const struct ieee80211_mgmt *) frame;
1899
1900 os_memset(&event, 0, sizeof(event));
1901 /* Note: Same offset for Reason Code in both frame subtypes */
1902 if (len >= 24 + sizeof(mgmt->u.deauth))
1903 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1904
1905 if (type == EVENT_UNPROT_DISASSOC) {
1906 event.unprot_disassoc.sa = mgmt->sa;
1907 event.unprot_disassoc.da = mgmt->da;
1908 event.unprot_disassoc.reason_code = reason_code;
1909 } else {
1910 event.unprot_deauth.sa = mgmt->sa;
1911 event.unprot_deauth.da = mgmt->da;
1912 event.unprot_deauth.reason_code = reason_code;
1913 }
1914
1915 wpa_supplicant_event(drv->ctx, type, &event);
1916}
1917
1918
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001919static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920 enum nl80211_commands cmd, struct nlattr *frame,
1921 struct nlattr *addr, struct nlattr *timed_out,
1922 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001923 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001925 struct wpa_driver_nl80211_data *drv = bss->drv;
1926 const u8 *data;
1927 size_t len;
1928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 if (timed_out && addr) {
1930 mlme_timeout_event(drv, cmd, addr);
1931 return;
1932 }
1933
1934 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001935 wpa_printf(MSG_DEBUG,
1936 "nl80211: MLME event %d (%s) without frame data",
1937 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938 return;
1939 }
1940
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001941 data = nla_data(frame);
1942 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001943 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001944 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1945 MACSTR ") - too short",
1946 cmd, nl80211_command_to_string(cmd), bss->ifname,
1947 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001948 return;
1949 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001950 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1951 ") A1=" MACSTR " A2=" MACSTR, cmd,
1952 nl80211_command_to_string(cmd), bss->ifname,
1953 MAC2STR(bss->addr), MAC2STR(data + 4),
1954 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001955 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001956 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1957 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001958 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1959 "for foreign address", bss->ifname);
1960 return;
1961 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1963 nla_data(frame), nla_len(frame));
1964
1965 switch (cmd) {
1966 case NL80211_CMD_AUTHENTICATE:
1967 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1968 break;
1969 case NL80211_CMD_ASSOCIATE:
1970 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1971 break;
1972 case NL80211_CMD_DEAUTHENTICATE:
1973 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1974 nla_data(frame), nla_len(frame));
1975 break;
1976 case NL80211_CMD_DISASSOCIATE:
1977 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1978 nla_data(frame), nla_len(frame));
1979 break;
1980 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07001981 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001982 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 break;
1984 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1986 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 break;
1988 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1989 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1990 nla_data(frame), nla_len(frame));
1991 break;
1992 case NL80211_CMD_UNPROT_DISASSOCIATE:
1993 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1994 nla_data(frame), nla_len(frame));
1995 break;
1996 default:
1997 break;
1998 }
1999}
2000
2001
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002002static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 struct nlattr *tb[])
2004{
2005 union wpa_event_data data;
2006
2007 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2008 os_memset(&data, 0, sizeof(data));
2009 if (tb[NL80211_ATTR_MAC]) {
2010 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2011 nla_data(tb[NL80211_ATTR_MAC]),
2012 nla_len(tb[NL80211_ATTR_MAC]));
2013 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
2014 }
2015 if (tb[NL80211_ATTR_KEY_SEQ]) {
2016 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2017 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2018 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2019 }
2020 if (tb[NL80211_ATTR_KEY_TYPE]) {
2021 enum nl80211_key_type key_type =
2022 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2023 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2024 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2025 data.michael_mic_failure.unicast = 1;
2026 } else
2027 data.michael_mic_failure.unicast = 1;
2028
2029 if (tb[NL80211_ATTR_KEY_IDX]) {
2030 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2031 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2032 }
2033
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002034 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035}
2036
2037
2038static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2039 struct nlattr *tb[])
2040{
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002041 unsigned int freq;
2042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 if (tb[NL80211_ATTR_MAC] == NULL) {
2044 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2045 "event");
2046 return;
2047 }
2048 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 drv->associated = 1;
2051 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2052 MAC2STR(drv->bssid));
2053
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002054 freq = nl80211_get_assoc_freq(drv);
2055 if (freq) {
2056 wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
2057 freq);
2058 drv->first_bss->freq = freq;
2059 }
2060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2062}
2063
2064
2065static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2066 int cancel_event, struct nlattr *tb[])
2067{
2068 unsigned int freq, chan_type, duration;
2069 union wpa_event_data data;
2070 u64 cookie;
2071
2072 if (tb[NL80211_ATTR_WIPHY_FREQ])
2073 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2074 else
2075 freq = 0;
2076
2077 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2078 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2079 else
2080 chan_type = 0;
2081
2082 if (tb[NL80211_ATTR_DURATION])
2083 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2084 else
2085 duration = 0;
2086
2087 if (tb[NL80211_ATTR_COOKIE])
2088 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2089 else
2090 cookie = 0;
2091
2092 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2093 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2094 cancel_event, freq, chan_type, duration,
2095 (long long unsigned int) cookie,
2096 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2097
2098 if (cookie != drv->remain_on_chan_cookie)
2099 return; /* not for us */
2100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 if (cancel_event)
2102 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103
2104 os_memset(&data, 0, sizeof(data));
2105 data.remain_on_channel.freq = freq;
2106 data.remain_on_channel.duration = duration;
2107 wpa_supplicant_event(drv->ctx, cancel_event ?
2108 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2109 EVENT_REMAIN_ON_CHANNEL, &data);
2110}
2111
2112
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002113static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2114 struct nlattr *tb[])
2115{
2116 union wpa_event_data data;
2117
2118 os_memset(&data, 0, sizeof(data));
2119
2120 if (tb[NL80211_ATTR_IE]) {
2121 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2122 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2123 }
2124
2125 if (tb[NL80211_ATTR_IE_RIC]) {
2126 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2127 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2128 }
2129
2130 if (tb[NL80211_ATTR_MAC])
2131 os_memcpy(data.ft_ies.target_ap,
2132 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2133
2134 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2135 MAC2STR(data.ft_ies.target_ap));
2136
2137 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2138}
2139
2140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2142 struct nlattr *tb[])
2143{
2144 union wpa_event_data event;
2145 struct nlattr *nl;
2146 int rem;
2147 struct scan_info *info;
2148#define MAX_REPORT_FREQS 50
2149 int freqs[MAX_REPORT_FREQS];
2150 int num_freqs = 0;
2151
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002152 if (drv->scan_for_auth) {
2153 drv->scan_for_auth = 0;
2154 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2155 "cfg80211 BSS entry");
2156 wpa_driver_nl80211_authenticate_retry(drv);
2157 return;
2158 }
2159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 os_memset(&event, 0, sizeof(event));
2161 info = &event.scan_info;
2162 info->aborted = aborted;
2163
2164 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2165 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2166 struct wpa_driver_scan_ssid *s =
2167 &info->ssids[info->num_ssids];
2168 s->ssid = nla_data(nl);
2169 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002170 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2171 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 info->num_ssids++;
2173 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2174 break;
2175 }
2176 }
2177 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002178 char msg[200], *pos, *end;
2179 int res;
2180
2181 pos = msg;
2182 end = pos + sizeof(msg);
2183 *pos = '\0';
2184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2186 {
2187 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002188 res = os_snprintf(pos, end - pos, " %d",
2189 freqs[num_freqs]);
2190 if (res > 0 && end - pos > res)
2191 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192 num_freqs++;
2193 if (num_freqs == MAX_REPORT_FREQS - 1)
2194 break;
2195 }
2196 info->freqs = freqs;
2197 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002198 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2199 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 }
2201 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2202}
2203
2204
2205static int get_link_signal(struct nl_msg *msg, void *arg)
2206{
2207 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2208 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2209 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2210 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2211 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002212 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 };
2214 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2215 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2216 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2217 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2218 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2219 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2220 };
2221 struct wpa_signal_info *sig_change = arg;
2222
2223 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2224 genlmsg_attrlen(gnlh, 0), NULL);
2225 if (!tb[NL80211_ATTR_STA_INFO] ||
2226 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2227 tb[NL80211_ATTR_STA_INFO], policy))
2228 return NL_SKIP;
2229 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2230 return NL_SKIP;
2231
2232 sig_change->current_signal =
2233 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2234
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002235 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2236 sig_change->avg_signal =
2237 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2238 else
2239 sig_change->avg_signal = 0;
2240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2242 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2243 sinfo[NL80211_STA_INFO_TX_BITRATE],
2244 rate_policy)) {
2245 sig_change->current_txrate = 0;
2246 } else {
2247 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2248 sig_change->current_txrate =
2249 nla_get_u16(rinfo[
2250 NL80211_RATE_INFO_BITRATE]) * 100;
2251 }
2252 }
2253 }
2254
2255 return NL_SKIP;
2256}
2257
2258
2259static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2260 struct wpa_signal_info *sig)
2261{
2262 struct nl_msg *msg;
2263
2264 sig->current_signal = -9999;
2265 sig->current_txrate = 0;
2266
2267 msg = nlmsg_alloc();
2268 if (!msg)
2269 return -ENOMEM;
2270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002271 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272
2273 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2274 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2275
2276 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2277 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002278 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 return -ENOBUFS;
2280}
2281
2282
2283static int get_link_noise(struct nl_msg *msg, void *arg)
2284{
2285 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2286 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2287 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2288 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2289 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2290 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2291 };
2292 struct wpa_signal_info *sig_change = arg;
2293
2294 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2295 genlmsg_attrlen(gnlh, 0), NULL);
2296
2297 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2298 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2299 return NL_SKIP;
2300 }
2301
2302 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2303 tb[NL80211_ATTR_SURVEY_INFO],
2304 survey_policy)) {
2305 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2306 "attributes!");
2307 return NL_SKIP;
2308 }
2309
2310 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2311 return NL_SKIP;
2312
2313 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2314 sig_change->frequency)
2315 return NL_SKIP;
2316
2317 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2318 return NL_SKIP;
2319
2320 sig_change->current_noise =
2321 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2322
2323 return NL_SKIP;
2324}
2325
2326
2327static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2328 struct wpa_signal_info *sig_change)
2329{
2330 struct nl_msg *msg;
2331
2332 sig_change->current_noise = 9999;
2333 sig_change->frequency = drv->assoc_freq;
2334
2335 msg = nlmsg_alloc();
2336 if (!msg)
2337 return -ENOMEM;
2338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002339 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340
2341 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2342
2343 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2344 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002345 nlmsg_free(msg);
2346 return -ENOBUFS;
2347}
2348
2349
2350static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2351{
2352 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2353 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2354 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2355 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2356 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2357 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2358 };
2359 struct wpa_scan_results *scan_results = arg;
2360 struct wpa_scan_res *scan_res;
2361 size_t i;
2362
2363 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2364 genlmsg_attrlen(gnlh, 0), NULL);
2365
2366 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2367 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2368 return NL_SKIP;
2369 }
2370
2371 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2372 tb[NL80211_ATTR_SURVEY_INFO],
2373 survey_policy)) {
2374 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2375 "attributes");
2376 return NL_SKIP;
2377 }
2378
2379 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2380 return NL_SKIP;
2381
2382 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2383 return NL_SKIP;
2384
2385 for (i = 0; i < scan_results->num; ++i) {
2386 scan_res = scan_results->res[i];
2387 if (!scan_res)
2388 continue;
2389 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2390 scan_res->freq)
2391 continue;
2392 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2393 continue;
2394 scan_res->noise = (s8)
2395 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2396 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2397 }
2398
2399 return NL_SKIP;
2400}
2401
2402
2403static int nl80211_get_noise_for_scan_results(
2404 struct wpa_driver_nl80211_data *drv,
2405 struct wpa_scan_results *scan_res)
2406{
2407 struct nl_msg *msg;
2408
2409 msg = nlmsg_alloc();
2410 if (!msg)
2411 return -ENOMEM;
2412
2413 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2414
2415 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2416
2417 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2418 scan_res);
2419 nla_put_failure:
2420 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 return -ENOBUFS;
2422}
2423
2424
2425static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2426 struct nlattr *tb[])
2427{
2428 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2429 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2430 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2431 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2432 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2433 };
2434 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2435 enum nl80211_cqm_rssi_threshold_event event;
2436 union wpa_event_data ed;
2437 struct wpa_signal_info sig;
2438 int res;
2439
2440 if (tb[NL80211_ATTR_CQM] == NULL ||
2441 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2442 cqm_policy)) {
2443 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2444 return;
2445 }
2446
2447 os_memset(&ed, 0, sizeof(ed));
2448
2449 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2450 if (!tb[NL80211_ATTR_MAC])
2451 return;
2452 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2453 ETH_ALEN);
2454 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2455 return;
2456 }
2457
2458 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2459 return;
2460 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2461
2462 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2463 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2464 "event: RSSI high");
2465 ed.signal_change.above_threshold = 1;
2466 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2467 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2468 "event: RSSI low");
2469 ed.signal_change.above_threshold = 0;
2470 } else
2471 return;
2472
2473 res = nl80211_get_link_signal(drv, &sig);
2474 if (res == 0) {
2475 ed.signal_change.current_signal = sig.current_signal;
2476 ed.signal_change.current_txrate = sig.current_txrate;
2477 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2478 sig.current_signal, sig.current_txrate);
2479 }
2480
2481 res = nl80211_get_link_noise(drv, &sig);
2482 if (res == 0) {
2483 ed.signal_change.current_noise = sig.current_noise;
2484 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2485 sig.current_noise);
2486 }
2487
2488 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2489}
2490
2491
2492static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2493 struct nlattr **tb)
2494{
2495 u8 *addr;
2496 union wpa_event_data data;
2497
2498 if (tb[NL80211_ATTR_MAC] == NULL)
2499 return;
2500 addr = nla_data(tb[NL80211_ATTR_MAC]);
2501 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002502
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002504 u8 *ies = NULL;
2505 size_t ies_len = 0;
2506 if (tb[NL80211_ATTR_IE]) {
2507 ies = nla_data(tb[NL80211_ATTR_IE]);
2508 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2509 }
2510 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2511 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2512 return;
2513 }
2514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2516 return;
2517
2518 os_memset(&data, 0, sizeof(data));
2519 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2520 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2521}
2522
2523
2524static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2525 struct nlattr **tb)
2526{
2527 u8 *addr;
2528 union wpa_event_data data;
2529
2530 if (tb[NL80211_ATTR_MAC] == NULL)
2531 return;
2532 addr = nla_data(tb[NL80211_ATTR_MAC]);
2533 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2534 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002536 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002537 drv_event_disassoc(drv->ctx, addr);
2538 return;
2539 }
2540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2542 return;
2543
2544 os_memset(&data, 0, sizeof(data));
2545 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2546 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2547}
2548
2549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002550static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2551 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002553 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2554 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2555 [NL80211_REKEY_DATA_KEK] = {
2556 .minlen = NL80211_KEK_LEN,
2557 .maxlen = NL80211_KEK_LEN,
2558 },
2559 [NL80211_REKEY_DATA_KCK] = {
2560 .minlen = NL80211_KCK_LEN,
2561 .maxlen = NL80211_KCK_LEN,
2562 },
2563 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2564 .minlen = NL80211_REPLAY_CTR_LEN,
2565 .maxlen = NL80211_REPLAY_CTR_LEN,
2566 },
2567 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 union wpa_event_data data;
2569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002570 if (!tb[NL80211_ATTR_MAC])
2571 return;
2572 if (!tb[NL80211_ATTR_REKEY_DATA])
2573 return;
2574 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2575 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2576 return;
2577 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2578 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580 os_memset(&data, 0, sizeof(data));
2581 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2582 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2583 MAC2STR(data.driver_gtk_rekey.bssid));
2584 data.driver_gtk_rekey.replay_ctr =
2585 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2586 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2587 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2588 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2589}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591
2592static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2593 struct nlattr **tb)
2594{
2595 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2596 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2597 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2598 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2599 .minlen = ETH_ALEN,
2600 .maxlen = ETH_ALEN,
2601 },
2602 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2603 };
2604 union wpa_event_data data;
2605
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002606 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2609 return;
2610 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2611 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2612 return;
2613 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2614 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2615 return;
2616
2617 os_memset(&data, 0, sizeof(data));
2618 os_memcpy(data.pmkid_candidate.bssid,
2619 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2620 data.pmkid_candidate.index =
2621 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2622 data.pmkid_candidate.preauth =
2623 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2624 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2625}
2626
2627
2628static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2629 struct nlattr **tb)
2630{
2631 union wpa_event_data data;
2632
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002633 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2636 return;
2637
2638 os_memset(&data, 0, sizeof(data));
2639 os_memcpy(data.client_poll.addr,
2640 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2641
2642 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2643}
2644
2645
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002646static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2647 struct nlattr **tb)
2648{
2649 union wpa_event_data data;
2650
2651 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2652
2653 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2654 return;
2655
2656 os_memset(&data, 0, sizeof(data));
2657 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2658 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2659 case NL80211_TDLS_SETUP:
2660 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2661 MACSTR, MAC2STR(data.tdls.peer));
2662 data.tdls.oper = TDLS_REQUEST_SETUP;
2663 break;
2664 case NL80211_TDLS_TEARDOWN:
2665 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2666 MACSTR, MAC2STR(data.tdls.peer));
2667 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2668 break;
2669 default:
2670 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2671 "event");
2672 return;
2673 }
2674 if (tb[NL80211_ATTR_REASON_CODE]) {
2675 data.tdls.reason_code =
2676 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2677 }
2678
2679 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2680}
2681
2682
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002683static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2684 struct nlattr **tb)
2685{
2686 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2687}
2688
2689
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002690static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2691 struct nlattr **tb)
2692{
2693 union wpa_event_data data;
2694 u32 reason;
2695
2696 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2697
2698 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2699 return;
2700
2701 os_memset(&data, 0, sizeof(data));
2702 os_memcpy(data.connect_failed_reason.addr,
2703 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2704
2705 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2706 switch (reason) {
2707 case NL80211_CONN_FAIL_MAX_CLIENTS:
2708 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2709 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2710 break;
2711 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2712 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2713 " tried to connect",
2714 MAC2STR(data.connect_failed_reason.addr));
2715 data.connect_failed_reason.code = BLOCKED_CLIENT;
2716 break;
2717 default:
2718 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2719 "%u", reason);
2720 return;
2721 }
2722
2723 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2724}
2725
2726
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002727static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2728 struct nlattr **tb)
2729{
2730 union wpa_event_data data;
2731 enum nl80211_radar_event event_type;
2732
2733 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2734 return;
2735
2736 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002737 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2738 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002739
Dmitry Shmidt051af732013-10-22 13:52:46 -07002740 /* Check HT params */
2741 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2742 data.dfs_event.ht_enabled = 1;
2743 data.dfs_event.chan_offset = 0;
2744
2745 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2746 case NL80211_CHAN_NO_HT:
2747 data.dfs_event.ht_enabled = 0;
2748 break;
2749 case NL80211_CHAN_HT20:
2750 break;
2751 case NL80211_CHAN_HT40PLUS:
2752 data.dfs_event.chan_offset = 1;
2753 break;
2754 case NL80211_CHAN_HT40MINUS:
2755 data.dfs_event.chan_offset = -1;
2756 break;
2757 }
2758 }
2759
2760 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002761 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2762 data.dfs_event.chan_width =
2763 convert2width(nla_get_u32(
2764 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2765 if (tb[NL80211_ATTR_CENTER_FREQ1])
2766 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002767 if (tb[NL80211_ATTR_CENTER_FREQ2])
2768 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2769
2770 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2771 data.dfs_event.freq, data.dfs_event.ht_enabled,
2772 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2773 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002774
2775 switch (event_type) {
2776 case NL80211_RADAR_DETECTED:
2777 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2778 break;
2779 case NL80211_RADAR_CAC_FINISHED:
2780 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2781 break;
2782 case NL80211_RADAR_CAC_ABORTED:
2783 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2784 break;
2785 case NL80211_RADAR_NOP_FINISHED:
2786 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2787 break;
2788 default:
2789 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2790 "received", event_type);
2791 break;
2792 }
2793}
2794
2795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002796static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2797 int wds)
2798{
2799 struct wpa_driver_nl80211_data *drv = bss->drv;
2800 union wpa_event_data event;
2801
2802 if (!tb[NL80211_ATTR_MAC])
2803 return;
2804
2805 os_memset(&event, 0, sizeof(event));
2806 event.rx_from_unknown.bssid = bss->addr;
2807 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2808 event.rx_from_unknown.wds = wds;
2809
2810 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2811}
2812
2813
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002814static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2815 const u8 *data, size_t len)
2816{
2817 u32 i, count;
2818 union wpa_event_data event;
2819 struct wpa_freq_range *range = NULL;
2820 const struct qca_avoid_freq_list *freq_range;
2821
2822 freq_range = (const struct qca_avoid_freq_list *) data;
2823 if (len < sizeof(freq_range->count))
2824 return;
2825
2826 count = freq_range->count;
2827 if (len < sizeof(freq_range->count) +
2828 count * sizeof(struct qca_avoid_freq_range)) {
2829 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2830 (unsigned int) len);
2831 return;
2832 }
2833
2834 if (count > 0) {
2835 range = os_calloc(count, sizeof(struct wpa_freq_range));
2836 if (range == NULL)
2837 return;
2838 }
2839
2840 os_memset(&event, 0, sizeof(event));
2841 for (i = 0; i < count; i++) {
2842 unsigned int idx = event.freq_range.num;
2843 range[idx].min = freq_range->range[i].start_freq;
2844 range[idx].max = freq_range->range[i].end_freq;
2845 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2846 range[idx].min, range[idx].max);
2847 if (range[idx].min > range[idx].max) {
2848 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2849 continue;
2850 }
2851 event.freq_range.num++;
2852 }
2853 event.freq_range.range = range;
2854
2855 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2856
2857 os_free(range);
2858}
2859
2860
2861static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2862 u32 subcmd, u8 *data, size_t len)
2863{
2864 switch (subcmd) {
2865 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2866 qca_nl80211_avoid_freq(drv, data, len);
2867 break;
2868 default:
2869 wpa_printf(MSG_DEBUG,
2870 "nl80211: Ignore unsupported QCA vendor event %u",
2871 subcmd);
2872 break;
2873 }
2874}
2875
2876
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002877static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2878 struct nlattr **tb)
2879{
2880 u32 vendor_id, subcmd, wiphy = 0;
2881 int wiphy_idx;
2882 u8 *data = NULL;
2883 size_t len = 0;
2884
2885 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2886 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2887 return;
2888
2889 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2890 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2891
2892 if (tb[NL80211_ATTR_WIPHY])
2893 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2894
2895 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2896 wiphy, vendor_id, subcmd);
2897
2898 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2899 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2900 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2901 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2902 }
2903
2904 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2905 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2906 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2907 wiphy, wiphy_idx);
2908 return;
2909 }
2910
2911 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002912 case OUI_QCA:
2913 nl80211_vendor_event_qca(drv, subcmd, data, len);
2914 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002915 default:
2916 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2917 break;
2918 }
2919}
2920
2921
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002922static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
2923 struct nlattr *tb[])
2924{
2925 union wpa_event_data data;
2926 enum nl80211_reg_initiator init;
2927
2928 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2929
2930 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2931 return;
2932
2933 os_memset(&data, 0, sizeof(data));
2934 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
2935 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
2936 switch (init) {
2937 case NL80211_REGDOM_SET_BY_CORE:
2938 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
2939 break;
2940 case NL80211_REGDOM_SET_BY_USER:
2941 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
2942 break;
2943 case NL80211_REGDOM_SET_BY_DRIVER:
2944 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
2945 break;
2946 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2947 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
2948 break;
2949 }
2950
2951 if (tb[NL80211_ATTR_REG_TYPE]) {
2952 enum nl80211_reg_type type;
2953 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
2954 wpa_printf(MSG_DEBUG, " * type=%d", type);
2955 switch (type) {
2956 case NL80211_REGDOM_TYPE_COUNTRY:
2957 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
2958 break;
2959 case NL80211_REGDOM_TYPE_WORLD:
2960 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
2961 break;
2962 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
2963 data.channel_list_changed.type =
2964 REGDOM_TYPE_CUSTOM_WORLD;
2965 break;
2966 case NL80211_REGDOM_TYPE_INTERSECTION:
2967 data.channel_list_changed.type =
2968 REGDOM_TYPE_INTERSECTION;
2969 break;
2970 }
2971 }
2972
2973 if (tb[NL80211_ATTR_REG_ALPHA2]) {
2974 os_strlcpy(data.channel_list_changed.alpha2,
2975 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
2976 sizeof(data.channel_list_changed.alpha2));
2977 wpa_printf(MSG_DEBUG, " * alpha2=%s",
2978 data.channel_list_changed.alpha2);
2979 }
2980
2981 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
2982}
2983
2984
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002985static void do_process_drv_event(struct i802_bss *bss, int cmd,
2986 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002987{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002988 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002989 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002990
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002991 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2992 cmd, nl80211_command_to_string(cmd), bss->ifname);
2993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002994 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2995 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2996 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002997 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002998 drv->ap_scan_as_station);
2999 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 }
3001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003002 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003004 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003005 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003006 if (drv->scan_for_auth) {
3007 /*
3008 * Cannot indicate EVENT_SCAN_STARTED here since we skip
3009 * EVENT_SCAN_RESULTS in scan_for_auth case and the
3010 * upper layer implementation could get confused about
3011 * scanning state.
3012 */
3013 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3014 break;
3015 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003016 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003018 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003019 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003020 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003021 break;
3022 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003023 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003024 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3026 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003028 wpa_dbg(drv->ctx, MSG_DEBUG,
3029 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003030 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 drv->scan_complete_events = 1;
3032 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3033 drv->ctx);
3034 send_scan_event(drv, 0, tb);
3035 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003036 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003037 wpa_dbg(drv->ctx, MSG_DEBUG,
3038 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003039 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003040 send_scan_event(drv, 0, tb);
3041 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003043 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003044 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 /*
3046 * Need to indicate that scan results are available in order
3047 * not to make wpa_supplicant stop its scanning.
3048 */
3049 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3050 drv->ctx);
3051 send_scan_event(drv, 1, tb);
3052 break;
3053 case NL80211_CMD_AUTHENTICATE:
3054 case NL80211_CMD_ASSOCIATE:
3055 case NL80211_CMD_DEAUTHENTICATE:
3056 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057 case NL80211_CMD_FRAME_TX_STATUS:
3058 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3059 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003060 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3062 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063 tb[NL80211_ATTR_COOKIE],
3064 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065 break;
3066 case NL80211_CMD_CONNECT:
3067 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003068 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 tb[NL80211_ATTR_STATUS_CODE],
3070 tb[NL80211_ATTR_MAC],
3071 tb[NL80211_ATTR_REQ_IE],
3072 tb[NL80211_ATTR_RESP_IE]);
3073 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003074 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003075 mlme_event_ch_switch(drv,
3076 tb[NL80211_ATTR_IFINDEX],
3077 tb[NL80211_ATTR_WIPHY_FREQ],
3078 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3079 tb[NL80211_ATTR_CHANNEL_WIDTH],
3080 tb[NL80211_ATTR_CENTER_FREQ1],
3081 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003082 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003084 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003085 tb[NL80211_ATTR_MAC],
3086 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087 break;
3088 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003089 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 break;
3091 case NL80211_CMD_JOIN_IBSS:
3092 mlme_event_join_ibss(drv, tb);
3093 break;
3094 case NL80211_CMD_REMAIN_ON_CHANNEL:
3095 mlme_event_remain_on_channel(drv, 0, tb);
3096 break;
3097 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3098 mlme_event_remain_on_channel(drv, 1, tb);
3099 break;
3100 case NL80211_CMD_NOTIFY_CQM:
3101 nl80211_cqm_event(drv, tb);
3102 break;
3103 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003104 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 break;
3106 case NL80211_CMD_REG_BEACON_HINT:
3107 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003108 os_memset(&data, 0, sizeof(data));
3109 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003111 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 break;
3113 case NL80211_CMD_NEW_STATION:
3114 nl80211_new_station_event(drv, tb);
3115 break;
3116 case NL80211_CMD_DEL_STATION:
3117 nl80211_del_station_event(drv, tb);
3118 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003119 case NL80211_CMD_SET_REKEY_OFFLOAD:
3120 nl80211_rekey_offload_event(drv, tb);
3121 break;
3122 case NL80211_CMD_PMKSA_CANDIDATE:
3123 nl80211_pmksa_candidate_event(drv, tb);
3124 break;
3125 case NL80211_CMD_PROBE_CLIENT:
3126 nl80211_client_probe_event(drv, tb);
3127 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003128 case NL80211_CMD_TDLS_OPER:
3129 nl80211_tdls_oper_event(drv, tb);
3130 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003131 case NL80211_CMD_CONN_FAILED:
3132 nl80211_connect_failed_event(drv, tb);
3133 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003134 case NL80211_CMD_FT_EVENT:
3135 mlme_event_ft_event(drv, tb);
3136 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003137 case NL80211_CMD_RADAR_DETECT:
3138 nl80211_radar_event(drv, tb);
3139 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003140 case NL80211_CMD_STOP_AP:
3141 nl80211_stop_ap(drv, tb);
3142 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003143 case NL80211_CMD_VENDOR:
3144 nl80211_vendor_event(drv, tb);
3145 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003146 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003147 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3148 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003149 break;
3150 }
3151}
3152
3153
3154static int process_drv_event(struct nl_msg *msg, void *arg)
3155{
3156 struct wpa_driver_nl80211_data *drv = arg;
3157 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3158 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003159 struct i802_bss *bss;
3160 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003161
3162 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3163 genlmsg_attrlen(gnlh, 0), NULL);
3164
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003165 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003166 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3167
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 (ifidx == -1 || ifidx == bss->ifindex) {
3170 do_process_drv_event(bss, gnlh->cmd, tb);
3171 return NL_SKIP;
3172 }
3173 wpa_printf(MSG_DEBUG,
3174 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3175 gnlh->cmd, ifidx);
3176 } else if (tb[NL80211_ATTR_WDEV]) {
3177 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3178 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003179 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003180 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3181 do_process_drv_event(bss, gnlh->cmd, tb);
3182 return NL_SKIP;
3183 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003184 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003185 wpa_printf(MSG_DEBUG,
3186 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3187 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003188 }
3189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003190 return NL_SKIP;
3191}
3192
3193
3194static int process_global_event(struct nl_msg *msg, void *arg)
3195{
3196 struct nl80211_global *global = arg;
3197 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3198 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003199 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003200 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003201 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003202 u64 wdev_id = 0;
3203 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003204
3205 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3206 genlmsg_attrlen(gnlh, 0), NULL);
3207
3208 if (tb[NL80211_ATTR_IFINDEX])
3209 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003210 else if (tb[NL80211_ATTR_WDEV]) {
3211 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3212 wdev_id_set = 1;
3213 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003214
Dmitry Shmidt04949592012-07-19 12:16:46 -07003215 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3216 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003217 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003218 if ((ifidx == -1 && !wdev_id_set) ||
3219 ifidx == bss->ifindex ||
3220 (wdev_id_set && bss->wdev_id_set &&
3221 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003222 do_process_drv_event(bss, gnlh->cmd, tb);
3223 return NL_SKIP;
3224 }
3225 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003226 }
3227
3228 return NL_SKIP;
3229}
3230
3231
3232static int process_bss_event(struct nl_msg *msg, void *arg)
3233{
3234 struct i802_bss *bss = arg;
3235 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3236 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3237
3238 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3239 genlmsg_attrlen(gnlh, 0), NULL);
3240
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003241 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3242 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3243 bss->ifname);
3244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003245 switch (gnlh->cmd) {
3246 case NL80211_CMD_FRAME:
3247 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003248 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003249 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3250 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003251 tb[NL80211_ATTR_COOKIE],
3252 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003253 break;
3254 case NL80211_CMD_UNEXPECTED_FRAME:
3255 nl80211_spurious_frame(bss, tb, 0);
3256 break;
3257 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3258 nl80211_spurious_frame(bss, tb, 1);
3259 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 default:
3261 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3262 "(cmd=%d)", gnlh->cmd);
3263 break;
3264 }
3265
3266 return NL_SKIP;
3267}
3268
3269
3270static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3271 void *handle)
3272{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003273 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003274 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003276 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003278 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07003279 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003280 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3281 __func__, res);
3282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283}
3284
3285
3286/**
3287 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3288 * @priv: driver_nl80211 private data
3289 * @alpha2_arg: country to which to switch to
3290 * Returns: 0 on success, -1 on failure
3291 *
3292 * This asks nl80211 to set the regulatory domain for given
3293 * country ISO / IEC alpha2.
3294 */
3295static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3296{
3297 struct i802_bss *bss = priv;
3298 struct wpa_driver_nl80211_data *drv = bss->drv;
3299 char alpha2[3];
3300 struct nl_msg *msg;
3301
3302 msg = nlmsg_alloc();
3303 if (!msg)
3304 return -ENOMEM;
3305
3306 alpha2[0] = alpha2_arg[0];
3307 alpha2[1] = alpha2_arg[1];
3308 alpha2[2] = '\0';
3309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311
3312 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3313 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3314 return -EINVAL;
3315 return 0;
3316nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003317 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 return -EINVAL;
3319}
3320
3321
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003322static int nl80211_get_country(struct nl_msg *msg, void *arg)
3323{
3324 char *alpha2 = arg;
3325 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3326 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3327
3328 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3329 genlmsg_attrlen(gnlh, 0), NULL);
3330 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3331 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3332 return NL_SKIP;
3333 }
3334 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3335 return NL_SKIP;
3336}
3337
3338
3339static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3340{
3341 struct i802_bss *bss = priv;
3342 struct wpa_driver_nl80211_data *drv = bss->drv;
3343 struct nl_msg *msg;
3344 int ret;
3345
3346 msg = nlmsg_alloc();
3347 if (!msg)
3348 return -ENOMEM;
3349
3350 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3351 alpha2[0] = '\0';
3352 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3353 if (!alpha2[0])
3354 ret = -1;
3355
3356 return ret;
3357}
3358
3359
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003360static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3361{
3362 u32 *feat = arg;
3363 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3364 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3365
3366 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3367 genlmsg_attrlen(gnlh, 0), NULL);
3368
3369 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3370 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3371
3372 return NL_SKIP;
3373}
3374
3375
3376static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3377{
3378 u32 feat = 0;
3379 struct nl_msg *msg;
3380
3381 msg = nlmsg_alloc();
3382 if (!msg)
3383 goto nla_put_failure;
3384
3385 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3386 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3387 return feat;
3388
3389 msg = NULL;
3390nla_put_failure:
3391 nlmsg_free(msg);
3392 return 0;
3393}
3394
3395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003397 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003398 struct wpa_driver_capa *capa;
3399
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003400 unsigned int num_multichan_concurrent;
3401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003402 unsigned int error:1;
3403 unsigned int device_ap_sme:1;
3404 unsigned int poll_command_supported:1;
3405 unsigned int data_tx_status:1;
3406 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003407 unsigned int auth_supported:1;
3408 unsigned int connect_supported:1;
3409 unsigned int p2p_go_supported:1;
3410 unsigned int p2p_client_supported:1;
3411 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003412 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003413 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414};
3415
3416
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003417static unsigned int probe_resp_offload_support(int supp_protocols)
3418{
3419 unsigned int prot = 0;
3420
3421 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3422 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3423 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3424 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3425 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3426 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3427 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3428 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3429
3430 return prot;
3431}
3432
3433
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003434static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3435 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003437 struct nlattr *nl_mode;
3438 int i;
3439
3440 if (tb == NULL)
3441 return;
3442
3443 nla_for_each_nested(nl_mode, tb, i) {
3444 switch (nla_type(nl_mode)) {
3445 case NL80211_IFTYPE_AP:
3446 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3447 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003448 case NL80211_IFTYPE_ADHOC:
3449 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3450 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003451 case NL80211_IFTYPE_P2P_DEVICE:
3452 info->capa->flags |=
3453 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3454 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003455 case NL80211_IFTYPE_P2P_GO:
3456 info->p2p_go_supported = 1;
3457 break;
3458 case NL80211_IFTYPE_P2P_CLIENT:
3459 info->p2p_client_supported = 1;
3460 break;
3461 case NL80211_IFTYPE_MONITOR:
3462 info->monitor_supported = 1;
3463 break;
3464 }
3465 }
3466}
3467
3468
3469static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3470 struct nlattr *nl_combi)
3471{
3472 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3473 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3474 struct nlattr *nl_limit, *nl_mode;
3475 int err, rem_limit, rem_mode;
3476 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003477 static struct nla_policy
3478 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3479 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3480 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3481 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3482 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003483 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003484 },
3485 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3486 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3487 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3488 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003490 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3491 nl_combi, iface_combination_policy);
3492 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3493 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3494 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3495 return 0; /* broken combination */
3496
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003497 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3498 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3499
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003500 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3501 rem_limit) {
3502 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3503 nl_limit, iface_limit_policy);
3504 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3505 return 0; /* broken combination */
3506
3507 nla_for_each_nested(nl_mode,
3508 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3509 rem_mode) {
3510 int ift = nla_type(nl_mode);
3511 if (ift == NL80211_IFTYPE_P2P_GO ||
3512 ift == NL80211_IFTYPE_P2P_CLIENT)
3513 combination_has_p2p = 1;
3514 if (ift == NL80211_IFTYPE_STATION)
3515 combination_has_mgd = 1;
3516 }
3517 if (combination_has_p2p && combination_has_mgd)
3518 break;
3519 }
3520
3521 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003522 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003523 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003524
3525 info->p2p_concurrent = 1;
3526 if (info->num_multichan_concurrent < num_channels)
3527 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003528 }
3529
3530 return 0;
3531}
3532
3533
3534static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3535 struct nlattr *tb)
3536{
3537 struct nlattr *nl_combi;
3538 int rem_combi;
3539
3540 if (tb == NULL)
3541 return;
3542
3543 nla_for_each_nested(nl_combi, tb, rem_combi) {
3544 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3545 break;
3546 }
3547}
3548
3549
3550static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3551 struct nlattr *tb)
3552{
3553 struct nlattr *nl_cmd;
3554 int i;
3555
3556 if (tb == NULL)
3557 return;
3558
3559 nla_for_each_nested(nl_cmd, tb, i) {
3560 switch (nla_get_u32(nl_cmd)) {
3561 case NL80211_CMD_AUTHENTICATE:
3562 info->auth_supported = 1;
3563 break;
3564 case NL80211_CMD_CONNECT:
3565 info->connect_supported = 1;
3566 break;
3567 case NL80211_CMD_START_SCHED_SCAN:
3568 info->capa->sched_scan_supported = 1;
3569 break;
3570 case NL80211_CMD_PROBE_CLIENT:
3571 info->poll_command_supported = 1;
3572 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003573 case NL80211_CMD_CHANNEL_SWITCH:
3574 info->channel_switch_supported = 1;
3575 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003576 case NL80211_CMD_SET_QOS_MAP:
3577 info->set_qos_map_supported = 1;
3578 break;
3579 }
3580 }
3581}
3582
3583
3584static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3585 struct nlattr *tb)
3586{
3587 int i, num;
3588 u32 *ciphers;
3589
3590 if (tb == NULL)
3591 return;
3592
3593 num = nla_len(tb) / sizeof(u32);
3594 ciphers = nla_data(tb);
3595 for (i = 0; i < num; i++) {
3596 u32 c = ciphers[i];
3597
3598 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3599 c >> 24, (c >> 16) & 0xff,
3600 (c >> 8) & 0xff, c & 0xff);
3601 switch (c) {
3602 case WLAN_CIPHER_SUITE_CCMP_256:
3603 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3604 break;
3605 case WLAN_CIPHER_SUITE_GCMP_256:
3606 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3607 break;
3608 case WLAN_CIPHER_SUITE_CCMP:
3609 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3610 break;
3611 case WLAN_CIPHER_SUITE_GCMP:
3612 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3613 break;
3614 case WLAN_CIPHER_SUITE_TKIP:
3615 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3616 break;
3617 case WLAN_CIPHER_SUITE_WEP104:
3618 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3619 break;
3620 case WLAN_CIPHER_SUITE_WEP40:
3621 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3622 break;
3623 case WLAN_CIPHER_SUITE_AES_CMAC:
3624 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3625 break;
3626 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3627 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3628 break;
3629 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3630 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3631 break;
3632 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3633 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3634 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003635 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3636 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3637 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003638 }
3639 }
3640}
3641
3642
3643static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3644 struct nlattr *tb)
3645{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003646 if (tb)
3647 capa->max_remain_on_chan = nla_get_u32(tb);
3648}
3649
3650
3651static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3652 struct nlattr *ext_setup)
3653{
3654 if (tdls == NULL)
3655 return;
3656
3657 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3658 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3659
3660 if (ext_setup) {
3661 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3662 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3663 }
3664}
3665
3666
3667static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3668 struct nlattr *tb)
3669{
3670 u32 flags;
3671 struct wpa_driver_capa *capa = info->capa;
3672
3673 if (tb == NULL)
3674 return;
3675
3676 flags = nla_get_u32(tb);
3677
3678 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3679 info->data_tx_status = 1;
3680
3681 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3682 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3683
3684 if (flags & NL80211_FEATURE_SAE)
3685 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3686
3687 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3688 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003689
3690 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3691 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003692}
3693
3694
3695static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3696 struct nlattr *tb)
3697{
3698 u32 protocols;
3699
3700 if (tb == NULL)
3701 return;
3702
3703 protocols = nla_get_u32(tb);
3704 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3705 "mode");
3706 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3707 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3708}
3709
3710
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003711static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3712 struct nlattr *tb)
3713{
3714 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3715
3716 if (tb == NULL)
3717 return;
3718
3719 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3720 tb, NULL))
3721 return;
3722
3723 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3724 capa->wowlan_triggers.any = 1;
3725 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3726 capa->wowlan_triggers.disconnect = 1;
3727 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3728 capa->wowlan_triggers.magic_pkt = 1;
3729 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3730 capa->wowlan_triggers.gtk_rekey_failure = 1;
3731 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3732 capa->wowlan_triggers.eap_identity_req = 1;
3733 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3734 capa->wowlan_triggers.four_way_handshake = 1;
3735 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3736 capa->wowlan_triggers.rfkill_release = 1;
3737}
3738
3739
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003740static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3741{
3742 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3743 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3744 struct wiphy_info_data *info = arg;
3745 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003746 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3749 genlmsg_attrlen(gnlh, 0), NULL);
3750
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003751 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003753 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3754 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003756 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003759 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3760 capa->max_sched_scan_ssids =
3761 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3762
3763 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3764 capa->max_match_sets =
3765 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3766
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003767 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3768 capa->max_acl_mac_addrs =
3769 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3770
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003771 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3772 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3773 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003774 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003776 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3777 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3778 "off-channel TX");
3779 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3780 }
3781
3782 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3783 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3784 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3785 }
3786
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003787 wiphy_info_max_roc(capa,
3788 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003789
3790 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3791 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003793 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3794 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003796 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3797 info->device_ap_sme = 1;
3798
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003799 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3800 wiphy_info_probe_resp_offload(capa,
3801 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003802
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003803 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3804 drv->extended_capa == NULL) {
3805 drv->extended_capa =
3806 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3807 if (drv->extended_capa) {
3808 os_memcpy(drv->extended_capa,
3809 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3810 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3811 drv->extended_capa_len =
3812 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3813 }
3814 drv->extended_capa_mask =
3815 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3816 if (drv->extended_capa_mask) {
3817 os_memcpy(drv->extended_capa_mask,
3818 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3819 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3820 } else {
3821 os_free(drv->extended_capa);
3822 drv->extended_capa = NULL;
3823 drv->extended_capa_len = 0;
3824 }
3825 }
3826
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003827 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3828 struct nlattr *nl;
3829 int rem;
3830
3831 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3832 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003833 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003834 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3835 continue;
3836 }
3837 vinfo = nla_data(nl);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003838 if (vinfo->subcmd ==
3839 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)
3840 drv->dfs_vendor_cmd_avail = 1;
3841
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003842 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3843 vinfo->vendor_id, vinfo->subcmd);
3844 }
3845 }
3846
3847 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3848 struct nlattr *nl;
3849 int rem;
3850
3851 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3852 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003853 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003854 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3855 continue;
3856 }
3857 vinfo = nla_data(nl);
3858 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3859 vinfo->vendor_id, vinfo->subcmd);
3860 }
3861 }
3862
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003863 wiphy_info_wowlan_triggers(capa,
3864 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
3865
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003866 if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
3867 capa->max_stations =
3868 nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
3869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 return NL_SKIP;
3871}
3872
3873
3874static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3875 struct wiphy_info_data *info)
3876{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003877 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 struct nl_msg *msg;
3879
3880 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003881 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003882 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883
3884 msg = nlmsg_alloc();
3885 if (!msg)
3886 return -1;
3887
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003888 feat = get_nl80211_protocol_features(drv);
3889 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3890 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3891 else
3892 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003894 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003895 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003896 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003898 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3899 return -1;
3900
3901 if (info->auth_supported)
3902 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3903 else if (!info->connect_supported) {
3904 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3905 "authentication/association or connect commands");
3906 info->error = 1;
3907 }
3908
3909 if (info->p2p_go_supported && info->p2p_client_supported)
3910 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3911 if (info->p2p_concurrent) {
3912 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3913 "interface (driver advertised support)");
3914 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3915 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3916 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003917 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003918 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3919 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003920 drv->capa.num_multichan_concurrent =
3921 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003922 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003923
3924 /* default to 5000 since early versions of mac80211 don't set it */
3925 if (!drv->capa.max_remain_on_chan)
3926 drv->capa.max_remain_on_chan = 5000;
3927
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003928 if (info->channel_switch_supported)
3929 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3930
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003931 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932nla_put_failure:
3933 nlmsg_free(msg);
3934 return -1;
3935}
3936
3937
3938static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3939{
3940 struct wiphy_info_data info;
3941 if (wpa_driver_nl80211_get_info(drv, &info))
3942 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003943
3944 if (info.error)
3945 return -1;
3946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3949 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3950 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3951 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3953 WPA_DRIVER_AUTH_SHARED |
3954 WPA_DRIVER_AUTH_LEAP;
3955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3957 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003959
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003960 /*
3961 * As all cfg80211 drivers must support cases where the AP interface is
3962 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
3963 * case that the user space daemon has crashed, they must be able to
3964 * cleanup all stations and key entries in the AP tear down flow. Thus,
3965 * this flag can/should always be set for cfg80211 drivers.
3966 */
3967 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
3968
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003969 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003970 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003971
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003972 /*
3973 * No AP SME is currently assumed to also indicate no AP MLME
3974 * in the driver/firmware.
3975 */
3976 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3977 }
3978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003979 drv->device_ap_sme = info.device_ap_sme;
3980 drv->poll_command_supported = info.poll_command_supported;
3981 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003982 if (info.set_qos_map_supported)
3983 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003984
3985 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003986 * If poll command and tx status are supported, mac80211 is new enough
3987 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003988 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003989 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003990
3991 if (drv->device_ap_sme && drv->use_monitor) {
3992 /*
3993 * Non-mac80211 drivers may not support monitor interface.
3994 * Make sure we do not get stuck with incorrect capability here
3995 * by explicitly testing this.
3996 */
3997 if (!info.monitor_supported) {
3998 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3999 "with device_ap_sme since no monitor mode "
4000 "support detected");
4001 drv->use_monitor = 0;
4002 }
4003 }
4004
4005 /*
4006 * If we aren't going to use monitor interfaces, but the
4007 * driver doesn't support data TX status, we won't get TX
4008 * status for EAPOL frames.
4009 */
4010 if (!drv->use_monitor && !info.data_tx_status)
4011 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012
4013 return 0;
4014}
4015
4016
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004017#ifdef ANDROID
4018static int android_genl_ctrl_resolve(struct nl_handle *handle,
4019 const char *name)
4020{
4021 /*
4022 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4023 * need to work around that.
4024 */
4025 struct nl_cache *cache = NULL;
4026 struct genl_family *nl80211 = NULL;
4027 int id = -1;
4028
4029 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4030 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4031 "netlink cache");
4032 goto fail;
4033 }
4034
4035 nl80211 = genl_ctrl_search_by_name(cache, name);
4036 if (nl80211 == NULL)
4037 goto fail;
4038
4039 id = genl_family_get_id(nl80211);
4040
4041fail:
4042 if (nl80211)
4043 genl_family_put(nl80211);
4044 if (cache)
4045 nl_cache_free(cache);
4046
4047 return id;
4048}
4049#define genl_ctrl_resolve android_genl_ctrl_resolve
4050#endif /* ANDROID */
4051
4052
4053static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054{
4055 int ret;
4056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4058 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4060 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 }
4063
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004064 global->nl = nl_create_handle(global->nl_cb, "nl");
4065 if (global->nl == NULL)
4066 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004068 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4069 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4071 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004072 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 }
4074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075 global->nl_event = nl_create_handle(global->nl_cb, "event");
4076 if (global->nl_event == NULL)
4077 goto err;
4078
4079 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004081 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 if (ret < 0) {
4083 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4084 "membership for scan events: %d (%s)",
4085 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004086 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 }
4088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004089 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004091 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 if (ret < 0) {
4093 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4094 "membership for mlme events: %d (%s)",
4095 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004096 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097 }
4098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004099 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004101 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 if (ret < 0) {
4103 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4104 "membership for regulatory events: %d (%s)",
4105 ret, strerror(-ret));
4106 /* Continue without regulatory events */
4107 }
4108
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004109 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4110 if (ret >= 0)
4111 ret = nl_socket_add_membership(global->nl_event, ret);
4112 if (ret < 0) {
4113 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4114 "membership for vendor events: %d (%s)",
4115 ret, strerror(-ret));
4116 /* Continue without vendor events */
4117 }
4118
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004119 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4120 no_seq_check, NULL);
4121 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4122 process_global_event, global);
4123
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004124 nl80211_register_eloop_read(&global->nl_event,
4125 wpa_driver_nl80211_event_receive,
4126 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127
4128 return 0;
4129
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004130err:
4131 nl_destroy_handles(&global->nl_event);
4132 nl_destroy_handles(&global->nl);
4133 nl_cb_put(global->nl_cb);
4134 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 return -1;
4136}
4137
4138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004139static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4140{
4141 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4142 if (!drv->nl_cb) {
4143 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4144 return -1;
4145 }
4146
4147 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4148 no_seq_check, NULL);
4149 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4150 process_drv_event, drv);
4151
4152 return 0;
4153}
4154
4155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4157{
4158 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4159 /*
4160 * This may be for any interface; use ifdown event to disable
4161 * interface.
4162 */
4163}
4164
4165
4166static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4167{
4168 struct wpa_driver_nl80211_data *drv = ctx;
4169 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004170 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4172 "after rfkill unblock");
4173 return;
4174 }
4175 /* rtnetlink ifup handler will report interface as enabled */
4176}
4177
4178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004179static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4180 void *eloop_ctx,
4181 void *handle)
4182{
4183 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4184 u8 data[2048];
4185 struct msghdr msg;
4186 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004187 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188 struct cmsghdr *cmsg;
4189 int res, found_ee = 0, found_wifi = 0, acked = 0;
4190 union wpa_event_data event;
4191
4192 memset(&msg, 0, sizeof(msg));
4193 msg.msg_iov = &entry;
4194 msg.msg_iovlen = 1;
4195 entry.iov_base = data;
4196 entry.iov_len = sizeof(data);
4197 msg.msg_control = &control;
4198 msg.msg_controllen = sizeof(control);
4199
4200 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4201 /* if error or not fitting 802.3 header, return */
4202 if (res < 14)
4203 return;
4204
4205 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4206 {
4207 if (cmsg->cmsg_level == SOL_SOCKET &&
4208 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4209 int *ack;
4210
4211 found_wifi = 1;
4212 ack = (void *)CMSG_DATA(cmsg);
4213 acked = *ack;
4214 }
4215
4216 if (cmsg->cmsg_level == SOL_PACKET &&
4217 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4218 struct sock_extended_err *err =
4219 (struct sock_extended_err *)CMSG_DATA(cmsg);
4220
4221 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4222 found_ee = 1;
4223 }
4224 }
4225
4226 if (!found_ee || !found_wifi)
4227 return;
4228
4229 memset(&event, 0, sizeof(event));
4230 event.eapol_tx_status.dst = data;
4231 event.eapol_tx_status.data = data + 14;
4232 event.eapol_tx_status.data_len = res - 14;
4233 event.eapol_tx_status.ack = acked;
4234 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4235}
4236
4237
4238static int nl80211_init_bss(struct i802_bss *bss)
4239{
4240 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4241 if (!bss->nl_cb)
4242 return -1;
4243
4244 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4245 no_seq_check, NULL);
4246 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4247 process_bss_event, bss);
4248
4249 return 0;
4250}
4251
4252
4253static void nl80211_destroy_bss(struct i802_bss *bss)
4254{
4255 nl_cb_put(bss->nl_cb);
4256 bss->nl_cb = NULL;
4257}
4258
4259
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004260static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4261 void *global_priv, int hostapd,
4262 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263{
4264 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004265 struct rfkill_config *rcfg;
4266 struct i802_bss *bss;
4267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004268 if (global_priv == NULL)
4269 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 drv = os_zalloc(sizeof(*drv));
4271 if (drv == NULL)
4272 return NULL;
4273 drv->global = global_priv;
4274 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004275 drv->hostapd = !!hostapd;
4276 drv->eapol_sock = -1;
4277 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4278 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004279
4280 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4281 if (!drv->first_bss) {
4282 os_free(drv);
4283 return NULL;
4284 }
4285 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004287 bss->ctx = ctx;
4288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4290 drv->monitor_ifidx = -1;
4291 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004292 drv->eapol_tx_sock = -1;
4293 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294
4295 if (wpa_driver_nl80211_init_nl(drv)) {
4296 os_free(drv);
4297 return NULL;
4298 }
4299
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004300 if (nl80211_init_bss(bss))
4301 goto failed;
4302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 rcfg = os_zalloc(sizeof(*rcfg));
4304 if (rcfg == NULL)
4305 goto failed;
4306 rcfg->ctx = drv;
4307 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4308 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4309 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4310 drv->rfkill = rfkill_init(rcfg);
4311 if (drv->rfkill == NULL) {
4312 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4313 os_free(rcfg);
4314 }
4315
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004316 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4317 drv->start_iface_up = 1;
4318
4319 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 goto failed;
4321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4323 if (drv->eapol_tx_sock < 0)
4324 goto failed;
4325
4326 if (drv->data_tx_status) {
4327 int enabled = 1;
4328
4329 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4330 &enabled, sizeof(enabled)) < 0) {
4331 wpa_printf(MSG_DEBUG,
4332 "nl80211: wifi status sockopt failed\n");
4333 drv->data_tx_status = 0;
4334 if (!drv->use_monitor)
4335 drv->capa.flags &=
4336 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4337 } else {
4338 eloop_register_read_sock(drv->eapol_tx_sock,
4339 wpa_driver_nl80211_handle_eapol_tx_status,
4340 drv, NULL);
4341 }
4342 }
4343
4344 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004346 drv->in_interface_list = 1;
4347 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348
4349 return bss;
4350
4351failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004352 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 return NULL;
4354}
4355
4356
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004357/**
4358 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4359 * @ctx: context to be used when calling wpa_supplicant functions,
4360 * e.g., wpa_supplicant_event()
4361 * @ifname: interface name, e.g., wlan0
4362 * @global_priv: private driver global data from global_init()
4363 * Returns: Pointer to private data, %NULL on failure
4364 */
4365static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4366 void *global_priv)
4367{
4368 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4369}
4370
4371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004372static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 struct nl_handle *nl_handle,
4374 u16 type, const u8 *match, size_t match_len)
4375{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 struct nl_msg *msg;
4378 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004379 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004380
4381 msg = nlmsg_alloc();
4382 if (!msg)
4383 return -1;
4384
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004385 buf[0] = '\0';
4386 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4387 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4388 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004390 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4391
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004392 if (nl80211_set_iface_id(msg, bss) < 0)
4393 goto nla_put_failure;
4394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4396 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4397
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004398 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399 msg = NULL;
4400 if (ret) {
4401 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4402 "failed (type=%u): ret=%d (%s)",
4403 type, ret, strerror(-ret));
4404 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4405 match, match_len);
4406 goto nla_put_failure;
4407 }
4408 ret = 0;
4409nla_put_failure:
4410 nlmsg_free(msg);
4411 return ret;
4412}
4413
4414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004415static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4416{
4417 struct wpa_driver_nl80211_data *drv = bss->drv;
4418
4419 if (bss->nl_mgmt) {
4420 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4421 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4422 return -1;
4423 }
4424
4425 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4426 if (bss->nl_mgmt == NULL)
4427 return -1;
4428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004429 return 0;
4430}
4431
4432
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004433static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4434{
4435 nl80211_register_eloop_read(&bss->nl_mgmt,
4436 wpa_driver_nl80211_event_receive,
4437 bss->nl_cb);
4438}
4439
4440
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004441static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 const u8 *match, size_t match_len)
4443{
4444 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004445 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 type, match, match_len);
4447}
4448
4449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004450static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004452 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004453 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004454
4455 if (nl80211_alloc_mgmt_handle(bss))
4456 return -1;
4457 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4458 "handle %p", bss->nl_mgmt);
4459
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004460 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4461 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4462
4463 /* register for any AUTH message */
4464 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4465 }
4466
Dmitry Shmidt051af732013-10-22 13:52:46 -07004467#ifdef CONFIG_INTERWORKING
4468 /* QoS Map Configure */
4469 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004470 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004471#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004472#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004473 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004474 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004475 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004476 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004477 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004478 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004480 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004481 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004483 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004484 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004485 /* Protected GAS Initial Request */
4486 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4487 ret = -1;
4488 /* Protected GAS Initial Response */
4489 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4490 ret = -1;
4491 /* Protected GAS Comeback Request */
4492 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4493 ret = -1;
4494 /* Protected GAS Comeback Response */
4495 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4496 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4498#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004500 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4502 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004503 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004505 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506 (u8 *) "\x7f\x50\x6f\x9a\x09",
4507 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004508 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004509#endif /* CONFIG_P2P */
4510#ifdef CONFIG_IEEE80211W
4511 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004512 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004513 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004515#ifdef CONFIG_TDLS
4516 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4517 /* TDLS Discovery Response */
4518 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4519 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004520 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004521 }
4522#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523
4524 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004525 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004526 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004527 else
4528 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4529 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004531 /* WNM - BSS Transition Management Request */
4532 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004533 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004534 /* WNM-Sleep Mode Response */
4535 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004536 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004537
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004538#ifdef CONFIG_HS20
4539 /* WNM-Notification */
4540 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4541 return -1;
4542#endif /* CONFIG_HS20 */
4543
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004544 nl80211_mgmt_handle_register_eloop(bss);
4545
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004546 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547}
4548
4549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004550static int nl80211_register_spurious_class3(struct i802_bss *bss)
4551{
4552 struct wpa_driver_nl80211_data *drv = bss->drv;
4553 struct nl_msg *msg;
4554 int ret = -1;
4555
4556 msg = nlmsg_alloc();
4557 if (!msg)
4558 return -1;
4559
4560 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4561
4562 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4563
4564 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4565 msg = NULL;
4566 if (ret) {
4567 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4568 "failed: ret=%d (%s)",
4569 ret, strerror(-ret));
4570 goto nla_put_failure;
4571 }
4572 ret = 0;
4573nla_put_failure:
4574 nlmsg_free(msg);
4575 return ret;
4576}
4577
4578
4579static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4580{
4581 static const int stypes[] = {
4582 WLAN_FC_STYPE_AUTH,
4583 WLAN_FC_STYPE_ASSOC_REQ,
4584 WLAN_FC_STYPE_REASSOC_REQ,
4585 WLAN_FC_STYPE_DISASSOC,
4586 WLAN_FC_STYPE_DEAUTH,
4587 WLAN_FC_STYPE_ACTION,
4588 WLAN_FC_STYPE_PROBE_REQ,
4589/* Beacon doesn't work as mac80211 doesn't currently allow
4590 * it, but it wouldn't really be the right thing anyway as
4591 * it isn't per interface ... maybe just dump the scan
4592 * results periodically for OLBC?
4593 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004594 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004595 };
4596 unsigned int i;
4597
4598 if (nl80211_alloc_mgmt_handle(bss))
4599 return -1;
4600 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4601 "handle %p", bss->nl_mgmt);
4602
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004603 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004604 if (nl80211_register_frame(bss, bss->nl_mgmt,
4605 (WLAN_FC_TYPE_MGMT << 2) |
4606 (stypes[i] << 4),
4607 NULL, 0) < 0) {
4608 goto out_err;
4609 }
4610 }
4611
4612 if (nl80211_register_spurious_class3(bss))
4613 goto out_err;
4614
4615 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4616 goto out_err;
4617
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004618 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004619 return 0;
4620
4621out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004622 nl_destroy_handles(&bss->nl_mgmt);
4623 return -1;
4624}
4625
4626
4627static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4628{
4629 if (nl80211_alloc_mgmt_handle(bss))
4630 return -1;
4631 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4632 "handle %p (device SME)", bss->nl_mgmt);
4633
4634 if (nl80211_register_frame(bss, bss->nl_mgmt,
4635 (WLAN_FC_TYPE_MGMT << 2) |
4636 (WLAN_FC_STYPE_ACTION << 4),
4637 NULL, 0) < 0)
4638 goto out_err;
4639
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004640 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004641 return 0;
4642
4643out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004644 nl_destroy_handles(&bss->nl_mgmt);
4645 return -1;
4646}
4647
4648
4649static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4650{
4651 if (bss->nl_mgmt == NULL)
4652 return;
4653 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4654 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004655 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004656
4657 nl80211_put_wiphy_data_ap(bss);
4658}
4659
4660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4662{
4663 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4664}
4665
4666
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004667static void nl80211_del_p2pdev(struct i802_bss *bss)
4668{
4669 struct wpa_driver_nl80211_data *drv = bss->drv;
4670 struct nl_msg *msg;
4671 int ret;
4672
4673 msg = nlmsg_alloc();
4674 if (!msg)
4675 return;
4676
4677 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4678 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4679
4680 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4681 msg = NULL;
4682
4683 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4684 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004685 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004686
4687nla_put_failure:
4688 nlmsg_free(msg);
4689}
4690
4691
4692static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4693{
4694 struct wpa_driver_nl80211_data *drv = bss->drv;
4695 struct nl_msg *msg;
4696 int ret = -1;
4697
4698 msg = nlmsg_alloc();
4699 if (!msg)
4700 return -1;
4701
4702 if (start)
4703 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4704 else
4705 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4706
4707 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4708
4709 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4710 msg = NULL;
4711
4712 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4713 start ? "Start" : "Stop",
4714 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004715 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004716
4717nla_put_failure:
4718 nlmsg_free(msg);
4719 return ret;
4720}
4721
4722
4723static int i802_set_iface_flags(struct i802_bss *bss, int up)
4724{
4725 enum nl80211_iftype nlmode;
4726
4727 nlmode = nl80211_get_ifmode(bss);
4728 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4729 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4730 bss->ifname, up);
4731 }
4732
4733 /* P2P Device has start/stop which is equivalent */
4734 return nl80211_set_p2pdev(bss, up);
4735}
4736
4737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004739wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4740 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004742 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004744 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745
4746 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004747 bss->ifindex = drv->ifindex;
4748 bss->wdev_id = drv->global->if_add_wdevid;
4749 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4750
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004751 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4752 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004753 drv->global->if_add_wdevid_set = 0;
4754
4755 if (wpa_driver_nl80211_capa(drv))
4756 return -1;
4757
4758 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4759 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004761 if (set_addr &&
4762 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4763 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4764 set_addr)))
4765 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004766
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004767 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4768 drv->start_mode_ap = 1;
4769
4770 if (drv->hostapd)
4771 nlmode = NL80211_IFTYPE_AP;
4772 else if (bss->if_dynamic)
4773 nlmode = nl80211_get_ifmode(bss);
4774 else
4775 nlmode = NL80211_IFTYPE_STATION;
4776
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004777 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004778 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004779 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004781
Dmitry Shmidt98660862014-03-11 17:26:21 -07004782 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004783 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784
Dmitry Shmidt98660862014-03-11 17:26:21 -07004785 if (!rfkill_is_blocked(drv->rfkill)) {
4786 int ret = i802_set_iface_flags(bss, 1);
4787 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004788 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4789 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004790 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004792 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4793 return ret;
4794 } else {
4795 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4796 "interface '%s' due to rfkill", bss->ifname);
4797 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4798 return 0;
4799 drv->if_disabled = 1;
4800 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 }
4802
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004803 if (!drv->hostapd)
4804 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4805 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004807 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4808 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 return -1;
4810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 if (send_rfkill_event) {
4812 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4813 drv, drv->ctx);
4814 }
4815
4816 return 0;
4817}
4818
4819
4820static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4821{
4822 struct nl_msg *msg;
4823
4824 msg = nlmsg_alloc();
4825 if (!msg)
4826 return -ENOMEM;
4827
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004828 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4829 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004830 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4832
4833 return send_and_recv_msgs(drv, msg, NULL, NULL);
4834 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004835 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 return -ENOBUFS;
4837}
4838
4839
4840/**
4841 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004842 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 *
4844 * Shut down driver interface and processing of driver events. Free
4845 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4846 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004847static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 struct wpa_driver_nl80211_data *drv = bss->drv;
4850
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004852 if (drv->data_tx_status)
4853 eloop_unregister_read_sock(drv->eapol_tx_sock);
4854 if (drv->eapol_tx_sock >= 0)
4855 close(drv->eapol_tx_sock);
4856
4857 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858 wpa_driver_nl80211_probe_req_report(bss, 0);
4859 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004860 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4861 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4863 "interface %s from bridge %s: %s",
4864 bss->ifname, bss->brname, strerror(errno));
4865 }
4866 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004867 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4869 "bridge %s: %s",
4870 bss->brname, strerror(errno));
4871 }
4872
4873 nl80211_remove_monitor_interface(drv);
4874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004875 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876 wpa_driver_nl80211_del_beacon(drv);
4877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 if (drv->eapol_sock >= 0) {
4879 eloop_unregister_read_sock(drv->eapol_sock);
4880 close(drv->eapol_sock);
4881 }
4882
4883 if (drv->if_indices != drv->default_if_indices)
4884 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004889 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4890 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004891 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 rfkill_deinit(drv->rfkill);
4893
4894 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4895
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004896 if (!drv->start_iface_up)
4897 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004898 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004899 if (!drv->hostapd || !drv->start_mode_ap)
4900 wpa_driver_nl80211_set_mode(bss,
4901 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004902 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004903 } else {
4904 nl80211_mgmt_unsubscribe(bss, "deinit");
4905 nl80211_del_p2pdev(bss);
4906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907 nl_cb_put(drv->nl_cb);
4908
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004909 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 os_free(drv->filter_ssids);
4912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004913 os_free(drv->auth_ie);
4914
4915 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 dl_list_del(&drv->list);
4917
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004918 os_free(drv->extended_capa);
4919 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004920 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921 os_free(drv);
4922}
4923
4924
4925/**
4926 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4927 * @eloop_ctx: Driver private data
4928 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4929 *
4930 * This function can be used as registered timeout when starting a scan to
4931 * generate a scan completed event if the driver does not report this.
4932 */
4933static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4934{
4935 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004936 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004937 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004938 drv->ap_scan_as_station);
4939 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 }
4941 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4942 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4943}
4944
4945
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004946static struct nl_msg *
4947nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004948 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004949{
4950 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004951 size_t i;
4952
4953 msg = nlmsg_alloc();
4954 if (!msg)
4955 return NULL;
4956
4957 nl80211_cmd(drv, msg, 0, cmd);
4958
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004959 if (!wdev_id)
4960 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4961 else
4962 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004963
4964 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004965 struct nlattr *ssids;
4966
4967 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004968 if (ssids == NULL)
4969 goto fail;
4970 for (i = 0; i < params->num_ssids; i++) {
4971 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4972 params->ssids[i].ssid,
4973 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004974 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4975 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004976 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004977 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004978 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004979 }
4980
4981 if (params->extra_ies) {
4982 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4983 params->extra_ies, params->extra_ies_len);
4984 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4985 params->extra_ies) < 0)
4986 goto fail;
4987 }
4988
4989 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004990 struct nlattr *freqs;
4991 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004992 if (freqs == NULL)
4993 goto fail;
4994 for (i = 0; params->freqs[i]; i++) {
4995 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4996 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004997 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004998 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004999 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005000 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005001 }
5002
5003 os_free(drv->filter_ssids);
5004 drv->filter_ssids = params->filter_ssids;
5005 params->filter_ssids = NULL;
5006 drv->num_filter_ssids = params->num_filter_ssids;
5007
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005008 if (params->only_new_results) {
5009 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
5010 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
5011 NL80211_SCAN_FLAG_FLUSH);
5012 }
5013
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005014 return msg;
5015
5016fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005017nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005018 nlmsg_free(msg);
5019 return NULL;
5020}
5021
5022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005023/**
5024 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005025 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 * @params: Scan parameters
5027 * Returns: 0 on success, -1 on failure
5028 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005029static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 struct wpa_driver_scan_params *params)
5031{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005033 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005034 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005036 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005037 drv->scan_for_auth = 0;
5038
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005039 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5040 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005041 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005044 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005045 struct nlattr *rates;
5046
Dmitry Shmidt04949592012-07-19 12:16:46 -07005047 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5048
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005049 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005050 if (rates == NULL)
5051 goto nla_put_failure;
5052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005053 /*
5054 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5055 * by masking out everything else apart from the OFDM rates 6,
5056 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5057 * rates are left enabled.
5058 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005059 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005060 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005061 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005062
5063 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
5064 }
5065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5067 msg = NULL;
5068 if (ret) {
5069 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5070 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005071 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005072 enum nl80211_iftype old_mode = drv->nlmode;
5073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 /*
5075 * mac80211 does not allow scan requests in AP mode, so
5076 * try to do this in station mode.
5077 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005078 if (wpa_driver_nl80211_set_mode(
5079 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 goto nla_put_failure;
5081
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005082 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005083 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084 goto nla_put_failure;
5085 }
5086
5087 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005088 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005089 ret = 0;
5090 } else
5091 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 }
5093
Dmitry Shmidt56052862013-10-04 10:23:25 -07005094 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095 /* Not all drivers generate "scan completed" wireless event, so try to
5096 * read results after a timeout. */
5097 timeout = 10;
5098 if (drv->scan_complete_events) {
5099 /*
5100 * The driver seems to deliver events to notify when scan is
5101 * complete, so use longer timeout to avoid race conditions
5102 * with scanning and following association request.
5103 */
5104 timeout = 30;
5105 }
5106 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5107 "seconds", ret, timeout);
5108 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5109 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5110 drv, drv->ctx);
5111
5112nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005113 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005114 return ret;
5115}
5116
5117
5118/**
5119 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5120 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5121 * @params: Scan parameters
5122 * @interval: Interval between scan cycles in milliseconds
5123 * Returns: 0 on success, -1 on failure or if not supported
5124 */
5125static int wpa_driver_nl80211_sched_scan(void *priv,
5126 struct wpa_driver_scan_params *params,
5127 u32 interval)
5128{
5129 struct i802_bss *bss = priv;
5130 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005131 int ret = -1;
5132 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005133 size_t i;
5134
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005135 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5136
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005137#ifdef ANDROID
5138 if (!drv->capa.sched_scan_supported)
5139 return android_pno_start(bss, params);
5140#endif /* ANDROID */
5141
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005142 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5143 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005144 if (!msg)
5145 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005146
5147 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5148
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005149 if ((drv->num_filter_ssids &&
5150 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5151 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005152 struct nlattr *match_sets;
5153 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005154 if (match_sets == NULL)
5155 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005156
5157 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005158 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005159 wpa_hexdump_ascii(MSG_MSGDUMP,
5160 "nl80211: Sched scan filter SSID",
5161 drv->filter_ssids[i].ssid,
5162 drv->filter_ssids[i].ssid_len);
5163
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005164 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005165 if (match_set_ssid == NULL)
5166 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005167 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005168 drv->filter_ssids[i].ssid_len,
5169 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005170 if (params->filter_rssi)
5171 NLA_PUT_U32(msg,
5172 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5173 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005175 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005176 }
5177
Dmitry Shmidt97672262014-02-03 13:02:54 -08005178 /*
5179 * Due to backward compatibility code, newer kernels treat this
5180 * matchset (with only an RSSI filter) as the default for all
5181 * other matchsets, unless it's the only one, in which case the
5182 * matchset will actually allow all SSIDs above the RSSI.
5183 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005184 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005185 struct nlattr *match_set_rssi;
5186 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005187 if (match_set_rssi == NULL)
5188 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005189 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005190 params->filter_rssi);
5191 wpa_printf(MSG_MSGDUMP,
5192 "nl80211: Sched scan RSSI filter %d dBm",
5193 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005194 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005195 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005196
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005197 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005198 }
5199
5200 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5201
5202 /* TODO: if we get an error here, we should fall back to normal scan */
5203
5204 msg = NULL;
5205 if (ret) {
5206 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5207 "ret=%d (%s)", ret, strerror(-ret));
5208 goto nla_put_failure;
5209 }
5210
5211 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5212 "scan interval %d msec", ret, interval);
5213
5214nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005215 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005216 return ret;
5217}
5218
5219
5220/**
5221 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5222 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5223 * Returns: 0 on success, -1 on failure or if not supported
5224 */
5225static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5226{
5227 struct i802_bss *bss = priv;
5228 struct wpa_driver_nl80211_data *drv = bss->drv;
5229 int ret = 0;
5230 struct nl_msg *msg;
5231
5232#ifdef ANDROID
5233 if (!drv->capa.sched_scan_supported)
5234 return android_pno_stop(bss);
5235#endif /* ANDROID */
5236
5237 msg = nlmsg_alloc();
5238 if (!msg)
5239 return -1;
5240
5241 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5242
5243 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5244
5245 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5246 msg = NULL;
5247 if (ret) {
5248 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5249 "ret=%d (%s)", ret, strerror(-ret));
5250 goto nla_put_failure;
5251 }
5252
5253 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5254
5255nla_put_failure:
5256 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 return ret;
5258}
5259
5260
5261static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5262{
5263 const u8 *end, *pos;
5264
5265 if (ies == NULL)
5266 return NULL;
5267
5268 pos = ies;
5269 end = ies + ies_len;
5270
5271 while (pos + 1 < end) {
5272 if (pos + 2 + pos[1] > end)
5273 break;
5274 if (pos[0] == ie)
5275 return pos;
5276 pos += 2 + pos[1];
5277 }
5278
5279 return NULL;
5280}
5281
5282
5283static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5284 const u8 *ie, size_t ie_len)
5285{
5286 const u8 *ssid;
5287 size_t i;
5288
5289 if (drv->filter_ssids == NULL)
5290 return 0;
5291
5292 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5293 if (ssid == NULL)
5294 return 1;
5295
5296 for (i = 0; i < drv->num_filter_ssids; i++) {
5297 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5298 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5299 0)
5300 return 0;
5301 }
5302
5303 return 1;
5304}
5305
5306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307static int bss_info_handler(struct nl_msg *msg, void *arg)
5308{
5309 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5310 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5311 struct nlattr *bss[NL80211_BSS_MAX + 1];
5312 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5313 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5314 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5315 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5316 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5317 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5318 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5319 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5320 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5321 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5322 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5323 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5324 };
5325 struct nl80211_bss_info_arg *_arg = arg;
5326 struct wpa_scan_results *res = _arg->res;
5327 struct wpa_scan_res **tmp;
5328 struct wpa_scan_res *r;
5329 const u8 *ie, *beacon_ie;
5330 size_t ie_len, beacon_ie_len;
5331 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005332 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333
5334 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5335 genlmsg_attrlen(gnlh, 0), NULL);
5336 if (!tb[NL80211_ATTR_BSS])
5337 return NL_SKIP;
5338 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5339 bss_policy))
5340 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005341 if (bss[NL80211_BSS_STATUS]) {
5342 enum nl80211_bss_status status;
5343 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5344 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5345 bss[NL80211_BSS_FREQUENCY]) {
5346 _arg->assoc_freq =
5347 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5348 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5349 _arg->assoc_freq);
5350 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07005351 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
5352 bss[NL80211_BSS_FREQUENCY]) {
5353 _arg->ibss_freq =
5354 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5355 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
5356 _arg->ibss_freq);
5357 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005358 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5359 bss[NL80211_BSS_BSSID]) {
5360 os_memcpy(_arg->assoc_bssid,
5361 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5362 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5363 MACSTR, MAC2STR(_arg->assoc_bssid));
5364 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005365 }
5366 if (!res)
5367 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5369 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5370 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5371 } else {
5372 ie = NULL;
5373 ie_len = 0;
5374 }
5375 if (bss[NL80211_BSS_BEACON_IES]) {
5376 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5377 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5378 } else {
5379 beacon_ie = NULL;
5380 beacon_ie_len = 0;
5381 }
5382
5383 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5384 ie ? ie_len : beacon_ie_len))
5385 return NL_SKIP;
5386
5387 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5388 if (r == NULL)
5389 return NL_SKIP;
5390 if (bss[NL80211_BSS_BSSID])
5391 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5392 ETH_ALEN);
5393 if (bss[NL80211_BSS_FREQUENCY])
5394 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5395 if (bss[NL80211_BSS_BEACON_INTERVAL])
5396 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5397 if (bss[NL80211_BSS_CAPABILITY])
5398 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5399 r->flags |= WPA_SCAN_NOISE_INVALID;
5400 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5401 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5402 r->level /= 100; /* mBm to dBm */
5403 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5404 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5405 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005406 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005407 } else
5408 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5409 if (bss[NL80211_BSS_TSF])
5410 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5411 if (bss[NL80211_BSS_SEEN_MS_AGO])
5412 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5413 r->ie_len = ie_len;
5414 pos = (u8 *) (r + 1);
5415 if (ie) {
5416 os_memcpy(pos, ie, ie_len);
5417 pos += ie_len;
5418 }
5419 r->beacon_ie_len = beacon_ie_len;
5420 if (beacon_ie)
5421 os_memcpy(pos, beacon_ie, beacon_ie_len);
5422
5423 if (bss[NL80211_BSS_STATUS]) {
5424 enum nl80211_bss_status status;
5425 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5426 switch (status) {
5427 case NL80211_BSS_STATUS_AUTHENTICATED:
5428 r->flags |= WPA_SCAN_AUTHENTICATED;
5429 break;
5430 case NL80211_BSS_STATUS_ASSOCIATED:
5431 r->flags |= WPA_SCAN_ASSOCIATED;
5432 break;
5433 default:
5434 break;
5435 }
5436 }
5437
Jouni Malinen87fd2792011-05-16 18:35:42 +03005438 /*
5439 * cfg80211 maintains separate BSS table entries for APs if the same
5440 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5441 * not use frequency as a separate key in the BSS table, so filter out
5442 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005443 * order to get the correct frequency into the BSS table. Similarly,
5444 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005445 */
5446 for (i = 0; i < res->num; i++) {
5447 const u8 *s1, *s2;
5448 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5449 continue;
5450
5451 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5452 res->res[i]->ie_len, WLAN_EID_SSID);
5453 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5454 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5455 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5456 continue;
5457
5458 /* Same BSSID,SSID was already included in scan results */
5459 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5460 "for " MACSTR, MAC2STR(r->bssid));
5461
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005462 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5463 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5464 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005465 os_free(res->res[i]);
5466 res->res[i] = r;
5467 } else
5468 os_free(r);
5469 return NL_SKIP;
5470 }
5471
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005472 tmp = os_realloc_array(res->res, res->num + 1,
5473 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 if (tmp == NULL) {
5475 os_free(r);
5476 return NL_SKIP;
5477 }
5478 tmp[res->num++] = r;
5479 res->res = tmp;
5480
5481 return NL_SKIP;
5482}
5483
5484
5485static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5486 const u8 *addr)
5487{
5488 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5489 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5490 "mismatch (" MACSTR ")", MAC2STR(addr));
5491 wpa_driver_nl80211_mlme(drv, addr,
5492 NL80211_CMD_DEAUTHENTICATE,
5493 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5494 }
5495}
5496
5497
5498static void wpa_driver_nl80211_check_bss_status(
5499 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5500{
5501 size_t i;
5502
5503 for (i = 0; i < res->num; i++) {
5504 struct wpa_scan_res *r = res->res[i];
5505 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5506 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5507 "indicates BSS status with " MACSTR
5508 " as authenticated",
5509 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005510 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5512 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5513 0) {
5514 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5515 " in local state (auth=" MACSTR
5516 " assoc=" MACSTR ")",
5517 MAC2STR(drv->auth_bssid),
5518 MAC2STR(drv->bssid));
5519 clear_state_mismatch(drv, r->bssid);
5520 }
5521 }
5522
5523 if (r->flags & WPA_SCAN_ASSOCIATED) {
5524 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5525 "indicate BSS status with " MACSTR
5526 " as associated",
5527 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005528 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 !drv->associated) {
5530 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5531 "(not associated) does not match "
5532 "with BSS state");
5533 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005534 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5536 0) {
5537 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5538 "(associated with " MACSTR ") does "
5539 "not match with BSS state",
5540 MAC2STR(drv->bssid));
5541 clear_state_mismatch(drv, r->bssid);
5542 clear_state_mismatch(drv, drv->bssid);
5543 }
5544 }
5545 }
5546}
5547
5548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549static struct wpa_scan_results *
5550nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5551{
5552 struct nl_msg *msg;
5553 struct wpa_scan_results *res;
5554 int ret;
5555 struct nl80211_bss_info_arg arg;
5556
5557 res = os_zalloc(sizeof(*res));
5558 if (res == NULL)
5559 return NULL;
5560 msg = nlmsg_alloc();
5561 if (!msg)
5562 goto nla_put_failure;
5563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005564 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005565 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005566 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567
5568 arg.drv = drv;
5569 arg.res = res;
5570 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5571 msg = NULL;
5572 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005573 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5574 "BSSes)", (unsigned long) res->num);
5575 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 return res;
5577 }
5578 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5579 "(%s)", ret, strerror(-ret));
5580nla_put_failure:
5581 nlmsg_free(msg);
5582 wpa_scan_results_free(res);
5583 return NULL;
5584}
5585
5586
5587/**
5588 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5589 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5590 * Returns: Scan results on success, -1 on failure
5591 */
5592static struct wpa_scan_results *
5593wpa_driver_nl80211_get_scan_results(void *priv)
5594{
5595 struct i802_bss *bss = priv;
5596 struct wpa_driver_nl80211_data *drv = bss->drv;
5597 struct wpa_scan_results *res;
5598
5599 res = nl80211_get_scan_results(drv);
5600 if (res)
5601 wpa_driver_nl80211_check_bss_status(drv, res);
5602 return res;
5603}
5604
5605
5606static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5607{
5608 struct wpa_scan_results *res;
5609 size_t i;
5610
5611 res = nl80211_get_scan_results(drv);
5612 if (res == NULL) {
5613 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5614 return;
5615 }
5616
5617 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5618 for (i = 0; i < res->num; i++) {
5619 struct wpa_scan_res *r = res->res[i];
5620 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5621 (int) i, (int) res->num, MAC2STR(r->bssid),
5622 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5623 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5624 }
5625
5626 wpa_scan_results_free(res);
5627}
5628
5629
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005630static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5631{
5632 switch (alg) {
5633 case WPA_ALG_WEP:
5634 if (key_len == 5)
5635 return WLAN_CIPHER_SUITE_WEP40;
5636 return WLAN_CIPHER_SUITE_WEP104;
5637 case WPA_ALG_TKIP:
5638 return WLAN_CIPHER_SUITE_TKIP;
5639 case WPA_ALG_CCMP:
5640 return WLAN_CIPHER_SUITE_CCMP;
5641 case WPA_ALG_GCMP:
5642 return WLAN_CIPHER_SUITE_GCMP;
5643 case WPA_ALG_CCMP_256:
5644 return WLAN_CIPHER_SUITE_CCMP_256;
5645 case WPA_ALG_GCMP_256:
5646 return WLAN_CIPHER_SUITE_GCMP_256;
5647 case WPA_ALG_IGTK:
5648 return WLAN_CIPHER_SUITE_AES_CMAC;
5649 case WPA_ALG_BIP_GMAC_128:
5650 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5651 case WPA_ALG_BIP_GMAC_256:
5652 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5653 case WPA_ALG_BIP_CMAC_256:
5654 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5655 case WPA_ALG_SMS4:
5656 return WLAN_CIPHER_SUITE_SMS4;
5657 case WPA_ALG_KRK:
5658 return WLAN_CIPHER_SUITE_KRK;
5659 case WPA_ALG_NONE:
5660 case WPA_ALG_PMK:
5661 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5662 alg);
5663 return 0;
5664 }
5665
5666 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5667 alg);
5668 return 0;
5669}
5670
5671
5672static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5673{
5674 switch (cipher) {
5675 case WPA_CIPHER_CCMP_256:
5676 return WLAN_CIPHER_SUITE_CCMP_256;
5677 case WPA_CIPHER_GCMP_256:
5678 return WLAN_CIPHER_SUITE_GCMP_256;
5679 case WPA_CIPHER_CCMP:
5680 return WLAN_CIPHER_SUITE_CCMP;
5681 case WPA_CIPHER_GCMP:
5682 return WLAN_CIPHER_SUITE_GCMP;
5683 case WPA_CIPHER_TKIP:
5684 return WLAN_CIPHER_SUITE_TKIP;
5685 case WPA_CIPHER_WEP104:
5686 return WLAN_CIPHER_SUITE_WEP104;
5687 case WPA_CIPHER_WEP40:
5688 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005689 case WPA_CIPHER_GTK_NOT_USED:
5690 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005691 }
5692
5693 return 0;
5694}
5695
5696
5697static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5698 int max_suites)
5699{
5700 int num_suites = 0;
5701
5702 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5703 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5704 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5705 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5706 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5707 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5708 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5709 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5710 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5711 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5712 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5713 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5714 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5715 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5716
5717 return num_suites;
5718}
5719
5720
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005721static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722 enum wpa_alg alg, const u8 *addr,
5723 int key_idx, int set_tx,
5724 const u8 *seq, size_t seq_len,
5725 const u8 *key, size_t key_len)
5726{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005728 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005729 struct nl_msg *msg;
5730 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005731 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005733 /* Ignore for P2P Device */
5734 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5735 return 0;
5736
5737 ifindex = if_nametoindex(ifname);
5738 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005740 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005741 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005742#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005743 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005744 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005745 tdls = 1;
5746 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748
5749 msg = nlmsg_alloc();
5750 if (!msg)
5751 return -ENOMEM;
5752
5753 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005755 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005756 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005758 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005759 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5760 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 }
5762
Dmitry Shmidt98660862014-03-11 17:26:21 -07005763 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005765 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005767
5768 if (addr && !is_broadcast_ether_addr(addr)) {
5769 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5770 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5771
5772 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5773 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5774 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5775 NL80211_KEYTYPE_GROUP);
5776 }
5777 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005778 struct nlattr *types;
5779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005780 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005781
5782 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783 if (!types)
5784 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005785 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5786 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787 }
5788 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5789 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5790
5791 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5792 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5793 ret = 0;
5794 if (ret)
5795 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5796 ret, strerror(-ret));
5797
5798 /*
5799 * If we failed or don't need to set the default TX key (below),
5800 * we're done here.
5801 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005802 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005804 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805 !is_broadcast_ether_addr(addr))
5806 return ret;
5807
5808 msg = nlmsg_alloc();
5809 if (!msg)
5810 return -ENOMEM;
5811
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005812 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5814 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5815 if (alg == WPA_ALG_IGTK)
5816 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5817 else
5818 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5819 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005820 struct nlattr *types;
5821
5822 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005823 if (!types)
5824 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005825 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5826 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005828 struct nlattr *types;
5829
5830 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005831 if (!types)
5832 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005833 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5834 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835 }
5836
5837 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5838 if (ret == -ENOENT)
5839 ret = 0;
5840 if (ret)
5841 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5842 "err=%d %s)", ret, strerror(-ret));
5843 return ret;
5844
5845nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005846 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847 return -ENOBUFS;
5848}
5849
5850
5851static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5852 int key_idx, int defkey,
5853 const u8 *seq, size_t seq_len,
5854 const u8 *key, size_t key_len)
5855{
5856 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5857 if (!key_attr)
5858 return -1;
5859
5860 if (defkey && alg == WPA_ALG_IGTK)
5861 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5862 else if (defkey)
5863 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5864
5865 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5866
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005867 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5868 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869
5870 if (seq && seq_len)
5871 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5872
5873 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5874
5875 nla_nest_end(msg, key_attr);
5876
5877 return 0;
5878 nla_put_failure:
5879 return -1;
5880}
5881
5882
5883static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5884 struct nl_msg *msg)
5885{
5886 int i, privacy = 0;
5887 struct nlattr *nl_keys, *nl_key;
5888
5889 for (i = 0; i < 4; i++) {
5890 if (!params->wep_key[i])
5891 continue;
5892 privacy = 1;
5893 break;
5894 }
5895 if (params->wps == WPS_MODE_PRIVACY)
5896 privacy = 1;
5897 if (params->pairwise_suite &&
5898 params->pairwise_suite != WPA_CIPHER_NONE)
5899 privacy = 1;
5900
5901 if (!privacy)
5902 return 0;
5903
5904 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5905
5906 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5907 if (!nl_keys)
5908 goto nla_put_failure;
5909
5910 for (i = 0; i < 4; i++) {
5911 if (!params->wep_key[i])
5912 continue;
5913
5914 nl_key = nla_nest_start(msg, i);
5915 if (!nl_key)
5916 goto nla_put_failure;
5917
5918 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5919 params->wep_key[i]);
5920 if (params->wep_key_len[i] == 5)
5921 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5922 WLAN_CIPHER_SUITE_WEP40);
5923 else
5924 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5925 WLAN_CIPHER_SUITE_WEP104);
5926
5927 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5928
5929 if (i == params->wep_tx_keyidx)
5930 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5931
5932 nla_nest_end(msg, nl_key);
5933 }
5934 nla_nest_end(msg, nl_keys);
5935
5936 return 0;
5937
5938nla_put_failure:
5939 return -ENOBUFS;
5940}
5941
5942
5943static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5944 const u8 *addr, int cmd, u16 reason_code,
5945 int local_state_change)
5946{
5947 int ret = -1;
5948 struct nl_msg *msg;
5949
5950 msg = nlmsg_alloc();
5951 if (!msg)
5952 return -1;
5953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005954 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005955
5956 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5957 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005958 if (addr)
5959 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005960 if (local_state_change)
5961 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5962
5963 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5964 msg = NULL;
5965 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005966 wpa_dbg(drv->ctx, MSG_DEBUG,
5967 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5968 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969 goto nla_put_failure;
5970 }
5971 ret = 0;
5972
5973nla_put_failure:
5974 nlmsg_free(msg);
5975 return ret;
5976}
5977
5978
5979static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005980 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005982 int ret;
5983
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005984 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005985 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005986 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005987 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5988 reason_code, 0);
5989 /*
5990 * For locally generated disconnect, supplicant already generates a
5991 * DEAUTH event, so ignore the event from NL80211.
5992 */
5993 drv->ignore_next_local_disconnect = ret == 0;
5994
5995 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996}
5997
5998
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005999static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
6000 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006001{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006002 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006003 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07006004
6005 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
6006 nl80211_mark_disconnected(drv);
6007 return nl80211_leave_ibss(drv);
6008 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006010 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
6012 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006013 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006014 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
6015 reason_code, 0);
6016 /*
6017 * For locally generated deauthenticate, supplicant already generates a
6018 * DEAUTH event, so ignore the event from NL80211.
6019 */
6020 drv->ignore_next_local_deauth = ret == 0;
6021 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022}
6023
6024
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006025static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6026 struct wpa_driver_auth_params *params)
6027{
6028 int i;
6029
6030 drv->auth_freq = params->freq;
6031 drv->auth_alg = params->auth_alg;
6032 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6033 drv->auth_local_state_change = params->local_state_change;
6034 drv->auth_p2p = params->p2p;
6035
6036 if (params->bssid)
6037 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6038 else
6039 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6040
6041 if (params->ssid) {
6042 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6043 drv->auth_ssid_len = params->ssid_len;
6044 } else
6045 drv->auth_ssid_len = 0;
6046
6047
6048 os_free(drv->auth_ie);
6049 drv->auth_ie = NULL;
6050 drv->auth_ie_len = 0;
6051 if (params->ie) {
6052 drv->auth_ie = os_malloc(params->ie_len);
6053 if (drv->auth_ie) {
6054 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6055 drv->auth_ie_len = params->ie_len;
6056 }
6057 }
6058
6059 for (i = 0; i < 4; i++) {
6060 if (params->wep_key[i] && params->wep_key_len[i] &&
6061 params->wep_key_len[i] <= 16) {
6062 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6063 params->wep_key_len[i]);
6064 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6065 } else
6066 drv->auth_wep_key_len[i] = 0;
6067 }
6068}
6069
6070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006071static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006072 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006073{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074 struct wpa_driver_nl80211_data *drv = bss->drv;
6075 int ret = -1, i;
6076 struct nl_msg *msg;
6077 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006078 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006080 int is_retry;
6081
6082 is_retry = drv->retry_auth;
6083 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006084 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006085
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006086 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006087 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006088 if (params->bssid)
6089 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6090 else
6091 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006092 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006093 nlmode = params->p2p ?
6094 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6095 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006096 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 return -1;
6098
6099retry:
6100 msg = nlmsg_alloc();
6101 if (!msg)
6102 return -1;
6103
6104 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6105 drv->ifindex);
6106
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006107 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006108
6109 for (i = 0; i < 4; i++) {
6110 if (!params->wep_key[i])
6111 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006112 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113 NULL, i,
6114 i == params->wep_tx_keyidx, NULL, 0,
6115 params->wep_key[i],
6116 params->wep_key_len[i]);
6117 if (params->wep_tx_keyidx != i)
6118 continue;
6119 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6120 params->wep_key[i], params->wep_key_len[i])) {
6121 nlmsg_free(msg);
6122 return -1;
6123 }
6124 }
6125
6126 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6127 if (params->bssid) {
6128 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6129 MAC2STR(params->bssid));
6130 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6131 }
6132 if (params->freq) {
6133 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6134 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6135 }
6136 if (params->ssid) {
6137 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6138 params->ssid, params->ssid_len);
6139 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6140 params->ssid);
6141 }
6142 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6143 if (params->ie)
6144 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006145 if (params->sae_data) {
6146 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6147 params->sae_data_len);
6148 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6149 params->sae_data);
6150 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6152 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6153 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6154 type = NL80211_AUTHTYPE_SHARED_KEY;
6155 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6156 type = NL80211_AUTHTYPE_NETWORK_EAP;
6157 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6158 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006159 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6160 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161 else
6162 goto nla_put_failure;
6163 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6164 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6165 if (params->local_state_change) {
6166 wpa_printf(MSG_DEBUG, " * Local state change only");
6167 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6168 }
6169
6170 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6171 msg = NULL;
6172 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006173 wpa_dbg(drv->ctx, MSG_DEBUG,
6174 "nl80211: MLME command failed (auth): ret=%d (%s)",
6175 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176 count++;
6177 if (ret == -EALREADY && count == 1 && params->bssid &&
6178 !params->local_state_change) {
6179 /*
6180 * mac80211 does not currently accept new
6181 * authentication if we are already authenticated. As a
6182 * workaround, force deauthentication and try again.
6183 */
6184 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6185 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006186 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187 wpa_driver_nl80211_deauthenticate(
6188 bss, params->bssid,
6189 WLAN_REASON_PREV_AUTH_NOT_VALID);
6190 nlmsg_free(msg);
6191 goto retry;
6192 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006193
6194 if (ret == -ENOENT && params->freq && !is_retry) {
6195 /*
6196 * cfg80211 has likely expired the BSS entry even
6197 * though it was previously available in our internal
6198 * BSS table. To recover quickly, start a single
6199 * channel scan on the specified channel.
6200 */
6201 struct wpa_driver_scan_params scan;
6202 int freqs[2];
6203
6204 os_memset(&scan, 0, sizeof(scan));
6205 scan.num_ssids = 1;
6206 if (params->ssid) {
6207 scan.ssids[0].ssid = params->ssid;
6208 scan.ssids[0].ssid_len = params->ssid_len;
6209 }
6210 freqs[0] = params->freq;
6211 freqs[1] = 0;
6212 scan.freqs = freqs;
6213 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6214 "channel scan to refresh cfg80211 BSS "
6215 "entry");
6216 ret = wpa_driver_nl80211_scan(bss, &scan);
6217 if (ret == 0) {
6218 nl80211_copy_auth_params(drv, params);
6219 drv->scan_for_auth = 1;
6220 }
6221 } else if (is_retry) {
6222 /*
6223 * Need to indicate this with an event since the return
6224 * value from the retry is not delivered to core code.
6225 */
6226 union wpa_event_data event;
6227 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6228 "failed");
6229 os_memset(&event, 0, sizeof(event));
6230 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6231 ETH_ALEN);
6232 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6233 &event);
6234 }
6235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 goto nla_put_failure;
6237 }
6238 ret = 0;
6239 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6240 "successfully");
6241
6242nla_put_failure:
6243 nlmsg_free(msg);
6244 return ret;
6245}
6246
6247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006248static int wpa_driver_nl80211_authenticate_retry(
6249 struct wpa_driver_nl80211_data *drv)
6250{
6251 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006252 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006253 int i;
6254
6255 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6256
6257 os_memset(&params, 0, sizeof(params));
6258 params.freq = drv->auth_freq;
6259 params.auth_alg = drv->auth_alg;
6260 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6261 params.local_state_change = drv->auth_local_state_change;
6262 params.p2p = drv->auth_p2p;
6263
6264 if (!is_zero_ether_addr(drv->auth_bssid_))
6265 params.bssid = drv->auth_bssid_;
6266
6267 if (drv->auth_ssid_len) {
6268 params.ssid = drv->auth_ssid;
6269 params.ssid_len = drv->auth_ssid_len;
6270 }
6271
6272 params.ie = drv->auth_ie;
6273 params.ie_len = drv->auth_ie_len;
6274
6275 for (i = 0; i < 4; i++) {
6276 if (drv->auth_wep_key_len[i]) {
6277 params.wep_key[i] = drv->auth_wep_key[i];
6278 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6279 }
6280 }
6281
6282 drv->retry_auth = 1;
6283 return wpa_driver_nl80211_authenticate(bss, &params);
6284}
6285
6286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006287struct phy_info_arg {
6288 u16 *num_modes;
6289 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006290 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291};
6292
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006293static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6294 struct nlattr *ampdu_factor,
6295 struct nlattr *ampdu_density,
6296 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006297{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006298 if (capa)
6299 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006301 if (ampdu_factor)
6302 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006304 if (ampdu_density)
6305 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6306
6307 if (mcs_set && nla_len(mcs_set) >= 16) {
6308 u8 *mcs;
6309 mcs = nla_data(mcs_set);
6310 os_memcpy(mode->mcs_set, mcs, 16);
6311 }
6312}
6313
6314
6315static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6316 struct nlattr *capa,
6317 struct nlattr *mcs_set)
6318{
6319 if (capa)
6320 mode->vht_capab = nla_get_u32(capa);
6321
6322 if (mcs_set && nla_len(mcs_set) >= 8) {
6323 u8 *mcs;
6324 mcs = nla_data(mcs_set);
6325 os_memcpy(mode->vht_mcs_set, mcs, 8);
6326 }
6327}
6328
6329
6330static void phy_info_freq(struct hostapd_hw_modes *mode,
6331 struct hostapd_channel_data *chan,
6332 struct nlattr *tb_freq[])
6333{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006334 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006335 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6336 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006337 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006338 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6339 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006340
6341 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6342 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006343 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6344 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006345 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6346 chan->flag |= HOSTAPD_CHAN_RADAR;
6347
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006348 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6349 enum nl80211_dfs_state state =
6350 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6351
6352 switch (state) {
6353 case NL80211_DFS_USABLE:
6354 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6355 break;
6356 case NL80211_DFS_AVAILABLE:
6357 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6358 break;
6359 case NL80211_DFS_UNAVAILABLE:
6360 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6361 break;
6362 }
6363 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006364
6365 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6366 chan->dfs_cac_ms = nla_get_u32(
6367 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6368 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006369}
6370
6371
6372static int phy_info_freqs(struct phy_info_arg *phy_info,
6373 struct hostapd_hw_modes *mode, struct nlattr *tb)
6374{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006375 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6376 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6377 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006378 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6380 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006381 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006382 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006383 int new_channels = 0;
6384 struct hostapd_channel_data *channel;
6385 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006386 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006387 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006389 if (tb == NULL)
6390 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006391
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006392 nla_for_each_nested(nl_freq, tb, rem_freq) {
6393 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6394 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6395 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6396 continue;
6397 new_channels++;
6398 }
6399
6400 channel = os_realloc_array(mode->channels,
6401 mode->num_channels + new_channels,
6402 sizeof(struct hostapd_channel_data));
6403 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404 return NL_SKIP;
6405
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006406 mode->channels = channel;
6407 mode->num_channels += new_channels;
6408
6409 idx = phy_info->last_chan_idx;
6410
6411 nla_for_each_nested(nl_freq, tb, rem_freq) {
6412 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6413 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6414 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6415 continue;
6416 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6417 idx++;
6418 }
6419 phy_info->last_chan_idx = idx;
6420
6421 return NL_OK;
6422}
6423
6424
6425static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6426{
6427 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6428 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6429 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6430 { .type = NLA_FLAG },
6431 };
6432 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6433 struct nlattr *nl_rate;
6434 int rem_rate, idx;
6435
6436 if (tb == NULL)
6437 return NL_OK;
6438
6439 nla_for_each_nested(nl_rate, tb, rem_rate) {
6440 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6441 nla_data(nl_rate), nla_len(nl_rate),
6442 rate_policy);
6443 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6444 continue;
6445 mode->num_rates++;
6446 }
6447
6448 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6449 if (!mode->rates)
6450 return NL_SKIP;
6451
6452 idx = 0;
6453
6454 nla_for_each_nested(nl_rate, tb, rem_rate) {
6455 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6456 nla_data(nl_rate), nla_len(nl_rate),
6457 rate_policy);
6458 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6459 continue;
6460 mode->rates[idx] = nla_get_u32(
6461 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006462 idx++;
6463 }
6464
6465 return NL_OK;
6466}
6467
6468
6469static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6470{
6471 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6472 struct hostapd_hw_modes *mode;
6473 int ret;
6474
6475 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006476 mode = os_realloc_array(phy_info->modes,
6477 *phy_info->num_modes + 1,
6478 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006479 if (!mode)
6480 return NL_SKIP;
6481 phy_info->modes = mode;
6482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006484 os_memset(mode, 0, sizeof(*mode));
6485 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006486 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6487 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6488
6489 /*
6490 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6491 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6492 * possible streams as unsupported. This will be overridden if
6493 * driver advertises VHT support.
6494 */
6495 mode->vht_mcs_set[0] = 0xff;
6496 mode->vht_mcs_set[1] = 0xff;
6497 mode->vht_mcs_set[4] = 0xff;
6498 mode->vht_mcs_set[5] = 0xff;
6499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006501 phy_info->last_mode = nl_band->nla_type;
6502 phy_info->last_chan_idx = 0;
6503 } else
6504 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006506 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6507 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006508
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006509 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6510 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6511 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6512 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6513 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6514 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6515 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6516 if (ret != NL_OK)
6517 return ret;
6518 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6519 if (ret != NL_OK)
6520 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006522 return NL_OK;
6523}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006526static int phy_info_handler(struct nl_msg *msg, void *arg)
6527{
6528 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6529 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6530 struct phy_info_arg *phy_info = arg;
6531 struct nlattr *nl_band;
6532 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006533
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006534 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6535 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006536
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006537 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6538 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006539
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006540 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6541 {
6542 int res = phy_info_band(phy_info, nl_band);
6543 if (res != NL_OK)
6544 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006545 }
6546
6547 return NL_SKIP;
6548}
6549
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006551static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006552wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6553 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554{
6555 u16 m;
6556 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6557 int i, mode11g_idx = -1;
6558
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006559 /* heuristic to set up modes */
6560 for (m = 0; m < *num_modes; m++) {
6561 if (!modes[m].num_channels)
6562 continue;
6563 if (modes[m].channels[0].freq < 4000) {
6564 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6565 for (i = 0; i < modes[m].num_rates; i++) {
6566 if (modes[m].rates[i] > 200) {
6567 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6568 break;
6569 }
6570 }
6571 } else if (modes[m].channels[0].freq > 50000)
6572 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6573 else
6574 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6575 }
6576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006577 /* If only 802.11g mode is included, use it to construct matching
6578 * 802.11b mode data. */
6579
6580 for (m = 0; m < *num_modes; m++) {
6581 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6582 return modes; /* 802.11b already included */
6583 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6584 mode11g_idx = m;
6585 }
6586
6587 if (mode11g_idx < 0)
6588 return modes; /* 2.4 GHz band not supported at all */
6589
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006590 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006591 if (nmodes == NULL)
6592 return modes; /* Could not add 802.11b mode */
6593
6594 mode = &nmodes[*num_modes];
6595 os_memset(mode, 0, sizeof(*mode));
6596 (*num_modes)++;
6597 modes = nmodes;
6598
6599 mode->mode = HOSTAPD_MODE_IEEE80211B;
6600
6601 mode11g = &modes[mode11g_idx];
6602 mode->num_channels = mode11g->num_channels;
6603 mode->channels = os_malloc(mode11g->num_channels *
6604 sizeof(struct hostapd_channel_data));
6605 if (mode->channels == NULL) {
6606 (*num_modes)--;
6607 return modes; /* Could not add 802.11b mode */
6608 }
6609 os_memcpy(mode->channels, mode11g->channels,
6610 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6611
6612 mode->num_rates = 0;
6613 mode->rates = os_malloc(4 * sizeof(int));
6614 if (mode->rates == NULL) {
6615 os_free(mode->channels);
6616 (*num_modes)--;
6617 return modes; /* Could not add 802.11b mode */
6618 }
6619
6620 for (i = 0; i < mode11g->num_rates; i++) {
6621 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6622 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6623 continue;
6624 mode->rates[mode->num_rates] = mode11g->rates[i];
6625 mode->num_rates++;
6626 if (mode->num_rates == 4)
6627 break;
6628 }
6629
6630 if (mode->num_rates == 0) {
6631 os_free(mode->channels);
6632 os_free(mode->rates);
6633 (*num_modes)--;
6634 return modes; /* No 802.11b rates */
6635 }
6636
6637 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6638 "information");
6639
6640 return modes;
6641}
6642
6643
6644static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6645 int end)
6646{
6647 int c;
6648
6649 for (c = 0; c < mode->num_channels; c++) {
6650 struct hostapd_channel_data *chan = &mode->channels[c];
6651 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6652 chan->flag |= HOSTAPD_CHAN_HT40;
6653 }
6654}
6655
6656
6657static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6658 int end)
6659{
6660 int c;
6661
6662 for (c = 0; c < mode->num_channels; c++) {
6663 struct hostapd_channel_data *chan = &mode->channels[c];
6664 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6665 continue;
6666 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6667 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6668 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6669 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6670 }
6671}
6672
6673
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006674static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006675 struct phy_info_arg *results)
6676{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006677 u16 m;
6678
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006679 for (m = 0; m < *results->num_modes; m++) {
6680 int c;
6681 struct hostapd_hw_modes *mode = &results->modes[m];
6682
6683 for (c = 0; c < mode->num_channels; c++) {
6684 struct hostapd_channel_data *chan = &mode->channels[c];
6685 if ((u32) chan->freq - 10 >= start &&
6686 (u32) chan->freq + 10 <= end)
6687 chan->max_tx_power = max_eirp;
6688 }
6689 }
6690}
6691
6692
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006693static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 struct phy_info_arg *results)
6695{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006696 u16 m;
6697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006698 for (m = 0; m < *results->num_modes; m++) {
6699 if (!(results->modes[m].ht_capab &
6700 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6701 continue;
6702 nl80211_set_ht40_mode(&results->modes[m], start, end);
6703 }
6704}
6705
6706
6707static void nl80211_reg_rule_sec(struct nlattr *tb[],
6708 struct phy_info_arg *results)
6709{
6710 u32 start, end, max_bw;
6711 u16 m;
6712
6713 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6714 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6715 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6716 return;
6717
6718 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6719 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6720 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6721
6722 if (max_bw < 20)
6723 return;
6724
6725 for (m = 0; m < *results->num_modes; m++) {
6726 if (!(results->modes[m].ht_capab &
6727 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6728 continue;
6729 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6730 }
6731}
6732
6733
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006734static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6735 int end)
6736{
6737 int c;
6738
6739 for (c = 0; c < mode->num_channels; c++) {
6740 struct hostapd_channel_data *chan = &mode->channels[c];
6741 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6742 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6743
6744 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6745 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6746
6747 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6748 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6749
6750 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6751 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6752 }
6753}
6754
6755
6756static void nl80211_reg_rule_vht(struct nlattr *tb[],
6757 struct phy_info_arg *results)
6758{
6759 u32 start, end, max_bw;
6760 u16 m;
6761
6762 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6763 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6764 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6765 return;
6766
6767 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6768 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6769 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6770
6771 if (max_bw < 80)
6772 return;
6773
6774 for (m = 0; m < *results->num_modes; m++) {
6775 if (!(results->modes[m].ht_capab &
6776 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6777 continue;
6778 /* TODO: use a real VHT support indication */
6779 if (!results->modes[m].vht_capab)
6780 continue;
6781
6782 nl80211_set_vht_mode(&results->modes[m], start, end);
6783 }
6784}
6785
6786
Dmitry Shmidt97672262014-02-03 13:02:54 -08006787static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6788{
6789 switch (region) {
6790 case NL80211_DFS_UNSET:
6791 return "DFS-UNSET";
6792 case NL80211_DFS_FCC:
6793 return "DFS-FCC";
6794 case NL80211_DFS_ETSI:
6795 return "DFS-ETSI";
6796 case NL80211_DFS_JP:
6797 return "DFS-JP";
6798 default:
6799 return "DFS-invalid";
6800 }
6801}
6802
6803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006804static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6805{
6806 struct phy_info_arg *results = arg;
6807 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6808 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6809 struct nlattr *nl_rule;
6810 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6811 int rem_rule;
6812 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6813 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6814 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6815 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6816 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6817 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6818 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6819 };
6820
6821 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6822 genlmsg_attrlen(gnlh, 0), NULL);
6823 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6824 !tb_msg[NL80211_ATTR_REG_RULES]) {
6825 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6826 "available");
6827 return NL_SKIP;
6828 }
6829
Dmitry Shmidt97672262014-02-03 13:02:54 -08006830 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6831 enum nl80211_dfs_regions dfs_domain;
6832 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6833 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6834 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6835 dfs_domain_name(dfs_domain));
6836 } else {
6837 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6838 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840
6841 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6842 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006843 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6845 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006846 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6847 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6848 continue;
6849 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6850 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6851 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6852 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6853 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6854 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006855 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6856 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006857
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006858 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6859 start, end, max_bw, max_eirp,
6860 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6861 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6862 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6863 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6864 "",
6865 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6866 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6867 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6868 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006869 if (max_bw >= 40)
6870 nl80211_reg_rule_ht40(start, end, results);
6871 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6872 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6873 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006874 }
6875
6876 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6877 {
6878 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6879 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6880 nl80211_reg_rule_sec(tb_rule, results);
6881 }
6882
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006883 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6884 {
6885 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6886 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6887 nl80211_reg_rule_vht(tb_rule, results);
6888 }
6889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006890 return NL_SKIP;
6891}
6892
6893
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006894static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6895 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006896{
6897 struct nl_msg *msg;
6898
6899 msg = nlmsg_alloc();
6900 if (!msg)
6901 return -ENOMEM;
6902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006903 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006904 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6905}
6906
6907
6908static struct hostapd_hw_modes *
6909wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6910{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006911 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006912 struct i802_bss *bss = priv;
6913 struct wpa_driver_nl80211_data *drv = bss->drv;
6914 struct nl_msg *msg;
6915 struct phy_info_arg result = {
6916 .num_modes = num_modes,
6917 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006918 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919 };
6920
6921 *num_modes = 0;
6922 *flags = 0;
6923
6924 msg = nlmsg_alloc();
6925 if (!msg)
6926 return NULL;
6927
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006928 feat = get_nl80211_protocol_features(drv);
6929 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6930 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6931 else
6932 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006934 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006935 if (nl80211_set_iface_id(msg, bss) < 0)
6936 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937
6938 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006939 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006940 return wpa_driver_nl80211_postprocess_modes(result.modes,
6941 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006942 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006943 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006944 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006945 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946 return NULL;
6947}
6948
6949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006950static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6951 const void *data, size_t len,
6952 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006953{
6954 __u8 rtap_hdr[] = {
6955 0x00, 0x00, /* radiotap version */
6956 0x0e, 0x00, /* radiotap length */
6957 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6958 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6959 0x00, /* padding */
6960 0x00, 0x00, /* RX and TX flags to indicate that */
6961 0x00, 0x00, /* this is the injected frame directly */
6962 };
6963 struct iovec iov[2] = {
6964 {
6965 .iov_base = &rtap_hdr,
6966 .iov_len = sizeof(rtap_hdr),
6967 },
6968 {
6969 .iov_base = (void *) data,
6970 .iov_len = len,
6971 }
6972 };
6973 struct msghdr msg = {
6974 .msg_name = NULL,
6975 .msg_namelen = 0,
6976 .msg_iov = iov,
6977 .msg_iovlen = 2,
6978 .msg_control = NULL,
6979 .msg_controllen = 0,
6980 .msg_flags = 0,
6981 };
6982 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006983 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006984
6985 if (encrypt)
6986 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6987
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006988 if (drv->monitor_sock < 0) {
6989 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6990 "for %s", __func__);
6991 return -1;
6992 }
6993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006994 if (noack)
6995 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006996 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006998 res = sendmsg(drv->monitor_sock, &msg, 0);
6999 if (res < 0) {
7000 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
7001 return -1;
7002 }
7003 return 0;
7004}
7005
7006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007007static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
7008 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007009 int encrypt, int noack,
7010 unsigned int freq, int no_cck,
7011 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007012{
7013 struct wpa_driver_nl80211_data *drv = bss->drv;
7014 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007015 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007016
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007017 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
7018 freq = nl80211_get_assoc_freq(drv);
7019 wpa_printf(MSG_DEBUG,
7020 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
7021 freq);
7022 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007023 if (freq == 0) {
7024 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
7025 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007026 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007027 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007028
Dmitry Shmidt56052862013-10-04 10:23:25 -07007029 if (drv->use_monitor) {
7030 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7031 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007032 return wpa_driver_nl80211_send_mntr(drv, data, len,
7033 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007034 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007035
Dmitry Shmidt56052862013-10-04 10:23:25 -07007036 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07007037 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7038 &cookie, no_cck, noack, offchanok);
7039 if (res == 0 && !noack) {
7040 const struct ieee80211_mgmt *mgmt;
7041 u16 fc;
7042
7043 mgmt = (const struct ieee80211_mgmt *) data;
7044 fc = le_to_host16(mgmt->frame_control);
7045 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7046 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7047 wpa_printf(MSG_MSGDUMP,
7048 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7049 (long long unsigned int)
7050 drv->send_action_cookie,
7051 (long long unsigned int) cookie);
7052 drv->send_action_cookie = cookie;
7053 }
7054 }
7055
7056 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007057}
7058
7059
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007060static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7061 size_t data_len, int noack,
7062 unsigned int freq, int no_cck,
7063 int offchanok,
7064 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007066 struct wpa_driver_nl80211_data *drv = bss->drv;
7067 struct ieee80211_mgmt *mgmt;
7068 int encrypt = 1;
7069 u16 fc;
7070
7071 mgmt = (struct ieee80211_mgmt *) data;
7072 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007073 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
7074 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007075
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007076 if ((is_sta_interface(drv->nlmode) ||
7077 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007078 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7079 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7080 /*
7081 * The use of last_mgmt_freq is a bit of a hack,
7082 * but it works due to the single-threaded nature
7083 * of wpa_supplicant.
7084 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007085 if (freq == 0) {
7086 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7087 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007088 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007089 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007090 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007091 data, data_len, NULL, 1, noack,
7092 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007093 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007094
7095 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007096 if (freq == 0) {
7097 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7098 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007099 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007101 return nl80211_send_frame_cmd(bss, freq,
7102 (int) freq == bss->freq ? 0 :
7103 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007104 data, data_len,
7105 &drv->send_action_cookie,
7106 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007107 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7110 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7111 /*
7112 * Only one of the authentication frame types is encrypted.
7113 * In order for static WEP encryption to work properly (i.e.,
7114 * to not encrypt the frame), we need to tell mac80211 about
7115 * the frames that must not be encrypted.
7116 */
7117 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7118 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7119 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7120 encrypt = 0;
7121 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007122
Dmitry Shmidt56052862013-10-04 10:23:25 -07007123 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007124 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007125 noack, freq, no_cck, offchanok,
7126 wait_time);
7127}
7128
7129
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007130static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7131 int slot, int ht_opmode, int ap_isolate,
7132 int *basic_rates)
7133{
7134 struct wpa_driver_nl80211_data *drv = bss->drv;
7135 struct nl_msg *msg;
7136
7137 msg = nlmsg_alloc();
7138 if (!msg)
7139 return -ENOMEM;
7140
7141 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7142
7143 if (cts >= 0)
7144 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7145 if (preamble >= 0)
7146 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7147 if (slot >= 0)
7148 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7149 if (ht_opmode >= 0)
7150 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7151 if (ap_isolate >= 0)
7152 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7153
7154 if (basic_rates) {
7155 u8 rates[NL80211_MAX_SUPP_RATES];
7156 u8 rates_len = 0;
7157 int i;
7158
7159 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7160 i++)
7161 rates[rates_len++] = basic_rates[i] / 5;
7162
7163 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7164 }
7165
7166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7167
7168 return send_and_recv_msgs(drv, msg, NULL, NULL);
7169 nla_put_failure:
7170 nlmsg_free(msg);
7171 return -ENOBUFS;
7172}
7173
7174
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007175static int wpa_driver_nl80211_set_acl(void *priv,
7176 struct hostapd_acl_params *params)
7177{
7178 struct i802_bss *bss = priv;
7179 struct wpa_driver_nl80211_data *drv = bss->drv;
7180 struct nl_msg *msg;
7181 struct nlattr *acl;
7182 unsigned int i;
7183 int ret = 0;
7184
7185 if (!(drv->capa.max_acl_mac_addrs))
7186 return -ENOTSUP;
7187
7188 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7189 return -ENOTSUP;
7190
7191 msg = nlmsg_alloc();
7192 if (!msg)
7193 return -ENOMEM;
7194
7195 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7196 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7197
7198 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7199
7200 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7201
7202 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7203 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7204 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7205
7206 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7207 if (acl == NULL)
7208 goto nla_put_failure;
7209
7210 for (i = 0; i < params->num_mac_acl; i++)
7211 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7212
7213 nla_nest_end(msg, acl);
7214
7215 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7216 msg = NULL;
7217 if (ret) {
7218 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7219 ret, strerror(-ret));
7220 }
7221
7222nla_put_failure:
7223 nlmsg_free(msg);
7224
7225 return ret;
7226}
7227
7228
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007229static int wpa_driver_nl80211_set_ap(void *priv,
7230 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231{
7232 struct i802_bss *bss = priv;
7233 struct wpa_driver_nl80211_data *drv = bss->drv;
7234 struct nl_msg *msg;
7235 u8 cmd = NL80211_CMD_NEW_BEACON;
7236 int ret;
7237 int beacon_set;
7238 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007239 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007240 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007241 u32 ver;
7242
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007243 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007244
7245 msg = nlmsg_alloc();
7246 if (!msg)
7247 return -ENOMEM;
7248
7249 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7250 beacon_set);
7251 if (beacon_set)
7252 cmd = NL80211_CMD_SET_BEACON;
7253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007254 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007255 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7256 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007257 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007258 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7259 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007260 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007261 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007263 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007264 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007265 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007266 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007267 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7268 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007269 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7270 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007271 if (params->proberesp && params->proberesp_len) {
7272 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7273 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007274 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7275 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007276 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007277 switch (params->hide_ssid) {
7278 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007279 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007280 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7281 NL80211_HIDDEN_SSID_NOT_IN_USE);
7282 break;
7283 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007284 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007285 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7286 NL80211_HIDDEN_SSID_ZERO_LEN);
7287 break;
7288 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007289 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007290 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7291 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7292 break;
7293 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007294 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007295 if (params->privacy)
7296 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007297 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007298 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7299 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7300 /* Leave out the attribute */
7301 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7302 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7303 NL80211_AUTHTYPE_SHARED_KEY);
7304 else
7305 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7306 NL80211_AUTHTYPE_OPEN_SYSTEM);
7307
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007308 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007309 ver = 0;
7310 if (params->wpa_version & WPA_PROTO_WPA)
7311 ver |= NL80211_WPA_VERSION_1;
7312 if (params->wpa_version & WPA_PROTO_RSN)
7313 ver |= NL80211_WPA_VERSION_2;
7314 if (ver)
7315 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7316
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007317 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7318 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007319 num_suites = 0;
7320 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7321 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7322 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7323 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7324 if (num_suites) {
7325 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7326 num_suites * sizeof(u32), suites);
7327 }
7328
7329 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7330 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7331 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7332
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007333 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7334 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007335 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7336 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007337 if (num_suites) {
7338 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7339 num_suites * sizeof(u32), suites);
7340 }
7341
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007342 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7343 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007344 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7345 if (suite)
7346 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007347
7348 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007349 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7350 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007351 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7352 wpabuf_head(params->beacon_ies));
7353 }
7354 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007355 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7356 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007357 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7358 wpabuf_len(params->proberesp_ies),
7359 wpabuf_head(params->proberesp_ies));
7360 }
7361 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007362 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7363 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007364 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7365 wpabuf_len(params->assocresp_ies),
7366 wpabuf_head(params->assocresp_ies));
7367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007368
Dmitry Shmidt04949592012-07-19 12:16:46 -07007369 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007370 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7371 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007372 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7373 params->ap_max_inactivity);
7374 }
7375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007376 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7377 if (ret) {
7378 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7379 ret, strerror(-ret));
7380 } else {
7381 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007382 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7383 params->short_slot_time, params->ht_opmode,
7384 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007385 if (beacon_set && params->freq &&
7386 params->freq->bandwidth != bss->bandwidth) {
7387 wpa_printf(MSG_DEBUG,
7388 "nl80211: Update BSS %s bandwidth: %d -> %d",
7389 bss->ifname, bss->bandwidth,
7390 params->freq->bandwidth);
7391 ret = nl80211_set_channel(bss, params->freq, 1);
7392 if (ret) {
7393 wpa_printf(MSG_DEBUG,
7394 "nl80211: Frequency set failed: %d (%s)",
7395 ret, strerror(-ret));
7396 } else {
7397 wpa_printf(MSG_DEBUG,
7398 "nl80211: Frequency set succeeded for ht2040 coex");
7399 bss->bandwidth = params->freq->bandwidth;
7400 }
7401 } else if (!beacon_set) {
7402 /*
7403 * cfg80211 updates the driver on frequence change in AP
7404 * mode only at the point when beaconing is started, so
7405 * set the initial value here.
7406 */
7407 bss->bandwidth = params->freq->bandwidth;
7408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007409 }
7410 return ret;
7411 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007412 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007413 return -ENOBUFS;
7414}
7415
7416
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007417static int nl80211_put_freq_params(struct nl_msg *msg,
7418 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007419{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007420 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7421 if (freq->vht_enabled) {
7422 switch (freq->bandwidth) {
7423 case 20:
7424 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7425 NL80211_CHAN_WIDTH_20);
7426 break;
7427 case 40:
7428 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7429 NL80211_CHAN_WIDTH_40);
7430 break;
7431 case 80:
7432 if (freq->center_freq2)
7433 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7434 NL80211_CHAN_WIDTH_80P80);
7435 else
7436 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7437 NL80211_CHAN_WIDTH_80);
7438 break;
7439 case 160:
7440 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7441 NL80211_CHAN_WIDTH_160);
7442 break;
7443 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007444 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007445 }
7446 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7447 if (freq->center_freq2)
7448 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7449 freq->center_freq2);
7450 } else if (freq->ht_enabled) {
7451 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452 case -1:
7453 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7454 NL80211_CHAN_HT40MINUS);
7455 break;
7456 case 1:
7457 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7458 NL80211_CHAN_HT40PLUS);
7459 break;
7460 default:
7461 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7462 NL80211_CHAN_HT20);
7463 break;
7464 }
7465 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007466 return 0;
7467
7468nla_put_failure:
7469 return -ENOBUFS;
7470}
7471
7472
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007473static int nl80211_set_channel(struct i802_bss *bss,
7474 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007475{
7476 struct wpa_driver_nl80211_data *drv = bss->drv;
7477 struct nl_msg *msg;
7478 int ret;
7479
7480 wpa_printf(MSG_DEBUG,
7481 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7482 freq->freq, freq->ht_enabled, freq->vht_enabled,
7483 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7484 msg = nlmsg_alloc();
7485 if (!msg)
7486 return -1;
7487
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007488 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7489 NL80211_CMD_SET_WIPHY);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007490
7491 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7492 if (nl80211_put_freq_params(msg, freq) < 0)
7493 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494
7495 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007496 msg = NULL;
7497 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007498 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007499 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007501 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007502 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007503nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007504 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007505 return -1;
7506}
7507
7508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007509static u32 sta_flags_nl80211(int flags)
7510{
7511 u32 f = 0;
7512
7513 if (flags & WPA_STA_AUTHORIZED)
7514 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7515 if (flags & WPA_STA_WMM)
7516 f |= BIT(NL80211_STA_FLAG_WME);
7517 if (flags & WPA_STA_SHORT_PREAMBLE)
7518 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7519 if (flags & WPA_STA_MFP)
7520 f |= BIT(NL80211_STA_FLAG_MFP);
7521 if (flags & WPA_STA_TDLS_PEER)
7522 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7523
7524 return f;
7525}
7526
7527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007528static int wpa_driver_nl80211_sta_add(void *priv,
7529 struct hostapd_sta_add_params *params)
7530{
7531 struct i802_bss *bss = priv;
7532 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007533 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007534 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007535 int ret = -ENOBUFS;
7536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007537 if ((params->flags & WPA_STA_TDLS_PEER) &&
7538 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7539 return -EOPNOTSUPP;
7540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007541 msg = nlmsg_alloc();
7542 if (!msg)
7543 return -ENOMEM;
7544
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007545 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7546 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007547 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7548 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549
7550 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7551 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7553 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007554 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7555 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007556 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007557 if (params->aid) {
7558 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7559 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7560 } else {
7561 /*
7562 * cfg80211 validates that AID is non-zero, so we have
7563 * to make this a non-zero value for the TDLS case where
7564 * a dummy STA entry is used for now.
7565 */
7566 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7567 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7568 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007569 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7570 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007571 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7572 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007573 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7574 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7575 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007576 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007578 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7579 (u8 *) params->ht_capabilities,
7580 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7582 sizeof(*params->ht_capabilities),
7583 params->ht_capabilities);
7584 }
7585
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007586 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007587 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7588 (u8 *) params->vht_capabilities,
7589 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007590 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7591 sizeof(*params->vht_capabilities),
7592 params->vht_capabilities);
7593 }
7594
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007595 if (params->vht_opmode_enabled) {
7596 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7597 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7598 params->vht_opmode);
7599 }
7600
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007601 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7602 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7603
7604 if (params->ext_capab) {
7605 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7606 params->ext_capab, params->ext_capab_len);
7607 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7608 params->ext_capab_len, params->ext_capab);
7609 }
7610
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007611 if (params->supp_channels) {
7612 wpa_hexdump(MSG_DEBUG, " * supported channels",
7613 params->supp_channels, params->supp_channels_len);
7614 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7615 params->supp_channels_len, params->supp_channels);
7616 }
7617
7618 if (params->supp_oper_classes) {
7619 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7620 params->supp_oper_classes,
7621 params->supp_oper_classes_len);
7622 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7623 params->supp_oper_classes_len,
7624 params->supp_oper_classes);
7625 }
7626
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007627 os_memset(&upd, 0, sizeof(upd));
7628 upd.mask = sta_flags_nl80211(params->flags);
7629 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007630 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7631 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007632 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7633
7634 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007635 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007637 if (!wme)
7638 goto nla_put_failure;
7639
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007640 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007641 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007642 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007643 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007644 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007645 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007646 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007647 }
7648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007649 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007650 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007651 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007652 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7653 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7654 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007655 if (ret == -EEXIST)
7656 ret = 0;
7657 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007658 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007659 return ret;
7660}
7661
7662
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007663static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007664{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007665 struct wpa_driver_nl80211_data *drv = bss->drv;
7666 struct nl_msg *msg;
7667 int ret;
7668
7669 msg = nlmsg_alloc();
7670 if (!msg)
7671 return -ENOMEM;
7672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007673 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007674
7675 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7676 if_nametoindex(bss->ifname));
7677 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7678
7679 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007680 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7681 " --> %d (%s)",
7682 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007683 if (ret == -ENOENT)
7684 return 0;
7685 return ret;
7686 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007687 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688 return -ENOBUFS;
7689}
7690
7691
7692static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7693 int ifidx)
7694{
7695 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007696 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697
7698 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007700 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007701 dl_list_for_each(drv2, &drv->global->interfaces,
7702 struct wpa_driver_nl80211_data, list)
7703 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007704
7705 msg = nlmsg_alloc();
7706 if (!msg)
7707 goto nla_put_failure;
7708
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007709 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7711
7712 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7713 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007714 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007715 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007716 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007717 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7718}
7719
7720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007721static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7722{
7723 switch (mode) {
7724 case NL80211_IFTYPE_ADHOC:
7725 return "ADHOC";
7726 case NL80211_IFTYPE_STATION:
7727 return "STATION";
7728 case NL80211_IFTYPE_AP:
7729 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007730 case NL80211_IFTYPE_AP_VLAN:
7731 return "AP_VLAN";
7732 case NL80211_IFTYPE_WDS:
7733 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007734 case NL80211_IFTYPE_MONITOR:
7735 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007736 case NL80211_IFTYPE_MESH_POINT:
7737 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007738 case NL80211_IFTYPE_P2P_CLIENT:
7739 return "P2P_CLIENT";
7740 case NL80211_IFTYPE_P2P_GO:
7741 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007742 case NL80211_IFTYPE_P2P_DEVICE:
7743 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007744 default:
7745 return "unknown";
7746 }
7747}
7748
7749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007750static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7751 const char *ifname,
7752 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007753 const u8 *addr, int wds,
7754 int (*handler)(struct nl_msg *, void *),
7755 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007756{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007757 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758 int ifidx;
7759 int ret = -ENOBUFS;
7760
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007761 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7762 iftype, nl80211_iftype_str(iftype));
7763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007764 msg = nlmsg_alloc();
7765 if (!msg)
7766 return -1;
7767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007768 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007769 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007770 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007771 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7772 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7773
7774 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007775 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007776
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007777 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007778 if (!flags)
7779 goto nla_put_failure;
7780
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007781 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007782
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007783 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007784 } else if (wds) {
7785 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7786 }
7787
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007788 /*
7789 * Tell cfg80211 that the interface belongs to the socket that created
7790 * it, and the interface should be deleted when the socket is closed.
7791 */
7792 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
7793
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007794 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007795 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007796 if (ret) {
7797 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007798 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7800 ifname, ret, strerror(-ret));
7801 return ret;
7802 }
7803
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007804 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7805 return 0;
7806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007807 ifidx = if_nametoindex(ifname);
7808 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7809 ifname, ifidx);
7810
7811 if (ifidx <= 0)
7812 return -1;
7813
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007814 /*
7815 * Some virtual interfaces need to process EAPOL packets and events on
7816 * the parent interface. This is used mainly with hostapd.
7817 */
7818 if (drv->hostapd ||
7819 iftype == NL80211_IFTYPE_AP_VLAN ||
7820 iftype == NL80211_IFTYPE_WDS ||
7821 iftype == NL80211_IFTYPE_MONITOR) {
7822 /* start listening for EAPOL on this interface */
7823 add_ifidx(drv, ifidx);
7824 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007825
7826 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007827 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007828 nl80211_remove_iface(drv, ifidx);
7829 return -1;
7830 }
7831
7832 return ifidx;
7833}
7834
7835
7836static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7837 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007838 const u8 *addr, int wds,
7839 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007840 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007841{
7842 int ret;
7843
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007844 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7845 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007847 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007849 if (use_existing) {
7850 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7851 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007852 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
7853 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7854 addr) < 0 &&
7855 (linux_set_iface_flags(drv->global->ioctl_sock,
7856 ifname, 0) < 0 ||
7857 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7858 addr) < 0 ||
7859 linux_set_iface_flags(drv->global->ioctl_sock,
7860 ifname, 1) < 0))
7861 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007862 return -ENFILE;
7863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007864 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7865
7866 /* Try to remove the interface that was already there. */
7867 nl80211_remove_iface(drv, if_nametoindex(ifname));
7868
7869 /* Try to create the interface again */
7870 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007871 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007872 }
7873
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007874 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007875 nl80211_disable_11b_rates(drv, ret, 1);
7876
7877 return ret;
7878}
7879
7880
7881static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7882{
7883 struct ieee80211_hdr *hdr;
7884 u16 fc;
7885 union wpa_event_data event;
7886
7887 hdr = (struct ieee80211_hdr *) buf;
7888 fc = le_to_host16(hdr->frame_control);
7889
7890 os_memset(&event, 0, sizeof(event));
7891 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7892 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7893 event.tx_status.dst = hdr->addr1;
7894 event.tx_status.data = buf;
7895 event.tx_status.data_len = len;
7896 event.tx_status.ack = ok;
7897 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7898}
7899
7900
7901static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7902 u8 *buf, size_t len)
7903{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007904 struct ieee80211_hdr *hdr = (void *)buf;
7905 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007906 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007907
7908 if (len < sizeof(*hdr))
7909 return;
7910
7911 fc = le_to_host16(hdr->frame_control);
7912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007913 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007914 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7915 event.rx_from_unknown.addr = hdr->addr2;
7916 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7917 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007918 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7919}
7920
7921
7922static void handle_frame(struct wpa_driver_nl80211_data *drv,
7923 u8 *buf, size_t len, int datarate, int ssi_signal)
7924{
7925 struct ieee80211_hdr *hdr;
7926 u16 fc;
7927 union wpa_event_data event;
7928
7929 hdr = (struct ieee80211_hdr *) buf;
7930 fc = le_to_host16(hdr->frame_control);
7931
7932 switch (WLAN_FC_GET_TYPE(fc)) {
7933 case WLAN_FC_TYPE_MGMT:
7934 os_memset(&event, 0, sizeof(event));
7935 event.rx_mgmt.frame = buf;
7936 event.rx_mgmt.frame_len = len;
7937 event.rx_mgmt.datarate = datarate;
7938 event.rx_mgmt.ssi_signal = ssi_signal;
7939 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7940 break;
7941 case WLAN_FC_TYPE_CTRL:
7942 /* can only get here with PS-Poll frames */
7943 wpa_printf(MSG_DEBUG, "CTRL");
7944 from_unknown_sta(drv, buf, len);
7945 break;
7946 case WLAN_FC_TYPE_DATA:
7947 from_unknown_sta(drv, buf, len);
7948 break;
7949 }
7950}
7951
7952
7953static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7954{
7955 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7956 int len;
7957 unsigned char buf[3000];
7958 struct ieee80211_radiotap_iterator iter;
7959 int ret;
7960 int datarate = 0, ssi_signal = 0;
7961 int injected = 0, failed = 0, rxflags = 0;
7962
7963 len = recv(sock, buf, sizeof(buf), 0);
7964 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007965 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7966 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967 return;
7968 }
7969
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007970 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007971 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007972 return;
7973 }
7974
7975 while (1) {
7976 ret = ieee80211_radiotap_iterator_next(&iter);
7977 if (ret == -ENOENT)
7978 break;
7979 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007980 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7981 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007982 return;
7983 }
7984 switch (iter.this_arg_index) {
7985 case IEEE80211_RADIOTAP_FLAGS:
7986 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7987 len -= 4;
7988 break;
7989 case IEEE80211_RADIOTAP_RX_FLAGS:
7990 rxflags = 1;
7991 break;
7992 case IEEE80211_RADIOTAP_TX_FLAGS:
7993 injected = 1;
7994 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7995 IEEE80211_RADIOTAP_F_TX_FAIL;
7996 break;
7997 case IEEE80211_RADIOTAP_DATA_RETRIES:
7998 break;
7999 case IEEE80211_RADIOTAP_CHANNEL:
8000 /* TODO: convert from freq/flags to channel number */
8001 break;
8002 case IEEE80211_RADIOTAP_RATE:
8003 datarate = *iter.this_arg * 5;
8004 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008005 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
8006 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008007 break;
8008 }
8009 }
8010
8011 if (rxflags && injected)
8012 return;
8013
8014 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008015 handle_frame(drv, buf + iter._max_length,
8016 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008017 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008018 handle_tx_callback(drv->ctx, buf + iter._max_length,
8019 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008020}
8021
8022
8023/*
8024 * we post-process the filter code later and rewrite
8025 * this to the offset to the last instruction
8026 */
8027#define PASS 0xFF
8028#define FAIL 0xFE
8029
8030static struct sock_filter msock_filter_insns[] = {
8031 /*
8032 * do a little-endian load of the radiotap length field
8033 */
8034 /* load lower byte into A */
8035 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
8036 /* put it into X (== index register) */
8037 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8038 /* load upper byte into A */
8039 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8040 /* left-shift it by 8 */
8041 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8042 /* or with X */
8043 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8044 /* put result into X */
8045 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8046
8047 /*
8048 * Allow management frames through, this also gives us those
8049 * management frames that we sent ourselves with status
8050 */
8051 /* load the lower byte of the IEEE 802.11 frame control field */
8052 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8053 /* mask off frame type and version */
8054 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8055 /* accept frame if it's both 0, fall through otherwise */
8056 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8057
8058 /*
8059 * TODO: add a bit to radiotap RX flags that indicates
8060 * that the sending station is not associated, then
8061 * add a filter here that filters on our DA and that flag
8062 * to allow us to deauth frames to that bad station.
8063 *
8064 * For now allow all To DS data frames through.
8065 */
8066 /* load the IEEE 802.11 frame control field */
8067 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8068 /* mask off frame type, version and DS status */
8069 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8070 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8071 */
8072 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
8073
8074#if 0
8075 /*
8076 * drop non-data frames
8077 */
8078 /* load the lower byte of the frame control field */
8079 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8080 /* mask off QoS bit */
8081 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8082 /* drop non-data frames */
8083 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
8084#endif
8085 /* load the upper byte of the frame control field */
8086 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
8087 /* mask off toDS/fromDS */
8088 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
8089 /* accept WDS frames */
8090 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
8091
8092 /*
8093 * add header length to index
8094 */
8095 /* load the lower byte of the frame control field */
8096 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8097 /* mask off QoS bit */
8098 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8099 /* right shift it by 6 to give 0 or 2 */
8100 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8101 /* add data frame header length */
8102 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8103 /* add index, was start of 802.11 header */
8104 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8105 /* move to index, now start of LL header */
8106 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8107
8108 /*
8109 * Accept empty data frames, we use those for
8110 * polling activity.
8111 */
8112 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8113 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8114
8115 /*
8116 * Accept EAPOL frames
8117 */
8118 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8119 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8120 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8121 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8122
8123 /* keep these last two statements or change the code below */
8124 /* return 0 == "DROP" */
8125 BPF_STMT(BPF_RET | BPF_K, 0),
8126 /* return ~0 == "keep all" */
8127 BPF_STMT(BPF_RET | BPF_K, ~0),
8128};
8129
8130static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008131 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008132 .filter = msock_filter_insns,
8133};
8134
8135
8136static int add_monitor_filter(int s)
8137{
8138 int idx;
8139
8140 /* rewrite all PASS/FAIL jump offsets */
8141 for (idx = 0; idx < msock_filter.len; idx++) {
8142 struct sock_filter *insn = &msock_filter_insns[idx];
8143
8144 if (BPF_CLASS(insn->code) == BPF_JMP) {
8145 if (insn->code == (BPF_JMP|BPF_JA)) {
8146 if (insn->k == PASS)
8147 insn->k = msock_filter.len - idx - 2;
8148 else if (insn->k == FAIL)
8149 insn->k = msock_filter.len - idx - 3;
8150 }
8151
8152 if (insn->jt == PASS)
8153 insn->jt = msock_filter.len - idx - 2;
8154 else if (insn->jt == FAIL)
8155 insn->jt = msock_filter.len - idx - 3;
8156
8157 if (insn->jf == PASS)
8158 insn->jf = msock_filter.len - idx - 2;
8159 else if (insn->jf == FAIL)
8160 insn->jf = msock_filter.len - idx - 3;
8161 }
8162 }
8163
8164 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8165 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008166 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8167 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008168 return -1;
8169 }
8170
8171 return 0;
8172}
8173
8174
8175static void nl80211_remove_monitor_interface(
8176 struct wpa_driver_nl80211_data *drv)
8177{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008178 if (drv->monitor_refcount > 0)
8179 drv->monitor_refcount--;
8180 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8181 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008182 if (drv->monitor_refcount > 0)
8183 return;
8184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008185 if (drv->monitor_ifidx >= 0) {
8186 nl80211_remove_iface(drv, drv->monitor_ifidx);
8187 drv->monitor_ifidx = -1;
8188 }
8189 if (drv->monitor_sock >= 0) {
8190 eloop_unregister_read_sock(drv->monitor_sock);
8191 close(drv->monitor_sock);
8192 drv->monitor_sock = -1;
8193 }
8194}
8195
8196
8197static int
8198nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8199{
8200 char buf[IFNAMSIZ];
8201 struct sockaddr_ll ll;
8202 int optval;
8203 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008204
8205 if (drv->monitor_ifidx >= 0) {
8206 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008207 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8208 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008209 return 0;
8210 }
8211
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008212 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008213 /*
8214 * P2P interface name is of the format p2p-%s-%d. For monitor
8215 * interface name corresponding to P2P GO, replace "p2p-" with
8216 * "mon-" to retain the same interface name length and to
8217 * indicate that it is a monitor interface.
8218 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008219 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008220 } else {
8221 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008222 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008223 }
8224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008225 buf[IFNAMSIZ - 1] = '\0';
8226
8227 drv->monitor_ifidx =
8228 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008229 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008230
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008231 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008232 /*
8233 * This is backward compatibility for a few versions of
8234 * the kernel only that didn't advertise the right
8235 * attributes for the only driver that then supported
8236 * AP mode w/o monitor -- ath6kl.
8237 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008238 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8239 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008240 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008241 }
8242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008243 if (drv->monitor_ifidx < 0)
8244 return -1;
8245
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008246 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008247 goto error;
8248
8249 memset(&ll, 0, sizeof(ll));
8250 ll.sll_family = AF_PACKET;
8251 ll.sll_ifindex = drv->monitor_ifidx;
8252 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8253 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008254 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8255 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008256 goto error;
8257 }
8258
8259 if (add_monitor_filter(drv->monitor_sock)) {
8260 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8261 "interface; do filtering in user space");
8262 /* This works, but will cost in performance. */
8263 }
8264
8265 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008266 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8267 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008268 goto error;
8269 }
8270
8271 optlen = sizeof(optval);
8272 optval = 20;
8273 if (setsockopt
8274 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008275 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8276 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008277 goto error;
8278 }
8279
8280 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8281 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008282 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008283 goto error;
8284 }
8285
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008286 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008287 return 0;
8288 error:
8289 nl80211_remove_monitor_interface(drv);
8290 return -1;
8291}
8292
8293
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008294static int nl80211_setup_ap(struct i802_bss *bss)
8295{
8296 struct wpa_driver_nl80211_data *drv = bss->drv;
8297
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008298 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8299 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008300
8301 /*
8302 * Disable Probe Request reporting unless we need it in this way for
8303 * devices that include the AP SME, in the other case (unless using
8304 * monitor iface) we'll get it through the nl_mgmt socket instead.
8305 */
8306 if (!drv->device_ap_sme)
8307 wpa_driver_nl80211_probe_req_report(bss, 0);
8308
8309 if (!drv->device_ap_sme && !drv->use_monitor)
8310 if (nl80211_mgmt_subscribe_ap(bss))
8311 return -1;
8312
8313 if (drv->device_ap_sme && !drv->use_monitor)
8314 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8315 return -1;
8316
8317 if (!drv->device_ap_sme && drv->use_monitor &&
8318 nl80211_create_monitor_interface(drv) &&
8319 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008320 return -1;
8321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008322 if (drv->device_ap_sme &&
8323 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8324 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8325 "Probe Request frame reporting in AP mode");
8326 /* Try to survive without this */
8327 }
8328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008329 return 0;
8330}
8331
8332
8333static void nl80211_teardown_ap(struct i802_bss *bss)
8334{
8335 struct wpa_driver_nl80211_data *drv = bss->drv;
8336
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008337 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8338 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008339 if (drv->device_ap_sme) {
8340 wpa_driver_nl80211_probe_req_report(bss, 0);
8341 if (!drv->use_monitor)
8342 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8343 } else if (drv->use_monitor)
8344 nl80211_remove_monitor_interface(drv);
8345 else
8346 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8347
8348 bss->beacon_set = 0;
8349}
8350
8351
8352static int nl80211_send_eapol_data(struct i802_bss *bss,
8353 const u8 *addr, const u8 *data,
8354 size_t data_len)
8355{
8356 struct sockaddr_ll ll;
8357 int ret;
8358
8359 if (bss->drv->eapol_tx_sock < 0) {
8360 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8361 return -1;
8362 }
8363
8364 os_memset(&ll, 0, sizeof(ll));
8365 ll.sll_family = AF_PACKET;
8366 ll.sll_ifindex = bss->ifindex;
8367 ll.sll_protocol = htons(ETH_P_PAE);
8368 ll.sll_halen = ETH_ALEN;
8369 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8370 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8371 (struct sockaddr *) &ll, sizeof(ll));
8372 if (ret < 0)
8373 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8374 strerror(errno));
8375
8376 return ret;
8377}
8378
8379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008380static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8381
8382static int wpa_driver_nl80211_hapd_send_eapol(
8383 void *priv, const u8 *addr, const u8 *data,
8384 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8385{
8386 struct i802_bss *bss = priv;
8387 struct wpa_driver_nl80211_data *drv = bss->drv;
8388 struct ieee80211_hdr *hdr;
8389 size_t len;
8390 u8 *pos;
8391 int res;
8392 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008394 if (drv->device_ap_sme || !drv->use_monitor)
8395 return nl80211_send_eapol_data(bss, addr, data, data_len);
8396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008397 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8398 data_len;
8399 hdr = os_zalloc(len);
8400 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008401 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8402 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008403 return -1;
8404 }
8405
8406 hdr->frame_control =
8407 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8408 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8409 if (encrypt)
8410 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8411 if (qos) {
8412 hdr->frame_control |=
8413 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8414 }
8415
8416 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8417 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8418 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8419 pos = (u8 *) (hdr + 1);
8420
8421 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008422 /* Set highest priority in QoS header */
8423 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008424 pos[1] = 0;
8425 pos += 2;
8426 }
8427
8428 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8429 pos += sizeof(rfc1042_header);
8430 WPA_PUT_BE16(pos, ETH_P_PAE);
8431 pos += 2;
8432 memcpy(pos, data, data_len);
8433
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008434 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8435 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008436 if (res < 0) {
8437 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8438 "failed: %d (%s)",
8439 (unsigned long) len, errno, strerror(errno));
8440 }
8441 os_free(hdr);
8442
8443 return res;
8444}
8445
8446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008447static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8448 int total_flags,
8449 int flags_or, int flags_and)
8450{
8451 struct i802_bss *bss = priv;
8452 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008453 struct nl_msg *msg;
8454 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008455 struct nl80211_sta_flag_update upd;
8456
8457 msg = nlmsg_alloc();
8458 if (!msg)
8459 return -ENOMEM;
8460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008461 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462
8463 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8464 if_nametoindex(bss->ifname));
8465 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8466
8467 /*
8468 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8469 * can be removed eventually.
8470 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008471 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8472 if (!flags)
8473 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008474 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008475 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008476
8477 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008478 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008479
8480 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008481 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008482
8483 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008484 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008486 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008487 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008488
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008489 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008490
8491 os_memset(&upd, 0, sizeof(upd));
8492 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8493 upd.set = sta_flags_nl80211(flags_or);
8494 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008496 return send_and_recv_msgs(drv, msg, NULL, NULL);
8497 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008498 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008499 return -ENOBUFS;
8500}
8501
8502
8503static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8504 struct wpa_driver_associate_params *params)
8505{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008506 enum nl80211_iftype nlmode, old_mode;
8507 struct hostapd_freq_params freq = {
8508 .freq = params->freq,
8509 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008510
8511 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008512 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8513 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008514 nlmode = NL80211_IFTYPE_P2P_GO;
8515 } else
8516 nlmode = NL80211_IFTYPE_AP;
8517
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008518 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008519 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008520 nl80211_remove_monitor_interface(drv);
8521 return -1;
8522 }
8523
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008524 if (nl80211_set_channel(drv->first_bss, &freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008525 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008526 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008527 nl80211_remove_monitor_interface(drv);
8528 return -1;
8529 }
8530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008531 return 0;
8532}
8533
8534
8535static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8536{
8537 struct nl_msg *msg;
8538 int ret = -1;
8539
8540 msg = nlmsg_alloc();
8541 if (!msg)
8542 return -1;
8543
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008544 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008545 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8546 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8547 msg = NULL;
8548 if (ret) {
8549 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8550 "(%s)", ret, strerror(-ret));
8551 goto nla_put_failure;
8552 }
8553
8554 ret = 0;
8555 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8556
8557nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008558 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008559 NL80211_IFTYPE_STATION)) {
8560 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8561 "station mode");
8562 }
8563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008564 nlmsg_free(msg);
8565 return ret;
8566}
8567
8568
8569static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8570 struct wpa_driver_associate_params *params)
8571{
8572 struct nl_msg *msg;
8573 int ret = -1;
8574 int count = 0;
8575
8576 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8577
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008578 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008579 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008580 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8581 "IBSS mode");
8582 return -1;
8583 }
8584
8585retry:
8586 msg = nlmsg_alloc();
8587 if (!msg)
8588 return -1;
8589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008590 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008591 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8592
8593 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8594 goto nla_put_failure;
8595
8596 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8597 params->ssid, params->ssid_len);
8598 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8599 params->ssid);
8600 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8601 drv->ssid_len = params->ssid_len;
8602
8603 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8604 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8605
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008606 if (params->beacon_int > 0) {
8607 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8608 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8609 params->beacon_int);
8610 }
8611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008612 ret = nl80211_set_conn_keys(params, msg);
8613 if (ret)
8614 goto nla_put_failure;
8615
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008616 if (params->bssid && params->fixed_bssid) {
8617 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8618 MAC2STR(params->bssid));
8619 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8620 }
8621
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008622 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8623 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8624 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8625 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008626 wpa_printf(MSG_DEBUG, " * control port");
8627 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8628 }
8629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008630 if (params->wpa_ie) {
8631 wpa_hexdump(MSG_DEBUG,
8632 " * Extra IEs for Beacon/Probe Response frames",
8633 params->wpa_ie, params->wpa_ie_len);
8634 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8635 params->wpa_ie);
8636 }
8637
8638 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8639 msg = NULL;
8640 if (ret) {
8641 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8642 ret, strerror(-ret));
8643 count++;
8644 if (ret == -EALREADY && count == 1) {
8645 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8646 "forced leave");
8647 nl80211_leave_ibss(drv);
8648 nlmsg_free(msg);
8649 goto retry;
8650 }
8651
8652 goto nla_put_failure;
8653 }
8654 ret = 0;
8655 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8656
8657nla_put_failure:
8658 nlmsg_free(msg);
8659 return ret;
8660}
8661
8662
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008663static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8664 struct wpa_driver_associate_params *params,
8665 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008666{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008667 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008669 if (params->bssid) {
8670 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8671 MAC2STR(params->bssid));
8672 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8673 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008674
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008675 if (params->bssid_hint) {
8676 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8677 MAC2STR(params->bssid_hint));
8678 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8679 params->bssid_hint);
8680 }
8681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008682 if (params->freq) {
8683 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8684 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008685 drv->assoc_freq = params->freq;
8686 } else
8687 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008688
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008689 if (params->freq_hint) {
8690 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8691 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8692 params->freq_hint);
8693 }
8694
Dmitry Shmidt04949592012-07-19 12:16:46 -07008695 if (params->bg_scan_period >= 0) {
8696 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8697 params->bg_scan_period);
8698 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8699 params->bg_scan_period);
8700 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008702 if (params->ssid) {
8703 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8704 params->ssid, params->ssid_len);
8705 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8706 params->ssid);
8707 if (params->ssid_len > sizeof(drv->ssid))
8708 goto nla_put_failure;
8709 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8710 drv->ssid_len = params->ssid_len;
8711 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008713 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8714 if (params->wpa_ie)
8715 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8716 params->wpa_ie);
8717
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008718 if (params->wpa_proto) {
8719 enum nl80211_wpa_versions ver = 0;
8720
8721 if (params->wpa_proto & WPA_PROTO_WPA)
8722 ver |= NL80211_WPA_VERSION_1;
8723 if (params->wpa_proto & WPA_PROTO_RSN)
8724 ver |= NL80211_WPA_VERSION_2;
8725
8726 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8727 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8728 }
8729
8730 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8731 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8732 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8733 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8734 }
8735
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008736 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8737 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8738 /*
8739 * This is likely to work even though many drivers do not
8740 * advertise support for operations without GTK.
8741 */
8742 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8743 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008744 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8745 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8746 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8747 }
8748
8749 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8750 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8751 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8752 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008753 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008754 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
8755 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8756 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008757 int mgmt = WLAN_AKM_SUITE_PSK;
8758
8759 switch (params->key_mgmt_suite) {
8760 case WPA_KEY_MGMT_CCKM:
8761 mgmt = WLAN_AKM_SUITE_CCKM;
8762 break;
8763 case WPA_KEY_MGMT_IEEE8021X:
8764 mgmt = WLAN_AKM_SUITE_8021X;
8765 break;
8766 case WPA_KEY_MGMT_FT_IEEE8021X:
8767 mgmt = WLAN_AKM_SUITE_FT_8021X;
8768 break;
8769 case WPA_KEY_MGMT_FT_PSK:
8770 mgmt = WLAN_AKM_SUITE_FT_PSK;
8771 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008772 case WPA_KEY_MGMT_IEEE8021X_SHA256:
8773 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
8774 break;
8775 case WPA_KEY_MGMT_PSK_SHA256:
8776 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
8777 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008778 case WPA_KEY_MGMT_OSEN:
8779 mgmt = WLAN_AKM_SUITE_OSEN;
8780 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008781 case WPA_KEY_MGMT_PSK:
8782 default:
8783 mgmt = WLAN_AKM_SUITE_PSK;
8784 break;
8785 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008786 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008787 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8788 }
8789
8790 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8791
8792 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8793 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8794
8795 if (params->disable_ht)
8796 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8797
8798 if (params->htcaps && params->htcaps_mask) {
8799 int sz = sizeof(struct ieee80211_ht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008800 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008801 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008802 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
8803 params->htcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008804 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8805 params->htcaps_mask);
8806 }
8807
8808#ifdef CONFIG_VHT_OVERRIDES
8809 if (params->disable_vht) {
8810 wpa_printf(MSG_DEBUG, " * VHT disabled");
8811 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8812 }
8813
8814 if (params->vhtcaps && params->vhtcaps_mask) {
8815 int sz = sizeof(struct ieee80211_vht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008816 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008817 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008818 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
8819 params->vhtcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008820 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8821 params->vhtcaps_mask);
8822 }
8823#endif /* CONFIG_VHT_OVERRIDES */
8824
8825 if (params->p2p)
8826 wpa_printf(MSG_DEBUG, " * P2P group");
8827
8828 return 0;
8829nla_put_failure:
8830 return -1;
8831}
8832
8833
8834static int wpa_driver_nl80211_try_connect(
8835 struct wpa_driver_nl80211_data *drv,
8836 struct wpa_driver_associate_params *params)
8837{
8838 struct nl_msg *msg;
8839 enum nl80211_auth_type type;
8840 int ret;
8841 int algs;
8842
8843 msg = nlmsg_alloc();
8844 if (!msg)
8845 return -1;
8846
8847 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8848 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8849
8850 ret = nl80211_connect_common(drv, params, msg);
8851 if (ret)
8852 goto nla_put_failure;
8853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008854 algs = 0;
8855 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8856 algs++;
8857 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8858 algs++;
8859 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8860 algs++;
8861 if (algs > 1) {
8862 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8863 "selection");
8864 goto skip_auth_type;
8865 }
8866
8867 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8868 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8869 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8870 type = NL80211_AUTHTYPE_SHARED_KEY;
8871 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8872 type = NL80211_AUTHTYPE_NETWORK_EAP;
8873 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8874 type = NL80211_AUTHTYPE_FT;
8875 else
8876 goto nla_put_failure;
8877
8878 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8879 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8880
8881skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008882 ret = nl80211_set_conn_keys(params, msg);
8883 if (ret)
8884 goto nla_put_failure;
8885
8886 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8887 msg = NULL;
8888 if (ret) {
8889 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8890 "(%s)", ret, strerror(-ret));
8891 goto nla_put_failure;
8892 }
8893 ret = 0;
8894 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8895
8896nla_put_failure:
8897 nlmsg_free(msg);
8898 return ret;
8899
8900}
8901
8902
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008903static int wpa_driver_nl80211_connect(
8904 struct wpa_driver_nl80211_data *drv,
8905 struct wpa_driver_associate_params *params)
8906{
8907 int ret = wpa_driver_nl80211_try_connect(drv, params);
8908 if (ret == -EALREADY) {
8909 /*
8910 * cfg80211 does not currently accept new connections if
8911 * we are already connected. As a workaround, force
8912 * disconnection and try again.
8913 */
8914 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8915 "disconnecting before reassociation "
8916 "attempt");
8917 if (wpa_driver_nl80211_disconnect(
8918 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8919 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008920 ret = wpa_driver_nl80211_try_connect(drv, params);
8921 }
8922 return ret;
8923}
8924
8925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008926static int wpa_driver_nl80211_associate(
8927 void *priv, struct wpa_driver_associate_params *params)
8928{
8929 struct i802_bss *bss = priv;
8930 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008931 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008932 struct nl_msg *msg;
8933
8934 if (params->mode == IEEE80211_MODE_AP)
8935 return wpa_driver_nl80211_ap(drv, params);
8936
8937 if (params->mode == IEEE80211_MODE_IBSS)
8938 return wpa_driver_nl80211_ibss(drv, params);
8939
8940 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008941 enum nl80211_iftype nlmode = params->p2p ?
8942 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8943
8944 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008945 return -1;
8946 return wpa_driver_nl80211_connect(drv, params);
8947 }
8948
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008949 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008950
8951 msg = nlmsg_alloc();
8952 if (!msg)
8953 return -1;
8954
8955 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8956 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008957 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008958
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008959 ret = nl80211_connect_common(drv, params, msg);
8960 if (ret)
8961 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008962
8963 if (params->prev_bssid) {
8964 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8965 MAC2STR(params->prev_bssid));
8966 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8967 params->prev_bssid);
8968 }
8969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008970 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8971 msg = NULL;
8972 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008973 wpa_dbg(drv->ctx, MSG_DEBUG,
8974 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8975 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008976 nl80211_dump_scan(drv);
8977 goto nla_put_failure;
8978 }
8979 ret = 0;
8980 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8981 "successfully");
8982
8983nla_put_failure:
8984 nlmsg_free(msg);
8985 return ret;
8986}
8987
8988
8989static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008990 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008991{
8992 struct nl_msg *msg;
8993 int ret = -ENOBUFS;
8994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008995 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8996 ifindex, mode, nl80211_iftype_str(mode));
8997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008998 msg = nlmsg_alloc();
8999 if (!msg)
9000 return -ENOMEM;
9001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009002 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009003 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009004 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009005 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
9006
9007 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009008 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009009 if (!ret)
9010 return 0;
9011nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009012 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009013 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
9014 " %d (%s)", ifindex, mode, ret, strerror(-ret));
9015 return ret;
9016}
9017
9018
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009019static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
9020 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009021{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009022 struct wpa_driver_nl80211_data *drv = bss->drv;
9023 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009024 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009025 int was_ap = is_ap_interface(drv->nlmode);
9026 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009028 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009029 if (res && nlmode == nl80211_get_ifmode(bss))
9030 res = 0;
9031
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009032 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009033 drv->nlmode = nlmode;
9034 ret = 0;
9035 goto done;
9036 }
9037
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009038 if (res == -ENODEV)
9039 return -1;
9040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009041 if (nlmode == drv->nlmode) {
9042 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
9043 "requested mode - ignore error");
9044 ret = 0;
9045 goto done; /* Already in the requested mode */
9046 }
9047
9048 /* mac80211 doesn't allow mode changes while the device is up, so
9049 * take the device down, try to set the mode again, and bring the
9050 * device back up.
9051 */
9052 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9053 "interface down");
9054 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009055 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009056 if (res == -EACCES || res == -ENODEV)
9057 break;
9058 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009059 /* Try to set the mode again while the interface is
9060 * down */
9061 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009062 if (ret == -EACCES)
9063 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009064 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009065 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009066 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009067 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009068 break;
9069 } else
9070 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9071 "interface down");
9072 os_sleep(0, 100000);
9073 }
9074
9075 if (!ret) {
9076 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9077 "interface is down");
9078 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009079 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009080 }
9081
9082done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009083 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009084 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9085 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009086 return ret;
9087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009088
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009089 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009090 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
9091 else if (drv->disabled_11b_rates)
9092 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
9093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009094 if (is_ap_interface(nlmode)) {
9095 nl80211_mgmt_unsubscribe(bss, "start AP");
9096 /* Setup additional AP mode functionality if needed */
9097 if (nl80211_setup_ap(bss))
9098 return -1;
9099 } else if (was_ap) {
9100 /* Remove additional AP mode functionality */
9101 nl80211_teardown_ap(bss);
9102 } else {
9103 nl80211_mgmt_unsubscribe(bss, "mode change");
9104 }
9105
Dmitry Shmidt04949592012-07-19 12:16:46 -07009106 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009107 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9108 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9109 "frame processing - ignore for now");
9110
9111 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009112}
9113
9114
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009115static int dfs_info_handler(struct nl_msg *msg, void *arg)
9116{
9117 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9118 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9119 int *dfs_capability_ptr = arg;
9120
9121 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9122 genlmsg_attrlen(gnlh, 0), NULL);
9123
9124 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9125 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9126 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9127
9128 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9129 nla_data(nl_vend), nla_len(nl_vend), NULL);
9130
9131 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9132 u32 val;
9133 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9134 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9135 val);
9136 *dfs_capability_ptr = val;
9137 }
9138 }
9139
9140 return NL_SKIP;
9141}
9142
9143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009144static int wpa_driver_nl80211_get_capa(void *priv,
9145 struct wpa_driver_capa *capa)
9146{
9147 struct i802_bss *bss = priv;
9148 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009149 struct nl_msg *msg;
9150 int dfs_capability = 0;
9151 int ret = 0;
9152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009153 if (!drv->has_capability)
9154 return -1;
9155 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009156 if (drv->extended_capa && drv->extended_capa_mask) {
9157 capa->extended_capa = drv->extended_capa;
9158 capa->extended_capa_mask = drv->extended_capa_mask;
9159 capa->extended_capa_len = drv->extended_capa_len;
9160 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009161
9162 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9163 !drv->allow_p2p_device) {
9164 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9165 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9166 }
9167
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009168 if (drv->dfs_vendor_cmd_avail == 1) {
9169 msg = nlmsg_alloc();
9170 if (!msg)
9171 return -ENOMEM;
9172
9173 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9174
9175 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9176 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9177 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9178 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9179
9180 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9181 &dfs_capability);
9182 if (!ret) {
9183 if (dfs_capability)
9184 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9185 }
9186 }
9187
9188 return ret;
9189
9190 nla_put_failure:
9191 nlmsg_free(msg);
9192 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009193}
9194
9195
9196static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9197{
9198 struct i802_bss *bss = priv;
9199 struct wpa_driver_nl80211_data *drv = bss->drv;
9200
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009201 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9202 bss->ifname, drv->operstate, state,
9203 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009204 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009205 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009206 state ? IF_OPER_UP : IF_OPER_DORMANT);
9207}
9208
9209
9210static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9211{
9212 struct i802_bss *bss = priv;
9213 struct wpa_driver_nl80211_data *drv = bss->drv;
9214 struct nl_msg *msg;
9215 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009216 int ret = -ENOBUFS;
9217
9218 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9219 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9220 return 0;
9221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009222
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009223 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9224 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009226 msg = nlmsg_alloc();
9227 if (!msg)
9228 return -ENOMEM;
9229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009230 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009231
9232 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9233 if_nametoindex(bss->ifname));
9234 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9235
9236 os_memset(&upd, 0, sizeof(upd));
9237 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9238 if (authorized)
9239 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9240 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9241
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009242 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9243 msg = NULL;
9244 if (!ret)
9245 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009246 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009247 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009248 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9249 ret, strerror(-ret));
9250 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009251}
9252
9253
Jouni Malinen75ecf522011-06-27 15:19:46 -07009254/* Set kernel driver on given frequency (MHz) */
9255static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009256{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009257 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009258 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009259}
9260
9261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009262static inline int min_int(int a, int b)
9263{
9264 if (a < b)
9265 return a;
9266 return b;
9267}
9268
9269
9270static int get_key_handler(struct nl_msg *msg, void *arg)
9271{
9272 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9273 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9274
9275 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9276 genlmsg_attrlen(gnlh, 0), NULL);
9277
9278 /*
9279 * TODO: validate the key index and mac address!
9280 * Otherwise, there's a race condition as soon as
9281 * the kernel starts sending key notifications.
9282 */
9283
9284 if (tb[NL80211_ATTR_KEY_SEQ])
9285 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9286 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9287 return NL_SKIP;
9288}
9289
9290
9291static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9292 int idx, u8 *seq)
9293{
9294 struct i802_bss *bss = priv;
9295 struct wpa_driver_nl80211_data *drv = bss->drv;
9296 struct nl_msg *msg;
9297
9298 msg = nlmsg_alloc();
9299 if (!msg)
9300 return -ENOMEM;
9301
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009302 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009303
9304 if (addr)
9305 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9306 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9307 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9308
9309 memset(seq, 0, 6);
9310
9311 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9312 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009313 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009314 return -ENOBUFS;
9315}
9316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009317
9318static int i802_set_rts(void *priv, int rts)
9319{
9320 struct i802_bss *bss = priv;
9321 struct wpa_driver_nl80211_data *drv = bss->drv;
9322 struct nl_msg *msg;
9323 int ret = -ENOBUFS;
9324 u32 val;
9325
9326 msg = nlmsg_alloc();
9327 if (!msg)
9328 return -ENOMEM;
9329
9330 if (rts >= 2347)
9331 val = (u32) -1;
9332 else
9333 val = rts;
9334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009335 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009336 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9337 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9338
9339 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009340 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009341 if (!ret)
9342 return 0;
9343nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009344 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009345 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9346 "%d (%s)", rts, ret, strerror(-ret));
9347 return ret;
9348}
9349
9350
9351static int i802_set_frag(void *priv, int frag)
9352{
9353 struct i802_bss *bss = priv;
9354 struct wpa_driver_nl80211_data *drv = bss->drv;
9355 struct nl_msg *msg;
9356 int ret = -ENOBUFS;
9357 u32 val;
9358
9359 msg = nlmsg_alloc();
9360 if (!msg)
9361 return -ENOMEM;
9362
9363 if (frag >= 2346)
9364 val = (u32) -1;
9365 else
9366 val = frag;
9367
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009368 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009369 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9370 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9371
9372 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009373 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009374 if (!ret)
9375 return 0;
9376nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009377 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009378 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9379 "%d: %d (%s)", frag, ret, strerror(-ret));
9380 return ret;
9381}
9382
9383
9384static int i802_flush(void *priv)
9385{
9386 struct i802_bss *bss = priv;
9387 struct wpa_driver_nl80211_data *drv = bss->drv;
9388 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009389 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009390
9391 msg = nlmsg_alloc();
9392 if (!msg)
9393 return -1;
9394
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009395 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9396 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009397 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009398
9399 /*
9400 * XXX: FIX! this needs to flush all VLANs too
9401 */
9402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9403 if_nametoindex(bss->ifname));
9404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009405 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9406 if (res) {
9407 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9408 "(%s)", res, strerror(-res));
9409 }
9410 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009411 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009412 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009413 return -ENOBUFS;
9414}
9415
9416
9417static int get_sta_handler(struct nl_msg *msg, void *arg)
9418{
9419 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9420 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9421 struct hostap_sta_driver_data *data = arg;
9422 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9423 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9424 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9425 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9426 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9427 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9428 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009429 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009430 };
9431
9432 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9433 genlmsg_attrlen(gnlh, 0), NULL);
9434
9435 /*
9436 * TODO: validate the interface and mac address!
9437 * Otherwise, there's a race condition as soon as
9438 * the kernel starts sending station notifications.
9439 */
9440
9441 if (!tb[NL80211_ATTR_STA_INFO]) {
9442 wpa_printf(MSG_DEBUG, "sta stats missing!");
9443 return NL_SKIP;
9444 }
9445 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9446 tb[NL80211_ATTR_STA_INFO],
9447 stats_policy)) {
9448 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9449 return NL_SKIP;
9450 }
9451
9452 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9453 data->inactive_msec =
9454 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9455 if (stats[NL80211_STA_INFO_RX_BYTES])
9456 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9457 if (stats[NL80211_STA_INFO_TX_BYTES])
9458 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9459 if (stats[NL80211_STA_INFO_RX_PACKETS])
9460 data->rx_packets =
9461 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9462 if (stats[NL80211_STA_INFO_TX_PACKETS])
9463 data->tx_packets =
9464 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009465 if (stats[NL80211_STA_INFO_TX_FAILED])
9466 data->tx_retry_failed =
9467 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009468
9469 return NL_SKIP;
9470}
9471
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009472static int i802_read_sta_data(struct i802_bss *bss,
9473 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009474 const u8 *addr)
9475{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009476 struct wpa_driver_nl80211_data *drv = bss->drv;
9477 struct nl_msg *msg;
9478
9479 os_memset(data, 0, sizeof(*data));
9480 msg = nlmsg_alloc();
9481 if (!msg)
9482 return -ENOMEM;
9483
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009484 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009485
9486 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9487 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9488
9489 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9490 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009491 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009492 return -ENOBUFS;
9493}
9494
9495
9496static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9497 int cw_min, int cw_max, int burst_time)
9498{
9499 struct i802_bss *bss = priv;
9500 struct wpa_driver_nl80211_data *drv = bss->drv;
9501 struct nl_msg *msg;
9502 struct nlattr *txq, *params;
9503
9504 msg = nlmsg_alloc();
9505 if (!msg)
9506 return -1;
9507
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009508 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009509
9510 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9511
9512 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9513 if (!txq)
9514 goto nla_put_failure;
9515
9516 /* We are only sending parameters for a single TXQ at a time */
9517 params = nla_nest_start(msg, 1);
9518 if (!params)
9519 goto nla_put_failure;
9520
9521 switch (queue) {
9522 case 0:
9523 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9524 break;
9525 case 1:
9526 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9527 break;
9528 case 2:
9529 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9530 break;
9531 case 3:
9532 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9533 break;
9534 }
9535 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9536 * 32 usec, so need to convert the value here. */
9537 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9538 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9539 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9540 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9541
9542 nla_nest_end(msg, params);
9543
9544 nla_nest_end(msg, txq);
9545
9546 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9547 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009548 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009549 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009550 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009551 return -1;
9552}
9553
9554
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009555static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009556 const char *ifname, int vlan_id)
9557{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009558 struct wpa_driver_nl80211_data *drv = bss->drv;
9559 struct nl_msg *msg;
9560 int ret = -ENOBUFS;
9561
9562 msg = nlmsg_alloc();
9563 if (!msg)
9564 return -ENOMEM;
9565
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009566 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9567 ", ifname=%s[%d], vlan_id=%d)",
9568 bss->ifname, if_nametoindex(bss->ifname),
9569 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009570 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009571
9572 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9573 if_nametoindex(bss->ifname));
9574 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9575 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9576 if_nametoindex(ifname));
9577
9578 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009579 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009580 if (ret < 0) {
9581 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9582 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9583 MAC2STR(addr), ifname, vlan_id, ret,
9584 strerror(-ret));
9585 }
9586 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009587 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009588 return ret;
9589}
9590
9591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009592static int i802_get_inact_sec(void *priv, const u8 *addr)
9593{
9594 struct hostap_sta_driver_data data;
9595 int ret;
9596
9597 data.inactive_msec = (unsigned long) -1;
9598 ret = i802_read_sta_data(priv, &data, addr);
9599 if (ret || data.inactive_msec == (unsigned long) -1)
9600 return -1;
9601 return data.inactive_msec / 1000;
9602}
9603
9604
9605static int i802_sta_clear_stats(void *priv, const u8 *addr)
9606{
9607#if 0
9608 /* TODO */
9609#endif
9610 return 0;
9611}
9612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009613
9614static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9615 int reason)
9616{
9617 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009618 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009619 struct ieee80211_mgmt mgmt;
9620
Dmitry Shmidt04949592012-07-19 12:16:46 -07009621 if (drv->device_ap_sme)
9622 return wpa_driver_nl80211_sta_remove(bss, addr);
9623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009624 memset(&mgmt, 0, sizeof(mgmt));
9625 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9626 WLAN_FC_STYPE_DEAUTH);
9627 memcpy(mgmt.da, addr, ETH_ALEN);
9628 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9629 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9630 mgmt.u.deauth.reason_code = host_to_le16(reason);
9631 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9632 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009633 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9634 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009635}
9636
9637
9638static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9639 int reason)
9640{
9641 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009642 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009643 struct ieee80211_mgmt mgmt;
9644
Dmitry Shmidt04949592012-07-19 12:16:46 -07009645 if (drv->device_ap_sme)
9646 return wpa_driver_nl80211_sta_remove(bss, addr);
9647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009648 memset(&mgmt, 0, sizeof(mgmt));
9649 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9650 WLAN_FC_STYPE_DISASSOC);
9651 memcpy(mgmt.da, addr, ETH_ALEN);
9652 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9653 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9654 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9655 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9656 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009657 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9658 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009659}
9660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009661
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009662static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9663{
9664 char buf[200], *pos, *end;
9665 int i, res;
9666
9667 pos = buf;
9668 end = pos + sizeof(buf);
9669
9670 for (i = 0; i < drv->num_if_indices; i++) {
9671 if (!drv->if_indices[i])
9672 continue;
9673 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9674 if (res < 0 || res >= end - pos)
9675 break;
9676 pos += res;
9677 }
9678 *pos = '\0';
9679
9680 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9681 drv->num_if_indices, buf);
9682}
9683
9684
Jouni Malinen75ecf522011-06-27 15:19:46 -07009685static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9686{
9687 int i;
9688 int *old;
9689
9690 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9691 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009692 if (have_ifidx(drv, ifidx)) {
9693 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9694 ifidx);
9695 return;
9696 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009697 for (i = 0; i < drv->num_if_indices; i++) {
9698 if (drv->if_indices[i] == 0) {
9699 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009700 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009701 return;
9702 }
9703 }
9704
9705 if (drv->if_indices != drv->default_if_indices)
9706 old = drv->if_indices;
9707 else
9708 old = NULL;
9709
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009710 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9711 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009712 if (!drv->if_indices) {
9713 if (!old)
9714 drv->if_indices = drv->default_if_indices;
9715 else
9716 drv->if_indices = old;
9717 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9718 "interfaces");
9719 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9720 return;
9721 } else if (!old)
9722 os_memcpy(drv->if_indices, drv->default_if_indices,
9723 sizeof(drv->default_if_indices));
9724 drv->if_indices[drv->num_if_indices] = ifidx;
9725 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009726 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009727}
9728
9729
9730static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9731{
9732 int i;
9733
9734 for (i = 0; i < drv->num_if_indices; i++) {
9735 if (drv->if_indices[i] == ifidx) {
9736 drv->if_indices[i] = 0;
9737 break;
9738 }
9739 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009740 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009741}
9742
9743
9744static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9745{
9746 int i;
9747
9748 for (i = 0; i < drv->num_if_indices; i++)
9749 if (drv->if_indices[i] == ifidx)
9750 return 1;
9751
9752 return 0;
9753}
9754
9755
9756static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009757 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009758{
9759 struct i802_bss *bss = priv;
9760 struct wpa_driver_nl80211_data *drv = bss->drv;
9761 char name[IFNAMSIZ + 1];
9762
9763 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009764 if (ifname_wds)
9765 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9766
Jouni Malinen75ecf522011-06-27 15:19:46 -07009767 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9768 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9769 if (val) {
9770 if (!if_nametoindex(name)) {
9771 if (nl80211_create_iface(drv, name,
9772 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009773 bss->addr, 1, NULL, NULL, 0) <
9774 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009775 return -1;
9776 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009777 linux_br_add_if(drv->global->ioctl_sock,
9778 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009779 return -1;
9780 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009781 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9782 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9783 "interface %s up", name);
9784 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009785 return i802_set_sta_vlan(priv, addr, name, 0);
9786 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009787 if (bridge_ifname)
9788 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9789 name);
9790
Jouni Malinen75ecf522011-06-27 15:19:46 -07009791 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009792 nl80211_remove_iface(drv, if_nametoindex(name));
9793 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009794 }
9795}
9796
9797
9798static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9799{
9800 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9801 struct sockaddr_ll lladdr;
9802 unsigned char buf[3000];
9803 int len;
9804 socklen_t fromlen = sizeof(lladdr);
9805
9806 len = recvfrom(sock, buf, sizeof(buf), 0,
9807 (struct sockaddr *)&lladdr, &fromlen);
9808 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009809 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9810 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009811 return;
9812 }
9813
9814 if (have_ifidx(drv, lladdr.sll_ifindex))
9815 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9816}
9817
9818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009819static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9820 struct i802_bss *bss,
9821 const char *brname, const char *ifname)
9822{
9823 int ifindex;
9824 char in_br[IFNAMSIZ];
9825
9826 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9827 ifindex = if_nametoindex(brname);
9828 if (ifindex == 0) {
9829 /*
9830 * Bridge was configured, but the bridge device does
9831 * not exist. Try to add it now.
9832 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009833 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009834 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9835 "bridge interface %s: %s",
9836 brname, strerror(errno));
9837 return -1;
9838 }
9839 bss->added_bridge = 1;
9840 add_ifidx(drv, if_nametoindex(brname));
9841 }
9842
9843 if (linux_br_get(in_br, ifname) == 0) {
9844 if (os_strcmp(in_br, brname) == 0)
9845 return 0; /* already in the bridge */
9846
9847 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9848 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009849 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9850 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009851 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9852 "remove interface %s from bridge "
9853 "%s: %s",
9854 ifname, brname, strerror(errno));
9855 return -1;
9856 }
9857 }
9858
9859 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9860 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009861 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009862 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9863 "into bridge %s: %s",
9864 ifname, brname, strerror(errno));
9865 return -1;
9866 }
9867 bss->added_if_into_bridge = 1;
9868
9869 return 0;
9870}
9871
9872
9873static void *i802_init(struct hostapd_data *hapd,
9874 struct wpa_init_params *params)
9875{
9876 struct wpa_driver_nl80211_data *drv;
9877 struct i802_bss *bss;
9878 size_t i;
9879 char brname[IFNAMSIZ];
9880 int ifindex, br_ifindex;
9881 int br_added = 0;
9882
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009883 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9884 params->global_priv, 1,
9885 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009886 if (bss == NULL)
9887 return NULL;
9888
9889 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009891 if (linux_br_get(brname, params->ifname) == 0) {
9892 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9893 params->ifname, brname);
9894 br_ifindex = if_nametoindex(brname);
9895 } else {
9896 brname[0] = '\0';
9897 br_ifindex = 0;
9898 }
9899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009900 for (i = 0; i < params->num_bridge; i++) {
9901 if (params->bridge[i]) {
9902 ifindex = if_nametoindex(params->bridge[i]);
9903 if (ifindex)
9904 add_ifidx(drv, ifindex);
9905 if (ifindex == br_ifindex)
9906 br_added = 1;
9907 }
9908 }
9909 if (!br_added && br_ifindex &&
9910 (params->num_bridge == 0 || !params->bridge[0]))
9911 add_ifidx(drv, br_ifindex);
9912
9913 /* start listening for EAPOL on the default AP interface */
9914 add_ifidx(drv, drv->ifindex);
9915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009916 if (params->num_bridge && params->bridge[0] &&
9917 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9918 goto failed;
9919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009920 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9921 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009922 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9923 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009924 goto failed;
9925 }
9926
9927 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9928 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009929 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009930 goto failed;
9931 }
9932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009933 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9934 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009935 goto failed;
9936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009937 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009939 return bss;
9940
9941failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009942 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009943 return NULL;
9944}
9945
9946
9947static void i802_deinit(void *priv)
9948{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009949 struct i802_bss *bss = priv;
9950 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009951}
9952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009953
9954static enum nl80211_iftype wpa_driver_nl80211_if_type(
9955 enum wpa_driver_if_type type)
9956{
9957 switch (type) {
9958 case WPA_IF_STATION:
9959 return NL80211_IFTYPE_STATION;
9960 case WPA_IF_P2P_CLIENT:
9961 case WPA_IF_P2P_GROUP:
9962 return NL80211_IFTYPE_P2P_CLIENT;
9963 case WPA_IF_AP_VLAN:
9964 return NL80211_IFTYPE_AP_VLAN;
9965 case WPA_IF_AP_BSS:
9966 return NL80211_IFTYPE_AP;
9967 case WPA_IF_P2P_GO:
9968 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009969 case WPA_IF_P2P_DEVICE:
9970 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009971 }
9972 return -1;
9973}
9974
9975
9976#ifdef CONFIG_P2P
9977
9978static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9979{
9980 struct wpa_driver_nl80211_data *drv;
9981 dl_list_for_each(drv, &global->interfaces,
9982 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009983 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009984 return 1;
9985 }
9986 return 0;
9987}
9988
9989
9990static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9991 u8 *new_addr)
9992{
9993 unsigned int idx;
9994
9995 if (!drv->global)
9996 return -1;
9997
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009998 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009999 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010000 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010001 new_addr[0] ^= idx << 2;
10002 if (!nl80211_addr_in_use(drv->global, new_addr))
10003 break;
10004 }
10005 if (idx == 64)
10006 return -1;
10007
10008 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
10009 MACSTR, MAC2STR(new_addr));
10010
10011 return 0;
10012}
10013
10014#endif /* CONFIG_P2P */
10015
10016
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010017struct wdev_info {
10018 u64 wdev_id;
10019 int wdev_id_set;
10020 u8 macaddr[ETH_ALEN];
10021};
10022
10023static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
10024{
10025 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10026 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10027 struct wdev_info *wi = arg;
10028
10029 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10030 genlmsg_attrlen(gnlh, 0), NULL);
10031 if (tb[NL80211_ATTR_WDEV]) {
10032 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
10033 wi->wdev_id_set = 1;
10034 }
10035
10036 if (tb[NL80211_ATTR_MAC])
10037 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
10038 ETH_ALEN);
10039
10040 return NL_SKIP;
10041}
10042
10043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010044static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
10045 const char *ifname, const u8 *addr,
10046 void *bss_ctx, void **drv_priv,
10047 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010048 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010049{
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010050 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010051 struct i802_bss *bss = priv;
10052 struct wpa_driver_nl80211_data *drv = bss->drv;
10053 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010054 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010055
10056 if (addr)
10057 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010058 nlmode = wpa_driver_nl80211_if_type(type);
10059 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
10060 struct wdev_info p2pdev_info;
10061
10062 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
10063 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
10064 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010065 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010066 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
10067 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10068 ifname);
10069 return -1;
10070 }
10071
10072 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10073 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10074 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10075 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10076 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10077 ifname,
10078 (long long unsigned int) p2pdev_info.wdev_id);
10079 } else {
10080 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010081 0, NULL, NULL, use_existing);
10082 if (use_existing && ifidx == -ENFILE) {
10083 added = 0;
10084 ifidx = if_nametoindex(ifname);
10085 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010086 return -1;
10087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010088 }
10089
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010090 if (!addr) {
10091 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10092 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10093 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10094 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010095 if (added)
10096 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010097 return -1;
10098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010099 }
10100
10101#ifdef CONFIG_P2P
10102 if (!addr &&
10103 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10104 type == WPA_IF_P2P_GO)) {
10105 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010106 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010107
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010108 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010109 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010110 if (added)
10111 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010112 return -1;
10113 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010114 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010115 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10116 "for P2P group interface");
10117 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010118 if (added)
10119 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010120 return -1;
10121 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010122 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010123 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010124 if (added)
10125 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010126 return -1;
10127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010128 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010129 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010130 }
10131#endif /* CONFIG_P2P */
10132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010133 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010134 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10135 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010136 if (added)
10137 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010138 return -1;
10139 }
10140
10141 if (bridge &&
10142 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10143 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10144 "interface %s to a bridge %s",
10145 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010146 if (added)
10147 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010148 os_free(new_bss);
10149 return -1;
10150 }
10151
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010152 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10153 {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010154 if (added)
10155 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010156 os_free(new_bss);
10157 return -1;
10158 }
10159 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010160 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010161 new_bss->ifindex = ifidx;
10162 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010163 new_bss->next = drv->first_bss->next;
10164 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010165 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010166 new_bss->added_if = added;
10167 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010168 if (drv_priv)
10169 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010170 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010171
10172 /* Subscribe management frames for this WPA_IF_AP_BSS */
10173 if (nl80211_setup_ap(new_bss))
10174 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010175 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010177 if (drv->global)
10178 drv->global->if_add_ifindex = ifidx;
10179
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010180 /*
10181 * Some virtual interfaces need to process EAPOL packets and events on
10182 * the parent interface. This is used mainly with hostapd.
10183 */
10184 if (ifidx > 0 &&
10185 (drv->hostapd ||
10186 nlmode == NL80211_IFTYPE_AP_VLAN ||
10187 nlmode == NL80211_IFTYPE_WDS ||
10188 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010189 add_ifidx(drv, ifidx);
10190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010191 return 0;
10192}
10193
10194
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010195static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010196 enum wpa_driver_if_type type,
10197 const char *ifname)
10198{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010199 struct wpa_driver_nl80211_data *drv = bss->drv;
10200 int ifindex = if_nametoindex(ifname);
10201
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010202 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10203 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010204 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010205 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -070010206 else if (ifindex > 0 && !bss->added_if) {
10207 struct wpa_driver_nl80211_data *drv2;
10208 dl_list_for_each(drv2, &drv->global->interfaces,
10209 struct wpa_driver_nl80211_data, list)
10210 del_ifidx(drv2, ifindex);
10211 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010212
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010213 if (type != WPA_IF_AP_BSS)
10214 return 0;
10215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010216 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010217 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10218 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010219 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10220 "interface %s from bridge %s: %s",
10221 bss->ifname, bss->brname, strerror(errno));
10222 }
10223 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010224 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010225 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10226 "bridge %s: %s",
10227 bss->brname, strerror(errno));
10228 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010229
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010230 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010231 struct i802_bss *tbss;
10232
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010233 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10234 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010235 if (tbss->next == bss) {
10236 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010237 /* Unsubscribe management frames */
10238 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010239 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010240 if (!bss->added_if)
10241 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010242 os_free(bss);
10243 bss = NULL;
10244 break;
10245 }
10246 }
10247 if (bss)
10248 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10249 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010250 } else {
10251 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10252 nl80211_teardown_ap(bss);
10253 if (!bss->added_if && !drv->first_bss->next)
10254 wpa_driver_nl80211_del_beacon(drv);
10255 nl80211_destroy_bss(bss);
10256 if (!bss->added_if)
10257 i802_set_iface_flags(bss, 0);
10258 if (drv->first_bss->next) {
10259 drv->first_bss = drv->first_bss->next;
10260 drv->ctx = drv->first_bss->ctx;
10261 os_free(bss);
10262 } else {
10263 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10264 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010266
10267 return 0;
10268}
10269
10270
10271static int cookie_handler(struct nl_msg *msg, void *arg)
10272{
10273 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10274 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10275 u64 *cookie = arg;
10276 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10277 genlmsg_attrlen(gnlh, 0), NULL);
10278 if (tb[NL80211_ATTR_COOKIE])
10279 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10280 return NL_SKIP;
10281}
10282
10283
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010284static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010285 unsigned int freq, unsigned int wait,
10286 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010287 u64 *cookie_out, int no_cck, int no_ack,
10288 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010289{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010290 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010291 struct nl_msg *msg;
10292 u64 cookie;
10293 int ret = -1;
10294
10295 msg = nlmsg_alloc();
10296 if (!msg)
10297 return -1;
10298
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010299 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010300 "no_ack=%d offchanok=%d",
10301 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010302 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010303 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010304
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010305 if (nl80211_set_iface_id(msg, bss) < 0)
10306 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010307 if (freq)
10308 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010309 if (wait)
10310 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010311 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10312 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010313 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10314 if (no_cck)
10315 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10316 if (no_ack)
10317 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010319 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10320
10321 cookie = 0;
10322 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10323 msg = NULL;
10324 if (ret) {
10325 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010326 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10327 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010328 goto nla_put_failure;
10329 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010330 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010331 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10332 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010333
10334 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010335 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010336
10337nla_put_failure:
10338 nlmsg_free(msg);
10339 return ret;
10340}
10341
10342
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010343static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10344 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010345 unsigned int wait_time,
10346 const u8 *dst, const u8 *src,
10347 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010348 const u8 *data, size_t data_len,
10349 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010350{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010351 struct wpa_driver_nl80211_data *drv = bss->drv;
10352 int ret = -1;
10353 u8 *buf;
10354 struct ieee80211_hdr *hdr;
10355
10356 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010357 "freq=%u MHz wait=%d ms no_cck=%d)",
10358 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010359
10360 buf = os_zalloc(24 + data_len);
10361 if (buf == NULL)
10362 return ret;
10363 os_memcpy(buf + 24, data, data_len);
10364 hdr = (struct ieee80211_hdr *) buf;
10365 hdr->frame_control =
10366 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10367 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10368 os_memcpy(hdr->addr2, src, ETH_ALEN);
10369 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10370
Dmitry Shmidt56052862013-10-04 10:23:25 -070010371 if (is_ap_interface(drv->nlmode) &&
10372 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10373 (int) freq == bss->freq || drv->device_ap_sme ||
10374 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010375 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10376 0, freq, no_cck, 1,
10377 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010378 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010379 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010380 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010381 &drv->send_action_cookie,
10382 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010383
10384 os_free(buf);
10385 return ret;
10386}
10387
10388
10389static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10390{
10391 struct i802_bss *bss = priv;
10392 struct wpa_driver_nl80211_data *drv = bss->drv;
10393 struct nl_msg *msg;
10394 int ret;
10395
10396 msg = nlmsg_alloc();
10397 if (!msg)
10398 return;
10399
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010400 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10401 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010402 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010403
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010404 if (nl80211_set_iface_id(msg, bss) < 0)
10405 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010406 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10407
10408 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10409 msg = NULL;
10410 if (ret)
10411 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10412 "(%s)", ret, strerror(-ret));
10413
10414 nla_put_failure:
10415 nlmsg_free(msg);
10416}
10417
10418
10419static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10420 unsigned int duration)
10421{
10422 struct i802_bss *bss = priv;
10423 struct wpa_driver_nl80211_data *drv = bss->drv;
10424 struct nl_msg *msg;
10425 int ret;
10426 u64 cookie;
10427
10428 msg = nlmsg_alloc();
10429 if (!msg)
10430 return -1;
10431
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010432 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010433
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010434 if (nl80211_set_iface_id(msg, bss) < 0)
10435 goto nla_put_failure;
10436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010437 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10438 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10439
10440 cookie = 0;
10441 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010442 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010443 if (ret == 0) {
10444 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10445 "0x%llx for freq=%u MHz duration=%u",
10446 (long long unsigned int) cookie, freq, duration);
10447 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010448 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010449 return 0;
10450 }
10451 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10452 "(freq=%d duration=%u): %d (%s)",
10453 freq, duration, ret, strerror(-ret));
10454nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010455 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010456 return -1;
10457}
10458
10459
10460static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10461{
10462 struct i802_bss *bss = priv;
10463 struct wpa_driver_nl80211_data *drv = bss->drv;
10464 struct nl_msg *msg;
10465 int ret;
10466
10467 if (!drv->pending_remain_on_chan) {
10468 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10469 "to cancel");
10470 return -1;
10471 }
10472
10473 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10474 "0x%llx",
10475 (long long unsigned int) drv->remain_on_chan_cookie);
10476
10477 msg = nlmsg_alloc();
10478 if (!msg)
10479 return -1;
10480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010481 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010482
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010483 if (nl80211_set_iface_id(msg, bss) < 0)
10484 goto nla_put_failure;
10485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010486 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10487
10488 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010489 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010490 if (ret == 0)
10491 return 0;
10492 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10493 "%d (%s)", ret, strerror(-ret));
10494nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010495 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010496 return -1;
10497}
10498
10499
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010500static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010501{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010502 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010504 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010505 if (bss->nl_preq && drv->device_ap_sme &&
10506 is_ap_interface(drv->nlmode)) {
10507 /*
10508 * Do not disable Probe Request reporting that was
10509 * enabled in nl80211_setup_ap().
10510 */
10511 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10512 "Probe Request reporting nl_preq=%p while "
10513 "in AP mode", bss->nl_preq);
10514 } else if (bss->nl_preq) {
10515 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10516 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010517 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010518 }
10519 return 0;
10520 }
10521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010522 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010523 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010524 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010525 return 0;
10526 }
10527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010528 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10529 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010530 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010531 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10532 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010534 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010535 (WLAN_FC_TYPE_MGMT << 2) |
10536 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010537 NULL, 0) < 0)
10538 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010539
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010540 nl80211_register_eloop_read(&bss->nl_preq,
10541 wpa_driver_nl80211_event_receive,
10542 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010543
10544 return 0;
10545
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010546 out_err:
10547 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010548 return -1;
10549}
10550
10551
10552static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10553 int ifindex, int disabled)
10554{
10555 struct nl_msg *msg;
10556 struct nlattr *bands, *band;
10557 int ret;
10558
10559 msg = nlmsg_alloc();
10560 if (!msg)
10561 return -1;
10562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010563 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010564 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10565
10566 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10567 if (!bands)
10568 goto nla_put_failure;
10569
10570 /*
10571 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10572 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10573 * rates. All 5 GHz rates are left enabled.
10574 */
10575 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10576 if (!band)
10577 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010578 if (disabled) {
10579 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10580 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010582 nla_nest_end(msg, band);
10583
10584 nla_nest_end(msg, bands);
10585
10586 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10587 msg = NULL;
10588 if (ret) {
10589 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10590 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010591 } else
10592 drv->disabled_11b_rates = disabled;
10593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010594 return ret;
10595
10596nla_put_failure:
10597 nlmsg_free(msg);
10598 return -1;
10599}
10600
10601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010602static int wpa_driver_nl80211_deinit_ap(void *priv)
10603{
10604 struct i802_bss *bss = priv;
10605 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010606 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010607 return -1;
10608 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010609
10610 /*
10611 * If the P2P GO interface was dynamically added, then it is
10612 * possible that the interface change to station is not possible.
10613 */
10614 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10615 return 0;
10616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010617 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010618}
10619
10620
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010621static int wpa_driver_nl80211_stop_ap(void *priv)
10622{
10623 struct i802_bss *bss = priv;
10624 struct wpa_driver_nl80211_data *drv = bss->drv;
10625 if (!is_ap_interface(drv->nlmode))
10626 return -1;
10627 wpa_driver_nl80211_del_beacon(drv);
10628 bss->beacon_set = 0;
10629 return 0;
10630}
10631
10632
Dmitry Shmidt04949592012-07-19 12:16:46 -070010633static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10634{
10635 struct i802_bss *bss = priv;
10636 struct wpa_driver_nl80211_data *drv = bss->drv;
10637 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10638 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010639
10640 /*
10641 * If the P2P Client interface was dynamically added, then it is
10642 * possible that the interface change to station is not possible.
10643 */
10644 if (bss->if_dynamic)
10645 return 0;
10646
Dmitry Shmidt04949592012-07-19 12:16:46 -070010647 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10648}
10649
10650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010651static void wpa_driver_nl80211_resume(void *priv)
10652{
10653 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010654
10655 if (i802_set_iface_flags(bss, 1))
10656 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010657}
10658
10659
10660static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10661 const u8 *ies, size_t ies_len)
10662{
10663 struct i802_bss *bss = priv;
10664 struct wpa_driver_nl80211_data *drv = bss->drv;
10665 int ret;
10666 u8 *data, *pos;
10667 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010668 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010669
10670 if (action != 1) {
10671 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10672 "action %d", action);
10673 return -1;
10674 }
10675
10676 /*
10677 * Action frame payload:
10678 * Category[1] = 6 (Fast BSS Transition)
10679 * Action[1] = 1 (Fast BSS Transition Request)
10680 * STA Address
10681 * Target AP Address
10682 * FT IEs
10683 */
10684
10685 data_len = 2 + 2 * ETH_ALEN + ies_len;
10686 data = os_malloc(data_len);
10687 if (data == NULL)
10688 return -1;
10689 pos = data;
10690 *pos++ = 0x06; /* FT Action category */
10691 *pos++ = action;
10692 os_memcpy(pos, own_addr, ETH_ALEN);
10693 pos += ETH_ALEN;
10694 os_memcpy(pos, target_ap, ETH_ALEN);
10695 pos += ETH_ALEN;
10696 os_memcpy(pos, ies, ies_len);
10697
10698 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10699 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010700 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010701 os_free(data);
10702
10703 return ret;
10704}
10705
10706
10707static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10708{
10709 struct i802_bss *bss = priv;
10710 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010711 struct nl_msg *msg;
10712 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010713 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010714
10715 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10716 "hysteresis=%d", threshold, hysteresis);
10717
10718 msg = nlmsg_alloc();
10719 if (!msg)
10720 return -1;
10721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010722 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010723
10724 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10725
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010726 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010727 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010728 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010729
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010730 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10731 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10732 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010733
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010734 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010735 msg = NULL;
10736
10737nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010738 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010739 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010740}
10741
10742
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010743static int get_channel_width(struct nl_msg *msg, void *arg)
10744{
10745 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10746 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10747 struct wpa_signal_info *sig_change = arg;
10748
10749 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10750 genlmsg_attrlen(gnlh, 0), NULL);
10751
10752 sig_change->center_frq1 = -1;
10753 sig_change->center_frq2 = -1;
10754 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10755
10756 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10757 sig_change->chanwidth = convert2width(
10758 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10759 if (tb[NL80211_ATTR_CENTER_FREQ1])
10760 sig_change->center_frq1 =
10761 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10762 if (tb[NL80211_ATTR_CENTER_FREQ2])
10763 sig_change->center_frq2 =
10764 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10765 }
10766
10767 return NL_SKIP;
10768}
10769
10770
10771static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10772 struct wpa_signal_info *sig)
10773{
10774 struct nl_msg *msg;
10775
10776 msg = nlmsg_alloc();
10777 if (!msg)
10778 return -ENOMEM;
10779
10780 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10781 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10782
10783 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10784
10785nla_put_failure:
10786 nlmsg_free(msg);
10787 return -ENOBUFS;
10788}
10789
10790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010791static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10792{
10793 struct i802_bss *bss = priv;
10794 struct wpa_driver_nl80211_data *drv = bss->drv;
10795 int res;
10796
10797 os_memset(si, 0, sizeof(*si));
10798 res = nl80211_get_link_signal(drv, si);
10799 if (res != 0)
10800 return res;
10801
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010802 res = nl80211_get_channel_width(drv, si);
10803 if (res != 0)
10804 return res;
10805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010806 return nl80211_get_link_noise(drv, si);
10807}
10808
10809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010810static int wpa_driver_nl80211_shared_freq(void *priv)
10811{
10812 struct i802_bss *bss = priv;
10813 struct wpa_driver_nl80211_data *drv = bss->drv;
10814 struct wpa_driver_nl80211_data *driver;
10815 int freq = 0;
10816
10817 /*
10818 * If the same PHY is in connected state with some other interface,
10819 * then retrieve the assoc freq.
10820 */
10821 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10822 drv->phyname);
10823
10824 dl_list_for_each(driver, &drv->global->interfaces,
10825 struct wpa_driver_nl80211_data, list) {
10826 if (drv == driver ||
10827 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010828 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010829 continue;
10830
10831 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10832 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010833 driver->phyname, driver->first_bss->ifname,
10834 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010835 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010836 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010837 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010838 freq = nl80211_get_assoc_freq(driver);
10839 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10840 drv->phyname, freq);
10841 }
10842
10843 if (!freq)
10844 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10845 "PHY (%s) in associated state", drv->phyname);
10846
10847 return freq;
10848}
10849
10850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010851static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10852 int encrypt)
10853{
10854 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010855 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10856 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010857}
10858
10859
10860static int nl80211_set_param(void *priv, const char *param)
10861{
10862 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10863 if (param == NULL)
10864 return 0;
10865
10866#ifdef CONFIG_P2P
10867 if (os_strstr(param, "use_p2p_group_interface=1")) {
10868 struct i802_bss *bss = priv;
10869 struct wpa_driver_nl80211_data *drv = bss->drv;
10870
10871 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10872 "interface");
10873 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10874 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10875 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010876
10877 if (os_strstr(param, "p2p_device=1")) {
10878 struct i802_bss *bss = priv;
10879 struct wpa_driver_nl80211_data *drv = bss->drv;
10880 drv->allow_p2p_device = 1;
10881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010882#endif /* CONFIG_P2P */
10883
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010884 if (os_strstr(param, "use_monitor=1")) {
10885 struct i802_bss *bss = priv;
10886 struct wpa_driver_nl80211_data *drv = bss->drv;
10887 drv->use_monitor = 1;
10888 }
10889
10890 if (os_strstr(param, "force_connect_cmd=1")) {
10891 struct i802_bss *bss = priv;
10892 struct wpa_driver_nl80211_data *drv = bss->drv;
10893 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10894 }
10895
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010896 if (os_strstr(param, "no_offchannel_tx=1")) {
10897 struct i802_bss *bss = priv;
10898 struct wpa_driver_nl80211_data *drv = bss->drv;
10899 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10900 drv->test_use_roc_tx = 1;
10901 }
10902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010903 return 0;
10904}
10905
10906
10907static void * nl80211_global_init(void)
10908{
10909 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010910 struct netlink_config *cfg;
10911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010912 global = os_zalloc(sizeof(*global));
10913 if (global == NULL)
10914 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010915 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010916 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010917 global->if_add_ifindex = -1;
10918
10919 cfg = os_zalloc(sizeof(*cfg));
10920 if (cfg == NULL)
10921 goto err;
10922
10923 cfg->ctx = global;
10924 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10925 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10926 global->netlink = netlink_init(cfg);
10927 if (global->netlink == NULL) {
10928 os_free(cfg);
10929 goto err;
10930 }
10931
10932 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10933 goto err;
10934
10935 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10936 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010937 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10938 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010939 goto err;
10940 }
10941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010942 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010943
10944err:
10945 nl80211_global_deinit(global);
10946 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010947}
10948
10949
10950static void nl80211_global_deinit(void *priv)
10951{
10952 struct nl80211_global *global = priv;
10953 if (global == NULL)
10954 return;
10955 if (!dl_list_empty(&global->interfaces)) {
10956 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10957 "nl80211_global_deinit",
10958 dl_list_len(&global->interfaces));
10959 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010960
10961 if (global->netlink)
10962 netlink_deinit(global->netlink);
10963
10964 nl_destroy_handles(&global->nl);
10965
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010966 if (global->nl_event)
10967 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010968
10969 nl_cb_put(global->nl_cb);
10970
10971 if (global->ioctl_sock >= 0)
10972 close(global->ioctl_sock);
10973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010974 os_free(global);
10975}
10976
10977
10978static const char * nl80211_get_radio_name(void *priv)
10979{
10980 struct i802_bss *bss = priv;
10981 struct wpa_driver_nl80211_data *drv = bss->drv;
10982 return drv->phyname;
10983}
10984
10985
Jouni Malinen75ecf522011-06-27 15:19:46 -070010986static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10987 const u8 *pmkid)
10988{
10989 struct nl_msg *msg;
10990
10991 msg = nlmsg_alloc();
10992 if (!msg)
10993 return -ENOMEM;
10994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010995 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010996
10997 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10998 if (pmkid)
10999 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
11000 if (bssid)
11001 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
11002
11003 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11004 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011005 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011006 return -ENOBUFS;
11007}
11008
11009
11010static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11011{
11012 struct i802_bss *bss = priv;
11013 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
11014 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
11015}
11016
11017
11018static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11019{
11020 struct i802_bss *bss = priv;
11021 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
11022 MAC2STR(bssid));
11023 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
11024}
11025
11026
11027static int nl80211_flush_pmkid(void *priv)
11028{
11029 struct i802_bss *bss = priv;
11030 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
11031 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
11032}
11033
11034
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011035static void clean_survey_results(struct survey_results *survey_results)
11036{
11037 struct freq_survey *survey, *tmp;
11038
11039 if (dl_list_empty(&survey_results->survey_list))
11040 return;
11041
11042 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
11043 struct freq_survey, list) {
11044 dl_list_del(&survey->list);
11045 os_free(survey);
11046 }
11047}
11048
11049
11050static void add_survey(struct nlattr **sinfo, u32 ifidx,
11051 struct dl_list *survey_list)
11052{
11053 struct freq_survey *survey;
11054
11055 survey = os_zalloc(sizeof(struct freq_survey));
11056 if (!survey)
11057 return;
11058
11059 survey->ifidx = ifidx;
11060 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11061 survey->filled = 0;
11062
11063 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11064 survey->nf = (int8_t)
11065 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11066 survey->filled |= SURVEY_HAS_NF;
11067 }
11068
11069 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11070 survey->channel_time =
11071 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11072 survey->filled |= SURVEY_HAS_CHAN_TIME;
11073 }
11074
11075 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11076 survey->channel_time_busy =
11077 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11078 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11079 }
11080
11081 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11082 survey->channel_time_rx =
11083 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11084 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11085 }
11086
11087 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11088 survey->channel_time_tx =
11089 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11090 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11091 }
11092
11093 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)",
11094 survey->freq,
11095 survey->nf,
11096 (unsigned long int) survey->channel_time,
11097 (unsigned long int) survey->channel_time_busy,
11098 (unsigned long int) survey->channel_time_tx,
11099 (unsigned long int) survey->channel_time_rx,
11100 survey->filled);
11101
11102 dl_list_add_tail(survey_list, &survey->list);
11103}
11104
11105
11106static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11107 unsigned int freq_filter)
11108{
11109 if (!freq_filter)
11110 return 1;
11111
11112 return freq_filter == surveyed_freq;
11113}
11114
11115
11116static int survey_handler(struct nl_msg *msg, void *arg)
11117{
11118 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11119 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11120 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11121 struct survey_results *survey_results;
11122 u32 surveyed_freq = 0;
11123 u32 ifidx;
11124
11125 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11126 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11127 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11128 };
11129
11130 survey_results = (struct survey_results *) arg;
11131
11132 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11133 genlmsg_attrlen(gnlh, 0), NULL);
11134
Dmitry Shmidt97672262014-02-03 13:02:54 -080011135 if (!tb[NL80211_ATTR_IFINDEX])
11136 return NL_SKIP;
11137
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011138 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11139
11140 if (!tb[NL80211_ATTR_SURVEY_INFO])
11141 return NL_SKIP;
11142
11143 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11144 tb[NL80211_ATTR_SURVEY_INFO],
11145 survey_policy))
11146 return NL_SKIP;
11147
11148 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11149 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11150 return NL_SKIP;
11151 }
11152
11153 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11154
11155 if (!check_survey_ok(sinfo, surveyed_freq,
11156 survey_results->freq_filter))
11157 return NL_SKIP;
11158
11159 if (survey_results->freq_filter &&
11160 survey_results->freq_filter != surveyed_freq) {
11161 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11162 surveyed_freq);
11163 return NL_SKIP;
11164 }
11165
11166 add_survey(sinfo, ifidx, &survey_results->survey_list);
11167
11168 return NL_SKIP;
11169}
11170
11171
11172static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11173{
11174 struct i802_bss *bss = priv;
11175 struct wpa_driver_nl80211_data *drv = bss->drv;
11176 struct nl_msg *msg;
11177 int err = -ENOBUFS;
11178 union wpa_event_data data;
11179 struct survey_results *survey_results;
11180
11181 os_memset(&data, 0, sizeof(data));
11182 survey_results = &data.survey_results;
11183
11184 dl_list_init(&survey_results->survey_list);
11185
11186 msg = nlmsg_alloc();
11187 if (!msg)
11188 goto nla_put_failure;
11189
11190 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11191
11192 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11193
11194 if (freq)
11195 data.survey_results.freq_filter = freq;
11196
11197 do {
11198 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11199 err = send_and_recv_msgs(drv, msg, survey_handler,
11200 survey_results);
11201 } while (err > 0);
11202
11203 if (err) {
11204 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11205 goto out_clean;
11206 }
11207
11208 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11209
11210out_clean:
11211 clean_survey_results(survey_results);
11212nla_put_failure:
11213 return err;
11214}
11215
11216
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011217static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11218 const u8 *replay_ctr)
11219{
11220 struct i802_bss *bss = priv;
11221 struct wpa_driver_nl80211_data *drv = bss->drv;
11222 struct nlattr *replay_nested;
11223 struct nl_msg *msg;
11224
11225 msg = nlmsg_alloc();
11226 if (!msg)
11227 return;
11228
11229 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11230
11231 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11232
11233 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11234 if (!replay_nested)
11235 goto nla_put_failure;
11236
11237 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11238 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11239 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11240 replay_ctr);
11241
11242 nla_nest_end(msg, replay_nested);
11243
11244 send_and_recv_msgs(drv, msg, NULL, NULL);
11245 return;
11246 nla_put_failure:
11247 nlmsg_free(msg);
11248}
11249
11250
11251static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11252 const u8 *addr, int qos)
11253{
11254 /* send data frame to poll STA and check whether
11255 * this frame is ACKed */
11256 struct {
11257 struct ieee80211_hdr hdr;
11258 u16 qos_ctl;
11259 } STRUCT_PACKED nulldata;
11260 size_t size;
11261
11262 /* Send data frame to poll STA and check whether this frame is ACKed */
11263
11264 os_memset(&nulldata, 0, sizeof(nulldata));
11265
11266 if (qos) {
11267 nulldata.hdr.frame_control =
11268 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11269 WLAN_FC_STYPE_QOS_NULL);
11270 size = sizeof(nulldata);
11271 } else {
11272 nulldata.hdr.frame_control =
11273 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11274 WLAN_FC_STYPE_NULLFUNC);
11275 size = sizeof(struct ieee80211_hdr);
11276 }
11277
11278 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11279 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11280 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11281 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11282
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011283 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11284 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011285 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11286 "send poll frame");
11287}
11288
11289static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11290 int qos)
11291{
11292 struct i802_bss *bss = priv;
11293 struct wpa_driver_nl80211_data *drv = bss->drv;
11294 struct nl_msg *msg;
11295
11296 if (!drv->poll_command_supported) {
11297 nl80211_send_null_frame(bss, own_addr, addr, qos);
11298 return;
11299 }
11300
11301 msg = nlmsg_alloc();
11302 if (!msg)
11303 return;
11304
11305 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11306
11307 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11308 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11309
11310 send_and_recv_msgs(drv, msg, NULL, NULL);
11311 return;
11312 nla_put_failure:
11313 nlmsg_free(msg);
11314}
11315
11316
11317static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11318{
11319 struct nl_msg *msg;
11320
11321 msg = nlmsg_alloc();
11322 if (!msg)
11323 return -ENOMEM;
11324
11325 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11326 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11327 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11328 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11329 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11330nla_put_failure:
11331 nlmsg_free(msg);
11332 return -ENOBUFS;
11333}
11334
11335
11336static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11337 int ctwindow)
11338{
11339 struct i802_bss *bss = priv;
11340
11341 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11342 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11343
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011344 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011345#ifdef ANDROID_P2P
11346 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011347#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011348 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011349#endif /* ANDROID_P2P */
11350 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011351
11352 if (legacy_ps == -1)
11353 return 0;
11354 if (legacy_ps != 0 && legacy_ps != 1)
11355 return -1; /* Not yet supported */
11356
11357 return nl80211_set_power_save(bss, legacy_ps);
11358}
11359
11360
Dmitry Shmidt051af732013-10-22 13:52:46 -070011361static int nl80211_start_radar_detection(void *priv,
11362 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011363{
11364 struct i802_bss *bss = priv;
11365 struct wpa_driver_nl80211_data *drv = bss->drv;
11366 struct nl_msg *msg;
11367 int ret;
11368
Dmitry Shmidt051af732013-10-22 13:52:46 -070011369 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)",
11370 freq->freq, freq->ht_enabled, freq->vht_enabled,
11371 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11372
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011373 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11374 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11375 "detection");
11376 return -1;
11377 }
11378
11379 msg = nlmsg_alloc();
11380 if (!msg)
11381 return -1;
11382
11383 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11384 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070011385 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011386
Dmitry Shmidt051af732013-10-22 13:52:46 -070011387 if (freq->vht_enabled) {
11388 switch (freq->bandwidth) {
11389 case 20:
11390 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11391 NL80211_CHAN_WIDTH_20);
11392 break;
11393 case 40:
11394 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11395 NL80211_CHAN_WIDTH_40);
11396 break;
11397 case 80:
11398 if (freq->center_freq2)
11399 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11400 NL80211_CHAN_WIDTH_80P80);
11401 else
11402 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11403 NL80211_CHAN_WIDTH_80);
11404 break;
11405 case 160:
11406 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11407 NL80211_CHAN_WIDTH_160);
11408 break;
11409 default:
11410 return -1;
11411 }
11412 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11413 if (freq->center_freq2)
11414 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11415 freq->center_freq2);
11416 } else if (freq->ht_enabled) {
11417 switch (freq->sec_channel_offset) {
11418 case -1:
11419 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11420 NL80211_CHAN_HT40MINUS);
11421 break;
11422 case 1:
11423 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11424 NL80211_CHAN_HT40PLUS);
11425 break;
11426 default:
11427 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11428 NL80211_CHAN_HT20);
11429 break;
11430 }
11431 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011432
11433 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11434 if (ret == 0)
11435 return 0;
11436 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11437 "%d (%s)", ret, strerror(-ret));
11438nla_put_failure:
11439 return -1;
11440}
11441
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011442#ifdef CONFIG_TDLS
11443
11444static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11445 u8 dialog_token, u16 status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011446 u32 peer_capab, const u8 *buf, size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011447{
11448 struct i802_bss *bss = priv;
11449 struct wpa_driver_nl80211_data *drv = bss->drv;
11450 struct nl_msg *msg;
11451
11452 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11453 return -EOPNOTSUPP;
11454
11455 if (!dst)
11456 return -EINVAL;
11457
11458 msg = nlmsg_alloc();
11459 if (!msg)
11460 return -ENOMEM;
11461
11462 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11463 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11464 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11465 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11466 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11467 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011468 if (peer_capab) {
11469 /*
11470 * The internal enum tdls_peer_capability definition is
11471 * currently identical with the nl80211 enum
11472 * nl80211_tdls_peer_capability, so no conversion is needed
11473 * here.
11474 */
11475 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11476 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011477 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11478
11479 return send_and_recv_msgs(drv, msg, NULL, NULL);
11480
11481nla_put_failure:
11482 nlmsg_free(msg);
11483 return -ENOBUFS;
11484}
11485
11486
11487static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11488{
11489 struct i802_bss *bss = priv;
11490 struct wpa_driver_nl80211_data *drv = bss->drv;
11491 struct nl_msg *msg;
11492 enum nl80211_tdls_operation nl80211_oper;
11493
11494 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11495 return -EOPNOTSUPP;
11496
11497 switch (oper) {
11498 case TDLS_DISCOVERY_REQ:
11499 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11500 break;
11501 case TDLS_SETUP:
11502 nl80211_oper = NL80211_TDLS_SETUP;
11503 break;
11504 case TDLS_TEARDOWN:
11505 nl80211_oper = NL80211_TDLS_TEARDOWN;
11506 break;
11507 case TDLS_ENABLE_LINK:
11508 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11509 break;
11510 case TDLS_DISABLE_LINK:
11511 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11512 break;
11513 case TDLS_ENABLE:
11514 return 0;
11515 case TDLS_DISABLE:
11516 return 0;
11517 default:
11518 return -EINVAL;
11519 }
11520
11521 msg = nlmsg_alloc();
11522 if (!msg)
11523 return -ENOMEM;
11524
11525 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11526 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11527 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11528 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11529
11530 return send_and_recv_msgs(drv, msg, NULL, NULL);
11531
11532nla_put_failure:
11533 nlmsg_free(msg);
11534 return -ENOBUFS;
11535}
11536
11537#endif /* CONFIG TDLS */
11538
11539
11540#ifdef ANDROID
11541
11542typedef struct android_wifi_priv_cmd {
11543 char *buf;
11544 int used_len;
11545 int total_len;
11546} android_wifi_priv_cmd;
11547
11548static int drv_errors = 0;
11549
11550static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11551{
11552 drv_errors++;
11553 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11554 drv_errors = 0;
11555 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11556 }
11557}
11558
11559
11560static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11561{
11562 struct wpa_driver_nl80211_data *drv = bss->drv;
11563 struct ifreq ifr;
11564 android_wifi_priv_cmd priv_cmd;
11565 char buf[MAX_DRV_CMD_SIZE];
11566 int ret;
11567
11568 os_memset(&ifr, 0, sizeof(ifr));
11569 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11570 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11571
11572 os_memset(buf, 0, sizeof(buf));
11573 os_strlcpy(buf, cmd, sizeof(buf));
11574
11575 priv_cmd.buf = buf;
11576 priv_cmd.used_len = sizeof(buf);
11577 priv_cmd.total_len = sizeof(buf);
11578 ifr.ifr_data = &priv_cmd;
11579
11580 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11581 if (ret < 0) {
11582 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11583 __func__);
11584 wpa_driver_send_hang_msg(drv);
11585 return ret;
11586 }
11587
11588 drv_errors = 0;
11589 return 0;
11590}
11591
11592
11593static int android_pno_start(struct i802_bss *bss,
11594 struct wpa_driver_scan_params *params)
11595{
11596 struct wpa_driver_nl80211_data *drv = bss->drv;
11597 struct ifreq ifr;
11598 android_wifi_priv_cmd priv_cmd;
11599 int ret = 0, i = 0, bp;
11600 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11601
11602 bp = WEXT_PNOSETUP_HEADER_SIZE;
11603 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11604 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11605 buf[bp++] = WEXT_PNO_TLV_VERSION;
11606 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11607 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11608
11609 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11610 /* Check that there is enough space needed for 1 more SSID, the
11611 * other sections and null termination */
11612 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11613 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11614 break;
11615 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11616 params->ssids[i].ssid,
11617 params->ssids[i].ssid_len);
11618 buf[bp++] = WEXT_PNO_SSID_SECTION;
11619 buf[bp++] = params->ssids[i].ssid_len;
11620 os_memcpy(&buf[bp], params->ssids[i].ssid,
11621 params->ssids[i].ssid_len);
11622 bp += params->ssids[i].ssid_len;
11623 i++;
11624 }
11625
11626 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11627 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11628 WEXT_PNO_SCAN_INTERVAL);
11629 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11630
11631 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11632 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11633 WEXT_PNO_REPEAT);
11634 bp += WEXT_PNO_REPEAT_LENGTH;
11635
11636 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11637 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11638 WEXT_PNO_MAX_REPEAT);
11639 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11640
11641 memset(&ifr, 0, sizeof(ifr));
11642 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011643 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011644
11645 priv_cmd.buf = buf;
11646 priv_cmd.used_len = bp;
11647 priv_cmd.total_len = bp;
11648 ifr.ifr_data = &priv_cmd;
11649
11650 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11651
11652 if (ret < 0) {
11653 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11654 ret);
11655 wpa_driver_send_hang_msg(drv);
11656 return ret;
11657 }
11658
11659 drv_errors = 0;
11660
11661 return android_priv_cmd(bss, "PNOFORCE 1");
11662}
11663
11664
11665static int android_pno_stop(struct i802_bss *bss)
11666{
11667 return android_priv_cmd(bss, "PNOFORCE 0");
11668}
11669
11670#endif /* ANDROID */
11671
11672
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011673static int driver_nl80211_set_key(const char *ifname, void *priv,
11674 enum wpa_alg alg, const u8 *addr,
11675 int key_idx, int set_tx,
11676 const u8 *seq, size_t seq_len,
11677 const u8 *key, size_t key_len)
11678{
11679 struct i802_bss *bss = priv;
11680 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11681 set_tx, seq, seq_len, key, key_len);
11682}
11683
11684
11685static int driver_nl80211_scan2(void *priv,
11686 struct wpa_driver_scan_params *params)
11687{
11688 struct i802_bss *bss = priv;
11689 return wpa_driver_nl80211_scan(bss, params);
11690}
11691
11692
11693static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11694 int reason_code)
11695{
11696 struct i802_bss *bss = priv;
11697 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11698}
11699
11700
11701static int driver_nl80211_authenticate(void *priv,
11702 struct wpa_driver_auth_params *params)
11703{
11704 struct i802_bss *bss = priv;
11705 return wpa_driver_nl80211_authenticate(bss, params);
11706}
11707
11708
11709static void driver_nl80211_deinit(void *priv)
11710{
11711 struct i802_bss *bss = priv;
11712 wpa_driver_nl80211_deinit(bss);
11713}
11714
11715
11716static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11717 const char *ifname)
11718{
11719 struct i802_bss *bss = priv;
11720 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11721}
11722
11723
11724static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11725 size_t data_len, int noack)
11726{
11727 struct i802_bss *bss = priv;
11728 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11729 0, 0, 0, 0);
11730}
11731
11732
11733static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11734{
11735 struct i802_bss *bss = priv;
11736 return wpa_driver_nl80211_sta_remove(bss, addr);
11737}
11738
11739
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011740static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11741 const char *ifname, int vlan_id)
11742{
11743 struct i802_bss *bss = priv;
11744 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11745}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011746
11747
11748static int driver_nl80211_read_sta_data(void *priv,
11749 struct hostap_sta_driver_data *data,
11750 const u8 *addr)
11751{
11752 struct i802_bss *bss = priv;
11753 return i802_read_sta_data(bss, data, addr);
11754}
11755
11756
11757static int driver_nl80211_send_action(void *priv, unsigned int freq,
11758 unsigned int wait_time,
11759 const u8 *dst, const u8 *src,
11760 const u8 *bssid,
11761 const u8 *data, size_t data_len,
11762 int no_cck)
11763{
11764 struct i802_bss *bss = priv;
11765 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11766 bssid, data, data_len, no_cck);
11767}
11768
11769
11770static int driver_nl80211_probe_req_report(void *priv, int report)
11771{
11772 struct i802_bss *bss = priv;
11773 return wpa_driver_nl80211_probe_req_report(bss, report);
11774}
11775
11776
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011777static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11778 const u8 *ies, size_t ies_len)
11779{
11780 int ret;
11781 struct nl_msg *msg;
11782 struct i802_bss *bss = priv;
11783 struct wpa_driver_nl80211_data *drv = bss->drv;
11784 u16 mdid = WPA_GET_LE16(md);
11785
11786 msg = nlmsg_alloc();
11787 if (!msg)
11788 return -ENOMEM;
11789
11790 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11791 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11793 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11794 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11795
11796 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11797 if (ret) {
11798 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11799 "err=%d (%s)", ret, strerror(-ret));
11800 }
11801
11802 return ret;
11803
11804nla_put_failure:
11805 nlmsg_free(msg);
11806 return -ENOBUFS;
11807}
11808
11809
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011810const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11811{
11812 struct i802_bss *bss = priv;
11813 struct wpa_driver_nl80211_data *drv = bss->drv;
11814
11815 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11816 return NULL;
11817
11818 return bss->addr;
11819}
11820
11821
Dmitry Shmidt56052862013-10-04 10:23:25 -070011822static const char * scan_state_str(enum scan_states scan_state)
11823{
11824 switch (scan_state) {
11825 case NO_SCAN:
11826 return "NO_SCAN";
11827 case SCAN_REQUESTED:
11828 return "SCAN_REQUESTED";
11829 case SCAN_STARTED:
11830 return "SCAN_STARTED";
11831 case SCAN_COMPLETED:
11832 return "SCAN_COMPLETED";
11833 case SCAN_ABORTED:
11834 return "SCAN_ABORTED";
11835 case SCHED_SCAN_STARTED:
11836 return "SCHED_SCAN_STARTED";
11837 case SCHED_SCAN_STOPPED:
11838 return "SCHED_SCAN_STOPPED";
11839 case SCHED_SCAN_RESULTS:
11840 return "SCHED_SCAN_RESULTS";
11841 }
11842
11843 return "??";
11844}
11845
11846
11847static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11848{
11849 struct i802_bss *bss = priv;
11850 struct wpa_driver_nl80211_data *drv = bss->drv;
11851 int res;
11852 char *pos, *end;
11853
11854 pos = buf;
11855 end = buf + buflen;
11856
11857 res = os_snprintf(pos, end - pos,
11858 "ifindex=%d\n"
11859 "ifname=%s\n"
11860 "brname=%s\n"
11861 "addr=" MACSTR "\n"
11862 "freq=%d\n"
11863 "%s%s%s%s%s",
11864 bss->ifindex,
11865 bss->ifname,
11866 bss->brname,
11867 MAC2STR(bss->addr),
11868 bss->freq,
11869 bss->beacon_set ? "beacon_set=1\n" : "",
11870 bss->added_if_into_bridge ?
11871 "added_if_into_bridge=1\n" : "",
11872 bss->added_bridge ? "added_bridge=1\n" : "",
11873 bss->in_deinit ? "in_deinit=1\n" : "",
11874 bss->if_dynamic ? "if_dynamic=1\n" : "");
11875 if (res < 0 || res >= end - pos)
11876 return pos - buf;
11877 pos += res;
11878
11879 if (bss->wdev_id_set) {
11880 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11881 (unsigned long long) bss->wdev_id);
11882 if (res < 0 || res >= end - pos)
11883 return pos - buf;
11884 pos += res;
11885 }
11886
11887 res = os_snprintf(pos, end - pos,
11888 "phyname=%s\n"
11889 "drv_ifindex=%d\n"
11890 "operstate=%d\n"
11891 "scan_state=%s\n"
11892 "auth_bssid=" MACSTR "\n"
11893 "auth_attempt_bssid=" MACSTR "\n"
11894 "bssid=" MACSTR "\n"
11895 "prev_bssid=" MACSTR "\n"
11896 "associated=%d\n"
11897 "assoc_freq=%u\n"
11898 "monitor_sock=%d\n"
11899 "monitor_ifidx=%d\n"
11900 "monitor_refcount=%d\n"
11901 "last_mgmt_freq=%u\n"
11902 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011903 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011904 drv->phyname,
11905 drv->ifindex,
11906 drv->operstate,
11907 scan_state_str(drv->scan_state),
11908 MAC2STR(drv->auth_bssid),
11909 MAC2STR(drv->auth_attempt_bssid),
11910 MAC2STR(drv->bssid),
11911 MAC2STR(drv->prev_bssid),
11912 drv->associated,
11913 drv->assoc_freq,
11914 drv->monitor_sock,
11915 drv->monitor_ifidx,
11916 drv->monitor_refcount,
11917 drv->last_mgmt_freq,
11918 drv->eapol_tx_sock,
11919 drv->ignore_if_down_event ?
11920 "ignore_if_down_event=1\n" : "",
11921 drv->scan_complete_events ?
11922 "scan_complete_events=1\n" : "",
11923 drv->disabled_11b_rates ?
11924 "disabled_11b_rates=1\n" : "",
11925 drv->pending_remain_on_chan ?
11926 "pending_remain_on_chan=1\n" : "",
11927 drv->in_interface_list ? "in_interface_list=1\n" : "",
11928 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11929 drv->poll_command_supported ?
11930 "poll_command_supported=1\n" : "",
11931 drv->data_tx_status ? "data_tx_status=1\n" : "",
11932 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11933 drv->retry_auth ? "retry_auth=1\n" : "",
11934 drv->use_monitor ? "use_monitor=1\n" : "",
11935 drv->ignore_next_local_disconnect ?
11936 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011937 drv->ignore_next_local_deauth ?
11938 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011939 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11940 if (res < 0 || res >= end - pos)
11941 return pos - buf;
11942 pos += res;
11943
11944 if (drv->has_capability) {
11945 res = os_snprintf(pos, end - pos,
11946 "capa.key_mgmt=0x%x\n"
11947 "capa.enc=0x%x\n"
11948 "capa.auth=0x%x\n"
11949 "capa.flags=0x%x\n"
11950 "capa.max_scan_ssids=%d\n"
11951 "capa.max_sched_scan_ssids=%d\n"
11952 "capa.sched_scan_supported=%d\n"
11953 "capa.max_match_sets=%d\n"
11954 "capa.max_remain_on_chan=%u\n"
11955 "capa.max_stations=%u\n"
11956 "capa.probe_resp_offloads=0x%x\n"
11957 "capa.max_acl_mac_addrs=%u\n"
11958 "capa.num_multichan_concurrent=%u\n",
11959 drv->capa.key_mgmt,
11960 drv->capa.enc,
11961 drv->capa.auth,
11962 drv->capa.flags,
11963 drv->capa.max_scan_ssids,
11964 drv->capa.max_sched_scan_ssids,
11965 drv->capa.sched_scan_supported,
11966 drv->capa.max_match_sets,
11967 drv->capa.max_remain_on_chan,
11968 drv->capa.max_stations,
11969 drv->capa.probe_resp_offloads,
11970 drv->capa.max_acl_mac_addrs,
11971 drv->capa.num_multichan_concurrent);
11972 if (res < 0 || res >= end - pos)
11973 return pos - buf;
11974 pos += res;
11975 }
11976
11977 return pos - buf;
11978}
11979
11980
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011981static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11982{
11983 if (settings->head)
11984 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11985 settings->head_len, settings->head);
11986
11987 if (settings->tail)
11988 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11989 settings->tail_len, settings->tail);
11990
11991 if (settings->beacon_ies)
11992 NLA_PUT(msg, NL80211_ATTR_IE,
11993 settings->beacon_ies_len, settings->beacon_ies);
11994
11995 if (settings->proberesp_ies)
11996 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11997 settings->proberesp_ies_len, settings->proberesp_ies);
11998
11999 if (settings->assocresp_ies)
12000 NLA_PUT(msg,
12001 NL80211_ATTR_IE_ASSOC_RESP,
12002 settings->assocresp_ies_len, settings->assocresp_ies);
12003
12004 if (settings->probe_resp)
12005 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
12006 settings->probe_resp_len, settings->probe_resp);
12007
12008 return 0;
12009
12010nla_put_failure:
12011 return -ENOBUFS;
12012}
12013
12014
12015static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
12016{
12017 struct nl_msg *msg;
12018 struct i802_bss *bss = priv;
12019 struct wpa_driver_nl80211_data *drv = bss->drv;
12020 struct nlattr *beacon_csa;
12021 int ret = -ENOBUFS;
12022
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012023 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 -080012024 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012025 settings->freq_params.freq, settings->freq_params.bandwidth,
12026 settings->freq_params.center_freq1,
12027 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012028
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012029 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012030 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
12031 return -EOPNOTSUPP;
12032 }
12033
12034 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
12035 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
12036 return -EOPNOTSUPP;
12037
12038 /* check settings validity */
12039 if (!settings->beacon_csa.tail ||
12040 ((settings->beacon_csa.tail_len <=
12041 settings->counter_offset_beacon) ||
12042 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
12043 settings->cs_count)))
12044 return -EINVAL;
12045
12046 if (settings->beacon_csa.probe_resp &&
12047 ((settings->beacon_csa.probe_resp_len <=
12048 settings->counter_offset_presp) ||
12049 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
12050 settings->cs_count)))
12051 return -EINVAL;
12052
12053 msg = nlmsg_alloc();
12054 if (!msg)
12055 return -ENOMEM;
12056
12057 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
12058 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12059 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
12060 ret = nl80211_put_freq_params(msg, &settings->freq_params);
12061 if (ret)
12062 goto error;
12063
12064 if (settings->block_tx)
12065 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12066
12067 /* beacon_after params */
12068 ret = set_beacon_data(msg, &settings->beacon_after);
12069 if (ret)
12070 goto error;
12071
12072 /* beacon_csa params */
12073 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12074 if (!beacon_csa)
12075 goto nla_put_failure;
12076
12077 ret = set_beacon_data(msg, &settings->beacon_csa);
12078 if (ret)
12079 goto error;
12080
12081 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12082 settings->counter_offset_beacon);
12083
12084 if (settings->beacon_csa.probe_resp)
12085 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12086 settings->counter_offset_presp);
12087
12088 nla_nest_end(msg, beacon_csa);
12089 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12090 if (ret) {
12091 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12092 ret, strerror(-ret));
12093 }
12094 return ret;
12095
12096nla_put_failure:
12097 ret = -ENOBUFS;
12098error:
12099 nlmsg_free(msg);
12100 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12101 return ret;
12102}
12103
12104
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012105#ifdef CONFIG_TESTING_OPTIONS
12106static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12107{
12108 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12109 struct wpabuf *buf = arg;
12110
12111 if (!buf)
12112 return NL_SKIP;
12113
12114 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12115 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12116 return NL_SKIP;
12117 }
12118
12119 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12120 genlmsg_attrlen(gnlh, 0));
12121
12122 return NL_SKIP;
12123}
12124#endif /* CONFIG_TESTING_OPTIONS */
12125
12126
12127static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12128{
12129 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12130 struct nlattr *nl_vendor_reply, *nl;
12131 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12132 struct wpabuf *buf = arg;
12133 int rem;
12134
12135 if (!buf)
12136 return NL_SKIP;
12137
12138 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12139 genlmsg_attrlen(gnlh, 0), NULL);
12140 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12141
12142 if (!nl_vendor_reply)
12143 return NL_SKIP;
12144
12145 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12146 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12147 return NL_SKIP;
12148 }
12149
12150 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12151 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12152 }
12153
12154 return NL_SKIP;
12155}
12156
12157
12158static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12159 unsigned int subcmd, const u8 *data,
12160 size_t data_len, struct wpabuf *buf)
12161{
12162 struct i802_bss *bss = priv;
12163 struct wpa_driver_nl80211_data *drv = bss->drv;
12164 struct nl_msg *msg;
12165 int ret;
12166
12167 msg = nlmsg_alloc();
12168 if (!msg)
12169 return -ENOMEM;
12170
12171#ifdef CONFIG_TESTING_OPTIONS
12172 if (vendor_id == 0xffffffff) {
12173 nl80211_cmd(drv, msg, 0, subcmd);
12174 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12175 0)
12176 goto nla_put_failure;
12177 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12178 if (ret)
12179 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12180 ret);
12181 return ret;
12182 }
12183#endif /* CONFIG_TESTING_OPTIONS */
12184
12185 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12186 if (nl80211_set_iface_id(msg, bss) < 0)
12187 goto nla_put_failure;
12188 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12189 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12190 if (data)
12191 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12192
12193 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12194 if (ret)
12195 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12196 ret);
12197 return ret;
12198
12199nla_put_failure:
12200 nlmsg_free(msg);
12201 return -ENOBUFS;
12202}
12203
12204
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012205static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12206 u8 qos_map_set_len)
12207{
12208 struct i802_bss *bss = priv;
12209 struct wpa_driver_nl80211_data *drv = bss->drv;
12210 struct nl_msg *msg;
12211 int ret;
12212
12213 msg = nlmsg_alloc();
12214 if (!msg)
12215 return -ENOMEM;
12216
12217 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12218 qos_map_set, qos_map_set_len);
12219
12220 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12221 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12222 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12223
12224 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12225 if (ret)
12226 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12227
12228 return ret;
12229
12230nla_put_failure:
12231 nlmsg_free(msg);
12232 return -ENOBUFS;
12233}
12234
12235
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012236static int nl80211_set_wowlan(void *priv,
12237 const struct wowlan_triggers *triggers)
12238{
12239 struct i802_bss *bss = priv;
12240 struct wpa_driver_nl80211_data *drv = bss->drv;
12241 struct nl_msg *msg;
12242 struct nlattr *wowlan_triggers;
12243 int ret;
12244
12245 msg = nlmsg_alloc();
12246 if (!msg)
12247 return -ENOMEM;
12248
12249 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12250
12251 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12253
12254 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12255 if (!wowlan_triggers)
12256 goto nla_put_failure;
12257
12258 if (triggers->any)
12259 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12260 if (triggers->disconnect)
12261 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12262 if (triggers->magic_pkt)
12263 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12264 if (triggers->gtk_rekey_failure)
12265 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12266 if (triggers->eap_identity_req)
12267 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12268 if (triggers->four_way_handshake)
12269 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12270 if (triggers->rfkill_release)
12271 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12272
12273 nla_nest_end(msg, wowlan_triggers);
12274
12275 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12276 if (ret)
12277 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12278
12279 return ret;
12280
12281nla_put_failure:
12282 nlmsg_free(msg);
12283 return -ENOBUFS;
12284}
12285
12286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012287const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12288 .name = "nl80211",
12289 .desc = "Linux nl80211/cfg80211",
12290 .get_bssid = wpa_driver_nl80211_get_bssid,
12291 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012292 .set_key = driver_nl80211_set_key,
12293 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012294 .sched_scan = wpa_driver_nl80211_sched_scan,
12295 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012296 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012297 .deauthenticate = driver_nl80211_deauthenticate,
12298 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012299 .associate = wpa_driver_nl80211_associate,
12300 .global_init = nl80211_global_init,
12301 .global_deinit = nl80211_global_deinit,
12302 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012303 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012304 .get_capa = wpa_driver_nl80211_get_capa,
12305 .set_operstate = wpa_driver_nl80211_set_operstate,
12306 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12307 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012308 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012309 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012310 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012311 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012312 .if_remove = driver_nl80211_if_remove,
12313 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012314 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12315 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012316 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012317 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12318 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012319 .hapd_init = i802_init,
12320 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012321 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012322 .get_seqnum = i802_get_seqnum,
12323 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012324 .get_inact_sec = i802_get_inact_sec,
12325 .sta_clear_stats = i802_sta_clear_stats,
12326 .set_rts = i802_set_rts,
12327 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012328 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012329 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012330 .sta_deauth = i802_sta_deauth,
12331 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012332 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012333 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012334 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012335 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12336 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12337 .cancel_remain_on_channel =
12338 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012339 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012340 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012341 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012342 .resume = wpa_driver_nl80211_resume,
12343 .send_ft_action = nl80211_send_ft_action,
12344 .signal_monitor = nl80211_signal_monitor,
12345 .signal_poll = nl80211_signal_poll,
12346 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012347 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012348 .set_param = nl80211_set_param,
12349 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012350 .add_pmkid = nl80211_add_pmkid,
12351 .remove_pmkid = nl80211_remove_pmkid,
12352 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012353 .set_rekey_info = nl80211_set_rekey_info,
12354 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012355 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012356 .start_dfs_cac = nl80211_start_radar_detection,
12357 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012358#ifdef CONFIG_TDLS
12359 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12360 .tdls_oper = nl80211_tdls_oper,
12361#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012362 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012363 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012364 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012365 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012366 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012367#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012368 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012369 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012370 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012371#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012372#ifdef ANDROID
12373 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012374#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012375 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012376 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012377 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012378};