blob: 4c8f29f349de40f4c530fc8bd27cd52fea7a4794 [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 Shmidt9ead16e2014-10-07 13:15:23 -0700334 struct nl_handle *rtnl_sk; /* nl_sock for NETLINK_ROUTE */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700335
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 Shmidt9ead16e2014-10-07 13:15:23 -0700361static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
362 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800365wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
366 const u8 *set_addr, int first);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
368 const u8 *addr, int cmd, u16 reason_code,
369 int local_state_change);
370static void nl80211_remove_monitor_interface(
371 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800372static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374 const u8 *buf, size_t buf_len, u64 *cookie,
375 int no_cck, int no_ack, int offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700376static int nl80211_register_frame(struct i802_bss *bss,
377 struct nl_handle *hl_handle,
378 u16 type, const u8 *match, size_t match_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800379static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
380 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381#ifdef ANDROID
382static int android_pno_start(struct i802_bss *bss,
383 struct wpa_driver_scan_params *params);
384static int android_pno_stop(struct i802_bss *bss);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800385extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
386 size_t buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800387#endif /* ANDROID */
388#ifdef ANDROID_P2P
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700389#ifdef ANDROID_P2P_STUB
390int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration) {
391 return 0;
392}
393int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len) {
394 return 0;
395}
396int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow) {
397 return -1;
398}
399int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
400 const struct wpabuf *proberesp,
401 const struct wpabuf *assocresp) {
402 return 0;
403}
404#else /* ANDROID_P2P_STUB */
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700405int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800406int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700407int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
408int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800409 const struct wpabuf *proberesp,
410 const struct wpabuf *assocresp);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700411#endif /* ANDROID_P2P_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800412#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
415static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
416static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800417static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 enum wpa_driver_if_type type,
419 const char *ifname);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700420
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700421static int nl80211_set_channel(struct i802_bss *bss,
422 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
424 int ifindex, int disabled);
425
426static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800427static int wpa_driver_nl80211_authenticate_retry(
428 struct wpa_driver_nl80211_data *drv);
429
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700430static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700431static int i802_set_iface_flags(struct i802_bss *bss, int up);
432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800433
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700434static const char * nl80211_command_to_string(enum nl80211_commands cmd)
435{
436#define C2S(x) case x: return #x;
437 switch (cmd) {
438 C2S(NL80211_CMD_UNSPEC)
439 C2S(NL80211_CMD_GET_WIPHY)
440 C2S(NL80211_CMD_SET_WIPHY)
441 C2S(NL80211_CMD_NEW_WIPHY)
442 C2S(NL80211_CMD_DEL_WIPHY)
443 C2S(NL80211_CMD_GET_INTERFACE)
444 C2S(NL80211_CMD_SET_INTERFACE)
445 C2S(NL80211_CMD_NEW_INTERFACE)
446 C2S(NL80211_CMD_DEL_INTERFACE)
447 C2S(NL80211_CMD_GET_KEY)
448 C2S(NL80211_CMD_SET_KEY)
449 C2S(NL80211_CMD_NEW_KEY)
450 C2S(NL80211_CMD_DEL_KEY)
451 C2S(NL80211_CMD_GET_BEACON)
452 C2S(NL80211_CMD_SET_BEACON)
453 C2S(NL80211_CMD_START_AP)
454 C2S(NL80211_CMD_STOP_AP)
455 C2S(NL80211_CMD_GET_STATION)
456 C2S(NL80211_CMD_SET_STATION)
457 C2S(NL80211_CMD_NEW_STATION)
458 C2S(NL80211_CMD_DEL_STATION)
459 C2S(NL80211_CMD_GET_MPATH)
460 C2S(NL80211_CMD_SET_MPATH)
461 C2S(NL80211_CMD_NEW_MPATH)
462 C2S(NL80211_CMD_DEL_MPATH)
463 C2S(NL80211_CMD_SET_BSS)
464 C2S(NL80211_CMD_SET_REG)
465 C2S(NL80211_CMD_REQ_SET_REG)
466 C2S(NL80211_CMD_GET_MESH_CONFIG)
467 C2S(NL80211_CMD_SET_MESH_CONFIG)
468 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
469 C2S(NL80211_CMD_GET_REG)
470 C2S(NL80211_CMD_GET_SCAN)
471 C2S(NL80211_CMD_TRIGGER_SCAN)
472 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
473 C2S(NL80211_CMD_SCAN_ABORTED)
474 C2S(NL80211_CMD_REG_CHANGE)
475 C2S(NL80211_CMD_AUTHENTICATE)
476 C2S(NL80211_CMD_ASSOCIATE)
477 C2S(NL80211_CMD_DEAUTHENTICATE)
478 C2S(NL80211_CMD_DISASSOCIATE)
479 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
480 C2S(NL80211_CMD_REG_BEACON_HINT)
481 C2S(NL80211_CMD_JOIN_IBSS)
482 C2S(NL80211_CMD_LEAVE_IBSS)
483 C2S(NL80211_CMD_TESTMODE)
484 C2S(NL80211_CMD_CONNECT)
485 C2S(NL80211_CMD_ROAM)
486 C2S(NL80211_CMD_DISCONNECT)
487 C2S(NL80211_CMD_SET_WIPHY_NETNS)
488 C2S(NL80211_CMD_GET_SURVEY)
489 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
490 C2S(NL80211_CMD_SET_PMKSA)
491 C2S(NL80211_CMD_DEL_PMKSA)
492 C2S(NL80211_CMD_FLUSH_PMKSA)
493 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
494 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
495 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
496 C2S(NL80211_CMD_REGISTER_FRAME)
497 C2S(NL80211_CMD_FRAME)
498 C2S(NL80211_CMD_FRAME_TX_STATUS)
499 C2S(NL80211_CMD_SET_POWER_SAVE)
500 C2S(NL80211_CMD_GET_POWER_SAVE)
501 C2S(NL80211_CMD_SET_CQM)
502 C2S(NL80211_CMD_NOTIFY_CQM)
503 C2S(NL80211_CMD_SET_CHANNEL)
504 C2S(NL80211_CMD_SET_WDS_PEER)
505 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
506 C2S(NL80211_CMD_JOIN_MESH)
507 C2S(NL80211_CMD_LEAVE_MESH)
508 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
509 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
510 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
511 C2S(NL80211_CMD_GET_WOWLAN)
512 C2S(NL80211_CMD_SET_WOWLAN)
513 C2S(NL80211_CMD_START_SCHED_SCAN)
514 C2S(NL80211_CMD_STOP_SCHED_SCAN)
515 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
516 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
517 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
518 C2S(NL80211_CMD_PMKSA_CANDIDATE)
519 C2S(NL80211_CMD_TDLS_OPER)
520 C2S(NL80211_CMD_TDLS_MGMT)
521 C2S(NL80211_CMD_UNEXPECTED_FRAME)
522 C2S(NL80211_CMD_PROBE_CLIENT)
523 C2S(NL80211_CMD_REGISTER_BEACONS)
524 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
525 C2S(NL80211_CMD_SET_NOACK_MAP)
526 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
527 C2S(NL80211_CMD_START_P2P_DEVICE)
528 C2S(NL80211_CMD_STOP_P2P_DEVICE)
529 C2S(NL80211_CMD_CONN_FAILED)
530 C2S(NL80211_CMD_SET_MCAST_RATE)
531 C2S(NL80211_CMD_SET_MAC_ACL)
532 C2S(NL80211_CMD_RADAR_DETECT)
533 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
534 C2S(NL80211_CMD_UPDATE_FT_IES)
535 C2S(NL80211_CMD_FT_EVENT)
536 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
537 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800538 C2S(NL80211_CMD_GET_COALESCE)
539 C2S(NL80211_CMD_SET_COALESCE)
540 C2S(NL80211_CMD_CHANNEL_SWITCH)
541 C2S(NL80211_CMD_VENDOR)
542 C2S(NL80211_CMD_SET_QOS_MAP)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700543 default:
544 return "NL80211_CMD_UNKNOWN";
545 }
546#undef C2S
547}
548
549
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800550/* Converts nl80211_chan_width to a common format */
551static enum chan_width convert2width(int width)
552{
553 switch (width) {
554 case NL80211_CHAN_WIDTH_20_NOHT:
555 return CHAN_WIDTH_20_NOHT;
556 case NL80211_CHAN_WIDTH_20:
557 return CHAN_WIDTH_20;
558 case NL80211_CHAN_WIDTH_40:
559 return CHAN_WIDTH_40;
560 case NL80211_CHAN_WIDTH_80:
561 return CHAN_WIDTH_80;
562 case NL80211_CHAN_WIDTH_80P80:
563 return CHAN_WIDTH_80P80;
564 case NL80211_CHAN_WIDTH_160:
565 return CHAN_WIDTH_160;
566 }
567 return CHAN_WIDTH_UNKNOWN;
568}
569
570
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571static int is_ap_interface(enum nl80211_iftype nlmode)
572{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700573 return nlmode == NL80211_IFTYPE_AP ||
574 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800575}
576
577
578static int is_sta_interface(enum nl80211_iftype nlmode)
579{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700580 return nlmode == NL80211_IFTYPE_STATION ||
581 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582}
583
584
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700585static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800586{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700587 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
588 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800589}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590
591
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700592static struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
593 int ifindex)
594{
595 struct i802_bss *bss;
596
597 for (bss = drv->first_bss; bss; bss = bss->next) {
598 if (bss->ifindex == ifindex)
599 return bss;
600 }
601
602 return NULL;
603}
604
605
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700606static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
607{
608 if (drv->associated)
609 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
610 drv->associated = 0;
611 os_memset(drv->bssid, 0, ETH_ALEN);
612}
613
614
Jouni Malinen87fd2792011-05-16 18:35:42 +0300615struct nl80211_bss_info_arg {
616 struct wpa_driver_nl80211_data *drv;
617 struct wpa_scan_results *res;
618 unsigned int assoc_freq;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700619 unsigned int ibss_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300621};
622
623static int bss_info_handler(struct nl_msg *msg, void *arg);
624
625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626/* nl80211 code */
627static int ack_handler(struct nl_msg *msg, void *arg)
628{
629 int *err = arg;
630 *err = 0;
631 return NL_STOP;
632}
633
634static int finish_handler(struct nl_msg *msg, void *arg)
635{
636 int *ret = arg;
637 *ret = 0;
638 return NL_SKIP;
639}
640
641static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
642 void *arg)
643{
644 int *ret = arg;
645 *ret = err->error;
646 return NL_SKIP;
647}
648
649
650static int no_seq_check(struct nl_msg *msg, void *arg)
651{
652 return NL_OK;
653}
654
655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800656static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 struct nl_handle *nl_handle, struct nl_msg *msg,
658 int (*valid_handler)(struct nl_msg *, void *),
659 void *valid_data)
660{
661 struct nl_cb *cb;
662 int err = -ENOMEM;
663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800664 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 if (!cb)
666 goto out;
667
668 err = nl_send_auto_complete(nl_handle, msg);
669 if (err < 0)
670 goto out;
671
672 err = 1;
673
674 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
675 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
676 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
677
678 if (valid_handler)
679 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
680 valid_handler, valid_data);
681
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700682 while (err > 0) {
683 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700684 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700685 wpa_printf(MSG_INFO,
686 "nl80211: %s->nl_recvmsgs failed: %d",
687 __func__, res);
688 }
689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 out:
691 nl_cb_put(cb);
692 nlmsg_free(msg);
693 return err;
694}
695
696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800697static int send_and_recv_msgs_global(struct nl80211_global *global,
698 struct nl_msg *msg,
699 int (*valid_handler)(struct nl_msg *, void *),
700 void *valid_data)
701{
702 return send_and_recv(global, global->nl, msg, valid_handler,
703 valid_data);
704}
705
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706
Dmitry Shmidt641185e2013-11-06 15:17:13 -0800707static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 struct nl_msg *msg,
709 int (*valid_handler)(struct nl_msg *, void *),
710 void *valid_data)
711{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800712 return send_and_recv(drv->global, drv->global->nl, msg,
713 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714}
715
716
717struct family_data {
718 const char *group;
719 int id;
720};
721
722
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700723static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
724{
725 if (bss->wdev_id_set)
726 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
727 else
728 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
729 return 0;
730
731nla_put_failure:
732 return -1;
733}
734
735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736static int family_handler(struct nl_msg *msg, void *arg)
737{
738 struct family_data *res = arg;
739 struct nlattr *tb[CTRL_ATTR_MAX + 1];
740 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
741 struct nlattr *mcgrp;
742 int i;
743
744 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
745 genlmsg_attrlen(gnlh, 0), NULL);
746 if (!tb[CTRL_ATTR_MCAST_GROUPS])
747 return NL_SKIP;
748
749 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
750 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
751 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
752 nla_len(mcgrp), NULL);
753 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
754 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
755 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
756 res->group,
757 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
758 continue;
759 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
760 break;
761 };
762
763 return NL_SKIP;
764}
765
766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 const char *family, const char *group)
769{
770 struct nl_msg *msg;
771 int ret = -1;
772 struct family_data res = { group, -ENOENT };
773
774 msg = nlmsg_alloc();
775 if (!msg)
776 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 0, 0, CTRL_CMD_GETFAMILY, 0);
779 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 msg = NULL;
783 if (ret == 0)
784 ret = res.id;
785
786nla_put_failure:
787 nlmsg_free(msg);
788 return ret;
789}
790
791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
793 struct nl_msg *msg, int flags, uint8_t cmd)
794{
795 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
796 0, flags, cmd, 0);
797}
798
799
800struct wiphy_idx_data {
801 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700802 enum nl80211_iftype nlmode;
803 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800804};
805
806
807static int netdev_info_handler(struct nl_msg *msg, void *arg)
808{
809 struct nlattr *tb[NL80211_ATTR_MAX + 1];
810 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
811 struct wiphy_idx_data *info = arg;
812
813 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
814 genlmsg_attrlen(gnlh, 0), NULL);
815
816 if (tb[NL80211_ATTR_WIPHY])
817 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
818
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700819 if (tb[NL80211_ATTR_IFTYPE])
820 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
821
822 if (tb[NL80211_ATTR_MAC] && info->macaddr)
823 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
824 ETH_ALEN);
825
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800826 return NL_SKIP;
827}
828
829
830static int nl80211_get_wiphy_index(struct i802_bss *bss)
831{
832 struct nl_msg *msg;
833 struct wiphy_idx_data data = {
834 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700835 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 };
837
838 msg = nlmsg_alloc();
839 if (!msg)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700840 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800841
842 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
843
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700844 if (nl80211_set_iface_id(msg, bss) < 0)
845 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846
847 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
848 return data.wiphy_idx;
849 msg = NULL;
850nla_put_failure:
851 nlmsg_free(msg);
852 return -1;
853}
854
855
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700856static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
857{
858 struct nl_msg *msg;
859 struct wiphy_idx_data data = {
860 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
861 .macaddr = NULL,
862 };
863
864 msg = nlmsg_alloc();
865 if (!msg)
866 return -1;
867
868 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
869
870 if (nl80211_set_iface_id(msg, bss) < 0)
871 goto nla_put_failure;
872
873 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
874 return data.nlmode;
875 msg = NULL;
876nla_put_failure:
877 nlmsg_free(msg);
878 return NL80211_IFTYPE_UNSPECIFIED;
879}
880
881
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700882static int nl80211_get_macaddr(struct i802_bss *bss)
883{
884 struct nl_msg *msg;
885 struct wiphy_idx_data data = {
886 .macaddr = bss->addr,
887 };
888
889 msg = nlmsg_alloc();
890 if (!msg)
891 return NL80211_IFTYPE_UNSPECIFIED;
892
893 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
894 if (nl80211_set_iface_id(msg, bss) < 0)
895 goto nla_put_failure;
896
897 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
898
899nla_put_failure:
900 nlmsg_free(msg);
901 return NL80211_IFTYPE_UNSPECIFIED;
902}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700903
904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
906 struct nl80211_wiphy_data *w)
907{
908 struct nl_msg *msg;
909 int ret = -1;
910
911 msg = nlmsg_alloc();
912 if (!msg)
913 return -1;
914
915 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
916
917 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
918
919 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
920 msg = NULL;
921 if (ret) {
922 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
923 "failed: ret=%d (%s)",
924 ret, strerror(-ret));
925 goto nla_put_failure;
926 }
927 ret = 0;
928nla_put_failure:
929 nlmsg_free(msg);
930 return ret;
931}
932
933
934static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
935{
936 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700937 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800938
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800939 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700941 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700942 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700943 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
944 __func__, res);
945 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800946}
947
948
949static int process_beacon_event(struct nl_msg *msg, void *arg)
950{
951 struct nl80211_wiphy_data *w = arg;
952 struct wpa_driver_nl80211_data *drv;
953 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
954 struct nlattr *tb[NL80211_ATTR_MAX + 1];
955 union wpa_event_data event;
956
957 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
958 genlmsg_attrlen(gnlh, 0), NULL);
959
960 if (gnlh->cmd != NL80211_CMD_FRAME) {
961 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
962 gnlh->cmd);
963 return NL_SKIP;
964 }
965
966 if (!tb[NL80211_ATTR_FRAME])
967 return NL_SKIP;
968
969 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
970 wiphy_list) {
971 os_memset(&event, 0, sizeof(event));
972 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
973 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
974 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
975 }
976
977 return NL_SKIP;
978}
979
980
981static struct nl80211_wiphy_data *
982nl80211_get_wiphy_data_ap(struct i802_bss *bss)
983{
984 static DEFINE_DL_LIST(nl80211_wiphys);
985 struct nl80211_wiphy_data *w;
986 int wiphy_idx, found = 0;
987 struct i802_bss *tmp_bss;
988
989 if (bss->wiphy_data != NULL)
990 return bss->wiphy_data;
991
992 wiphy_idx = nl80211_get_wiphy_index(bss);
993
994 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
995 if (w->wiphy_idx == wiphy_idx)
996 goto add;
997 }
998
999 /* alloc new one */
1000 w = os_zalloc(sizeof(*w));
1001 if (w == NULL)
1002 return NULL;
1003 w->wiphy_idx = wiphy_idx;
1004 dl_list_init(&w->bsss);
1005 dl_list_init(&w->drvs);
1006
1007 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1008 if (!w->nl_cb) {
1009 os_free(w);
1010 return NULL;
1011 }
1012 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
1013 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
1014 w);
1015
1016 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
1017 "wiphy beacons");
1018 if (w->nl_beacons == NULL) {
1019 os_free(w);
1020 return NULL;
1021 }
1022
1023 if (nl80211_register_beacons(bss->drv, w)) {
1024 nl_destroy_handles(&w->nl_beacons);
1025 os_free(w);
1026 return NULL;
1027 }
1028
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001029 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001030
1031 dl_list_add(&nl80211_wiphys, &w->list);
1032
1033add:
1034 /* drv entry for this bss already there? */
1035 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1036 if (tmp_bss->drv == bss->drv) {
1037 found = 1;
1038 break;
1039 }
1040 }
1041 /* if not add it */
1042 if (!found)
1043 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
1044
1045 dl_list_add(&w->bsss, &bss->wiphy_list);
1046 bss->wiphy_data = w;
1047 return w;
1048}
1049
1050
1051static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
1052{
1053 struct nl80211_wiphy_data *w = bss->wiphy_data;
1054 struct i802_bss *tmp_bss;
1055 int found = 0;
1056
1057 if (w == NULL)
1058 return;
1059 bss->wiphy_data = NULL;
1060 dl_list_del(&bss->wiphy_list);
1061
1062 /* still any for this drv present? */
1063 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
1064 if (tmp_bss->drv == bss->drv) {
1065 found = 1;
1066 break;
1067 }
1068 }
1069 /* if not remove it */
1070 if (!found)
1071 dl_list_del(&bss->drv->wiphy_list);
1072
1073 if (!dl_list_empty(&w->bsss))
1074 return;
1075
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001076 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001077
1078 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001079 dl_list_del(&w->list);
1080 os_free(w);
1081}
1082
1083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
1085{
1086 struct i802_bss *bss = priv;
1087 struct wpa_driver_nl80211_data *drv = bss->drv;
1088 if (!drv->associated)
1089 return -1;
1090 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1091 return 0;
1092}
1093
1094
1095static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
1096{
1097 struct i802_bss *bss = priv;
1098 struct wpa_driver_nl80211_data *drv = bss->drv;
1099 if (!drv->associated)
1100 return -1;
1101 os_memcpy(ssid, drv->ssid, drv->ssid_len);
1102 return drv->ssid_len;
1103}
1104
1105
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001106static void wpa_driver_nl80211_event_newlink(
1107 struct wpa_driver_nl80211_data *drv, char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108{
1109 union wpa_event_data event;
1110
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001111 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1112 if (if_nametoindex(drv->first_bss->ifname) == 0) {
1113 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
1114 drv->first_bss->ifname);
1115 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001116 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001117 if (!drv->if_removed)
1118 return;
1119 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
1120 drv->first_bss->ifname);
1121 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 }
1123
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001124 os_memset(&event, 0, sizeof(event));
1125 os_strlcpy(event.interface_status.ifname, ifname,
1126 sizeof(event.interface_status.ifname));
1127 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
1128 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1129}
1130
1131
1132static void wpa_driver_nl80211_event_dellink(
1133 struct wpa_driver_nl80211_data *drv, char *ifname)
1134{
1135 union wpa_event_data event;
1136
1137 if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
1138 if (drv->if_removed) {
1139 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
1140 ifname);
1141 return;
1142 }
1143 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
1144 ifname);
1145 drv->if_removed = 1;
1146 } else {
1147 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
1148 ifname);
1149 }
1150
1151 os_memset(&event, 0, sizeof(event));
1152 os_strlcpy(event.interface_status.ifname, ifname,
1153 sizeof(event.interface_status.ifname));
1154 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
1156}
1157
1158
1159static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
1160 u8 *buf, size_t len)
1161{
1162 int attrlen, rta_len;
1163 struct rtattr *attr;
1164
1165 attrlen = len;
1166 attr = (struct rtattr *) buf;
1167
1168 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1169 while (RTA_OK(attr, attrlen)) {
1170 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001171 if (os_strcmp(((char *) attr) + rta_len,
1172 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 return 1;
1174 else
1175 break;
1176 }
1177 attr = RTA_NEXT(attr, attrlen);
1178 }
1179
1180 return 0;
1181}
1182
1183
1184static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1185 int ifindex, u8 *buf, size_t len)
1186{
1187 if (drv->ifindex == ifindex)
1188 return 1;
1189
1190 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1192 "interface");
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001193 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 return 1;
1195 }
1196
1197 return 0;
1198}
1199
1200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001201static struct wpa_driver_nl80211_data *
1202nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1203{
1204 struct wpa_driver_nl80211_data *drv;
1205 dl_list_for_each(drv, &global->interfaces,
1206 struct wpa_driver_nl80211_data, list) {
1207 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1208 have_ifidx(drv, idx))
1209 return drv;
1210 }
1211 return NULL;
1212}
1213
1214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1216 struct ifinfomsg *ifi,
1217 u8 *buf, size_t len)
1218{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219 struct nl80211_global *global = ctx;
1220 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001221 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 struct rtattr *attr;
1223 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001224 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001225 char ifname[IFNAMSIZ + 1];
1226 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1229 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001230 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
1231 ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 return;
1233 }
1234
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001235 extra[0] = '\0';
1236 pos = extra;
1237 end = pos + sizeof(extra);
1238 ifname[0] = '\0';
1239
1240 attrlen = len;
1241 attr = (struct rtattr *) buf;
1242 while (RTA_OK(attr, attrlen)) {
1243 switch (attr->rta_type) {
1244 case IFLA_IFNAME:
1245 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1246 break;
1247 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1248 ifname[RTA_PAYLOAD(attr)] = '\0';
1249 break;
1250 case IFLA_MASTER:
1251 brid = nla_get_u32((struct nlattr *) attr);
1252 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1253 break;
1254 case IFLA_WIRELESS:
1255 pos += os_snprintf(pos, end - pos, " wext");
1256 break;
1257 case IFLA_OPERSTATE:
1258 pos += os_snprintf(pos, end - pos, " operstate=%u",
1259 nla_get_u32((struct nlattr *) attr));
1260 break;
1261 case IFLA_LINKMODE:
1262 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1263 nla_get_u32((struct nlattr *) attr));
1264 break;
1265 }
1266 attr = RTA_NEXT(attr, attrlen);
1267 }
1268 extra[sizeof(extra) - 1] = '\0';
1269
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001270 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1271 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1272 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1274 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1275 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1276 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1277
1278 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279 if (if_indextoname(ifi->ifi_index, namebuf) &&
1280 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001281 drv->first_bss->ifname) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001282 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1283 "event since interface %s is up", namebuf);
1284 return;
1285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001287 if (drv->ignore_if_down_event) {
1288 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1289 "event generated by mode change");
1290 drv->ignore_if_down_event = 0;
1291 } else {
1292 drv->if_disabled = 1;
1293 wpa_supplicant_event(drv->ctx,
1294 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001295
1296 /*
1297 * Try to get drv again, since it may be removed as
1298 * part of the EVENT_INTERFACE_DISABLED handling for
1299 * dynamic interfaces
1300 */
1301 drv = nl80211_find_drv(global, ifi->ifi_index,
1302 buf, len);
1303 if (!drv)
1304 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 }
1307
1308 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001309 if (if_indextoname(ifi->ifi_index, namebuf) &&
1310 linux_iface_up(drv->global->ioctl_sock,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001311 drv->first_bss->ifname) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001312 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1313 "event since interface %s is down",
1314 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001315 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001316 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1317 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001318 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001319 } else if (drv->if_removed) {
1320 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1321 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001322 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001323 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001324 struct i802_bss *bss;
1325 u8 addr[ETH_ALEN];
1326
1327 /* Re-read MAC address as it may have changed */
1328 bss = get_bss_ifindex(drv, ifi->ifi_index);
1329 if (bss &&
1330 linux_get_ifhwaddr(drv->global->ioctl_sock,
1331 bss->ifname, addr) < 0) {
1332 wpa_printf(MSG_DEBUG,
1333 "nl80211: %s: failed to re-read MAC address",
1334 bss->ifname);
1335 } else if (bss &&
1336 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1337 wpa_printf(MSG_DEBUG,
1338 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1339 MACSTR " to " MACSTR,
1340 ifi->ifi_index, bss->ifname,
1341 MAC2STR(bss->addr),
1342 MAC2STR(addr));
1343 os_memcpy(bss->addr, addr, ETH_ALEN);
1344 }
1345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1347 drv->if_disabled = 0;
1348 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1349 NULL);
1350 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 }
1352
1353 /*
1354 * Some drivers send the association event before the operup event--in
1355 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1356 * fails. This will hit us when wpa_supplicant does not need to do
1357 * IEEE 802.1X authentication
1358 */
1359 if (drv->operstate == 1 &&
1360 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001361 !(ifi->ifi_flags & IFF_RUNNING)) {
1362 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001363 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 }
1366
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001367 if (ifname[0])
1368 wpa_driver_nl80211_event_newlink(drv, ifname);
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 if (ifi->ifi_family == AF_BRIDGE && brid) {
1371 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372 if_indextoname(brid, namebuf);
1373 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1374 brid, namebuf);
1375 add_ifidx(drv, brid);
1376 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377}
1378
1379
1380static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1381 struct ifinfomsg *ifi,
1382 u8 *buf, size_t len)
1383{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001384 struct nl80211_global *global = ctx;
1385 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001386 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 struct rtattr *attr;
1388 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001389 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001390 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001392 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1393 if (!drv) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001394 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1395 ifi->ifi_index);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001396 return;
1397 }
1398
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001399 extra[0] = '\0';
1400 pos = extra;
1401 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001402 ifname[0] = '\0';
1403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 attrlen = len;
1405 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001407 switch (attr->rta_type) {
1408 case IFLA_IFNAME:
1409 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1410 break;
1411 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1412 ifname[RTA_PAYLOAD(attr)] = '\0';
1413 break;
1414 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001416 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1417 break;
1418 case IFLA_OPERSTATE:
1419 pos += os_snprintf(pos, end - pos, " operstate=%u",
1420 nla_get_u32((struct nlattr *) attr));
1421 break;
1422 case IFLA_LINKMODE:
1423 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1424 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001425 break;
1426 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 attr = RTA_NEXT(attr, attrlen);
1428 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001429 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001431 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1432 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1433 ifi->ifi_flags,
1434 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1435 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1436 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1437 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1438
1439 if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001440 wpa_driver_nl80211_event_dellink(drv, ifname);
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 if (ifi->ifi_family == AF_BRIDGE && brid) {
1443 /* device has been removed from bridge */
1444 char namebuf[IFNAMSIZ];
1445 if_indextoname(brid, namebuf);
1446 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1447 "%s", brid, namebuf);
1448 del_ifidx(drv, brid);
1449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450}
1451
1452
1453static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1454 const u8 *frame, size_t len)
1455{
1456 const struct ieee80211_mgmt *mgmt;
1457 union wpa_event_data event;
1458
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001459 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1460 drv->force_connect_cmd) {
1461 /*
1462 * Avoid reporting two association events that would confuse
1463 * the core code.
1464 */
1465 wpa_printf(MSG_DEBUG,
1466 "nl80211: Ignore auth event when using driver SME");
1467 return;
1468 }
1469
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001470 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 mgmt = (const struct ieee80211_mgmt *) frame;
1472 if (len < 24 + sizeof(mgmt->u.auth)) {
1473 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1474 "frame");
1475 return;
1476 }
1477
1478 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001479 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 os_memset(&event, 0, sizeof(event));
1481 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1482 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001483 event.auth.auth_transaction =
1484 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001485 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1486 if (len > 24 + sizeof(mgmt->u.auth)) {
1487 event.auth.ies = mgmt->u.auth.variable;
1488 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1489 }
1490
1491 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1492}
1493
1494
Jouni Malinen87fd2792011-05-16 18:35:42 +03001495static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1496{
1497 struct nl_msg *msg;
1498 int ret;
1499 struct nl80211_bss_info_arg arg;
1500
1501 os_memset(&arg, 0, sizeof(arg));
1502 msg = nlmsg_alloc();
1503 if (!msg)
1504 goto nla_put_failure;
1505
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001506 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001507 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1508
1509 arg.drv = drv;
1510 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1511 msg = NULL;
1512 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001513 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1514 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001515 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001516 "associated BSS from scan results: %u MHz", freq);
1517 if (freq)
1518 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001519 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001520 }
1521 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1522 "(%s)", ret, strerror(-ret));
1523nla_put_failure:
1524 nlmsg_free(msg);
1525 return drv->assoc_freq;
1526}
1527
1528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1530 const u8 *frame, size_t len)
1531{
1532 const struct ieee80211_mgmt *mgmt;
1533 union wpa_event_data event;
1534 u16 status;
1535
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001536 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1537 drv->force_connect_cmd) {
1538 /*
1539 * Avoid reporting two association events that would confuse
1540 * the core code.
1541 */
1542 wpa_printf(MSG_DEBUG,
1543 "nl80211: Ignore assoc event when using driver SME");
1544 return;
1545 }
1546
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001547 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 mgmt = (const struct ieee80211_mgmt *) frame;
1549 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1550 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1551 "frame");
1552 return;
1553 }
1554
1555 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1556 if (status != WLAN_STATUS_SUCCESS) {
1557 os_memset(&event, 0, sizeof(event));
1558 event.assoc_reject.bssid = mgmt->bssid;
1559 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1560 event.assoc_reject.resp_ies =
1561 (u8 *) mgmt->u.assoc_resp.variable;
1562 event.assoc_reject.resp_ies_len =
1563 len - 24 - sizeof(mgmt->u.assoc_resp);
1564 }
1565 event.assoc_reject.status_code = status;
1566
1567 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1568 return;
1569 }
1570
1571 drv->associated = 1;
1572 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001573 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574
1575 os_memset(&event, 0, sizeof(event));
1576 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1577 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1578 event.assoc_info.resp_ies_len =
1579 len - 24 - sizeof(mgmt->u.assoc_resp);
1580 }
1581
1582 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1585}
1586
1587
1588static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1589 enum nl80211_commands cmd, struct nlattr *status,
1590 struct nlattr *addr, struct nlattr *req_ie,
1591 struct nlattr *resp_ie)
1592{
1593 union wpa_event_data event;
Jithu Jancea7c60b42014-12-03 18:54:40 +05301594 u16 status_code;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595
1596 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1597 /*
1598 * Avoid reporting two association events that would confuse
1599 * the core code.
1600 */
1601 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1602 "when using userspace SME", cmd);
1603 return;
1604 }
1605
Jithu Jancea7c60b42014-12-03 18:54:40 +05301606 status_code = status ? nla_get_u16(status) : WLAN_STATUS_SUCCESS;
1607
1608 if (cmd == NL80211_CMD_CONNECT) {
1609 wpa_printf(MSG_DEBUG,
1610 "nl80211: Connect event (status=%u ignore_next_local_disconnect=%d)",
1611 status_code, drv->ignore_next_local_disconnect);
1612 } else if (cmd == NL80211_CMD_ROAM) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001613 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
Jithu Jancea7c60b42014-12-03 18:54:40 +05301614 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616 os_memset(&event, 0, sizeof(event));
Jithu Jancea7c60b42014-12-03 18:54:40 +05301617 if (cmd == NL80211_CMD_CONNECT && status_code != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 if (addr)
1619 event.assoc_reject.bssid = nla_data(addr);
Jithu Jancea7c60b42014-12-03 18:54:40 +05301620 if (drv->ignore_next_local_disconnect) {
1621 drv->ignore_next_local_disconnect = 0;
1622 if (!event.assoc_reject.bssid ||
1623 (os_memcmp(event.assoc_reject.bssid,
1624 drv->auth_attempt_bssid,
1625 ETH_ALEN) != 0)) {
1626 /*
1627 * Ignore the event that came without a BSSID or
1628 * for the old connection since this is likely
1629 * not relevant to the new Connect command.
1630 */
1631 wpa_printf(MSG_DEBUG,
1632 "nl80211: Ignore connection failure event triggered during reassociation");
1633 return;
1634 }
1635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 if (resp_ie) {
1637 event.assoc_reject.resp_ies = nla_data(resp_ie);
1638 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1639 }
Jithu Jancea7c60b42014-12-03 18:54:40 +05301640 event.assoc_reject.status_code = status_code;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1642 return;
1643 }
1644
1645 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001646 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001648 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650
1651 if (req_ie) {
1652 event.assoc_info.req_ies = nla_data(req_ie);
1653 event.assoc_info.req_ies_len = nla_len(req_ie);
1654 }
1655 if (resp_ie) {
1656 event.assoc_info.resp_ies = nla_data(resp_ie);
1657 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1658 }
1659
Jouni Malinen87fd2792011-05-16 18:35:42 +03001660 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1663}
1664
1665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001666static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001667 struct nlattr *reason, struct nlattr *addr,
1668 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001669{
1670 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001671 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001672
1673 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1674 /*
1675 * Avoid reporting two disassociation events that could
1676 * confuse the core code.
1677 */
1678 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1679 "event when using userspace SME");
1680 return;
1681 }
1682
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001683 if (drv->ignore_next_local_disconnect) {
1684 drv->ignore_next_local_disconnect = 0;
1685 if (locally_generated) {
1686 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1687 "event triggered during reassociation");
1688 return;
1689 }
1690 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1691 "disconnect but got another disconnect "
1692 "event first");
1693 }
1694
1695 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001696 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 os_memset(&data, 0, sizeof(data));
1698 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001699 data.deauth_info.reason_code = nla_get_u16(reason);
1700 data.deauth_info.locally_generated = by_ap == NULL;
1701 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1702}
1703
1704
Dmitry Shmidt97672262014-02-03 13:02:54 -08001705static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1706{
1707 int freq1 = 0;
1708
1709 switch (convert2width(width)) {
1710 case CHAN_WIDTH_20_NOHT:
1711 case CHAN_WIDTH_20:
1712 return 0;
1713 case CHAN_WIDTH_40:
1714 freq1 = cf1 - 10;
1715 break;
1716 case CHAN_WIDTH_80:
1717 freq1 = cf1 - 30;
1718 break;
1719 case CHAN_WIDTH_160:
1720 freq1 = cf1 - 70;
1721 break;
1722 case CHAN_WIDTH_UNKNOWN:
1723 case CHAN_WIDTH_80P80:
1724 /* FIXME: implement this */
1725 return 0;
1726 }
1727
1728 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1729}
1730
1731
Dmitry Shmidt04949592012-07-19 12:16:46 -07001732static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001733 struct nlattr *ifindex, struct nlattr *freq,
1734 struct nlattr *type, struct nlattr *bw,
1735 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001736{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001737 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001738 union wpa_event_data data;
1739 int ht_enabled = 1;
1740 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001741 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001742
1743 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1744
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001745 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001746 return;
1747
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001748 ifidx = nla_get_u32(ifindex);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001749 bss = get_bss_ifindex(drv, ifidx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001750 if (bss == NULL) {
1751 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1752 ifidx);
1753 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001754 }
1755
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001756 if (type) {
1757 switch (nla_get_u32(type)) {
1758 case NL80211_CHAN_NO_HT:
1759 ht_enabled = 0;
1760 break;
1761 case NL80211_CHAN_HT20:
1762 break;
1763 case NL80211_CHAN_HT40PLUS:
1764 chan_offset = 1;
1765 break;
1766 case NL80211_CHAN_HT40MINUS:
1767 chan_offset = -1;
1768 break;
1769 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001770 } else if (bw && cf1) {
1771 /* This can happen for example with VHT80 ch switch */
1772 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1773 nla_get_u32(freq),
1774 nla_get_u32(cf1),
1775 cf2 ? nla_get_u32(cf2) : 0);
1776 } else {
1777 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001778 }
1779
1780 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001781 data.ch_switch.freq = nla_get_u32(freq);
1782 data.ch_switch.ht_enabled = ht_enabled;
1783 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001784 if (bw)
1785 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1786 if (cf1)
1787 data.ch_switch.cf1 = nla_get_u32(cf1);
1788 if (cf2)
1789 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001790
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001791 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001792
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001793 wpa_supplicant_event(bss->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001794}
1795
1796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1798 enum nl80211_commands cmd, struct nlattr *addr)
1799{
1800 union wpa_event_data event;
1801 enum wpa_event_type ev;
1802
1803 if (nla_len(addr) != ETH_ALEN)
1804 return;
1805
1806 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1807 cmd, MAC2STR((u8 *) nla_data(addr)));
1808
1809 if (cmd == NL80211_CMD_AUTHENTICATE)
1810 ev = EVENT_AUTH_TIMED_OUT;
1811 else if (cmd == NL80211_CMD_ASSOCIATE)
1812 ev = EVENT_ASSOC_TIMED_OUT;
1813 else
1814 return;
1815
1816 os_memset(&event, 0, sizeof(event));
1817 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1818 wpa_supplicant_event(drv->ctx, ev, &event);
1819}
1820
1821
Dmitry Shmidt98660862014-03-11 17:26:21 -07001822static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001823 struct nlattr *freq, struct nlattr *sig,
1824 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001826 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 const struct ieee80211_mgmt *mgmt;
1828 union wpa_event_data event;
1829 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001830 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001831 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001833 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001834 mgmt = (const struct ieee80211_mgmt *) frame;
1835 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001836 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 return;
1838 }
1839
1840 fc = le_to_host16(mgmt->frame_control);
1841 stype = WLAN_FC_GET_STYPE(fc);
1842
Dmitry Shmidt04949592012-07-19 12:16:46 -07001843 if (sig)
1844 ssi_signal = (s32) nla_get_u32(sig);
1845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 os_memset(&event, 0, sizeof(event));
1847 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001848 event.rx_mgmt.freq = nla_get_u32(freq);
1849 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001851 wpa_printf(MSG_DEBUG,
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001852 "nl80211: RX frame sa=" MACSTR
1853 " freq=%d ssi_signal=%d stype=%u (%s) len=%u",
1854 MAC2STR(mgmt->sa), rx_freq, ssi_signal, stype, fc2str(fc),
1855 (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001856 event.rx_mgmt.frame = frame;
1857 event.rx_mgmt.frame_len = len;
1858 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001859 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001860 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861}
1862
1863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001864static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1865 struct nlattr *cookie, const u8 *frame,
1866 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867{
1868 union wpa_event_data event;
1869 const struct ieee80211_hdr *hdr;
1870 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001872 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873 if (!is_ap_interface(drv->nlmode)) {
1874 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876 if (!cookie)
1877 return;
1878
1879 cookie_val = nla_get_u64(cookie);
1880 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1881 " cookie=0%llx%s (ack=%d)",
1882 (long long unsigned int) cookie_val,
1883 cookie_val == drv->send_action_cookie ?
1884 " (match)" : " (unknown)", ack != NULL);
1885 if (cookie_val != drv->send_action_cookie)
1886 return;
1887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888
1889 hdr = (const struct ieee80211_hdr *) frame;
1890 fc = le_to_host16(hdr->frame_control);
1891
1892 os_memset(&event, 0, sizeof(event));
1893 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1894 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1895 event.tx_status.dst = hdr->addr1;
1896 event.tx_status.data = frame;
1897 event.tx_status.data_len = len;
1898 event.tx_status.ack = ack != NULL;
1899 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1900}
1901
1902
1903static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1904 enum wpa_event_type type,
1905 const u8 *frame, size_t len)
1906{
1907 const struct ieee80211_mgmt *mgmt;
1908 union wpa_event_data event;
1909 const u8 *bssid = NULL;
1910 u16 reason_code = 0;
1911
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001912 if (type == EVENT_DEAUTH)
1913 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1914 else
1915 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 mgmt = (const struct ieee80211_mgmt *) frame;
1918 if (len >= 24) {
1919 bssid = mgmt->bssid;
1920
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001921 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1922 !drv->associated &&
1923 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1924 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1925 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1926 /*
1927 * Avoid issues with some roaming cases where
1928 * disconnection event for the old AP may show up after
1929 * we have started connection with the new AP.
1930 */
1931 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1932 MAC2STR(bssid),
1933 MAC2STR(drv->auth_attempt_bssid));
1934 return;
1935 }
1936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 if (drv->associated != 0 &&
1938 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1939 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1940 /*
1941 * We have presumably received this deauth as a
1942 * response to a clear_state_mismatch() outgoing
1943 * deauth. Don't let it take us offline!
1944 */
1945 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1946 "from Unknown BSSID " MACSTR " -- ignoring",
1947 MAC2STR(bssid));
1948 return;
1949 }
1950 }
1951
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001952 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 os_memset(&event, 0, sizeof(event));
1954
1955 /* Note: Same offset for Reason Code in both frame subtypes */
1956 if (len >= 24 + sizeof(mgmt->u.deauth))
1957 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1958
1959 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001960 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001961 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 event.disassoc_info.addr = bssid;
1963 event.disassoc_info.reason_code = reason_code;
1964 if (frame + len > mgmt->u.disassoc.variable) {
1965 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1966 event.disassoc_info.ie_len = frame + len -
1967 mgmt->u.disassoc.variable;
1968 }
1969 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001970 if (drv->ignore_deauth_event) {
1971 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1972 drv->ignore_deauth_event = 0;
1973 return;
1974 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001975 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001976 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001977 if (drv->ignore_next_local_deauth) {
1978 drv->ignore_next_local_deauth = 0;
1979 if (event.deauth_info.locally_generated) {
1980 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1981 return;
1982 }
1983 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 event.deauth_info.addr = bssid;
1986 event.deauth_info.reason_code = reason_code;
1987 if (frame + len > mgmt->u.deauth.variable) {
1988 event.deauth_info.ie = mgmt->u.deauth.variable;
1989 event.deauth_info.ie_len = frame + len -
1990 mgmt->u.deauth.variable;
1991 }
1992 }
1993
1994 wpa_supplicant_event(drv->ctx, type, &event);
1995}
1996
1997
1998static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1999 enum wpa_event_type type,
2000 const u8 *frame, size_t len)
2001{
2002 const struct ieee80211_mgmt *mgmt;
2003 union wpa_event_data event;
2004 u16 reason_code = 0;
2005
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002006 if (type == EVENT_UNPROT_DEAUTH)
2007 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
2008 else
2009 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
2010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 if (len < 24)
2012 return;
2013
2014 mgmt = (const struct ieee80211_mgmt *) frame;
2015
2016 os_memset(&event, 0, sizeof(event));
2017 /* Note: Same offset for Reason Code in both frame subtypes */
2018 if (len >= 24 + sizeof(mgmt->u.deauth))
2019 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
2020
2021 if (type == EVENT_UNPROT_DISASSOC) {
2022 event.unprot_disassoc.sa = mgmt->sa;
2023 event.unprot_disassoc.da = mgmt->da;
2024 event.unprot_disassoc.reason_code = reason_code;
2025 } else {
2026 event.unprot_deauth.sa = mgmt->sa;
2027 event.unprot_deauth.da = mgmt->da;
2028 event.unprot_deauth.reason_code = reason_code;
2029 }
2030
2031 wpa_supplicant_event(drv->ctx, type, &event);
2032}
2033
2034
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002035static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 enum nl80211_commands cmd, struct nlattr *frame,
2037 struct nlattr *addr, struct nlattr *timed_out,
2038 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002039 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002041 struct wpa_driver_nl80211_data *drv = bss->drv;
2042 const u8 *data;
2043 size_t len;
2044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 if (timed_out && addr) {
2046 mlme_timeout_event(drv, cmd, addr);
2047 return;
2048 }
2049
2050 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002051 wpa_printf(MSG_DEBUG,
2052 "nl80211: MLME event %d (%s) without frame data",
2053 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 return;
2055 }
2056
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002057 data = nla_data(frame);
2058 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002059 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002060 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
2061 MACSTR ") - too short",
2062 cmd, nl80211_command_to_string(cmd), bss->ifname,
2063 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002064 return;
2065 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002066 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
2067 ") A1=" MACSTR " A2=" MACSTR, cmd,
2068 nl80211_command_to_string(cmd), bss->ifname,
2069 MAC2STR(bss->addr), MAC2STR(data + 4),
2070 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002071 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002072 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
2073 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002074 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
2075 "for foreign address", bss->ifname);
2076 return;
2077 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
2079 nla_data(frame), nla_len(frame));
2080
2081 switch (cmd) {
2082 case NL80211_CMD_AUTHENTICATE:
2083 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
2084 break;
2085 case NL80211_CMD_ASSOCIATE:
2086 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
2087 break;
2088 case NL80211_CMD_DEAUTHENTICATE:
2089 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
2090 nla_data(frame), nla_len(frame));
2091 break;
2092 case NL80211_CMD_DISASSOCIATE:
2093 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
2094 nla_data(frame), nla_len(frame));
2095 break;
2096 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07002097 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07002098 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 break;
2100 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
2102 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 break;
2104 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2105 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
2106 nla_data(frame), nla_len(frame));
2107 break;
2108 case NL80211_CMD_UNPROT_DISASSOCIATE:
2109 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
2110 nla_data(frame), nla_len(frame));
2111 break;
2112 default:
2113 break;
2114 }
2115}
2116
2117
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002118static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 struct nlattr *tb[])
2120{
2121 union wpa_event_data data;
2122
2123 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2124 os_memset(&data, 0, sizeof(data));
2125 if (tb[NL80211_ATTR_MAC]) {
2126 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2127 nla_data(tb[NL80211_ATTR_MAC]),
2128 nla_len(tb[NL80211_ATTR_MAC]));
2129 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
2130 }
2131 if (tb[NL80211_ATTR_KEY_SEQ]) {
2132 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2133 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2134 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2135 }
2136 if (tb[NL80211_ATTR_KEY_TYPE]) {
2137 enum nl80211_key_type key_type =
2138 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2139 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2140 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2141 data.michael_mic_failure.unicast = 1;
2142 } else
2143 data.michael_mic_failure.unicast = 1;
2144
2145 if (tb[NL80211_ATTR_KEY_IDX]) {
2146 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2147 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2148 }
2149
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002150 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151}
2152
2153
2154static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2155 struct nlattr *tb[])
2156{
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002157 unsigned int freq;
2158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 if (tb[NL80211_ATTR_MAC] == NULL) {
2160 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2161 "event");
2162 return;
2163 }
2164 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 drv->associated = 1;
2167 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2168 MAC2STR(drv->bssid));
2169
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002170 freq = nl80211_get_assoc_freq(drv);
2171 if (freq) {
2172 wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
2173 freq);
2174 drv->first_bss->freq = freq;
2175 }
2176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2178}
2179
2180
2181static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2182 int cancel_event, struct nlattr *tb[])
2183{
2184 unsigned int freq, chan_type, duration;
2185 union wpa_event_data data;
2186 u64 cookie;
2187
2188 if (tb[NL80211_ATTR_WIPHY_FREQ])
2189 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2190 else
2191 freq = 0;
2192
2193 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2194 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2195 else
2196 chan_type = 0;
2197
2198 if (tb[NL80211_ATTR_DURATION])
2199 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2200 else
2201 duration = 0;
2202
2203 if (tb[NL80211_ATTR_COOKIE])
2204 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2205 else
2206 cookie = 0;
2207
2208 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2209 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2210 cancel_event, freq, chan_type, duration,
2211 (long long unsigned int) cookie,
2212 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2213
2214 if (cookie != drv->remain_on_chan_cookie)
2215 return; /* not for us */
2216
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217 if (cancel_event)
2218 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219
2220 os_memset(&data, 0, sizeof(data));
2221 data.remain_on_channel.freq = freq;
2222 data.remain_on_channel.duration = duration;
2223 wpa_supplicant_event(drv->ctx, cancel_event ?
2224 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2225 EVENT_REMAIN_ON_CHANNEL, &data);
2226}
2227
2228
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002229static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2230 struct nlattr *tb[])
2231{
2232 union wpa_event_data data;
2233
2234 os_memset(&data, 0, sizeof(data));
2235
2236 if (tb[NL80211_ATTR_IE]) {
2237 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2238 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2239 }
2240
2241 if (tb[NL80211_ATTR_IE_RIC]) {
2242 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2243 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2244 }
2245
2246 if (tb[NL80211_ATTR_MAC])
2247 os_memcpy(data.ft_ies.target_ap,
2248 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2249
2250 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2251 MAC2STR(data.ft_ies.target_ap));
2252
2253 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2254}
2255
2256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2258 struct nlattr *tb[])
2259{
2260 union wpa_event_data event;
2261 struct nlattr *nl;
2262 int rem;
2263 struct scan_info *info;
2264#define MAX_REPORT_FREQS 50
2265 int freqs[MAX_REPORT_FREQS];
2266 int num_freqs = 0;
2267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002268 if (drv->scan_for_auth) {
2269 drv->scan_for_auth = 0;
2270 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2271 "cfg80211 BSS entry");
2272 wpa_driver_nl80211_authenticate_retry(drv);
2273 return;
2274 }
2275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 os_memset(&event, 0, sizeof(event));
2277 info = &event.scan_info;
2278 info->aborted = aborted;
2279
2280 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2281 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2282 struct wpa_driver_scan_ssid *s =
2283 &info->ssids[info->num_ssids];
2284 s->ssid = nla_data(nl);
2285 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002286 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2287 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 info->num_ssids++;
2289 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2290 break;
2291 }
2292 }
2293 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002294 char msg[200], *pos, *end;
2295 int res;
2296
2297 pos = msg;
2298 end = pos + sizeof(msg);
2299 *pos = '\0';
2300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2302 {
2303 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002304 res = os_snprintf(pos, end - pos, " %d",
2305 freqs[num_freqs]);
2306 if (res > 0 && end - pos > res)
2307 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308 num_freqs++;
2309 if (num_freqs == MAX_REPORT_FREQS - 1)
2310 break;
2311 }
2312 info->freqs = freqs;
2313 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002314 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2315 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 }
2317 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2318}
2319
2320
2321static int get_link_signal(struct nl_msg *msg, void *arg)
2322{
2323 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2324 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2325 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2326 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2327 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002328 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 };
2330 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2331 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2332 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2333 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2334 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2335 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2336 };
2337 struct wpa_signal_info *sig_change = arg;
2338
2339 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2340 genlmsg_attrlen(gnlh, 0), NULL);
2341 if (!tb[NL80211_ATTR_STA_INFO] ||
2342 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2343 tb[NL80211_ATTR_STA_INFO], policy))
2344 return NL_SKIP;
2345 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2346 return NL_SKIP;
2347
2348 sig_change->current_signal =
2349 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2350
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002351 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2352 sig_change->avg_signal =
2353 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2354 else
2355 sig_change->avg_signal = 0;
2356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2358 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2359 sinfo[NL80211_STA_INFO_TX_BITRATE],
2360 rate_policy)) {
2361 sig_change->current_txrate = 0;
2362 } else {
2363 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2364 sig_change->current_txrate =
2365 nla_get_u16(rinfo[
2366 NL80211_RATE_INFO_BITRATE]) * 100;
2367 }
2368 }
2369 }
2370
2371 return NL_SKIP;
2372}
2373
2374
2375static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2376 struct wpa_signal_info *sig)
2377{
2378 struct nl_msg *msg;
2379
2380 sig->current_signal = -9999;
2381 sig->current_txrate = 0;
2382
2383 msg = nlmsg_alloc();
2384 if (!msg)
2385 return -ENOMEM;
2386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388
2389 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2390 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2391
2392 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2393 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002394 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 return -ENOBUFS;
2396}
2397
2398
2399static int get_link_noise(struct nl_msg *msg, void *arg)
2400{
2401 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2402 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2403 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2404 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2405 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2406 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2407 };
2408 struct wpa_signal_info *sig_change = arg;
2409
2410 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2411 genlmsg_attrlen(gnlh, 0), NULL);
2412
2413 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2414 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2415 return NL_SKIP;
2416 }
2417
2418 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2419 tb[NL80211_ATTR_SURVEY_INFO],
2420 survey_policy)) {
2421 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2422 "attributes!");
2423 return NL_SKIP;
2424 }
2425
2426 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2427 return NL_SKIP;
2428
2429 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2430 sig_change->frequency)
2431 return NL_SKIP;
2432
2433 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2434 return NL_SKIP;
2435
2436 sig_change->current_noise =
2437 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2438
2439 return NL_SKIP;
2440}
2441
2442
2443static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2444 struct wpa_signal_info *sig_change)
2445{
2446 struct nl_msg *msg;
2447
2448 sig_change->current_noise = 9999;
2449 sig_change->frequency = drv->assoc_freq;
2450
2451 msg = nlmsg_alloc();
2452 if (!msg)
2453 return -ENOMEM;
2454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456
2457 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2458
2459 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2460 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002461 nlmsg_free(msg);
2462 return -ENOBUFS;
2463}
2464
2465
2466static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2467{
2468 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2469 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2470 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2471 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2472 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2473 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2474 };
2475 struct wpa_scan_results *scan_results = arg;
2476 struct wpa_scan_res *scan_res;
2477 size_t i;
2478
2479 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2480 genlmsg_attrlen(gnlh, 0), NULL);
2481
2482 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2483 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2484 return NL_SKIP;
2485 }
2486
2487 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2488 tb[NL80211_ATTR_SURVEY_INFO],
2489 survey_policy)) {
2490 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2491 "attributes");
2492 return NL_SKIP;
2493 }
2494
2495 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2496 return NL_SKIP;
2497
2498 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2499 return NL_SKIP;
2500
2501 for (i = 0; i < scan_results->num; ++i) {
2502 scan_res = scan_results->res[i];
2503 if (!scan_res)
2504 continue;
2505 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2506 scan_res->freq)
2507 continue;
2508 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2509 continue;
2510 scan_res->noise = (s8)
2511 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2512 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2513 }
2514
2515 return NL_SKIP;
2516}
2517
2518
2519static int nl80211_get_noise_for_scan_results(
2520 struct wpa_driver_nl80211_data *drv,
2521 struct wpa_scan_results *scan_res)
2522{
2523 struct nl_msg *msg;
2524
2525 msg = nlmsg_alloc();
2526 if (!msg)
2527 return -ENOMEM;
2528
2529 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2530
2531 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2532
2533 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2534 scan_res);
2535 nla_put_failure:
2536 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 return -ENOBUFS;
2538}
2539
2540
2541static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2542 struct nlattr *tb[])
2543{
2544 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2545 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2546 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2547 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2548 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2549 };
2550 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2551 enum nl80211_cqm_rssi_threshold_event event;
2552 union wpa_event_data ed;
2553 struct wpa_signal_info sig;
2554 int res;
2555
2556 if (tb[NL80211_ATTR_CQM] == NULL ||
2557 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2558 cqm_policy)) {
2559 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2560 return;
2561 }
2562
2563 os_memset(&ed, 0, sizeof(ed));
2564
2565 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2566 if (!tb[NL80211_ATTR_MAC])
2567 return;
2568 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2569 ETH_ALEN);
2570 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2571 return;
2572 }
2573
2574 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2575 return;
2576 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2577
2578 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2579 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2580 "event: RSSI high");
2581 ed.signal_change.above_threshold = 1;
2582 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2583 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2584 "event: RSSI low");
2585 ed.signal_change.above_threshold = 0;
2586 } else
2587 return;
2588
2589 res = nl80211_get_link_signal(drv, &sig);
2590 if (res == 0) {
2591 ed.signal_change.current_signal = sig.current_signal;
2592 ed.signal_change.current_txrate = sig.current_txrate;
2593 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2594 sig.current_signal, sig.current_txrate);
2595 }
2596
2597 res = nl80211_get_link_noise(drv, &sig);
2598 if (res == 0) {
2599 ed.signal_change.current_noise = sig.current_noise;
2600 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2601 sig.current_noise);
2602 }
2603
2604 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2605}
2606
2607
2608static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2609 struct nlattr **tb)
2610{
2611 u8 *addr;
2612 union wpa_event_data data;
2613
2614 if (tb[NL80211_ATTR_MAC] == NULL)
2615 return;
2616 addr = nla_data(tb[NL80211_ATTR_MAC]);
2617 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002619 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002620 u8 *ies = NULL;
2621 size_t ies_len = 0;
2622 if (tb[NL80211_ATTR_IE]) {
2623 ies = nla_data(tb[NL80211_ATTR_IE]);
2624 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2625 }
2626 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2627 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2628 return;
2629 }
2630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2632 return;
2633
2634 os_memset(&data, 0, sizeof(data));
2635 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2636 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2637}
2638
2639
2640static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2641 struct nlattr **tb)
2642{
2643 u8 *addr;
2644 union wpa_event_data data;
2645
2646 if (tb[NL80211_ATTR_MAC] == NULL)
2647 return;
2648 addr = nla_data(tb[NL80211_ATTR_MAC]);
2649 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2650 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002651
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002653 drv_event_disassoc(drv->ctx, addr);
2654 return;
2655 }
2656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2658 return;
2659
2660 os_memset(&data, 0, sizeof(data));
2661 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2662 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2663}
2664
2665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002666static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2667 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2670 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2671 [NL80211_REKEY_DATA_KEK] = {
2672 .minlen = NL80211_KEK_LEN,
2673 .maxlen = NL80211_KEK_LEN,
2674 },
2675 [NL80211_REKEY_DATA_KCK] = {
2676 .minlen = NL80211_KCK_LEN,
2677 .maxlen = NL80211_KCK_LEN,
2678 },
2679 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2680 .minlen = NL80211_REPLAY_CTR_LEN,
2681 .maxlen = NL80211_REPLAY_CTR_LEN,
2682 },
2683 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 union wpa_event_data data;
2685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002686 if (!tb[NL80211_ATTR_MAC])
2687 return;
2688 if (!tb[NL80211_ATTR_REKEY_DATA])
2689 return;
2690 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2691 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2692 return;
2693 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2694 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002696 os_memset(&data, 0, sizeof(data));
2697 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2698 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2699 MAC2STR(data.driver_gtk_rekey.bssid));
2700 data.driver_gtk_rekey.replay_ctr =
2701 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2702 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2703 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2704 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2705}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002707
2708static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2709 struct nlattr **tb)
2710{
2711 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2712 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2713 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2714 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2715 .minlen = ETH_ALEN,
2716 .maxlen = ETH_ALEN,
2717 },
2718 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2719 };
2720 union wpa_event_data data;
2721
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002722 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2723
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002724 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2725 return;
2726 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2727 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2728 return;
2729 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2730 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2731 return;
2732
2733 os_memset(&data, 0, sizeof(data));
2734 os_memcpy(data.pmkid_candidate.bssid,
2735 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2736 data.pmkid_candidate.index =
2737 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2738 data.pmkid_candidate.preauth =
2739 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2740 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2741}
2742
2743
2744static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2745 struct nlattr **tb)
2746{
2747 union wpa_event_data data;
2748
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002749 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002751 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2752 return;
2753
2754 os_memset(&data, 0, sizeof(data));
2755 os_memcpy(data.client_poll.addr,
2756 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2757
2758 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2759}
2760
2761
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002762static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2763 struct nlattr **tb)
2764{
2765 union wpa_event_data data;
2766
2767 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2768
2769 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2770 return;
2771
2772 os_memset(&data, 0, sizeof(data));
2773 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2774 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2775 case NL80211_TDLS_SETUP:
2776 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2777 MACSTR, MAC2STR(data.tdls.peer));
2778 data.tdls.oper = TDLS_REQUEST_SETUP;
2779 break;
2780 case NL80211_TDLS_TEARDOWN:
2781 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2782 MACSTR, MAC2STR(data.tdls.peer));
2783 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2784 break;
2785 default:
2786 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2787 "event");
2788 return;
2789 }
2790 if (tb[NL80211_ATTR_REASON_CODE]) {
2791 data.tdls.reason_code =
2792 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2793 }
2794
2795 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2796}
2797
2798
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002799static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2800 struct nlattr **tb)
2801{
2802 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2803}
2804
2805
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002806static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2807 struct nlattr **tb)
2808{
2809 union wpa_event_data data;
2810 u32 reason;
2811
2812 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2813
2814 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2815 return;
2816
2817 os_memset(&data, 0, sizeof(data));
2818 os_memcpy(data.connect_failed_reason.addr,
2819 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2820
2821 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2822 switch (reason) {
2823 case NL80211_CONN_FAIL_MAX_CLIENTS:
2824 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2825 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2826 break;
2827 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2828 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2829 " tried to connect",
2830 MAC2STR(data.connect_failed_reason.addr));
2831 data.connect_failed_reason.code = BLOCKED_CLIENT;
2832 break;
2833 default:
2834 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2835 "%u", reason);
2836 return;
2837 }
2838
2839 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2840}
2841
2842
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002843static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2844 struct nlattr **tb)
2845{
2846 union wpa_event_data data;
2847 enum nl80211_radar_event event_type;
2848
2849 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2850 return;
2851
2852 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002853 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2854 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002855
Dmitry Shmidt051af732013-10-22 13:52:46 -07002856 /* Check HT params */
2857 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2858 data.dfs_event.ht_enabled = 1;
2859 data.dfs_event.chan_offset = 0;
2860
2861 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2862 case NL80211_CHAN_NO_HT:
2863 data.dfs_event.ht_enabled = 0;
2864 break;
2865 case NL80211_CHAN_HT20:
2866 break;
2867 case NL80211_CHAN_HT40PLUS:
2868 data.dfs_event.chan_offset = 1;
2869 break;
2870 case NL80211_CHAN_HT40MINUS:
2871 data.dfs_event.chan_offset = -1;
2872 break;
2873 }
2874 }
2875
2876 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002877 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2878 data.dfs_event.chan_width =
2879 convert2width(nla_get_u32(
2880 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2881 if (tb[NL80211_ATTR_CENTER_FREQ1])
2882 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002883 if (tb[NL80211_ATTR_CENTER_FREQ2])
2884 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2885
2886 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2887 data.dfs_event.freq, data.dfs_event.ht_enabled,
2888 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2889 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002890
2891 switch (event_type) {
2892 case NL80211_RADAR_DETECTED:
2893 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2894 break;
2895 case NL80211_RADAR_CAC_FINISHED:
2896 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2897 break;
2898 case NL80211_RADAR_CAC_ABORTED:
2899 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2900 break;
2901 case NL80211_RADAR_NOP_FINISHED:
2902 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2903 break;
2904 default:
2905 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2906 "received", event_type);
2907 break;
2908 }
2909}
2910
2911
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002912static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2913 int wds)
2914{
2915 struct wpa_driver_nl80211_data *drv = bss->drv;
2916 union wpa_event_data event;
2917
2918 if (!tb[NL80211_ATTR_MAC])
2919 return;
2920
2921 os_memset(&event, 0, sizeof(event));
2922 event.rx_from_unknown.bssid = bss->addr;
2923 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2924 event.rx_from_unknown.wds = wds;
2925
2926 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2927}
2928
2929
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002930static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2931 const u8 *data, size_t len)
2932{
2933 u32 i, count;
2934 union wpa_event_data event;
2935 struct wpa_freq_range *range = NULL;
2936 const struct qca_avoid_freq_list *freq_range;
2937
2938 freq_range = (const struct qca_avoid_freq_list *) data;
2939 if (len < sizeof(freq_range->count))
2940 return;
2941
2942 count = freq_range->count;
2943 if (len < sizeof(freq_range->count) +
2944 count * sizeof(struct qca_avoid_freq_range)) {
2945 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2946 (unsigned int) len);
2947 return;
2948 }
2949
2950 if (count > 0) {
2951 range = os_calloc(count, sizeof(struct wpa_freq_range));
2952 if (range == NULL)
2953 return;
2954 }
2955
2956 os_memset(&event, 0, sizeof(event));
2957 for (i = 0; i < count; i++) {
2958 unsigned int idx = event.freq_range.num;
2959 range[idx].min = freq_range->range[i].start_freq;
2960 range[idx].max = freq_range->range[i].end_freq;
2961 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2962 range[idx].min, range[idx].max);
2963 if (range[idx].min > range[idx].max) {
2964 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2965 continue;
2966 }
2967 event.freq_range.num++;
2968 }
2969 event.freq_range.range = range;
2970
2971 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2972
2973 os_free(range);
2974}
2975
2976
2977static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2978 u32 subcmd, u8 *data, size_t len)
2979{
2980 switch (subcmd) {
2981 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2982 qca_nl80211_avoid_freq(drv, data, len);
2983 break;
2984 default:
2985 wpa_printf(MSG_DEBUG,
2986 "nl80211: Ignore unsupported QCA vendor event %u",
2987 subcmd);
2988 break;
2989 }
2990}
2991
2992
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002993static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2994 struct nlattr **tb)
2995{
2996 u32 vendor_id, subcmd, wiphy = 0;
2997 int wiphy_idx;
2998 u8 *data = NULL;
2999 size_t len = 0;
3000
3001 if (!tb[NL80211_ATTR_VENDOR_ID] ||
3002 !tb[NL80211_ATTR_VENDOR_SUBCMD])
3003 return;
3004
3005 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
3006 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
3007
3008 if (tb[NL80211_ATTR_WIPHY])
3009 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3010
3011 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
3012 wiphy, vendor_id, subcmd);
3013
3014 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3015 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
3016 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
3017 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
3018 }
3019
3020 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
3021 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
3022 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
3023 wiphy, wiphy_idx);
3024 return;
3025 }
3026
3027 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003028 case OUI_QCA:
3029 nl80211_vendor_event_qca(drv, subcmd, data, len);
3030 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003031 default:
3032 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
3033 break;
3034 }
3035}
3036
3037
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003038static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
3039 struct nlattr *tb[])
3040{
3041 union wpa_event_data data;
3042 enum nl80211_reg_initiator init;
3043
3044 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
3045
3046 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
3047 return;
3048
3049 os_memset(&data, 0, sizeof(data));
3050 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
3051 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
3052 switch (init) {
3053 case NL80211_REGDOM_SET_BY_CORE:
3054 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
3055 break;
3056 case NL80211_REGDOM_SET_BY_USER:
3057 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
3058 break;
3059 case NL80211_REGDOM_SET_BY_DRIVER:
3060 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
3061 break;
3062 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3063 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
3064 break;
3065 }
3066
3067 if (tb[NL80211_ATTR_REG_TYPE]) {
3068 enum nl80211_reg_type type;
3069 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
3070 wpa_printf(MSG_DEBUG, " * type=%d", type);
3071 switch (type) {
3072 case NL80211_REGDOM_TYPE_COUNTRY:
3073 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
3074 break;
3075 case NL80211_REGDOM_TYPE_WORLD:
3076 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
3077 break;
3078 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
3079 data.channel_list_changed.type =
3080 REGDOM_TYPE_CUSTOM_WORLD;
3081 break;
3082 case NL80211_REGDOM_TYPE_INTERSECTION:
3083 data.channel_list_changed.type =
3084 REGDOM_TYPE_INTERSECTION;
3085 break;
3086 }
3087 }
3088
3089 if (tb[NL80211_ATTR_REG_ALPHA2]) {
3090 os_strlcpy(data.channel_list_changed.alpha2,
3091 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
3092 sizeof(data.channel_list_changed.alpha2));
3093 wpa_printf(MSG_DEBUG, " * alpha2=%s",
3094 data.channel_list_changed.alpha2);
3095 }
3096
3097 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
3098}
3099
3100
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003101static void do_process_drv_event(struct i802_bss *bss, int cmd,
3102 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003103{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003104 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003105 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003106
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003107 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
3108 cmd, nl80211_command_to_string(cmd), bss->ifname);
3109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
3111 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
3112 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003113 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114 drv->ap_scan_as_station);
3115 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 }
3117
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003118 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003119 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003120 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003121 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003122 if (drv->scan_for_auth) {
3123 /*
3124 * Cannot indicate EVENT_SCAN_STARTED here since we skip
3125 * EVENT_SCAN_RESULTS in scan_for_auth case and the
3126 * upper layer implementation could get confused about
3127 * scanning state.
3128 */
3129 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3130 break;
3131 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003132 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003134 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003135 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003136 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003137 break;
3138 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003139 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003140 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003141 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3142 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003144 wpa_dbg(drv->ctx, MSG_DEBUG,
3145 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003146 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147 drv->scan_complete_events = 1;
3148 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3149 drv->ctx);
3150 send_scan_event(drv, 0, tb);
3151 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003152 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003153 wpa_dbg(drv->ctx, MSG_DEBUG,
3154 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003155 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003156 send_scan_event(drv, 0, tb);
3157 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003159 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003160 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 /*
3162 * Need to indicate that scan results are available in order
3163 * not to make wpa_supplicant stop its scanning.
3164 */
3165 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3166 drv->ctx);
3167 send_scan_event(drv, 1, tb);
3168 break;
3169 case NL80211_CMD_AUTHENTICATE:
3170 case NL80211_CMD_ASSOCIATE:
3171 case NL80211_CMD_DEAUTHENTICATE:
3172 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173 case NL80211_CMD_FRAME_TX_STATUS:
3174 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3175 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003176 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3178 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003179 tb[NL80211_ATTR_COOKIE],
3180 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 break;
3182 case NL80211_CMD_CONNECT:
3183 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003184 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185 tb[NL80211_ATTR_STATUS_CODE],
3186 tb[NL80211_ATTR_MAC],
3187 tb[NL80211_ATTR_REQ_IE],
3188 tb[NL80211_ATTR_RESP_IE]);
3189 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003190 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003191 mlme_event_ch_switch(drv,
3192 tb[NL80211_ATTR_IFINDEX],
3193 tb[NL80211_ATTR_WIPHY_FREQ],
3194 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3195 tb[NL80211_ATTR_CHANNEL_WIDTH],
3196 tb[NL80211_ATTR_CENTER_FREQ1],
3197 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003198 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003200 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003201 tb[NL80211_ATTR_MAC],
3202 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203 break;
3204 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003205 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206 break;
3207 case NL80211_CMD_JOIN_IBSS:
3208 mlme_event_join_ibss(drv, tb);
3209 break;
3210 case NL80211_CMD_REMAIN_ON_CHANNEL:
3211 mlme_event_remain_on_channel(drv, 0, tb);
3212 break;
3213 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3214 mlme_event_remain_on_channel(drv, 1, tb);
3215 break;
3216 case NL80211_CMD_NOTIFY_CQM:
3217 nl80211_cqm_event(drv, tb);
3218 break;
3219 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003220 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221 break;
3222 case NL80211_CMD_REG_BEACON_HINT:
3223 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003224 os_memset(&data, 0, sizeof(data));
3225 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003227 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 break;
3229 case NL80211_CMD_NEW_STATION:
3230 nl80211_new_station_event(drv, tb);
3231 break;
3232 case NL80211_CMD_DEL_STATION:
3233 nl80211_del_station_event(drv, tb);
3234 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003235 case NL80211_CMD_SET_REKEY_OFFLOAD:
3236 nl80211_rekey_offload_event(drv, tb);
3237 break;
3238 case NL80211_CMD_PMKSA_CANDIDATE:
3239 nl80211_pmksa_candidate_event(drv, tb);
3240 break;
3241 case NL80211_CMD_PROBE_CLIENT:
3242 nl80211_client_probe_event(drv, tb);
3243 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003244 case NL80211_CMD_TDLS_OPER:
3245 nl80211_tdls_oper_event(drv, tb);
3246 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003247 case NL80211_CMD_CONN_FAILED:
3248 nl80211_connect_failed_event(drv, tb);
3249 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003250 case NL80211_CMD_FT_EVENT:
3251 mlme_event_ft_event(drv, tb);
3252 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003253 case NL80211_CMD_RADAR_DETECT:
3254 nl80211_radar_event(drv, tb);
3255 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003256 case NL80211_CMD_STOP_AP:
3257 nl80211_stop_ap(drv, tb);
3258 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003259 case NL80211_CMD_VENDOR:
3260 nl80211_vendor_event(drv, tb);
3261 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003262 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003263 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3264 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003265 break;
3266 }
3267}
3268
3269
3270static int process_drv_event(struct nl_msg *msg, void *arg)
3271{
3272 struct wpa_driver_nl80211_data *drv = arg;
3273 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3274 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003275 struct i802_bss *bss;
3276 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003277
3278 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3279 genlmsg_attrlen(gnlh, 0), NULL);
3280
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003281 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003282 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3283
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003284 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003285 if (ifidx == -1 || ifidx == bss->ifindex) {
3286 do_process_drv_event(bss, gnlh->cmd, tb);
3287 return NL_SKIP;
3288 }
3289 wpa_printf(MSG_DEBUG,
3290 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3291 gnlh->cmd, ifidx);
3292 } else if (tb[NL80211_ATTR_WDEV]) {
3293 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3294 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003295 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003296 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3297 do_process_drv_event(bss, gnlh->cmd, tb);
3298 return NL_SKIP;
3299 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003300 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003301 wpa_printf(MSG_DEBUG,
3302 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3303 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003304 }
3305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003306 return NL_SKIP;
3307}
3308
3309
3310static int process_global_event(struct nl_msg *msg, void *arg)
3311{
3312 struct nl80211_global *global = arg;
3313 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3314 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003315 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003316 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003317 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003318 u64 wdev_id = 0;
3319 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003320
3321 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3322 genlmsg_attrlen(gnlh, 0), NULL);
3323
3324 if (tb[NL80211_ATTR_IFINDEX])
3325 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003326 else if (tb[NL80211_ATTR_WDEV]) {
3327 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3328 wdev_id_set = 1;
3329 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003330
Dmitry Shmidt04949592012-07-19 12:16:46 -07003331 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3332 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003333 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003334 if ((ifidx == -1 && !wdev_id_set) ||
3335 ifidx == bss->ifindex ||
3336 (wdev_id_set && bss->wdev_id_set &&
3337 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003338 do_process_drv_event(bss, gnlh->cmd, tb);
3339 return NL_SKIP;
3340 }
3341 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003342 }
3343
3344 return NL_SKIP;
3345}
3346
3347
3348static int process_bss_event(struct nl_msg *msg, void *arg)
3349{
3350 struct i802_bss *bss = arg;
3351 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3352 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3353
3354 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3355 genlmsg_attrlen(gnlh, 0), NULL);
3356
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003357 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3358 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3359 bss->ifname);
3360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003361 switch (gnlh->cmd) {
3362 case NL80211_CMD_FRAME:
3363 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003364 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003365 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3366 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003367 tb[NL80211_ATTR_COOKIE],
3368 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003369 break;
3370 case NL80211_CMD_UNEXPECTED_FRAME:
3371 nl80211_spurious_frame(bss, tb, 0);
3372 break;
3373 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3374 nl80211_spurious_frame(bss, tb, 1);
3375 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 default:
3377 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3378 "(cmd=%d)", gnlh->cmd);
3379 break;
3380 }
3381
3382 return NL_SKIP;
3383}
3384
3385
3386static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3387 void *handle)
3388{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003389 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003390 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003392 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003394 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07003395 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003396 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3397 __func__, res);
3398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399}
3400
3401
3402/**
3403 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3404 * @priv: driver_nl80211 private data
3405 * @alpha2_arg: country to which to switch to
3406 * Returns: 0 on success, -1 on failure
3407 *
3408 * This asks nl80211 to set the regulatory domain for given
3409 * country ISO / IEC alpha2.
3410 */
3411static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3412{
3413 struct i802_bss *bss = priv;
3414 struct wpa_driver_nl80211_data *drv = bss->drv;
3415 char alpha2[3];
3416 struct nl_msg *msg;
3417
3418 msg = nlmsg_alloc();
3419 if (!msg)
3420 return -ENOMEM;
3421
3422 alpha2[0] = alpha2_arg[0];
3423 alpha2[1] = alpha2_arg[1];
3424 alpha2[2] = '\0';
3425
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003426 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427
3428 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3429 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3430 return -EINVAL;
3431 return 0;
3432nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003433 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 return -EINVAL;
3435}
3436
3437
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003438static int nl80211_get_country(struct nl_msg *msg, void *arg)
3439{
3440 char *alpha2 = arg;
3441 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3442 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3443
3444 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3445 genlmsg_attrlen(gnlh, 0), NULL);
3446 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3447 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3448 return NL_SKIP;
3449 }
3450 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3451 return NL_SKIP;
3452}
3453
3454
3455static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3456{
3457 struct i802_bss *bss = priv;
3458 struct wpa_driver_nl80211_data *drv = bss->drv;
3459 struct nl_msg *msg;
3460 int ret;
3461
3462 msg = nlmsg_alloc();
3463 if (!msg)
3464 return -ENOMEM;
3465
3466 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3467 alpha2[0] = '\0';
3468 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3469 if (!alpha2[0])
3470 ret = -1;
3471
3472 return ret;
3473}
3474
3475
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003476static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3477{
3478 u32 *feat = arg;
3479 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3480 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3481
3482 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3483 genlmsg_attrlen(gnlh, 0), NULL);
3484
3485 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3486 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3487
3488 return NL_SKIP;
3489}
3490
3491
3492static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3493{
3494 u32 feat = 0;
3495 struct nl_msg *msg;
3496
3497 msg = nlmsg_alloc();
3498 if (!msg)
3499 goto nla_put_failure;
3500
3501 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3502 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3503 return feat;
3504
3505 msg = NULL;
3506nla_put_failure:
3507 nlmsg_free(msg);
3508 return 0;
3509}
3510
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003513 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003514 struct wpa_driver_capa *capa;
3515
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003516 unsigned int num_multichan_concurrent;
3517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003518 unsigned int error:1;
3519 unsigned int device_ap_sme:1;
3520 unsigned int poll_command_supported:1;
3521 unsigned int data_tx_status:1;
3522 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003523 unsigned int auth_supported:1;
3524 unsigned int connect_supported:1;
3525 unsigned int p2p_go_supported:1;
3526 unsigned int p2p_client_supported:1;
3527 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003528 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003529 unsigned int set_qos_map_supported:1;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003530 unsigned int have_low_prio_scan:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531};
3532
3533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003534static unsigned int probe_resp_offload_support(int supp_protocols)
3535{
3536 unsigned int prot = 0;
3537
3538 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3539 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3540 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3541 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3542 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3543 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3544 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3545 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3546
3547 return prot;
3548}
3549
3550
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003551static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3552 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003553{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003554 struct nlattr *nl_mode;
3555 int i;
3556
3557 if (tb == NULL)
3558 return;
3559
3560 nla_for_each_nested(nl_mode, tb, i) {
3561 switch (nla_type(nl_mode)) {
3562 case NL80211_IFTYPE_AP:
3563 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3564 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003565 case NL80211_IFTYPE_ADHOC:
3566 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3567 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003568 case NL80211_IFTYPE_P2P_DEVICE:
3569 info->capa->flags |=
3570 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3571 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003572 case NL80211_IFTYPE_P2P_GO:
3573 info->p2p_go_supported = 1;
3574 break;
3575 case NL80211_IFTYPE_P2P_CLIENT:
3576 info->p2p_client_supported = 1;
3577 break;
3578 case NL80211_IFTYPE_MONITOR:
3579 info->monitor_supported = 1;
3580 break;
3581 }
3582 }
3583}
3584
3585
3586static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3587 struct nlattr *nl_combi)
3588{
3589 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3590 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3591 struct nlattr *nl_limit, *nl_mode;
3592 int err, rem_limit, rem_mode;
3593 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003594 static struct nla_policy
3595 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3596 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3597 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3598 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3599 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003600 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003601 },
3602 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3603 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3604 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3605 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003607 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3608 nl_combi, iface_combination_policy);
3609 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3610 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3611 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3612 return 0; /* broken combination */
3613
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003614 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3615 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3616
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003617 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3618 rem_limit) {
3619 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3620 nl_limit, iface_limit_policy);
3621 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3622 return 0; /* broken combination */
3623
3624 nla_for_each_nested(nl_mode,
3625 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3626 rem_mode) {
3627 int ift = nla_type(nl_mode);
3628 if (ift == NL80211_IFTYPE_P2P_GO ||
3629 ift == NL80211_IFTYPE_P2P_CLIENT)
3630 combination_has_p2p = 1;
3631 if (ift == NL80211_IFTYPE_STATION)
3632 combination_has_mgd = 1;
3633 }
3634 if (combination_has_p2p && combination_has_mgd)
3635 break;
3636 }
3637
3638 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003639 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003640 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003641
3642 info->p2p_concurrent = 1;
3643 if (info->num_multichan_concurrent < num_channels)
3644 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003645 }
3646
3647 return 0;
3648}
3649
3650
3651static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3652 struct nlattr *tb)
3653{
3654 struct nlattr *nl_combi;
3655 int rem_combi;
3656
3657 if (tb == NULL)
3658 return;
3659
3660 nla_for_each_nested(nl_combi, tb, rem_combi) {
3661 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3662 break;
3663 }
3664}
3665
3666
3667static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3668 struct nlattr *tb)
3669{
3670 struct nlattr *nl_cmd;
3671 int i;
3672
3673 if (tb == NULL)
3674 return;
3675
3676 nla_for_each_nested(nl_cmd, tb, i) {
3677 switch (nla_get_u32(nl_cmd)) {
3678 case NL80211_CMD_AUTHENTICATE:
3679 info->auth_supported = 1;
3680 break;
3681 case NL80211_CMD_CONNECT:
3682 info->connect_supported = 1;
3683 break;
3684 case NL80211_CMD_START_SCHED_SCAN:
3685 info->capa->sched_scan_supported = 1;
3686 break;
3687 case NL80211_CMD_PROBE_CLIENT:
3688 info->poll_command_supported = 1;
3689 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003690 case NL80211_CMD_CHANNEL_SWITCH:
3691 info->channel_switch_supported = 1;
3692 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003693 case NL80211_CMD_SET_QOS_MAP:
3694 info->set_qos_map_supported = 1;
3695 break;
3696 }
3697 }
3698}
3699
3700
3701static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3702 struct nlattr *tb)
3703{
3704 int i, num;
3705 u32 *ciphers;
3706
3707 if (tb == NULL)
3708 return;
3709
3710 num = nla_len(tb) / sizeof(u32);
3711 ciphers = nla_data(tb);
3712 for (i = 0; i < num; i++) {
3713 u32 c = ciphers[i];
3714
3715 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3716 c >> 24, (c >> 16) & 0xff,
3717 (c >> 8) & 0xff, c & 0xff);
3718 switch (c) {
3719 case WLAN_CIPHER_SUITE_CCMP_256:
3720 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3721 break;
3722 case WLAN_CIPHER_SUITE_GCMP_256:
3723 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3724 break;
3725 case WLAN_CIPHER_SUITE_CCMP:
3726 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3727 break;
3728 case WLAN_CIPHER_SUITE_GCMP:
3729 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3730 break;
3731 case WLAN_CIPHER_SUITE_TKIP:
3732 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3733 break;
3734 case WLAN_CIPHER_SUITE_WEP104:
3735 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3736 break;
3737 case WLAN_CIPHER_SUITE_WEP40:
3738 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3739 break;
3740 case WLAN_CIPHER_SUITE_AES_CMAC:
3741 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3742 break;
3743 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3744 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3745 break;
3746 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3747 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3748 break;
3749 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3750 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3751 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003752 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3753 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3754 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003755 }
3756 }
3757}
3758
3759
3760static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3761 struct nlattr *tb)
3762{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003763 if (tb)
3764 capa->max_remain_on_chan = nla_get_u32(tb);
3765}
3766
3767
3768static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3769 struct nlattr *ext_setup)
3770{
3771 if (tdls == NULL)
3772 return;
3773
3774 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3775 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3776
3777 if (ext_setup) {
3778 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3779 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3780 }
3781}
3782
3783
3784static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3785 struct nlattr *tb)
3786{
3787 u32 flags;
3788 struct wpa_driver_capa *capa = info->capa;
3789
3790 if (tb == NULL)
3791 return;
3792
3793 flags = nla_get_u32(tb);
3794
3795 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3796 info->data_tx_status = 1;
3797
3798 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3799 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3800
3801 if (flags & NL80211_FEATURE_SAE)
3802 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3803
3804 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3805 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003806
3807 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3808 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003809
3810 if (flags & NL80211_FEATURE_LOW_PRIORITY_SCAN)
3811 info->have_low_prio_scan = 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003812}
3813
3814
3815static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3816 struct nlattr *tb)
3817{
3818 u32 protocols;
3819
3820 if (tb == NULL)
3821 return;
3822
3823 protocols = nla_get_u32(tb);
3824 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3825 "mode");
3826 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3827 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3828}
3829
3830
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003831static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3832 struct nlattr *tb)
3833{
3834 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3835
3836 if (tb == NULL)
3837 return;
3838
3839 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3840 tb, NULL))
3841 return;
3842
3843 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3844 capa->wowlan_triggers.any = 1;
3845 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3846 capa->wowlan_triggers.disconnect = 1;
3847 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3848 capa->wowlan_triggers.magic_pkt = 1;
3849 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3850 capa->wowlan_triggers.gtk_rekey_failure = 1;
3851 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3852 capa->wowlan_triggers.eap_identity_req = 1;
3853 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3854 capa->wowlan_triggers.four_way_handshake = 1;
3855 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3856 capa->wowlan_triggers.rfkill_release = 1;
3857}
3858
3859
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003860static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3861{
3862 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3863 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3864 struct wiphy_info_data *info = arg;
3865 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003866 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3869 genlmsg_attrlen(gnlh, 0), NULL);
3870
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003871 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003872 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003873 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3874 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003876 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003879 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3880 capa->max_sched_scan_ssids =
3881 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3882
3883 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3884 capa->max_match_sets =
3885 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3886
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003887 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3888 capa->max_acl_mac_addrs =
3889 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3890
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003891 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3892 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3893 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003894 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003896 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3897 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3898 "off-channel TX");
3899 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3900 }
3901
3902 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3903 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3904 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3905 }
3906
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003907 wiphy_info_max_roc(capa,
3908 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003909
3910 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3911 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003913 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3914 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003915
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003916 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3917 info->device_ap_sme = 1;
3918
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003919 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3920 wiphy_info_probe_resp_offload(capa,
3921 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003922
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003923 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3924 drv->extended_capa == NULL) {
3925 drv->extended_capa =
3926 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3927 if (drv->extended_capa) {
3928 os_memcpy(drv->extended_capa,
3929 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3930 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3931 drv->extended_capa_len =
3932 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3933 }
3934 drv->extended_capa_mask =
3935 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3936 if (drv->extended_capa_mask) {
3937 os_memcpy(drv->extended_capa_mask,
3938 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3939 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3940 } else {
3941 os_free(drv->extended_capa);
3942 drv->extended_capa = NULL;
3943 drv->extended_capa_len = 0;
3944 }
3945 }
3946
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003947 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3948 struct nlattr *nl;
3949 int rem;
3950
3951 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3952 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003953 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003954 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3955 continue;
3956 }
3957 vinfo = nla_data(nl);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003958 switch (vinfo->subcmd) {
3959 case QCA_NL80211_VENDOR_SUBCMD_ROAMING:
3960 drv->roaming_vendor_cmd_avail = 1;
3961 break;
3962 case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003963 drv->dfs_vendor_cmd_avail = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003964 break;
3965 }
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003966
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003967 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3968 vinfo->vendor_id, vinfo->subcmd);
3969 }
3970 }
3971
3972 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3973 struct nlattr *nl;
3974 int rem;
3975
3976 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3977 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003978 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003979 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3980 continue;
3981 }
3982 vinfo = nla_data(nl);
3983 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3984 vinfo->vendor_id, vinfo->subcmd);
3985 }
3986 }
3987
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003988 wiphy_info_wowlan_triggers(capa,
3989 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
3990
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003991 if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
3992 capa->max_stations =
3993 nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
3994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 return NL_SKIP;
3996}
3997
3998
3999static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
4000 struct wiphy_info_data *info)
4001{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004002 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 struct nl_msg *msg;
4004
4005 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004006 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004007 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008
4009 msg = nlmsg_alloc();
4010 if (!msg)
4011 return -1;
4012
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004013 feat = get_nl80211_protocol_features(drv);
4014 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
4015 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
4016 else
4017 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004019 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004020 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004021 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004023 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
4024 return -1;
4025
4026 if (info->auth_supported)
4027 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
4028 else if (!info->connect_supported) {
4029 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
4030 "authentication/association or connect commands");
4031 info->error = 1;
4032 }
4033
4034 if (info->p2p_go_supported && info->p2p_client_supported)
4035 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
4036 if (info->p2p_concurrent) {
4037 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
4038 "interface (driver advertised support)");
4039 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
4040 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
4041 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004042 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004043 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
4044 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004045 drv->capa.num_multichan_concurrent =
4046 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004047 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004048
4049 /* default to 5000 since early versions of mac80211 don't set it */
4050 if (!drv->capa.max_remain_on_chan)
4051 drv->capa.max_remain_on_chan = 5000;
4052
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004053 if (info->channel_switch_supported)
4054 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
4055
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004056 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057nla_put_failure:
4058 nlmsg_free(msg);
4059 return -1;
4060}
4061
4062
4063static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
4064{
4065 struct wiphy_info_data info;
4066 if (wpa_driver_nl80211_get_info(drv, &info))
4067 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004068
4069 if (info.error)
4070 return -1;
4071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4074 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4075 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4076 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
4078 WPA_DRIVER_AUTH_SHARED |
4079 WPA_DRIVER_AUTH_LEAP;
4080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
4082 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004083 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004084
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004085 /*
4086 * As all cfg80211 drivers must support cases where the AP interface is
4087 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
4088 * case that the user space daemon has crashed, they must be able to
4089 * cleanup all stations and key entries in the AP tear down flow. Thus,
4090 * this flag can/should always be set for cfg80211 drivers.
4091 */
4092 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
4093
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004094 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004095 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004096
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004097 /*
4098 * No AP SME is currently assumed to also indicate no AP MLME
4099 * in the driver/firmware.
4100 */
4101 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
4102 }
4103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004104 drv->device_ap_sme = info.device_ap_sme;
4105 drv->poll_command_supported = info.poll_command_supported;
4106 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004107 if (info.set_qos_map_supported)
4108 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004109 drv->have_low_prio_scan = info.have_low_prio_scan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004110
4111 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004112 * If poll command and tx status are supported, mac80211 is new enough
4113 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004114 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004115 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004116
4117 if (drv->device_ap_sme && drv->use_monitor) {
4118 /*
4119 * Non-mac80211 drivers may not support monitor interface.
4120 * Make sure we do not get stuck with incorrect capability here
4121 * by explicitly testing this.
4122 */
4123 if (!info.monitor_supported) {
4124 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
4125 "with device_ap_sme since no monitor mode "
4126 "support detected");
4127 drv->use_monitor = 0;
4128 }
4129 }
4130
4131 /*
4132 * If we aren't going to use monitor interfaces, but the
4133 * driver doesn't support data TX status, we won't get TX
4134 * status for EAPOL frames.
4135 */
4136 if (!drv->use_monitor && !info.data_tx_status)
4137 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138
4139 return 0;
4140}
4141
4142
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143#ifdef ANDROID
4144static int android_genl_ctrl_resolve(struct nl_handle *handle,
4145 const char *name)
4146{
4147 /*
4148 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4149 * need to work around that.
4150 */
4151 struct nl_cache *cache = NULL;
4152 struct genl_family *nl80211 = NULL;
4153 int id = -1;
4154
4155 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4156 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4157 "netlink cache");
4158 goto fail;
4159 }
4160
4161 nl80211 = genl_ctrl_search_by_name(cache, name);
4162 if (nl80211 == NULL)
4163 goto fail;
4164
4165 id = genl_family_get_id(nl80211);
4166
4167fail:
4168 if (nl80211)
4169 genl_family_put(nl80211);
4170 if (cache)
4171 nl_cache_free(cache);
4172
4173 return id;
4174}
4175#define genl_ctrl_resolve android_genl_ctrl_resolve
4176#endif /* ANDROID */
4177
4178
4179static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180{
4181 int ret;
4182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004183 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4184 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4186 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004187 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188 }
4189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004190 global->nl = nl_create_handle(global->nl_cb, "nl");
4191 if (global->nl == NULL)
4192 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004194 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4195 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4197 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004198 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 }
4200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004201 global->nl_event = nl_create_handle(global->nl_cb, "event");
4202 if (global->nl_event == NULL)
4203 goto err;
4204
4205 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004206 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004207 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208 if (ret < 0) {
4209 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4210 "membership for scan events: %d (%s)",
4211 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004212 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213 }
4214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004215 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 if (ret < 0) {
4219 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4220 "membership for mlme events: %d (%s)",
4221 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004222 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 }
4224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004226 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004227 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 if (ret < 0) {
4229 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4230 "membership for regulatory events: %d (%s)",
4231 ret, strerror(-ret));
4232 /* Continue without regulatory events */
4233 }
4234
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004235 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4236 if (ret >= 0)
4237 ret = nl_socket_add_membership(global->nl_event, ret);
4238 if (ret < 0) {
4239 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4240 "membership for vendor events: %d (%s)",
4241 ret, strerror(-ret));
4242 /* Continue without vendor events */
4243 }
4244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004245 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4246 no_seq_check, NULL);
4247 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4248 process_global_event, global);
4249
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004250 nl80211_register_eloop_read(&global->nl_event,
4251 wpa_driver_nl80211_event_receive,
4252 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253
4254 return 0;
4255
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004256err:
4257 nl_destroy_handles(&global->nl_event);
4258 nl_destroy_handles(&global->nl);
4259 nl_cb_put(global->nl_cb);
4260 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261 return -1;
4262}
4263
4264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4266{
4267 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4268 if (!drv->nl_cb) {
4269 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4270 return -1;
4271 }
4272
4273 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4274 no_seq_check, NULL);
4275 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4276 process_drv_event, drv);
4277
4278 return 0;
4279}
4280
4281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4283{
4284 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4285 /*
4286 * This may be for any interface; use ifdown event to disable
4287 * interface.
4288 */
4289}
4290
4291
4292static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4293{
4294 struct wpa_driver_nl80211_data *drv = ctx;
4295 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004296 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4298 "after rfkill unblock");
4299 return;
4300 }
4301 /* rtnetlink ifup handler will report interface as enabled */
4302}
4303
4304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004305static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4306 void *eloop_ctx,
4307 void *handle)
4308{
4309 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4310 u8 data[2048];
4311 struct msghdr msg;
4312 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004313 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314 struct cmsghdr *cmsg;
4315 int res, found_ee = 0, found_wifi = 0, acked = 0;
4316 union wpa_event_data event;
4317
4318 memset(&msg, 0, sizeof(msg));
4319 msg.msg_iov = &entry;
4320 msg.msg_iovlen = 1;
4321 entry.iov_base = data;
4322 entry.iov_len = sizeof(data);
4323 msg.msg_control = &control;
4324 msg.msg_controllen = sizeof(control);
4325
4326 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4327 /* if error or not fitting 802.3 header, return */
4328 if (res < 14)
4329 return;
4330
4331 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4332 {
4333 if (cmsg->cmsg_level == SOL_SOCKET &&
4334 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4335 int *ack;
4336
4337 found_wifi = 1;
4338 ack = (void *)CMSG_DATA(cmsg);
4339 acked = *ack;
4340 }
4341
4342 if (cmsg->cmsg_level == SOL_PACKET &&
4343 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4344 struct sock_extended_err *err =
4345 (struct sock_extended_err *)CMSG_DATA(cmsg);
4346
4347 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4348 found_ee = 1;
4349 }
4350 }
4351
4352 if (!found_ee || !found_wifi)
4353 return;
4354
4355 memset(&event, 0, sizeof(event));
4356 event.eapol_tx_status.dst = data;
4357 event.eapol_tx_status.data = data + 14;
4358 event.eapol_tx_status.data_len = res - 14;
4359 event.eapol_tx_status.ack = acked;
4360 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4361}
4362
4363
4364static int nl80211_init_bss(struct i802_bss *bss)
4365{
4366 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4367 if (!bss->nl_cb)
4368 return -1;
4369
4370 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4371 no_seq_check, NULL);
4372 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4373 process_bss_event, bss);
4374
4375 return 0;
4376}
4377
4378
4379static void nl80211_destroy_bss(struct i802_bss *bss)
4380{
4381 nl_cb_put(bss->nl_cb);
4382 bss->nl_cb = NULL;
4383}
4384
4385
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004386static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4387 void *global_priv, int hostapd,
4388 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389{
4390 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 struct rfkill_config *rcfg;
4392 struct i802_bss *bss;
4393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004394 if (global_priv == NULL)
4395 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 drv = os_zalloc(sizeof(*drv));
4397 if (drv == NULL)
4398 return NULL;
4399 drv->global = global_priv;
4400 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004401 drv->hostapd = !!hostapd;
4402 drv->eapol_sock = -1;
4403 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4404 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004405
4406 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4407 if (!drv->first_bss) {
4408 os_free(drv);
4409 return NULL;
4410 }
4411 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004413 bss->ctx = ctx;
4414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4416 drv->monitor_ifidx = -1;
4417 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004418 drv->eapol_tx_sock = -1;
4419 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420
4421 if (wpa_driver_nl80211_init_nl(drv)) {
4422 os_free(drv);
4423 return NULL;
4424 }
4425
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004426 if (nl80211_init_bss(bss))
4427 goto failed;
4428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004429 rcfg = os_zalloc(sizeof(*rcfg));
4430 if (rcfg == NULL)
4431 goto failed;
4432 rcfg->ctx = drv;
4433 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4434 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4435 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4436 drv->rfkill = rfkill_init(rcfg);
4437 if (drv->rfkill == NULL) {
4438 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4439 os_free(rcfg);
4440 }
4441
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004442 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4443 drv->start_iface_up = 1;
4444
4445 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 goto failed;
4447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004448 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4449 if (drv->eapol_tx_sock < 0)
4450 goto failed;
4451
4452 if (drv->data_tx_status) {
4453 int enabled = 1;
4454
4455 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4456 &enabled, sizeof(enabled)) < 0) {
4457 wpa_printf(MSG_DEBUG,
4458 "nl80211: wifi status sockopt failed\n");
4459 drv->data_tx_status = 0;
4460 if (!drv->use_monitor)
4461 drv->capa.flags &=
4462 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4463 } else {
4464 eloop_register_read_sock(drv->eapol_tx_sock,
4465 wpa_driver_nl80211_handle_eapol_tx_status,
4466 drv, NULL);
4467 }
4468 }
4469
4470 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004472 drv->in_interface_list = 1;
4473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474
4475 return bss;
4476
4477failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004478 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 return NULL;
4480}
4481
4482
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004483/**
4484 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4485 * @ctx: context to be used when calling wpa_supplicant functions,
4486 * e.g., wpa_supplicant_event()
4487 * @ifname: interface name, e.g., wlan0
4488 * @global_priv: private driver global data from global_init()
4489 * Returns: Pointer to private data, %NULL on failure
4490 */
4491static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4492 void *global_priv)
4493{
4494 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4495}
4496
4497
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004498static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 struct nl_handle *nl_handle,
4500 u16 type, const u8 *match, size_t match_len)
4501{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004502 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 struct nl_msg *msg;
4504 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004505 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506
4507 msg = nlmsg_alloc();
4508 if (!msg)
4509 return -1;
4510
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004511 buf[0] = '\0';
4512 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004513 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
4514 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004516 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4517
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004518 if (nl80211_set_iface_id(msg, bss) < 0)
4519 goto nla_put_failure;
4520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4522 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004524 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 msg = NULL;
4526 if (ret) {
4527 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4528 "failed (type=%u): ret=%d (%s)",
4529 type, ret, strerror(-ret));
4530 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4531 match, match_len);
4532 goto nla_put_failure;
4533 }
4534 ret = 0;
4535nla_put_failure:
4536 nlmsg_free(msg);
4537 return ret;
4538}
4539
4540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004541static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4542{
4543 struct wpa_driver_nl80211_data *drv = bss->drv;
4544
4545 if (bss->nl_mgmt) {
4546 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4547 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4548 return -1;
4549 }
4550
4551 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4552 if (bss->nl_mgmt == NULL)
4553 return -1;
4554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004555 return 0;
4556}
4557
4558
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004559static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4560{
4561 nl80211_register_eloop_read(&bss->nl_mgmt,
4562 wpa_driver_nl80211_event_receive,
4563 bss->nl_cb);
4564}
4565
4566
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004567static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 const u8 *match, size_t match_len)
4569{
4570 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004571 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 type, match, match_len);
4573}
4574
4575
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004576static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004578 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004579 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004580
4581 if (nl80211_alloc_mgmt_handle(bss))
4582 return -1;
4583 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4584 "handle %p", bss->nl_mgmt);
4585
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004586 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4587 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4588
4589 /* register for any AUTH message */
4590 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4591 }
4592
Dmitry Shmidt051af732013-10-22 13:52:46 -07004593#ifdef CONFIG_INTERWORKING
4594 /* QoS Map Configure */
4595 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004596 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004597#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004598#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004599 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004600 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004601 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004603 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004604 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004606 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004607 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004609 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004610 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004611 /* Protected GAS Initial Request */
4612 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4613 ret = -1;
4614 /* Protected GAS Initial Response */
4615 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4616 ret = -1;
4617 /* Protected GAS Comeback Request */
4618 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4619 ret = -1;
4620 /* Protected GAS Comeback Response */
4621 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4622 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004623#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4624#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004626 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4628 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004629 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004630 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004631 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 (u8 *) "\x7f\x50\x6f\x9a\x09",
4633 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004634 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635#endif /* CONFIG_P2P */
4636#ifdef CONFIG_IEEE80211W
4637 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004638 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004639 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004641#ifdef CONFIG_TDLS
4642 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4643 /* TDLS Discovery Response */
4644 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4645 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004646 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004647 }
4648#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649
4650 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004651 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004652 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653 else
4654 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4655 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4656
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004657 /* WNM - BSS Transition Management Request */
4658 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004659 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004660 /* WNM-Sleep Mode Response */
4661 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004662 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004663
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004664#ifdef CONFIG_HS20
4665 /* WNM-Notification */
4666 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004667 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004668#endif /* CONFIG_HS20 */
4669
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004670 nl80211_mgmt_handle_register_eloop(bss);
4671
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004672 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673}
4674
4675
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004676static int nl80211_register_spurious_class3(struct i802_bss *bss)
4677{
4678 struct wpa_driver_nl80211_data *drv = bss->drv;
4679 struct nl_msg *msg;
4680 int ret = -1;
4681
4682 msg = nlmsg_alloc();
4683 if (!msg)
4684 return -1;
4685
4686 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4687
4688 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4689
4690 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4691 msg = NULL;
4692 if (ret) {
4693 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4694 "failed: ret=%d (%s)",
4695 ret, strerror(-ret));
4696 goto nla_put_failure;
4697 }
4698 ret = 0;
4699nla_put_failure:
4700 nlmsg_free(msg);
4701 return ret;
4702}
4703
4704
4705static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4706{
4707 static const int stypes[] = {
4708 WLAN_FC_STYPE_AUTH,
4709 WLAN_FC_STYPE_ASSOC_REQ,
4710 WLAN_FC_STYPE_REASSOC_REQ,
4711 WLAN_FC_STYPE_DISASSOC,
4712 WLAN_FC_STYPE_DEAUTH,
4713 WLAN_FC_STYPE_ACTION,
4714 WLAN_FC_STYPE_PROBE_REQ,
4715/* Beacon doesn't work as mac80211 doesn't currently allow
4716 * it, but it wouldn't really be the right thing anyway as
4717 * it isn't per interface ... maybe just dump the scan
4718 * results periodically for OLBC?
4719 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004720 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004721 };
4722 unsigned int i;
4723
4724 if (nl80211_alloc_mgmt_handle(bss))
4725 return -1;
4726 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4727 "handle %p", bss->nl_mgmt);
4728
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004729 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004730 if (nl80211_register_frame(bss, bss->nl_mgmt,
4731 (WLAN_FC_TYPE_MGMT << 2) |
4732 (stypes[i] << 4),
4733 NULL, 0) < 0) {
4734 goto out_err;
4735 }
4736 }
4737
4738 if (nl80211_register_spurious_class3(bss))
4739 goto out_err;
4740
4741 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4742 goto out_err;
4743
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004744 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004745 return 0;
4746
4747out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004748 nl_destroy_handles(&bss->nl_mgmt);
4749 return -1;
4750}
4751
4752
4753static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4754{
4755 if (nl80211_alloc_mgmt_handle(bss))
4756 return -1;
4757 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4758 "handle %p (device SME)", bss->nl_mgmt);
4759
4760 if (nl80211_register_frame(bss, bss->nl_mgmt,
4761 (WLAN_FC_TYPE_MGMT << 2) |
4762 (WLAN_FC_STYPE_ACTION << 4),
4763 NULL, 0) < 0)
4764 goto out_err;
4765
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004766 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004767 return 0;
4768
4769out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004770 nl_destroy_handles(&bss->nl_mgmt);
4771 return -1;
4772}
4773
4774
4775static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4776{
4777 if (bss->nl_mgmt == NULL)
4778 return;
4779 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4780 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004781 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004782
4783 nl80211_put_wiphy_data_ap(bss);
4784}
4785
4786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4788{
4789 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4790}
4791
4792
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004793static void nl80211_del_p2pdev(struct i802_bss *bss)
4794{
4795 struct wpa_driver_nl80211_data *drv = bss->drv;
4796 struct nl_msg *msg;
4797 int ret;
4798
4799 msg = nlmsg_alloc();
4800 if (!msg)
4801 return;
4802
4803 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4804 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4805
4806 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4807 msg = NULL;
4808
4809 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4810 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004811 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004812
4813nla_put_failure:
4814 nlmsg_free(msg);
4815}
4816
4817
4818static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4819{
4820 struct wpa_driver_nl80211_data *drv = bss->drv;
4821 struct nl_msg *msg;
4822 int ret = -1;
4823
4824 msg = nlmsg_alloc();
4825 if (!msg)
4826 return -1;
4827
4828 if (start)
4829 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4830 else
4831 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4832
4833 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4834
4835 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4836 msg = NULL;
4837
4838 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4839 start ? "Start" : "Stop",
4840 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004841 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004842
4843nla_put_failure:
4844 nlmsg_free(msg);
4845 return ret;
4846}
4847
4848
4849static int i802_set_iface_flags(struct i802_bss *bss, int up)
4850{
4851 enum nl80211_iftype nlmode;
4852
4853 nlmode = nl80211_get_ifmode(bss);
4854 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4855 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4856 bss->ifname, up);
4857 }
4858
4859 /* P2P Device has start/stop which is equivalent */
4860 return nl80211_set_p2pdev(bss, up);
4861}
4862
4863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004865wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4866 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004868 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004870 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871
4872 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004873 bss->ifindex = drv->ifindex;
4874 bss->wdev_id = drv->global->if_add_wdevid;
4875 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4876
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004877 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4878 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004879 drv->global->if_add_wdevid_set = 0;
4880
Dmitry Shmidt03658832014-08-13 11:03:49 -07004881 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4882 bss->static_ap = 1;
4883
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004884 if (wpa_driver_nl80211_capa(drv))
4885 return -1;
4886
4887 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4888 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004890 if (set_addr &&
4891 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4892 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4893 set_addr)))
4894 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004895
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004896 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4897 drv->start_mode_ap = 1;
4898
Dmitry Shmidt03658832014-08-13 11:03:49 -07004899 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004900 nlmode = NL80211_IFTYPE_AP;
4901 else if (bss->if_dynamic)
4902 nlmode = nl80211_get_ifmode(bss);
4903 else
4904 nlmode = NL80211_IFTYPE_STATION;
4905
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004906 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004907 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004908 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004910
Dmitry Shmidt98660862014-03-11 17:26:21 -07004911 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004912 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913
Dmitry Shmidt98660862014-03-11 17:26:21 -07004914 if (!rfkill_is_blocked(drv->rfkill)) {
4915 int ret = i802_set_iface_flags(bss, 1);
4916 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4918 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004919 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004921 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4922 return ret;
4923 } else {
4924 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4925 "interface '%s' due to rfkill", bss->ifname);
4926 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4927 return 0;
4928 drv->if_disabled = 1;
4929 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 }
4931
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004932 if (!drv->hostapd)
4933 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4934 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004936 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4937 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004939 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 if (send_rfkill_event) {
4942 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4943 drv, drv->ctx);
4944 }
4945
4946 return 0;
4947}
4948
4949
4950static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4951{
4952 struct nl_msg *msg;
4953
4954 msg = nlmsg_alloc();
4955 if (!msg)
4956 return -ENOMEM;
4957
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004958 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4959 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004960 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4962
4963 return send_and_recv_msgs(drv, msg, NULL, NULL);
4964 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004965 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 return -ENOBUFS;
4967}
4968
4969
4970/**
4971 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004972 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 *
4974 * Shut down driver interface and processing of driver events. Free
4975 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4976 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004977static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 struct wpa_driver_nl80211_data *drv = bss->drv;
4980
Dmitry Shmidt04949592012-07-19 12:16:46 -07004981 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004982 if (drv->data_tx_status)
4983 eloop_unregister_read_sock(drv->eapol_tx_sock);
4984 if (drv->eapol_tx_sock >= 0)
4985 close(drv->eapol_tx_sock);
4986
4987 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 wpa_driver_nl80211_probe_req_report(bss, 0);
4989 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004990 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4991 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4993 "interface %s from bridge %s: %s",
4994 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004995 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004996 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 }
4998 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004999 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5001 "bridge %s: %s",
5002 bss->brname, strerror(errno));
5003 }
5004
5005 nl80211_remove_monitor_interface(drv);
5006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005007 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 wpa_driver_nl80211_del_beacon(drv);
5009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 if (drv->eapol_sock >= 0) {
5011 eloop_unregister_read_sock(drv->eapol_sock);
5012 close(drv->eapol_sock);
5013 }
5014
5015 if (drv->if_indices != drv->default_if_indices)
5016 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005018 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
5020
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005021 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
5022 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005023 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024 rfkill_deinit(drv->rfkill);
5025
5026 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5027
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005028 if (!drv->start_iface_up)
5029 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005030
5031 if (drv->addr_changed) {
5032 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
5033 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5034 drv->perm_addr) < 0) {
5035 wpa_printf(MSG_DEBUG,
5036 "nl80211: Could not restore permanent MAC address");
5037 }
5038 }
5039
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005040 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005041 if (!drv->hostapd || !drv->start_mode_ap)
5042 wpa_driver_nl80211_set_mode(bss,
5043 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005044 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005045 } else {
5046 nl80211_mgmt_unsubscribe(bss, "deinit");
5047 nl80211_del_p2pdev(bss);
5048 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049 nl_cb_put(drv->nl_cb);
5050
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005051 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053 os_free(drv->filter_ssids);
5054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005055 os_free(drv->auth_ie);
5056
5057 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058 dl_list_del(&drv->list);
5059
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005060 os_free(drv->extended_capa);
5061 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005062 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 os_free(drv);
5064}
5065
5066
5067/**
5068 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
5069 * @eloop_ctx: Driver private data
5070 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
5071 *
5072 * This function can be used as registered timeout when starting a scan to
5073 * generate a scan completed event if the driver does not report this.
5074 */
5075static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
5076{
5077 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005078 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005079 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005080 drv->ap_scan_as_station);
5081 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 }
5083 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
5084 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
5085}
5086
5087
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005088static struct nl_msg *
5089nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005090 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005091{
5092 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005093 size_t i;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005094 u32 scan_flags = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005095
5096 msg = nlmsg_alloc();
5097 if (!msg)
5098 return NULL;
5099
5100 nl80211_cmd(drv, msg, 0, cmd);
5101
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005102 if (!wdev_id)
5103 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5104 else
5105 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005106
5107 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005108 struct nlattr *ssids;
5109
5110 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005111 if (ssids == NULL)
5112 goto fail;
5113 for (i = 0; i < params->num_ssids; i++) {
5114 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
5115 params->ssids[i].ssid,
5116 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005117 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
5118 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005119 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005120 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005121 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005122 }
5123
5124 if (params->extra_ies) {
5125 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
5126 params->extra_ies, params->extra_ies_len);
5127 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
5128 params->extra_ies) < 0)
5129 goto fail;
5130 }
5131
5132 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005133 struct nlattr *freqs;
5134 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005135 if (freqs == NULL)
5136 goto fail;
5137 for (i = 0; params->freqs[i]; i++) {
5138 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
5139 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005140 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005141 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005142 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005143 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005144 }
5145
5146 os_free(drv->filter_ssids);
5147 drv->filter_ssids = params->filter_ssids;
5148 params->filter_ssids = NULL;
5149 drv->num_filter_ssids = params->num_filter_ssids;
5150
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005151 if (params->only_new_results) {
5152 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005153 scan_flags |= NL80211_SCAN_FLAG_FLUSH;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005154 }
5155
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005156 if (params->low_priority && drv->have_low_prio_scan) {
5157 wpa_printf(MSG_DEBUG,
5158 "nl80211: Add NL80211_SCAN_FLAG_LOW_PRIORITY");
5159 scan_flags |= NL80211_SCAN_FLAG_LOW_PRIORITY;
5160 }
5161
5162 if (scan_flags)
5163 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags);
5164
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005165 return msg;
5166
5167fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005168nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005169 nlmsg_free(msg);
5170 return NULL;
5171}
5172
5173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174/**
5175 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005176 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177 * @params: Scan parameters
5178 * Returns: 0 on success, -1 on failure
5179 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005180static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181 struct wpa_driver_scan_params *params)
5182{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005184 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005185 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005187 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005188 drv->scan_for_auth = 0;
5189
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005190 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5191 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005192 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005195 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005196 struct nlattr *rates;
5197
Dmitry Shmidt04949592012-07-19 12:16:46 -07005198 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5199
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005200 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005201 if (rates == NULL)
5202 goto nla_put_failure;
5203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005204 /*
5205 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5206 * by masking out everything else apart from the OFDM rates 6,
5207 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5208 * rates are left enabled.
5209 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005210 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005211 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005212 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005213
5214 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
5215 }
5216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5218 msg = NULL;
5219 if (ret) {
5220 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5221 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005222 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005223 enum nl80211_iftype old_mode = drv->nlmode;
5224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005225 /*
5226 * mac80211 does not allow scan requests in AP mode, so
5227 * try to do this in station mode.
5228 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005229 if (wpa_driver_nl80211_set_mode(
5230 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231 goto nla_put_failure;
5232
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005233 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005234 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235 goto nla_put_failure;
5236 }
5237
5238 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005239 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240 ret = 0;
5241 } else
5242 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005243 }
5244
Dmitry Shmidt56052862013-10-04 10:23:25 -07005245 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 /* Not all drivers generate "scan completed" wireless event, so try to
5247 * read results after a timeout. */
5248 timeout = 10;
5249 if (drv->scan_complete_events) {
5250 /*
5251 * The driver seems to deliver events to notify when scan is
5252 * complete, so use longer timeout to avoid race conditions
5253 * with scanning and following association request.
5254 */
5255 timeout = 30;
5256 }
5257 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5258 "seconds", ret, timeout);
5259 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5260 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5261 drv, drv->ctx);
5262
5263nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005265 return ret;
5266}
5267
5268
5269/**
5270 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5271 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5272 * @params: Scan parameters
5273 * @interval: Interval between scan cycles in milliseconds
5274 * Returns: 0 on success, -1 on failure or if not supported
5275 */
5276static int wpa_driver_nl80211_sched_scan(void *priv,
5277 struct wpa_driver_scan_params *params,
5278 u32 interval)
5279{
5280 struct i802_bss *bss = priv;
5281 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005282 int ret = -1;
5283 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005284 size_t i;
5285
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005286 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5287
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005288#ifdef ANDROID
5289 if (!drv->capa.sched_scan_supported)
5290 return android_pno_start(bss, params);
5291#endif /* ANDROID */
5292
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005293 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5294 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005295 if (!msg)
5296 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005297
5298 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5299
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005300 if ((drv->num_filter_ssids &&
5301 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5302 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005303 struct nlattr *match_sets;
5304 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005305 if (match_sets == NULL)
5306 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005307
5308 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005309 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005310 wpa_hexdump_ascii(MSG_MSGDUMP,
5311 "nl80211: Sched scan filter SSID",
5312 drv->filter_ssids[i].ssid,
5313 drv->filter_ssids[i].ssid_len);
5314
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005315 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005316 if (match_set_ssid == NULL)
5317 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005318 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005319 drv->filter_ssids[i].ssid_len,
5320 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005321 if (params->filter_rssi)
5322 NLA_PUT_U32(msg,
5323 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5324 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005325
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005326 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005327 }
5328
Dmitry Shmidt97672262014-02-03 13:02:54 -08005329 /*
5330 * Due to backward compatibility code, newer kernels treat this
5331 * matchset (with only an RSSI filter) as the default for all
5332 * other matchsets, unless it's the only one, in which case the
5333 * matchset will actually allow all SSIDs above the RSSI.
5334 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005335 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005336 struct nlattr *match_set_rssi;
5337 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005338 if (match_set_rssi == NULL)
5339 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005340 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005341 params->filter_rssi);
5342 wpa_printf(MSG_MSGDUMP,
5343 "nl80211: Sched scan RSSI filter %d dBm",
5344 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005345 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005346 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005347
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005348 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005349 }
5350
5351 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5352
5353 /* TODO: if we get an error here, we should fall back to normal scan */
5354
5355 msg = NULL;
5356 if (ret) {
5357 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5358 "ret=%d (%s)", ret, strerror(-ret));
5359 goto nla_put_failure;
5360 }
5361
5362 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5363 "scan interval %d msec", ret, interval);
5364
5365nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005366 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005367 return ret;
5368}
5369
5370
5371/**
5372 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5373 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5374 * Returns: 0 on success, -1 on failure or if not supported
5375 */
5376static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5377{
5378 struct i802_bss *bss = priv;
5379 struct wpa_driver_nl80211_data *drv = bss->drv;
5380 int ret = 0;
5381 struct nl_msg *msg;
5382
5383#ifdef ANDROID
5384 if (!drv->capa.sched_scan_supported)
5385 return android_pno_stop(bss);
5386#endif /* ANDROID */
5387
5388 msg = nlmsg_alloc();
5389 if (!msg)
5390 return -1;
5391
5392 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5393
5394 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5395
5396 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5397 msg = NULL;
5398 if (ret) {
5399 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5400 "ret=%d (%s)", ret, strerror(-ret));
5401 goto nla_put_failure;
5402 }
5403
5404 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5405
5406nla_put_failure:
5407 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005408 return ret;
5409}
5410
5411
5412static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5413{
5414 const u8 *end, *pos;
5415
5416 if (ies == NULL)
5417 return NULL;
5418
5419 pos = ies;
5420 end = ies + ies_len;
5421
5422 while (pos + 1 < end) {
5423 if (pos + 2 + pos[1] > end)
5424 break;
5425 if (pos[0] == ie)
5426 return pos;
5427 pos += 2 + pos[1];
5428 }
5429
5430 return NULL;
5431}
5432
5433
5434static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5435 const u8 *ie, size_t ie_len)
5436{
5437 const u8 *ssid;
5438 size_t i;
5439
5440 if (drv->filter_ssids == NULL)
5441 return 0;
5442
5443 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5444 if (ssid == NULL)
5445 return 1;
5446
5447 for (i = 0; i < drv->num_filter_ssids; i++) {
5448 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5449 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5450 0)
5451 return 0;
5452 }
5453
5454 return 1;
5455}
5456
5457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458static int bss_info_handler(struct nl_msg *msg, void *arg)
5459{
5460 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5461 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5462 struct nlattr *bss[NL80211_BSS_MAX + 1];
5463 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5464 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5465 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5466 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5467 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5468 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5469 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5470 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5471 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5472 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5473 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5474 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5475 };
5476 struct nl80211_bss_info_arg *_arg = arg;
5477 struct wpa_scan_results *res = _arg->res;
5478 struct wpa_scan_res **tmp;
5479 struct wpa_scan_res *r;
5480 const u8 *ie, *beacon_ie;
5481 size_t ie_len, beacon_ie_len;
5482 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005483 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484
5485 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5486 genlmsg_attrlen(gnlh, 0), NULL);
5487 if (!tb[NL80211_ATTR_BSS])
5488 return NL_SKIP;
5489 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5490 bss_policy))
5491 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005492 if (bss[NL80211_BSS_STATUS]) {
5493 enum nl80211_bss_status status;
5494 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5495 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5496 bss[NL80211_BSS_FREQUENCY]) {
5497 _arg->assoc_freq =
5498 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5499 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5500 _arg->assoc_freq);
5501 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07005502 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
5503 bss[NL80211_BSS_FREQUENCY]) {
5504 _arg->ibss_freq =
5505 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5506 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
5507 _arg->ibss_freq);
5508 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005509 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5510 bss[NL80211_BSS_BSSID]) {
5511 os_memcpy(_arg->assoc_bssid,
5512 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5513 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5514 MACSTR, MAC2STR(_arg->assoc_bssid));
5515 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005516 }
5517 if (!res)
5518 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5520 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5521 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5522 } else {
5523 ie = NULL;
5524 ie_len = 0;
5525 }
5526 if (bss[NL80211_BSS_BEACON_IES]) {
5527 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5528 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5529 } else {
5530 beacon_ie = NULL;
5531 beacon_ie_len = 0;
5532 }
5533
5534 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5535 ie ? ie_len : beacon_ie_len))
5536 return NL_SKIP;
5537
5538 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5539 if (r == NULL)
5540 return NL_SKIP;
5541 if (bss[NL80211_BSS_BSSID])
5542 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5543 ETH_ALEN);
5544 if (bss[NL80211_BSS_FREQUENCY])
5545 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5546 if (bss[NL80211_BSS_BEACON_INTERVAL])
5547 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5548 if (bss[NL80211_BSS_CAPABILITY])
5549 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5550 r->flags |= WPA_SCAN_NOISE_INVALID;
5551 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5552 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5553 r->level /= 100; /* mBm to dBm */
5554 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5555 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5556 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005557 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558 } else
5559 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5560 if (bss[NL80211_BSS_TSF])
5561 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5562 if (bss[NL80211_BSS_SEEN_MS_AGO])
5563 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5564 r->ie_len = ie_len;
5565 pos = (u8 *) (r + 1);
5566 if (ie) {
5567 os_memcpy(pos, ie, ie_len);
5568 pos += ie_len;
5569 }
5570 r->beacon_ie_len = beacon_ie_len;
5571 if (beacon_ie)
5572 os_memcpy(pos, beacon_ie, beacon_ie_len);
5573
5574 if (bss[NL80211_BSS_STATUS]) {
5575 enum nl80211_bss_status status;
5576 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5577 switch (status) {
5578 case NL80211_BSS_STATUS_AUTHENTICATED:
5579 r->flags |= WPA_SCAN_AUTHENTICATED;
5580 break;
5581 case NL80211_BSS_STATUS_ASSOCIATED:
5582 r->flags |= WPA_SCAN_ASSOCIATED;
5583 break;
5584 default:
5585 break;
5586 }
5587 }
5588
Jouni Malinen87fd2792011-05-16 18:35:42 +03005589 /*
5590 * cfg80211 maintains separate BSS table entries for APs if the same
5591 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5592 * not use frequency as a separate key in the BSS table, so filter out
5593 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005594 * order to get the correct frequency into the BSS table. Similarly,
5595 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005596 */
5597 for (i = 0; i < res->num; i++) {
5598 const u8 *s1, *s2;
5599 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5600 continue;
5601
5602 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5603 res->res[i]->ie_len, WLAN_EID_SSID);
5604 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5605 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5606 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5607 continue;
5608
5609 /* Same BSSID,SSID was already included in scan results */
5610 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5611 "for " MACSTR, MAC2STR(r->bssid));
5612
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005613 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5614 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5615 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005616 os_free(res->res[i]);
5617 res->res[i] = r;
5618 } else
5619 os_free(r);
5620 return NL_SKIP;
5621 }
5622
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005623 tmp = os_realloc_array(res->res, res->num + 1,
5624 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 if (tmp == NULL) {
5626 os_free(r);
5627 return NL_SKIP;
5628 }
5629 tmp[res->num++] = r;
5630 res->res = tmp;
5631
5632 return NL_SKIP;
5633}
5634
5635
5636static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5637 const u8 *addr)
5638{
5639 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5640 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5641 "mismatch (" MACSTR ")", MAC2STR(addr));
5642 wpa_driver_nl80211_mlme(drv, addr,
5643 NL80211_CMD_DEAUTHENTICATE,
5644 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5645 }
5646}
5647
5648
5649static void wpa_driver_nl80211_check_bss_status(
5650 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5651{
5652 size_t i;
5653
5654 for (i = 0; i < res->num; i++) {
5655 struct wpa_scan_res *r = res->res[i];
5656 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5657 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5658 "indicates BSS status with " MACSTR
5659 " as authenticated",
5660 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005661 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5663 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5664 0) {
5665 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5666 " in local state (auth=" MACSTR
5667 " assoc=" MACSTR ")",
5668 MAC2STR(drv->auth_bssid),
5669 MAC2STR(drv->bssid));
5670 clear_state_mismatch(drv, r->bssid);
5671 }
5672 }
5673
5674 if (r->flags & WPA_SCAN_ASSOCIATED) {
5675 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5676 "indicate BSS status with " MACSTR
5677 " as associated",
5678 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005679 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 !drv->associated) {
5681 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5682 "(not associated) does not match "
5683 "with BSS state");
5684 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005685 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5687 0) {
5688 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5689 "(associated with " MACSTR ") does "
5690 "not match with BSS state",
5691 MAC2STR(drv->bssid));
5692 clear_state_mismatch(drv, r->bssid);
5693 clear_state_mismatch(drv, drv->bssid);
5694 }
5695 }
5696 }
5697}
5698
5699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700static struct wpa_scan_results *
5701nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5702{
5703 struct nl_msg *msg;
5704 struct wpa_scan_results *res;
5705 int ret;
5706 struct nl80211_bss_info_arg arg;
5707
5708 res = os_zalloc(sizeof(*res));
5709 if (res == NULL)
5710 return NULL;
5711 msg = nlmsg_alloc();
5712 if (!msg)
5713 goto nla_put_failure;
5714
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005715 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005716 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005717 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718
5719 arg.drv = drv;
5720 arg.res = res;
5721 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5722 msg = NULL;
5723 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005724 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5725 "BSSes)", (unsigned long) res->num);
5726 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727 return res;
5728 }
5729 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5730 "(%s)", ret, strerror(-ret));
5731nla_put_failure:
5732 nlmsg_free(msg);
5733 wpa_scan_results_free(res);
5734 return NULL;
5735}
5736
5737
5738/**
5739 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5740 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5741 * Returns: Scan results on success, -1 on failure
5742 */
5743static struct wpa_scan_results *
5744wpa_driver_nl80211_get_scan_results(void *priv)
5745{
5746 struct i802_bss *bss = priv;
5747 struct wpa_driver_nl80211_data *drv = bss->drv;
5748 struct wpa_scan_results *res;
5749
5750 res = nl80211_get_scan_results(drv);
5751 if (res)
5752 wpa_driver_nl80211_check_bss_status(drv, res);
5753 return res;
5754}
5755
5756
5757static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5758{
5759 struct wpa_scan_results *res;
5760 size_t i;
5761
5762 res = nl80211_get_scan_results(drv);
5763 if (res == NULL) {
5764 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5765 return;
5766 }
5767
5768 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5769 for (i = 0; i < res->num; i++) {
5770 struct wpa_scan_res *r = res->res[i];
5771 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5772 (int) i, (int) res->num, MAC2STR(r->bssid),
5773 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5774 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5775 }
5776
5777 wpa_scan_results_free(res);
5778}
5779
5780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005781static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5782{
5783 switch (alg) {
5784 case WPA_ALG_WEP:
5785 if (key_len == 5)
5786 return WLAN_CIPHER_SUITE_WEP40;
5787 return WLAN_CIPHER_SUITE_WEP104;
5788 case WPA_ALG_TKIP:
5789 return WLAN_CIPHER_SUITE_TKIP;
5790 case WPA_ALG_CCMP:
5791 return WLAN_CIPHER_SUITE_CCMP;
5792 case WPA_ALG_GCMP:
5793 return WLAN_CIPHER_SUITE_GCMP;
5794 case WPA_ALG_CCMP_256:
5795 return WLAN_CIPHER_SUITE_CCMP_256;
5796 case WPA_ALG_GCMP_256:
5797 return WLAN_CIPHER_SUITE_GCMP_256;
5798 case WPA_ALG_IGTK:
5799 return WLAN_CIPHER_SUITE_AES_CMAC;
5800 case WPA_ALG_BIP_GMAC_128:
5801 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5802 case WPA_ALG_BIP_GMAC_256:
5803 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5804 case WPA_ALG_BIP_CMAC_256:
5805 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5806 case WPA_ALG_SMS4:
5807 return WLAN_CIPHER_SUITE_SMS4;
5808 case WPA_ALG_KRK:
5809 return WLAN_CIPHER_SUITE_KRK;
5810 case WPA_ALG_NONE:
5811 case WPA_ALG_PMK:
5812 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5813 alg);
5814 return 0;
5815 }
5816
5817 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5818 alg);
5819 return 0;
5820}
5821
5822
5823static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5824{
5825 switch (cipher) {
5826 case WPA_CIPHER_CCMP_256:
5827 return WLAN_CIPHER_SUITE_CCMP_256;
5828 case WPA_CIPHER_GCMP_256:
5829 return WLAN_CIPHER_SUITE_GCMP_256;
5830 case WPA_CIPHER_CCMP:
5831 return WLAN_CIPHER_SUITE_CCMP;
5832 case WPA_CIPHER_GCMP:
5833 return WLAN_CIPHER_SUITE_GCMP;
5834 case WPA_CIPHER_TKIP:
5835 return WLAN_CIPHER_SUITE_TKIP;
5836 case WPA_CIPHER_WEP104:
5837 return WLAN_CIPHER_SUITE_WEP104;
5838 case WPA_CIPHER_WEP40:
5839 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005840 case WPA_CIPHER_GTK_NOT_USED:
5841 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005842 }
5843
5844 return 0;
5845}
5846
5847
5848static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5849 int max_suites)
5850{
5851 int num_suites = 0;
5852
5853 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5854 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5855 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5856 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5857 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5858 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5859 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5860 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5861 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5862 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5863 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5864 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5865 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5866 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5867
5868 return num_suites;
5869}
5870
5871
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005872static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005873 enum wpa_alg alg, const u8 *addr,
5874 int key_idx, int set_tx,
5875 const u8 *seq, size_t seq_len,
5876 const u8 *key, size_t key_len)
5877{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005879 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005880 struct nl_msg *msg;
5881 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005882 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005884 /* Ignore for P2P Device */
5885 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5886 return 0;
5887
5888 ifindex = if_nametoindex(ifname);
5889 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005891 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005893#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005894 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005895 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005896 tdls = 1;
5897 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005898#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899
5900 msg = nlmsg_alloc();
5901 if (!msg)
5902 return -ENOMEM;
5903
5904 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005905 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005907 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005908 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005909 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005910 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5911 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912 }
5913
Dmitry Shmidt98660862014-03-11 17:26:21 -07005914 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005916 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918
5919 if (addr && !is_broadcast_ether_addr(addr)) {
5920 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5921 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5922
5923 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5924 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5925 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5926 NL80211_KEYTYPE_GROUP);
5927 }
5928 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005929 struct nlattr *types;
5930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005932
5933 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934 if (!types)
5935 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005936 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5937 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005938 }
5939 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5940 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5941
5942 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5943 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5944 ret = 0;
5945 if (ret)
5946 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5947 ret, strerror(-ret));
5948
5949 /*
5950 * If we failed or don't need to set the default TX key (below),
5951 * we're done here.
5952 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005953 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005954 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005955 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956 !is_broadcast_ether_addr(addr))
5957 return ret;
5958
5959 msg = nlmsg_alloc();
5960 if (!msg)
5961 return -ENOMEM;
5962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005963 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5965 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5966 if (alg == WPA_ALG_IGTK)
5967 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5968 else
5969 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5970 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005971 struct nlattr *types;
5972
5973 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974 if (!types)
5975 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005976 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5977 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005978 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005979 struct nlattr *types;
5980
5981 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 if (!types)
5983 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005984 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5985 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 }
5987
5988 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5989 if (ret == -ENOENT)
5990 ret = 0;
5991 if (ret)
5992 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5993 "err=%d %s)", ret, strerror(-ret));
5994 return ret;
5995
5996nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005997 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005998 return -ENOBUFS;
5999}
6000
6001
6002static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
6003 int key_idx, int defkey,
6004 const u8 *seq, size_t seq_len,
6005 const u8 *key, size_t key_len)
6006{
6007 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
6008 if (!key_attr)
6009 return -1;
6010
6011 if (defkey && alg == WPA_ALG_IGTK)
6012 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
6013 else if (defkey)
6014 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
6015
6016 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
6017
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006018 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6019 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020
6021 if (seq && seq_len)
6022 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
6023
6024 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
6025
6026 nla_nest_end(msg, key_attr);
6027
6028 return 0;
6029 nla_put_failure:
6030 return -1;
6031}
6032
6033
6034static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
6035 struct nl_msg *msg)
6036{
6037 int i, privacy = 0;
6038 struct nlattr *nl_keys, *nl_key;
6039
6040 for (i = 0; i < 4; i++) {
6041 if (!params->wep_key[i])
6042 continue;
6043 privacy = 1;
6044 break;
6045 }
6046 if (params->wps == WPS_MODE_PRIVACY)
6047 privacy = 1;
6048 if (params->pairwise_suite &&
6049 params->pairwise_suite != WPA_CIPHER_NONE)
6050 privacy = 1;
6051
6052 if (!privacy)
6053 return 0;
6054
6055 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
6056
6057 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
6058 if (!nl_keys)
6059 goto nla_put_failure;
6060
6061 for (i = 0; i < 4; i++) {
6062 if (!params->wep_key[i])
6063 continue;
6064
6065 nl_key = nla_nest_start(msg, i);
6066 if (!nl_key)
6067 goto nla_put_failure;
6068
6069 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
6070 params->wep_key[i]);
6071 if (params->wep_key_len[i] == 5)
6072 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6073 WLAN_CIPHER_SUITE_WEP40);
6074 else
6075 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6076 WLAN_CIPHER_SUITE_WEP104);
6077
6078 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
6079
6080 if (i == params->wep_tx_keyidx)
6081 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
6082
6083 nla_nest_end(msg, nl_key);
6084 }
6085 nla_nest_end(msg, nl_keys);
6086
6087 return 0;
6088
6089nla_put_failure:
6090 return -ENOBUFS;
6091}
6092
6093
6094static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
6095 const u8 *addr, int cmd, u16 reason_code,
6096 int local_state_change)
6097{
6098 int ret = -1;
6099 struct nl_msg *msg;
6100
6101 msg = nlmsg_alloc();
6102 if (!msg)
6103 return -1;
6104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006105 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106
6107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6108 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006109 if (addr)
6110 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111 if (local_state_change)
6112 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6113
6114 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6115 msg = NULL;
6116 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006117 wpa_dbg(drv->ctx, MSG_DEBUG,
6118 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
6119 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120 goto nla_put_failure;
6121 }
6122 ret = 0;
6123
6124nla_put_failure:
6125 nlmsg_free(msg);
6126 return ret;
6127}
6128
6129
6130static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006131 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006133 int ret;
6134
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006135 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006136 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006137 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006138 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
6139 reason_code, 0);
6140 /*
6141 * For locally generated disconnect, supplicant already generates a
6142 * DEAUTH event, so ignore the event from NL80211.
6143 */
6144 drv->ignore_next_local_disconnect = ret == 0;
6145
6146 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006147}
6148
6149
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006150static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
6151 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006154 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07006155
6156 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
6157 nl80211_mark_disconnected(drv);
6158 return nl80211_leave_ibss(drv);
6159 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006160 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006161 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
6163 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006164 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006165 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
6166 reason_code, 0);
6167 /*
6168 * For locally generated deauthenticate, supplicant already generates a
6169 * DEAUTH event, so ignore the event from NL80211.
6170 */
6171 drv->ignore_next_local_deauth = ret == 0;
6172 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173}
6174
6175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006176static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6177 struct wpa_driver_auth_params *params)
6178{
6179 int i;
6180
6181 drv->auth_freq = params->freq;
6182 drv->auth_alg = params->auth_alg;
6183 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6184 drv->auth_local_state_change = params->local_state_change;
6185 drv->auth_p2p = params->p2p;
6186
6187 if (params->bssid)
6188 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6189 else
6190 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6191
6192 if (params->ssid) {
6193 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6194 drv->auth_ssid_len = params->ssid_len;
6195 } else
6196 drv->auth_ssid_len = 0;
6197
6198
6199 os_free(drv->auth_ie);
6200 drv->auth_ie = NULL;
6201 drv->auth_ie_len = 0;
6202 if (params->ie) {
6203 drv->auth_ie = os_malloc(params->ie_len);
6204 if (drv->auth_ie) {
6205 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6206 drv->auth_ie_len = params->ie_len;
6207 }
6208 }
6209
6210 for (i = 0; i < 4; i++) {
6211 if (params->wep_key[i] && params->wep_key_len[i] &&
6212 params->wep_key_len[i] <= 16) {
6213 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6214 params->wep_key_len[i]);
6215 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6216 } else
6217 drv->auth_wep_key_len[i] = 0;
6218 }
6219}
6220
6221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006223 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006224{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225 struct wpa_driver_nl80211_data *drv = bss->drv;
6226 int ret = -1, i;
6227 struct nl_msg *msg;
6228 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006229 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006231 int is_retry;
6232
6233 is_retry = drv->retry_auth;
6234 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006235 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006237 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006239 if (params->bssid)
6240 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6241 else
6242 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006244 nlmode = params->p2p ?
6245 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6246 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006247 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 return -1;
6249
6250retry:
6251 msg = nlmsg_alloc();
6252 if (!msg)
6253 return -1;
6254
6255 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6256 drv->ifindex);
6257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006258 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259
6260 for (i = 0; i < 4; i++) {
6261 if (!params->wep_key[i])
6262 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006263 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006264 NULL, i,
6265 i == params->wep_tx_keyidx, NULL, 0,
6266 params->wep_key[i],
6267 params->wep_key_len[i]);
6268 if (params->wep_tx_keyidx != i)
6269 continue;
6270 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6271 params->wep_key[i], params->wep_key_len[i])) {
6272 nlmsg_free(msg);
6273 return -1;
6274 }
6275 }
6276
6277 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6278 if (params->bssid) {
6279 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6280 MAC2STR(params->bssid));
6281 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6282 }
6283 if (params->freq) {
6284 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6285 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6286 }
6287 if (params->ssid) {
6288 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6289 params->ssid, params->ssid_len);
6290 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6291 params->ssid);
6292 }
6293 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6294 if (params->ie)
6295 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006296 if (params->sae_data) {
6297 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6298 params->sae_data_len);
6299 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6300 params->sae_data);
6301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6303 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6304 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6305 type = NL80211_AUTHTYPE_SHARED_KEY;
6306 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6307 type = NL80211_AUTHTYPE_NETWORK_EAP;
6308 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6309 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006310 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6311 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312 else
6313 goto nla_put_failure;
6314 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6315 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6316 if (params->local_state_change) {
6317 wpa_printf(MSG_DEBUG, " * Local state change only");
6318 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6319 }
6320
6321 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6322 msg = NULL;
6323 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006324 wpa_dbg(drv->ctx, MSG_DEBUG,
6325 "nl80211: MLME command failed (auth): ret=%d (%s)",
6326 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006327 count++;
6328 if (ret == -EALREADY && count == 1 && params->bssid &&
6329 !params->local_state_change) {
6330 /*
6331 * mac80211 does not currently accept new
6332 * authentication if we are already authenticated. As a
6333 * workaround, force deauthentication and try again.
6334 */
6335 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6336 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006337 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338 wpa_driver_nl80211_deauthenticate(
6339 bss, params->bssid,
6340 WLAN_REASON_PREV_AUTH_NOT_VALID);
6341 nlmsg_free(msg);
6342 goto retry;
6343 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006344
6345 if (ret == -ENOENT && params->freq && !is_retry) {
6346 /*
6347 * cfg80211 has likely expired the BSS entry even
6348 * though it was previously available in our internal
6349 * BSS table. To recover quickly, start a single
6350 * channel scan on the specified channel.
6351 */
6352 struct wpa_driver_scan_params scan;
6353 int freqs[2];
6354
6355 os_memset(&scan, 0, sizeof(scan));
6356 scan.num_ssids = 1;
6357 if (params->ssid) {
6358 scan.ssids[0].ssid = params->ssid;
6359 scan.ssids[0].ssid_len = params->ssid_len;
6360 }
6361 freqs[0] = params->freq;
6362 freqs[1] = 0;
6363 scan.freqs = freqs;
6364 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6365 "channel scan to refresh cfg80211 BSS "
6366 "entry");
6367 ret = wpa_driver_nl80211_scan(bss, &scan);
6368 if (ret == 0) {
6369 nl80211_copy_auth_params(drv, params);
6370 drv->scan_for_auth = 1;
6371 }
6372 } else if (is_retry) {
6373 /*
6374 * Need to indicate this with an event since the return
6375 * value from the retry is not delivered to core code.
6376 */
6377 union wpa_event_data event;
6378 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6379 "failed");
6380 os_memset(&event, 0, sizeof(event));
6381 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6382 ETH_ALEN);
6383 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6384 &event);
6385 }
6386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006387 goto nla_put_failure;
6388 }
6389 ret = 0;
6390 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6391 "successfully");
6392
6393nla_put_failure:
6394 nlmsg_free(msg);
6395 return ret;
6396}
6397
6398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006399static int wpa_driver_nl80211_authenticate_retry(
6400 struct wpa_driver_nl80211_data *drv)
6401{
6402 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006403 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006404 int i;
6405
6406 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6407
6408 os_memset(&params, 0, sizeof(params));
6409 params.freq = drv->auth_freq;
6410 params.auth_alg = drv->auth_alg;
6411 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6412 params.local_state_change = drv->auth_local_state_change;
6413 params.p2p = drv->auth_p2p;
6414
6415 if (!is_zero_ether_addr(drv->auth_bssid_))
6416 params.bssid = drv->auth_bssid_;
6417
6418 if (drv->auth_ssid_len) {
6419 params.ssid = drv->auth_ssid;
6420 params.ssid_len = drv->auth_ssid_len;
6421 }
6422
6423 params.ie = drv->auth_ie;
6424 params.ie_len = drv->auth_ie_len;
6425
6426 for (i = 0; i < 4; i++) {
6427 if (drv->auth_wep_key_len[i]) {
6428 params.wep_key[i] = drv->auth_wep_key[i];
6429 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6430 }
6431 }
6432
6433 drv->retry_auth = 1;
6434 return wpa_driver_nl80211_authenticate(bss, &params);
6435}
6436
6437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006438struct phy_info_arg {
6439 u16 *num_modes;
6440 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006441 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442};
6443
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006444static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6445 struct nlattr *ampdu_factor,
6446 struct nlattr *ampdu_density,
6447 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006448{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006449 if (capa)
6450 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006451
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006452 if (ampdu_factor)
6453 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006455 if (ampdu_density)
6456 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6457
6458 if (mcs_set && nla_len(mcs_set) >= 16) {
6459 u8 *mcs;
6460 mcs = nla_data(mcs_set);
6461 os_memcpy(mode->mcs_set, mcs, 16);
6462 }
6463}
6464
6465
6466static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6467 struct nlattr *capa,
6468 struct nlattr *mcs_set)
6469{
6470 if (capa)
6471 mode->vht_capab = nla_get_u32(capa);
6472
6473 if (mcs_set && nla_len(mcs_set) >= 8) {
6474 u8 *mcs;
6475 mcs = nla_data(mcs_set);
6476 os_memcpy(mode->vht_mcs_set, mcs, 8);
6477 }
6478}
6479
6480
6481static void phy_info_freq(struct hostapd_hw_modes *mode,
6482 struct hostapd_channel_data *chan,
6483 struct nlattr *tb_freq[])
6484{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006485 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006486 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6487 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006488 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006489 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6490 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006491
6492 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6493 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006494 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6495 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006496 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6497 chan->flag |= HOSTAPD_CHAN_RADAR;
6498
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006499 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6500 enum nl80211_dfs_state state =
6501 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6502
6503 switch (state) {
6504 case NL80211_DFS_USABLE:
6505 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6506 break;
6507 case NL80211_DFS_AVAILABLE:
6508 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6509 break;
6510 case NL80211_DFS_UNAVAILABLE:
6511 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6512 break;
6513 }
6514 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006515
6516 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6517 chan->dfs_cac_ms = nla_get_u32(
6518 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6519 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006520}
6521
6522
6523static int phy_info_freqs(struct phy_info_arg *phy_info,
6524 struct hostapd_hw_modes *mode, struct nlattr *tb)
6525{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006526 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6527 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6528 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006529 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006530 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6531 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006532 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006533 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006534 int new_channels = 0;
6535 struct hostapd_channel_data *channel;
6536 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006537 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006538 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006539
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006540 if (tb == NULL)
6541 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006543 nla_for_each_nested(nl_freq, tb, rem_freq) {
6544 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6545 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6546 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6547 continue;
6548 new_channels++;
6549 }
6550
6551 channel = os_realloc_array(mode->channels,
6552 mode->num_channels + new_channels,
6553 sizeof(struct hostapd_channel_data));
6554 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555 return NL_SKIP;
6556
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006557 mode->channels = channel;
6558 mode->num_channels += new_channels;
6559
6560 idx = phy_info->last_chan_idx;
6561
6562 nla_for_each_nested(nl_freq, tb, rem_freq) {
6563 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6564 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6565 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6566 continue;
6567 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6568 idx++;
6569 }
6570 phy_info->last_chan_idx = idx;
6571
6572 return NL_OK;
6573}
6574
6575
6576static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6577{
6578 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6579 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6580 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6581 { .type = NLA_FLAG },
6582 };
6583 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6584 struct nlattr *nl_rate;
6585 int rem_rate, idx;
6586
6587 if (tb == NULL)
6588 return NL_OK;
6589
6590 nla_for_each_nested(nl_rate, tb, rem_rate) {
6591 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6592 nla_data(nl_rate), nla_len(nl_rate),
6593 rate_policy);
6594 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6595 continue;
6596 mode->num_rates++;
6597 }
6598
6599 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6600 if (!mode->rates)
6601 return NL_SKIP;
6602
6603 idx = 0;
6604
6605 nla_for_each_nested(nl_rate, tb, rem_rate) {
6606 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6607 nla_data(nl_rate), nla_len(nl_rate),
6608 rate_policy);
6609 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6610 continue;
6611 mode->rates[idx] = nla_get_u32(
6612 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006613 idx++;
6614 }
6615
6616 return NL_OK;
6617}
6618
6619
6620static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6621{
6622 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6623 struct hostapd_hw_modes *mode;
6624 int ret;
6625
6626 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006627 mode = os_realloc_array(phy_info->modes,
6628 *phy_info->num_modes + 1,
6629 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006630 if (!mode)
6631 return NL_SKIP;
6632 phy_info->modes = mode;
6633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006634 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006635 os_memset(mode, 0, sizeof(*mode));
6636 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006637 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6638 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6639
6640 /*
6641 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6642 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6643 * possible streams as unsupported. This will be overridden if
6644 * driver advertises VHT support.
6645 */
6646 mode->vht_mcs_set[0] = 0xff;
6647 mode->vht_mcs_set[1] = 0xff;
6648 mode->vht_mcs_set[4] = 0xff;
6649 mode->vht_mcs_set[5] = 0xff;
6650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006651 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006652 phy_info->last_mode = nl_band->nla_type;
6653 phy_info->last_chan_idx = 0;
6654 } else
6655 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006656
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006657 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6658 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006660 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6661 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6662 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6663 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6664 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6665 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6666 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6667 if (ret != NL_OK)
6668 return ret;
6669 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6670 if (ret != NL_OK)
6671 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006672
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006673 return NL_OK;
6674}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006677static int phy_info_handler(struct nl_msg *msg, void *arg)
6678{
6679 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6680 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6681 struct phy_info_arg *phy_info = arg;
6682 struct nlattr *nl_band;
6683 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006685 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6686 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006687
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006688 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6689 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006690
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006691 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6692 {
6693 int res = phy_info_band(phy_info, nl_band);
6694 if (res != NL_OK)
6695 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006696 }
6697
6698 return NL_SKIP;
6699}
6700
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006702static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006703wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6704 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006705{
6706 u16 m;
6707 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6708 int i, mode11g_idx = -1;
6709
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006710 /* heuristic to set up modes */
6711 for (m = 0; m < *num_modes; m++) {
6712 if (!modes[m].num_channels)
6713 continue;
6714 if (modes[m].channels[0].freq < 4000) {
6715 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6716 for (i = 0; i < modes[m].num_rates; i++) {
6717 if (modes[m].rates[i] > 200) {
6718 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6719 break;
6720 }
6721 }
6722 } else if (modes[m].channels[0].freq > 50000)
6723 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6724 else
6725 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6726 }
6727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006728 /* If only 802.11g mode is included, use it to construct matching
6729 * 802.11b mode data. */
6730
6731 for (m = 0; m < *num_modes; m++) {
6732 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6733 return modes; /* 802.11b already included */
6734 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6735 mode11g_idx = m;
6736 }
6737
6738 if (mode11g_idx < 0)
6739 return modes; /* 2.4 GHz band not supported at all */
6740
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006741 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006742 if (nmodes == NULL)
6743 return modes; /* Could not add 802.11b mode */
6744
6745 mode = &nmodes[*num_modes];
6746 os_memset(mode, 0, sizeof(*mode));
6747 (*num_modes)++;
6748 modes = nmodes;
6749
6750 mode->mode = HOSTAPD_MODE_IEEE80211B;
6751
6752 mode11g = &modes[mode11g_idx];
6753 mode->num_channels = mode11g->num_channels;
6754 mode->channels = os_malloc(mode11g->num_channels *
6755 sizeof(struct hostapd_channel_data));
6756 if (mode->channels == NULL) {
6757 (*num_modes)--;
6758 return modes; /* Could not add 802.11b mode */
6759 }
6760 os_memcpy(mode->channels, mode11g->channels,
6761 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6762
6763 mode->num_rates = 0;
6764 mode->rates = os_malloc(4 * sizeof(int));
6765 if (mode->rates == NULL) {
6766 os_free(mode->channels);
6767 (*num_modes)--;
6768 return modes; /* Could not add 802.11b mode */
6769 }
6770
6771 for (i = 0; i < mode11g->num_rates; i++) {
6772 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6773 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6774 continue;
6775 mode->rates[mode->num_rates] = mode11g->rates[i];
6776 mode->num_rates++;
6777 if (mode->num_rates == 4)
6778 break;
6779 }
6780
6781 if (mode->num_rates == 0) {
6782 os_free(mode->channels);
6783 os_free(mode->rates);
6784 (*num_modes)--;
6785 return modes; /* No 802.11b rates */
6786 }
6787
6788 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6789 "information");
6790
6791 return modes;
6792}
6793
6794
6795static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6796 int end)
6797{
6798 int c;
6799
6800 for (c = 0; c < mode->num_channels; c++) {
6801 struct hostapd_channel_data *chan = &mode->channels[c];
6802 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6803 chan->flag |= HOSTAPD_CHAN_HT40;
6804 }
6805}
6806
6807
6808static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6809 int end)
6810{
6811 int c;
6812
6813 for (c = 0; c < mode->num_channels; c++) {
6814 struct hostapd_channel_data *chan = &mode->channels[c];
6815 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6816 continue;
6817 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6818 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6819 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6820 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6821 }
6822}
6823
6824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006825static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006826 struct phy_info_arg *results)
6827{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006828 u16 m;
6829
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006830 for (m = 0; m < *results->num_modes; m++) {
6831 int c;
6832 struct hostapd_hw_modes *mode = &results->modes[m];
6833
6834 for (c = 0; c < mode->num_channels; c++) {
6835 struct hostapd_channel_data *chan = &mode->channels[c];
6836 if ((u32) chan->freq - 10 >= start &&
6837 (u32) chan->freq + 10 <= end)
6838 chan->max_tx_power = max_eirp;
6839 }
6840 }
6841}
6842
6843
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006844static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006845 struct phy_info_arg *results)
6846{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006847 u16 m;
6848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849 for (m = 0; m < *results->num_modes; m++) {
6850 if (!(results->modes[m].ht_capab &
6851 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6852 continue;
6853 nl80211_set_ht40_mode(&results->modes[m], start, end);
6854 }
6855}
6856
6857
6858static void nl80211_reg_rule_sec(struct nlattr *tb[],
6859 struct phy_info_arg *results)
6860{
6861 u32 start, end, max_bw;
6862 u16 m;
6863
6864 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6865 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6866 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6867 return;
6868
6869 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6870 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6871 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6872
6873 if (max_bw < 20)
6874 return;
6875
6876 for (m = 0; m < *results->num_modes; m++) {
6877 if (!(results->modes[m].ht_capab &
6878 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6879 continue;
6880 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6881 }
6882}
6883
6884
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006885static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6886 int end)
6887{
6888 int c;
6889
6890 for (c = 0; c < mode->num_channels; c++) {
6891 struct hostapd_channel_data *chan = &mode->channels[c];
6892 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6893 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6894
6895 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6896 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6897
6898 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6899 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6900
6901 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6902 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6903 }
6904}
6905
6906
6907static void nl80211_reg_rule_vht(struct nlattr *tb[],
6908 struct phy_info_arg *results)
6909{
6910 u32 start, end, max_bw;
6911 u16 m;
6912
6913 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6914 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6915 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6916 return;
6917
6918 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6919 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6920 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6921
6922 if (max_bw < 80)
6923 return;
6924
6925 for (m = 0; m < *results->num_modes; m++) {
6926 if (!(results->modes[m].ht_capab &
6927 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6928 continue;
6929 /* TODO: use a real VHT support indication */
6930 if (!results->modes[m].vht_capab)
6931 continue;
6932
6933 nl80211_set_vht_mode(&results->modes[m], start, end);
6934 }
6935}
6936
6937
Dmitry Shmidt97672262014-02-03 13:02:54 -08006938static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6939{
6940 switch (region) {
6941 case NL80211_DFS_UNSET:
6942 return "DFS-UNSET";
6943 case NL80211_DFS_FCC:
6944 return "DFS-FCC";
6945 case NL80211_DFS_ETSI:
6946 return "DFS-ETSI";
6947 case NL80211_DFS_JP:
6948 return "DFS-JP";
6949 default:
6950 return "DFS-invalid";
6951 }
6952}
6953
6954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6956{
6957 struct phy_info_arg *results = arg;
6958 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6959 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6960 struct nlattr *nl_rule;
6961 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6962 int rem_rule;
6963 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6964 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6965 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6966 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6967 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6968 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6969 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6970 };
6971
6972 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6973 genlmsg_attrlen(gnlh, 0), NULL);
6974 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6975 !tb_msg[NL80211_ATTR_REG_RULES]) {
6976 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6977 "available");
6978 return NL_SKIP;
6979 }
6980
Dmitry Shmidt97672262014-02-03 13:02:54 -08006981 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6982 enum nl80211_dfs_regions dfs_domain;
6983 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6984 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6985 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6986 dfs_domain_name(dfs_domain));
6987 } else {
6988 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6989 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991
6992 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6993 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006994 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6996 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006997 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6998 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6999 continue;
7000 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
7001 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
7002 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7003 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
7004 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7005 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007006 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
7007 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007008
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007009 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
7010 start, end, max_bw, max_eirp,
7011 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
7012 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
7013 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
7014 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
7015 "",
7016 flags & NL80211_RRF_DFS ? " (DFS)" : "",
7017 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
7018 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
7019 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007020 if (max_bw >= 40)
7021 nl80211_reg_rule_ht40(start, end, results);
7022 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7023 nl80211_reg_rule_max_eirp(start, end, max_eirp,
7024 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007025 }
7026
7027 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7028 {
7029 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7030 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
7031 nl80211_reg_rule_sec(tb_rule, results);
7032 }
7033
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007034 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7035 {
7036 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7037 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
7038 nl80211_reg_rule_vht(tb_rule, results);
7039 }
7040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007041 return NL_SKIP;
7042}
7043
7044
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007045static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
7046 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047{
7048 struct nl_msg *msg;
7049
7050 msg = nlmsg_alloc();
7051 if (!msg)
7052 return -ENOMEM;
7053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007054 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007055 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
7056}
7057
7058
7059static struct hostapd_hw_modes *
7060wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
7061{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007062 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007063 struct i802_bss *bss = priv;
7064 struct wpa_driver_nl80211_data *drv = bss->drv;
7065 struct nl_msg *msg;
7066 struct phy_info_arg result = {
7067 .num_modes = num_modes,
7068 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007069 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 };
7071
7072 *num_modes = 0;
7073 *flags = 0;
7074
7075 msg = nlmsg_alloc();
7076 if (!msg)
7077 return NULL;
7078
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007079 feat = get_nl80211_protocol_features(drv);
7080 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
7081 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
7082 else
7083 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007085 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007086 if (nl80211_set_iface_id(msg, bss) < 0)
7087 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088
7089 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007090 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007091 return wpa_driver_nl80211_postprocess_modes(result.modes,
7092 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007093 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007094 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007095 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007096 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007097 return NULL;
7098}
7099
7100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007101static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
7102 const void *data, size_t len,
7103 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007104{
7105 __u8 rtap_hdr[] = {
7106 0x00, 0x00, /* radiotap version */
7107 0x0e, 0x00, /* radiotap length */
7108 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
7109 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
7110 0x00, /* padding */
7111 0x00, 0x00, /* RX and TX flags to indicate that */
7112 0x00, 0x00, /* this is the injected frame directly */
7113 };
7114 struct iovec iov[2] = {
7115 {
7116 .iov_base = &rtap_hdr,
7117 .iov_len = sizeof(rtap_hdr),
7118 },
7119 {
7120 .iov_base = (void *) data,
7121 .iov_len = len,
7122 }
7123 };
7124 struct msghdr msg = {
7125 .msg_name = NULL,
7126 .msg_namelen = 0,
7127 .msg_iov = iov,
7128 .msg_iovlen = 2,
7129 .msg_control = NULL,
7130 .msg_controllen = 0,
7131 .msg_flags = 0,
7132 };
7133 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007134 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007135
7136 if (encrypt)
7137 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
7138
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007139 if (drv->monitor_sock < 0) {
7140 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
7141 "for %s", __func__);
7142 return -1;
7143 }
7144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007145 if (noack)
7146 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007147 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007149 res = sendmsg(drv->monitor_sock, &msg, 0);
7150 if (res < 0) {
7151 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
7152 return -1;
7153 }
7154 return 0;
7155}
7156
7157
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007158static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
7159 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007160 int encrypt, int noack,
7161 unsigned int freq, int no_cck,
7162 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007163{
7164 struct wpa_driver_nl80211_data *drv = bss->drv;
7165 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007166 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007167
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007168 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
7169 freq = nl80211_get_assoc_freq(drv);
7170 wpa_printf(MSG_DEBUG,
7171 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
7172 freq);
7173 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007174 if (freq == 0) {
7175 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
7176 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007177 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007178 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007179
Dmitry Shmidt56052862013-10-04 10:23:25 -07007180 if (drv->use_monitor) {
7181 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7182 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007183 return wpa_driver_nl80211_send_mntr(drv, data, len,
7184 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007185 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007186
Dmitry Shmidt56052862013-10-04 10:23:25 -07007187 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07007188 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7189 &cookie, no_cck, noack, offchanok);
7190 if (res == 0 && !noack) {
7191 const struct ieee80211_mgmt *mgmt;
7192 u16 fc;
7193
7194 mgmt = (const struct ieee80211_mgmt *) data;
7195 fc = le_to_host16(mgmt->frame_control);
7196 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7197 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7198 wpa_printf(MSG_MSGDUMP,
7199 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7200 (long long unsigned int)
7201 drv->send_action_cookie,
7202 (long long unsigned int) cookie);
7203 drv->send_action_cookie = cookie;
7204 }
7205 }
7206
7207 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007208}
7209
7210
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007211static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7212 size_t data_len, int noack,
7213 unsigned int freq, int no_cck,
7214 int offchanok,
7215 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007216{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007217 struct wpa_driver_nl80211_data *drv = bss->drv;
7218 struct ieee80211_mgmt *mgmt;
7219 int encrypt = 1;
7220 u16 fc;
7221
7222 mgmt = (struct ieee80211_mgmt *) data;
7223 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007224 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
7225 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
7226 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
7227 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007228
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007229 if ((is_sta_interface(drv->nlmode) ||
7230 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7232 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7233 /*
7234 * The use of last_mgmt_freq is a bit of a hack,
7235 * but it works due to the single-threaded nature
7236 * of wpa_supplicant.
7237 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007238 if (freq == 0) {
7239 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7240 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007241 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007242 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007243 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007244 data, data_len, NULL, 1, noack,
7245 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007246 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007247
7248 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007249 if (freq == 0) {
7250 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7251 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007252 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007253 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007254 return nl80211_send_frame_cmd(bss, freq,
7255 (int) freq == bss->freq ? 0 :
7256 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007257 data, data_len,
7258 &drv->send_action_cookie,
7259 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007260 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007262 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7263 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7264 /*
7265 * Only one of the authentication frame types is encrypted.
7266 * In order for static WEP encryption to work properly (i.e.,
7267 * to not encrypt the frame), we need to tell mac80211 about
7268 * the frames that must not be encrypted.
7269 */
7270 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7271 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7272 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7273 encrypt = 0;
7274 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007275
Dmitry Shmidt56052862013-10-04 10:23:25 -07007276 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007277 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007278 noack, freq, no_cck, offchanok,
7279 wait_time);
7280}
7281
7282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007283static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7284 int slot, int ht_opmode, int ap_isolate,
7285 int *basic_rates)
7286{
7287 struct wpa_driver_nl80211_data *drv = bss->drv;
7288 struct nl_msg *msg;
7289
7290 msg = nlmsg_alloc();
7291 if (!msg)
7292 return -ENOMEM;
7293
7294 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7295
7296 if (cts >= 0)
7297 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7298 if (preamble >= 0)
7299 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7300 if (slot >= 0)
7301 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7302 if (ht_opmode >= 0)
7303 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7304 if (ap_isolate >= 0)
7305 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7306
7307 if (basic_rates) {
7308 u8 rates[NL80211_MAX_SUPP_RATES];
7309 u8 rates_len = 0;
7310 int i;
7311
7312 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7313 i++)
7314 rates[rates_len++] = basic_rates[i] / 5;
7315
7316 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7317 }
7318
7319 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7320
7321 return send_and_recv_msgs(drv, msg, NULL, NULL);
7322 nla_put_failure:
7323 nlmsg_free(msg);
7324 return -ENOBUFS;
7325}
7326
7327
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007328static int wpa_driver_nl80211_set_acl(void *priv,
7329 struct hostapd_acl_params *params)
7330{
7331 struct i802_bss *bss = priv;
7332 struct wpa_driver_nl80211_data *drv = bss->drv;
7333 struct nl_msg *msg;
7334 struct nlattr *acl;
7335 unsigned int i;
7336 int ret = 0;
7337
7338 if (!(drv->capa.max_acl_mac_addrs))
7339 return -ENOTSUP;
7340
7341 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7342 return -ENOTSUP;
7343
7344 msg = nlmsg_alloc();
7345 if (!msg)
7346 return -ENOMEM;
7347
7348 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7349 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7350
7351 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7352
7353 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7354
7355 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7356 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7357 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7358
7359 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7360 if (acl == NULL)
7361 goto nla_put_failure;
7362
7363 for (i = 0; i < params->num_mac_acl; i++)
7364 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7365
7366 nla_nest_end(msg, acl);
7367
7368 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7369 msg = NULL;
7370 if (ret) {
7371 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7372 ret, strerror(-ret));
7373 }
7374
7375nla_put_failure:
7376 nlmsg_free(msg);
7377
7378 return ret;
7379}
7380
7381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007382static int wpa_driver_nl80211_set_ap(void *priv,
7383 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007384{
7385 struct i802_bss *bss = priv;
7386 struct wpa_driver_nl80211_data *drv = bss->drv;
7387 struct nl_msg *msg;
7388 u8 cmd = NL80211_CMD_NEW_BEACON;
7389 int ret;
7390 int beacon_set;
7391 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007392 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007393 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007394 u32 ver;
7395
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007396 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397
7398 msg = nlmsg_alloc();
7399 if (!msg)
7400 return -ENOMEM;
7401
7402 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7403 beacon_set);
7404 if (beacon_set)
7405 cmd = NL80211_CMD_SET_BEACON;
7406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007407 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007408 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7409 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007410 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007411 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7412 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007413 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007414 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007415 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007416 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007417 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007418 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007419 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007420 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7421 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007422 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7423 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007424 if (params->proberesp && params->proberesp_len) {
7425 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7426 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007427 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7428 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007429 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007430 switch (params->hide_ssid) {
7431 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007432 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007433 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7434 NL80211_HIDDEN_SSID_NOT_IN_USE);
7435 break;
7436 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007437 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007438 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7439 NL80211_HIDDEN_SSID_ZERO_LEN);
7440 break;
7441 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007442 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007443 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7444 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7445 break;
7446 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007447 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007448 if (params->privacy)
7449 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007450 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007451 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7452 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7453 /* Leave out the attribute */
7454 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7455 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7456 NL80211_AUTHTYPE_SHARED_KEY);
7457 else
7458 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7459 NL80211_AUTHTYPE_OPEN_SYSTEM);
7460
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007461 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007462 ver = 0;
7463 if (params->wpa_version & WPA_PROTO_WPA)
7464 ver |= NL80211_WPA_VERSION_1;
7465 if (params->wpa_version & WPA_PROTO_RSN)
7466 ver |= NL80211_WPA_VERSION_2;
7467 if (ver)
7468 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7469
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007470 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7471 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007472 num_suites = 0;
7473 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7474 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7475 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7476 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7477 if (num_suites) {
7478 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7479 num_suites * sizeof(u32), suites);
7480 }
7481
7482 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7483 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7484 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7485
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007486 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7487 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007488 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7489 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007490 if (num_suites) {
7491 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7492 num_suites * sizeof(u32), suites);
7493 }
7494
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007495 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7496 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007497 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7498 if (suite)
7499 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007500
7501 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007502 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7503 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007504 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7505 wpabuf_head(params->beacon_ies));
7506 }
7507 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007508 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7509 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007510 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7511 wpabuf_len(params->proberesp_ies),
7512 wpabuf_head(params->proberesp_ies));
7513 }
7514 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007515 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7516 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007517 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7518 wpabuf_len(params->assocresp_ies),
7519 wpabuf_head(params->assocresp_ies));
7520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521
Dmitry Shmidt04949592012-07-19 12:16:46 -07007522 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007523 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7524 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007525 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7526 params->ap_max_inactivity);
7527 }
7528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007529 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7530 if (ret) {
7531 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7532 ret, strerror(-ret));
7533 } else {
7534 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007535 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7536 params->short_slot_time, params->ht_opmode,
7537 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007538 if (beacon_set && params->freq &&
7539 params->freq->bandwidth != bss->bandwidth) {
7540 wpa_printf(MSG_DEBUG,
7541 "nl80211: Update BSS %s bandwidth: %d -> %d",
7542 bss->ifname, bss->bandwidth,
7543 params->freq->bandwidth);
7544 ret = nl80211_set_channel(bss, params->freq, 1);
7545 if (ret) {
7546 wpa_printf(MSG_DEBUG,
7547 "nl80211: Frequency set failed: %d (%s)",
7548 ret, strerror(-ret));
7549 } else {
7550 wpa_printf(MSG_DEBUG,
7551 "nl80211: Frequency set succeeded for ht2040 coex");
7552 bss->bandwidth = params->freq->bandwidth;
7553 }
7554 } else if (!beacon_set) {
7555 /*
7556 * cfg80211 updates the driver on frequence change in AP
7557 * mode only at the point when beaconing is started, so
7558 * set the initial value here.
7559 */
7560 bss->bandwidth = params->freq->bandwidth;
7561 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007562 }
7563 return ret;
7564 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007565 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007566 return -ENOBUFS;
7567}
7568
7569
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007570static int nl80211_put_freq_params(struct nl_msg *msg,
7571 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007572{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007573 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7574 if (freq->vht_enabled) {
7575 switch (freq->bandwidth) {
7576 case 20:
7577 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7578 NL80211_CHAN_WIDTH_20);
7579 break;
7580 case 40:
7581 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7582 NL80211_CHAN_WIDTH_40);
7583 break;
7584 case 80:
7585 if (freq->center_freq2)
7586 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7587 NL80211_CHAN_WIDTH_80P80);
7588 else
7589 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7590 NL80211_CHAN_WIDTH_80);
7591 break;
7592 case 160:
7593 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7594 NL80211_CHAN_WIDTH_160);
7595 break;
7596 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007597 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007598 }
7599 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7600 if (freq->center_freq2)
7601 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7602 freq->center_freq2);
7603 } else if (freq->ht_enabled) {
7604 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007605 case -1:
7606 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7607 NL80211_CHAN_HT40MINUS);
7608 break;
7609 case 1:
7610 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7611 NL80211_CHAN_HT40PLUS);
7612 break;
7613 default:
7614 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7615 NL80211_CHAN_HT20);
7616 break;
7617 }
7618 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007619 return 0;
7620
7621nla_put_failure:
7622 return -ENOBUFS;
7623}
7624
7625
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007626static int nl80211_set_channel(struct i802_bss *bss,
7627 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007628{
7629 struct wpa_driver_nl80211_data *drv = bss->drv;
7630 struct nl_msg *msg;
7631 int ret;
7632
7633 wpa_printf(MSG_DEBUG,
7634 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7635 freq->freq, freq->ht_enabled, freq->vht_enabled,
7636 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7637 msg = nlmsg_alloc();
7638 if (!msg)
7639 return -1;
7640
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007641 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7642 NL80211_CMD_SET_WIPHY);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007643
7644 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7645 if (nl80211_put_freq_params(msg, freq) < 0)
7646 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647
7648 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007649 msg = NULL;
7650 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007651 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007652 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007654 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007655 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007656nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007657 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007658 return -1;
7659}
7660
7661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007662static u32 sta_flags_nl80211(int flags)
7663{
7664 u32 f = 0;
7665
7666 if (flags & WPA_STA_AUTHORIZED)
7667 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7668 if (flags & WPA_STA_WMM)
7669 f |= BIT(NL80211_STA_FLAG_WME);
7670 if (flags & WPA_STA_SHORT_PREAMBLE)
7671 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7672 if (flags & WPA_STA_MFP)
7673 f |= BIT(NL80211_STA_FLAG_MFP);
7674 if (flags & WPA_STA_TDLS_PEER)
7675 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7676
7677 return f;
7678}
7679
7680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681static int wpa_driver_nl80211_sta_add(void *priv,
7682 struct hostapd_sta_add_params *params)
7683{
7684 struct i802_bss *bss = priv;
7685 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007686 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007687 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688 int ret = -ENOBUFS;
7689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007690 if ((params->flags & WPA_STA_TDLS_PEER) &&
7691 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7692 return -EOPNOTSUPP;
7693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007694 msg = nlmsg_alloc();
7695 if (!msg)
7696 return -ENOMEM;
7697
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007698 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7699 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007700 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7701 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007702
7703 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7704 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007705 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7706 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007707 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7708 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007709 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007710 if (params->aid) {
7711 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7712 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7713 } else {
7714 /*
7715 * cfg80211 validates that AID is non-zero, so we have
7716 * to make this a non-zero value for the TDLS case where
7717 * a dummy STA entry is used for now.
7718 */
7719 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7720 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7721 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007722 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7723 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007724 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7725 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007726 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7727 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7728 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007729 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007730 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007731 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7732 (u8 *) params->ht_capabilities,
7733 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007734 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7735 sizeof(*params->ht_capabilities),
7736 params->ht_capabilities);
7737 }
7738
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007739 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007740 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7741 (u8 *) params->vht_capabilities,
7742 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007743 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7744 sizeof(*params->vht_capabilities),
7745 params->vht_capabilities);
7746 }
7747
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007748 if (params->vht_opmode_enabled) {
7749 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7750 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7751 params->vht_opmode);
7752 }
7753
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007754 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7755 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7756
7757 if (params->ext_capab) {
7758 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7759 params->ext_capab, params->ext_capab_len);
7760 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7761 params->ext_capab_len, params->ext_capab);
7762 }
7763
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007764 if (params->supp_channels) {
7765 wpa_hexdump(MSG_DEBUG, " * supported channels",
7766 params->supp_channels, params->supp_channels_len);
7767 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7768 params->supp_channels_len, params->supp_channels);
7769 }
7770
7771 if (params->supp_oper_classes) {
7772 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7773 params->supp_oper_classes,
7774 params->supp_oper_classes_len);
7775 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7776 params->supp_oper_classes_len,
7777 params->supp_oper_classes);
7778 }
7779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007780 os_memset(&upd, 0, sizeof(upd));
7781 upd.mask = sta_flags_nl80211(params->flags);
7782 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007783 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7784 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007785 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7786
7787 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007788 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007790 if (!wme)
7791 goto nla_put_failure;
7792
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007793 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007794 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007795 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007796 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007797 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007798 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007799 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007800 }
7801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007802 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007803 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007804 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007805 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7806 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7807 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007808 if (ret == -EEXIST)
7809 ret = 0;
7810 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007811 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 return ret;
7813}
7814
7815
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007816static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
7817{
7818#ifdef CONFIG_LIBNL3_ROUTE
7819 struct wpa_driver_nl80211_data *drv = bss->drv;
7820 struct rtnl_neigh *rn;
7821 struct nl_addr *nl_addr;
7822 int err;
7823
7824 rn = rtnl_neigh_alloc();
7825 if (!rn)
7826 return;
7827
7828 rtnl_neigh_set_family(rn, AF_BRIDGE);
7829 rtnl_neigh_set_ifindex(rn, bss->ifindex);
7830 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
7831 if (!nl_addr) {
7832 rtnl_neigh_put(rn);
7833 return;
7834 }
7835 rtnl_neigh_set_lladdr(rn, nl_addr);
7836
7837 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
7838 if (err < 0) {
7839 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
7840 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
7841 bss->ifindex, nl_geterror(err));
7842 } else {
7843 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
7844 MACSTR, MAC2STR(addr));
7845 }
7846
7847 nl_addr_put(nl_addr);
7848 rtnl_neigh_put(rn);
7849#endif /* CONFIG_LIBNL3_ROUTE */
7850}
7851
7852
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007853static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007854{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007855 struct wpa_driver_nl80211_data *drv = bss->drv;
7856 struct nl_msg *msg;
7857 int ret;
7858
7859 msg = nlmsg_alloc();
7860 if (!msg)
7861 return -ENOMEM;
7862
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007863 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007864
7865 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7866 if_nametoindex(bss->ifname));
7867 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7868
7869 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007870 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7871 " --> %d (%s)",
7872 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007873
7874 if (drv->rtnl_sk)
7875 rtnl_neigh_delete_fdb_entry(bss, addr);
7876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007877 if (ret == -ENOENT)
7878 return 0;
7879 return ret;
7880 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007881 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007882 return -ENOBUFS;
7883}
7884
7885
7886static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7887 int ifidx)
7888{
7889 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007890 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007891
7892 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007895 dl_list_for_each(drv2, &drv->global->interfaces,
7896 struct wpa_driver_nl80211_data, list)
7897 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007898
7899 msg = nlmsg_alloc();
7900 if (!msg)
7901 goto nla_put_failure;
7902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007903 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007904 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7905
7906 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7907 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007908 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007909 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007910 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7912}
7913
7914
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007915static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7916{
7917 switch (mode) {
7918 case NL80211_IFTYPE_ADHOC:
7919 return "ADHOC";
7920 case NL80211_IFTYPE_STATION:
7921 return "STATION";
7922 case NL80211_IFTYPE_AP:
7923 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007924 case NL80211_IFTYPE_AP_VLAN:
7925 return "AP_VLAN";
7926 case NL80211_IFTYPE_WDS:
7927 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007928 case NL80211_IFTYPE_MONITOR:
7929 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007930 case NL80211_IFTYPE_MESH_POINT:
7931 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007932 case NL80211_IFTYPE_P2P_CLIENT:
7933 return "P2P_CLIENT";
7934 case NL80211_IFTYPE_P2P_GO:
7935 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007936 case NL80211_IFTYPE_P2P_DEVICE:
7937 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007938 default:
7939 return "unknown";
7940 }
7941}
7942
7943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007944static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7945 const char *ifname,
7946 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007947 const u8 *addr, int wds,
7948 int (*handler)(struct nl_msg *, void *),
7949 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007951 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007952 int ifidx;
7953 int ret = -ENOBUFS;
7954
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007955 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7956 iftype, nl80211_iftype_str(iftype));
7957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007958 msg = nlmsg_alloc();
7959 if (!msg)
7960 return -1;
7961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007962 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007963 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007964 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007965 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7966 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7967
7968 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007969 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007970
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007971 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007972 if (!flags)
7973 goto nla_put_failure;
7974
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007975 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007976
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007977 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007978 } else if (wds) {
7979 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7980 }
7981
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007982 /*
7983 * Tell cfg80211 that the interface belongs to the socket that created
7984 * it, and the interface should be deleted when the socket is closed.
7985 */
7986 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
7987
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007988 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007989 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007990 if (ret) {
7991 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007992 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007993 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7994 ifname, ret, strerror(-ret));
7995 return ret;
7996 }
7997
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007998 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7999 return 0;
8000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008001 ifidx = if_nametoindex(ifname);
8002 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
8003 ifname, ifidx);
8004
8005 if (ifidx <= 0)
8006 return -1;
8007
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07008008 /*
8009 * Some virtual interfaces need to process EAPOL packets and events on
8010 * the parent interface. This is used mainly with hostapd.
8011 */
8012 if (drv->hostapd ||
8013 iftype == NL80211_IFTYPE_AP_VLAN ||
8014 iftype == NL80211_IFTYPE_WDS ||
8015 iftype == NL80211_IFTYPE_MONITOR) {
8016 /* start listening for EAPOL on this interface */
8017 add_ifidx(drv, ifidx);
8018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008019
8020 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008021 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022 nl80211_remove_iface(drv, ifidx);
8023 return -1;
8024 }
8025
8026 return ifidx;
8027}
8028
8029
8030static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
8031 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008032 const u8 *addr, int wds,
8033 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008034 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008035{
8036 int ret;
8037
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008038 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
8039 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008041 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008042 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008043 if (use_existing) {
8044 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
8045 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008046 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
8047 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8048 addr) < 0 &&
8049 (linux_set_iface_flags(drv->global->ioctl_sock,
8050 ifname, 0) < 0 ||
8051 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8052 addr) < 0 ||
8053 linux_set_iface_flags(drv->global->ioctl_sock,
8054 ifname, 1) < 0))
8055 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008056 return -ENFILE;
8057 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008058 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
8059
8060 /* Try to remove the interface that was already there. */
8061 nl80211_remove_iface(drv, if_nametoindex(ifname));
8062
8063 /* Try to create the interface again */
8064 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008065 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008066 }
8067
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008068 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008069 nl80211_disable_11b_rates(drv, ret, 1);
8070
8071 return ret;
8072}
8073
8074
8075static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
8076{
8077 struct ieee80211_hdr *hdr;
8078 u16 fc;
8079 union wpa_event_data event;
8080
8081 hdr = (struct ieee80211_hdr *) buf;
8082 fc = le_to_host16(hdr->frame_control);
8083
8084 os_memset(&event, 0, sizeof(event));
8085 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
8086 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
8087 event.tx_status.dst = hdr->addr1;
8088 event.tx_status.data = buf;
8089 event.tx_status.data_len = len;
8090 event.tx_status.ack = ok;
8091 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
8092}
8093
8094
8095static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
8096 u8 *buf, size_t len)
8097{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008098 struct ieee80211_hdr *hdr = (void *)buf;
8099 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008100 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008101
8102 if (len < sizeof(*hdr))
8103 return;
8104
8105 fc = le_to_host16(hdr->frame_control);
8106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008107 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008108 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
8109 event.rx_from_unknown.addr = hdr->addr2;
8110 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
8111 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008112 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
8113}
8114
8115
8116static void handle_frame(struct wpa_driver_nl80211_data *drv,
8117 u8 *buf, size_t len, int datarate, int ssi_signal)
8118{
8119 struct ieee80211_hdr *hdr;
8120 u16 fc;
8121 union wpa_event_data event;
8122
8123 hdr = (struct ieee80211_hdr *) buf;
8124 fc = le_to_host16(hdr->frame_control);
8125
8126 switch (WLAN_FC_GET_TYPE(fc)) {
8127 case WLAN_FC_TYPE_MGMT:
8128 os_memset(&event, 0, sizeof(event));
8129 event.rx_mgmt.frame = buf;
8130 event.rx_mgmt.frame_len = len;
8131 event.rx_mgmt.datarate = datarate;
8132 event.rx_mgmt.ssi_signal = ssi_signal;
8133 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
8134 break;
8135 case WLAN_FC_TYPE_CTRL:
8136 /* can only get here with PS-Poll frames */
8137 wpa_printf(MSG_DEBUG, "CTRL");
8138 from_unknown_sta(drv, buf, len);
8139 break;
8140 case WLAN_FC_TYPE_DATA:
8141 from_unknown_sta(drv, buf, len);
8142 break;
8143 }
8144}
8145
8146
8147static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
8148{
8149 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8150 int len;
8151 unsigned char buf[3000];
8152 struct ieee80211_radiotap_iterator iter;
8153 int ret;
8154 int datarate = 0, ssi_signal = 0;
8155 int injected = 0, failed = 0, rxflags = 0;
8156
8157 len = recv(sock, buf, sizeof(buf), 0);
8158 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008159 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
8160 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008161 return;
8162 }
8163
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008164 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008165 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008166 return;
8167 }
8168
8169 while (1) {
8170 ret = ieee80211_radiotap_iterator_next(&iter);
8171 if (ret == -ENOENT)
8172 break;
8173 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008174 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
8175 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008176 return;
8177 }
8178 switch (iter.this_arg_index) {
8179 case IEEE80211_RADIOTAP_FLAGS:
8180 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
8181 len -= 4;
8182 break;
8183 case IEEE80211_RADIOTAP_RX_FLAGS:
8184 rxflags = 1;
8185 break;
8186 case IEEE80211_RADIOTAP_TX_FLAGS:
8187 injected = 1;
8188 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
8189 IEEE80211_RADIOTAP_F_TX_FAIL;
8190 break;
8191 case IEEE80211_RADIOTAP_DATA_RETRIES:
8192 break;
8193 case IEEE80211_RADIOTAP_CHANNEL:
8194 /* TODO: convert from freq/flags to channel number */
8195 break;
8196 case IEEE80211_RADIOTAP_RATE:
8197 datarate = *iter.this_arg * 5;
8198 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008199 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
8200 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008201 break;
8202 }
8203 }
8204
8205 if (rxflags && injected)
8206 return;
8207
8208 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008209 handle_frame(drv, buf + iter._max_length,
8210 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008211 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008212 handle_tx_callback(drv->ctx, buf + iter._max_length,
8213 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008214}
8215
8216
8217/*
8218 * we post-process the filter code later and rewrite
8219 * this to the offset to the last instruction
8220 */
8221#define PASS 0xFF
8222#define FAIL 0xFE
8223
8224static struct sock_filter msock_filter_insns[] = {
8225 /*
8226 * do a little-endian load of the radiotap length field
8227 */
8228 /* load lower byte into A */
8229 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
8230 /* put it into X (== index register) */
8231 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8232 /* load upper byte into A */
8233 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8234 /* left-shift it by 8 */
8235 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8236 /* or with X */
8237 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8238 /* put result into X */
8239 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8240
8241 /*
8242 * Allow management frames through, this also gives us those
8243 * management frames that we sent ourselves with status
8244 */
8245 /* load the lower byte of the IEEE 802.11 frame control field */
8246 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8247 /* mask off frame type and version */
8248 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8249 /* accept frame if it's both 0, fall through otherwise */
8250 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8251
8252 /*
8253 * TODO: add a bit to radiotap RX flags that indicates
8254 * that the sending station is not associated, then
8255 * add a filter here that filters on our DA and that flag
8256 * to allow us to deauth frames to that bad station.
8257 *
8258 * For now allow all To DS data frames through.
8259 */
8260 /* load the IEEE 802.11 frame control field */
8261 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8262 /* mask off frame type, version and DS status */
8263 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8264 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8265 */
8266 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
8267
8268#if 0
8269 /*
8270 * drop non-data frames
8271 */
8272 /* load the lower byte of the frame control field */
8273 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8274 /* mask off QoS bit */
8275 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8276 /* drop non-data frames */
8277 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
8278#endif
8279 /* load the upper byte of the frame control field */
8280 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
8281 /* mask off toDS/fromDS */
8282 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
8283 /* accept WDS frames */
8284 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
8285
8286 /*
8287 * add header length to index
8288 */
8289 /* load the lower byte of the frame control field */
8290 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8291 /* mask off QoS bit */
8292 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8293 /* right shift it by 6 to give 0 or 2 */
8294 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8295 /* add data frame header length */
8296 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8297 /* add index, was start of 802.11 header */
8298 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8299 /* move to index, now start of LL header */
8300 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8301
8302 /*
8303 * Accept empty data frames, we use those for
8304 * polling activity.
8305 */
8306 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8307 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8308
8309 /*
8310 * Accept EAPOL frames
8311 */
8312 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8313 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8314 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8315 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8316
8317 /* keep these last two statements or change the code below */
8318 /* return 0 == "DROP" */
8319 BPF_STMT(BPF_RET | BPF_K, 0),
8320 /* return ~0 == "keep all" */
8321 BPF_STMT(BPF_RET | BPF_K, ~0),
8322};
8323
8324static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008325 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008326 .filter = msock_filter_insns,
8327};
8328
8329
8330static int add_monitor_filter(int s)
8331{
8332 int idx;
8333
8334 /* rewrite all PASS/FAIL jump offsets */
8335 for (idx = 0; idx < msock_filter.len; idx++) {
8336 struct sock_filter *insn = &msock_filter_insns[idx];
8337
8338 if (BPF_CLASS(insn->code) == BPF_JMP) {
8339 if (insn->code == (BPF_JMP|BPF_JA)) {
8340 if (insn->k == PASS)
8341 insn->k = msock_filter.len - idx - 2;
8342 else if (insn->k == FAIL)
8343 insn->k = msock_filter.len - idx - 3;
8344 }
8345
8346 if (insn->jt == PASS)
8347 insn->jt = msock_filter.len - idx - 2;
8348 else if (insn->jt == FAIL)
8349 insn->jt = msock_filter.len - idx - 3;
8350
8351 if (insn->jf == PASS)
8352 insn->jf = msock_filter.len - idx - 2;
8353 else if (insn->jf == FAIL)
8354 insn->jf = msock_filter.len - idx - 3;
8355 }
8356 }
8357
8358 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8359 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008360 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8361 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008362 return -1;
8363 }
8364
8365 return 0;
8366}
8367
8368
8369static void nl80211_remove_monitor_interface(
8370 struct wpa_driver_nl80211_data *drv)
8371{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008372 if (drv->monitor_refcount > 0)
8373 drv->monitor_refcount--;
8374 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8375 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008376 if (drv->monitor_refcount > 0)
8377 return;
8378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008379 if (drv->monitor_ifidx >= 0) {
8380 nl80211_remove_iface(drv, drv->monitor_ifidx);
8381 drv->monitor_ifidx = -1;
8382 }
8383 if (drv->monitor_sock >= 0) {
8384 eloop_unregister_read_sock(drv->monitor_sock);
8385 close(drv->monitor_sock);
8386 drv->monitor_sock = -1;
8387 }
8388}
8389
8390
8391static int
8392nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8393{
8394 char buf[IFNAMSIZ];
8395 struct sockaddr_ll ll;
8396 int optval;
8397 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008398
8399 if (drv->monitor_ifidx >= 0) {
8400 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008401 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8402 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008403 return 0;
8404 }
8405
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008406 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008407 /*
8408 * P2P interface name is of the format p2p-%s-%d. For monitor
8409 * interface name corresponding to P2P GO, replace "p2p-" with
8410 * "mon-" to retain the same interface name length and to
8411 * indicate that it is a monitor interface.
8412 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008413 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008414 } else {
8415 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008416 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008417 }
8418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008419 buf[IFNAMSIZ - 1] = '\0';
8420
8421 drv->monitor_ifidx =
8422 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008423 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008424
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008425 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008426 /*
8427 * This is backward compatibility for a few versions of
8428 * the kernel only that didn't advertise the right
8429 * attributes for the only driver that then supported
8430 * AP mode w/o monitor -- ath6kl.
8431 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008432 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8433 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008434 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008435 }
8436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008437 if (drv->monitor_ifidx < 0)
8438 return -1;
8439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008440 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008441 goto error;
8442
8443 memset(&ll, 0, sizeof(ll));
8444 ll.sll_family = AF_PACKET;
8445 ll.sll_ifindex = drv->monitor_ifidx;
8446 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8447 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008448 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8449 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008450 goto error;
8451 }
8452
8453 if (add_monitor_filter(drv->monitor_sock)) {
8454 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8455 "interface; do filtering in user space");
8456 /* This works, but will cost in performance. */
8457 }
8458
8459 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008460 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8461 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462 goto error;
8463 }
8464
8465 optlen = sizeof(optval);
8466 optval = 20;
8467 if (setsockopt
8468 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008469 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8470 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008471 goto error;
8472 }
8473
8474 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8475 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008476 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008477 goto error;
8478 }
8479
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008480 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008481 return 0;
8482 error:
8483 nl80211_remove_monitor_interface(drv);
8484 return -1;
8485}
8486
8487
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008488static int nl80211_setup_ap(struct i802_bss *bss)
8489{
8490 struct wpa_driver_nl80211_data *drv = bss->drv;
8491
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008492 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8493 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008494
8495 /*
8496 * Disable Probe Request reporting unless we need it in this way for
8497 * devices that include the AP SME, in the other case (unless using
8498 * monitor iface) we'll get it through the nl_mgmt socket instead.
8499 */
8500 if (!drv->device_ap_sme)
8501 wpa_driver_nl80211_probe_req_report(bss, 0);
8502
8503 if (!drv->device_ap_sme && !drv->use_monitor)
8504 if (nl80211_mgmt_subscribe_ap(bss))
8505 return -1;
8506
8507 if (drv->device_ap_sme && !drv->use_monitor)
8508 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8509 return -1;
8510
8511 if (!drv->device_ap_sme && drv->use_monitor &&
8512 nl80211_create_monitor_interface(drv) &&
8513 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008514 return -1;
8515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008516 if (drv->device_ap_sme &&
8517 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8518 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8519 "Probe Request frame reporting in AP mode");
8520 /* Try to survive without this */
8521 }
8522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008523 return 0;
8524}
8525
8526
8527static void nl80211_teardown_ap(struct i802_bss *bss)
8528{
8529 struct wpa_driver_nl80211_data *drv = bss->drv;
8530
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008531 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8532 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008533 if (drv->device_ap_sme) {
8534 wpa_driver_nl80211_probe_req_report(bss, 0);
8535 if (!drv->use_monitor)
8536 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8537 } else if (drv->use_monitor)
8538 nl80211_remove_monitor_interface(drv);
8539 else
8540 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8541
8542 bss->beacon_set = 0;
8543}
8544
8545
8546static int nl80211_send_eapol_data(struct i802_bss *bss,
8547 const u8 *addr, const u8 *data,
8548 size_t data_len)
8549{
8550 struct sockaddr_ll ll;
8551 int ret;
8552
8553 if (bss->drv->eapol_tx_sock < 0) {
8554 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8555 return -1;
8556 }
8557
8558 os_memset(&ll, 0, sizeof(ll));
8559 ll.sll_family = AF_PACKET;
8560 ll.sll_ifindex = bss->ifindex;
8561 ll.sll_protocol = htons(ETH_P_PAE);
8562 ll.sll_halen = ETH_ALEN;
8563 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8564 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8565 (struct sockaddr *) &ll, sizeof(ll));
8566 if (ret < 0)
8567 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8568 strerror(errno));
8569
8570 return ret;
8571}
8572
8573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008574static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8575
8576static int wpa_driver_nl80211_hapd_send_eapol(
8577 void *priv, const u8 *addr, const u8 *data,
8578 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8579{
8580 struct i802_bss *bss = priv;
8581 struct wpa_driver_nl80211_data *drv = bss->drv;
8582 struct ieee80211_hdr *hdr;
8583 size_t len;
8584 u8 *pos;
8585 int res;
8586 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008587
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008588 if (drv->device_ap_sme || !drv->use_monitor)
8589 return nl80211_send_eapol_data(bss, addr, data, data_len);
8590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008591 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8592 data_len;
8593 hdr = os_zalloc(len);
8594 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008595 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8596 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008597 return -1;
8598 }
8599
8600 hdr->frame_control =
8601 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8602 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8603 if (encrypt)
8604 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8605 if (qos) {
8606 hdr->frame_control |=
8607 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8608 }
8609
8610 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8611 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8612 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8613 pos = (u8 *) (hdr + 1);
8614
8615 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008616 /* Set highest priority in QoS header */
8617 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008618 pos[1] = 0;
8619 pos += 2;
8620 }
8621
8622 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8623 pos += sizeof(rfc1042_header);
8624 WPA_PUT_BE16(pos, ETH_P_PAE);
8625 pos += 2;
8626 memcpy(pos, data, data_len);
8627
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008628 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8629 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008630 if (res < 0) {
8631 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8632 "failed: %d (%s)",
8633 (unsigned long) len, errno, strerror(errno));
8634 }
8635 os_free(hdr);
8636
8637 return res;
8638}
8639
8640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008641static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8642 int total_flags,
8643 int flags_or, int flags_and)
8644{
8645 struct i802_bss *bss = priv;
8646 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008647 struct nl_msg *msg;
8648 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008649 struct nl80211_sta_flag_update upd;
8650
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008651 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
8652 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
8653 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
8654 !!(total_flags & WPA_STA_AUTHORIZED));
8655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008656 msg = nlmsg_alloc();
8657 if (!msg)
8658 return -ENOMEM;
8659
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008660 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008661
8662 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8663 if_nametoindex(bss->ifname));
8664 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8665
8666 /*
8667 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8668 * can be removed eventually.
8669 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008670 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8671 if (!flags)
8672 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008673 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008674 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675
8676 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008677 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008678
8679 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008680 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008681
8682 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008683 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008685 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008686 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008687
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008688 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008689
8690 os_memset(&upd, 0, sizeof(upd));
8691 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8692 upd.set = sta_flags_nl80211(flags_or);
8693 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008695 return send_and_recv_msgs(drv, msg, NULL, NULL);
8696 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008697 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008698 return -ENOBUFS;
8699}
8700
8701
8702static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8703 struct wpa_driver_associate_params *params)
8704{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008705 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008706
8707 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008708 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8709 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008710 nlmode = NL80211_IFTYPE_P2P_GO;
8711 } else
8712 nlmode = NL80211_IFTYPE_AP;
8713
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008714 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008715 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008716 nl80211_remove_monitor_interface(drv);
8717 return -1;
8718 }
8719
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008720 if (nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008721 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008722 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008723 nl80211_remove_monitor_interface(drv);
8724 return -1;
8725 }
8726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008727 return 0;
8728}
8729
8730
8731static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8732{
8733 struct nl_msg *msg;
8734 int ret = -1;
8735
8736 msg = nlmsg_alloc();
8737 if (!msg)
8738 return -1;
8739
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008740 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008741 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8742 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8743 msg = NULL;
8744 if (ret) {
8745 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8746 "(%s)", ret, strerror(-ret));
8747 goto nla_put_failure;
8748 }
8749
8750 ret = 0;
8751 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8752
8753nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008754 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008755 NL80211_IFTYPE_STATION)) {
8756 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8757 "station mode");
8758 }
8759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008760 nlmsg_free(msg);
8761 return ret;
8762}
8763
8764
8765static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8766 struct wpa_driver_associate_params *params)
8767{
8768 struct nl_msg *msg;
8769 int ret = -1;
8770 int count = 0;
8771
8772 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8773
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008774 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008775 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8776 "IBSS mode");
8777 return -1;
8778 }
8779
8780retry:
8781 msg = nlmsg_alloc();
8782 if (!msg)
8783 return -1;
8784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008785 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008786 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8787
8788 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8789 goto nla_put_failure;
8790
8791 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8792 params->ssid, params->ssid_len);
8793 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8794 params->ssid);
8795 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8796 drv->ssid_len = params->ssid_len;
8797
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008798 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
8799 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", params->freq.ht_enabled);
8800 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
8801 params->freq.sec_channel_offset);
8802 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", params->freq.vht_enabled);
8803 wpa_printf(MSG_DEBUG, " * center_freq1=%d", params->freq.center_freq1);
8804 wpa_printf(MSG_DEBUG, " * center_freq2=%d", params->freq.center_freq2);
8805 wpa_printf(MSG_DEBUG, " * bandwidth=%d", params->freq.bandwidth);
8806 if (nl80211_put_freq_params(msg, &params->freq) < 0)
8807 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008808
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008809 if (params->beacon_int > 0) {
8810 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8811 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8812 params->beacon_int);
8813 }
8814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008815 ret = nl80211_set_conn_keys(params, msg);
8816 if (ret)
8817 goto nla_put_failure;
8818
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008819 if (params->bssid && params->fixed_bssid) {
8820 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8821 MAC2STR(params->bssid));
8822 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8823 }
8824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008825 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8826 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8827 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8828 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008829 wpa_printf(MSG_DEBUG, " * control port");
8830 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8831 }
8832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008833 if (params->wpa_ie) {
8834 wpa_hexdump(MSG_DEBUG,
8835 " * Extra IEs for Beacon/Probe Response frames",
8836 params->wpa_ie, params->wpa_ie_len);
8837 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8838 params->wpa_ie);
8839 }
8840
8841 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8842 msg = NULL;
8843 if (ret) {
8844 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8845 ret, strerror(-ret));
8846 count++;
8847 if (ret == -EALREADY && count == 1) {
8848 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8849 "forced leave");
8850 nl80211_leave_ibss(drv);
8851 nlmsg_free(msg);
8852 goto retry;
8853 }
8854
8855 goto nla_put_failure;
8856 }
8857 ret = 0;
8858 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8859
8860nla_put_failure:
8861 nlmsg_free(msg);
8862 return ret;
8863}
8864
8865
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008866static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8867 struct wpa_driver_associate_params *params,
8868 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008869{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008870 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008872 if (params->bssid) {
8873 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8874 MAC2STR(params->bssid));
8875 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8876 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008877
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008878 if (params->bssid_hint) {
8879 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8880 MAC2STR(params->bssid_hint));
8881 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8882 params->bssid_hint);
8883 }
8884
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008885 if (params->freq.freq) {
8886 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
8887 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq.freq);
8888 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008889 } else
8890 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008891
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008892 if (params->freq_hint) {
8893 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8894 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8895 params->freq_hint);
8896 }
8897
Dmitry Shmidt04949592012-07-19 12:16:46 -07008898 if (params->bg_scan_period >= 0) {
8899 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8900 params->bg_scan_period);
8901 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8902 params->bg_scan_period);
8903 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008905 if (params->ssid) {
8906 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8907 params->ssid, params->ssid_len);
8908 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8909 params->ssid);
8910 if (params->ssid_len > sizeof(drv->ssid))
8911 goto nla_put_failure;
8912 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8913 drv->ssid_len = params->ssid_len;
8914 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008916 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8917 if (params->wpa_ie)
8918 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8919 params->wpa_ie);
8920
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008921 if (params->wpa_proto) {
8922 enum nl80211_wpa_versions ver = 0;
8923
8924 if (params->wpa_proto & WPA_PROTO_WPA)
8925 ver |= NL80211_WPA_VERSION_1;
8926 if (params->wpa_proto & WPA_PROTO_RSN)
8927 ver |= NL80211_WPA_VERSION_2;
8928
8929 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8930 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8931 }
8932
8933 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8934 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8935 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8936 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8937 }
8938
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008939 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8940 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8941 /*
8942 * This is likely to work even though many drivers do not
8943 * advertise support for operations without GTK.
8944 */
8945 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8946 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008947 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8948 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8949 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8950 }
8951
8952 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8953 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8954 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8955 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008956 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008957 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
8958 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8959 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008960 int mgmt = WLAN_AKM_SUITE_PSK;
8961
8962 switch (params->key_mgmt_suite) {
8963 case WPA_KEY_MGMT_CCKM:
8964 mgmt = WLAN_AKM_SUITE_CCKM;
8965 break;
8966 case WPA_KEY_MGMT_IEEE8021X:
8967 mgmt = WLAN_AKM_SUITE_8021X;
8968 break;
8969 case WPA_KEY_MGMT_FT_IEEE8021X:
8970 mgmt = WLAN_AKM_SUITE_FT_8021X;
8971 break;
8972 case WPA_KEY_MGMT_FT_PSK:
8973 mgmt = WLAN_AKM_SUITE_FT_PSK;
8974 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008975 case WPA_KEY_MGMT_IEEE8021X_SHA256:
8976 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
8977 break;
8978 case WPA_KEY_MGMT_PSK_SHA256:
8979 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
8980 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008981 case WPA_KEY_MGMT_OSEN:
8982 mgmt = WLAN_AKM_SUITE_OSEN;
8983 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008984 case WPA_KEY_MGMT_PSK:
8985 default:
8986 mgmt = WLAN_AKM_SUITE_PSK;
8987 break;
8988 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008989 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008990 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8991 }
8992
8993 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8994
8995 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8996 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8997
8998 if (params->disable_ht)
8999 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
9000
9001 if (params->htcaps && params->htcaps_mask) {
9002 int sz = sizeof(struct ieee80211_ht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009003 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009004 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009005 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
9006 params->htcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009007 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
9008 params->htcaps_mask);
9009 }
9010
9011#ifdef CONFIG_VHT_OVERRIDES
9012 if (params->disable_vht) {
9013 wpa_printf(MSG_DEBUG, " * VHT disabled");
9014 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
9015 }
9016
9017 if (params->vhtcaps && params->vhtcaps_mask) {
9018 int sz = sizeof(struct ieee80211_vht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009019 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009020 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009021 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
9022 params->vhtcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009023 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
9024 params->vhtcaps_mask);
9025 }
9026#endif /* CONFIG_VHT_OVERRIDES */
9027
9028 if (params->p2p)
9029 wpa_printf(MSG_DEBUG, " * P2P group");
9030
9031 return 0;
9032nla_put_failure:
9033 return -1;
9034}
9035
9036
9037static int wpa_driver_nl80211_try_connect(
9038 struct wpa_driver_nl80211_data *drv,
9039 struct wpa_driver_associate_params *params)
9040{
9041 struct nl_msg *msg;
9042 enum nl80211_auth_type type;
9043 int ret;
9044 int algs;
9045
9046 msg = nlmsg_alloc();
9047 if (!msg)
9048 return -1;
9049
9050 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9051 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
9052
9053 ret = nl80211_connect_common(drv, params, msg);
9054 if (ret)
9055 goto nla_put_failure;
9056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009057 algs = 0;
9058 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9059 algs++;
9060 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9061 algs++;
9062 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9063 algs++;
9064 if (algs > 1) {
9065 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
9066 "selection");
9067 goto skip_auth_type;
9068 }
9069
9070 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9071 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
9072 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9073 type = NL80211_AUTHTYPE_SHARED_KEY;
9074 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9075 type = NL80211_AUTHTYPE_NETWORK_EAP;
9076 else if (params->auth_alg & WPA_AUTH_ALG_FT)
9077 type = NL80211_AUTHTYPE_FT;
9078 else
9079 goto nla_put_failure;
9080
9081 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
9082 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
9083
9084skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009085 ret = nl80211_set_conn_keys(params, msg);
9086 if (ret)
9087 goto nla_put_failure;
9088
9089 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9090 msg = NULL;
9091 if (ret) {
9092 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
9093 "(%s)", ret, strerror(-ret));
9094 goto nla_put_failure;
9095 }
9096 ret = 0;
9097 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
9098
9099nla_put_failure:
9100 nlmsg_free(msg);
9101 return ret;
9102
9103}
9104
9105
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009106static int wpa_driver_nl80211_connect(
9107 struct wpa_driver_nl80211_data *drv,
9108 struct wpa_driver_associate_params *params)
9109{
Jithu Jancea7c60b42014-12-03 18:54:40 +05309110 int ret;
9111
9112 /* Store the connection attempted bssid for future use */
9113 if (params->bssid)
9114 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
9115 else
9116 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
9117
9118 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009119 if (ret == -EALREADY) {
9120 /*
9121 * cfg80211 does not currently accept new connections if
9122 * we are already connected. As a workaround, force
9123 * disconnection and try again.
9124 */
9125 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
9126 "disconnecting before reassociation "
9127 "attempt");
9128 if (wpa_driver_nl80211_disconnect(
9129 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
9130 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009131 ret = wpa_driver_nl80211_try_connect(drv, params);
9132 }
9133 return ret;
9134}
9135
9136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009137static int wpa_driver_nl80211_associate(
9138 void *priv, struct wpa_driver_associate_params *params)
9139{
9140 struct i802_bss *bss = priv;
9141 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009142 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009143 struct nl_msg *msg;
9144
9145 if (params->mode == IEEE80211_MODE_AP)
9146 return wpa_driver_nl80211_ap(drv, params);
9147
9148 if (params->mode == IEEE80211_MODE_IBSS)
9149 return wpa_driver_nl80211_ibss(drv, params);
9150
9151 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009152 enum nl80211_iftype nlmode = params->p2p ?
9153 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
9154
9155 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009156 return -1;
9157 return wpa_driver_nl80211_connect(drv, params);
9158 }
9159
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009160 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009161
9162 msg = nlmsg_alloc();
9163 if (!msg)
9164 return -1;
9165
9166 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
9167 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009168 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009169
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009170 ret = nl80211_connect_common(drv, params, msg);
9171 if (ret)
9172 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009173
9174 if (params->prev_bssid) {
9175 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
9176 MAC2STR(params->prev_bssid));
9177 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
9178 params->prev_bssid);
9179 }
9180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009181 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9182 msg = NULL;
9183 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009184 wpa_dbg(drv->ctx, MSG_DEBUG,
9185 "nl80211: MLME command failed (assoc): ret=%d (%s)",
9186 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009187 nl80211_dump_scan(drv);
9188 goto nla_put_failure;
9189 }
9190 ret = 0;
9191 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
9192 "successfully");
9193
9194nla_put_failure:
9195 nlmsg_free(msg);
9196 return ret;
9197}
9198
9199
9200static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009201 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009202{
9203 struct nl_msg *msg;
9204 int ret = -ENOBUFS;
9205
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009206 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
9207 ifindex, mode, nl80211_iftype_str(mode));
9208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009209 msg = nlmsg_alloc();
9210 if (!msg)
9211 return -ENOMEM;
9212
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009213 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009214 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009215 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009216 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
9217
9218 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009219 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009220 if (!ret)
9221 return 0;
9222nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009223 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009224 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
9225 " %d (%s)", ifindex, mode, ret, strerror(-ret));
9226 return ret;
9227}
9228
9229
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009230static int wpa_driver_nl80211_set_mode_impl(
9231 struct i802_bss *bss,
9232 enum nl80211_iftype nlmode,
9233 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009234{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009235 struct wpa_driver_nl80211_data *drv = bss->drv;
9236 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009237 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009238 int was_ap = is_ap_interface(drv->nlmode);
9239 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009240 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009241
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009242 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9243 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
9244 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009245
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009246 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009247 drv->nlmode = nlmode;
9248 ret = 0;
9249 goto done;
9250 }
9251
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009252 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009253 return -1;
9254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009255 if (nlmode == drv->nlmode) {
9256 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
9257 "requested mode - ignore error");
9258 ret = 0;
9259 goto done; /* Already in the requested mode */
9260 }
9261
9262 /* mac80211 doesn't allow mode changes while the device is up, so
9263 * take the device down, try to set the mode again, and bring the
9264 * device back up.
9265 */
9266 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9267 "interface down");
9268 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009269 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009270 if (res == -EACCES || res == -ENODEV)
9271 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009272 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009273 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9274 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009275 os_sleep(0, 100000);
9276 continue;
9277 }
9278
9279 /*
9280 * Setting the mode will fail for some drivers if the phy is
9281 * on a frequency that the mode is disallowed in.
9282 */
9283 if (desired_freq_params) {
9284 res = i802_set_freq(bss, desired_freq_params);
9285 if (res) {
9286 wpa_printf(MSG_DEBUG,
9287 "nl80211: Failed to set frequency on interface");
9288 }
9289 }
9290
9291 /* Try to set the mode again while the interface is down */
9292 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9293 if (mode_switch_res == -EBUSY) {
9294 wpa_printf(MSG_DEBUG,
9295 "nl80211: Delaying mode set while interface going down");
9296 os_sleep(0, 100000);
9297 continue;
9298 }
9299 ret = mode_switch_res;
9300 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009301 }
9302
9303 if (!ret) {
9304 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9305 "interface is down");
9306 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009307 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009308 }
9309
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009310 /* Bring the interface back up */
9311 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
9312 if (res != 0) {
9313 wpa_printf(MSG_DEBUG,
9314 "nl80211: Failed to set interface up after switching mode");
9315 ret = -1;
9316 }
9317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009318done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009319 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009320 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9321 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009322 return ret;
9323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009324
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009325 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009326 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
9327 else if (drv->disabled_11b_rates)
9328 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
9329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009330 if (is_ap_interface(nlmode)) {
9331 nl80211_mgmt_unsubscribe(bss, "start AP");
9332 /* Setup additional AP mode functionality if needed */
9333 if (nl80211_setup_ap(bss))
9334 return -1;
9335 } else if (was_ap) {
9336 /* Remove additional AP mode functionality */
9337 nl80211_teardown_ap(bss);
9338 } else {
9339 nl80211_mgmt_unsubscribe(bss, "mode change");
9340 }
9341
Dmitry Shmidt04949592012-07-19 12:16:46 -07009342 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009343 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9344 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9345 "frame processing - ignore for now");
9346
9347 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009348}
9349
9350
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009351static int dfs_info_handler(struct nl_msg *msg, void *arg)
9352{
9353 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9354 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9355 int *dfs_capability_ptr = arg;
9356
9357 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9358 genlmsg_attrlen(gnlh, 0), NULL);
9359
9360 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9361 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9362 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9363
9364 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9365 nla_data(nl_vend), nla_len(nl_vend), NULL);
9366
9367 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9368 u32 val;
9369 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9370 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9371 val);
9372 *dfs_capability_ptr = val;
9373 }
9374 }
9375
9376 return NL_SKIP;
9377}
9378
9379
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009380static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
9381 enum nl80211_iftype nlmode)
9382{
9383 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
9384}
9385
9386
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07009387static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
9388 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009389{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009390 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07009391 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009392}
9393
9394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009395static int wpa_driver_nl80211_get_capa(void *priv,
9396 struct wpa_driver_capa *capa)
9397{
9398 struct i802_bss *bss = priv;
9399 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009400 struct nl_msg *msg;
9401 int dfs_capability = 0;
9402 int ret = 0;
9403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009404 if (!drv->has_capability)
9405 return -1;
9406 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009407 if (drv->extended_capa && drv->extended_capa_mask) {
9408 capa->extended_capa = drv->extended_capa;
9409 capa->extended_capa_mask = drv->extended_capa_mask;
9410 capa->extended_capa_len = drv->extended_capa_len;
9411 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009412
9413 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9414 !drv->allow_p2p_device) {
9415 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9416 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9417 }
9418
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009419 if (drv->dfs_vendor_cmd_avail == 1) {
9420 msg = nlmsg_alloc();
9421 if (!msg)
9422 return -ENOMEM;
9423
9424 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9425
9426 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9427 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9428 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9429 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9430
9431 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9432 &dfs_capability);
9433 if (!ret) {
9434 if (dfs_capability)
9435 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9436 }
9437 }
9438
9439 return ret;
9440
9441 nla_put_failure:
9442 nlmsg_free(msg);
9443 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009444}
9445
9446
9447static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9448{
9449 struct i802_bss *bss = priv;
9450 struct wpa_driver_nl80211_data *drv = bss->drv;
9451
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009452 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9453 bss->ifname, drv->operstate, state,
9454 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009455 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009456 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009457 state ? IF_OPER_UP : IF_OPER_DORMANT);
9458}
9459
9460
9461static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9462{
9463 struct i802_bss *bss = priv;
9464 struct wpa_driver_nl80211_data *drv = bss->drv;
9465 struct nl_msg *msg;
9466 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009467 int ret = -ENOBUFS;
9468
9469 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9470 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9471 return 0;
9472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009473
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009474 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9475 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009477 msg = nlmsg_alloc();
9478 if (!msg)
9479 return -ENOMEM;
9480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009481 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009482
9483 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9484 if_nametoindex(bss->ifname));
9485 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9486
9487 os_memset(&upd, 0, sizeof(upd));
9488 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9489 if (authorized)
9490 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9491 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9492
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009493 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9494 msg = NULL;
9495 if (!ret)
9496 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009497 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009498 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009499 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9500 ret, strerror(-ret));
9501 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009502}
9503
9504
Jouni Malinen75ecf522011-06-27 15:19:46 -07009505/* Set kernel driver on given frequency (MHz) */
9506static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009507{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009508 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009509 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009510}
9511
9512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009513static inline int min_int(int a, int b)
9514{
9515 if (a < b)
9516 return a;
9517 return b;
9518}
9519
9520
9521static int get_key_handler(struct nl_msg *msg, void *arg)
9522{
9523 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9524 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9525
9526 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9527 genlmsg_attrlen(gnlh, 0), NULL);
9528
9529 /*
9530 * TODO: validate the key index and mac address!
9531 * Otherwise, there's a race condition as soon as
9532 * the kernel starts sending key notifications.
9533 */
9534
9535 if (tb[NL80211_ATTR_KEY_SEQ])
9536 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9537 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9538 return NL_SKIP;
9539}
9540
9541
9542static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9543 int idx, u8 *seq)
9544{
9545 struct i802_bss *bss = priv;
9546 struct wpa_driver_nl80211_data *drv = bss->drv;
9547 struct nl_msg *msg;
9548
9549 msg = nlmsg_alloc();
9550 if (!msg)
9551 return -ENOMEM;
9552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009553 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009554
9555 if (addr)
9556 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9557 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9558 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9559
9560 memset(seq, 0, 6);
9561
9562 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9563 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009564 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009565 return -ENOBUFS;
9566}
9567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009568
9569static int i802_set_rts(void *priv, int rts)
9570{
9571 struct i802_bss *bss = priv;
9572 struct wpa_driver_nl80211_data *drv = bss->drv;
9573 struct nl_msg *msg;
9574 int ret = -ENOBUFS;
9575 u32 val;
9576
9577 msg = nlmsg_alloc();
9578 if (!msg)
9579 return -ENOMEM;
9580
9581 if (rts >= 2347)
9582 val = (u32) -1;
9583 else
9584 val = rts;
9585
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009586 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009587 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9588 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9589
9590 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009591 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009592 if (!ret)
9593 return 0;
9594nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009595 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009596 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9597 "%d (%s)", rts, ret, strerror(-ret));
9598 return ret;
9599}
9600
9601
9602static int i802_set_frag(void *priv, int frag)
9603{
9604 struct i802_bss *bss = priv;
9605 struct wpa_driver_nl80211_data *drv = bss->drv;
9606 struct nl_msg *msg;
9607 int ret = -ENOBUFS;
9608 u32 val;
9609
9610 msg = nlmsg_alloc();
9611 if (!msg)
9612 return -ENOMEM;
9613
9614 if (frag >= 2346)
9615 val = (u32) -1;
9616 else
9617 val = frag;
9618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009619 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9621 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9622
9623 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009624 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009625 if (!ret)
9626 return 0;
9627nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009628 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009629 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9630 "%d: %d (%s)", frag, ret, strerror(-ret));
9631 return ret;
9632}
9633
9634
9635static int i802_flush(void *priv)
9636{
9637 struct i802_bss *bss = priv;
9638 struct wpa_driver_nl80211_data *drv = bss->drv;
9639 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009640 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009641
9642 msg = nlmsg_alloc();
9643 if (!msg)
9644 return -1;
9645
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009646 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9647 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009648 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009649
9650 /*
9651 * XXX: FIX! this needs to flush all VLANs too
9652 */
9653 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9654 if_nametoindex(bss->ifname));
9655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009656 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9657 if (res) {
9658 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9659 "(%s)", res, strerror(-res));
9660 }
9661 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009662 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009663 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009664 return -ENOBUFS;
9665}
9666
9667
9668static int get_sta_handler(struct nl_msg *msg, void *arg)
9669{
9670 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9671 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9672 struct hostap_sta_driver_data *data = arg;
9673 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9674 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9675 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9676 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9677 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9678 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9679 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009680 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009681 };
9682
9683 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9684 genlmsg_attrlen(gnlh, 0), NULL);
9685
9686 /*
9687 * TODO: validate the interface and mac address!
9688 * Otherwise, there's a race condition as soon as
9689 * the kernel starts sending station notifications.
9690 */
9691
9692 if (!tb[NL80211_ATTR_STA_INFO]) {
9693 wpa_printf(MSG_DEBUG, "sta stats missing!");
9694 return NL_SKIP;
9695 }
9696 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9697 tb[NL80211_ATTR_STA_INFO],
9698 stats_policy)) {
9699 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9700 return NL_SKIP;
9701 }
9702
9703 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9704 data->inactive_msec =
9705 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9706 if (stats[NL80211_STA_INFO_RX_BYTES])
9707 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9708 if (stats[NL80211_STA_INFO_TX_BYTES])
9709 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9710 if (stats[NL80211_STA_INFO_RX_PACKETS])
9711 data->rx_packets =
9712 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9713 if (stats[NL80211_STA_INFO_TX_PACKETS])
9714 data->tx_packets =
9715 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009716 if (stats[NL80211_STA_INFO_TX_FAILED])
9717 data->tx_retry_failed =
9718 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009719
9720 return NL_SKIP;
9721}
9722
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009723static int i802_read_sta_data(struct i802_bss *bss,
9724 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009725 const u8 *addr)
9726{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009727 struct wpa_driver_nl80211_data *drv = bss->drv;
9728 struct nl_msg *msg;
9729
9730 os_memset(data, 0, sizeof(*data));
9731 msg = nlmsg_alloc();
9732 if (!msg)
9733 return -ENOMEM;
9734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009735 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009736
9737 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9738 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9739
9740 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9741 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009742 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009743 return -ENOBUFS;
9744}
9745
9746
9747static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9748 int cw_min, int cw_max, int burst_time)
9749{
9750 struct i802_bss *bss = priv;
9751 struct wpa_driver_nl80211_data *drv = bss->drv;
9752 struct nl_msg *msg;
9753 struct nlattr *txq, *params;
9754
9755 msg = nlmsg_alloc();
9756 if (!msg)
9757 return -1;
9758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009759 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009760
9761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9762
9763 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9764 if (!txq)
9765 goto nla_put_failure;
9766
9767 /* We are only sending parameters for a single TXQ at a time */
9768 params = nla_nest_start(msg, 1);
9769 if (!params)
9770 goto nla_put_failure;
9771
9772 switch (queue) {
9773 case 0:
9774 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9775 break;
9776 case 1:
9777 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9778 break;
9779 case 2:
9780 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9781 break;
9782 case 3:
9783 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9784 break;
9785 }
9786 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9787 * 32 usec, so need to convert the value here. */
9788 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9789 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9790 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9791 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9792
9793 nla_nest_end(msg, params);
9794
9795 nla_nest_end(msg, txq);
9796
9797 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9798 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009799 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009800 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009801 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009802 return -1;
9803}
9804
9805
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009806static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009807 const char *ifname, int vlan_id)
9808{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009809 struct wpa_driver_nl80211_data *drv = bss->drv;
9810 struct nl_msg *msg;
9811 int ret = -ENOBUFS;
9812
9813 msg = nlmsg_alloc();
9814 if (!msg)
9815 return -ENOMEM;
9816
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009817 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9818 ", ifname=%s[%d], vlan_id=%d)",
9819 bss->ifname, if_nametoindex(bss->ifname),
9820 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009821 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009822
9823 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9824 if_nametoindex(bss->ifname));
9825 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9826 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9827 if_nametoindex(ifname));
9828
9829 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009830 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009831 if (ret < 0) {
9832 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9833 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9834 MAC2STR(addr), ifname, vlan_id, ret,
9835 strerror(-ret));
9836 }
9837 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009838 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009839 return ret;
9840}
9841
9842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009843static int i802_get_inact_sec(void *priv, const u8 *addr)
9844{
9845 struct hostap_sta_driver_data data;
9846 int ret;
9847
9848 data.inactive_msec = (unsigned long) -1;
9849 ret = i802_read_sta_data(priv, &data, addr);
9850 if (ret || data.inactive_msec == (unsigned long) -1)
9851 return -1;
9852 return data.inactive_msec / 1000;
9853}
9854
9855
9856static int i802_sta_clear_stats(void *priv, const u8 *addr)
9857{
9858#if 0
9859 /* TODO */
9860#endif
9861 return 0;
9862}
9863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009864
9865static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9866 int reason)
9867{
9868 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009869 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009870 struct ieee80211_mgmt mgmt;
9871
Dmitry Shmidt04949592012-07-19 12:16:46 -07009872 if (drv->device_ap_sme)
9873 return wpa_driver_nl80211_sta_remove(bss, addr);
9874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009875 memset(&mgmt, 0, sizeof(mgmt));
9876 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9877 WLAN_FC_STYPE_DEAUTH);
9878 memcpy(mgmt.da, addr, ETH_ALEN);
9879 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9880 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9881 mgmt.u.deauth.reason_code = host_to_le16(reason);
9882 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9883 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009884 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9885 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009886}
9887
9888
9889static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9890 int reason)
9891{
9892 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009893 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009894 struct ieee80211_mgmt mgmt;
9895
Dmitry Shmidt04949592012-07-19 12:16:46 -07009896 if (drv->device_ap_sme)
9897 return wpa_driver_nl80211_sta_remove(bss, addr);
9898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009899 memset(&mgmt, 0, sizeof(mgmt));
9900 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9901 WLAN_FC_STYPE_DISASSOC);
9902 memcpy(mgmt.da, addr, ETH_ALEN);
9903 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9904 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9905 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9906 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9907 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009908 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9909 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009910}
9911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009912
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009913static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9914{
9915 char buf[200], *pos, *end;
9916 int i, res;
9917
9918 pos = buf;
9919 end = pos + sizeof(buf);
9920
9921 for (i = 0; i < drv->num_if_indices; i++) {
9922 if (!drv->if_indices[i])
9923 continue;
9924 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9925 if (res < 0 || res >= end - pos)
9926 break;
9927 pos += res;
9928 }
9929 *pos = '\0';
9930
9931 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9932 drv->num_if_indices, buf);
9933}
9934
9935
Jouni Malinen75ecf522011-06-27 15:19:46 -07009936static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9937{
9938 int i;
9939 int *old;
9940
9941 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9942 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009943 if (have_ifidx(drv, ifidx)) {
9944 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9945 ifidx);
9946 return;
9947 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009948 for (i = 0; i < drv->num_if_indices; i++) {
9949 if (drv->if_indices[i] == 0) {
9950 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009951 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009952 return;
9953 }
9954 }
9955
9956 if (drv->if_indices != drv->default_if_indices)
9957 old = drv->if_indices;
9958 else
9959 old = NULL;
9960
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009961 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9962 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009963 if (!drv->if_indices) {
9964 if (!old)
9965 drv->if_indices = drv->default_if_indices;
9966 else
9967 drv->if_indices = old;
9968 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9969 "interfaces");
9970 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9971 return;
9972 } else if (!old)
9973 os_memcpy(drv->if_indices, drv->default_if_indices,
9974 sizeof(drv->default_if_indices));
9975 drv->if_indices[drv->num_if_indices] = ifidx;
9976 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009977 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009978}
9979
9980
9981static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9982{
9983 int i;
9984
9985 for (i = 0; i < drv->num_if_indices; i++) {
9986 if (drv->if_indices[i] == ifidx) {
9987 drv->if_indices[i] = 0;
9988 break;
9989 }
9990 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009991 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009992}
9993
9994
9995static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9996{
9997 int i;
9998
9999 for (i = 0; i < drv->num_if_indices; i++)
10000 if (drv->if_indices[i] == ifidx)
10001 return 1;
10002
10003 return 0;
10004}
10005
10006
10007static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070010008 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010009{
10010 struct i802_bss *bss = priv;
10011 struct wpa_driver_nl80211_data *drv = bss->drv;
10012 char name[IFNAMSIZ + 1];
10013
10014 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010015 if (ifname_wds)
10016 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
10017
Jouni Malinen75ecf522011-06-27 15:19:46 -070010018 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
10019 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
10020 if (val) {
10021 if (!if_nametoindex(name)) {
10022 if (nl80211_create_iface(drv, name,
10023 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010024 bss->addr, 1, NULL, NULL, 0) <
10025 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010026 return -1;
10027 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010028 linux_br_add_if(drv->global->ioctl_sock,
10029 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010030 return -1;
10031 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010032 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
10033 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
10034 "interface %s up", name);
10035 }
Jouni Malinen75ecf522011-06-27 15:19:46 -070010036 return i802_set_sta_vlan(priv, addr, name, 0);
10037 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010038 if (bridge_ifname)
10039 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
10040 name);
10041
Jouni Malinen75ecf522011-06-27 15:19:46 -070010042 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080010043 nl80211_remove_iface(drv, if_nametoindex(name));
10044 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010045 }
10046}
10047
10048
10049static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
10050{
10051 struct wpa_driver_nl80211_data *drv = eloop_ctx;
10052 struct sockaddr_ll lladdr;
10053 unsigned char buf[3000];
10054 int len;
10055 socklen_t fromlen = sizeof(lladdr);
10056
10057 len = recvfrom(sock, buf, sizeof(buf), 0,
10058 (struct sockaddr *)&lladdr, &fromlen);
10059 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010060 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
10061 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -070010062 return;
10063 }
10064
10065 if (have_ifidx(drv, lladdr.sll_ifindex))
10066 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
10067}
10068
10069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010070static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
10071 struct i802_bss *bss,
10072 const char *brname, const char *ifname)
10073{
10074 int ifindex;
10075 char in_br[IFNAMSIZ];
10076
10077 os_strlcpy(bss->brname, brname, IFNAMSIZ);
10078 ifindex = if_nametoindex(brname);
10079 if (ifindex == 0) {
10080 /*
10081 * Bridge was configured, but the bridge device does
10082 * not exist. Try to add it now.
10083 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010084 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010085 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
10086 "bridge interface %s: %s",
10087 brname, strerror(errno));
10088 return -1;
10089 }
10090 bss->added_bridge = 1;
10091 add_ifidx(drv, if_nametoindex(brname));
10092 }
10093
10094 if (linux_br_get(in_br, ifname) == 0) {
10095 if (os_strcmp(in_br, brname) == 0)
10096 return 0; /* already in the bridge */
10097
10098 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
10099 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010100 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
10101 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010102 wpa_printf(MSG_ERROR, "nl80211: Failed to "
10103 "remove interface %s from bridge "
10104 "%s: %s",
10105 ifname, brname, strerror(errno));
10106 return -1;
10107 }
10108 }
10109
10110 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
10111 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010112 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010113 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
10114 "into bridge %s: %s",
10115 ifname, brname, strerror(errno));
10116 return -1;
10117 }
10118 bss->added_if_into_bridge = 1;
10119
10120 return 0;
10121}
10122
10123
10124static void *i802_init(struct hostapd_data *hapd,
10125 struct wpa_init_params *params)
10126{
10127 struct wpa_driver_nl80211_data *drv;
10128 struct i802_bss *bss;
10129 size_t i;
10130 char brname[IFNAMSIZ];
10131 int ifindex, br_ifindex;
10132 int br_added = 0;
10133
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080010134 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
10135 params->global_priv, 1,
10136 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010137 if (bss == NULL)
10138 return NULL;
10139
10140 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010142 if (linux_br_get(brname, params->ifname) == 0) {
10143 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
10144 params->ifname, brname);
10145 br_ifindex = if_nametoindex(brname);
10146 } else {
10147 brname[0] = '\0';
10148 br_ifindex = 0;
10149 }
10150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010151 for (i = 0; i < params->num_bridge; i++) {
10152 if (params->bridge[i]) {
10153 ifindex = if_nametoindex(params->bridge[i]);
10154 if (ifindex)
10155 add_ifidx(drv, ifindex);
10156 if (ifindex == br_ifindex)
10157 br_added = 1;
10158 }
10159 }
10160 if (!br_added && br_ifindex &&
10161 (params->num_bridge == 0 || !params->bridge[0]))
10162 add_ifidx(drv, br_ifindex);
10163
10164 /* start listening for EAPOL on the default AP interface */
10165 add_ifidx(drv, drv->ifindex);
10166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010167 if (params->num_bridge && params->bridge[0] &&
10168 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
10169 goto failed;
10170
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010171#ifdef CONFIG_LIBNL3_ROUTE
10172 if (bss->added_if_into_bridge) {
10173 drv->rtnl_sk = nl_socket_alloc();
10174 if (drv->rtnl_sk == NULL) {
10175 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
10176 goto failed;
10177 }
10178
10179 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
10180 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
10181 strerror(errno));
10182 goto failed;
10183 }
10184 }
10185#endif /* CONFIG_LIBNL3_ROUTE */
10186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010187 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
10188 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010189 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
10190 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010191 goto failed;
10192 }
10193
10194 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
10195 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010196 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010197 goto failed;
10198 }
10199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010200 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
10201 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010202 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010203 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010205 memcpy(bss->addr, params->own_addr, ETH_ALEN);
10206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010207 return bss;
10208
10209failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010210 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010211 return NULL;
10212}
10213
10214
10215static void i802_deinit(void *priv)
10216{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010217 struct i802_bss *bss = priv;
10218 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010219}
10220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010221
10222static enum nl80211_iftype wpa_driver_nl80211_if_type(
10223 enum wpa_driver_if_type type)
10224{
10225 switch (type) {
10226 case WPA_IF_STATION:
10227 return NL80211_IFTYPE_STATION;
10228 case WPA_IF_P2P_CLIENT:
10229 case WPA_IF_P2P_GROUP:
10230 return NL80211_IFTYPE_P2P_CLIENT;
10231 case WPA_IF_AP_VLAN:
10232 return NL80211_IFTYPE_AP_VLAN;
10233 case WPA_IF_AP_BSS:
10234 return NL80211_IFTYPE_AP;
10235 case WPA_IF_P2P_GO:
10236 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010237 case WPA_IF_P2P_DEVICE:
10238 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010239 }
10240 return -1;
10241}
10242
10243
10244#ifdef CONFIG_P2P
10245
10246static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
10247{
10248 struct wpa_driver_nl80211_data *drv;
10249 dl_list_for_each(drv, &global->interfaces,
10250 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010251 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010252 return 1;
10253 }
10254 return 0;
10255}
10256
10257
10258static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
10259 u8 *new_addr)
10260{
10261 unsigned int idx;
10262
10263 if (!drv->global)
10264 return -1;
10265
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010266 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010267 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010268 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010269 new_addr[0] ^= idx << 2;
10270 if (!nl80211_addr_in_use(drv->global, new_addr))
10271 break;
10272 }
10273 if (idx == 64)
10274 return -1;
10275
10276 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
10277 MACSTR, MAC2STR(new_addr));
10278
10279 return 0;
10280}
10281
10282#endif /* CONFIG_P2P */
10283
10284
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010285struct wdev_info {
10286 u64 wdev_id;
10287 int wdev_id_set;
10288 u8 macaddr[ETH_ALEN];
10289};
10290
10291static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
10292{
10293 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10294 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10295 struct wdev_info *wi = arg;
10296
10297 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10298 genlmsg_attrlen(gnlh, 0), NULL);
10299 if (tb[NL80211_ATTR_WDEV]) {
10300 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
10301 wi->wdev_id_set = 1;
10302 }
10303
10304 if (tb[NL80211_ATTR_MAC])
10305 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
10306 ETH_ALEN);
10307
10308 return NL_SKIP;
10309}
10310
10311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010312static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
10313 const char *ifname, const u8 *addr,
10314 void *bss_ctx, void **drv_priv,
10315 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010316 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010317{
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010318 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010319 struct i802_bss *bss = priv;
10320 struct wpa_driver_nl80211_data *drv = bss->drv;
10321 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010322 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010323
10324 if (addr)
10325 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010326 nlmode = wpa_driver_nl80211_if_type(type);
10327 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
10328 struct wdev_info p2pdev_info;
10329
10330 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
10331 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
10332 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010333 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010334 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
10335 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10336 ifname);
10337 return -1;
10338 }
10339
10340 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10341 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10342 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10343 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10344 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10345 ifname,
10346 (long long unsigned int) p2pdev_info.wdev_id);
10347 } else {
10348 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010349 0, NULL, NULL, use_existing);
10350 if (use_existing && ifidx == -ENFILE) {
10351 added = 0;
10352 ifidx = if_nametoindex(ifname);
10353 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010354 return -1;
10355 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010356 }
10357
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010358 if (!addr) {
10359 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10360 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10361 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10362 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010363 if (added)
10364 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010365 return -1;
10366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010367 }
10368
10369#ifdef CONFIG_P2P
10370 if (!addr &&
10371 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10372 type == WPA_IF_P2P_GO)) {
10373 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010374 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010375
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010376 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010377 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010378 if (added)
10379 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010380 return -1;
10381 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010382 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010383 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10384 "for P2P group interface");
10385 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010386 if (added)
10387 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010388 return -1;
10389 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010390 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010391 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010392 if (added)
10393 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010394 return -1;
10395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010396 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010397 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010398 }
10399#endif /* CONFIG_P2P */
10400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010401 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010402 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10403 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010404 if (added)
10405 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010406 return -1;
10407 }
10408
10409 if (bridge &&
10410 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10411 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10412 "interface %s to a bridge %s",
10413 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010414 if (added)
10415 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010416 os_free(new_bss);
10417 return -1;
10418 }
10419
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010420 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10421 {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010422 if (added)
10423 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010424 os_free(new_bss);
10425 return -1;
10426 }
10427 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010428 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010429 new_bss->ifindex = ifidx;
10430 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010431 new_bss->next = drv->first_bss->next;
10432 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010433 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010434 new_bss->added_if = added;
10435 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010436 if (drv_priv)
10437 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010438 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010439
10440 /* Subscribe management frames for this WPA_IF_AP_BSS */
10441 if (nl80211_setup_ap(new_bss))
10442 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010444
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010445 if (drv->global)
10446 drv->global->if_add_ifindex = ifidx;
10447
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010448 /*
10449 * Some virtual interfaces need to process EAPOL packets and events on
10450 * the parent interface. This is used mainly with hostapd.
10451 */
10452 if (ifidx > 0 &&
10453 (drv->hostapd ||
10454 nlmode == NL80211_IFTYPE_AP_VLAN ||
10455 nlmode == NL80211_IFTYPE_WDS ||
10456 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010457 add_ifidx(drv, ifidx);
10458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010459 return 0;
10460}
10461
10462
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010463static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010464 enum wpa_driver_if_type type,
10465 const char *ifname)
10466{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010467 struct wpa_driver_nl80211_data *drv = bss->drv;
10468 int ifindex = if_nametoindex(ifname);
10469
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010470 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10471 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010472 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010473 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -070010474 else if (ifindex > 0 && !bss->added_if) {
10475 struct wpa_driver_nl80211_data *drv2;
10476 dl_list_for_each(drv2, &drv->global->interfaces,
10477 struct wpa_driver_nl80211_data, list)
10478 del_ifidx(drv2, ifindex);
10479 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010480
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010481 if (type != WPA_IF_AP_BSS)
10482 return 0;
10483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010484 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010485 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10486 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010487 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10488 "interface %s from bridge %s: %s",
10489 bss->ifname, bss->brname, strerror(errno));
10490 }
10491 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010492 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010493 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10494 "bridge %s: %s",
10495 bss->brname, strerror(errno));
10496 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010497
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010498 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010499 struct i802_bss *tbss;
10500
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010501 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10502 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010503 if (tbss->next == bss) {
10504 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010505 /* Unsubscribe management frames */
10506 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010507 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010508 if (!bss->added_if)
10509 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010510 os_free(bss);
10511 bss = NULL;
10512 break;
10513 }
10514 }
10515 if (bss)
10516 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10517 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010518 } else {
10519 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10520 nl80211_teardown_ap(bss);
10521 if (!bss->added_if && !drv->first_bss->next)
10522 wpa_driver_nl80211_del_beacon(drv);
10523 nl80211_destroy_bss(bss);
10524 if (!bss->added_if)
10525 i802_set_iface_flags(bss, 0);
10526 if (drv->first_bss->next) {
10527 drv->first_bss = drv->first_bss->next;
10528 drv->ctx = drv->first_bss->ctx;
10529 os_free(bss);
10530 } else {
10531 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010534
10535 return 0;
10536}
10537
10538
10539static int cookie_handler(struct nl_msg *msg, void *arg)
10540{
10541 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10542 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10543 u64 *cookie = arg;
10544 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10545 genlmsg_attrlen(gnlh, 0), NULL);
10546 if (tb[NL80211_ATTR_COOKIE])
10547 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10548 return NL_SKIP;
10549}
10550
10551
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010552static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010553 unsigned int freq, unsigned int wait,
10554 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010555 u64 *cookie_out, int no_cck, int no_ack,
10556 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010557{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010558 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010559 struct nl_msg *msg;
10560 u64 cookie;
10561 int ret = -1;
10562
10563 msg = nlmsg_alloc();
10564 if (!msg)
10565 return -1;
10566
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010567 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010568 "no_ack=%d offchanok=%d",
10569 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010570 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010571 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010572
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010573 if (nl80211_set_iface_id(msg, bss) < 0)
10574 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010575 if (freq)
10576 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010577 if (wait)
10578 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010579 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10580 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010581 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10582 if (no_cck)
10583 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10584 if (no_ack)
10585 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010587 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10588
10589 cookie = 0;
10590 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10591 msg = NULL;
10592 if (ret) {
10593 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010594 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10595 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010596 goto nla_put_failure;
10597 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010598 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010599 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10600 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010601
10602 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010603 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010604
10605nla_put_failure:
10606 nlmsg_free(msg);
10607 return ret;
10608}
10609
10610
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010611static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10612 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010613 unsigned int wait_time,
10614 const u8 *dst, const u8 *src,
10615 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010616 const u8 *data, size_t data_len,
10617 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010618{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010619 struct wpa_driver_nl80211_data *drv = bss->drv;
10620 int ret = -1;
10621 u8 *buf;
10622 struct ieee80211_hdr *hdr;
10623
10624 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010625 "freq=%u MHz wait=%d ms no_cck=%d)",
10626 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010627
10628 buf = os_zalloc(24 + data_len);
10629 if (buf == NULL)
10630 return ret;
10631 os_memcpy(buf + 24, data, data_len);
10632 hdr = (struct ieee80211_hdr *) buf;
10633 hdr->frame_control =
10634 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10635 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10636 os_memcpy(hdr->addr2, src, ETH_ALEN);
10637 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10638
Dmitry Shmidt56052862013-10-04 10:23:25 -070010639 if (is_ap_interface(drv->nlmode) &&
10640 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10641 (int) freq == bss->freq || drv->device_ap_sme ||
10642 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010643 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10644 0, freq, no_cck, 1,
10645 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010646 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010647 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010648 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010649 &drv->send_action_cookie,
10650 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010651
10652 os_free(buf);
10653 return ret;
10654}
10655
10656
10657static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10658{
10659 struct i802_bss *bss = priv;
10660 struct wpa_driver_nl80211_data *drv = bss->drv;
10661 struct nl_msg *msg;
10662 int ret;
10663
10664 msg = nlmsg_alloc();
10665 if (!msg)
10666 return;
10667
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010668 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10669 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010670 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010671
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010672 if (nl80211_set_iface_id(msg, bss) < 0)
10673 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010674 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10675
10676 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10677 msg = NULL;
10678 if (ret)
10679 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10680 "(%s)", ret, strerror(-ret));
10681
10682 nla_put_failure:
10683 nlmsg_free(msg);
10684}
10685
10686
10687static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10688 unsigned int duration)
10689{
10690 struct i802_bss *bss = priv;
10691 struct wpa_driver_nl80211_data *drv = bss->drv;
10692 struct nl_msg *msg;
10693 int ret;
10694 u64 cookie;
10695
10696 msg = nlmsg_alloc();
10697 if (!msg)
10698 return -1;
10699
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010700 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010701
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010702 if (nl80211_set_iface_id(msg, bss) < 0)
10703 goto nla_put_failure;
10704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010705 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10706 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10707
10708 cookie = 0;
10709 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010710 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010711 if (ret == 0) {
10712 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10713 "0x%llx for freq=%u MHz duration=%u",
10714 (long long unsigned int) cookie, freq, duration);
10715 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010716 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010717 return 0;
10718 }
10719 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10720 "(freq=%d duration=%u): %d (%s)",
10721 freq, duration, ret, strerror(-ret));
10722nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010723 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010724 return -1;
10725}
10726
10727
10728static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10729{
10730 struct i802_bss *bss = priv;
10731 struct wpa_driver_nl80211_data *drv = bss->drv;
10732 struct nl_msg *msg;
10733 int ret;
10734
10735 if (!drv->pending_remain_on_chan) {
10736 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10737 "to cancel");
10738 return -1;
10739 }
10740
10741 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10742 "0x%llx",
10743 (long long unsigned int) drv->remain_on_chan_cookie);
10744
10745 msg = nlmsg_alloc();
10746 if (!msg)
10747 return -1;
10748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010749 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010750
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010751 if (nl80211_set_iface_id(msg, bss) < 0)
10752 goto nla_put_failure;
10753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010754 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10755
10756 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010757 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010758 if (ret == 0)
10759 return 0;
10760 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10761 "%d (%s)", ret, strerror(-ret));
10762nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010763 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010764 return -1;
10765}
10766
10767
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010768static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010769{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010770 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010772 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010773 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -070010774 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
10775 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010776 /*
10777 * Do not disable Probe Request reporting that was
10778 * enabled in nl80211_setup_ap().
10779 */
10780 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10781 "Probe Request reporting nl_preq=%p while "
10782 "in AP mode", bss->nl_preq);
10783 } else if (bss->nl_preq) {
10784 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10785 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010786 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010787 }
10788 return 0;
10789 }
10790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010791 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010792 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010793 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010794 return 0;
10795 }
10796
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010797 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10798 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010799 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010800 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10801 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010803 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010804 (WLAN_FC_TYPE_MGMT << 2) |
10805 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010806 NULL, 0) < 0)
10807 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010808
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010809 nl80211_register_eloop_read(&bss->nl_preq,
10810 wpa_driver_nl80211_event_receive,
10811 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010812
10813 return 0;
10814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010815 out_err:
10816 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010817 return -1;
10818}
10819
10820
10821static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10822 int ifindex, int disabled)
10823{
10824 struct nl_msg *msg;
10825 struct nlattr *bands, *band;
10826 int ret;
10827
10828 msg = nlmsg_alloc();
10829 if (!msg)
10830 return -1;
10831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010832 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010833 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10834
10835 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10836 if (!bands)
10837 goto nla_put_failure;
10838
10839 /*
10840 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10841 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10842 * rates. All 5 GHz rates are left enabled.
10843 */
10844 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10845 if (!band)
10846 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010847 if (disabled) {
10848 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10849 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10850 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010851 nla_nest_end(msg, band);
10852
10853 nla_nest_end(msg, bands);
10854
10855 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10856 msg = NULL;
10857 if (ret) {
10858 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10859 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010860 } else
10861 drv->disabled_11b_rates = disabled;
10862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010863 return ret;
10864
10865nla_put_failure:
10866 nlmsg_free(msg);
10867 return -1;
10868}
10869
10870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010871static int wpa_driver_nl80211_deinit_ap(void *priv)
10872{
10873 struct i802_bss *bss = priv;
10874 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010875 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010876 return -1;
10877 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010878
10879 /*
10880 * If the P2P GO interface was dynamically added, then it is
10881 * possible that the interface change to station is not possible.
10882 */
10883 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10884 return 0;
10885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010886 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010887}
10888
10889
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010890static int wpa_driver_nl80211_stop_ap(void *priv)
10891{
10892 struct i802_bss *bss = priv;
10893 struct wpa_driver_nl80211_data *drv = bss->drv;
10894 if (!is_ap_interface(drv->nlmode))
10895 return -1;
10896 wpa_driver_nl80211_del_beacon(drv);
10897 bss->beacon_set = 0;
10898 return 0;
10899}
10900
10901
Dmitry Shmidt04949592012-07-19 12:16:46 -070010902static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10903{
10904 struct i802_bss *bss = priv;
10905 struct wpa_driver_nl80211_data *drv = bss->drv;
10906 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10907 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010908
10909 /*
10910 * If the P2P Client interface was dynamically added, then it is
10911 * possible that the interface change to station is not possible.
10912 */
10913 if (bss->if_dynamic)
10914 return 0;
10915
Dmitry Shmidt04949592012-07-19 12:16:46 -070010916 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10917}
10918
10919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010920static void wpa_driver_nl80211_resume(void *priv)
10921{
10922 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010923
10924 if (i802_set_iface_flags(bss, 1))
10925 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010926}
10927
10928
10929static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10930 const u8 *ies, size_t ies_len)
10931{
10932 struct i802_bss *bss = priv;
10933 struct wpa_driver_nl80211_data *drv = bss->drv;
10934 int ret;
10935 u8 *data, *pos;
10936 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010937 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010938
10939 if (action != 1) {
10940 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10941 "action %d", action);
10942 return -1;
10943 }
10944
10945 /*
10946 * Action frame payload:
10947 * Category[1] = 6 (Fast BSS Transition)
10948 * Action[1] = 1 (Fast BSS Transition Request)
10949 * STA Address
10950 * Target AP Address
10951 * FT IEs
10952 */
10953
10954 data_len = 2 + 2 * ETH_ALEN + ies_len;
10955 data = os_malloc(data_len);
10956 if (data == NULL)
10957 return -1;
10958 pos = data;
10959 *pos++ = 0x06; /* FT Action category */
10960 *pos++ = action;
10961 os_memcpy(pos, own_addr, ETH_ALEN);
10962 pos += ETH_ALEN;
10963 os_memcpy(pos, target_ap, ETH_ALEN);
10964 pos += ETH_ALEN;
10965 os_memcpy(pos, ies, ies_len);
10966
10967 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10968 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010969 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010970 os_free(data);
10971
10972 return ret;
10973}
10974
10975
10976static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10977{
10978 struct i802_bss *bss = priv;
10979 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010980 struct nl_msg *msg;
10981 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010982 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010983
10984 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10985 "hysteresis=%d", threshold, hysteresis);
10986
10987 msg = nlmsg_alloc();
10988 if (!msg)
10989 return -1;
10990
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010991 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010992
10993 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10994
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010995 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010996 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010997 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010998
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010999 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
11000 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
11001 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011002
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070011003 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011004 msg = NULL;
11005
11006nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011007 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070011008 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011009}
11010
11011
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011012static int get_channel_width(struct nl_msg *msg, void *arg)
11013{
11014 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11015 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11016 struct wpa_signal_info *sig_change = arg;
11017
11018 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11019 genlmsg_attrlen(gnlh, 0), NULL);
11020
11021 sig_change->center_frq1 = -1;
11022 sig_change->center_frq2 = -1;
11023 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
11024
11025 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
11026 sig_change->chanwidth = convert2width(
11027 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
11028 if (tb[NL80211_ATTR_CENTER_FREQ1])
11029 sig_change->center_frq1 =
11030 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
11031 if (tb[NL80211_ATTR_CENTER_FREQ2])
11032 sig_change->center_frq2 =
11033 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
11034 }
11035
11036 return NL_SKIP;
11037}
11038
11039
11040static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
11041 struct wpa_signal_info *sig)
11042{
11043 struct nl_msg *msg;
11044
11045 msg = nlmsg_alloc();
11046 if (!msg)
11047 return -ENOMEM;
11048
11049 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
11050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11051
11052 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
11053
11054nla_put_failure:
11055 nlmsg_free(msg);
11056 return -ENOBUFS;
11057}
11058
11059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011060static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
11061{
11062 struct i802_bss *bss = priv;
11063 struct wpa_driver_nl80211_data *drv = bss->drv;
11064 int res;
11065
11066 os_memset(si, 0, sizeof(*si));
11067 res = nl80211_get_link_signal(drv, si);
11068 if (res != 0)
11069 return res;
11070
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011071 res = nl80211_get_channel_width(drv, si);
11072 if (res != 0)
11073 return res;
11074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011075 return nl80211_get_link_noise(drv, si);
11076}
11077
11078
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011079static int wpa_driver_nl80211_shared_freq(void *priv)
11080{
11081 struct i802_bss *bss = priv;
11082 struct wpa_driver_nl80211_data *drv = bss->drv;
11083 struct wpa_driver_nl80211_data *driver;
11084 int freq = 0;
11085
11086 /*
11087 * If the same PHY is in connected state with some other interface,
11088 * then retrieve the assoc freq.
11089 */
11090 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
11091 drv->phyname);
11092
11093 dl_list_for_each(driver, &drv->global->interfaces,
11094 struct wpa_driver_nl80211_data, list) {
11095 if (drv == driver ||
11096 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011097 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011098 continue;
11099
11100 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
11101 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011102 driver->phyname, driver->first_bss->ifname,
11103 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070011104 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011105 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011106 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011107 freq = nl80211_get_assoc_freq(driver);
11108 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
11109 drv->phyname, freq);
11110 }
11111
11112 if (!freq)
11113 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
11114 "PHY (%s) in associated state", drv->phyname);
11115
11116 return freq;
11117}
11118
11119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011120static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
11121 int encrypt)
11122{
11123 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011124 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
11125 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011126}
11127
11128
11129static int nl80211_set_param(void *priv, const char *param)
11130{
11131 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
11132 if (param == NULL)
11133 return 0;
11134
11135#ifdef CONFIG_P2P
11136 if (os_strstr(param, "use_p2p_group_interface=1")) {
11137 struct i802_bss *bss = priv;
11138 struct wpa_driver_nl80211_data *drv = bss->drv;
11139
11140 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
11141 "interface");
11142 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
11143 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
11144 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011145
11146 if (os_strstr(param, "p2p_device=1")) {
11147 struct i802_bss *bss = priv;
11148 struct wpa_driver_nl80211_data *drv = bss->drv;
11149 drv->allow_p2p_device = 1;
11150 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011151#endif /* CONFIG_P2P */
11152
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011153 if (os_strstr(param, "use_monitor=1")) {
11154 struct i802_bss *bss = priv;
11155 struct wpa_driver_nl80211_data *drv = bss->drv;
11156 drv->use_monitor = 1;
11157 }
11158
11159 if (os_strstr(param, "force_connect_cmd=1")) {
11160 struct i802_bss *bss = priv;
11161 struct wpa_driver_nl80211_data *drv = bss->drv;
11162 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011163 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011164 }
11165
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080011166 if (os_strstr(param, "no_offchannel_tx=1")) {
11167 struct i802_bss *bss = priv;
11168 struct wpa_driver_nl80211_data *drv = bss->drv;
11169 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
11170 drv->test_use_roc_tx = 1;
11171 }
11172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011173 return 0;
11174}
11175
11176
11177static void * nl80211_global_init(void)
11178{
11179 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011180 struct netlink_config *cfg;
11181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011182 global = os_zalloc(sizeof(*global));
11183 if (global == NULL)
11184 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011185 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011186 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011187 global->if_add_ifindex = -1;
11188
11189 cfg = os_zalloc(sizeof(*cfg));
11190 if (cfg == NULL)
11191 goto err;
11192
11193 cfg->ctx = global;
11194 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
11195 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
11196 global->netlink = netlink_init(cfg);
11197 if (global->netlink == NULL) {
11198 os_free(cfg);
11199 goto err;
11200 }
11201
11202 if (wpa_driver_nl80211_init_nl_global(global) < 0)
11203 goto err;
11204
11205 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
11206 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011207 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
11208 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011209 goto err;
11210 }
11211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011212 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011213
11214err:
11215 nl80211_global_deinit(global);
11216 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011217}
11218
11219
11220static void nl80211_global_deinit(void *priv)
11221{
11222 struct nl80211_global *global = priv;
11223 if (global == NULL)
11224 return;
11225 if (!dl_list_empty(&global->interfaces)) {
11226 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
11227 "nl80211_global_deinit",
11228 dl_list_len(&global->interfaces));
11229 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011230
11231 if (global->netlink)
11232 netlink_deinit(global->netlink);
11233
11234 nl_destroy_handles(&global->nl);
11235
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011236 if (global->nl_event)
11237 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011238
11239 nl_cb_put(global->nl_cb);
11240
11241 if (global->ioctl_sock >= 0)
11242 close(global->ioctl_sock);
11243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011244 os_free(global);
11245}
11246
11247
11248static const char * nl80211_get_radio_name(void *priv)
11249{
11250 struct i802_bss *bss = priv;
11251 struct wpa_driver_nl80211_data *drv = bss->drv;
11252 return drv->phyname;
11253}
11254
11255
Jouni Malinen75ecf522011-06-27 15:19:46 -070011256static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
11257 const u8 *pmkid)
11258{
11259 struct nl_msg *msg;
11260
11261 msg = nlmsg_alloc();
11262 if (!msg)
11263 return -ENOMEM;
11264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011265 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011266
11267 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
11268 if (pmkid)
11269 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
11270 if (bssid)
11271 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
11272
11273 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11274 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011275 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011276 return -ENOBUFS;
11277}
11278
11279
11280static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11281{
11282 struct i802_bss *bss = priv;
11283 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
11284 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
11285}
11286
11287
11288static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11289{
11290 struct i802_bss *bss = priv;
11291 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
11292 MAC2STR(bssid));
11293 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
11294}
11295
11296
11297static int nl80211_flush_pmkid(void *priv)
11298{
11299 struct i802_bss *bss = priv;
11300 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
11301 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
11302}
11303
11304
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011305static void clean_survey_results(struct survey_results *survey_results)
11306{
11307 struct freq_survey *survey, *tmp;
11308
11309 if (dl_list_empty(&survey_results->survey_list))
11310 return;
11311
11312 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
11313 struct freq_survey, list) {
11314 dl_list_del(&survey->list);
11315 os_free(survey);
11316 }
11317}
11318
11319
11320static void add_survey(struct nlattr **sinfo, u32 ifidx,
11321 struct dl_list *survey_list)
11322{
11323 struct freq_survey *survey;
11324
11325 survey = os_zalloc(sizeof(struct freq_survey));
11326 if (!survey)
11327 return;
11328
11329 survey->ifidx = ifidx;
11330 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11331 survey->filled = 0;
11332
11333 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11334 survey->nf = (int8_t)
11335 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11336 survey->filled |= SURVEY_HAS_NF;
11337 }
11338
11339 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11340 survey->channel_time =
11341 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11342 survey->filled |= SURVEY_HAS_CHAN_TIME;
11343 }
11344
11345 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11346 survey->channel_time_busy =
11347 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11348 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11349 }
11350
11351 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11352 survey->channel_time_rx =
11353 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11354 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11355 }
11356
11357 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11358 survey->channel_time_tx =
11359 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11360 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11361 }
11362
11363 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)",
11364 survey->freq,
11365 survey->nf,
11366 (unsigned long int) survey->channel_time,
11367 (unsigned long int) survey->channel_time_busy,
11368 (unsigned long int) survey->channel_time_tx,
11369 (unsigned long int) survey->channel_time_rx,
11370 survey->filled);
11371
11372 dl_list_add_tail(survey_list, &survey->list);
11373}
11374
11375
11376static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11377 unsigned int freq_filter)
11378{
11379 if (!freq_filter)
11380 return 1;
11381
11382 return freq_filter == surveyed_freq;
11383}
11384
11385
11386static int survey_handler(struct nl_msg *msg, void *arg)
11387{
11388 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11389 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11390 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11391 struct survey_results *survey_results;
11392 u32 surveyed_freq = 0;
11393 u32 ifidx;
11394
11395 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11396 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11397 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11398 };
11399
11400 survey_results = (struct survey_results *) arg;
11401
11402 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11403 genlmsg_attrlen(gnlh, 0), NULL);
11404
Dmitry Shmidt97672262014-02-03 13:02:54 -080011405 if (!tb[NL80211_ATTR_IFINDEX])
11406 return NL_SKIP;
11407
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011408 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11409
11410 if (!tb[NL80211_ATTR_SURVEY_INFO])
11411 return NL_SKIP;
11412
11413 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11414 tb[NL80211_ATTR_SURVEY_INFO],
11415 survey_policy))
11416 return NL_SKIP;
11417
11418 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11419 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11420 return NL_SKIP;
11421 }
11422
11423 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11424
11425 if (!check_survey_ok(sinfo, surveyed_freq,
11426 survey_results->freq_filter))
11427 return NL_SKIP;
11428
11429 if (survey_results->freq_filter &&
11430 survey_results->freq_filter != surveyed_freq) {
11431 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11432 surveyed_freq);
11433 return NL_SKIP;
11434 }
11435
11436 add_survey(sinfo, ifidx, &survey_results->survey_list);
11437
11438 return NL_SKIP;
11439}
11440
11441
11442static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11443{
11444 struct i802_bss *bss = priv;
11445 struct wpa_driver_nl80211_data *drv = bss->drv;
11446 struct nl_msg *msg;
11447 int err = -ENOBUFS;
11448 union wpa_event_data data;
11449 struct survey_results *survey_results;
11450
11451 os_memset(&data, 0, sizeof(data));
11452 survey_results = &data.survey_results;
11453
11454 dl_list_init(&survey_results->survey_list);
11455
11456 msg = nlmsg_alloc();
11457 if (!msg)
11458 goto nla_put_failure;
11459
11460 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11461
11462 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11463
11464 if (freq)
11465 data.survey_results.freq_filter = freq;
11466
11467 do {
11468 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11469 err = send_and_recv_msgs(drv, msg, survey_handler,
11470 survey_results);
11471 } while (err > 0);
11472
11473 if (err) {
11474 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11475 goto out_clean;
11476 }
11477
11478 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11479
11480out_clean:
11481 clean_survey_results(survey_results);
11482nla_put_failure:
11483 return err;
11484}
11485
11486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011487static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11488 const u8 *replay_ctr)
11489{
11490 struct i802_bss *bss = priv;
11491 struct wpa_driver_nl80211_data *drv = bss->drv;
11492 struct nlattr *replay_nested;
11493 struct nl_msg *msg;
11494
11495 msg = nlmsg_alloc();
11496 if (!msg)
11497 return;
11498
11499 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11500
11501 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11502
11503 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11504 if (!replay_nested)
11505 goto nla_put_failure;
11506
11507 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11508 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11509 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11510 replay_ctr);
11511
11512 nla_nest_end(msg, replay_nested);
11513
11514 send_and_recv_msgs(drv, msg, NULL, NULL);
11515 return;
11516 nla_put_failure:
11517 nlmsg_free(msg);
11518}
11519
11520
11521static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11522 const u8 *addr, int qos)
11523{
11524 /* send data frame to poll STA and check whether
11525 * this frame is ACKed */
11526 struct {
11527 struct ieee80211_hdr hdr;
11528 u16 qos_ctl;
11529 } STRUCT_PACKED nulldata;
11530 size_t size;
11531
11532 /* Send data frame to poll STA and check whether this frame is ACKed */
11533
11534 os_memset(&nulldata, 0, sizeof(nulldata));
11535
11536 if (qos) {
11537 nulldata.hdr.frame_control =
11538 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11539 WLAN_FC_STYPE_QOS_NULL);
11540 size = sizeof(nulldata);
11541 } else {
11542 nulldata.hdr.frame_control =
11543 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11544 WLAN_FC_STYPE_NULLFUNC);
11545 size = sizeof(struct ieee80211_hdr);
11546 }
11547
11548 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11549 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11550 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11551 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11552
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011553 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11554 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011555 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11556 "send poll frame");
11557}
11558
11559static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11560 int qos)
11561{
11562 struct i802_bss *bss = priv;
11563 struct wpa_driver_nl80211_data *drv = bss->drv;
11564 struct nl_msg *msg;
11565
11566 if (!drv->poll_command_supported) {
11567 nl80211_send_null_frame(bss, own_addr, addr, qos);
11568 return;
11569 }
11570
11571 msg = nlmsg_alloc();
11572 if (!msg)
11573 return;
11574
11575 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11576
11577 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11578 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11579
11580 send_and_recv_msgs(drv, msg, NULL, NULL);
11581 return;
11582 nla_put_failure:
11583 nlmsg_free(msg);
11584}
11585
11586
11587static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11588{
11589 struct nl_msg *msg;
11590
11591 msg = nlmsg_alloc();
11592 if (!msg)
11593 return -ENOMEM;
11594
11595 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11596 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11597 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11598 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11599 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11600nla_put_failure:
11601 nlmsg_free(msg);
11602 return -ENOBUFS;
11603}
11604
11605
11606static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11607 int ctwindow)
11608{
11609 struct i802_bss *bss = priv;
11610
11611 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11612 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11613
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011614 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011615#ifdef ANDROID_P2P
11616 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011617#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011618 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011619#endif /* ANDROID_P2P */
11620 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011621
11622 if (legacy_ps == -1)
11623 return 0;
11624 if (legacy_ps != 0 && legacy_ps != 1)
11625 return -1; /* Not yet supported */
11626
11627 return nl80211_set_power_save(bss, legacy_ps);
11628}
11629
11630
Dmitry Shmidt051af732013-10-22 13:52:46 -070011631static int nl80211_start_radar_detection(void *priv,
11632 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011633{
11634 struct i802_bss *bss = priv;
11635 struct wpa_driver_nl80211_data *drv = bss->drv;
11636 struct nl_msg *msg;
11637 int ret;
11638
Dmitry Shmidt051af732013-10-22 13:52:46 -070011639 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)",
11640 freq->freq, freq->ht_enabled, freq->vht_enabled,
11641 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11642
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011643 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11644 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11645 "detection");
11646 return -1;
11647 }
11648
11649 msg = nlmsg_alloc();
11650 if (!msg)
11651 return -1;
11652
11653 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11654 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011655
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011656 if (nl80211_put_freq_params(msg, freq) < 0)
11657 goto nla_put_failure;
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011658
11659 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011660 msg = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011661 if (ret == 0)
11662 return 0;
11663 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11664 "%d (%s)", ret, strerror(-ret));
11665nla_put_failure:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011666 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011667 return -1;
11668}
11669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011670#ifdef CONFIG_TDLS
11671
11672static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11673 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070011674 u32 peer_capab, int initiator, const u8 *buf,
11675 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011676{
11677 struct i802_bss *bss = priv;
11678 struct wpa_driver_nl80211_data *drv = bss->drv;
11679 struct nl_msg *msg;
11680
11681 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11682 return -EOPNOTSUPP;
11683
11684 if (!dst)
11685 return -EINVAL;
11686
11687 msg = nlmsg_alloc();
11688 if (!msg)
11689 return -ENOMEM;
11690
11691 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11692 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11693 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11694 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11695 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11696 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011697 if (peer_capab) {
11698 /*
11699 * The internal enum tdls_peer_capability definition is
11700 * currently identical with the nl80211 enum
11701 * nl80211_tdls_peer_capability, so no conversion is needed
11702 * here.
11703 */
11704 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11705 }
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070011706 if (initiator)
11707 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_INITIATOR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011708 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11709
11710 return send_and_recv_msgs(drv, msg, NULL, NULL);
11711
11712nla_put_failure:
11713 nlmsg_free(msg);
11714 return -ENOBUFS;
11715}
11716
11717
11718static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11719{
11720 struct i802_bss *bss = priv;
11721 struct wpa_driver_nl80211_data *drv = bss->drv;
11722 struct nl_msg *msg;
11723 enum nl80211_tdls_operation nl80211_oper;
11724
11725 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11726 return -EOPNOTSUPP;
11727
11728 switch (oper) {
11729 case TDLS_DISCOVERY_REQ:
11730 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11731 break;
11732 case TDLS_SETUP:
11733 nl80211_oper = NL80211_TDLS_SETUP;
11734 break;
11735 case TDLS_TEARDOWN:
11736 nl80211_oper = NL80211_TDLS_TEARDOWN;
11737 break;
11738 case TDLS_ENABLE_LINK:
11739 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11740 break;
11741 case TDLS_DISABLE_LINK:
11742 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11743 break;
11744 case TDLS_ENABLE:
11745 return 0;
11746 case TDLS_DISABLE:
11747 return 0;
11748 default:
11749 return -EINVAL;
11750 }
11751
11752 msg = nlmsg_alloc();
11753 if (!msg)
11754 return -ENOMEM;
11755
11756 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11757 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11758 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11759 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11760
11761 return send_and_recv_msgs(drv, msg, NULL, NULL);
11762
11763nla_put_failure:
11764 nlmsg_free(msg);
11765 return -ENOBUFS;
11766}
11767
11768#endif /* CONFIG TDLS */
11769
11770
11771#ifdef ANDROID
11772
11773typedef struct android_wifi_priv_cmd {
11774 char *buf;
11775 int used_len;
11776 int total_len;
11777} android_wifi_priv_cmd;
11778
11779static int drv_errors = 0;
11780
11781static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11782{
11783 drv_errors++;
11784 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11785 drv_errors = 0;
11786 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11787 }
11788}
11789
11790
11791static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11792{
11793 struct wpa_driver_nl80211_data *drv = bss->drv;
11794 struct ifreq ifr;
11795 android_wifi_priv_cmd priv_cmd;
11796 char buf[MAX_DRV_CMD_SIZE];
11797 int ret;
11798
11799 os_memset(&ifr, 0, sizeof(ifr));
11800 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11801 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11802
11803 os_memset(buf, 0, sizeof(buf));
11804 os_strlcpy(buf, cmd, sizeof(buf));
11805
11806 priv_cmd.buf = buf;
11807 priv_cmd.used_len = sizeof(buf);
11808 priv_cmd.total_len = sizeof(buf);
11809 ifr.ifr_data = &priv_cmd;
11810
11811 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11812 if (ret < 0) {
11813 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11814 __func__);
11815 wpa_driver_send_hang_msg(drv);
11816 return ret;
11817 }
11818
11819 drv_errors = 0;
11820 return 0;
11821}
11822
11823
11824static int android_pno_start(struct i802_bss *bss,
11825 struct wpa_driver_scan_params *params)
11826{
11827 struct wpa_driver_nl80211_data *drv = bss->drv;
11828 struct ifreq ifr;
11829 android_wifi_priv_cmd priv_cmd;
11830 int ret = 0, i = 0, bp;
11831 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11832
11833 bp = WEXT_PNOSETUP_HEADER_SIZE;
11834 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11835 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11836 buf[bp++] = WEXT_PNO_TLV_VERSION;
11837 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11838 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11839
11840 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11841 /* Check that there is enough space needed for 1 more SSID, the
11842 * other sections and null termination */
11843 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11844 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11845 break;
11846 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11847 params->ssids[i].ssid,
11848 params->ssids[i].ssid_len);
11849 buf[bp++] = WEXT_PNO_SSID_SECTION;
11850 buf[bp++] = params->ssids[i].ssid_len;
11851 os_memcpy(&buf[bp], params->ssids[i].ssid,
11852 params->ssids[i].ssid_len);
11853 bp += params->ssids[i].ssid_len;
11854 i++;
11855 }
11856
11857 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11858 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11859 WEXT_PNO_SCAN_INTERVAL);
11860 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11861
11862 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11863 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11864 WEXT_PNO_REPEAT);
11865 bp += WEXT_PNO_REPEAT_LENGTH;
11866
11867 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11868 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11869 WEXT_PNO_MAX_REPEAT);
11870 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11871
11872 memset(&ifr, 0, sizeof(ifr));
11873 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011874 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011875
11876 priv_cmd.buf = buf;
11877 priv_cmd.used_len = bp;
11878 priv_cmd.total_len = bp;
11879 ifr.ifr_data = &priv_cmd;
11880
11881 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11882
11883 if (ret < 0) {
11884 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11885 ret);
11886 wpa_driver_send_hang_msg(drv);
11887 return ret;
11888 }
11889
11890 drv_errors = 0;
11891
11892 return android_priv_cmd(bss, "PNOFORCE 1");
11893}
11894
11895
11896static int android_pno_stop(struct i802_bss *bss)
11897{
11898 return android_priv_cmd(bss, "PNOFORCE 0");
11899}
11900
11901#endif /* ANDROID */
11902
11903
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011904static int driver_nl80211_set_key(const char *ifname, void *priv,
11905 enum wpa_alg alg, const u8 *addr,
11906 int key_idx, int set_tx,
11907 const u8 *seq, size_t seq_len,
11908 const u8 *key, size_t key_len)
11909{
11910 struct i802_bss *bss = priv;
11911 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11912 set_tx, seq, seq_len, key, key_len);
11913}
11914
11915
11916static int driver_nl80211_scan2(void *priv,
11917 struct wpa_driver_scan_params *params)
11918{
11919 struct i802_bss *bss = priv;
11920 return wpa_driver_nl80211_scan(bss, params);
11921}
11922
11923
11924static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11925 int reason_code)
11926{
11927 struct i802_bss *bss = priv;
11928 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11929}
11930
11931
11932static int driver_nl80211_authenticate(void *priv,
11933 struct wpa_driver_auth_params *params)
11934{
11935 struct i802_bss *bss = priv;
11936 return wpa_driver_nl80211_authenticate(bss, params);
11937}
11938
11939
11940static void driver_nl80211_deinit(void *priv)
11941{
11942 struct i802_bss *bss = priv;
11943 wpa_driver_nl80211_deinit(bss);
11944}
11945
11946
11947static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11948 const char *ifname)
11949{
11950 struct i802_bss *bss = priv;
11951 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11952}
11953
11954
11955static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11956 size_t data_len, int noack)
11957{
11958 struct i802_bss *bss = priv;
11959 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11960 0, 0, 0, 0);
11961}
11962
11963
11964static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11965{
11966 struct i802_bss *bss = priv;
11967 return wpa_driver_nl80211_sta_remove(bss, addr);
11968}
11969
11970
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011971static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11972 const char *ifname, int vlan_id)
11973{
11974 struct i802_bss *bss = priv;
11975 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11976}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011977
11978
11979static int driver_nl80211_read_sta_data(void *priv,
11980 struct hostap_sta_driver_data *data,
11981 const u8 *addr)
11982{
11983 struct i802_bss *bss = priv;
11984 return i802_read_sta_data(bss, data, addr);
11985}
11986
11987
11988static int driver_nl80211_send_action(void *priv, unsigned int freq,
11989 unsigned int wait_time,
11990 const u8 *dst, const u8 *src,
11991 const u8 *bssid,
11992 const u8 *data, size_t data_len,
11993 int no_cck)
11994{
11995 struct i802_bss *bss = priv;
11996 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11997 bssid, data, data_len, no_cck);
11998}
11999
12000
12001static int driver_nl80211_probe_req_report(void *priv, int report)
12002{
12003 struct i802_bss *bss = priv;
12004 return wpa_driver_nl80211_probe_req_report(bss, report);
12005}
12006
12007
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012008static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
12009 const u8 *ies, size_t ies_len)
12010{
12011 int ret;
12012 struct nl_msg *msg;
12013 struct i802_bss *bss = priv;
12014 struct wpa_driver_nl80211_data *drv = bss->drv;
12015 u16 mdid = WPA_GET_LE16(md);
12016
12017 msg = nlmsg_alloc();
12018 if (!msg)
12019 return -ENOMEM;
12020
12021 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
12022 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
12023 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12024 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
12025 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
12026
12027 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12028 if (ret) {
12029 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
12030 "err=%d (%s)", ret, strerror(-ret));
12031 }
12032
12033 return ret;
12034
12035nla_put_failure:
12036 nlmsg_free(msg);
12037 return -ENOBUFS;
12038}
12039
12040
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012041const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
12042{
12043 struct i802_bss *bss = priv;
12044 struct wpa_driver_nl80211_data *drv = bss->drv;
12045
12046 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
12047 return NULL;
12048
12049 return bss->addr;
12050}
12051
12052
Dmitry Shmidt56052862013-10-04 10:23:25 -070012053static const char * scan_state_str(enum scan_states scan_state)
12054{
12055 switch (scan_state) {
12056 case NO_SCAN:
12057 return "NO_SCAN";
12058 case SCAN_REQUESTED:
12059 return "SCAN_REQUESTED";
12060 case SCAN_STARTED:
12061 return "SCAN_STARTED";
12062 case SCAN_COMPLETED:
12063 return "SCAN_COMPLETED";
12064 case SCAN_ABORTED:
12065 return "SCAN_ABORTED";
12066 case SCHED_SCAN_STARTED:
12067 return "SCHED_SCAN_STARTED";
12068 case SCHED_SCAN_STOPPED:
12069 return "SCHED_SCAN_STOPPED";
12070 case SCHED_SCAN_RESULTS:
12071 return "SCHED_SCAN_RESULTS";
12072 }
12073
12074 return "??";
12075}
12076
12077
12078static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
12079{
12080 struct i802_bss *bss = priv;
12081 struct wpa_driver_nl80211_data *drv = bss->drv;
12082 int res;
12083 char *pos, *end;
12084
12085 pos = buf;
12086 end = buf + buflen;
12087
12088 res = os_snprintf(pos, end - pos,
12089 "ifindex=%d\n"
12090 "ifname=%s\n"
12091 "brname=%s\n"
12092 "addr=" MACSTR "\n"
12093 "freq=%d\n"
12094 "%s%s%s%s%s",
12095 bss->ifindex,
12096 bss->ifname,
12097 bss->brname,
12098 MAC2STR(bss->addr),
12099 bss->freq,
12100 bss->beacon_set ? "beacon_set=1\n" : "",
12101 bss->added_if_into_bridge ?
12102 "added_if_into_bridge=1\n" : "",
12103 bss->added_bridge ? "added_bridge=1\n" : "",
12104 bss->in_deinit ? "in_deinit=1\n" : "",
12105 bss->if_dynamic ? "if_dynamic=1\n" : "");
12106 if (res < 0 || res >= end - pos)
12107 return pos - buf;
12108 pos += res;
12109
12110 if (bss->wdev_id_set) {
12111 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
12112 (unsigned long long) bss->wdev_id);
12113 if (res < 0 || res >= end - pos)
12114 return pos - buf;
12115 pos += res;
12116 }
12117
12118 res = os_snprintf(pos, end - pos,
12119 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012120 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070012121 "drv_ifindex=%d\n"
12122 "operstate=%d\n"
12123 "scan_state=%s\n"
12124 "auth_bssid=" MACSTR "\n"
12125 "auth_attempt_bssid=" MACSTR "\n"
12126 "bssid=" MACSTR "\n"
12127 "prev_bssid=" MACSTR "\n"
12128 "associated=%d\n"
12129 "assoc_freq=%u\n"
12130 "monitor_sock=%d\n"
12131 "monitor_ifidx=%d\n"
12132 "monitor_refcount=%d\n"
12133 "last_mgmt_freq=%u\n"
12134 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070012135 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070012136 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012137 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -070012138 drv->ifindex,
12139 drv->operstate,
12140 scan_state_str(drv->scan_state),
12141 MAC2STR(drv->auth_bssid),
12142 MAC2STR(drv->auth_attempt_bssid),
12143 MAC2STR(drv->bssid),
12144 MAC2STR(drv->prev_bssid),
12145 drv->associated,
12146 drv->assoc_freq,
12147 drv->monitor_sock,
12148 drv->monitor_ifidx,
12149 drv->monitor_refcount,
12150 drv->last_mgmt_freq,
12151 drv->eapol_tx_sock,
12152 drv->ignore_if_down_event ?
12153 "ignore_if_down_event=1\n" : "",
12154 drv->scan_complete_events ?
12155 "scan_complete_events=1\n" : "",
12156 drv->disabled_11b_rates ?
12157 "disabled_11b_rates=1\n" : "",
12158 drv->pending_remain_on_chan ?
12159 "pending_remain_on_chan=1\n" : "",
12160 drv->in_interface_list ? "in_interface_list=1\n" : "",
12161 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
12162 drv->poll_command_supported ?
12163 "poll_command_supported=1\n" : "",
12164 drv->data_tx_status ? "data_tx_status=1\n" : "",
12165 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
12166 drv->retry_auth ? "retry_auth=1\n" : "",
12167 drv->use_monitor ? "use_monitor=1\n" : "",
12168 drv->ignore_next_local_disconnect ?
12169 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070012170 drv->ignore_next_local_deauth ?
12171 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070012172 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
12173 if (res < 0 || res >= end - pos)
12174 return pos - buf;
12175 pos += res;
12176
12177 if (drv->has_capability) {
12178 res = os_snprintf(pos, end - pos,
12179 "capa.key_mgmt=0x%x\n"
12180 "capa.enc=0x%x\n"
12181 "capa.auth=0x%x\n"
12182 "capa.flags=0x%x\n"
12183 "capa.max_scan_ssids=%d\n"
12184 "capa.max_sched_scan_ssids=%d\n"
12185 "capa.sched_scan_supported=%d\n"
12186 "capa.max_match_sets=%d\n"
12187 "capa.max_remain_on_chan=%u\n"
12188 "capa.max_stations=%u\n"
12189 "capa.probe_resp_offloads=0x%x\n"
12190 "capa.max_acl_mac_addrs=%u\n"
12191 "capa.num_multichan_concurrent=%u\n",
12192 drv->capa.key_mgmt,
12193 drv->capa.enc,
12194 drv->capa.auth,
12195 drv->capa.flags,
12196 drv->capa.max_scan_ssids,
12197 drv->capa.max_sched_scan_ssids,
12198 drv->capa.sched_scan_supported,
12199 drv->capa.max_match_sets,
12200 drv->capa.max_remain_on_chan,
12201 drv->capa.max_stations,
12202 drv->capa.probe_resp_offloads,
12203 drv->capa.max_acl_mac_addrs,
12204 drv->capa.num_multichan_concurrent);
12205 if (res < 0 || res >= end - pos)
12206 return pos - buf;
12207 pos += res;
12208 }
12209
12210 return pos - buf;
12211}
12212
12213
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012214static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
12215{
12216 if (settings->head)
12217 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
12218 settings->head_len, settings->head);
12219
12220 if (settings->tail)
12221 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
12222 settings->tail_len, settings->tail);
12223
12224 if (settings->beacon_ies)
12225 NLA_PUT(msg, NL80211_ATTR_IE,
12226 settings->beacon_ies_len, settings->beacon_ies);
12227
12228 if (settings->proberesp_ies)
12229 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
12230 settings->proberesp_ies_len, settings->proberesp_ies);
12231
12232 if (settings->assocresp_ies)
12233 NLA_PUT(msg,
12234 NL80211_ATTR_IE_ASSOC_RESP,
12235 settings->assocresp_ies_len, settings->assocresp_ies);
12236
12237 if (settings->probe_resp)
12238 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
12239 settings->probe_resp_len, settings->probe_resp);
12240
12241 return 0;
12242
12243nla_put_failure:
12244 return -ENOBUFS;
12245}
12246
12247
12248static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
12249{
12250 struct nl_msg *msg;
12251 struct i802_bss *bss = priv;
12252 struct wpa_driver_nl80211_data *drv = bss->drv;
12253 struct nlattr *beacon_csa;
12254 int ret = -ENOBUFS;
12255
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012256 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 -080012257 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012258 settings->freq_params.freq, settings->freq_params.bandwidth,
12259 settings->freq_params.center_freq1,
12260 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012262 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012263 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
12264 return -EOPNOTSUPP;
12265 }
12266
12267 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
12268 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
12269 return -EOPNOTSUPP;
12270
12271 /* check settings validity */
12272 if (!settings->beacon_csa.tail ||
12273 ((settings->beacon_csa.tail_len <=
12274 settings->counter_offset_beacon) ||
12275 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
12276 settings->cs_count)))
12277 return -EINVAL;
12278
12279 if (settings->beacon_csa.probe_resp &&
12280 ((settings->beacon_csa.probe_resp_len <=
12281 settings->counter_offset_presp) ||
12282 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
12283 settings->cs_count)))
12284 return -EINVAL;
12285
12286 msg = nlmsg_alloc();
12287 if (!msg)
12288 return -ENOMEM;
12289
12290 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -070012291 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012292 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
12293 ret = nl80211_put_freq_params(msg, &settings->freq_params);
12294 if (ret)
12295 goto error;
12296
12297 if (settings->block_tx)
12298 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12299
12300 /* beacon_after params */
12301 ret = set_beacon_data(msg, &settings->beacon_after);
12302 if (ret)
12303 goto error;
12304
12305 /* beacon_csa params */
12306 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12307 if (!beacon_csa)
12308 goto nla_put_failure;
12309
12310 ret = set_beacon_data(msg, &settings->beacon_csa);
12311 if (ret)
12312 goto error;
12313
12314 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12315 settings->counter_offset_beacon);
12316
12317 if (settings->beacon_csa.probe_resp)
12318 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12319 settings->counter_offset_presp);
12320
12321 nla_nest_end(msg, beacon_csa);
12322 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12323 if (ret) {
12324 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12325 ret, strerror(-ret));
12326 }
12327 return ret;
12328
12329nla_put_failure:
12330 ret = -ENOBUFS;
12331error:
12332 nlmsg_free(msg);
12333 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12334 return ret;
12335}
12336
12337
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012338#ifdef CONFIG_TESTING_OPTIONS
12339static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12340{
12341 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12342 struct wpabuf *buf = arg;
12343
12344 if (!buf)
12345 return NL_SKIP;
12346
12347 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12348 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12349 return NL_SKIP;
12350 }
12351
12352 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12353 genlmsg_attrlen(gnlh, 0));
12354
12355 return NL_SKIP;
12356}
12357#endif /* CONFIG_TESTING_OPTIONS */
12358
12359
12360static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12361{
12362 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12363 struct nlattr *nl_vendor_reply, *nl;
12364 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12365 struct wpabuf *buf = arg;
12366 int rem;
12367
12368 if (!buf)
12369 return NL_SKIP;
12370
12371 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12372 genlmsg_attrlen(gnlh, 0), NULL);
12373 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12374
12375 if (!nl_vendor_reply)
12376 return NL_SKIP;
12377
12378 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12379 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12380 return NL_SKIP;
12381 }
12382
12383 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12384 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12385 }
12386
12387 return NL_SKIP;
12388}
12389
12390
12391static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12392 unsigned int subcmd, const u8 *data,
12393 size_t data_len, struct wpabuf *buf)
12394{
12395 struct i802_bss *bss = priv;
12396 struct wpa_driver_nl80211_data *drv = bss->drv;
12397 struct nl_msg *msg;
12398 int ret;
12399
12400 msg = nlmsg_alloc();
12401 if (!msg)
12402 return -ENOMEM;
12403
12404#ifdef CONFIG_TESTING_OPTIONS
12405 if (vendor_id == 0xffffffff) {
12406 nl80211_cmd(drv, msg, 0, subcmd);
12407 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12408 0)
12409 goto nla_put_failure;
12410 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12411 if (ret)
12412 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12413 ret);
12414 return ret;
12415 }
12416#endif /* CONFIG_TESTING_OPTIONS */
12417
12418 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12419 if (nl80211_set_iface_id(msg, bss) < 0)
12420 goto nla_put_failure;
12421 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12422 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12423 if (data)
12424 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12425
12426 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12427 if (ret)
12428 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12429 ret);
12430 return ret;
12431
12432nla_put_failure:
12433 nlmsg_free(msg);
12434 return -ENOBUFS;
12435}
12436
12437
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012438static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12439 u8 qos_map_set_len)
12440{
12441 struct i802_bss *bss = priv;
12442 struct wpa_driver_nl80211_data *drv = bss->drv;
12443 struct nl_msg *msg;
12444 int ret;
12445
12446 msg = nlmsg_alloc();
12447 if (!msg)
12448 return -ENOMEM;
12449
12450 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12451 qos_map_set, qos_map_set_len);
12452
12453 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12454 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12455 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12456
12457 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12458 if (ret)
12459 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12460
12461 return ret;
12462
12463nla_put_failure:
12464 nlmsg_free(msg);
12465 return -ENOBUFS;
12466}
12467
12468
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012469static int nl80211_set_wowlan(void *priv,
12470 const struct wowlan_triggers *triggers)
12471{
12472 struct i802_bss *bss = priv;
12473 struct wpa_driver_nl80211_data *drv = bss->drv;
12474 struct nl_msg *msg;
12475 struct nlattr *wowlan_triggers;
12476 int ret;
12477
12478 msg = nlmsg_alloc();
12479 if (!msg)
12480 return -ENOMEM;
12481
12482 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12483
12484 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12486
12487 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12488 if (!wowlan_triggers)
12489 goto nla_put_failure;
12490
12491 if (triggers->any)
12492 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12493 if (triggers->disconnect)
12494 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12495 if (triggers->magic_pkt)
12496 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12497 if (triggers->gtk_rekey_failure)
12498 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12499 if (triggers->eap_identity_req)
12500 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12501 if (triggers->four_way_handshake)
12502 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12503 if (triggers->rfkill_release)
12504 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12505
12506 nla_nest_end(msg, wowlan_triggers);
12507
12508 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12509 if (ret)
12510 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12511
12512 return ret;
12513
12514nla_put_failure:
12515 nlmsg_free(msg);
12516 return -ENOBUFS;
12517}
12518
12519
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012520static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
12521{
12522 struct i802_bss *bss = priv;
12523 struct wpa_driver_nl80211_data *drv = bss->drv;
12524 struct nl_msg *msg;
12525 struct nlattr *params;
12526
12527 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
12528
12529 if (!drv->roaming_vendor_cmd_avail) {
12530 wpa_printf(MSG_DEBUG,
12531 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
12532 return -1;
12533 }
12534
12535 msg = nlmsg_alloc();
12536 if (!msg)
12537 return -ENOMEM;
12538
12539 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12540
12541 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12542 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
12543 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12544 QCA_NL80211_VENDOR_SUBCMD_ROAMING);
12545
12546 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
12547 if (!params)
12548 goto nla_put_failure;
12549 NLA_PUT_U32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
12550 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
12551 QCA_ROAMING_NOT_ALLOWED);
12552 if (bssid)
12553 NLA_PUT(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid);
12554 nla_nest_end(msg, params);
12555
12556 return send_and_recv_msgs(drv, msg, NULL, NULL);
12557
12558 nla_put_failure:
12559 nlmsg_free(msg);
12560 return -1;
12561}
12562
12563
12564static int nl80211_set_mac_addr(void *priv, const u8 *addr)
12565{
12566 struct i802_bss *bss = priv;
12567 struct wpa_driver_nl80211_data *drv = bss->drv;
12568 int new_addr = addr != NULL;
12569
12570 if (!addr)
12571 addr = drv->perm_addr;
12572
12573 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
12574 return -1;
12575
12576 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
12577 {
12578 wpa_printf(MSG_DEBUG,
12579 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12580 bss->ifname, MAC2STR(addr));
12581 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
12582 1) < 0) {
12583 wpa_printf(MSG_DEBUG,
12584 "nl80211: Could not restore interface UP after failed set_mac_addr");
12585 }
12586 return -1;
12587 }
12588
12589 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
12590 bss->ifname, MAC2STR(addr));
12591 drv->addr_changed = new_addr;
12592 os_memcpy(bss->addr, addr, ETH_ALEN);
12593
12594 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12595 {
12596 wpa_printf(MSG_DEBUG,
12597 "nl80211: Could not restore interface UP after set_mac_addr");
12598 }
12599
12600 return 0;
12601}
12602
12603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012604const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12605 .name = "nl80211",
12606 .desc = "Linux nl80211/cfg80211",
12607 .get_bssid = wpa_driver_nl80211_get_bssid,
12608 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012609 .set_key = driver_nl80211_set_key,
12610 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012611 .sched_scan = wpa_driver_nl80211_sched_scan,
12612 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012613 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012614 .deauthenticate = driver_nl80211_deauthenticate,
12615 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012616 .associate = wpa_driver_nl80211_associate,
12617 .global_init = nl80211_global_init,
12618 .global_deinit = nl80211_global_deinit,
12619 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012620 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012621 .get_capa = wpa_driver_nl80211_get_capa,
12622 .set_operstate = wpa_driver_nl80211_set_operstate,
12623 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12624 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012625 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012626 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012627 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012628 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012629 .if_remove = driver_nl80211_if_remove,
12630 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012631 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12632 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012633 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012634 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12635 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012636 .hapd_init = i802_init,
12637 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012638 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012639 .get_seqnum = i802_get_seqnum,
12640 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012641 .get_inact_sec = i802_get_inact_sec,
12642 .sta_clear_stats = i802_sta_clear_stats,
12643 .set_rts = i802_set_rts,
12644 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012645 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012646 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012647 .sta_deauth = i802_sta_deauth,
12648 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012649 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012650 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012651 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012652 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12653 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12654 .cancel_remain_on_channel =
12655 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012656 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012657 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012658 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012659 .resume = wpa_driver_nl80211_resume,
12660 .send_ft_action = nl80211_send_ft_action,
12661 .signal_monitor = nl80211_signal_monitor,
12662 .signal_poll = nl80211_signal_poll,
12663 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012664 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012665 .set_param = nl80211_set_param,
12666 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012667 .add_pmkid = nl80211_add_pmkid,
12668 .remove_pmkid = nl80211_remove_pmkid,
12669 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012670 .set_rekey_info = nl80211_set_rekey_info,
12671 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012672 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012673 .start_dfs_cac = nl80211_start_radar_detection,
12674 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012675#ifdef CONFIG_TDLS
12676 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12677 .tdls_oper = nl80211_tdls_oper,
12678#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012679 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012680 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012681 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012682 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012683 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012684#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012685 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012686 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012687 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012688#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012689#ifdef ANDROID
12690 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012691#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012692 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012693 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012694 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012695 .roaming = nl80211_roaming,
12696 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012697};