blob: 5c922a0576f654df812643a463ac55d3bd024b5b [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>
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070022#ifdef CONFIG_LIBNL3_ROUTE
23#include <netlink/route/neighbour.h>
24#endif /* CONFIG_LIBNL3_ROUTE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include <linux/rtnetlink.h>
26#include <netpacket/packet.h>
27#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080028#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "nl80211_copy.h"
30
31#include "common.h"
32#include "eloop.h"
33#include "utils/list.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080034#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070035#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080037#include "common/ieee802_11_common.h"
38#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "netlink.h"
40#include "linux_ioctl.h"
41#include "radiotap.h"
42#include "radiotap_iter.h"
43#include "rfkill.h"
44#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046#ifndef SO_WIFI_STATUS
47# if defined(__sparc__)
48# define SO_WIFI_STATUS 0x0025
49# elif defined(__parisc__)
50# define SO_WIFI_STATUS 0x4022
51# else
52# define SO_WIFI_STATUS 41
53# endif
54
55# define SCM_WIFI_STATUS SO_WIFI_STATUS
56#endif
57
58#ifndef SO_EE_ORIGIN_TXSTATUS
59#define SO_EE_ORIGIN_TXSTATUS 4
60#endif
61
62#ifndef PACKET_TX_TIMESTAMP
63#define PACKET_TX_TIMESTAMP 16
64#endif
65
66#ifdef ANDROID
67#include "android_drv.h"
68#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069#ifdef CONFIG_LIBNL20
70/* libnl 2.0 compatibility code */
71#define nl_handle nl_sock
72#define nl80211_handle_alloc nl_socket_alloc_cb
73#define nl80211_handle_destroy nl_socket_free
74#else
75/*
76 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
77 * but when you free a socket again it will mess up its bitmap and
78 * and use the wrong number the next time it needs a socket ID.
79 * Therefore, we wrap the handle alloc/destroy and add our own pid
80 * accounting.
81 */
82static uint32_t port_bitmap[32] = { 0 };
83
84static struct nl_handle *nl80211_handle_alloc(void *cb)
85{
86 struct nl_handle *handle;
87 uint32_t pid = getpid() & 0x3FFFFF;
88 int i;
89
90 handle = nl_handle_alloc_cb(cb);
91
92 for (i = 0; i < 1024; i++) {
93 if (port_bitmap[i / 32] & (1 << (i % 32)))
94 continue;
95 port_bitmap[i / 32] |= 1 << (i % 32);
96 pid += i << 22;
97 break;
98 }
99
100 nl_socket_set_local_port(handle, pid);
101
102 return handle;
103}
104
105static void nl80211_handle_destroy(struct nl_handle *handle)
106{
107 uint32_t port = nl_socket_get_local_port(handle);
108
109 port >>= 22;
110 port_bitmap[port / 32] &= ~(1 << (port % 32));
111
112 nl_handle_destroy(handle);
113}
114#endif /* CONFIG_LIBNL20 */
115
116
Dmitry Shmidt54605472013-11-08 11:10:19 -0800117#ifdef ANDROID
118/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
119static int android_nl_socket_set_nonblocking(struct nl_handle *handle)
120{
121 return fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
122}
123#undef nl_socket_set_nonblocking
124#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
125#endif /* ANDROID */
126
127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800128static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
129{
130 struct nl_handle *handle;
131
132 handle = nl80211_handle_alloc(cb);
133 if (handle == NULL) {
134 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
135 "callbacks (%s)", dbg);
136 return NULL;
137 }
138
139 if (genl_connect(handle)) {
140 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
141 "netlink (%s)", dbg);
142 nl80211_handle_destroy(handle);
143 return NULL;
144 }
145
146 return handle;
147}
148
149
150static void nl_destroy_handles(struct nl_handle **handle)
151{
152 if (*handle == NULL)
153 return;
154 nl80211_handle_destroy(*handle);
155 *handle = NULL;
156}
157
158
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700159#if __WORDSIZE == 64
160#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
161#else
162#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
163#endif
164
165static void nl80211_register_eloop_read(struct nl_handle **handle,
166 eloop_sock_handler handler,
167 void *eloop_data)
168{
169 nl_socket_set_nonblocking(*handle);
170 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
171 eloop_data, *handle);
172 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
173}
174
175
176static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
177{
178 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
179 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
180 nl_destroy_handles(handle);
181}
182
183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184#ifndef IFF_LOWER_UP
185#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
186#endif
187#ifndef IFF_DORMANT
188#define IFF_DORMANT 0x20000 /* driver signals dormant */
189#endif
190
191#ifndef IF_OPER_DORMANT
192#define IF_OPER_DORMANT 5
193#endif
194#ifndef IF_OPER_UP
195#define IF_OPER_UP 6
196#endif
197
198struct nl80211_global {
199 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200 int if_add_ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700201 u64 if_add_wdevid;
202 int if_add_wdevid_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 struct netlink_data *netlink;
204 struct nl_cb *nl_cb;
205 struct nl_handle *nl;
206 int nl80211_id;
207 int ioctl_sock; /* socket for ioctl() use */
208
209 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210};
211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800212struct nl80211_wiphy_data {
213 struct dl_list list;
214 struct dl_list bsss;
215 struct dl_list drvs;
216
217 struct nl_handle *nl_beacons;
218 struct nl_cb *nl_cb;
219
220 int wiphy_idx;
221};
222
223static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225struct i802_bss {
226 struct wpa_driver_nl80211_data *drv;
227 struct i802_bss *next;
228 int ifindex;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700229 u64 wdev_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 char ifname[IFNAMSIZ + 1];
231 char brname[IFNAMSIZ];
232 unsigned int beacon_set:1;
233 unsigned int added_if_into_bridge:1;
234 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700235 unsigned int in_deinit:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700236 unsigned int wdev_id_set:1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800237 unsigned int added_if:1;
Dmitry Shmidt03658832014-08-13 11:03:49 -0700238 unsigned int static_ap:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239
240 u8 addr[ETH_ALEN];
241
242 int freq;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700243 int bandwidth;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700244 int if_dynamic;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800246 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 struct nl_handle *nl_preq, *nl_mgmt;
248 struct nl_cb *nl_cb;
249
250 struct nl80211_wiphy_data *wiphy_data;
251 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252};
253
254struct wpa_driver_nl80211_data {
255 struct nl80211_global *global;
256 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800257 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 char phyname[32];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700259 u8 perm_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 int ifindex;
262 int if_removed;
263 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 struct rfkill_data *rfkill;
266 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700267 u8 *extended_capa, *extended_capa_mask;
268 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 int has_capability;
270
271 int operstate;
272
273 int scan_complete_events;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700274 enum scan_states {
275 NO_SCAN, SCAN_REQUESTED, SCAN_STARTED, SCAN_COMPLETED,
276 SCAN_ABORTED, SCHED_SCAN_STARTED, SCHED_SCAN_STOPPED,
277 SCHED_SCAN_RESULTS
278 } scan_state;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281
282 u8 auth_bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700283 u8 auth_attempt_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 u8 bssid[ETH_ALEN];
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700285 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 int associated;
287 u8 ssid[32];
288 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289 enum nl80211_iftype nlmode;
290 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 unsigned int assoc_freq;
292
293 int monitor_sock;
294 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800295 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800297 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800299 unsigned int in_interface_list:1;
300 unsigned int device_ap_sme:1;
301 unsigned int poll_command_supported:1;
302 unsigned int data_tx_status:1;
303 unsigned int scan_for_auth:1;
304 unsigned int retry_auth:1;
305 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800306 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -0700307 unsigned int ignore_next_local_deauth:1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700308 unsigned int allow_p2p_device:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800309 unsigned int hostapd:1;
310 unsigned int start_mode_ap:1;
311 unsigned int start_iface_up:1;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800312 unsigned int test_use_roc_tx:1;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700313 unsigned int ignore_deauth_event:1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700314 unsigned int roaming_vendor_cmd_avail:1;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700315 unsigned int dfs_vendor_cmd_avail:1;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700316 unsigned int have_low_prio_scan:1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700317 unsigned int force_connect_cmd:1;
318 unsigned int addr_changed:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319
320 u64 remain_on_chan_cookie;
321 u64 send_action_cookie;
322
323 unsigned int last_mgmt_freq;
324
325 struct wpa_driver_scan_filter *filter_ssids;
326 size_t num_filter_ssids;
327
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800328 struct i802_bss *first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330 int eapol_tx_sock;
331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 int eapol_sock; /* socket for EAPOL frames */
333
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700334 struct nl_sock *rtnl_sk; /* nl_sock for NETLINK_ROUTE */
335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 int default_if_indices[16];
337 int *if_indices;
338 int num_if_indices;
339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800340 /* From failed authentication command */
341 int auth_freq;
342 u8 auth_bssid_[ETH_ALEN];
343 u8 auth_ssid[32];
344 size_t auth_ssid_len;
345 int auth_alg;
346 u8 *auth_ie;
347 size_t auth_ie_len;
348 u8 auth_wep_key[4][16];
349 size_t auth_wep_key_len[4];
350 int auth_wep_tx_keyidx;
351 int auth_local_state_change;
352 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353};
354
355
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800356static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
358 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800359static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
360 enum nl80211_iftype nlmode);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700361static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss, int freq);
362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800364wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
365 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
367 const u8 *addr, int cmd, u16 reason_code,
368 int local_state_change);
369static void nl80211_remove_monitor_interface(
370 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800371static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373 const u8 *buf, size_t buf_len, u64 *cookie,
374 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700375static int nl80211_register_frame(struct i802_bss *bss,
376 struct nl_handle *hl_handle,
377 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800378static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
379 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800380#ifdef ANDROID
381static int android_pno_start(struct i802_bss *bss,
382 struct wpa_driver_scan_params *params);
383static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800384extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
385 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800386#endif /* ANDROID */
387#ifdef ANDROID_P2P
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700388#ifdef ANDROID_P2P_STUB
389int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration) {
390 return 0;
391}
392int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len) {
393 return 0;
394}
395int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow) {
396 return -1;
397}
398int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
399 const struct wpabuf *proberesp,
400 const struct wpabuf *assocresp) {
401 return 0;
402}
403#else /* ANDROID_P2P_STUB */
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700404int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800405int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700406int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
407int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800408 const struct wpabuf *proberesp,
409 const struct wpabuf *assocresp);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700410#endif /* ANDROID_P2P_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800411#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
414static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
415static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800416static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 enum wpa_driver_if_type type,
418 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700419
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700420static int nl80211_set_channel(struct i802_bss *bss,
421 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
423 int ifindex, int disabled);
424
425static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800426static int wpa_driver_nl80211_authenticate_retry(
427 struct wpa_driver_nl80211_data *drv);
428
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700429static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700430static int i802_set_iface_flags(struct i802_bss *bss, int up);
431
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800432
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700433static const char * nl80211_command_to_string(enum nl80211_commands cmd)
434{
435#define C2S(x) case x: return #x;
436 switch (cmd) {
437 C2S(NL80211_CMD_UNSPEC)
438 C2S(NL80211_CMD_GET_WIPHY)
439 C2S(NL80211_CMD_SET_WIPHY)
440 C2S(NL80211_CMD_NEW_WIPHY)
441 C2S(NL80211_CMD_DEL_WIPHY)
442 C2S(NL80211_CMD_GET_INTERFACE)
443 C2S(NL80211_CMD_SET_INTERFACE)
444 C2S(NL80211_CMD_NEW_INTERFACE)
445 C2S(NL80211_CMD_DEL_INTERFACE)
446 C2S(NL80211_CMD_GET_KEY)
447 C2S(NL80211_CMD_SET_KEY)
448 C2S(NL80211_CMD_NEW_KEY)
449 C2S(NL80211_CMD_DEL_KEY)
450 C2S(NL80211_CMD_GET_BEACON)
451 C2S(NL80211_CMD_SET_BEACON)
452 C2S(NL80211_CMD_START_AP)
453 C2S(NL80211_CMD_STOP_AP)
454 C2S(NL80211_CMD_GET_STATION)
455 C2S(NL80211_CMD_SET_STATION)
456 C2S(NL80211_CMD_NEW_STATION)
457 C2S(NL80211_CMD_DEL_STATION)
458 C2S(NL80211_CMD_GET_MPATH)
459 C2S(NL80211_CMD_SET_MPATH)
460 C2S(NL80211_CMD_NEW_MPATH)
461 C2S(NL80211_CMD_DEL_MPATH)
462 C2S(NL80211_CMD_SET_BSS)
463 C2S(NL80211_CMD_SET_REG)
464 C2S(NL80211_CMD_REQ_SET_REG)
465 C2S(NL80211_CMD_GET_MESH_CONFIG)
466 C2S(NL80211_CMD_SET_MESH_CONFIG)
467 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
468 C2S(NL80211_CMD_GET_REG)
469 C2S(NL80211_CMD_GET_SCAN)
470 C2S(NL80211_CMD_TRIGGER_SCAN)
471 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
472 C2S(NL80211_CMD_SCAN_ABORTED)
473 C2S(NL80211_CMD_REG_CHANGE)
474 C2S(NL80211_CMD_AUTHENTICATE)
475 C2S(NL80211_CMD_ASSOCIATE)
476 C2S(NL80211_CMD_DEAUTHENTICATE)
477 C2S(NL80211_CMD_DISASSOCIATE)
478 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
479 C2S(NL80211_CMD_REG_BEACON_HINT)
480 C2S(NL80211_CMD_JOIN_IBSS)
481 C2S(NL80211_CMD_LEAVE_IBSS)
482 C2S(NL80211_CMD_TESTMODE)
483 C2S(NL80211_CMD_CONNECT)
484 C2S(NL80211_CMD_ROAM)
485 C2S(NL80211_CMD_DISCONNECT)
486 C2S(NL80211_CMD_SET_WIPHY_NETNS)
487 C2S(NL80211_CMD_GET_SURVEY)
488 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
489 C2S(NL80211_CMD_SET_PMKSA)
490 C2S(NL80211_CMD_DEL_PMKSA)
491 C2S(NL80211_CMD_FLUSH_PMKSA)
492 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
493 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
494 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
495 C2S(NL80211_CMD_REGISTER_FRAME)
496 C2S(NL80211_CMD_FRAME)
497 C2S(NL80211_CMD_FRAME_TX_STATUS)
498 C2S(NL80211_CMD_SET_POWER_SAVE)
499 C2S(NL80211_CMD_GET_POWER_SAVE)
500 C2S(NL80211_CMD_SET_CQM)
501 C2S(NL80211_CMD_NOTIFY_CQM)
502 C2S(NL80211_CMD_SET_CHANNEL)
503 C2S(NL80211_CMD_SET_WDS_PEER)
504 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
505 C2S(NL80211_CMD_JOIN_MESH)
506 C2S(NL80211_CMD_LEAVE_MESH)
507 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
508 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
509 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
510 C2S(NL80211_CMD_GET_WOWLAN)
511 C2S(NL80211_CMD_SET_WOWLAN)
512 C2S(NL80211_CMD_START_SCHED_SCAN)
513 C2S(NL80211_CMD_STOP_SCHED_SCAN)
514 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
515 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
516 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
517 C2S(NL80211_CMD_PMKSA_CANDIDATE)
518 C2S(NL80211_CMD_TDLS_OPER)
519 C2S(NL80211_CMD_TDLS_MGMT)
520 C2S(NL80211_CMD_UNEXPECTED_FRAME)
521 C2S(NL80211_CMD_PROBE_CLIENT)
522 C2S(NL80211_CMD_REGISTER_BEACONS)
523 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
524 C2S(NL80211_CMD_SET_NOACK_MAP)
525 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
526 C2S(NL80211_CMD_START_P2P_DEVICE)
527 C2S(NL80211_CMD_STOP_P2P_DEVICE)
528 C2S(NL80211_CMD_CONN_FAILED)
529 C2S(NL80211_CMD_SET_MCAST_RATE)
530 C2S(NL80211_CMD_SET_MAC_ACL)
531 C2S(NL80211_CMD_RADAR_DETECT)
532 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
533 C2S(NL80211_CMD_UPDATE_FT_IES)
534 C2S(NL80211_CMD_FT_EVENT)
535 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
536 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800537 C2S(NL80211_CMD_GET_COALESCE)
538 C2S(NL80211_CMD_SET_COALESCE)
539 C2S(NL80211_CMD_CHANNEL_SWITCH)
540 C2S(NL80211_CMD_VENDOR)
541 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700542 default:
543 return "NL80211_CMD_UNKNOWN";
544 }
545#undef C2S
546}
547
548
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800549/* Converts nl80211_chan_width to a common format */
550static enum chan_width convert2width(int width)
551{
552 switch (width) {
553 case NL80211_CHAN_WIDTH_20_NOHT:
554 return CHAN_WIDTH_20_NOHT;
555 case NL80211_CHAN_WIDTH_20:
556 return CHAN_WIDTH_20;
557 case NL80211_CHAN_WIDTH_40:
558 return CHAN_WIDTH_40;
559 case NL80211_CHAN_WIDTH_80:
560 return CHAN_WIDTH_80;
561 case NL80211_CHAN_WIDTH_80P80:
562 return CHAN_WIDTH_80P80;
563 case NL80211_CHAN_WIDTH_160:
564 return CHAN_WIDTH_160;
565 }
566 return CHAN_WIDTH_UNKNOWN;
567}
568
569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800570static int is_ap_interface(enum nl80211_iftype nlmode)
571{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700572 return nlmode == NL80211_IFTYPE_AP ||
573 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800574}
575
576
577static int is_sta_interface(enum nl80211_iftype nlmode)
578{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700579 return nlmode == NL80211_IFTYPE_STATION ||
580 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800581}
582
583
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700584static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700586 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
587 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800588}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589
590
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700591static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
592{
593 if (drv->associated)
594 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
595 drv->associated = 0;
596 os_memset(drv->bssid, 0, ETH_ALEN);
597}
598
599
Jouni Malinen87fd2792011-05-16 18:35:42 +0300600struct nl80211_bss_info_arg {
601 struct wpa_driver_nl80211_data *drv;
602 struct wpa_scan_results *res;
603 unsigned int assoc_freq;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700604 unsigned int ibss_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300606};
607
608static int bss_info_handler(struct nl_msg *msg, void *arg);
609
610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611/* nl80211 code */
612static int ack_handler(struct nl_msg *msg, void *arg)
613{
614 int *err = arg;
615 *err = 0;
616 return NL_STOP;
617}
618
619static int finish_handler(struct nl_msg *msg, void *arg)
620{
621 int *ret = arg;
622 *ret = 0;
623 return NL_SKIP;
624}
625
626static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
627 void *arg)
628{
629 int *ret = arg;
630 *ret = err->error;
631 return NL_SKIP;
632}
633
634
635static int no_seq_check(struct nl_msg *msg, void *arg)
636{
637 return NL_OK;
638}
639
640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642 struct nl_handle *nl_handle, struct nl_msg *msg,
643 int (*valid_handler)(struct nl_msg *, void *),
644 void *valid_data)
645{
646 struct nl_cb *cb;
647 int err = -ENOMEM;
648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800649 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 if (!cb)
651 goto out;
652
653 err = nl_send_auto_complete(nl_handle, msg);
654 if (err < 0)
655 goto out;
656
657 err = 1;
658
659 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
660 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
661 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
662
663 if (valid_handler)
664 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
665 valid_handler, valid_data);
666
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700667 while (err > 0) {
668 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700669 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700670 wpa_printf(MSG_INFO,
671 "nl80211: %s->nl_recvmsgs failed: %d",
672 __func__, res);
673 }
674 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 out:
676 nl_cb_put(cb);
677 nlmsg_free(msg);
678 return err;
679}
680
681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682static int send_and_recv_msgs_global(struct nl80211_global *global,
683 struct nl_msg *msg,
684 int (*valid_handler)(struct nl_msg *, void *),
685 void *valid_data)
686{
687 return send_and_recv(global, global->nl, msg, valid_handler,
688 valid_data);
689}
690
Dmitry Shmidt04949592012-07-19 12:16:46 -0700691
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800692static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 struct nl_msg *msg,
694 int (*valid_handler)(struct nl_msg *, void *),
695 void *valid_data)
696{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800697 return send_and_recv(drv->global, drv->global->nl, msg,
698 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699}
700
701
702struct family_data {
703 const char *group;
704 int id;
705};
706
707
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700708static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
709{
710 if (bss->wdev_id_set)
711 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
712 else
713 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
714 return 0;
715
716nla_put_failure:
717 return -1;
718}
719
720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721static int family_handler(struct nl_msg *msg, void *arg)
722{
723 struct family_data *res = arg;
724 struct nlattr *tb[CTRL_ATTR_MAX + 1];
725 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
726 struct nlattr *mcgrp;
727 int i;
728
729 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
730 genlmsg_attrlen(gnlh, 0), NULL);
731 if (!tb[CTRL_ATTR_MCAST_GROUPS])
732 return NL_SKIP;
733
734 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
735 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
736 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
737 nla_len(mcgrp), NULL);
738 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
739 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
740 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
741 res->group,
742 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
743 continue;
744 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
745 break;
746 };
747
748 return NL_SKIP;
749}
750
751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 const char *family, const char *group)
754{
755 struct nl_msg *msg;
756 int ret = -1;
757 struct family_data res = { group, -ENOENT };
758
759 msg = nlmsg_alloc();
760 if (!msg)
761 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 0, 0, CTRL_CMD_GETFAMILY, 0);
764 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800766 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 msg = NULL;
768 if (ret == 0)
769 ret = res.id;
770
771nla_put_failure:
772 nlmsg_free(msg);
773 return ret;
774}
775
776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
778 struct nl_msg *msg, int flags, uint8_t cmd)
779{
780 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
781 0, flags, cmd, 0);
782}
783
784
785struct wiphy_idx_data {
786 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700787 enum nl80211_iftype nlmode;
788 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800789};
790
791
792static int netdev_info_handler(struct nl_msg *msg, void *arg)
793{
794 struct nlattr *tb[NL80211_ATTR_MAX + 1];
795 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
796 struct wiphy_idx_data *info = arg;
797
798 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
799 genlmsg_attrlen(gnlh, 0), NULL);
800
801 if (tb[NL80211_ATTR_WIPHY])
802 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
803
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700804 if (tb[NL80211_ATTR_IFTYPE])
805 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
806
807 if (tb[NL80211_ATTR_MAC] && info->macaddr)
808 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
809 ETH_ALEN);
810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811 return NL_SKIP;
812}
813
814
815static int nl80211_get_wiphy_index(struct i802_bss *bss)
816{
817 struct nl_msg *msg;
818 struct wiphy_idx_data data = {
819 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700820 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800821 };
822
823 msg = nlmsg_alloc();
824 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700825 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800826
827 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
828
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700829 if (nl80211_set_iface_id(msg, bss) < 0)
830 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831
832 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
833 return data.wiphy_idx;
834 msg = NULL;
835nla_put_failure:
836 nlmsg_free(msg);
837 return -1;
838}
839
840
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700841static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
842{
843 struct nl_msg *msg;
844 struct wiphy_idx_data data = {
845 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
846 .macaddr = NULL,
847 };
848
849 msg = nlmsg_alloc();
850 if (!msg)
851 return -1;
852
853 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
854
855 if (nl80211_set_iface_id(msg, bss) < 0)
856 goto nla_put_failure;
857
858 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
859 return data.nlmode;
860 msg = NULL;
861nla_put_failure:
862 nlmsg_free(msg);
863 return NL80211_IFTYPE_UNSPECIFIED;
864}
865
866
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700867static int nl80211_get_macaddr(struct i802_bss *bss)
868{
869 struct nl_msg *msg;
870 struct wiphy_idx_data data = {
871 .macaddr = bss->addr,
872 };
873
874 msg = nlmsg_alloc();
875 if (!msg)
876 return NL80211_IFTYPE_UNSPECIFIED;
877
878 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
879 if (nl80211_set_iface_id(msg, bss) < 0)
880 goto nla_put_failure;
881
882 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
883
884nla_put_failure:
885 nlmsg_free(msg);
886 return NL80211_IFTYPE_UNSPECIFIED;
887}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700888
889
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800890static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
891 struct nl80211_wiphy_data *w)
892{
893 struct nl_msg *msg;
894 int ret = -1;
895
896 msg = nlmsg_alloc();
897 if (!msg)
898 return -1;
899
900 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
901
902 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
903
904 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
905 msg = NULL;
906 if (ret) {
907 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
908 "failed: ret=%d (%s)",
909 ret, strerror(-ret));
910 goto nla_put_failure;
911 }
912 ret = 0;
913nla_put_failure:
914 nlmsg_free(msg);
915 return ret;
916}
917
918
919static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
920{
921 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700922 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800923
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800924 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800925
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700926 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700927 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700928 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
929 __func__, res);
930 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800931}
932
933
934static int process_beacon_event(struct nl_msg *msg, void *arg)
935{
936 struct nl80211_wiphy_data *w = arg;
937 struct wpa_driver_nl80211_data *drv;
938 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
939 struct nlattr *tb[NL80211_ATTR_MAX + 1];
940 union wpa_event_data event;
941
942 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
943 genlmsg_attrlen(gnlh, 0), NULL);
944
945 if (gnlh->cmd != NL80211_CMD_FRAME) {
946 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
947 gnlh->cmd);
948 return NL_SKIP;
949 }
950
951 if (!tb[NL80211_ATTR_FRAME])
952 return NL_SKIP;
953
954 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
955 wiphy_list) {
956 os_memset(&event, 0, sizeof(event));
957 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
958 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
959 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
960 }
961
962 return NL_SKIP;
963}
964
965
966static struct nl80211_wiphy_data *
967nl80211_get_wiphy_data_ap(struct i802_bss *bss)
968{
969 static DEFINE_DL_LIST(nl80211_wiphys);
970 struct nl80211_wiphy_data *w;
971 int wiphy_idx, found = 0;
972 struct i802_bss *tmp_bss;
973
974 if (bss->wiphy_data != NULL)
975 return bss->wiphy_data;
976
977 wiphy_idx = nl80211_get_wiphy_index(bss);
978
979 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
980 if (w->wiphy_idx == wiphy_idx)
981 goto add;
982 }
983
984 /* alloc new one */
985 w = os_zalloc(sizeof(*w));
986 if (w == NULL)
987 return NULL;
988 w->wiphy_idx = wiphy_idx;
989 dl_list_init(&w->bsss);
990 dl_list_init(&w->drvs);
991
992 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
993 if (!w->nl_cb) {
994 os_free(w);
995 return NULL;
996 }
997 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
998 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
999 w);
1000
1001 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
1002 "wiphy beacons");
1003 if (w->nl_beacons == NULL) {
1004 os_free(w);
1005 return NULL;
1006 }
1007
1008 if (nl80211_register_beacons(bss->drv, w)) {
1009 nl_destroy_handles(&w->nl_beacons);
1010 os_free(w);
1011 return NULL;
1012 }
1013
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001014 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001015
1016 dl_list_add(&nl80211_wiphys, &w->list);
1017
1018add:
1019 /* drv entry for this bss already there? */
1020 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1021 if (tmp_bss->drv == bss->drv) {
1022 found = 1;
1023 break;
1024 }
1025 }
1026 /* if not add it */
1027 if (!found)
1028 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
1029
1030 dl_list_add(&w->bsss, &bss->wiphy_list);
1031 bss->wiphy_data = w;
1032 return w;
1033}
1034
1035
1036static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1037{
1038 struct nl80211_wiphy_data *w = bss->wiphy_data;
1039 struct i802_bss *tmp_bss;
1040 int found = 0;
1041
1042 if (w == NULL)
1043 return;
1044 bss->wiphy_data = NULL;
1045 dl_list_del(&bss->wiphy_list);
1046
1047 /* still any for this drv present? */
1048 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1049 if (tmp_bss->drv == bss->drv) {
1050 found = 1;
1051 break;
1052 }
1053 }
1054 /* if not remove it */
1055 if (!found)
1056 dl_list_del(&bss->drv->wiphy_list);
1057
1058 if (!dl_list_empty(&w->bsss))
1059 return;
1060
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001061 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001062
1063 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064 dl_list_del(&w->list);
1065 os_free(w);
1066}
1067
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1070{
1071 struct i802_bss *bss = priv;
1072 struct wpa_driver_nl80211_data *drv = bss->drv;
1073 if (!drv->associated)
1074 return -1;
1075 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1076 return 0;
1077}
1078
1079
1080static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1081{
1082 struct i802_bss *bss = priv;
1083 struct wpa_driver_nl80211_data *drv = bss->drv;
1084 if (!drv->associated)
1085 return -1;
1086 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1087 return drv->ssid_len;
1088}
1089
1090
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001091static void wpa_driver_nl80211_event_newlink(
1092 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093{
1094 union wpa_event_data event;
1095
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001096 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1097 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1098 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1099 drv->first_bss->ifname);
1100 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001102 if (!drv->if_removed)
1103 return;
1104 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1105 drv->first_bss->ifname);
1106 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 }
1108
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001109 os_memset(&event, 0, sizeof(event));
1110 os_strlcpy(event.interface_status.ifname, ifname,
1111 sizeof(event.interface_status.ifname));
1112 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1113 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1114}
1115
1116
1117static void wpa_driver_nl80211_event_dellink(
1118 struct wpa_driver_nl80211_data *drv, char *ifname)
1119{
1120 union wpa_event_data event;
1121
1122 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1123 if (drv->if_removed) {
1124 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1125 ifname);
1126 return;
1127 }
1128 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1129 ifname);
1130 drv->if_removed = 1;
1131 } else {
1132 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1133 ifname);
1134 }
1135
1136 os_memset(&event, 0, sizeof(event));
1137 os_strlcpy(event.interface_status.ifname, ifname,
1138 sizeof(event.interface_status.ifname));
1139 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1141}
1142
1143
1144static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1145 u8 *buf, size_t len)
1146{
1147 int attrlen, rta_len;
1148 struct rtattr *attr;
1149
1150 attrlen = len;
1151 attr = (struct rtattr *) buf;
1152
1153 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1154 while (RTA_OK(attr, attrlen)) {
1155 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001156 if (os_strcmp(((char *) attr) + rta_len,
1157 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 return 1;
1159 else
1160 break;
1161 }
1162 attr = RTA_NEXT(attr, attrlen);
1163 }
1164
1165 return 0;
1166}
1167
1168
1169static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1170 int ifindex, u8 *buf, size_t len)
1171{
1172 if (drv->ifindex == ifindex)
1173 return 1;
1174
1175 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1177 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001178 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 return 1;
1180 }
1181
1182 return 0;
1183}
1184
1185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001186static struct wpa_driver_nl80211_data *
1187nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1188{
1189 struct wpa_driver_nl80211_data *drv;
1190 dl_list_for_each(drv, &global->interfaces,
1191 struct wpa_driver_nl80211_data, list) {
1192 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1193 have_ifidx(drv, idx))
1194 return drv;
1195 }
1196 return NULL;
1197}
1198
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1201 struct ifinfomsg *ifi,
1202 u8 *buf, size_t len)
1203{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001204 struct nl80211_global *global = ctx;
1205 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001206 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 struct rtattr *attr;
1208 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001209 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001210 char ifname[IFNAMSIZ + 1];
1211 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001213 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1214 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001215 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1216 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 return;
1218 }
1219
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001220 extra[0] = '\0';
1221 pos = extra;
1222 end = pos + sizeof(extra);
1223 ifname[0] = '\0';
1224
1225 attrlen = len;
1226 attr = (struct rtattr *) buf;
1227 while (RTA_OK(attr, attrlen)) {
1228 switch (attr->rta_type) {
1229 case IFLA_IFNAME:
1230 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1231 break;
1232 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1233 ifname[RTA_PAYLOAD(attr)] = '\0';
1234 break;
1235 case IFLA_MASTER:
1236 brid = nla_get_u32((struct nlattr *) attr);
1237 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1238 break;
1239 case IFLA_WIRELESS:
1240 pos += os_snprintf(pos, end - pos, " wext");
1241 break;
1242 case IFLA_OPERSTATE:
1243 pos += os_snprintf(pos, end - pos, " operstate=%u",
1244 nla_get_u32((struct nlattr *) attr));
1245 break;
1246 case IFLA_LINKMODE:
1247 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1248 nla_get_u32((struct nlattr *) attr));
1249 break;
1250 }
1251 attr = RTA_NEXT(attr, attrlen);
1252 }
1253 extra[sizeof(extra) - 1] = '\0';
1254
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001255 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1256 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1257 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1259 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1260 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1261 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1262
1263 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264 if (if_indextoname(ifi->ifi_index, namebuf) &&
1265 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001266 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001267 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1268 "event since interface %s is up", namebuf);
1269 return;
1270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 if (drv->ignore_if_down_event) {
1273 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1274 "event generated by mode change");
1275 drv->ignore_if_down_event = 0;
1276 } else {
1277 drv->if_disabled = 1;
1278 wpa_supplicant_event(drv->ctx,
1279 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001280
1281 /*
1282 * Try to get drv again, since it may be removed as
1283 * part of the EVENT_INTERFACE_DISABLED handling for
1284 * dynamic interfaces
1285 */
1286 drv = nl80211_find_drv(global, ifi->ifi_index,
1287 buf, len);
1288 if (!drv)
1289 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 }
1292
1293 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001294 if (if_indextoname(ifi->ifi_index, namebuf) &&
1295 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001296 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1298 "event since interface %s is down",
1299 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001300 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001301 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1302 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001303 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001304 } else if (drv->if_removed) {
1305 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1306 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001307 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001308 } else {
1309 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1310 drv->if_disabled = 0;
1311 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1312 NULL);
1313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 }
1315
1316 /*
1317 * Some drivers send the association event before the operup event--in
1318 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1319 * fails. This will hit us when wpa_supplicant does not need to do
1320 * IEEE 802.1X authentication
1321 */
1322 if (drv->operstate == 1 &&
1323 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001324 !(ifi->ifi_flags & IFF_RUNNING)) {
1325 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001326 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 }
1329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001330 if (ifname[0])
1331 wpa_driver_nl80211_event_newlink(drv, ifname);
1332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 if (ifi->ifi_family == AF_BRIDGE && brid) {
1334 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 if_indextoname(brid, namebuf);
1336 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1337 brid, namebuf);
1338 add_ifidx(drv, brid);
1339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340}
1341
1342
1343static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1344 struct ifinfomsg *ifi,
1345 u8 *buf, size_t len)
1346{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001347 struct nl80211_global *global = ctx;
1348 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001349 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 struct rtattr *attr;
1351 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001352 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001353 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1356 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001357 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1358 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001359 return;
1360 }
1361
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001362 extra[0] = '\0';
1363 pos = extra;
1364 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001365 ifname[0] = '\0';
1366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 attrlen = len;
1368 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001370 switch (attr->rta_type) {
1371 case IFLA_IFNAME:
1372 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1373 break;
1374 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1375 ifname[RTA_PAYLOAD(attr)] = '\0';
1376 break;
1377 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001379 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1380 break;
1381 case IFLA_OPERSTATE:
1382 pos += os_snprintf(pos, end - pos, " operstate=%u",
1383 nla_get_u32((struct nlattr *) attr));
1384 break;
1385 case IFLA_LINKMODE:
1386 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1387 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001388 break;
1389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 attr = RTA_NEXT(attr, attrlen);
1391 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001392 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001394 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1395 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1396 ifi->ifi_flags,
1397 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1398 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1399 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1400 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1401
1402 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001403 wpa_driver_nl80211_event_dellink(drv, ifname);
1404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 if (ifi->ifi_family == AF_BRIDGE && brid) {
1406 /* device has been removed from bridge */
1407 char namebuf[IFNAMSIZ];
1408 if_indextoname(brid, namebuf);
1409 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1410 "%s", brid, namebuf);
1411 del_ifidx(drv, brid);
1412 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413}
1414
1415
1416static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1417 const u8 *frame, size_t len)
1418{
1419 const struct ieee80211_mgmt *mgmt;
1420 union wpa_event_data event;
1421
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001422 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1423 drv->force_connect_cmd) {
1424 /*
1425 * Avoid reporting two association events that would confuse
1426 * the core code.
1427 */
1428 wpa_printf(MSG_DEBUG,
1429 "nl80211: Ignore auth event when using driver SME");
1430 return;
1431 }
1432
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001433 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 mgmt = (const struct ieee80211_mgmt *) frame;
1435 if (len < 24 + sizeof(mgmt->u.auth)) {
1436 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1437 "frame");
1438 return;
1439 }
1440
1441 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001442 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 os_memset(&event, 0, sizeof(event));
1444 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1445 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001446 event.auth.auth_transaction =
1447 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1449 if (len > 24 + sizeof(mgmt->u.auth)) {
1450 event.auth.ies = mgmt->u.auth.variable;
1451 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1452 }
1453
1454 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1455}
1456
1457
Jouni Malinen87fd2792011-05-16 18:35:42 +03001458static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1459{
1460 struct nl_msg *msg;
1461 int ret;
1462 struct nl80211_bss_info_arg arg;
1463
1464 os_memset(&arg, 0, sizeof(arg));
1465 msg = nlmsg_alloc();
1466 if (!msg)
1467 goto nla_put_failure;
1468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001469 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001470 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1471
1472 arg.drv = drv;
1473 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1474 msg = NULL;
1475 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001476 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1477 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001478 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001479 "associated BSS from scan results: %u MHz", freq);
1480 if (freq)
1481 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001482 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001483 }
1484 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1485 "(%s)", ret, strerror(-ret));
1486nla_put_failure:
1487 nlmsg_free(msg);
1488 return drv->assoc_freq;
1489}
1490
1491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1493 const u8 *frame, size_t len)
1494{
1495 const struct ieee80211_mgmt *mgmt;
1496 union wpa_event_data event;
1497 u16 status;
1498
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001499 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1500 drv->force_connect_cmd) {
1501 /*
1502 * Avoid reporting two association events that would confuse
1503 * the core code.
1504 */
1505 wpa_printf(MSG_DEBUG,
1506 "nl80211: Ignore assoc event when using driver SME");
1507 return;
1508 }
1509
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001510 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 mgmt = (const struct ieee80211_mgmt *) frame;
1512 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1513 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1514 "frame");
1515 return;
1516 }
1517
1518 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1519 if (status != WLAN_STATUS_SUCCESS) {
1520 os_memset(&event, 0, sizeof(event));
1521 event.assoc_reject.bssid = mgmt->bssid;
1522 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1523 event.assoc_reject.resp_ies =
1524 (u8 *) mgmt->u.assoc_resp.variable;
1525 event.assoc_reject.resp_ies_len =
1526 len - 24 - sizeof(mgmt->u.assoc_resp);
1527 }
1528 event.assoc_reject.status_code = status;
1529
1530 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1531 return;
1532 }
1533
1534 drv->associated = 1;
1535 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001536 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537
1538 os_memset(&event, 0, sizeof(event));
1539 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1540 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1541 event.assoc_info.resp_ies_len =
1542 len - 24 - sizeof(mgmt->u.assoc_resp);
1543 }
1544
1545 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1548}
1549
1550
1551static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1552 enum nl80211_commands cmd, struct nlattr *status,
1553 struct nlattr *addr, struct nlattr *req_ie,
1554 struct nlattr *resp_ie)
1555{
1556 union wpa_event_data event;
1557
1558 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1559 /*
1560 * Avoid reporting two association events that would confuse
1561 * the core code.
1562 */
1563 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1564 "when using userspace SME", cmd);
1565 return;
1566 }
1567
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001568 if (cmd == NL80211_CMD_CONNECT)
1569 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1570 else if (cmd == NL80211_CMD_ROAM)
1571 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 os_memset(&event, 0, sizeof(event));
1574 if (cmd == NL80211_CMD_CONNECT &&
1575 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1576 if (addr)
1577 event.assoc_reject.bssid = nla_data(addr);
1578 if (resp_ie) {
1579 event.assoc_reject.resp_ies = nla_data(resp_ie);
1580 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1581 }
1582 event.assoc_reject.status_code = nla_get_u16(status);
1583 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1584 return;
1585 }
1586
1587 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001588 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001590 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592
1593 if (req_ie) {
1594 event.assoc_info.req_ies = nla_data(req_ie);
1595 event.assoc_info.req_ies_len = nla_len(req_ie);
1596 }
1597 if (resp_ie) {
1598 event.assoc_info.resp_ies = nla_data(resp_ie);
1599 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1600 }
1601
Jouni Malinen87fd2792011-05-16 18:35:42 +03001602 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1605}
1606
1607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001609 struct nlattr *reason, struct nlattr *addr,
1610 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611{
1612 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001613 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001614
1615 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1616 /*
1617 * Avoid reporting two disassociation events that could
1618 * confuse the core code.
1619 */
1620 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1621 "event when using userspace SME");
1622 return;
1623 }
1624
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001625 if (drv->ignore_next_local_disconnect) {
1626 drv->ignore_next_local_disconnect = 0;
1627 if (locally_generated) {
1628 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1629 "event triggered during reassociation");
1630 return;
1631 }
1632 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1633 "disconnect but got another disconnect "
1634 "event first");
1635 }
1636
1637 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001638 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001639 os_memset(&data, 0, sizeof(data));
1640 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001641 data.deauth_info.reason_code = nla_get_u16(reason);
1642 data.deauth_info.locally_generated = by_ap == NULL;
1643 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1644}
1645
1646
Dmitry Shmidt97672262014-02-03 13:02:54 -08001647static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1648{
1649 int freq1 = 0;
1650
1651 switch (convert2width(width)) {
1652 case CHAN_WIDTH_20_NOHT:
1653 case CHAN_WIDTH_20:
1654 return 0;
1655 case CHAN_WIDTH_40:
1656 freq1 = cf1 - 10;
1657 break;
1658 case CHAN_WIDTH_80:
1659 freq1 = cf1 - 30;
1660 break;
1661 case CHAN_WIDTH_160:
1662 freq1 = cf1 - 70;
1663 break;
1664 case CHAN_WIDTH_UNKNOWN:
1665 case CHAN_WIDTH_80P80:
1666 /* FIXME: implement this */
1667 return 0;
1668 }
1669
1670 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1671}
1672
1673
Dmitry Shmidt04949592012-07-19 12:16:46 -07001674static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001675 struct nlattr *ifindex, struct nlattr *freq,
1676 struct nlattr *type, struct nlattr *bw,
1677 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001678{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001679 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001680 union wpa_event_data data;
1681 int ht_enabled = 1;
1682 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001683 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001684
1685 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1686
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001687 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001688 return;
1689
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001690 ifidx = nla_get_u32(ifindex);
1691 for (bss = drv->first_bss; bss; bss = bss->next)
1692 if (bss->ifindex == ifidx)
1693 break;
1694
1695 if (bss == NULL) {
1696 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1697 ifidx);
1698 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001699 }
1700
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001701 if (type) {
1702 switch (nla_get_u32(type)) {
1703 case NL80211_CHAN_NO_HT:
1704 ht_enabled = 0;
1705 break;
1706 case NL80211_CHAN_HT20:
1707 break;
1708 case NL80211_CHAN_HT40PLUS:
1709 chan_offset = 1;
1710 break;
1711 case NL80211_CHAN_HT40MINUS:
1712 chan_offset = -1;
1713 break;
1714 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001715 } else if (bw && cf1) {
1716 /* This can happen for example with VHT80 ch switch */
1717 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1718 nla_get_u32(freq),
1719 nla_get_u32(cf1),
1720 cf2 ? nla_get_u32(cf2) : 0);
1721 } else {
1722 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001723 }
1724
1725 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001726 data.ch_switch.freq = nla_get_u32(freq);
1727 data.ch_switch.ht_enabled = ht_enabled;
1728 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001729 if (bw)
1730 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1731 if (cf1)
1732 data.ch_switch.cf1 = nla_get_u32(cf1);
1733 if (cf2)
1734 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001735
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001736 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001737
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001738 wpa_supplicant_event(bss->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739}
1740
1741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1743 enum nl80211_commands cmd, struct nlattr *addr)
1744{
1745 union wpa_event_data event;
1746 enum wpa_event_type ev;
1747
1748 if (nla_len(addr) != ETH_ALEN)
1749 return;
1750
1751 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1752 cmd, MAC2STR((u8 *) nla_data(addr)));
1753
1754 if (cmd == NL80211_CMD_AUTHENTICATE)
1755 ev = EVENT_AUTH_TIMED_OUT;
1756 else if (cmd == NL80211_CMD_ASSOCIATE)
1757 ev = EVENT_ASSOC_TIMED_OUT;
1758 else
1759 return;
1760
1761 os_memset(&event, 0, sizeof(event));
1762 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1763 wpa_supplicant_event(drv->ctx, ev, &event);
1764}
1765
1766
Dmitry Shmidt98660862014-03-11 17:26:21 -07001767static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001768 struct nlattr *freq, struct nlattr *sig,
1769 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001771 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772 const struct ieee80211_mgmt *mgmt;
1773 union wpa_event_data event;
1774 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001775 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001776 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001778 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 mgmt = (const struct ieee80211_mgmt *) frame;
1780 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001781 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 return;
1783 }
1784
1785 fc = le_to_host16(mgmt->frame_control);
1786 stype = WLAN_FC_GET_STYPE(fc);
1787
Dmitry Shmidt04949592012-07-19 12:16:46 -07001788 if (sig)
1789 ssi_signal = (s32) nla_get_u32(sig);
1790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 os_memset(&event, 0, sizeof(event));
1792 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001793 event.rx_mgmt.freq = nla_get_u32(freq);
1794 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001796 wpa_printf(MSG_DEBUG,
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001797 "nl80211: RX frame sa=" MACSTR
1798 " freq=%d ssi_signal=%d stype=%u (%s) len=%u",
1799 MAC2STR(mgmt->sa), rx_freq, ssi_signal, stype, fc2str(fc),
1800 (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001801 event.rx_mgmt.frame = frame;
1802 event.rx_mgmt.frame_len = len;
1803 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001804 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001805 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806}
1807
1808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001809static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1810 struct nlattr *cookie, const u8 *frame,
1811 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812{
1813 union wpa_event_data event;
1814 const struct ieee80211_hdr *hdr;
1815 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001817 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001818 if (!is_ap_interface(drv->nlmode)) {
1819 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001821 if (!cookie)
1822 return;
1823
1824 cookie_val = nla_get_u64(cookie);
1825 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1826 " cookie=0%llx%s (ack=%d)",
1827 (long long unsigned int) cookie_val,
1828 cookie_val == drv->send_action_cookie ?
1829 " (match)" : " (unknown)", ack != NULL);
1830 if (cookie_val != drv->send_action_cookie)
1831 return;
1832 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833
1834 hdr = (const struct ieee80211_hdr *) frame;
1835 fc = le_to_host16(hdr->frame_control);
1836
1837 os_memset(&event, 0, sizeof(event));
1838 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1839 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1840 event.tx_status.dst = hdr->addr1;
1841 event.tx_status.data = frame;
1842 event.tx_status.data_len = len;
1843 event.tx_status.ack = ack != NULL;
1844 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1845}
1846
1847
1848static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1849 enum wpa_event_type type,
1850 const u8 *frame, size_t len)
1851{
1852 const struct ieee80211_mgmt *mgmt;
1853 union wpa_event_data event;
1854 const u8 *bssid = NULL;
1855 u16 reason_code = 0;
1856
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001857 if (type == EVENT_DEAUTH)
1858 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1859 else
1860 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 mgmt = (const struct ieee80211_mgmt *) frame;
1863 if (len >= 24) {
1864 bssid = mgmt->bssid;
1865
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001866 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1867 !drv->associated &&
1868 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1869 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1870 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1871 /*
1872 * Avoid issues with some roaming cases where
1873 * disconnection event for the old AP may show up after
1874 * we have started connection with the new AP.
1875 */
1876 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1877 MAC2STR(bssid),
1878 MAC2STR(drv->auth_attempt_bssid));
1879 return;
1880 }
1881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 if (drv->associated != 0 &&
1883 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1884 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1885 /*
1886 * We have presumably received this deauth as a
1887 * response to a clear_state_mismatch() outgoing
1888 * deauth. Don't let it take us offline!
1889 */
1890 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1891 "from Unknown BSSID " MACSTR " -- ignoring",
1892 MAC2STR(bssid));
1893 return;
1894 }
1895 }
1896
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001897 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 os_memset(&event, 0, sizeof(event));
1899
1900 /* Note: Same offset for Reason Code in both frame subtypes */
1901 if (len >= 24 + sizeof(mgmt->u.deauth))
1902 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1903
1904 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001905 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001906 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 event.disassoc_info.addr = bssid;
1908 event.disassoc_info.reason_code = reason_code;
1909 if (frame + len > mgmt->u.disassoc.variable) {
1910 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1911 event.disassoc_info.ie_len = frame + len -
1912 mgmt->u.disassoc.variable;
1913 }
1914 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001915 if (drv->ignore_deauth_event) {
1916 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1917 drv->ignore_deauth_event = 0;
1918 return;
1919 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001920 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001921 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001922 if (drv->ignore_next_local_deauth) {
1923 drv->ignore_next_local_deauth = 0;
1924 if (event.deauth_info.locally_generated) {
1925 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1926 return;
1927 }
1928 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 event.deauth_info.addr = bssid;
1931 event.deauth_info.reason_code = reason_code;
1932 if (frame + len > mgmt->u.deauth.variable) {
1933 event.deauth_info.ie = mgmt->u.deauth.variable;
1934 event.deauth_info.ie_len = frame + len -
1935 mgmt->u.deauth.variable;
1936 }
1937 }
1938
1939 wpa_supplicant_event(drv->ctx, type, &event);
1940}
1941
1942
1943static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1944 enum wpa_event_type type,
1945 const u8 *frame, size_t len)
1946{
1947 const struct ieee80211_mgmt *mgmt;
1948 union wpa_event_data event;
1949 u16 reason_code = 0;
1950
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001951 if (type == EVENT_UNPROT_DEAUTH)
1952 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1953 else
1954 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 if (len < 24)
1957 return;
1958
1959 mgmt = (const struct ieee80211_mgmt *) frame;
1960
1961 os_memset(&event, 0, sizeof(event));
1962 /* Note: Same offset for Reason Code in both frame subtypes */
1963 if (len >= 24 + sizeof(mgmt->u.deauth))
1964 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1965
1966 if (type == EVENT_UNPROT_DISASSOC) {
1967 event.unprot_disassoc.sa = mgmt->sa;
1968 event.unprot_disassoc.da = mgmt->da;
1969 event.unprot_disassoc.reason_code = reason_code;
1970 } else {
1971 event.unprot_deauth.sa = mgmt->sa;
1972 event.unprot_deauth.da = mgmt->da;
1973 event.unprot_deauth.reason_code = reason_code;
1974 }
1975
1976 wpa_supplicant_event(drv->ctx, type, &event);
1977}
1978
1979
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001980static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 enum nl80211_commands cmd, struct nlattr *frame,
1982 struct nlattr *addr, struct nlattr *timed_out,
1983 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001984 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001986 struct wpa_driver_nl80211_data *drv = bss->drv;
1987 const u8 *data;
1988 size_t len;
1989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 if (timed_out && addr) {
1991 mlme_timeout_event(drv, cmd, addr);
1992 return;
1993 }
1994
1995 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001996 wpa_printf(MSG_DEBUG,
1997 "nl80211: MLME event %d (%s) without frame data",
1998 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 return;
2000 }
2001
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002002 data = nla_data(frame);
2003 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002004 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002005 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
2006 MACSTR ") - too short",
2007 cmd, nl80211_command_to_string(cmd), bss->ifname,
2008 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002009 return;
2010 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002011 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
2012 ") A1=" MACSTR " A2=" MACSTR, cmd,
2013 nl80211_command_to_string(cmd), bss->ifname,
2014 MAC2STR(bss->addr), MAC2STR(data + 4),
2015 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002016 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002017 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
2018 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002019 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
2020 "for foreign address", bss->ifname);
2021 return;
2022 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
2024 nla_data(frame), nla_len(frame));
2025
2026 switch (cmd) {
2027 case NL80211_CMD_AUTHENTICATE:
2028 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
2029 break;
2030 case NL80211_CMD_ASSOCIATE:
2031 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
2032 break;
2033 case NL80211_CMD_DEAUTHENTICATE:
2034 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
2035 nla_data(frame), nla_len(frame));
2036 break;
2037 case NL80211_CMD_DISASSOCIATE:
2038 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
2039 nla_data(frame), nla_len(frame));
2040 break;
2041 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07002042 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07002043 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 break;
2045 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002046 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
2047 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 break;
2049 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2050 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
2051 nla_data(frame), nla_len(frame));
2052 break;
2053 case NL80211_CMD_UNPROT_DISASSOCIATE:
2054 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
2055 nla_data(frame), nla_len(frame));
2056 break;
2057 default:
2058 break;
2059 }
2060}
2061
2062
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002063static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 struct nlattr *tb[])
2065{
2066 union wpa_event_data data;
2067
2068 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2069 os_memset(&data, 0, sizeof(data));
2070 if (tb[NL80211_ATTR_MAC]) {
2071 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2072 nla_data(tb[NL80211_ATTR_MAC]),
2073 nla_len(tb[NL80211_ATTR_MAC]));
2074 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
2075 }
2076 if (tb[NL80211_ATTR_KEY_SEQ]) {
2077 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2078 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2079 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2080 }
2081 if (tb[NL80211_ATTR_KEY_TYPE]) {
2082 enum nl80211_key_type key_type =
2083 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2084 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2085 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2086 data.michael_mic_failure.unicast = 1;
2087 } else
2088 data.michael_mic_failure.unicast = 1;
2089
2090 if (tb[NL80211_ATTR_KEY_IDX]) {
2091 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2092 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2093 }
2094
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002095 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096}
2097
2098
2099static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2100 struct nlattr *tb[])
2101{
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002102 unsigned int freq;
2103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 if (tb[NL80211_ATTR_MAC] == NULL) {
2105 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2106 "event");
2107 return;
2108 }
2109 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 drv->associated = 1;
2112 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2113 MAC2STR(drv->bssid));
2114
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002115 freq = nl80211_get_assoc_freq(drv);
2116 if (freq) {
2117 wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
2118 freq);
2119 drv->first_bss->freq = freq;
2120 }
2121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2123}
2124
2125
2126static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2127 int cancel_event, struct nlattr *tb[])
2128{
2129 unsigned int freq, chan_type, duration;
2130 union wpa_event_data data;
2131 u64 cookie;
2132
2133 if (tb[NL80211_ATTR_WIPHY_FREQ])
2134 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2135 else
2136 freq = 0;
2137
2138 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2139 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2140 else
2141 chan_type = 0;
2142
2143 if (tb[NL80211_ATTR_DURATION])
2144 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2145 else
2146 duration = 0;
2147
2148 if (tb[NL80211_ATTR_COOKIE])
2149 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2150 else
2151 cookie = 0;
2152
2153 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2154 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2155 cancel_event, freq, chan_type, duration,
2156 (long long unsigned int) cookie,
2157 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2158
2159 if (cookie != drv->remain_on_chan_cookie)
2160 return; /* not for us */
2161
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002162 if (cancel_event)
2163 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164
2165 os_memset(&data, 0, sizeof(data));
2166 data.remain_on_channel.freq = freq;
2167 data.remain_on_channel.duration = duration;
2168 wpa_supplicant_event(drv->ctx, cancel_event ?
2169 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2170 EVENT_REMAIN_ON_CHANNEL, &data);
2171}
2172
2173
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002174static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2175 struct nlattr *tb[])
2176{
2177 union wpa_event_data data;
2178
2179 os_memset(&data, 0, sizeof(data));
2180
2181 if (tb[NL80211_ATTR_IE]) {
2182 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2183 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2184 }
2185
2186 if (tb[NL80211_ATTR_IE_RIC]) {
2187 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2188 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2189 }
2190
2191 if (tb[NL80211_ATTR_MAC])
2192 os_memcpy(data.ft_ies.target_ap,
2193 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2194
2195 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2196 MAC2STR(data.ft_ies.target_ap));
2197
2198 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2199}
2200
2201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2203 struct nlattr *tb[])
2204{
2205 union wpa_event_data event;
2206 struct nlattr *nl;
2207 int rem;
2208 struct scan_info *info;
2209#define MAX_REPORT_FREQS 50
2210 int freqs[MAX_REPORT_FREQS];
2211 int num_freqs = 0;
2212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002213 if (drv->scan_for_auth) {
2214 drv->scan_for_auth = 0;
2215 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2216 "cfg80211 BSS entry");
2217 wpa_driver_nl80211_authenticate_retry(drv);
2218 return;
2219 }
2220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 os_memset(&event, 0, sizeof(event));
2222 info = &event.scan_info;
2223 info->aborted = aborted;
2224
2225 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2226 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2227 struct wpa_driver_scan_ssid *s =
2228 &info->ssids[info->num_ssids];
2229 s->ssid = nla_data(nl);
2230 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002231 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2232 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 info->num_ssids++;
2234 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2235 break;
2236 }
2237 }
2238 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002239 char msg[200], *pos, *end;
2240 int res;
2241
2242 pos = msg;
2243 end = pos + sizeof(msg);
2244 *pos = '\0';
2245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2247 {
2248 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002249 res = os_snprintf(pos, end - pos, " %d",
2250 freqs[num_freqs]);
2251 if (res > 0 && end - pos > res)
2252 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 num_freqs++;
2254 if (num_freqs == MAX_REPORT_FREQS - 1)
2255 break;
2256 }
2257 info->freqs = freqs;
2258 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002259 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2260 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 }
2262 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2263}
2264
2265
2266static int get_link_signal(struct nl_msg *msg, void *arg)
2267{
2268 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2269 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2270 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2271 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2272 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002273 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274 };
2275 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2276 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2277 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2278 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2279 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2280 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2281 };
2282 struct wpa_signal_info *sig_change = arg;
2283
2284 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2285 genlmsg_attrlen(gnlh, 0), NULL);
2286 if (!tb[NL80211_ATTR_STA_INFO] ||
2287 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2288 tb[NL80211_ATTR_STA_INFO], policy))
2289 return NL_SKIP;
2290 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2291 return NL_SKIP;
2292
2293 sig_change->current_signal =
2294 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2295
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002296 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2297 sig_change->avg_signal =
2298 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2299 else
2300 sig_change->avg_signal = 0;
2301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2303 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2304 sinfo[NL80211_STA_INFO_TX_BITRATE],
2305 rate_policy)) {
2306 sig_change->current_txrate = 0;
2307 } else {
2308 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2309 sig_change->current_txrate =
2310 nla_get_u16(rinfo[
2311 NL80211_RATE_INFO_BITRATE]) * 100;
2312 }
2313 }
2314 }
2315
2316 return NL_SKIP;
2317}
2318
2319
2320static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2321 struct wpa_signal_info *sig)
2322{
2323 struct nl_msg *msg;
2324
2325 sig->current_signal = -9999;
2326 sig->current_txrate = 0;
2327
2328 msg = nlmsg_alloc();
2329 if (!msg)
2330 return -ENOMEM;
2331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002332 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333
2334 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2335 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2336
2337 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2338 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002339 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 return -ENOBUFS;
2341}
2342
2343
2344static int get_link_noise(struct nl_msg *msg, void *arg)
2345{
2346 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2347 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2348 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2349 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2350 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2351 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2352 };
2353 struct wpa_signal_info *sig_change = arg;
2354
2355 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2356 genlmsg_attrlen(gnlh, 0), NULL);
2357
2358 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2359 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2360 return NL_SKIP;
2361 }
2362
2363 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2364 tb[NL80211_ATTR_SURVEY_INFO],
2365 survey_policy)) {
2366 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2367 "attributes!");
2368 return NL_SKIP;
2369 }
2370
2371 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2372 return NL_SKIP;
2373
2374 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2375 sig_change->frequency)
2376 return NL_SKIP;
2377
2378 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2379 return NL_SKIP;
2380
2381 sig_change->current_noise =
2382 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2383
2384 return NL_SKIP;
2385}
2386
2387
2388static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2389 struct wpa_signal_info *sig_change)
2390{
2391 struct nl_msg *msg;
2392
2393 sig_change->current_noise = 9999;
2394 sig_change->frequency = drv->assoc_freq;
2395
2396 msg = nlmsg_alloc();
2397 if (!msg)
2398 return -ENOMEM;
2399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002400 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401
2402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2403
2404 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2405 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002406 nlmsg_free(msg);
2407 return -ENOBUFS;
2408}
2409
2410
2411static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2412{
2413 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2414 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2415 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2416 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2417 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2418 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2419 };
2420 struct wpa_scan_results *scan_results = arg;
2421 struct wpa_scan_res *scan_res;
2422 size_t i;
2423
2424 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2425 genlmsg_attrlen(gnlh, 0), NULL);
2426
2427 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2428 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2429 return NL_SKIP;
2430 }
2431
2432 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2433 tb[NL80211_ATTR_SURVEY_INFO],
2434 survey_policy)) {
2435 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2436 "attributes");
2437 return NL_SKIP;
2438 }
2439
2440 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2441 return NL_SKIP;
2442
2443 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2444 return NL_SKIP;
2445
2446 for (i = 0; i < scan_results->num; ++i) {
2447 scan_res = scan_results->res[i];
2448 if (!scan_res)
2449 continue;
2450 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2451 scan_res->freq)
2452 continue;
2453 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2454 continue;
2455 scan_res->noise = (s8)
2456 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2457 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2458 }
2459
2460 return NL_SKIP;
2461}
2462
2463
2464static int nl80211_get_noise_for_scan_results(
2465 struct wpa_driver_nl80211_data *drv,
2466 struct wpa_scan_results *scan_res)
2467{
2468 struct nl_msg *msg;
2469
2470 msg = nlmsg_alloc();
2471 if (!msg)
2472 return -ENOMEM;
2473
2474 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2475
2476 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2477
2478 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2479 scan_res);
2480 nla_put_failure:
2481 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 return -ENOBUFS;
2483}
2484
2485
2486static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2487 struct nlattr *tb[])
2488{
2489 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2490 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2491 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2492 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2493 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2494 };
2495 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2496 enum nl80211_cqm_rssi_threshold_event event;
2497 union wpa_event_data ed;
2498 struct wpa_signal_info sig;
2499 int res;
2500
2501 if (tb[NL80211_ATTR_CQM] == NULL ||
2502 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2503 cqm_policy)) {
2504 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2505 return;
2506 }
2507
2508 os_memset(&ed, 0, sizeof(ed));
2509
2510 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2511 if (!tb[NL80211_ATTR_MAC])
2512 return;
2513 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2514 ETH_ALEN);
2515 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2516 return;
2517 }
2518
2519 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2520 return;
2521 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2522
2523 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2524 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2525 "event: RSSI high");
2526 ed.signal_change.above_threshold = 1;
2527 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2528 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2529 "event: RSSI low");
2530 ed.signal_change.above_threshold = 0;
2531 } else
2532 return;
2533
2534 res = nl80211_get_link_signal(drv, &sig);
2535 if (res == 0) {
2536 ed.signal_change.current_signal = sig.current_signal;
2537 ed.signal_change.current_txrate = sig.current_txrate;
2538 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2539 sig.current_signal, sig.current_txrate);
2540 }
2541
2542 res = nl80211_get_link_noise(drv, &sig);
2543 if (res == 0) {
2544 ed.signal_change.current_noise = sig.current_noise;
2545 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2546 sig.current_noise);
2547 }
2548
2549 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2550}
2551
2552
2553static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2554 struct nlattr **tb)
2555{
2556 u8 *addr;
2557 union wpa_event_data data;
2558
2559 if (tb[NL80211_ATTR_MAC] == NULL)
2560 return;
2561 addr = nla_data(tb[NL80211_ATTR_MAC]);
2562 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002564 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002565 u8 *ies = NULL;
2566 size_t ies_len = 0;
2567 if (tb[NL80211_ATTR_IE]) {
2568 ies = nla_data(tb[NL80211_ATTR_IE]);
2569 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2570 }
2571 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2572 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2573 return;
2574 }
2575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2577 return;
2578
2579 os_memset(&data, 0, sizeof(data));
2580 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2581 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2582}
2583
2584
2585static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2586 struct nlattr **tb)
2587{
2588 u8 *addr;
2589 union wpa_event_data data;
2590
2591 if (tb[NL80211_ATTR_MAC] == NULL)
2592 return;
2593 addr = nla_data(tb[NL80211_ATTR_MAC]);
2594 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2595 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002597 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002598 drv_event_disassoc(drv->ctx, addr);
2599 return;
2600 }
2601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2603 return;
2604
2605 os_memset(&data, 0, sizeof(data));
2606 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2607 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2608}
2609
2610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002611static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2612 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002614 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2615 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2616 [NL80211_REKEY_DATA_KEK] = {
2617 .minlen = NL80211_KEK_LEN,
2618 .maxlen = NL80211_KEK_LEN,
2619 },
2620 [NL80211_REKEY_DATA_KCK] = {
2621 .minlen = NL80211_KCK_LEN,
2622 .maxlen = NL80211_KCK_LEN,
2623 },
2624 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2625 .minlen = NL80211_REPLAY_CTR_LEN,
2626 .maxlen = NL80211_REPLAY_CTR_LEN,
2627 },
2628 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 union wpa_event_data data;
2630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002631 if (!tb[NL80211_ATTR_MAC])
2632 return;
2633 if (!tb[NL80211_ATTR_REKEY_DATA])
2634 return;
2635 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2636 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2637 return;
2638 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2639 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002641 os_memset(&data, 0, sizeof(data));
2642 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2643 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2644 MAC2STR(data.driver_gtk_rekey.bssid));
2645 data.driver_gtk_rekey.replay_ctr =
2646 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2647 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2648 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2649 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2650}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652
2653static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2654 struct nlattr **tb)
2655{
2656 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2657 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2658 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2659 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2660 .minlen = ETH_ALEN,
2661 .maxlen = ETH_ALEN,
2662 },
2663 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2664 };
2665 union wpa_event_data data;
2666
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002667 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2668
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2670 return;
2671 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2672 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2673 return;
2674 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2675 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2676 return;
2677
2678 os_memset(&data, 0, sizeof(data));
2679 os_memcpy(data.pmkid_candidate.bssid,
2680 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2681 data.pmkid_candidate.index =
2682 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2683 data.pmkid_candidate.preauth =
2684 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2685 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2686}
2687
2688
2689static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2690 struct nlattr **tb)
2691{
2692 union wpa_event_data data;
2693
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002694 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002696 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2697 return;
2698
2699 os_memset(&data, 0, sizeof(data));
2700 os_memcpy(data.client_poll.addr,
2701 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2702
2703 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2704}
2705
2706
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002707static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2708 struct nlattr **tb)
2709{
2710 union wpa_event_data data;
2711
2712 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2713
2714 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2715 return;
2716
2717 os_memset(&data, 0, sizeof(data));
2718 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2719 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2720 case NL80211_TDLS_SETUP:
2721 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2722 MACSTR, MAC2STR(data.tdls.peer));
2723 data.tdls.oper = TDLS_REQUEST_SETUP;
2724 break;
2725 case NL80211_TDLS_TEARDOWN:
2726 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2727 MACSTR, MAC2STR(data.tdls.peer));
2728 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2729 break;
2730 default:
2731 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2732 "event");
2733 return;
2734 }
2735 if (tb[NL80211_ATTR_REASON_CODE]) {
2736 data.tdls.reason_code =
2737 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2738 }
2739
2740 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2741}
2742
2743
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002744static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2745 struct nlattr **tb)
2746{
2747 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2748}
2749
2750
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002751static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2752 struct nlattr **tb)
2753{
2754 union wpa_event_data data;
2755 u32 reason;
2756
2757 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2758
2759 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2760 return;
2761
2762 os_memset(&data, 0, sizeof(data));
2763 os_memcpy(data.connect_failed_reason.addr,
2764 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2765
2766 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2767 switch (reason) {
2768 case NL80211_CONN_FAIL_MAX_CLIENTS:
2769 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2770 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2771 break;
2772 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2773 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2774 " tried to connect",
2775 MAC2STR(data.connect_failed_reason.addr));
2776 data.connect_failed_reason.code = BLOCKED_CLIENT;
2777 break;
2778 default:
2779 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2780 "%u", reason);
2781 return;
2782 }
2783
2784 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2785}
2786
2787
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002788static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2789 struct nlattr **tb)
2790{
2791 union wpa_event_data data;
2792 enum nl80211_radar_event event_type;
2793
2794 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2795 return;
2796
2797 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002798 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2799 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002800
Dmitry Shmidt051af732013-10-22 13:52:46 -07002801 /* Check HT params */
2802 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2803 data.dfs_event.ht_enabled = 1;
2804 data.dfs_event.chan_offset = 0;
2805
2806 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2807 case NL80211_CHAN_NO_HT:
2808 data.dfs_event.ht_enabled = 0;
2809 break;
2810 case NL80211_CHAN_HT20:
2811 break;
2812 case NL80211_CHAN_HT40PLUS:
2813 data.dfs_event.chan_offset = 1;
2814 break;
2815 case NL80211_CHAN_HT40MINUS:
2816 data.dfs_event.chan_offset = -1;
2817 break;
2818 }
2819 }
2820
2821 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002822 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2823 data.dfs_event.chan_width =
2824 convert2width(nla_get_u32(
2825 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2826 if (tb[NL80211_ATTR_CENTER_FREQ1])
2827 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002828 if (tb[NL80211_ATTR_CENTER_FREQ2])
2829 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2830
2831 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2832 data.dfs_event.freq, data.dfs_event.ht_enabled,
2833 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2834 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002835
2836 switch (event_type) {
2837 case NL80211_RADAR_DETECTED:
2838 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2839 break;
2840 case NL80211_RADAR_CAC_FINISHED:
2841 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2842 break;
2843 case NL80211_RADAR_CAC_ABORTED:
2844 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2845 break;
2846 case NL80211_RADAR_NOP_FINISHED:
2847 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2848 break;
2849 default:
2850 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2851 "received", event_type);
2852 break;
2853 }
2854}
2855
2856
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002857static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2858 int wds)
2859{
2860 struct wpa_driver_nl80211_data *drv = bss->drv;
2861 union wpa_event_data event;
2862
2863 if (!tb[NL80211_ATTR_MAC])
2864 return;
2865
2866 os_memset(&event, 0, sizeof(event));
2867 event.rx_from_unknown.bssid = bss->addr;
2868 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2869 event.rx_from_unknown.wds = wds;
2870
2871 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2872}
2873
2874
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002875static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2876 const u8 *data, size_t len)
2877{
2878 u32 i, count;
2879 union wpa_event_data event;
2880 struct wpa_freq_range *range = NULL;
2881 const struct qca_avoid_freq_list *freq_range;
2882
2883 freq_range = (const struct qca_avoid_freq_list *) data;
2884 if (len < sizeof(freq_range->count))
2885 return;
2886
2887 count = freq_range->count;
2888 if (len < sizeof(freq_range->count) +
2889 count * sizeof(struct qca_avoid_freq_range)) {
2890 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2891 (unsigned int) len);
2892 return;
2893 }
2894
2895 if (count > 0) {
2896 range = os_calloc(count, sizeof(struct wpa_freq_range));
2897 if (range == NULL)
2898 return;
2899 }
2900
2901 os_memset(&event, 0, sizeof(event));
2902 for (i = 0; i < count; i++) {
2903 unsigned int idx = event.freq_range.num;
2904 range[idx].min = freq_range->range[i].start_freq;
2905 range[idx].max = freq_range->range[i].end_freq;
2906 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2907 range[idx].min, range[idx].max);
2908 if (range[idx].min > range[idx].max) {
2909 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2910 continue;
2911 }
2912 event.freq_range.num++;
2913 }
2914 event.freq_range.range = range;
2915
2916 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2917
2918 os_free(range);
2919}
2920
2921
2922static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2923 u32 subcmd, u8 *data, size_t len)
2924{
2925 switch (subcmd) {
2926 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2927 qca_nl80211_avoid_freq(drv, data, len);
2928 break;
2929 default:
2930 wpa_printf(MSG_DEBUG,
2931 "nl80211: Ignore unsupported QCA vendor event %u",
2932 subcmd);
2933 break;
2934 }
2935}
2936
2937
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002938static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2939 struct nlattr **tb)
2940{
2941 u32 vendor_id, subcmd, wiphy = 0;
2942 int wiphy_idx;
2943 u8 *data = NULL;
2944 size_t len = 0;
2945
2946 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2947 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2948 return;
2949
2950 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2951 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2952
2953 if (tb[NL80211_ATTR_WIPHY])
2954 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2955
2956 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2957 wiphy, vendor_id, subcmd);
2958
2959 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2960 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2961 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2962 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2963 }
2964
2965 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2966 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2967 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2968 wiphy, wiphy_idx);
2969 return;
2970 }
2971
2972 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002973 case OUI_QCA:
2974 nl80211_vendor_event_qca(drv, subcmd, data, len);
2975 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002976 default:
2977 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2978 break;
2979 }
2980}
2981
2982
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002983static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
2984 struct nlattr *tb[])
2985{
2986 union wpa_event_data data;
2987 enum nl80211_reg_initiator init;
2988
2989 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2990
2991 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2992 return;
2993
2994 os_memset(&data, 0, sizeof(data));
2995 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
2996 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
2997 switch (init) {
2998 case NL80211_REGDOM_SET_BY_CORE:
2999 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
3000 break;
3001 case NL80211_REGDOM_SET_BY_USER:
3002 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
3003 break;
3004 case NL80211_REGDOM_SET_BY_DRIVER:
3005 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
3006 break;
3007 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3008 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
3009 break;
3010 }
3011
3012 if (tb[NL80211_ATTR_REG_TYPE]) {
3013 enum nl80211_reg_type type;
3014 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
3015 wpa_printf(MSG_DEBUG, " * type=%d", type);
3016 switch (type) {
3017 case NL80211_REGDOM_TYPE_COUNTRY:
3018 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
3019 break;
3020 case NL80211_REGDOM_TYPE_WORLD:
3021 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
3022 break;
3023 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
3024 data.channel_list_changed.type =
3025 REGDOM_TYPE_CUSTOM_WORLD;
3026 break;
3027 case NL80211_REGDOM_TYPE_INTERSECTION:
3028 data.channel_list_changed.type =
3029 REGDOM_TYPE_INTERSECTION;
3030 break;
3031 }
3032 }
3033
3034 if (tb[NL80211_ATTR_REG_ALPHA2]) {
3035 os_strlcpy(data.channel_list_changed.alpha2,
3036 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
3037 sizeof(data.channel_list_changed.alpha2));
3038 wpa_printf(MSG_DEBUG, " * alpha2=%s",
3039 data.channel_list_changed.alpha2);
3040 }
3041
3042 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
3043}
3044
3045
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003046static void do_process_drv_event(struct i802_bss *bss, int cmd,
3047 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003048{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003049 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003050 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003051
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003052 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
3053 cmd, nl80211_command_to_string(cmd), bss->ifname);
3054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003055 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
3056 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
3057 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003058 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003059 drv->ap_scan_as_station);
3060 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 }
3062
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003063 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003065 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003066 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003067 if (drv->scan_for_auth) {
3068 /*
3069 * Cannot indicate EVENT_SCAN_STARTED here since we skip
3070 * EVENT_SCAN_RESULTS in scan_for_auth case and the
3071 * upper layer implementation could get confused about
3072 * scanning state.
3073 */
3074 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3075 break;
3076 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003077 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003079 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003080 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003081 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003082 break;
3083 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003084 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003085 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003086 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3087 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003089 wpa_dbg(drv->ctx, MSG_DEBUG,
3090 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003091 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 drv->scan_complete_events = 1;
3093 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3094 drv->ctx);
3095 send_scan_event(drv, 0, tb);
3096 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003097 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003098 wpa_dbg(drv->ctx, MSG_DEBUG,
3099 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003100 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003101 send_scan_event(drv, 0, tb);
3102 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003104 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003105 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 /*
3107 * Need to indicate that scan results are available in order
3108 * not to make wpa_supplicant stop its scanning.
3109 */
3110 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3111 drv->ctx);
3112 send_scan_event(drv, 1, tb);
3113 break;
3114 case NL80211_CMD_AUTHENTICATE:
3115 case NL80211_CMD_ASSOCIATE:
3116 case NL80211_CMD_DEAUTHENTICATE:
3117 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 case NL80211_CMD_FRAME_TX_STATUS:
3119 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3120 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003121 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3123 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003124 tb[NL80211_ATTR_COOKIE],
3125 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 break;
3127 case NL80211_CMD_CONNECT:
3128 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003129 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130 tb[NL80211_ATTR_STATUS_CODE],
3131 tb[NL80211_ATTR_MAC],
3132 tb[NL80211_ATTR_REQ_IE],
3133 tb[NL80211_ATTR_RESP_IE]);
3134 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003135 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003136 mlme_event_ch_switch(drv,
3137 tb[NL80211_ATTR_IFINDEX],
3138 tb[NL80211_ATTR_WIPHY_FREQ],
3139 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3140 tb[NL80211_ATTR_CHANNEL_WIDTH],
3141 tb[NL80211_ATTR_CENTER_FREQ1],
3142 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003143 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003146 tb[NL80211_ATTR_MAC],
3147 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003148 break;
3149 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003150 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 break;
3152 case NL80211_CMD_JOIN_IBSS:
3153 mlme_event_join_ibss(drv, tb);
3154 break;
3155 case NL80211_CMD_REMAIN_ON_CHANNEL:
3156 mlme_event_remain_on_channel(drv, 0, tb);
3157 break;
3158 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3159 mlme_event_remain_on_channel(drv, 1, tb);
3160 break;
3161 case NL80211_CMD_NOTIFY_CQM:
3162 nl80211_cqm_event(drv, tb);
3163 break;
3164 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003165 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 break;
3167 case NL80211_CMD_REG_BEACON_HINT:
3168 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003169 os_memset(&data, 0, sizeof(data));
3170 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003171 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003172 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173 break;
3174 case NL80211_CMD_NEW_STATION:
3175 nl80211_new_station_event(drv, tb);
3176 break;
3177 case NL80211_CMD_DEL_STATION:
3178 nl80211_del_station_event(drv, tb);
3179 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003180 case NL80211_CMD_SET_REKEY_OFFLOAD:
3181 nl80211_rekey_offload_event(drv, tb);
3182 break;
3183 case NL80211_CMD_PMKSA_CANDIDATE:
3184 nl80211_pmksa_candidate_event(drv, tb);
3185 break;
3186 case NL80211_CMD_PROBE_CLIENT:
3187 nl80211_client_probe_event(drv, tb);
3188 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003189 case NL80211_CMD_TDLS_OPER:
3190 nl80211_tdls_oper_event(drv, tb);
3191 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003192 case NL80211_CMD_CONN_FAILED:
3193 nl80211_connect_failed_event(drv, tb);
3194 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003195 case NL80211_CMD_FT_EVENT:
3196 mlme_event_ft_event(drv, tb);
3197 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003198 case NL80211_CMD_RADAR_DETECT:
3199 nl80211_radar_event(drv, tb);
3200 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003201 case NL80211_CMD_STOP_AP:
3202 nl80211_stop_ap(drv, tb);
3203 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003204 case NL80211_CMD_VENDOR:
3205 nl80211_vendor_event(drv, tb);
3206 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003207 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003208 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3209 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003210 break;
3211 }
3212}
3213
3214
3215static int process_drv_event(struct nl_msg *msg, void *arg)
3216{
3217 struct wpa_driver_nl80211_data *drv = arg;
3218 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3219 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003220 struct i802_bss *bss;
3221 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003222
3223 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3224 genlmsg_attrlen(gnlh, 0), NULL);
3225
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003226 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003227 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3228
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003229 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003230 if (ifidx == -1 || ifidx == bss->ifindex) {
3231 do_process_drv_event(bss, gnlh->cmd, tb);
3232 return NL_SKIP;
3233 }
3234 wpa_printf(MSG_DEBUG,
3235 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3236 gnlh->cmd, ifidx);
3237 } else if (tb[NL80211_ATTR_WDEV]) {
3238 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3239 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003240 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003241 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3242 do_process_drv_event(bss, gnlh->cmd, tb);
3243 return NL_SKIP;
3244 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003245 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003246 wpa_printf(MSG_DEBUG,
3247 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3248 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003249 }
3250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003251 return NL_SKIP;
3252}
3253
3254
3255static int process_global_event(struct nl_msg *msg, void *arg)
3256{
3257 struct nl80211_global *global = arg;
3258 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3259 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003260 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003261 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003262 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003263 u64 wdev_id = 0;
3264 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003265
3266 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3267 genlmsg_attrlen(gnlh, 0), NULL);
3268
3269 if (tb[NL80211_ATTR_IFINDEX])
3270 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003271 else if (tb[NL80211_ATTR_WDEV]) {
3272 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3273 wdev_id_set = 1;
3274 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003275
Dmitry Shmidt04949592012-07-19 12:16:46 -07003276 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3277 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003278 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003279 if ((ifidx == -1 && !wdev_id_set) ||
3280 ifidx == bss->ifindex ||
3281 (wdev_id_set && bss->wdev_id_set &&
3282 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003283 do_process_drv_event(bss, gnlh->cmd, tb);
3284 return NL_SKIP;
3285 }
3286 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003287 }
3288
3289 return NL_SKIP;
3290}
3291
3292
3293static int process_bss_event(struct nl_msg *msg, void *arg)
3294{
3295 struct i802_bss *bss = arg;
3296 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3297 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3298
3299 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3300 genlmsg_attrlen(gnlh, 0), NULL);
3301
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003302 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3303 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3304 bss->ifname);
3305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003306 switch (gnlh->cmd) {
3307 case NL80211_CMD_FRAME:
3308 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003309 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3311 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003312 tb[NL80211_ATTR_COOKIE],
3313 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003314 break;
3315 case NL80211_CMD_UNEXPECTED_FRAME:
3316 nl80211_spurious_frame(bss, tb, 0);
3317 break;
3318 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3319 nl80211_spurious_frame(bss, tb, 1);
3320 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 default:
3322 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3323 "(cmd=%d)", gnlh->cmd);
3324 break;
3325 }
3326
3327 return NL_SKIP;
3328}
3329
3330
3331static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3332 void *handle)
3333{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003334 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003335 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003336
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003337 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003339 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07003340 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003341 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3342 __func__, res);
3343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344}
3345
3346
3347/**
3348 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3349 * @priv: driver_nl80211 private data
3350 * @alpha2_arg: country to which to switch to
3351 * Returns: 0 on success, -1 on failure
3352 *
3353 * This asks nl80211 to set the regulatory domain for given
3354 * country ISO / IEC alpha2.
3355 */
3356static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3357{
3358 struct i802_bss *bss = priv;
3359 struct wpa_driver_nl80211_data *drv = bss->drv;
3360 char alpha2[3];
3361 struct nl_msg *msg;
3362
3363 msg = nlmsg_alloc();
3364 if (!msg)
3365 return -ENOMEM;
3366
3367 alpha2[0] = alpha2_arg[0];
3368 alpha2[1] = alpha2_arg[1];
3369 alpha2[2] = '\0';
3370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003371 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
3373 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3374 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3375 return -EINVAL;
3376 return 0;
3377nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003378 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003379 return -EINVAL;
3380}
3381
3382
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003383static int nl80211_get_country(struct nl_msg *msg, void *arg)
3384{
3385 char *alpha2 = arg;
3386 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3387 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3388
3389 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3390 genlmsg_attrlen(gnlh, 0), NULL);
3391 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3392 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3393 return NL_SKIP;
3394 }
3395 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3396 return NL_SKIP;
3397}
3398
3399
3400static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3401{
3402 struct i802_bss *bss = priv;
3403 struct wpa_driver_nl80211_data *drv = bss->drv;
3404 struct nl_msg *msg;
3405 int ret;
3406
3407 msg = nlmsg_alloc();
3408 if (!msg)
3409 return -ENOMEM;
3410
3411 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3412 alpha2[0] = '\0';
3413 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3414 if (!alpha2[0])
3415 ret = -1;
3416
3417 return ret;
3418}
3419
3420
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003421static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3422{
3423 u32 *feat = arg;
3424 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3425 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3426
3427 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3428 genlmsg_attrlen(gnlh, 0), NULL);
3429
3430 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3431 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3432
3433 return NL_SKIP;
3434}
3435
3436
3437static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3438{
3439 u32 feat = 0;
3440 struct nl_msg *msg;
3441
3442 msg = nlmsg_alloc();
3443 if (!msg)
3444 goto nla_put_failure;
3445
3446 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3447 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3448 return feat;
3449
3450 msg = NULL;
3451nla_put_failure:
3452 nlmsg_free(msg);
3453 return 0;
3454}
3455
3456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003458 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003459 struct wpa_driver_capa *capa;
3460
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003461 unsigned int num_multichan_concurrent;
3462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003463 unsigned int error:1;
3464 unsigned int device_ap_sme:1;
3465 unsigned int poll_command_supported:1;
3466 unsigned int data_tx_status:1;
3467 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003468 unsigned int auth_supported:1;
3469 unsigned int connect_supported:1;
3470 unsigned int p2p_go_supported:1;
3471 unsigned int p2p_client_supported:1;
3472 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003473 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003474 unsigned int set_qos_map_supported:1;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003475 unsigned int have_low_prio_scan:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476};
3477
3478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003479static unsigned int probe_resp_offload_support(int supp_protocols)
3480{
3481 unsigned int prot = 0;
3482
3483 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3484 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3485 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3486 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3487 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3488 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3489 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3490 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3491
3492 return prot;
3493}
3494
3495
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003496static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3497 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003499 struct nlattr *nl_mode;
3500 int i;
3501
3502 if (tb == NULL)
3503 return;
3504
3505 nla_for_each_nested(nl_mode, tb, i) {
3506 switch (nla_type(nl_mode)) {
3507 case NL80211_IFTYPE_AP:
3508 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3509 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003510 case NL80211_IFTYPE_ADHOC:
3511 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3512 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003513 case NL80211_IFTYPE_P2P_DEVICE:
3514 info->capa->flags |=
3515 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3516 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003517 case NL80211_IFTYPE_P2P_GO:
3518 info->p2p_go_supported = 1;
3519 break;
3520 case NL80211_IFTYPE_P2P_CLIENT:
3521 info->p2p_client_supported = 1;
3522 break;
3523 case NL80211_IFTYPE_MONITOR:
3524 info->monitor_supported = 1;
3525 break;
3526 }
3527 }
3528}
3529
3530
3531static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3532 struct nlattr *nl_combi)
3533{
3534 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3535 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3536 struct nlattr *nl_limit, *nl_mode;
3537 int err, rem_limit, rem_mode;
3538 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003539 static struct nla_policy
3540 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3541 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3542 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3543 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3544 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003545 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003546 },
3547 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3548 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3549 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3550 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003552 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3553 nl_combi, iface_combination_policy);
3554 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3555 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3556 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3557 return 0; /* broken combination */
3558
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003559 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3560 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3561
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003562 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3563 rem_limit) {
3564 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3565 nl_limit, iface_limit_policy);
3566 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3567 return 0; /* broken combination */
3568
3569 nla_for_each_nested(nl_mode,
3570 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3571 rem_mode) {
3572 int ift = nla_type(nl_mode);
3573 if (ift == NL80211_IFTYPE_P2P_GO ||
3574 ift == NL80211_IFTYPE_P2P_CLIENT)
3575 combination_has_p2p = 1;
3576 if (ift == NL80211_IFTYPE_STATION)
3577 combination_has_mgd = 1;
3578 }
3579 if (combination_has_p2p && combination_has_mgd)
3580 break;
3581 }
3582
3583 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003584 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003585 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003586
3587 info->p2p_concurrent = 1;
3588 if (info->num_multichan_concurrent < num_channels)
3589 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003590 }
3591
3592 return 0;
3593}
3594
3595
3596static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3597 struct nlattr *tb)
3598{
3599 struct nlattr *nl_combi;
3600 int rem_combi;
3601
3602 if (tb == NULL)
3603 return;
3604
3605 nla_for_each_nested(nl_combi, tb, rem_combi) {
3606 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3607 break;
3608 }
3609}
3610
3611
3612static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3613 struct nlattr *tb)
3614{
3615 struct nlattr *nl_cmd;
3616 int i;
3617
3618 if (tb == NULL)
3619 return;
3620
3621 nla_for_each_nested(nl_cmd, tb, i) {
3622 switch (nla_get_u32(nl_cmd)) {
3623 case NL80211_CMD_AUTHENTICATE:
3624 info->auth_supported = 1;
3625 break;
3626 case NL80211_CMD_CONNECT:
3627 info->connect_supported = 1;
3628 break;
3629 case NL80211_CMD_START_SCHED_SCAN:
3630 info->capa->sched_scan_supported = 1;
3631 break;
3632 case NL80211_CMD_PROBE_CLIENT:
3633 info->poll_command_supported = 1;
3634 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003635 case NL80211_CMD_CHANNEL_SWITCH:
3636 info->channel_switch_supported = 1;
3637 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003638 case NL80211_CMD_SET_QOS_MAP:
3639 info->set_qos_map_supported = 1;
3640 break;
3641 }
3642 }
3643}
3644
3645
3646static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3647 struct nlattr *tb)
3648{
3649 int i, num;
3650 u32 *ciphers;
3651
3652 if (tb == NULL)
3653 return;
3654
3655 num = nla_len(tb) / sizeof(u32);
3656 ciphers = nla_data(tb);
3657 for (i = 0; i < num; i++) {
3658 u32 c = ciphers[i];
3659
3660 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3661 c >> 24, (c >> 16) & 0xff,
3662 (c >> 8) & 0xff, c & 0xff);
3663 switch (c) {
3664 case WLAN_CIPHER_SUITE_CCMP_256:
3665 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3666 break;
3667 case WLAN_CIPHER_SUITE_GCMP_256:
3668 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3669 break;
3670 case WLAN_CIPHER_SUITE_CCMP:
3671 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3672 break;
3673 case WLAN_CIPHER_SUITE_GCMP:
3674 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3675 break;
3676 case WLAN_CIPHER_SUITE_TKIP:
3677 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3678 break;
3679 case WLAN_CIPHER_SUITE_WEP104:
3680 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3681 break;
3682 case WLAN_CIPHER_SUITE_WEP40:
3683 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3684 break;
3685 case WLAN_CIPHER_SUITE_AES_CMAC:
3686 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3687 break;
3688 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3689 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3690 break;
3691 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3692 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3693 break;
3694 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3695 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3696 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003697 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3698 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3699 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003700 }
3701 }
3702}
3703
3704
3705static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3706 struct nlattr *tb)
3707{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003708 if (tb)
3709 capa->max_remain_on_chan = nla_get_u32(tb);
3710}
3711
3712
3713static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3714 struct nlattr *ext_setup)
3715{
3716 if (tdls == NULL)
3717 return;
3718
3719 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3720 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3721
3722 if (ext_setup) {
3723 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3724 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3725 }
3726}
3727
3728
3729static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3730 struct nlattr *tb)
3731{
3732 u32 flags;
3733 struct wpa_driver_capa *capa = info->capa;
3734
3735 if (tb == NULL)
3736 return;
3737
3738 flags = nla_get_u32(tb);
3739
3740 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3741 info->data_tx_status = 1;
3742
3743 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3744 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3745
3746 if (flags & NL80211_FEATURE_SAE)
3747 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3748
3749 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3750 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003751
3752 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3753 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003754
3755 if (flags & NL80211_FEATURE_LOW_PRIORITY_SCAN)
3756 info->have_low_prio_scan = 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003757}
3758
3759
3760static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3761 struct nlattr *tb)
3762{
3763 u32 protocols;
3764
3765 if (tb == NULL)
3766 return;
3767
3768 protocols = nla_get_u32(tb);
3769 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3770 "mode");
3771 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3772 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3773}
3774
3775
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003776static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3777 struct nlattr *tb)
3778{
3779 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3780
3781 if (tb == NULL)
3782 return;
3783
3784 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3785 tb, NULL))
3786 return;
3787
3788 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3789 capa->wowlan_triggers.any = 1;
3790 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3791 capa->wowlan_triggers.disconnect = 1;
3792 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3793 capa->wowlan_triggers.magic_pkt = 1;
3794 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3795 capa->wowlan_triggers.gtk_rekey_failure = 1;
3796 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3797 capa->wowlan_triggers.eap_identity_req = 1;
3798 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3799 capa->wowlan_triggers.four_way_handshake = 1;
3800 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3801 capa->wowlan_triggers.rfkill_release = 1;
3802}
3803
3804
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003805static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3806{
3807 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3808 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3809 struct wiphy_info_data *info = arg;
3810 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003811 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3814 genlmsg_attrlen(gnlh, 0), NULL);
3815
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003816 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003817 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003818 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3819 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003821 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3823
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003824 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3825 capa->max_sched_scan_ssids =
3826 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3827
3828 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3829 capa->max_match_sets =
3830 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3831
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003832 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3833 capa->max_acl_mac_addrs =
3834 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3835
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003836 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3837 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3838 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003839 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003841 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3842 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3843 "off-channel TX");
3844 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3845 }
3846
3847 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3848 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3849 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3850 }
3851
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003852 wiphy_info_max_roc(capa,
3853 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003854
3855 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3856 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003858 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3859 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003860
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003861 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3862 info->device_ap_sme = 1;
3863
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003864 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3865 wiphy_info_probe_resp_offload(capa,
3866 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003867
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003868 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3869 drv->extended_capa == NULL) {
3870 drv->extended_capa =
3871 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3872 if (drv->extended_capa) {
3873 os_memcpy(drv->extended_capa,
3874 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3875 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3876 drv->extended_capa_len =
3877 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3878 }
3879 drv->extended_capa_mask =
3880 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3881 if (drv->extended_capa_mask) {
3882 os_memcpy(drv->extended_capa_mask,
3883 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3884 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3885 } else {
3886 os_free(drv->extended_capa);
3887 drv->extended_capa = NULL;
3888 drv->extended_capa_len = 0;
3889 }
3890 }
3891
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003892 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3893 struct nlattr *nl;
3894 int rem;
3895
3896 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3897 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003898 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003899 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3900 continue;
3901 }
3902 vinfo = nla_data(nl);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003903 switch (vinfo->subcmd) {
3904 case QCA_NL80211_VENDOR_SUBCMD_ROAMING:
3905 drv->roaming_vendor_cmd_avail = 1;
3906 break;
3907 case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003908 drv->dfs_vendor_cmd_avail = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003909 break;
3910 }
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003911
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003912 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3913 vinfo->vendor_id, vinfo->subcmd);
3914 }
3915 }
3916
3917 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3918 struct nlattr *nl;
3919 int rem;
3920
3921 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3922 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003923 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003924 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3925 continue;
3926 }
3927 vinfo = nla_data(nl);
3928 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3929 vinfo->vendor_id, vinfo->subcmd);
3930 }
3931 }
3932
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003933 wiphy_info_wowlan_triggers(capa,
3934 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
3935
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003936 if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
3937 capa->max_stations =
3938 nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
3939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003940 return NL_SKIP;
3941}
3942
3943
3944static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3945 struct wiphy_info_data *info)
3946{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003947 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 struct nl_msg *msg;
3949
3950 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003952 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953
3954 msg = nlmsg_alloc();
3955 if (!msg)
3956 return -1;
3957
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003958 feat = get_nl80211_protocol_features(drv);
3959 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3960 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3961 else
3962 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003964 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003965 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003966 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003968 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3969 return -1;
3970
3971 if (info->auth_supported)
3972 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3973 else if (!info->connect_supported) {
3974 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3975 "authentication/association or connect commands");
3976 info->error = 1;
3977 }
3978
3979 if (info->p2p_go_supported && info->p2p_client_supported)
3980 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3981 if (info->p2p_concurrent) {
3982 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3983 "interface (driver advertised support)");
3984 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3985 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3986 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003987 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003988 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3989 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003990 drv->capa.num_multichan_concurrent =
3991 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003992 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003993
3994 /* default to 5000 since early versions of mac80211 don't set it */
3995 if (!drv->capa.max_remain_on_chan)
3996 drv->capa.max_remain_on_chan = 5000;
3997
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003998 if (info->channel_switch_supported)
3999 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
4000
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004001 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004002nla_put_failure:
4003 nlmsg_free(msg);
4004 return -1;
4005}
4006
4007
4008static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
4009{
4010 struct wiphy_info_data info;
4011 if (wpa_driver_nl80211_get_info(drv, &info))
4012 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004013
4014 if (info.error)
4015 return -1;
4016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4019 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4020 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4021 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
4023 WPA_DRIVER_AUTH_SHARED |
4024 WPA_DRIVER_AUTH_LEAP;
4025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004026 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
4027 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004029
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004030 /*
4031 * As all cfg80211 drivers must support cases where the AP interface is
4032 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
4033 * case that the user space daemon has crashed, they must be able to
4034 * cleanup all stations and key entries in the AP tear down flow. Thus,
4035 * this flag can/should always be set for cfg80211 drivers.
4036 */
4037 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
4038
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004039 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004040 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004041
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004042 /*
4043 * No AP SME is currently assumed to also indicate no AP MLME
4044 * in the driver/firmware.
4045 */
4046 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
4047 }
4048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004049 drv->device_ap_sme = info.device_ap_sme;
4050 drv->poll_command_supported = info.poll_command_supported;
4051 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004052 if (info.set_qos_map_supported)
4053 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004054 drv->have_low_prio_scan = info.have_low_prio_scan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004055
4056 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004057 * If poll command and tx status are supported, mac80211 is new enough
4058 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004059 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004060 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061
4062 if (drv->device_ap_sme && drv->use_monitor) {
4063 /*
4064 * Non-mac80211 drivers may not support monitor interface.
4065 * Make sure we do not get stuck with incorrect capability here
4066 * by explicitly testing this.
4067 */
4068 if (!info.monitor_supported) {
4069 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
4070 "with device_ap_sme since no monitor mode "
4071 "support detected");
4072 drv->use_monitor = 0;
4073 }
4074 }
4075
4076 /*
4077 * If we aren't going to use monitor interfaces, but the
4078 * driver doesn't support data TX status, we won't get TX
4079 * status for EAPOL frames.
4080 */
4081 if (!drv->use_monitor && !info.data_tx_status)
4082 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004083
4084 return 0;
4085}
4086
4087
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004088#ifdef ANDROID
4089static int android_genl_ctrl_resolve(struct nl_handle *handle,
4090 const char *name)
4091{
4092 /*
4093 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4094 * need to work around that.
4095 */
4096 struct nl_cache *cache = NULL;
4097 struct genl_family *nl80211 = NULL;
4098 int id = -1;
4099
4100 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4101 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4102 "netlink cache");
4103 goto fail;
4104 }
4105
4106 nl80211 = genl_ctrl_search_by_name(cache, name);
4107 if (nl80211 == NULL)
4108 goto fail;
4109
4110 id = genl_family_get_id(nl80211);
4111
4112fail:
4113 if (nl80211)
4114 genl_family_put(nl80211);
4115 if (cache)
4116 nl_cache_free(cache);
4117
4118 return id;
4119}
4120#define genl_ctrl_resolve android_genl_ctrl_resolve
4121#endif /* ANDROID */
4122
4123
4124static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125{
4126 int ret;
4127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004128 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4129 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4131 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004132 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 }
4134
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004135 global->nl = nl_create_handle(global->nl_cb, "nl");
4136 if (global->nl == NULL)
4137 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004139 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4140 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4142 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 }
4145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004146 global->nl_event = nl_create_handle(global->nl_cb, "event");
4147 if (global->nl_event == NULL)
4148 goto err;
4149
4150 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004152 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153 if (ret < 0) {
4154 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4155 "membership for scan events: %d (%s)",
4156 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004157 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 }
4159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004160 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004162 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163 if (ret < 0) {
4164 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4165 "membership for mlme events: %d (%s)",
4166 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004167 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 }
4169
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004170 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004172 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 if (ret < 0) {
4174 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4175 "membership for regulatory events: %d (%s)",
4176 ret, strerror(-ret));
4177 /* Continue without regulatory events */
4178 }
4179
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004180 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4181 if (ret >= 0)
4182 ret = nl_socket_add_membership(global->nl_event, ret);
4183 if (ret < 0) {
4184 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4185 "membership for vendor events: %d (%s)",
4186 ret, strerror(-ret));
4187 /* Continue without vendor events */
4188 }
4189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004190 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4191 no_seq_check, NULL);
4192 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4193 process_global_event, global);
4194
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004195 nl80211_register_eloop_read(&global->nl_event,
4196 wpa_driver_nl80211_event_receive,
4197 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198
4199 return 0;
4200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004201err:
4202 nl_destroy_handles(&global->nl_event);
4203 nl_destroy_handles(&global->nl);
4204 nl_cb_put(global->nl_cb);
4205 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004206 return -1;
4207}
4208
4209
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004210static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4211{
4212 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4213 if (!drv->nl_cb) {
4214 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4215 return -1;
4216 }
4217
4218 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4219 no_seq_check, NULL);
4220 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4221 process_drv_event, drv);
4222
4223 return 0;
4224}
4225
4226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4228{
4229 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4230 /*
4231 * This may be for any interface; use ifdown event to disable
4232 * interface.
4233 */
4234}
4235
4236
4237static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4238{
4239 struct wpa_driver_nl80211_data *drv = ctx;
4240 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004241 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4243 "after rfkill unblock");
4244 return;
4245 }
4246 /* rtnetlink ifup handler will report interface as enabled */
4247}
4248
4249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004250static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4251 void *eloop_ctx,
4252 void *handle)
4253{
4254 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4255 u8 data[2048];
4256 struct msghdr msg;
4257 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004258 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004259 struct cmsghdr *cmsg;
4260 int res, found_ee = 0, found_wifi = 0, acked = 0;
4261 union wpa_event_data event;
4262
4263 memset(&msg, 0, sizeof(msg));
4264 msg.msg_iov = &entry;
4265 msg.msg_iovlen = 1;
4266 entry.iov_base = data;
4267 entry.iov_len = sizeof(data);
4268 msg.msg_control = &control;
4269 msg.msg_controllen = sizeof(control);
4270
4271 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4272 /* if error or not fitting 802.3 header, return */
4273 if (res < 14)
4274 return;
4275
4276 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4277 {
4278 if (cmsg->cmsg_level == SOL_SOCKET &&
4279 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4280 int *ack;
4281
4282 found_wifi = 1;
4283 ack = (void *)CMSG_DATA(cmsg);
4284 acked = *ack;
4285 }
4286
4287 if (cmsg->cmsg_level == SOL_PACKET &&
4288 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4289 struct sock_extended_err *err =
4290 (struct sock_extended_err *)CMSG_DATA(cmsg);
4291
4292 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4293 found_ee = 1;
4294 }
4295 }
4296
4297 if (!found_ee || !found_wifi)
4298 return;
4299
4300 memset(&event, 0, sizeof(event));
4301 event.eapol_tx_status.dst = data;
4302 event.eapol_tx_status.data = data + 14;
4303 event.eapol_tx_status.data_len = res - 14;
4304 event.eapol_tx_status.ack = acked;
4305 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4306}
4307
4308
4309static int nl80211_init_bss(struct i802_bss *bss)
4310{
4311 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4312 if (!bss->nl_cb)
4313 return -1;
4314
4315 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4316 no_seq_check, NULL);
4317 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4318 process_bss_event, bss);
4319
4320 return 0;
4321}
4322
4323
4324static void nl80211_destroy_bss(struct i802_bss *bss)
4325{
4326 nl_cb_put(bss->nl_cb);
4327 bss->nl_cb = NULL;
4328}
4329
4330
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004331static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4332 void *global_priv, int hostapd,
4333 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334{
4335 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 struct rfkill_config *rcfg;
4337 struct i802_bss *bss;
4338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339 if (global_priv == NULL)
4340 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 drv = os_zalloc(sizeof(*drv));
4342 if (drv == NULL)
4343 return NULL;
4344 drv->global = global_priv;
4345 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004346 drv->hostapd = !!hostapd;
4347 drv->eapol_sock = -1;
4348 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4349 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004350
4351 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4352 if (!drv->first_bss) {
4353 os_free(drv);
4354 return NULL;
4355 }
4356 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004358 bss->ctx = ctx;
4359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004360 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4361 drv->monitor_ifidx = -1;
4362 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004363 drv->eapol_tx_sock = -1;
4364 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365
4366 if (wpa_driver_nl80211_init_nl(drv)) {
4367 os_free(drv);
4368 return NULL;
4369 }
4370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004371 if (nl80211_init_bss(bss))
4372 goto failed;
4373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004374 rcfg = os_zalloc(sizeof(*rcfg));
4375 if (rcfg == NULL)
4376 goto failed;
4377 rcfg->ctx = drv;
4378 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4379 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4380 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4381 drv->rfkill = rfkill_init(rcfg);
4382 if (drv->rfkill == NULL) {
4383 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4384 os_free(rcfg);
4385 }
4386
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004387 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4388 drv->start_iface_up = 1;
4389
4390 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 goto failed;
4392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004393 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4394 if (drv->eapol_tx_sock < 0)
4395 goto failed;
4396
4397 if (drv->data_tx_status) {
4398 int enabled = 1;
4399
4400 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4401 &enabled, sizeof(enabled)) < 0) {
4402 wpa_printf(MSG_DEBUG,
4403 "nl80211: wifi status sockopt failed\n");
4404 drv->data_tx_status = 0;
4405 if (!drv->use_monitor)
4406 drv->capa.flags &=
4407 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4408 } else {
4409 eloop_register_read_sock(drv->eapol_tx_sock,
4410 wpa_driver_nl80211_handle_eapol_tx_status,
4411 drv, NULL);
4412 }
4413 }
4414
4415 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004417 drv->in_interface_list = 1;
4418 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419
4420 return bss;
4421
4422failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004423 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 return NULL;
4425}
4426
4427
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004428/**
4429 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4430 * @ctx: context to be used when calling wpa_supplicant functions,
4431 * e.g., wpa_supplicant_event()
4432 * @ifname: interface name, e.g., wlan0
4433 * @global_priv: private driver global data from global_init()
4434 * Returns: Pointer to private data, %NULL on failure
4435 */
4436static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4437 void *global_priv)
4438{
4439 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4440}
4441
4442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004443static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 struct nl_handle *nl_handle,
4445 u16 type, const u8 *match, size_t match_len)
4446{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004447 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 struct nl_msg *msg;
4449 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004450 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451
4452 msg = nlmsg_alloc();
4453 if (!msg)
4454 return -1;
4455
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004456 buf[0] = '\0';
4457 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004458 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
4459 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004461 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4462
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004463 if (nl80211_set_iface_id(msg, bss) < 0)
4464 goto nla_put_failure;
4465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4467 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004469 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 msg = NULL;
4471 if (ret) {
4472 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4473 "failed (type=%u): ret=%d (%s)",
4474 type, ret, strerror(-ret));
4475 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4476 match, match_len);
4477 goto nla_put_failure;
4478 }
4479 ret = 0;
4480nla_put_failure:
4481 nlmsg_free(msg);
4482 return ret;
4483}
4484
4485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004486static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4487{
4488 struct wpa_driver_nl80211_data *drv = bss->drv;
4489
4490 if (bss->nl_mgmt) {
4491 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4492 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4493 return -1;
4494 }
4495
4496 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4497 if (bss->nl_mgmt == NULL)
4498 return -1;
4499
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004500 return 0;
4501}
4502
4503
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004504static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4505{
4506 nl80211_register_eloop_read(&bss->nl_mgmt,
4507 wpa_driver_nl80211_event_receive,
4508 bss->nl_cb);
4509}
4510
4511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004512static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 const u8 *match, size_t match_len)
4514{
4515 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004516 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 type, match, match_len);
4518}
4519
4520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004521static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004523 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004524 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004525
4526 if (nl80211_alloc_mgmt_handle(bss))
4527 return -1;
4528 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4529 "handle %p", bss->nl_mgmt);
4530
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004531 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4532 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4533
4534 /* register for any AUTH message */
4535 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4536 }
4537
Dmitry Shmidt051af732013-10-22 13:52:46 -07004538#ifdef CONFIG_INTERWORKING
4539 /* QoS Map Configure */
4540 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004541 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004542#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004543#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004545 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004546 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004548 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004549 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004551 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004552 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004554 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004555 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004556 /* Protected GAS Initial Request */
4557 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4558 ret = -1;
4559 /* Protected GAS Initial Response */
4560 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4561 ret = -1;
4562 /* Protected GAS Comeback Request */
4563 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4564 ret = -1;
4565 /* Protected GAS Comeback Response */
4566 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4567 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004568#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4569#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004571 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4573 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004574 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004576 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 (u8 *) "\x7f\x50\x6f\x9a\x09",
4578 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004579 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580#endif /* CONFIG_P2P */
4581#ifdef CONFIG_IEEE80211W
4582 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004583 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004584 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004586#ifdef CONFIG_TDLS
4587 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4588 /* TDLS Discovery Response */
4589 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4590 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004591 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004592 }
4593#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594
4595 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004596 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004597 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 else
4599 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4600 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4601
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004602 /* WNM - BSS Transition Management Request */
4603 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004604 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004605 /* WNM-Sleep Mode Response */
4606 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004607 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004608
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004609#ifdef CONFIG_HS20
4610 /* WNM-Notification */
4611 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004612 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004613#endif /* CONFIG_HS20 */
4614
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004615 nl80211_mgmt_handle_register_eloop(bss);
4616
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004617 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618}
4619
4620
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004621static int nl80211_register_spurious_class3(struct i802_bss *bss)
4622{
4623 struct wpa_driver_nl80211_data *drv = bss->drv;
4624 struct nl_msg *msg;
4625 int ret = -1;
4626
4627 msg = nlmsg_alloc();
4628 if (!msg)
4629 return -1;
4630
4631 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4632
4633 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4634
4635 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4636 msg = NULL;
4637 if (ret) {
4638 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4639 "failed: ret=%d (%s)",
4640 ret, strerror(-ret));
4641 goto nla_put_failure;
4642 }
4643 ret = 0;
4644nla_put_failure:
4645 nlmsg_free(msg);
4646 return ret;
4647}
4648
4649
4650static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4651{
4652 static const int stypes[] = {
4653 WLAN_FC_STYPE_AUTH,
4654 WLAN_FC_STYPE_ASSOC_REQ,
4655 WLAN_FC_STYPE_REASSOC_REQ,
4656 WLAN_FC_STYPE_DISASSOC,
4657 WLAN_FC_STYPE_DEAUTH,
4658 WLAN_FC_STYPE_ACTION,
4659 WLAN_FC_STYPE_PROBE_REQ,
4660/* Beacon doesn't work as mac80211 doesn't currently allow
4661 * it, but it wouldn't really be the right thing anyway as
4662 * it isn't per interface ... maybe just dump the scan
4663 * results periodically for OLBC?
4664 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004665 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004666 };
4667 unsigned int i;
4668
4669 if (nl80211_alloc_mgmt_handle(bss))
4670 return -1;
4671 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4672 "handle %p", bss->nl_mgmt);
4673
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004674 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004675 if (nl80211_register_frame(bss, bss->nl_mgmt,
4676 (WLAN_FC_TYPE_MGMT << 2) |
4677 (stypes[i] << 4),
4678 NULL, 0) < 0) {
4679 goto out_err;
4680 }
4681 }
4682
4683 if (nl80211_register_spurious_class3(bss))
4684 goto out_err;
4685
4686 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4687 goto out_err;
4688
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004689 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004690 return 0;
4691
4692out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004693 nl_destroy_handles(&bss->nl_mgmt);
4694 return -1;
4695}
4696
4697
4698static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4699{
4700 if (nl80211_alloc_mgmt_handle(bss))
4701 return -1;
4702 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4703 "handle %p (device SME)", bss->nl_mgmt);
4704
4705 if (nl80211_register_frame(bss, bss->nl_mgmt,
4706 (WLAN_FC_TYPE_MGMT << 2) |
4707 (WLAN_FC_STYPE_ACTION << 4),
4708 NULL, 0) < 0)
4709 goto out_err;
4710
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004711 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004712 return 0;
4713
4714out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004715 nl_destroy_handles(&bss->nl_mgmt);
4716 return -1;
4717}
4718
4719
4720static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4721{
4722 if (bss->nl_mgmt == NULL)
4723 return;
4724 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4725 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004726 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004727
4728 nl80211_put_wiphy_data_ap(bss);
4729}
4730
4731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4733{
4734 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4735}
4736
4737
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004738static void nl80211_del_p2pdev(struct i802_bss *bss)
4739{
4740 struct wpa_driver_nl80211_data *drv = bss->drv;
4741 struct nl_msg *msg;
4742 int ret;
4743
4744 msg = nlmsg_alloc();
4745 if (!msg)
4746 return;
4747
4748 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4749 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4750
4751 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4752 msg = NULL;
4753
4754 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4755 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004756 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004757
4758nla_put_failure:
4759 nlmsg_free(msg);
4760}
4761
4762
4763static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4764{
4765 struct wpa_driver_nl80211_data *drv = bss->drv;
4766 struct nl_msg *msg;
4767 int ret = -1;
4768
4769 msg = nlmsg_alloc();
4770 if (!msg)
4771 return -1;
4772
4773 if (start)
4774 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4775 else
4776 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4777
4778 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4779
4780 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4781 msg = NULL;
4782
4783 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4784 start ? "Start" : "Stop",
4785 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004786 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004787
4788nla_put_failure:
4789 nlmsg_free(msg);
4790 return ret;
4791}
4792
4793
4794static int i802_set_iface_flags(struct i802_bss *bss, int up)
4795{
4796 enum nl80211_iftype nlmode;
4797
4798 nlmode = nl80211_get_ifmode(bss);
4799 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4800 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4801 bss->ifname, up);
4802 }
4803
4804 /* P2P Device has start/stop which is equivalent */
4805 return nl80211_set_p2pdev(bss, up);
4806}
4807
4808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004810wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4811 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004813 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004815 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816
4817 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004818 bss->ifindex = drv->ifindex;
4819 bss->wdev_id = drv->global->if_add_wdevid;
4820 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4821
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004822 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4823 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004824 drv->global->if_add_wdevid_set = 0;
4825
Dmitry Shmidt03658832014-08-13 11:03:49 -07004826 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4827 bss->static_ap = 1;
4828
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004829 if (wpa_driver_nl80211_capa(drv))
4830 return -1;
4831
4832 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4833 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004835 if (set_addr &&
4836 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4837 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4838 set_addr)))
4839 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004840
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004841 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4842 drv->start_mode_ap = 1;
4843
Dmitry Shmidt03658832014-08-13 11:03:49 -07004844 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004845 nlmode = NL80211_IFTYPE_AP;
4846 else if (bss->if_dynamic)
4847 nlmode = nl80211_get_ifmode(bss);
4848 else
4849 nlmode = NL80211_IFTYPE_STATION;
4850
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004851 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004852 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004853 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004855
Dmitry Shmidt98660862014-03-11 17:26:21 -07004856 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004857 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858
Dmitry Shmidt98660862014-03-11 17:26:21 -07004859 if (!rfkill_is_blocked(drv->rfkill)) {
4860 int ret = i802_set_iface_flags(bss, 1);
4861 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4863 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004864 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004866 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4867 return ret;
4868 } else {
4869 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4870 "interface '%s' due to rfkill", bss->ifname);
4871 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4872 return 0;
4873 drv->if_disabled = 1;
4874 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 }
4876
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004877 if (!drv->hostapd)
4878 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4879 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004881 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4882 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004884 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004886 if (send_rfkill_event) {
4887 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4888 drv, drv->ctx);
4889 }
4890
4891 return 0;
4892}
4893
4894
4895static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4896{
4897 struct nl_msg *msg;
4898
4899 msg = nlmsg_alloc();
4900 if (!msg)
4901 return -ENOMEM;
4902
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004903 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4904 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004905 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4907
4908 return send_and_recv_msgs(drv, msg, NULL, NULL);
4909 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004910 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 return -ENOBUFS;
4912}
4913
4914
4915/**
4916 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004917 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 *
4919 * Shut down driver interface and processing of driver events. Free
4920 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4921 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004922static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 struct wpa_driver_nl80211_data *drv = bss->drv;
4925
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004927 if (drv->data_tx_status)
4928 eloop_unregister_read_sock(drv->eapol_tx_sock);
4929 if (drv->eapol_tx_sock >= 0)
4930 close(drv->eapol_tx_sock);
4931
4932 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 wpa_driver_nl80211_probe_req_report(bss, 0);
4934 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4936 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4938 "interface %s from bridge %s: %s",
4939 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004940 if (drv->rtnl_sk)
4941 nl_socket_free(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 }
4943 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004944 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4946 "bridge %s: %s",
4947 bss->brname, strerror(errno));
4948 }
4949
4950 nl80211_remove_monitor_interface(drv);
4951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 wpa_driver_nl80211_del_beacon(drv);
4954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 if (drv->eapol_sock >= 0) {
4956 eloop_unregister_read_sock(drv->eapol_sock);
4957 close(drv->eapol_sock);
4958 }
4959
4960 if (drv->if_indices != drv->default_if_indices)
4961 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004963 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4965
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004966 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4967 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004968 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004969 rfkill_deinit(drv->rfkill);
4970
4971 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4972
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004973 if (!drv->start_iface_up)
4974 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004975
4976 if (drv->addr_changed) {
4977 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
4978 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4979 drv->perm_addr) < 0) {
4980 wpa_printf(MSG_DEBUG,
4981 "nl80211: Could not restore permanent MAC address");
4982 }
4983 }
4984
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004985 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004986 if (!drv->hostapd || !drv->start_mode_ap)
4987 wpa_driver_nl80211_set_mode(bss,
4988 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004989 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004990 } else {
4991 nl80211_mgmt_unsubscribe(bss, "deinit");
4992 nl80211_del_p2pdev(bss);
4993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994 nl_cb_put(drv->nl_cb);
4995
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004996 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 os_free(drv->filter_ssids);
4999
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005000 os_free(drv->auth_ie);
5001
5002 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 dl_list_del(&drv->list);
5004
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005005 os_free(drv->extended_capa);
5006 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005007 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 os_free(drv);
5009}
5010
5011
5012/**
5013 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
5014 * @eloop_ctx: Driver private data
5015 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
5016 *
5017 * This function can be used as registered timeout when starting a scan to
5018 * generate a scan completed event if the driver does not report this.
5019 */
5020static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
5021{
5022 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005023 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005024 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005025 drv->ap_scan_as_station);
5026 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027 }
5028 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
5029 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
5030}
5031
5032
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005033static struct nl_msg *
5034nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005035 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005036{
5037 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005038 size_t i;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005039 u32 scan_flags = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005040
5041 msg = nlmsg_alloc();
5042 if (!msg)
5043 return NULL;
5044
5045 nl80211_cmd(drv, msg, 0, cmd);
5046
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005047 if (!wdev_id)
5048 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5049 else
5050 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005051
5052 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005053 struct nlattr *ssids;
5054
5055 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005056 if (ssids == NULL)
5057 goto fail;
5058 for (i = 0; i < params->num_ssids; i++) {
5059 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
5060 params->ssids[i].ssid,
5061 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005062 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
5063 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005064 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005065 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005066 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005067 }
5068
5069 if (params->extra_ies) {
5070 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
5071 params->extra_ies, params->extra_ies_len);
5072 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
5073 params->extra_ies) < 0)
5074 goto fail;
5075 }
5076
5077 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005078 struct nlattr *freqs;
5079 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005080 if (freqs == NULL)
5081 goto fail;
5082 for (i = 0; params->freqs[i]; i++) {
5083 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
5084 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005085 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005086 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005087 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005088 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005089 }
5090
5091 os_free(drv->filter_ssids);
5092 drv->filter_ssids = params->filter_ssids;
5093 params->filter_ssids = NULL;
5094 drv->num_filter_ssids = params->num_filter_ssids;
5095
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005096 if (params->only_new_results) {
5097 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005098 scan_flags |= NL80211_SCAN_FLAG_FLUSH;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005099 }
5100
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005101 if (params->low_priority && drv->have_low_prio_scan) {
5102 wpa_printf(MSG_DEBUG,
5103 "nl80211: Add NL80211_SCAN_FLAG_LOW_PRIORITY");
5104 scan_flags |= NL80211_SCAN_FLAG_LOW_PRIORITY;
5105 }
5106
5107 if (scan_flags)
5108 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags);
5109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005110 return msg;
5111
5112fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005113nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005114 nlmsg_free(msg);
5115 return NULL;
5116}
5117
5118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119/**
5120 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005121 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 * @params: Scan parameters
5123 * Returns: 0 on success, -1 on failure
5124 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005125static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126 struct wpa_driver_scan_params *params)
5127{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005129 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005130 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005132 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005133 drv->scan_for_auth = 0;
5134
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005135 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5136 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005137 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005140 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005141 struct nlattr *rates;
5142
Dmitry Shmidt04949592012-07-19 12:16:46 -07005143 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5144
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005145 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005146 if (rates == NULL)
5147 goto nla_put_failure;
5148
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005149 /*
5150 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5151 * by masking out everything else apart from the OFDM rates 6,
5152 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5153 * rates are left enabled.
5154 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005155 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005156 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005157 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005158
5159 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
5160 }
5161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5163 msg = NULL;
5164 if (ret) {
5165 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5166 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005167 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005168 enum nl80211_iftype old_mode = drv->nlmode;
5169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 /*
5171 * mac80211 does not allow scan requests in AP mode, so
5172 * try to do this in station mode.
5173 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174 if (wpa_driver_nl80211_set_mode(
5175 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176 goto nla_put_failure;
5177
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005178 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005179 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 goto nla_put_failure;
5181 }
5182
5183 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005184 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 ret = 0;
5186 } else
5187 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188 }
5189
Dmitry Shmidt56052862013-10-04 10:23:25 -07005190 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005191 /* Not all drivers generate "scan completed" wireless event, so try to
5192 * read results after a timeout. */
5193 timeout = 10;
5194 if (drv->scan_complete_events) {
5195 /*
5196 * The driver seems to deliver events to notify when scan is
5197 * complete, so use longer timeout to avoid race conditions
5198 * with scanning and following association request.
5199 */
5200 timeout = 30;
5201 }
5202 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5203 "seconds", ret, timeout);
5204 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5205 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5206 drv, drv->ctx);
5207
5208nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005210 return ret;
5211}
5212
5213
5214/**
5215 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5216 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5217 * @params: Scan parameters
5218 * @interval: Interval between scan cycles in milliseconds
5219 * Returns: 0 on success, -1 on failure or if not supported
5220 */
5221static int wpa_driver_nl80211_sched_scan(void *priv,
5222 struct wpa_driver_scan_params *params,
5223 u32 interval)
5224{
5225 struct i802_bss *bss = priv;
5226 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005227 int ret = -1;
5228 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005229 size_t i;
5230
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005231 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5232
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005233#ifdef ANDROID
5234 if (!drv->capa.sched_scan_supported)
5235 return android_pno_start(bss, params);
5236#endif /* ANDROID */
5237
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005238 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5239 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005240 if (!msg)
5241 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005242
5243 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5244
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005245 if ((drv->num_filter_ssids &&
5246 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5247 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005248 struct nlattr *match_sets;
5249 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005250 if (match_sets == NULL)
5251 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005252
5253 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005254 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005255 wpa_hexdump_ascii(MSG_MSGDUMP,
5256 "nl80211: Sched scan filter SSID",
5257 drv->filter_ssids[i].ssid,
5258 drv->filter_ssids[i].ssid_len);
5259
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005260 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005261 if (match_set_ssid == NULL)
5262 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005263 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005264 drv->filter_ssids[i].ssid_len,
5265 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005266 if (params->filter_rssi)
5267 NLA_PUT_U32(msg,
5268 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5269 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005270
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005271 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005272 }
5273
Dmitry Shmidt97672262014-02-03 13:02:54 -08005274 /*
5275 * Due to backward compatibility code, newer kernels treat this
5276 * matchset (with only an RSSI filter) as the default for all
5277 * other matchsets, unless it's the only one, in which case the
5278 * matchset will actually allow all SSIDs above the RSSI.
5279 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005280 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005281 struct nlattr *match_set_rssi;
5282 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005283 if (match_set_rssi == NULL)
5284 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005285 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005286 params->filter_rssi);
5287 wpa_printf(MSG_MSGDUMP,
5288 "nl80211: Sched scan RSSI filter %d dBm",
5289 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005290 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005291 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005292
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005293 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005294 }
5295
5296 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5297
5298 /* TODO: if we get an error here, we should fall back to normal scan */
5299
5300 msg = NULL;
5301 if (ret) {
5302 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5303 "ret=%d (%s)", ret, strerror(-ret));
5304 goto nla_put_failure;
5305 }
5306
5307 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5308 "scan interval %d msec", ret, interval);
5309
5310nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005311 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005312 return ret;
5313}
5314
5315
5316/**
5317 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5318 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5319 * Returns: 0 on success, -1 on failure or if not supported
5320 */
5321static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5322{
5323 struct i802_bss *bss = priv;
5324 struct wpa_driver_nl80211_data *drv = bss->drv;
5325 int ret = 0;
5326 struct nl_msg *msg;
5327
5328#ifdef ANDROID
5329 if (!drv->capa.sched_scan_supported)
5330 return android_pno_stop(bss);
5331#endif /* ANDROID */
5332
5333 msg = nlmsg_alloc();
5334 if (!msg)
5335 return -1;
5336
5337 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5338
5339 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5340
5341 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5342 msg = NULL;
5343 if (ret) {
5344 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5345 "ret=%d (%s)", ret, strerror(-ret));
5346 goto nla_put_failure;
5347 }
5348
5349 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5350
5351nla_put_failure:
5352 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353 return ret;
5354}
5355
5356
5357static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5358{
5359 const u8 *end, *pos;
5360
5361 if (ies == NULL)
5362 return NULL;
5363
5364 pos = ies;
5365 end = ies + ies_len;
5366
5367 while (pos + 1 < end) {
5368 if (pos + 2 + pos[1] > end)
5369 break;
5370 if (pos[0] == ie)
5371 return pos;
5372 pos += 2 + pos[1];
5373 }
5374
5375 return NULL;
5376}
5377
5378
5379static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5380 const u8 *ie, size_t ie_len)
5381{
5382 const u8 *ssid;
5383 size_t i;
5384
5385 if (drv->filter_ssids == NULL)
5386 return 0;
5387
5388 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5389 if (ssid == NULL)
5390 return 1;
5391
5392 for (i = 0; i < drv->num_filter_ssids; i++) {
5393 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5394 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5395 0)
5396 return 0;
5397 }
5398
5399 return 1;
5400}
5401
5402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005403static int bss_info_handler(struct nl_msg *msg, void *arg)
5404{
5405 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5406 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5407 struct nlattr *bss[NL80211_BSS_MAX + 1];
5408 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5409 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5410 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5411 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5412 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5413 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5414 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5415 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5416 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5417 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5418 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5419 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5420 };
5421 struct nl80211_bss_info_arg *_arg = arg;
5422 struct wpa_scan_results *res = _arg->res;
5423 struct wpa_scan_res **tmp;
5424 struct wpa_scan_res *r;
5425 const u8 *ie, *beacon_ie;
5426 size_t ie_len, beacon_ie_len;
5427 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005428 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429
5430 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5431 genlmsg_attrlen(gnlh, 0), NULL);
5432 if (!tb[NL80211_ATTR_BSS])
5433 return NL_SKIP;
5434 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5435 bss_policy))
5436 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005437 if (bss[NL80211_BSS_STATUS]) {
5438 enum nl80211_bss_status status;
5439 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5440 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5441 bss[NL80211_BSS_FREQUENCY]) {
5442 _arg->assoc_freq =
5443 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5444 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5445 _arg->assoc_freq);
5446 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07005447 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
5448 bss[NL80211_BSS_FREQUENCY]) {
5449 _arg->ibss_freq =
5450 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5451 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
5452 _arg->ibss_freq);
5453 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005454 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5455 bss[NL80211_BSS_BSSID]) {
5456 os_memcpy(_arg->assoc_bssid,
5457 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5458 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5459 MACSTR, MAC2STR(_arg->assoc_bssid));
5460 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005461 }
5462 if (!res)
5463 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5465 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5466 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5467 } else {
5468 ie = NULL;
5469 ie_len = 0;
5470 }
5471 if (bss[NL80211_BSS_BEACON_IES]) {
5472 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5473 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5474 } else {
5475 beacon_ie = NULL;
5476 beacon_ie_len = 0;
5477 }
5478
5479 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5480 ie ? ie_len : beacon_ie_len))
5481 return NL_SKIP;
5482
5483 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5484 if (r == NULL)
5485 return NL_SKIP;
5486 if (bss[NL80211_BSS_BSSID])
5487 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5488 ETH_ALEN);
5489 if (bss[NL80211_BSS_FREQUENCY])
5490 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5491 if (bss[NL80211_BSS_BEACON_INTERVAL])
5492 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5493 if (bss[NL80211_BSS_CAPABILITY])
5494 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5495 r->flags |= WPA_SCAN_NOISE_INVALID;
5496 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5497 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5498 r->level /= 100; /* mBm to dBm */
5499 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5500 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5501 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005502 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503 } else
5504 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5505 if (bss[NL80211_BSS_TSF])
5506 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5507 if (bss[NL80211_BSS_SEEN_MS_AGO])
5508 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5509 r->ie_len = ie_len;
5510 pos = (u8 *) (r + 1);
5511 if (ie) {
5512 os_memcpy(pos, ie, ie_len);
5513 pos += ie_len;
5514 }
5515 r->beacon_ie_len = beacon_ie_len;
5516 if (beacon_ie)
5517 os_memcpy(pos, beacon_ie, beacon_ie_len);
5518
5519 if (bss[NL80211_BSS_STATUS]) {
5520 enum nl80211_bss_status status;
5521 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5522 switch (status) {
5523 case NL80211_BSS_STATUS_AUTHENTICATED:
5524 r->flags |= WPA_SCAN_AUTHENTICATED;
5525 break;
5526 case NL80211_BSS_STATUS_ASSOCIATED:
5527 r->flags |= WPA_SCAN_ASSOCIATED;
5528 break;
5529 default:
5530 break;
5531 }
5532 }
5533
Jouni Malinen87fd2792011-05-16 18:35:42 +03005534 /*
5535 * cfg80211 maintains separate BSS table entries for APs if the same
5536 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5537 * not use frequency as a separate key in the BSS table, so filter out
5538 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005539 * order to get the correct frequency into the BSS table. Similarly,
5540 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005541 */
5542 for (i = 0; i < res->num; i++) {
5543 const u8 *s1, *s2;
5544 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5545 continue;
5546
5547 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5548 res->res[i]->ie_len, WLAN_EID_SSID);
5549 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5550 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5551 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5552 continue;
5553
5554 /* Same BSSID,SSID was already included in scan results */
5555 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5556 "for " MACSTR, MAC2STR(r->bssid));
5557
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005558 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5559 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5560 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005561 os_free(res->res[i]);
5562 res->res[i] = r;
5563 } else
5564 os_free(r);
5565 return NL_SKIP;
5566 }
5567
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005568 tmp = os_realloc_array(res->res, res->num + 1,
5569 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 if (tmp == NULL) {
5571 os_free(r);
5572 return NL_SKIP;
5573 }
5574 tmp[res->num++] = r;
5575 res->res = tmp;
5576
5577 return NL_SKIP;
5578}
5579
5580
5581static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5582 const u8 *addr)
5583{
5584 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5585 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5586 "mismatch (" MACSTR ")", MAC2STR(addr));
5587 wpa_driver_nl80211_mlme(drv, addr,
5588 NL80211_CMD_DEAUTHENTICATE,
5589 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5590 }
5591}
5592
5593
5594static void wpa_driver_nl80211_check_bss_status(
5595 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5596{
5597 size_t i;
5598
5599 for (i = 0; i < res->num; i++) {
5600 struct wpa_scan_res *r = res->res[i];
5601 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5602 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5603 "indicates BSS status with " MACSTR
5604 " as authenticated",
5605 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005606 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5608 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5609 0) {
5610 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5611 " in local state (auth=" MACSTR
5612 " assoc=" MACSTR ")",
5613 MAC2STR(drv->auth_bssid),
5614 MAC2STR(drv->bssid));
5615 clear_state_mismatch(drv, r->bssid);
5616 }
5617 }
5618
5619 if (r->flags & WPA_SCAN_ASSOCIATED) {
5620 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5621 "indicate BSS status with " MACSTR
5622 " as associated",
5623 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 !drv->associated) {
5626 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5627 "(not associated) does not match "
5628 "with BSS state");
5629 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005630 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005631 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5632 0) {
5633 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5634 "(associated with " MACSTR ") does "
5635 "not match with BSS state",
5636 MAC2STR(drv->bssid));
5637 clear_state_mismatch(drv, r->bssid);
5638 clear_state_mismatch(drv, drv->bssid);
5639 }
5640 }
5641 }
5642}
5643
5644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645static struct wpa_scan_results *
5646nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5647{
5648 struct nl_msg *msg;
5649 struct wpa_scan_results *res;
5650 int ret;
5651 struct nl80211_bss_info_arg arg;
5652
5653 res = os_zalloc(sizeof(*res));
5654 if (res == NULL)
5655 return NULL;
5656 msg = nlmsg_alloc();
5657 if (!msg)
5658 goto nla_put_failure;
5659
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005660 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005661 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005662 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663
5664 arg.drv = drv;
5665 arg.res = res;
5666 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5667 msg = NULL;
5668 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005669 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5670 "BSSes)", (unsigned long) res->num);
5671 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 return res;
5673 }
5674 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5675 "(%s)", ret, strerror(-ret));
5676nla_put_failure:
5677 nlmsg_free(msg);
5678 wpa_scan_results_free(res);
5679 return NULL;
5680}
5681
5682
5683/**
5684 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5685 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5686 * Returns: Scan results on success, -1 on failure
5687 */
5688static struct wpa_scan_results *
5689wpa_driver_nl80211_get_scan_results(void *priv)
5690{
5691 struct i802_bss *bss = priv;
5692 struct wpa_driver_nl80211_data *drv = bss->drv;
5693 struct wpa_scan_results *res;
5694
5695 res = nl80211_get_scan_results(drv);
5696 if (res)
5697 wpa_driver_nl80211_check_bss_status(drv, res);
5698 return res;
5699}
5700
5701
5702static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5703{
5704 struct wpa_scan_results *res;
5705 size_t i;
5706
5707 res = nl80211_get_scan_results(drv);
5708 if (res == NULL) {
5709 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5710 return;
5711 }
5712
5713 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5714 for (i = 0; i < res->num; i++) {
5715 struct wpa_scan_res *r = res->res[i];
5716 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5717 (int) i, (int) res->num, MAC2STR(r->bssid),
5718 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5719 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5720 }
5721
5722 wpa_scan_results_free(res);
5723}
5724
5725
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005726static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5727{
5728 switch (alg) {
5729 case WPA_ALG_WEP:
5730 if (key_len == 5)
5731 return WLAN_CIPHER_SUITE_WEP40;
5732 return WLAN_CIPHER_SUITE_WEP104;
5733 case WPA_ALG_TKIP:
5734 return WLAN_CIPHER_SUITE_TKIP;
5735 case WPA_ALG_CCMP:
5736 return WLAN_CIPHER_SUITE_CCMP;
5737 case WPA_ALG_GCMP:
5738 return WLAN_CIPHER_SUITE_GCMP;
5739 case WPA_ALG_CCMP_256:
5740 return WLAN_CIPHER_SUITE_CCMP_256;
5741 case WPA_ALG_GCMP_256:
5742 return WLAN_CIPHER_SUITE_GCMP_256;
5743 case WPA_ALG_IGTK:
5744 return WLAN_CIPHER_SUITE_AES_CMAC;
5745 case WPA_ALG_BIP_GMAC_128:
5746 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5747 case WPA_ALG_BIP_GMAC_256:
5748 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5749 case WPA_ALG_BIP_CMAC_256:
5750 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5751 case WPA_ALG_SMS4:
5752 return WLAN_CIPHER_SUITE_SMS4;
5753 case WPA_ALG_KRK:
5754 return WLAN_CIPHER_SUITE_KRK;
5755 case WPA_ALG_NONE:
5756 case WPA_ALG_PMK:
5757 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5758 alg);
5759 return 0;
5760 }
5761
5762 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5763 alg);
5764 return 0;
5765}
5766
5767
5768static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5769{
5770 switch (cipher) {
5771 case WPA_CIPHER_CCMP_256:
5772 return WLAN_CIPHER_SUITE_CCMP_256;
5773 case WPA_CIPHER_GCMP_256:
5774 return WLAN_CIPHER_SUITE_GCMP_256;
5775 case WPA_CIPHER_CCMP:
5776 return WLAN_CIPHER_SUITE_CCMP;
5777 case WPA_CIPHER_GCMP:
5778 return WLAN_CIPHER_SUITE_GCMP;
5779 case WPA_CIPHER_TKIP:
5780 return WLAN_CIPHER_SUITE_TKIP;
5781 case WPA_CIPHER_WEP104:
5782 return WLAN_CIPHER_SUITE_WEP104;
5783 case WPA_CIPHER_WEP40:
5784 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005785 case WPA_CIPHER_GTK_NOT_USED:
5786 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005787 }
5788
5789 return 0;
5790}
5791
5792
5793static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5794 int max_suites)
5795{
5796 int num_suites = 0;
5797
5798 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5799 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5800 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5801 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5802 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5803 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5804 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5805 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5806 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5807 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5808 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5809 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5810 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5811 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5812
5813 return num_suites;
5814}
5815
5816
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005817static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818 enum wpa_alg alg, const u8 *addr,
5819 int key_idx, int set_tx,
5820 const u8 *seq, size_t seq_len,
5821 const u8 *key, size_t key_len)
5822{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005823 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005824 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 struct nl_msg *msg;
5826 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005827 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005829 /* Ignore for P2P Device */
5830 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5831 return 0;
5832
5833 ifindex = if_nametoindex(ifname);
5834 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005836 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005837 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005838#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005839 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005840 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005841 tdls = 1;
5842 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005843#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005844
5845 msg = nlmsg_alloc();
5846 if (!msg)
5847 return -ENOMEM;
5848
5849 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005850 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005852 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005854 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005855 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5856 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857 }
5858
Dmitry Shmidt98660862014-03-11 17:26:21 -07005859 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005861 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5862 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005863
5864 if (addr && !is_broadcast_ether_addr(addr)) {
5865 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5866 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5867
5868 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5869 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5870 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5871 NL80211_KEYTYPE_GROUP);
5872 }
5873 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005874 struct nlattr *types;
5875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005877
5878 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 if (!types)
5880 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005881 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5882 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 }
5884 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5885 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5886
5887 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5888 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5889 ret = 0;
5890 if (ret)
5891 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5892 ret, strerror(-ret));
5893
5894 /*
5895 * If we failed or don't need to set the default TX key (below),
5896 * we're done here.
5897 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005898 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005900 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901 !is_broadcast_ether_addr(addr))
5902 return ret;
5903
5904 msg = nlmsg_alloc();
5905 if (!msg)
5906 return -ENOMEM;
5907
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005908 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005909 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5910 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5911 if (alg == WPA_ALG_IGTK)
5912 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5913 else
5914 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5915 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005916 struct nlattr *types;
5917
5918 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919 if (!types)
5920 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005921 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5922 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005924 struct nlattr *types;
5925
5926 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005927 if (!types)
5928 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005929 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5930 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 }
5932
5933 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5934 if (ret == -ENOENT)
5935 ret = 0;
5936 if (ret)
5937 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5938 "err=%d %s)", ret, strerror(-ret));
5939 return ret;
5940
5941nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005942 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943 return -ENOBUFS;
5944}
5945
5946
5947static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5948 int key_idx, int defkey,
5949 const u8 *seq, size_t seq_len,
5950 const u8 *key, size_t key_len)
5951{
5952 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5953 if (!key_attr)
5954 return -1;
5955
5956 if (defkey && alg == WPA_ALG_IGTK)
5957 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5958 else if (defkey)
5959 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5960
5961 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5962
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005963 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5964 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965
5966 if (seq && seq_len)
5967 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
5968
5969 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
5970
5971 nla_nest_end(msg, key_attr);
5972
5973 return 0;
5974 nla_put_failure:
5975 return -1;
5976}
5977
5978
5979static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
5980 struct nl_msg *msg)
5981{
5982 int i, privacy = 0;
5983 struct nlattr *nl_keys, *nl_key;
5984
5985 for (i = 0; i < 4; i++) {
5986 if (!params->wep_key[i])
5987 continue;
5988 privacy = 1;
5989 break;
5990 }
5991 if (params->wps == WPS_MODE_PRIVACY)
5992 privacy = 1;
5993 if (params->pairwise_suite &&
5994 params->pairwise_suite != WPA_CIPHER_NONE)
5995 privacy = 1;
5996
5997 if (!privacy)
5998 return 0;
5999
6000 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
6001
6002 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
6003 if (!nl_keys)
6004 goto nla_put_failure;
6005
6006 for (i = 0; i < 4; i++) {
6007 if (!params->wep_key[i])
6008 continue;
6009
6010 nl_key = nla_nest_start(msg, i);
6011 if (!nl_key)
6012 goto nla_put_failure;
6013
6014 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
6015 params->wep_key[i]);
6016 if (params->wep_key_len[i] == 5)
6017 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6018 WLAN_CIPHER_SUITE_WEP40);
6019 else
6020 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6021 WLAN_CIPHER_SUITE_WEP104);
6022
6023 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
6024
6025 if (i == params->wep_tx_keyidx)
6026 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
6027
6028 nla_nest_end(msg, nl_key);
6029 }
6030 nla_nest_end(msg, nl_keys);
6031
6032 return 0;
6033
6034nla_put_failure:
6035 return -ENOBUFS;
6036}
6037
6038
6039static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
6040 const u8 *addr, int cmd, u16 reason_code,
6041 int local_state_change)
6042{
6043 int ret = -1;
6044 struct nl_msg *msg;
6045
6046 msg = nlmsg_alloc();
6047 if (!msg)
6048 return -1;
6049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006050 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006051
6052 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6053 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006054 if (addr)
6055 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 if (local_state_change)
6057 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6058
6059 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6060 msg = NULL;
6061 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006062 wpa_dbg(drv->ctx, MSG_DEBUG,
6063 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
6064 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 goto nla_put_failure;
6066 }
6067 ret = 0;
6068
6069nla_put_failure:
6070 nlmsg_free(msg);
6071 return ret;
6072}
6073
6074
6075static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006076 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006077{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006078 int ret;
6079
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006080 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006081 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006082 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006083 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
6084 reason_code, 0);
6085 /*
6086 * For locally generated disconnect, supplicant already generates a
6087 * DEAUTH event, so ignore the event from NL80211.
6088 */
6089 drv->ignore_next_local_disconnect = ret == 0;
6090
6091 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006092}
6093
6094
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006095static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
6096 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006099 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07006100
6101 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
6102 nl80211_mark_disconnected(drv);
6103 return nl80211_leave_ibss(drv);
6104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006106 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
6108 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006109 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006110 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
6111 reason_code, 0);
6112 /*
6113 * For locally generated deauthenticate, supplicant already generates a
6114 * DEAUTH event, so ignore the event from NL80211.
6115 */
6116 drv->ignore_next_local_deauth = ret == 0;
6117 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006118}
6119
6120
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006121static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6122 struct wpa_driver_auth_params *params)
6123{
6124 int i;
6125
6126 drv->auth_freq = params->freq;
6127 drv->auth_alg = params->auth_alg;
6128 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6129 drv->auth_local_state_change = params->local_state_change;
6130 drv->auth_p2p = params->p2p;
6131
6132 if (params->bssid)
6133 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6134 else
6135 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6136
6137 if (params->ssid) {
6138 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6139 drv->auth_ssid_len = params->ssid_len;
6140 } else
6141 drv->auth_ssid_len = 0;
6142
6143
6144 os_free(drv->auth_ie);
6145 drv->auth_ie = NULL;
6146 drv->auth_ie_len = 0;
6147 if (params->ie) {
6148 drv->auth_ie = os_malloc(params->ie_len);
6149 if (drv->auth_ie) {
6150 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6151 drv->auth_ie_len = params->ie_len;
6152 }
6153 }
6154
6155 for (i = 0; i < 4; i++) {
6156 if (params->wep_key[i] && params->wep_key_len[i] &&
6157 params->wep_key_len[i] <= 16) {
6158 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6159 params->wep_key_len[i]);
6160 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6161 } else
6162 drv->auth_wep_key_len[i] = 0;
6163 }
6164}
6165
6166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006168 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006169{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 struct wpa_driver_nl80211_data *drv = bss->drv;
6171 int ret = -1, i;
6172 struct nl_msg *msg;
6173 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006175 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006176 int is_retry;
6177
6178 is_retry = drv->retry_auth;
6179 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006180 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006182 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006183 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006184 if (params->bssid)
6185 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6186 else
6187 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006189 nlmode = params->p2p ?
6190 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6191 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006192 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 return -1;
6194
6195retry:
6196 msg = nlmsg_alloc();
6197 if (!msg)
6198 return -1;
6199
6200 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6201 drv->ifindex);
6202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006203 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204
6205 for (i = 0; i < 4; i++) {
6206 if (!params->wep_key[i])
6207 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006208 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 NULL, i,
6210 i == params->wep_tx_keyidx, NULL, 0,
6211 params->wep_key[i],
6212 params->wep_key_len[i]);
6213 if (params->wep_tx_keyidx != i)
6214 continue;
6215 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6216 params->wep_key[i], params->wep_key_len[i])) {
6217 nlmsg_free(msg);
6218 return -1;
6219 }
6220 }
6221
6222 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6223 if (params->bssid) {
6224 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6225 MAC2STR(params->bssid));
6226 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6227 }
6228 if (params->freq) {
6229 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6230 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6231 }
6232 if (params->ssid) {
6233 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6234 params->ssid, params->ssid_len);
6235 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6236 params->ssid);
6237 }
6238 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6239 if (params->ie)
6240 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006241 if (params->sae_data) {
6242 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6243 params->sae_data_len);
6244 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6245 params->sae_data);
6246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006247 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6248 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6249 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6250 type = NL80211_AUTHTYPE_SHARED_KEY;
6251 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6252 type = NL80211_AUTHTYPE_NETWORK_EAP;
6253 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6254 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006255 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6256 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257 else
6258 goto nla_put_failure;
6259 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6260 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6261 if (params->local_state_change) {
6262 wpa_printf(MSG_DEBUG, " * Local state change only");
6263 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6264 }
6265
6266 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6267 msg = NULL;
6268 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006269 wpa_dbg(drv->ctx, MSG_DEBUG,
6270 "nl80211: MLME command failed (auth): ret=%d (%s)",
6271 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 count++;
6273 if (ret == -EALREADY && count == 1 && params->bssid &&
6274 !params->local_state_change) {
6275 /*
6276 * mac80211 does not currently accept new
6277 * authentication if we are already authenticated. As a
6278 * workaround, force deauthentication and try again.
6279 */
6280 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6281 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006282 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006283 wpa_driver_nl80211_deauthenticate(
6284 bss, params->bssid,
6285 WLAN_REASON_PREV_AUTH_NOT_VALID);
6286 nlmsg_free(msg);
6287 goto retry;
6288 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006289
6290 if (ret == -ENOENT && params->freq && !is_retry) {
6291 /*
6292 * cfg80211 has likely expired the BSS entry even
6293 * though it was previously available in our internal
6294 * BSS table. To recover quickly, start a single
6295 * channel scan on the specified channel.
6296 */
6297 struct wpa_driver_scan_params scan;
6298 int freqs[2];
6299
6300 os_memset(&scan, 0, sizeof(scan));
6301 scan.num_ssids = 1;
6302 if (params->ssid) {
6303 scan.ssids[0].ssid = params->ssid;
6304 scan.ssids[0].ssid_len = params->ssid_len;
6305 }
6306 freqs[0] = params->freq;
6307 freqs[1] = 0;
6308 scan.freqs = freqs;
6309 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6310 "channel scan to refresh cfg80211 BSS "
6311 "entry");
6312 ret = wpa_driver_nl80211_scan(bss, &scan);
6313 if (ret == 0) {
6314 nl80211_copy_auth_params(drv, params);
6315 drv->scan_for_auth = 1;
6316 }
6317 } else if (is_retry) {
6318 /*
6319 * Need to indicate this with an event since the return
6320 * value from the retry is not delivered to core code.
6321 */
6322 union wpa_event_data event;
6323 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6324 "failed");
6325 os_memset(&event, 0, sizeof(event));
6326 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6327 ETH_ALEN);
6328 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6329 &event);
6330 }
6331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006332 goto nla_put_failure;
6333 }
6334 ret = 0;
6335 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6336 "successfully");
6337
6338nla_put_failure:
6339 nlmsg_free(msg);
6340 return ret;
6341}
6342
6343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006344static int wpa_driver_nl80211_authenticate_retry(
6345 struct wpa_driver_nl80211_data *drv)
6346{
6347 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006348 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006349 int i;
6350
6351 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6352
6353 os_memset(&params, 0, sizeof(params));
6354 params.freq = drv->auth_freq;
6355 params.auth_alg = drv->auth_alg;
6356 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6357 params.local_state_change = drv->auth_local_state_change;
6358 params.p2p = drv->auth_p2p;
6359
6360 if (!is_zero_ether_addr(drv->auth_bssid_))
6361 params.bssid = drv->auth_bssid_;
6362
6363 if (drv->auth_ssid_len) {
6364 params.ssid = drv->auth_ssid;
6365 params.ssid_len = drv->auth_ssid_len;
6366 }
6367
6368 params.ie = drv->auth_ie;
6369 params.ie_len = drv->auth_ie_len;
6370
6371 for (i = 0; i < 4; i++) {
6372 if (drv->auth_wep_key_len[i]) {
6373 params.wep_key[i] = drv->auth_wep_key[i];
6374 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6375 }
6376 }
6377
6378 drv->retry_auth = 1;
6379 return wpa_driver_nl80211_authenticate(bss, &params);
6380}
6381
6382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383struct phy_info_arg {
6384 u16 *num_modes;
6385 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006386 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006387};
6388
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006389static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6390 struct nlattr *ampdu_factor,
6391 struct nlattr *ampdu_density,
6392 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006394 if (capa)
6395 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006396
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006397 if (ampdu_factor)
6398 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006399
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006400 if (ampdu_density)
6401 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6402
6403 if (mcs_set && nla_len(mcs_set) >= 16) {
6404 u8 *mcs;
6405 mcs = nla_data(mcs_set);
6406 os_memcpy(mode->mcs_set, mcs, 16);
6407 }
6408}
6409
6410
6411static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6412 struct nlattr *capa,
6413 struct nlattr *mcs_set)
6414{
6415 if (capa)
6416 mode->vht_capab = nla_get_u32(capa);
6417
6418 if (mcs_set && nla_len(mcs_set) >= 8) {
6419 u8 *mcs;
6420 mcs = nla_data(mcs_set);
6421 os_memcpy(mode->vht_mcs_set, mcs, 8);
6422 }
6423}
6424
6425
6426static void phy_info_freq(struct hostapd_hw_modes *mode,
6427 struct hostapd_channel_data *chan,
6428 struct nlattr *tb_freq[])
6429{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006430 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006431 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6432 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006433 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006434 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6435 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006436
6437 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6438 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006439 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6440 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006441 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6442 chan->flag |= HOSTAPD_CHAN_RADAR;
6443
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006444 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6445 enum nl80211_dfs_state state =
6446 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6447
6448 switch (state) {
6449 case NL80211_DFS_USABLE:
6450 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6451 break;
6452 case NL80211_DFS_AVAILABLE:
6453 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6454 break;
6455 case NL80211_DFS_UNAVAILABLE:
6456 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6457 break;
6458 }
6459 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006460
6461 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6462 chan->dfs_cac_ms = nla_get_u32(
6463 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6464 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006465}
6466
6467
6468static int phy_info_freqs(struct phy_info_arg *phy_info,
6469 struct hostapd_hw_modes *mode, struct nlattr *tb)
6470{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006471 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6472 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6473 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006474 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006475 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6476 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006477 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006479 int new_channels = 0;
6480 struct hostapd_channel_data *channel;
6481 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006483 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006484
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006485 if (tb == NULL)
6486 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006488 nla_for_each_nested(nl_freq, tb, rem_freq) {
6489 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6490 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6491 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6492 continue;
6493 new_channels++;
6494 }
6495
6496 channel = os_realloc_array(mode->channels,
6497 mode->num_channels + new_channels,
6498 sizeof(struct hostapd_channel_data));
6499 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500 return NL_SKIP;
6501
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006502 mode->channels = channel;
6503 mode->num_channels += new_channels;
6504
6505 idx = phy_info->last_chan_idx;
6506
6507 nla_for_each_nested(nl_freq, tb, rem_freq) {
6508 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6509 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6510 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6511 continue;
6512 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6513 idx++;
6514 }
6515 phy_info->last_chan_idx = idx;
6516
6517 return NL_OK;
6518}
6519
6520
6521static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6522{
6523 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6524 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6525 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6526 { .type = NLA_FLAG },
6527 };
6528 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6529 struct nlattr *nl_rate;
6530 int rem_rate, idx;
6531
6532 if (tb == NULL)
6533 return NL_OK;
6534
6535 nla_for_each_nested(nl_rate, tb, rem_rate) {
6536 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6537 nla_data(nl_rate), nla_len(nl_rate),
6538 rate_policy);
6539 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6540 continue;
6541 mode->num_rates++;
6542 }
6543
6544 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6545 if (!mode->rates)
6546 return NL_SKIP;
6547
6548 idx = 0;
6549
6550 nla_for_each_nested(nl_rate, tb, rem_rate) {
6551 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6552 nla_data(nl_rate), nla_len(nl_rate),
6553 rate_policy);
6554 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6555 continue;
6556 mode->rates[idx] = nla_get_u32(
6557 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006558 idx++;
6559 }
6560
6561 return NL_OK;
6562}
6563
6564
6565static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6566{
6567 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6568 struct hostapd_hw_modes *mode;
6569 int ret;
6570
6571 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006572 mode = os_realloc_array(phy_info->modes,
6573 *phy_info->num_modes + 1,
6574 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006575 if (!mode)
6576 return NL_SKIP;
6577 phy_info->modes = mode;
6578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006579 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006580 os_memset(mode, 0, sizeof(*mode));
6581 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006582 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6583 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6584
6585 /*
6586 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6587 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6588 * possible streams as unsupported. This will be overridden if
6589 * driver advertises VHT support.
6590 */
6591 mode->vht_mcs_set[0] = 0xff;
6592 mode->vht_mcs_set[1] = 0xff;
6593 mode->vht_mcs_set[4] = 0xff;
6594 mode->vht_mcs_set[5] = 0xff;
6595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006597 phy_info->last_mode = nl_band->nla_type;
6598 phy_info->last_chan_idx = 0;
6599 } else
6600 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006601
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006602 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6603 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006605 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6606 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6607 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6608 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6609 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6610 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6611 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6612 if (ret != NL_OK)
6613 return ret;
6614 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6615 if (ret != NL_OK)
6616 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006618 return NL_OK;
6619}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006622static int phy_info_handler(struct nl_msg *msg, void *arg)
6623{
6624 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6625 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6626 struct phy_info_arg *phy_info = arg;
6627 struct nlattr *nl_band;
6628 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006629
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006630 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6631 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006632
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006633 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6634 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006635
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006636 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6637 {
6638 int res = phy_info_band(phy_info, nl_band);
6639 if (res != NL_OK)
6640 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006641 }
6642
6643 return NL_SKIP;
6644}
6645
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006647static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006648wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6649 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006650{
6651 u16 m;
6652 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6653 int i, mode11g_idx = -1;
6654
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006655 /* heuristic to set up modes */
6656 for (m = 0; m < *num_modes; m++) {
6657 if (!modes[m].num_channels)
6658 continue;
6659 if (modes[m].channels[0].freq < 4000) {
6660 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6661 for (i = 0; i < modes[m].num_rates; i++) {
6662 if (modes[m].rates[i] > 200) {
6663 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6664 break;
6665 }
6666 }
6667 } else if (modes[m].channels[0].freq > 50000)
6668 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6669 else
6670 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6671 }
6672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006673 /* If only 802.11g mode is included, use it to construct matching
6674 * 802.11b mode data. */
6675
6676 for (m = 0; m < *num_modes; m++) {
6677 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6678 return modes; /* 802.11b already included */
6679 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6680 mode11g_idx = m;
6681 }
6682
6683 if (mode11g_idx < 0)
6684 return modes; /* 2.4 GHz band not supported at all */
6685
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006686 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006687 if (nmodes == NULL)
6688 return modes; /* Could not add 802.11b mode */
6689
6690 mode = &nmodes[*num_modes];
6691 os_memset(mode, 0, sizeof(*mode));
6692 (*num_modes)++;
6693 modes = nmodes;
6694
6695 mode->mode = HOSTAPD_MODE_IEEE80211B;
6696
6697 mode11g = &modes[mode11g_idx];
6698 mode->num_channels = mode11g->num_channels;
6699 mode->channels = os_malloc(mode11g->num_channels *
6700 sizeof(struct hostapd_channel_data));
6701 if (mode->channels == NULL) {
6702 (*num_modes)--;
6703 return modes; /* Could not add 802.11b mode */
6704 }
6705 os_memcpy(mode->channels, mode11g->channels,
6706 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6707
6708 mode->num_rates = 0;
6709 mode->rates = os_malloc(4 * sizeof(int));
6710 if (mode->rates == NULL) {
6711 os_free(mode->channels);
6712 (*num_modes)--;
6713 return modes; /* Could not add 802.11b mode */
6714 }
6715
6716 for (i = 0; i < mode11g->num_rates; i++) {
6717 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6718 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6719 continue;
6720 mode->rates[mode->num_rates] = mode11g->rates[i];
6721 mode->num_rates++;
6722 if (mode->num_rates == 4)
6723 break;
6724 }
6725
6726 if (mode->num_rates == 0) {
6727 os_free(mode->channels);
6728 os_free(mode->rates);
6729 (*num_modes)--;
6730 return modes; /* No 802.11b rates */
6731 }
6732
6733 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6734 "information");
6735
6736 return modes;
6737}
6738
6739
6740static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6741 int end)
6742{
6743 int c;
6744
6745 for (c = 0; c < mode->num_channels; c++) {
6746 struct hostapd_channel_data *chan = &mode->channels[c];
6747 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6748 chan->flag |= HOSTAPD_CHAN_HT40;
6749 }
6750}
6751
6752
6753static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6754 int end)
6755{
6756 int c;
6757
6758 for (c = 0; c < mode->num_channels; c++) {
6759 struct hostapd_channel_data *chan = &mode->channels[c];
6760 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6761 continue;
6762 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6763 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6764 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6765 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6766 }
6767}
6768
6769
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006770static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006771 struct phy_info_arg *results)
6772{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006773 u16 m;
6774
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006775 for (m = 0; m < *results->num_modes; m++) {
6776 int c;
6777 struct hostapd_hw_modes *mode = &results->modes[m];
6778
6779 for (c = 0; c < mode->num_channels; c++) {
6780 struct hostapd_channel_data *chan = &mode->channels[c];
6781 if ((u32) chan->freq - 10 >= start &&
6782 (u32) chan->freq + 10 <= end)
6783 chan->max_tx_power = max_eirp;
6784 }
6785 }
6786}
6787
6788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006789static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006790 struct phy_info_arg *results)
6791{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006792 u16 m;
6793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794 for (m = 0; m < *results->num_modes; m++) {
6795 if (!(results->modes[m].ht_capab &
6796 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6797 continue;
6798 nl80211_set_ht40_mode(&results->modes[m], start, end);
6799 }
6800}
6801
6802
6803static void nl80211_reg_rule_sec(struct nlattr *tb[],
6804 struct phy_info_arg *results)
6805{
6806 u32 start, end, max_bw;
6807 u16 m;
6808
6809 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6810 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6811 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6812 return;
6813
6814 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6815 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6816 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6817
6818 if (max_bw < 20)
6819 return;
6820
6821 for (m = 0; m < *results->num_modes; m++) {
6822 if (!(results->modes[m].ht_capab &
6823 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6824 continue;
6825 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6826 }
6827}
6828
6829
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006830static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6831 int end)
6832{
6833 int c;
6834
6835 for (c = 0; c < mode->num_channels; c++) {
6836 struct hostapd_channel_data *chan = &mode->channels[c];
6837 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6838 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6839
6840 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6841 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6842
6843 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6844 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6845
6846 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6847 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6848 }
6849}
6850
6851
6852static void nl80211_reg_rule_vht(struct nlattr *tb[],
6853 struct phy_info_arg *results)
6854{
6855 u32 start, end, max_bw;
6856 u16 m;
6857
6858 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6859 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6860 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6861 return;
6862
6863 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6864 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6865 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6866
6867 if (max_bw < 80)
6868 return;
6869
6870 for (m = 0; m < *results->num_modes; m++) {
6871 if (!(results->modes[m].ht_capab &
6872 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6873 continue;
6874 /* TODO: use a real VHT support indication */
6875 if (!results->modes[m].vht_capab)
6876 continue;
6877
6878 nl80211_set_vht_mode(&results->modes[m], start, end);
6879 }
6880}
6881
6882
Dmitry Shmidt97672262014-02-03 13:02:54 -08006883static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6884{
6885 switch (region) {
6886 case NL80211_DFS_UNSET:
6887 return "DFS-UNSET";
6888 case NL80211_DFS_FCC:
6889 return "DFS-FCC";
6890 case NL80211_DFS_ETSI:
6891 return "DFS-ETSI";
6892 case NL80211_DFS_JP:
6893 return "DFS-JP";
6894 default:
6895 return "DFS-invalid";
6896 }
6897}
6898
6899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6901{
6902 struct phy_info_arg *results = arg;
6903 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6904 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6905 struct nlattr *nl_rule;
6906 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6907 int rem_rule;
6908 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6909 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6910 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6911 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6912 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6913 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6914 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6915 };
6916
6917 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6918 genlmsg_attrlen(gnlh, 0), NULL);
6919 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6920 !tb_msg[NL80211_ATTR_REG_RULES]) {
6921 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6922 "available");
6923 return NL_SKIP;
6924 }
6925
Dmitry Shmidt97672262014-02-03 13:02:54 -08006926 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6927 enum nl80211_dfs_regions dfs_domain;
6928 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6929 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6930 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6931 dfs_domain_name(dfs_domain));
6932 } else {
6933 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6934 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6935 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936
6937 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6938 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006939 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006940 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6941 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006942 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6943 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6944 continue;
6945 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6946 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6947 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6948 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6949 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6950 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006951 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6952 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006953
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006954 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6955 start, end, max_bw, max_eirp,
6956 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6957 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6958 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6959 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6960 "",
6961 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6962 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6963 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6964 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006965 if (max_bw >= 40)
6966 nl80211_reg_rule_ht40(start, end, results);
6967 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6968 nl80211_reg_rule_max_eirp(start, end, max_eirp,
6969 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970 }
6971
6972 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6973 {
6974 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6975 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6976 nl80211_reg_rule_sec(tb_rule, results);
6977 }
6978
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006979 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6980 {
6981 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6982 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
6983 nl80211_reg_rule_vht(tb_rule, results);
6984 }
6985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986 return NL_SKIP;
6987}
6988
6989
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006990static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
6991 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006992{
6993 struct nl_msg *msg;
6994
6995 msg = nlmsg_alloc();
6996 if (!msg)
6997 return -ENOMEM;
6998
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006999 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
7001}
7002
7003
7004static struct hostapd_hw_modes *
7005wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
7006{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007007 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008 struct i802_bss *bss = priv;
7009 struct wpa_driver_nl80211_data *drv = bss->drv;
7010 struct nl_msg *msg;
7011 struct phy_info_arg result = {
7012 .num_modes = num_modes,
7013 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007014 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007015 };
7016
7017 *num_modes = 0;
7018 *flags = 0;
7019
7020 msg = nlmsg_alloc();
7021 if (!msg)
7022 return NULL;
7023
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007024 feat = get_nl80211_protocol_features(drv);
7025 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
7026 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
7027 else
7028 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007029
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007030 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007031 if (nl80211_set_iface_id(msg, bss) < 0)
7032 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033
7034 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007035 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007036 return wpa_driver_nl80211_postprocess_modes(result.modes,
7037 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007039 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007040 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007041 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 return NULL;
7043}
7044
7045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
7047 const void *data, size_t len,
7048 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049{
7050 __u8 rtap_hdr[] = {
7051 0x00, 0x00, /* radiotap version */
7052 0x0e, 0x00, /* radiotap length */
7053 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
7054 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
7055 0x00, /* padding */
7056 0x00, 0x00, /* RX and TX flags to indicate that */
7057 0x00, 0x00, /* this is the injected frame directly */
7058 };
7059 struct iovec iov[2] = {
7060 {
7061 .iov_base = &rtap_hdr,
7062 .iov_len = sizeof(rtap_hdr),
7063 },
7064 {
7065 .iov_base = (void *) data,
7066 .iov_len = len,
7067 }
7068 };
7069 struct msghdr msg = {
7070 .msg_name = NULL,
7071 .msg_namelen = 0,
7072 .msg_iov = iov,
7073 .msg_iovlen = 2,
7074 .msg_control = NULL,
7075 .msg_controllen = 0,
7076 .msg_flags = 0,
7077 };
7078 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007079 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080
7081 if (encrypt)
7082 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
7083
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007084 if (drv->monitor_sock < 0) {
7085 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
7086 "for %s", __func__);
7087 return -1;
7088 }
7089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007090 if (noack)
7091 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007092 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094 res = sendmsg(drv->monitor_sock, &msg, 0);
7095 if (res < 0) {
7096 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
7097 return -1;
7098 }
7099 return 0;
7100}
7101
7102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007103static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
7104 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007105 int encrypt, int noack,
7106 unsigned int freq, int no_cck,
7107 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007108{
7109 struct wpa_driver_nl80211_data *drv = bss->drv;
7110 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007111 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007112
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007113 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
7114 freq = nl80211_get_assoc_freq(drv);
7115 wpa_printf(MSG_DEBUG,
7116 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
7117 freq);
7118 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007119 if (freq == 0) {
7120 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
7121 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007122 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007123 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007124
Dmitry Shmidt56052862013-10-04 10:23:25 -07007125 if (drv->use_monitor) {
7126 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7127 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007128 return wpa_driver_nl80211_send_mntr(drv, data, len,
7129 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007130 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007131
Dmitry Shmidt56052862013-10-04 10:23:25 -07007132 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07007133 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7134 &cookie, no_cck, noack, offchanok);
7135 if (res == 0 && !noack) {
7136 const struct ieee80211_mgmt *mgmt;
7137 u16 fc;
7138
7139 mgmt = (const struct ieee80211_mgmt *) data;
7140 fc = le_to_host16(mgmt->frame_control);
7141 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7142 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7143 wpa_printf(MSG_MSGDUMP,
7144 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7145 (long long unsigned int)
7146 drv->send_action_cookie,
7147 (long long unsigned int) cookie);
7148 drv->send_action_cookie = cookie;
7149 }
7150 }
7151
7152 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007153}
7154
7155
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007156static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7157 size_t data_len, int noack,
7158 unsigned int freq, int no_cck,
7159 int offchanok,
7160 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007161{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007162 struct wpa_driver_nl80211_data *drv = bss->drv;
7163 struct ieee80211_mgmt *mgmt;
7164 int encrypt = 1;
7165 u16 fc;
7166
7167 mgmt = (struct ieee80211_mgmt *) data;
7168 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007169 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
7170 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
7171 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
7172 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007173
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007174 if ((is_sta_interface(drv->nlmode) ||
7175 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007176 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7177 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7178 /*
7179 * The use of last_mgmt_freq is a bit of a hack,
7180 * but it works due to the single-threaded nature
7181 * of wpa_supplicant.
7182 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007183 if (freq == 0) {
7184 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7185 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007186 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007187 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007188 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007189 data, data_len, NULL, 1, noack,
7190 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007191 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007192
7193 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007194 if (freq == 0) {
7195 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7196 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007197 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007198 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007199 return nl80211_send_frame_cmd(bss, freq,
7200 (int) freq == bss->freq ? 0 :
7201 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007202 data, data_len,
7203 &drv->send_action_cookie,
7204 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007205 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7208 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7209 /*
7210 * Only one of the authentication frame types is encrypted.
7211 * In order for static WEP encryption to work properly (i.e.,
7212 * to not encrypt the frame), we need to tell mac80211 about
7213 * the frames that must not be encrypted.
7214 */
7215 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7216 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7217 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7218 encrypt = 0;
7219 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007220
Dmitry Shmidt56052862013-10-04 10:23:25 -07007221 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007222 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007223 noack, freq, no_cck, offchanok,
7224 wait_time);
7225}
7226
7227
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007228static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7229 int slot, int ht_opmode, int ap_isolate,
7230 int *basic_rates)
7231{
7232 struct wpa_driver_nl80211_data *drv = bss->drv;
7233 struct nl_msg *msg;
7234
7235 msg = nlmsg_alloc();
7236 if (!msg)
7237 return -ENOMEM;
7238
7239 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7240
7241 if (cts >= 0)
7242 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7243 if (preamble >= 0)
7244 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7245 if (slot >= 0)
7246 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7247 if (ht_opmode >= 0)
7248 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7249 if (ap_isolate >= 0)
7250 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7251
7252 if (basic_rates) {
7253 u8 rates[NL80211_MAX_SUPP_RATES];
7254 u8 rates_len = 0;
7255 int i;
7256
7257 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7258 i++)
7259 rates[rates_len++] = basic_rates[i] / 5;
7260
7261 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7262 }
7263
7264 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7265
7266 return send_and_recv_msgs(drv, msg, NULL, NULL);
7267 nla_put_failure:
7268 nlmsg_free(msg);
7269 return -ENOBUFS;
7270}
7271
7272
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007273static int wpa_driver_nl80211_set_acl(void *priv,
7274 struct hostapd_acl_params *params)
7275{
7276 struct i802_bss *bss = priv;
7277 struct wpa_driver_nl80211_data *drv = bss->drv;
7278 struct nl_msg *msg;
7279 struct nlattr *acl;
7280 unsigned int i;
7281 int ret = 0;
7282
7283 if (!(drv->capa.max_acl_mac_addrs))
7284 return -ENOTSUP;
7285
7286 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7287 return -ENOTSUP;
7288
7289 msg = nlmsg_alloc();
7290 if (!msg)
7291 return -ENOMEM;
7292
7293 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7294 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7295
7296 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7297
7298 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7299
7300 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7301 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7302 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7303
7304 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7305 if (acl == NULL)
7306 goto nla_put_failure;
7307
7308 for (i = 0; i < params->num_mac_acl; i++)
7309 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7310
7311 nla_nest_end(msg, acl);
7312
7313 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7314 msg = NULL;
7315 if (ret) {
7316 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7317 ret, strerror(-ret));
7318 }
7319
7320nla_put_failure:
7321 nlmsg_free(msg);
7322
7323 return ret;
7324}
7325
7326
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327static int wpa_driver_nl80211_set_ap(void *priv,
7328 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007329{
7330 struct i802_bss *bss = priv;
7331 struct wpa_driver_nl80211_data *drv = bss->drv;
7332 struct nl_msg *msg;
7333 u8 cmd = NL80211_CMD_NEW_BEACON;
7334 int ret;
7335 int beacon_set;
7336 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007337 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007338 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007339 u32 ver;
7340
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007341 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007342
7343 msg = nlmsg_alloc();
7344 if (!msg)
7345 return -ENOMEM;
7346
7347 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7348 beacon_set);
7349 if (beacon_set)
7350 cmd = NL80211_CMD_SET_BEACON;
7351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007352 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007353 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7354 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007355 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007356 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7357 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007358 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007359 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007361 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007362 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007363 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007364 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007365 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7366 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007367 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7368 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007369 if (params->proberesp && params->proberesp_len) {
7370 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7371 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007372 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7373 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007374 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007375 switch (params->hide_ssid) {
7376 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007377 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007378 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7379 NL80211_HIDDEN_SSID_NOT_IN_USE);
7380 break;
7381 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007382 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007383 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7384 NL80211_HIDDEN_SSID_ZERO_LEN);
7385 break;
7386 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007387 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007388 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7389 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7390 break;
7391 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007392 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007393 if (params->privacy)
7394 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007395 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007396 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7397 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7398 /* Leave out the attribute */
7399 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7400 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7401 NL80211_AUTHTYPE_SHARED_KEY);
7402 else
7403 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7404 NL80211_AUTHTYPE_OPEN_SYSTEM);
7405
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007406 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007407 ver = 0;
7408 if (params->wpa_version & WPA_PROTO_WPA)
7409 ver |= NL80211_WPA_VERSION_1;
7410 if (params->wpa_version & WPA_PROTO_RSN)
7411 ver |= NL80211_WPA_VERSION_2;
7412 if (ver)
7413 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7414
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007415 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7416 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007417 num_suites = 0;
7418 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7419 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7420 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7421 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7422 if (num_suites) {
7423 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7424 num_suites * sizeof(u32), suites);
7425 }
7426
7427 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7428 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7429 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7430
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007431 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7432 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007433 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7434 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007435 if (num_suites) {
7436 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7437 num_suites * sizeof(u32), suites);
7438 }
7439
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007440 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7441 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007442 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7443 if (suite)
7444 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007445
7446 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007447 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7448 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007449 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7450 wpabuf_head(params->beacon_ies));
7451 }
7452 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007453 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7454 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007455 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7456 wpabuf_len(params->proberesp_ies),
7457 wpabuf_head(params->proberesp_ies));
7458 }
7459 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007460 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7461 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007462 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7463 wpabuf_len(params->assocresp_ies),
7464 wpabuf_head(params->assocresp_ies));
7465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466
Dmitry Shmidt04949592012-07-19 12:16:46 -07007467 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007468 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7469 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007470 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7471 params->ap_max_inactivity);
7472 }
7473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7475 if (ret) {
7476 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7477 ret, strerror(-ret));
7478 } else {
7479 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007480 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7481 params->short_slot_time, params->ht_opmode,
7482 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007483 if (beacon_set && params->freq &&
7484 params->freq->bandwidth != bss->bandwidth) {
7485 wpa_printf(MSG_DEBUG,
7486 "nl80211: Update BSS %s bandwidth: %d -> %d",
7487 bss->ifname, bss->bandwidth,
7488 params->freq->bandwidth);
7489 ret = nl80211_set_channel(bss, params->freq, 1);
7490 if (ret) {
7491 wpa_printf(MSG_DEBUG,
7492 "nl80211: Frequency set failed: %d (%s)",
7493 ret, strerror(-ret));
7494 } else {
7495 wpa_printf(MSG_DEBUG,
7496 "nl80211: Frequency set succeeded for ht2040 coex");
7497 bss->bandwidth = params->freq->bandwidth;
7498 }
7499 } else if (!beacon_set) {
7500 /*
7501 * cfg80211 updates the driver on frequence change in AP
7502 * mode only at the point when beaconing is started, so
7503 * set the initial value here.
7504 */
7505 bss->bandwidth = params->freq->bandwidth;
7506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507 }
7508 return ret;
7509 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007510 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511 return -ENOBUFS;
7512}
7513
7514
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007515static int nl80211_put_freq_params(struct nl_msg *msg,
7516 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007517{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007518 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7519 if (freq->vht_enabled) {
7520 switch (freq->bandwidth) {
7521 case 20:
7522 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7523 NL80211_CHAN_WIDTH_20);
7524 break;
7525 case 40:
7526 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7527 NL80211_CHAN_WIDTH_40);
7528 break;
7529 case 80:
7530 if (freq->center_freq2)
7531 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7532 NL80211_CHAN_WIDTH_80P80);
7533 else
7534 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7535 NL80211_CHAN_WIDTH_80);
7536 break;
7537 case 160:
7538 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7539 NL80211_CHAN_WIDTH_160);
7540 break;
7541 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007542 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007543 }
7544 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7545 if (freq->center_freq2)
7546 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7547 freq->center_freq2);
7548 } else if (freq->ht_enabled) {
7549 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 case -1:
7551 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7552 NL80211_CHAN_HT40MINUS);
7553 break;
7554 case 1:
7555 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7556 NL80211_CHAN_HT40PLUS);
7557 break;
7558 default:
7559 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7560 NL80211_CHAN_HT20);
7561 break;
7562 }
7563 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007564 return 0;
7565
7566nla_put_failure:
7567 return -ENOBUFS;
7568}
7569
7570
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007571static int nl80211_set_channel(struct i802_bss *bss,
7572 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007573{
7574 struct wpa_driver_nl80211_data *drv = bss->drv;
7575 struct nl_msg *msg;
7576 int ret;
7577
7578 wpa_printf(MSG_DEBUG,
7579 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7580 freq->freq, freq->ht_enabled, freq->vht_enabled,
7581 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7582 msg = nlmsg_alloc();
7583 if (!msg)
7584 return -1;
7585
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007586 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7587 NL80211_CMD_SET_WIPHY);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007588
7589 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7590 if (nl80211_put_freq_params(msg, freq) < 0)
7591 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592
7593 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007594 msg = NULL;
7595 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007596 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007600 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007602 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007603 return -1;
7604}
7605
7606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007607static u32 sta_flags_nl80211(int flags)
7608{
7609 u32 f = 0;
7610
7611 if (flags & WPA_STA_AUTHORIZED)
7612 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7613 if (flags & WPA_STA_WMM)
7614 f |= BIT(NL80211_STA_FLAG_WME);
7615 if (flags & WPA_STA_SHORT_PREAMBLE)
7616 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7617 if (flags & WPA_STA_MFP)
7618 f |= BIT(NL80211_STA_FLAG_MFP);
7619 if (flags & WPA_STA_TDLS_PEER)
7620 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7621
7622 return f;
7623}
7624
7625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007626static int wpa_driver_nl80211_sta_add(void *priv,
7627 struct hostapd_sta_add_params *params)
7628{
7629 struct i802_bss *bss = priv;
7630 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007631 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007632 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633 int ret = -ENOBUFS;
7634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007635 if ((params->flags & WPA_STA_TDLS_PEER) &&
7636 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7637 return -EOPNOTSUPP;
7638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007639 msg = nlmsg_alloc();
7640 if (!msg)
7641 return -ENOMEM;
7642
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007643 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7644 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007645 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7646 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647
7648 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7649 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007650 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7651 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007652 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7653 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007654 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007655 if (params->aid) {
7656 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7657 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7658 } else {
7659 /*
7660 * cfg80211 validates that AID is non-zero, so we have
7661 * to make this a non-zero value for the TDLS case where
7662 * a dummy STA entry is used for now.
7663 */
7664 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7665 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7666 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007667 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7668 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007669 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7670 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007671 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7672 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7673 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007674 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007675 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007676 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7677 (u8 *) params->ht_capabilities,
7678 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007679 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7680 sizeof(*params->ht_capabilities),
7681 params->ht_capabilities);
7682 }
7683
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007684 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007685 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7686 (u8 *) params->vht_capabilities,
7687 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007688 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7689 sizeof(*params->vht_capabilities),
7690 params->vht_capabilities);
7691 }
7692
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007693 if (params->vht_opmode_enabled) {
7694 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7695 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7696 params->vht_opmode);
7697 }
7698
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007699 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7700 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7701
7702 if (params->ext_capab) {
7703 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7704 params->ext_capab, params->ext_capab_len);
7705 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7706 params->ext_capab_len, params->ext_capab);
7707 }
7708
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007709 if (params->supp_channels) {
7710 wpa_hexdump(MSG_DEBUG, " * supported channels",
7711 params->supp_channels, params->supp_channels_len);
7712 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7713 params->supp_channels_len, params->supp_channels);
7714 }
7715
7716 if (params->supp_oper_classes) {
7717 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7718 params->supp_oper_classes,
7719 params->supp_oper_classes_len);
7720 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7721 params->supp_oper_classes_len,
7722 params->supp_oper_classes);
7723 }
7724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007725 os_memset(&upd, 0, sizeof(upd));
7726 upd.mask = sta_flags_nl80211(params->flags);
7727 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007728 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7729 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007730 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7731
7732 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007733 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007735 if (!wme)
7736 goto nla_put_failure;
7737
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007738 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007739 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007740 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007741 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007742 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007743 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007744 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007745 }
7746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007748 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007749 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007750 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7751 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7752 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007753 if (ret == -EEXIST)
7754 ret = 0;
7755 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007756 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757 return ret;
7758}
7759
7760
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007761static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
7762{
7763#ifdef CONFIG_LIBNL3_ROUTE
7764 struct wpa_driver_nl80211_data *drv = bss->drv;
7765 struct rtnl_neigh *rn;
7766 struct nl_addr *nl_addr;
7767 int err;
7768
7769 rn = rtnl_neigh_alloc();
7770 if (!rn)
7771 return;
7772
7773 rtnl_neigh_set_family(rn, AF_BRIDGE);
7774 rtnl_neigh_set_ifindex(rn, bss->ifindex);
7775 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
7776 if (!nl_addr) {
7777 rtnl_neigh_put(rn);
7778 return;
7779 }
7780 rtnl_neigh_set_lladdr(rn, nl_addr);
7781
7782 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
7783 if (err < 0) {
7784 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
7785 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
7786 bss->ifindex, nl_geterror(err));
7787 } else {
7788 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
7789 MACSTR, MAC2STR(addr));
7790 }
7791
7792 nl_addr_put(nl_addr);
7793 rtnl_neigh_put(rn);
7794#endif /* CONFIG_LIBNL3_ROUTE */
7795}
7796
7797
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007798static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007800 struct wpa_driver_nl80211_data *drv = bss->drv;
7801 struct nl_msg *msg;
7802 int ret;
7803
7804 msg = nlmsg_alloc();
7805 if (!msg)
7806 return -ENOMEM;
7807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007808 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007809
7810 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7811 if_nametoindex(bss->ifname));
7812 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7813
7814 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007815 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7816 " --> %d (%s)",
7817 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007818
7819 if (drv->rtnl_sk)
7820 rtnl_neigh_delete_fdb_entry(bss, addr);
7821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007822 if (ret == -ENOENT)
7823 return 0;
7824 return ret;
7825 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007826 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007827 return -ENOBUFS;
7828}
7829
7830
7831static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7832 int ifidx)
7833{
7834 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007835 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007836
7837 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007839 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007840 dl_list_for_each(drv2, &drv->global->interfaces,
7841 struct wpa_driver_nl80211_data, list)
7842 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007843
7844 msg = nlmsg_alloc();
7845 if (!msg)
7846 goto nla_put_failure;
7847
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007848 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007849 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7850
7851 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7852 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007853 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007854 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007855 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007856 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7857}
7858
7859
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007860static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7861{
7862 switch (mode) {
7863 case NL80211_IFTYPE_ADHOC:
7864 return "ADHOC";
7865 case NL80211_IFTYPE_STATION:
7866 return "STATION";
7867 case NL80211_IFTYPE_AP:
7868 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007869 case NL80211_IFTYPE_AP_VLAN:
7870 return "AP_VLAN";
7871 case NL80211_IFTYPE_WDS:
7872 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007873 case NL80211_IFTYPE_MONITOR:
7874 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007875 case NL80211_IFTYPE_MESH_POINT:
7876 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007877 case NL80211_IFTYPE_P2P_CLIENT:
7878 return "P2P_CLIENT";
7879 case NL80211_IFTYPE_P2P_GO:
7880 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007881 case NL80211_IFTYPE_P2P_DEVICE:
7882 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007883 default:
7884 return "unknown";
7885 }
7886}
7887
7888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7890 const char *ifname,
7891 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007892 const u8 *addr, int wds,
7893 int (*handler)(struct nl_msg *, void *),
7894 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007895{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007896 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007897 int ifidx;
7898 int ret = -ENOBUFS;
7899
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007900 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7901 iftype, nl80211_iftype_str(iftype));
7902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007903 msg = nlmsg_alloc();
7904 if (!msg)
7905 return -1;
7906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007907 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007908 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007909 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007910 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7911 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7912
7913 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007914 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007915
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007916 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007917 if (!flags)
7918 goto nla_put_failure;
7919
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007920 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007921
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007922 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007923 } else if (wds) {
7924 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7925 }
7926
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007927 /*
7928 * Tell cfg80211 that the interface belongs to the socket that created
7929 * it, and the interface should be deleted when the socket is closed.
7930 */
7931 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
7932
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007933 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007934 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007935 if (ret) {
7936 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007937 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7939 ifname, ret, strerror(-ret));
7940 return ret;
7941 }
7942
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007943 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7944 return 0;
7945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007946 ifidx = if_nametoindex(ifname);
7947 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7948 ifname, ifidx);
7949
7950 if (ifidx <= 0)
7951 return -1;
7952
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007953 /*
7954 * Some virtual interfaces need to process EAPOL packets and events on
7955 * the parent interface. This is used mainly with hostapd.
7956 */
7957 if (drv->hostapd ||
7958 iftype == NL80211_IFTYPE_AP_VLAN ||
7959 iftype == NL80211_IFTYPE_WDS ||
7960 iftype == NL80211_IFTYPE_MONITOR) {
7961 /* start listening for EAPOL on this interface */
7962 add_ifidx(drv, ifidx);
7963 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007964
7965 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007966 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967 nl80211_remove_iface(drv, ifidx);
7968 return -1;
7969 }
7970
7971 return ifidx;
7972}
7973
7974
7975static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
7976 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007977 const u8 *addr, int wds,
7978 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007979 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980{
7981 int ret;
7982
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007983 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
7984 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007987 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007988 if (use_existing) {
7989 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
7990 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007991 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
7992 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7993 addr) < 0 &&
7994 (linux_set_iface_flags(drv->global->ioctl_sock,
7995 ifname, 0) < 0 ||
7996 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7997 addr) < 0 ||
7998 linux_set_iface_flags(drv->global->ioctl_sock,
7999 ifname, 1) < 0))
8000 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008001 return -ENFILE;
8002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
8004
8005 /* Try to remove the interface that was already there. */
8006 nl80211_remove_iface(drv, if_nametoindex(ifname));
8007
8008 /* Try to create the interface again */
8009 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008010 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008011 }
8012
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008013 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014 nl80211_disable_11b_rates(drv, ret, 1);
8015
8016 return ret;
8017}
8018
8019
8020static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
8021{
8022 struct ieee80211_hdr *hdr;
8023 u16 fc;
8024 union wpa_event_data event;
8025
8026 hdr = (struct ieee80211_hdr *) buf;
8027 fc = le_to_host16(hdr->frame_control);
8028
8029 os_memset(&event, 0, sizeof(event));
8030 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
8031 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
8032 event.tx_status.dst = hdr->addr1;
8033 event.tx_status.data = buf;
8034 event.tx_status.data_len = len;
8035 event.tx_status.ack = ok;
8036 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
8037}
8038
8039
8040static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
8041 u8 *buf, size_t len)
8042{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008043 struct ieee80211_hdr *hdr = (void *)buf;
8044 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008046
8047 if (len < sizeof(*hdr))
8048 return;
8049
8050 fc = le_to_host16(hdr->frame_control);
8051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008053 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
8054 event.rx_from_unknown.addr = hdr->addr2;
8055 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
8056 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008057 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
8058}
8059
8060
8061static void handle_frame(struct wpa_driver_nl80211_data *drv,
8062 u8 *buf, size_t len, int datarate, int ssi_signal)
8063{
8064 struct ieee80211_hdr *hdr;
8065 u16 fc;
8066 union wpa_event_data event;
8067
8068 hdr = (struct ieee80211_hdr *) buf;
8069 fc = le_to_host16(hdr->frame_control);
8070
8071 switch (WLAN_FC_GET_TYPE(fc)) {
8072 case WLAN_FC_TYPE_MGMT:
8073 os_memset(&event, 0, sizeof(event));
8074 event.rx_mgmt.frame = buf;
8075 event.rx_mgmt.frame_len = len;
8076 event.rx_mgmt.datarate = datarate;
8077 event.rx_mgmt.ssi_signal = ssi_signal;
8078 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
8079 break;
8080 case WLAN_FC_TYPE_CTRL:
8081 /* can only get here with PS-Poll frames */
8082 wpa_printf(MSG_DEBUG, "CTRL");
8083 from_unknown_sta(drv, buf, len);
8084 break;
8085 case WLAN_FC_TYPE_DATA:
8086 from_unknown_sta(drv, buf, len);
8087 break;
8088 }
8089}
8090
8091
8092static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
8093{
8094 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8095 int len;
8096 unsigned char buf[3000];
8097 struct ieee80211_radiotap_iterator iter;
8098 int ret;
8099 int datarate = 0, ssi_signal = 0;
8100 int injected = 0, failed = 0, rxflags = 0;
8101
8102 len = recv(sock, buf, sizeof(buf), 0);
8103 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008104 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
8105 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008106 return;
8107 }
8108
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008109 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008110 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008111 return;
8112 }
8113
8114 while (1) {
8115 ret = ieee80211_radiotap_iterator_next(&iter);
8116 if (ret == -ENOENT)
8117 break;
8118 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008119 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
8120 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008121 return;
8122 }
8123 switch (iter.this_arg_index) {
8124 case IEEE80211_RADIOTAP_FLAGS:
8125 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
8126 len -= 4;
8127 break;
8128 case IEEE80211_RADIOTAP_RX_FLAGS:
8129 rxflags = 1;
8130 break;
8131 case IEEE80211_RADIOTAP_TX_FLAGS:
8132 injected = 1;
8133 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
8134 IEEE80211_RADIOTAP_F_TX_FAIL;
8135 break;
8136 case IEEE80211_RADIOTAP_DATA_RETRIES:
8137 break;
8138 case IEEE80211_RADIOTAP_CHANNEL:
8139 /* TODO: convert from freq/flags to channel number */
8140 break;
8141 case IEEE80211_RADIOTAP_RATE:
8142 datarate = *iter.this_arg * 5;
8143 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008144 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
8145 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008146 break;
8147 }
8148 }
8149
8150 if (rxflags && injected)
8151 return;
8152
8153 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008154 handle_frame(drv, buf + iter._max_length,
8155 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008156 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008157 handle_tx_callback(drv->ctx, buf + iter._max_length,
8158 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008159}
8160
8161
8162/*
8163 * we post-process the filter code later and rewrite
8164 * this to the offset to the last instruction
8165 */
8166#define PASS 0xFF
8167#define FAIL 0xFE
8168
8169static struct sock_filter msock_filter_insns[] = {
8170 /*
8171 * do a little-endian load of the radiotap length field
8172 */
8173 /* load lower byte into A */
8174 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
8175 /* put it into X (== index register) */
8176 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8177 /* load upper byte into A */
8178 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8179 /* left-shift it by 8 */
8180 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8181 /* or with X */
8182 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8183 /* put result into X */
8184 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8185
8186 /*
8187 * Allow management frames through, this also gives us those
8188 * management frames that we sent ourselves with status
8189 */
8190 /* load the lower byte of the IEEE 802.11 frame control field */
8191 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8192 /* mask off frame type and version */
8193 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8194 /* accept frame if it's both 0, fall through otherwise */
8195 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8196
8197 /*
8198 * TODO: add a bit to radiotap RX flags that indicates
8199 * that the sending station is not associated, then
8200 * add a filter here that filters on our DA and that flag
8201 * to allow us to deauth frames to that bad station.
8202 *
8203 * For now allow all To DS data frames through.
8204 */
8205 /* load the IEEE 802.11 frame control field */
8206 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8207 /* mask off frame type, version and DS status */
8208 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8209 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8210 */
8211 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
8212
8213#if 0
8214 /*
8215 * drop non-data frames
8216 */
8217 /* load the lower byte of the frame control field */
8218 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8219 /* mask off QoS bit */
8220 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8221 /* drop non-data frames */
8222 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
8223#endif
8224 /* load the upper byte of the frame control field */
8225 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
8226 /* mask off toDS/fromDS */
8227 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
8228 /* accept WDS frames */
8229 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
8230
8231 /*
8232 * add header length to index
8233 */
8234 /* load the lower byte of the frame control field */
8235 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8236 /* mask off QoS bit */
8237 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8238 /* right shift it by 6 to give 0 or 2 */
8239 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8240 /* add data frame header length */
8241 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8242 /* add index, was start of 802.11 header */
8243 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8244 /* move to index, now start of LL header */
8245 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8246
8247 /*
8248 * Accept empty data frames, we use those for
8249 * polling activity.
8250 */
8251 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8252 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8253
8254 /*
8255 * Accept EAPOL frames
8256 */
8257 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8258 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8259 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8260 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8261
8262 /* keep these last two statements or change the code below */
8263 /* return 0 == "DROP" */
8264 BPF_STMT(BPF_RET | BPF_K, 0),
8265 /* return ~0 == "keep all" */
8266 BPF_STMT(BPF_RET | BPF_K, ~0),
8267};
8268
8269static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008270 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008271 .filter = msock_filter_insns,
8272};
8273
8274
8275static int add_monitor_filter(int s)
8276{
8277 int idx;
8278
8279 /* rewrite all PASS/FAIL jump offsets */
8280 for (idx = 0; idx < msock_filter.len; idx++) {
8281 struct sock_filter *insn = &msock_filter_insns[idx];
8282
8283 if (BPF_CLASS(insn->code) == BPF_JMP) {
8284 if (insn->code == (BPF_JMP|BPF_JA)) {
8285 if (insn->k == PASS)
8286 insn->k = msock_filter.len - idx - 2;
8287 else if (insn->k == FAIL)
8288 insn->k = msock_filter.len - idx - 3;
8289 }
8290
8291 if (insn->jt == PASS)
8292 insn->jt = msock_filter.len - idx - 2;
8293 else if (insn->jt == FAIL)
8294 insn->jt = msock_filter.len - idx - 3;
8295
8296 if (insn->jf == PASS)
8297 insn->jf = msock_filter.len - idx - 2;
8298 else if (insn->jf == FAIL)
8299 insn->jf = msock_filter.len - idx - 3;
8300 }
8301 }
8302
8303 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8304 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008305 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8306 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008307 return -1;
8308 }
8309
8310 return 0;
8311}
8312
8313
8314static void nl80211_remove_monitor_interface(
8315 struct wpa_driver_nl80211_data *drv)
8316{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008317 if (drv->monitor_refcount > 0)
8318 drv->monitor_refcount--;
8319 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8320 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008321 if (drv->monitor_refcount > 0)
8322 return;
8323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324 if (drv->monitor_ifidx >= 0) {
8325 nl80211_remove_iface(drv, drv->monitor_ifidx);
8326 drv->monitor_ifidx = -1;
8327 }
8328 if (drv->monitor_sock >= 0) {
8329 eloop_unregister_read_sock(drv->monitor_sock);
8330 close(drv->monitor_sock);
8331 drv->monitor_sock = -1;
8332 }
8333}
8334
8335
8336static int
8337nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8338{
8339 char buf[IFNAMSIZ];
8340 struct sockaddr_ll ll;
8341 int optval;
8342 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008343
8344 if (drv->monitor_ifidx >= 0) {
8345 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008346 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8347 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008348 return 0;
8349 }
8350
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008351 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008352 /*
8353 * P2P interface name is of the format p2p-%s-%d. For monitor
8354 * interface name corresponding to P2P GO, replace "p2p-" with
8355 * "mon-" to retain the same interface name length and to
8356 * indicate that it is a monitor interface.
8357 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008358 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008359 } else {
8360 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008361 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008362 }
8363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008364 buf[IFNAMSIZ - 1] = '\0';
8365
8366 drv->monitor_ifidx =
8367 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008368 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008369
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008370 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008371 /*
8372 * This is backward compatibility for a few versions of
8373 * the kernel only that didn't advertise the right
8374 * attributes for the only driver that then supported
8375 * AP mode w/o monitor -- ath6kl.
8376 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008377 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8378 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008379 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008380 }
8381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008382 if (drv->monitor_ifidx < 0)
8383 return -1;
8384
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008385 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008386 goto error;
8387
8388 memset(&ll, 0, sizeof(ll));
8389 ll.sll_family = AF_PACKET;
8390 ll.sll_ifindex = drv->monitor_ifidx;
8391 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8392 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008393 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8394 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008395 goto error;
8396 }
8397
8398 if (add_monitor_filter(drv->monitor_sock)) {
8399 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8400 "interface; do filtering in user space");
8401 /* This works, but will cost in performance. */
8402 }
8403
8404 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008405 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8406 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008407 goto error;
8408 }
8409
8410 optlen = sizeof(optval);
8411 optval = 20;
8412 if (setsockopt
8413 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008414 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8415 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008416 goto error;
8417 }
8418
8419 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8420 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008421 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008422 goto error;
8423 }
8424
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008425 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008426 return 0;
8427 error:
8428 nl80211_remove_monitor_interface(drv);
8429 return -1;
8430}
8431
8432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008433static int nl80211_setup_ap(struct i802_bss *bss)
8434{
8435 struct wpa_driver_nl80211_data *drv = bss->drv;
8436
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008437 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8438 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008439
8440 /*
8441 * Disable Probe Request reporting unless we need it in this way for
8442 * devices that include the AP SME, in the other case (unless using
8443 * monitor iface) we'll get it through the nl_mgmt socket instead.
8444 */
8445 if (!drv->device_ap_sme)
8446 wpa_driver_nl80211_probe_req_report(bss, 0);
8447
8448 if (!drv->device_ap_sme && !drv->use_monitor)
8449 if (nl80211_mgmt_subscribe_ap(bss))
8450 return -1;
8451
8452 if (drv->device_ap_sme && !drv->use_monitor)
8453 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8454 return -1;
8455
8456 if (!drv->device_ap_sme && drv->use_monitor &&
8457 nl80211_create_monitor_interface(drv) &&
8458 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008459 return -1;
8460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008461 if (drv->device_ap_sme &&
8462 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8463 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8464 "Probe Request frame reporting in AP mode");
8465 /* Try to survive without this */
8466 }
8467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008468 return 0;
8469}
8470
8471
8472static void nl80211_teardown_ap(struct i802_bss *bss)
8473{
8474 struct wpa_driver_nl80211_data *drv = bss->drv;
8475
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008476 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8477 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008478 if (drv->device_ap_sme) {
8479 wpa_driver_nl80211_probe_req_report(bss, 0);
8480 if (!drv->use_monitor)
8481 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8482 } else if (drv->use_monitor)
8483 nl80211_remove_monitor_interface(drv);
8484 else
8485 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8486
8487 bss->beacon_set = 0;
8488}
8489
8490
8491static int nl80211_send_eapol_data(struct i802_bss *bss,
8492 const u8 *addr, const u8 *data,
8493 size_t data_len)
8494{
8495 struct sockaddr_ll ll;
8496 int ret;
8497
8498 if (bss->drv->eapol_tx_sock < 0) {
8499 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8500 return -1;
8501 }
8502
8503 os_memset(&ll, 0, sizeof(ll));
8504 ll.sll_family = AF_PACKET;
8505 ll.sll_ifindex = bss->ifindex;
8506 ll.sll_protocol = htons(ETH_P_PAE);
8507 ll.sll_halen = ETH_ALEN;
8508 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8509 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8510 (struct sockaddr *) &ll, sizeof(ll));
8511 if (ret < 0)
8512 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8513 strerror(errno));
8514
8515 return ret;
8516}
8517
8518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008519static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8520
8521static int wpa_driver_nl80211_hapd_send_eapol(
8522 void *priv, const u8 *addr, const u8 *data,
8523 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8524{
8525 struct i802_bss *bss = priv;
8526 struct wpa_driver_nl80211_data *drv = bss->drv;
8527 struct ieee80211_hdr *hdr;
8528 size_t len;
8529 u8 *pos;
8530 int res;
8531 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008533 if (drv->device_ap_sme || !drv->use_monitor)
8534 return nl80211_send_eapol_data(bss, addr, data, data_len);
8535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008536 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8537 data_len;
8538 hdr = os_zalloc(len);
8539 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008540 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8541 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008542 return -1;
8543 }
8544
8545 hdr->frame_control =
8546 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8547 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8548 if (encrypt)
8549 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8550 if (qos) {
8551 hdr->frame_control |=
8552 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8553 }
8554
8555 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8556 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8557 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8558 pos = (u8 *) (hdr + 1);
8559
8560 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008561 /* Set highest priority in QoS header */
8562 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008563 pos[1] = 0;
8564 pos += 2;
8565 }
8566
8567 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8568 pos += sizeof(rfc1042_header);
8569 WPA_PUT_BE16(pos, ETH_P_PAE);
8570 pos += 2;
8571 memcpy(pos, data, data_len);
8572
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008573 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8574 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008575 if (res < 0) {
8576 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8577 "failed: %d (%s)",
8578 (unsigned long) len, errno, strerror(errno));
8579 }
8580 os_free(hdr);
8581
8582 return res;
8583}
8584
8585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008586static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8587 int total_flags,
8588 int flags_or, int flags_and)
8589{
8590 struct i802_bss *bss = priv;
8591 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008592 struct nl_msg *msg;
8593 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008594 struct nl80211_sta_flag_update upd;
8595
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008596 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
8597 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
8598 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
8599 !!(total_flags & WPA_STA_AUTHORIZED));
8600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008601 msg = nlmsg_alloc();
8602 if (!msg)
8603 return -ENOMEM;
8604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008605 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008606
8607 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8608 if_nametoindex(bss->ifname));
8609 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8610
8611 /*
8612 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8613 * can be removed eventually.
8614 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008615 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8616 if (!flags)
8617 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008618 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008619 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008620
8621 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008622 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008623
8624 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008625 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008626
8627 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008628 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008630 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008631 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008632
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008633 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008634
8635 os_memset(&upd, 0, sizeof(upd));
8636 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8637 upd.set = sta_flags_nl80211(flags_or);
8638 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008640 return send_and_recv_msgs(drv, msg, NULL, NULL);
8641 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008642 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008643 return -ENOBUFS;
8644}
8645
8646
8647static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8648 struct wpa_driver_associate_params *params)
8649{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008650 enum nl80211_iftype nlmode, old_mode;
8651 struct hostapd_freq_params freq = {
8652 .freq = params->freq,
8653 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008654
8655 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008656 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8657 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008658 nlmode = NL80211_IFTYPE_P2P_GO;
8659 } else
8660 nlmode = NL80211_IFTYPE_AP;
8661
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008662 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008663 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008664 nl80211_remove_monitor_interface(drv);
8665 return -1;
8666 }
8667
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008668 if (nl80211_set_channel(drv->first_bss, &freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008669 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008670 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008671 nl80211_remove_monitor_interface(drv);
8672 return -1;
8673 }
8674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675 return 0;
8676}
8677
8678
8679static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8680{
8681 struct nl_msg *msg;
8682 int ret = -1;
8683
8684 msg = nlmsg_alloc();
8685 if (!msg)
8686 return -1;
8687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008688 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008689 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8690 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8691 msg = NULL;
8692 if (ret) {
8693 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8694 "(%s)", ret, strerror(-ret));
8695 goto nla_put_failure;
8696 }
8697
8698 ret = 0;
8699 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8700
8701nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008702 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008703 NL80211_IFTYPE_STATION)) {
8704 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8705 "station mode");
8706 }
8707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008708 nlmsg_free(msg);
8709 return ret;
8710}
8711
8712
8713static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8714 struct wpa_driver_associate_params *params)
8715{
8716 struct nl_msg *msg;
8717 int ret = -1;
8718 int count = 0;
8719
8720 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8721
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07008722 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008723 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8724 "IBSS mode");
8725 return -1;
8726 }
8727
8728retry:
8729 msg = nlmsg_alloc();
8730 if (!msg)
8731 return -1;
8732
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008733 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008734 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8735
8736 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8737 goto nla_put_failure;
8738
8739 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8740 params->ssid, params->ssid_len);
8741 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8742 params->ssid);
8743 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8744 drv->ssid_len = params->ssid_len;
8745
8746 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8747 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
8748
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008749 if (params->beacon_int > 0) {
8750 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8751 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8752 params->beacon_int);
8753 }
8754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008755 ret = nl80211_set_conn_keys(params, msg);
8756 if (ret)
8757 goto nla_put_failure;
8758
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008759 if (params->bssid && params->fixed_bssid) {
8760 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8761 MAC2STR(params->bssid));
8762 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8763 }
8764
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008765 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8766 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8767 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8768 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008769 wpa_printf(MSG_DEBUG, " * control port");
8770 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8771 }
8772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008773 if (params->wpa_ie) {
8774 wpa_hexdump(MSG_DEBUG,
8775 " * Extra IEs for Beacon/Probe Response frames",
8776 params->wpa_ie, params->wpa_ie_len);
8777 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8778 params->wpa_ie);
8779 }
8780
8781 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8782 msg = NULL;
8783 if (ret) {
8784 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8785 ret, strerror(-ret));
8786 count++;
8787 if (ret == -EALREADY && count == 1) {
8788 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8789 "forced leave");
8790 nl80211_leave_ibss(drv);
8791 nlmsg_free(msg);
8792 goto retry;
8793 }
8794
8795 goto nla_put_failure;
8796 }
8797 ret = 0;
8798 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8799
8800nla_put_failure:
8801 nlmsg_free(msg);
8802 return ret;
8803}
8804
8805
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008806static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8807 struct wpa_driver_associate_params *params,
8808 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008809{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008810 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008812 if (params->bssid) {
8813 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8814 MAC2STR(params->bssid));
8815 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8816 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008817
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008818 if (params->bssid_hint) {
8819 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8820 MAC2STR(params->bssid_hint));
8821 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8822 params->bssid_hint);
8823 }
8824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008825 if (params->freq) {
8826 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
8827 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008828 drv->assoc_freq = params->freq;
8829 } else
8830 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008831
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008832 if (params->freq_hint) {
8833 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8834 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8835 params->freq_hint);
8836 }
8837
Dmitry Shmidt04949592012-07-19 12:16:46 -07008838 if (params->bg_scan_period >= 0) {
8839 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8840 params->bg_scan_period);
8841 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8842 params->bg_scan_period);
8843 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008845 if (params->ssid) {
8846 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8847 params->ssid, params->ssid_len);
8848 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8849 params->ssid);
8850 if (params->ssid_len > sizeof(drv->ssid))
8851 goto nla_put_failure;
8852 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8853 drv->ssid_len = params->ssid_len;
8854 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008856 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8857 if (params->wpa_ie)
8858 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8859 params->wpa_ie);
8860
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008861 if (params->wpa_proto) {
8862 enum nl80211_wpa_versions ver = 0;
8863
8864 if (params->wpa_proto & WPA_PROTO_WPA)
8865 ver |= NL80211_WPA_VERSION_1;
8866 if (params->wpa_proto & WPA_PROTO_RSN)
8867 ver |= NL80211_WPA_VERSION_2;
8868
8869 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8870 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8871 }
8872
8873 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8874 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8875 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8876 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8877 }
8878
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008879 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8880 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8881 /*
8882 * This is likely to work even though many drivers do not
8883 * advertise support for operations without GTK.
8884 */
8885 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8886 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008887 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8888 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8889 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8890 }
8891
8892 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8893 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8894 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8895 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008896 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008897 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
8898 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8899 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008900 int mgmt = WLAN_AKM_SUITE_PSK;
8901
8902 switch (params->key_mgmt_suite) {
8903 case WPA_KEY_MGMT_CCKM:
8904 mgmt = WLAN_AKM_SUITE_CCKM;
8905 break;
8906 case WPA_KEY_MGMT_IEEE8021X:
8907 mgmt = WLAN_AKM_SUITE_8021X;
8908 break;
8909 case WPA_KEY_MGMT_FT_IEEE8021X:
8910 mgmt = WLAN_AKM_SUITE_FT_8021X;
8911 break;
8912 case WPA_KEY_MGMT_FT_PSK:
8913 mgmt = WLAN_AKM_SUITE_FT_PSK;
8914 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008915 case WPA_KEY_MGMT_IEEE8021X_SHA256:
8916 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
8917 break;
8918 case WPA_KEY_MGMT_PSK_SHA256:
8919 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
8920 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008921 case WPA_KEY_MGMT_OSEN:
8922 mgmt = WLAN_AKM_SUITE_OSEN;
8923 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008924 case WPA_KEY_MGMT_PSK:
8925 default:
8926 mgmt = WLAN_AKM_SUITE_PSK;
8927 break;
8928 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008929 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008930 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8931 }
8932
8933 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8934
8935 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8936 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8937
8938 if (params->disable_ht)
8939 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8940
8941 if (params->htcaps && params->htcaps_mask) {
8942 int sz = sizeof(struct ieee80211_ht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008943 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008944 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008945 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
8946 params->htcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008947 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8948 params->htcaps_mask);
8949 }
8950
8951#ifdef CONFIG_VHT_OVERRIDES
8952 if (params->disable_vht) {
8953 wpa_printf(MSG_DEBUG, " * VHT disabled");
8954 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8955 }
8956
8957 if (params->vhtcaps && params->vhtcaps_mask) {
8958 int sz = sizeof(struct ieee80211_vht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008959 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008960 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008961 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
8962 params->vhtcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008963 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
8964 params->vhtcaps_mask);
8965 }
8966#endif /* CONFIG_VHT_OVERRIDES */
8967
8968 if (params->p2p)
8969 wpa_printf(MSG_DEBUG, " * P2P group");
8970
8971 return 0;
8972nla_put_failure:
8973 return -1;
8974}
8975
8976
8977static int wpa_driver_nl80211_try_connect(
8978 struct wpa_driver_nl80211_data *drv,
8979 struct wpa_driver_associate_params *params)
8980{
8981 struct nl_msg *msg;
8982 enum nl80211_auth_type type;
8983 int ret;
8984 int algs;
8985
8986 msg = nlmsg_alloc();
8987 if (!msg)
8988 return -1;
8989
8990 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
8991 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
8992
8993 ret = nl80211_connect_common(drv, params, msg);
8994 if (ret)
8995 goto nla_put_failure;
8996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008997 algs = 0;
8998 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
8999 algs++;
9000 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9001 algs++;
9002 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9003 algs++;
9004 if (algs > 1) {
9005 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
9006 "selection");
9007 goto skip_auth_type;
9008 }
9009
9010 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9011 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
9012 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9013 type = NL80211_AUTHTYPE_SHARED_KEY;
9014 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9015 type = NL80211_AUTHTYPE_NETWORK_EAP;
9016 else if (params->auth_alg & WPA_AUTH_ALG_FT)
9017 type = NL80211_AUTHTYPE_FT;
9018 else
9019 goto nla_put_failure;
9020
9021 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
9022 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
9023
9024skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009025 ret = nl80211_set_conn_keys(params, msg);
9026 if (ret)
9027 goto nla_put_failure;
9028
9029 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9030 msg = NULL;
9031 if (ret) {
9032 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
9033 "(%s)", ret, strerror(-ret));
9034 goto nla_put_failure;
9035 }
9036 ret = 0;
9037 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
9038
9039nla_put_failure:
9040 nlmsg_free(msg);
9041 return ret;
9042
9043}
9044
9045
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009046static int wpa_driver_nl80211_connect(
9047 struct wpa_driver_nl80211_data *drv,
9048 struct wpa_driver_associate_params *params)
9049{
9050 int ret = wpa_driver_nl80211_try_connect(drv, params);
9051 if (ret == -EALREADY) {
9052 /*
9053 * cfg80211 does not currently accept new connections if
9054 * we are already connected. As a workaround, force
9055 * disconnection and try again.
9056 */
9057 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
9058 "disconnecting before reassociation "
9059 "attempt");
9060 if (wpa_driver_nl80211_disconnect(
9061 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
9062 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009063 ret = wpa_driver_nl80211_try_connect(drv, params);
9064 }
9065 return ret;
9066}
9067
9068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009069static int wpa_driver_nl80211_associate(
9070 void *priv, struct wpa_driver_associate_params *params)
9071{
9072 struct i802_bss *bss = priv;
9073 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009074 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009075 struct nl_msg *msg;
9076
9077 if (params->mode == IEEE80211_MODE_AP)
9078 return wpa_driver_nl80211_ap(drv, params);
9079
9080 if (params->mode == IEEE80211_MODE_IBSS)
9081 return wpa_driver_nl80211_ibss(drv, params);
9082
9083 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009084 enum nl80211_iftype nlmode = params->p2p ?
9085 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
9086
9087 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009088 return -1;
9089 return wpa_driver_nl80211_connect(drv, params);
9090 }
9091
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009092 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009093
9094 msg = nlmsg_alloc();
9095 if (!msg)
9096 return -1;
9097
9098 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
9099 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009100 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009101
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009102 ret = nl80211_connect_common(drv, params, msg);
9103 if (ret)
9104 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009105
9106 if (params->prev_bssid) {
9107 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
9108 MAC2STR(params->prev_bssid));
9109 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
9110 params->prev_bssid);
9111 }
9112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009113 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9114 msg = NULL;
9115 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009116 wpa_dbg(drv->ctx, MSG_DEBUG,
9117 "nl80211: MLME command failed (assoc): ret=%d (%s)",
9118 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009119 nl80211_dump_scan(drv);
9120 goto nla_put_failure;
9121 }
9122 ret = 0;
9123 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
9124 "successfully");
9125
9126nla_put_failure:
9127 nlmsg_free(msg);
9128 return ret;
9129}
9130
9131
9132static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009133 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009134{
9135 struct nl_msg *msg;
9136 int ret = -ENOBUFS;
9137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009138 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
9139 ifindex, mode, nl80211_iftype_str(mode));
9140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009141 msg = nlmsg_alloc();
9142 if (!msg)
9143 return -ENOMEM;
9144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009145 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009146 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009147 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009148 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
9149
9150 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009151 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009152 if (!ret)
9153 return 0;
9154nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009155 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009156 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
9157 " %d (%s)", ifindex, mode, ret, strerror(-ret));
9158 return ret;
9159}
9160
9161
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009162static int wpa_driver_nl80211_set_mode_impl(
9163 struct i802_bss *bss,
9164 enum nl80211_iftype nlmode,
9165 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009166{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009167 struct wpa_driver_nl80211_data *drv = bss->drv;
9168 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009169 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009170 int was_ap = is_ap_interface(drv->nlmode);
9171 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009172 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009173
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009174 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9175 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
9176 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009177
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009178 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009179 drv->nlmode = nlmode;
9180 ret = 0;
9181 goto done;
9182 }
9183
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009184 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009185 return -1;
9186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009187 if (nlmode == drv->nlmode) {
9188 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
9189 "requested mode - ignore error");
9190 ret = 0;
9191 goto done; /* Already in the requested mode */
9192 }
9193
9194 /* mac80211 doesn't allow mode changes while the device is up, so
9195 * take the device down, try to set the mode again, and bring the
9196 * device back up.
9197 */
9198 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9199 "interface down");
9200 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009201 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009202 if (res == -EACCES || res == -ENODEV)
9203 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009204 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009205 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9206 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009207 os_sleep(0, 100000);
9208 continue;
9209 }
9210
9211 /*
9212 * Setting the mode will fail for some drivers if the phy is
9213 * on a frequency that the mode is disallowed in.
9214 */
9215 if (desired_freq_params) {
9216 res = i802_set_freq(bss, desired_freq_params);
9217 if (res) {
9218 wpa_printf(MSG_DEBUG,
9219 "nl80211: Failed to set frequency on interface");
9220 }
9221 }
9222
9223 /* Try to set the mode again while the interface is down */
9224 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9225 if (mode_switch_res == -EBUSY) {
9226 wpa_printf(MSG_DEBUG,
9227 "nl80211: Delaying mode set while interface going down");
9228 os_sleep(0, 100000);
9229 continue;
9230 }
9231 ret = mode_switch_res;
9232 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009233 }
9234
9235 if (!ret) {
9236 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9237 "interface is down");
9238 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009239 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009240 }
9241
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009242 /* Bring the interface back up */
9243 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
9244 if (res != 0) {
9245 wpa_printf(MSG_DEBUG,
9246 "nl80211: Failed to set interface up after switching mode");
9247 ret = -1;
9248 }
9249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009250done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009251 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009252 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9253 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009254 return ret;
9255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009256
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009257 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009258 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
9259 else if (drv->disabled_11b_rates)
9260 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
9261
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009262 if (is_ap_interface(nlmode)) {
9263 nl80211_mgmt_unsubscribe(bss, "start AP");
9264 /* Setup additional AP mode functionality if needed */
9265 if (nl80211_setup_ap(bss))
9266 return -1;
9267 } else if (was_ap) {
9268 /* Remove additional AP mode functionality */
9269 nl80211_teardown_ap(bss);
9270 } else {
9271 nl80211_mgmt_unsubscribe(bss, "mode change");
9272 }
9273
Dmitry Shmidt04949592012-07-19 12:16:46 -07009274 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009275 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9276 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9277 "frame processing - ignore for now");
9278
9279 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009280}
9281
9282
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009283static int dfs_info_handler(struct nl_msg *msg, void *arg)
9284{
9285 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9286 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9287 int *dfs_capability_ptr = arg;
9288
9289 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9290 genlmsg_attrlen(gnlh, 0), NULL);
9291
9292 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9293 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9294 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9295
9296 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9297 nla_data(nl_vend), nla_len(nl_vend), NULL);
9298
9299 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9300 u32 val;
9301 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9302 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9303 val);
9304 *dfs_capability_ptr = val;
9305 }
9306 }
9307
9308 return NL_SKIP;
9309}
9310
9311
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009312static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
9313 enum nl80211_iftype nlmode)
9314{
9315 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
9316}
9317
9318
9319static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss, int freq)
9320{
9321 struct hostapd_freq_params freq_params;
9322 os_memset(&freq_params, 0, sizeof(freq_params));
9323 freq_params.freq = freq;
9324 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
9325 &freq_params);
9326}
9327
9328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009329static int wpa_driver_nl80211_get_capa(void *priv,
9330 struct wpa_driver_capa *capa)
9331{
9332 struct i802_bss *bss = priv;
9333 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009334 struct nl_msg *msg;
9335 int dfs_capability = 0;
9336 int ret = 0;
9337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009338 if (!drv->has_capability)
9339 return -1;
9340 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009341 if (drv->extended_capa && drv->extended_capa_mask) {
9342 capa->extended_capa = drv->extended_capa;
9343 capa->extended_capa_mask = drv->extended_capa_mask;
9344 capa->extended_capa_len = drv->extended_capa_len;
9345 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009346
9347 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9348 !drv->allow_p2p_device) {
9349 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9350 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9351 }
9352
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009353 if (drv->dfs_vendor_cmd_avail == 1) {
9354 msg = nlmsg_alloc();
9355 if (!msg)
9356 return -ENOMEM;
9357
9358 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9359
9360 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9361 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9362 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9363 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9364
9365 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9366 &dfs_capability);
9367 if (!ret) {
9368 if (dfs_capability)
9369 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9370 }
9371 }
9372
9373 return ret;
9374
9375 nla_put_failure:
9376 nlmsg_free(msg);
9377 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009378}
9379
9380
9381static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9382{
9383 struct i802_bss *bss = priv;
9384 struct wpa_driver_nl80211_data *drv = bss->drv;
9385
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009386 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9387 bss->ifname, drv->operstate, state,
9388 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009389 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009390 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009391 state ? IF_OPER_UP : IF_OPER_DORMANT);
9392}
9393
9394
9395static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9396{
9397 struct i802_bss *bss = priv;
9398 struct wpa_driver_nl80211_data *drv = bss->drv;
9399 struct nl_msg *msg;
9400 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009401 int ret = -ENOBUFS;
9402
9403 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9404 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9405 return 0;
9406 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009407
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009408 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9409 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009411 msg = nlmsg_alloc();
9412 if (!msg)
9413 return -ENOMEM;
9414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009415 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009416
9417 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9418 if_nametoindex(bss->ifname));
9419 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9420
9421 os_memset(&upd, 0, sizeof(upd));
9422 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9423 if (authorized)
9424 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9425 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9426
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009427 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9428 msg = NULL;
9429 if (!ret)
9430 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009431 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009432 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009433 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9434 ret, strerror(-ret));
9435 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009436}
9437
9438
Jouni Malinen75ecf522011-06-27 15:19:46 -07009439/* Set kernel driver on given frequency (MHz) */
9440static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009441{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009442 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009443 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009444}
9445
9446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009447static inline int min_int(int a, int b)
9448{
9449 if (a < b)
9450 return a;
9451 return b;
9452}
9453
9454
9455static int get_key_handler(struct nl_msg *msg, void *arg)
9456{
9457 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9458 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9459
9460 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9461 genlmsg_attrlen(gnlh, 0), NULL);
9462
9463 /*
9464 * TODO: validate the key index and mac address!
9465 * Otherwise, there's a race condition as soon as
9466 * the kernel starts sending key notifications.
9467 */
9468
9469 if (tb[NL80211_ATTR_KEY_SEQ])
9470 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9471 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9472 return NL_SKIP;
9473}
9474
9475
9476static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9477 int idx, u8 *seq)
9478{
9479 struct i802_bss *bss = priv;
9480 struct wpa_driver_nl80211_data *drv = bss->drv;
9481 struct nl_msg *msg;
9482
9483 msg = nlmsg_alloc();
9484 if (!msg)
9485 return -ENOMEM;
9486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009487 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009488
9489 if (addr)
9490 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9491 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9492 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9493
9494 memset(seq, 0, 6);
9495
9496 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9497 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009498 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009499 return -ENOBUFS;
9500}
9501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009502
9503static int i802_set_rts(void *priv, int rts)
9504{
9505 struct i802_bss *bss = priv;
9506 struct wpa_driver_nl80211_data *drv = bss->drv;
9507 struct nl_msg *msg;
9508 int ret = -ENOBUFS;
9509 u32 val;
9510
9511 msg = nlmsg_alloc();
9512 if (!msg)
9513 return -ENOMEM;
9514
9515 if (rts >= 2347)
9516 val = (u32) -1;
9517 else
9518 val = rts;
9519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009520 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009521 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9522 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9523
9524 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009525 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009526 if (!ret)
9527 return 0;
9528nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009529 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009530 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9531 "%d (%s)", rts, ret, strerror(-ret));
9532 return ret;
9533}
9534
9535
9536static int i802_set_frag(void *priv, int frag)
9537{
9538 struct i802_bss *bss = priv;
9539 struct wpa_driver_nl80211_data *drv = bss->drv;
9540 struct nl_msg *msg;
9541 int ret = -ENOBUFS;
9542 u32 val;
9543
9544 msg = nlmsg_alloc();
9545 if (!msg)
9546 return -ENOMEM;
9547
9548 if (frag >= 2346)
9549 val = (u32) -1;
9550 else
9551 val = frag;
9552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009553 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009554 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9555 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9556
9557 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009558 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009559 if (!ret)
9560 return 0;
9561nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009562 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009563 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9564 "%d: %d (%s)", frag, ret, strerror(-ret));
9565 return ret;
9566}
9567
9568
9569static int i802_flush(void *priv)
9570{
9571 struct i802_bss *bss = priv;
9572 struct wpa_driver_nl80211_data *drv = bss->drv;
9573 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009574 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009575
9576 msg = nlmsg_alloc();
9577 if (!msg)
9578 return -1;
9579
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009580 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9581 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009582 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009583
9584 /*
9585 * XXX: FIX! this needs to flush all VLANs too
9586 */
9587 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9588 if_nametoindex(bss->ifname));
9589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009590 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9591 if (res) {
9592 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9593 "(%s)", res, strerror(-res));
9594 }
9595 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009596 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009597 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009598 return -ENOBUFS;
9599}
9600
9601
9602static int get_sta_handler(struct nl_msg *msg, void *arg)
9603{
9604 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9605 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9606 struct hostap_sta_driver_data *data = arg;
9607 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9608 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9609 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9610 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9611 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9612 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9613 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009614 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009615 };
9616
9617 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9618 genlmsg_attrlen(gnlh, 0), NULL);
9619
9620 /*
9621 * TODO: validate the interface and mac address!
9622 * Otherwise, there's a race condition as soon as
9623 * the kernel starts sending station notifications.
9624 */
9625
9626 if (!tb[NL80211_ATTR_STA_INFO]) {
9627 wpa_printf(MSG_DEBUG, "sta stats missing!");
9628 return NL_SKIP;
9629 }
9630 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9631 tb[NL80211_ATTR_STA_INFO],
9632 stats_policy)) {
9633 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9634 return NL_SKIP;
9635 }
9636
9637 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9638 data->inactive_msec =
9639 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9640 if (stats[NL80211_STA_INFO_RX_BYTES])
9641 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9642 if (stats[NL80211_STA_INFO_TX_BYTES])
9643 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9644 if (stats[NL80211_STA_INFO_RX_PACKETS])
9645 data->rx_packets =
9646 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9647 if (stats[NL80211_STA_INFO_TX_PACKETS])
9648 data->tx_packets =
9649 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009650 if (stats[NL80211_STA_INFO_TX_FAILED])
9651 data->tx_retry_failed =
9652 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009653
9654 return NL_SKIP;
9655}
9656
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009657static int i802_read_sta_data(struct i802_bss *bss,
9658 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009659 const u8 *addr)
9660{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009661 struct wpa_driver_nl80211_data *drv = bss->drv;
9662 struct nl_msg *msg;
9663
9664 os_memset(data, 0, sizeof(*data));
9665 msg = nlmsg_alloc();
9666 if (!msg)
9667 return -ENOMEM;
9668
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009669 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009670
9671 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9672 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9673
9674 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9675 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009676 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009677 return -ENOBUFS;
9678}
9679
9680
9681static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9682 int cw_min, int cw_max, int burst_time)
9683{
9684 struct i802_bss *bss = priv;
9685 struct wpa_driver_nl80211_data *drv = bss->drv;
9686 struct nl_msg *msg;
9687 struct nlattr *txq, *params;
9688
9689 msg = nlmsg_alloc();
9690 if (!msg)
9691 return -1;
9692
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009693 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009694
9695 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9696
9697 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9698 if (!txq)
9699 goto nla_put_failure;
9700
9701 /* We are only sending parameters for a single TXQ at a time */
9702 params = nla_nest_start(msg, 1);
9703 if (!params)
9704 goto nla_put_failure;
9705
9706 switch (queue) {
9707 case 0:
9708 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9709 break;
9710 case 1:
9711 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9712 break;
9713 case 2:
9714 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9715 break;
9716 case 3:
9717 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9718 break;
9719 }
9720 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9721 * 32 usec, so need to convert the value here. */
9722 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9723 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9724 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9725 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9726
9727 nla_nest_end(msg, params);
9728
9729 nla_nest_end(msg, txq);
9730
9731 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9732 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009733 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009734 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009735 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009736 return -1;
9737}
9738
9739
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009740static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009741 const char *ifname, int vlan_id)
9742{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009743 struct wpa_driver_nl80211_data *drv = bss->drv;
9744 struct nl_msg *msg;
9745 int ret = -ENOBUFS;
9746
9747 msg = nlmsg_alloc();
9748 if (!msg)
9749 return -ENOMEM;
9750
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009751 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9752 ", ifname=%s[%d], vlan_id=%d)",
9753 bss->ifname, if_nametoindex(bss->ifname),
9754 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009755 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009756
9757 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9758 if_nametoindex(bss->ifname));
9759 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9760 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9761 if_nametoindex(ifname));
9762
9763 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009764 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009765 if (ret < 0) {
9766 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9767 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9768 MAC2STR(addr), ifname, vlan_id, ret,
9769 strerror(-ret));
9770 }
9771 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009772 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009773 return ret;
9774}
9775
9776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009777static int i802_get_inact_sec(void *priv, const u8 *addr)
9778{
9779 struct hostap_sta_driver_data data;
9780 int ret;
9781
9782 data.inactive_msec = (unsigned long) -1;
9783 ret = i802_read_sta_data(priv, &data, addr);
9784 if (ret || data.inactive_msec == (unsigned long) -1)
9785 return -1;
9786 return data.inactive_msec / 1000;
9787}
9788
9789
9790static int i802_sta_clear_stats(void *priv, const u8 *addr)
9791{
9792#if 0
9793 /* TODO */
9794#endif
9795 return 0;
9796}
9797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009798
9799static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9800 int reason)
9801{
9802 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009803 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009804 struct ieee80211_mgmt mgmt;
9805
Dmitry Shmidt04949592012-07-19 12:16:46 -07009806 if (drv->device_ap_sme)
9807 return wpa_driver_nl80211_sta_remove(bss, addr);
9808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009809 memset(&mgmt, 0, sizeof(mgmt));
9810 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9811 WLAN_FC_STYPE_DEAUTH);
9812 memcpy(mgmt.da, addr, ETH_ALEN);
9813 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9814 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9815 mgmt.u.deauth.reason_code = host_to_le16(reason);
9816 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9817 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009818 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9819 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009820}
9821
9822
9823static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9824 int reason)
9825{
9826 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009827 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009828 struct ieee80211_mgmt mgmt;
9829
Dmitry Shmidt04949592012-07-19 12:16:46 -07009830 if (drv->device_ap_sme)
9831 return wpa_driver_nl80211_sta_remove(bss, addr);
9832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009833 memset(&mgmt, 0, sizeof(mgmt));
9834 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9835 WLAN_FC_STYPE_DISASSOC);
9836 memcpy(mgmt.da, addr, ETH_ALEN);
9837 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9838 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9839 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9840 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9841 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009842 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9843 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009844}
9845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009846
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009847static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9848{
9849 char buf[200], *pos, *end;
9850 int i, res;
9851
9852 pos = buf;
9853 end = pos + sizeof(buf);
9854
9855 for (i = 0; i < drv->num_if_indices; i++) {
9856 if (!drv->if_indices[i])
9857 continue;
9858 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9859 if (res < 0 || res >= end - pos)
9860 break;
9861 pos += res;
9862 }
9863 *pos = '\0';
9864
9865 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9866 drv->num_if_indices, buf);
9867}
9868
9869
Jouni Malinen75ecf522011-06-27 15:19:46 -07009870static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9871{
9872 int i;
9873 int *old;
9874
9875 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9876 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009877 if (have_ifidx(drv, ifidx)) {
9878 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9879 ifidx);
9880 return;
9881 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009882 for (i = 0; i < drv->num_if_indices; i++) {
9883 if (drv->if_indices[i] == 0) {
9884 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009885 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009886 return;
9887 }
9888 }
9889
9890 if (drv->if_indices != drv->default_if_indices)
9891 old = drv->if_indices;
9892 else
9893 old = NULL;
9894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009895 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9896 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009897 if (!drv->if_indices) {
9898 if (!old)
9899 drv->if_indices = drv->default_if_indices;
9900 else
9901 drv->if_indices = old;
9902 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9903 "interfaces");
9904 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9905 return;
9906 } else if (!old)
9907 os_memcpy(drv->if_indices, drv->default_if_indices,
9908 sizeof(drv->default_if_indices));
9909 drv->if_indices[drv->num_if_indices] = ifidx;
9910 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009911 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009912}
9913
9914
9915static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9916{
9917 int i;
9918
9919 for (i = 0; i < drv->num_if_indices; i++) {
9920 if (drv->if_indices[i] == ifidx) {
9921 drv->if_indices[i] = 0;
9922 break;
9923 }
9924 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009925 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009926}
9927
9928
9929static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9930{
9931 int i;
9932
9933 for (i = 0; i < drv->num_if_indices; i++)
9934 if (drv->if_indices[i] == ifidx)
9935 return 1;
9936
9937 return 0;
9938}
9939
9940
9941static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009942 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009943{
9944 struct i802_bss *bss = priv;
9945 struct wpa_driver_nl80211_data *drv = bss->drv;
9946 char name[IFNAMSIZ + 1];
9947
9948 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009949 if (ifname_wds)
9950 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9951
Jouni Malinen75ecf522011-06-27 15:19:46 -07009952 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9953 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9954 if (val) {
9955 if (!if_nametoindex(name)) {
9956 if (nl80211_create_iface(drv, name,
9957 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009958 bss->addr, 1, NULL, NULL, 0) <
9959 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009960 return -1;
9961 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009962 linux_br_add_if(drv->global->ioctl_sock,
9963 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009964 return -1;
9965 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009966 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
9967 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
9968 "interface %s up", name);
9969 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009970 return i802_set_sta_vlan(priv, addr, name, 0);
9971 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07009972 if (bridge_ifname)
9973 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
9974 name);
9975
Jouni Malinen75ecf522011-06-27 15:19:46 -07009976 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009977 nl80211_remove_iface(drv, if_nametoindex(name));
9978 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009979 }
9980}
9981
9982
9983static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
9984{
9985 struct wpa_driver_nl80211_data *drv = eloop_ctx;
9986 struct sockaddr_ll lladdr;
9987 unsigned char buf[3000];
9988 int len;
9989 socklen_t fromlen = sizeof(lladdr);
9990
9991 len = recvfrom(sock, buf, sizeof(buf), 0,
9992 (struct sockaddr *)&lladdr, &fromlen);
9993 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009994 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
9995 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009996 return;
9997 }
9998
9999 if (have_ifidx(drv, lladdr.sll_ifindex))
10000 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
10001}
10002
10003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010004static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
10005 struct i802_bss *bss,
10006 const char *brname, const char *ifname)
10007{
10008 int ifindex;
10009 char in_br[IFNAMSIZ];
10010
10011 os_strlcpy(bss->brname, brname, IFNAMSIZ);
10012 ifindex = if_nametoindex(brname);
10013 if (ifindex == 0) {
10014 /*
10015 * Bridge was configured, but the bridge device does
10016 * not exist. Try to add it now.
10017 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010018 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010019 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
10020 "bridge interface %s: %s",
10021 brname, strerror(errno));
10022 return -1;
10023 }
10024 bss->added_bridge = 1;
10025 add_ifidx(drv, if_nametoindex(brname));
10026 }
10027
10028 if (linux_br_get(in_br, ifname) == 0) {
10029 if (os_strcmp(in_br, brname) == 0)
10030 return 0; /* already in the bridge */
10031
10032 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
10033 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010034 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
10035 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010036 wpa_printf(MSG_ERROR, "nl80211: Failed to "
10037 "remove interface %s from bridge "
10038 "%s: %s",
10039 ifname, brname, strerror(errno));
10040 return -1;
10041 }
10042 }
10043
10044 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
10045 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010046 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010047 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
10048 "into bridge %s: %s",
10049 ifname, brname, strerror(errno));
10050 return -1;
10051 }
10052 bss->added_if_into_bridge = 1;
10053
10054 return 0;
10055}
10056
10057
10058static void *i802_init(struct hostapd_data *hapd,
10059 struct wpa_init_params *params)
10060{
10061 struct wpa_driver_nl80211_data *drv;
10062 struct i802_bss *bss;
10063 size_t i;
10064 char brname[IFNAMSIZ];
10065 int ifindex, br_ifindex;
10066 int br_added = 0;
10067
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080010068 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
10069 params->global_priv, 1,
10070 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010071 if (bss == NULL)
10072 return NULL;
10073
10074 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010076 if (linux_br_get(brname, params->ifname) == 0) {
10077 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
10078 params->ifname, brname);
10079 br_ifindex = if_nametoindex(brname);
10080 } else {
10081 brname[0] = '\0';
10082 br_ifindex = 0;
10083 }
10084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010085 for (i = 0; i < params->num_bridge; i++) {
10086 if (params->bridge[i]) {
10087 ifindex = if_nametoindex(params->bridge[i]);
10088 if (ifindex)
10089 add_ifidx(drv, ifindex);
10090 if (ifindex == br_ifindex)
10091 br_added = 1;
10092 }
10093 }
10094 if (!br_added && br_ifindex &&
10095 (params->num_bridge == 0 || !params->bridge[0]))
10096 add_ifidx(drv, br_ifindex);
10097
10098 /* start listening for EAPOL on the default AP interface */
10099 add_ifidx(drv, drv->ifindex);
10100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010101 if (params->num_bridge && params->bridge[0] &&
10102 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
10103 goto failed;
10104
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010105#ifdef CONFIG_LIBNL3_ROUTE
10106 if (bss->added_if_into_bridge) {
10107 drv->rtnl_sk = nl_socket_alloc();
10108 if (drv->rtnl_sk == NULL) {
10109 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
10110 goto failed;
10111 }
10112
10113 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
10114 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
10115 strerror(errno));
10116 goto failed;
10117 }
10118 }
10119#endif /* CONFIG_LIBNL3_ROUTE */
10120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010121 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
10122 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010123 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
10124 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010125 goto failed;
10126 }
10127
10128 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
10129 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010130 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010131 goto failed;
10132 }
10133
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010134 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
10135 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010136 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010137 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010139 memcpy(bss->addr, params->own_addr, ETH_ALEN);
10140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010141 return bss;
10142
10143failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010144 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010145 return NULL;
10146}
10147
10148
10149static void i802_deinit(void *priv)
10150{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010151 struct i802_bss *bss = priv;
10152 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010153}
10154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010155
10156static enum nl80211_iftype wpa_driver_nl80211_if_type(
10157 enum wpa_driver_if_type type)
10158{
10159 switch (type) {
10160 case WPA_IF_STATION:
10161 return NL80211_IFTYPE_STATION;
10162 case WPA_IF_P2P_CLIENT:
10163 case WPA_IF_P2P_GROUP:
10164 return NL80211_IFTYPE_P2P_CLIENT;
10165 case WPA_IF_AP_VLAN:
10166 return NL80211_IFTYPE_AP_VLAN;
10167 case WPA_IF_AP_BSS:
10168 return NL80211_IFTYPE_AP;
10169 case WPA_IF_P2P_GO:
10170 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010171 case WPA_IF_P2P_DEVICE:
10172 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010173 }
10174 return -1;
10175}
10176
10177
10178#ifdef CONFIG_P2P
10179
10180static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
10181{
10182 struct wpa_driver_nl80211_data *drv;
10183 dl_list_for_each(drv, &global->interfaces,
10184 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010185 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010186 return 1;
10187 }
10188 return 0;
10189}
10190
10191
10192static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
10193 u8 *new_addr)
10194{
10195 unsigned int idx;
10196
10197 if (!drv->global)
10198 return -1;
10199
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010200 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010201 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010202 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010203 new_addr[0] ^= idx << 2;
10204 if (!nl80211_addr_in_use(drv->global, new_addr))
10205 break;
10206 }
10207 if (idx == 64)
10208 return -1;
10209
10210 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
10211 MACSTR, MAC2STR(new_addr));
10212
10213 return 0;
10214}
10215
10216#endif /* CONFIG_P2P */
10217
10218
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010219struct wdev_info {
10220 u64 wdev_id;
10221 int wdev_id_set;
10222 u8 macaddr[ETH_ALEN];
10223};
10224
10225static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
10226{
10227 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10228 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10229 struct wdev_info *wi = arg;
10230
10231 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10232 genlmsg_attrlen(gnlh, 0), NULL);
10233 if (tb[NL80211_ATTR_WDEV]) {
10234 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
10235 wi->wdev_id_set = 1;
10236 }
10237
10238 if (tb[NL80211_ATTR_MAC])
10239 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
10240 ETH_ALEN);
10241
10242 return NL_SKIP;
10243}
10244
10245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010246static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
10247 const char *ifname, const u8 *addr,
10248 void *bss_ctx, void **drv_priv,
10249 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010250 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010251{
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010252 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010253 struct i802_bss *bss = priv;
10254 struct wpa_driver_nl80211_data *drv = bss->drv;
10255 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010256 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010257
10258 if (addr)
10259 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010260 nlmode = wpa_driver_nl80211_if_type(type);
10261 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
10262 struct wdev_info p2pdev_info;
10263
10264 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
10265 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
10266 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010267 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010268 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
10269 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10270 ifname);
10271 return -1;
10272 }
10273
10274 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10275 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10276 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10277 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10278 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10279 ifname,
10280 (long long unsigned int) p2pdev_info.wdev_id);
10281 } else {
10282 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010283 0, NULL, NULL, use_existing);
10284 if (use_existing && ifidx == -ENFILE) {
10285 added = 0;
10286 ifidx = if_nametoindex(ifname);
10287 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010288 return -1;
10289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010290 }
10291
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010292 if (!addr) {
10293 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10294 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10295 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10296 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010297 if (added)
10298 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010299 return -1;
10300 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010301 }
10302
10303#ifdef CONFIG_P2P
10304 if (!addr &&
10305 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10306 type == WPA_IF_P2P_GO)) {
10307 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010308 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010309
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010310 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010311 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010312 if (added)
10313 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010314 return -1;
10315 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010316 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010317 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10318 "for P2P group interface");
10319 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010320 if (added)
10321 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010322 return -1;
10323 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010324 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010325 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010326 if (added)
10327 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010328 return -1;
10329 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010330 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010331 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010332 }
10333#endif /* CONFIG_P2P */
10334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010335 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010336 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10337 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010338 if (added)
10339 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010340 return -1;
10341 }
10342
10343 if (bridge &&
10344 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10345 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10346 "interface %s to a bridge %s",
10347 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010348 if (added)
10349 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010350 os_free(new_bss);
10351 return -1;
10352 }
10353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010354 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10355 {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010356 if (added)
10357 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010358 os_free(new_bss);
10359 return -1;
10360 }
10361 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010362 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010363 new_bss->ifindex = ifidx;
10364 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010365 new_bss->next = drv->first_bss->next;
10366 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010367 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010368 new_bss->added_if = added;
10369 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010370 if (drv_priv)
10371 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010372 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010373
10374 /* Subscribe management frames for this WPA_IF_AP_BSS */
10375 if (nl80211_setup_ap(new_bss))
10376 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010379 if (drv->global)
10380 drv->global->if_add_ifindex = ifidx;
10381
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010382 /*
10383 * Some virtual interfaces need to process EAPOL packets and events on
10384 * the parent interface. This is used mainly with hostapd.
10385 */
10386 if (ifidx > 0 &&
10387 (drv->hostapd ||
10388 nlmode == NL80211_IFTYPE_AP_VLAN ||
10389 nlmode == NL80211_IFTYPE_WDS ||
10390 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010391 add_ifidx(drv, ifidx);
10392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010393 return 0;
10394}
10395
10396
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010397static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010398 enum wpa_driver_if_type type,
10399 const char *ifname)
10400{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010401 struct wpa_driver_nl80211_data *drv = bss->drv;
10402 int ifindex = if_nametoindex(ifname);
10403
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010404 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10405 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010406 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010407 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -070010408 else if (ifindex > 0 && !bss->added_if) {
10409 struct wpa_driver_nl80211_data *drv2;
10410 dl_list_for_each(drv2, &drv->global->interfaces,
10411 struct wpa_driver_nl80211_data, list)
10412 del_ifidx(drv2, ifindex);
10413 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010414
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010415 if (type != WPA_IF_AP_BSS)
10416 return 0;
10417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010418 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010419 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10420 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010421 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10422 "interface %s from bridge %s: %s",
10423 bss->ifname, bss->brname, strerror(errno));
10424 }
10425 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010426 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010427 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10428 "bridge %s: %s",
10429 bss->brname, strerror(errno));
10430 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010431
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010432 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010433 struct i802_bss *tbss;
10434
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010435 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10436 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010437 if (tbss->next == bss) {
10438 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010439 /* Unsubscribe management frames */
10440 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010441 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010442 if (!bss->added_if)
10443 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010444 os_free(bss);
10445 bss = NULL;
10446 break;
10447 }
10448 }
10449 if (bss)
10450 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10451 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010452 } else {
10453 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10454 nl80211_teardown_ap(bss);
10455 if (!bss->added_if && !drv->first_bss->next)
10456 wpa_driver_nl80211_del_beacon(drv);
10457 nl80211_destroy_bss(bss);
10458 if (!bss->added_if)
10459 i802_set_iface_flags(bss, 0);
10460 if (drv->first_bss->next) {
10461 drv->first_bss = drv->first_bss->next;
10462 drv->ctx = drv->first_bss->ctx;
10463 os_free(bss);
10464 } else {
10465 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010468
10469 return 0;
10470}
10471
10472
10473static int cookie_handler(struct nl_msg *msg, void *arg)
10474{
10475 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10476 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10477 u64 *cookie = arg;
10478 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10479 genlmsg_attrlen(gnlh, 0), NULL);
10480 if (tb[NL80211_ATTR_COOKIE])
10481 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10482 return NL_SKIP;
10483}
10484
10485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010486static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010487 unsigned int freq, unsigned int wait,
10488 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010489 u64 *cookie_out, int no_cck, int no_ack,
10490 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010491{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010492 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010493 struct nl_msg *msg;
10494 u64 cookie;
10495 int ret = -1;
10496
10497 msg = nlmsg_alloc();
10498 if (!msg)
10499 return -1;
10500
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010501 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010502 "no_ack=%d offchanok=%d",
10503 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010504 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010505 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010506
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010507 if (nl80211_set_iface_id(msg, bss) < 0)
10508 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010509 if (freq)
10510 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010511 if (wait)
10512 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010513 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10514 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010515 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10516 if (no_cck)
10517 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10518 if (no_ack)
10519 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010521 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10522
10523 cookie = 0;
10524 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10525 msg = NULL;
10526 if (ret) {
10527 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010528 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10529 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010530 goto nla_put_failure;
10531 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010532 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010533 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10534 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010535
10536 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010537 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010538
10539nla_put_failure:
10540 nlmsg_free(msg);
10541 return ret;
10542}
10543
10544
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010545static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10546 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010547 unsigned int wait_time,
10548 const u8 *dst, const u8 *src,
10549 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010550 const u8 *data, size_t data_len,
10551 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010552{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010553 struct wpa_driver_nl80211_data *drv = bss->drv;
10554 int ret = -1;
10555 u8 *buf;
10556 struct ieee80211_hdr *hdr;
10557
10558 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010559 "freq=%u MHz wait=%d ms no_cck=%d)",
10560 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010561
10562 buf = os_zalloc(24 + data_len);
10563 if (buf == NULL)
10564 return ret;
10565 os_memcpy(buf + 24, data, data_len);
10566 hdr = (struct ieee80211_hdr *) buf;
10567 hdr->frame_control =
10568 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10569 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10570 os_memcpy(hdr->addr2, src, ETH_ALEN);
10571 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10572
Dmitry Shmidt56052862013-10-04 10:23:25 -070010573 if (is_ap_interface(drv->nlmode) &&
10574 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10575 (int) freq == bss->freq || drv->device_ap_sme ||
10576 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010577 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10578 0, freq, no_cck, 1,
10579 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010580 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010581 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010582 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010583 &drv->send_action_cookie,
10584 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010585
10586 os_free(buf);
10587 return ret;
10588}
10589
10590
10591static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10592{
10593 struct i802_bss *bss = priv;
10594 struct wpa_driver_nl80211_data *drv = bss->drv;
10595 struct nl_msg *msg;
10596 int ret;
10597
10598 msg = nlmsg_alloc();
10599 if (!msg)
10600 return;
10601
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010602 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10603 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010604 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010605
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010606 if (nl80211_set_iface_id(msg, bss) < 0)
10607 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010608 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10609
10610 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10611 msg = NULL;
10612 if (ret)
10613 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10614 "(%s)", ret, strerror(-ret));
10615
10616 nla_put_failure:
10617 nlmsg_free(msg);
10618}
10619
10620
10621static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10622 unsigned int duration)
10623{
10624 struct i802_bss *bss = priv;
10625 struct wpa_driver_nl80211_data *drv = bss->drv;
10626 struct nl_msg *msg;
10627 int ret;
10628 u64 cookie;
10629
10630 msg = nlmsg_alloc();
10631 if (!msg)
10632 return -1;
10633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010634 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010635
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010636 if (nl80211_set_iface_id(msg, bss) < 0)
10637 goto nla_put_failure;
10638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010639 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10640 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10641
10642 cookie = 0;
10643 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010644 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010645 if (ret == 0) {
10646 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10647 "0x%llx for freq=%u MHz duration=%u",
10648 (long long unsigned int) cookie, freq, duration);
10649 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010650 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010651 return 0;
10652 }
10653 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10654 "(freq=%d duration=%u): %d (%s)",
10655 freq, duration, ret, strerror(-ret));
10656nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010657 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010658 return -1;
10659}
10660
10661
10662static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10663{
10664 struct i802_bss *bss = priv;
10665 struct wpa_driver_nl80211_data *drv = bss->drv;
10666 struct nl_msg *msg;
10667 int ret;
10668
10669 if (!drv->pending_remain_on_chan) {
10670 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10671 "to cancel");
10672 return -1;
10673 }
10674
10675 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10676 "0x%llx",
10677 (long long unsigned int) drv->remain_on_chan_cookie);
10678
10679 msg = nlmsg_alloc();
10680 if (!msg)
10681 return -1;
10682
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010683 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010684
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010685 if (nl80211_set_iface_id(msg, bss) < 0)
10686 goto nla_put_failure;
10687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010688 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10689
10690 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010691 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010692 if (ret == 0)
10693 return 0;
10694 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10695 "%d (%s)", ret, strerror(-ret));
10696nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010697 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010698 return -1;
10699}
10700
10701
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010702static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010703{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010704 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010706 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010707 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -070010708 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
10709 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010710 /*
10711 * Do not disable Probe Request reporting that was
10712 * enabled in nl80211_setup_ap().
10713 */
10714 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10715 "Probe Request reporting nl_preq=%p while "
10716 "in AP mode", bss->nl_preq);
10717 } else if (bss->nl_preq) {
10718 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10719 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010720 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010721 }
10722 return 0;
10723 }
10724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010725 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010726 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010727 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010728 return 0;
10729 }
10730
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010731 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10732 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010733 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010734 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10735 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010737 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010738 (WLAN_FC_TYPE_MGMT << 2) |
10739 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010740 NULL, 0) < 0)
10741 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010742
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010743 nl80211_register_eloop_read(&bss->nl_preq,
10744 wpa_driver_nl80211_event_receive,
10745 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010746
10747 return 0;
10748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010749 out_err:
10750 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010751 return -1;
10752}
10753
10754
10755static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10756 int ifindex, int disabled)
10757{
10758 struct nl_msg *msg;
10759 struct nlattr *bands, *band;
10760 int ret;
10761
10762 msg = nlmsg_alloc();
10763 if (!msg)
10764 return -1;
10765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010766 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010767 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10768
10769 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10770 if (!bands)
10771 goto nla_put_failure;
10772
10773 /*
10774 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10775 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10776 * rates. All 5 GHz rates are left enabled.
10777 */
10778 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10779 if (!band)
10780 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010781 if (disabled) {
10782 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10783 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010785 nla_nest_end(msg, band);
10786
10787 nla_nest_end(msg, bands);
10788
10789 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10790 msg = NULL;
10791 if (ret) {
10792 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10793 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010794 } else
10795 drv->disabled_11b_rates = disabled;
10796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010797 return ret;
10798
10799nla_put_failure:
10800 nlmsg_free(msg);
10801 return -1;
10802}
10803
10804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010805static int wpa_driver_nl80211_deinit_ap(void *priv)
10806{
10807 struct i802_bss *bss = priv;
10808 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010809 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010810 return -1;
10811 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010812
10813 /*
10814 * If the P2P GO interface was dynamically added, then it is
10815 * possible that the interface change to station is not possible.
10816 */
10817 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10818 return 0;
10819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010820 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010821}
10822
10823
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010824static int wpa_driver_nl80211_stop_ap(void *priv)
10825{
10826 struct i802_bss *bss = priv;
10827 struct wpa_driver_nl80211_data *drv = bss->drv;
10828 if (!is_ap_interface(drv->nlmode))
10829 return -1;
10830 wpa_driver_nl80211_del_beacon(drv);
10831 bss->beacon_set = 0;
10832 return 0;
10833}
10834
10835
Dmitry Shmidt04949592012-07-19 12:16:46 -070010836static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10837{
10838 struct i802_bss *bss = priv;
10839 struct wpa_driver_nl80211_data *drv = bss->drv;
10840 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10841 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010842
10843 /*
10844 * If the P2P Client interface was dynamically added, then it is
10845 * possible that the interface change to station is not possible.
10846 */
10847 if (bss->if_dynamic)
10848 return 0;
10849
Dmitry Shmidt04949592012-07-19 12:16:46 -070010850 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10851}
10852
10853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010854static void wpa_driver_nl80211_resume(void *priv)
10855{
10856 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010857
10858 if (i802_set_iface_flags(bss, 1))
10859 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010860}
10861
10862
10863static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10864 const u8 *ies, size_t ies_len)
10865{
10866 struct i802_bss *bss = priv;
10867 struct wpa_driver_nl80211_data *drv = bss->drv;
10868 int ret;
10869 u8 *data, *pos;
10870 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010871 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010872
10873 if (action != 1) {
10874 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10875 "action %d", action);
10876 return -1;
10877 }
10878
10879 /*
10880 * Action frame payload:
10881 * Category[1] = 6 (Fast BSS Transition)
10882 * Action[1] = 1 (Fast BSS Transition Request)
10883 * STA Address
10884 * Target AP Address
10885 * FT IEs
10886 */
10887
10888 data_len = 2 + 2 * ETH_ALEN + ies_len;
10889 data = os_malloc(data_len);
10890 if (data == NULL)
10891 return -1;
10892 pos = data;
10893 *pos++ = 0x06; /* FT Action category */
10894 *pos++ = action;
10895 os_memcpy(pos, own_addr, ETH_ALEN);
10896 pos += ETH_ALEN;
10897 os_memcpy(pos, target_ap, ETH_ALEN);
10898 pos += ETH_ALEN;
10899 os_memcpy(pos, ies, ies_len);
10900
10901 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10902 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010903 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010904 os_free(data);
10905
10906 return ret;
10907}
10908
10909
10910static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10911{
10912 struct i802_bss *bss = priv;
10913 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010914 struct nl_msg *msg;
10915 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010916 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010917
10918 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10919 "hysteresis=%d", threshold, hysteresis);
10920
10921 msg = nlmsg_alloc();
10922 if (!msg)
10923 return -1;
10924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010925 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010926
10927 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10928
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010929 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010930 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010931 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010932
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010933 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10934 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10935 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010936
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010937 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010938 msg = NULL;
10939
10940nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010941 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010942 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010943}
10944
10945
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010946static int get_channel_width(struct nl_msg *msg, void *arg)
10947{
10948 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10949 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10950 struct wpa_signal_info *sig_change = arg;
10951
10952 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10953 genlmsg_attrlen(gnlh, 0), NULL);
10954
10955 sig_change->center_frq1 = -1;
10956 sig_change->center_frq2 = -1;
10957 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10958
10959 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10960 sig_change->chanwidth = convert2width(
10961 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10962 if (tb[NL80211_ATTR_CENTER_FREQ1])
10963 sig_change->center_frq1 =
10964 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
10965 if (tb[NL80211_ATTR_CENTER_FREQ2])
10966 sig_change->center_frq2 =
10967 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
10968 }
10969
10970 return NL_SKIP;
10971}
10972
10973
10974static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
10975 struct wpa_signal_info *sig)
10976{
10977 struct nl_msg *msg;
10978
10979 msg = nlmsg_alloc();
10980 if (!msg)
10981 return -ENOMEM;
10982
10983 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
10984 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10985
10986 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
10987
10988nla_put_failure:
10989 nlmsg_free(msg);
10990 return -ENOBUFS;
10991}
10992
10993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010994static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
10995{
10996 struct i802_bss *bss = priv;
10997 struct wpa_driver_nl80211_data *drv = bss->drv;
10998 int res;
10999
11000 os_memset(si, 0, sizeof(*si));
11001 res = nl80211_get_link_signal(drv, si);
11002 if (res != 0)
11003 return res;
11004
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011005 res = nl80211_get_channel_width(drv, si);
11006 if (res != 0)
11007 return res;
11008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011009 return nl80211_get_link_noise(drv, si);
11010}
11011
11012
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011013static int wpa_driver_nl80211_shared_freq(void *priv)
11014{
11015 struct i802_bss *bss = priv;
11016 struct wpa_driver_nl80211_data *drv = bss->drv;
11017 struct wpa_driver_nl80211_data *driver;
11018 int freq = 0;
11019
11020 /*
11021 * If the same PHY is in connected state with some other interface,
11022 * then retrieve the assoc freq.
11023 */
11024 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
11025 drv->phyname);
11026
11027 dl_list_for_each(driver, &drv->global->interfaces,
11028 struct wpa_driver_nl80211_data, list) {
11029 if (drv == driver ||
11030 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011031 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011032 continue;
11033
11034 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
11035 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011036 driver->phyname, driver->first_bss->ifname,
11037 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070011038 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011039 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011040 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011041 freq = nl80211_get_assoc_freq(driver);
11042 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
11043 drv->phyname, freq);
11044 }
11045
11046 if (!freq)
11047 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
11048 "PHY (%s) in associated state", drv->phyname);
11049
11050 return freq;
11051}
11052
11053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011054static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
11055 int encrypt)
11056{
11057 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011058 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
11059 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011060}
11061
11062
11063static int nl80211_set_param(void *priv, const char *param)
11064{
11065 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
11066 if (param == NULL)
11067 return 0;
11068
11069#ifdef CONFIG_P2P
11070 if (os_strstr(param, "use_p2p_group_interface=1")) {
11071 struct i802_bss *bss = priv;
11072 struct wpa_driver_nl80211_data *drv = bss->drv;
11073
11074 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
11075 "interface");
11076 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
11077 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
11078 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011079
11080 if (os_strstr(param, "p2p_device=1")) {
11081 struct i802_bss *bss = priv;
11082 struct wpa_driver_nl80211_data *drv = bss->drv;
11083 drv->allow_p2p_device = 1;
11084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011085#endif /* CONFIG_P2P */
11086
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011087 if (os_strstr(param, "use_monitor=1")) {
11088 struct i802_bss *bss = priv;
11089 struct wpa_driver_nl80211_data *drv = bss->drv;
11090 drv->use_monitor = 1;
11091 }
11092
11093 if (os_strstr(param, "force_connect_cmd=1")) {
11094 struct i802_bss *bss = priv;
11095 struct wpa_driver_nl80211_data *drv = bss->drv;
11096 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011097 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011098 }
11099
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080011100 if (os_strstr(param, "no_offchannel_tx=1")) {
11101 struct i802_bss *bss = priv;
11102 struct wpa_driver_nl80211_data *drv = bss->drv;
11103 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
11104 drv->test_use_roc_tx = 1;
11105 }
11106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011107 return 0;
11108}
11109
11110
11111static void * nl80211_global_init(void)
11112{
11113 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011114 struct netlink_config *cfg;
11115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011116 global = os_zalloc(sizeof(*global));
11117 if (global == NULL)
11118 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011119 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011120 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011121 global->if_add_ifindex = -1;
11122
11123 cfg = os_zalloc(sizeof(*cfg));
11124 if (cfg == NULL)
11125 goto err;
11126
11127 cfg->ctx = global;
11128 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
11129 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
11130 global->netlink = netlink_init(cfg);
11131 if (global->netlink == NULL) {
11132 os_free(cfg);
11133 goto err;
11134 }
11135
11136 if (wpa_driver_nl80211_init_nl_global(global) < 0)
11137 goto err;
11138
11139 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
11140 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011141 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
11142 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011143 goto err;
11144 }
11145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011146 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011147
11148err:
11149 nl80211_global_deinit(global);
11150 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011151}
11152
11153
11154static void nl80211_global_deinit(void *priv)
11155{
11156 struct nl80211_global *global = priv;
11157 if (global == NULL)
11158 return;
11159 if (!dl_list_empty(&global->interfaces)) {
11160 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
11161 "nl80211_global_deinit",
11162 dl_list_len(&global->interfaces));
11163 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011164
11165 if (global->netlink)
11166 netlink_deinit(global->netlink);
11167
11168 nl_destroy_handles(&global->nl);
11169
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011170 if (global->nl_event)
11171 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011172
11173 nl_cb_put(global->nl_cb);
11174
11175 if (global->ioctl_sock >= 0)
11176 close(global->ioctl_sock);
11177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011178 os_free(global);
11179}
11180
11181
11182static const char * nl80211_get_radio_name(void *priv)
11183{
11184 struct i802_bss *bss = priv;
11185 struct wpa_driver_nl80211_data *drv = bss->drv;
11186 return drv->phyname;
11187}
11188
11189
Jouni Malinen75ecf522011-06-27 15:19:46 -070011190static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
11191 const u8 *pmkid)
11192{
11193 struct nl_msg *msg;
11194
11195 msg = nlmsg_alloc();
11196 if (!msg)
11197 return -ENOMEM;
11198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011199 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011200
11201 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
11202 if (pmkid)
11203 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
11204 if (bssid)
11205 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
11206
11207 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11208 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011209 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011210 return -ENOBUFS;
11211}
11212
11213
11214static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11215{
11216 struct i802_bss *bss = priv;
11217 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
11218 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
11219}
11220
11221
11222static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11223{
11224 struct i802_bss *bss = priv;
11225 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
11226 MAC2STR(bssid));
11227 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
11228}
11229
11230
11231static int nl80211_flush_pmkid(void *priv)
11232{
11233 struct i802_bss *bss = priv;
11234 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
11235 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
11236}
11237
11238
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011239static void clean_survey_results(struct survey_results *survey_results)
11240{
11241 struct freq_survey *survey, *tmp;
11242
11243 if (dl_list_empty(&survey_results->survey_list))
11244 return;
11245
11246 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
11247 struct freq_survey, list) {
11248 dl_list_del(&survey->list);
11249 os_free(survey);
11250 }
11251}
11252
11253
11254static void add_survey(struct nlattr **sinfo, u32 ifidx,
11255 struct dl_list *survey_list)
11256{
11257 struct freq_survey *survey;
11258
11259 survey = os_zalloc(sizeof(struct freq_survey));
11260 if (!survey)
11261 return;
11262
11263 survey->ifidx = ifidx;
11264 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11265 survey->filled = 0;
11266
11267 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11268 survey->nf = (int8_t)
11269 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11270 survey->filled |= SURVEY_HAS_NF;
11271 }
11272
11273 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11274 survey->channel_time =
11275 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11276 survey->filled |= SURVEY_HAS_CHAN_TIME;
11277 }
11278
11279 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11280 survey->channel_time_busy =
11281 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11282 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11283 }
11284
11285 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11286 survey->channel_time_rx =
11287 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11288 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11289 }
11290
11291 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11292 survey->channel_time_tx =
11293 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11294 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11295 }
11296
11297 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)",
11298 survey->freq,
11299 survey->nf,
11300 (unsigned long int) survey->channel_time,
11301 (unsigned long int) survey->channel_time_busy,
11302 (unsigned long int) survey->channel_time_tx,
11303 (unsigned long int) survey->channel_time_rx,
11304 survey->filled);
11305
11306 dl_list_add_tail(survey_list, &survey->list);
11307}
11308
11309
11310static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11311 unsigned int freq_filter)
11312{
11313 if (!freq_filter)
11314 return 1;
11315
11316 return freq_filter == surveyed_freq;
11317}
11318
11319
11320static int survey_handler(struct nl_msg *msg, void *arg)
11321{
11322 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11323 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11324 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11325 struct survey_results *survey_results;
11326 u32 surveyed_freq = 0;
11327 u32 ifidx;
11328
11329 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11330 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11331 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11332 };
11333
11334 survey_results = (struct survey_results *) arg;
11335
11336 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11337 genlmsg_attrlen(gnlh, 0), NULL);
11338
Dmitry Shmidt97672262014-02-03 13:02:54 -080011339 if (!tb[NL80211_ATTR_IFINDEX])
11340 return NL_SKIP;
11341
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011342 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11343
11344 if (!tb[NL80211_ATTR_SURVEY_INFO])
11345 return NL_SKIP;
11346
11347 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11348 tb[NL80211_ATTR_SURVEY_INFO],
11349 survey_policy))
11350 return NL_SKIP;
11351
11352 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11353 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11354 return NL_SKIP;
11355 }
11356
11357 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11358
11359 if (!check_survey_ok(sinfo, surveyed_freq,
11360 survey_results->freq_filter))
11361 return NL_SKIP;
11362
11363 if (survey_results->freq_filter &&
11364 survey_results->freq_filter != surveyed_freq) {
11365 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11366 surveyed_freq);
11367 return NL_SKIP;
11368 }
11369
11370 add_survey(sinfo, ifidx, &survey_results->survey_list);
11371
11372 return NL_SKIP;
11373}
11374
11375
11376static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11377{
11378 struct i802_bss *bss = priv;
11379 struct wpa_driver_nl80211_data *drv = bss->drv;
11380 struct nl_msg *msg;
11381 int err = -ENOBUFS;
11382 union wpa_event_data data;
11383 struct survey_results *survey_results;
11384
11385 os_memset(&data, 0, sizeof(data));
11386 survey_results = &data.survey_results;
11387
11388 dl_list_init(&survey_results->survey_list);
11389
11390 msg = nlmsg_alloc();
11391 if (!msg)
11392 goto nla_put_failure;
11393
11394 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11395
11396 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11397
11398 if (freq)
11399 data.survey_results.freq_filter = freq;
11400
11401 do {
11402 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11403 err = send_and_recv_msgs(drv, msg, survey_handler,
11404 survey_results);
11405 } while (err > 0);
11406
11407 if (err) {
11408 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11409 goto out_clean;
11410 }
11411
11412 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11413
11414out_clean:
11415 clean_survey_results(survey_results);
11416nla_put_failure:
11417 return err;
11418}
11419
11420
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011421static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11422 const u8 *replay_ctr)
11423{
11424 struct i802_bss *bss = priv;
11425 struct wpa_driver_nl80211_data *drv = bss->drv;
11426 struct nlattr *replay_nested;
11427 struct nl_msg *msg;
11428
11429 msg = nlmsg_alloc();
11430 if (!msg)
11431 return;
11432
11433 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11434
11435 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11436
11437 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11438 if (!replay_nested)
11439 goto nla_put_failure;
11440
11441 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11442 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11443 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11444 replay_ctr);
11445
11446 nla_nest_end(msg, replay_nested);
11447
11448 send_and_recv_msgs(drv, msg, NULL, NULL);
11449 return;
11450 nla_put_failure:
11451 nlmsg_free(msg);
11452}
11453
11454
11455static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11456 const u8 *addr, int qos)
11457{
11458 /* send data frame to poll STA and check whether
11459 * this frame is ACKed */
11460 struct {
11461 struct ieee80211_hdr hdr;
11462 u16 qos_ctl;
11463 } STRUCT_PACKED nulldata;
11464 size_t size;
11465
11466 /* Send data frame to poll STA and check whether this frame is ACKed */
11467
11468 os_memset(&nulldata, 0, sizeof(nulldata));
11469
11470 if (qos) {
11471 nulldata.hdr.frame_control =
11472 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11473 WLAN_FC_STYPE_QOS_NULL);
11474 size = sizeof(nulldata);
11475 } else {
11476 nulldata.hdr.frame_control =
11477 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11478 WLAN_FC_STYPE_NULLFUNC);
11479 size = sizeof(struct ieee80211_hdr);
11480 }
11481
11482 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11483 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11484 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11485 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11486
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011487 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11488 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011489 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11490 "send poll frame");
11491}
11492
11493static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11494 int qos)
11495{
11496 struct i802_bss *bss = priv;
11497 struct wpa_driver_nl80211_data *drv = bss->drv;
11498 struct nl_msg *msg;
11499
11500 if (!drv->poll_command_supported) {
11501 nl80211_send_null_frame(bss, own_addr, addr, qos);
11502 return;
11503 }
11504
11505 msg = nlmsg_alloc();
11506 if (!msg)
11507 return;
11508
11509 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11510
11511 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11512 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11513
11514 send_and_recv_msgs(drv, msg, NULL, NULL);
11515 return;
11516 nla_put_failure:
11517 nlmsg_free(msg);
11518}
11519
11520
11521static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11522{
11523 struct nl_msg *msg;
11524
11525 msg = nlmsg_alloc();
11526 if (!msg)
11527 return -ENOMEM;
11528
11529 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11530 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11531 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11532 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11533 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11534nla_put_failure:
11535 nlmsg_free(msg);
11536 return -ENOBUFS;
11537}
11538
11539
11540static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11541 int ctwindow)
11542{
11543 struct i802_bss *bss = priv;
11544
11545 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11546 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11547
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011548 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011549#ifdef ANDROID_P2P
11550 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011551#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011552 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011553#endif /* ANDROID_P2P */
11554 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011555
11556 if (legacy_ps == -1)
11557 return 0;
11558 if (legacy_ps != 0 && legacy_ps != 1)
11559 return -1; /* Not yet supported */
11560
11561 return nl80211_set_power_save(bss, legacy_ps);
11562}
11563
11564
Dmitry Shmidt051af732013-10-22 13:52:46 -070011565static int nl80211_start_radar_detection(void *priv,
11566 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011567{
11568 struct i802_bss *bss = priv;
11569 struct wpa_driver_nl80211_data *drv = bss->drv;
11570 struct nl_msg *msg;
11571 int ret;
11572
Dmitry Shmidt051af732013-10-22 13:52:46 -070011573 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)",
11574 freq->freq, freq->ht_enabled, freq->vht_enabled,
11575 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11576
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011577 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11578 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11579 "detection");
11580 return -1;
11581 }
11582
11583 msg = nlmsg_alloc();
11584 if (!msg)
11585 return -1;
11586
11587 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11588 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011589
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011590 if (nl80211_put_freq_params(msg, freq) < 0)
11591 goto nla_put_failure;
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011592
11593 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011594 msg = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011595 if (ret == 0)
11596 return 0;
11597 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11598 "%d (%s)", ret, strerror(-ret));
11599nla_put_failure:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011600 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011601 return -1;
11602}
11603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011604#ifdef CONFIG_TDLS
11605
11606static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11607 u8 dialog_token, u16 status_code,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011608 u32 peer_capab, const u8 *buf, size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011609{
11610 struct i802_bss *bss = priv;
11611 struct wpa_driver_nl80211_data *drv = bss->drv;
11612 struct nl_msg *msg;
11613
11614 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11615 return -EOPNOTSUPP;
11616
11617 if (!dst)
11618 return -EINVAL;
11619
11620 msg = nlmsg_alloc();
11621 if (!msg)
11622 return -ENOMEM;
11623
11624 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11625 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11626 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11627 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11628 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11629 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011630 if (peer_capab) {
11631 /*
11632 * The internal enum tdls_peer_capability definition is
11633 * currently identical with the nl80211 enum
11634 * nl80211_tdls_peer_capability, so no conversion is needed
11635 * here.
11636 */
11637 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11638 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011639 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11640
11641 return send_and_recv_msgs(drv, msg, NULL, NULL);
11642
11643nla_put_failure:
11644 nlmsg_free(msg);
11645 return -ENOBUFS;
11646}
11647
11648
11649static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11650{
11651 struct i802_bss *bss = priv;
11652 struct wpa_driver_nl80211_data *drv = bss->drv;
11653 struct nl_msg *msg;
11654 enum nl80211_tdls_operation nl80211_oper;
11655
11656 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11657 return -EOPNOTSUPP;
11658
11659 switch (oper) {
11660 case TDLS_DISCOVERY_REQ:
11661 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11662 break;
11663 case TDLS_SETUP:
11664 nl80211_oper = NL80211_TDLS_SETUP;
11665 break;
11666 case TDLS_TEARDOWN:
11667 nl80211_oper = NL80211_TDLS_TEARDOWN;
11668 break;
11669 case TDLS_ENABLE_LINK:
11670 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11671 break;
11672 case TDLS_DISABLE_LINK:
11673 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11674 break;
11675 case TDLS_ENABLE:
11676 return 0;
11677 case TDLS_DISABLE:
11678 return 0;
11679 default:
11680 return -EINVAL;
11681 }
11682
11683 msg = nlmsg_alloc();
11684 if (!msg)
11685 return -ENOMEM;
11686
11687 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11688 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11689 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11690 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11691
11692 return send_and_recv_msgs(drv, msg, NULL, NULL);
11693
11694nla_put_failure:
11695 nlmsg_free(msg);
11696 return -ENOBUFS;
11697}
11698
11699#endif /* CONFIG TDLS */
11700
11701
11702#ifdef ANDROID
11703
11704typedef struct android_wifi_priv_cmd {
11705 char *buf;
11706 int used_len;
11707 int total_len;
11708} android_wifi_priv_cmd;
11709
11710static int drv_errors = 0;
11711
11712static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11713{
11714 drv_errors++;
11715 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11716 drv_errors = 0;
11717 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11718 }
11719}
11720
11721
11722static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11723{
11724 struct wpa_driver_nl80211_data *drv = bss->drv;
11725 struct ifreq ifr;
11726 android_wifi_priv_cmd priv_cmd;
11727 char buf[MAX_DRV_CMD_SIZE];
11728 int ret;
11729
11730 os_memset(&ifr, 0, sizeof(ifr));
11731 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11732 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11733
11734 os_memset(buf, 0, sizeof(buf));
11735 os_strlcpy(buf, cmd, sizeof(buf));
11736
11737 priv_cmd.buf = buf;
11738 priv_cmd.used_len = sizeof(buf);
11739 priv_cmd.total_len = sizeof(buf);
11740 ifr.ifr_data = &priv_cmd;
11741
11742 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11743 if (ret < 0) {
11744 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11745 __func__);
11746 wpa_driver_send_hang_msg(drv);
11747 return ret;
11748 }
11749
11750 drv_errors = 0;
11751 return 0;
11752}
11753
11754
11755static int android_pno_start(struct i802_bss *bss,
11756 struct wpa_driver_scan_params *params)
11757{
11758 struct wpa_driver_nl80211_data *drv = bss->drv;
11759 struct ifreq ifr;
11760 android_wifi_priv_cmd priv_cmd;
11761 int ret = 0, i = 0, bp;
11762 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11763
11764 bp = WEXT_PNOSETUP_HEADER_SIZE;
11765 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11766 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11767 buf[bp++] = WEXT_PNO_TLV_VERSION;
11768 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11769 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11770
11771 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11772 /* Check that there is enough space needed for 1 more SSID, the
11773 * other sections and null termination */
11774 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11775 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11776 break;
11777 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11778 params->ssids[i].ssid,
11779 params->ssids[i].ssid_len);
11780 buf[bp++] = WEXT_PNO_SSID_SECTION;
11781 buf[bp++] = params->ssids[i].ssid_len;
11782 os_memcpy(&buf[bp], params->ssids[i].ssid,
11783 params->ssids[i].ssid_len);
11784 bp += params->ssids[i].ssid_len;
11785 i++;
11786 }
11787
11788 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11789 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11790 WEXT_PNO_SCAN_INTERVAL);
11791 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11792
11793 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11794 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11795 WEXT_PNO_REPEAT);
11796 bp += WEXT_PNO_REPEAT_LENGTH;
11797
11798 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11799 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11800 WEXT_PNO_MAX_REPEAT);
11801 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11802
11803 memset(&ifr, 0, sizeof(ifr));
11804 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011805 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011806
11807 priv_cmd.buf = buf;
11808 priv_cmd.used_len = bp;
11809 priv_cmd.total_len = bp;
11810 ifr.ifr_data = &priv_cmd;
11811
11812 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11813
11814 if (ret < 0) {
11815 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11816 ret);
11817 wpa_driver_send_hang_msg(drv);
11818 return ret;
11819 }
11820
11821 drv_errors = 0;
11822
11823 return android_priv_cmd(bss, "PNOFORCE 1");
11824}
11825
11826
11827static int android_pno_stop(struct i802_bss *bss)
11828{
11829 return android_priv_cmd(bss, "PNOFORCE 0");
11830}
11831
11832#endif /* ANDROID */
11833
11834
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011835static int driver_nl80211_set_key(const char *ifname, void *priv,
11836 enum wpa_alg alg, const u8 *addr,
11837 int key_idx, int set_tx,
11838 const u8 *seq, size_t seq_len,
11839 const u8 *key, size_t key_len)
11840{
11841 struct i802_bss *bss = priv;
11842 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11843 set_tx, seq, seq_len, key, key_len);
11844}
11845
11846
11847static int driver_nl80211_scan2(void *priv,
11848 struct wpa_driver_scan_params *params)
11849{
11850 struct i802_bss *bss = priv;
11851 return wpa_driver_nl80211_scan(bss, params);
11852}
11853
11854
11855static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11856 int reason_code)
11857{
11858 struct i802_bss *bss = priv;
11859 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11860}
11861
11862
11863static int driver_nl80211_authenticate(void *priv,
11864 struct wpa_driver_auth_params *params)
11865{
11866 struct i802_bss *bss = priv;
11867 return wpa_driver_nl80211_authenticate(bss, params);
11868}
11869
11870
11871static void driver_nl80211_deinit(void *priv)
11872{
11873 struct i802_bss *bss = priv;
11874 wpa_driver_nl80211_deinit(bss);
11875}
11876
11877
11878static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11879 const char *ifname)
11880{
11881 struct i802_bss *bss = priv;
11882 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11883}
11884
11885
11886static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11887 size_t data_len, int noack)
11888{
11889 struct i802_bss *bss = priv;
11890 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11891 0, 0, 0, 0);
11892}
11893
11894
11895static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11896{
11897 struct i802_bss *bss = priv;
11898 return wpa_driver_nl80211_sta_remove(bss, addr);
11899}
11900
11901
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011902static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11903 const char *ifname, int vlan_id)
11904{
11905 struct i802_bss *bss = priv;
11906 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11907}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011908
11909
11910static int driver_nl80211_read_sta_data(void *priv,
11911 struct hostap_sta_driver_data *data,
11912 const u8 *addr)
11913{
11914 struct i802_bss *bss = priv;
11915 return i802_read_sta_data(bss, data, addr);
11916}
11917
11918
11919static int driver_nl80211_send_action(void *priv, unsigned int freq,
11920 unsigned int wait_time,
11921 const u8 *dst, const u8 *src,
11922 const u8 *bssid,
11923 const u8 *data, size_t data_len,
11924 int no_cck)
11925{
11926 struct i802_bss *bss = priv;
11927 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11928 bssid, data, data_len, no_cck);
11929}
11930
11931
11932static int driver_nl80211_probe_req_report(void *priv, int report)
11933{
11934 struct i802_bss *bss = priv;
11935 return wpa_driver_nl80211_probe_req_report(bss, report);
11936}
11937
11938
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011939static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11940 const u8 *ies, size_t ies_len)
11941{
11942 int ret;
11943 struct nl_msg *msg;
11944 struct i802_bss *bss = priv;
11945 struct wpa_driver_nl80211_data *drv = bss->drv;
11946 u16 mdid = WPA_GET_LE16(md);
11947
11948 msg = nlmsg_alloc();
11949 if (!msg)
11950 return -ENOMEM;
11951
11952 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11953 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11954 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11955 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11956 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11957
11958 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11959 if (ret) {
11960 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
11961 "err=%d (%s)", ret, strerror(-ret));
11962 }
11963
11964 return ret;
11965
11966nla_put_failure:
11967 nlmsg_free(msg);
11968 return -ENOBUFS;
11969}
11970
11971
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011972const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
11973{
11974 struct i802_bss *bss = priv;
11975 struct wpa_driver_nl80211_data *drv = bss->drv;
11976
11977 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
11978 return NULL;
11979
11980 return bss->addr;
11981}
11982
11983
Dmitry Shmidt56052862013-10-04 10:23:25 -070011984static const char * scan_state_str(enum scan_states scan_state)
11985{
11986 switch (scan_state) {
11987 case NO_SCAN:
11988 return "NO_SCAN";
11989 case SCAN_REQUESTED:
11990 return "SCAN_REQUESTED";
11991 case SCAN_STARTED:
11992 return "SCAN_STARTED";
11993 case SCAN_COMPLETED:
11994 return "SCAN_COMPLETED";
11995 case SCAN_ABORTED:
11996 return "SCAN_ABORTED";
11997 case SCHED_SCAN_STARTED:
11998 return "SCHED_SCAN_STARTED";
11999 case SCHED_SCAN_STOPPED:
12000 return "SCHED_SCAN_STOPPED";
12001 case SCHED_SCAN_RESULTS:
12002 return "SCHED_SCAN_RESULTS";
12003 }
12004
12005 return "??";
12006}
12007
12008
12009static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
12010{
12011 struct i802_bss *bss = priv;
12012 struct wpa_driver_nl80211_data *drv = bss->drv;
12013 int res;
12014 char *pos, *end;
12015
12016 pos = buf;
12017 end = buf + buflen;
12018
12019 res = os_snprintf(pos, end - pos,
12020 "ifindex=%d\n"
12021 "ifname=%s\n"
12022 "brname=%s\n"
12023 "addr=" MACSTR "\n"
12024 "freq=%d\n"
12025 "%s%s%s%s%s",
12026 bss->ifindex,
12027 bss->ifname,
12028 bss->brname,
12029 MAC2STR(bss->addr),
12030 bss->freq,
12031 bss->beacon_set ? "beacon_set=1\n" : "",
12032 bss->added_if_into_bridge ?
12033 "added_if_into_bridge=1\n" : "",
12034 bss->added_bridge ? "added_bridge=1\n" : "",
12035 bss->in_deinit ? "in_deinit=1\n" : "",
12036 bss->if_dynamic ? "if_dynamic=1\n" : "");
12037 if (res < 0 || res >= end - pos)
12038 return pos - buf;
12039 pos += res;
12040
12041 if (bss->wdev_id_set) {
12042 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
12043 (unsigned long long) bss->wdev_id);
12044 if (res < 0 || res >= end - pos)
12045 return pos - buf;
12046 pos += res;
12047 }
12048
12049 res = os_snprintf(pos, end - pos,
12050 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012051 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070012052 "drv_ifindex=%d\n"
12053 "operstate=%d\n"
12054 "scan_state=%s\n"
12055 "auth_bssid=" MACSTR "\n"
12056 "auth_attempt_bssid=" MACSTR "\n"
12057 "bssid=" MACSTR "\n"
12058 "prev_bssid=" MACSTR "\n"
12059 "associated=%d\n"
12060 "assoc_freq=%u\n"
12061 "monitor_sock=%d\n"
12062 "monitor_ifidx=%d\n"
12063 "monitor_refcount=%d\n"
12064 "last_mgmt_freq=%u\n"
12065 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070012066 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070012067 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012068 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -070012069 drv->ifindex,
12070 drv->operstate,
12071 scan_state_str(drv->scan_state),
12072 MAC2STR(drv->auth_bssid),
12073 MAC2STR(drv->auth_attempt_bssid),
12074 MAC2STR(drv->bssid),
12075 MAC2STR(drv->prev_bssid),
12076 drv->associated,
12077 drv->assoc_freq,
12078 drv->monitor_sock,
12079 drv->monitor_ifidx,
12080 drv->monitor_refcount,
12081 drv->last_mgmt_freq,
12082 drv->eapol_tx_sock,
12083 drv->ignore_if_down_event ?
12084 "ignore_if_down_event=1\n" : "",
12085 drv->scan_complete_events ?
12086 "scan_complete_events=1\n" : "",
12087 drv->disabled_11b_rates ?
12088 "disabled_11b_rates=1\n" : "",
12089 drv->pending_remain_on_chan ?
12090 "pending_remain_on_chan=1\n" : "",
12091 drv->in_interface_list ? "in_interface_list=1\n" : "",
12092 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
12093 drv->poll_command_supported ?
12094 "poll_command_supported=1\n" : "",
12095 drv->data_tx_status ? "data_tx_status=1\n" : "",
12096 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
12097 drv->retry_auth ? "retry_auth=1\n" : "",
12098 drv->use_monitor ? "use_monitor=1\n" : "",
12099 drv->ignore_next_local_disconnect ?
12100 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070012101 drv->ignore_next_local_deauth ?
12102 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070012103 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
12104 if (res < 0 || res >= end - pos)
12105 return pos - buf;
12106 pos += res;
12107
12108 if (drv->has_capability) {
12109 res = os_snprintf(pos, end - pos,
12110 "capa.key_mgmt=0x%x\n"
12111 "capa.enc=0x%x\n"
12112 "capa.auth=0x%x\n"
12113 "capa.flags=0x%x\n"
12114 "capa.max_scan_ssids=%d\n"
12115 "capa.max_sched_scan_ssids=%d\n"
12116 "capa.sched_scan_supported=%d\n"
12117 "capa.max_match_sets=%d\n"
12118 "capa.max_remain_on_chan=%u\n"
12119 "capa.max_stations=%u\n"
12120 "capa.probe_resp_offloads=0x%x\n"
12121 "capa.max_acl_mac_addrs=%u\n"
12122 "capa.num_multichan_concurrent=%u\n",
12123 drv->capa.key_mgmt,
12124 drv->capa.enc,
12125 drv->capa.auth,
12126 drv->capa.flags,
12127 drv->capa.max_scan_ssids,
12128 drv->capa.max_sched_scan_ssids,
12129 drv->capa.sched_scan_supported,
12130 drv->capa.max_match_sets,
12131 drv->capa.max_remain_on_chan,
12132 drv->capa.max_stations,
12133 drv->capa.probe_resp_offloads,
12134 drv->capa.max_acl_mac_addrs,
12135 drv->capa.num_multichan_concurrent);
12136 if (res < 0 || res >= end - pos)
12137 return pos - buf;
12138 pos += res;
12139 }
12140
12141 return pos - buf;
12142}
12143
12144
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012145static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
12146{
12147 if (settings->head)
12148 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
12149 settings->head_len, settings->head);
12150
12151 if (settings->tail)
12152 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
12153 settings->tail_len, settings->tail);
12154
12155 if (settings->beacon_ies)
12156 NLA_PUT(msg, NL80211_ATTR_IE,
12157 settings->beacon_ies_len, settings->beacon_ies);
12158
12159 if (settings->proberesp_ies)
12160 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
12161 settings->proberesp_ies_len, settings->proberesp_ies);
12162
12163 if (settings->assocresp_ies)
12164 NLA_PUT(msg,
12165 NL80211_ATTR_IE_ASSOC_RESP,
12166 settings->assocresp_ies_len, settings->assocresp_ies);
12167
12168 if (settings->probe_resp)
12169 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
12170 settings->probe_resp_len, settings->probe_resp);
12171
12172 return 0;
12173
12174nla_put_failure:
12175 return -ENOBUFS;
12176}
12177
12178
12179static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
12180{
12181 struct nl_msg *msg;
12182 struct i802_bss *bss = priv;
12183 struct wpa_driver_nl80211_data *drv = bss->drv;
12184 struct nlattr *beacon_csa;
12185 int ret = -ENOBUFS;
12186
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012187 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 -080012188 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012189 settings->freq_params.freq, settings->freq_params.bandwidth,
12190 settings->freq_params.center_freq1,
12191 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012192
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012193 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012194 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
12195 return -EOPNOTSUPP;
12196 }
12197
12198 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
12199 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
12200 return -EOPNOTSUPP;
12201
12202 /* check settings validity */
12203 if (!settings->beacon_csa.tail ||
12204 ((settings->beacon_csa.tail_len <=
12205 settings->counter_offset_beacon) ||
12206 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
12207 settings->cs_count)))
12208 return -EINVAL;
12209
12210 if (settings->beacon_csa.probe_resp &&
12211 ((settings->beacon_csa.probe_resp_len <=
12212 settings->counter_offset_presp) ||
12213 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
12214 settings->cs_count)))
12215 return -EINVAL;
12216
12217 msg = nlmsg_alloc();
12218 if (!msg)
12219 return -ENOMEM;
12220
12221 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -070012222 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012223 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
12224 ret = nl80211_put_freq_params(msg, &settings->freq_params);
12225 if (ret)
12226 goto error;
12227
12228 if (settings->block_tx)
12229 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12230
12231 /* beacon_after params */
12232 ret = set_beacon_data(msg, &settings->beacon_after);
12233 if (ret)
12234 goto error;
12235
12236 /* beacon_csa params */
12237 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12238 if (!beacon_csa)
12239 goto nla_put_failure;
12240
12241 ret = set_beacon_data(msg, &settings->beacon_csa);
12242 if (ret)
12243 goto error;
12244
12245 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12246 settings->counter_offset_beacon);
12247
12248 if (settings->beacon_csa.probe_resp)
12249 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12250 settings->counter_offset_presp);
12251
12252 nla_nest_end(msg, beacon_csa);
12253 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12254 if (ret) {
12255 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12256 ret, strerror(-ret));
12257 }
12258 return ret;
12259
12260nla_put_failure:
12261 ret = -ENOBUFS;
12262error:
12263 nlmsg_free(msg);
12264 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12265 return ret;
12266}
12267
12268
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012269#ifdef CONFIG_TESTING_OPTIONS
12270static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12271{
12272 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12273 struct wpabuf *buf = arg;
12274
12275 if (!buf)
12276 return NL_SKIP;
12277
12278 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12279 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12280 return NL_SKIP;
12281 }
12282
12283 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12284 genlmsg_attrlen(gnlh, 0));
12285
12286 return NL_SKIP;
12287}
12288#endif /* CONFIG_TESTING_OPTIONS */
12289
12290
12291static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12292{
12293 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12294 struct nlattr *nl_vendor_reply, *nl;
12295 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12296 struct wpabuf *buf = arg;
12297 int rem;
12298
12299 if (!buf)
12300 return NL_SKIP;
12301
12302 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12303 genlmsg_attrlen(gnlh, 0), NULL);
12304 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12305
12306 if (!nl_vendor_reply)
12307 return NL_SKIP;
12308
12309 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12310 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12311 return NL_SKIP;
12312 }
12313
12314 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12315 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12316 }
12317
12318 return NL_SKIP;
12319}
12320
12321
12322static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12323 unsigned int subcmd, const u8 *data,
12324 size_t data_len, struct wpabuf *buf)
12325{
12326 struct i802_bss *bss = priv;
12327 struct wpa_driver_nl80211_data *drv = bss->drv;
12328 struct nl_msg *msg;
12329 int ret;
12330
12331 msg = nlmsg_alloc();
12332 if (!msg)
12333 return -ENOMEM;
12334
12335#ifdef CONFIG_TESTING_OPTIONS
12336 if (vendor_id == 0xffffffff) {
12337 nl80211_cmd(drv, msg, 0, subcmd);
12338 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12339 0)
12340 goto nla_put_failure;
12341 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12342 if (ret)
12343 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12344 ret);
12345 return ret;
12346 }
12347#endif /* CONFIG_TESTING_OPTIONS */
12348
12349 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12350 if (nl80211_set_iface_id(msg, bss) < 0)
12351 goto nla_put_failure;
12352 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12353 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12354 if (data)
12355 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12356
12357 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12358 if (ret)
12359 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12360 ret);
12361 return ret;
12362
12363nla_put_failure:
12364 nlmsg_free(msg);
12365 return -ENOBUFS;
12366}
12367
12368
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012369static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12370 u8 qos_map_set_len)
12371{
12372 struct i802_bss *bss = priv;
12373 struct wpa_driver_nl80211_data *drv = bss->drv;
12374 struct nl_msg *msg;
12375 int ret;
12376
12377 msg = nlmsg_alloc();
12378 if (!msg)
12379 return -ENOMEM;
12380
12381 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12382 qos_map_set, qos_map_set_len);
12383
12384 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12385 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12386 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12387
12388 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12389 if (ret)
12390 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12391
12392 return ret;
12393
12394nla_put_failure:
12395 nlmsg_free(msg);
12396 return -ENOBUFS;
12397}
12398
12399
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012400static int nl80211_set_wowlan(void *priv,
12401 const struct wowlan_triggers *triggers)
12402{
12403 struct i802_bss *bss = priv;
12404 struct wpa_driver_nl80211_data *drv = bss->drv;
12405 struct nl_msg *msg;
12406 struct nlattr *wowlan_triggers;
12407 int ret;
12408
12409 msg = nlmsg_alloc();
12410 if (!msg)
12411 return -ENOMEM;
12412
12413 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12414
12415 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12416 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12417
12418 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12419 if (!wowlan_triggers)
12420 goto nla_put_failure;
12421
12422 if (triggers->any)
12423 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12424 if (triggers->disconnect)
12425 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12426 if (triggers->magic_pkt)
12427 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12428 if (triggers->gtk_rekey_failure)
12429 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12430 if (triggers->eap_identity_req)
12431 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12432 if (triggers->four_way_handshake)
12433 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12434 if (triggers->rfkill_release)
12435 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12436
12437 nla_nest_end(msg, wowlan_triggers);
12438
12439 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12440 if (ret)
12441 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12442
12443 return ret;
12444
12445nla_put_failure:
12446 nlmsg_free(msg);
12447 return -ENOBUFS;
12448}
12449
12450
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012451static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
12452{
12453 struct i802_bss *bss = priv;
12454 struct wpa_driver_nl80211_data *drv = bss->drv;
12455 struct nl_msg *msg;
12456 struct nlattr *params;
12457
12458 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
12459
12460 if (!drv->roaming_vendor_cmd_avail) {
12461 wpa_printf(MSG_DEBUG,
12462 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
12463 return -1;
12464 }
12465
12466 msg = nlmsg_alloc();
12467 if (!msg)
12468 return -ENOMEM;
12469
12470 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12471
12472 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12473 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
12474 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12475 QCA_NL80211_VENDOR_SUBCMD_ROAMING);
12476
12477 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
12478 if (!params)
12479 goto nla_put_failure;
12480 NLA_PUT_U32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
12481 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
12482 QCA_ROAMING_NOT_ALLOWED);
12483 if (bssid)
12484 NLA_PUT(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid);
12485 nla_nest_end(msg, params);
12486
12487 return send_and_recv_msgs(drv, msg, NULL, NULL);
12488
12489 nla_put_failure:
12490 nlmsg_free(msg);
12491 return -1;
12492}
12493
12494
12495static int nl80211_set_mac_addr(void *priv, const u8 *addr)
12496{
12497 struct i802_bss *bss = priv;
12498 struct wpa_driver_nl80211_data *drv = bss->drv;
12499 int new_addr = addr != NULL;
12500
12501 if (!addr)
12502 addr = drv->perm_addr;
12503
12504 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
12505 return -1;
12506
12507 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
12508 {
12509 wpa_printf(MSG_DEBUG,
12510 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12511 bss->ifname, MAC2STR(addr));
12512 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
12513 1) < 0) {
12514 wpa_printf(MSG_DEBUG,
12515 "nl80211: Could not restore interface UP after failed set_mac_addr");
12516 }
12517 return -1;
12518 }
12519
12520 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
12521 bss->ifname, MAC2STR(addr));
12522 drv->addr_changed = new_addr;
12523 os_memcpy(bss->addr, addr, ETH_ALEN);
12524
12525 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12526 {
12527 wpa_printf(MSG_DEBUG,
12528 "nl80211: Could not restore interface UP after set_mac_addr");
12529 }
12530
12531 return 0;
12532}
12533
12534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012535const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12536 .name = "nl80211",
12537 .desc = "Linux nl80211/cfg80211",
12538 .get_bssid = wpa_driver_nl80211_get_bssid,
12539 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012540 .set_key = driver_nl80211_set_key,
12541 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012542 .sched_scan = wpa_driver_nl80211_sched_scan,
12543 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012544 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012545 .deauthenticate = driver_nl80211_deauthenticate,
12546 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012547 .associate = wpa_driver_nl80211_associate,
12548 .global_init = nl80211_global_init,
12549 .global_deinit = nl80211_global_deinit,
12550 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012551 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012552 .get_capa = wpa_driver_nl80211_get_capa,
12553 .set_operstate = wpa_driver_nl80211_set_operstate,
12554 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12555 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012556 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012557 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012558 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012559 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012560 .if_remove = driver_nl80211_if_remove,
12561 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012562 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12563 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012564 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012565 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12566 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012567 .hapd_init = i802_init,
12568 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012569 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012570 .get_seqnum = i802_get_seqnum,
12571 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012572 .get_inact_sec = i802_get_inact_sec,
12573 .sta_clear_stats = i802_sta_clear_stats,
12574 .set_rts = i802_set_rts,
12575 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012576 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012577 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012578 .sta_deauth = i802_sta_deauth,
12579 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012580 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012581 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012582 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012583 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12584 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12585 .cancel_remain_on_channel =
12586 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012587 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012588 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012589 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012590 .resume = wpa_driver_nl80211_resume,
12591 .send_ft_action = nl80211_send_ft_action,
12592 .signal_monitor = nl80211_signal_monitor,
12593 .signal_poll = nl80211_signal_poll,
12594 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012595 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012596 .set_param = nl80211_set_param,
12597 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012598 .add_pmkid = nl80211_add_pmkid,
12599 .remove_pmkid = nl80211_remove_pmkid,
12600 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012601 .set_rekey_info = nl80211_set_rekey_info,
12602 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012603 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012604 .start_dfs_cac = nl80211_start_radar_detection,
12605 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012606#ifdef CONFIG_TDLS
12607 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12608 .tdls_oper = nl80211_tdls_oper,
12609#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012610 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012611 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012612 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012613 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012614 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012615#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012616 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012617 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012618 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012619#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012620#ifdef ANDROID
12621 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012622#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012623 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012624 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012625 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012626 .roaming = nl80211_roaming,
12627 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012628};