blob: d0304743f35b3d501f9f920f7d3ddda519e8513f [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;
1594
1595 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1596 /*
1597 * Avoid reporting two association events that would confuse
1598 * the core code.
1599 */
1600 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1601 "when using userspace SME", cmd);
1602 return;
1603 }
1604
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001605 if (cmd == NL80211_CMD_CONNECT)
1606 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1607 else if (cmd == NL80211_CMD_ROAM)
1608 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 os_memset(&event, 0, sizeof(event));
1611 if (cmd == NL80211_CMD_CONNECT &&
1612 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1613 if (addr)
1614 event.assoc_reject.bssid = nla_data(addr);
1615 if (resp_ie) {
1616 event.assoc_reject.resp_ies = nla_data(resp_ie);
1617 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1618 }
1619 event.assoc_reject.status_code = nla_get_u16(status);
1620 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1621 return;
1622 }
1623
1624 drv->associated = 1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001625 if (addr) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001627 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629
1630 if (req_ie) {
1631 event.assoc_info.req_ies = nla_data(req_ie);
1632 event.assoc_info.req_ies_len = nla_len(req_ie);
1633 }
1634 if (resp_ie) {
1635 event.assoc_info.resp_ies = nla_data(resp_ie);
1636 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1637 }
1638
Jouni Malinen87fd2792011-05-16 18:35:42 +03001639 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1642}
1643
1644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001645static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001646 struct nlattr *reason, struct nlattr *addr,
1647 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001648{
1649 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001650 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651
1652 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1653 /*
1654 * Avoid reporting two disassociation events that could
1655 * confuse the core code.
1656 */
1657 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1658 "event when using userspace SME");
1659 return;
1660 }
1661
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001662 if (drv->ignore_next_local_disconnect) {
1663 drv->ignore_next_local_disconnect = 0;
1664 if (locally_generated) {
1665 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1666 "event triggered during reassociation");
1667 return;
1668 }
1669 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1670 "disconnect but got another disconnect "
1671 "event first");
1672 }
1673
1674 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001675 nl80211_mark_disconnected(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001676 os_memset(&data, 0, sizeof(data));
1677 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001678 data.deauth_info.reason_code = nla_get_u16(reason);
1679 data.deauth_info.locally_generated = by_ap == NULL;
1680 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1681}
1682
1683
Dmitry Shmidt97672262014-02-03 13:02:54 -08001684static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
1685{
1686 int freq1 = 0;
1687
1688 switch (convert2width(width)) {
1689 case CHAN_WIDTH_20_NOHT:
1690 case CHAN_WIDTH_20:
1691 return 0;
1692 case CHAN_WIDTH_40:
1693 freq1 = cf1 - 10;
1694 break;
1695 case CHAN_WIDTH_80:
1696 freq1 = cf1 - 30;
1697 break;
1698 case CHAN_WIDTH_160:
1699 freq1 = cf1 - 70;
1700 break;
1701 case CHAN_WIDTH_UNKNOWN:
1702 case CHAN_WIDTH_80P80:
1703 /* FIXME: implement this */
1704 return 0;
1705 }
1706
1707 return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
1708}
1709
1710
Dmitry Shmidt04949592012-07-19 12:16:46 -07001711static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001712 struct nlattr *ifindex, struct nlattr *freq,
1713 struct nlattr *type, struct nlattr *bw,
1714 struct nlattr *cf1, struct nlattr *cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001715{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001716 struct i802_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001717 union wpa_event_data data;
1718 int ht_enabled = 1;
1719 int chan_offset = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001720 int ifidx;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001721
1722 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1723
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001724 if (!freq)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001725 return;
1726
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001727 ifidx = nla_get_u32(ifindex);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001728 bss = get_bss_ifindex(drv, ifidx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001729 if (bss == NULL) {
1730 wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
1731 ifidx);
1732 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001733 }
1734
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001735 if (type) {
1736 switch (nla_get_u32(type)) {
1737 case NL80211_CHAN_NO_HT:
1738 ht_enabled = 0;
1739 break;
1740 case NL80211_CHAN_HT20:
1741 break;
1742 case NL80211_CHAN_HT40PLUS:
1743 chan_offset = 1;
1744 break;
1745 case NL80211_CHAN_HT40MINUS:
1746 chan_offset = -1;
1747 break;
1748 }
Dmitry Shmidt97672262014-02-03 13:02:54 -08001749 } else if (bw && cf1) {
1750 /* This can happen for example with VHT80 ch switch */
1751 chan_offset = calculate_chan_offset(nla_get_u32(bw),
1752 nla_get_u32(freq),
1753 nla_get_u32(cf1),
1754 cf2 ? nla_get_u32(cf2) : 0);
1755 } else {
1756 wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001757 }
1758
1759 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001760 data.ch_switch.freq = nla_get_u32(freq);
1761 data.ch_switch.ht_enabled = ht_enabled;
1762 data.ch_switch.ch_offset = chan_offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001763 if (bw)
1764 data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
1765 if (cf1)
1766 data.ch_switch.cf1 = nla_get_u32(cf1);
1767 if (cf2)
1768 data.ch_switch.cf2 = nla_get_u32(cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001769
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001770 bss->freq = data.ch_switch.freq;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001771
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001772 wpa_supplicant_event(bss->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001773}
1774
1775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1777 enum nl80211_commands cmd, struct nlattr *addr)
1778{
1779 union wpa_event_data event;
1780 enum wpa_event_type ev;
1781
1782 if (nla_len(addr) != ETH_ALEN)
1783 return;
1784
1785 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1786 cmd, MAC2STR((u8 *) nla_data(addr)));
1787
1788 if (cmd == NL80211_CMD_AUTHENTICATE)
1789 ev = EVENT_AUTH_TIMED_OUT;
1790 else if (cmd == NL80211_CMD_ASSOCIATE)
1791 ev = EVENT_ASSOC_TIMED_OUT;
1792 else
1793 return;
1794
1795 os_memset(&event, 0, sizeof(event));
1796 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1797 wpa_supplicant_event(drv->ctx, ev, &event);
1798}
1799
1800
Dmitry Shmidt98660862014-03-11 17:26:21 -07001801static void mlme_event_mgmt(struct i802_bss *bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001802 struct nlattr *freq, struct nlattr *sig,
1803 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001805 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 const struct ieee80211_mgmt *mgmt;
1807 union wpa_event_data event;
1808 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001809 int ssi_signal = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001810 int rx_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001812 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 mgmt = (const struct ieee80211_mgmt *) frame;
1814 if (len < 24) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001815 wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 return;
1817 }
1818
1819 fc = le_to_host16(mgmt->frame_control);
1820 stype = WLAN_FC_GET_STYPE(fc);
1821
Dmitry Shmidt04949592012-07-19 12:16:46 -07001822 if (sig)
1823 ssi_signal = (s32) nla_get_u32(sig);
1824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 os_memset(&event, 0, sizeof(event));
1826 if (freq) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001827 event.rx_mgmt.freq = nla_get_u32(freq);
1828 rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07001830 wpa_printf(MSG_DEBUG,
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001831 "nl80211: RX frame sa=" MACSTR
1832 " freq=%d ssi_signal=%d stype=%u (%s) len=%u",
1833 MAC2STR(mgmt->sa), rx_freq, ssi_signal, stype, fc2str(fc),
1834 (unsigned int) len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001835 event.rx_mgmt.frame = frame;
1836 event.rx_mgmt.frame_len = len;
1837 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001838 event.rx_mgmt.drv_priv = bss;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001839 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840}
1841
1842
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001843static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1844 struct nlattr *cookie, const u8 *frame,
1845 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846{
1847 union wpa_event_data event;
1848 const struct ieee80211_hdr *hdr;
1849 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001851 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 if (!is_ap_interface(drv->nlmode)) {
1853 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001855 if (!cookie)
1856 return;
1857
1858 cookie_val = nla_get_u64(cookie);
1859 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1860 " cookie=0%llx%s (ack=%d)",
1861 (long long unsigned int) cookie_val,
1862 cookie_val == drv->send_action_cookie ?
1863 " (match)" : " (unknown)", ack != NULL);
1864 if (cookie_val != drv->send_action_cookie)
1865 return;
1866 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867
1868 hdr = (const struct ieee80211_hdr *) frame;
1869 fc = le_to_host16(hdr->frame_control);
1870
1871 os_memset(&event, 0, sizeof(event));
1872 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1873 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1874 event.tx_status.dst = hdr->addr1;
1875 event.tx_status.data = frame;
1876 event.tx_status.data_len = len;
1877 event.tx_status.ack = ack != NULL;
1878 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1879}
1880
1881
1882static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1883 enum wpa_event_type type,
1884 const u8 *frame, size_t len)
1885{
1886 const struct ieee80211_mgmt *mgmt;
1887 union wpa_event_data event;
1888 const u8 *bssid = NULL;
1889 u16 reason_code = 0;
1890
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001891 if (type == EVENT_DEAUTH)
1892 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1893 else
1894 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 mgmt = (const struct ieee80211_mgmt *) frame;
1897 if (len >= 24) {
1898 bssid = mgmt->bssid;
1899
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001900 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1901 !drv->associated &&
1902 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1903 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1904 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1905 /*
1906 * Avoid issues with some roaming cases where
1907 * disconnection event for the old AP may show up after
1908 * we have started connection with the new AP.
1909 */
1910 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1911 MAC2STR(bssid),
1912 MAC2STR(drv->auth_attempt_bssid));
1913 return;
1914 }
1915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 if (drv->associated != 0 &&
1917 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1918 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1919 /*
1920 * We have presumably received this deauth as a
1921 * response to a clear_state_mismatch() outgoing
1922 * deauth. Don't let it take us offline!
1923 */
1924 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1925 "from Unknown BSSID " MACSTR " -- ignoring",
1926 MAC2STR(bssid));
1927 return;
1928 }
1929 }
1930
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001931 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 os_memset(&event, 0, sizeof(event));
1933
1934 /* Note: Same offset for Reason Code in both frame subtypes */
1935 if (len >= 24 + sizeof(mgmt->u.deauth))
1936 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1937
1938 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001939 event.disassoc_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001940 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 event.disassoc_info.addr = bssid;
1942 event.disassoc_info.reason_code = reason_code;
1943 if (frame + len > mgmt->u.disassoc.variable) {
1944 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1945 event.disassoc_info.ie_len = frame + len -
1946 mgmt->u.disassoc.variable;
1947 }
1948 } else {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001949 if (drv->ignore_deauth_event) {
1950 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
1951 drv->ignore_deauth_event = 0;
1952 return;
1953 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001954 event.deauth_info.locally_generated =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001955 !os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07001956 if (drv->ignore_next_local_deauth) {
1957 drv->ignore_next_local_deauth = 0;
1958 if (event.deauth_info.locally_generated) {
1959 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
1960 return;
1961 }
1962 wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
1963 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 event.deauth_info.addr = bssid;
1965 event.deauth_info.reason_code = reason_code;
1966 if (frame + len > mgmt->u.deauth.variable) {
1967 event.deauth_info.ie = mgmt->u.deauth.variable;
1968 event.deauth_info.ie_len = frame + len -
1969 mgmt->u.deauth.variable;
1970 }
1971 }
1972
1973 wpa_supplicant_event(drv->ctx, type, &event);
1974}
1975
1976
1977static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1978 enum wpa_event_type type,
1979 const u8 *frame, size_t len)
1980{
1981 const struct ieee80211_mgmt *mgmt;
1982 union wpa_event_data event;
1983 u16 reason_code = 0;
1984
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001985 if (type == EVENT_UNPROT_DEAUTH)
1986 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1987 else
1988 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 if (len < 24)
1991 return;
1992
1993 mgmt = (const struct ieee80211_mgmt *) frame;
1994
1995 os_memset(&event, 0, sizeof(event));
1996 /* Note: Same offset for Reason Code in both frame subtypes */
1997 if (len >= 24 + sizeof(mgmt->u.deauth))
1998 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1999
2000 if (type == EVENT_UNPROT_DISASSOC) {
2001 event.unprot_disassoc.sa = mgmt->sa;
2002 event.unprot_disassoc.da = mgmt->da;
2003 event.unprot_disassoc.reason_code = reason_code;
2004 } else {
2005 event.unprot_deauth.sa = mgmt->sa;
2006 event.unprot_deauth.da = mgmt->da;
2007 event.unprot_deauth.reason_code = reason_code;
2008 }
2009
2010 wpa_supplicant_event(drv->ctx, type, &event);
2011}
2012
2013
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002014static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 enum nl80211_commands cmd, struct nlattr *frame,
2016 struct nlattr *addr, struct nlattr *timed_out,
2017 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002018 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002020 struct wpa_driver_nl80211_data *drv = bss->drv;
2021 const u8 *data;
2022 size_t len;
2023
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 if (timed_out && addr) {
2025 mlme_timeout_event(drv, cmd, addr);
2026 return;
2027 }
2028
2029 if (frame == NULL) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002030 wpa_printf(MSG_DEBUG,
2031 "nl80211: MLME event %d (%s) without frame data",
2032 cmd, nl80211_command_to_string(cmd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 return;
2034 }
2035
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002036 data = nla_data(frame);
2037 len = nla_len(frame);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002038 if (len < 4 + 2 * ETH_ALEN) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002039 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
2040 MACSTR ") - too short",
2041 cmd, nl80211_command_to_string(cmd), bss->ifname,
2042 MAC2STR(bss->addr));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002043 return;
2044 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002045 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
2046 ") A1=" MACSTR " A2=" MACSTR, cmd,
2047 nl80211_command_to_string(cmd), bss->ifname,
2048 MAC2STR(bss->addr), MAC2STR(data + 4),
2049 MAC2STR(data + 4 + ETH_ALEN));
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002050 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002051 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
2052 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002053 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
2054 "for foreign address", bss->ifname);
2055 return;
2056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
2058 nla_data(frame), nla_len(frame));
2059
2060 switch (cmd) {
2061 case NL80211_CMD_AUTHENTICATE:
2062 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
2063 break;
2064 case NL80211_CMD_ASSOCIATE:
2065 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
2066 break;
2067 case NL80211_CMD_DEAUTHENTICATE:
2068 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
2069 nla_data(frame), nla_len(frame));
2070 break;
2071 case NL80211_CMD_DISASSOCIATE:
2072 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
2073 nla_data(frame), nla_len(frame));
2074 break;
2075 case NL80211_CMD_FRAME:
Dmitry Shmidt98660862014-03-11 17:26:21 -07002076 mlme_event_mgmt(bss, freq, sig, nla_data(frame),
Dmitry Shmidt04949592012-07-19 12:16:46 -07002077 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 break;
2079 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
2081 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 break;
2083 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2084 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
2085 nla_data(frame), nla_len(frame));
2086 break;
2087 case NL80211_CMD_UNPROT_DISASSOCIATE:
2088 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
2089 nla_data(frame), nla_len(frame));
2090 break;
2091 default:
2092 break;
2093 }
2094}
2095
2096
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002097static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 struct nlattr *tb[])
2099{
2100 union wpa_event_data data;
2101
2102 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
2103 os_memset(&data, 0, sizeof(data));
2104 if (tb[NL80211_ATTR_MAC]) {
2105 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
2106 nla_data(tb[NL80211_ATTR_MAC]),
2107 nla_len(tb[NL80211_ATTR_MAC]));
2108 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
2109 }
2110 if (tb[NL80211_ATTR_KEY_SEQ]) {
2111 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
2112 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
2113 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
2114 }
2115 if (tb[NL80211_ATTR_KEY_TYPE]) {
2116 enum nl80211_key_type key_type =
2117 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
2118 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
2119 if (key_type == NL80211_KEYTYPE_PAIRWISE)
2120 data.michael_mic_failure.unicast = 1;
2121 } else
2122 data.michael_mic_failure.unicast = 1;
2123
2124 if (tb[NL80211_ATTR_KEY_IDX]) {
2125 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
2126 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
2127 }
2128
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002129 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130}
2131
2132
2133static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
2134 struct nlattr *tb[])
2135{
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002136 unsigned int freq;
2137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 if (tb[NL80211_ATTR_MAC] == NULL) {
2139 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
2140 "event");
2141 return;
2142 }
2143 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 drv->associated = 1;
2146 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
2147 MAC2STR(drv->bssid));
2148
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002149 freq = nl80211_get_assoc_freq(drv);
2150 if (freq) {
2151 wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
2152 freq);
2153 drv->first_bss->freq = freq;
2154 }
2155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
2157}
2158
2159
2160static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
2161 int cancel_event, struct nlattr *tb[])
2162{
2163 unsigned int freq, chan_type, duration;
2164 union wpa_event_data data;
2165 u64 cookie;
2166
2167 if (tb[NL80211_ATTR_WIPHY_FREQ])
2168 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2169 else
2170 freq = 0;
2171
2172 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
2173 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2174 else
2175 chan_type = 0;
2176
2177 if (tb[NL80211_ATTR_DURATION])
2178 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
2179 else
2180 duration = 0;
2181
2182 if (tb[NL80211_ATTR_COOKIE])
2183 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
2184 else
2185 cookie = 0;
2186
2187 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
2188 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
2189 cancel_event, freq, chan_type, duration,
2190 (long long unsigned int) cookie,
2191 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
2192
2193 if (cookie != drv->remain_on_chan_cookie)
2194 return; /* not for us */
2195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002196 if (cancel_event)
2197 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198
2199 os_memset(&data, 0, sizeof(data));
2200 data.remain_on_channel.freq = freq;
2201 data.remain_on_channel.duration = duration;
2202 wpa_supplicant_event(drv->ctx, cancel_event ?
2203 EVENT_CANCEL_REMAIN_ON_CHANNEL :
2204 EVENT_REMAIN_ON_CHANNEL, &data);
2205}
2206
2207
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002208static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
2209 struct nlattr *tb[])
2210{
2211 union wpa_event_data data;
2212
2213 os_memset(&data, 0, sizeof(data));
2214
2215 if (tb[NL80211_ATTR_IE]) {
2216 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
2217 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
2218 }
2219
2220 if (tb[NL80211_ATTR_IE_RIC]) {
2221 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
2222 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
2223 }
2224
2225 if (tb[NL80211_ATTR_MAC])
2226 os_memcpy(data.ft_ies.target_ap,
2227 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2228
2229 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
2230 MAC2STR(data.ft_ies.target_ap));
2231
2232 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
2233}
2234
2235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
2237 struct nlattr *tb[])
2238{
2239 union wpa_event_data event;
2240 struct nlattr *nl;
2241 int rem;
2242 struct scan_info *info;
2243#define MAX_REPORT_FREQS 50
2244 int freqs[MAX_REPORT_FREQS];
2245 int num_freqs = 0;
2246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002247 if (drv->scan_for_auth) {
2248 drv->scan_for_auth = 0;
2249 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
2250 "cfg80211 BSS entry");
2251 wpa_driver_nl80211_authenticate_retry(drv);
2252 return;
2253 }
2254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255 os_memset(&event, 0, sizeof(event));
2256 info = &event.scan_info;
2257 info->aborted = aborted;
2258
2259 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
2260 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
2261 struct wpa_driver_scan_ssid *s =
2262 &info->ssids[info->num_ssids];
2263 s->ssid = nla_data(nl);
2264 s->ssid_len = nla_len(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002265 wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
2266 wpa_ssid_txt(s->ssid, s->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 info->num_ssids++;
2268 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2269 break;
2270 }
2271 }
2272 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002273 char msg[200], *pos, *end;
2274 int res;
2275
2276 pos = msg;
2277 end = pos + sizeof(msg);
2278 *pos = '\0';
2279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
2281 {
2282 freqs[num_freqs] = nla_get_u32(nl);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002283 res = os_snprintf(pos, end - pos, " %d",
2284 freqs[num_freqs]);
2285 if (res > 0 && end - pos > res)
2286 pos += res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 num_freqs++;
2288 if (num_freqs == MAX_REPORT_FREQS - 1)
2289 break;
2290 }
2291 info->freqs = freqs;
2292 info->num_freqs = num_freqs;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002293 wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2294 msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 }
2296 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2297}
2298
2299
2300static int get_link_signal(struct nl_msg *msg, void *arg)
2301{
2302 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2303 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2304 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
2305 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
2306 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002307 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308 };
2309 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
2310 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
2311 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
2312 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
2313 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
2314 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
2315 };
2316 struct wpa_signal_info *sig_change = arg;
2317
2318 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2319 genlmsg_attrlen(gnlh, 0), NULL);
2320 if (!tb[NL80211_ATTR_STA_INFO] ||
2321 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
2322 tb[NL80211_ATTR_STA_INFO], policy))
2323 return NL_SKIP;
2324 if (!sinfo[NL80211_STA_INFO_SIGNAL])
2325 return NL_SKIP;
2326
2327 sig_change->current_signal =
2328 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
2329
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002330 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
2331 sig_change->avg_signal =
2332 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
2333 else
2334 sig_change->avg_signal = 0;
2335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
2337 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
2338 sinfo[NL80211_STA_INFO_TX_BITRATE],
2339 rate_policy)) {
2340 sig_change->current_txrate = 0;
2341 } else {
2342 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
2343 sig_change->current_txrate =
2344 nla_get_u16(rinfo[
2345 NL80211_RATE_INFO_BITRATE]) * 100;
2346 }
2347 }
2348 }
2349
2350 return NL_SKIP;
2351}
2352
2353
2354static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
2355 struct wpa_signal_info *sig)
2356{
2357 struct nl_msg *msg;
2358
2359 sig->current_signal = -9999;
2360 sig->current_txrate = 0;
2361
2362 msg = nlmsg_alloc();
2363 if (!msg)
2364 return -ENOMEM;
2365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002366 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367
2368 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2369 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
2370
2371 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
2372 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 return -ENOBUFS;
2375}
2376
2377
2378static int get_link_noise(struct nl_msg *msg, void *arg)
2379{
2380 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2381 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2382 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2383 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2384 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2385 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2386 };
2387 struct wpa_signal_info *sig_change = arg;
2388
2389 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2390 genlmsg_attrlen(gnlh, 0), NULL);
2391
2392 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2393 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2394 return NL_SKIP;
2395 }
2396
2397 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2398 tb[NL80211_ATTR_SURVEY_INFO],
2399 survey_policy)) {
2400 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2401 "attributes!");
2402 return NL_SKIP;
2403 }
2404
2405 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2406 return NL_SKIP;
2407
2408 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2409 sig_change->frequency)
2410 return NL_SKIP;
2411
2412 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2413 return NL_SKIP;
2414
2415 sig_change->current_noise =
2416 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2417
2418 return NL_SKIP;
2419}
2420
2421
2422static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2423 struct wpa_signal_info *sig_change)
2424{
2425 struct nl_msg *msg;
2426
2427 sig_change->current_noise = 9999;
2428 sig_change->frequency = drv->assoc_freq;
2429
2430 msg = nlmsg_alloc();
2431 if (!msg)
2432 return -ENOMEM;
2433
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435
2436 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2437
2438 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2439 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002440 nlmsg_free(msg);
2441 return -ENOBUFS;
2442}
2443
2444
2445static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2446{
2447 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2448 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2449 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2450 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2451 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2452 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2453 };
2454 struct wpa_scan_results *scan_results = arg;
2455 struct wpa_scan_res *scan_res;
2456 size_t i;
2457
2458 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2459 genlmsg_attrlen(gnlh, 0), NULL);
2460
2461 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2462 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2463 return NL_SKIP;
2464 }
2465
2466 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2467 tb[NL80211_ATTR_SURVEY_INFO],
2468 survey_policy)) {
2469 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2470 "attributes");
2471 return NL_SKIP;
2472 }
2473
2474 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2475 return NL_SKIP;
2476
2477 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2478 return NL_SKIP;
2479
2480 for (i = 0; i < scan_results->num; ++i) {
2481 scan_res = scan_results->res[i];
2482 if (!scan_res)
2483 continue;
2484 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2485 scan_res->freq)
2486 continue;
2487 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2488 continue;
2489 scan_res->noise = (s8)
2490 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2491 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2492 }
2493
2494 return NL_SKIP;
2495}
2496
2497
2498static int nl80211_get_noise_for_scan_results(
2499 struct wpa_driver_nl80211_data *drv,
2500 struct wpa_scan_results *scan_res)
2501{
2502 struct nl_msg *msg;
2503
2504 msg = nlmsg_alloc();
2505 if (!msg)
2506 return -ENOMEM;
2507
2508 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2509
2510 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2511
2512 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2513 scan_res);
2514 nla_put_failure:
2515 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 return -ENOBUFS;
2517}
2518
2519
2520static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2521 struct nlattr *tb[])
2522{
2523 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2524 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2525 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2526 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2527 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2528 };
2529 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2530 enum nl80211_cqm_rssi_threshold_event event;
2531 union wpa_event_data ed;
2532 struct wpa_signal_info sig;
2533 int res;
2534
2535 if (tb[NL80211_ATTR_CQM] == NULL ||
2536 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2537 cqm_policy)) {
2538 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2539 return;
2540 }
2541
2542 os_memset(&ed, 0, sizeof(ed));
2543
2544 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2545 if (!tb[NL80211_ATTR_MAC])
2546 return;
2547 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2548 ETH_ALEN);
2549 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2550 return;
2551 }
2552
2553 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2554 return;
2555 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2556
2557 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2558 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2559 "event: RSSI high");
2560 ed.signal_change.above_threshold = 1;
2561 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2562 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2563 "event: RSSI low");
2564 ed.signal_change.above_threshold = 0;
2565 } else
2566 return;
2567
2568 res = nl80211_get_link_signal(drv, &sig);
2569 if (res == 0) {
2570 ed.signal_change.current_signal = sig.current_signal;
2571 ed.signal_change.current_txrate = sig.current_txrate;
2572 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2573 sig.current_signal, sig.current_txrate);
2574 }
2575
2576 res = nl80211_get_link_noise(drv, &sig);
2577 if (res == 0) {
2578 ed.signal_change.current_noise = sig.current_noise;
2579 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2580 sig.current_noise);
2581 }
2582
2583 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2584}
2585
2586
2587static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2588 struct nlattr **tb)
2589{
2590 u8 *addr;
2591 union wpa_event_data data;
2592
2593 if (tb[NL80211_ATTR_MAC] == NULL)
2594 return;
2595 addr = nla_data(tb[NL80211_ATTR_MAC]);
2596 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002598 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002599 u8 *ies = NULL;
2600 size_t ies_len = 0;
2601 if (tb[NL80211_ATTR_IE]) {
2602 ies = nla_data(tb[NL80211_ATTR_IE]);
2603 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2604 }
2605 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2606 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2607 return;
2608 }
2609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2611 return;
2612
2613 os_memset(&data, 0, sizeof(data));
2614 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2615 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2616}
2617
2618
2619static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2620 struct nlattr **tb)
2621{
2622 u8 *addr;
2623 union wpa_event_data data;
2624
2625 if (tb[NL80211_ATTR_MAC] == NULL)
2626 return;
2627 addr = nla_data(tb[NL80211_ATTR_MAC]);
2628 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2629 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002631 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002632 drv_event_disassoc(drv->ctx, addr);
2633 return;
2634 }
2635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2637 return;
2638
2639 os_memset(&data, 0, sizeof(data));
2640 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2641 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2642}
2643
2644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2646 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002648 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2649 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2650 [NL80211_REKEY_DATA_KEK] = {
2651 .minlen = NL80211_KEK_LEN,
2652 .maxlen = NL80211_KEK_LEN,
2653 },
2654 [NL80211_REKEY_DATA_KCK] = {
2655 .minlen = NL80211_KCK_LEN,
2656 .maxlen = NL80211_KCK_LEN,
2657 },
2658 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2659 .minlen = NL80211_REPLAY_CTR_LEN,
2660 .maxlen = NL80211_REPLAY_CTR_LEN,
2661 },
2662 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 union wpa_event_data data;
2664
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002665 if (!tb[NL80211_ATTR_MAC])
2666 return;
2667 if (!tb[NL80211_ATTR_REKEY_DATA])
2668 return;
2669 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2670 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2671 return;
2672 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2673 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002675 os_memset(&data, 0, sizeof(data));
2676 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2677 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2678 MAC2STR(data.driver_gtk_rekey.bssid));
2679 data.driver_gtk_rekey.replay_ctr =
2680 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2681 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2682 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2683 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2684}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002686
2687static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2688 struct nlattr **tb)
2689{
2690 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2691 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2692 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2693 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2694 .minlen = ETH_ALEN,
2695 .maxlen = ETH_ALEN,
2696 },
2697 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2698 };
2699 union wpa_event_data data;
2700
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002701 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002703 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2704 return;
2705 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2706 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2707 return;
2708 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2709 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2710 return;
2711
2712 os_memset(&data, 0, sizeof(data));
2713 os_memcpy(data.pmkid_candidate.bssid,
2714 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2715 data.pmkid_candidate.index =
2716 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2717 data.pmkid_candidate.preauth =
2718 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2719 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2720}
2721
2722
2723static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2724 struct nlattr **tb)
2725{
2726 union wpa_event_data data;
2727
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002728 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002730 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2731 return;
2732
2733 os_memset(&data, 0, sizeof(data));
2734 os_memcpy(data.client_poll.addr,
2735 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2736
2737 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2738}
2739
2740
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002741static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2742 struct nlattr **tb)
2743{
2744 union wpa_event_data data;
2745
2746 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2747
2748 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2749 return;
2750
2751 os_memset(&data, 0, sizeof(data));
2752 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2753 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2754 case NL80211_TDLS_SETUP:
2755 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2756 MACSTR, MAC2STR(data.tdls.peer));
2757 data.tdls.oper = TDLS_REQUEST_SETUP;
2758 break;
2759 case NL80211_TDLS_TEARDOWN:
2760 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2761 MACSTR, MAC2STR(data.tdls.peer));
2762 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2763 break;
2764 default:
2765 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2766 "event");
2767 return;
2768 }
2769 if (tb[NL80211_ATTR_REASON_CODE]) {
2770 data.tdls.reason_code =
2771 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2772 }
2773
2774 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2775}
2776
2777
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07002778static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
2779 struct nlattr **tb)
2780{
2781 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
2782}
2783
2784
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002785static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2786 struct nlattr **tb)
2787{
2788 union wpa_event_data data;
2789 u32 reason;
2790
2791 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2792
2793 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2794 return;
2795
2796 os_memset(&data, 0, sizeof(data));
2797 os_memcpy(data.connect_failed_reason.addr,
2798 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2799
2800 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2801 switch (reason) {
2802 case NL80211_CONN_FAIL_MAX_CLIENTS:
2803 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2804 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2805 break;
2806 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2807 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2808 " tried to connect",
2809 MAC2STR(data.connect_failed_reason.addr));
2810 data.connect_failed_reason.code = BLOCKED_CLIENT;
2811 break;
2812 default:
2813 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2814 "%u", reason);
2815 return;
2816 }
2817
2818 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2819}
2820
2821
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002822static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2823 struct nlattr **tb)
2824{
2825 union wpa_event_data data;
2826 enum nl80211_radar_event event_type;
2827
2828 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2829 return;
2830
2831 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt051af732013-10-22 13:52:46 -07002832 data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2833 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002834
Dmitry Shmidt051af732013-10-22 13:52:46 -07002835 /* Check HT params */
2836 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2837 data.dfs_event.ht_enabled = 1;
2838 data.dfs_event.chan_offset = 0;
2839
2840 switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2841 case NL80211_CHAN_NO_HT:
2842 data.dfs_event.ht_enabled = 0;
2843 break;
2844 case NL80211_CHAN_HT20:
2845 break;
2846 case NL80211_CHAN_HT40PLUS:
2847 data.dfs_event.chan_offset = 1;
2848 break;
2849 case NL80211_CHAN_HT40MINUS:
2850 data.dfs_event.chan_offset = -1;
2851 break;
2852 }
2853 }
2854
2855 /* Get VHT params */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002856 if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2857 data.dfs_event.chan_width =
2858 convert2width(nla_get_u32(
2859 tb[NL80211_ATTR_CHANNEL_WIDTH]));
2860 if (tb[NL80211_ATTR_CENTER_FREQ1])
2861 data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002862 if (tb[NL80211_ATTR_CENTER_FREQ2])
2863 data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2864
2865 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2866 data.dfs_event.freq, data.dfs_event.ht_enabled,
2867 data.dfs_event.chan_offset, data.dfs_event.chan_width,
2868 data.dfs_event.cf1, data.dfs_event.cf2);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002869
2870 switch (event_type) {
2871 case NL80211_RADAR_DETECTED:
2872 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2873 break;
2874 case NL80211_RADAR_CAC_FINISHED:
2875 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2876 break;
2877 case NL80211_RADAR_CAC_ABORTED:
2878 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2879 break;
2880 case NL80211_RADAR_NOP_FINISHED:
2881 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2882 break;
2883 default:
2884 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2885 "received", event_type);
2886 break;
2887 }
2888}
2889
2890
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002891static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2892 int wds)
2893{
2894 struct wpa_driver_nl80211_data *drv = bss->drv;
2895 union wpa_event_data event;
2896
2897 if (!tb[NL80211_ATTR_MAC])
2898 return;
2899
2900 os_memset(&event, 0, sizeof(event));
2901 event.rx_from_unknown.bssid = bss->addr;
2902 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2903 event.rx_from_unknown.wds = wds;
2904
2905 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2906}
2907
2908
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002909static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
2910 const u8 *data, size_t len)
2911{
2912 u32 i, count;
2913 union wpa_event_data event;
2914 struct wpa_freq_range *range = NULL;
2915 const struct qca_avoid_freq_list *freq_range;
2916
2917 freq_range = (const struct qca_avoid_freq_list *) data;
2918 if (len < sizeof(freq_range->count))
2919 return;
2920
2921 count = freq_range->count;
2922 if (len < sizeof(freq_range->count) +
2923 count * sizeof(struct qca_avoid_freq_range)) {
2924 wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
2925 (unsigned int) len);
2926 return;
2927 }
2928
2929 if (count > 0) {
2930 range = os_calloc(count, sizeof(struct wpa_freq_range));
2931 if (range == NULL)
2932 return;
2933 }
2934
2935 os_memset(&event, 0, sizeof(event));
2936 for (i = 0; i < count; i++) {
2937 unsigned int idx = event.freq_range.num;
2938 range[idx].min = freq_range->range[i].start_freq;
2939 range[idx].max = freq_range->range[i].end_freq;
2940 wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
2941 range[idx].min, range[idx].max);
2942 if (range[idx].min > range[idx].max) {
2943 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
2944 continue;
2945 }
2946 event.freq_range.num++;
2947 }
2948 event.freq_range.range = range;
2949
2950 wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
2951
2952 os_free(range);
2953}
2954
2955
2956static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2957 u32 subcmd, u8 *data, size_t len)
2958{
2959 switch (subcmd) {
2960 case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2961 qca_nl80211_avoid_freq(drv, data, len);
2962 break;
2963 default:
2964 wpa_printf(MSG_DEBUG,
2965 "nl80211: Ignore unsupported QCA vendor event %u",
2966 subcmd);
2967 break;
2968 }
2969}
2970
2971
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002972static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2973 struct nlattr **tb)
2974{
2975 u32 vendor_id, subcmd, wiphy = 0;
2976 int wiphy_idx;
2977 u8 *data = NULL;
2978 size_t len = 0;
2979
2980 if (!tb[NL80211_ATTR_VENDOR_ID] ||
2981 !tb[NL80211_ATTR_VENDOR_SUBCMD])
2982 return;
2983
2984 vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2985 subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2986
2987 if (tb[NL80211_ATTR_WIPHY])
2988 wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2989
2990 wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2991 wiphy, vendor_id, subcmd);
2992
2993 if (tb[NL80211_ATTR_VENDOR_DATA]) {
2994 data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2995 len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2996 wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2997 }
2998
2999 wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
3000 if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
3001 wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
3002 wiphy, wiphy_idx);
3003 return;
3004 }
3005
3006 switch (vendor_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003007 case OUI_QCA:
3008 nl80211_vendor_event_qca(drv, subcmd, data, len);
3009 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003010 default:
3011 wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
3012 break;
3013 }
3014}
3015
3016
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003017static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
3018 struct nlattr *tb[])
3019{
3020 union wpa_event_data data;
3021 enum nl80211_reg_initiator init;
3022
3023 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
3024
3025 if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
3026 return;
3027
3028 os_memset(&data, 0, sizeof(data));
3029 init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
3030 wpa_printf(MSG_DEBUG, " * initiator=%d", init);
3031 switch (init) {
3032 case NL80211_REGDOM_SET_BY_CORE:
3033 data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
3034 break;
3035 case NL80211_REGDOM_SET_BY_USER:
3036 data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
3037 break;
3038 case NL80211_REGDOM_SET_BY_DRIVER:
3039 data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
3040 break;
3041 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3042 data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
3043 break;
3044 }
3045
3046 if (tb[NL80211_ATTR_REG_TYPE]) {
3047 enum nl80211_reg_type type;
3048 type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
3049 wpa_printf(MSG_DEBUG, " * type=%d", type);
3050 switch (type) {
3051 case NL80211_REGDOM_TYPE_COUNTRY:
3052 data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
3053 break;
3054 case NL80211_REGDOM_TYPE_WORLD:
3055 data.channel_list_changed.type = REGDOM_TYPE_WORLD;
3056 break;
3057 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
3058 data.channel_list_changed.type =
3059 REGDOM_TYPE_CUSTOM_WORLD;
3060 break;
3061 case NL80211_REGDOM_TYPE_INTERSECTION:
3062 data.channel_list_changed.type =
3063 REGDOM_TYPE_INTERSECTION;
3064 break;
3065 }
3066 }
3067
3068 if (tb[NL80211_ATTR_REG_ALPHA2]) {
3069 os_strlcpy(data.channel_list_changed.alpha2,
3070 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
3071 sizeof(data.channel_list_changed.alpha2));
3072 wpa_printf(MSG_DEBUG, " * alpha2=%s",
3073 data.channel_list_changed.alpha2);
3074 }
3075
3076 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
3077}
3078
3079
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003080static void do_process_drv_event(struct i802_bss *bss, int cmd,
3081 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003082{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003083 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003084 union wpa_event_data data;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003085
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003086 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
3087 cmd, nl80211_command_to_string(cmd), bss->ifname);
3088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003089 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
3090 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
3091 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003092 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003093 drv->ap_scan_as_station);
3094 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 }
3096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003097 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003099 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003100 drv->scan_state = SCAN_STARTED;
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003101 if (drv->scan_for_auth) {
3102 /*
3103 * Cannot indicate EVENT_SCAN_STARTED here since we skip
3104 * EVENT_SCAN_RESULTS in scan_for_auth case and the
3105 * upper layer implementation could get confused about
3106 * scanning state.
3107 */
3108 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
3109 break;
3110 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003111 wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003113 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003114 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003115 drv->scan_state = SCHED_SCAN_STARTED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003116 break;
3117 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003118 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003119 drv->scan_state = SCHED_SCAN_STOPPED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003120 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
3121 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003123 wpa_dbg(drv->ctx, MSG_DEBUG,
3124 "nl80211: New scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003125 drv->scan_state = SCAN_COMPLETED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 drv->scan_complete_events = 1;
3127 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3128 drv->ctx);
3129 send_scan_event(drv, 0, tb);
3130 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003131 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003132 wpa_dbg(drv->ctx, MSG_DEBUG,
3133 "nl80211: New sched scan results available");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003134 drv->scan_state = SCHED_SCAN_RESULTS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003135 send_scan_event(drv, 0, tb);
3136 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003138 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt56052862013-10-04 10:23:25 -07003139 drv->scan_state = SCAN_ABORTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 /*
3141 * Need to indicate that scan results are available in order
3142 * not to make wpa_supplicant stop its scanning.
3143 */
3144 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
3145 drv->ctx);
3146 send_scan_event(drv, 1, tb);
3147 break;
3148 case NL80211_CMD_AUTHENTICATE:
3149 case NL80211_CMD_ASSOCIATE:
3150 case NL80211_CMD_DEAUTHENTICATE:
3151 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 case NL80211_CMD_FRAME_TX_STATUS:
3153 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
3154 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003155 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3157 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003158 tb[NL80211_ATTR_COOKIE],
3159 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003160 break;
3161 case NL80211_CMD_CONNECT:
3162 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003163 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 tb[NL80211_ATTR_STATUS_CODE],
3165 tb[NL80211_ATTR_MAC],
3166 tb[NL80211_ATTR_REQ_IE],
3167 tb[NL80211_ATTR_RESP_IE]);
3168 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003169 case NL80211_CMD_CH_SWITCH_NOTIFY:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003170 mlme_event_ch_switch(drv,
3171 tb[NL80211_ATTR_IFINDEX],
3172 tb[NL80211_ATTR_WIPHY_FREQ],
3173 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3174 tb[NL80211_ATTR_CHANNEL_WIDTH],
3175 tb[NL80211_ATTR_CENTER_FREQ1],
3176 tb[NL80211_ATTR_CENTER_FREQ2]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003177 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003180 tb[NL80211_ATTR_MAC],
3181 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 break;
3183 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003184 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185 break;
3186 case NL80211_CMD_JOIN_IBSS:
3187 mlme_event_join_ibss(drv, tb);
3188 break;
3189 case NL80211_CMD_REMAIN_ON_CHANNEL:
3190 mlme_event_remain_on_channel(drv, 0, tb);
3191 break;
3192 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3193 mlme_event_remain_on_channel(drv, 1, tb);
3194 break;
3195 case NL80211_CMD_NOTIFY_CQM:
3196 nl80211_cqm_event(drv, tb);
3197 break;
3198 case NL80211_CMD_REG_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003199 nl80211_reg_change_event(drv, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 break;
3201 case NL80211_CMD_REG_BEACON_HINT:
3202 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
Dmitry Shmidt97672262014-02-03 13:02:54 -08003203 os_memset(&data, 0, sizeof(data));
3204 data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
Dmitry Shmidt97672262014-02-03 13:02:54 -08003206 &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 break;
3208 case NL80211_CMD_NEW_STATION:
3209 nl80211_new_station_event(drv, tb);
3210 break;
3211 case NL80211_CMD_DEL_STATION:
3212 nl80211_del_station_event(drv, tb);
3213 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003214 case NL80211_CMD_SET_REKEY_OFFLOAD:
3215 nl80211_rekey_offload_event(drv, tb);
3216 break;
3217 case NL80211_CMD_PMKSA_CANDIDATE:
3218 nl80211_pmksa_candidate_event(drv, tb);
3219 break;
3220 case NL80211_CMD_PROBE_CLIENT:
3221 nl80211_client_probe_event(drv, tb);
3222 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003223 case NL80211_CMD_TDLS_OPER:
3224 nl80211_tdls_oper_event(drv, tb);
3225 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003226 case NL80211_CMD_CONN_FAILED:
3227 nl80211_connect_failed_event(drv, tb);
3228 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003229 case NL80211_CMD_FT_EVENT:
3230 mlme_event_ft_event(drv, tb);
3231 break;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003232 case NL80211_CMD_RADAR_DETECT:
3233 nl80211_radar_event(drv, tb);
3234 break;
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -07003235 case NL80211_CMD_STOP_AP:
3236 nl80211_stop_ap(drv, tb);
3237 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003238 case NL80211_CMD_VENDOR:
3239 nl80211_vendor_event(drv, tb);
3240 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003241 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003242 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3243 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003244 break;
3245 }
3246}
3247
3248
3249static int process_drv_event(struct nl_msg *msg, void *arg)
3250{
3251 struct wpa_driver_nl80211_data *drv = arg;
3252 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3253 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003254 struct i802_bss *bss;
3255 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003256
3257 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3258 genlmsg_attrlen(gnlh, 0), NULL);
3259
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003260 if (tb[NL80211_ATTR_IFINDEX]) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003261 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3262
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003263 for (bss = drv->first_bss; bss; bss = bss->next)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003264 if (ifidx == -1 || ifidx == bss->ifindex) {
3265 do_process_drv_event(bss, gnlh->cmd, tb);
3266 return NL_SKIP;
3267 }
3268 wpa_printf(MSG_DEBUG,
3269 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
3270 gnlh->cmd, ifidx);
3271 } else if (tb[NL80211_ATTR_WDEV]) {
3272 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3273 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003274 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003275 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
3276 do_process_drv_event(bss, gnlh->cmd, tb);
3277 return NL_SKIP;
3278 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003279 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003280 wpa_printf(MSG_DEBUG,
3281 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
3282 gnlh->cmd, (long long unsigned int) wdev_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003283 }
3284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003285 return NL_SKIP;
3286}
3287
3288
3289static int process_global_event(struct nl_msg *msg, void *arg)
3290{
3291 struct nl80211_global *global = arg;
3292 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3293 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003294 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003295 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003296 struct i802_bss *bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003297 u64 wdev_id = 0;
3298 int wdev_id_set = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003299
3300 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3301 genlmsg_attrlen(gnlh, 0), NULL);
3302
3303 if (tb[NL80211_ATTR_IFINDEX])
3304 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003305 else if (tb[NL80211_ATTR_WDEV]) {
3306 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3307 wdev_id_set = 1;
3308 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309
Dmitry Shmidt04949592012-07-19 12:16:46 -07003310 dl_list_for_each_safe(drv, tmp, &global->interfaces,
3311 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003312 for (bss = drv->first_bss; bss; bss = bss->next) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003313 if ((ifidx == -1 && !wdev_id_set) ||
3314 ifidx == bss->ifindex ||
3315 (wdev_id_set && bss->wdev_id_set &&
3316 wdev_id == bss->wdev_id)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003317 do_process_drv_event(bss, gnlh->cmd, tb);
3318 return NL_SKIP;
3319 }
3320 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003321 }
3322
3323 return NL_SKIP;
3324}
3325
3326
3327static int process_bss_event(struct nl_msg *msg, void *arg)
3328{
3329 struct i802_bss *bss = arg;
3330 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3331 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3332
3333 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3334 genlmsg_attrlen(gnlh, 0), NULL);
3335
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003336 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3337 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3338 bss->ifname);
3339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003340 switch (gnlh->cmd) {
3341 case NL80211_CMD_FRAME:
3342 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003343 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3345 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07003346 tb[NL80211_ATTR_COOKIE],
3347 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003348 break;
3349 case NL80211_CMD_UNEXPECTED_FRAME:
3350 nl80211_spurious_frame(bss, tb, 0);
3351 break;
3352 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3353 nl80211_spurious_frame(bss, tb, 1);
3354 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355 default:
3356 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3357 "(cmd=%d)", gnlh->cmd);
3358 break;
3359 }
3360
3361 return NL_SKIP;
3362}
3363
3364
3365static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
3366 void *handle)
3367{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003369 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003371 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003373 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07003374 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003375 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
3376 __func__, res);
3377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378}
3379
3380
3381/**
3382 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
3383 * @priv: driver_nl80211 private data
3384 * @alpha2_arg: country to which to switch to
3385 * Returns: 0 on success, -1 on failure
3386 *
3387 * This asks nl80211 to set the regulatory domain for given
3388 * country ISO / IEC alpha2.
3389 */
3390static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
3391{
3392 struct i802_bss *bss = priv;
3393 struct wpa_driver_nl80211_data *drv = bss->drv;
3394 char alpha2[3];
3395 struct nl_msg *msg;
3396
3397 msg = nlmsg_alloc();
3398 if (!msg)
3399 return -ENOMEM;
3400
3401 alpha2[0] = alpha2_arg[0];
3402 alpha2[1] = alpha2_arg[1];
3403 alpha2[2] = '\0';
3404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406
3407 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
3408 if (send_and_recv_msgs(drv, msg, NULL, NULL))
3409 return -EINVAL;
3410 return 0;
3411nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003412 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 return -EINVAL;
3414}
3415
3416
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003417static int nl80211_get_country(struct nl_msg *msg, void *arg)
3418{
3419 char *alpha2 = arg;
3420 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3421 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3422
3423 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3424 genlmsg_attrlen(gnlh, 0), NULL);
3425 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
3426 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
3427 return NL_SKIP;
3428 }
3429 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
3430 return NL_SKIP;
3431}
3432
3433
3434static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
3435{
3436 struct i802_bss *bss = priv;
3437 struct wpa_driver_nl80211_data *drv = bss->drv;
3438 struct nl_msg *msg;
3439 int ret;
3440
3441 msg = nlmsg_alloc();
3442 if (!msg)
3443 return -ENOMEM;
3444
3445 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
3446 alpha2[0] = '\0';
3447 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
3448 if (!alpha2[0])
3449 ret = -1;
3450
3451 return ret;
3452}
3453
3454
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003455static int protocol_feature_handler(struct nl_msg *msg, void *arg)
3456{
3457 u32 *feat = arg;
3458 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3459 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3460
3461 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3462 genlmsg_attrlen(gnlh, 0), NULL);
3463
3464 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
3465 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
3466
3467 return NL_SKIP;
3468}
3469
3470
3471static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
3472{
3473 u32 feat = 0;
3474 struct nl_msg *msg;
3475
3476 msg = nlmsg_alloc();
3477 if (!msg)
3478 goto nla_put_failure;
3479
3480 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
3481 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
3482 return feat;
3483
3484 msg = NULL;
3485nla_put_failure:
3486 nlmsg_free(msg);
3487 return 0;
3488}
3489
3490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003492 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003493 struct wpa_driver_capa *capa;
3494
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003495 unsigned int num_multichan_concurrent;
3496
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003497 unsigned int error:1;
3498 unsigned int device_ap_sme:1;
3499 unsigned int poll_command_supported:1;
3500 unsigned int data_tx_status:1;
3501 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003502 unsigned int auth_supported:1;
3503 unsigned int connect_supported:1;
3504 unsigned int p2p_go_supported:1;
3505 unsigned int p2p_client_supported:1;
3506 unsigned int p2p_concurrent:1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003507 unsigned int channel_switch_supported:1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003508 unsigned int set_qos_map_supported:1;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003509 unsigned int have_low_prio_scan:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510};
3511
3512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003513static unsigned int probe_resp_offload_support(int supp_protocols)
3514{
3515 unsigned int prot = 0;
3516
3517 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
3518 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
3519 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
3520 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
3521 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
3522 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
3523 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
3524 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
3525
3526 return prot;
3527}
3528
3529
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003530static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
3531 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003533 struct nlattr *nl_mode;
3534 int i;
3535
3536 if (tb == NULL)
3537 return;
3538
3539 nla_for_each_nested(nl_mode, tb, i) {
3540 switch (nla_type(nl_mode)) {
3541 case NL80211_IFTYPE_AP:
3542 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
3543 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003544 case NL80211_IFTYPE_ADHOC:
3545 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
3546 break;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003547 case NL80211_IFTYPE_P2P_DEVICE:
3548 info->capa->flags |=
3549 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
3550 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003551 case NL80211_IFTYPE_P2P_GO:
3552 info->p2p_go_supported = 1;
3553 break;
3554 case NL80211_IFTYPE_P2P_CLIENT:
3555 info->p2p_client_supported = 1;
3556 break;
3557 case NL80211_IFTYPE_MONITOR:
3558 info->monitor_supported = 1;
3559 break;
3560 }
3561 }
3562}
3563
3564
3565static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
3566 struct nlattr *nl_combi)
3567{
3568 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
3569 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
3570 struct nlattr *nl_limit, *nl_mode;
3571 int err, rem_limit, rem_mode;
3572 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003573 static struct nla_policy
3574 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
3575 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
3576 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
3577 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
3578 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003579 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003580 },
3581 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
3582 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
3583 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
3584 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003585
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003586 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
3587 nl_combi, iface_combination_policy);
3588 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
3589 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
3590 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
3591 return 0; /* broken combination */
3592
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003593 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
3594 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
3595
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003596 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
3597 rem_limit) {
3598 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
3599 nl_limit, iface_limit_policy);
3600 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
3601 return 0; /* broken combination */
3602
3603 nla_for_each_nested(nl_mode,
3604 tb_limit[NL80211_IFACE_LIMIT_TYPES],
3605 rem_mode) {
3606 int ift = nla_type(nl_mode);
3607 if (ift == NL80211_IFTYPE_P2P_GO ||
3608 ift == NL80211_IFTYPE_P2P_CLIENT)
3609 combination_has_p2p = 1;
3610 if (ift == NL80211_IFTYPE_STATION)
3611 combination_has_mgd = 1;
3612 }
3613 if (combination_has_p2p && combination_has_mgd)
3614 break;
3615 }
3616
3617 if (combination_has_p2p && combination_has_mgd) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003618 unsigned int num_channels =
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003619 nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003620
3621 info->p2p_concurrent = 1;
3622 if (info->num_multichan_concurrent < num_channels)
3623 info->num_multichan_concurrent = num_channels;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003624 }
3625
3626 return 0;
3627}
3628
3629
3630static void wiphy_info_iface_comb(struct wiphy_info_data *info,
3631 struct nlattr *tb)
3632{
3633 struct nlattr *nl_combi;
3634 int rem_combi;
3635
3636 if (tb == NULL)
3637 return;
3638
3639 nla_for_each_nested(nl_combi, tb, rem_combi) {
3640 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
3641 break;
3642 }
3643}
3644
3645
3646static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
3647 struct nlattr *tb)
3648{
3649 struct nlattr *nl_cmd;
3650 int i;
3651
3652 if (tb == NULL)
3653 return;
3654
3655 nla_for_each_nested(nl_cmd, tb, i) {
3656 switch (nla_get_u32(nl_cmd)) {
3657 case NL80211_CMD_AUTHENTICATE:
3658 info->auth_supported = 1;
3659 break;
3660 case NL80211_CMD_CONNECT:
3661 info->connect_supported = 1;
3662 break;
3663 case NL80211_CMD_START_SCHED_SCAN:
3664 info->capa->sched_scan_supported = 1;
3665 break;
3666 case NL80211_CMD_PROBE_CLIENT:
3667 info->poll_command_supported = 1;
3668 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003669 case NL80211_CMD_CHANNEL_SWITCH:
3670 info->channel_switch_supported = 1;
3671 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003672 case NL80211_CMD_SET_QOS_MAP:
3673 info->set_qos_map_supported = 1;
3674 break;
3675 }
3676 }
3677}
3678
3679
3680static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
3681 struct nlattr *tb)
3682{
3683 int i, num;
3684 u32 *ciphers;
3685
3686 if (tb == NULL)
3687 return;
3688
3689 num = nla_len(tb) / sizeof(u32);
3690 ciphers = nla_data(tb);
3691 for (i = 0; i < num; i++) {
3692 u32 c = ciphers[i];
3693
3694 wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
3695 c >> 24, (c >> 16) & 0xff,
3696 (c >> 8) & 0xff, c & 0xff);
3697 switch (c) {
3698 case WLAN_CIPHER_SUITE_CCMP_256:
3699 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
3700 break;
3701 case WLAN_CIPHER_SUITE_GCMP_256:
3702 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
3703 break;
3704 case WLAN_CIPHER_SUITE_CCMP:
3705 info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
3706 break;
3707 case WLAN_CIPHER_SUITE_GCMP:
3708 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
3709 break;
3710 case WLAN_CIPHER_SUITE_TKIP:
3711 info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
3712 break;
3713 case WLAN_CIPHER_SUITE_WEP104:
3714 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
3715 break;
3716 case WLAN_CIPHER_SUITE_WEP40:
3717 info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
3718 break;
3719 case WLAN_CIPHER_SUITE_AES_CMAC:
3720 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
3721 break;
3722 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3723 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
3724 break;
3725 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3726 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
3727 break;
3728 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3729 info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
3730 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003731 case WLAN_CIPHER_SUITE_NO_GROUP_ADDR:
3732 info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
3733 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003734 }
3735 }
3736}
3737
3738
3739static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3740 struct nlattr *tb)
3741{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003742 if (tb)
3743 capa->max_remain_on_chan = nla_get_u32(tb);
3744}
3745
3746
3747static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3748 struct nlattr *ext_setup)
3749{
3750 if (tdls == NULL)
3751 return;
3752
3753 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3754 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3755
3756 if (ext_setup) {
3757 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3758 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3759 }
3760}
3761
3762
3763static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3764 struct nlattr *tb)
3765{
3766 u32 flags;
3767 struct wpa_driver_capa *capa = info->capa;
3768
3769 if (tb == NULL)
3770 return;
3771
3772 flags = nla_get_u32(tb);
3773
3774 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3775 info->data_tx_status = 1;
3776
3777 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3778 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3779
3780 if (flags & NL80211_FEATURE_SAE)
3781 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3782
3783 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3784 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003785
3786 if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
3787 capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003788
3789 if (flags & NL80211_FEATURE_LOW_PRIORITY_SCAN)
3790 info->have_low_prio_scan = 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003791}
3792
3793
3794static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3795 struct nlattr *tb)
3796{
3797 u32 protocols;
3798
3799 if (tb == NULL)
3800 return;
3801
3802 protocols = nla_get_u32(tb);
3803 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3804 "mode");
3805 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3806 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3807}
3808
3809
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003810static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
3811 struct nlattr *tb)
3812{
3813 struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
3814
3815 if (tb == NULL)
3816 return;
3817
3818 if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
3819 tb, NULL))
3820 return;
3821
3822 if (triggers[NL80211_WOWLAN_TRIG_ANY])
3823 capa->wowlan_triggers.any = 1;
3824 if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
3825 capa->wowlan_triggers.disconnect = 1;
3826 if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
3827 capa->wowlan_triggers.magic_pkt = 1;
3828 if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
3829 capa->wowlan_triggers.gtk_rekey_failure = 1;
3830 if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
3831 capa->wowlan_triggers.eap_identity_req = 1;
3832 if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
3833 capa->wowlan_triggers.four_way_handshake = 1;
3834 if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
3835 capa->wowlan_triggers.rfkill_release = 1;
3836}
3837
3838
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003839static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3840{
3841 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3842 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3843 struct wiphy_info_data *info = arg;
3844 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003845 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3848 genlmsg_attrlen(gnlh, 0), NULL);
3849
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003850 if (tb[NL80211_ATTR_WIPHY_NAME])
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003851 os_strlcpy(drv->phyname,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003852 nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
3853 sizeof(drv->phyname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003855 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3857
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003858 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3859 capa->max_sched_scan_ssids =
3860 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3861
3862 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3863 capa->max_match_sets =
3864 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3865
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003866 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3867 capa->max_acl_mac_addrs =
3868 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3869
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003870 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3871 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3872 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003873 wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003875 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3876 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3877 "off-channel TX");
3878 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3879 }
3880
3881 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3882 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3883 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3884 }
3885
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003886 wiphy_info_max_roc(capa,
3887 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003888
3889 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3890 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003892 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3893 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003894
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003895 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3896 info->device_ap_sme = 1;
3897
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003898 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3899 wiphy_info_probe_resp_offload(capa,
3900 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003901
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003902 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3903 drv->extended_capa == NULL) {
3904 drv->extended_capa =
3905 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3906 if (drv->extended_capa) {
3907 os_memcpy(drv->extended_capa,
3908 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3909 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3910 drv->extended_capa_len =
3911 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3912 }
3913 drv->extended_capa_mask =
3914 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3915 if (drv->extended_capa_mask) {
3916 os_memcpy(drv->extended_capa_mask,
3917 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3918 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3919 } else {
3920 os_free(drv->extended_capa);
3921 drv->extended_capa = NULL;
3922 drv->extended_capa_len = 0;
3923 }
3924 }
3925
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003926 if (tb[NL80211_ATTR_VENDOR_DATA]) {
3927 struct nlattr *nl;
3928 int rem;
3929
3930 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
3931 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003932 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003933 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3934 continue;
3935 }
3936 vinfo = nla_data(nl);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003937 switch (vinfo->subcmd) {
3938 case QCA_NL80211_VENDOR_SUBCMD_ROAMING:
3939 drv->roaming_vendor_cmd_avail = 1;
3940 break;
3941 case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003942 drv->dfs_vendor_cmd_avail = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003943 break;
3944 }
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003945
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003946 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
3947 vinfo->vendor_id, vinfo->subcmd);
3948 }
3949 }
3950
3951 if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
3952 struct nlattr *nl;
3953 int rem;
3954
3955 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
3956 struct nl80211_vendor_cmd_info *vinfo;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003957 if (nla_len(nl) != sizeof(*vinfo)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003958 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
3959 continue;
3960 }
3961 vinfo = nla_data(nl);
3962 wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
3963 vinfo->vendor_id, vinfo->subcmd);
3964 }
3965 }
3966
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003967 wiphy_info_wowlan_triggers(capa,
3968 tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
3969
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003970 if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
3971 capa->max_stations =
3972 nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
3973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974 return NL_SKIP;
3975}
3976
3977
3978static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3979 struct wiphy_info_data *info)
3980{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003981 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982 struct nl_msg *msg;
3983
3984 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003986 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987
3988 msg = nlmsg_alloc();
3989 if (!msg)
3990 return -1;
3991
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003992 feat = get_nl80211_protocol_features(drv);
3993 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3994 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3995 else
3996 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003998 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003999 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004000 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004002 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
4003 return -1;
4004
4005 if (info->auth_supported)
4006 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
4007 else if (!info->connect_supported) {
4008 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
4009 "authentication/association or connect commands");
4010 info->error = 1;
4011 }
4012
4013 if (info->p2p_go_supported && info->p2p_client_supported)
4014 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
4015 if (info->p2p_concurrent) {
4016 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
4017 "interface (driver advertised support)");
4018 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
4019 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
4020 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004021 if (info->num_multichan_concurrent > 1) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004022 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
4023 "concurrent (driver advertised support)");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004024 drv->capa.num_multichan_concurrent =
4025 info->num_multichan_concurrent;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004026 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004027
4028 /* default to 5000 since early versions of mac80211 don't set it */
4029 if (!drv->capa.max_remain_on_chan)
4030 drv->capa.max_remain_on_chan = 5000;
4031
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004032 if (info->channel_switch_supported)
4033 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
4034
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004035 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036nla_put_failure:
4037 nlmsg_free(msg);
4038 return -1;
4039}
4040
4041
4042static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
4043{
4044 struct wiphy_info_data info;
4045 if (wpa_driver_nl80211_get_info(drv, &info))
4046 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004047
4048 if (info.error)
4049 return -1;
4050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051 drv->has_capability = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4053 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4054 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4055 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
4057 WPA_DRIVER_AUTH_SHARED |
4058 WPA_DRIVER_AUTH_LEAP;
4059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
4061 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004063
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004064 /*
4065 * As all cfg80211 drivers must support cases where the AP interface is
4066 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
4067 * case that the user space daemon has crashed, they must be able to
4068 * cleanup all stations and key entries in the AP tear down flow. Thus,
4069 * this flag can/should always be set for cfg80211 drivers.
4070 */
4071 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
4072
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004073 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004074 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004076 /*
4077 * No AP SME is currently assumed to also indicate no AP MLME
4078 * in the driver/firmware.
4079 */
4080 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
4081 }
4082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004083 drv->device_ap_sme = info.device_ap_sme;
4084 drv->poll_command_supported = info.poll_command_supported;
4085 drv->data_tx_status = info.data_tx_status;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004086 if (info.set_qos_map_supported)
4087 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004088 drv->have_low_prio_scan = info.have_low_prio_scan;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004089
4090 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004091 * If poll command and tx status are supported, mac80211 is new enough
4092 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004093 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004094 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004095
4096 if (drv->device_ap_sme && drv->use_monitor) {
4097 /*
4098 * Non-mac80211 drivers may not support monitor interface.
4099 * Make sure we do not get stuck with incorrect capability here
4100 * by explicitly testing this.
4101 */
4102 if (!info.monitor_supported) {
4103 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
4104 "with device_ap_sme since no monitor mode "
4105 "support detected");
4106 drv->use_monitor = 0;
4107 }
4108 }
4109
4110 /*
4111 * If we aren't going to use monitor interfaces, but the
4112 * driver doesn't support data TX status, we won't get TX
4113 * status for EAPOL frames.
4114 */
4115 if (!drv->use_monitor && !info.data_tx_status)
4116 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117
4118 return 0;
4119}
4120
4121
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004122#ifdef ANDROID
4123static int android_genl_ctrl_resolve(struct nl_handle *handle,
4124 const char *name)
4125{
4126 /*
4127 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
4128 * need to work around that.
4129 */
4130 struct nl_cache *cache = NULL;
4131 struct genl_family *nl80211 = NULL;
4132 int id = -1;
4133
4134 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
4135 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
4136 "netlink cache");
4137 goto fail;
4138 }
4139
4140 nl80211 = genl_ctrl_search_by_name(cache, name);
4141 if (nl80211 == NULL)
4142 goto fail;
4143
4144 id = genl_family_get_id(nl80211);
4145
4146fail:
4147 if (nl80211)
4148 genl_family_put(nl80211);
4149 if (cache)
4150 nl_cache_free(cache);
4151
4152 return id;
4153}
4154#define genl_ctrl_resolve android_genl_ctrl_resolve
4155#endif /* ANDROID */
4156
4157
4158static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159{
4160 int ret;
4161
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004162 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4163 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
4165 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004166 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 }
4168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004169 global->nl = nl_create_handle(global->nl_cb, "nl");
4170 if (global->nl == NULL)
4171 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004173 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
4174 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
4176 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004177 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 }
4179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004180 global->nl_event = nl_create_handle(global->nl_cb, "event");
4181 if (global->nl_event == NULL)
4182 goto err;
4183
4184 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187 if (ret < 0) {
4188 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4189 "membership for scan events: %d (%s)",
4190 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004191 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192 }
4193
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004194 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004196 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 if (ret < 0) {
4198 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
4199 "membership for mlme events: %d (%s)",
4200 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004201 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 }
4203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004204 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004206 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 if (ret < 0) {
4208 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4209 "membership for regulatory events: %d (%s)",
4210 ret, strerror(-ret));
4211 /* Continue without regulatory events */
4212 }
4213
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004214 ret = nl_get_multicast_id(global, "nl80211", "vendor");
4215 if (ret >= 0)
4216 ret = nl_socket_add_membership(global->nl_event, ret);
4217 if (ret < 0) {
4218 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
4219 "membership for vendor events: %d (%s)",
4220 ret, strerror(-ret));
4221 /* Continue without vendor events */
4222 }
4223
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004224 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4225 no_seq_check, NULL);
4226 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4227 process_global_event, global);
4228
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004229 nl80211_register_eloop_read(&global->nl_event,
4230 wpa_driver_nl80211_event_receive,
4231 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232
4233 return 0;
4234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004235err:
4236 nl_destroy_handles(&global->nl_event);
4237 nl_destroy_handles(&global->nl);
4238 nl_cb_put(global->nl_cb);
4239 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 return -1;
4241}
4242
4243
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004244static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
4245{
4246 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4247 if (!drv->nl_cb) {
4248 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
4249 return -1;
4250 }
4251
4252 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4253 no_seq_check, NULL);
4254 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4255 process_drv_event, drv);
4256
4257 return 0;
4258}
4259
4260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
4262{
4263 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
4264 /*
4265 * This may be for any interface; use ifdown event to disable
4266 * interface.
4267 */
4268}
4269
4270
4271static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
4272{
4273 struct wpa_driver_nl80211_data *drv = ctx;
4274 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004275 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
4277 "after rfkill unblock");
4278 return;
4279 }
4280 /* rtnetlink ifup handler will report interface as enabled */
4281}
4282
4283
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004284static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
4285 void *eloop_ctx,
4286 void *handle)
4287{
4288 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4289 u8 data[2048];
4290 struct msghdr msg;
4291 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004292 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004293 struct cmsghdr *cmsg;
4294 int res, found_ee = 0, found_wifi = 0, acked = 0;
4295 union wpa_event_data event;
4296
4297 memset(&msg, 0, sizeof(msg));
4298 msg.msg_iov = &entry;
4299 msg.msg_iovlen = 1;
4300 entry.iov_base = data;
4301 entry.iov_len = sizeof(data);
4302 msg.msg_control = &control;
4303 msg.msg_controllen = sizeof(control);
4304
4305 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
4306 /* if error or not fitting 802.3 header, return */
4307 if (res < 14)
4308 return;
4309
4310 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
4311 {
4312 if (cmsg->cmsg_level == SOL_SOCKET &&
4313 cmsg->cmsg_type == SCM_WIFI_STATUS) {
4314 int *ack;
4315
4316 found_wifi = 1;
4317 ack = (void *)CMSG_DATA(cmsg);
4318 acked = *ack;
4319 }
4320
4321 if (cmsg->cmsg_level == SOL_PACKET &&
4322 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
4323 struct sock_extended_err *err =
4324 (struct sock_extended_err *)CMSG_DATA(cmsg);
4325
4326 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
4327 found_ee = 1;
4328 }
4329 }
4330
4331 if (!found_ee || !found_wifi)
4332 return;
4333
4334 memset(&event, 0, sizeof(event));
4335 event.eapol_tx_status.dst = data;
4336 event.eapol_tx_status.data = data + 14;
4337 event.eapol_tx_status.data_len = res - 14;
4338 event.eapol_tx_status.ack = acked;
4339 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
4340}
4341
4342
4343static int nl80211_init_bss(struct i802_bss *bss)
4344{
4345 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
4346 if (!bss->nl_cb)
4347 return -1;
4348
4349 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
4350 no_seq_check, NULL);
4351 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
4352 process_bss_event, bss);
4353
4354 return 0;
4355}
4356
4357
4358static void nl80211_destroy_bss(struct i802_bss *bss)
4359{
4360 nl_cb_put(bss->nl_cb);
4361 bss->nl_cb = NULL;
4362}
4363
4364
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004365static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
4366 void *global_priv, int hostapd,
4367 const u8 *set_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368{
4369 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 struct rfkill_config *rcfg;
4371 struct i802_bss *bss;
4372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004373 if (global_priv == NULL)
4374 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375 drv = os_zalloc(sizeof(*drv));
4376 if (drv == NULL)
4377 return NULL;
4378 drv->global = global_priv;
4379 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004380 drv->hostapd = !!hostapd;
4381 drv->eapol_sock = -1;
4382 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4383 drv->if_indices = drv->default_if_indices;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004384
4385 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
4386 if (!drv->first_bss) {
4387 os_free(drv);
4388 return NULL;
4389 }
4390 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004392 bss->ctx = ctx;
4393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
4395 drv->monitor_ifidx = -1;
4396 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004397 drv->eapol_tx_sock = -1;
4398 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399
4400 if (wpa_driver_nl80211_init_nl(drv)) {
4401 os_free(drv);
4402 return NULL;
4403 }
4404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004405 if (nl80211_init_bss(bss))
4406 goto failed;
4407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408 rcfg = os_zalloc(sizeof(*rcfg));
4409 if (rcfg == NULL)
4410 goto failed;
4411 rcfg->ctx = drv;
4412 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
4413 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
4414 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
4415 drv->rfkill = rfkill_init(rcfg);
4416 if (drv->rfkill == NULL) {
4417 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
4418 os_free(rcfg);
4419 }
4420
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004421 if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
4422 drv->start_iface_up = 1;
4423
4424 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 goto failed;
4426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004427 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
4428 if (drv->eapol_tx_sock < 0)
4429 goto failed;
4430
4431 if (drv->data_tx_status) {
4432 int enabled = 1;
4433
4434 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
4435 &enabled, sizeof(enabled)) < 0) {
4436 wpa_printf(MSG_DEBUG,
4437 "nl80211: wifi status sockopt failed\n");
4438 drv->data_tx_status = 0;
4439 if (!drv->use_monitor)
4440 drv->capa.flags &=
4441 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
4442 } else {
4443 eloop_register_read_sock(drv->eapol_tx_sock,
4444 wpa_driver_nl80211_handle_eapol_tx_status,
4445 drv, NULL);
4446 }
4447 }
4448
4449 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004451 drv->in_interface_list = 1;
4452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453
4454 return bss;
4455
4456failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004457 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 return NULL;
4459}
4460
4461
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004462/**
4463 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
4464 * @ctx: context to be used when calling wpa_supplicant functions,
4465 * e.g., wpa_supplicant_event()
4466 * @ifname: interface name, e.g., wlan0
4467 * @global_priv: private driver global data from global_init()
4468 * Returns: Pointer to private data, %NULL on failure
4469 */
4470static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
4471 void *global_priv)
4472{
4473 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL);
4474}
4475
4476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004477static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 struct nl_handle *nl_handle,
4479 u16 type, const u8 *match, size_t match_len)
4480{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004481 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 struct nl_msg *msg;
4483 int ret = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004484 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485
4486 msg = nlmsg_alloc();
4487 if (!msg)
4488 return -1;
4489
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004490 buf[0] = '\0';
4491 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004492 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
4493 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004495 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
4496
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004497 if (nl80211_set_iface_id(msg, bss) < 0)
4498 goto nla_put_failure;
4499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004500 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
4501 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
4502
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004503 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 msg = NULL;
4505 if (ret) {
4506 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
4507 "failed (type=%u): ret=%d (%s)",
4508 type, ret, strerror(-ret));
4509 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
4510 match, match_len);
4511 goto nla_put_failure;
4512 }
4513 ret = 0;
4514nla_put_failure:
4515 nlmsg_free(msg);
4516 return ret;
4517}
4518
4519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004520static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
4521{
4522 struct wpa_driver_nl80211_data *drv = bss->drv;
4523
4524 if (bss->nl_mgmt) {
4525 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
4526 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
4527 return -1;
4528 }
4529
4530 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
4531 if (bss->nl_mgmt == NULL)
4532 return -1;
4533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004534 return 0;
4535}
4536
4537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004538static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
4539{
4540 nl80211_register_eloop_read(&bss->nl_mgmt,
4541 wpa_driver_nl80211_event_receive,
4542 bss->nl_cb);
4543}
4544
4545
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004546static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 const u8 *match, size_t match_len)
4548{
4549 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004550 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004551 type, match, match_len);
4552}
4553
4554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004555static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004557 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004558 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004559
4560 if (nl80211_alloc_mgmt_handle(bss))
4561 return -1;
4562 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
4563 "handle %p", bss->nl_mgmt);
4564
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004565 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
4566 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
4567
4568 /* register for any AUTH message */
4569 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
4570 }
4571
Dmitry Shmidt051af732013-10-22 13:52:46 -07004572#ifdef CONFIG_INTERWORKING
4573 /* QoS Map Configure */
4574 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004575 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004576#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004577#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004580 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004582 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004583 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004585 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004586 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004588 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004589 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004590 /* Protected GAS Initial Request */
4591 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
4592 ret = -1;
4593 /* Protected GAS Initial Response */
4594 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
4595 ret = -1;
4596 /* Protected GAS Comeback Request */
4597 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
4598 ret = -1;
4599 /* Protected GAS Comeback Response */
4600 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
4601 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004602#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
4603#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004605 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
4607 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004608 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004610 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 (u8 *) "\x7f\x50\x6f\x9a\x09",
4612 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004613 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614#endif /* CONFIG_P2P */
4615#ifdef CONFIG_IEEE80211W
4616 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004617 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004618 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004620#ifdef CONFIG_TDLS
4621 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
4622 /* TDLS Discovery Response */
4623 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
4624 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004625 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004626 }
4627#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628
4629 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004630 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004631 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 else
4633 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
4634 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
4635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004636 /* WNM - BSS Transition Management Request */
4637 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004638 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004639 /* WNM-Sleep Mode Response */
4640 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004641 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004642
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004643#ifdef CONFIG_HS20
4644 /* WNM-Notification */
4645 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004646 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004647#endif /* CONFIG_HS20 */
4648
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004649 nl80211_mgmt_handle_register_eloop(bss);
4650
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004651 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652}
4653
4654
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004655static int nl80211_register_spurious_class3(struct i802_bss *bss)
4656{
4657 struct wpa_driver_nl80211_data *drv = bss->drv;
4658 struct nl_msg *msg;
4659 int ret = -1;
4660
4661 msg = nlmsg_alloc();
4662 if (!msg)
4663 return -1;
4664
4665 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
4666
4667 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
4668
4669 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
4670 msg = NULL;
4671 if (ret) {
4672 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
4673 "failed: ret=%d (%s)",
4674 ret, strerror(-ret));
4675 goto nla_put_failure;
4676 }
4677 ret = 0;
4678nla_put_failure:
4679 nlmsg_free(msg);
4680 return ret;
4681}
4682
4683
4684static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
4685{
4686 static const int stypes[] = {
4687 WLAN_FC_STYPE_AUTH,
4688 WLAN_FC_STYPE_ASSOC_REQ,
4689 WLAN_FC_STYPE_REASSOC_REQ,
4690 WLAN_FC_STYPE_DISASSOC,
4691 WLAN_FC_STYPE_DEAUTH,
4692 WLAN_FC_STYPE_ACTION,
4693 WLAN_FC_STYPE_PROBE_REQ,
4694/* Beacon doesn't work as mac80211 doesn't currently allow
4695 * it, but it wouldn't really be the right thing anyway as
4696 * it isn't per interface ... maybe just dump the scan
4697 * results periodically for OLBC?
4698 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004699 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004700 };
4701 unsigned int i;
4702
4703 if (nl80211_alloc_mgmt_handle(bss))
4704 return -1;
4705 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4706 "handle %p", bss->nl_mgmt);
4707
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004708 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004709 if (nl80211_register_frame(bss, bss->nl_mgmt,
4710 (WLAN_FC_TYPE_MGMT << 2) |
4711 (stypes[i] << 4),
4712 NULL, 0) < 0) {
4713 goto out_err;
4714 }
4715 }
4716
4717 if (nl80211_register_spurious_class3(bss))
4718 goto out_err;
4719
4720 if (nl80211_get_wiphy_data_ap(bss) == NULL)
4721 goto out_err;
4722
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004723 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004724 return 0;
4725
4726out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004727 nl_destroy_handles(&bss->nl_mgmt);
4728 return -1;
4729}
4730
4731
4732static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
4733{
4734 if (nl80211_alloc_mgmt_handle(bss))
4735 return -1;
4736 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
4737 "handle %p (device SME)", bss->nl_mgmt);
4738
4739 if (nl80211_register_frame(bss, bss->nl_mgmt,
4740 (WLAN_FC_TYPE_MGMT << 2) |
4741 (WLAN_FC_STYPE_ACTION << 4),
4742 NULL, 0) < 0)
4743 goto out_err;
4744
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004745 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004746 return 0;
4747
4748out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004749 nl_destroy_handles(&bss->nl_mgmt);
4750 return -1;
4751}
4752
4753
4754static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
4755{
4756 if (bss->nl_mgmt == NULL)
4757 return;
4758 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
4759 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004760 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004761
4762 nl80211_put_wiphy_data_ap(bss);
4763}
4764
4765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
4767{
4768 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
4769}
4770
4771
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004772static void nl80211_del_p2pdev(struct i802_bss *bss)
4773{
4774 struct wpa_driver_nl80211_data *drv = bss->drv;
4775 struct nl_msg *msg;
4776 int ret;
4777
4778 msg = nlmsg_alloc();
4779 if (!msg)
4780 return;
4781
4782 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4783 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4784
4785 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4786 msg = NULL;
4787
4788 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
4789 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004790 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004791
4792nla_put_failure:
4793 nlmsg_free(msg);
4794}
4795
4796
4797static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
4798{
4799 struct wpa_driver_nl80211_data *drv = bss->drv;
4800 struct nl_msg *msg;
4801 int ret = -1;
4802
4803 msg = nlmsg_alloc();
4804 if (!msg)
4805 return -1;
4806
4807 if (start)
4808 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
4809 else
4810 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
4811
4812 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
4813
4814 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4815 msg = NULL;
4816
4817 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
4818 start ? "Start" : "Stop",
4819 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004820 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004821
4822nla_put_failure:
4823 nlmsg_free(msg);
4824 return ret;
4825}
4826
4827
4828static int i802_set_iface_flags(struct i802_bss *bss, int up)
4829{
4830 enum nl80211_iftype nlmode;
4831
4832 nlmode = nl80211_get_ifmode(bss);
4833 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
4834 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
4835 bss->ifname, up);
4836 }
4837
4838 /* P2P Device has start/stop which is equivalent */
4839 return nl80211_set_p2pdev(bss, up);
4840}
4841
4842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004844wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
4845 const u8 *set_addr, int first)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004847 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004849 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
4851 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004852 bss->ifindex = drv->ifindex;
4853 bss->wdev_id = drv->global->if_add_wdevid;
4854 bss->wdev_id_set = drv->global->if_add_wdevid_set;
4855
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004856 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
4857 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004858 drv->global->if_add_wdevid_set = 0;
4859
Dmitry Shmidt03658832014-08-13 11:03:49 -07004860 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4861 bss->static_ap = 1;
4862
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004863 if (wpa_driver_nl80211_capa(drv))
4864 return -1;
4865
4866 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
4867 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004869 if (set_addr &&
4870 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
4871 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4872 set_addr)))
4873 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004874
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004875 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
4876 drv->start_mode_ap = 1;
4877
Dmitry Shmidt03658832014-08-13 11:03:49 -07004878 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004879 nlmode = NL80211_IFTYPE_AP;
4880 else if (bss->if_dynamic)
4881 nlmode = nl80211_get_ifmode(bss);
4882 else
4883 nlmode = NL80211_IFTYPE_STATION;
4884
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004885 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004886 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004887 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004889
Dmitry Shmidt98660862014-03-11 17:26:21 -07004890 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004891 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892
Dmitry Shmidt98660862014-03-11 17:26:21 -07004893 if (!rfkill_is_blocked(drv->rfkill)) {
4894 int ret = i802_set_iface_flags(bss, 1);
4895 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 wpa_printf(MSG_ERROR, "nl80211: Could not set "
4897 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07004898 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07004900 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4901 return ret;
4902 } else {
4903 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
4904 "interface '%s' due to rfkill", bss->ifname);
4905 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
4906 return 0;
4907 drv->if_disabled = 1;
4908 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909 }
4910
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004911 if (!drv->hostapd)
4912 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
4913 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004915 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
4916 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004918 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 if (send_rfkill_event) {
4921 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
4922 drv, drv->ctx);
4923 }
4924
4925 return 0;
4926}
4927
4928
4929static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
4930{
4931 struct nl_msg *msg;
4932
4933 msg = nlmsg_alloc();
4934 if (!msg)
4935 return -ENOMEM;
4936
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004937 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
4938 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004939 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4941
4942 return send_and_recv_msgs(drv, msg, NULL, NULL);
4943 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004944 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 return -ENOBUFS;
4946}
4947
4948
4949/**
4950 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004951 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 *
4953 * Shut down driver interface and processing of driver events. Free
4954 * private data buffer if one was allocated in wpa_driver_nl80211_init().
4955 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004956static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 struct wpa_driver_nl80211_data *drv = bss->drv;
4959
Dmitry Shmidt04949592012-07-19 12:16:46 -07004960 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004961 if (drv->data_tx_status)
4962 eloop_unregister_read_sock(drv->eapol_tx_sock);
4963 if (drv->eapol_tx_sock >= 0)
4964 close(drv->eapol_tx_sock);
4965
4966 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 wpa_driver_nl80211_probe_req_report(bss, 0);
4968 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004969 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4970 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4972 "interface %s from bridge %s: %s",
4973 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004974 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004975 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976 }
4977 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004978 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4980 "bridge %s: %s",
4981 bss->brname, strerror(errno));
4982 }
4983
4984 nl80211_remove_monitor_interface(drv);
4985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004986 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 wpa_driver_nl80211_del_beacon(drv);
4988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 if (drv->eapol_sock >= 0) {
4990 eloop_unregister_read_sock(drv->eapol_sock);
4991 close(drv->eapol_sock);
4992 }
4993
4994 if (drv->if_indices != drv->default_if_indices)
4995 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004997 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4999
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005000 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
5001 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005002 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 rfkill_deinit(drv->rfkill);
5004
5005 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5006
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005007 if (!drv->start_iface_up)
5008 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005009
5010 if (drv->addr_changed) {
5011 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
5012 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5013 drv->perm_addr) < 0) {
5014 wpa_printf(MSG_DEBUG,
5015 "nl80211: Could not restore permanent MAC address");
5016 }
5017 }
5018
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005019 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005020 if (!drv->hostapd || !drv->start_mode_ap)
5021 wpa_driver_nl80211_set_mode(bss,
5022 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005023 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005024 } else {
5025 nl80211_mgmt_unsubscribe(bss, "deinit");
5026 nl80211_del_p2pdev(bss);
5027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 nl_cb_put(drv->nl_cb);
5029
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005030 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 os_free(drv->filter_ssids);
5033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005034 os_free(drv->auth_ie);
5035
5036 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 dl_list_del(&drv->list);
5038
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005039 os_free(drv->extended_capa);
5040 os_free(drv->extended_capa_mask);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005041 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 os_free(drv);
5043}
5044
5045
5046/**
5047 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
5048 * @eloop_ctx: Driver private data
5049 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
5050 *
5051 * This function can be used as registered timeout when starting a scan to
5052 * generate a scan completed event if the driver does not report this.
5053 */
5054static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
5055{
5056 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005057 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005058 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059 drv->ap_scan_as_station);
5060 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 }
5062 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
5063 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
5064}
5065
5066
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005067static struct nl_msg *
5068nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005069 struct wpa_driver_scan_params *params, u64 *wdev_id)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005070{
5071 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005072 size_t i;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005073 u32 scan_flags = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005074
5075 msg = nlmsg_alloc();
5076 if (!msg)
5077 return NULL;
5078
5079 nl80211_cmd(drv, msg, 0, cmd);
5080
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005081 if (!wdev_id)
5082 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5083 else
5084 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005085
5086 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005087 struct nlattr *ssids;
5088
5089 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005090 if (ssids == NULL)
5091 goto fail;
5092 for (i = 0; i < params->num_ssids; i++) {
5093 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
5094 params->ssids[i].ssid,
5095 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005096 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
5097 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005098 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005099 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005100 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005101 }
5102
5103 if (params->extra_ies) {
5104 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
5105 params->extra_ies, params->extra_ies_len);
5106 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
5107 params->extra_ies) < 0)
5108 goto fail;
5109 }
5110
5111 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005112 struct nlattr *freqs;
5113 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005114 if (freqs == NULL)
5115 goto fail;
5116 for (i = 0; params->freqs[i]; i++) {
5117 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
5118 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005119 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005120 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005121 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005122 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005123 }
5124
5125 os_free(drv->filter_ssids);
5126 drv->filter_ssids = params->filter_ssids;
5127 params->filter_ssids = NULL;
5128 drv->num_filter_ssids = params->num_filter_ssids;
5129
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005130 if (params->only_new_results) {
5131 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005132 scan_flags |= NL80211_SCAN_FLAG_FLUSH;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005133 }
5134
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005135 if (params->low_priority && drv->have_low_prio_scan) {
5136 wpa_printf(MSG_DEBUG,
5137 "nl80211: Add NL80211_SCAN_FLAG_LOW_PRIORITY");
5138 scan_flags |= NL80211_SCAN_FLAG_LOW_PRIORITY;
5139 }
5140
5141 if (scan_flags)
5142 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags);
5143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005144 return msg;
5145
5146fail:
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005147nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005148 nlmsg_free(msg);
5149 return NULL;
5150}
5151
5152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153/**
5154 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005155 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005156 * @params: Scan parameters
5157 * Returns: 0 on success, -1 on failure
5158 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005159static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160 struct wpa_driver_scan_params *params)
5161{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005163 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005164 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005166 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005167 drv->scan_for_auth = 0;
5168
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005169 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
5170 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005171 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005175 struct nlattr *rates;
5176
Dmitry Shmidt04949592012-07-19 12:16:46 -07005177 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
5178
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005179 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005180 if (rates == NULL)
5181 goto nla_put_failure;
5182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005183 /*
5184 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
5185 * by masking out everything else apart from the OFDM rates 6,
5186 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
5187 * rates are left enabled.
5188 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005189 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005190 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005191 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005192
5193 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
5194 }
5195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5197 msg = NULL;
5198 if (ret) {
5199 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
5200 "(%s)", ret, strerror(-ret));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005201 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
Dmitry Shmidted003d22014-02-06 10:09:12 -08005202 enum nl80211_iftype old_mode = drv->nlmode;
5203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 /*
5205 * mac80211 does not allow scan requests in AP mode, so
5206 * try to do this in station mode.
5207 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005208 if (wpa_driver_nl80211_set_mode(
5209 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 goto nla_put_failure;
5211
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005212 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005213 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 goto nla_put_failure;
5215 }
5216
5217 /* Restore AP mode when processing scan results */
Dmitry Shmidted003d22014-02-06 10:09:12 -08005218 drv->ap_scan_as_station = old_mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005219 ret = 0;
5220 } else
5221 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005222 }
5223
Dmitry Shmidt56052862013-10-04 10:23:25 -07005224 drv->scan_state = SCAN_REQUESTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005225 /* Not all drivers generate "scan completed" wireless event, so try to
5226 * read results after a timeout. */
5227 timeout = 10;
5228 if (drv->scan_complete_events) {
5229 /*
5230 * The driver seems to deliver events to notify when scan is
5231 * complete, so use longer timeout to avoid race conditions
5232 * with scanning and following association request.
5233 */
5234 timeout = 30;
5235 }
5236 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
5237 "seconds", ret, timeout);
5238 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
5239 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
5240 drv, drv->ctx);
5241
5242nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005243 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005244 return ret;
5245}
5246
5247
5248/**
5249 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
5250 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5251 * @params: Scan parameters
5252 * @interval: Interval between scan cycles in milliseconds
5253 * Returns: 0 on success, -1 on failure or if not supported
5254 */
5255static int wpa_driver_nl80211_sched_scan(void *priv,
5256 struct wpa_driver_scan_params *params,
5257 u32 interval)
5258{
5259 struct i802_bss *bss = priv;
5260 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005261 int ret = -1;
5262 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 size_t i;
5264
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005265 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
5266
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005267#ifdef ANDROID
5268 if (!drv->capa.sched_scan_supported)
5269 return android_pno_start(bss, params);
5270#endif /* ANDROID */
5271
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005272 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
5273 bss->wdev_id_set ? &bss->wdev_id : NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005274 if (!msg)
5275 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005276
5277 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
5278
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005279 if ((drv->num_filter_ssids &&
5280 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
5281 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005282 struct nlattr *match_sets;
5283 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005284 if (match_sets == NULL)
5285 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005286
5287 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005288 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005289 wpa_hexdump_ascii(MSG_MSGDUMP,
5290 "nl80211: Sched scan filter SSID",
5291 drv->filter_ssids[i].ssid,
5292 drv->filter_ssids[i].ssid_len);
5293
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005294 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005295 if (match_set_ssid == NULL)
5296 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005297 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005298 drv->filter_ssids[i].ssid_len,
5299 drv->filter_ssids[i].ssid);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005300 if (params->filter_rssi)
5301 NLA_PUT_U32(msg,
5302 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
5303 params->filter_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005304
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005305 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005306 }
5307
Dmitry Shmidt97672262014-02-03 13:02:54 -08005308 /*
5309 * Due to backward compatibility code, newer kernels treat this
5310 * matchset (with only an RSSI filter) as the default for all
5311 * other matchsets, unless it's the only one, in which case the
5312 * matchset will actually allow all SSIDs above the RSSI.
5313 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005314 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005315 struct nlattr *match_set_rssi;
5316 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005317 if (match_set_rssi == NULL)
5318 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005319 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005320 params->filter_rssi);
5321 wpa_printf(MSG_MSGDUMP,
5322 "nl80211: Sched scan RSSI filter %d dBm",
5323 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005324 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005325 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005326
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005327 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005328 }
5329
5330 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5331
5332 /* TODO: if we get an error here, we should fall back to normal scan */
5333
5334 msg = NULL;
5335 if (ret) {
5336 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
5337 "ret=%d (%s)", ret, strerror(-ret));
5338 goto nla_put_failure;
5339 }
5340
5341 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
5342 "scan interval %d msec", ret, interval);
5343
5344nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005345 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005346 return ret;
5347}
5348
5349
5350/**
5351 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
5352 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
5353 * Returns: 0 on success, -1 on failure or if not supported
5354 */
5355static int wpa_driver_nl80211_stop_sched_scan(void *priv)
5356{
5357 struct i802_bss *bss = priv;
5358 struct wpa_driver_nl80211_data *drv = bss->drv;
5359 int ret = 0;
5360 struct nl_msg *msg;
5361
5362#ifdef ANDROID
5363 if (!drv->capa.sched_scan_supported)
5364 return android_pno_stop(bss);
5365#endif /* ANDROID */
5366
5367 msg = nlmsg_alloc();
5368 if (!msg)
5369 return -1;
5370
5371 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
5372
5373 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5374
5375 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5376 msg = NULL;
5377 if (ret) {
5378 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
5379 "ret=%d (%s)", ret, strerror(-ret));
5380 goto nla_put_failure;
5381 }
5382
5383 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
5384
5385nla_put_failure:
5386 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 return ret;
5388}
5389
5390
5391static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
5392{
5393 const u8 *end, *pos;
5394
5395 if (ies == NULL)
5396 return NULL;
5397
5398 pos = ies;
5399 end = ies + ies_len;
5400
5401 while (pos + 1 < end) {
5402 if (pos + 2 + pos[1] > end)
5403 break;
5404 if (pos[0] == ie)
5405 return pos;
5406 pos += 2 + pos[1];
5407 }
5408
5409 return NULL;
5410}
5411
5412
5413static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
5414 const u8 *ie, size_t ie_len)
5415{
5416 const u8 *ssid;
5417 size_t i;
5418
5419 if (drv->filter_ssids == NULL)
5420 return 0;
5421
5422 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
5423 if (ssid == NULL)
5424 return 1;
5425
5426 for (i = 0; i < drv->num_filter_ssids; i++) {
5427 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
5428 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
5429 0)
5430 return 0;
5431 }
5432
5433 return 1;
5434}
5435
5436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437static int bss_info_handler(struct nl_msg *msg, void *arg)
5438{
5439 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5440 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5441 struct nlattr *bss[NL80211_BSS_MAX + 1];
5442 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
5443 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
5444 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
5445 [NL80211_BSS_TSF] = { .type = NLA_U64 },
5446 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
5447 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
5448 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
5449 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
5450 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
5451 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
5452 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
5453 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
5454 };
5455 struct nl80211_bss_info_arg *_arg = arg;
5456 struct wpa_scan_results *res = _arg->res;
5457 struct wpa_scan_res **tmp;
5458 struct wpa_scan_res *r;
5459 const u8 *ie, *beacon_ie;
5460 size_t ie_len, beacon_ie_len;
5461 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005462 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463
5464 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5465 genlmsg_attrlen(gnlh, 0), NULL);
5466 if (!tb[NL80211_ATTR_BSS])
5467 return NL_SKIP;
5468 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
5469 bss_policy))
5470 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03005471 if (bss[NL80211_BSS_STATUS]) {
5472 enum nl80211_bss_status status;
5473 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5474 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5475 bss[NL80211_BSS_FREQUENCY]) {
5476 _arg->assoc_freq =
5477 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5478 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
5479 _arg->assoc_freq);
5480 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07005481 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
5482 bss[NL80211_BSS_FREQUENCY]) {
5483 _arg->ibss_freq =
5484 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5485 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
5486 _arg->ibss_freq);
5487 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005488 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
5489 bss[NL80211_BSS_BSSID]) {
5490 os_memcpy(_arg->assoc_bssid,
5491 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
5492 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
5493 MACSTR, MAC2STR(_arg->assoc_bssid));
5494 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03005495 }
5496 if (!res)
5497 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005498 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
5499 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5500 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
5501 } else {
5502 ie = NULL;
5503 ie_len = 0;
5504 }
5505 if (bss[NL80211_BSS_BEACON_IES]) {
5506 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
5507 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
5508 } else {
5509 beacon_ie = NULL;
5510 beacon_ie_len = 0;
5511 }
5512
5513 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
5514 ie ? ie_len : beacon_ie_len))
5515 return NL_SKIP;
5516
5517 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
5518 if (r == NULL)
5519 return NL_SKIP;
5520 if (bss[NL80211_BSS_BSSID])
5521 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
5522 ETH_ALEN);
5523 if (bss[NL80211_BSS_FREQUENCY])
5524 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
5525 if (bss[NL80211_BSS_BEACON_INTERVAL])
5526 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
5527 if (bss[NL80211_BSS_CAPABILITY])
5528 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
5529 r->flags |= WPA_SCAN_NOISE_INVALID;
5530 if (bss[NL80211_BSS_SIGNAL_MBM]) {
5531 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
5532 r->level /= 100; /* mBm to dBm */
5533 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
5534 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
5535 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005536 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 } else
5538 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
5539 if (bss[NL80211_BSS_TSF])
5540 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
5541 if (bss[NL80211_BSS_SEEN_MS_AGO])
5542 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
5543 r->ie_len = ie_len;
5544 pos = (u8 *) (r + 1);
5545 if (ie) {
5546 os_memcpy(pos, ie, ie_len);
5547 pos += ie_len;
5548 }
5549 r->beacon_ie_len = beacon_ie_len;
5550 if (beacon_ie)
5551 os_memcpy(pos, beacon_ie, beacon_ie_len);
5552
5553 if (bss[NL80211_BSS_STATUS]) {
5554 enum nl80211_bss_status status;
5555 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
5556 switch (status) {
5557 case NL80211_BSS_STATUS_AUTHENTICATED:
5558 r->flags |= WPA_SCAN_AUTHENTICATED;
5559 break;
5560 case NL80211_BSS_STATUS_ASSOCIATED:
5561 r->flags |= WPA_SCAN_ASSOCIATED;
5562 break;
5563 default:
5564 break;
5565 }
5566 }
5567
Jouni Malinen87fd2792011-05-16 18:35:42 +03005568 /*
5569 * cfg80211 maintains separate BSS table entries for APs if the same
5570 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
5571 * not use frequency as a separate key in the BSS table, so filter out
5572 * duplicated entries. Prefer associated BSS entry in such a case in
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005573 * order to get the correct frequency into the BSS table. Similarly,
5574 * prefer newer entries over older.
Jouni Malinen87fd2792011-05-16 18:35:42 +03005575 */
5576 for (i = 0; i < res->num; i++) {
5577 const u8 *s1, *s2;
5578 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
5579 continue;
5580
5581 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
5582 res->res[i]->ie_len, WLAN_EID_SSID);
5583 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
5584 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
5585 os_memcmp(s1, s2, 2 + s1[1]) != 0)
5586 continue;
5587
5588 /* Same BSSID,SSID was already included in scan results */
5589 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
5590 "for " MACSTR, MAC2STR(r->bssid));
5591
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005592 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
5593 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
5594 r->age < res->res[i]->age) {
Jouni Malinen87fd2792011-05-16 18:35:42 +03005595 os_free(res->res[i]);
5596 res->res[i] = r;
5597 } else
5598 os_free(r);
5599 return NL_SKIP;
5600 }
5601
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005602 tmp = os_realloc_array(res->res, res->num + 1,
5603 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604 if (tmp == NULL) {
5605 os_free(r);
5606 return NL_SKIP;
5607 }
5608 tmp[res->num++] = r;
5609 res->res = tmp;
5610
5611 return NL_SKIP;
5612}
5613
5614
5615static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
5616 const u8 *addr)
5617{
5618 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
5619 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
5620 "mismatch (" MACSTR ")", MAC2STR(addr));
5621 wpa_driver_nl80211_mlme(drv, addr,
5622 NL80211_CMD_DEAUTHENTICATE,
5623 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
5624 }
5625}
5626
5627
5628static void wpa_driver_nl80211_check_bss_status(
5629 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
5630{
5631 size_t i;
5632
5633 for (i = 0; i < res->num; i++) {
5634 struct wpa_scan_res *r = res->res[i];
5635 if (r->flags & WPA_SCAN_AUTHENTICATED) {
5636 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5637 "indicates BSS status with " MACSTR
5638 " as authenticated",
5639 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005640 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
5642 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
5643 0) {
5644 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
5645 " in local state (auth=" MACSTR
5646 " assoc=" MACSTR ")",
5647 MAC2STR(drv->auth_bssid),
5648 MAC2STR(drv->bssid));
5649 clear_state_mismatch(drv, r->bssid);
5650 }
5651 }
5652
5653 if (r->flags & WPA_SCAN_ASSOCIATED) {
5654 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
5655 "indicate BSS status with " MACSTR
5656 " as associated",
5657 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005658 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 !drv->associated) {
5660 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5661 "(not associated) does not match "
5662 "with BSS state");
5663 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005664 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
5666 0) {
5667 wpa_printf(MSG_DEBUG, "nl80211: Local state "
5668 "(associated with " MACSTR ") does "
5669 "not match with BSS state",
5670 MAC2STR(drv->bssid));
5671 clear_state_mismatch(drv, r->bssid);
5672 clear_state_mismatch(drv, drv->bssid);
5673 }
5674 }
5675 }
5676}
5677
5678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679static struct wpa_scan_results *
5680nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
5681{
5682 struct nl_msg *msg;
5683 struct wpa_scan_results *res;
5684 int ret;
5685 struct nl80211_bss_info_arg arg;
5686
5687 res = os_zalloc(sizeof(*res));
5688 if (res == NULL)
5689 return NULL;
5690 msg = nlmsg_alloc();
5691 if (!msg)
5692 goto nla_put_failure;
5693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005694 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005695 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005696 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697
5698 arg.drv = drv;
5699 arg.res = res;
5700 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5701 msg = NULL;
5702 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005703 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
5704 "BSSes)", (unsigned long) res->num);
5705 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 return res;
5707 }
5708 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5709 "(%s)", ret, strerror(-ret));
5710nla_put_failure:
5711 nlmsg_free(msg);
5712 wpa_scan_results_free(res);
5713 return NULL;
5714}
5715
5716
5717/**
5718 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
5719 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
5720 * Returns: Scan results on success, -1 on failure
5721 */
5722static struct wpa_scan_results *
5723wpa_driver_nl80211_get_scan_results(void *priv)
5724{
5725 struct i802_bss *bss = priv;
5726 struct wpa_driver_nl80211_data *drv = bss->drv;
5727 struct wpa_scan_results *res;
5728
5729 res = nl80211_get_scan_results(drv);
5730 if (res)
5731 wpa_driver_nl80211_check_bss_status(drv, res);
5732 return res;
5733}
5734
5735
5736static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
5737{
5738 struct wpa_scan_results *res;
5739 size_t i;
5740
5741 res = nl80211_get_scan_results(drv);
5742 if (res == NULL) {
5743 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
5744 return;
5745 }
5746
5747 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
5748 for (i = 0; i < res->num; i++) {
5749 struct wpa_scan_res *r = res->res[i];
5750 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
5751 (int) i, (int) res->num, MAC2STR(r->bssid),
5752 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
5753 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
5754 }
5755
5756 wpa_scan_results_free(res);
5757}
5758
5759
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005760static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
5761{
5762 switch (alg) {
5763 case WPA_ALG_WEP:
5764 if (key_len == 5)
5765 return WLAN_CIPHER_SUITE_WEP40;
5766 return WLAN_CIPHER_SUITE_WEP104;
5767 case WPA_ALG_TKIP:
5768 return WLAN_CIPHER_SUITE_TKIP;
5769 case WPA_ALG_CCMP:
5770 return WLAN_CIPHER_SUITE_CCMP;
5771 case WPA_ALG_GCMP:
5772 return WLAN_CIPHER_SUITE_GCMP;
5773 case WPA_ALG_CCMP_256:
5774 return WLAN_CIPHER_SUITE_CCMP_256;
5775 case WPA_ALG_GCMP_256:
5776 return WLAN_CIPHER_SUITE_GCMP_256;
5777 case WPA_ALG_IGTK:
5778 return WLAN_CIPHER_SUITE_AES_CMAC;
5779 case WPA_ALG_BIP_GMAC_128:
5780 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
5781 case WPA_ALG_BIP_GMAC_256:
5782 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
5783 case WPA_ALG_BIP_CMAC_256:
5784 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
5785 case WPA_ALG_SMS4:
5786 return WLAN_CIPHER_SUITE_SMS4;
5787 case WPA_ALG_KRK:
5788 return WLAN_CIPHER_SUITE_KRK;
5789 case WPA_ALG_NONE:
5790 case WPA_ALG_PMK:
5791 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
5792 alg);
5793 return 0;
5794 }
5795
5796 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
5797 alg);
5798 return 0;
5799}
5800
5801
5802static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
5803{
5804 switch (cipher) {
5805 case WPA_CIPHER_CCMP_256:
5806 return WLAN_CIPHER_SUITE_CCMP_256;
5807 case WPA_CIPHER_GCMP_256:
5808 return WLAN_CIPHER_SUITE_GCMP_256;
5809 case WPA_CIPHER_CCMP:
5810 return WLAN_CIPHER_SUITE_CCMP;
5811 case WPA_CIPHER_GCMP:
5812 return WLAN_CIPHER_SUITE_GCMP;
5813 case WPA_CIPHER_TKIP:
5814 return WLAN_CIPHER_SUITE_TKIP;
5815 case WPA_CIPHER_WEP104:
5816 return WLAN_CIPHER_SUITE_WEP104;
5817 case WPA_CIPHER_WEP40:
5818 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005819 case WPA_CIPHER_GTK_NOT_USED:
5820 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005821 }
5822
5823 return 0;
5824}
5825
5826
5827static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
5828 int max_suites)
5829{
5830 int num_suites = 0;
5831
5832 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
5833 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
5834 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
5835 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
5836 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
5837 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5838 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
5839 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
5840 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
5841 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5842 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
5843 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5844 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
5845 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5846
5847 return num_suites;
5848}
5849
5850
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005851static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852 enum wpa_alg alg, const u8 *addr,
5853 int key_idx, int set_tx,
5854 const u8 *seq, size_t seq_len,
5855 const u8 *key, size_t key_len)
5856{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005858 int ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005859 struct nl_msg *msg;
5860 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005861 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005863 /* Ignore for P2P Device */
5864 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5865 return 0;
5866
5867 ifindex = if_nametoindex(ifname);
5868 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005870 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005872#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005873 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005874 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005875 tdls = 1;
5876 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005877#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878
5879 msg = nlmsg_alloc();
5880 if (!msg)
5881 return -ENOMEM;
5882
5883 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005884 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005885 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005886 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005887 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005888 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005889 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
5890 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005891 }
5892
Dmitry Shmidt98660862014-03-11 17:26:21 -07005893 if (seq && seq_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
Dmitry Shmidt98660862014-03-11 17:26:21 -07005895 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
5896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897
5898 if (addr && !is_broadcast_ether_addr(addr)) {
5899 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
5900 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5901
5902 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
5903 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
5904 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
5905 NL80211_KEYTYPE_GROUP);
5906 }
5907 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005908 struct nlattr *types;
5909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005911
5912 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913 if (!types)
5914 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005915 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5916 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917 }
5918 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5919 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5920
5921 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5922 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
5923 ret = 0;
5924 if (ret)
5925 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
5926 ret, strerror(-ret));
5927
5928 /*
5929 * If we failed or don't need to set the default TX key (below),
5930 * we're done here.
5931 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07005932 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005934 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 !is_broadcast_ether_addr(addr))
5936 return ret;
5937
5938 msg = nlmsg_alloc();
5939 if (!msg)
5940 return -ENOMEM;
5941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005942 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
5944 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5945 if (alg == WPA_ALG_IGTK)
5946 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
5947 else
5948 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
5949 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005950 struct nlattr *types;
5951
5952 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953 if (!types)
5954 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005955 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
5956 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005958 struct nlattr *types;
5959
5960 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961 if (!types)
5962 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07005963 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
5964 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965 }
5966
5967 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5968 if (ret == -ENOENT)
5969 ret = 0;
5970 if (ret)
5971 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
5972 "err=%d %s)", ret, strerror(-ret));
5973 return ret;
5974
5975nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005976 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005977 return -ENOBUFS;
5978}
5979
5980
5981static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
5982 int key_idx, int defkey,
5983 const u8 *seq, size_t seq_len,
5984 const u8 *key, size_t key_len)
5985{
5986 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
5987 if (!key_attr)
5988 return -1;
5989
5990 if (defkey && alg == WPA_ALG_IGTK)
5991 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
5992 else if (defkey)
5993 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5994
5995 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
5996
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005997 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5998 wpa_alg_to_cipher_suite(alg, key_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005999
6000 if (seq && seq_len)
6001 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
6002
6003 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
6004
6005 nla_nest_end(msg, key_attr);
6006
6007 return 0;
6008 nla_put_failure:
6009 return -1;
6010}
6011
6012
6013static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
6014 struct nl_msg *msg)
6015{
6016 int i, privacy = 0;
6017 struct nlattr *nl_keys, *nl_key;
6018
6019 for (i = 0; i < 4; i++) {
6020 if (!params->wep_key[i])
6021 continue;
6022 privacy = 1;
6023 break;
6024 }
6025 if (params->wps == WPS_MODE_PRIVACY)
6026 privacy = 1;
6027 if (params->pairwise_suite &&
6028 params->pairwise_suite != WPA_CIPHER_NONE)
6029 privacy = 1;
6030
6031 if (!privacy)
6032 return 0;
6033
6034 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
6035
6036 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
6037 if (!nl_keys)
6038 goto nla_put_failure;
6039
6040 for (i = 0; i < 4; i++) {
6041 if (!params->wep_key[i])
6042 continue;
6043
6044 nl_key = nla_nest_start(msg, i);
6045 if (!nl_key)
6046 goto nla_put_failure;
6047
6048 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
6049 params->wep_key[i]);
6050 if (params->wep_key_len[i] == 5)
6051 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6052 WLAN_CIPHER_SUITE_WEP40);
6053 else
6054 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
6055 WLAN_CIPHER_SUITE_WEP104);
6056
6057 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
6058
6059 if (i == params->wep_tx_keyidx)
6060 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
6061
6062 nla_nest_end(msg, nl_key);
6063 }
6064 nla_nest_end(msg, nl_keys);
6065
6066 return 0;
6067
6068nla_put_failure:
6069 return -ENOBUFS;
6070}
6071
6072
6073static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
6074 const u8 *addr, int cmd, u16 reason_code,
6075 int local_state_change)
6076{
6077 int ret = -1;
6078 struct nl_msg *msg;
6079
6080 msg = nlmsg_alloc();
6081 if (!msg)
6082 return -1;
6083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006084 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006085
6086 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6087 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006088 if (addr)
6089 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090 if (local_state_change)
6091 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6092
6093 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6094 msg = NULL;
6095 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096 wpa_dbg(drv->ctx, MSG_DEBUG,
6097 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
6098 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099 goto nla_put_failure;
6100 }
6101 ret = 0;
6102
6103nla_put_failure:
6104 nlmsg_free(msg);
6105 return ret;
6106}
6107
6108
6109static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006110 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006112 int ret;
6113
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006114 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006115 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006116 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006117 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
6118 reason_code, 0);
6119 /*
6120 * For locally generated disconnect, supplicant already generates a
6121 * DEAUTH event, so ignore the event from NL80211.
6122 */
6123 drv->ignore_next_local_disconnect = ret == 0;
6124
6125 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126}
6127
6128
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006129static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
6130 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006131{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006133 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07006134
6135 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
6136 nl80211_mark_disconnected(drv);
6137 return nl80211_leave_ibss(drv);
6138 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006140 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006141 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
6142 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006143 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07006144 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
6145 reason_code, 0);
6146 /*
6147 * For locally generated deauthenticate, supplicant already generates a
6148 * DEAUTH event, so ignore the event from NL80211.
6149 */
6150 drv->ignore_next_local_deauth = ret == 0;
6151 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152}
6153
6154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006155static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
6156 struct wpa_driver_auth_params *params)
6157{
6158 int i;
6159
6160 drv->auth_freq = params->freq;
6161 drv->auth_alg = params->auth_alg;
6162 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
6163 drv->auth_local_state_change = params->local_state_change;
6164 drv->auth_p2p = params->p2p;
6165
6166 if (params->bssid)
6167 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
6168 else
6169 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
6170
6171 if (params->ssid) {
6172 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
6173 drv->auth_ssid_len = params->ssid_len;
6174 } else
6175 drv->auth_ssid_len = 0;
6176
6177
6178 os_free(drv->auth_ie);
6179 drv->auth_ie = NULL;
6180 drv->auth_ie_len = 0;
6181 if (params->ie) {
6182 drv->auth_ie = os_malloc(params->ie_len);
6183 if (drv->auth_ie) {
6184 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
6185 drv->auth_ie_len = params->ie_len;
6186 }
6187 }
6188
6189 for (i = 0; i < 4; i++) {
6190 if (params->wep_key[i] && params->wep_key_len[i] &&
6191 params->wep_key_len[i] <= 16) {
6192 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
6193 params->wep_key_len[i]);
6194 drv->auth_wep_key_len[i] = params->wep_key_len[i];
6195 } else
6196 drv->auth_wep_key_len[i] = 0;
6197 }
6198}
6199
6200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006202 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204 struct wpa_driver_nl80211_data *drv = bss->drv;
6205 int ret = -1, i;
6206 struct nl_msg *msg;
6207 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006208 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006210 int is_retry;
6211
6212 is_retry = drv->retry_auth;
6213 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07006214 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006216 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006218 if (params->bssid)
6219 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
6220 else
6221 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006223 nlmode = params->p2p ?
6224 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6225 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006226 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227 return -1;
6228
6229retry:
6230 msg = nlmsg_alloc();
6231 if (!msg)
6232 return -1;
6233
6234 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
6235 drv->ifindex);
6236
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006237 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238
6239 for (i = 0; i < 4; i++) {
6240 if (!params->wep_key[i])
6241 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006242 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243 NULL, i,
6244 i == params->wep_tx_keyidx, NULL, 0,
6245 params->wep_key[i],
6246 params->wep_key_len[i]);
6247 if (params->wep_tx_keyidx != i)
6248 continue;
6249 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
6250 params->wep_key[i], params->wep_key_len[i])) {
6251 nlmsg_free(msg);
6252 return -1;
6253 }
6254 }
6255
6256 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6257 if (params->bssid) {
6258 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6259 MAC2STR(params->bssid));
6260 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6261 }
6262 if (params->freq) {
6263 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6264 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6265 }
6266 if (params->ssid) {
6267 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6268 params->ssid, params->ssid_len);
6269 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6270 params->ssid);
6271 }
6272 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
6273 if (params->ie)
6274 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006275 if (params->sae_data) {
6276 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
6277 params->sae_data_len);
6278 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
6279 params->sae_data);
6280 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6282 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6283 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6284 type = NL80211_AUTHTYPE_SHARED_KEY;
6285 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6286 type = NL80211_AUTHTYPE_NETWORK_EAP;
6287 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6288 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006289 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
6290 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 else
6292 goto nla_put_failure;
6293 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6294 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6295 if (params->local_state_change) {
6296 wpa_printf(MSG_DEBUG, " * Local state change only");
6297 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
6298 }
6299
6300 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6301 msg = NULL;
6302 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006303 wpa_dbg(drv->ctx, MSG_DEBUG,
6304 "nl80211: MLME command failed (auth): ret=%d (%s)",
6305 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006306 count++;
6307 if (ret == -EALREADY && count == 1 && params->bssid &&
6308 !params->local_state_change) {
6309 /*
6310 * mac80211 does not currently accept new
6311 * authentication if we are already authenticated. As a
6312 * workaround, force deauthentication and try again.
6313 */
6314 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
6315 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07006316 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317 wpa_driver_nl80211_deauthenticate(
6318 bss, params->bssid,
6319 WLAN_REASON_PREV_AUTH_NOT_VALID);
6320 nlmsg_free(msg);
6321 goto retry;
6322 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006323
6324 if (ret == -ENOENT && params->freq && !is_retry) {
6325 /*
6326 * cfg80211 has likely expired the BSS entry even
6327 * though it was previously available in our internal
6328 * BSS table. To recover quickly, start a single
6329 * channel scan on the specified channel.
6330 */
6331 struct wpa_driver_scan_params scan;
6332 int freqs[2];
6333
6334 os_memset(&scan, 0, sizeof(scan));
6335 scan.num_ssids = 1;
6336 if (params->ssid) {
6337 scan.ssids[0].ssid = params->ssid;
6338 scan.ssids[0].ssid_len = params->ssid_len;
6339 }
6340 freqs[0] = params->freq;
6341 freqs[1] = 0;
6342 scan.freqs = freqs;
6343 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
6344 "channel scan to refresh cfg80211 BSS "
6345 "entry");
6346 ret = wpa_driver_nl80211_scan(bss, &scan);
6347 if (ret == 0) {
6348 nl80211_copy_auth_params(drv, params);
6349 drv->scan_for_auth = 1;
6350 }
6351 } else if (is_retry) {
6352 /*
6353 * Need to indicate this with an event since the return
6354 * value from the retry is not delivered to core code.
6355 */
6356 union wpa_event_data event;
6357 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
6358 "failed");
6359 os_memset(&event, 0, sizeof(event));
6360 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
6361 ETH_ALEN);
6362 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
6363 &event);
6364 }
6365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006366 goto nla_put_failure;
6367 }
6368 ret = 0;
6369 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
6370 "successfully");
6371
6372nla_put_failure:
6373 nlmsg_free(msg);
6374 return ret;
6375}
6376
6377
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006378static int wpa_driver_nl80211_authenticate_retry(
6379 struct wpa_driver_nl80211_data *drv)
6380{
6381 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006382 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006383 int i;
6384
6385 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
6386
6387 os_memset(&params, 0, sizeof(params));
6388 params.freq = drv->auth_freq;
6389 params.auth_alg = drv->auth_alg;
6390 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
6391 params.local_state_change = drv->auth_local_state_change;
6392 params.p2p = drv->auth_p2p;
6393
6394 if (!is_zero_ether_addr(drv->auth_bssid_))
6395 params.bssid = drv->auth_bssid_;
6396
6397 if (drv->auth_ssid_len) {
6398 params.ssid = drv->auth_ssid;
6399 params.ssid_len = drv->auth_ssid_len;
6400 }
6401
6402 params.ie = drv->auth_ie;
6403 params.ie_len = drv->auth_ie_len;
6404
6405 for (i = 0; i < 4; i++) {
6406 if (drv->auth_wep_key_len[i]) {
6407 params.wep_key[i] = drv->auth_wep_key[i];
6408 params.wep_key_len[i] = drv->auth_wep_key_len[i];
6409 }
6410 }
6411
6412 drv->retry_auth = 1;
6413 return wpa_driver_nl80211_authenticate(bss, &params);
6414}
6415
6416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417struct phy_info_arg {
6418 u16 *num_modes;
6419 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006420 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006421};
6422
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006423static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
6424 struct nlattr *ampdu_factor,
6425 struct nlattr *ampdu_density,
6426 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006427{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006428 if (capa)
6429 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006430
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006431 if (ampdu_factor)
6432 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006433
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006434 if (ampdu_density)
6435 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
6436
6437 if (mcs_set && nla_len(mcs_set) >= 16) {
6438 u8 *mcs;
6439 mcs = nla_data(mcs_set);
6440 os_memcpy(mode->mcs_set, mcs, 16);
6441 }
6442}
6443
6444
6445static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
6446 struct nlattr *capa,
6447 struct nlattr *mcs_set)
6448{
6449 if (capa)
6450 mode->vht_capab = nla_get_u32(capa);
6451
6452 if (mcs_set && nla_len(mcs_set) >= 8) {
6453 u8 *mcs;
6454 mcs = nla_data(mcs_set);
6455 os_memcpy(mode->vht_mcs_set, mcs, 8);
6456 }
6457}
6458
6459
6460static void phy_info_freq(struct hostapd_hw_modes *mode,
6461 struct hostapd_channel_data *chan,
6462 struct nlattr *tb_freq[])
6463{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006464 u8 channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006465 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
6466 chan->flag = 0;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006467 chan->dfs_cac_ms = 0;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006468 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
6469 chan->chan = channel;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006470
6471 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
6472 chan->flag |= HOSTAPD_CHAN_DISABLED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006473 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
6474 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN | HOSTAPD_CHAN_NO_IBSS;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006475 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
6476 chan->flag |= HOSTAPD_CHAN_RADAR;
6477
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006478 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
6479 enum nl80211_dfs_state state =
6480 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
6481
6482 switch (state) {
6483 case NL80211_DFS_USABLE:
6484 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
6485 break;
6486 case NL80211_DFS_AVAILABLE:
6487 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
6488 break;
6489 case NL80211_DFS_UNAVAILABLE:
6490 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
6491 break;
6492 }
6493 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006494
6495 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
6496 chan->dfs_cac_ms = nla_get_u32(
6497 tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
6498 }
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006499}
6500
6501
6502static int phy_info_freqs(struct phy_info_arg *phy_info,
6503 struct hostapd_hw_modes *mode, struct nlattr *tb)
6504{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6506 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
6507 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006508 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
6510 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006511 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006512 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006513 int new_channels = 0;
6514 struct hostapd_channel_data *channel;
6515 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006517 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006518
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006519 if (tb == NULL)
6520 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006522 nla_for_each_nested(nl_freq, tb, rem_freq) {
6523 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6524 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6525 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6526 continue;
6527 new_channels++;
6528 }
6529
6530 channel = os_realloc_array(mode->channels,
6531 mode->num_channels + new_channels,
6532 sizeof(struct hostapd_channel_data));
6533 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006534 return NL_SKIP;
6535
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006536 mode->channels = channel;
6537 mode->num_channels += new_channels;
6538
6539 idx = phy_info->last_chan_idx;
6540
6541 nla_for_each_nested(nl_freq, tb, rem_freq) {
6542 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
6543 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
6544 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
6545 continue;
6546 phy_info_freq(mode, &mode->channels[idx], tb_freq);
6547 idx++;
6548 }
6549 phy_info->last_chan_idx = idx;
6550
6551 return NL_OK;
6552}
6553
6554
6555static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
6556{
6557 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
6558 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
6559 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
6560 { .type = NLA_FLAG },
6561 };
6562 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
6563 struct nlattr *nl_rate;
6564 int rem_rate, idx;
6565
6566 if (tb == NULL)
6567 return NL_OK;
6568
6569 nla_for_each_nested(nl_rate, tb, rem_rate) {
6570 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6571 nla_data(nl_rate), nla_len(nl_rate),
6572 rate_policy);
6573 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6574 continue;
6575 mode->num_rates++;
6576 }
6577
6578 mode->rates = os_calloc(mode->num_rates, sizeof(int));
6579 if (!mode->rates)
6580 return NL_SKIP;
6581
6582 idx = 0;
6583
6584 nla_for_each_nested(nl_rate, tb, rem_rate) {
6585 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
6586 nla_data(nl_rate), nla_len(nl_rate),
6587 rate_policy);
6588 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
6589 continue;
6590 mode->rates[idx] = nla_get_u32(
6591 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006592 idx++;
6593 }
6594
6595 return NL_OK;
6596}
6597
6598
6599static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
6600{
6601 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
6602 struct hostapd_hw_modes *mode;
6603 int ret;
6604
6605 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006606 mode = os_realloc_array(phy_info->modes,
6607 *phy_info->num_modes + 1,
6608 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006609 if (!mode)
6610 return NL_SKIP;
6611 phy_info->modes = mode;
6612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006613 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006614 os_memset(mode, 0, sizeof(*mode));
6615 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006616 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
6617 HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
6618
6619 /*
6620 * Unsupported VHT MCS stream is defined as value 3, so the VHT
6621 * MCS RX/TX map must be initialized with 0xffff to mark all 8
6622 * possible streams as unsupported. This will be overridden if
6623 * driver advertises VHT support.
6624 */
6625 mode->vht_mcs_set[0] = 0xff;
6626 mode->vht_mcs_set[1] = 0xff;
6627 mode->vht_mcs_set[4] = 0xff;
6628 mode->vht_mcs_set[5] = 0xff;
6629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006630 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006631 phy_info->last_mode = nl_band->nla_type;
6632 phy_info->last_chan_idx = 0;
6633 } else
6634 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006636 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
6637 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006638
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006639 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
6640 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
6641 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
6642 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
6643 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
6644 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
6645 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
6646 if (ret != NL_OK)
6647 return ret;
6648 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
6649 if (ret != NL_OK)
6650 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006651
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006652 return NL_OK;
6653}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006655
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006656static int phy_info_handler(struct nl_msg *msg, void *arg)
6657{
6658 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6659 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6660 struct phy_info_arg *phy_info = arg;
6661 struct nlattr *nl_band;
6662 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006663
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006664 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6665 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006666
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006667 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
6668 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006669
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006670 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
6671 {
6672 int res = phy_info_band(phy_info, nl_band);
6673 if (res != NL_OK)
6674 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006675 }
6676
6677 return NL_SKIP;
6678}
6679
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006682wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
6683 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684{
6685 u16 m;
6686 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
6687 int i, mode11g_idx = -1;
6688
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006689 /* heuristic to set up modes */
6690 for (m = 0; m < *num_modes; m++) {
6691 if (!modes[m].num_channels)
6692 continue;
6693 if (modes[m].channels[0].freq < 4000) {
6694 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
6695 for (i = 0; i < modes[m].num_rates; i++) {
6696 if (modes[m].rates[i] > 200) {
6697 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
6698 break;
6699 }
6700 }
6701 } else if (modes[m].channels[0].freq > 50000)
6702 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
6703 else
6704 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
6705 }
6706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006707 /* If only 802.11g mode is included, use it to construct matching
6708 * 802.11b mode data. */
6709
6710 for (m = 0; m < *num_modes; m++) {
6711 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
6712 return modes; /* 802.11b already included */
6713 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
6714 mode11g_idx = m;
6715 }
6716
6717 if (mode11g_idx < 0)
6718 return modes; /* 2.4 GHz band not supported at all */
6719
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006720 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006721 if (nmodes == NULL)
6722 return modes; /* Could not add 802.11b mode */
6723
6724 mode = &nmodes[*num_modes];
6725 os_memset(mode, 0, sizeof(*mode));
6726 (*num_modes)++;
6727 modes = nmodes;
6728
6729 mode->mode = HOSTAPD_MODE_IEEE80211B;
6730
6731 mode11g = &modes[mode11g_idx];
6732 mode->num_channels = mode11g->num_channels;
6733 mode->channels = os_malloc(mode11g->num_channels *
6734 sizeof(struct hostapd_channel_data));
6735 if (mode->channels == NULL) {
6736 (*num_modes)--;
6737 return modes; /* Could not add 802.11b mode */
6738 }
6739 os_memcpy(mode->channels, mode11g->channels,
6740 mode11g->num_channels * sizeof(struct hostapd_channel_data));
6741
6742 mode->num_rates = 0;
6743 mode->rates = os_malloc(4 * sizeof(int));
6744 if (mode->rates == NULL) {
6745 os_free(mode->channels);
6746 (*num_modes)--;
6747 return modes; /* Could not add 802.11b mode */
6748 }
6749
6750 for (i = 0; i < mode11g->num_rates; i++) {
6751 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
6752 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
6753 continue;
6754 mode->rates[mode->num_rates] = mode11g->rates[i];
6755 mode->num_rates++;
6756 if (mode->num_rates == 4)
6757 break;
6758 }
6759
6760 if (mode->num_rates == 0) {
6761 os_free(mode->channels);
6762 os_free(mode->rates);
6763 (*num_modes)--;
6764 return modes; /* No 802.11b rates */
6765 }
6766
6767 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
6768 "information");
6769
6770 return modes;
6771}
6772
6773
6774static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
6775 int end)
6776{
6777 int c;
6778
6779 for (c = 0; c < mode->num_channels; c++) {
6780 struct hostapd_channel_data *chan = &mode->channels[c];
6781 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
6782 chan->flag |= HOSTAPD_CHAN_HT40;
6783 }
6784}
6785
6786
6787static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
6788 int end)
6789{
6790 int c;
6791
6792 for (c = 0; c < mode->num_channels; c++) {
6793 struct hostapd_channel_data *chan = &mode->channels[c];
6794 if (!(chan->flag & HOSTAPD_CHAN_HT40))
6795 continue;
6796 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
6797 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
6798 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
6799 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
6800 }
6801}
6802
6803
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006804static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006805 struct phy_info_arg *results)
6806{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006807 u16 m;
6808
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006809 for (m = 0; m < *results->num_modes; m++) {
6810 int c;
6811 struct hostapd_hw_modes *mode = &results->modes[m];
6812
6813 for (c = 0; c < mode->num_channels; c++) {
6814 struct hostapd_channel_data *chan = &mode->channels[c];
6815 if ((u32) chan->freq - 10 >= start &&
6816 (u32) chan->freq + 10 <= end)
6817 chan->max_tx_power = max_eirp;
6818 }
6819 }
6820}
6821
6822
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006823static void nl80211_reg_rule_ht40(u32 start, u32 end,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006824 struct phy_info_arg *results)
6825{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826 u16 m;
6827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006828 for (m = 0; m < *results->num_modes; m++) {
6829 if (!(results->modes[m].ht_capab &
6830 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6831 continue;
6832 nl80211_set_ht40_mode(&results->modes[m], start, end);
6833 }
6834}
6835
6836
6837static void nl80211_reg_rule_sec(struct nlattr *tb[],
6838 struct phy_info_arg *results)
6839{
6840 u32 start, end, max_bw;
6841 u16 m;
6842
6843 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6844 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6845 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6846 return;
6847
6848 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6849 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6850 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6851
6852 if (max_bw < 20)
6853 return;
6854
6855 for (m = 0; m < *results->num_modes; m++) {
6856 if (!(results->modes[m].ht_capab &
6857 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6858 continue;
6859 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
6860 }
6861}
6862
6863
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006864static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
6865 int end)
6866{
6867 int c;
6868
6869 for (c = 0; c < mode->num_channels; c++) {
6870 struct hostapd_channel_data *chan = &mode->channels[c];
6871 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
6872 chan->flag |= HOSTAPD_CHAN_VHT_10_70;
6873
6874 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
6875 chan->flag |= HOSTAPD_CHAN_VHT_30_50;
6876
6877 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
6878 chan->flag |= HOSTAPD_CHAN_VHT_50_30;
6879
6880 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
6881 chan->flag |= HOSTAPD_CHAN_VHT_70_10;
6882 }
6883}
6884
6885
6886static void nl80211_reg_rule_vht(struct nlattr *tb[],
6887 struct phy_info_arg *results)
6888{
6889 u32 start, end, max_bw;
6890 u16 m;
6891
6892 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6893 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
6894 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
6895 return;
6896
6897 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6898 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6899 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
6900
6901 if (max_bw < 80)
6902 return;
6903
6904 for (m = 0; m < *results->num_modes; m++) {
6905 if (!(results->modes[m].ht_capab &
6906 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
6907 continue;
6908 /* TODO: use a real VHT support indication */
6909 if (!results->modes[m].vht_capab)
6910 continue;
6911
6912 nl80211_set_vht_mode(&results->modes[m], start, end);
6913 }
6914}
6915
6916
Dmitry Shmidt97672262014-02-03 13:02:54 -08006917static const char * dfs_domain_name(enum nl80211_dfs_regions region)
6918{
6919 switch (region) {
6920 case NL80211_DFS_UNSET:
6921 return "DFS-UNSET";
6922 case NL80211_DFS_FCC:
6923 return "DFS-FCC";
6924 case NL80211_DFS_ETSI:
6925 return "DFS-ETSI";
6926 case NL80211_DFS_JP:
6927 return "DFS-JP";
6928 default:
6929 return "DFS-invalid";
6930 }
6931}
6932
6933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934static int nl80211_get_reg(struct nl_msg *msg, void *arg)
6935{
6936 struct phy_info_arg *results = arg;
6937 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
6938 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6939 struct nlattr *nl_rule;
6940 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
6941 int rem_rule;
6942 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
6943 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6944 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6945 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6946 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6947 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6948 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6949 };
6950
6951 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6952 genlmsg_attrlen(gnlh, 0), NULL);
6953 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
6954 !tb_msg[NL80211_ATTR_REG_RULES]) {
6955 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
6956 "available");
6957 return NL_SKIP;
6958 }
6959
Dmitry Shmidt97672262014-02-03 13:02:54 -08006960 if (tb_msg[NL80211_ATTR_DFS_REGION]) {
6961 enum nl80211_dfs_regions dfs_domain;
6962 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
6963 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
6964 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
6965 dfs_domain_name(dfs_domain));
6966 } else {
6967 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
6968 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
6969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970
6971 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
6972 {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006973 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
6975 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006976 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
6977 tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
6978 continue;
6979 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
6980 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
6981 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6982 max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
6983 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6984 max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006985 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
6986 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006987
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006988 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
6989 start, end, max_bw, max_eirp,
6990 flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
6991 flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
6992 flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
6993 flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
6994 "",
6995 flags & NL80211_RRF_DFS ? " (DFS)" : "",
6996 flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
6997 flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
6998 flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006999 if (max_bw >= 40)
7000 nl80211_reg_rule_ht40(start, end, results);
7001 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7002 nl80211_reg_rule_max_eirp(start, end, max_eirp,
7003 results);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004 }
7005
7006 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7007 {
7008 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7009 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
7010 nl80211_reg_rule_sec(tb_rule, results);
7011 }
7012
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007013 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
7014 {
7015 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
7016 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
7017 nl80211_reg_rule_vht(tb_rule, results);
7018 }
7019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007020 return NL_SKIP;
7021}
7022
7023
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007024static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
7025 struct phy_info_arg *results)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026{
7027 struct nl_msg *msg;
7028
7029 msg = nlmsg_alloc();
7030 if (!msg)
7031 return -ENOMEM;
7032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007033 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007034 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
7035}
7036
7037
7038static struct hostapd_hw_modes *
7039wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
7040{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007041 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 struct i802_bss *bss = priv;
7043 struct wpa_driver_nl80211_data *drv = bss->drv;
7044 struct nl_msg *msg;
7045 struct phy_info_arg result = {
7046 .num_modes = num_modes,
7047 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007048 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049 };
7050
7051 *num_modes = 0;
7052 *flags = 0;
7053
7054 msg = nlmsg_alloc();
7055 if (!msg)
7056 return NULL;
7057
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007058 feat = get_nl80211_protocol_features(drv);
7059 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
7060 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
7061 else
7062 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007063
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007064 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007065 if (nl80211_set_iface_id(msg, bss) < 0)
7066 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067
7068 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007069 nl80211_set_regulatory_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007070 return wpa_driver_nl80211_postprocess_modes(result.modes,
7071 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007072 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007073 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007075 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076 return NULL;
7077}
7078
7079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007080static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
7081 const void *data, size_t len,
7082 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007083{
7084 __u8 rtap_hdr[] = {
7085 0x00, 0x00, /* radiotap version */
7086 0x0e, 0x00, /* radiotap length */
7087 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
7088 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
7089 0x00, /* padding */
7090 0x00, 0x00, /* RX and TX flags to indicate that */
7091 0x00, 0x00, /* this is the injected frame directly */
7092 };
7093 struct iovec iov[2] = {
7094 {
7095 .iov_base = &rtap_hdr,
7096 .iov_len = sizeof(rtap_hdr),
7097 },
7098 {
7099 .iov_base = (void *) data,
7100 .iov_len = len,
7101 }
7102 };
7103 struct msghdr msg = {
7104 .msg_name = NULL,
7105 .msg_namelen = 0,
7106 .msg_iov = iov,
7107 .msg_iovlen = 2,
7108 .msg_control = NULL,
7109 .msg_controllen = 0,
7110 .msg_flags = 0,
7111 };
7112 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007113 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007114
7115 if (encrypt)
7116 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
7117
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07007118 if (drv->monitor_sock < 0) {
7119 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
7120 "for %s", __func__);
7121 return -1;
7122 }
7123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007124 if (noack)
7125 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007126 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007128 res = sendmsg(drv->monitor_sock, &msg, 0);
7129 if (res < 0) {
7130 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
7131 return -1;
7132 }
7133 return 0;
7134}
7135
7136
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007137static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
7138 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007139 int encrypt, int noack,
7140 unsigned int freq, int no_cck,
7141 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007142{
7143 struct wpa_driver_nl80211_data *drv = bss->drv;
7144 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007145 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007146
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007147 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
7148 freq = nl80211_get_assoc_freq(drv);
7149 wpa_printf(MSG_DEBUG,
7150 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
7151 freq);
7152 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007153 if (freq == 0) {
7154 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
7155 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007156 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007157 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007158
Dmitry Shmidt56052862013-10-04 10:23:25 -07007159 if (drv->use_monitor) {
7160 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
7161 freq, bss->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007162 return wpa_driver_nl80211_send_mntr(drv, data, len,
7163 encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007164 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007165
Dmitry Shmidt56052862013-10-04 10:23:25 -07007166 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07007167 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
7168 &cookie, no_cck, noack, offchanok);
7169 if (res == 0 && !noack) {
7170 const struct ieee80211_mgmt *mgmt;
7171 u16 fc;
7172
7173 mgmt = (const struct ieee80211_mgmt *) data;
7174 fc = le_to_host16(mgmt->frame_control);
7175 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7176 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
7177 wpa_printf(MSG_MSGDUMP,
7178 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
7179 (long long unsigned int)
7180 drv->send_action_cookie,
7181 (long long unsigned int) cookie);
7182 drv->send_action_cookie = cookie;
7183 }
7184 }
7185
7186 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007187}
7188
7189
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007190static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
7191 size_t data_len, int noack,
7192 unsigned int freq, int no_cck,
7193 int offchanok,
7194 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007195{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196 struct wpa_driver_nl80211_data *drv = bss->drv;
7197 struct ieee80211_mgmt *mgmt;
7198 int encrypt = 1;
7199 u16 fc;
7200
7201 mgmt = (struct ieee80211_mgmt *) data;
7202 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007203 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
7204 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
7205 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
7206 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007207
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007208 if ((is_sta_interface(drv->nlmode) ||
7209 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7211 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
7212 /*
7213 * The use of last_mgmt_freq is a bit of a hack,
7214 * but it works due to the single-threaded nature
7215 * of wpa_supplicant.
7216 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007217 if (freq == 0) {
7218 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
7219 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007220 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007221 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007222 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007223 data, data_len, NULL, 1, noack,
7224 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007225 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007226
7227 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007228 if (freq == 0) {
7229 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
7230 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007231 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007232 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007233 return nl80211_send_frame_cmd(bss, freq,
7234 (int) freq == bss->freq ? 0 :
7235 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007236 data, data_len,
7237 &drv->send_action_cookie,
7238 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007239 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07007240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007241 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
7242 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
7243 /*
7244 * Only one of the authentication frame types is encrypted.
7245 * In order for static WEP encryption to work properly (i.e.,
7246 * to not encrypt the frame), we need to tell mac80211 about
7247 * the frames that must not be encrypted.
7248 */
7249 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
7250 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7251 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
7252 encrypt = 0;
7253 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007254
Dmitry Shmidt56052862013-10-04 10:23:25 -07007255 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007256 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007257 noack, freq, no_cck, offchanok,
7258 wait_time);
7259}
7260
7261
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007262static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
7263 int slot, int ht_opmode, int ap_isolate,
7264 int *basic_rates)
7265{
7266 struct wpa_driver_nl80211_data *drv = bss->drv;
7267 struct nl_msg *msg;
7268
7269 msg = nlmsg_alloc();
7270 if (!msg)
7271 return -ENOMEM;
7272
7273 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
7274
7275 if (cts >= 0)
7276 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
7277 if (preamble >= 0)
7278 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
7279 if (slot >= 0)
7280 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
7281 if (ht_opmode >= 0)
7282 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
7283 if (ap_isolate >= 0)
7284 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
7285
7286 if (basic_rates) {
7287 u8 rates[NL80211_MAX_SUPP_RATES];
7288 u8 rates_len = 0;
7289 int i;
7290
7291 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
7292 i++)
7293 rates[rates_len++] = basic_rates[i] / 5;
7294
7295 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
7296 }
7297
7298 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7299
7300 return send_and_recv_msgs(drv, msg, NULL, NULL);
7301 nla_put_failure:
7302 nlmsg_free(msg);
7303 return -ENOBUFS;
7304}
7305
7306
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007307static int wpa_driver_nl80211_set_acl(void *priv,
7308 struct hostapd_acl_params *params)
7309{
7310 struct i802_bss *bss = priv;
7311 struct wpa_driver_nl80211_data *drv = bss->drv;
7312 struct nl_msg *msg;
7313 struct nlattr *acl;
7314 unsigned int i;
7315 int ret = 0;
7316
7317 if (!(drv->capa.max_acl_mac_addrs))
7318 return -ENOTSUP;
7319
7320 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
7321 return -ENOTSUP;
7322
7323 msg = nlmsg_alloc();
7324 if (!msg)
7325 return -ENOMEM;
7326
7327 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
7328 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
7329
7330 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
7331
7332 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7333
7334 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
7335 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
7336 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
7337
7338 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
7339 if (acl == NULL)
7340 goto nla_put_failure;
7341
7342 for (i = 0; i < params->num_mac_acl; i++)
7343 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
7344
7345 nla_nest_end(msg, acl);
7346
7347 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7348 msg = NULL;
7349 if (ret) {
7350 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
7351 ret, strerror(-ret));
7352 }
7353
7354nla_put_failure:
7355 nlmsg_free(msg);
7356
7357 return ret;
7358}
7359
7360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007361static int wpa_driver_nl80211_set_ap(void *priv,
7362 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363{
7364 struct i802_bss *bss = priv;
7365 struct wpa_driver_nl80211_data *drv = bss->drv;
7366 struct nl_msg *msg;
7367 u8 cmd = NL80211_CMD_NEW_BEACON;
7368 int ret;
7369 int beacon_set;
7370 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007371 int num_suites;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007372 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007373 u32 ver;
7374
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007375 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007376
7377 msg = nlmsg_alloc();
7378 if (!msg)
7379 return -ENOMEM;
7380
7381 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
7382 beacon_set);
7383 if (beacon_set)
7384 cmd = NL80211_CMD_SET_BEACON;
7385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007386 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007387 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
7388 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007389 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007390 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
7391 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007392 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007393 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007395 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007396 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007397 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007398 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007399 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
7400 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007401 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7402 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007403 if (params->proberesp && params->proberesp_len) {
7404 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
7405 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007406 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
7407 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007408 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007409 switch (params->hide_ssid) {
7410 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007411 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007412 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7413 NL80211_HIDDEN_SSID_NOT_IN_USE);
7414 break;
7415 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007416 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007417 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7418 NL80211_HIDDEN_SSID_ZERO_LEN);
7419 break;
7420 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007421 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007422 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
7423 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
7424 break;
7425 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007426 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007427 if (params->privacy)
7428 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007429 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007430 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
7431 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
7432 /* Leave out the attribute */
7433 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
7434 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7435 NL80211_AUTHTYPE_SHARED_KEY);
7436 else
7437 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
7438 NL80211_AUTHTYPE_OPEN_SYSTEM);
7439
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007440 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007441 ver = 0;
7442 if (params->wpa_version & WPA_PROTO_WPA)
7443 ver |= NL80211_WPA_VERSION_1;
7444 if (params->wpa_version & WPA_PROTO_RSN)
7445 ver |= NL80211_WPA_VERSION_2;
7446 if (ver)
7447 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7448
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007449 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
7450 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007451 num_suites = 0;
7452 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
7453 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
7454 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
7455 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
7456 if (num_suites) {
7457 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
7458 num_suites * sizeof(u32), suites);
7459 }
7460
7461 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
7462 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
7463 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
7464
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007465 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
7466 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007467 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
7468 suites, ARRAY_SIZE(suites));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007469 if (num_suites) {
7470 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
7471 num_suites * sizeof(u32), suites);
7472 }
7473
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007474 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
7475 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007476 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
7477 if (suite)
7478 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007479
7480 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007481 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
7482 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007483 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
7484 wpabuf_head(params->beacon_ies));
7485 }
7486 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007487 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
7488 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007489 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
7490 wpabuf_len(params->proberesp_ies),
7491 wpabuf_head(params->proberesp_ies));
7492 }
7493 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007494 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
7495 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007496 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
7497 wpabuf_len(params->assocresp_ies),
7498 wpabuf_head(params->assocresp_ies));
7499 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007500
Dmitry Shmidt04949592012-07-19 12:16:46 -07007501 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07007502 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
7503 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007504 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
7505 params->ap_max_inactivity);
7506 }
7507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7509 if (ret) {
7510 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
7511 ret, strerror(-ret));
7512 } else {
7513 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007514 nl80211_set_bss(bss, params->cts_protect, params->preamble,
7515 params->short_slot_time, params->ht_opmode,
7516 params->isolate, params->basic_rates);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007517 if (beacon_set && params->freq &&
7518 params->freq->bandwidth != bss->bandwidth) {
7519 wpa_printf(MSG_DEBUG,
7520 "nl80211: Update BSS %s bandwidth: %d -> %d",
7521 bss->ifname, bss->bandwidth,
7522 params->freq->bandwidth);
7523 ret = nl80211_set_channel(bss, params->freq, 1);
7524 if (ret) {
7525 wpa_printf(MSG_DEBUG,
7526 "nl80211: Frequency set failed: %d (%s)",
7527 ret, strerror(-ret));
7528 } else {
7529 wpa_printf(MSG_DEBUG,
7530 "nl80211: Frequency set succeeded for ht2040 coex");
7531 bss->bandwidth = params->freq->bandwidth;
7532 }
7533 } else if (!beacon_set) {
7534 /*
7535 * cfg80211 updates the driver on frequence change in AP
7536 * mode only at the point when beaconing is started, so
7537 * set the initial value here.
7538 */
7539 bss->bandwidth = params->freq->bandwidth;
7540 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007541 }
7542 return ret;
7543 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007544 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007545 return -ENOBUFS;
7546}
7547
7548
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007549static int nl80211_put_freq_params(struct nl_msg *msg,
7550 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007552 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
7553 if (freq->vht_enabled) {
7554 switch (freq->bandwidth) {
7555 case 20:
7556 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7557 NL80211_CHAN_WIDTH_20);
7558 break;
7559 case 40:
7560 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7561 NL80211_CHAN_WIDTH_40);
7562 break;
7563 case 80:
7564 if (freq->center_freq2)
7565 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7566 NL80211_CHAN_WIDTH_80P80);
7567 else
7568 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7569 NL80211_CHAN_WIDTH_80);
7570 break;
7571 case 160:
7572 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
7573 NL80211_CHAN_WIDTH_160);
7574 break;
7575 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007576 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007577 }
7578 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
7579 if (freq->center_freq2)
7580 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
7581 freq->center_freq2);
7582 } else if (freq->ht_enabled) {
7583 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007584 case -1:
7585 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7586 NL80211_CHAN_HT40MINUS);
7587 break;
7588 case 1:
7589 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7590 NL80211_CHAN_HT40PLUS);
7591 break;
7592 default:
7593 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
7594 NL80211_CHAN_HT20);
7595 break;
7596 }
7597 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007598 return 0;
7599
7600nla_put_failure:
7601 return -ENOBUFS;
7602}
7603
7604
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007605static int nl80211_set_channel(struct i802_bss *bss,
7606 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007607{
7608 struct wpa_driver_nl80211_data *drv = bss->drv;
7609 struct nl_msg *msg;
7610 int ret;
7611
7612 wpa_printf(MSG_DEBUG,
7613 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
7614 freq->freq, freq->ht_enabled, freq->vht_enabled,
7615 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7616 msg = nlmsg_alloc();
7617 if (!msg)
7618 return -1;
7619
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007620 nl80211_cmd(drv, msg, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
7621 NL80211_CMD_SET_WIPHY);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007622
7623 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7624 if (nl80211_put_freq_params(msg, freq) < 0)
7625 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007626
7627 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007628 msg = NULL;
7629 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007630 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007634 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007636 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637 return -1;
7638}
7639
7640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007641static u32 sta_flags_nl80211(int flags)
7642{
7643 u32 f = 0;
7644
7645 if (flags & WPA_STA_AUTHORIZED)
7646 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
7647 if (flags & WPA_STA_WMM)
7648 f |= BIT(NL80211_STA_FLAG_WME);
7649 if (flags & WPA_STA_SHORT_PREAMBLE)
7650 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
7651 if (flags & WPA_STA_MFP)
7652 f |= BIT(NL80211_STA_FLAG_MFP);
7653 if (flags & WPA_STA_TDLS_PEER)
7654 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7655
7656 return f;
7657}
7658
7659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660static int wpa_driver_nl80211_sta_add(void *priv,
7661 struct hostapd_sta_add_params *params)
7662{
7663 struct i802_bss *bss = priv;
7664 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007665 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007666 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667 int ret = -ENOBUFS;
7668
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007669 if ((params->flags & WPA_STA_TDLS_PEER) &&
7670 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7671 return -EOPNOTSUPP;
7672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673 msg = nlmsg_alloc();
7674 if (!msg)
7675 return -ENOMEM;
7676
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007677 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
7678 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007679 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
7680 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681
7682 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7683 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007684 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
7685 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007686 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
7687 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007688 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07007689 if (params->aid) {
7690 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
7691 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
7692 } else {
7693 /*
7694 * cfg80211 validates that AID is non-zero, so we have
7695 * to make this a non-zero value for the TDLS case where
7696 * a dummy STA entry is used for now.
7697 */
7698 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
7699 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
7700 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007701 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
7702 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007703 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
7704 params->listen_interval);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007705 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
7706 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
7707 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007708 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007709 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007710 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
7711 (u8 *) params->ht_capabilities,
7712 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007713 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
7714 sizeof(*params->ht_capabilities),
7715 params->ht_capabilities);
7716 }
7717
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007718 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007719 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
7720 (u8 *) params->vht_capabilities,
7721 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007722 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
7723 sizeof(*params->vht_capabilities),
7724 params->vht_capabilities);
7725 }
7726
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08007727 if (params->vht_opmode_enabled) {
7728 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
7729 NLA_PUT_U8(msg, NL80211_ATTR_OPMODE_NOTIF,
7730 params->vht_opmode);
7731 }
7732
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007733 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
7734 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
7735
7736 if (params->ext_capab) {
7737 wpa_hexdump(MSG_DEBUG, " * ext_capab",
7738 params->ext_capab, params->ext_capab_len);
7739 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
7740 params->ext_capab_len, params->ext_capab);
7741 }
7742
Dmitry Shmidt344abd32014-01-14 13:17:00 -08007743 if (params->supp_channels) {
7744 wpa_hexdump(MSG_DEBUG, " * supported channels",
7745 params->supp_channels, params->supp_channels_len);
7746 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
7747 params->supp_channels_len, params->supp_channels);
7748 }
7749
7750 if (params->supp_oper_classes) {
7751 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
7752 params->supp_oper_classes,
7753 params->supp_oper_classes_len);
7754 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
7755 params->supp_oper_classes_len,
7756 params->supp_oper_classes);
7757 }
7758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007759 os_memset(&upd, 0, sizeof(upd));
7760 upd.mask = sta_flags_nl80211(params->flags);
7761 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007762 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
7763 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007764 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7765
7766 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007767 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
7768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007769 if (!wme)
7770 goto nla_put_failure;
7771
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007772 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007773 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007774 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007775 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007776 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007777 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007778 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007779 }
7780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007781 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007782 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007783 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007784 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
7785 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
7786 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787 if (ret == -EEXIST)
7788 ret = 0;
7789 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007790 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007791 return ret;
7792}
7793
7794
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007795static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
7796{
7797#ifdef CONFIG_LIBNL3_ROUTE
7798 struct wpa_driver_nl80211_data *drv = bss->drv;
7799 struct rtnl_neigh *rn;
7800 struct nl_addr *nl_addr;
7801 int err;
7802
7803 rn = rtnl_neigh_alloc();
7804 if (!rn)
7805 return;
7806
7807 rtnl_neigh_set_family(rn, AF_BRIDGE);
7808 rtnl_neigh_set_ifindex(rn, bss->ifindex);
7809 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
7810 if (!nl_addr) {
7811 rtnl_neigh_put(rn);
7812 return;
7813 }
7814 rtnl_neigh_set_lladdr(rn, nl_addr);
7815
7816 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
7817 if (err < 0) {
7818 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
7819 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
7820 bss->ifindex, nl_geterror(err));
7821 } else {
7822 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
7823 MACSTR, MAC2STR(addr));
7824 }
7825
7826 nl_addr_put(nl_addr);
7827 rtnl_neigh_put(rn);
7828#endif /* CONFIG_LIBNL3_ROUTE */
7829}
7830
7831
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007832static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007833{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007834 struct wpa_driver_nl80211_data *drv = bss->drv;
7835 struct nl_msg *msg;
7836 int ret;
7837
7838 msg = nlmsg_alloc();
7839 if (!msg)
7840 return -ENOMEM;
7841
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007842 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007843
7844 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7845 if_nametoindex(bss->ifname));
7846 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7847
7848 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007849 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
7850 " --> %d (%s)",
7851 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007852
7853 if (drv->rtnl_sk)
7854 rtnl_neigh_delete_fdb_entry(bss, addr);
7855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007856 if (ret == -ENOENT)
7857 return 0;
7858 return ret;
7859 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007860 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007861 return -ENOBUFS;
7862}
7863
7864
7865static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
7866 int ifidx)
7867{
7868 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007869 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870
7871 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
7872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007873 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007874 dl_list_for_each(drv2, &drv->global->interfaces,
7875 struct wpa_driver_nl80211_data, list)
7876 del_ifidx(drv2, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007877
7878 msg = nlmsg_alloc();
7879 if (!msg)
7880 goto nla_put_failure;
7881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007882 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007883 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
7884
7885 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7886 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007887 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007888 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007889 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007890 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
7891}
7892
7893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007894static const char * nl80211_iftype_str(enum nl80211_iftype mode)
7895{
7896 switch (mode) {
7897 case NL80211_IFTYPE_ADHOC:
7898 return "ADHOC";
7899 case NL80211_IFTYPE_STATION:
7900 return "STATION";
7901 case NL80211_IFTYPE_AP:
7902 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007903 case NL80211_IFTYPE_AP_VLAN:
7904 return "AP_VLAN";
7905 case NL80211_IFTYPE_WDS:
7906 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007907 case NL80211_IFTYPE_MONITOR:
7908 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07007909 case NL80211_IFTYPE_MESH_POINT:
7910 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007911 case NL80211_IFTYPE_P2P_CLIENT:
7912 return "P2P_CLIENT";
7913 case NL80211_IFTYPE_P2P_GO:
7914 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007915 case NL80211_IFTYPE_P2P_DEVICE:
7916 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007917 default:
7918 return "unknown";
7919 }
7920}
7921
7922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007923static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
7924 const char *ifname,
7925 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007926 const u8 *addr, int wds,
7927 int (*handler)(struct nl_msg *, void *),
7928 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007929{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007930 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007931 int ifidx;
7932 int ret = -ENOBUFS;
7933
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007934 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
7935 iftype, nl80211_iftype_str(iftype));
7936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007937 msg = nlmsg_alloc();
7938 if (!msg)
7939 return -1;
7940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007941 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08007942 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007943 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007944 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
7945 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
7946
7947 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007948 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007949
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007950 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007951 if (!flags)
7952 goto nla_put_failure;
7953
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007954 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007955
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007956 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007957 } else if (wds) {
7958 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
7959 }
7960
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007961 /*
7962 * Tell cfg80211 that the interface belongs to the socket that created
7963 * it, and the interface should be deleted when the socket is closed.
7964 */
7965 NLA_PUT_FLAG(msg, NL80211_ATTR_IFACE_SOCKET_OWNER);
7966
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007967 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007968 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007969 if (ret) {
7970 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007971 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007972 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
7973 ifname, ret, strerror(-ret));
7974 return ret;
7975 }
7976
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007977 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
7978 return 0;
7979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980 ifidx = if_nametoindex(ifname);
7981 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
7982 ifname, ifidx);
7983
7984 if (ifidx <= 0)
7985 return -1;
7986
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07007987 /*
7988 * Some virtual interfaces need to process EAPOL packets and events on
7989 * the parent interface. This is used mainly with hostapd.
7990 */
7991 if (drv->hostapd ||
7992 iftype == NL80211_IFTYPE_AP_VLAN ||
7993 iftype == NL80211_IFTYPE_WDS ||
7994 iftype == NL80211_IFTYPE_MONITOR) {
7995 /* start listening for EAPOL on this interface */
7996 add_ifidx(drv, ifidx);
7997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007998
7999 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008000 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008001 nl80211_remove_iface(drv, ifidx);
8002 return -1;
8003 }
8004
8005 return ifidx;
8006}
8007
8008
8009static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
8010 const char *ifname, enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008011 const u8 *addr, int wds,
8012 int (*handler)(struct nl_msg *, void *),
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008013 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014{
8015 int ret;
8016
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008017 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
8018 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008019
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008020 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008021 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008022 if (use_existing) {
8023 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
8024 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008025 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
8026 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8027 addr) < 0 &&
8028 (linux_set_iface_flags(drv->global->ioctl_sock,
8029 ifname, 0) < 0 ||
8030 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8031 addr) < 0 ||
8032 linux_set_iface_flags(drv->global->ioctl_sock,
8033 ifname, 1) < 0))
8034 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008035 return -ENFILE;
8036 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008037 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
8038
8039 /* Try to remove the interface that was already there. */
8040 nl80211_remove_iface(drv, if_nametoindex(ifname));
8041
8042 /* Try to create the interface again */
8043 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008044 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045 }
8046
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008047 if (ret >= 0 && is_p2p_net_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008048 nl80211_disable_11b_rates(drv, ret, 1);
8049
8050 return ret;
8051}
8052
8053
8054static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
8055{
8056 struct ieee80211_hdr *hdr;
8057 u16 fc;
8058 union wpa_event_data event;
8059
8060 hdr = (struct ieee80211_hdr *) buf;
8061 fc = le_to_host16(hdr->frame_control);
8062
8063 os_memset(&event, 0, sizeof(event));
8064 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
8065 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
8066 event.tx_status.dst = hdr->addr1;
8067 event.tx_status.data = buf;
8068 event.tx_status.data_len = len;
8069 event.tx_status.ack = ok;
8070 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
8071}
8072
8073
8074static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
8075 u8 *buf, size_t len)
8076{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008077 struct ieee80211_hdr *hdr = (void *)buf;
8078 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008079 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008080
8081 if (len < sizeof(*hdr))
8082 return;
8083
8084 fc = le_to_host16(hdr->frame_control);
8085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008086 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008087 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
8088 event.rx_from_unknown.addr = hdr->addr2;
8089 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
8090 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008091 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
8092}
8093
8094
8095static void handle_frame(struct wpa_driver_nl80211_data *drv,
8096 u8 *buf, size_t len, int datarate, int ssi_signal)
8097{
8098 struct ieee80211_hdr *hdr;
8099 u16 fc;
8100 union wpa_event_data event;
8101
8102 hdr = (struct ieee80211_hdr *) buf;
8103 fc = le_to_host16(hdr->frame_control);
8104
8105 switch (WLAN_FC_GET_TYPE(fc)) {
8106 case WLAN_FC_TYPE_MGMT:
8107 os_memset(&event, 0, sizeof(event));
8108 event.rx_mgmt.frame = buf;
8109 event.rx_mgmt.frame_len = len;
8110 event.rx_mgmt.datarate = datarate;
8111 event.rx_mgmt.ssi_signal = ssi_signal;
8112 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
8113 break;
8114 case WLAN_FC_TYPE_CTRL:
8115 /* can only get here with PS-Poll frames */
8116 wpa_printf(MSG_DEBUG, "CTRL");
8117 from_unknown_sta(drv, buf, len);
8118 break;
8119 case WLAN_FC_TYPE_DATA:
8120 from_unknown_sta(drv, buf, len);
8121 break;
8122 }
8123}
8124
8125
8126static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
8127{
8128 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8129 int len;
8130 unsigned char buf[3000];
8131 struct ieee80211_radiotap_iterator iter;
8132 int ret;
8133 int datarate = 0, ssi_signal = 0;
8134 int injected = 0, failed = 0, rxflags = 0;
8135
8136 len = recv(sock, buf, sizeof(buf), 0);
8137 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008138 wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
8139 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008140 return;
8141 }
8142
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008143 if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008144 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008145 return;
8146 }
8147
8148 while (1) {
8149 ret = ieee80211_radiotap_iterator_next(&iter);
8150 if (ret == -ENOENT)
8151 break;
8152 if (ret) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008153 wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
8154 ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008155 return;
8156 }
8157 switch (iter.this_arg_index) {
8158 case IEEE80211_RADIOTAP_FLAGS:
8159 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
8160 len -= 4;
8161 break;
8162 case IEEE80211_RADIOTAP_RX_FLAGS:
8163 rxflags = 1;
8164 break;
8165 case IEEE80211_RADIOTAP_TX_FLAGS:
8166 injected = 1;
8167 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
8168 IEEE80211_RADIOTAP_F_TX_FAIL;
8169 break;
8170 case IEEE80211_RADIOTAP_DATA_RETRIES:
8171 break;
8172 case IEEE80211_RADIOTAP_CHANNEL:
8173 /* TODO: convert from freq/flags to channel number */
8174 break;
8175 case IEEE80211_RADIOTAP_RATE:
8176 datarate = *iter.this_arg * 5;
8177 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008178 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
8179 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008180 break;
8181 }
8182 }
8183
8184 if (rxflags && injected)
8185 return;
8186
8187 if (!injected)
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008188 handle_frame(drv, buf + iter._max_length,
8189 len - iter._max_length, datarate, ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008190 else
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008191 handle_tx_callback(drv->ctx, buf + iter._max_length,
8192 len - iter._max_length, !failed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008193}
8194
8195
8196/*
8197 * we post-process the filter code later and rewrite
8198 * this to the offset to the last instruction
8199 */
8200#define PASS 0xFF
8201#define FAIL 0xFE
8202
8203static struct sock_filter msock_filter_insns[] = {
8204 /*
8205 * do a little-endian load of the radiotap length field
8206 */
8207 /* load lower byte into A */
8208 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
8209 /* put it into X (== index register) */
8210 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8211 /* load upper byte into A */
8212 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
8213 /* left-shift it by 8 */
8214 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
8215 /* or with X */
8216 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
8217 /* put result into X */
8218 BPF_STMT(BPF_MISC| BPF_TAX, 0),
8219
8220 /*
8221 * Allow management frames through, this also gives us those
8222 * management frames that we sent ourselves with status
8223 */
8224 /* load the lower byte of the IEEE 802.11 frame control field */
8225 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8226 /* mask off frame type and version */
8227 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
8228 /* accept frame if it's both 0, fall through otherwise */
8229 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
8230
8231 /*
8232 * TODO: add a bit to radiotap RX flags that indicates
8233 * that the sending station is not associated, then
8234 * add a filter here that filters on our DA and that flag
8235 * to allow us to deauth frames to that bad station.
8236 *
8237 * For now allow all To DS data frames through.
8238 */
8239 /* load the IEEE 802.11 frame control field */
8240 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
8241 /* mask off frame type, version and DS status */
8242 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
8243 /* accept frame if version 0, type 2 and To DS, fall through otherwise
8244 */
8245 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
8246
8247#if 0
8248 /*
8249 * drop non-data frames
8250 */
8251 /* load the lower byte of the frame control field */
8252 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8253 /* mask off QoS bit */
8254 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
8255 /* drop non-data frames */
8256 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
8257#endif
8258 /* load the upper byte of the frame control field */
8259 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
8260 /* mask off toDS/fromDS */
8261 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
8262 /* accept WDS frames */
8263 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
8264
8265 /*
8266 * add header length to index
8267 */
8268 /* load the lower byte of the frame control field */
8269 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
8270 /* mask off QoS bit */
8271 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
8272 /* right shift it by 6 to give 0 or 2 */
8273 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
8274 /* add data frame header length */
8275 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
8276 /* add index, was start of 802.11 header */
8277 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
8278 /* move to index, now start of LL header */
8279 BPF_STMT(BPF_MISC | BPF_TAX, 0),
8280
8281 /*
8282 * Accept empty data frames, we use those for
8283 * polling activity.
8284 */
8285 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
8286 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
8287
8288 /*
8289 * Accept EAPOL frames
8290 */
8291 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
8292 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
8293 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
8294 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
8295
8296 /* keep these last two statements or change the code below */
8297 /* return 0 == "DROP" */
8298 BPF_STMT(BPF_RET | BPF_K, 0),
8299 /* return ~0 == "keep all" */
8300 BPF_STMT(BPF_RET | BPF_K, ~0),
8301};
8302
8303static struct sock_fprog msock_filter = {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008304 .len = ARRAY_SIZE(msock_filter_insns),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008305 .filter = msock_filter_insns,
8306};
8307
8308
8309static int add_monitor_filter(int s)
8310{
8311 int idx;
8312
8313 /* rewrite all PASS/FAIL jump offsets */
8314 for (idx = 0; idx < msock_filter.len; idx++) {
8315 struct sock_filter *insn = &msock_filter_insns[idx];
8316
8317 if (BPF_CLASS(insn->code) == BPF_JMP) {
8318 if (insn->code == (BPF_JMP|BPF_JA)) {
8319 if (insn->k == PASS)
8320 insn->k = msock_filter.len - idx - 2;
8321 else if (insn->k == FAIL)
8322 insn->k = msock_filter.len - idx - 3;
8323 }
8324
8325 if (insn->jt == PASS)
8326 insn->jt = msock_filter.len - idx - 2;
8327 else if (insn->jt == FAIL)
8328 insn->jt = msock_filter.len - idx - 3;
8329
8330 if (insn->jf == PASS)
8331 insn->jf = msock_filter.len - idx - 2;
8332 else if (insn->jf == FAIL)
8333 insn->jf = msock_filter.len - idx - 3;
8334 }
8335 }
8336
8337 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
8338 &msock_filter, sizeof(msock_filter))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008339 wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
8340 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008341 return -1;
8342 }
8343
8344 return 0;
8345}
8346
8347
8348static void nl80211_remove_monitor_interface(
8349 struct wpa_driver_nl80211_data *drv)
8350{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008351 if (drv->monitor_refcount > 0)
8352 drv->monitor_refcount--;
8353 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
8354 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008355 if (drv->monitor_refcount > 0)
8356 return;
8357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008358 if (drv->monitor_ifidx >= 0) {
8359 nl80211_remove_iface(drv, drv->monitor_ifidx);
8360 drv->monitor_ifidx = -1;
8361 }
8362 if (drv->monitor_sock >= 0) {
8363 eloop_unregister_read_sock(drv->monitor_sock);
8364 close(drv->monitor_sock);
8365 drv->monitor_sock = -1;
8366 }
8367}
8368
8369
8370static int
8371nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
8372{
8373 char buf[IFNAMSIZ];
8374 struct sockaddr_ll ll;
8375 int optval;
8376 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008377
8378 if (drv->monitor_ifidx >= 0) {
8379 drv->monitor_refcount++;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008380 wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
8381 drv->monitor_refcount);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008382 return 0;
8383 }
8384
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008385 if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008386 /*
8387 * P2P interface name is of the format p2p-%s-%d. For monitor
8388 * interface name corresponding to P2P GO, replace "p2p-" with
8389 * "mon-" to retain the same interface name length and to
8390 * indicate that it is a monitor interface.
8391 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008392 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008393 } else {
8394 /* Non-P2P interface with AP functionality. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008395 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008396 }
8397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008398 buf[IFNAMSIZ - 1] = '\0';
8399
8400 drv->monitor_ifidx =
8401 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008402 0, NULL, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008403
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008404 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008405 /*
8406 * This is backward compatibility for a few versions of
8407 * the kernel only that didn't advertise the right
8408 * attributes for the only driver that then supported
8409 * AP mode w/o monitor -- ath6kl.
8410 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008411 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
8412 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008413 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07008414 }
8415
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008416 if (drv->monitor_ifidx < 0)
8417 return -1;
8418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008419 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008420 goto error;
8421
8422 memset(&ll, 0, sizeof(ll));
8423 ll.sll_family = AF_PACKET;
8424 ll.sll_ifindex = drv->monitor_ifidx;
8425 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
8426 if (drv->monitor_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008427 wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
8428 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008429 goto error;
8430 }
8431
8432 if (add_monitor_filter(drv->monitor_sock)) {
8433 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
8434 "interface; do filtering in user space");
8435 /* This works, but will cost in performance. */
8436 }
8437
8438 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008439 wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
8440 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008441 goto error;
8442 }
8443
8444 optlen = sizeof(optval);
8445 optval = 20;
8446 if (setsockopt
8447 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008448 wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
8449 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008450 goto error;
8451 }
8452
8453 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
8454 drv, NULL)) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008455 wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008456 goto error;
8457 }
8458
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008459 drv->monitor_refcount++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008460 return 0;
8461 error:
8462 nl80211_remove_monitor_interface(drv);
8463 return -1;
8464}
8465
8466
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008467static int nl80211_setup_ap(struct i802_bss *bss)
8468{
8469 struct wpa_driver_nl80211_data *drv = bss->drv;
8470
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008471 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
8472 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008473
8474 /*
8475 * Disable Probe Request reporting unless we need it in this way for
8476 * devices that include the AP SME, in the other case (unless using
8477 * monitor iface) we'll get it through the nl_mgmt socket instead.
8478 */
8479 if (!drv->device_ap_sme)
8480 wpa_driver_nl80211_probe_req_report(bss, 0);
8481
8482 if (!drv->device_ap_sme && !drv->use_monitor)
8483 if (nl80211_mgmt_subscribe_ap(bss))
8484 return -1;
8485
8486 if (drv->device_ap_sme && !drv->use_monitor)
8487 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
8488 return -1;
8489
8490 if (!drv->device_ap_sme && drv->use_monitor &&
8491 nl80211_create_monitor_interface(drv) &&
8492 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008493 return -1;
8494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008495 if (drv->device_ap_sme &&
8496 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
8497 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
8498 "Probe Request frame reporting in AP mode");
8499 /* Try to survive without this */
8500 }
8501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008502 return 0;
8503}
8504
8505
8506static void nl80211_teardown_ap(struct i802_bss *bss)
8507{
8508 struct wpa_driver_nl80211_data *drv = bss->drv;
8509
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008510 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
8511 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008512 if (drv->device_ap_sme) {
8513 wpa_driver_nl80211_probe_req_report(bss, 0);
8514 if (!drv->use_monitor)
8515 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
8516 } else if (drv->use_monitor)
8517 nl80211_remove_monitor_interface(drv);
8518 else
8519 nl80211_mgmt_unsubscribe(bss, "AP teardown");
8520
8521 bss->beacon_set = 0;
8522}
8523
8524
8525static int nl80211_send_eapol_data(struct i802_bss *bss,
8526 const u8 *addr, const u8 *data,
8527 size_t data_len)
8528{
8529 struct sockaddr_ll ll;
8530 int ret;
8531
8532 if (bss->drv->eapol_tx_sock < 0) {
8533 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
8534 return -1;
8535 }
8536
8537 os_memset(&ll, 0, sizeof(ll));
8538 ll.sll_family = AF_PACKET;
8539 ll.sll_ifindex = bss->ifindex;
8540 ll.sll_protocol = htons(ETH_P_PAE);
8541 ll.sll_halen = ETH_ALEN;
8542 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
8543 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
8544 (struct sockaddr *) &ll, sizeof(ll));
8545 if (ret < 0)
8546 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
8547 strerror(errno));
8548
8549 return ret;
8550}
8551
8552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008553static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
8554
8555static int wpa_driver_nl80211_hapd_send_eapol(
8556 void *priv, const u8 *addr, const u8 *data,
8557 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
8558{
8559 struct i802_bss *bss = priv;
8560 struct wpa_driver_nl80211_data *drv = bss->drv;
8561 struct ieee80211_hdr *hdr;
8562 size_t len;
8563 u8 *pos;
8564 int res;
8565 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08008566
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008567 if (drv->device_ap_sme || !drv->use_monitor)
8568 return nl80211_send_eapol_data(bss, addr, data, data_len);
8569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008570 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
8571 data_len;
8572 hdr = os_zalloc(len);
8573 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008574 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
8575 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008576 return -1;
8577 }
8578
8579 hdr->frame_control =
8580 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
8581 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
8582 if (encrypt)
8583 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
8584 if (qos) {
8585 hdr->frame_control |=
8586 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
8587 }
8588
8589 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8590 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8591 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8592 pos = (u8 *) (hdr + 1);
8593
8594 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008595 /* Set highest priority in QoS header */
8596 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008597 pos[1] = 0;
8598 pos += 2;
8599 }
8600
8601 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
8602 pos += sizeof(rfc1042_header);
8603 WPA_PUT_BE16(pos, ETH_P_PAE);
8604 pos += 2;
8605 memcpy(pos, data, data_len);
8606
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008607 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
8608 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008609 if (res < 0) {
8610 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
8611 "failed: %d (%s)",
8612 (unsigned long) len, errno, strerror(errno));
8613 }
8614 os_free(hdr);
8615
8616 return res;
8617}
8618
8619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008620static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
8621 int total_flags,
8622 int flags_or, int flags_and)
8623{
8624 struct i802_bss *bss = priv;
8625 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008626 struct nl_msg *msg;
8627 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008628 struct nl80211_sta_flag_update upd;
8629
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008630 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
8631 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
8632 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
8633 !!(total_flags & WPA_STA_AUTHORIZED));
8634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008635 msg = nlmsg_alloc();
8636 if (!msg)
8637 return -ENOMEM;
8638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008639 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008640
8641 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8642 if_nametoindex(bss->ifname));
8643 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8644
8645 /*
8646 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
8647 * can be removed eventually.
8648 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008649 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
8650 if (!flags)
8651 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008652 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008653 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008654
8655 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008656 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008657
8658 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008659 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008660
8661 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008662 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008664 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008665 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008666
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008667 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008668
8669 os_memset(&upd, 0, sizeof(upd));
8670 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
8671 upd.set = sta_flags_nl80211(flags_or);
8672 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008674 return send_and_recv_msgs(drv, msg, NULL, NULL);
8675 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008676 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008677 return -ENOBUFS;
8678}
8679
8680
8681static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
8682 struct wpa_driver_associate_params *params)
8683{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008684 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008685
8686 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008687 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
8688 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008689 nlmode = NL80211_IFTYPE_P2P_GO;
8690 } else
8691 nlmode = NL80211_IFTYPE_AP;
8692
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008693 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008694 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008695 nl80211_remove_monitor_interface(drv);
8696 return -1;
8697 }
8698
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008699 if (nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008700 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008701 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008702 nl80211_remove_monitor_interface(drv);
8703 return -1;
8704 }
8705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008706 return 0;
8707}
8708
8709
8710static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
8711{
8712 struct nl_msg *msg;
8713 int ret = -1;
8714
8715 msg = nlmsg_alloc();
8716 if (!msg)
8717 return -1;
8718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008719 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008720 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8721 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8722 msg = NULL;
8723 if (ret) {
8724 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
8725 "(%s)", ret, strerror(-ret));
8726 goto nla_put_failure;
8727 }
8728
8729 ret = 0;
8730 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
8731
8732nla_put_failure:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08008733 if (wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008734 NL80211_IFTYPE_STATION)) {
8735 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8736 "station mode");
8737 }
8738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008739 nlmsg_free(msg);
8740 return ret;
8741}
8742
8743
8744static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
8745 struct wpa_driver_associate_params *params)
8746{
8747 struct nl_msg *msg;
8748 int ret = -1;
8749 int count = 0;
8750
8751 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
8752
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008753 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008754 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
8755 "IBSS mode");
8756 return -1;
8757 }
8758
8759retry:
8760 msg = nlmsg_alloc();
8761 if (!msg)
8762 return -1;
8763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008764 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008765 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8766
8767 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
8768 goto nla_put_failure;
8769
8770 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8771 params->ssid, params->ssid_len);
8772 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8773 params->ssid);
8774 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8775 drv->ssid_len = params->ssid_len;
8776
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008777 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
8778 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", params->freq.ht_enabled);
8779 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
8780 params->freq.sec_channel_offset);
8781 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", params->freq.vht_enabled);
8782 wpa_printf(MSG_DEBUG, " * center_freq1=%d", params->freq.center_freq1);
8783 wpa_printf(MSG_DEBUG, " * center_freq2=%d", params->freq.center_freq2);
8784 wpa_printf(MSG_DEBUG, " * bandwidth=%d", params->freq.bandwidth);
8785 if (nl80211_put_freq_params(msg, &params->freq) < 0)
8786 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008787
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08008788 if (params->beacon_int > 0) {
8789 wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int);
8790 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL,
8791 params->beacon_int);
8792 }
8793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008794 ret = nl80211_set_conn_keys(params, msg);
8795 if (ret)
8796 goto nla_put_failure;
8797
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008798 if (params->bssid && params->fixed_bssid) {
8799 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
8800 MAC2STR(params->bssid));
8801 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8802 }
8803
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008804 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8805 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8806 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8807 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008808 wpa_printf(MSG_DEBUG, " * control port");
8809 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8810 }
8811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008812 if (params->wpa_ie) {
8813 wpa_hexdump(MSG_DEBUG,
8814 " * Extra IEs for Beacon/Probe Response frames",
8815 params->wpa_ie, params->wpa_ie_len);
8816 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8817 params->wpa_ie);
8818 }
8819
8820 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8821 msg = NULL;
8822 if (ret) {
8823 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
8824 ret, strerror(-ret));
8825 count++;
8826 if (ret == -EALREADY && count == 1) {
8827 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
8828 "forced leave");
8829 nl80211_leave_ibss(drv);
8830 nlmsg_free(msg);
8831 goto retry;
8832 }
8833
8834 goto nla_put_failure;
8835 }
8836 ret = 0;
8837 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
8838
8839nla_put_failure:
8840 nlmsg_free(msg);
8841 return ret;
8842}
8843
8844
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008845static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
8846 struct wpa_driver_associate_params *params,
8847 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008848{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008849 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008851 if (params->bssid) {
8852 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
8853 MAC2STR(params->bssid));
8854 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
8855 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008856
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008857 if (params->bssid_hint) {
8858 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
8859 MAC2STR(params->bssid_hint));
8860 NLA_PUT(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
8861 params->bssid_hint);
8862 }
8863
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07008864 if (params->freq.freq) {
8865 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
8866 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq.freq);
8867 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07008868 } else
8869 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008870
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008871 if (params->freq_hint) {
8872 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
8873 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
8874 params->freq_hint);
8875 }
8876
Dmitry Shmidt04949592012-07-19 12:16:46 -07008877 if (params->bg_scan_period >= 0) {
8878 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
8879 params->bg_scan_period);
8880 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
8881 params->bg_scan_period);
8882 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008884 if (params->ssid) {
8885 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
8886 params->ssid, params->ssid_len);
8887 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
8888 params->ssid);
8889 if (params->ssid_len > sizeof(drv->ssid))
8890 goto nla_put_failure;
8891 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
8892 drv->ssid_len = params->ssid_len;
8893 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008895 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
8896 if (params->wpa_ie)
8897 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
8898 params->wpa_ie);
8899
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008900 if (params->wpa_proto) {
8901 enum nl80211_wpa_versions ver = 0;
8902
8903 if (params->wpa_proto & WPA_PROTO_WPA)
8904 ver |= NL80211_WPA_VERSION_1;
8905 if (params->wpa_proto & WPA_PROTO_RSN)
8906 ver |= NL80211_WPA_VERSION_2;
8907
8908 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
8909 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
8910 }
8911
8912 if (params->pairwise_suite != WPA_CIPHER_NONE) {
8913 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
8914 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
8915 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
8916 }
8917
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008918 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
8919 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
8920 /*
8921 * This is likely to work even though many drivers do not
8922 * advertise support for operations without GTK.
8923 */
8924 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
8925 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008926 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
8927 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
8928 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
8929 }
8930
8931 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
8932 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
8933 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
8934 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07008935 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008936 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
8937 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
8938 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008939 int mgmt = WLAN_AKM_SUITE_PSK;
8940
8941 switch (params->key_mgmt_suite) {
8942 case WPA_KEY_MGMT_CCKM:
8943 mgmt = WLAN_AKM_SUITE_CCKM;
8944 break;
8945 case WPA_KEY_MGMT_IEEE8021X:
8946 mgmt = WLAN_AKM_SUITE_8021X;
8947 break;
8948 case WPA_KEY_MGMT_FT_IEEE8021X:
8949 mgmt = WLAN_AKM_SUITE_FT_8021X;
8950 break;
8951 case WPA_KEY_MGMT_FT_PSK:
8952 mgmt = WLAN_AKM_SUITE_FT_PSK;
8953 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07008954 case WPA_KEY_MGMT_IEEE8021X_SHA256:
8955 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
8956 break;
8957 case WPA_KEY_MGMT_PSK_SHA256:
8958 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
8959 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07008960 case WPA_KEY_MGMT_OSEN:
8961 mgmt = WLAN_AKM_SUITE_OSEN;
8962 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008963 case WPA_KEY_MGMT_PSK:
8964 default:
8965 mgmt = WLAN_AKM_SUITE_PSK;
8966 break;
8967 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008968 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008969 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
8970 }
8971
8972 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
8973
8974 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
8975 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
8976
8977 if (params->disable_ht)
8978 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
8979
8980 if (params->htcaps && params->htcaps_mask) {
8981 int sz = sizeof(struct ieee80211_ht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008982 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008983 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008984 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
8985 params->htcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008986 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
8987 params->htcaps_mask);
8988 }
8989
8990#ifdef CONFIG_VHT_OVERRIDES
8991 if (params->disable_vht) {
8992 wpa_printf(MSG_DEBUG, " * VHT disabled");
8993 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
8994 }
8995
8996 if (params->vhtcaps && params->vhtcaps_mask) {
8997 int sz = sizeof(struct ieee80211_vht_capabilities);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07008998 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008999 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07009000 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
9001 params->vhtcaps_mask, sz);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009002 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
9003 params->vhtcaps_mask);
9004 }
9005#endif /* CONFIG_VHT_OVERRIDES */
9006
9007 if (params->p2p)
9008 wpa_printf(MSG_DEBUG, " * P2P group");
9009
9010 return 0;
9011nla_put_failure:
9012 return -1;
9013}
9014
9015
9016static int wpa_driver_nl80211_try_connect(
9017 struct wpa_driver_nl80211_data *drv,
9018 struct wpa_driver_associate_params *params)
9019{
9020 struct nl_msg *msg;
9021 enum nl80211_auth_type type;
9022 int ret;
9023 int algs;
9024
9025 msg = nlmsg_alloc();
9026 if (!msg)
9027 return -1;
9028
9029 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9030 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
9031
9032 ret = nl80211_connect_common(drv, params, msg);
9033 if (ret)
9034 goto nla_put_failure;
9035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009036 algs = 0;
9037 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9038 algs++;
9039 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9040 algs++;
9041 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9042 algs++;
9043 if (algs > 1) {
9044 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
9045 "selection");
9046 goto skip_auth_type;
9047 }
9048
9049 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
9050 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
9051 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
9052 type = NL80211_AUTHTYPE_SHARED_KEY;
9053 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
9054 type = NL80211_AUTHTYPE_NETWORK_EAP;
9055 else if (params->auth_alg & WPA_AUTH_ALG_FT)
9056 type = NL80211_AUTHTYPE_FT;
9057 else
9058 goto nla_put_failure;
9059
9060 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
9061 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
9062
9063skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009064 ret = nl80211_set_conn_keys(params, msg);
9065 if (ret)
9066 goto nla_put_failure;
9067
9068 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9069 msg = NULL;
9070 if (ret) {
9071 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
9072 "(%s)", ret, strerror(-ret));
9073 goto nla_put_failure;
9074 }
9075 ret = 0;
9076 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
9077
9078nla_put_failure:
9079 nlmsg_free(msg);
9080 return ret;
9081
9082}
9083
9084
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009085static int wpa_driver_nl80211_connect(
9086 struct wpa_driver_nl80211_data *drv,
9087 struct wpa_driver_associate_params *params)
9088{
9089 int ret = wpa_driver_nl80211_try_connect(drv, params);
9090 if (ret == -EALREADY) {
9091 /*
9092 * cfg80211 does not currently accept new connections if
9093 * we are already connected. As a workaround, force
9094 * disconnection and try again.
9095 */
9096 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
9097 "disconnecting before reassociation "
9098 "attempt");
9099 if (wpa_driver_nl80211_disconnect(
9100 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
9101 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08009102 ret = wpa_driver_nl80211_try_connect(drv, params);
9103 }
9104 return ret;
9105}
9106
9107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009108static int wpa_driver_nl80211_associate(
9109 void *priv, struct wpa_driver_associate_params *params)
9110{
9111 struct i802_bss *bss = priv;
9112 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009113 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009114 struct nl_msg *msg;
9115
9116 if (params->mode == IEEE80211_MODE_AP)
9117 return wpa_driver_nl80211_ap(drv, params);
9118
9119 if (params->mode == IEEE80211_MODE_IBSS)
9120 return wpa_driver_nl80211_ibss(drv, params);
9121
9122 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009123 enum nl80211_iftype nlmode = params->p2p ?
9124 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
9125
9126 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009127 return -1;
9128 return wpa_driver_nl80211_connect(drv, params);
9129 }
9130
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009131 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009132
9133 msg = nlmsg_alloc();
9134 if (!msg)
9135 return -1;
9136
9137 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
9138 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009139 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009140
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009141 ret = nl80211_connect_common(drv, params, msg);
9142 if (ret)
9143 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009144
9145 if (params->prev_bssid) {
9146 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
9147 MAC2STR(params->prev_bssid));
9148 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
9149 params->prev_bssid);
9150 }
9151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009152 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9153 msg = NULL;
9154 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009155 wpa_dbg(drv->ctx, MSG_DEBUG,
9156 "nl80211: MLME command failed (assoc): ret=%d (%s)",
9157 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009158 nl80211_dump_scan(drv);
9159 goto nla_put_failure;
9160 }
9161 ret = 0;
9162 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
9163 "successfully");
9164
9165nla_put_failure:
9166 nlmsg_free(msg);
9167 return ret;
9168}
9169
9170
9171static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009172 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009173{
9174 struct nl_msg *msg;
9175 int ret = -ENOBUFS;
9176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009177 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
9178 ifindex, mode, nl80211_iftype_str(mode));
9179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009180 msg = nlmsg_alloc();
9181 if (!msg)
9182 return -ENOMEM;
9183
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009184 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009185 if (nl80211_set_iface_id(msg, drv->first_bss) < 0)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009186 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009187 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
9188
9189 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009190 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009191 if (!ret)
9192 return 0;
9193nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009194 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009195 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
9196 " %d (%s)", ifindex, mode, ret, strerror(-ret));
9197 return ret;
9198}
9199
9200
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009201static int wpa_driver_nl80211_set_mode_impl(
9202 struct i802_bss *bss,
9203 enum nl80211_iftype nlmode,
9204 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009205{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009206 struct wpa_driver_nl80211_data *drv = bss->drv;
9207 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009208 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009209 int was_ap = is_ap_interface(drv->nlmode);
9210 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009211 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009212
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009213 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9214 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
9215 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009216
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009217 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009218 drv->nlmode = nlmode;
9219 ret = 0;
9220 goto done;
9221 }
9222
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009223 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009224 return -1;
9225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009226 if (nlmode == drv->nlmode) {
9227 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
9228 "requested mode - ignore error");
9229 ret = 0;
9230 goto done; /* Already in the requested mode */
9231 }
9232
9233 /* mac80211 doesn't allow mode changes while the device is up, so
9234 * take the device down, try to set the mode again, and bring the
9235 * device back up.
9236 */
9237 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
9238 "interface down");
9239 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009240 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009241 if (res == -EACCES || res == -ENODEV)
9242 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009243 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009244 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
9245 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009246 os_sleep(0, 100000);
9247 continue;
9248 }
9249
9250 /*
9251 * Setting the mode will fail for some drivers if the phy is
9252 * on a frequency that the mode is disallowed in.
9253 */
9254 if (desired_freq_params) {
9255 res = i802_set_freq(bss, desired_freq_params);
9256 if (res) {
9257 wpa_printf(MSG_DEBUG,
9258 "nl80211: Failed to set frequency on interface");
9259 }
9260 }
9261
9262 /* Try to set the mode again while the interface is down */
9263 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
9264 if (mode_switch_res == -EBUSY) {
9265 wpa_printf(MSG_DEBUG,
9266 "nl80211: Delaying mode set while interface going down");
9267 os_sleep(0, 100000);
9268 continue;
9269 }
9270 ret = mode_switch_res;
9271 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009272 }
9273
9274 if (!ret) {
9275 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
9276 "interface is down");
9277 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009278 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009279 }
9280
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009281 /* Bring the interface back up */
9282 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
9283 if (res != 0) {
9284 wpa_printf(MSG_DEBUG,
9285 "nl80211: Failed to set interface up after switching mode");
9286 ret = -1;
9287 }
9288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009289done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009290 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009291 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
9292 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009293 return ret;
9294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009295
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009296 if (is_p2p_net_interface(nlmode))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009297 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
9298 else if (drv->disabled_11b_rates)
9299 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
9300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009301 if (is_ap_interface(nlmode)) {
9302 nl80211_mgmt_unsubscribe(bss, "start AP");
9303 /* Setup additional AP mode functionality if needed */
9304 if (nl80211_setup_ap(bss))
9305 return -1;
9306 } else if (was_ap) {
9307 /* Remove additional AP mode functionality */
9308 nl80211_teardown_ap(bss);
9309 } else {
9310 nl80211_mgmt_unsubscribe(bss, "mode change");
9311 }
9312
Dmitry Shmidt04949592012-07-19 12:16:46 -07009313 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009314 nl80211_mgmt_subscribe_non_ap(bss) < 0)
9315 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
9316 "frame processing - ignore for now");
9317
9318 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009319}
9320
9321
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009322static int dfs_info_handler(struct nl_msg *msg, void *arg)
9323{
9324 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9325 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9326 int *dfs_capability_ptr = arg;
9327
9328 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9329 genlmsg_attrlen(gnlh, 0), NULL);
9330
9331 if (tb[NL80211_ATTR_VENDOR_DATA]) {
9332 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9333 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9334
9335 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9336 nla_data(nl_vend), nla_len(nl_vend), NULL);
9337
9338 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
9339 u32 val;
9340 val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
9341 wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
9342 val);
9343 *dfs_capability_ptr = val;
9344 }
9345 }
9346
9347 return NL_SKIP;
9348}
9349
9350
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009351static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
9352 enum nl80211_iftype nlmode)
9353{
9354 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
9355}
9356
9357
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07009358static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
9359 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009360{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009361 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07009362 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07009363}
9364
9365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009366static int wpa_driver_nl80211_get_capa(void *priv,
9367 struct wpa_driver_capa *capa)
9368{
9369 struct i802_bss *bss = priv;
9370 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009371 struct nl_msg *msg;
9372 int dfs_capability = 0;
9373 int ret = 0;
9374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009375 if (!drv->has_capability)
9376 return -1;
9377 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07009378 if (drv->extended_capa && drv->extended_capa_mask) {
9379 capa->extended_capa = drv->extended_capa;
9380 capa->extended_capa_mask = drv->extended_capa_mask;
9381 capa->extended_capa_len = drv->extended_capa_len;
9382 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009383
9384 if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
9385 !drv->allow_p2p_device) {
9386 wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
9387 capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
9388 }
9389
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07009390 if (drv->dfs_vendor_cmd_avail == 1) {
9391 msg = nlmsg_alloc();
9392 if (!msg)
9393 return -ENOMEM;
9394
9395 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
9396
9397 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9398 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
9399 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9400 QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY);
9401
9402 ret = send_and_recv_msgs(drv, msg, dfs_info_handler,
9403 &dfs_capability);
9404 if (!ret) {
9405 if (dfs_capability)
9406 capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
9407 }
9408 }
9409
9410 return ret;
9411
9412 nla_put_failure:
9413 nlmsg_free(msg);
9414 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009415}
9416
9417
9418static int wpa_driver_nl80211_set_operstate(void *priv, int state)
9419{
9420 struct i802_bss *bss = priv;
9421 struct wpa_driver_nl80211_data *drv = bss->drv;
9422
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009423 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
9424 bss->ifname, drv->operstate, state,
9425 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009426 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009427 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009428 state ? IF_OPER_UP : IF_OPER_DORMANT);
9429}
9430
9431
9432static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
9433{
9434 struct i802_bss *bss = priv;
9435 struct wpa_driver_nl80211_data *drv = bss->drv;
9436 struct nl_msg *msg;
9437 struct nl80211_sta_flag_update upd;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009438 int ret = -ENOBUFS;
9439
9440 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
9441 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
9442 return 0;
9443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009444
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009445 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
9446 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
9447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009448 msg = nlmsg_alloc();
9449 if (!msg)
9450 return -ENOMEM;
9451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009452 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009453
9454 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9455 if_nametoindex(bss->ifname));
9456 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
9457
9458 os_memset(&upd, 0, sizeof(upd));
9459 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
9460 if (authorized)
9461 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
9462 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
9463
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009464 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9465 msg = NULL;
9466 if (!ret)
9467 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009468 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009469 nlmsg_free(msg);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009470 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
9471 ret, strerror(-ret));
9472 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009473}
9474
9475
Jouni Malinen75ecf522011-06-27 15:19:46 -07009476/* Set kernel driver on given frequency (MHz) */
9477static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009478{
Jouni Malinen75ecf522011-06-27 15:19:46 -07009479 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009480 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009481}
9482
9483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009484static inline int min_int(int a, int b)
9485{
9486 if (a < b)
9487 return a;
9488 return b;
9489}
9490
9491
9492static int get_key_handler(struct nl_msg *msg, void *arg)
9493{
9494 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9495 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9496
9497 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9498 genlmsg_attrlen(gnlh, 0), NULL);
9499
9500 /*
9501 * TODO: validate the key index and mac address!
9502 * Otherwise, there's a race condition as soon as
9503 * the kernel starts sending key notifications.
9504 */
9505
9506 if (tb[NL80211_ATTR_KEY_SEQ])
9507 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
9508 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
9509 return NL_SKIP;
9510}
9511
9512
9513static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
9514 int idx, u8 *seq)
9515{
9516 struct i802_bss *bss = priv;
9517 struct wpa_driver_nl80211_data *drv = bss->drv;
9518 struct nl_msg *msg;
9519
9520 msg = nlmsg_alloc();
9521 if (!msg)
9522 return -ENOMEM;
9523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009524 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009525
9526 if (addr)
9527 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9528 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
9529 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
9530
9531 memset(seq, 0, 6);
9532
9533 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
9534 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009535 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009536 return -ENOBUFS;
9537}
9538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009539
9540static int i802_set_rts(void *priv, int rts)
9541{
9542 struct i802_bss *bss = priv;
9543 struct wpa_driver_nl80211_data *drv = bss->drv;
9544 struct nl_msg *msg;
9545 int ret = -ENOBUFS;
9546 u32 val;
9547
9548 msg = nlmsg_alloc();
9549 if (!msg)
9550 return -ENOMEM;
9551
9552 if (rts >= 2347)
9553 val = (u32) -1;
9554 else
9555 val = rts;
9556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009557 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009558 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9559 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
9560
9561 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009562 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009563 if (!ret)
9564 return 0;
9565nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009566 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009567 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
9568 "%d (%s)", rts, ret, strerror(-ret));
9569 return ret;
9570}
9571
9572
9573static int i802_set_frag(void *priv, int frag)
9574{
9575 struct i802_bss *bss = priv;
9576 struct wpa_driver_nl80211_data *drv = bss->drv;
9577 struct nl_msg *msg;
9578 int ret = -ENOBUFS;
9579 u32 val;
9580
9581 msg = nlmsg_alloc();
9582 if (!msg)
9583 return -ENOMEM;
9584
9585 if (frag >= 2346)
9586 val = (u32) -1;
9587 else
9588 val = frag;
9589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009590 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009591 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9592 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
9593
9594 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009595 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009596 if (!ret)
9597 return 0;
9598nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009599 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009600 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
9601 "%d: %d (%s)", frag, ret, strerror(-ret));
9602 return ret;
9603}
9604
9605
9606static int i802_flush(void *priv)
9607{
9608 struct i802_bss *bss = priv;
9609 struct wpa_driver_nl80211_data *drv = bss->drv;
9610 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009611 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009612
9613 msg = nlmsg_alloc();
9614 if (!msg)
9615 return -1;
9616
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009617 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
9618 bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009619 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009620
9621 /*
9622 * XXX: FIX! this needs to flush all VLANs too
9623 */
9624 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9625 if_nametoindex(bss->ifname));
9626
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009627 res = send_and_recv_msgs(drv, msg, NULL, NULL);
9628 if (res) {
9629 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
9630 "(%s)", res, strerror(-res));
9631 }
9632 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009633 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009634 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009635 return -ENOBUFS;
9636}
9637
9638
9639static int get_sta_handler(struct nl_msg *msg, void *arg)
9640{
9641 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9642 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9643 struct hostap_sta_driver_data *data = arg;
9644 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
9645 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
9646 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
9647 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
9648 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
9649 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
9650 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009651 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009652 };
9653
9654 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9655 genlmsg_attrlen(gnlh, 0), NULL);
9656
9657 /*
9658 * TODO: validate the interface and mac address!
9659 * Otherwise, there's a race condition as soon as
9660 * the kernel starts sending station notifications.
9661 */
9662
9663 if (!tb[NL80211_ATTR_STA_INFO]) {
9664 wpa_printf(MSG_DEBUG, "sta stats missing!");
9665 return NL_SKIP;
9666 }
9667 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
9668 tb[NL80211_ATTR_STA_INFO],
9669 stats_policy)) {
9670 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
9671 return NL_SKIP;
9672 }
9673
9674 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
9675 data->inactive_msec =
9676 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
9677 if (stats[NL80211_STA_INFO_RX_BYTES])
9678 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
9679 if (stats[NL80211_STA_INFO_TX_BYTES])
9680 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
9681 if (stats[NL80211_STA_INFO_RX_PACKETS])
9682 data->rx_packets =
9683 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
9684 if (stats[NL80211_STA_INFO_TX_PACKETS])
9685 data->tx_packets =
9686 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03009687 if (stats[NL80211_STA_INFO_TX_FAILED])
9688 data->tx_retry_failed =
9689 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009690
9691 return NL_SKIP;
9692}
9693
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009694static int i802_read_sta_data(struct i802_bss *bss,
9695 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009696 const u8 *addr)
9697{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009698 struct wpa_driver_nl80211_data *drv = bss->drv;
9699 struct nl_msg *msg;
9700
9701 os_memset(data, 0, sizeof(*data));
9702 msg = nlmsg_alloc();
9703 if (!msg)
9704 return -ENOMEM;
9705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009706 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009707
9708 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9709 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9710
9711 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
9712 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009713 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009714 return -ENOBUFS;
9715}
9716
9717
9718static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
9719 int cw_min, int cw_max, int burst_time)
9720{
9721 struct i802_bss *bss = priv;
9722 struct wpa_driver_nl80211_data *drv = bss->drv;
9723 struct nl_msg *msg;
9724 struct nlattr *txq, *params;
9725
9726 msg = nlmsg_alloc();
9727 if (!msg)
9728 return -1;
9729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009730 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009731
9732 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9733
9734 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
9735 if (!txq)
9736 goto nla_put_failure;
9737
9738 /* We are only sending parameters for a single TXQ at a time */
9739 params = nla_nest_start(msg, 1);
9740 if (!params)
9741 goto nla_put_failure;
9742
9743 switch (queue) {
9744 case 0:
9745 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
9746 break;
9747 case 1:
9748 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
9749 break;
9750 case 2:
9751 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
9752 break;
9753 case 3:
9754 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
9755 break;
9756 }
9757 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
9758 * 32 usec, so need to convert the value here. */
9759 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
9760 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
9761 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
9762 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
9763
9764 nla_nest_end(msg, params);
9765
9766 nla_nest_end(msg, txq);
9767
9768 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
9769 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009770 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009771 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009772 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009773 return -1;
9774}
9775
9776
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009777static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009778 const char *ifname, int vlan_id)
9779{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009780 struct wpa_driver_nl80211_data *drv = bss->drv;
9781 struct nl_msg *msg;
9782 int ret = -ENOBUFS;
9783
9784 msg = nlmsg_alloc();
9785 if (!msg)
9786 return -ENOMEM;
9787
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009788 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
9789 ", ifname=%s[%d], vlan_id=%d)",
9790 bss->ifname, if_nametoindex(bss->ifname),
9791 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009792 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009793
9794 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
9795 if_nametoindex(bss->ifname));
9796 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9797 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
9798 if_nametoindex(ifname));
9799
9800 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009801 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009802 if (ret < 0) {
9803 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
9804 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
9805 MAC2STR(addr), ifname, vlan_id, ret,
9806 strerror(-ret));
9807 }
9808 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009809 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009810 return ret;
9811}
9812
9813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814static int i802_get_inact_sec(void *priv, const u8 *addr)
9815{
9816 struct hostap_sta_driver_data data;
9817 int ret;
9818
9819 data.inactive_msec = (unsigned long) -1;
9820 ret = i802_read_sta_data(priv, &data, addr);
9821 if (ret || data.inactive_msec == (unsigned long) -1)
9822 return -1;
9823 return data.inactive_msec / 1000;
9824}
9825
9826
9827static int i802_sta_clear_stats(void *priv, const u8 *addr)
9828{
9829#if 0
9830 /* TODO */
9831#endif
9832 return 0;
9833}
9834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009835
9836static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
9837 int reason)
9838{
9839 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009840 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009841 struct ieee80211_mgmt mgmt;
9842
Dmitry Shmidt04949592012-07-19 12:16:46 -07009843 if (drv->device_ap_sme)
9844 return wpa_driver_nl80211_sta_remove(bss, addr);
9845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009846 memset(&mgmt, 0, sizeof(mgmt));
9847 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9848 WLAN_FC_STYPE_DEAUTH);
9849 memcpy(mgmt.da, addr, ETH_ALEN);
9850 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9851 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9852 mgmt.u.deauth.reason_code = host_to_le16(reason);
9853 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9854 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009855 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
9856 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009857}
9858
9859
9860static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
9861 int reason)
9862{
9863 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009864 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009865 struct ieee80211_mgmt mgmt;
9866
Dmitry Shmidt04949592012-07-19 12:16:46 -07009867 if (drv->device_ap_sme)
9868 return wpa_driver_nl80211_sta_remove(bss, addr);
9869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009870 memset(&mgmt, 0, sizeof(mgmt));
9871 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
9872 WLAN_FC_STYPE_DISASSOC);
9873 memcpy(mgmt.da, addr, ETH_ALEN);
9874 memcpy(mgmt.sa, own_addr, ETH_ALEN);
9875 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
9876 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9877 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9878 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009879 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
9880 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009881}
9882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009883
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009884static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
9885{
9886 char buf[200], *pos, *end;
9887 int i, res;
9888
9889 pos = buf;
9890 end = pos + sizeof(buf);
9891
9892 for (i = 0; i < drv->num_if_indices; i++) {
9893 if (!drv->if_indices[i])
9894 continue;
9895 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
9896 if (res < 0 || res >= end - pos)
9897 break;
9898 pos += res;
9899 }
9900 *pos = '\0';
9901
9902 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
9903 drv->num_if_indices, buf);
9904}
9905
9906
Jouni Malinen75ecf522011-06-27 15:19:46 -07009907static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9908{
9909 int i;
9910 int *old;
9911
9912 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
9913 ifidx);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009914 if (have_ifidx(drv, ifidx)) {
9915 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
9916 ifidx);
9917 return;
9918 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07009919 for (i = 0; i < drv->num_if_indices; i++) {
9920 if (drv->if_indices[i] == 0) {
9921 drv->if_indices[i] = ifidx;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009922 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009923 return;
9924 }
9925 }
9926
9927 if (drv->if_indices != drv->default_if_indices)
9928 old = drv->if_indices;
9929 else
9930 old = NULL;
9931
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009932 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
9933 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07009934 if (!drv->if_indices) {
9935 if (!old)
9936 drv->if_indices = drv->default_if_indices;
9937 else
9938 drv->if_indices = old;
9939 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
9940 "interfaces");
9941 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
9942 return;
9943 } else if (!old)
9944 os_memcpy(drv->if_indices, drv->default_if_indices,
9945 sizeof(drv->default_if_indices));
9946 drv->if_indices[drv->num_if_indices] = ifidx;
9947 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009948 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009949}
9950
9951
9952static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9953{
9954 int i;
9955
9956 for (i = 0; i < drv->num_if_indices; i++) {
9957 if (drv->if_indices[i] == ifidx) {
9958 drv->if_indices[i] = 0;
9959 break;
9960 }
9961 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009962 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009963}
9964
9965
9966static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
9967{
9968 int i;
9969
9970 for (i = 0; i < drv->num_if_indices; i++)
9971 if (drv->if_indices[i] == ifidx)
9972 return 1;
9973
9974 return 0;
9975}
9976
9977
9978static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009979 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009980{
9981 struct i802_bss *bss = priv;
9982 struct wpa_driver_nl80211_data *drv = bss->drv;
9983 char name[IFNAMSIZ + 1];
9984
9985 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07009986 if (ifname_wds)
9987 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
9988
Jouni Malinen75ecf522011-06-27 15:19:46 -07009989 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
9990 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
9991 if (val) {
9992 if (!if_nametoindex(name)) {
9993 if (nl80211_create_iface(drv, name,
9994 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009995 bss->addr, 1, NULL, NULL, 0) <
9996 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07009997 return -1;
9998 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009999 linux_br_add_if(drv->global->ioctl_sock,
10000 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -070010001 return -1;
10002 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010003 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
10004 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
10005 "interface %s up", name);
10006 }
Jouni Malinen75ecf522011-06-27 15:19:46 -070010007 return i802_set_sta_vlan(priv, addr, name, 0);
10008 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010009 if (bridge_ifname)
10010 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
10011 name);
10012
Jouni Malinen75ecf522011-06-27 15:19:46 -070010013 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -080010014 nl80211_remove_iface(drv, if_nametoindex(name));
10015 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -070010016 }
10017}
10018
10019
10020static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
10021{
10022 struct wpa_driver_nl80211_data *drv = eloop_ctx;
10023 struct sockaddr_ll lladdr;
10024 unsigned char buf[3000];
10025 int len;
10026 socklen_t fromlen = sizeof(lladdr);
10027
10028 len = recvfrom(sock, buf, sizeof(buf), 0,
10029 (struct sockaddr *)&lladdr, &fromlen);
10030 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010031 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
10032 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -070010033 return;
10034 }
10035
10036 if (have_ifidx(drv, lladdr.sll_ifindex))
10037 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
10038}
10039
10040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010041static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
10042 struct i802_bss *bss,
10043 const char *brname, const char *ifname)
10044{
10045 int ifindex;
10046 char in_br[IFNAMSIZ];
10047
10048 os_strlcpy(bss->brname, brname, IFNAMSIZ);
10049 ifindex = if_nametoindex(brname);
10050 if (ifindex == 0) {
10051 /*
10052 * Bridge was configured, but the bridge device does
10053 * not exist. Try to add it now.
10054 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010055 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010056 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
10057 "bridge interface %s: %s",
10058 brname, strerror(errno));
10059 return -1;
10060 }
10061 bss->added_bridge = 1;
10062 add_ifidx(drv, if_nametoindex(brname));
10063 }
10064
10065 if (linux_br_get(in_br, ifname) == 0) {
10066 if (os_strcmp(in_br, brname) == 0)
10067 return 0; /* already in the bridge */
10068
10069 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
10070 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010071 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
10072 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010073 wpa_printf(MSG_ERROR, "nl80211: Failed to "
10074 "remove interface %s from bridge "
10075 "%s: %s",
10076 ifname, brname, strerror(errno));
10077 return -1;
10078 }
10079 }
10080
10081 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
10082 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010083 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010084 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
10085 "into bridge %s: %s",
10086 ifname, brname, strerror(errno));
10087 return -1;
10088 }
10089 bss->added_if_into_bridge = 1;
10090
10091 return 0;
10092}
10093
10094
10095static void *i802_init(struct hostapd_data *hapd,
10096 struct wpa_init_params *params)
10097{
10098 struct wpa_driver_nl80211_data *drv;
10099 struct i802_bss *bss;
10100 size_t i;
10101 char brname[IFNAMSIZ];
10102 int ifindex, br_ifindex;
10103 int br_added = 0;
10104
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080010105 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
10106 params->global_priv, 1,
10107 params->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010108 if (bss == NULL)
10109 return NULL;
10110
10111 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010113 if (linux_br_get(brname, params->ifname) == 0) {
10114 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
10115 params->ifname, brname);
10116 br_ifindex = if_nametoindex(brname);
10117 } else {
10118 brname[0] = '\0';
10119 br_ifindex = 0;
10120 }
10121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010122 for (i = 0; i < params->num_bridge; i++) {
10123 if (params->bridge[i]) {
10124 ifindex = if_nametoindex(params->bridge[i]);
10125 if (ifindex)
10126 add_ifidx(drv, ifindex);
10127 if (ifindex == br_ifindex)
10128 br_added = 1;
10129 }
10130 }
10131 if (!br_added && br_ifindex &&
10132 (params->num_bridge == 0 || !params->bridge[0]))
10133 add_ifidx(drv, br_ifindex);
10134
10135 /* start listening for EAPOL on the default AP interface */
10136 add_ifidx(drv, drv->ifindex);
10137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010138 if (params->num_bridge && params->bridge[0] &&
10139 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
10140 goto failed;
10141
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010142#ifdef CONFIG_LIBNL3_ROUTE
10143 if (bss->added_if_into_bridge) {
10144 drv->rtnl_sk = nl_socket_alloc();
10145 if (drv->rtnl_sk == NULL) {
10146 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
10147 goto failed;
10148 }
10149
10150 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
10151 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
10152 strerror(errno));
10153 goto failed;
10154 }
10155 }
10156#endif /* CONFIG_LIBNL3_ROUTE */
10157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010158 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
10159 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010160 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
10161 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010162 goto failed;
10163 }
10164
10165 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
10166 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010167 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010168 goto failed;
10169 }
10170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010171 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
10172 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010173 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070010174 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010176 memcpy(bss->addr, params->own_addr, ETH_ALEN);
10177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010178 return bss;
10179
10180failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010181 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010182 return NULL;
10183}
10184
10185
10186static void i802_deinit(void *priv)
10187{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010188 struct i802_bss *bss = priv;
10189 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010190}
10191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010192
10193static enum nl80211_iftype wpa_driver_nl80211_if_type(
10194 enum wpa_driver_if_type type)
10195{
10196 switch (type) {
10197 case WPA_IF_STATION:
10198 return NL80211_IFTYPE_STATION;
10199 case WPA_IF_P2P_CLIENT:
10200 case WPA_IF_P2P_GROUP:
10201 return NL80211_IFTYPE_P2P_CLIENT;
10202 case WPA_IF_AP_VLAN:
10203 return NL80211_IFTYPE_AP_VLAN;
10204 case WPA_IF_AP_BSS:
10205 return NL80211_IFTYPE_AP;
10206 case WPA_IF_P2P_GO:
10207 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010208 case WPA_IF_P2P_DEVICE:
10209 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010210 }
10211 return -1;
10212}
10213
10214
10215#ifdef CONFIG_P2P
10216
10217static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
10218{
10219 struct wpa_driver_nl80211_data *drv;
10220 dl_list_for_each(drv, &global->interfaces,
10221 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010222 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010223 return 1;
10224 }
10225 return 0;
10226}
10227
10228
10229static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
10230 u8 *new_addr)
10231{
10232 unsigned int idx;
10233
10234 if (!drv->global)
10235 return -1;
10236
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010237 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010238 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010239 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010240 new_addr[0] ^= idx << 2;
10241 if (!nl80211_addr_in_use(drv->global, new_addr))
10242 break;
10243 }
10244 if (idx == 64)
10245 return -1;
10246
10247 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
10248 MACSTR, MAC2STR(new_addr));
10249
10250 return 0;
10251}
10252
10253#endif /* CONFIG_P2P */
10254
10255
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010256struct wdev_info {
10257 u64 wdev_id;
10258 int wdev_id_set;
10259 u8 macaddr[ETH_ALEN];
10260};
10261
10262static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
10263{
10264 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10265 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10266 struct wdev_info *wi = arg;
10267
10268 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10269 genlmsg_attrlen(gnlh, 0), NULL);
10270 if (tb[NL80211_ATTR_WDEV]) {
10271 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
10272 wi->wdev_id_set = 1;
10273 }
10274
10275 if (tb[NL80211_ATTR_MAC])
10276 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
10277 ETH_ALEN);
10278
10279 return NL_SKIP;
10280}
10281
10282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010283static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
10284 const char *ifname, const u8 *addr,
10285 void *bss_ctx, void **drv_priv,
10286 char *force_ifname, u8 *if_addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010287 const char *bridge, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010288{
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010289 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010290 struct i802_bss *bss = priv;
10291 struct wpa_driver_nl80211_data *drv = bss->drv;
10292 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010293 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010294
10295 if (addr)
10296 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010297 nlmode = wpa_driver_nl80211_if_type(type);
10298 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
10299 struct wdev_info p2pdev_info;
10300
10301 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
10302 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
10303 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010304 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010305 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
10306 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
10307 ifname);
10308 return -1;
10309 }
10310
10311 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
10312 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
10313 if (!is_zero_ether_addr(p2pdev_info.macaddr))
10314 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
10315 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
10316 ifname,
10317 (long long unsigned int) p2pdev_info.wdev_id);
10318 } else {
10319 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010320 0, NULL, NULL, use_existing);
10321 if (use_existing && ifidx == -ENFILE) {
10322 added = 0;
10323 ifidx = if_nametoindex(ifname);
10324 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010325 return -1;
10326 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010327 }
10328
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010329 if (!addr) {
10330 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
10331 os_memcpy(if_addr, bss->addr, ETH_ALEN);
10332 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
10333 bss->ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010334 if (added)
10335 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010336 return -1;
10337 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010338 }
10339
10340#ifdef CONFIG_P2P
10341 if (!addr &&
10342 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
10343 type == WPA_IF_P2P_GO)) {
10344 /* Enforce unique P2P Interface Address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010345 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010346
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010347 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010348 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010349 if (added)
10350 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010351 return -1;
10352 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010353 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010354 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
10355 "for P2P group interface");
10356 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010357 if (added)
10358 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010359 return -1;
10360 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010361 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010362 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010363 if (added)
10364 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010365 return -1;
10366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010367 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010368 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010369 }
10370#endif /* CONFIG_P2P */
10371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010372 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010373 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
10374 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010375 if (added)
10376 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010377 return -1;
10378 }
10379
10380 if (bridge &&
10381 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
10382 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
10383 "interface %s to a bridge %s",
10384 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010385 if (added)
10386 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010387 os_free(new_bss);
10388 return -1;
10389 }
10390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010391 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
10392 {
Dmitry Shmidt71757432014-06-02 13:50:35 -070010393 if (added)
10394 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010395 os_free(new_bss);
10396 return -1;
10397 }
10398 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010399 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010400 new_bss->ifindex = ifidx;
10401 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010402 new_bss->next = drv->first_bss->next;
10403 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080010404 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010405 new_bss->added_if = added;
10406 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010407 if (drv_priv)
10408 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010409 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010410
10411 /* Subscribe management frames for this WPA_IF_AP_BSS */
10412 if (nl80211_setup_ap(new_bss))
10413 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010415
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010416 if (drv->global)
10417 drv->global->if_add_ifindex = ifidx;
10418
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010419 /*
10420 * Some virtual interfaces need to process EAPOL packets and events on
10421 * the parent interface. This is used mainly with hostapd.
10422 */
10423 if (ifidx > 0 &&
10424 (drv->hostapd ||
10425 nlmode == NL80211_IFTYPE_AP_VLAN ||
10426 nlmode == NL80211_IFTYPE_WDS ||
10427 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010428 add_ifidx(drv, ifidx);
10429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010430 return 0;
10431}
10432
10433
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010434static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010435 enum wpa_driver_if_type type,
10436 const char *ifname)
10437{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010438 struct wpa_driver_nl80211_data *drv = bss->drv;
10439 int ifindex = if_nametoindex(ifname);
10440
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010441 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
10442 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -080010443 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -070010444 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -070010445 else if (ifindex > 0 && !bss->added_if) {
10446 struct wpa_driver_nl80211_data *drv2;
10447 dl_list_for_each(drv2, &drv->global->interfaces,
10448 struct wpa_driver_nl80211_data, list)
10449 del_ifidx(drv2, ifindex);
10450 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010451
Dmitry Shmidtaa532512012-09-24 10:35:31 -070010452 if (type != WPA_IF_AP_BSS)
10453 return 0;
10454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010455 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010456 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
10457 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010458 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10459 "interface %s from bridge %s: %s",
10460 bss->ifname, bss->brname, strerror(errno));
10461 }
10462 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010463 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010464 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
10465 "bridge %s: %s",
10466 bss->brname, strerror(errno));
10467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010468
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010469 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010470 struct i802_bss *tbss;
10471
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010472 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
10473 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010474 if (tbss->next == bss) {
10475 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010476 /* Unsubscribe management frames */
10477 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010478 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070010479 if (!bss->added_if)
10480 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010481 os_free(bss);
10482 bss = NULL;
10483 break;
10484 }
10485 }
10486 if (bss)
10487 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
10488 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080010489 } else {
10490 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
10491 nl80211_teardown_ap(bss);
10492 if (!bss->added_if && !drv->first_bss->next)
10493 wpa_driver_nl80211_del_beacon(drv);
10494 nl80211_destroy_bss(bss);
10495 if (!bss->added_if)
10496 i802_set_iface_flags(bss, 0);
10497 if (drv->first_bss->next) {
10498 drv->first_bss = drv->first_bss->next;
10499 drv->ctx = drv->first_bss->ctx;
10500 os_free(bss);
10501 } else {
10502 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
10503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010505
10506 return 0;
10507}
10508
10509
10510static int cookie_handler(struct nl_msg *msg, void *arg)
10511{
10512 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10513 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10514 u64 *cookie = arg;
10515 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10516 genlmsg_attrlen(gnlh, 0), NULL);
10517 if (tb[NL80211_ATTR_COOKIE])
10518 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
10519 return NL_SKIP;
10520}
10521
10522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010523static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010524 unsigned int freq, unsigned int wait,
10525 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010526 u64 *cookie_out, int no_cck, int no_ack,
10527 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010528{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010529 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010530 struct nl_msg *msg;
10531 u64 cookie;
10532 int ret = -1;
10533
10534 msg = nlmsg_alloc();
10535 if (!msg)
10536 return -1;
10537
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010538 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -070010539 "no_ack=%d offchanok=%d",
10540 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010541 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010542 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010543
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010544 if (nl80211_set_iface_id(msg, bss) < 0)
10545 goto nla_put_failure;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -070010546 if (freq)
10547 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010548 if (wait)
10549 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080010550 if (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10551 drv->test_use_roc_tx))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010552 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
10553 if (no_cck)
10554 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
10555 if (no_ack)
10556 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
10557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010558 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
10559
10560 cookie = 0;
10561 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
10562 msg = NULL;
10563 if (ret) {
10564 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010565 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
10566 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010567 goto nla_put_failure;
10568 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010569 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010570 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
10571 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010572
10573 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010574 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010575
10576nla_put_failure:
10577 nlmsg_free(msg);
10578 return ret;
10579}
10580
10581
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010582static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
10583 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010584 unsigned int wait_time,
10585 const u8 *dst, const u8 *src,
10586 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010587 const u8 *data, size_t data_len,
10588 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010589{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010590 struct wpa_driver_nl80211_data *drv = bss->drv;
10591 int ret = -1;
10592 u8 *buf;
10593 struct ieee80211_hdr *hdr;
10594
10595 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080010596 "freq=%u MHz wait=%d ms no_cck=%d)",
10597 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010598
10599 buf = os_zalloc(24 + data_len);
10600 if (buf == NULL)
10601 return ret;
10602 os_memcpy(buf + 24, data, data_len);
10603 hdr = (struct ieee80211_hdr *) buf;
10604 hdr->frame_control =
10605 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
10606 os_memcpy(hdr->addr1, dst, ETH_ALEN);
10607 os_memcpy(hdr->addr2, src, ETH_ALEN);
10608 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
10609
Dmitry Shmidt56052862013-10-04 10:23:25 -070010610 if (is_ap_interface(drv->nlmode) &&
10611 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
10612 (int) freq == bss->freq || drv->device_ap_sme ||
10613 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010614 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
10615 0, freq, no_cck, 1,
10616 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010617 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010618 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010619 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010620 &drv->send_action_cookie,
10621 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010622
10623 os_free(buf);
10624 return ret;
10625}
10626
10627
10628static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
10629{
10630 struct i802_bss *bss = priv;
10631 struct wpa_driver_nl80211_data *drv = bss->drv;
10632 struct nl_msg *msg;
10633 int ret;
10634
10635 msg = nlmsg_alloc();
10636 if (!msg)
10637 return;
10638
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -080010639 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
10640 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010641 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010642
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010643 if (nl80211_set_iface_id(msg, bss) < 0)
10644 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010645 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
10646
10647 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10648 msg = NULL;
10649 if (ret)
10650 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
10651 "(%s)", ret, strerror(-ret));
10652
10653 nla_put_failure:
10654 nlmsg_free(msg);
10655}
10656
10657
10658static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
10659 unsigned int duration)
10660{
10661 struct i802_bss *bss = priv;
10662 struct wpa_driver_nl80211_data *drv = bss->drv;
10663 struct nl_msg *msg;
10664 int ret;
10665 u64 cookie;
10666
10667 msg = nlmsg_alloc();
10668 if (!msg)
10669 return -1;
10670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010671 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010672
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010673 if (nl80211_set_iface_id(msg, bss) < 0)
10674 goto nla_put_failure;
10675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010676 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
10677 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
10678
10679 cookie = 0;
10680 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010681 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010682 if (ret == 0) {
10683 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
10684 "0x%llx for freq=%u MHz duration=%u",
10685 (long long unsigned int) cookie, freq, duration);
10686 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010687 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010688 return 0;
10689 }
10690 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
10691 "(freq=%d duration=%u): %d (%s)",
10692 freq, duration, ret, strerror(-ret));
10693nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010694 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010695 return -1;
10696}
10697
10698
10699static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
10700{
10701 struct i802_bss *bss = priv;
10702 struct wpa_driver_nl80211_data *drv = bss->drv;
10703 struct nl_msg *msg;
10704 int ret;
10705
10706 if (!drv->pending_remain_on_chan) {
10707 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
10708 "to cancel");
10709 return -1;
10710 }
10711
10712 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
10713 "0x%llx",
10714 (long long unsigned int) drv->remain_on_chan_cookie);
10715
10716 msg = nlmsg_alloc();
10717 if (!msg)
10718 return -1;
10719
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010720 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010721
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010722 if (nl80211_set_iface_id(msg, bss) < 0)
10723 goto nla_put_failure;
10724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010725 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
10726
10727 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010728 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010729 if (ret == 0)
10730 return 0;
10731 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
10732 "%d (%s)", ret, strerror(-ret));
10733nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010734 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010735 return -1;
10736}
10737
10738
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080010739static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010740{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010741 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070010742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010743 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010744 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -070010745 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
10746 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010747 /*
10748 * Do not disable Probe Request reporting that was
10749 * enabled in nl80211_setup_ap().
10750 */
10751 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
10752 "Probe Request reporting nl_preq=%p while "
10753 "in AP mode", bss->nl_preq);
10754 } else if (bss->nl_preq) {
10755 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
10756 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010757 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010758 }
10759 return 0;
10760 }
10761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010762 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010763 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010764 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010765 return 0;
10766 }
10767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010768 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
10769 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010770 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010771 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
10772 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010774 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010775 (WLAN_FC_TYPE_MGMT << 2) |
10776 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010777 NULL, 0) < 0)
10778 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070010779
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070010780 nl80211_register_eloop_read(&bss->nl_preq,
10781 wpa_driver_nl80211_event_receive,
10782 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010783
10784 return 0;
10785
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010786 out_err:
10787 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010788 return -1;
10789}
10790
10791
10792static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
10793 int ifindex, int disabled)
10794{
10795 struct nl_msg *msg;
10796 struct nlattr *bands, *band;
10797 int ret;
10798
10799 msg = nlmsg_alloc();
10800 if (!msg)
10801 return -1;
10802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010803 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010804 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
10805
10806 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
10807 if (!bands)
10808 goto nla_put_failure;
10809
10810 /*
10811 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
10812 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
10813 * rates. All 5 GHz rates are left enabled.
10814 */
10815 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
10816 if (!band)
10817 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010818 if (disabled) {
10819 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
10820 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
10821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010822 nla_nest_end(msg, band);
10823
10824 nla_nest_end(msg, bands);
10825
10826 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10827 msg = NULL;
10828 if (ret) {
10829 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
10830 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010831 } else
10832 drv->disabled_11b_rates = disabled;
10833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010834 return ret;
10835
10836nla_put_failure:
10837 nlmsg_free(msg);
10838 return -1;
10839}
10840
10841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010842static int wpa_driver_nl80211_deinit_ap(void *priv)
10843{
10844 struct i802_bss *bss = priv;
10845 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010846 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010847 return -1;
10848 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010849
10850 /*
10851 * If the P2P GO interface was dynamically added, then it is
10852 * possible that the interface change to station is not possible.
10853 */
10854 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
10855 return 0;
10856
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010857 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010858}
10859
10860
Dmitry Shmidtea69e842013-05-13 14:52:28 -070010861static int wpa_driver_nl80211_stop_ap(void *priv)
10862{
10863 struct i802_bss *bss = priv;
10864 struct wpa_driver_nl80211_data *drv = bss->drv;
10865 if (!is_ap_interface(drv->nlmode))
10866 return -1;
10867 wpa_driver_nl80211_del_beacon(drv);
10868 bss->beacon_set = 0;
10869 return 0;
10870}
10871
10872
Dmitry Shmidt04949592012-07-19 12:16:46 -070010873static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
10874{
10875 struct i802_bss *bss = priv;
10876 struct wpa_driver_nl80211_data *drv = bss->drv;
10877 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
10878 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070010879
10880 /*
10881 * If the P2P Client interface was dynamically added, then it is
10882 * possible that the interface change to station is not possible.
10883 */
10884 if (bss->if_dynamic)
10885 return 0;
10886
Dmitry Shmidt04949592012-07-19 12:16:46 -070010887 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
10888}
10889
10890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010891static void wpa_driver_nl80211_resume(void *priv)
10892{
10893 struct i802_bss *bss = priv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010894
10895 if (i802_set_iface_flags(bss, 1))
10896 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010897}
10898
10899
10900static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
10901 const u8 *ies, size_t ies_len)
10902{
10903 struct i802_bss *bss = priv;
10904 struct wpa_driver_nl80211_data *drv = bss->drv;
10905 int ret;
10906 u8 *data, *pos;
10907 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010908 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010909
10910 if (action != 1) {
10911 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
10912 "action %d", action);
10913 return -1;
10914 }
10915
10916 /*
10917 * Action frame payload:
10918 * Category[1] = 6 (Fast BSS Transition)
10919 * Action[1] = 1 (Fast BSS Transition Request)
10920 * STA Address
10921 * Target AP Address
10922 * FT IEs
10923 */
10924
10925 data_len = 2 + 2 * ETH_ALEN + ies_len;
10926 data = os_malloc(data_len);
10927 if (data == NULL)
10928 return -1;
10929 pos = data;
10930 *pos++ = 0x06; /* FT Action category */
10931 *pos++ = action;
10932 os_memcpy(pos, own_addr, ETH_ALEN);
10933 pos += ETH_ALEN;
10934 os_memcpy(pos, target_ap, ETH_ALEN);
10935 pos += ETH_ALEN;
10936 os_memcpy(pos, ies, ies_len);
10937
10938 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
10939 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010940 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010941 os_free(data);
10942
10943 return ret;
10944}
10945
10946
10947static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
10948{
10949 struct i802_bss *bss = priv;
10950 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010951 struct nl_msg *msg;
10952 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010953 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010954
10955 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
10956 "hysteresis=%d", threshold, hysteresis);
10957
10958 msg = nlmsg_alloc();
10959 if (!msg)
10960 return -1;
10961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080010962 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010963
10964 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
10965
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010966 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010967 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010968 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010969
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070010970 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
10971 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
10972 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010973
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010974 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010975 msg = NULL;
10976
10977nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010978 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070010979 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070010980}
10981
10982
Dmitry Shmidt34af3062013-07-11 10:46:32 -070010983static int get_channel_width(struct nl_msg *msg, void *arg)
10984{
10985 struct nlattr *tb[NL80211_ATTR_MAX + 1];
10986 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
10987 struct wpa_signal_info *sig_change = arg;
10988
10989 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
10990 genlmsg_attrlen(gnlh, 0), NULL);
10991
10992 sig_change->center_frq1 = -1;
10993 sig_change->center_frq2 = -1;
10994 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
10995
10996 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
10997 sig_change->chanwidth = convert2width(
10998 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
10999 if (tb[NL80211_ATTR_CENTER_FREQ1])
11000 sig_change->center_frq1 =
11001 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
11002 if (tb[NL80211_ATTR_CENTER_FREQ2])
11003 sig_change->center_frq2 =
11004 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
11005 }
11006
11007 return NL_SKIP;
11008}
11009
11010
11011static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
11012 struct wpa_signal_info *sig)
11013{
11014 struct nl_msg *msg;
11015
11016 msg = nlmsg_alloc();
11017 if (!msg)
11018 return -ENOMEM;
11019
11020 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
11021 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11022
11023 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
11024
11025nla_put_failure:
11026 nlmsg_free(msg);
11027 return -ENOBUFS;
11028}
11029
11030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011031static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
11032{
11033 struct i802_bss *bss = priv;
11034 struct wpa_driver_nl80211_data *drv = bss->drv;
11035 int res;
11036
11037 os_memset(si, 0, sizeof(*si));
11038 res = nl80211_get_link_signal(drv, si);
11039 if (res != 0)
11040 return res;
11041
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011042 res = nl80211_get_channel_width(drv, si);
11043 if (res != 0)
11044 return res;
11045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011046 return nl80211_get_link_noise(drv, si);
11047}
11048
11049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011050static int wpa_driver_nl80211_shared_freq(void *priv)
11051{
11052 struct i802_bss *bss = priv;
11053 struct wpa_driver_nl80211_data *drv = bss->drv;
11054 struct wpa_driver_nl80211_data *driver;
11055 int freq = 0;
11056
11057 /*
11058 * If the same PHY is in connected state with some other interface,
11059 * then retrieve the assoc freq.
11060 */
11061 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
11062 drv->phyname);
11063
11064 dl_list_for_each(driver, &drv->global->interfaces,
11065 struct wpa_driver_nl80211_data, list) {
11066 if (drv == driver ||
11067 os_strcmp(drv->phyname, driver->phyname) != 0 ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011068 !driver->associated)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011069 continue;
11070
11071 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
11072 MACSTR,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011073 driver->phyname, driver->first_bss->ifname,
11074 MAC2STR(driver->first_bss->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -070011075 if (is_ap_interface(driver->nlmode))
Dmitry Shmidtcce06662013-11-04 18:44:24 -080011076 freq = driver->first_bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011077 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011078 freq = nl80211_get_assoc_freq(driver);
11079 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
11080 drv->phyname, freq);
11081 }
11082
11083 if (!freq)
11084 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
11085 "PHY (%s) in associated state", drv->phyname);
11086
11087 return freq;
11088}
11089
11090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011091static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
11092 int encrypt)
11093{
11094 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011095 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
11096 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011097}
11098
11099
11100static int nl80211_set_param(void *priv, const char *param)
11101{
11102 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
11103 if (param == NULL)
11104 return 0;
11105
11106#ifdef CONFIG_P2P
11107 if (os_strstr(param, "use_p2p_group_interface=1")) {
11108 struct i802_bss *bss = priv;
11109 struct wpa_driver_nl80211_data *drv = bss->drv;
11110
11111 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
11112 "interface");
11113 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
11114 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
11115 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -070011116
11117 if (os_strstr(param, "p2p_device=1")) {
11118 struct i802_bss *bss = priv;
11119 struct wpa_driver_nl80211_data *drv = bss->drv;
11120 drv->allow_p2p_device = 1;
11121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011122#endif /* CONFIG_P2P */
11123
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011124 if (os_strstr(param, "use_monitor=1")) {
11125 struct i802_bss *bss = priv;
11126 struct wpa_driver_nl80211_data *drv = bss->drv;
11127 drv->use_monitor = 1;
11128 }
11129
11130 if (os_strstr(param, "force_connect_cmd=1")) {
11131 struct i802_bss *bss = priv;
11132 struct wpa_driver_nl80211_data *drv = bss->drv;
11133 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011134 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080011135 }
11136
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -080011137 if (os_strstr(param, "no_offchannel_tx=1")) {
11138 struct i802_bss *bss = priv;
11139 struct wpa_driver_nl80211_data *drv = bss->drv;
11140 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
11141 drv->test_use_roc_tx = 1;
11142 }
11143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011144 return 0;
11145}
11146
11147
11148static void * nl80211_global_init(void)
11149{
11150 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011151 struct netlink_config *cfg;
11152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011153 global = os_zalloc(sizeof(*global));
11154 if (global == NULL)
11155 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011156 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011157 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011158 global->if_add_ifindex = -1;
11159
11160 cfg = os_zalloc(sizeof(*cfg));
11161 if (cfg == NULL)
11162 goto err;
11163
11164 cfg->ctx = global;
11165 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
11166 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
11167 global->netlink = netlink_init(cfg);
11168 if (global->netlink == NULL) {
11169 os_free(cfg);
11170 goto err;
11171 }
11172
11173 if (wpa_driver_nl80211_init_nl_global(global) < 0)
11174 goto err;
11175
11176 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
11177 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011178 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
11179 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011180 goto err;
11181 }
11182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011183 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011184
11185err:
11186 nl80211_global_deinit(global);
11187 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011188}
11189
11190
11191static void nl80211_global_deinit(void *priv)
11192{
11193 struct nl80211_global *global = priv;
11194 if (global == NULL)
11195 return;
11196 if (!dl_list_empty(&global->interfaces)) {
11197 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
11198 "nl80211_global_deinit",
11199 dl_list_len(&global->interfaces));
11200 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011201
11202 if (global->netlink)
11203 netlink_deinit(global->netlink);
11204
11205 nl_destroy_handles(&global->nl);
11206
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011207 if (global->nl_event)
11208 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011209
11210 nl_cb_put(global->nl_cb);
11211
11212 if (global->ioctl_sock >= 0)
11213 close(global->ioctl_sock);
11214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011215 os_free(global);
11216}
11217
11218
11219static const char * nl80211_get_radio_name(void *priv)
11220{
11221 struct i802_bss *bss = priv;
11222 struct wpa_driver_nl80211_data *drv = bss->drv;
11223 return drv->phyname;
11224}
11225
11226
Jouni Malinen75ecf522011-06-27 15:19:46 -070011227static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
11228 const u8 *pmkid)
11229{
11230 struct nl_msg *msg;
11231
11232 msg = nlmsg_alloc();
11233 if (!msg)
11234 return -ENOMEM;
11235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011236 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011237
11238 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
11239 if (pmkid)
11240 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
11241 if (bssid)
11242 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
11243
11244 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11245 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011246 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -070011247 return -ENOBUFS;
11248}
11249
11250
11251static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11252{
11253 struct i802_bss *bss = priv;
11254 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
11255 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
11256}
11257
11258
11259static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
11260{
11261 struct i802_bss *bss = priv;
11262 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
11263 MAC2STR(bssid));
11264 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
11265}
11266
11267
11268static int nl80211_flush_pmkid(void *priv)
11269{
11270 struct i802_bss *bss = priv;
11271 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
11272 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
11273}
11274
11275
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011276static void clean_survey_results(struct survey_results *survey_results)
11277{
11278 struct freq_survey *survey, *tmp;
11279
11280 if (dl_list_empty(&survey_results->survey_list))
11281 return;
11282
11283 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
11284 struct freq_survey, list) {
11285 dl_list_del(&survey->list);
11286 os_free(survey);
11287 }
11288}
11289
11290
11291static void add_survey(struct nlattr **sinfo, u32 ifidx,
11292 struct dl_list *survey_list)
11293{
11294 struct freq_survey *survey;
11295
11296 survey = os_zalloc(sizeof(struct freq_survey));
11297 if (!survey)
11298 return;
11299
11300 survey->ifidx = ifidx;
11301 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11302 survey->filled = 0;
11303
11304 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
11305 survey->nf = (int8_t)
11306 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
11307 survey->filled |= SURVEY_HAS_NF;
11308 }
11309
11310 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
11311 survey->channel_time =
11312 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
11313 survey->filled |= SURVEY_HAS_CHAN_TIME;
11314 }
11315
11316 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
11317 survey->channel_time_busy =
11318 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
11319 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
11320 }
11321
11322 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
11323 survey->channel_time_rx =
11324 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
11325 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
11326 }
11327
11328 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
11329 survey->channel_time_tx =
11330 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
11331 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
11332 }
11333
11334 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)",
11335 survey->freq,
11336 survey->nf,
11337 (unsigned long int) survey->channel_time,
11338 (unsigned long int) survey->channel_time_busy,
11339 (unsigned long int) survey->channel_time_tx,
11340 (unsigned long int) survey->channel_time_rx,
11341 survey->filled);
11342
11343 dl_list_add_tail(survey_list, &survey->list);
11344}
11345
11346
11347static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
11348 unsigned int freq_filter)
11349{
11350 if (!freq_filter)
11351 return 1;
11352
11353 return freq_filter == surveyed_freq;
11354}
11355
11356
11357static int survey_handler(struct nl_msg *msg, void *arg)
11358{
11359 struct nlattr *tb[NL80211_ATTR_MAX + 1];
11360 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
11361 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
11362 struct survey_results *survey_results;
11363 u32 surveyed_freq = 0;
11364 u32 ifidx;
11365
11366 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
11367 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
11368 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
11369 };
11370
11371 survey_results = (struct survey_results *) arg;
11372
11373 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
11374 genlmsg_attrlen(gnlh, 0), NULL);
11375
Dmitry Shmidt97672262014-02-03 13:02:54 -080011376 if (!tb[NL80211_ATTR_IFINDEX])
11377 return NL_SKIP;
11378
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070011379 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
11380
11381 if (!tb[NL80211_ATTR_SURVEY_INFO])
11382 return NL_SKIP;
11383
11384 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
11385 tb[NL80211_ATTR_SURVEY_INFO],
11386 survey_policy))
11387 return NL_SKIP;
11388
11389 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
11390 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
11391 return NL_SKIP;
11392 }
11393
11394 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
11395
11396 if (!check_survey_ok(sinfo, surveyed_freq,
11397 survey_results->freq_filter))
11398 return NL_SKIP;
11399
11400 if (survey_results->freq_filter &&
11401 survey_results->freq_filter != surveyed_freq) {
11402 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
11403 surveyed_freq);
11404 return NL_SKIP;
11405 }
11406
11407 add_survey(sinfo, ifidx, &survey_results->survey_list);
11408
11409 return NL_SKIP;
11410}
11411
11412
11413static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
11414{
11415 struct i802_bss *bss = priv;
11416 struct wpa_driver_nl80211_data *drv = bss->drv;
11417 struct nl_msg *msg;
11418 int err = -ENOBUFS;
11419 union wpa_event_data data;
11420 struct survey_results *survey_results;
11421
11422 os_memset(&data, 0, sizeof(data));
11423 survey_results = &data.survey_results;
11424
11425 dl_list_init(&survey_results->survey_list);
11426
11427 msg = nlmsg_alloc();
11428 if (!msg)
11429 goto nla_put_failure;
11430
11431 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
11432
11433 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11434
11435 if (freq)
11436 data.survey_results.freq_filter = freq;
11437
11438 do {
11439 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
11440 err = send_and_recv_msgs(drv, msg, survey_handler,
11441 survey_results);
11442 } while (err > 0);
11443
11444 if (err) {
11445 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
11446 goto out_clean;
11447 }
11448
11449 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
11450
11451out_clean:
11452 clean_survey_results(survey_results);
11453nla_put_failure:
11454 return err;
11455}
11456
11457
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011458static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
11459 const u8 *replay_ctr)
11460{
11461 struct i802_bss *bss = priv;
11462 struct wpa_driver_nl80211_data *drv = bss->drv;
11463 struct nlattr *replay_nested;
11464 struct nl_msg *msg;
11465
11466 msg = nlmsg_alloc();
11467 if (!msg)
11468 return;
11469
11470 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11471
11472 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11473
11474 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11475 if (!replay_nested)
11476 goto nla_put_failure;
11477
11478 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
11479 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
11480 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
11481 replay_ctr);
11482
11483 nla_nest_end(msg, replay_nested);
11484
11485 send_and_recv_msgs(drv, msg, NULL, NULL);
11486 return;
11487 nla_put_failure:
11488 nlmsg_free(msg);
11489}
11490
11491
11492static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
11493 const u8 *addr, int qos)
11494{
11495 /* send data frame to poll STA and check whether
11496 * this frame is ACKed */
11497 struct {
11498 struct ieee80211_hdr hdr;
11499 u16 qos_ctl;
11500 } STRUCT_PACKED nulldata;
11501 size_t size;
11502
11503 /* Send data frame to poll STA and check whether this frame is ACKed */
11504
11505 os_memset(&nulldata, 0, sizeof(nulldata));
11506
11507 if (qos) {
11508 nulldata.hdr.frame_control =
11509 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11510 WLAN_FC_STYPE_QOS_NULL);
11511 size = sizeof(nulldata);
11512 } else {
11513 nulldata.hdr.frame_control =
11514 IEEE80211_FC(WLAN_FC_TYPE_DATA,
11515 WLAN_FC_STYPE_NULLFUNC);
11516 size = sizeof(struct ieee80211_hdr);
11517 }
11518
11519 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
11520 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
11521 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
11522 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
11523
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011524 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
11525 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011526 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
11527 "send poll frame");
11528}
11529
11530static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
11531 int qos)
11532{
11533 struct i802_bss *bss = priv;
11534 struct wpa_driver_nl80211_data *drv = bss->drv;
11535 struct nl_msg *msg;
11536
11537 if (!drv->poll_command_supported) {
11538 nl80211_send_null_frame(bss, own_addr, addr, qos);
11539 return;
11540 }
11541
11542 msg = nlmsg_alloc();
11543 if (!msg)
11544 return;
11545
11546 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
11547
11548 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11549 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
11550
11551 send_and_recv_msgs(drv, msg, NULL, NULL);
11552 return;
11553 nla_put_failure:
11554 nlmsg_free(msg);
11555}
11556
11557
11558static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
11559{
11560 struct nl_msg *msg;
11561
11562 msg = nlmsg_alloc();
11563 if (!msg)
11564 return -ENOMEM;
11565
11566 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
11567 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
11568 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
11569 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
11570 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
11571nla_put_failure:
11572 nlmsg_free(msg);
11573 return -ENOBUFS;
11574}
11575
11576
11577static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
11578 int ctwindow)
11579{
11580 struct i802_bss *bss = priv;
11581
11582 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
11583 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
11584
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011585 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080011586#ifdef ANDROID_P2P
11587 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011588#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011589 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080011590#endif /* ANDROID_P2P */
11591 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011592
11593 if (legacy_ps == -1)
11594 return 0;
11595 if (legacy_ps != 0 && legacy_ps != 1)
11596 return -1; /* Not yet supported */
11597
11598 return nl80211_set_power_save(bss, legacy_ps);
11599}
11600
11601
Dmitry Shmidt051af732013-10-22 13:52:46 -070011602static int nl80211_start_radar_detection(void *priv,
11603 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011604{
11605 struct i802_bss *bss = priv;
11606 struct wpa_driver_nl80211_data *drv = bss->drv;
11607 struct nl_msg *msg;
11608 int ret;
11609
Dmitry Shmidt051af732013-10-22 13:52:46 -070011610 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)",
11611 freq->freq, freq->ht_enabled, freq->vht_enabled,
11612 freq->bandwidth, freq->center_freq1, freq->center_freq2);
11613
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011614 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
11615 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
11616 "detection");
11617 return -1;
11618 }
11619
11620 msg = nlmsg_alloc();
11621 if (!msg)
11622 return -1;
11623
11624 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
11625 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011626
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011627 if (nl80211_put_freq_params(msg, freq) < 0)
11628 goto nla_put_failure;
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011629
11630 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011631 msg = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011632 if (ret == 0)
11633 return 0;
11634 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
11635 "%d (%s)", ret, strerror(-ret));
11636nla_put_failure:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070011637 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -070011638 return -1;
11639}
11640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011641#ifdef CONFIG_TDLS
11642
11643static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
11644 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070011645 u32 peer_capab, int initiator, const u8 *buf,
11646 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011647{
11648 struct i802_bss *bss = priv;
11649 struct wpa_driver_nl80211_data *drv = bss->drv;
11650 struct nl_msg *msg;
11651
11652 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11653 return -EOPNOTSUPP;
11654
11655 if (!dst)
11656 return -EINVAL;
11657
11658 msg = nlmsg_alloc();
11659 if (!msg)
11660 return -ENOMEM;
11661
11662 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
11663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11664 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
11665 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
11666 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
11667 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070011668 if (peer_capab) {
11669 /*
11670 * The internal enum tdls_peer_capability definition is
11671 * currently identical with the nl80211 enum
11672 * nl80211_tdls_peer_capability, so no conversion is needed
11673 * here.
11674 */
11675 NLA_PUT_U32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, peer_capab);
11676 }
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070011677 if (initiator)
11678 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_INITIATOR);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011679 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
11680
11681 return send_and_recv_msgs(drv, msg, NULL, NULL);
11682
11683nla_put_failure:
11684 nlmsg_free(msg);
11685 return -ENOBUFS;
11686}
11687
11688
11689static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
11690{
11691 struct i802_bss *bss = priv;
11692 struct wpa_driver_nl80211_data *drv = bss->drv;
11693 struct nl_msg *msg;
11694 enum nl80211_tdls_operation nl80211_oper;
11695
11696 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
11697 return -EOPNOTSUPP;
11698
11699 switch (oper) {
11700 case TDLS_DISCOVERY_REQ:
11701 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
11702 break;
11703 case TDLS_SETUP:
11704 nl80211_oper = NL80211_TDLS_SETUP;
11705 break;
11706 case TDLS_TEARDOWN:
11707 nl80211_oper = NL80211_TDLS_TEARDOWN;
11708 break;
11709 case TDLS_ENABLE_LINK:
11710 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
11711 break;
11712 case TDLS_DISABLE_LINK:
11713 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
11714 break;
11715 case TDLS_ENABLE:
11716 return 0;
11717 case TDLS_DISABLE:
11718 return 0;
11719 default:
11720 return -EINVAL;
11721 }
11722
11723 msg = nlmsg_alloc();
11724 if (!msg)
11725 return -ENOMEM;
11726
11727 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
11728 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
11729 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11730 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
11731
11732 return send_and_recv_msgs(drv, msg, NULL, NULL);
11733
11734nla_put_failure:
11735 nlmsg_free(msg);
11736 return -ENOBUFS;
11737}
11738
11739#endif /* CONFIG TDLS */
11740
11741
11742#ifdef ANDROID
11743
11744typedef struct android_wifi_priv_cmd {
11745 char *buf;
11746 int used_len;
11747 int total_len;
11748} android_wifi_priv_cmd;
11749
11750static int drv_errors = 0;
11751
11752static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
11753{
11754 drv_errors++;
11755 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
11756 drv_errors = 0;
11757 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
11758 }
11759}
11760
11761
11762static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
11763{
11764 struct wpa_driver_nl80211_data *drv = bss->drv;
11765 struct ifreq ifr;
11766 android_wifi_priv_cmd priv_cmd;
11767 char buf[MAX_DRV_CMD_SIZE];
11768 int ret;
11769
11770 os_memset(&ifr, 0, sizeof(ifr));
11771 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
11772 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
11773
11774 os_memset(buf, 0, sizeof(buf));
11775 os_strlcpy(buf, cmd, sizeof(buf));
11776
11777 priv_cmd.buf = buf;
11778 priv_cmd.used_len = sizeof(buf);
11779 priv_cmd.total_len = sizeof(buf);
11780 ifr.ifr_data = &priv_cmd;
11781
11782 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11783 if (ret < 0) {
11784 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
11785 __func__);
11786 wpa_driver_send_hang_msg(drv);
11787 return ret;
11788 }
11789
11790 drv_errors = 0;
11791 return 0;
11792}
11793
11794
11795static int android_pno_start(struct i802_bss *bss,
11796 struct wpa_driver_scan_params *params)
11797{
11798 struct wpa_driver_nl80211_data *drv = bss->drv;
11799 struct ifreq ifr;
11800 android_wifi_priv_cmd priv_cmd;
11801 int ret = 0, i = 0, bp;
11802 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
11803
11804 bp = WEXT_PNOSETUP_HEADER_SIZE;
11805 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
11806 buf[bp++] = WEXT_PNO_TLV_PREFIX;
11807 buf[bp++] = WEXT_PNO_TLV_VERSION;
11808 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
11809 buf[bp++] = WEXT_PNO_TLV_RESERVED;
11810
11811 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
11812 /* Check that there is enough space needed for 1 more SSID, the
11813 * other sections and null termination */
11814 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
11815 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
11816 break;
11817 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
11818 params->ssids[i].ssid,
11819 params->ssids[i].ssid_len);
11820 buf[bp++] = WEXT_PNO_SSID_SECTION;
11821 buf[bp++] = params->ssids[i].ssid_len;
11822 os_memcpy(&buf[bp], params->ssids[i].ssid,
11823 params->ssids[i].ssid_len);
11824 bp += params->ssids[i].ssid_len;
11825 i++;
11826 }
11827
11828 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
11829 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
11830 WEXT_PNO_SCAN_INTERVAL);
11831 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
11832
11833 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
11834 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
11835 WEXT_PNO_REPEAT);
11836 bp += WEXT_PNO_REPEAT_LENGTH;
11837
11838 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
11839 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
11840 WEXT_PNO_MAX_REPEAT);
11841 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
11842
11843 memset(&ifr, 0, sizeof(ifr));
11844 memset(&priv_cmd, 0, sizeof(priv_cmd));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070011845 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011846
11847 priv_cmd.buf = buf;
11848 priv_cmd.used_len = bp;
11849 priv_cmd.total_len = bp;
11850 ifr.ifr_data = &priv_cmd;
11851
11852 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
11853
11854 if (ret < 0) {
11855 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
11856 ret);
11857 wpa_driver_send_hang_msg(drv);
11858 return ret;
11859 }
11860
11861 drv_errors = 0;
11862
11863 return android_priv_cmd(bss, "PNOFORCE 1");
11864}
11865
11866
11867static int android_pno_stop(struct i802_bss *bss)
11868{
11869 return android_priv_cmd(bss, "PNOFORCE 0");
11870}
11871
11872#endif /* ANDROID */
11873
11874
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011875static int driver_nl80211_set_key(const char *ifname, void *priv,
11876 enum wpa_alg alg, const u8 *addr,
11877 int key_idx, int set_tx,
11878 const u8 *seq, size_t seq_len,
11879 const u8 *key, size_t key_len)
11880{
11881 struct i802_bss *bss = priv;
11882 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
11883 set_tx, seq, seq_len, key, key_len);
11884}
11885
11886
11887static int driver_nl80211_scan2(void *priv,
11888 struct wpa_driver_scan_params *params)
11889{
11890 struct i802_bss *bss = priv;
11891 return wpa_driver_nl80211_scan(bss, params);
11892}
11893
11894
11895static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
11896 int reason_code)
11897{
11898 struct i802_bss *bss = priv;
11899 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
11900}
11901
11902
11903static int driver_nl80211_authenticate(void *priv,
11904 struct wpa_driver_auth_params *params)
11905{
11906 struct i802_bss *bss = priv;
11907 return wpa_driver_nl80211_authenticate(bss, params);
11908}
11909
11910
11911static void driver_nl80211_deinit(void *priv)
11912{
11913 struct i802_bss *bss = priv;
11914 wpa_driver_nl80211_deinit(bss);
11915}
11916
11917
11918static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
11919 const char *ifname)
11920{
11921 struct i802_bss *bss = priv;
11922 return wpa_driver_nl80211_if_remove(bss, type, ifname);
11923}
11924
11925
11926static int driver_nl80211_send_mlme(void *priv, const u8 *data,
11927 size_t data_len, int noack)
11928{
11929 struct i802_bss *bss = priv;
11930 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
11931 0, 0, 0, 0);
11932}
11933
11934
11935static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
11936{
11937 struct i802_bss *bss = priv;
11938 return wpa_driver_nl80211_sta_remove(bss, addr);
11939}
11940
11941
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011942static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
11943 const char *ifname, int vlan_id)
11944{
11945 struct i802_bss *bss = priv;
11946 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
11947}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080011948
11949
11950static int driver_nl80211_read_sta_data(void *priv,
11951 struct hostap_sta_driver_data *data,
11952 const u8 *addr)
11953{
11954 struct i802_bss *bss = priv;
11955 return i802_read_sta_data(bss, data, addr);
11956}
11957
11958
11959static int driver_nl80211_send_action(void *priv, unsigned int freq,
11960 unsigned int wait_time,
11961 const u8 *dst, const u8 *src,
11962 const u8 *bssid,
11963 const u8 *data, size_t data_len,
11964 int no_cck)
11965{
11966 struct i802_bss *bss = priv;
11967 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
11968 bssid, data, data_len, no_cck);
11969}
11970
11971
11972static int driver_nl80211_probe_req_report(void *priv, int report)
11973{
11974 struct i802_bss *bss = priv;
11975 return wpa_driver_nl80211_probe_req_report(bss, report);
11976}
11977
11978
Dmitry Shmidt700a1372013-03-15 14:14:44 -070011979static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
11980 const u8 *ies, size_t ies_len)
11981{
11982 int ret;
11983 struct nl_msg *msg;
11984 struct i802_bss *bss = priv;
11985 struct wpa_driver_nl80211_data *drv = bss->drv;
11986 u16 mdid = WPA_GET_LE16(md);
11987
11988 msg = nlmsg_alloc();
11989 if (!msg)
11990 return -ENOMEM;
11991
11992 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
11993 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
11994 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
11995 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
11996 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
11997
11998 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
11999 if (ret) {
12000 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
12001 "err=%d (%s)", ret, strerror(-ret));
12002 }
12003
12004 return ret;
12005
12006nla_put_failure:
12007 nlmsg_free(msg);
12008 return -ENOBUFS;
12009}
12010
12011
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012012const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
12013{
12014 struct i802_bss *bss = priv;
12015 struct wpa_driver_nl80211_data *drv = bss->drv;
12016
12017 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
12018 return NULL;
12019
12020 return bss->addr;
12021}
12022
12023
Dmitry Shmidt56052862013-10-04 10:23:25 -070012024static const char * scan_state_str(enum scan_states scan_state)
12025{
12026 switch (scan_state) {
12027 case NO_SCAN:
12028 return "NO_SCAN";
12029 case SCAN_REQUESTED:
12030 return "SCAN_REQUESTED";
12031 case SCAN_STARTED:
12032 return "SCAN_STARTED";
12033 case SCAN_COMPLETED:
12034 return "SCAN_COMPLETED";
12035 case SCAN_ABORTED:
12036 return "SCAN_ABORTED";
12037 case SCHED_SCAN_STARTED:
12038 return "SCHED_SCAN_STARTED";
12039 case SCHED_SCAN_STOPPED:
12040 return "SCHED_SCAN_STOPPED";
12041 case SCHED_SCAN_RESULTS:
12042 return "SCHED_SCAN_RESULTS";
12043 }
12044
12045 return "??";
12046}
12047
12048
12049static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
12050{
12051 struct i802_bss *bss = priv;
12052 struct wpa_driver_nl80211_data *drv = bss->drv;
12053 int res;
12054 char *pos, *end;
12055
12056 pos = buf;
12057 end = buf + buflen;
12058
12059 res = os_snprintf(pos, end - pos,
12060 "ifindex=%d\n"
12061 "ifname=%s\n"
12062 "brname=%s\n"
12063 "addr=" MACSTR "\n"
12064 "freq=%d\n"
12065 "%s%s%s%s%s",
12066 bss->ifindex,
12067 bss->ifname,
12068 bss->brname,
12069 MAC2STR(bss->addr),
12070 bss->freq,
12071 bss->beacon_set ? "beacon_set=1\n" : "",
12072 bss->added_if_into_bridge ?
12073 "added_if_into_bridge=1\n" : "",
12074 bss->added_bridge ? "added_bridge=1\n" : "",
12075 bss->in_deinit ? "in_deinit=1\n" : "",
12076 bss->if_dynamic ? "if_dynamic=1\n" : "");
12077 if (res < 0 || res >= end - pos)
12078 return pos - buf;
12079 pos += res;
12080
12081 if (bss->wdev_id_set) {
12082 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
12083 (unsigned long long) bss->wdev_id);
12084 if (res < 0 || res >= end - pos)
12085 return pos - buf;
12086 pos += res;
12087 }
12088
12089 res = os_snprintf(pos, end - pos,
12090 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012091 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -070012092 "drv_ifindex=%d\n"
12093 "operstate=%d\n"
12094 "scan_state=%s\n"
12095 "auth_bssid=" MACSTR "\n"
12096 "auth_attempt_bssid=" MACSTR "\n"
12097 "bssid=" MACSTR "\n"
12098 "prev_bssid=" MACSTR "\n"
12099 "associated=%d\n"
12100 "assoc_freq=%u\n"
12101 "monitor_sock=%d\n"
12102 "monitor_ifidx=%d\n"
12103 "monitor_refcount=%d\n"
12104 "last_mgmt_freq=%u\n"
12105 "eapol_tx_sock=%d\n"
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070012106 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -070012107 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012108 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -070012109 drv->ifindex,
12110 drv->operstate,
12111 scan_state_str(drv->scan_state),
12112 MAC2STR(drv->auth_bssid),
12113 MAC2STR(drv->auth_attempt_bssid),
12114 MAC2STR(drv->bssid),
12115 MAC2STR(drv->prev_bssid),
12116 drv->associated,
12117 drv->assoc_freq,
12118 drv->monitor_sock,
12119 drv->monitor_ifidx,
12120 drv->monitor_refcount,
12121 drv->last_mgmt_freq,
12122 drv->eapol_tx_sock,
12123 drv->ignore_if_down_event ?
12124 "ignore_if_down_event=1\n" : "",
12125 drv->scan_complete_events ?
12126 "scan_complete_events=1\n" : "",
12127 drv->disabled_11b_rates ?
12128 "disabled_11b_rates=1\n" : "",
12129 drv->pending_remain_on_chan ?
12130 "pending_remain_on_chan=1\n" : "",
12131 drv->in_interface_list ? "in_interface_list=1\n" : "",
12132 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
12133 drv->poll_command_supported ?
12134 "poll_command_supported=1\n" : "",
12135 drv->data_tx_status ? "data_tx_status=1\n" : "",
12136 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
12137 drv->retry_auth ? "retry_auth=1\n" : "",
12138 drv->use_monitor ? "use_monitor=1\n" : "",
12139 drv->ignore_next_local_disconnect ?
12140 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -070012141 drv->ignore_next_local_deauth ?
12142 "ignore_next_local_deauth=1\n" : "",
Dmitry Shmidt56052862013-10-04 10:23:25 -070012143 drv->allow_p2p_device ? "allow_p2p_device=1\n" : "");
12144 if (res < 0 || res >= end - pos)
12145 return pos - buf;
12146 pos += res;
12147
12148 if (drv->has_capability) {
12149 res = os_snprintf(pos, end - pos,
12150 "capa.key_mgmt=0x%x\n"
12151 "capa.enc=0x%x\n"
12152 "capa.auth=0x%x\n"
12153 "capa.flags=0x%x\n"
12154 "capa.max_scan_ssids=%d\n"
12155 "capa.max_sched_scan_ssids=%d\n"
12156 "capa.sched_scan_supported=%d\n"
12157 "capa.max_match_sets=%d\n"
12158 "capa.max_remain_on_chan=%u\n"
12159 "capa.max_stations=%u\n"
12160 "capa.probe_resp_offloads=0x%x\n"
12161 "capa.max_acl_mac_addrs=%u\n"
12162 "capa.num_multichan_concurrent=%u\n",
12163 drv->capa.key_mgmt,
12164 drv->capa.enc,
12165 drv->capa.auth,
12166 drv->capa.flags,
12167 drv->capa.max_scan_ssids,
12168 drv->capa.max_sched_scan_ssids,
12169 drv->capa.sched_scan_supported,
12170 drv->capa.max_match_sets,
12171 drv->capa.max_remain_on_chan,
12172 drv->capa.max_stations,
12173 drv->capa.probe_resp_offloads,
12174 drv->capa.max_acl_mac_addrs,
12175 drv->capa.num_multichan_concurrent);
12176 if (res < 0 || res >= end - pos)
12177 return pos - buf;
12178 pos += res;
12179 }
12180
12181 return pos - buf;
12182}
12183
12184
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012185static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
12186{
12187 if (settings->head)
12188 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD,
12189 settings->head_len, settings->head);
12190
12191 if (settings->tail)
12192 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL,
12193 settings->tail_len, settings->tail);
12194
12195 if (settings->beacon_ies)
12196 NLA_PUT(msg, NL80211_ATTR_IE,
12197 settings->beacon_ies_len, settings->beacon_ies);
12198
12199 if (settings->proberesp_ies)
12200 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
12201 settings->proberesp_ies_len, settings->proberesp_ies);
12202
12203 if (settings->assocresp_ies)
12204 NLA_PUT(msg,
12205 NL80211_ATTR_IE_ASSOC_RESP,
12206 settings->assocresp_ies_len, settings->assocresp_ies);
12207
12208 if (settings->probe_resp)
12209 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP,
12210 settings->probe_resp_len, settings->probe_resp);
12211
12212 return 0;
12213
12214nla_put_failure:
12215 return -ENOBUFS;
12216}
12217
12218
12219static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
12220{
12221 struct nl_msg *msg;
12222 struct i802_bss *bss = priv;
12223 struct wpa_driver_nl80211_data *drv = bss->drv;
12224 struct nlattr *beacon_csa;
12225 int ret = -ENOBUFS;
12226
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012227 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 -080012228 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080012229 settings->freq_params.freq, settings->freq_params.bandwidth,
12230 settings->freq_params.center_freq1,
12231 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012232
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012233 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012234 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
12235 return -EOPNOTSUPP;
12236 }
12237
12238 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
12239 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
12240 return -EOPNOTSUPP;
12241
12242 /* check settings validity */
12243 if (!settings->beacon_csa.tail ||
12244 ((settings->beacon_csa.tail_len <=
12245 settings->counter_offset_beacon) ||
12246 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
12247 settings->cs_count)))
12248 return -EINVAL;
12249
12250 if (settings->beacon_csa.probe_resp &&
12251 ((settings->beacon_csa.probe_resp_len <=
12252 settings->counter_offset_presp) ||
12253 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
12254 settings->cs_count)))
12255 return -EINVAL;
12256
12257 msg = nlmsg_alloc();
12258 if (!msg)
12259 return -ENOMEM;
12260
12261 nl80211_cmd(drv, msg, 0, NL80211_CMD_CHANNEL_SWITCH);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -070012262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012263 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, settings->cs_count);
12264 ret = nl80211_put_freq_params(msg, &settings->freq_params);
12265 if (ret)
12266 goto error;
12267
12268 if (settings->block_tx)
12269 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
12270
12271 /* beacon_after params */
12272 ret = set_beacon_data(msg, &settings->beacon_after);
12273 if (ret)
12274 goto error;
12275
12276 /* beacon_csa params */
12277 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
12278 if (!beacon_csa)
12279 goto nla_put_failure;
12280
12281 ret = set_beacon_data(msg, &settings->beacon_csa);
12282 if (ret)
12283 goto error;
12284
12285 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
12286 settings->counter_offset_beacon);
12287
12288 if (settings->beacon_csa.probe_resp)
12289 NLA_PUT_U16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
12290 settings->counter_offset_presp);
12291
12292 nla_nest_end(msg, beacon_csa);
12293 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12294 if (ret) {
12295 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
12296 ret, strerror(-ret));
12297 }
12298 return ret;
12299
12300nla_put_failure:
12301 ret = -ENOBUFS;
12302error:
12303 nlmsg_free(msg);
12304 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
12305 return ret;
12306}
12307
12308
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012309#ifdef CONFIG_TESTING_OPTIONS
12310static int cmd_reply_handler(struct nl_msg *msg, void *arg)
12311{
12312 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12313 struct wpabuf *buf = arg;
12314
12315 if (!buf)
12316 return NL_SKIP;
12317
12318 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
12319 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
12320 return NL_SKIP;
12321 }
12322
12323 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
12324 genlmsg_attrlen(gnlh, 0));
12325
12326 return NL_SKIP;
12327}
12328#endif /* CONFIG_TESTING_OPTIONS */
12329
12330
12331static int vendor_reply_handler(struct nl_msg *msg, void *arg)
12332{
12333 struct nlattr *tb[NL80211_ATTR_MAX + 1];
12334 struct nlattr *nl_vendor_reply, *nl;
12335 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
12336 struct wpabuf *buf = arg;
12337 int rem;
12338
12339 if (!buf)
12340 return NL_SKIP;
12341
12342 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
12343 genlmsg_attrlen(gnlh, 0), NULL);
12344 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
12345
12346 if (!nl_vendor_reply)
12347 return NL_SKIP;
12348
12349 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
12350 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
12351 return NL_SKIP;
12352 }
12353
12354 nla_for_each_nested(nl, nl_vendor_reply, rem) {
12355 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
12356 }
12357
12358 return NL_SKIP;
12359}
12360
12361
12362static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
12363 unsigned int subcmd, const u8 *data,
12364 size_t data_len, struct wpabuf *buf)
12365{
12366 struct i802_bss *bss = priv;
12367 struct wpa_driver_nl80211_data *drv = bss->drv;
12368 struct nl_msg *msg;
12369 int ret;
12370
12371 msg = nlmsg_alloc();
12372 if (!msg)
12373 return -ENOMEM;
12374
12375#ifdef CONFIG_TESTING_OPTIONS
12376 if (vendor_id == 0xffffffff) {
12377 nl80211_cmd(drv, msg, 0, subcmd);
12378 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
12379 0)
12380 goto nla_put_failure;
12381 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
12382 if (ret)
12383 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
12384 ret);
12385 return ret;
12386 }
12387#endif /* CONFIG_TESTING_OPTIONS */
12388
12389 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12390 if (nl80211_set_iface_id(msg, bss) < 0)
12391 goto nla_put_failure;
12392 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, vendor_id);
12393 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
12394 if (data)
12395 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, data_len, data);
12396
12397 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
12398 if (ret)
12399 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
12400 ret);
12401 return ret;
12402
12403nla_put_failure:
12404 nlmsg_free(msg);
12405 return -ENOBUFS;
12406}
12407
12408
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012409static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
12410 u8 qos_map_set_len)
12411{
12412 struct i802_bss *bss = priv;
12413 struct wpa_driver_nl80211_data *drv = bss->drv;
12414 struct nl_msg *msg;
12415 int ret;
12416
12417 msg = nlmsg_alloc();
12418 if (!msg)
12419 return -ENOMEM;
12420
12421 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
12422 qos_map_set, qos_map_set_len);
12423
12424 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_QOS_MAP);
12425 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12426 NLA_PUT(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set);
12427
12428 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12429 if (ret)
12430 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
12431
12432 return ret;
12433
12434nla_put_failure:
12435 nlmsg_free(msg);
12436 return -ENOBUFS;
12437}
12438
12439
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012440static int nl80211_set_wowlan(void *priv,
12441 const struct wowlan_triggers *triggers)
12442{
12443 struct i802_bss *bss = priv;
12444 struct wpa_driver_nl80211_data *drv = bss->drv;
12445 struct nl_msg *msg;
12446 struct nlattr *wowlan_triggers;
12447 int ret;
12448
12449 msg = nlmsg_alloc();
12450 if (!msg)
12451 return -ENOMEM;
12452
12453 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
12454
12455 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WOWLAN);
12456 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12457
12458 wowlan_triggers = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
12459 if (!wowlan_triggers)
12460 goto nla_put_failure;
12461
12462 if (triggers->any)
12463 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
12464 if (triggers->disconnect)
12465 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
12466 if (triggers->magic_pkt)
12467 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
12468 if (triggers->gtk_rekey_failure)
12469 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
12470 if (triggers->eap_identity_req)
12471 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
12472 if (triggers->four_way_handshake)
12473 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
12474 if (triggers->rfkill_release)
12475 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
12476
12477 nla_nest_end(msg, wowlan_triggers);
12478
12479 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
12480 if (ret)
12481 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
12482
12483 return ret;
12484
12485nla_put_failure:
12486 nlmsg_free(msg);
12487 return -ENOBUFS;
12488}
12489
12490
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012491static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
12492{
12493 struct i802_bss *bss = priv;
12494 struct wpa_driver_nl80211_data *drv = bss->drv;
12495 struct nl_msg *msg;
12496 struct nlattr *params;
12497
12498 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
12499
12500 if (!drv->roaming_vendor_cmd_avail) {
12501 wpa_printf(MSG_DEBUG,
12502 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
12503 return -1;
12504 }
12505
12506 msg = nlmsg_alloc();
12507 if (!msg)
12508 return -ENOMEM;
12509
12510 nl80211_cmd(drv, msg, 0, NL80211_CMD_VENDOR);
12511
12512 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
12513 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA);
12514 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD,
12515 QCA_NL80211_VENDOR_SUBCMD_ROAMING);
12516
12517 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
12518 if (!params)
12519 goto nla_put_failure;
12520 NLA_PUT_U32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
12521 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
12522 QCA_ROAMING_NOT_ALLOWED);
12523 if (bssid)
12524 NLA_PUT(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid);
12525 nla_nest_end(msg, params);
12526
12527 return send_and_recv_msgs(drv, msg, NULL, NULL);
12528
12529 nla_put_failure:
12530 nlmsg_free(msg);
12531 return -1;
12532}
12533
12534
12535static int nl80211_set_mac_addr(void *priv, const u8 *addr)
12536{
12537 struct i802_bss *bss = priv;
12538 struct wpa_driver_nl80211_data *drv = bss->drv;
12539 int new_addr = addr != NULL;
12540
12541 if (!addr)
12542 addr = drv->perm_addr;
12543
12544 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
12545 return -1;
12546
12547 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
12548 {
12549 wpa_printf(MSG_DEBUG,
12550 "nl80211: failed to set_mac_addr for %s to " MACSTR,
12551 bss->ifname, MAC2STR(addr));
12552 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
12553 1) < 0) {
12554 wpa_printf(MSG_DEBUG,
12555 "nl80211: Could not restore interface UP after failed set_mac_addr");
12556 }
12557 return -1;
12558 }
12559
12560 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
12561 bss->ifname, MAC2STR(addr));
12562 drv->addr_changed = new_addr;
12563 os_memcpy(bss->addr, addr, ETH_ALEN);
12564
12565 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
12566 {
12567 wpa_printf(MSG_DEBUG,
12568 "nl80211: Could not restore interface UP after set_mac_addr");
12569 }
12570
12571 return 0;
12572}
12573
12574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012575const struct wpa_driver_ops wpa_driver_nl80211_ops = {
12576 .name = "nl80211",
12577 .desc = "Linux nl80211/cfg80211",
12578 .get_bssid = wpa_driver_nl80211_get_bssid,
12579 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012580 .set_key = driver_nl80211_set_key,
12581 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012582 .sched_scan = wpa_driver_nl80211_sched_scan,
12583 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012584 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012585 .deauthenticate = driver_nl80211_deauthenticate,
12586 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012587 .associate = wpa_driver_nl80211_associate,
12588 .global_init = nl80211_global_init,
12589 .global_deinit = nl80211_global_deinit,
12590 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012591 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012592 .get_capa = wpa_driver_nl80211_get_capa,
12593 .set_operstate = wpa_driver_nl80211_set_operstate,
12594 .set_supp_port = wpa_driver_nl80211_set_supp_port,
12595 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -080012596 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012597 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -070012598 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012599 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012600 .if_remove = driver_nl80211_if_remove,
12601 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012602 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
12603 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012604 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012605 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
12606 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012607 .hapd_init = i802_init,
12608 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012609 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012610 .get_seqnum = i802_get_seqnum,
12611 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012612 .get_inact_sec = i802_get_inact_sec,
12613 .sta_clear_stats = i802_sta_clear_stats,
12614 .set_rts = i802_set_rts,
12615 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012616 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012617 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012618 .sta_deauth = i802_sta_deauth,
12619 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012620 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012621 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012622 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012623 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
12624 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
12625 .cancel_remain_on_channel =
12626 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080012627 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012628 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -070012629 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012630 .resume = wpa_driver_nl80211_resume,
12631 .send_ft_action = nl80211_send_ft_action,
12632 .signal_monitor = nl80211_signal_monitor,
12633 .signal_poll = nl80211_signal_poll,
12634 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012635 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012636 .set_param = nl80211_set_param,
12637 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -070012638 .add_pmkid = nl80211_add_pmkid,
12639 .remove_pmkid = nl80211_remove_pmkid,
12640 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012641 .set_rekey_info = nl80211_set_rekey_info,
12642 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012643 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -070012644 .start_dfs_cac = nl80211_start_radar_detection,
12645 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012646#ifdef CONFIG_TDLS
12647 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
12648 .tdls_oper = nl80211_tdls_oper,
12649#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -070012650 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -070012651 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070012652 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -070012653 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080012654 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012655#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012656 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080012657 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -070012658 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012659#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -070012660#ifdef ANDROID
12661 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -080012662#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -080012663 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012664 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -070012665 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070012666 .roaming = nl80211_roaming,
12667 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070012668};