blob: 5895efabb65205c442e583767ed92deb309bdc1a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <net/if.h>
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
22#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
24#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "nl80211_copy.h"
27
28#include "common.h"
29#include "eloop.h"
30#include "utils/list.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080031#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070032#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080034#include "common/ieee802_11_common.h"
35#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "netlink.h"
37#include "linux_ioctl.h"
38#include "radiotap.h"
39#include "radiotap_iter.h"
40#include "rfkill.h"
41#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080042
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043#ifndef SO_WIFI_STATUS
44# if defined(__sparc__)
45# define SO_WIFI_STATUS 0x0025
46# elif defined(__parisc__)
47# define SO_WIFI_STATUS 0x4022
48# else
49# define SO_WIFI_STATUS 41
50# endif
51
52# define SCM_WIFI_STATUS SO_WIFI_STATUS
53#endif
54
55#ifndef SO_EE_ORIGIN_TXSTATUS
56#define SO_EE_ORIGIN_TXSTATUS 4
57#endif
58
59#ifndef PACKET_TX_TIMESTAMP
60#define PACKET_TX_TIMESTAMP 16
61#endif
62
63#ifdef ANDROID
64#include "android_drv.h"
65#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066#ifdef CONFIG_LIBNL20
67/* libnl 2.0 compatibility code */
68#define nl_handle nl_sock
69#define nl80211_handle_alloc nl_socket_alloc_cb
70#define nl80211_handle_destroy nl_socket_free
71#else
72/*
73 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
74 * but when you free a socket again it will mess up its bitmap and
75 * and use the wrong number the next time it needs a socket ID.
76 * Therefore, we wrap the handle alloc/destroy and add our own pid
77 * accounting.
78 */
79static uint32_t port_bitmap[32] = { 0 };
80
81static struct nl_handle *nl80211_handle_alloc(void *cb)
82{
83 struct nl_handle *handle;
84 uint32_t pid = getpid() & 0x3FFFFF;
85 int i;
86
87 handle = nl_handle_alloc_cb(cb);
88
89 for (i = 0; i < 1024; i++) {
90 if (port_bitmap[i / 32] & (1 << (i % 32)))
91 continue;
92 port_bitmap[i / 32] |= 1 << (i % 32);
93 pid += i << 22;
94 break;
95 }
96
97 nl_socket_set_local_port(handle, pid);
98
99 return handle;
100}
101
102static void nl80211_handle_destroy(struct nl_handle *handle)
103{
104 uint32_t port = nl_socket_get_local_port(handle);
105
106 port >>= 22;
107 port_bitmap[port / 32] &= ~(1 << (port % 32));
108
109 nl_handle_destroy(handle);
110}
111#endif /* CONFIG_LIBNL20 */
112
113
Dmitry Shmidt54605472013-11-08 11:10:19 -0800114#ifdef ANDROID
115/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
116static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
117{
118 return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
119}
120#undef nl_socket_set_nonblocking
121#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
122#endif /* ANDROID */
123
124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800125static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
126{
127 struct nl_handle *handle;
128
129 handle = nl80211_handle_alloc(cb);
130 if (handle == NULL) {
131 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
132 "callbacks (%s)", dbg);
133 return NULL;
134 }
135
136 if (genl_connect(handle)) {
137 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
138 "netlink (%s)", dbg);
139 nl80211_handle_destroy(handle);
140 return NULL;
141 }
142
143 return handle;
144}
145
146
147static void nl_destroy_handles(struct nl_handle **handle)
148{
149 if (*handle == NULL)
150 return;
151 nl80211_handle_destroy(*handle);
152 *handle = NULL;
153}
154
155
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700156#if __WORDSIZE == 64
157#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
158#else
159#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
160#endif
161
162static void nl80211_register_eloop_read(struct nl_handle **handle,
163 eloop_sock_handler handler,
164 void *eloop_data)
165{
166 nl_socket_set_nonblocking(*handle);
167 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
168 eloop_data, *handle);
169 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
170}
171
172
173static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
174{
175 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
176 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
177 nl_destroy_handles(handle);
178}
179
180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181#ifndef IFF_LOWER_UP
182#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
183#endif
184#ifndef IFF_DORMANT
185#define IFF_DORMANT 0x20000 /* driver signals dormant */
186#endif
187
188#ifndef IF_OPER_DORMANT
189#define IF_OPER_DORMANT 5
190#endif
191#ifndef IF_OPER_UP
192#define IF_OPER_UP 6
193#endif
194
195struct nl80211_global {
196 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197 int if_add_ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700198 u64 if_add_wdevid;
199 int if_add_wdevid_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200 struct netlink_data *netlink;
201 struct nl_cb *nl_cb;
202 struct nl_handle *nl;
203 int nl80211_id;
204 int ioctl_sock; /* socket for ioctl() use */
205
206 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207};
208
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209struct nl80211_wiphy_data {
210 struct dl_list list;
211 struct dl_list bsss;
212 struct dl_list drvs;
213
214 struct nl_handle *nl_beacons;
215 struct nl_cb *nl_cb;
216
217 int wiphy_idx;
218};
219
220static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222struct i802_bss {
223 struct wpa_driver_nl80211_data *drv;
224 struct i802_bss *next;
225 int ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700226 u64 wdev_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 char ifname[IFNAMSIZ + 1];
228 char brname[IFNAMSIZ];
229 unsigned int beacon_set:1;
230 unsigned int added_if_into_bridge:1;
231 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232 unsigned int in_deinit:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700233 unsigned int wdev_id_set:1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800234 unsigned int added_if:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800235
236 u8 addr[ETH_ALEN];
237
238 int freq;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700239 int bandwidth;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700240 int if_dynamic;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800242 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800243 struct nl_handle *nl_preq, *nl_mgmt;
244 struct nl_cb *nl_cb;
245
246 struct nl80211_wiphy_data *wiphy_data;
247 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248};
249
250struct wpa_driver_nl80211_data {
251 struct nl80211_global *global;
252 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800253 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 char phyname[32];
255 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 int ifindex;
257 int if_removed;
258 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800259 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 struct rfkill_data *rfkill;
261 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700262 u8 *extended_capa, *extended_capa_mask;
263 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 int has_capability;
265
266 int operstate;
267
268 int scan_complete_events;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700269 enum scan_states {
270 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
271 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
272 SCHED_SCAN_RESULTS
273 } scan_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276
277 u8 auth_bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700278 u8 auth_attempt_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 u8 bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700280 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 int associated;
282 u8 ssid[32];
283 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284 enum nl80211_iftype nlmode;
285 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 unsigned int assoc_freq;
287
288 int monitor_sock;
289 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800292 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800294 unsigned int in_interface_list:1;
295 unsigned int device_ap_sme:1;
296 unsigned int poll_command_supported:1;
297 unsigned int data_tx_status:1;
298 unsigned int scan_for_auth:1;
299 unsigned int retry_auth:1;
300 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800301 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700302 unsigned int ignore_next_local_deauth:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700303 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800304 unsigned int hostapd:1;
305 unsigned int start_mode_ap:1;
306 unsigned int start_iface_up:1;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800307 unsigned int test_use_roc_tx:1;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700308 unsigned int ignore_deauth_event:1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700309 unsigned int dfs_vendor_cmd_avail:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310
311 u64 remain_on_chan_cookie;
312 u64 send_action_cookie;
313
314 unsigned int last_mgmt_freq;
315
316 struct wpa_driver_scan_filter *filter_ssids;
317 size_t num_filter_ssids;
318
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800319 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321 int eapol_tx_sock;
322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 int eapol_sock; /* socket for EAPOL frames */
324
325 int default_if_indices[16];
326 int *if_indices;
327 int num_if_indices;
328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800329 /* From failed authentication command */
330 int auth_freq;
331 u8 auth_bssid_[ETH_ALEN];
332 u8 auth_ssid[32];
333 size_t auth_ssid_len;
334 int auth_alg;
335 u8 *auth_ie;
336 size_t auth_ie_len;
337 u8 auth_wep_key[4][16];
338 size_t auth_wep_key_len[4];
339 int auth_wep_tx_keyidx;
340 int auth_local_state_change;
341 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342};
343
344
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800345static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
347 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
349 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800351wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
352 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
354 const u8 *addr, int cmd, u16 reason_code,
355 int local_state_change);
356static void nl80211_remove_monitor_interface(
357 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800358static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 const u8 *buf, size_t buf_len, u64 *cookie,
361 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700362static int nl80211_register_frame(struct i802_bss *bss,
363 struct nl_handle *hl_handle,
364 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800365static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
366 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367#ifdef ANDROID
368static int android_pno_start(struct i802_bss *bss,
369 struct wpa_driver_scan_params *params);
370static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800371extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
372 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373#endif /* ANDROID */
374#ifdef ANDROID_P2P
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700375#ifdef ANDROID_P2P_STUB
376int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration) {
377 return 0;
378}
379int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len) {
380 return 0;
381}
382int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow) {
383 return -1;
384}
385int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
386 const struct wpabuf *proberesp,
387 const struct wpabuf *assocresp) {
388 return 0;
389}
390#else /* ANDROID_P2P_STUB */
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700391int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700393int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
394int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800395 const struct wpabuf *proberesp,
396 const struct wpabuf *assocresp);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700397#endif /* ANDROID_P2P_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800398#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
401static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
402static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800403static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 enum wpa_driver_if_type type,
405 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700406
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700407static int nl80211_set_channel(struct i802_bss *bss,
408 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
410 int ifindex, int disabled);
411
412static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800413static int wpa_driver_nl80211_authenticate_retry(
414 struct wpa_driver_nl80211_data *drv);
415
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700416static int i802_set_iface_flags(struct i802_bss *bss, int up);
417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700419static const char * nl80211_command_to_string(enum nl80211_commands cmd)
420{
421#define C2S(x) case x: return #x;
422 switch (cmd) {
423 C2S(NL80211_CMD_UNSPEC)
424 C2S(NL80211_CMD_GET_WIPHY)
425 C2S(NL80211_CMD_SET_WIPHY)
426 C2S(NL80211_CMD_NEW_WIPHY)
427 C2S(NL80211_CMD_DEL_WIPHY)
428 C2S(NL80211_CMD_GET_INTERFACE)
429 C2S(NL80211_CMD_SET_INTERFACE)
430 C2S(NL80211_CMD_NEW_INTERFACE)
431 C2S(NL80211_CMD_DEL_INTERFACE)
432 C2S(NL80211_CMD_GET_KEY)
433 C2S(NL80211_CMD_SET_KEY)
434 C2S(NL80211_CMD_NEW_KEY)
435 C2S(NL80211_CMD_DEL_KEY)
436 C2S(NL80211_CMD_GET_BEACON)
437 C2S(NL80211_CMD_SET_BEACON)
438 C2S(NL80211_CMD_START_AP)
439 C2S(NL80211_CMD_STOP_AP)
440 C2S(NL80211_CMD_GET_STATION)
441 C2S(NL80211_CMD_SET_STATION)
442 C2S(NL80211_CMD_NEW_STATION)
443 C2S(NL80211_CMD_DEL_STATION)
444 C2S(NL80211_CMD_GET_MPATH)
445 C2S(NL80211_CMD_SET_MPATH)
446 C2S(NL80211_CMD_NEW_MPATH)
447 C2S(NL80211_CMD_DEL_MPATH)
448 C2S(NL80211_CMD_SET_BSS)
449 C2S(NL80211_CMD_SET_REG)
450 C2S(NL80211_CMD_REQ_SET_REG)
451 C2S(NL80211_CMD_GET_MESH_CONFIG)
452 C2S(NL80211_CMD_SET_MESH_CONFIG)
453 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
454 C2S(NL80211_CMD_GET_REG)
455 C2S(NL80211_CMD_GET_SCAN)
456 C2S(NL80211_CMD_TRIGGER_SCAN)
457 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
458 C2S(NL80211_CMD_SCAN_ABORTED)
459 C2S(NL80211_CMD_REG_CHANGE)
460 C2S(NL80211_CMD_AUTHENTICATE)
461 C2S(NL80211_CMD_ASSOCIATE)
462 C2S(NL80211_CMD_DEAUTHENTICATE)
463 C2S(NL80211_CMD_DISASSOCIATE)
464 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
465 C2S(NL80211_CMD_REG_BEACON_HINT)
466 C2S(NL80211_CMD_JOIN_IBSS)
467 C2S(NL80211_CMD_LEAVE_IBSS)
468 C2S(NL80211_CMD_TESTMODE)
469 C2S(NL80211_CMD_CONNECT)
470 C2S(NL80211_CMD_ROAM)
471 C2S(NL80211_CMD_DISCONNECT)
472 C2S(NL80211_CMD_SET_WIPHY_NETNS)
473 C2S(NL80211_CMD_GET_SURVEY)
474 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
475 C2S(NL80211_CMD_SET_PMKSA)
476 C2S(NL80211_CMD_DEL_PMKSA)
477 C2S(NL80211_CMD_FLUSH_PMKSA)
478 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
479 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
480 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
481 C2S(NL80211_CMD_REGISTER_FRAME)
482 C2S(NL80211_CMD_FRAME)
483 C2S(NL80211_CMD_FRAME_TX_STATUS)
484 C2S(NL80211_CMD_SET_POWER_SAVE)
485 C2S(NL80211_CMD_GET_POWER_SAVE)
486 C2S(NL80211_CMD_SET_CQM)
487 C2S(NL80211_CMD_NOTIFY_CQM)
488 C2S(NL80211_CMD_SET_CHANNEL)
489 C2S(NL80211_CMD_SET_WDS_PEER)
490 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
491 C2S(NL80211_CMD_JOIN_MESH)
492 C2S(NL80211_CMD_LEAVE_MESH)
493 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
494 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
495 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
496 C2S(NL80211_CMD_GET_WOWLAN)
497 C2S(NL80211_CMD_SET_WOWLAN)
498 C2S(NL80211_CMD_START_SCHED_SCAN)
499 C2S(NL80211_CMD_STOP_SCHED_SCAN)
500 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
501 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
502 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
503 C2S(NL80211_CMD_PMKSA_CANDIDATE)
504 C2S(NL80211_CMD_TDLS_OPER)
505 C2S(NL80211_CMD_TDLS_MGMT)
506 C2S(NL80211_CMD_UNEXPECTED_FRAME)
507 C2S(NL80211_CMD_PROBE_CLIENT)
508 C2S(NL80211_CMD_REGISTER_BEACONS)
509 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
510 C2S(NL80211_CMD_SET_NOACK_MAP)
511 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
512 C2S(NL80211_CMD_START_P2P_DEVICE)
513 C2S(NL80211_CMD_STOP_P2P_DEVICE)
514 C2S(NL80211_CMD_CONN_FAILED)
515 C2S(NL80211_CMD_SET_MCAST_RATE)
516 C2S(NL80211_CMD_SET_MAC_ACL)
517 C2S(NL80211_CMD_RADAR_DETECT)
518 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
519 C2S(NL80211_CMD_UPDATE_FT_IES)
520 C2S(NL80211_CMD_FT_EVENT)
521 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
522 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800523 C2S(NL80211_CMD_GET_COALESCE)
524 C2S(NL80211_CMD_SET_COALESCE)
525 C2S(NL80211_CMD_CHANNEL_SWITCH)
526 C2S(NL80211_CMD_VENDOR)
527 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700528 default:
529 return "NL80211_CMD_UNKNOWN";
530 }
531#undef C2S
532}
533
534
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800535/* Converts nl80211_chan_width to a common format */
536static enum chan_width convert2width(int width)
537{
538 switch (width) {
539 case NL80211_CHAN_WIDTH_20_NOHT:
540 return CHAN_WIDTH_20_NOHT;
541 case NL80211_CHAN_WIDTH_20:
542 return CHAN_WIDTH_20;
543 case NL80211_CHAN_WIDTH_40:
544 return CHAN_WIDTH_40;
545 case NL80211_CHAN_WIDTH_80:
546 return CHAN_WIDTH_80;
547 case NL80211_CHAN_WIDTH_80P80:
548 return CHAN_WIDTH_80P80;
549 case NL80211_CHAN_WIDTH_160:
550 return CHAN_WIDTH_160;
551 }
552 return CHAN_WIDTH_UNKNOWN;
553}
554
555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800556static int is_ap_interface(enum nl80211_iftype nlmode)
557{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700558 return nlmode == NL80211_IFTYPE_AP ||
559 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800560}
561
562
563static int is_sta_interface(enum nl80211_iftype nlmode)
564{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700565 return nlmode == NL80211_IFTYPE_STATION ||
566 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800567}
568
569
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700570static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700572 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
573 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575
576
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700577static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
578{
579 if (drv->associated)
580 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
581 drv->associated = 0;
582 os_memset(drv->bssid, 0, ETH_ALEN);
583}
584
585
Jouni Malinen87fd2792011-05-16 18:35:42 +0300586struct nl80211_bss_info_arg {
587 struct wpa_driver_nl80211_data *drv;
588 struct wpa_scan_results *res;
589 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300591};
592
593static int bss_info_handler(struct nl_msg *msg, void *arg);
594
595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596/* nl80211 code */
597static int ack_handler(struct nl_msg *msg, void *arg)
598{
599 int *err = arg;
600 *err = 0;
601 return NL_STOP;
602}
603
604static int finish_handler(struct nl_msg *msg, void *arg)
605{
606 int *ret = arg;
607 *ret = 0;
608 return NL_SKIP;
609}
610
611static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
612 void *arg)
613{
614 int *ret = arg;
615 *ret = err->error;
616 return NL_SKIP;
617}
618
619
620static int no_seq_check(struct nl_msg *msg, void *arg)
621{
622 return NL_OK;
623}
624
625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 struct nl_handle *nl_handle, struct nl_msg *msg,
628 int (*valid_handler)(struct nl_msg *, void *),
629 void *valid_data)
630{
631 struct nl_cb *cb;
632 int err = -ENOMEM;
633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800634 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 if (!cb)
636 goto out;
637
638 err = nl_send_auto_complete(nl_handle, msg);
639 if (err < 0)
640 goto out;
641
642 err = 1;
643
644 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
645 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
646 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
647
648 if (valid_handler)
649 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
650 valid_handler, valid_data);
651
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700652 while (err > 0) {
653 int res = nl_recvmsgs(nl_handle, cb);
654 if (res) {
655 wpa_printf(MSG_INFO,
656 "nl80211: %s->nl_recvmsgs failed: %d",
657 __func__, res);
658 }
659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660 out:
661 nl_cb_put(cb);
662 nlmsg_free(msg);
663 return err;
664}
665
666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667static int send_and_recv_msgs_global(struct nl80211_global *global,
668 struct nl_msg *msg,
669 int (*valid_handler)(struct nl_msg *, void *),
670 void *valid_data)
671{
672 return send_and_recv(global, global->nl, msg, valid_handler,
673 valid_data);
674}
675
Dmitry Shmidt04949592012-07-19 12:16:46 -0700676
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800677static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 struct nl_msg *msg,
679 int (*valid_handler)(struct nl_msg *, void *),
680 void *valid_data)
681{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682 return send_and_recv(drv->global, drv->global->nl, msg,
683 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684}
685
686
687struct family_data {
688 const char *group;
689 int id;
690};
691
692
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700693static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
694{
695 if (bss->wdev_id_set)
696 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
697 else
698 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
699 return 0;
700
701nla_put_failure:
702 return -1;
703}
704
705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706static int family_handler(struct nl_msg *msg, void *arg)
707{
708 struct family_data *res = arg;
709 struct nlattr *tb[CTRL_ATTR_MAX + 1];
710 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
711 struct nlattr *mcgrp;
712 int i;
713
714 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
715 genlmsg_attrlen(gnlh, 0), NULL);
716 if (!tb[CTRL_ATTR_MCAST_GROUPS])
717 return NL_SKIP;
718
719 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
720 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
721 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
722 nla_len(mcgrp), NULL);
723 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
724 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
725 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
726 res->group,
727 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
728 continue;
729 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
730 break;
731 };
732
733 return NL_SKIP;
734}
735
736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 const char *family, const char *group)
739{
740 struct nl_msg *msg;
741 int ret = -1;
742 struct family_data res = { group, -ENOENT };
743
744 msg = nlmsg_alloc();
745 if (!msg)
746 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 0, 0, CTRL_CMD_GETFAMILY, 0);
749 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 msg = NULL;
753 if (ret == 0)
754 ret = res.id;
755
756nla_put_failure:
757 nlmsg_free(msg);
758 return ret;
759}
760
761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
763 struct nl_msg *msg, int flags, uint8_t cmd)
764{
765 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
766 0, flags, cmd, 0);
767}
768
769
770struct wiphy_idx_data {
771 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700772 enum nl80211_iftype nlmode;
773 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774};
775
776
777static int netdev_info_handler(struct nl_msg *msg, void *arg)
778{
779 struct nlattr *tb[NL80211_ATTR_MAX + 1];
780 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
781 struct wiphy_idx_data *info = arg;
782
783 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
784 genlmsg_attrlen(gnlh, 0), NULL);
785
786 if (tb[NL80211_ATTR_WIPHY])
787 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
788
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700789 if (tb[NL80211_ATTR_IFTYPE])
790 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
791
792 if (tb[NL80211_ATTR_MAC] && info->macaddr)
793 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
794 ETH_ALEN);
795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 return NL_SKIP;
797}
798
799
800static int nl80211_get_wiphy_index(struct i802_bss *bss)
801{
802 struct nl_msg *msg;
803 struct wiphy_idx_data data = {
804 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700805 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800806 };
807
808 msg = nlmsg_alloc();
809 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700810 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811
812 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
813
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700814 if (nl80211_set_iface_id(msg, bss) < 0)
815 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816
817 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
818 return data.wiphy_idx;
819 msg = NULL;
820nla_put_failure:
821 nlmsg_free(msg);
822 return -1;
823}
824
825
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700826static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
827{
828 struct nl_msg *msg;
829 struct wiphy_idx_data data = {
830 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
831 .macaddr = NULL,
832 };
833
834 msg = nlmsg_alloc();
835 if (!msg)
836 return -1;
837
838 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
839
840 if (nl80211_set_iface_id(msg, bss) < 0)
841 goto nla_put_failure;
842
843 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
844 return data.nlmode;
845 msg = NULL;
846nla_put_failure:
847 nlmsg_free(msg);
848 return NL80211_IFTYPE_UNSPECIFIED;
849}
850
851
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700852static int nl80211_get_macaddr(struct i802_bss *bss)
853{
854 struct nl_msg *msg;
855 struct wiphy_idx_data data = {
856 .macaddr = bss->addr,
857 };
858
859 msg = nlmsg_alloc();
860 if (!msg)
861 return NL80211_IFTYPE_UNSPECIFIED;
862
863 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
864 if (nl80211_set_iface_id(msg, bss) < 0)
865 goto nla_put_failure;
866
867 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
868
869nla_put_failure:
870 nlmsg_free(msg);
871 return NL80211_IFTYPE_UNSPECIFIED;
872}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700873
874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
876 struct nl80211_wiphy_data *w)
877{
878 struct nl_msg *msg;
879 int ret = -1;
880
881 msg = nlmsg_alloc();
882 if (!msg)
883 return -1;
884
885 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
886
887 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
888
889 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
890 msg = NULL;
891 if (ret) {
892 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
893 "failed: ret=%d (%s)",
894 ret, strerror(-ret));
895 goto nla_put_failure;
896 }
897 ret = 0;
898nla_put_failure:
899 nlmsg_free(msg);
900 return ret;
901}
902
903
904static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
905{
906 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700907 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800908
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800909 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800910
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700911 res = nl_recvmsgs(handle, w->nl_cb);
912 if (res) {
913 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
914 __func__, res);
915 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800916}
917
918
919static int process_beacon_event(struct nl_msg *msg, void *arg)
920{
921 struct nl80211_wiphy_data *w = arg;
922 struct wpa_driver_nl80211_data *drv;
923 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
924 struct nlattr *tb[NL80211_ATTR_MAX + 1];
925 union wpa_event_data event;
926
927 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
928 genlmsg_attrlen(gnlh, 0), NULL);
929
930 if (gnlh->cmd != NL80211_CMD_FRAME) {
931 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
932 gnlh->cmd);
933 return NL_SKIP;
934 }
935
936 if (!tb[NL80211_ATTR_FRAME])
937 return NL_SKIP;
938
939 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
940 wiphy_list) {
941 os_memset(&event, 0, sizeof(event));
942 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
943 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
944 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
945 }
946
947 return NL_SKIP;
948}
949
950
951static struct nl80211_wiphy_data *
952nl80211_get_wiphy_data_ap(struct i802_bss *bss)
953{
954 static DEFINE_DL_LIST(nl80211_wiphys);
955 struct nl80211_wiphy_data *w;
956 int wiphy_idx, found = 0;
957 struct i802_bss *tmp_bss;
958
959 if (bss->wiphy_data != NULL)
960 return bss->wiphy_data;
961
962 wiphy_idx = nl80211_get_wiphy_index(bss);
963
964 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
965 if (w->wiphy_idx == wiphy_idx)
966 goto add;
967 }
968
969 /* alloc new one */
970 w = os_zalloc(sizeof(*w));
971 if (w == NULL)
972 return NULL;
973 w->wiphy_idx = wiphy_idx;
974 dl_list_init(&w->bsss);
975 dl_list_init(&w->drvs);
976
977 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
978 if (!w->nl_cb) {
979 os_free(w);
980 return NULL;
981 }
982 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
983 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
984 w);
985
986 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
987 "wiphy beacons");
988 if (w->nl_beacons == NULL) {
989 os_free(w);
990 return NULL;
991 }
992
993 if (nl80211_register_beacons(bss->drv, w)) {
994 nl_destroy_handles(&w->nl_beacons);
995 os_free(w);
996 return NULL;
997 }
998
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700999 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000
1001 dl_list_add(&nl80211_wiphys, &w->list);
1002
1003add:
1004 /* drv entry for this bss already there? */
1005 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1006 if (tmp_bss->drv == bss->drv) {
1007 found = 1;
1008 break;
1009 }
1010 }
1011 /* if not add it */
1012 if (!found)
1013 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
1014
1015 dl_list_add(&w->bsss, &bss->wiphy_list);
1016 bss->wiphy_data = w;
1017 return w;
1018}
1019
1020
1021static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1022{
1023 struct nl80211_wiphy_data *w = bss->wiphy_data;
1024 struct i802_bss *tmp_bss;
1025 int found = 0;
1026
1027 if (w == NULL)
1028 return;
1029 bss->wiphy_data = NULL;
1030 dl_list_del(&bss->wiphy_list);
1031
1032 /* still any for this drv present? */
1033 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1034 if (tmp_bss->drv == bss->drv) {
1035 found = 1;
1036 break;
1037 }
1038 }
1039 /* if not remove it */
1040 if (!found)
1041 dl_list_del(&bss->drv->wiphy_list);
1042
1043 if (!dl_list_empty(&w->bsss))
1044 return;
1045
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001046 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001047
1048 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 dl_list_del(&w->list);
1050 os_free(w);
1051}
1052
1053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1055{
1056 struct i802_bss *bss = priv;
1057 struct wpa_driver_nl80211_data *drv = bss->drv;
1058 if (!drv->associated)
1059 return -1;
1060 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1061 return 0;
1062}
1063
1064
1065static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1066{
1067 struct i802_bss *bss = priv;
1068 struct wpa_driver_nl80211_data *drv = bss->drv;
1069 if (!drv->associated)
1070 return -1;
1071 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1072 return drv->ssid_len;
1073}
1074
1075
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001076static void wpa_driver_nl80211_event_newlink(
1077 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078{
1079 union wpa_event_data event;
1080
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001081 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1082 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1083 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1084 drv->first_bss->ifname);
1085 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001087 if (!drv->if_removed)
1088 return;
1089 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1090 drv->first_bss->ifname);
1091 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 }
1093
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094 os_memset(&event, 0, sizeof(event));
1095 os_strlcpy(event.interface_status.ifname, ifname,
1096 sizeof(event.interface_status.ifname));
1097 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1098 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1099}
1100
1101
1102static void wpa_driver_nl80211_event_dellink(
1103 struct wpa_driver_nl80211_data *drv, char *ifname)
1104{
1105 union wpa_event_data event;
1106
1107 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1108 if (drv->if_removed) {
1109 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1110 ifname);
1111 return;
1112 }
1113 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1114 ifname);
1115 drv->if_removed = 1;
1116 } else {
1117 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1118 ifname);
1119 }
1120
1121 os_memset(&event, 0, sizeof(event));
1122 os_strlcpy(event.interface_status.ifname, ifname,
1123 sizeof(event.interface_status.ifname));
1124 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1126}
1127
1128
1129static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1130 u8 *buf, size_t len)
1131{
1132 int attrlen, rta_len;
1133 struct rtattr *attr;
1134
1135 attrlen = len;
1136 attr = (struct rtattr *) buf;
1137
1138 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1139 while (RTA_OK(attr, attrlen)) {
1140 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001141 if (os_strcmp(((char *) attr) + rta_len,
1142 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return 1;
1144 else
1145 break;
1146 }
1147 attr = RTA_NEXT(attr, attrlen);
1148 }
1149
1150 return 0;
1151}
1152
1153
1154static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1155 int ifindex, u8 *buf, size_t len)
1156{
1157 if (drv->ifindex == ifindex)
1158 return 1;
1159
1160 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1162 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001163 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 return 1;
1165 }
1166
1167 return 0;
1168}
1169
1170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001171static struct wpa_driver_nl80211_data *
1172nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1173{
1174 struct wpa_driver_nl80211_data *drv;
1175 dl_list_for_each(drv, &global->interfaces,
1176 struct wpa_driver_nl80211_data, list) {
1177 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1178 have_ifidx(drv, idx))
1179 return drv;
1180 }
1181 return NULL;
1182}
1183
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1186 struct ifinfomsg *ifi,
1187 u8 *buf, size_t len)
1188{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001189 struct nl80211_global *global = ctx;
1190 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001191 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 struct rtattr *attr;
1193 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001194 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001195 char ifname[IFNAMSIZ + 1];
1196 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1199 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001200 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1201 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 return;
1203 }
1204
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001205 extra[0] = '\0';
1206 pos = extra;
1207 end = pos + sizeof(extra);
1208 ifname[0] = '\0';
1209
1210 attrlen = len;
1211 attr = (struct rtattr *) buf;
1212 while (RTA_OK(attr, attrlen)) {
1213 switch (attr->rta_type) {
1214 case IFLA_IFNAME:
1215 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1216 break;
1217 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1218 ifname[RTA_PAYLOAD(attr)] = '\0';
1219 break;
1220 case IFLA_MASTER:
1221 brid = nla_get_u32((struct nlattr *) attr);
1222 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1223 break;
1224 case IFLA_WIRELESS:
1225 pos += os_snprintf(pos, end - pos, " wext");
1226 break;
1227 case IFLA_OPERSTATE:
1228 pos += os_snprintf(pos, end - pos, " operstate=%u",
1229 nla_get_u32((struct nlattr *) attr));
1230 break;
1231 case IFLA_LINKMODE:
1232 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1233 nla_get_u32((struct nlattr *) attr));
1234 break;
1235 }
1236 attr = RTA_NEXT(attr, attrlen);
1237 }
1238 extra[sizeof(extra) - 1] = '\0';
1239
1240 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_flags=0x%x (%s%s%s%s)",
1241 ifi->ifi_index, ifname, extra, ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1243 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1244 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1245 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1246
1247 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001248 if (if_indextoname(ifi->ifi_index, namebuf) &&
1249 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001250 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1252 "event since interface %s is up", namebuf);
1253 return;
1254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001256 if (drv->ignore_if_down_event) {
1257 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1258 "event generated by mode change");
1259 drv->ignore_if_down_event = 0;
1260 } else {
1261 drv->if_disabled = 1;
1262 wpa_supplicant_event(drv->ctx,
1263 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001264
1265 /*
1266 * Try to get drv again, since it may be removed as
1267 * part of the EVENT_INTERFACE_DISABLED handling for
1268 * dynamic interfaces
1269 */
1270 drv = nl80211_find_drv(global, ifi->ifi_index,
1271 buf, len);
1272 if (!drv)
1273 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 }
1276
1277 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001278 if (if_indextoname(ifi->ifi_index, namebuf) &&
1279 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001280 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001281 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1282 "event since interface %s is down",
1283 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001284 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001285 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1286 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001287 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001288 } else if (drv->if_removed) {
1289 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1290 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001291 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292 } else {
1293 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1294 drv->if_disabled = 0;
1295 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1296 NULL);
1297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 }
1299
1300 /*
1301 * Some drivers send the association event before the operup event--in
1302 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1303 * fails. This will hit us when wpa_supplicant does not need to do
1304 * IEEE 802.1X authentication
1305 */
1306 if (drv->operstate == 1 &&
1307 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001308 !(ifi->ifi_flags & IFF_RUNNING)) {
1309 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 }
1313
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001314 if (ifname[0])
1315 wpa_driver_nl80211_event_newlink(drv, ifname);
1316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 if (ifi->ifi_family == AF_BRIDGE && brid) {
1318 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 if_indextoname(brid, namebuf);
1320 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1321 brid, namebuf);
1322 add_ifidx(drv, brid);
1323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324}
1325
1326
1327static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1328 struct ifinfomsg *ifi,
1329 u8 *buf, size_t len)
1330{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001331 struct nl80211_global *global = ctx;
1332 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001333 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 struct rtattr *attr;
1335 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001336 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001338 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1339 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001340 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1341 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 return;
1343 }
1344
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001345 ifname[0] = '\0';
1346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 attrlen = len;
1348 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001350 switch (attr->rta_type) {
1351 case IFLA_IFNAME:
1352 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1353 break;
1354 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1355 ifname[RTA_PAYLOAD(attr)] = '\0';
1356 break;
1357 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001359 break;
1360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 attr = RTA_NEXT(attr, attrlen);
1362 }
1363
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001364 if (ifname[0])
1365 wpa_driver_nl80211_event_dellink(drv, ifname);
1366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 if (ifi->ifi_family == AF_BRIDGE && brid) {
1368 /* device has been removed from bridge */
1369 char namebuf[IFNAMSIZ];
1370 if_indextoname(brid, namebuf);
1371 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1372 "%s", brid, namebuf);
1373 del_ifidx(drv, brid);
1374 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375}
1376
1377
1378static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1379 const u8 *frame, size_t len)
1380{
1381 const struct ieee80211_mgmt *mgmt;
1382 union wpa_event_data event;
1383
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001384 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 mgmt = (const struct ieee80211_mgmt *) frame;
1386 if (len < 24 + sizeof(mgmt->u.auth)) {
1387 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1388 "frame");
1389 return;
1390 }
1391
1392 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001393 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 os_memset(&event, 0, sizeof(event));
1395 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1396 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001397 event.auth.auth_transaction =
1398 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1400 if (len > 24 + sizeof(mgmt->u.auth)) {
1401 event.auth.ies = mgmt->u.auth.variable;
1402 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1403 }
1404
1405 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1406}
1407
1408
Jouni Malinen87fd2792011-05-16 18:35:42 +03001409static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1410{
1411 struct nl_msg *msg;
1412 int ret;
1413 struct nl80211_bss_info_arg arg;
1414
1415 os_memset(&arg, 0, sizeof(arg));
1416 msg = nlmsg_alloc();
1417 if (!msg)
1418 goto nla_put_failure;
1419
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001420 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001421 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1422
1423 arg.drv = drv;
1424 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1425 msg = NULL;
1426 if (ret == 0) {
1427 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1428 "associated BSS from scan results: %u MHz",
1429 arg.assoc_freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001430 if (arg.assoc_freq)
1431 drv->assoc_freq = arg.assoc_freq;
1432 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001433 }
1434 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1435 "(%s)", ret, strerror(-ret));
1436nla_put_failure:
1437 nlmsg_free(msg);
1438 return drv->assoc_freq;
1439}
1440
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1443 const u8 *frame, size_t len)
1444{
1445 const struct ieee80211_mgmt *mgmt;
1446 union wpa_event_data event;
1447 u16 status;
1448
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001449 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 mgmt = (const struct ieee80211_mgmt *) frame;
1451 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1452 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1453 "frame");
1454 return;
1455 }
1456
1457 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1458 if (status != WLAN_STATUS_SUCCESS) {
1459 os_memset(&event, 0, sizeof(event));
1460 event.assoc_reject.bssid = mgmt->bssid;
1461 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1462 event.assoc_reject.resp_ies =
1463 (u8 *) mgmt->u.assoc_resp.variable;
1464 event.assoc_reject.resp_ies_len =
1465 len - 24 - sizeof(mgmt->u.assoc_resp);
1466 }
1467 event.assoc_reject.status_code = status;
1468
1469 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1470 return;
1471 }
1472
1473 drv->associated = 1;
1474 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001475 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476
1477 os_memset(&event, 0, sizeof(event));
1478 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1479 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1480 event.assoc_info.resp_ies_len =
1481 len - 24 - sizeof(mgmt->u.assoc_resp);
1482 }
1483
1484 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1487}
1488
1489
1490static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1491 enum nl80211_commands cmd, struct nlattr *status,
1492 struct nlattr *addr, struct nlattr *req_ie,
1493 struct nlattr *resp_ie)
1494{
1495 union wpa_event_data event;
1496
1497 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1498 /*
1499 * Avoid reporting two association events that would confuse
1500 * the core code.
1501 */
1502 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1503 "when using userspace SME", cmd);
1504 return;
1505 }
1506
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001507 if (cmd == NL80211_CMD_CONNECT)
1508 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1509 else if (cmd == NL80211_CMD_ROAM)
1510 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 os_memset(&event, 0, sizeof(event));
1513 if (cmd == NL80211_CMD_CONNECT &&
1514 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1515 if (addr)
1516 event.assoc_reject.bssid = nla_data(addr);
1517 if (resp_ie) {
1518 event.assoc_reject.resp_ies = nla_data(resp_ie);
1519 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1520 }
1521 event.assoc_reject.status_code = nla_get_u16(status);
1522 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1523 return;
1524 }
1525
1526 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001527 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001529 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1530 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531
1532 if (req_ie) {
1533 event.assoc_info.req_ies = nla_data(req_ie);
1534 event.assoc_info.req_ies_len = nla_len(req_ie);
1535 }
1536 if (resp_ie) {
1537 event.assoc_info.resp_ies = nla_data(resp_ie);
1538 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1539 }
1540
Jouni Malinen87fd2792011-05-16 18:35:42 +03001541 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1544}
1545
1546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001547static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001548 struct nlattr *reason, struct nlattr *addr,
1549 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001550{
1551 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001552 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553
1554 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1555 /*
1556 * Avoid reporting two disassociation events that could
1557 * confuse the core code.
1558 */
1559 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1560 "event when using userspace SME");
1561 return;
1562 }
1563
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001564 if (drv->ignore_next_local_disconnect) {
1565 drv->ignore_next_local_disconnect = 0;
1566 if (locally_generated) {
1567 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1568 "event triggered during reassociation");
1569 return;
1570 }
1571 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1572 "disconnect but got another disconnect "
1573 "event first");
1574 }
1575
1576 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001577 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001578 os_memset(&data, 0, sizeof(data));
1579 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001580 data.deauth_info.reason_code = nla_get_u16(reason);
1581 data.deauth_info.locally_generated = by_ap == NULL;
1582 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1583}
1584
1585
Dmitry Shmidt97672262014-02-03 13:02:54 -08001586static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1587{
1588 int freq1 = 0;
1589
1590 switch (convert2width(width)) {
1591 case CHAN_WIDTH_20_NOHT:
1592 case CHAN_WIDTH_20:
1593 return 0;
1594 case CHAN_WIDTH_40:
1595 freq1 = cf1 - 10;
1596 break;
1597 case CHAN_WIDTH_80:
1598 freq1 = cf1 - 30;
1599 break;
1600 case CHAN_WIDTH_160:
1601 freq1 = cf1 - 70;
1602 break;
1603 case CHAN_WIDTH_UNKNOWN:
1604 case CHAN_WIDTH_80P80:
1605 /* FIXME: implement this */
1606 return 0;
1607 }
1608
1609 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1610}
1611
1612
Dmitry Shmidt04949592012-07-19 12:16:46 -07001613static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001614 struct nlattr *ifindex, struct nlattr *freq,
1615 struct nlattr *type, struct nlattr *bw,
1616 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001617{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001618 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001619 union wpa_event_data data;
1620 int ht_enabled = 1;
1621 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001622 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001623
1624 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1625
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001626 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001627 return;
1628
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001629 ifidx = nla_get_u32(ifindex);
1630 for (bss = drv->first_bss; bss; bss = bss->next)
1631 if (bss->ifindex == ifidx)
1632 break;
1633
1634 if (bss == NULL) {
1635 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1636 ifidx);
1637 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001638 }
1639
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001640 if (type) {
1641 switch (nla_get_u32(type)) {
1642 case NL80211_CHAN_NO_HT:
1643 ht_enabled = 0;
1644 break;
1645 case NL80211_CHAN_HT20:
1646 break;
1647 case NL80211_CHAN_HT40PLUS:
1648 chan_offset = 1;
1649 break;
1650 case NL80211_CHAN_HT40MINUS:
1651 chan_offset = -1;
1652 break;
1653 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001654 } else if (bw && cf1) {
1655 /* This can happen for example with VHT80 ch switch */
1656 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1657 nla_get_u32(freq),
1658 nla_get_u32(cf1),
1659 cf2 ? nla_get_u32(cf2) : 0);
1660 } else {
1661 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001662 }
1663
1664 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001665 data.ch_switch.freq = nla_get_u32(freq);
1666 data.ch_switch.ht_enabled = ht_enabled;
1667 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001668 if (bw)
1669 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1670 if (cf1)
1671 data.ch_switch.cf1 = nla_get_u32(cf1);
1672 if (cf2)
1673 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001675 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001676
Dmitry Shmidt04949592012-07-19 12:16:46 -07001677 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001678}
1679
1680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1682 enum nl80211_commands cmd, struct nlattr *addr)
1683{
1684 union wpa_event_data event;
1685 enum wpa_event_type ev;
1686
1687 if (nla_len(addr) != ETH_ALEN)
1688 return;
1689
1690 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1691 cmd, MAC2STR((u8 *) nla_data(addr)));
1692
1693 if (cmd == NL80211_CMD_AUTHENTICATE)
1694 ev = EVENT_AUTH_TIMED_OUT;
1695 else if (cmd == NL80211_CMD_ASSOCIATE)
1696 ev = EVENT_ASSOC_TIMED_OUT;
1697 else
1698 return;
1699
1700 os_memset(&event, 0, sizeof(event));
1701 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1702 wpa_supplicant_event(drv->ctx, ev, &event);
1703}
1704
1705
Dmitry Shmidt98660862014-03-11 17:26:21 -07001706static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001707 struct nlattr *freq, struct nlattr *sig,
1708 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001710 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 const struct ieee80211_mgmt *mgmt;
1712 union wpa_event_data event;
1713 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001714 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001715 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001717 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 mgmt = (const struct ieee80211_mgmt *) frame;
1719 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001720 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 return;
1722 }
1723
1724 fc = le_to_host16(mgmt->frame_control);
1725 stype = WLAN_FC_GET_STYPE(fc);
1726
Dmitry Shmidt04949592012-07-19 12:16:46 -07001727 if (sig)
1728 ssi_signal = (s32) nla_get_u32(sig);
1729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 os_memset(&event, 0, sizeof(event));
1731 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001732 event.rx_mgmt.freq = nla_get_u32(freq);
1733 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001735 wpa_printf(MSG_DEBUG,
1736 "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
1737 rx_freq, ssi_signal, stype, (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001738 event.rx_mgmt.frame = frame;
1739 event.rx_mgmt.frame_len = len;
1740 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001741 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001742 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743}
1744
1745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001746static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1747 struct nlattr *cookie, const u8 *frame,
1748 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749{
1750 union wpa_event_data event;
1751 const struct ieee80211_hdr *hdr;
1752 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001754 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001755 if (!is_ap_interface(drv->nlmode)) {
1756 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001758 if (!cookie)
1759 return;
1760
1761 cookie_val = nla_get_u64(cookie);
1762 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1763 " cookie=0%llx%s (ack=%d)",
1764 (long long unsigned int) cookie_val,
1765 cookie_val == drv->send_action_cookie ?
1766 " (match)" : " (unknown)", ack != NULL);
1767 if (cookie_val != drv->send_action_cookie)
1768 return;
1769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770
1771 hdr = (const struct ieee80211_hdr *) frame;
1772 fc = le_to_host16(hdr->frame_control);
1773
1774 os_memset(&event, 0, sizeof(event));
1775 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1776 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1777 event.tx_status.dst = hdr->addr1;
1778 event.tx_status.data = frame;
1779 event.tx_status.data_len = len;
1780 event.tx_status.ack = ack != NULL;
1781 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1782}
1783
1784
1785static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1786 enum wpa_event_type type,
1787 const u8 *frame, size_t len)
1788{
1789 const struct ieee80211_mgmt *mgmt;
1790 union wpa_event_data event;
1791 const u8 *bssid = NULL;
1792 u16 reason_code = 0;
1793
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001794 if (type == EVENT_DEAUTH)
1795 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1796 else
1797 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 mgmt = (const struct ieee80211_mgmt *) frame;
1800 if (len >= 24) {
1801 bssid = mgmt->bssid;
1802
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001803 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1804 !drv->associated &&
1805 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1806 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1807 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1808 /*
1809 * Avoid issues with some roaming cases where
1810 * disconnection event for the old AP may show up after
1811 * we have started connection with the new AP.
1812 */
1813 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1814 MAC2STR(bssid),
1815 MAC2STR(drv->auth_attempt_bssid));
1816 return;
1817 }
1818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 if (drv->associated != 0 &&
1820 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1821 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1822 /*
1823 * We have presumably received this deauth as a
1824 * response to a clear_state_mismatch() outgoing
1825 * deauth. Don't let it take us offline!
1826 */
1827 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1828 "from Unknown BSSID " MACSTR " -- ignoring",
1829 MAC2STR(bssid));
1830 return;
1831 }
1832 }
1833
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001834 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 os_memset(&event, 0, sizeof(event));
1836
1837 /* Note: Same offset for Reason Code in both frame subtypes */
1838 if (len >= 24 + sizeof(mgmt->u.deauth))
1839 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1840
1841 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001842 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001843 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 event.disassoc_info.addr = bssid;
1845 event.disassoc_info.reason_code = reason_code;
1846 if (frame + len > mgmt->u.disassoc.variable) {
1847 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1848 event.disassoc_info.ie_len = frame + len -
1849 mgmt->u.disassoc.variable;
1850 }
1851 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001852 if (drv->ignore_deauth_event) {
1853 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1854 drv->ignore_deauth_event = 0;
1855 return;
1856 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001857 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001858 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001859 if (drv->ignore_next_local_deauth) {
1860 drv->ignore_next_local_deauth = 0;
1861 if (event.deauth_info.locally_generated) {
1862 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1863 return;
1864 }
1865 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1866 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 event.deauth_info.addr = bssid;
1868 event.deauth_info.reason_code = reason_code;
1869 if (frame + len > mgmt->u.deauth.variable) {
1870 event.deauth_info.ie = mgmt->u.deauth.variable;
1871 event.deauth_info.ie_len = frame + len -
1872 mgmt->u.deauth.variable;
1873 }
1874 }
1875
1876 wpa_supplicant_event(drv->ctx, type, &event);
1877}
1878
1879
1880static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1881 enum wpa_event_type type,
1882 const u8 *frame, size_t len)
1883{
1884 const struct ieee80211_mgmt *mgmt;
1885 union wpa_event_data event;
1886 u16 reason_code = 0;
1887
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001888 if (type == EVENT_UNPROT_DEAUTH)
1889 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1890 else
1891 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 if (len < 24)
1894 return;
1895
1896 mgmt = (const struct ieee80211_mgmt *) frame;
1897
1898 os_memset(&event, 0, sizeof(event));
1899 /* Note: Same offset for Reason Code in both frame subtypes */
1900 if (len >= 24 + sizeof(mgmt->u.deauth))
1901 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1902
1903 if (type == EVENT_UNPROT_DISASSOC) {
1904 event.unprot_disassoc.sa = mgmt->sa;
1905 event.unprot_disassoc.da = mgmt->da;
1906 event.unprot_disassoc.reason_code = reason_code;
1907 } else {
1908 event.unprot_deauth.sa = mgmt->sa;
1909 event.unprot_deauth.da = mgmt->da;
1910 event.unprot_deauth.reason_code = reason_code;
1911 }
1912
1913 wpa_supplicant_event(drv->ctx, type, &event);
1914}
1915
1916
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001917static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 enum nl80211_commands cmd, struct nlattr *frame,
1919 struct nlattr *addr, struct nlattr *timed_out,
1920 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001921 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001923 struct wpa_driver_nl80211_data *drv = bss->drv;
1924 const u8 *data;
1925 size_t len;
1926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 if (timed_out && addr) {
1928 mlme_timeout_event(drv, cmd, addr);
1929 return;
1930 }
1931
1932 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001933 wpa_printf(MSG_DEBUG,
1934 "nl80211: MLME event %d (%s) without frame data",
1935 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 return;
1937 }
1938
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001939 data = nla_data(frame);
1940 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001941 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001942 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1943 MACSTR ") - too short",
1944 cmd, nl80211_command_to_string(cmd), bss->ifname,
1945 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001946 return;
1947 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001948 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1949 ") A1=" MACSTR " A2=" MACSTR, cmd,
1950 nl80211_command_to_string(cmd), bss->ifname,
1951 MAC2STR(bss->addr), MAC2STR(data + 4),
1952 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001953 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001954 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1955 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001956 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1957 "for foreign address", bss->ifname);
1958 return;
1959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1961 nla_data(frame), nla_len(frame));
1962
1963 switch (cmd) {
1964 case NL80211_CMD_AUTHENTICATE:
1965 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1966 break;
1967 case NL80211_CMD_ASSOCIATE:
1968 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1969 break;
1970 case NL80211_CMD_DEAUTHENTICATE:
1971 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1972 nla_data(frame), nla_len(frame));
1973 break;
1974 case NL80211_CMD_DISASSOCIATE:
1975 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1976 nla_data(frame), nla_len(frame));
1977 break;
1978 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07001979 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001980 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 break;
1982 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1984 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 break;
1986 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1987 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1988 nla_data(frame), nla_len(frame));
1989 break;
1990 case NL80211_CMD_UNPROT_DISASSOCIATE:
1991 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1992 nla_data(frame), nla_len(frame));
1993 break;
1994 default:
1995 break;
1996 }
1997}
1998
1999
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002000static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 struct nlattr *tb[])
2002{
2003 union wpa_event_data data;
2004
2005 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2006 os_memset(&data, 0, sizeof(data));
2007 if (tb[NL80211_ATTR_MAC]) {
2008 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2009 nla_data(tb[NL80211_ATTR_MAC]),
2010 nla_len(tb[NL80211_ATTR_MAC]));
2011 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
2012 }
2013 if (tb[NL80211_ATTR_KEY_SEQ]) {
2014 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2015 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2016 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2017 }
2018 if (tb[NL80211_ATTR_KEY_TYPE]) {
2019 enum nl80211_key_type key_type =
2020 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2021 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2022 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2023 data.michael_mic_failure.unicast = 1;
2024 } else
2025 data.michael_mic_failure.unicast = 1;
2026
2027 if (tb[NL80211_ATTR_KEY_IDX]) {
2028 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2029 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2030 }
2031
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002032 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033}
2034
2035
2036static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2037 struct nlattr *tb[])
2038{
2039 if (tb[NL80211_ATTR_MAC] == NULL) {
2040 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2041 "event");
2042 return;
2043 }
2044 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 drv->associated = 1;
2047 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2048 MAC2STR(drv->bssid));
2049
2050 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2051}
2052
2053
2054static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2055 int cancel_event, struct nlattr *tb[])
2056{
2057 unsigned int freq, chan_type, duration;
2058 union wpa_event_data data;
2059 u64 cookie;
2060
2061 if (tb[NL80211_ATTR_WIPHY_FREQ])
2062 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2063 else
2064 freq = 0;
2065
2066 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2067 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2068 else
2069 chan_type = 0;
2070
2071 if (tb[NL80211_ATTR_DURATION])
2072 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2073 else
2074 duration = 0;
2075
2076 if (tb[NL80211_ATTR_COOKIE])
2077 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2078 else
2079 cookie = 0;
2080
2081 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2082 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2083 cancel_event, freq, chan_type, duration,
2084 (long long unsigned int) cookie,
2085 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2086
2087 if (cookie != drv->remain_on_chan_cookie)
2088 return; /* not for us */
2089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 if (cancel_event)
2091 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092
2093 os_memset(&data, 0, sizeof(data));
2094 data.remain_on_channel.freq = freq;
2095 data.remain_on_channel.duration = duration;
2096 wpa_supplicant_event(drv->ctx, cancel_event ?
2097 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2098 EVENT_REMAIN_ON_CHANNEL, &data);
2099}
2100
2101
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002102static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2103 struct nlattr *tb[])
2104{
2105 union wpa_event_data data;
2106
2107 os_memset(&data, 0, sizeof(data));
2108
2109 if (tb[NL80211_ATTR_IE]) {
2110 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2111 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2112 }
2113
2114 if (tb[NL80211_ATTR_IE_RIC]) {
2115 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2116 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2117 }
2118
2119 if (tb[NL80211_ATTR_MAC])
2120 os_memcpy(data.ft_ies.target_ap,
2121 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2122
2123 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2124 MAC2STR(data.ft_ies.target_ap));
2125
2126 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2127}
2128
2129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2131 struct nlattr *tb[])
2132{
2133 union wpa_event_data event;
2134 struct nlattr *nl;
2135 int rem;
2136 struct scan_info *info;
2137#define MAX_REPORT_FREQS 50
2138 int freqs[MAX_REPORT_FREQS];
2139 int num_freqs = 0;
2140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002141 if (drv->scan_for_auth) {
2142 drv->scan_for_auth = 0;
2143 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2144 "cfg80211 BSS entry");
2145 wpa_driver_nl80211_authenticate_retry(drv);
2146 return;
2147 }
2148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 os_memset(&event, 0, sizeof(event));
2150 info = &event.scan_info;
2151 info->aborted = aborted;
2152
2153 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2154 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2155 struct wpa_driver_scan_ssid *s =
2156 &info->ssids[info->num_ssids];
2157 s->ssid = nla_data(nl);
2158 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002159 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2160 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 info->num_ssids++;
2162 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2163 break;
2164 }
2165 }
2166 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002167 char msg[200], *pos, *end;
2168 int res;
2169
2170 pos = msg;
2171 end = pos + sizeof(msg);
2172 *pos = '\0';
2173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2175 {
2176 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002177 res = os_snprintf(pos, end - pos, " %d",
2178 freqs[num_freqs]);
2179 if (res > 0 && end - pos > res)
2180 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 num_freqs++;
2182 if (num_freqs == MAX_REPORT_FREQS - 1)
2183 break;
2184 }
2185 info->freqs = freqs;
2186 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002187 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2188 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 }
2190 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2191}
2192
2193
2194static int get_link_signal(struct nl_msg *msg, void *arg)
2195{
2196 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2197 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2198 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2199 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2200 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002201 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 };
2203 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2204 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2205 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2206 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2207 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2208 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2209 };
2210 struct wpa_signal_info *sig_change = arg;
2211
2212 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2213 genlmsg_attrlen(gnlh, 0), NULL);
2214 if (!tb[NL80211_ATTR_STA_INFO] ||
2215 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2216 tb[NL80211_ATTR_STA_INFO], policy))
2217 return NL_SKIP;
2218 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2219 return NL_SKIP;
2220
2221 sig_change->current_signal =
2222 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2223
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002224 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2225 sig_change->avg_signal =
2226 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2227 else
2228 sig_change->avg_signal = 0;
2229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2231 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2232 sinfo[NL80211_STA_INFO_TX_BITRATE],
2233 rate_policy)) {
2234 sig_change->current_txrate = 0;
2235 } else {
2236 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2237 sig_change->current_txrate =
2238 nla_get_u16(rinfo[
2239 NL80211_RATE_INFO_BITRATE]) * 100;
2240 }
2241 }
2242 }
2243
2244 return NL_SKIP;
2245}
2246
2247
2248static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2249 struct wpa_signal_info *sig)
2250{
2251 struct nl_msg *msg;
2252
2253 sig->current_signal = -9999;
2254 sig->current_txrate = 0;
2255
2256 msg = nlmsg_alloc();
2257 if (!msg)
2258 return -ENOMEM;
2259
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002260 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261
2262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2263 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2264
2265 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2266 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 return -ENOBUFS;
2269}
2270
2271
2272static int get_link_noise(struct nl_msg *msg, void *arg)
2273{
2274 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2275 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2276 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2277 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2278 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2279 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2280 };
2281 struct wpa_signal_info *sig_change = arg;
2282
2283 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2284 genlmsg_attrlen(gnlh, 0), NULL);
2285
2286 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2287 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2288 return NL_SKIP;
2289 }
2290
2291 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2292 tb[NL80211_ATTR_SURVEY_INFO],
2293 survey_policy)) {
2294 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2295 "attributes!");
2296 return NL_SKIP;
2297 }
2298
2299 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2300 return NL_SKIP;
2301
2302 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2303 sig_change->frequency)
2304 return NL_SKIP;
2305
2306 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2307 return NL_SKIP;
2308
2309 sig_change->current_noise =
2310 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2311
2312 return NL_SKIP;
2313}
2314
2315
2316static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2317 struct wpa_signal_info *sig_change)
2318{
2319 struct nl_msg *msg;
2320
2321 sig_change->current_noise = 9999;
2322 sig_change->frequency = drv->assoc_freq;
2323
2324 msg = nlmsg_alloc();
2325 if (!msg)
2326 return -ENOMEM;
2327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002328 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329
2330 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2331
2332 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2333 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002334 nlmsg_free(msg);
2335 return -ENOBUFS;
2336}
2337
2338
2339static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2340{
2341 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2342 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2343 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2344 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2345 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2346 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2347 };
2348 struct wpa_scan_results *scan_results = arg;
2349 struct wpa_scan_res *scan_res;
2350 size_t i;
2351
2352 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2353 genlmsg_attrlen(gnlh, 0), NULL);
2354
2355 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2356 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2357 return NL_SKIP;
2358 }
2359
2360 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2361 tb[NL80211_ATTR_SURVEY_INFO],
2362 survey_policy)) {
2363 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2364 "attributes");
2365 return NL_SKIP;
2366 }
2367
2368 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2369 return NL_SKIP;
2370
2371 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2372 return NL_SKIP;
2373
2374 for (i = 0; i < scan_results->num; ++i) {
2375 scan_res = scan_results->res[i];
2376 if (!scan_res)
2377 continue;
2378 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2379 scan_res->freq)
2380 continue;
2381 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2382 continue;
2383 scan_res->noise = (s8)
2384 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2385 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2386 }
2387
2388 return NL_SKIP;
2389}
2390
2391
2392static int nl80211_get_noise_for_scan_results(
2393 struct wpa_driver_nl80211_data *drv,
2394 struct wpa_scan_results *scan_res)
2395{
2396 struct nl_msg *msg;
2397
2398 msg = nlmsg_alloc();
2399 if (!msg)
2400 return -ENOMEM;
2401
2402 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2403
2404 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2405
2406 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2407 scan_res);
2408 nla_put_failure:
2409 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410 return -ENOBUFS;
2411}
2412
2413
2414static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2415 struct nlattr *tb[])
2416{
2417 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2418 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2419 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2420 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2421 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2422 };
2423 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2424 enum nl80211_cqm_rssi_threshold_event event;
2425 union wpa_event_data ed;
2426 struct wpa_signal_info sig;
2427 int res;
2428
2429 if (tb[NL80211_ATTR_CQM] == NULL ||
2430 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2431 cqm_policy)) {
2432 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2433 return;
2434 }
2435
2436 os_memset(&ed, 0, sizeof(ed));
2437
2438 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2439 if (!tb[NL80211_ATTR_MAC])
2440 return;
2441 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2442 ETH_ALEN);
2443 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2444 return;
2445 }
2446
2447 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2448 return;
2449 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2450
2451 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2452 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2453 "event: RSSI high");
2454 ed.signal_change.above_threshold = 1;
2455 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2456 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2457 "event: RSSI low");
2458 ed.signal_change.above_threshold = 0;
2459 } else
2460 return;
2461
2462 res = nl80211_get_link_signal(drv, &sig);
2463 if (res == 0) {
2464 ed.signal_change.current_signal = sig.current_signal;
2465 ed.signal_change.current_txrate = sig.current_txrate;
2466 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2467 sig.current_signal, sig.current_txrate);
2468 }
2469
2470 res = nl80211_get_link_noise(drv, &sig);
2471 if (res == 0) {
2472 ed.signal_change.current_noise = sig.current_noise;
2473 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2474 sig.current_noise);
2475 }
2476
2477 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2478}
2479
2480
2481static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2482 struct nlattr **tb)
2483{
2484 u8 *addr;
2485 union wpa_event_data data;
2486
2487 if (tb[NL80211_ATTR_MAC] == NULL)
2488 return;
2489 addr = nla_data(tb[NL80211_ATTR_MAC]);
2490 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002492 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002493 u8 *ies = NULL;
2494 size_t ies_len = 0;
2495 if (tb[NL80211_ATTR_IE]) {
2496 ies = nla_data(tb[NL80211_ATTR_IE]);
2497 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2498 }
2499 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2500 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2501 return;
2502 }
2503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2505 return;
2506
2507 os_memset(&data, 0, sizeof(data));
2508 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2509 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2510}
2511
2512
2513static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2514 struct nlattr **tb)
2515{
2516 u8 *addr;
2517 union wpa_event_data data;
2518
2519 if (tb[NL80211_ATTR_MAC] == NULL)
2520 return;
2521 addr = nla_data(tb[NL80211_ATTR_MAC]);
2522 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2523 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002525 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002526 drv_event_disassoc(drv->ctx, addr);
2527 return;
2528 }
2529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2531 return;
2532
2533 os_memset(&data, 0, sizeof(data));
2534 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2535 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2536}
2537
2538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002539static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2540 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2543 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2544 [NL80211_REKEY_DATA_KEK] = {
2545 .minlen = NL80211_KEK_LEN,
2546 .maxlen = NL80211_KEK_LEN,
2547 },
2548 [NL80211_REKEY_DATA_KCK] = {
2549 .minlen = NL80211_KCK_LEN,
2550 .maxlen = NL80211_KCK_LEN,
2551 },
2552 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2553 .minlen = NL80211_REPLAY_CTR_LEN,
2554 .maxlen = NL80211_REPLAY_CTR_LEN,
2555 },
2556 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 union wpa_event_data data;
2558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002559 if (!tb[NL80211_ATTR_MAC])
2560 return;
2561 if (!tb[NL80211_ATTR_REKEY_DATA])
2562 return;
2563 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2564 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2565 return;
2566 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2567 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002569 os_memset(&data, 0, sizeof(data));
2570 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2571 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2572 MAC2STR(data.driver_gtk_rekey.bssid));
2573 data.driver_gtk_rekey.replay_ctr =
2574 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2575 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2576 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2577 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2578}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580
2581static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2582 struct nlattr **tb)
2583{
2584 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2585 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2586 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2587 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2588 .minlen = ETH_ALEN,
2589 .maxlen = ETH_ALEN,
2590 },
2591 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2592 };
2593 union wpa_event_data data;
2594
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002595 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2598 return;
2599 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2600 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2601 return;
2602 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2603 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2604 return;
2605
2606 os_memset(&data, 0, sizeof(data));
2607 os_memcpy(data.pmkid_candidate.bssid,
2608 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2609 data.pmkid_candidate.index =
2610 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2611 data.pmkid_candidate.preauth =
2612 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2613 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2614}
2615
2616
2617static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2618 struct nlattr **tb)
2619{
2620 union wpa_event_data data;
2621
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002622 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002624 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2625 return;
2626
2627 os_memset(&data, 0, sizeof(data));
2628 os_memcpy(data.client_poll.addr,
2629 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2630
2631 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2632}
2633
2634
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002635static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2636 struct nlattr **tb)
2637{
2638 union wpa_event_data data;
2639
2640 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2641
2642 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2643 return;
2644
2645 os_memset(&data, 0, sizeof(data));
2646 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2647 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2648 case NL80211_TDLS_SETUP:
2649 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2650 MACSTR, MAC2STR(data.tdls.peer));
2651 data.tdls.oper = TDLS_REQUEST_SETUP;
2652 break;
2653 case NL80211_TDLS_TEARDOWN:
2654 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2655 MACSTR, MAC2STR(data.tdls.peer));
2656 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2657 break;
2658 default:
2659 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2660 "event");
2661 return;
2662 }
2663 if (tb[NL80211_ATTR_REASON_CODE]) {
2664 data.tdls.reason_code =
2665 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2666 }
2667
2668 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2669}
2670
2671
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002672static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2673 struct nlattr **tb)
2674{
2675 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2676}
2677
2678
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002679static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2680 struct nlattr **tb)
2681{
2682 union wpa_event_data data;
2683 u32 reason;
2684
2685 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2686
2687 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2688 return;
2689
2690 os_memset(&data, 0, sizeof(data));
2691 os_memcpy(data.connect_failed_reason.addr,
2692 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2693
2694 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2695 switch (reason) {
2696 case NL80211_CONN_FAIL_MAX_CLIENTS:
2697 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2698 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2699 break;
2700 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2701 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2702 " tried to connect",
2703 MAC2STR(data.connect_failed_reason.addr));
2704 data.connect_failed_reason.code = BLOCKED_CLIENT;
2705 break;
2706 default:
2707 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2708 "%u", reason);
2709 return;
2710 }
2711
2712 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2713}
2714
2715
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002716static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2717 struct nlattr **tb)
2718{
2719 union wpa_event_data data;
2720 enum nl80211_radar_event event_type;
2721
2722 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2723 return;
2724
2725 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002726 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2727 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002728
Dmitry Shmidt051af732013-10-22 13:52:46 -07002729 /* Check HT params */
2730 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2731 data.dfs_event.ht_enabled = 1;
2732 data.dfs_event.chan_offset = 0;
2733
2734 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2735 case NL80211_CHAN_NO_HT:
2736 data.dfs_event.ht_enabled = 0;
2737 break;
2738 case NL80211_CHAN_HT20:
2739 break;
2740 case NL80211_CHAN_HT40PLUS:
2741 data.dfs_event.chan_offset = 1;
2742 break;
2743 case NL80211_CHAN_HT40MINUS:
2744 data.dfs_event.chan_offset = -1;
2745 break;
2746 }
2747 }
2748
2749 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002750 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2751 data.dfs_event.chan_width =
2752 convert2width(nla_get_u32(
2753 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2754 if (tb[NL80211_ATTR_CENTER_FREQ1])
2755 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002756 if (tb[NL80211_ATTR_CENTER_FREQ2])
2757 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2758
2759 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2760 data.dfs_event.freq, data.dfs_event.ht_enabled,
2761 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2762 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002763
2764 switch (event_type) {
2765 case NL80211_RADAR_DETECTED:
2766 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2767 break;
2768 case NL80211_RADAR_CAC_FINISHED:
2769 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2770 break;
2771 case NL80211_RADAR_CAC_ABORTED:
2772 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2773 break;
2774 case NL80211_RADAR_NOP_FINISHED:
2775 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2776 break;
2777 default:
2778 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2779 "received", event_type);
2780 break;
2781 }
2782}
2783
2784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002785static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2786 int wds)
2787{
2788 struct wpa_driver_nl80211_data *drv = bss->drv;
2789 union wpa_event_data event;
2790
2791 if (!tb[NL80211_ATTR_MAC])
2792 return;
2793
2794 os_memset(&event, 0, sizeof(event));
2795 event.rx_from_unknown.bssid = bss->addr;
2796 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2797 event.rx_from_unknown.wds = wds;
2798
2799 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2800}
2801
2802
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002803static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2804 const u8 *data, size_t len)
2805{
2806 u32 i, count;
2807 union wpa_event_data event;
2808 struct wpa_freq_range *range = NULL;
2809 const struct qca_avoid_freq_list *freq_range;
2810
2811 freq_range = (const struct qca_avoid_freq_list *) data;
2812 if (len < sizeof(freq_range->count))
2813 return;
2814
2815 count = freq_range->count;
2816 if (len < sizeof(freq_range->count) +
2817 count * sizeof(struct qca_avoid_freq_range)) {
2818 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2819 (unsigned int) len);
2820 return;
2821 }
2822
2823 if (count > 0) {
2824 range = os_calloc(count, sizeof(struct wpa_freq_range));
2825 if (range == NULL)
2826 return;
2827 }
2828
2829 os_memset(&event, 0, sizeof(event));
2830 for (i = 0; i < count; i++) {
2831 unsigned int idx = event.freq_range.num;
2832 range[idx].min = freq_range->range[i].start_freq;
2833 range[idx].max = freq_range->range[i].end_freq;
2834 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2835 range[idx].min, range[idx].max);
2836 if (range[idx].min > range[idx].max) {
2837 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2838 continue;
2839 }
2840 event.freq_range.num++;
2841 }
2842 event.freq_range.range = range;
2843
2844 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2845
2846 os_free(range);
2847}
2848
2849
2850static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2851 u32 subcmd, u8 *data, size_t len)
2852{
2853 switch (subcmd) {
2854 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2855 qca_nl80211_avoid_freq(drv, data, len);
2856 break;
2857 default:
2858 wpa_printf(MSG_DEBUG,
2859 "nl80211: Ignore unsupported QCA vendor event %u",
2860 subcmd);
2861 break;
2862 }
2863}
2864
2865
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002866static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2867 struct nlattr **tb)
2868{
2869 u32 vendor_id, subcmd, wiphy = 0;
2870 int wiphy_idx;
2871 u8 *data = NULL;
2872 size_t len = 0;
2873
2874 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2875 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2876 return;
2877
2878 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2879 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2880
2881 if (tb[NL80211_ATTR_WIPHY])
2882 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2883
2884 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2885 wiphy, vendor_id, subcmd);
2886
2887 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2888 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2889 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2890 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2891 }
2892
2893 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2894 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2895 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2896 wiphy, wiphy_idx);
2897 return;
2898 }
2899
2900 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002901 case OUI_QCA:
2902 nl80211_vendor_event_qca(drv, subcmd, data, len);
2903 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002904 default:
2905 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2906 break;
2907 }
2908}
2909
2910
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002911static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
2912 struct nlattr *tb[])
2913{
2914 union wpa_event_data data;
2915 enum nl80211_reg_initiator init;
2916
2917 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2918
2919 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2920 return;
2921
2922 os_memset(&data, 0, sizeof(data));
2923 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
2924 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
2925 switch (init) {
2926 case NL80211_REGDOM_SET_BY_CORE:
2927 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
2928 break;
2929 case NL80211_REGDOM_SET_BY_USER:
2930 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
2931 break;
2932 case NL80211_REGDOM_SET_BY_DRIVER:
2933 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
2934 break;
2935 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2936 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
2937 break;
2938 }
2939
2940 if (tb[NL80211_ATTR_REG_TYPE]) {
2941 enum nl80211_reg_type type;
2942 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
2943 wpa_printf(MSG_DEBUG, " * type=%d", type);
2944 switch (type) {
2945 case NL80211_REGDOM_TYPE_COUNTRY:
2946 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
2947 break;
2948 case NL80211_REGDOM_TYPE_WORLD:
2949 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
2950 break;
2951 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
2952 data.channel_list_changed.type =
2953 REGDOM_TYPE_CUSTOM_WORLD;
2954 break;
2955 case NL80211_REGDOM_TYPE_INTERSECTION:
2956 data.channel_list_changed.type =
2957 REGDOM_TYPE_INTERSECTION;
2958 break;
2959 }
2960 }
2961
2962 if (tb[NL80211_ATTR_REG_ALPHA2]) {
2963 os_strlcpy(data.channel_list_changed.alpha2,
2964 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
2965 sizeof(data.channel_list_changed.alpha2));
2966 wpa_printf(MSG_DEBUG, " * alpha2=%s",
2967 data.channel_list_changed.alpha2);
2968 }
2969
2970 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
2971}
2972
2973
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002974static void do_process_drv_event(struct i802_bss *bss, int cmd,
2975 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002976{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002977 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002978 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002979
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002980 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2981 cmd, nl80211_command_to_string(cmd), bss->ifname);
2982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002983 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2984 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2985 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002986 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002987 drv->ap_scan_as_station);
2988 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989 }
2990
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002991 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002993 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07002994 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002995 if (drv->scan_for_auth) {
2996 /*
2997 * Cannot indicate EVENT_SCAN_STARTED here since we skip
2998 * EVENT_SCAN_RESULTS in scan_for_auth case and the
2999 * upper layer implementation could get confused about
3000 * scanning state.
3001 */
3002 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3003 break;
3004 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003005 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003007 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003008 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003009 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003010 break;
3011 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003012 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003013 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003014 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3015 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003017 wpa_dbg(drv->ctx, MSG_DEBUG,
3018 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003019 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 drv->scan_complete_events = 1;
3021 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3022 drv->ctx);
3023 send_scan_event(drv, 0, tb);
3024 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003026 wpa_dbg(drv->ctx, MSG_DEBUG,
3027 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003028 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003029 send_scan_event(drv, 0, tb);
3030 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003032 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003033 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 /*
3035 * Need to indicate that scan results are available in order
3036 * not to make wpa_supplicant stop its scanning.
3037 */
3038 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3039 drv->ctx);
3040 send_scan_event(drv, 1, tb);
3041 break;
3042 case NL80211_CMD_AUTHENTICATE:
3043 case NL80211_CMD_ASSOCIATE:
3044 case NL80211_CMD_DEAUTHENTICATE:
3045 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 case NL80211_CMD_FRAME_TX_STATUS:
3047 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3048 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003049 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3051 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003052 tb[NL80211_ATTR_COOKIE],
3053 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 break;
3055 case NL80211_CMD_CONNECT:
3056 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003057 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 tb[NL80211_ATTR_STATUS_CODE],
3059 tb[NL80211_ATTR_MAC],
3060 tb[NL80211_ATTR_REQ_IE],
3061 tb[NL80211_ATTR_RESP_IE]);
3062 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003064 mlme_event_ch_switch(drv,
3065 tb[NL80211_ATTR_IFINDEX],
3066 tb[NL80211_ATTR_WIPHY_FREQ],
3067 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3068 tb[NL80211_ATTR_CHANNEL_WIDTH],
3069 tb[NL80211_ATTR_CENTER_FREQ1],
3070 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003071 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003073 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003074 tb[NL80211_ATTR_MAC],
3075 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 break;
3077 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003078 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 break;
3080 case NL80211_CMD_JOIN_IBSS:
3081 mlme_event_join_ibss(drv, tb);
3082 break;
3083 case NL80211_CMD_REMAIN_ON_CHANNEL:
3084 mlme_event_remain_on_channel(drv, 0, tb);
3085 break;
3086 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3087 mlme_event_remain_on_channel(drv, 1, tb);
3088 break;
3089 case NL80211_CMD_NOTIFY_CQM:
3090 nl80211_cqm_event(drv, tb);
3091 break;
3092 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003093 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 break;
3095 case NL80211_CMD_REG_BEACON_HINT:
3096 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003097 os_memset(&data, 0, sizeof(data));
3098 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003100 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 break;
3102 case NL80211_CMD_NEW_STATION:
3103 nl80211_new_station_event(drv, tb);
3104 break;
3105 case NL80211_CMD_DEL_STATION:
3106 nl80211_del_station_event(drv, tb);
3107 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003108 case NL80211_CMD_SET_REKEY_OFFLOAD:
3109 nl80211_rekey_offload_event(drv, tb);
3110 break;
3111 case NL80211_CMD_PMKSA_CANDIDATE:
3112 nl80211_pmksa_candidate_event(drv, tb);
3113 break;
3114 case NL80211_CMD_PROBE_CLIENT:
3115 nl80211_client_probe_event(drv, tb);
3116 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003117 case NL80211_CMD_TDLS_OPER:
3118 nl80211_tdls_oper_event(drv, tb);
3119 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003120 case NL80211_CMD_CONN_FAILED:
3121 nl80211_connect_failed_event(drv, tb);
3122 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003123 case NL80211_CMD_FT_EVENT:
3124 mlme_event_ft_event(drv, tb);
3125 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003126 case NL80211_CMD_RADAR_DETECT:
3127 nl80211_radar_event(drv, tb);
3128 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003129 case NL80211_CMD_STOP_AP:
3130 nl80211_stop_ap(drv, tb);
3131 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003132 case NL80211_CMD_VENDOR:
3133 nl80211_vendor_event(drv, tb);
3134 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003135 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003136 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3137 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003138 break;
3139 }
3140}
3141
3142
3143static int process_drv_event(struct nl_msg *msg, void *arg)
3144{
3145 struct wpa_driver_nl80211_data *drv = arg;
3146 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3147 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003148 struct i802_bss *bss;
3149 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003150
3151 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3152 genlmsg_attrlen(gnlh, 0), NULL);
3153
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003154 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003155 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3156
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003157 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003158 if (ifidx == -1 || ifidx == bss->ifindex) {
3159 do_process_drv_event(bss, gnlh->cmd, tb);
3160 return NL_SKIP;
3161 }
3162 wpa_printf(MSG_DEBUG,
3163 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3164 gnlh->cmd, ifidx);
3165 } else if (tb[NL80211_ATTR_WDEV]) {
3166 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3167 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003168 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003169 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3170 do_process_drv_event(bss, gnlh->cmd, tb);
3171 return NL_SKIP;
3172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003173 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003174 wpa_printf(MSG_DEBUG,
3175 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3176 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003177 }
3178
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179 return NL_SKIP;
3180}
3181
3182
3183static int process_global_event(struct nl_msg *msg, void *arg)
3184{
3185 struct nl80211_global *global = arg;
3186 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3187 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003188 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003189 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003190 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003191 u64 wdev_id = 0;
3192 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003193
3194 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3195 genlmsg_attrlen(gnlh, 0), NULL);
3196
3197 if (tb[NL80211_ATTR_IFINDEX])
3198 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003199 else if (tb[NL80211_ATTR_WDEV]) {
3200 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3201 wdev_id_set = 1;
3202 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003203
Dmitry Shmidt04949592012-07-19 12:16:46 -07003204 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3205 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003206 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003207 if ((ifidx == -1 && !wdev_id_set) ||
3208 ifidx == bss->ifindex ||
3209 (wdev_id_set && bss->wdev_id_set &&
3210 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003211 do_process_drv_event(bss, gnlh->cmd, tb);
3212 return NL_SKIP;
3213 }
3214 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003215 }
3216
3217 return NL_SKIP;
3218}
3219
3220
3221static int process_bss_event(struct nl_msg *msg, void *arg)
3222{
3223 struct i802_bss *bss = arg;
3224 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3225 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3226
3227 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3228 genlmsg_attrlen(gnlh, 0), NULL);
3229
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003230 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3231 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3232 bss->ifname);
3233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234 switch (gnlh->cmd) {
3235 case NL80211_CMD_FRAME:
3236 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003237 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003238 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3239 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003240 tb[NL80211_ATTR_COOKIE],
3241 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003242 break;
3243 case NL80211_CMD_UNEXPECTED_FRAME:
3244 nl80211_spurious_frame(bss, tb, 0);
3245 break;
3246 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3247 nl80211_spurious_frame(bss, tb, 1);
3248 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 default:
3250 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3251 "(cmd=%d)", gnlh->cmd);
3252 break;
3253 }
3254
3255 return NL_SKIP;
3256}
3257
3258
3259static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3260 void *handle)
3261{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003262 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003263 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003265 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003267 res = nl_recvmsgs(handle, cb);
3268 if (res) {
3269 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3270 __func__, res);
3271 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272}
3273
3274
3275/**
3276 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3277 * @priv: driver_nl80211 private data
3278 * @alpha2_arg: country to which to switch to
3279 * Returns: 0 on success, -1 on failure
3280 *
3281 * This asks nl80211 to set the regulatory domain for given
3282 * country ISO / IEC alpha2.
3283 */
3284static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3285{
3286 struct i802_bss *bss = priv;
3287 struct wpa_driver_nl80211_data *drv = bss->drv;
3288 char alpha2[3];
3289 struct nl_msg *msg;
3290
3291 msg = nlmsg_alloc();
3292 if (!msg)
3293 return -ENOMEM;
3294
3295 alpha2[0] = alpha2_arg[0];
3296 alpha2[1] = alpha2_arg[1];
3297 alpha2[2] = '\0';
3298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003299 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300
3301 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3302 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3303 return -EINVAL;
3304 return 0;
3305nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003306 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 return -EINVAL;
3308}
3309
3310
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003311static int nl80211_get_country(struct nl_msg *msg, void *arg)
3312{
3313 char *alpha2 = arg;
3314 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3315 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3316
3317 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3318 genlmsg_attrlen(gnlh, 0), NULL);
3319 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3320 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3321 return NL_SKIP;
3322 }
3323 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3324 return NL_SKIP;
3325}
3326
3327
3328static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3329{
3330 struct i802_bss *bss = priv;
3331 struct wpa_driver_nl80211_data *drv = bss->drv;
3332 struct nl_msg *msg;
3333 int ret;
3334
3335 msg = nlmsg_alloc();
3336 if (!msg)
3337 return -ENOMEM;
3338
3339 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3340 alpha2[0] = '\0';
3341 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3342 if (!alpha2[0])
3343 ret = -1;
3344
3345 return ret;
3346}
3347
3348
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003349static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3350{
3351 u32 *feat = arg;
3352 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3353 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3354
3355 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3356 genlmsg_attrlen(gnlh, 0), NULL);
3357
3358 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3359 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3360
3361 return NL_SKIP;
3362}
3363
3364
3365static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3366{
3367 u32 feat = 0;
3368 struct nl_msg *msg;
3369
3370 msg = nlmsg_alloc();
3371 if (!msg)
3372 goto nla_put_failure;
3373
3374 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3375 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3376 return feat;
3377
3378 msg = NULL;
3379nla_put_failure:
3380 nlmsg_free(msg);
3381 return 0;
3382}
3383
3384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003386 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003387 struct wpa_driver_capa *capa;
3388
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003389 unsigned int num_multichan_concurrent;
3390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003391 unsigned int error:1;
3392 unsigned int device_ap_sme:1;
3393 unsigned int poll_command_supported:1;
3394 unsigned int data_tx_status:1;
3395 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003396 unsigned int auth_supported:1;
3397 unsigned int connect_supported:1;
3398 unsigned int p2p_go_supported:1;
3399 unsigned int p2p_client_supported:1;
3400 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003401 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003402 unsigned int set_qos_map_supported:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403};
3404
3405
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003406static unsigned int probe_resp_offload_support(int supp_protocols)
3407{
3408 unsigned int prot = 0;
3409
3410 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3411 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3412 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3413 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3414 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3415 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3416 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3417 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3418
3419 return prot;
3420}
3421
3422
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003423static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3424 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003426 struct nlattr *nl_mode;
3427 int i;
3428
3429 if (tb == NULL)
3430 return;
3431
3432 nla_for_each_nested(nl_mode, tb, i) {
3433 switch (nla_type(nl_mode)) {
3434 case NL80211_IFTYPE_AP:
3435 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3436 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003437 case NL80211_IFTYPE_ADHOC:
3438 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3439 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003440 case NL80211_IFTYPE_P2P_DEVICE:
3441 info->capa->flags |=
3442 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3443 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003444 case NL80211_IFTYPE_P2P_GO:
3445 info->p2p_go_supported = 1;
3446 break;
3447 case NL80211_IFTYPE_P2P_CLIENT:
3448 info->p2p_client_supported = 1;
3449 break;
3450 case NL80211_IFTYPE_MONITOR:
3451 info->monitor_supported = 1;
3452 break;
3453 }
3454 }
3455}
3456
3457
3458static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3459 struct nlattr *nl_combi)
3460{
3461 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3462 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3463 struct nlattr *nl_limit, *nl_mode;
3464 int err, rem_limit, rem_mode;
3465 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 static struct nla_policy
3467 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3468 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3469 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3470 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3471 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003472 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003473 },
3474 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3475 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3476 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3477 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003479 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3480 nl_combi, iface_combination_policy);
3481 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3482 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3483 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3484 return 0; /* broken combination */
3485
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003486 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3487 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3488
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003489 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3490 rem_limit) {
3491 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3492 nl_limit, iface_limit_policy);
3493 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3494 return 0; /* broken combination */
3495
3496 nla_for_each_nested(nl_mode,
3497 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3498 rem_mode) {
3499 int ift = nla_type(nl_mode);
3500 if (ift == NL80211_IFTYPE_P2P_GO ||
3501 ift == NL80211_IFTYPE_P2P_CLIENT)
3502 combination_has_p2p = 1;
3503 if (ift == NL80211_IFTYPE_STATION)
3504 combination_has_mgd = 1;
3505 }
3506 if (combination_has_p2p && combination_has_mgd)
3507 break;
3508 }
3509
3510 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003511 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003512 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003513
3514 info->p2p_concurrent = 1;
3515 if (info->num_multichan_concurrent < num_channels)
3516 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003517 }
3518
3519 return 0;
3520}
3521
3522
3523static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3524 struct nlattr *tb)
3525{
3526 struct nlattr *nl_combi;
3527 int rem_combi;
3528
3529 if (tb == NULL)
3530 return;
3531
3532 nla_for_each_nested(nl_combi, tb, rem_combi) {
3533 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3534 break;
3535 }
3536}
3537
3538
3539static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3540 struct nlattr *tb)
3541{
3542 struct nlattr *nl_cmd;
3543 int i;
3544
3545 if (tb == NULL)
3546 return;
3547
3548 nla_for_each_nested(nl_cmd, tb, i) {
3549 switch (nla_get_u32(nl_cmd)) {
3550 case NL80211_CMD_AUTHENTICATE:
3551 info->auth_supported = 1;
3552 break;
3553 case NL80211_CMD_CONNECT:
3554 info->connect_supported = 1;
3555 break;
3556 case NL80211_CMD_START_SCHED_SCAN:
3557 info->capa->sched_scan_supported = 1;
3558 break;
3559 case NL80211_CMD_PROBE_CLIENT:
3560 info->poll_command_supported = 1;
3561 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003562 case NL80211_CMD_CHANNEL_SWITCH:
3563 info->channel_switch_supported = 1;
3564 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003565 case NL80211_CMD_SET_QOS_MAP:
3566 info->set_qos_map_supported = 1;
3567 break;
3568 }
3569 }
3570}
3571
3572
3573static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3574 struct nlattr *tb)
3575{
3576 int i, num;
3577 u32 *ciphers;
3578
3579 if (tb == NULL)
3580 return;
3581
3582 num = nla_len(tb) / sizeof(u32);
3583 ciphers = nla_data(tb);
3584 for (i = 0; i < num; i++) {
3585 u32 c = ciphers[i];
3586
3587 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3588 c >> 24, (c >> 16) & 0xff,
3589 (c >> 8) & 0xff, c & 0xff);
3590 switch (c) {
3591 case WLAN_CIPHER_SUITE_CCMP_256:
3592 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3593 break;
3594 case WLAN_CIPHER_SUITE_GCMP_256:
3595 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3596 break;
3597 case WLAN_CIPHER_SUITE_CCMP:
3598 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3599 break;
3600 case WLAN_CIPHER_SUITE_GCMP:
3601 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3602 break;
3603 case WLAN_CIPHER_SUITE_TKIP:
3604 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3605 break;
3606 case WLAN_CIPHER_SUITE_WEP104:
3607 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3608 break;
3609 case WLAN_CIPHER_SUITE_WEP40:
3610 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3611 break;
3612 case WLAN_CIPHER_SUITE_AES_CMAC:
3613 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3614 break;
3615 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3616 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3617 break;
3618 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3619 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3620 break;
3621 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3622 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3623 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003624 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3625 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3626 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003627 }
3628 }
3629}
3630
3631
3632static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3633 struct nlattr *tb)
3634{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003635 if (tb)
3636 capa->max_remain_on_chan = nla_get_u32(tb);
3637}
3638
3639
3640static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3641 struct nlattr *ext_setup)
3642{
3643 if (tdls == NULL)
3644 return;
3645
3646 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3647 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3648
3649 if (ext_setup) {
3650 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3651 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3652 }
3653}
3654
3655
3656static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3657 struct nlattr *tb)
3658{
3659 u32 flags;
3660 struct wpa_driver_capa *capa = info->capa;
3661
3662 if (tb == NULL)
3663 return;
3664
3665 flags = nla_get_u32(tb);
3666
3667 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3668 info->data_tx_status = 1;
3669
3670 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3671 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3672
3673 if (flags & NL80211_FEATURE_SAE)
3674 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3675
3676 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3677 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003678
3679 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3680 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003681}
3682
3683
3684static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3685 struct nlattr *tb)
3686{
3687 u32 protocols;
3688
3689 if (tb == NULL)
3690 return;
3691
3692 protocols = nla_get_u32(tb);
3693 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3694 "mode");
3695 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3696 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3697}
3698
3699
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003700static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3701 struct nlattr *tb)
3702{
3703 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3704
3705 if (tb == NULL)
3706 return;
3707
3708 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3709 tb, NULL))
3710 return;
3711
3712 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3713 capa->wowlan_triggers.any = 1;
3714 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3715 capa->wowlan_triggers.disconnect = 1;
3716 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3717 capa->wowlan_triggers.magic_pkt = 1;
3718 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3719 capa->wowlan_triggers.gtk_rekey_failure = 1;
3720 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3721 capa->wowlan_triggers.eap_identity_req = 1;
3722 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3723 capa->wowlan_triggers.four_way_handshake = 1;
3724 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3725 capa->wowlan_triggers.rfkill_release = 1;
3726}
3727
3728
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003729static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3730{
3731 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3732 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3733 struct wiphy_info_data *info = arg;
3734 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003735 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3738 genlmsg_attrlen(gnlh, 0), NULL);
3739
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003740 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003741 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003742 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3743 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003745 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003748 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3749 capa->max_sched_scan_ssids =
3750 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3751
3752 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3753 capa->max_match_sets =
3754 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3755
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003756 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3757 capa->max_acl_mac_addrs =
3758 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3759
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003760 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3761 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3762 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003763 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003765 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3766 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3767 "off-channel TX");
3768 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3769 }
3770
3771 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3772 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3773 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3774 }
3775
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003776 wiphy_info_max_roc(capa,
3777 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003778
3779 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3780 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003782 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3783 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003785 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3786 info->device_ap_sme = 1;
3787
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003788 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3789 wiphy_info_probe_resp_offload(capa,
3790 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003791
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003792 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3793 drv->extended_capa == NULL) {
3794 drv->extended_capa =
3795 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3796 if (drv->extended_capa) {
3797 os_memcpy(drv->extended_capa,
3798 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3799 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3800 drv->extended_capa_len =
3801 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3802 }
3803 drv->extended_capa_mask =
3804 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3805 if (drv->extended_capa_mask) {
3806 os_memcpy(drv->extended_capa_mask,
3807 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3808 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3809 } else {
3810 os_free(drv->extended_capa);
3811 drv->extended_capa = NULL;
3812 drv->extended_capa_len = 0;
3813 }
3814 }
3815
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003816 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3817 struct nlattr *nl;
3818 int rem;
3819
3820 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3821 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003822 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003823 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3824 continue;
3825 }
3826 vinfo = nla_data(nl);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003827 if (vinfo->subcmd ==
3828 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)
3829 drv->dfs_vendor_cmd_avail = 1;
3830
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003831 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3832 vinfo->vendor_id, vinfo->subcmd);
3833 }
3834 }
3835
3836 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3837 struct nlattr *nl;
3838 int rem;
3839
3840 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3841 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003842 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003843 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3844 continue;
3845 }
3846 vinfo = nla_data(nl);
3847 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3848 vinfo->vendor_id, vinfo->subcmd);
3849 }
3850 }
3851
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003852 wiphy_info_wowlan_triggers(capa,
3853 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
3854
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003855 if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
3856 capa->max_stations =
3857 nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
3858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859 return NL_SKIP;
3860}
3861
3862
3863static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3864 struct wiphy_info_data *info)
3865{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003866 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 struct nl_msg *msg;
3868
3869 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003870 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003871 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872
3873 msg = nlmsg_alloc();
3874 if (!msg)
3875 return -1;
3876
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003877 feat = get_nl80211_protocol_features(drv);
3878 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3879 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3880 else
3881 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003883 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003884 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003885 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003887 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3888 return -1;
3889
3890 if (info->auth_supported)
3891 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3892 else if (!info->connect_supported) {
3893 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3894 "authentication/association or connect commands");
3895 info->error = 1;
3896 }
3897
3898 if (info->p2p_go_supported && info->p2p_client_supported)
3899 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3900 if (info->p2p_concurrent) {
3901 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3902 "interface (driver advertised support)");
3903 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3904 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3905 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003906 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003907 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3908 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003909 drv->capa.num_multichan_concurrent =
3910 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003911 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003912
3913 /* default to 5000 since early versions of mac80211 don't set it */
3914 if (!drv->capa.max_remain_on_chan)
3915 drv->capa.max_remain_on_chan = 5000;
3916
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003917 if (info->channel_switch_supported)
3918 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
3919
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003920 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921nla_put_failure:
3922 nlmsg_free(msg);
3923 return -1;
3924}
3925
3926
3927static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3928{
3929 struct wiphy_info_data info;
3930 if (wpa_driver_nl80211_get_info(drv, &info))
3931 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003932
3933 if (info.error)
3934 return -1;
3935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3938 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3939 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3940 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3942 WPA_DRIVER_AUTH_SHARED |
3943 WPA_DRIVER_AUTH_LEAP;
3944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3946 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003948
Dmitry Shmidta38abf92014-03-06 13:38:44 -08003949 /*
3950 * As all cfg80211 drivers must support cases where the AP interface is
3951 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
3952 * case that the user space daemon has crashed, they must be able to
3953 * cleanup all stations and key entries in the AP tear down flow. Thus,
3954 * this flag can/should always be set for cfg80211 drivers.
3955 */
3956 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
3957
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003958 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003959 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003960
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003961 /*
3962 * No AP SME is currently assumed to also indicate no AP MLME
3963 * in the driver/firmware.
3964 */
3965 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3966 }
3967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003968 drv->device_ap_sme = info.device_ap_sme;
3969 drv->poll_command_supported = info.poll_command_supported;
3970 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003971 if (info.set_qos_map_supported)
3972 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003973
3974 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003975 * If poll command and tx status are supported, mac80211 is new enough
3976 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003977 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003978 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003979
3980 if (drv->device_ap_sme && drv->use_monitor) {
3981 /*
3982 * Non-mac80211 drivers may not support monitor interface.
3983 * Make sure we do not get stuck with incorrect capability here
3984 * by explicitly testing this.
3985 */
3986 if (!info.monitor_supported) {
3987 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3988 "with device_ap_sme since no monitor mode "
3989 "support detected");
3990 drv->use_monitor = 0;
3991 }
3992 }
3993
3994 /*
3995 * If we aren't going to use monitor interfaces, but the
3996 * driver doesn't support data TX status, we won't get TX
3997 * status for EAPOL frames.
3998 */
3999 if (!drv->use_monitor && !info.data_tx_status)
4000 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001
4002 return 0;
4003}
4004
4005
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004006#ifdef ANDROID
4007static int android_genl_ctrl_resolve(struct nl_handle *handle,
4008 const char *name)
4009{
4010 /*
4011 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4012 * need to work around that.
4013 */
4014 struct nl_cache *cache = NULL;
4015 struct genl_family *nl80211 = NULL;
4016 int id = -1;
4017
4018 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4019 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4020 "netlink cache");
4021 goto fail;
4022 }
4023
4024 nl80211 = genl_ctrl_search_by_name(cache, name);
4025 if (nl80211 == NULL)
4026 goto fail;
4027
4028 id = genl_family_get_id(nl80211);
4029
4030fail:
4031 if (nl80211)
4032 genl_family_put(nl80211);
4033 if (cache)
4034 nl_cache_free(cache);
4035
4036 return id;
4037}
4038#define genl_ctrl_resolve android_genl_ctrl_resolve
4039#endif /* ANDROID */
4040
4041
4042static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043{
4044 int ret;
4045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4047 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4049 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004050 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051 }
4052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004053 global->nl = nl_create_handle(global->nl_cb, "nl");
4054 if (global->nl == NULL)
4055 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4058 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4060 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 }
4063
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004064 global->nl_event = nl_create_handle(global->nl_cb, "event");
4065 if (global->nl_event == NULL)
4066 goto err;
4067
4068 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004070 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 if (ret < 0) {
4072 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4073 "membership for scan events: %d (%s)",
4074 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 }
4077
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004078 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004080 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 if (ret < 0) {
4082 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4083 "membership for mlme events: %d (%s)",
4084 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004085 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 }
4087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004088 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004090 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 if (ret < 0) {
4092 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4093 "membership for regulatory events: %d (%s)",
4094 ret, strerror(-ret));
4095 /* Continue without regulatory events */
4096 }
4097
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004098 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4099 if (ret >= 0)
4100 ret = nl_socket_add_membership(global->nl_event, ret);
4101 if (ret < 0) {
4102 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4103 "membership for vendor events: %d (%s)",
4104 ret, strerror(-ret));
4105 /* Continue without vendor events */
4106 }
4107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004108 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4109 no_seq_check, NULL);
4110 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4111 process_global_event, global);
4112
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004113 nl80211_register_eloop_read(&global->nl_event,
4114 wpa_driver_nl80211_event_receive,
4115 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116
4117 return 0;
4118
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004119err:
4120 nl_destroy_handles(&global->nl_event);
4121 nl_destroy_handles(&global->nl);
4122 nl_cb_put(global->nl_cb);
4123 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 return -1;
4125}
4126
4127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004128static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4129{
4130 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4131 if (!drv->nl_cb) {
4132 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4133 return -1;
4134 }
4135
4136 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4137 no_seq_check, NULL);
4138 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4139 process_drv_event, drv);
4140
4141 return 0;
4142}
4143
4144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4146{
4147 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4148 /*
4149 * This may be for any interface; use ifdown event to disable
4150 * interface.
4151 */
4152}
4153
4154
4155static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4156{
4157 struct wpa_driver_nl80211_data *drv = ctx;
4158 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004159 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4161 "after rfkill unblock");
4162 return;
4163 }
4164 /* rtnetlink ifup handler will report interface as enabled */
4165}
4166
4167
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004168static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4169 void *eloop_ctx,
4170 void *handle)
4171{
4172 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4173 u8 data[2048];
4174 struct msghdr msg;
4175 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004176 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004177 struct cmsghdr *cmsg;
4178 int res, found_ee = 0, found_wifi = 0, acked = 0;
4179 union wpa_event_data event;
4180
4181 memset(&msg, 0, sizeof(msg));
4182 msg.msg_iov = &entry;
4183 msg.msg_iovlen = 1;
4184 entry.iov_base = data;
4185 entry.iov_len = sizeof(data);
4186 msg.msg_control = &control;
4187 msg.msg_controllen = sizeof(control);
4188
4189 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4190 /* if error or not fitting 802.3 header, return */
4191 if (res < 14)
4192 return;
4193
4194 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4195 {
4196 if (cmsg->cmsg_level == SOL_SOCKET &&
4197 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4198 int *ack;
4199
4200 found_wifi = 1;
4201 ack = (void *)CMSG_DATA(cmsg);
4202 acked = *ack;
4203 }
4204
4205 if (cmsg->cmsg_level == SOL_PACKET &&
4206 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4207 struct sock_extended_err *err =
4208 (struct sock_extended_err *)CMSG_DATA(cmsg);
4209
4210 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4211 found_ee = 1;
4212 }
4213 }
4214
4215 if (!found_ee || !found_wifi)
4216 return;
4217
4218 memset(&event, 0, sizeof(event));
4219 event.eapol_tx_status.dst = data;
4220 event.eapol_tx_status.data = data + 14;
4221 event.eapol_tx_status.data_len = res - 14;
4222 event.eapol_tx_status.ack = acked;
4223 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4224}
4225
4226
4227static int nl80211_init_bss(struct i802_bss *bss)
4228{
4229 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4230 if (!bss->nl_cb)
4231 return -1;
4232
4233 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4234 no_seq_check, NULL);
4235 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4236 process_bss_event, bss);
4237
4238 return 0;
4239}
4240
4241
4242static void nl80211_destroy_bss(struct i802_bss *bss)
4243{
4244 nl_cb_put(bss->nl_cb);
4245 bss->nl_cb = NULL;
4246}
4247
4248
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004249static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4250 void *global_priv, int hostapd,
4251 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252{
4253 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004254 struct rfkill_config *rcfg;
4255 struct i802_bss *bss;
4256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257 if (global_priv == NULL)
4258 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 drv = os_zalloc(sizeof(*drv));
4260 if (drv == NULL)
4261 return NULL;
4262 drv->global = global_priv;
4263 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004264 drv->hostapd = !!hostapd;
4265 drv->eapol_sock = -1;
4266 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4267 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004268
4269 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4270 if (!drv->first_bss) {
4271 os_free(drv);
4272 return NULL;
4273 }
4274 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004276 bss->ctx = ctx;
4277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4279 drv->monitor_ifidx = -1;
4280 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004281 drv->eapol_tx_sock = -1;
4282 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283
4284 if (wpa_driver_nl80211_init_nl(drv)) {
4285 os_free(drv);
4286 return NULL;
4287 }
4288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004289 if (nl80211_init_bss(bss))
4290 goto failed;
4291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 rcfg = os_zalloc(sizeof(*rcfg));
4293 if (rcfg == NULL)
4294 goto failed;
4295 rcfg->ctx = drv;
4296 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4297 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4298 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4299 drv->rfkill = rfkill_init(rcfg);
4300 if (drv->rfkill == NULL) {
4301 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4302 os_free(rcfg);
4303 }
4304
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004305 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4306 drv->start_iface_up = 1;
4307
4308 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 goto failed;
4310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004311 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4312 if (drv->eapol_tx_sock < 0)
4313 goto failed;
4314
4315 if (drv->data_tx_status) {
4316 int enabled = 1;
4317
4318 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4319 &enabled, sizeof(enabled)) < 0) {
4320 wpa_printf(MSG_DEBUG,
4321 "nl80211: wifi status sockopt failed\n");
4322 drv->data_tx_status = 0;
4323 if (!drv->use_monitor)
4324 drv->capa.flags &=
4325 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4326 } else {
4327 eloop_register_read_sock(drv->eapol_tx_sock,
4328 wpa_driver_nl80211_handle_eapol_tx_status,
4329 drv, NULL);
4330 }
4331 }
4332
4333 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004335 drv->in_interface_list = 1;
4336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337
4338 return bss;
4339
4340failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004341 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342 return NULL;
4343}
4344
4345
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004346/**
4347 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4348 * @ctx: context to be used when calling wpa_supplicant functions,
4349 * e.g., wpa_supplicant_event()
4350 * @ifname: interface name, e.g., wlan0
4351 * @global_priv: private driver global data from global_init()
4352 * Returns: Pointer to private data, %NULL on failure
4353 */
4354static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4355 void *global_priv)
4356{
4357 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4358}
4359
4360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004361static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 struct nl_handle *nl_handle,
4363 u16 type, const u8 *match, size_t match_len)
4364{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 struct nl_msg *msg;
4367 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004368 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369
4370 msg = nlmsg_alloc();
4371 if (!msg)
4372 return -1;
4373
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004374 buf[0] = '\0';
4375 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
4376 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p match=%s",
4377 type, nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004379 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4380
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004381 if (nl80211_set_iface_id(msg, bss) < 0)
4382 goto nla_put_failure;
4383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4385 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004387 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388 msg = NULL;
4389 if (ret) {
4390 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4391 "failed (type=%u): ret=%d (%s)",
4392 type, ret, strerror(-ret));
4393 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4394 match, match_len);
4395 goto nla_put_failure;
4396 }
4397 ret = 0;
4398nla_put_failure:
4399 nlmsg_free(msg);
4400 return ret;
4401}
4402
4403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004404static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4405{
4406 struct wpa_driver_nl80211_data *drv = bss->drv;
4407
4408 if (bss->nl_mgmt) {
4409 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4410 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4411 return -1;
4412 }
4413
4414 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4415 if (bss->nl_mgmt == NULL)
4416 return -1;
4417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004418 return 0;
4419}
4420
4421
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004422static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4423{
4424 nl80211_register_eloop_read(&bss->nl_mgmt,
4425 wpa_driver_nl80211_event_receive,
4426 bss->nl_cb);
4427}
4428
4429
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004430static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 const u8 *match, size_t match_len)
4432{
4433 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004434 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 type, match, match_len);
4436}
4437
4438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004439static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004441 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004442 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004443
4444 if (nl80211_alloc_mgmt_handle(bss))
4445 return -1;
4446 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4447 "handle %p", bss->nl_mgmt);
4448
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004449 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4450 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4451
4452 /* register for any AUTH message */
4453 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4454 }
4455
Dmitry Shmidt051af732013-10-22 13:52:46 -07004456#ifdef CONFIG_INTERWORKING
4457 /* QoS Map Configure */
4458 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004459 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004460#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004461#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004463 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004464 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004466 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004467 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004469 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004470 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004472 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004473 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004474 /* Protected GAS Initial Request */
4475 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4476 ret = -1;
4477 /* Protected GAS Initial Response */
4478 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4479 ret = -1;
4480 /* Protected GAS Comeback Request */
4481 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4482 ret = -1;
4483 /* Protected GAS Comeback Response */
4484 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4485 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004486#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4487#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004489 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4491 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004492 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004494 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495 (u8 *) "\x7f\x50\x6f\x9a\x09",
4496 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004497 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498#endif /* CONFIG_P2P */
4499#ifdef CONFIG_IEEE80211W
4500 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004501 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004502 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004504#ifdef CONFIG_TDLS
4505 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4506 /* TDLS Discovery Response */
4507 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4508 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004509 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004510 }
4511#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512
4513 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004514 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004515 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516 else
4517 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4518 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004520 /* WNM - BSS Transition Management Request */
4521 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004522 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004523 /* WNM-Sleep Mode Response */
4524 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004525 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004526
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004527#ifdef CONFIG_HS20
4528 /* WNM-Notification */
4529 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
4530 return -1;
4531#endif /* CONFIG_HS20 */
4532
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004533 nl80211_mgmt_handle_register_eloop(bss);
4534
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004535 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536}
4537
4538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004539static int nl80211_register_spurious_class3(struct i802_bss *bss)
4540{
4541 struct wpa_driver_nl80211_data *drv = bss->drv;
4542 struct nl_msg *msg;
4543 int ret = -1;
4544
4545 msg = nlmsg_alloc();
4546 if (!msg)
4547 return -1;
4548
4549 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4550
4551 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4552
4553 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4554 msg = NULL;
4555 if (ret) {
4556 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4557 "failed: ret=%d (%s)",
4558 ret, strerror(-ret));
4559 goto nla_put_failure;
4560 }
4561 ret = 0;
4562nla_put_failure:
4563 nlmsg_free(msg);
4564 return ret;
4565}
4566
4567
4568static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4569{
4570 static const int stypes[] = {
4571 WLAN_FC_STYPE_AUTH,
4572 WLAN_FC_STYPE_ASSOC_REQ,
4573 WLAN_FC_STYPE_REASSOC_REQ,
4574 WLAN_FC_STYPE_DISASSOC,
4575 WLAN_FC_STYPE_DEAUTH,
4576 WLAN_FC_STYPE_ACTION,
4577 WLAN_FC_STYPE_PROBE_REQ,
4578/* Beacon doesn't work as mac80211 doesn't currently allow
4579 * it, but it wouldn't really be the right thing anyway as
4580 * it isn't per interface ... maybe just dump the scan
4581 * results periodically for OLBC?
4582 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004583 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004584 };
4585 unsigned int i;
4586
4587 if (nl80211_alloc_mgmt_handle(bss))
4588 return -1;
4589 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4590 "handle %p", bss->nl_mgmt);
4591
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004592 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004593 if (nl80211_register_frame(bss, bss->nl_mgmt,
4594 (WLAN_FC_TYPE_MGMT << 2) |
4595 (stypes[i] << 4),
4596 NULL, 0) < 0) {
4597 goto out_err;
4598 }
4599 }
4600
4601 if (nl80211_register_spurious_class3(bss))
4602 goto out_err;
4603
4604 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4605 goto out_err;
4606
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004607 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004608 return 0;
4609
4610out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004611 nl_destroy_handles(&bss->nl_mgmt);
4612 return -1;
4613}
4614
4615
4616static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4617{
4618 if (nl80211_alloc_mgmt_handle(bss))
4619 return -1;
4620 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4621 "handle %p (device SME)", bss->nl_mgmt);
4622
4623 if (nl80211_register_frame(bss, bss->nl_mgmt,
4624 (WLAN_FC_TYPE_MGMT << 2) |
4625 (WLAN_FC_STYPE_ACTION << 4),
4626 NULL, 0) < 0)
4627 goto out_err;
4628
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004629 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004630 return 0;
4631
4632out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004633 nl_destroy_handles(&bss->nl_mgmt);
4634 return -1;
4635}
4636
4637
4638static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4639{
4640 if (bss->nl_mgmt == NULL)
4641 return;
4642 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4643 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004644 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004645
4646 nl80211_put_wiphy_data_ap(bss);
4647}
4648
4649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4651{
4652 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4653}
4654
4655
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004656static void nl80211_del_p2pdev(struct i802_bss *bss)
4657{
4658 struct wpa_driver_nl80211_data *drv = bss->drv;
4659 struct nl_msg *msg;
4660 int ret;
4661
4662 msg = nlmsg_alloc();
4663 if (!msg)
4664 return;
4665
4666 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4667 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4668
4669 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4670 msg = NULL;
4671
4672 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4673 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004674 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004675
4676nla_put_failure:
4677 nlmsg_free(msg);
4678}
4679
4680
4681static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4682{
4683 struct wpa_driver_nl80211_data *drv = bss->drv;
4684 struct nl_msg *msg;
4685 int ret = -1;
4686
4687 msg = nlmsg_alloc();
4688 if (!msg)
4689 return -1;
4690
4691 if (start)
4692 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4693 else
4694 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4695
4696 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4697
4698 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4699 msg = NULL;
4700
4701 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4702 start ? "Start" : "Stop",
4703 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004704 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004705
4706nla_put_failure:
4707 nlmsg_free(msg);
4708 return ret;
4709}
4710
4711
4712static int i802_set_iface_flags(struct i802_bss *bss, int up)
4713{
4714 enum nl80211_iftype nlmode;
4715
4716 nlmode = nl80211_get_ifmode(bss);
4717 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4718 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4719 bss->ifname, up);
4720 }
4721
4722 /* P2P Device has start/stop which is equivalent */
4723 return nl80211_set_p2pdev(bss, up);
4724}
4725
4726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004728wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4729 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004731 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004733 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734
4735 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004736 bss->ifindex = drv->ifindex;
4737 bss->wdev_id = drv->global->if_add_wdevid;
4738 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4739
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004740 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4741 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004742 drv->global->if_add_wdevid_set = 0;
4743
4744 if (wpa_driver_nl80211_capa(drv))
4745 return -1;
4746
4747 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4748 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004750 if (set_addr &&
4751 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4752 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4753 set_addr)))
4754 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004755
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004756 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4757 drv->start_mode_ap = 1;
4758
4759 if (drv->hostapd)
4760 nlmode = NL80211_IFTYPE_AP;
4761 else if (bss->if_dynamic)
4762 nlmode = nl80211_get_ifmode(bss);
4763 else
4764 nlmode = NL80211_IFTYPE_STATION;
4765
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004766 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004767 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004770
Dmitry Shmidt98660862014-03-11 17:26:21 -07004771 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004772 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773
Dmitry Shmidt98660862014-03-11 17:26:21 -07004774 if (!rfkill_is_blocked(drv->rfkill)) {
4775 int ret = i802_set_iface_flags(bss, 1);
4776 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4778 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004779 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004781 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4782 return ret;
4783 } else {
4784 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4785 "interface '%s' due to rfkill", bss->ifname);
4786 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4787 return 0;
4788 drv->if_disabled = 1;
4789 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 }
4791
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004792 if (!drv->hostapd)
4793 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4794 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004796 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4797 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 return -1;
4799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 if (send_rfkill_event) {
4801 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4802 drv, drv->ctx);
4803 }
4804
4805 return 0;
4806}
4807
4808
4809static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4810{
4811 struct nl_msg *msg;
4812
4813 msg = nlmsg_alloc();
4814 if (!msg)
4815 return -ENOMEM;
4816
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004817 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4818 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004819 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4821
4822 return send_and_recv_msgs(drv, msg, NULL, NULL);
4823 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004824 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004825 return -ENOBUFS;
4826}
4827
4828
4829/**
4830 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004831 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 *
4833 * Shut down driver interface and processing of driver events. Free
4834 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4835 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004836static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 struct wpa_driver_nl80211_data *drv = bss->drv;
4839
Dmitry Shmidt04949592012-07-19 12:16:46 -07004840 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004841 if (drv->data_tx_status)
4842 eloop_unregister_read_sock(drv->eapol_tx_sock);
4843 if (drv->eapol_tx_sock >= 0)
4844 close(drv->eapol_tx_sock);
4845
4846 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 wpa_driver_nl80211_probe_req_report(bss, 0);
4848 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004849 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4850 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4852 "interface %s from bridge %s: %s",
4853 bss->ifname, bss->brname, strerror(errno));
4854 }
4855 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004856 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4858 "bridge %s: %s",
4859 bss->brname, strerror(errno));
4860 }
4861
4862 nl80211_remove_monitor_interface(drv);
4863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004864 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 wpa_driver_nl80211_del_beacon(drv);
4866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 if (drv->eapol_sock >= 0) {
4868 eloop_unregister_read_sock(drv->eapol_sock);
4869 close(drv->eapol_sock);
4870 }
4871
4872 if (drv->if_indices != drv->default_if_indices)
4873 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004875 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004878 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4879 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004880 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 rfkill_deinit(drv->rfkill);
4882
4883 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4884
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004885 if (!drv->start_iface_up)
4886 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004887 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004888 if (!drv->hostapd || !drv->start_mode_ap)
4889 wpa_driver_nl80211_set_mode(bss,
4890 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004891 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004892 } else {
4893 nl80211_mgmt_unsubscribe(bss, "deinit");
4894 nl80211_del_p2pdev(bss);
4895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 nl_cb_put(drv->nl_cb);
4897
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004898 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 os_free(drv->filter_ssids);
4901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004902 os_free(drv->auth_ie);
4903
4904 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 dl_list_del(&drv->list);
4906
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004907 os_free(drv->extended_capa);
4908 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004909 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 os_free(drv);
4911}
4912
4913
4914/**
4915 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4916 * @eloop_ctx: Driver private data
4917 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4918 *
4919 * This function can be used as registered timeout when starting a scan to
4920 * generate a scan completed event if the driver does not report this.
4921 */
4922static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4923{
4924 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004925 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004926 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004927 drv->ap_scan_as_station);
4928 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929 }
4930 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4931 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4932}
4933
4934
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004935static struct nl_msg *
4936nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004937 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004938{
4939 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004940 size_t i;
4941
4942 msg = nlmsg_alloc();
4943 if (!msg)
4944 return NULL;
4945
4946 nl80211_cmd(drv, msg, 0, cmd);
4947
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004948 if (!wdev_id)
4949 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4950 else
4951 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004952
4953 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004954 struct nlattr *ssids;
4955
4956 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004957 if (ssids == NULL)
4958 goto fail;
4959 for (i = 0; i < params->num_ssids; i++) {
4960 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4961 params->ssids[i].ssid,
4962 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004963 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4964 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004965 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004966 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004967 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004968 }
4969
4970 if (params->extra_ies) {
4971 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4972 params->extra_ies, params->extra_ies_len);
4973 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4974 params->extra_ies) < 0)
4975 goto fail;
4976 }
4977
4978 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004979 struct nlattr *freqs;
4980 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004981 if (freqs == NULL)
4982 goto fail;
4983 for (i = 0; params->freqs[i]; i++) {
4984 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4985 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004986 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004987 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004988 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004989 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004990 }
4991
4992 os_free(drv->filter_ssids);
4993 drv->filter_ssids = params->filter_ssids;
4994 params->filter_ssids = NULL;
4995 drv->num_filter_ssids = params->num_filter_ssids;
4996
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004997 if (params->only_new_results) {
4998 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
4999 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS,
5000 NL80211_SCAN_FLAG_FLUSH);
5001 }
5002
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005003 return msg;
5004
5005fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005006nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005007 nlmsg_free(msg);
5008 return NULL;
5009}
5010
5011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012/**
5013 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005014 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015 * @params: Scan parameters
5016 * Returns: 0 on success, -1 on failure
5017 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005018static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 struct wpa_driver_scan_params *params)
5020{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005022 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005023 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005025 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005026 drv->scan_for_auth = 0;
5027
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005028 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5029 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005030 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005033 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005034 struct nlattr *rates;
5035
Dmitry Shmidt04949592012-07-19 12:16:46 -07005036 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5037
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005038 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005039 if (rates == NULL)
5040 goto nla_put_failure;
5041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042 /*
5043 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5044 * by masking out everything else apart from the OFDM rates 6,
5045 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5046 * rates are left enabled.
5047 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005048 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005049 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005050 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005051
5052 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
5053 }
5054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5056 msg = NULL;
5057 if (ret) {
5058 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5059 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005060 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005061 enum nl80211_iftype old_mode = drv->nlmode;
5062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 /*
5064 * mac80211 does not allow scan requests in AP mode, so
5065 * try to do this in station mode.
5066 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005067 if (wpa_driver_nl80211_set_mode(
5068 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069 goto nla_put_failure;
5070
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005071 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005072 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005073 goto nla_put_failure;
5074 }
5075
5076 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005077 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 ret = 0;
5079 } else
5080 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081 }
5082
Dmitry Shmidt56052862013-10-04 10:23:25 -07005083 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084 /* Not all drivers generate "scan completed" wireless event, so try to
5085 * read results after a timeout. */
5086 timeout = 10;
5087 if (drv->scan_complete_events) {
5088 /*
5089 * The driver seems to deliver events to notify when scan is
5090 * complete, so use longer timeout to avoid race conditions
5091 * with scanning and following association request.
5092 */
5093 timeout = 30;
5094 }
5095 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5096 "seconds", ret, timeout);
5097 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5098 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5099 drv, drv->ctx);
5100
5101nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005103 return ret;
5104}
5105
5106
5107/**
5108 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5109 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5110 * @params: Scan parameters
5111 * @interval: Interval between scan cycles in milliseconds
5112 * Returns: 0 on success, -1 on failure or if not supported
5113 */
5114static int wpa_driver_nl80211_sched_scan(void *priv,
5115 struct wpa_driver_scan_params *params,
5116 u32 interval)
5117{
5118 struct i802_bss *bss = priv;
5119 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005120 int ret = -1;
5121 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005122 size_t i;
5123
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005124 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5125
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005126#ifdef ANDROID
5127 if (!drv->capa.sched_scan_supported)
5128 return android_pno_start(bss, params);
5129#endif /* ANDROID */
5130
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005131 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5132 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005133 if (!msg)
5134 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005135
5136 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5137
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005138 if ((drv->num_filter_ssids &&
5139 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5140 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005141 struct nlattr *match_sets;
5142 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005143 if (match_sets == NULL)
5144 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005145
5146 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005147 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005148 wpa_hexdump_ascii(MSG_MSGDUMP,
5149 "nl80211: Sched scan filter SSID",
5150 drv->filter_ssids[i].ssid,
5151 drv->filter_ssids[i].ssid_len);
5152
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005153 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005154 if (match_set_ssid == NULL)
5155 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005156 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005157 drv->filter_ssids[i].ssid_len,
5158 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005159 if (params->filter_rssi)
5160 NLA_PUT_U32(msg,
5161 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5162 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005163
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005164 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005165 }
5166
Dmitry Shmidt97672262014-02-03 13:02:54 -08005167 /*
5168 * Due to backward compatibility code, newer kernels treat this
5169 * matchset (with only an RSSI filter) as the default for all
5170 * other matchsets, unless it's the only one, in which case the
5171 * matchset will actually allow all SSIDs above the RSSI.
5172 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005173 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005174 struct nlattr *match_set_rssi;
5175 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005176 if (match_set_rssi == NULL)
5177 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005178 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005179 params->filter_rssi);
5180 wpa_printf(MSG_MSGDUMP,
5181 "nl80211: Sched scan RSSI filter %d dBm",
5182 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005183 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005184 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005185
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005186 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005187 }
5188
5189 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5190
5191 /* TODO: if we get an error here, we should fall back to normal scan */
5192
5193 msg = NULL;
5194 if (ret) {
5195 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5196 "ret=%d (%s)", ret, strerror(-ret));
5197 goto nla_put_failure;
5198 }
5199
5200 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5201 "scan interval %d msec", ret, interval);
5202
5203nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005204 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005205 return ret;
5206}
5207
5208
5209/**
5210 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5211 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5212 * Returns: 0 on success, -1 on failure or if not supported
5213 */
5214static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5215{
5216 struct i802_bss *bss = priv;
5217 struct wpa_driver_nl80211_data *drv = bss->drv;
5218 int ret = 0;
5219 struct nl_msg *msg;
5220
5221#ifdef ANDROID
5222 if (!drv->capa.sched_scan_supported)
5223 return android_pno_stop(bss);
5224#endif /* ANDROID */
5225
5226 msg = nlmsg_alloc();
5227 if (!msg)
5228 return -1;
5229
5230 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5231
5232 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5233
5234 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5235 msg = NULL;
5236 if (ret) {
5237 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5238 "ret=%d (%s)", ret, strerror(-ret));
5239 goto nla_put_failure;
5240 }
5241
5242 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5243
5244nla_put_failure:
5245 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 return ret;
5247}
5248
5249
5250static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5251{
5252 const u8 *end, *pos;
5253
5254 if (ies == NULL)
5255 return NULL;
5256
5257 pos = ies;
5258 end = ies + ies_len;
5259
5260 while (pos + 1 < end) {
5261 if (pos + 2 + pos[1] > end)
5262 break;
5263 if (pos[0] == ie)
5264 return pos;
5265 pos += 2 + pos[1];
5266 }
5267
5268 return NULL;
5269}
5270
5271
5272static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5273 const u8 *ie, size_t ie_len)
5274{
5275 const u8 *ssid;
5276 size_t i;
5277
5278 if (drv->filter_ssids == NULL)
5279 return 0;
5280
5281 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5282 if (ssid == NULL)
5283 return 1;
5284
5285 for (i = 0; i < drv->num_filter_ssids; i++) {
5286 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5287 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5288 0)
5289 return 0;
5290 }
5291
5292 return 1;
5293}
5294
5295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296static int bss_info_handler(struct nl_msg *msg, void *arg)
5297{
5298 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5299 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5300 struct nlattr *bss[NL80211_BSS_MAX + 1];
5301 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5302 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5303 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5304 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5305 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5306 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5307 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5308 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5309 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5310 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5311 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5312 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5313 };
5314 struct nl80211_bss_info_arg *_arg = arg;
5315 struct wpa_scan_results *res = _arg->res;
5316 struct wpa_scan_res **tmp;
5317 struct wpa_scan_res *r;
5318 const u8 *ie, *beacon_ie;
5319 size_t ie_len, beacon_ie_len;
5320 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005321 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005322
5323 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5324 genlmsg_attrlen(gnlh, 0), NULL);
5325 if (!tb[NL80211_ATTR_BSS])
5326 return NL_SKIP;
5327 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5328 bss_policy))
5329 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005330 if (bss[NL80211_BSS_STATUS]) {
5331 enum nl80211_bss_status status;
5332 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5333 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5334 bss[NL80211_BSS_FREQUENCY]) {
5335 _arg->assoc_freq =
5336 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5337 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5338 _arg->assoc_freq);
5339 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005340 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5341 bss[NL80211_BSS_BSSID]) {
5342 os_memcpy(_arg->assoc_bssid,
5343 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5344 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5345 MACSTR, MAC2STR(_arg->assoc_bssid));
5346 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005347 }
5348 if (!res)
5349 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5351 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5352 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5353 } else {
5354 ie = NULL;
5355 ie_len = 0;
5356 }
5357 if (bss[NL80211_BSS_BEACON_IES]) {
5358 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5359 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5360 } else {
5361 beacon_ie = NULL;
5362 beacon_ie_len = 0;
5363 }
5364
5365 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5366 ie ? ie_len : beacon_ie_len))
5367 return NL_SKIP;
5368
5369 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5370 if (r == NULL)
5371 return NL_SKIP;
5372 if (bss[NL80211_BSS_BSSID])
5373 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5374 ETH_ALEN);
5375 if (bss[NL80211_BSS_FREQUENCY])
5376 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5377 if (bss[NL80211_BSS_BEACON_INTERVAL])
5378 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5379 if (bss[NL80211_BSS_CAPABILITY])
5380 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5381 r->flags |= WPA_SCAN_NOISE_INVALID;
5382 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5383 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5384 r->level /= 100; /* mBm to dBm */
5385 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5386 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5387 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005388 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 } else
5390 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5391 if (bss[NL80211_BSS_TSF])
5392 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5393 if (bss[NL80211_BSS_SEEN_MS_AGO])
5394 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5395 r->ie_len = ie_len;
5396 pos = (u8 *) (r + 1);
5397 if (ie) {
5398 os_memcpy(pos, ie, ie_len);
5399 pos += ie_len;
5400 }
5401 r->beacon_ie_len = beacon_ie_len;
5402 if (beacon_ie)
5403 os_memcpy(pos, beacon_ie, beacon_ie_len);
5404
5405 if (bss[NL80211_BSS_STATUS]) {
5406 enum nl80211_bss_status status;
5407 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5408 switch (status) {
5409 case NL80211_BSS_STATUS_AUTHENTICATED:
5410 r->flags |= WPA_SCAN_AUTHENTICATED;
5411 break;
5412 case NL80211_BSS_STATUS_ASSOCIATED:
5413 r->flags |= WPA_SCAN_ASSOCIATED;
5414 break;
5415 default:
5416 break;
5417 }
5418 }
5419
Jouni Malinen87fd2792011-05-16 18:35:42 +03005420 /*
5421 * cfg80211 maintains separate BSS table entries for APs if the same
5422 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5423 * not use frequency as a separate key in the BSS table, so filter out
5424 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005425 * order to get the correct frequency into the BSS table. Similarly,
5426 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005427 */
5428 for (i = 0; i < res->num; i++) {
5429 const u8 *s1, *s2;
5430 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5431 continue;
5432
5433 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5434 res->res[i]->ie_len, WLAN_EID_SSID);
5435 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5436 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5437 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5438 continue;
5439
5440 /* Same BSSID,SSID was already included in scan results */
5441 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5442 "for " MACSTR, MAC2STR(r->bssid));
5443
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005444 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5445 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5446 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005447 os_free(res->res[i]);
5448 res->res[i] = r;
5449 } else
5450 os_free(r);
5451 return NL_SKIP;
5452 }
5453
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005454 tmp = os_realloc_array(res->res, res->num + 1,
5455 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 if (tmp == NULL) {
5457 os_free(r);
5458 return NL_SKIP;
5459 }
5460 tmp[res->num++] = r;
5461 res->res = tmp;
5462
5463 return NL_SKIP;
5464}
5465
5466
5467static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5468 const u8 *addr)
5469{
5470 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5471 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5472 "mismatch (" MACSTR ")", MAC2STR(addr));
5473 wpa_driver_nl80211_mlme(drv, addr,
5474 NL80211_CMD_DEAUTHENTICATE,
5475 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5476 }
5477}
5478
5479
5480static void wpa_driver_nl80211_check_bss_status(
5481 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5482{
5483 size_t i;
5484
5485 for (i = 0; i < res->num; i++) {
5486 struct wpa_scan_res *r = res->res[i];
5487 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5488 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5489 "indicates BSS status with " MACSTR
5490 " as authenticated",
5491 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005492 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5494 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5495 0) {
5496 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5497 " in local state (auth=" MACSTR
5498 " assoc=" MACSTR ")",
5499 MAC2STR(drv->auth_bssid),
5500 MAC2STR(drv->bssid));
5501 clear_state_mismatch(drv, r->bssid);
5502 }
5503 }
5504
5505 if (r->flags & WPA_SCAN_ASSOCIATED) {
5506 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5507 "indicate BSS status with " MACSTR
5508 " as associated",
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 !drv->associated) {
5512 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5513 "(not associated) does not match "
5514 "with BSS state");
5515 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005516 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5518 0) {
5519 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5520 "(associated with " MACSTR ") does "
5521 "not match with BSS state",
5522 MAC2STR(drv->bssid));
5523 clear_state_mismatch(drv, r->bssid);
5524 clear_state_mismatch(drv, drv->bssid);
5525 }
5526 }
5527 }
5528}
5529
5530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005531static struct wpa_scan_results *
5532nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5533{
5534 struct nl_msg *msg;
5535 struct wpa_scan_results *res;
5536 int ret;
5537 struct nl80211_bss_info_arg arg;
5538
5539 res = os_zalloc(sizeof(*res));
5540 if (res == NULL)
5541 return NULL;
5542 msg = nlmsg_alloc();
5543 if (!msg)
5544 goto nla_put_failure;
5545
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005546 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005547 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005548 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549
5550 arg.drv = drv;
5551 arg.res = res;
5552 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5553 msg = NULL;
5554 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005555 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5556 "BSSes)", (unsigned long) res->num);
5557 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558 return res;
5559 }
5560 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5561 "(%s)", ret, strerror(-ret));
5562nla_put_failure:
5563 nlmsg_free(msg);
5564 wpa_scan_results_free(res);
5565 return NULL;
5566}
5567
5568
5569/**
5570 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5571 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5572 * Returns: Scan results on success, -1 on failure
5573 */
5574static struct wpa_scan_results *
5575wpa_driver_nl80211_get_scan_results(void *priv)
5576{
5577 struct i802_bss *bss = priv;
5578 struct wpa_driver_nl80211_data *drv = bss->drv;
5579 struct wpa_scan_results *res;
5580
5581 res = nl80211_get_scan_results(drv);
5582 if (res)
5583 wpa_driver_nl80211_check_bss_status(drv, res);
5584 return res;
5585}
5586
5587
5588static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5589{
5590 struct wpa_scan_results *res;
5591 size_t i;
5592
5593 res = nl80211_get_scan_results(drv);
5594 if (res == NULL) {
5595 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5596 return;
5597 }
5598
5599 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5600 for (i = 0; i < res->num; i++) {
5601 struct wpa_scan_res *r = res->res[i];
5602 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5603 (int) i, (int) res->num, MAC2STR(r->bssid),
5604 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5605 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5606 }
5607
5608 wpa_scan_results_free(res);
5609}
5610
5611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005612static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5613{
5614 switch (alg) {
5615 case WPA_ALG_WEP:
5616 if (key_len == 5)
5617 return WLAN_CIPHER_SUITE_WEP40;
5618 return WLAN_CIPHER_SUITE_WEP104;
5619 case WPA_ALG_TKIP:
5620 return WLAN_CIPHER_SUITE_TKIP;
5621 case WPA_ALG_CCMP:
5622 return WLAN_CIPHER_SUITE_CCMP;
5623 case WPA_ALG_GCMP:
5624 return WLAN_CIPHER_SUITE_GCMP;
5625 case WPA_ALG_CCMP_256:
5626 return WLAN_CIPHER_SUITE_CCMP_256;
5627 case WPA_ALG_GCMP_256:
5628 return WLAN_CIPHER_SUITE_GCMP_256;
5629 case WPA_ALG_IGTK:
5630 return WLAN_CIPHER_SUITE_AES_CMAC;
5631 case WPA_ALG_BIP_GMAC_128:
5632 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5633 case WPA_ALG_BIP_GMAC_256:
5634 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5635 case WPA_ALG_BIP_CMAC_256:
5636 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5637 case WPA_ALG_SMS4:
5638 return WLAN_CIPHER_SUITE_SMS4;
5639 case WPA_ALG_KRK:
5640 return WLAN_CIPHER_SUITE_KRK;
5641 case WPA_ALG_NONE:
5642 case WPA_ALG_PMK:
5643 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5644 alg);
5645 return 0;
5646 }
5647
5648 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5649 alg);
5650 return 0;
5651}
5652
5653
5654static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5655{
5656 switch (cipher) {
5657 case WPA_CIPHER_CCMP_256:
5658 return WLAN_CIPHER_SUITE_CCMP_256;
5659 case WPA_CIPHER_GCMP_256:
5660 return WLAN_CIPHER_SUITE_GCMP_256;
5661 case WPA_CIPHER_CCMP:
5662 return WLAN_CIPHER_SUITE_CCMP;
5663 case WPA_CIPHER_GCMP:
5664 return WLAN_CIPHER_SUITE_GCMP;
5665 case WPA_CIPHER_TKIP:
5666 return WLAN_CIPHER_SUITE_TKIP;
5667 case WPA_CIPHER_WEP104:
5668 return WLAN_CIPHER_SUITE_WEP104;
5669 case WPA_CIPHER_WEP40:
5670 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005671 case WPA_CIPHER_GTK_NOT_USED:
5672 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005673 }
5674
5675 return 0;
5676}
5677
5678
5679static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5680 int max_suites)
5681{
5682 int num_suites = 0;
5683
5684 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5685 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5686 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5687 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5688 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5689 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5690 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5691 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5692 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5693 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5694 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5695 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5696 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5697 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5698
5699 return num_suites;
5700}
5701
5702
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005703static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704 enum wpa_alg alg, const u8 *addr,
5705 int key_idx, int set_tx,
5706 const u8 *seq, size_t seq_len,
5707 const u8 *key, size_t key_len)
5708{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005710 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 struct nl_msg *msg;
5712 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005713 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005715 /* Ignore for P2P Device */
5716 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5717 return 0;
5718
5719 ifindex = if_nametoindex(ifname);
5720 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005722 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005723 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005724#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005725 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005726 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005727 tdls = 1;
5728 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005729#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730
5731 msg = nlmsg_alloc();
5732 if (!msg)
5733 return -ENOMEM;
5734
5735 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005736 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005738 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005740 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005741 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5742 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743 }
5744
Dmitry Shmidt98660862014-03-11 17:26:21 -07005745 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005747 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5748 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749
5750 if (addr && !is_broadcast_ether_addr(addr)) {
5751 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5752 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5753
5754 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5755 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5756 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5757 NL80211_KEYTYPE_GROUP);
5758 }
5759 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005760 struct nlattr *types;
5761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005763
5764 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005765 if (!types)
5766 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005767 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5768 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005769 }
5770 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5771 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5772
5773 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5774 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5775 ret = 0;
5776 if (ret)
5777 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5778 ret, strerror(-ret));
5779
5780 /*
5781 * If we failed or don't need to set the default TX key (below),
5782 * we're done here.
5783 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005784 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005786 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787 !is_broadcast_ether_addr(addr))
5788 return ret;
5789
5790 msg = nlmsg_alloc();
5791 if (!msg)
5792 return -ENOMEM;
5793
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005794 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5796 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5797 if (alg == WPA_ALG_IGTK)
5798 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5799 else
5800 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5801 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005802 struct nlattr *types;
5803
5804 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805 if (!types)
5806 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005807 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5808 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005810 struct nlattr *types;
5811
5812 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813 if (!types)
5814 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005815 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5816 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817 }
5818
5819 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5820 if (ret == -ENOENT)
5821 ret = 0;
5822 if (ret)
5823 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5824 "err=%d %s)", ret, strerror(-ret));
5825 return ret;
5826
5827nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005828 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829 return -ENOBUFS;
5830}
5831
5832
5833static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5834 int key_idx, int defkey,
5835 const u8 *seq, size_t seq_len,
5836 const u8 *key, size_t key_len)
5837{
5838 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5839 if (!key_attr)
5840 return -1;
5841
5842 if (defkey && alg == WPA_ALG_IGTK)
5843 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5844 else if (defkey)
5845 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5846
5847 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5848
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005849 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5850 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851
5852 if (seq && seq_len)
5853 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5854
5855 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5856
5857 nla_nest_end(msg, key_attr);
5858
5859 return 0;
5860 nla_put_failure:
5861 return -1;
5862}
5863
5864
5865static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5866 struct nl_msg *msg)
5867{
5868 int i, privacy = 0;
5869 struct nlattr *nl_keys, *nl_key;
5870
5871 for (i = 0; i < 4; i++) {
5872 if (!params->wep_key[i])
5873 continue;
5874 privacy = 1;
5875 break;
5876 }
5877 if (params->wps == WPS_MODE_PRIVACY)
5878 privacy = 1;
5879 if (params->pairwise_suite &&
5880 params->pairwise_suite != WPA_CIPHER_NONE)
5881 privacy = 1;
5882
5883 if (!privacy)
5884 return 0;
5885
5886 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5887
5888 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5889 if (!nl_keys)
5890 goto nla_put_failure;
5891
5892 for (i = 0; i < 4; i++) {
5893 if (!params->wep_key[i])
5894 continue;
5895
5896 nl_key = nla_nest_start(msg, i);
5897 if (!nl_key)
5898 goto nla_put_failure;
5899
5900 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5901 params->wep_key[i]);
5902 if (params->wep_key_len[i] == 5)
5903 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5904 WLAN_CIPHER_SUITE_WEP40);
5905 else
5906 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5907 WLAN_CIPHER_SUITE_WEP104);
5908
5909 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5910
5911 if (i == params->wep_tx_keyidx)
5912 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5913
5914 nla_nest_end(msg, nl_key);
5915 }
5916 nla_nest_end(msg, nl_keys);
5917
5918 return 0;
5919
5920nla_put_failure:
5921 return -ENOBUFS;
5922}
5923
5924
5925static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5926 const u8 *addr, int cmd, u16 reason_code,
5927 int local_state_change)
5928{
5929 int ret = -1;
5930 struct nl_msg *msg;
5931
5932 msg = nlmsg_alloc();
5933 if (!msg)
5934 return -1;
5935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005936 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937
5938 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5939 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005940 if (addr)
5941 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005942 if (local_state_change)
5943 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5944
5945 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5946 msg = NULL;
5947 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005948 wpa_dbg(drv->ctx, MSG_DEBUG,
5949 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5950 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951 goto nla_put_failure;
5952 }
5953 ret = 0;
5954
5955nla_put_failure:
5956 nlmsg_free(msg);
5957 return ret;
5958}
5959
5960
5961static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005962 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005963{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005964 int ret;
5965
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005966 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005967 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005968 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005969 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5970 reason_code, 0);
5971 /*
5972 * For locally generated disconnect, supplicant already generates a
5973 * DEAUTH event, so ignore the event from NL80211.
5974 */
5975 drv->ignore_next_local_disconnect = ret == 0;
5976
5977 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005978}
5979
5980
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005981static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5982 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005983{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005985 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07005986
5987 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
5988 nl80211_mark_disconnected(drv);
5989 return nl80211_leave_ibss(drv);
5990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005992 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005993 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5994 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005995 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07005996 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5997 reason_code, 0);
5998 /*
5999 * For locally generated deauthenticate, supplicant already generates a
6000 * DEAUTH event, so ignore the event from NL80211.
6001 */
6002 drv->ignore_next_local_deauth = ret == 0;
6003 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006004}
6005
6006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006007static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6008 struct wpa_driver_auth_params *params)
6009{
6010 int i;
6011
6012 drv->auth_freq = params->freq;
6013 drv->auth_alg = params->auth_alg;
6014 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6015 drv->auth_local_state_change = params->local_state_change;
6016 drv->auth_p2p = params->p2p;
6017
6018 if (params->bssid)
6019 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6020 else
6021 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6022
6023 if (params->ssid) {
6024 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6025 drv->auth_ssid_len = params->ssid_len;
6026 } else
6027 drv->auth_ssid_len = 0;
6028
6029
6030 os_free(drv->auth_ie);
6031 drv->auth_ie = NULL;
6032 drv->auth_ie_len = 0;
6033 if (params->ie) {
6034 drv->auth_ie = os_malloc(params->ie_len);
6035 if (drv->auth_ie) {
6036 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6037 drv->auth_ie_len = params->ie_len;
6038 }
6039 }
6040
6041 for (i = 0; i < 4; i++) {
6042 if (params->wep_key[i] && params->wep_key_len[i] &&
6043 params->wep_key_len[i] <= 16) {
6044 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6045 params->wep_key_len[i]);
6046 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6047 } else
6048 drv->auth_wep_key_len[i] = 0;
6049 }
6050}
6051
6052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006054 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006055{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 struct wpa_driver_nl80211_data *drv = bss->drv;
6057 int ret = -1, i;
6058 struct nl_msg *msg;
6059 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006060 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006062 int is_retry;
6063
6064 is_retry = drv->retry_auth;
6065 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006066 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006068 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006070 if (params->bssid)
6071 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6072 else
6073 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006075 nlmode = params->p2p ?
6076 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6077 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006078 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 return -1;
6080
6081retry:
6082 msg = nlmsg_alloc();
6083 if (!msg)
6084 return -1;
6085
6086 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6087 drv->ifindex);
6088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006089 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090
6091 for (i = 0; i < 4; i++) {
6092 if (!params->wep_key[i])
6093 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006094 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006095 NULL, i,
6096 i == params->wep_tx_keyidx, NULL, 0,
6097 params->wep_key[i],
6098 params->wep_key_len[i]);
6099 if (params->wep_tx_keyidx != i)
6100 continue;
6101 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6102 params->wep_key[i], params->wep_key_len[i])) {
6103 nlmsg_free(msg);
6104 return -1;
6105 }
6106 }
6107
6108 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6109 if (params->bssid) {
6110 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6111 MAC2STR(params->bssid));
6112 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6113 }
6114 if (params->freq) {
6115 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6116 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6117 }
6118 if (params->ssid) {
6119 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6120 params->ssid, params->ssid_len);
6121 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6122 params->ssid);
6123 }
6124 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6125 if (params->ie)
6126 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006127 if (params->sae_data) {
6128 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6129 params->sae_data_len);
6130 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6131 params->sae_data);
6132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6134 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6135 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6136 type = NL80211_AUTHTYPE_SHARED_KEY;
6137 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6138 type = NL80211_AUTHTYPE_NETWORK_EAP;
6139 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6140 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006141 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6142 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006143 else
6144 goto nla_put_failure;
6145 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6146 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6147 if (params->local_state_change) {
6148 wpa_printf(MSG_DEBUG, " * Local state change only");
6149 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6150 }
6151
6152 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6153 msg = NULL;
6154 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006155 wpa_dbg(drv->ctx, MSG_DEBUG,
6156 "nl80211: MLME command failed (auth): ret=%d (%s)",
6157 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 count++;
6159 if (ret == -EALREADY && count == 1 && params->bssid &&
6160 !params->local_state_change) {
6161 /*
6162 * mac80211 does not currently accept new
6163 * authentication if we are already authenticated. As a
6164 * workaround, force deauthentication and try again.
6165 */
6166 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6167 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006168 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006169 wpa_driver_nl80211_deauthenticate(
6170 bss, params->bssid,
6171 WLAN_REASON_PREV_AUTH_NOT_VALID);
6172 nlmsg_free(msg);
6173 goto retry;
6174 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006175
6176 if (ret == -ENOENT && params->freq && !is_retry) {
6177 /*
6178 * cfg80211 has likely expired the BSS entry even
6179 * though it was previously available in our internal
6180 * BSS table. To recover quickly, start a single
6181 * channel scan on the specified channel.
6182 */
6183 struct wpa_driver_scan_params scan;
6184 int freqs[2];
6185
6186 os_memset(&scan, 0, sizeof(scan));
6187 scan.num_ssids = 1;
6188 if (params->ssid) {
6189 scan.ssids[0].ssid = params->ssid;
6190 scan.ssids[0].ssid_len = params->ssid_len;
6191 }
6192 freqs[0] = params->freq;
6193 freqs[1] = 0;
6194 scan.freqs = freqs;
6195 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6196 "channel scan to refresh cfg80211 BSS "
6197 "entry");
6198 ret = wpa_driver_nl80211_scan(bss, &scan);
6199 if (ret == 0) {
6200 nl80211_copy_auth_params(drv, params);
6201 drv->scan_for_auth = 1;
6202 }
6203 } else if (is_retry) {
6204 /*
6205 * Need to indicate this with an event since the return
6206 * value from the retry is not delivered to core code.
6207 */
6208 union wpa_event_data event;
6209 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6210 "failed");
6211 os_memset(&event, 0, sizeof(event));
6212 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6213 ETH_ALEN);
6214 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6215 &event);
6216 }
6217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218 goto nla_put_failure;
6219 }
6220 ret = 0;
6221 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6222 "successfully");
6223
6224nla_put_failure:
6225 nlmsg_free(msg);
6226 return ret;
6227}
6228
6229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006230static int wpa_driver_nl80211_authenticate_retry(
6231 struct wpa_driver_nl80211_data *drv)
6232{
6233 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006234 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006235 int i;
6236
6237 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6238
6239 os_memset(&params, 0, sizeof(params));
6240 params.freq = drv->auth_freq;
6241 params.auth_alg = drv->auth_alg;
6242 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6243 params.local_state_change = drv->auth_local_state_change;
6244 params.p2p = drv->auth_p2p;
6245
6246 if (!is_zero_ether_addr(drv->auth_bssid_))
6247 params.bssid = drv->auth_bssid_;
6248
6249 if (drv->auth_ssid_len) {
6250 params.ssid = drv->auth_ssid;
6251 params.ssid_len = drv->auth_ssid_len;
6252 }
6253
6254 params.ie = drv->auth_ie;
6255 params.ie_len = drv->auth_ie_len;
6256
6257 for (i = 0; i < 4; i++) {
6258 if (drv->auth_wep_key_len[i]) {
6259 params.wep_key[i] = drv->auth_wep_key[i];
6260 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6261 }
6262 }
6263
6264 drv->retry_auth = 1;
6265 return wpa_driver_nl80211_authenticate(bss, &params);
6266}
6267
6268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269struct phy_info_arg {
6270 u16 *num_modes;
6271 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006272 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006273};
6274
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006275static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6276 struct nlattr *ampdu_factor,
6277 struct nlattr *ampdu_density,
6278 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006280 if (capa)
6281 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006283 if (ampdu_factor)
6284 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006285
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006286 if (ampdu_density)
6287 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6288
6289 if (mcs_set && nla_len(mcs_set) >= 16) {
6290 u8 *mcs;
6291 mcs = nla_data(mcs_set);
6292 os_memcpy(mode->mcs_set, mcs, 16);
6293 }
6294}
6295
6296
6297static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6298 struct nlattr *capa,
6299 struct nlattr *mcs_set)
6300{
6301 if (capa)
6302 mode->vht_capab = nla_get_u32(capa);
6303
6304 if (mcs_set && nla_len(mcs_set) >= 8) {
6305 u8 *mcs;
6306 mcs = nla_data(mcs_set);
6307 os_memcpy(mode->vht_mcs_set, mcs, 8);
6308 }
6309}
6310
6311
6312static void phy_info_freq(struct hostapd_hw_modes *mode,
6313 struct hostapd_channel_data *chan,
6314 struct nlattr *tb_freq[])
6315{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006316 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006317 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6318 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006319 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006320 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6321 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006322
6323 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6324 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006325 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6326 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006327 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6328 chan->flag |= HOSTAPD_CHAN_RADAR;
6329
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006330 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6331 enum nl80211_dfs_state state =
6332 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6333
6334 switch (state) {
6335 case NL80211_DFS_USABLE:
6336 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6337 break;
6338 case NL80211_DFS_AVAILABLE:
6339 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6340 break;
6341 case NL80211_DFS_UNAVAILABLE:
6342 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6343 break;
6344 }
6345 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006346
6347 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6348 chan->dfs_cac_ms = nla_get_u32(
6349 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6350 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006351}
6352
6353
6354static int phy_info_freqs(struct phy_info_arg *phy_info,
6355 struct hostapd_hw_modes *mode, struct nlattr *tb)
6356{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6358 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6359 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006360 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006361 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6362 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006363 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006365 int new_channels = 0;
6366 struct hostapd_channel_data *channel;
6367 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006369 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006370
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006371 if (tb == NULL)
6372 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006373
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006374 nla_for_each_nested(nl_freq, tb, rem_freq) {
6375 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6376 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6377 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6378 continue;
6379 new_channels++;
6380 }
6381
6382 channel = os_realloc_array(mode->channels,
6383 mode->num_channels + new_channels,
6384 sizeof(struct hostapd_channel_data));
6385 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006386 return NL_SKIP;
6387
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006388 mode->channels = channel;
6389 mode->num_channels += new_channels;
6390
6391 idx = phy_info->last_chan_idx;
6392
6393 nla_for_each_nested(nl_freq, tb, rem_freq) {
6394 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6395 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6396 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6397 continue;
6398 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6399 idx++;
6400 }
6401 phy_info->last_chan_idx = idx;
6402
6403 return NL_OK;
6404}
6405
6406
6407static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6408{
6409 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6410 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6411 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6412 { .type = NLA_FLAG },
6413 };
6414 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6415 struct nlattr *nl_rate;
6416 int rem_rate, idx;
6417
6418 if (tb == NULL)
6419 return NL_OK;
6420
6421 nla_for_each_nested(nl_rate, tb, rem_rate) {
6422 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6423 nla_data(nl_rate), nla_len(nl_rate),
6424 rate_policy);
6425 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6426 continue;
6427 mode->num_rates++;
6428 }
6429
6430 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6431 if (!mode->rates)
6432 return NL_SKIP;
6433
6434 idx = 0;
6435
6436 nla_for_each_nested(nl_rate, tb, rem_rate) {
6437 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6438 nla_data(nl_rate), nla_len(nl_rate),
6439 rate_policy);
6440 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6441 continue;
6442 mode->rates[idx] = nla_get_u32(
6443 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006444 idx++;
6445 }
6446
6447 return NL_OK;
6448}
6449
6450
6451static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6452{
6453 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6454 struct hostapd_hw_modes *mode;
6455 int ret;
6456
6457 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006458 mode = os_realloc_array(phy_info->modes,
6459 *phy_info->num_modes + 1,
6460 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006461 if (!mode)
6462 return NL_SKIP;
6463 phy_info->modes = mode;
6464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006465 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006466 os_memset(mode, 0, sizeof(*mode));
6467 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006468 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6469 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6470
6471 /*
6472 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6473 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6474 * possible streams as unsupported. This will be overridden if
6475 * driver advertises VHT support.
6476 */
6477 mode->vht_mcs_set[0] = 0xff;
6478 mode->vht_mcs_set[1] = 0xff;
6479 mode->vht_mcs_set[4] = 0xff;
6480 mode->vht_mcs_set[5] = 0xff;
6481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006483 phy_info->last_mode = nl_band->nla_type;
6484 phy_info->last_chan_idx = 0;
6485 } else
6486 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006488 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6489 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006490
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006491 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6492 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6493 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6494 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6495 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6496 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6497 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6498 if (ret != NL_OK)
6499 return ret;
6500 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6501 if (ret != NL_OK)
6502 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006504 return NL_OK;
6505}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006508static int phy_info_handler(struct nl_msg *msg, void *arg)
6509{
6510 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6511 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6512 struct phy_info_arg *phy_info = arg;
6513 struct nlattr *nl_band;
6514 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006515
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006516 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6517 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006518
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006519 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6520 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006521
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006522 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6523 {
6524 int res = phy_info_band(phy_info, nl_band);
6525 if (res != NL_OK)
6526 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006527 }
6528
6529 return NL_SKIP;
6530}
6531
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006533static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006534wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6535 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006536{
6537 u16 m;
6538 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6539 int i, mode11g_idx = -1;
6540
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006541 /* heuristic to set up modes */
6542 for (m = 0; m < *num_modes; m++) {
6543 if (!modes[m].num_channels)
6544 continue;
6545 if (modes[m].channels[0].freq < 4000) {
6546 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6547 for (i = 0; i < modes[m].num_rates; i++) {
6548 if (modes[m].rates[i] > 200) {
6549 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6550 break;
6551 }
6552 }
6553 } else if (modes[m].channels[0].freq > 50000)
6554 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6555 else
6556 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6557 }
6558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559 /* If only 802.11g mode is included, use it to construct matching
6560 * 802.11b mode data. */
6561
6562 for (m = 0; m < *num_modes; m++) {
6563 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6564 return modes; /* 802.11b already included */
6565 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6566 mode11g_idx = m;
6567 }
6568
6569 if (mode11g_idx < 0)
6570 return modes; /* 2.4 GHz band not supported at all */
6571
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006572 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573 if (nmodes == NULL)
6574 return modes; /* Could not add 802.11b mode */
6575
6576 mode = &nmodes[*num_modes];
6577 os_memset(mode, 0, sizeof(*mode));
6578 (*num_modes)++;
6579 modes = nmodes;
6580
6581 mode->mode = HOSTAPD_MODE_IEEE80211B;
6582
6583 mode11g = &modes[mode11g_idx];
6584 mode->num_channels = mode11g->num_channels;
6585 mode->channels = os_malloc(mode11g->num_channels *
6586 sizeof(struct hostapd_channel_data));
6587 if (mode->channels == NULL) {
6588 (*num_modes)--;
6589 return modes; /* Could not add 802.11b mode */
6590 }
6591 os_memcpy(mode->channels, mode11g->channels,
6592 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6593
6594 mode->num_rates = 0;
6595 mode->rates = os_malloc(4 * sizeof(int));
6596 if (mode->rates == NULL) {
6597 os_free(mode->channels);
6598 (*num_modes)--;
6599 return modes; /* Could not add 802.11b mode */
6600 }
6601
6602 for (i = 0; i < mode11g->num_rates; i++) {
6603 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6604 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6605 continue;
6606 mode->rates[mode->num_rates] = mode11g->rates[i];
6607 mode->num_rates++;
6608 if (mode->num_rates == 4)
6609 break;
6610 }
6611
6612 if (mode->num_rates == 0) {
6613 os_free(mode->channels);
6614 os_free(mode->rates);
6615 (*num_modes)--;
6616 return modes; /* No 802.11b rates */
6617 }
6618
6619 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6620 "information");
6621
6622 return modes;
6623}
6624
6625
6626static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6627 int end)
6628{
6629 int c;
6630
6631 for (c = 0; c < mode->num_channels; c++) {
6632 struct hostapd_channel_data *chan = &mode->channels[c];
6633 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6634 chan->flag |= HOSTAPD_CHAN_HT40;
6635 }
6636}
6637
6638
6639static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6640 int end)
6641{
6642 int c;
6643
6644 for (c = 0; c < mode->num_channels; c++) {
6645 struct hostapd_channel_data *chan = &mode->channels[c];
6646 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6647 continue;
6648 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6649 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6650 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6651 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6652 }
6653}
6654
6655
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006656static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006657 struct phy_info_arg *results)
6658{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006659 u16 m;
6660
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006661 for (m = 0; m < *results->num_modes; m++) {
6662 int c;
6663 struct hostapd_hw_modes *mode = &results->modes[m];
6664
6665 for (c = 0; c < mode->num_channels; c++) {
6666 struct hostapd_channel_data *chan = &mode->channels[c];
6667 if ((u32) chan->freq - 10 >= start &&
6668 (u32) chan->freq + 10 <= end)
6669 chan->max_tx_power = max_eirp;
6670 }
6671 }
6672}
6673
6674
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006675static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676 struct phy_info_arg *results)
6677{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006678 u16 m;
6679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006680 for (m = 0; m < *results->num_modes; m++) {
6681 if (!(results->modes[m].ht_capab &
6682 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6683 continue;
6684 nl80211_set_ht40_mode(&results->modes[m], start, end);
6685 }
6686}
6687
6688
6689static void nl80211_reg_rule_sec(struct nlattr *tb[],
6690 struct phy_info_arg *results)
6691{
6692 u32 start, end, max_bw;
6693 u16 m;
6694
6695 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6696 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6697 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6698 return;
6699
6700 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6701 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6702 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6703
6704 if (max_bw < 20)
6705 return;
6706
6707 for (m = 0; m < *results->num_modes; m++) {
6708 if (!(results->modes[m].ht_capab &
6709 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6710 continue;
6711 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6712 }
6713}
6714
6715
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006716static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6717 int end)
6718{
6719 int c;
6720
6721 for (c = 0; c < mode->num_channels; c++) {
6722 struct hostapd_channel_data *chan = &mode->channels[c];
6723 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6724 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6725
6726 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6727 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6728
6729 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6730 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6731
6732 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6733 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6734 }
6735}
6736
6737
6738static void nl80211_reg_rule_vht(struct nlattr *tb[],
6739 struct phy_info_arg *results)
6740{
6741 u32 start, end, max_bw;
6742 u16 m;
6743
6744 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6745 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6746 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6747 return;
6748
6749 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6750 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6751 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6752
6753 if (max_bw < 80)
6754 return;
6755
6756 for (m = 0; m < *results->num_modes; m++) {
6757 if (!(results->modes[m].ht_capab &
6758 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6759 continue;
6760 /* TODO: use a real VHT support indication */
6761 if (!results->modes[m].vht_capab)
6762 continue;
6763
6764 nl80211_set_vht_mode(&results->modes[m], start, end);
6765 }
6766}
6767
6768
Dmitry Shmidt97672262014-02-03 13:02:54 -08006769static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6770{
6771 switch (region) {
6772 case NL80211_DFS_UNSET:
6773 return "DFS-UNSET";
6774 case NL80211_DFS_FCC:
6775 return "DFS-FCC";
6776 case NL80211_DFS_ETSI:
6777 return "DFS-ETSI";
6778 case NL80211_DFS_JP:
6779 return "DFS-JP";
6780 default:
6781 return "DFS-invalid";
6782 }
6783}
6784
6785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006786static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6787{
6788 struct phy_info_arg *results = arg;
6789 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6790 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6791 struct nlattr *nl_rule;
6792 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6793 int rem_rule;
6794 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6795 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6796 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6797 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6798 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6799 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6800 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6801 };
6802
6803 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6804 genlmsg_attrlen(gnlh, 0), NULL);
6805 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6806 !tb_msg[NL80211_ATTR_REG_RULES]) {
6807 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6808 "available");
6809 return NL_SKIP;
6810 }
6811
Dmitry Shmidt97672262014-02-03 13:02:54 -08006812 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6813 enum nl80211_dfs_regions dfs_domain;
6814 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6815 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6816 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6817 dfs_domain_name(dfs_domain));
6818 } else {
6819 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6820 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006822
6823 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6824 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006825 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6827 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006828 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6829 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6830 continue;
6831 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6832 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6833 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6834 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6835 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6836 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006837 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6838 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006839
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006840 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6841 start, end, max_bw, max_eirp,
6842 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6843 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6844 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6845 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6846 "",
6847 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6848 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6849 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6850 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006851 if (max_bw >= 40)
6852 nl80211_reg_rule_ht40(start, end, results);
6853 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6854 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6855 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006856 }
6857
6858 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6859 {
6860 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6861 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6862 nl80211_reg_rule_sec(tb_rule, results);
6863 }
6864
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006865 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6866 {
6867 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6868 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6869 nl80211_reg_rule_vht(tb_rule, results);
6870 }
6871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 return NL_SKIP;
6873}
6874
6875
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006876static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6877 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006878{
6879 struct nl_msg *msg;
6880
6881 msg = nlmsg_alloc();
6882 if (!msg)
6883 return -ENOMEM;
6884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006885 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
6887}
6888
6889
6890static struct hostapd_hw_modes *
6891wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
6892{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006893 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894 struct i802_bss *bss = priv;
6895 struct wpa_driver_nl80211_data *drv = bss->drv;
6896 struct nl_msg *msg;
6897 struct phy_info_arg result = {
6898 .num_modes = num_modes,
6899 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006900 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901 };
6902
6903 *num_modes = 0;
6904 *flags = 0;
6905
6906 msg = nlmsg_alloc();
6907 if (!msg)
6908 return NULL;
6909
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006910 feat = get_nl80211_protocol_features(drv);
6911 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
6912 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
6913 else
6914 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006916 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006917 if (nl80211_set_iface_id(msg, bss) < 0)
6918 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919
6920 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006921 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006922 return wpa_driver_nl80211_postprocess_modes(result.modes,
6923 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006925 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006927 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928 return NULL;
6929}
6930
6931
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006932static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
6933 const void *data, size_t len,
6934 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935{
6936 __u8 rtap_hdr[] = {
6937 0x00, 0x00, /* radiotap version */
6938 0x0e, 0x00, /* radiotap length */
6939 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
6940 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
6941 0x00, /* padding */
6942 0x00, 0x00, /* RX and TX flags to indicate that */
6943 0x00, 0x00, /* this is the injected frame directly */
6944 };
6945 struct iovec iov[2] = {
6946 {
6947 .iov_base = &rtap_hdr,
6948 .iov_len = sizeof(rtap_hdr),
6949 },
6950 {
6951 .iov_base = (void *) data,
6952 .iov_len = len,
6953 }
6954 };
6955 struct msghdr msg = {
6956 .msg_name = NULL,
6957 .msg_namelen = 0,
6958 .msg_iov = iov,
6959 .msg_iovlen = 2,
6960 .msg_control = NULL,
6961 .msg_controllen = 0,
6962 .msg_flags = 0,
6963 };
6964 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006965 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006966
6967 if (encrypt)
6968 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
6969
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006970 if (drv->monitor_sock < 0) {
6971 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
6972 "for %s", __func__);
6973 return -1;
6974 }
6975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006976 if (noack)
6977 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006978 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006980 res = sendmsg(drv->monitor_sock, &msg, 0);
6981 if (res < 0) {
6982 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
6983 return -1;
6984 }
6985 return 0;
6986}
6987
6988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006989static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
6990 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006991 int encrypt, int noack,
6992 unsigned int freq, int no_cck,
6993 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006994{
6995 struct wpa_driver_nl80211_data *drv = bss->drv;
6996 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006997 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006998
Dmitry Shmidt56052862013-10-04 10:23:25 -07006999 if (freq == 0) {
7000 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
7001 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007002 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007003 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007004
Dmitry Shmidt56052862013-10-04 10:23:25 -07007005 if (drv->use_monitor) {
7006 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7007 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007008 return wpa_driver_nl80211_send_mntr(drv, data, len,
7009 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007010 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007011
Dmitry Shmidt56052862013-10-04 10:23:25 -07007012 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07007013 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7014 &cookie, no_cck, noack, offchanok);
7015 if (res == 0 && !noack) {
7016 const struct ieee80211_mgmt *mgmt;
7017 u16 fc;
7018
7019 mgmt = (const struct ieee80211_mgmt *) data;
7020 fc = le_to_host16(mgmt->frame_control);
7021 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7022 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7023 wpa_printf(MSG_MSGDUMP,
7024 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7025 (long long unsigned int)
7026 drv->send_action_cookie,
7027 (long long unsigned int) cookie);
7028 drv->send_action_cookie = cookie;
7029 }
7030 }
7031
7032 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007033}
7034
7035
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007036static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7037 size_t data_len, int noack,
7038 unsigned int freq, int no_cck,
7039 int offchanok,
7040 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007041{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 struct wpa_driver_nl80211_data *drv = bss->drv;
7043 struct ieee80211_mgmt *mgmt;
7044 int encrypt = 1;
7045 u16 fc;
7046
7047 mgmt = (struct ieee80211_mgmt *) data;
7048 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007049 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
7050 noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007051
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007052 if ((is_sta_interface(drv->nlmode) ||
7053 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007054 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7055 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7056 /*
7057 * The use of last_mgmt_freq is a bit of a hack,
7058 * but it works due to the single-threaded nature
7059 * of wpa_supplicant.
7060 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007061 if (freq == 0) {
7062 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7063 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007064 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007065 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007066 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007067 data, data_len, NULL, 1, noack,
7068 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007069 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007070
7071 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007072 if (freq == 0) {
7073 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7074 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007075 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007076 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007077 return nl80211_send_frame_cmd(bss, freq,
7078 (int) freq == bss->freq ? 0 :
7079 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007080 data, data_len,
7081 &drv->send_action_cookie,
7082 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007083 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007085 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7086 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7087 /*
7088 * Only one of the authentication frame types is encrypted.
7089 * In order for static WEP encryption to work properly (i.e.,
7090 * to not encrypt the frame), we need to tell mac80211 about
7091 * the frames that must not be encrypted.
7092 */
7093 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7094 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7095 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7096 encrypt = 0;
7097 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007098
Dmitry Shmidt56052862013-10-04 10:23:25 -07007099 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007100 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007101 noack, freq, no_cck, offchanok,
7102 wait_time);
7103}
7104
7105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7107 int slot, int ht_opmode, int ap_isolate,
7108 int *basic_rates)
7109{
7110 struct wpa_driver_nl80211_data *drv = bss->drv;
7111 struct nl_msg *msg;
7112
7113 msg = nlmsg_alloc();
7114 if (!msg)
7115 return -ENOMEM;
7116
7117 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7118
7119 if (cts >= 0)
7120 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7121 if (preamble >= 0)
7122 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7123 if (slot >= 0)
7124 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7125 if (ht_opmode >= 0)
7126 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7127 if (ap_isolate >= 0)
7128 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7129
7130 if (basic_rates) {
7131 u8 rates[NL80211_MAX_SUPP_RATES];
7132 u8 rates_len = 0;
7133 int i;
7134
7135 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7136 i++)
7137 rates[rates_len++] = basic_rates[i] / 5;
7138
7139 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7140 }
7141
7142 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7143
7144 return send_and_recv_msgs(drv, msg, NULL, NULL);
7145 nla_put_failure:
7146 nlmsg_free(msg);
7147 return -ENOBUFS;
7148}
7149
7150
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007151static int wpa_driver_nl80211_set_acl(void *priv,
7152 struct hostapd_acl_params *params)
7153{
7154 struct i802_bss *bss = priv;
7155 struct wpa_driver_nl80211_data *drv = bss->drv;
7156 struct nl_msg *msg;
7157 struct nlattr *acl;
7158 unsigned int i;
7159 int ret = 0;
7160
7161 if (!(drv->capa.max_acl_mac_addrs))
7162 return -ENOTSUP;
7163
7164 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7165 return -ENOTSUP;
7166
7167 msg = nlmsg_alloc();
7168 if (!msg)
7169 return -ENOMEM;
7170
7171 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7172 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7173
7174 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7175
7176 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7177
7178 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7179 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7180 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7181
7182 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7183 if (acl == NULL)
7184 goto nla_put_failure;
7185
7186 for (i = 0; i < params->num_mac_acl; i++)
7187 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7188
7189 nla_nest_end(msg, acl);
7190
7191 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7192 msg = NULL;
7193 if (ret) {
7194 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7195 ret, strerror(-ret));
7196 }
7197
7198nla_put_failure:
7199 nlmsg_free(msg);
7200
7201 return ret;
7202}
7203
7204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007205static int wpa_driver_nl80211_set_ap(void *priv,
7206 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207{
7208 struct i802_bss *bss = priv;
7209 struct wpa_driver_nl80211_data *drv = bss->drv;
7210 struct nl_msg *msg;
7211 u8 cmd = NL80211_CMD_NEW_BEACON;
7212 int ret;
7213 int beacon_set;
7214 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007215 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007216 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007217 u32 ver;
7218
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007219 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220
7221 msg = nlmsg_alloc();
7222 if (!msg)
7223 return -ENOMEM;
7224
7225 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7226 beacon_set);
7227 if (beacon_set)
7228 cmd = NL80211_CMD_SET_BEACON;
7229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007230 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007231 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7232 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007233 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007234 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7235 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007236 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007237 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007238 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007239 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007240 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007241 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007242 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007243 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7244 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007245 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7246 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007247 if (params->proberesp && params->proberesp_len) {
7248 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7249 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007250 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7251 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007252 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007253 switch (params->hide_ssid) {
7254 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007255 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007256 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7257 NL80211_HIDDEN_SSID_NOT_IN_USE);
7258 break;
7259 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007260 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007261 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7262 NL80211_HIDDEN_SSID_ZERO_LEN);
7263 break;
7264 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007265 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007266 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7267 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7268 break;
7269 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007270 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007271 if (params->privacy)
7272 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007273 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007274 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7275 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7276 /* Leave out the attribute */
7277 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7278 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7279 NL80211_AUTHTYPE_SHARED_KEY);
7280 else
7281 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7282 NL80211_AUTHTYPE_OPEN_SYSTEM);
7283
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007284 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007285 ver = 0;
7286 if (params->wpa_version & WPA_PROTO_WPA)
7287 ver |= NL80211_WPA_VERSION_1;
7288 if (params->wpa_version & WPA_PROTO_RSN)
7289 ver |= NL80211_WPA_VERSION_2;
7290 if (ver)
7291 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7292
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007293 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7294 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007295 num_suites = 0;
7296 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7297 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7298 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7299 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7300 if (num_suites) {
7301 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7302 num_suites * sizeof(u32), suites);
7303 }
7304
7305 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7306 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7307 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7308
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007309 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7310 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007311 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7312 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007313 if (num_suites) {
7314 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7315 num_suites * sizeof(u32), suites);
7316 }
7317
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007318 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7319 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007320 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7321 if (suite)
7322 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007323
7324 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007325 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7326 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7328 wpabuf_head(params->beacon_ies));
7329 }
7330 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007331 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7332 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007333 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7334 wpabuf_len(params->proberesp_ies),
7335 wpabuf_head(params->proberesp_ies));
7336 }
7337 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007338 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7339 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007340 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7341 wpabuf_len(params->assocresp_ies),
7342 wpabuf_head(params->assocresp_ies));
7343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007344
Dmitry Shmidt04949592012-07-19 12:16:46 -07007345 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007346 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7347 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007348 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7349 params->ap_max_inactivity);
7350 }
7351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7353 if (ret) {
7354 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7355 ret, strerror(-ret));
7356 } else {
7357 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007358 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7359 params->short_slot_time, params->ht_opmode,
7360 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007361 if (beacon_set && params->freq &&
7362 params->freq->bandwidth != bss->bandwidth) {
7363 wpa_printf(MSG_DEBUG,
7364 "nl80211: Update BSS %s bandwidth: %d -> %d",
7365 bss->ifname, bss->bandwidth,
7366 params->freq->bandwidth);
7367 ret = nl80211_set_channel(bss, params->freq, 1);
7368 if (ret) {
7369 wpa_printf(MSG_DEBUG,
7370 "nl80211: Frequency set failed: %d (%s)",
7371 ret, strerror(-ret));
7372 } else {
7373 wpa_printf(MSG_DEBUG,
7374 "nl80211: Frequency set succeeded for ht2040 coex");
7375 bss->bandwidth = params->freq->bandwidth;
7376 }
7377 } else if (!beacon_set) {
7378 /*
7379 * cfg80211 updates the driver on frequence change in AP
7380 * mode only at the point when beaconing is started, so
7381 * set the initial value here.
7382 */
7383 bss->bandwidth = params->freq->bandwidth;
7384 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385 }
7386 return ret;
7387 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007388 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389 return -ENOBUFS;
7390}
7391
7392
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007393static int nl80211_put_freq_params(struct nl_msg *msg,
7394 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007396 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7397 if (freq->vht_enabled) {
7398 switch (freq->bandwidth) {
7399 case 20:
7400 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7401 NL80211_CHAN_WIDTH_20);
7402 break;
7403 case 40:
7404 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7405 NL80211_CHAN_WIDTH_40);
7406 break;
7407 case 80:
7408 if (freq->center_freq2)
7409 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7410 NL80211_CHAN_WIDTH_80P80);
7411 else
7412 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7413 NL80211_CHAN_WIDTH_80);
7414 break;
7415 case 160:
7416 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7417 NL80211_CHAN_WIDTH_160);
7418 break;
7419 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007420 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007421 }
7422 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7423 if (freq->center_freq2)
7424 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7425 freq->center_freq2);
7426 } else if (freq->ht_enabled) {
7427 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428 case -1:
7429 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7430 NL80211_CHAN_HT40MINUS);
7431 break;
7432 case 1:
7433 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7434 NL80211_CHAN_HT40PLUS);
7435 break;
7436 default:
7437 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7438 NL80211_CHAN_HT20);
7439 break;
7440 }
7441 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007442 return 0;
7443
7444nla_put_failure:
7445 return -ENOBUFS;
7446}
7447
7448
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007449static int nl80211_set_channel(struct i802_bss *bss,
7450 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007451{
7452 struct wpa_driver_nl80211_data *drv = bss->drv;
7453 struct nl_msg *msg;
7454 int ret;
7455
7456 wpa_printf(MSG_DEBUG,
7457 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7458 freq->freq, freq->ht_enabled, freq->vht_enabled,
7459 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7460 msg = nlmsg_alloc();
7461 if (!msg)
7462 return -1;
7463
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007464 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7465 NL80211_CMD_SET_WIPHY);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007466
7467 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7468 if (nl80211_put_freq_params(msg, freq) < 0)
7469 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007470
7471 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007472 msg = NULL;
7473 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007474 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007475 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007477 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007478 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007479nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007480 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007481 return -1;
7482}
7483
7484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007485static u32 sta_flags_nl80211(int flags)
7486{
7487 u32 f = 0;
7488
7489 if (flags & WPA_STA_AUTHORIZED)
7490 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7491 if (flags & WPA_STA_WMM)
7492 f |= BIT(NL80211_STA_FLAG_WME);
7493 if (flags & WPA_STA_SHORT_PREAMBLE)
7494 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7495 if (flags & WPA_STA_MFP)
7496 f |= BIT(NL80211_STA_FLAG_MFP);
7497 if (flags & WPA_STA_TDLS_PEER)
7498 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7499
7500 return f;
7501}
7502
7503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007504static int wpa_driver_nl80211_sta_add(void *priv,
7505 struct hostapd_sta_add_params *params)
7506{
7507 struct i802_bss *bss = priv;
7508 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007509 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007510 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511 int ret = -ENOBUFS;
7512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007513 if ((params->flags & WPA_STA_TDLS_PEER) &&
7514 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7515 return -EOPNOTSUPP;
7516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007517 msg = nlmsg_alloc();
7518 if (!msg)
7519 return -ENOMEM;
7520
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007521 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7522 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007523 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7524 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007525
7526 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7527 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007528 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7529 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007530 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7531 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007532 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007533 if (params->aid) {
7534 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7535 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7536 } else {
7537 /*
7538 * cfg80211 validates that AID is non-zero, so we have
7539 * to make this a non-zero value for the TDLS case where
7540 * a dummy STA entry is used for now.
7541 */
7542 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7543 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7544 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007545 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7546 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007547 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7548 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007549 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7550 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7551 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007554 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7555 (u8 *) params->ht_capabilities,
7556 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7558 sizeof(*params->ht_capabilities),
7559 params->ht_capabilities);
7560 }
7561
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007562 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007563 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7564 (u8 *) params->vht_capabilities,
7565 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007566 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7567 sizeof(*params->vht_capabilities),
7568 params->vht_capabilities);
7569 }
7570
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007571 if (params->vht_opmode_enabled) {
7572 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7573 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7574 params->vht_opmode);
7575 }
7576
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007577 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7578 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7579
7580 if (params->ext_capab) {
7581 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7582 params->ext_capab, params->ext_capab_len);
7583 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7584 params->ext_capab_len, params->ext_capab);
7585 }
7586
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007587 if (params->supp_channels) {
7588 wpa_hexdump(MSG_DEBUG, " * supported channels",
7589 params->supp_channels, params->supp_channels_len);
7590 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7591 params->supp_channels_len, params->supp_channels);
7592 }
7593
7594 if (params->supp_oper_classes) {
7595 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7596 params->supp_oper_classes,
7597 params->supp_oper_classes_len);
7598 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7599 params->supp_oper_classes_len,
7600 params->supp_oper_classes);
7601 }
7602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007603 os_memset(&upd, 0, sizeof(upd));
7604 upd.mask = sta_flags_nl80211(params->flags);
7605 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007606 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7607 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007608 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7609
7610 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007611 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007613 if (!wme)
7614 goto nla_put_failure;
7615
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007616 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007617 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007618 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007619 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007620 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007621 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007622 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007623 }
7624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007625 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007626 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007628 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7629 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7630 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631 if (ret == -EEXIST)
7632 ret = 0;
7633 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007634 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635 return ret;
7636}
7637
7638
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007639static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007640{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007641 struct wpa_driver_nl80211_data *drv = bss->drv;
7642 struct nl_msg *msg;
7643 int ret;
7644
7645 msg = nlmsg_alloc();
7646 if (!msg)
7647 return -ENOMEM;
7648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007649 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007650
7651 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7652 if_nametoindex(bss->ifname));
7653 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7654
7655 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007656 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7657 " --> %d (%s)",
7658 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007659 if (ret == -ENOENT)
7660 return 0;
7661 return ret;
7662 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007663 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007664 return -ENOBUFS;
7665}
7666
7667
7668static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7669 int ifidx)
7670{
7671 struct nl_msg *msg;
7672
7673 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007675 /* stop listening for EAPOL on this interface */
7676 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007677
7678 msg = nlmsg_alloc();
7679 if (!msg)
7680 goto nla_put_failure;
7681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007682 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007683 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7684
7685 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7686 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007687 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007689 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007690 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7691}
7692
7693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007694static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7695{
7696 switch (mode) {
7697 case NL80211_IFTYPE_ADHOC:
7698 return "ADHOC";
7699 case NL80211_IFTYPE_STATION:
7700 return "STATION";
7701 case NL80211_IFTYPE_AP:
7702 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007703 case NL80211_IFTYPE_AP_VLAN:
7704 return "AP_VLAN";
7705 case NL80211_IFTYPE_WDS:
7706 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007707 case NL80211_IFTYPE_MONITOR:
7708 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007709 case NL80211_IFTYPE_MESH_POINT:
7710 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007711 case NL80211_IFTYPE_P2P_CLIENT:
7712 return "P2P_CLIENT";
7713 case NL80211_IFTYPE_P2P_GO:
7714 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007715 case NL80211_IFTYPE_P2P_DEVICE:
7716 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007717 default:
7718 return "unknown";
7719 }
7720}
7721
7722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7724 const char *ifname,
7725 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007726 const u8 *addr, int wds,
7727 int (*handler)(struct nl_msg *, void *),
7728 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007730 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007731 int ifidx;
7732 int ret = -ENOBUFS;
7733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007734 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7735 iftype, nl80211_iftype_str(iftype));
7736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737 msg = nlmsg_alloc();
7738 if (!msg)
7739 return -1;
7740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007741 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007742 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007743 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007744 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7745 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7746
7747 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007748 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007749
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007750 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007751 if (!flags)
7752 goto nla_put_failure;
7753
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007754 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007756 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757 } else if (wds) {
7758 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7759 }
7760
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007761 /*
7762 * Tell cfg80211 that the interface belongs to the socket that created
7763 * it, and the interface should be deleted when the socket is closed.
7764 */
7765 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
7766
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007767 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007768 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769 if (ret) {
7770 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007771 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007772 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7773 ifname, ret, strerror(-ret));
7774 return ret;
7775 }
7776
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007777 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7778 return 0;
7779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 ifidx = if_nametoindex(ifname);
7781 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7782 ifname, ifidx);
7783
7784 if (ifidx <= 0)
7785 return -1;
7786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787 /* start listening for EAPOL on this interface */
7788 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789
7790 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007791 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792 nl80211_remove_iface(drv, ifidx);
7793 return -1;
7794 }
7795
7796 return ifidx;
7797}
7798
7799
7800static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7801 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007802 const u8 *addr, int wds,
7803 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007804 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007805{
7806 int ret;
7807
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007808 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7809 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007811 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007813 if (use_existing) {
7814 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7815 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007816 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
7817 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7818 addr) < 0 &&
7819 (linux_set_iface_flags(drv->global->ioctl_sock,
7820 ifname, 0) < 0 ||
7821 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7822 addr) < 0 ||
7823 linux_set_iface_flags(drv->global->ioctl_sock,
7824 ifname, 1) < 0))
7825 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007826 return -ENFILE;
7827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007828 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
7829
7830 /* Try to remove the interface that was already there. */
7831 nl80211_remove_iface(drv, if_nametoindex(ifname));
7832
7833 /* Try to create the interface again */
7834 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007835 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007836 }
7837
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007838 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007839 nl80211_disable_11b_rates(drv, ret, 1);
7840
7841 return ret;
7842}
7843
7844
7845static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
7846{
7847 struct ieee80211_hdr *hdr;
7848 u16 fc;
7849 union wpa_event_data event;
7850
7851 hdr = (struct ieee80211_hdr *) buf;
7852 fc = le_to_host16(hdr->frame_control);
7853
7854 os_memset(&event, 0, sizeof(event));
7855 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
7856 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
7857 event.tx_status.dst = hdr->addr1;
7858 event.tx_status.data = buf;
7859 event.tx_status.data_len = len;
7860 event.tx_status.ack = ok;
7861 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
7862}
7863
7864
7865static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
7866 u8 *buf, size_t len)
7867{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007868 struct ieee80211_hdr *hdr = (void *)buf;
7869 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007871
7872 if (len < sizeof(*hdr))
7873 return;
7874
7875 fc = le_to_host16(hdr->frame_control);
7876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007877 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007878 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
7879 event.rx_from_unknown.addr = hdr->addr2;
7880 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
7881 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007882 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
7883}
7884
7885
7886static void handle_frame(struct wpa_driver_nl80211_data *drv,
7887 u8 *buf, size_t len, int datarate, int ssi_signal)
7888{
7889 struct ieee80211_hdr *hdr;
7890 u16 fc;
7891 union wpa_event_data event;
7892
7893 hdr = (struct ieee80211_hdr *) buf;
7894 fc = le_to_host16(hdr->frame_control);
7895
7896 switch (WLAN_FC_GET_TYPE(fc)) {
7897 case WLAN_FC_TYPE_MGMT:
7898 os_memset(&event, 0, sizeof(event));
7899 event.rx_mgmt.frame = buf;
7900 event.rx_mgmt.frame_len = len;
7901 event.rx_mgmt.datarate = datarate;
7902 event.rx_mgmt.ssi_signal = ssi_signal;
7903 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
7904 break;
7905 case WLAN_FC_TYPE_CTRL:
7906 /* can only get here with PS-Poll frames */
7907 wpa_printf(MSG_DEBUG, "CTRL");
7908 from_unknown_sta(drv, buf, len);
7909 break;
7910 case WLAN_FC_TYPE_DATA:
7911 from_unknown_sta(drv, buf, len);
7912 break;
7913 }
7914}
7915
7916
7917static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
7918{
7919 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7920 int len;
7921 unsigned char buf[3000];
7922 struct ieee80211_radiotap_iterator iter;
7923 int ret;
7924 int datarate = 0, ssi_signal = 0;
7925 int injected = 0, failed = 0, rxflags = 0;
7926
7927 len = recv(sock, buf, sizeof(buf), 0);
7928 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007929 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
7930 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007931 return;
7932 }
7933
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007934 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007935 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936 return;
7937 }
7938
7939 while (1) {
7940 ret = ieee80211_radiotap_iterator_next(&iter);
7941 if (ret == -ENOENT)
7942 break;
7943 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007944 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
7945 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007946 return;
7947 }
7948 switch (iter.this_arg_index) {
7949 case IEEE80211_RADIOTAP_FLAGS:
7950 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
7951 len -= 4;
7952 break;
7953 case IEEE80211_RADIOTAP_RX_FLAGS:
7954 rxflags = 1;
7955 break;
7956 case IEEE80211_RADIOTAP_TX_FLAGS:
7957 injected = 1;
7958 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
7959 IEEE80211_RADIOTAP_F_TX_FAIL;
7960 break;
7961 case IEEE80211_RADIOTAP_DATA_RETRIES:
7962 break;
7963 case IEEE80211_RADIOTAP_CHANNEL:
7964 /* TODO: convert from freq/flags to channel number */
7965 break;
7966 case IEEE80211_RADIOTAP_RATE:
7967 datarate = *iter.this_arg * 5;
7968 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007969 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
7970 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007971 break;
7972 }
7973 }
7974
7975 if (rxflags && injected)
7976 return;
7977
7978 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007979 handle_frame(drv, buf + iter._max_length,
7980 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07007982 handle_tx_callback(drv->ctx, buf + iter._max_length,
7983 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007984}
7985
7986
7987/*
7988 * we post-process the filter code later and rewrite
7989 * this to the offset to the last instruction
7990 */
7991#define PASS 0xFF
7992#define FAIL 0xFE
7993
7994static struct sock_filter msock_filter_insns[] = {
7995 /*
7996 * do a little-endian load of the radiotap length field
7997 */
7998 /* load lower byte into A */
7999 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
8000 /* put it into X (== index register) */
8001 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8002 /* load upper byte into A */
8003 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8004 /* left-shift it by 8 */
8005 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8006 /* or with X */
8007 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8008 /* put result into X */
8009 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8010
8011 /*
8012 * Allow management frames through, this also gives us those
8013 * management frames that we sent ourselves with status
8014 */
8015 /* load the lower byte of the IEEE 802.11 frame control field */
8016 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8017 /* mask off frame type and version */
8018 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8019 /* accept frame if it's both 0, fall through otherwise */
8020 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8021
8022 /*
8023 * TODO: add a bit to radiotap RX flags that indicates
8024 * that the sending station is not associated, then
8025 * add a filter here that filters on our DA and that flag
8026 * to allow us to deauth frames to that bad station.
8027 *
8028 * For now allow all To DS data frames through.
8029 */
8030 /* load the IEEE 802.11 frame control field */
8031 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8032 /* mask off frame type, version and DS status */
8033 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8034 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8035 */
8036 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
8037
8038#if 0
8039 /*
8040 * drop non-data frames
8041 */
8042 /* load the lower byte of the frame control field */
8043 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8044 /* mask off QoS bit */
8045 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8046 /* drop non-data frames */
8047 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
8048#endif
8049 /* load the upper byte of the frame control field */
8050 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
8051 /* mask off toDS/fromDS */
8052 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
8053 /* accept WDS frames */
8054 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
8055
8056 /*
8057 * add header length to index
8058 */
8059 /* load the lower byte of the frame control field */
8060 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8061 /* mask off QoS bit */
8062 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8063 /* right shift it by 6 to give 0 or 2 */
8064 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8065 /* add data frame header length */
8066 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8067 /* add index, was start of 802.11 header */
8068 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8069 /* move to index, now start of LL header */
8070 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8071
8072 /*
8073 * Accept empty data frames, we use those for
8074 * polling activity.
8075 */
8076 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8077 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8078
8079 /*
8080 * Accept EAPOL frames
8081 */
8082 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8083 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8084 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8085 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8086
8087 /* keep these last two statements or change the code below */
8088 /* return 0 == "DROP" */
8089 BPF_STMT(BPF_RET | BPF_K, 0),
8090 /* return ~0 == "keep all" */
8091 BPF_STMT(BPF_RET | BPF_K, ~0),
8092};
8093
8094static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008095 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008096 .filter = msock_filter_insns,
8097};
8098
8099
8100static int add_monitor_filter(int s)
8101{
8102 int idx;
8103
8104 /* rewrite all PASS/FAIL jump offsets */
8105 for (idx = 0; idx < msock_filter.len; idx++) {
8106 struct sock_filter *insn = &msock_filter_insns[idx];
8107
8108 if (BPF_CLASS(insn->code) == BPF_JMP) {
8109 if (insn->code == (BPF_JMP|BPF_JA)) {
8110 if (insn->k == PASS)
8111 insn->k = msock_filter.len - idx - 2;
8112 else if (insn->k == FAIL)
8113 insn->k = msock_filter.len - idx - 3;
8114 }
8115
8116 if (insn->jt == PASS)
8117 insn->jt = msock_filter.len - idx - 2;
8118 else if (insn->jt == FAIL)
8119 insn->jt = msock_filter.len - idx - 3;
8120
8121 if (insn->jf == PASS)
8122 insn->jf = msock_filter.len - idx - 2;
8123 else if (insn->jf == FAIL)
8124 insn->jf = msock_filter.len - idx - 3;
8125 }
8126 }
8127
8128 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8129 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008130 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8131 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008132 return -1;
8133 }
8134
8135 return 0;
8136}
8137
8138
8139static void nl80211_remove_monitor_interface(
8140 struct wpa_driver_nl80211_data *drv)
8141{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008142 if (drv->monitor_refcount > 0)
8143 drv->monitor_refcount--;
8144 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8145 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008146 if (drv->monitor_refcount > 0)
8147 return;
8148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008149 if (drv->monitor_ifidx >= 0) {
8150 nl80211_remove_iface(drv, drv->monitor_ifidx);
8151 drv->monitor_ifidx = -1;
8152 }
8153 if (drv->monitor_sock >= 0) {
8154 eloop_unregister_read_sock(drv->monitor_sock);
8155 close(drv->monitor_sock);
8156 drv->monitor_sock = -1;
8157 }
8158}
8159
8160
8161static int
8162nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8163{
8164 char buf[IFNAMSIZ];
8165 struct sockaddr_ll ll;
8166 int optval;
8167 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008168
8169 if (drv->monitor_ifidx >= 0) {
8170 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008171 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8172 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008173 return 0;
8174 }
8175
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008176 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008177 /*
8178 * P2P interface name is of the format p2p-%s-%d. For monitor
8179 * interface name corresponding to P2P GO, replace "p2p-" with
8180 * "mon-" to retain the same interface name length and to
8181 * indicate that it is a monitor interface.
8182 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008183 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008184 } else {
8185 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008186 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008187 }
8188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008189 buf[IFNAMSIZ - 1] = '\0';
8190
8191 drv->monitor_ifidx =
8192 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008193 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008194
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008195 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008196 /*
8197 * This is backward compatibility for a few versions of
8198 * the kernel only that didn't advertise the right
8199 * attributes for the only driver that then supported
8200 * AP mode w/o monitor -- ath6kl.
8201 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008202 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8203 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008204 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008205 }
8206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008207 if (drv->monitor_ifidx < 0)
8208 return -1;
8209
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008210 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008211 goto error;
8212
8213 memset(&ll, 0, sizeof(ll));
8214 ll.sll_family = AF_PACKET;
8215 ll.sll_ifindex = drv->monitor_ifidx;
8216 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8217 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008218 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8219 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008220 goto error;
8221 }
8222
8223 if (add_monitor_filter(drv->monitor_sock)) {
8224 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8225 "interface; do filtering in user space");
8226 /* This works, but will cost in performance. */
8227 }
8228
8229 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008230 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8231 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008232 goto error;
8233 }
8234
8235 optlen = sizeof(optval);
8236 optval = 20;
8237 if (setsockopt
8238 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008239 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8240 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008241 goto error;
8242 }
8243
8244 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8245 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008246 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008247 goto error;
8248 }
8249
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008250 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 return 0;
8252 error:
8253 nl80211_remove_monitor_interface(drv);
8254 return -1;
8255}
8256
8257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008258static int nl80211_setup_ap(struct i802_bss *bss)
8259{
8260 struct wpa_driver_nl80211_data *drv = bss->drv;
8261
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008262 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8263 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008264
8265 /*
8266 * Disable Probe Request reporting unless we need it in this way for
8267 * devices that include the AP SME, in the other case (unless using
8268 * monitor iface) we'll get it through the nl_mgmt socket instead.
8269 */
8270 if (!drv->device_ap_sme)
8271 wpa_driver_nl80211_probe_req_report(bss, 0);
8272
8273 if (!drv->device_ap_sme && !drv->use_monitor)
8274 if (nl80211_mgmt_subscribe_ap(bss))
8275 return -1;
8276
8277 if (drv->device_ap_sme && !drv->use_monitor)
8278 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8279 return -1;
8280
8281 if (!drv->device_ap_sme && drv->use_monitor &&
8282 nl80211_create_monitor_interface(drv) &&
8283 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008284 return -1;
8285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008286 if (drv->device_ap_sme &&
8287 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8288 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8289 "Probe Request frame reporting in AP mode");
8290 /* Try to survive without this */
8291 }
8292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008293 return 0;
8294}
8295
8296
8297static void nl80211_teardown_ap(struct i802_bss *bss)
8298{
8299 struct wpa_driver_nl80211_data *drv = bss->drv;
8300
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008301 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8302 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008303 if (drv->device_ap_sme) {
8304 wpa_driver_nl80211_probe_req_report(bss, 0);
8305 if (!drv->use_monitor)
8306 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8307 } else if (drv->use_monitor)
8308 nl80211_remove_monitor_interface(drv);
8309 else
8310 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8311
8312 bss->beacon_set = 0;
8313}
8314
8315
8316static int nl80211_send_eapol_data(struct i802_bss *bss,
8317 const u8 *addr, const u8 *data,
8318 size_t data_len)
8319{
8320 struct sockaddr_ll ll;
8321 int ret;
8322
8323 if (bss->drv->eapol_tx_sock < 0) {
8324 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8325 return -1;
8326 }
8327
8328 os_memset(&ll, 0, sizeof(ll));
8329 ll.sll_family = AF_PACKET;
8330 ll.sll_ifindex = bss->ifindex;
8331 ll.sll_protocol = htons(ETH_P_PAE);
8332 ll.sll_halen = ETH_ALEN;
8333 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8334 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8335 (struct sockaddr *) &ll, sizeof(ll));
8336 if (ret < 0)
8337 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8338 strerror(errno));
8339
8340 return ret;
8341}
8342
8343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008344static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8345
8346static int wpa_driver_nl80211_hapd_send_eapol(
8347 void *priv, const u8 *addr, const u8 *data,
8348 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8349{
8350 struct i802_bss *bss = priv;
8351 struct wpa_driver_nl80211_data *drv = bss->drv;
8352 struct ieee80211_hdr *hdr;
8353 size_t len;
8354 u8 *pos;
8355 int res;
8356 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008357
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008358 if (drv->device_ap_sme || !drv->use_monitor)
8359 return nl80211_send_eapol_data(bss, addr, data, data_len);
8360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008361 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8362 data_len;
8363 hdr = os_zalloc(len);
8364 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008365 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8366 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008367 return -1;
8368 }
8369
8370 hdr->frame_control =
8371 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8372 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8373 if (encrypt)
8374 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8375 if (qos) {
8376 hdr->frame_control |=
8377 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8378 }
8379
8380 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8381 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8382 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8383 pos = (u8 *) (hdr + 1);
8384
8385 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008386 /* Set highest priority in QoS header */
8387 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388 pos[1] = 0;
8389 pos += 2;
8390 }
8391
8392 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8393 pos += sizeof(rfc1042_header);
8394 WPA_PUT_BE16(pos, ETH_P_PAE);
8395 pos += 2;
8396 memcpy(pos, data, data_len);
8397
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008398 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8399 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008400 if (res < 0) {
8401 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8402 "failed: %d (%s)",
8403 (unsigned long) len, errno, strerror(errno));
8404 }
8405 os_free(hdr);
8406
8407 return res;
8408}
8409
8410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008411static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8412 int total_flags,
8413 int flags_or, int flags_and)
8414{
8415 struct i802_bss *bss = priv;
8416 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008417 struct nl_msg *msg;
8418 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008419 struct nl80211_sta_flag_update upd;
8420
8421 msg = nlmsg_alloc();
8422 if (!msg)
8423 return -ENOMEM;
8424
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008425 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008426
8427 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8428 if_nametoindex(bss->ifname));
8429 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8430
8431 /*
8432 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8433 * can be removed eventually.
8434 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008435 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8436 if (!flags)
8437 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008438 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008439 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008440
8441 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008442 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008443
8444 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008445 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008446
8447 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008448 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008450 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008451 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008452
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008453 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008454
8455 os_memset(&upd, 0, sizeof(upd));
8456 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8457 upd.set = sta_flags_nl80211(flags_or);
8458 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008460 return send_and_recv_msgs(drv, msg, NULL, NULL);
8461 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008462 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008463 return -ENOBUFS;
8464}
8465
8466
8467static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8468 struct wpa_driver_associate_params *params)
8469{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008470 enum nl80211_iftype nlmode, old_mode;
8471 struct hostapd_freq_params freq = {
8472 .freq = params->freq,
8473 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008474
8475 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008476 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8477 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008478 nlmode = NL80211_IFTYPE_P2P_GO;
8479 } else
8480 nlmode = NL80211_IFTYPE_AP;
8481
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008482 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008483 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008484 nl80211_remove_monitor_interface(drv);
8485 return -1;
8486 }
8487
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008488 if (nl80211_set_channel(drv->first_bss, &freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008489 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008490 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008491 nl80211_remove_monitor_interface(drv);
8492 return -1;
8493 }
8494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008495 return 0;
8496}
8497
8498
8499static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8500{
8501 struct nl_msg *msg;
8502 int ret = -1;
8503
8504 msg = nlmsg_alloc();
8505 if (!msg)
8506 return -1;
8507
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008508 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008509 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8510 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8511 msg = NULL;
8512 if (ret) {
8513 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8514 "(%s)", ret, strerror(-ret));
8515 goto nla_put_failure;
8516 }
8517
8518 ret = 0;
8519 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8520
8521nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008522 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008523 NL80211_IFTYPE_STATION)) {
8524 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8525 "station mode");
8526 }
8527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008528 nlmsg_free(msg);
8529 return ret;
8530}
8531
8532
8533static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8534 struct wpa_driver_associate_params *params)
8535{
8536 struct nl_msg *msg;
8537 int ret = -1;
8538 int count = 0;
8539
8540 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8541
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008542 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008543 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008544 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8545 "IBSS mode");
8546 return -1;
8547 }
8548
8549retry:
8550 msg = nlmsg_alloc();
8551 if (!msg)
8552 return -1;
8553
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008554 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008555 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8556
8557 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8558 goto nla_put_failure;
8559
8560 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8561 params->ssid, params->ssid_len);
8562 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8563 params->ssid);
8564 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8565 drv->ssid_len = params->ssid_len;
8566
8567 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8568 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8569
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008570 if (params->beacon_int > 0) {
8571 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8572 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8573 params->beacon_int);
8574 }
8575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008576 ret = nl80211_set_conn_keys(params, msg);
8577 if (ret)
8578 goto nla_put_failure;
8579
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008580 if (params->bssid && params->fixed_bssid) {
8581 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8582 MAC2STR(params->bssid));
8583 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8584 }
8585
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008586 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8587 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8588 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8589 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008590 wpa_printf(MSG_DEBUG, " * control port");
8591 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8592 }
8593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008594 if (params->wpa_ie) {
8595 wpa_hexdump(MSG_DEBUG,
8596 " * Extra IEs for Beacon/Probe Response frames",
8597 params->wpa_ie, params->wpa_ie_len);
8598 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8599 params->wpa_ie);
8600 }
8601
8602 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8603 msg = NULL;
8604 if (ret) {
8605 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8606 ret, strerror(-ret));
8607 count++;
8608 if (ret == -EALREADY && count == 1) {
8609 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8610 "forced leave");
8611 nl80211_leave_ibss(drv);
8612 nlmsg_free(msg);
8613 goto retry;
8614 }
8615
8616 goto nla_put_failure;
8617 }
8618 ret = 0;
8619 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8620
8621nla_put_failure:
8622 nlmsg_free(msg);
8623 return ret;
8624}
8625
8626
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008627static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8628 struct wpa_driver_associate_params *params,
8629 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008630{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008631 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008633 if (params->bssid) {
8634 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8635 MAC2STR(params->bssid));
8636 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8637 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008638
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008639 if (params->bssid_hint) {
8640 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8641 MAC2STR(params->bssid_hint));
8642 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8643 params->bssid_hint);
8644 }
8645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008646 if (params->freq) {
8647 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8648 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008649 drv->assoc_freq = params->freq;
8650 } else
8651 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008652
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008653 if (params->freq_hint) {
8654 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8655 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8656 params->freq_hint);
8657 }
8658
Dmitry Shmidt04949592012-07-19 12:16:46 -07008659 if (params->bg_scan_period >= 0) {
8660 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8661 params->bg_scan_period);
8662 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8663 params->bg_scan_period);
8664 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008666 if (params->ssid) {
8667 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8668 params->ssid, params->ssid_len);
8669 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8670 params->ssid);
8671 if (params->ssid_len > sizeof(drv->ssid))
8672 goto nla_put_failure;
8673 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8674 drv->ssid_len = params->ssid_len;
8675 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008677 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8678 if (params->wpa_ie)
8679 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8680 params->wpa_ie);
8681
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008682 if (params->wpa_proto) {
8683 enum nl80211_wpa_versions ver = 0;
8684
8685 if (params->wpa_proto & WPA_PROTO_WPA)
8686 ver |= NL80211_WPA_VERSION_1;
8687 if (params->wpa_proto & WPA_PROTO_RSN)
8688 ver |= NL80211_WPA_VERSION_2;
8689
8690 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8691 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8692 }
8693
8694 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8695 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8696 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8697 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8698 }
8699
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008700 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8701 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8702 /*
8703 * This is likely to work even though many drivers do not
8704 * advertise support for operations without GTK.
8705 */
8706 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8707 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008708 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8709 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8710 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8711 }
8712
8713 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8714 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8715 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8716 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008717 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8718 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008719 int mgmt = WLAN_AKM_SUITE_PSK;
8720
8721 switch (params->key_mgmt_suite) {
8722 case WPA_KEY_MGMT_CCKM:
8723 mgmt = WLAN_AKM_SUITE_CCKM;
8724 break;
8725 case WPA_KEY_MGMT_IEEE8021X:
8726 mgmt = WLAN_AKM_SUITE_8021X;
8727 break;
8728 case WPA_KEY_MGMT_FT_IEEE8021X:
8729 mgmt = WLAN_AKM_SUITE_FT_8021X;
8730 break;
8731 case WPA_KEY_MGMT_FT_PSK:
8732 mgmt = WLAN_AKM_SUITE_FT_PSK;
8733 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008734 case WPA_KEY_MGMT_OSEN:
8735 mgmt = WLAN_AKM_SUITE_OSEN;
8736 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008737 case WPA_KEY_MGMT_PSK:
8738 default:
8739 mgmt = WLAN_AKM_SUITE_PSK;
8740 break;
8741 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008742 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008743 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8744 }
8745
8746 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8747
8748 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8749 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8750
8751 if (params->disable_ht)
8752 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8753
8754 if (params->htcaps && params->htcaps_mask) {
8755 int sz = sizeof(struct ieee80211_ht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008756 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008757 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008758 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
8759 params->htcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008760 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8761 params->htcaps_mask);
8762 }
8763
8764#ifdef CONFIG_VHT_OVERRIDES
8765 if (params->disable_vht) {
8766 wpa_printf(MSG_DEBUG, " * VHT disabled");
8767 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8768 }
8769
8770 if (params->vhtcaps && params->vhtcaps_mask) {
8771 int sz = sizeof(struct ieee80211_vht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008772 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008773 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008774 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
8775 params->vhtcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008776 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8777 params->vhtcaps_mask);
8778 }
8779#endif /* CONFIG_VHT_OVERRIDES */
8780
8781 if (params->p2p)
8782 wpa_printf(MSG_DEBUG, " * P2P group");
8783
8784 return 0;
8785nla_put_failure:
8786 return -1;
8787}
8788
8789
8790static int wpa_driver_nl80211_try_connect(
8791 struct wpa_driver_nl80211_data *drv,
8792 struct wpa_driver_associate_params *params)
8793{
8794 struct nl_msg *msg;
8795 enum nl80211_auth_type type;
8796 int ret;
8797 int algs;
8798
8799 msg = nlmsg_alloc();
8800 if (!msg)
8801 return -1;
8802
8803 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8804 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8805
8806 ret = nl80211_connect_common(drv, params, msg);
8807 if (ret)
8808 goto nla_put_failure;
8809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008810 algs = 0;
8811 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8812 algs++;
8813 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8814 algs++;
8815 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8816 algs++;
8817 if (algs > 1) {
8818 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
8819 "selection");
8820 goto skip_auth_type;
8821 }
8822
8823 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8824 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
8825 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
8826 type = NL80211_AUTHTYPE_SHARED_KEY;
8827 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
8828 type = NL80211_AUTHTYPE_NETWORK_EAP;
8829 else if (params->auth_alg & WPA_AUTH_ALG_FT)
8830 type = NL80211_AUTHTYPE_FT;
8831 else
8832 goto nla_put_failure;
8833
8834 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
8835 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
8836
8837skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008838 ret = nl80211_set_conn_keys(params, msg);
8839 if (ret)
8840 goto nla_put_failure;
8841
8842 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8843 msg = NULL;
8844 if (ret) {
8845 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
8846 "(%s)", ret, strerror(-ret));
8847 goto nla_put_failure;
8848 }
8849 ret = 0;
8850 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
8851
8852nla_put_failure:
8853 nlmsg_free(msg);
8854 return ret;
8855
8856}
8857
8858
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008859static int wpa_driver_nl80211_connect(
8860 struct wpa_driver_nl80211_data *drv,
8861 struct wpa_driver_associate_params *params)
8862{
8863 int ret = wpa_driver_nl80211_try_connect(drv, params);
8864 if (ret == -EALREADY) {
8865 /*
8866 * cfg80211 does not currently accept new connections if
8867 * we are already connected. As a workaround, force
8868 * disconnection and try again.
8869 */
8870 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
8871 "disconnecting before reassociation "
8872 "attempt");
8873 if (wpa_driver_nl80211_disconnect(
8874 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
8875 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008876 ret = wpa_driver_nl80211_try_connect(drv, params);
8877 }
8878 return ret;
8879}
8880
8881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008882static int wpa_driver_nl80211_associate(
8883 void *priv, struct wpa_driver_associate_params *params)
8884{
8885 struct i802_bss *bss = priv;
8886 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008887 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008888 struct nl_msg *msg;
8889
8890 if (params->mode == IEEE80211_MODE_AP)
8891 return wpa_driver_nl80211_ap(drv, params);
8892
8893 if (params->mode == IEEE80211_MODE_IBSS)
8894 return wpa_driver_nl80211_ibss(drv, params);
8895
8896 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008897 enum nl80211_iftype nlmode = params->p2p ?
8898 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
8899
8900 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008901 return -1;
8902 return wpa_driver_nl80211_connect(drv, params);
8903 }
8904
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008905 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008906
8907 msg = nlmsg_alloc();
8908 if (!msg)
8909 return -1;
8910
8911 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
8912 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008913 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008915 ret = nl80211_connect_common(drv, params, msg);
8916 if (ret)
8917 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008918
8919 if (params->prev_bssid) {
8920 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
8921 MAC2STR(params->prev_bssid));
8922 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
8923 params->prev_bssid);
8924 }
8925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008926 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8927 msg = NULL;
8928 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008929 wpa_dbg(drv->ctx, MSG_DEBUG,
8930 "nl80211: MLME command failed (assoc): ret=%d (%s)",
8931 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008932 nl80211_dump_scan(drv);
8933 goto nla_put_failure;
8934 }
8935 ret = 0;
8936 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
8937 "successfully");
8938
8939nla_put_failure:
8940 nlmsg_free(msg);
8941 return ret;
8942}
8943
8944
8945static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008946 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008947{
8948 struct nl_msg *msg;
8949 int ret = -ENOBUFS;
8950
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008951 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
8952 ifindex, mode, nl80211_iftype_str(mode));
8953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008954 msg = nlmsg_alloc();
8955 if (!msg)
8956 return -ENOMEM;
8957
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008958 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008959 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008960 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008961 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
8962
8963 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008964 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008965 if (!ret)
8966 return 0;
8967nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008968 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008969 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
8970 " %d (%s)", ifindex, mode, ret, strerror(-ret));
8971 return ret;
8972}
8973
8974
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008975static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
8976 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008977{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008978 struct wpa_driver_nl80211_data *drv = bss->drv;
8979 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008980 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008981 int was_ap = is_ap_interface(drv->nlmode);
8982 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008984 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008985 if (res && nlmode == nl80211_get_ifmode(bss))
8986 res = 0;
8987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008988 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008989 drv->nlmode = nlmode;
8990 ret = 0;
8991 goto done;
8992 }
8993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008994 if (res == -ENODEV)
8995 return -1;
8996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008997 if (nlmode == drv->nlmode) {
8998 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
8999 "requested mode - ignore error");
9000 ret = 0;
9001 goto done; /* Already in the requested mode */
9002 }
9003
9004 /* mac80211 doesn't allow mode changes while the device is up, so
9005 * take the device down, try to set the mode again, and bring the
9006 * device back up.
9007 */
9008 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9009 "interface down");
9010 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009011 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009012 if (res == -EACCES || res == -ENODEV)
9013 break;
9014 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009015 /* Try to set the mode again while the interface is
9016 * down */
9017 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009018 if (ret == -EACCES)
9019 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009020 res = i802_set_iface_flags(bss, 1);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009021 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009022 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009023 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009024 break;
9025 } else
9026 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9027 "interface down");
9028 os_sleep(0, 100000);
9029 }
9030
9031 if (!ret) {
9032 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9033 "interface is down");
9034 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009035 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009036 }
9037
9038done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009039 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009040 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9041 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009042 return ret;
9043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009044
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009045 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009046 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
9047 else if (drv->disabled_11b_rates)
9048 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
9049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009050 if (is_ap_interface(nlmode)) {
9051 nl80211_mgmt_unsubscribe(bss, "start AP");
9052 /* Setup additional AP mode functionality if needed */
9053 if (nl80211_setup_ap(bss))
9054 return -1;
9055 } else if (was_ap) {
9056 /* Remove additional AP mode functionality */
9057 nl80211_teardown_ap(bss);
9058 } else {
9059 nl80211_mgmt_unsubscribe(bss, "mode change");
9060 }
9061
Dmitry Shmidt04949592012-07-19 12:16:46 -07009062 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009063 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9064 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9065 "frame processing - ignore for now");
9066
9067 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009068}
9069
9070
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009071static int dfs_info_handler(struct nl_msg *msg, void *arg)
9072{
9073 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9074 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9075 int *dfs_capability_ptr = arg;
9076
9077 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9078 genlmsg_attrlen(gnlh, 0), NULL);
9079
9080 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9081 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9082 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9083
9084 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9085 nla_data(nl_vend), nla_len(nl_vend), NULL);
9086
9087 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9088 u32 val;
9089 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9090 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9091 val);
9092 *dfs_capability_ptr = val;
9093 }
9094 }
9095
9096 return NL_SKIP;
9097}
9098
9099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009100static int wpa_driver_nl80211_get_capa(void *priv,
9101 struct wpa_driver_capa *capa)
9102{
9103 struct i802_bss *bss = priv;
9104 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009105 struct nl_msg *msg;
9106 int dfs_capability = 0;
9107 int ret = 0;
9108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009109 if (!drv->has_capability)
9110 return -1;
9111 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009112 if (drv->extended_capa && drv->extended_capa_mask) {
9113 capa->extended_capa = drv->extended_capa;
9114 capa->extended_capa_mask = drv->extended_capa_mask;
9115 capa->extended_capa_len = drv->extended_capa_len;
9116 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009117
9118 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9119 !drv->allow_p2p_device) {
9120 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9121 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9122 }
9123
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009124 if (drv->dfs_vendor_cmd_avail == 1) {
9125 msg = nlmsg_alloc();
9126 if (!msg)
9127 return -ENOMEM;
9128
9129 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9130
9131 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9132 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9133 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9134 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9135
9136 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9137 &dfs_capability);
9138 if (!ret) {
9139 if (dfs_capability)
9140 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9141 }
9142 }
9143
9144 return ret;
9145
9146 nla_put_failure:
9147 nlmsg_free(msg);
9148 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009149}
9150
9151
9152static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9153{
9154 struct i802_bss *bss = priv;
9155 struct wpa_driver_nl80211_data *drv = bss->drv;
9156
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009157 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9158 bss->ifname, drv->operstate, state,
9159 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009160 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009161 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009162 state ? IF_OPER_UP : IF_OPER_DORMANT);
9163}
9164
9165
9166static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9167{
9168 struct i802_bss *bss = priv;
9169 struct wpa_driver_nl80211_data *drv = bss->drv;
9170 struct nl_msg *msg;
9171 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009172 int ret = -ENOBUFS;
9173
9174 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9175 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9176 return 0;
9177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009178
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009179 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9180 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009182 msg = nlmsg_alloc();
9183 if (!msg)
9184 return -ENOMEM;
9185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009186 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009187
9188 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9189 if_nametoindex(bss->ifname));
9190 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9191
9192 os_memset(&upd, 0, sizeof(upd));
9193 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9194 if (authorized)
9195 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9196 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9197
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009198 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9199 msg = NULL;
9200 if (!ret)
9201 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009202 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009203 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009204 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9205 ret, strerror(-ret));
9206 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009207}
9208
9209
Jouni Malinen75ecf522011-06-27 15:19:46 -07009210/* Set kernel driver on given frequency (MHz) */
9211static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009212{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009213 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009214 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009215}
9216
9217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009218static inline int min_int(int a, int b)
9219{
9220 if (a < b)
9221 return a;
9222 return b;
9223}
9224
9225
9226static int get_key_handler(struct nl_msg *msg, void *arg)
9227{
9228 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9229 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9230
9231 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9232 genlmsg_attrlen(gnlh, 0), NULL);
9233
9234 /*
9235 * TODO: validate the key index and mac address!
9236 * Otherwise, there's a race condition as soon as
9237 * the kernel starts sending key notifications.
9238 */
9239
9240 if (tb[NL80211_ATTR_KEY_SEQ])
9241 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9242 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9243 return NL_SKIP;
9244}
9245
9246
9247static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9248 int idx, u8 *seq)
9249{
9250 struct i802_bss *bss = priv;
9251 struct wpa_driver_nl80211_data *drv = bss->drv;
9252 struct nl_msg *msg;
9253
9254 msg = nlmsg_alloc();
9255 if (!msg)
9256 return -ENOMEM;
9257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009258 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009259
9260 if (addr)
9261 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9262 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9263 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9264
9265 memset(seq, 0, 6);
9266
9267 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9268 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009269 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009270 return -ENOBUFS;
9271}
9272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009273
9274static int i802_set_rts(void *priv, int rts)
9275{
9276 struct i802_bss *bss = priv;
9277 struct wpa_driver_nl80211_data *drv = bss->drv;
9278 struct nl_msg *msg;
9279 int ret = -ENOBUFS;
9280 u32 val;
9281
9282 msg = nlmsg_alloc();
9283 if (!msg)
9284 return -ENOMEM;
9285
9286 if (rts >= 2347)
9287 val = (u32) -1;
9288 else
9289 val = rts;
9290
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009291 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009292 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9293 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9294
9295 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009296 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009297 if (!ret)
9298 return 0;
9299nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009300 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009301 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9302 "%d (%s)", rts, ret, strerror(-ret));
9303 return ret;
9304}
9305
9306
9307static int i802_set_frag(void *priv, int frag)
9308{
9309 struct i802_bss *bss = priv;
9310 struct wpa_driver_nl80211_data *drv = bss->drv;
9311 struct nl_msg *msg;
9312 int ret = -ENOBUFS;
9313 u32 val;
9314
9315 msg = nlmsg_alloc();
9316 if (!msg)
9317 return -ENOMEM;
9318
9319 if (frag >= 2346)
9320 val = (u32) -1;
9321 else
9322 val = frag;
9323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009324 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009325 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9326 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9327
9328 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009329 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009330 if (!ret)
9331 return 0;
9332nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009333 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009334 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9335 "%d: %d (%s)", frag, ret, strerror(-ret));
9336 return ret;
9337}
9338
9339
9340static int i802_flush(void *priv)
9341{
9342 struct i802_bss *bss = priv;
9343 struct wpa_driver_nl80211_data *drv = bss->drv;
9344 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009345 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009346
9347 msg = nlmsg_alloc();
9348 if (!msg)
9349 return -1;
9350
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009351 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9352 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009353 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009354
9355 /*
9356 * XXX: FIX! this needs to flush all VLANs too
9357 */
9358 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9359 if_nametoindex(bss->ifname));
9360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009361 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9362 if (res) {
9363 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9364 "(%s)", res, strerror(-res));
9365 }
9366 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009367 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009368 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009369 return -ENOBUFS;
9370}
9371
9372
9373static int get_sta_handler(struct nl_msg *msg, void *arg)
9374{
9375 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9376 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9377 struct hostap_sta_driver_data *data = arg;
9378 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9379 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9380 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9381 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9382 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9383 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9384 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009385 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009386 };
9387
9388 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9389 genlmsg_attrlen(gnlh, 0), NULL);
9390
9391 /*
9392 * TODO: validate the interface and mac address!
9393 * Otherwise, there's a race condition as soon as
9394 * the kernel starts sending station notifications.
9395 */
9396
9397 if (!tb[NL80211_ATTR_STA_INFO]) {
9398 wpa_printf(MSG_DEBUG, "sta stats missing!");
9399 return NL_SKIP;
9400 }
9401 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9402 tb[NL80211_ATTR_STA_INFO],
9403 stats_policy)) {
9404 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9405 return NL_SKIP;
9406 }
9407
9408 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9409 data->inactive_msec =
9410 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9411 if (stats[NL80211_STA_INFO_RX_BYTES])
9412 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9413 if (stats[NL80211_STA_INFO_TX_BYTES])
9414 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9415 if (stats[NL80211_STA_INFO_RX_PACKETS])
9416 data->rx_packets =
9417 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9418 if (stats[NL80211_STA_INFO_TX_PACKETS])
9419 data->tx_packets =
9420 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009421 if (stats[NL80211_STA_INFO_TX_FAILED])
9422 data->tx_retry_failed =
9423 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009424
9425 return NL_SKIP;
9426}
9427
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009428static int i802_read_sta_data(struct i802_bss *bss,
9429 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009430 const u8 *addr)
9431{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009432 struct wpa_driver_nl80211_data *drv = bss->drv;
9433 struct nl_msg *msg;
9434
9435 os_memset(data, 0, sizeof(*data));
9436 msg = nlmsg_alloc();
9437 if (!msg)
9438 return -ENOMEM;
9439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009440 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009441
9442 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9444
9445 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9446 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009447 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009448 return -ENOBUFS;
9449}
9450
9451
9452static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9453 int cw_min, int cw_max, int burst_time)
9454{
9455 struct i802_bss *bss = priv;
9456 struct wpa_driver_nl80211_data *drv = bss->drv;
9457 struct nl_msg *msg;
9458 struct nlattr *txq, *params;
9459
9460 msg = nlmsg_alloc();
9461 if (!msg)
9462 return -1;
9463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009464 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009465
9466 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9467
9468 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9469 if (!txq)
9470 goto nla_put_failure;
9471
9472 /* We are only sending parameters for a single TXQ at a time */
9473 params = nla_nest_start(msg, 1);
9474 if (!params)
9475 goto nla_put_failure;
9476
9477 switch (queue) {
9478 case 0:
9479 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9480 break;
9481 case 1:
9482 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9483 break;
9484 case 2:
9485 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9486 break;
9487 case 3:
9488 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9489 break;
9490 }
9491 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9492 * 32 usec, so need to convert the value here. */
9493 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9494 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9495 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9496 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9497
9498 nla_nest_end(msg, params);
9499
9500 nla_nest_end(msg, txq);
9501
9502 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9503 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009504 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009505 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009506 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009507 return -1;
9508}
9509
9510
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009511static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009512 const char *ifname, int vlan_id)
9513{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009514 struct wpa_driver_nl80211_data *drv = bss->drv;
9515 struct nl_msg *msg;
9516 int ret = -ENOBUFS;
9517
9518 msg = nlmsg_alloc();
9519 if (!msg)
9520 return -ENOMEM;
9521
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009522 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9523 ", ifname=%s[%d], vlan_id=%d)",
9524 bss->ifname, if_nametoindex(bss->ifname),
9525 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009526 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009527
9528 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9529 if_nametoindex(bss->ifname));
9530 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9531 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9532 if_nametoindex(ifname));
9533
9534 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009535 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009536 if (ret < 0) {
9537 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9538 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9539 MAC2STR(addr), ifname, vlan_id, ret,
9540 strerror(-ret));
9541 }
9542 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009543 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009544 return ret;
9545}
9546
9547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009548static int i802_get_inact_sec(void *priv, const u8 *addr)
9549{
9550 struct hostap_sta_driver_data data;
9551 int ret;
9552
9553 data.inactive_msec = (unsigned long) -1;
9554 ret = i802_read_sta_data(priv, &data, addr);
9555 if (ret || data.inactive_msec == (unsigned long) -1)
9556 return -1;
9557 return data.inactive_msec / 1000;
9558}
9559
9560
9561static int i802_sta_clear_stats(void *priv, const u8 *addr)
9562{
9563#if 0
9564 /* TODO */
9565#endif
9566 return 0;
9567}
9568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009569
9570static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9571 int reason)
9572{
9573 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009574 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009575 struct ieee80211_mgmt mgmt;
9576
Dmitry Shmidt04949592012-07-19 12:16:46 -07009577 if (drv->device_ap_sme)
9578 return wpa_driver_nl80211_sta_remove(bss, addr);
9579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009580 memset(&mgmt, 0, sizeof(mgmt));
9581 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9582 WLAN_FC_STYPE_DEAUTH);
9583 memcpy(mgmt.da, addr, ETH_ALEN);
9584 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9585 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9586 mgmt.u.deauth.reason_code = host_to_le16(reason);
9587 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9588 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009589 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9590 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009591}
9592
9593
9594static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9595 int reason)
9596{
9597 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009598 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009599 struct ieee80211_mgmt mgmt;
9600
Dmitry Shmidt04949592012-07-19 12:16:46 -07009601 if (drv->device_ap_sme)
9602 return wpa_driver_nl80211_sta_remove(bss, addr);
9603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009604 memset(&mgmt, 0, sizeof(mgmt));
9605 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9606 WLAN_FC_STYPE_DISASSOC);
9607 memcpy(mgmt.da, addr, ETH_ALEN);
9608 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9609 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9610 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9611 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9612 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009613 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9614 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009615}
9616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009617
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009618static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9619{
9620 char buf[200], *pos, *end;
9621 int i, res;
9622
9623 pos = buf;
9624 end = pos + sizeof(buf);
9625
9626 for (i = 0; i < drv->num_if_indices; i++) {
9627 if (!drv->if_indices[i])
9628 continue;
9629 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9630 if (res < 0 || res >= end - pos)
9631 break;
9632 pos += res;
9633 }
9634 *pos = '\0';
9635
9636 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9637 drv->num_if_indices, buf);
9638}
9639
9640
Jouni Malinen75ecf522011-06-27 15:19:46 -07009641static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9642{
9643 int i;
9644 int *old;
9645
9646 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9647 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009648 if (have_ifidx(drv, ifidx)) {
9649 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9650 ifidx);
9651 return;
9652 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009653 for (i = 0; i < drv->num_if_indices; i++) {
9654 if (drv->if_indices[i] == 0) {
9655 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009656 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009657 return;
9658 }
9659 }
9660
9661 if (drv->if_indices != drv->default_if_indices)
9662 old = drv->if_indices;
9663 else
9664 old = NULL;
9665
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009666 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9667 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009668 if (!drv->if_indices) {
9669 if (!old)
9670 drv->if_indices = drv->default_if_indices;
9671 else
9672 drv->if_indices = old;
9673 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9674 "interfaces");
9675 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9676 return;
9677 } else if (!old)
9678 os_memcpy(drv->if_indices, drv->default_if_indices,
9679 sizeof(drv->default_if_indices));
9680 drv->if_indices[drv->num_if_indices] = ifidx;
9681 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009682 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009683}
9684
9685
9686static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9687{
9688 int i;
9689
9690 for (i = 0; i < drv->num_if_indices; i++) {
9691 if (drv->if_indices[i] == ifidx) {
9692 drv->if_indices[i] = 0;
9693 break;
9694 }
9695 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009696 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009697}
9698
9699
9700static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9701{
9702 int i;
9703
9704 for (i = 0; i < drv->num_if_indices; i++)
9705 if (drv->if_indices[i] == ifidx)
9706 return 1;
9707
9708 return 0;
9709}
9710
9711
9712static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009713 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009714{
9715 struct i802_bss *bss = priv;
9716 struct wpa_driver_nl80211_data *drv = bss->drv;
9717 char name[IFNAMSIZ + 1];
9718
9719 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009720 if (ifname_wds)
9721 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9722
Jouni Malinen75ecf522011-06-27 15:19:46 -07009723 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9724 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9725 if (val) {
9726 if (!if_nametoindex(name)) {
9727 if (nl80211_create_iface(drv, name,
9728 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009729 bss->addr, 1, NULL, NULL, 0) <
9730 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009731 return -1;
9732 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009733 linux_br_add_if(drv->global->ioctl_sock,
9734 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009735 return -1;
9736 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009737 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9738 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9739 "interface %s up", name);
9740 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009741 return i802_set_sta_vlan(priv, addr, name, 0);
9742 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009743 if (bridge_ifname)
9744 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9745 name);
9746
Jouni Malinen75ecf522011-06-27 15:19:46 -07009747 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009748 nl80211_remove_iface(drv, if_nametoindex(name));
9749 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009750 }
9751}
9752
9753
9754static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9755{
9756 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9757 struct sockaddr_ll lladdr;
9758 unsigned char buf[3000];
9759 int len;
9760 socklen_t fromlen = sizeof(lladdr);
9761
9762 len = recvfrom(sock, buf, sizeof(buf), 0,
9763 (struct sockaddr *)&lladdr, &fromlen);
9764 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009765 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9766 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009767 return;
9768 }
9769
9770 if (have_ifidx(drv, lladdr.sll_ifindex))
9771 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
9772}
9773
9774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009775static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
9776 struct i802_bss *bss,
9777 const char *brname, const char *ifname)
9778{
9779 int ifindex;
9780 char in_br[IFNAMSIZ];
9781
9782 os_strlcpy(bss->brname, brname, IFNAMSIZ);
9783 ifindex = if_nametoindex(brname);
9784 if (ifindex == 0) {
9785 /*
9786 * Bridge was configured, but the bridge device does
9787 * not exist. Try to add it now.
9788 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009789 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009790 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
9791 "bridge interface %s: %s",
9792 brname, strerror(errno));
9793 return -1;
9794 }
9795 bss->added_bridge = 1;
9796 add_ifidx(drv, if_nametoindex(brname));
9797 }
9798
9799 if (linux_br_get(in_br, ifname) == 0) {
9800 if (os_strcmp(in_br, brname) == 0)
9801 return 0; /* already in the bridge */
9802
9803 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
9804 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009805 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
9806 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009807 wpa_printf(MSG_ERROR, "nl80211: Failed to "
9808 "remove interface %s from bridge "
9809 "%s: %s",
9810 ifname, brname, strerror(errno));
9811 return -1;
9812 }
9813 }
9814
9815 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
9816 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009817 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009818 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
9819 "into bridge %s: %s",
9820 ifname, brname, strerror(errno));
9821 return -1;
9822 }
9823 bss->added_if_into_bridge = 1;
9824
9825 return 0;
9826}
9827
9828
9829static void *i802_init(struct hostapd_data *hapd,
9830 struct wpa_init_params *params)
9831{
9832 struct wpa_driver_nl80211_data *drv;
9833 struct i802_bss *bss;
9834 size_t i;
9835 char brname[IFNAMSIZ];
9836 int ifindex, br_ifindex;
9837 int br_added = 0;
9838
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009839 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
9840 params->global_priv, 1,
9841 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009842 if (bss == NULL)
9843 return NULL;
9844
9845 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009847 if (linux_br_get(brname, params->ifname) == 0) {
9848 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
9849 params->ifname, brname);
9850 br_ifindex = if_nametoindex(brname);
9851 } else {
9852 brname[0] = '\0';
9853 br_ifindex = 0;
9854 }
9855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009856 for (i = 0; i < params->num_bridge; i++) {
9857 if (params->bridge[i]) {
9858 ifindex = if_nametoindex(params->bridge[i]);
9859 if (ifindex)
9860 add_ifidx(drv, ifindex);
9861 if (ifindex == br_ifindex)
9862 br_added = 1;
9863 }
9864 }
9865 if (!br_added && br_ifindex &&
9866 (params->num_bridge == 0 || !params->bridge[0]))
9867 add_ifidx(drv, br_ifindex);
9868
9869 /* start listening for EAPOL on the default AP interface */
9870 add_ifidx(drv, drv->ifindex);
9871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009872 if (params->num_bridge && params->bridge[0] &&
9873 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
9874 goto failed;
9875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009876 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
9877 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009878 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
9879 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009880 goto failed;
9881 }
9882
9883 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
9884 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009885 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009886 goto failed;
9887 }
9888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009889 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9890 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009891 goto failed;
9892
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009893 memcpy(bss->addr, params->own_addr, ETH_ALEN);
9894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009895 return bss;
9896
9897failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009898 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009899 return NULL;
9900}
9901
9902
9903static void i802_deinit(void *priv)
9904{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009905 struct i802_bss *bss = priv;
9906 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009907}
9908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009909
9910static enum nl80211_iftype wpa_driver_nl80211_if_type(
9911 enum wpa_driver_if_type type)
9912{
9913 switch (type) {
9914 case WPA_IF_STATION:
9915 return NL80211_IFTYPE_STATION;
9916 case WPA_IF_P2P_CLIENT:
9917 case WPA_IF_P2P_GROUP:
9918 return NL80211_IFTYPE_P2P_CLIENT;
9919 case WPA_IF_AP_VLAN:
9920 return NL80211_IFTYPE_AP_VLAN;
9921 case WPA_IF_AP_BSS:
9922 return NL80211_IFTYPE_AP;
9923 case WPA_IF_P2P_GO:
9924 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009925 case WPA_IF_P2P_DEVICE:
9926 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009927 }
9928 return -1;
9929}
9930
9931
9932#ifdef CONFIG_P2P
9933
9934static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
9935{
9936 struct wpa_driver_nl80211_data *drv;
9937 dl_list_for_each(drv, &global->interfaces,
9938 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009939 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009940 return 1;
9941 }
9942 return 0;
9943}
9944
9945
9946static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
9947 u8 *new_addr)
9948{
9949 unsigned int idx;
9950
9951 if (!drv->global)
9952 return -1;
9953
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009954 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009955 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009956 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009957 new_addr[0] ^= idx << 2;
9958 if (!nl80211_addr_in_use(drv->global, new_addr))
9959 break;
9960 }
9961 if (idx == 64)
9962 return -1;
9963
9964 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
9965 MACSTR, MAC2STR(new_addr));
9966
9967 return 0;
9968}
9969
9970#endif /* CONFIG_P2P */
9971
9972
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009973struct wdev_info {
9974 u64 wdev_id;
9975 int wdev_id_set;
9976 u8 macaddr[ETH_ALEN];
9977};
9978
9979static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
9980{
9981 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9982 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9983 struct wdev_info *wi = arg;
9984
9985 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9986 genlmsg_attrlen(gnlh, 0), NULL);
9987 if (tb[NL80211_ATTR_WDEV]) {
9988 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
9989 wi->wdev_id_set = 1;
9990 }
9991
9992 if (tb[NL80211_ATTR_MAC])
9993 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
9994 ETH_ALEN);
9995
9996 return NL_SKIP;
9997}
9998
9999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010000static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
10001 const char *ifname, const u8 *addr,
10002 void *bss_ctx, void **drv_priv,
10003 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010004 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010005{
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010006 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010007 struct i802_bss *bss = priv;
10008 struct wpa_driver_nl80211_data *drv = bss->drv;
10009 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010010 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010011
10012 if (addr)
10013 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010014 nlmode = wpa_driver_nl80211_if_type(type);
10015 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
10016 struct wdev_info p2pdev_info;
10017
10018 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
10019 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
10020 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010021 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010022 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
10023 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10024 ifname);
10025 return -1;
10026 }
10027
10028 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10029 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10030 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10031 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10032 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10033 ifname,
10034 (long long unsigned int) p2pdev_info.wdev_id);
10035 } else {
10036 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010037 0, NULL, NULL, use_existing);
10038 if (use_existing && ifidx == -ENFILE) {
10039 added = 0;
10040 ifidx = if_nametoindex(ifname);
10041 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010042 return -1;
10043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010044 }
10045
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010046 if (!addr) {
10047 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10048 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10049 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10050 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010051 if (added)
10052 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010053 return -1;
10054 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010055 }
10056
10057#ifdef CONFIG_P2P
10058 if (!addr &&
10059 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10060 type == WPA_IF_P2P_GO)) {
10061 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010062 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010063
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010064 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010065 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010066 nl80211_remove_iface(drv, ifidx);
10067 return -1;
10068 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010069 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010070 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10071 "for P2P group interface");
10072 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
10073 nl80211_remove_iface(drv, ifidx);
10074 return -1;
10075 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010076 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010077 new_addr) < 0) {
10078 nl80211_remove_iface(drv, ifidx);
10079 return -1;
10080 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010081 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010082 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010083 }
10084#endif /* CONFIG_P2P */
10085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010086 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010087 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10088 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010089 if (added)
10090 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010091 return -1;
10092 }
10093
10094 if (bridge &&
10095 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10096 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10097 "interface %s to a bridge %s",
10098 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010099 if (added)
10100 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010101 os_free(new_bss);
10102 return -1;
10103 }
10104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010105 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10106 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010107 nl80211_remove_iface(drv, ifidx);
10108 os_free(new_bss);
10109 return -1;
10110 }
10111 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010112 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010113 new_bss->ifindex = ifidx;
10114 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010115 new_bss->next = drv->first_bss->next;
10116 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010117 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010118 new_bss->added_if = added;
10119 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010120 if (drv_priv)
10121 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010122 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010123
10124 /* Subscribe management frames for this WPA_IF_AP_BSS */
10125 if (nl80211_setup_ap(new_bss))
10126 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010128
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010129 if (drv->global)
10130 drv->global->if_add_ifindex = ifidx;
10131
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010132 if (ifidx > 0)
10133 add_ifidx(drv, ifidx);
10134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010135 return 0;
10136}
10137
10138
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010139static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010140 enum wpa_driver_if_type type,
10141 const char *ifname)
10142{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010143 struct wpa_driver_nl80211_data *drv = bss->drv;
10144 int ifindex = if_nametoindex(ifname);
10145
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010146 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10147 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010148 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010149 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010150 else if (ifindex > 0 && !bss->added_if)
10151 del_ifidx(drv, ifindex);
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010152
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010153 if (type != WPA_IF_AP_BSS)
10154 return 0;
10155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010156 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010157 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10158 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010159 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10160 "interface %s from bridge %s: %s",
10161 bss->ifname, bss->brname, strerror(errno));
10162 }
10163 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010164 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010165 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10166 "bridge %s: %s",
10167 bss->brname, strerror(errno));
10168 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010169
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010170 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010171 struct i802_bss *tbss;
10172
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010173 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10174 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010175 if (tbss->next == bss) {
10176 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010177 /* Unsubscribe management frames */
10178 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010179 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010180 if (!bss->added_if)
10181 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010182 os_free(bss);
10183 bss = NULL;
10184 break;
10185 }
10186 }
10187 if (bss)
10188 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10189 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010190 } else {
10191 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10192 nl80211_teardown_ap(bss);
10193 if (!bss->added_if && !drv->first_bss->next)
10194 wpa_driver_nl80211_del_beacon(drv);
10195 nl80211_destroy_bss(bss);
10196 if (!bss->added_if)
10197 i802_set_iface_flags(bss, 0);
10198 if (drv->first_bss->next) {
10199 drv->first_bss = drv->first_bss->next;
10200 drv->ctx = drv->first_bss->ctx;
10201 os_free(bss);
10202 } else {
10203 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010206
10207 return 0;
10208}
10209
10210
10211static int cookie_handler(struct nl_msg *msg, void *arg)
10212{
10213 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10214 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10215 u64 *cookie = arg;
10216 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10217 genlmsg_attrlen(gnlh, 0), NULL);
10218 if (tb[NL80211_ATTR_COOKIE])
10219 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10220 return NL_SKIP;
10221}
10222
10223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010224static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010225 unsigned int freq, unsigned int wait,
10226 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010227 u64 *cookie_out, int no_cck, int no_ack,
10228 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010229{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010230 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010231 struct nl_msg *msg;
10232 u64 cookie;
10233 int ret = -1;
10234
10235 msg = nlmsg_alloc();
10236 if (!msg)
10237 return -1;
10238
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010239 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010240 "no_ack=%d offchanok=%d",
10241 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010242 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010243 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010244
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010245 if (nl80211_set_iface_id(msg, bss) < 0)
10246 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010247 if (freq)
10248 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010249 if (wait)
10250 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010251 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10252 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010253 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10254 if (no_cck)
10255 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10256 if (no_ack)
10257 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010259 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10260
10261 cookie = 0;
10262 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10263 msg = NULL;
10264 if (ret) {
10265 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010266 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10267 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010268 goto nla_put_failure;
10269 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010270 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010271 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10272 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010273
10274 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010275 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010276
10277nla_put_failure:
10278 nlmsg_free(msg);
10279 return ret;
10280}
10281
10282
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010283static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10284 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010285 unsigned int wait_time,
10286 const u8 *dst, const u8 *src,
10287 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010288 const u8 *data, size_t data_len,
10289 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010290{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010291 struct wpa_driver_nl80211_data *drv = bss->drv;
10292 int ret = -1;
10293 u8 *buf;
10294 struct ieee80211_hdr *hdr;
10295
10296 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010297 "freq=%u MHz wait=%d ms no_cck=%d)",
10298 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010299
10300 buf = os_zalloc(24 + data_len);
10301 if (buf == NULL)
10302 return ret;
10303 os_memcpy(buf + 24, data, data_len);
10304 hdr = (struct ieee80211_hdr *) buf;
10305 hdr->frame_control =
10306 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10307 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10308 os_memcpy(hdr->addr2, src, ETH_ALEN);
10309 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10310
Dmitry Shmidt56052862013-10-04 10:23:25 -070010311 if (is_ap_interface(drv->nlmode) &&
10312 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10313 (int) freq == bss->freq || drv->device_ap_sme ||
10314 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010315 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10316 0, freq, no_cck, 1,
10317 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010318 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010319 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010320 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010321 &drv->send_action_cookie,
10322 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010323
10324 os_free(buf);
10325 return ret;
10326}
10327
10328
10329static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10330{
10331 struct i802_bss *bss = priv;
10332 struct wpa_driver_nl80211_data *drv = bss->drv;
10333 struct nl_msg *msg;
10334 int ret;
10335
10336 msg = nlmsg_alloc();
10337 if (!msg)
10338 return;
10339
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010340 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10341 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010342 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010343
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010344 if (nl80211_set_iface_id(msg, bss) < 0)
10345 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010346 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10347
10348 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10349 msg = NULL;
10350 if (ret)
10351 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10352 "(%s)", ret, strerror(-ret));
10353
10354 nla_put_failure:
10355 nlmsg_free(msg);
10356}
10357
10358
10359static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10360 unsigned int duration)
10361{
10362 struct i802_bss *bss = priv;
10363 struct wpa_driver_nl80211_data *drv = bss->drv;
10364 struct nl_msg *msg;
10365 int ret;
10366 u64 cookie;
10367
10368 msg = nlmsg_alloc();
10369 if (!msg)
10370 return -1;
10371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010372 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010373
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010374 if (nl80211_set_iface_id(msg, bss) < 0)
10375 goto nla_put_failure;
10376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010377 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10378 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10379
10380 cookie = 0;
10381 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010382 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010383 if (ret == 0) {
10384 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10385 "0x%llx for freq=%u MHz duration=%u",
10386 (long long unsigned int) cookie, freq, duration);
10387 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010388 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010389 return 0;
10390 }
10391 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10392 "(freq=%d duration=%u): %d (%s)",
10393 freq, duration, ret, strerror(-ret));
10394nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010395 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010396 return -1;
10397}
10398
10399
10400static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10401{
10402 struct i802_bss *bss = priv;
10403 struct wpa_driver_nl80211_data *drv = bss->drv;
10404 struct nl_msg *msg;
10405 int ret;
10406
10407 if (!drv->pending_remain_on_chan) {
10408 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10409 "to cancel");
10410 return -1;
10411 }
10412
10413 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10414 "0x%llx",
10415 (long long unsigned int) drv->remain_on_chan_cookie);
10416
10417 msg = nlmsg_alloc();
10418 if (!msg)
10419 return -1;
10420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010421 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010422
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010423 if (nl80211_set_iface_id(msg, bss) < 0)
10424 goto nla_put_failure;
10425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010426 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10427
10428 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010429 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010430 if (ret == 0)
10431 return 0;
10432 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10433 "%d (%s)", ret, strerror(-ret));
10434nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010435 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010436 return -1;
10437}
10438
10439
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010440static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010441{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010442 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010444 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010445 if (bss->nl_preq && drv->device_ap_sme &&
10446 is_ap_interface(drv->nlmode)) {
10447 /*
10448 * Do not disable Probe Request reporting that was
10449 * enabled in nl80211_setup_ap().
10450 */
10451 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10452 "Probe Request reporting nl_preq=%p while "
10453 "in AP mode", bss->nl_preq);
10454 } else if (bss->nl_preq) {
10455 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10456 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010457 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010458 }
10459 return 0;
10460 }
10461
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010462 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010463 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010464 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010465 return 0;
10466 }
10467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010468 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10469 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010470 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010471 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10472 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010473
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010474 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010475 (WLAN_FC_TYPE_MGMT << 2) |
10476 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010477 NULL, 0) < 0)
10478 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010479
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010480 nl80211_register_eloop_read(&bss->nl_preq,
10481 wpa_driver_nl80211_event_receive,
10482 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010483
10484 return 0;
10485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010486 out_err:
10487 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010488 return -1;
10489}
10490
10491
10492static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10493 int ifindex, int disabled)
10494{
10495 struct nl_msg *msg;
10496 struct nlattr *bands, *band;
10497 int ret;
10498
10499 msg = nlmsg_alloc();
10500 if (!msg)
10501 return -1;
10502
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010503 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010504 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10505
10506 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10507 if (!bands)
10508 goto nla_put_failure;
10509
10510 /*
10511 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10512 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10513 * rates. All 5 GHz rates are left enabled.
10514 */
10515 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10516 if (!band)
10517 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010518 if (disabled) {
10519 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10520 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10521 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010522 nla_nest_end(msg, band);
10523
10524 nla_nest_end(msg, bands);
10525
10526 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10527 msg = NULL;
10528 if (ret) {
10529 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10530 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010531 } else
10532 drv->disabled_11b_rates = disabled;
10533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010534 return ret;
10535
10536nla_put_failure:
10537 nlmsg_free(msg);
10538 return -1;
10539}
10540
10541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010542static int wpa_driver_nl80211_deinit_ap(void *priv)
10543{
10544 struct i802_bss *bss = priv;
10545 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010546 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010547 return -1;
10548 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010549
10550 /*
10551 * If the P2P GO interface was dynamically added, then it is
10552 * possible that the interface change to station is not possible.
10553 */
10554 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10555 return 0;
10556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010557 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010558}
10559
10560
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010561static int wpa_driver_nl80211_stop_ap(void *priv)
10562{
10563 struct i802_bss *bss = priv;
10564 struct wpa_driver_nl80211_data *drv = bss->drv;
10565 if (!is_ap_interface(drv->nlmode))
10566 return -1;
10567 wpa_driver_nl80211_del_beacon(drv);
10568 bss->beacon_set = 0;
10569 return 0;
10570}
10571
10572
Dmitry Shmidt04949592012-07-19 12:16:46 -070010573static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10574{
10575 struct i802_bss *bss = priv;
10576 struct wpa_driver_nl80211_data *drv = bss->drv;
10577 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10578 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010579
10580 /*
10581 * If the P2P Client interface was dynamically added, then it is
10582 * possible that the interface change to station is not possible.
10583 */
10584 if (bss->if_dynamic)
10585 return 0;
10586
Dmitry Shmidt04949592012-07-19 12:16:46 -070010587 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10588}
10589
10590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010591static void wpa_driver_nl80211_resume(void *priv)
10592{
10593 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010594
10595 if (i802_set_iface_flags(bss, 1))
10596 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010597}
10598
10599
10600static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10601 const u8 *ies, size_t ies_len)
10602{
10603 struct i802_bss *bss = priv;
10604 struct wpa_driver_nl80211_data *drv = bss->drv;
10605 int ret;
10606 u8 *data, *pos;
10607 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010608 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010609
10610 if (action != 1) {
10611 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10612 "action %d", action);
10613 return -1;
10614 }
10615
10616 /*
10617 * Action frame payload:
10618 * Category[1] = 6 (Fast BSS Transition)
10619 * Action[1] = 1 (Fast BSS Transition Request)
10620 * STA Address
10621 * Target AP Address
10622 * FT IEs
10623 */
10624
10625 data_len = 2 + 2 * ETH_ALEN + ies_len;
10626 data = os_malloc(data_len);
10627 if (data == NULL)
10628 return -1;
10629 pos = data;
10630 *pos++ = 0x06; /* FT Action category */
10631 *pos++ = action;
10632 os_memcpy(pos, own_addr, ETH_ALEN);
10633 pos += ETH_ALEN;
10634 os_memcpy(pos, target_ap, ETH_ALEN);
10635 pos += ETH_ALEN;
10636 os_memcpy(pos, ies, ies_len);
10637
10638 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10639 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010640 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010641 os_free(data);
10642
10643 return ret;
10644}
10645
10646
10647static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10648{
10649 struct i802_bss *bss = priv;
10650 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010651 struct nl_msg *msg;
10652 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010653 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010654
10655 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10656 "hysteresis=%d", threshold, hysteresis);
10657
10658 msg = nlmsg_alloc();
10659 if (!msg)
10660 return -1;
10661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010662 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010663
10664 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10665
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010666 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010667 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010668 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010669
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010670 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10671 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10672 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010673
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010674 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010675 msg = NULL;
10676
10677nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010678 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010679 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010680}
10681
10682
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010683static int get_channel_width(struct nl_msg *msg, void *arg)
10684{
10685 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10686 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10687 struct wpa_signal_info *sig_change = arg;
10688
10689 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10690 genlmsg_attrlen(gnlh, 0), NULL);
10691
10692 sig_change->center_frq1 = -1;
10693 sig_change->center_frq2 = -1;
10694 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10695
10696 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10697 sig_change->chanwidth = convert2width(
10698 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10699 if (tb[NL80211_ATTR_CENTER_FREQ1])
10700 sig_change->center_frq1 =
10701 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10702 if (tb[NL80211_ATTR_CENTER_FREQ2])
10703 sig_change->center_frq2 =
10704 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10705 }
10706
10707 return NL_SKIP;
10708}
10709
10710
10711static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10712 struct wpa_signal_info *sig)
10713{
10714 struct nl_msg *msg;
10715
10716 msg = nlmsg_alloc();
10717 if (!msg)
10718 return -ENOMEM;
10719
10720 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10721 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10722
10723 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10724
10725nla_put_failure:
10726 nlmsg_free(msg);
10727 return -ENOBUFS;
10728}
10729
10730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010731static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10732{
10733 struct i802_bss *bss = priv;
10734 struct wpa_driver_nl80211_data *drv = bss->drv;
10735 int res;
10736
10737 os_memset(si, 0, sizeof(*si));
10738 res = nl80211_get_link_signal(drv, si);
10739 if (res != 0)
10740 return res;
10741
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010742 res = nl80211_get_channel_width(drv, si);
10743 if (res != 0)
10744 return res;
10745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010746 return nl80211_get_link_noise(drv, si);
10747}
10748
10749
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010750static int wpa_driver_nl80211_shared_freq(void *priv)
10751{
10752 struct i802_bss *bss = priv;
10753 struct wpa_driver_nl80211_data *drv = bss->drv;
10754 struct wpa_driver_nl80211_data *driver;
10755 int freq = 0;
10756
10757 /*
10758 * If the same PHY is in connected state with some other interface,
10759 * then retrieve the assoc freq.
10760 */
10761 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
10762 drv->phyname);
10763
10764 dl_list_for_each(driver, &drv->global->interfaces,
10765 struct wpa_driver_nl80211_data, list) {
10766 if (drv == driver ||
10767 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010768 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010769 continue;
10770
10771 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
10772 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010773 driver->phyname, driver->first_bss->ifname,
10774 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070010775 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010776 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010777 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010778 freq = nl80211_get_assoc_freq(driver);
10779 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
10780 drv->phyname, freq);
10781 }
10782
10783 if (!freq)
10784 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
10785 "PHY (%s) in associated state", drv->phyname);
10786
10787 return freq;
10788}
10789
10790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010791static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
10792 int encrypt)
10793{
10794 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010795 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
10796 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010797}
10798
10799
10800static int nl80211_set_param(void *priv, const char *param)
10801{
10802 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
10803 if (param == NULL)
10804 return 0;
10805
10806#ifdef CONFIG_P2P
10807 if (os_strstr(param, "use_p2p_group_interface=1")) {
10808 struct i802_bss *bss = priv;
10809 struct wpa_driver_nl80211_data *drv = bss->drv;
10810
10811 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
10812 "interface");
10813 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
10814 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
10815 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010816
10817 if (os_strstr(param, "p2p_device=1")) {
10818 struct i802_bss *bss = priv;
10819 struct wpa_driver_nl80211_data *drv = bss->drv;
10820 drv->allow_p2p_device = 1;
10821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010822#endif /* CONFIG_P2P */
10823
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080010824 if (os_strstr(param, "use_monitor=1")) {
10825 struct i802_bss *bss = priv;
10826 struct wpa_driver_nl80211_data *drv = bss->drv;
10827 drv->use_monitor = 1;
10828 }
10829
10830 if (os_strstr(param, "force_connect_cmd=1")) {
10831 struct i802_bss *bss = priv;
10832 struct wpa_driver_nl80211_data *drv = bss->drv;
10833 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
10834 }
10835
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010836 if (os_strstr(param, "no_offchannel_tx=1")) {
10837 struct i802_bss *bss = priv;
10838 struct wpa_driver_nl80211_data *drv = bss->drv;
10839 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
10840 drv->test_use_roc_tx = 1;
10841 }
10842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010843 return 0;
10844}
10845
10846
10847static void * nl80211_global_init(void)
10848{
10849 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010850 struct netlink_config *cfg;
10851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010852 global = os_zalloc(sizeof(*global));
10853 if (global == NULL)
10854 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010855 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010856 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010857 global->if_add_ifindex = -1;
10858
10859 cfg = os_zalloc(sizeof(*cfg));
10860 if (cfg == NULL)
10861 goto err;
10862
10863 cfg->ctx = global;
10864 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
10865 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
10866 global->netlink = netlink_init(cfg);
10867 if (global->netlink == NULL) {
10868 os_free(cfg);
10869 goto err;
10870 }
10871
10872 if (wpa_driver_nl80211_init_nl_global(global) < 0)
10873 goto err;
10874
10875 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
10876 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010877 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
10878 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010879 goto err;
10880 }
10881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010882 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010883
10884err:
10885 nl80211_global_deinit(global);
10886 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010887}
10888
10889
10890static void nl80211_global_deinit(void *priv)
10891{
10892 struct nl80211_global *global = priv;
10893 if (global == NULL)
10894 return;
10895 if (!dl_list_empty(&global->interfaces)) {
10896 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
10897 "nl80211_global_deinit",
10898 dl_list_len(&global->interfaces));
10899 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010900
10901 if (global->netlink)
10902 netlink_deinit(global->netlink);
10903
10904 nl_destroy_handles(&global->nl);
10905
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010906 if (global->nl_event)
10907 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010908
10909 nl_cb_put(global->nl_cb);
10910
10911 if (global->ioctl_sock >= 0)
10912 close(global->ioctl_sock);
10913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010914 os_free(global);
10915}
10916
10917
10918static const char * nl80211_get_radio_name(void *priv)
10919{
10920 struct i802_bss *bss = priv;
10921 struct wpa_driver_nl80211_data *drv = bss->drv;
10922 return drv->phyname;
10923}
10924
10925
Jouni Malinen75ecf522011-06-27 15:19:46 -070010926static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
10927 const u8 *pmkid)
10928{
10929 struct nl_msg *msg;
10930
10931 msg = nlmsg_alloc();
10932 if (!msg)
10933 return -ENOMEM;
10934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010935 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010936
10937 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
10938 if (pmkid)
10939 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
10940 if (bssid)
10941 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
10942
10943 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
10944 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010945 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070010946 return -ENOBUFS;
10947}
10948
10949
10950static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10951{
10952 struct i802_bss *bss = priv;
10953 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
10954 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
10955}
10956
10957
10958static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
10959{
10960 struct i802_bss *bss = priv;
10961 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
10962 MAC2STR(bssid));
10963 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
10964}
10965
10966
10967static int nl80211_flush_pmkid(void *priv)
10968{
10969 struct i802_bss *bss = priv;
10970 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
10971 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
10972}
10973
10974
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010975static void clean_survey_results(struct survey_results *survey_results)
10976{
10977 struct freq_survey *survey, *tmp;
10978
10979 if (dl_list_empty(&survey_results->survey_list))
10980 return;
10981
10982 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
10983 struct freq_survey, list) {
10984 dl_list_del(&survey->list);
10985 os_free(survey);
10986 }
10987}
10988
10989
10990static void add_survey(struct nlattr **sinfo, u32 ifidx,
10991 struct dl_list *survey_list)
10992{
10993 struct freq_survey *survey;
10994
10995 survey = os_zalloc(sizeof(struct freq_survey));
10996 if (!survey)
10997 return;
10998
10999 survey->ifidx = ifidx;
11000 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11001 survey->filled = 0;
11002
11003 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11004 survey->nf = (int8_t)
11005 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11006 survey->filled |= SURVEY_HAS_NF;
11007 }
11008
11009 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11010 survey->channel_time =
11011 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11012 survey->filled |= SURVEY_HAS_CHAN_TIME;
11013 }
11014
11015 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11016 survey->channel_time_busy =
11017 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11018 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11019 }
11020
11021 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11022 survey->channel_time_rx =
11023 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11024 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11025 }
11026
11027 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11028 survey->channel_time_tx =
11029 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11030 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11031 }
11032
11033 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)",
11034 survey->freq,
11035 survey->nf,
11036 (unsigned long int) survey->channel_time,
11037 (unsigned long int) survey->channel_time_busy,
11038 (unsigned long int) survey->channel_time_tx,
11039 (unsigned long int) survey->channel_time_rx,
11040 survey->filled);
11041
11042 dl_list_add_tail(survey_list, &survey->list);
11043}
11044
11045
11046static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11047 unsigned int freq_filter)
11048{
11049 if (!freq_filter)
11050 return 1;
11051
11052 return freq_filter == surveyed_freq;
11053}
11054
11055
11056static int survey_handler(struct nl_msg *msg, void *arg)
11057{
11058 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11059 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11060 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11061 struct survey_results *survey_results;
11062 u32 surveyed_freq = 0;
11063 u32 ifidx;
11064
11065 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11066 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11067 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11068 };
11069
11070 survey_results = (struct survey_results *) arg;
11071
11072 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11073 genlmsg_attrlen(gnlh, 0), NULL);
11074
Dmitry Shmidt97672262014-02-03 13:02:54 -080011075 if (!tb[NL80211_ATTR_IFINDEX])
11076 return NL_SKIP;
11077
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011078 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11079
11080 if (!tb[NL80211_ATTR_SURVEY_INFO])
11081 return NL_SKIP;
11082
11083 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11084 tb[NL80211_ATTR_SURVEY_INFO],
11085 survey_policy))
11086 return NL_SKIP;
11087
11088 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11089 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11090 return NL_SKIP;
11091 }
11092
11093 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11094
11095 if (!check_survey_ok(sinfo, surveyed_freq,
11096 survey_results->freq_filter))
11097 return NL_SKIP;
11098
11099 if (survey_results->freq_filter &&
11100 survey_results->freq_filter != surveyed_freq) {
11101 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11102 surveyed_freq);
11103 return NL_SKIP;
11104 }
11105
11106 add_survey(sinfo, ifidx, &survey_results->survey_list);
11107
11108 return NL_SKIP;
11109}
11110
11111
11112static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11113{
11114 struct i802_bss *bss = priv;
11115 struct wpa_driver_nl80211_data *drv = bss->drv;
11116 struct nl_msg *msg;
11117 int err = -ENOBUFS;
11118 union wpa_event_data data;
11119 struct survey_results *survey_results;
11120
11121 os_memset(&data, 0, sizeof(data));
11122 survey_results = &data.survey_results;
11123
11124 dl_list_init(&survey_results->survey_list);
11125
11126 msg = nlmsg_alloc();
11127 if (!msg)
11128 goto nla_put_failure;
11129
11130 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11131
11132 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11133
11134 if (freq)
11135 data.survey_results.freq_filter = freq;
11136
11137 do {
11138 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11139 err = send_and_recv_msgs(drv, msg, survey_handler,
11140 survey_results);
11141 } while (err > 0);
11142
11143 if (err) {
11144 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11145 goto out_clean;
11146 }
11147
11148 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11149
11150out_clean:
11151 clean_survey_results(survey_results);
11152nla_put_failure:
11153 return err;
11154}
11155
11156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011157static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11158 const u8 *replay_ctr)
11159{
11160 struct i802_bss *bss = priv;
11161 struct wpa_driver_nl80211_data *drv = bss->drv;
11162 struct nlattr *replay_nested;
11163 struct nl_msg *msg;
11164
11165 msg = nlmsg_alloc();
11166 if (!msg)
11167 return;
11168
11169 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11170
11171 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11172
11173 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11174 if (!replay_nested)
11175 goto nla_put_failure;
11176
11177 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11178 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11179 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11180 replay_ctr);
11181
11182 nla_nest_end(msg, replay_nested);
11183
11184 send_and_recv_msgs(drv, msg, NULL, NULL);
11185 return;
11186 nla_put_failure:
11187 nlmsg_free(msg);
11188}
11189
11190
11191static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11192 const u8 *addr, int qos)
11193{
11194 /* send data frame to poll STA and check whether
11195 * this frame is ACKed */
11196 struct {
11197 struct ieee80211_hdr hdr;
11198 u16 qos_ctl;
11199 } STRUCT_PACKED nulldata;
11200 size_t size;
11201
11202 /* Send data frame to poll STA and check whether this frame is ACKed */
11203
11204 os_memset(&nulldata, 0, sizeof(nulldata));
11205
11206 if (qos) {
11207 nulldata.hdr.frame_control =
11208 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11209 WLAN_FC_STYPE_QOS_NULL);
11210 size = sizeof(nulldata);
11211 } else {
11212 nulldata.hdr.frame_control =
11213 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11214 WLAN_FC_STYPE_NULLFUNC);
11215 size = sizeof(struct ieee80211_hdr);
11216 }
11217
11218 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11219 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11220 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11221 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11222
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011223 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11224 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011225 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11226 "send poll frame");
11227}
11228
11229static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11230 int qos)
11231{
11232 struct i802_bss *bss = priv;
11233 struct wpa_driver_nl80211_data *drv = bss->drv;
11234 struct nl_msg *msg;
11235
11236 if (!drv->poll_command_supported) {
11237 nl80211_send_null_frame(bss, own_addr, addr, qos);
11238 return;
11239 }
11240
11241 msg = nlmsg_alloc();
11242 if (!msg)
11243 return;
11244
11245 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11246
11247 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11248 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11249
11250 send_and_recv_msgs(drv, msg, NULL, NULL);
11251 return;
11252 nla_put_failure:
11253 nlmsg_free(msg);
11254}
11255
11256
11257static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11258{
11259 struct nl_msg *msg;
11260
11261 msg = nlmsg_alloc();
11262 if (!msg)
11263 return -ENOMEM;
11264
11265 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11266 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11267 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11268 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11269 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11270nla_put_failure:
11271 nlmsg_free(msg);
11272 return -ENOBUFS;
11273}
11274
11275
11276static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11277 int ctwindow)
11278{
11279 struct i802_bss *bss = priv;
11280
11281 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11282 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11283
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011284 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011285#ifdef ANDROID_P2P
11286 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011287#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011288 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011289#endif /* ANDROID_P2P */
11290 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011291
11292 if (legacy_ps == -1)
11293 return 0;
11294 if (legacy_ps != 0 && legacy_ps != 1)
11295 return -1; /* Not yet supported */
11296
11297 return nl80211_set_power_save(bss, legacy_ps);
11298}
11299
11300
Dmitry Shmidt051af732013-10-22 13:52:46 -070011301static int nl80211_start_radar_detection(void *priv,
11302 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011303{
11304 struct i802_bss *bss = priv;
11305 struct wpa_driver_nl80211_data *drv = bss->drv;
11306 struct nl_msg *msg;
11307 int ret;
11308
Dmitry Shmidt051af732013-10-22 13:52:46 -070011309 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)",
11310 freq->freq, freq->ht_enabled, freq->vht_enabled,
11311 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11312
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011313 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11314 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11315 "detection");
11316 return -1;
11317 }
11318
11319 msg = nlmsg_alloc();
11320 if (!msg)
11321 return -1;
11322
11323 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11324 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidt051af732013-10-22 13:52:46 -070011325 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011326
Dmitry Shmidt051af732013-10-22 13:52:46 -070011327 if (freq->vht_enabled) {
11328 switch (freq->bandwidth) {
11329 case 20:
11330 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11331 NL80211_CHAN_WIDTH_20);
11332 break;
11333 case 40:
11334 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11335 NL80211_CHAN_WIDTH_40);
11336 break;
11337 case 80:
11338 if (freq->center_freq2)
11339 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11340 NL80211_CHAN_WIDTH_80P80);
11341 else
11342 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11343 NL80211_CHAN_WIDTH_80);
11344 break;
11345 case 160:
11346 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
11347 NL80211_CHAN_WIDTH_160);
11348 break;
11349 default:
11350 return -1;
11351 }
11352 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
11353 if (freq->center_freq2)
11354 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
11355 freq->center_freq2);
11356 } else if (freq->ht_enabled) {
11357 switch (freq->sec_channel_offset) {
11358 case -1:
11359 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11360 NL80211_CHAN_HT40MINUS);
11361 break;
11362 case 1:
11363 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11364 NL80211_CHAN_HT40PLUS);
11365 break;
11366 default:
11367 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11368 NL80211_CHAN_HT20);
11369 break;
11370 }
11371 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011372
11373 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11374 if (ret == 0)
11375 return 0;
11376 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11377 "%d (%s)", ret, strerror(-ret));
11378nla_put_failure:
11379 return -1;
11380}
11381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011382#ifdef CONFIG_TDLS
11383
11384static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11385 u8 dialog_token, u16 status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011386 u32 peer_capab, const u8 *buf, size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011387{
11388 struct i802_bss *bss = priv;
11389 struct wpa_driver_nl80211_data *drv = bss->drv;
11390 struct nl_msg *msg;
11391
11392 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11393 return -EOPNOTSUPP;
11394
11395 if (!dst)
11396 return -EINVAL;
11397
11398 msg = nlmsg_alloc();
11399 if (!msg)
11400 return -ENOMEM;
11401
11402 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11403 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11404 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11405 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11406 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11407 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011408 if (peer_capab) {
11409 /*
11410 * The internal enum tdls_peer_capability definition is
11411 * currently identical with the nl80211 enum
11412 * nl80211_tdls_peer_capability, so no conversion is needed
11413 * here.
11414 */
11415 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11416 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011417 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11418
11419 return send_and_recv_msgs(drv, msg, NULL, NULL);
11420
11421nla_put_failure:
11422 nlmsg_free(msg);
11423 return -ENOBUFS;
11424}
11425
11426
11427static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11428{
11429 struct i802_bss *bss = priv;
11430 struct wpa_driver_nl80211_data *drv = bss->drv;
11431 struct nl_msg *msg;
11432 enum nl80211_tdls_operation nl80211_oper;
11433
11434 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11435 return -EOPNOTSUPP;
11436
11437 switch (oper) {
11438 case TDLS_DISCOVERY_REQ:
11439 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11440 break;
11441 case TDLS_SETUP:
11442 nl80211_oper = NL80211_TDLS_SETUP;
11443 break;
11444 case TDLS_TEARDOWN:
11445 nl80211_oper = NL80211_TDLS_TEARDOWN;
11446 break;
11447 case TDLS_ENABLE_LINK:
11448 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11449 break;
11450 case TDLS_DISABLE_LINK:
11451 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11452 break;
11453 case TDLS_ENABLE:
11454 return 0;
11455 case TDLS_DISABLE:
11456 return 0;
11457 default:
11458 return -EINVAL;
11459 }
11460
11461 msg = nlmsg_alloc();
11462 if (!msg)
11463 return -ENOMEM;
11464
11465 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11466 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11467 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11468 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11469
11470 return send_and_recv_msgs(drv, msg, NULL, NULL);
11471
11472nla_put_failure:
11473 nlmsg_free(msg);
11474 return -ENOBUFS;
11475}
11476
11477#endif /* CONFIG TDLS */
11478
11479
11480#ifdef ANDROID
11481
11482typedef struct android_wifi_priv_cmd {
11483 char *buf;
11484 int used_len;
11485 int total_len;
11486} android_wifi_priv_cmd;
11487
11488static int drv_errors = 0;
11489
11490static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11491{
11492 drv_errors++;
11493 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11494 drv_errors = 0;
11495 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11496 }
11497}
11498
11499
11500static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11501{
11502 struct wpa_driver_nl80211_data *drv = bss->drv;
11503 struct ifreq ifr;
11504 android_wifi_priv_cmd priv_cmd;
11505 char buf[MAX_DRV_CMD_SIZE];
11506 int ret;
11507
11508 os_memset(&ifr, 0, sizeof(ifr));
11509 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11510 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11511
11512 os_memset(buf, 0, sizeof(buf));
11513 os_strlcpy(buf, cmd, sizeof(buf));
11514
11515 priv_cmd.buf = buf;
11516 priv_cmd.used_len = sizeof(buf);
11517 priv_cmd.total_len = sizeof(buf);
11518 ifr.ifr_data = &priv_cmd;
11519
11520 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11521 if (ret < 0) {
11522 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11523 __func__);
11524 wpa_driver_send_hang_msg(drv);
11525 return ret;
11526 }
11527
11528 drv_errors = 0;
11529 return 0;
11530}
11531
11532
11533static int android_pno_start(struct i802_bss *bss,
11534 struct wpa_driver_scan_params *params)
11535{
11536 struct wpa_driver_nl80211_data *drv = bss->drv;
11537 struct ifreq ifr;
11538 android_wifi_priv_cmd priv_cmd;
11539 int ret = 0, i = 0, bp;
11540 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11541
11542 bp = WEXT_PNOSETUP_HEADER_SIZE;
11543 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11544 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11545 buf[bp++] = WEXT_PNO_TLV_VERSION;
11546 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11547 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11548
11549 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11550 /* Check that there is enough space needed for 1 more SSID, the
11551 * other sections and null termination */
11552 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11553 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11554 break;
11555 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11556 params->ssids[i].ssid,
11557 params->ssids[i].ssid_len);
11558 buf[bp++] = WEXT_PNO_SSID_SECTION;
11559 buf[bp++] = params->ssids[i].ssid_len;
11560 os_memcpy(&buf[bp], params->ssids[i].ssid,
11561 params->ssids[i].ssid_len);
11562 bp += params->ssids[i].ssid_len;
11563 i++;
11564 }
11565
11566 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11567 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11568 WEXT_PNO_SCAN_INTERVAL);
11569 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11570
11571 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11572 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11573 WEXT_PNO_REPEAT);
11574 bp += WEXT_PNO_REPEAT_LENGTH;
11575
11576 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11577 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11578 WEXT_PNO_MAX_REPEAT);
11579 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11580
11581 memset(&ifr, 0, sizeof(ifr));
11582 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011583 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011584
11585 priv_cmd.buf = buf;
11586 priv_cmd.used_len = bp;
11587 priv_cmd.total_len = bp;
11588 ifr.ifr_data = &priv_cmd;
11589
11590 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11591
11592 if (ret < 0) {
11593 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11594 ret);
11595 wpa_driver_send_hang_msg(drv);
11596 return ret;
11597 }
11598
11599 drv_errors = 0;
11600
11601 return android_priv_cmd(bss, "PNOFORCE 1");
11602}
11603
11604
11605static int android_pno_stop(struct i802_bss *bss)
11606{
11607 return android_priv_cmd(bss, "PNOFORCE 0");
11608}
11609
11610#endif /* ANDROID */
11611
11612
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011613static int driver_nl80211_set_key(const char *ifname, void *priv,
11614 enum wpa_alg alg, const u8 *addr,
11615 int key_idx, int set_tx,
11616 const u8 *seq, size_t seq_len,
11617 const u8 *key, size_t key_len)
11618{
11619 struct i802_bss *bss = priv;
11620 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11621 set_tx, seq, seq_len, key, key_len);
11622}
11623
11624
11625static int driver_nl80211_scan2(void *priv,
11626 struct wpa_driver_scan_params *params)
11627{
11628 struct i802_bss *bss = priv;
11629 return wpa_driver_nl80211_scan(bss, params);
11630}
11631
11632
11633static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11634 int reason_code)
11635{
11636 struct i802_bss *bss = priv;
11637 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11638}
11639
11640
11641static int driver_nl80211_authenticate(void *priv,
11642 struct wpa_driver_auth_params *params)
11643{
11644 struct i802_bss *bss = priv;
11645 return wpa_driver_nl80211_authenticate(bss, params);
11646}
11647
11648
11649static void driver_nl80211_deinit(void *priv)
11650{
11651 struct i802_bss *bss = priv;
11652 wpa_driver_nl80211_deinit(bss);
11653}
11654
11655
11656static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11657 const char *ifname)
11658{
11659 struct i802_bss *bss = priv;
11660 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11661}
11662
11663
11664static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11665 size_t data_len, int noack)
11666{
11667 struct i802_bss *bss = priv;
11668 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11669 0, 0, 0, 0);
11670}
11671
11672
11673static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11674{
11675 struct i802_bss *bss = priv;
11676 return wpa_driver_nl80211_sta_remove(bss, addr);
11677}
11678
11679
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011680static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11681 const char *ifname, int vlan_id)
11682{
11683 struct i802_bss *bss = priv;
11684 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11685}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011686
11687
11688static int driver_nl80211_read_sta_data(void *priv,
11689 struct hostap_sta_driver_data *data,
11690 const u8 *addr)
11691{
11692 struct i802_bss *bss = priv;
11693 return i802_read_sta_data(bss, data, addr);
11694}
11695
11696
11697static int driver_nl80211_send_action(void *priv, unsigned int freq,
11698 unsigned int wait_time,
11699 const u8 *dst, const u8 *src,
11700 const u8 *bssid,
11701 const u8 *data, size_t data_len,
11702 int no_cck)
11703{
11704 struct i802_bss *bss = priv;
11705 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11706 bssid, data, data_len, no_cck);
11707}
11708
11709
11710static int driver_nl80211_probe_req_report(void *priv, int report)
11711{
11712 struct i802_bss *bss = priv;
11713 return wpa_driver_nl80211_probe_req_report(bss, report);
11714}
11715
11716
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011717static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11718 const u8 *ies, size_t ies_len)
11719{
11720 int ret;
11721 struct nl_msg *msg;
11722 struct i802_bss *bss = priv;
11723 struct wpa_driver_nl80211_data *drv = bss->drv;
11724 u16 mdid = WPA_GET_LE16(md);
11725
11726 msg = nlmsg_alloc();
11727 if (!msg)
11728 return -ENOMEM;
11729
11730 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11731 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11732 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11733 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11734 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11735
11736 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11737 if (ret) {
11738 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11739 "err=%d (%s)", ret, strerror(-ret));
11740 }
11741
11742 return ret;
11743
11744nla_put_failure:
11745 nlmsg_free(msg);
11746 return -ENOBUFS;
11747}
11748
11749
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011750const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11751{
11752 struct i802_bss *bss = priv;
11753 struct wpa_driver_nl80211_data *drv = bss->drv;
11754
11755 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11756 return NULL;
11757
11758 return bss->addr;
11759}
11760
11761
Dmitry Shmidt56052862013-10-04 10:23:25 -070011762static const char * scan_state_str(enum scan_states scan_state)
11763{
11764 switch (scan_state) {
11765 case NO_SCAN:
11766 return "NO_SCAN";
11767 case SCAN_REQUESTED:
11768 return "SCAN_REQUESTED";
11769 case SCAN_STARTED:
11770 return "SCAN_STARTED";
11771 case SCAN_COMPLETED:
11772 return "SCAN_COMPLETED";
11773 case SCAN_ABORTED:
11774 return "SCAN_ABORTED";
11775 case SCHED_SCAN_STARTED:
11776 return "SCHED_SCAN_STARTED";
11777 case SCHED_SCAN_STOPPED:
11778 return "SCHED_SCAN_STOPPED";
11779 case SCHED_SCAN_RESULTS:
11780 return "SCHED_SCAN_RESULTS";
11781 }
11782
11783 return "??";
11784}
11785
11786
11787static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
11788{
11789 struct i802_bss *bss = priv;
11790 struct wpa_driver_nl80211_data *drv = bss->drv;
11791 int res;
11792 char *pos, *end;
11793
11794 pos = buf;
11795 end = buf + buflen;
11796
11797 res = os_snprintf(pos, end - pos,
11798 "ifindex=%d\n"
11799 "ifname=%s\n"
11800 "brname=%s\n"
11801 "addr=" MACSTR "\n"
11802 "freq=%d\n"
11803 "%s%s%s%s%s",
11804 bss->ifindex,
11805 bss->ifname,
11806 bss->brname,
11807 MAC2STR(bss->addr),
11808 bss->freq,
11809 bss->beacon_set ? "beacon_set=1\n" : "",
11810 bss->added_if_into_bridge ?
11811 "added_if_into_bridge=1\n" : "",
11812 bss->added_bridge ? "added_bridge=1\n" : "",
11813 bss->in_deinit ? "in_deinit=1\n" : "",
11814 bss->if_dynamic ? "if_dynamic=1\n" : "");
11815 if (res < 0 || res >= end - pos)
11816 return pos - buf;
11817 pos += res;
11818
11819 if (bss->wdev_id_set) {
11820 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
11821 (unsigned long long) bss->wdev_id);
11822 if (res < 0 || res >= end - pos)
11823 return pos - buf;
11824 pos += res;
11825 }
11826
11827 res = os_snprintf(pos, end - pos,
11828 "phyname=%s\n"
11829 "drv_ifindex=%d\n"
11830 "operstate=%d\n"
11831 "scan_state=%s\n"
11832 "auth_bssid=" MACSTR "\n"
11833 "auth_attempt_bssid=" MACSTR "\n"
11834 "bssid=" MACSTR "\n"
11835 "prev_bssid=" MACSTR "\n"
11836 "associated=%d\n"
11837 "assoc_freq=%u\n"
11838 "monitor_sock=%d\n"
11839 "monitor_ifidx=%d\n"
11840 "monitor_refcount=%d\n"
11841 "last_mgmt_freq=%u\n"
11842 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011843 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011844 drv->phyname,
11845 drv->ifindex,
11846 drv->operstate,
11847 scan_state_str(drv->scan_state),
11848 MAC2STR(drv->auth_bssid),
11849 MAC2STR(drv->auth_attempt_bssid),
11850 MAC2STR(drv->bssid),
11851 MAC2STR(drv->prev_bssid),
11852 drv->associated,
11853 drv->assoc_freq,
11854 drv->monitor_sock,
11855 drv->monitor_ifidx,
11856 drv->monitor_refcount,
11857 drv->last_mgmt_freq,
11858 drv->eapol_tx_sock,
11859 drv->ignore_if_down_event ?
11860 "ignore_if_down_event=1\n" : "",
11861 drv->scan_complete_events ?
11862 "scan_complete_events=1\n" : "",
11863 drv->disabled_11b_rates ?
11864 "disabled_11b_rates=1\n" : "",
11865 drv->pending_remain_on_chan ?
11866 "pending_remain_on_chan=1\n" : "",
11867 drv->in_interface_list ? "in_interface_list=1\n" : "",
11868 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
11869 drv->poll_command_supported ?
11870 "poll_command_supported=1\n" : "",
11871 drv->data_tx_status ? "data_tx_status=1\n" : "",
11872 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
11873 drv->retry_auth ? "retry_auth=1\n" : "",
11874 drv->use_monitor ? "use_monitor=1\n" : "",
11875 drv->ignore_next_local_disconnect ?
11876 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070011877 drv->ignore_next_local_deauth ?
11878 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070011879 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
11880 if (res < 0 || res >= end - pos)
11881 return pos - buf;
11882 pos += res;
11883
11884 if (drv->has_capability) {
11885 res = os_snprintf(pos, end - pos,
11886 "capa.key_mgmt=0x%x\n"
11887 "capa.enc=0x%x\n"
11888 "capa.auth=0x%x\n"
11889 "capa.flags=0x%x\n"
11890 "capa.max_scan_ssids=%d\n"
11891 "capa.max_sched_scan_ssids=%d\n"
11892 "capa.sched_scan_supported=%d\n"
11893 "capa.max_match_sets=%d\n"
11894 "capa.max_remain_on_chan=%u\n"
11895 "capa.max_stations=%u\n"
11896 "capa.probe_resp_offloads=0x%x\n"
11897 "capa.max_acl_mac_addrs=%u\n"
11898 "capa.num_multichan_concurrent=%u\n",
11899 drv->capa.key_mgmt,
11900 drv->capa.enc,
11901 drv->capa.auth,
11902 drv->capa.flags,
11903 drv->capa.max_scan_ssids,
11904 drv->capa.max_sched_scan_ssids,
11905 drv->capa.sched_scan_supported,
11906 drv->capa.max_match_sets,
11907 drv->capa.max_remain_on_chan,
11908 drv->capa.max_stations,
11909 drv->capa.probe_resp_offloads,
11910 drv->capa.max_acl_mac_addrs,
11911 drv->capa.num_multichan_concurrent);
11912 if (res < 0 || res >= end - pos)
11913 return pos - buf;
11914 pos += res;
11915 }
11916
11917 return pos - buf;
11918}
11919
11920
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011921static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
11922{
11923 if (settings->head)
11924 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
11925 settings->head_len, settings->head);
11926
11927 if (settings->tail)
11928 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
11929 settings->tail_len, settings->tail);
11930
11931 if (settings->beacon_ies)
11932 NLA_PUT(msg, NL80211_ATTR_IE,
11933 settings->beacon_ies_len, settings->beacon_ies);
11934
11935 if (settings->proberesp_ies)
11936 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
11937 settings->proberesp_ies_len, settings->proberesp_ies);
11938
11939 if (settings->assocresp_ies)
11940 NLA_PUT(msg,
11941 NL80211_ATTR_IE_ASSOC_RESP,
11942 settings->assocresp_ies_len, settings->assocresp_ies);
11943
11944 if (settings->probe_resp)
11945 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
11946 settings->probe_resp_len, settings->probe_resp);
11947
11948 return 0;
11949
11950nla_put_failure:
11951 return -ENOBUFS;
11952}
11953
11954
11955static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
11956{
11957 struct nl_msg *msg;
11958 struct i802_bss *bss = priv;
11959 struct wpa_driver_nl80211_data *drv = bss->drv;
11960 struct nlattr *beacon_csa;
11961 int ret = -ENOBUFS;
11962
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011963 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 -080011964 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080011965 settings->freq_params.freq, settings->freq_params.bandwidth,
11966 settings->freq_params.center_freq1,
11967 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011968
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011969 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080011970 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
11971 return -EOPNOTSUPP;
11972 }
11973
11974 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
11975 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
11976 return -EOPNOTSUPP;
11977
11978 /* check settings validity */
11979 if (!settings->beacon_csa.tail ||
11980 ((settings->beacon_csa.tail_len <=
11981 settings->counter_offset_beacon) ||
11982 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
11983 settings->cs_count)))
11984 return -EINVAL;
11985
11986 if (settings->beacon_csa.probe_resp &&
11987 ((settings->beacon_csa.probe_resp_len <=
11988 settings->counter_offset_presp) ||
11989 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
11990 settings->cs_count)))
11991 return -EINVAL;
11992
11993 msg = nlmsg_alloc();
11994 if (!msg)
11995 return -ENOMEM;
11996
11997 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
11998 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11999 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
12000 ret = nl80211_put_freq_params(msg, &settings->freq_params);
12001 if (ret)
12002 goto error;
12003
12004 if (settings->block_tx)
12005 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12006
12007 /* beacon_after params */
12008 ret = set_beacon_data(msg, &settings->beacon_after);
12009 if (ret)
12010 goto error;
12011
12012 /* beacon_csa params */
12013 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12014 if (!beacon_csa)
12015 goto nla_put_failure;
12016
12017 ret = set_beacon_data(msg, &settings->beacon_csa);
12018 if (ret)
12019 goto error;
12020
12021 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12022 settings->counter_offset_beacon);
12023
12024 if (settings->beacon_csa.probe_resp)
12025 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12026 settings->counter_offset_presp);
12027
12028 nla_nest_end(msg, beacon_csa);
12029 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12030 if (ret) {
12031 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12032 ret, strerror(-ret));
12033 }
12034 return ret;
12035
12036nla_put_failure:
12037 ret = -ENOBUFS;
12038error:
12039 nlmsg_free(msg);
12040 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12041 return ret;
12042}
12043
12044
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012045#ifdef CONFIG_TESTING_OPTIONS
12046static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12047{
12048 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12049 struct wpabuf *buf = arg;
12050
12051 if (!buf)
12052 return NL_SKIP;
12053
12054 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12055 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12056 return NL_SKIP;
12057 }
12058
12059 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12060 genlmsg_attrlen(gnlh, 0));
12061
12062 return NL_SKIP;
12063}
12064#endif /* CONFIG_TESTING_OPTIONS */
12065
12066
12067static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12068{
12069 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12070 struct nlattr *nl_vendor_reply, *nl;
12071 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12072 struct wpabuf *buf = arg;
12073 int rem;
12074
12075 if (!buf)
12076 return NL_SKIP;
12077
12078 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12079 genlmsg_attrlen(gnlh, 0), NULL);
12080 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12081
12082 if (!nl_vendor_reply)
12083 return NL_SKIP;
12084
12085 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12086 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12087 return NL_SKIP;
12088 }
12089
12090 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12091 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12092 }
12093
12094 return NL_SKIP;
12095}
12096
12097
12098static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12099 unsigned int subcmd, const u8 *data,
12100 size_t data_len, struct wpabuf *buf)
12101{
12102 struct i802_bss *bss = priv;
12103 struct wpa_driver_nl80211_data *drv = bss->drv;
12104 struct nl_msg *msg;
12105 int ret;
12106
12107 msg = nlmsg_alloc();
12108 if (!msg)
12109 return -ENOMEM;
12110
12111#ifdef CONFIG_TESTING_OPTIONS
12112 if (vendor_id == 0xffffffff) {
12113 nl80211_cmd(drv, msg, 0, subcmd);
12114 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12115 0)
12116 goto nla_put_failure;
12117 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12118 if (ret)
12119 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12120 ret);
12121 return ret;
12122 }
12123#endif /* CONFIG_TESTING_OPTIONS */
12124
12125 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12126 if (nl80211_set_iface_id(msg, bss) < 0)
12127 goto nla_put_failure;
12128 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12129 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12130 if (data)
12131 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12132
12133 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12134 if (ret)
12135 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12136 ret);
12137 return ret;
12138
12139nla_put_failure:
12140 nlmsg_free(msg);
12141 return -ENOBUFS;
12142}
12143
12144
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012145static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12146 u8 qos_map_set_len)
12147{
12148 struct i802_bss *bss = priv;
12149 struct wpa_driver_nl80211_data *drv = bss->drv;
12150 struct nl_msg *msg;
12151 int ret;
12152
12153 msg = nlmsg_alloc();
12154 if (!msg)
12155 return -ENOMEM;
12156
12157 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12158 qos_map_set, qos_map_set_len);
12159
12160 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12161 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12162 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12163
12164 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12165 if (ret)
12166 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12167
12168 return ret;
12169
12170nla_put_failure:
12171 nlmsg_free(msg);
12172 return -ENOBUFS;
12173}
12174
12175
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012176static int nl80211_set_wowlan(void *priv,
12177 const struct wowlan_triggers *triggers)
12178{
12179 struct i802_bss *bss = priv;
12180 struct wpa_driver_nl80211_data *drv = bss->drv;
12181 struct nl_msg *msg;
12182 struct nlattr *wowlan_triggers;
12183 int ret;
12184
12185 msg = nlmsg_alloc();
12186 if (!msg)
12187 return -ENOMEM;
12188
12189 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12190
12191 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12192 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12193
12194 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12195 if (!wowlan_triggers)
12196 goto nla_put_failure;
12197
12198 if (triggers->any)
12199 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12200 if (triggers->disconnect)
12201 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12202 if (triggers->magic_pkt)
12203 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12204 if (triggers->gtk_rekey_failure)
12205 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12206 if (triggers->eap_identity_req)
12207 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12208 if (triggers->four_way_handshake)
12209 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12210 if (triggers->rfkill_release)
12211 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12212
12213 nla_nest_end(msg, wowlan_triggers);
12214
12215 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12216 if (ret)
12217 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12218
12219 return ret;
12220
12221nla_put_failure:
12222 nlmsg_free(msg);
12223 return -ENOBUFS;
12224}
12225
12226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012227const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12228 .name = "nl80211",
12229 .desc = "Linux nl80211/cfg80211",
12230 .get_bssid = wpa_driver_nl80211_get_bssid,
12231 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012232 .set_key = driver_nl80211_set_key,
12233 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012234 .sched_scan = wpa_driver_nl80211_sched_scan,
12235 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012236 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012237 .deauthenticate = driver_nl80211_deauthenticate,
12238 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012239 .associate = wpa_driver_nl80211_associate,
12240 .global_init = nl80211_global_init,
12241 .global_deinit = nl80211_global_deinit,
12242 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012243 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012244 .get_capa = wpa_driver_nl80211_get_capa,
12245 .set_operstate = wpa_driver_nl80211_set_operstate,
12246 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12247 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012248 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012249 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012250 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012251 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012252 .if_remove = driver_nl80211_if_remove,
12253 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012254 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12255 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012256 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012257 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12258 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012259 .hapd_init = i802_init,
12260 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012261 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012262 .get_seqnum = i802_get_seqnum,
12263 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012264 .get_inact_sec = i802_get_inact_sec,
12265 .sta_clear_stats = i802_sta_clear_stats,
12266 .set_rts = i802_set_rts,
12267 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012268 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012269 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012270 .sta_deauth = i802_sta_deauth,
12271 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012272 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012273 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012274 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012275 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12276 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12277 .cancel_remain_on_channel =
12278 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012279 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012280 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012281 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012282 .resume = wpa_driver_nl80211_resume,
12283 .send_ft_action = nl80211_send_ft_action,
12284 .signal_monitor = nl80211_signal_monitor,
12285 .signal_poll = nl80211_signal_poll,
12286 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012287 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012288 .set_param = nl80211_set_param,
12289 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012290 .add_pmkid = nl80211_add_pmkid,
12291 .remove_pmkid = nl80211_remove_pmkid,
12292 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012293 .set_rekey_info = nl80211_set_rekey_info,
12294 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012295 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012296 .start_dfs_cac = nl80211_start_radar_detection,
12297 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012298#ifdef CONFIG_TDLS
12299 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12300 .tdls_oper = nl80211_tdls_oper,
12301#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012302 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012303 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012304 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012305 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012306 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012307#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012308 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012309 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012310 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012311#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012312#ifdef ANDROID
12313 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012314#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012315 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012316 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012317 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012318};