blob: 0b5a7dc6213a92aaeaee029e6b6218c056848985 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <net/if.h>
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
22#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
24#include <linux/filter.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "nl80211_copy.h"
27
28#include "common.h"
29#include "eloop.h"
30#include "utils/list.h"
31#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032#include "common/ieee802_11_common.h"
33#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "netlink.h"
35#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
39#include "driver.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041#ifndef SO_WIFI_STATUS
42# if defined(__sparc__)
43# define SO_WIFI_STATUS 0x0025
44# elif defined(__parisc__)
45# define SO_WIFI_STATUS 0x4022
46# else
47# define SO_WIFI_STATUS 41
48# endif
49
50# define SCM_WIFI_STATUS SO_WIFI_STATUS
51#endif
52
53#ifndef SO_EE_ORIGIN_TXSTATUS
54#define SO_EE_ORIGIN_TXSTATUS 4
55#endif
56
57#ifndef PACKET_TX_TIMESTAMP
58#define PACKET_TX_TIMESTAMP 16
59#endif
60
61#ifdef ANDROID
62#include "android_drv.h"
63#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064#ifdef CONFIG_LIBNL20
65/* libnl 2.0 compatibility code */
66#define nl_handle nl_sock
67#define nl80211_handle_alloc nl_socket_alloc_cb
68#define nl80211_handle_destroy nl_socket_free
69#else
70/*
71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72 * but when you free a socket again it will mess up its bitmap and
73 * and use the wrong number the next time it needs a socket ID.
74 * Therefore, we wrap the handle alloc/destroy and add our own pid
75 * accounting.
76 */
77static uint32_t port_bitmap[32] = { 0 };
78
79static struct nl_handle *nl80211_handle_alloc(void *cb)
80{
81 struct nl_handle *handle;
82 uint32_t pid = getpid() & 0x3FFFFF;
83 int i;
84
85 handle = nl_handle_alloc_cb(cb);
86
87 for (i = 0; i < 1024; i++) {
88 if (port_bitmap[i / 32] & (1 << (i % 32)))
89 continue;
90 port_bitmap[i / 32] |= 1 << (i % 32);
91 pid += i << 22;
92 break;
93 }
94
95 nl_socket_set_local_port(handle, pid);
96
97 return handle;
98}
99
100static void nl80211_handle_destroy(struct nl_handle *handle)
101{
102 uint32_t port = nl_socket_get_local_port(handle);
103
104 port >>= 22;
105 port_bitmap[port / 32] &= ~(1 << (port % 32));
106
107 nl_handle_destroy(handle);
108}
109#endif /* CONFIG_LIBNL20 */
110
111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800112static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
113{
114 struct nl_handle *handle;
115
116 handle = nl80211_handle_alloc(cb);
117 if (handle == NULL) {
118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
119 "callbacks (%s)", dbg);
120 return NULL;
121 }
122
123 if (genl_connect(handle)) {
124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
125 "netlink (%s)", dbg);
126 nl80211_handle_destroy(handle);
127 return NULL;
128 }
129
130 return handle;
131}
132
133
134static void nl_destroy_handles(struct nl_handle **handle)
135{
136 if (*handle == NULL)
137 return;
138 nl80211_handle_destroy(*handle);
139 *handle = NULL;
140}
141
142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143#ifndef IFF_LOWER_UP
144#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
145#endif
146#ifndef IFF_DORMANT
147#define IFF_DORMANT 0x20000 /* driver signals dormant */
148#endif
149
150#ifndef IF_OPER_DORMANT
151#define IF_OPER_DORMANT 5
152#endif
153#ifndef IF_OPER_UP
154#define IF_OPER_UP 6
155#endif
156
157struct nl80211_global {
158 struct dl_list interfaces;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800159 int if_add_ifindex;
160 struct netlink_data *netlink;
161 struct nl_cb *nl_cb;
162 struct nl_handle *nl;
163 int nl80211_id;
164 int ioctl_sock; /* socket for ioctl() use */
165
166 struct nl_handle *nl_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167};
168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800169struct nl80211_wiphy_data {
170 struct dl_list list;
171 struct dl_list bsss;
172 struct dl_list drvs;
173
174 struct nl_handle *nl_beacons;
175 struct nl_cb *nl_cb;
176
177 int wiphy_idx;
178};
179
180static void nl80211_global_deinit(void *priv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182struct i802_bss {
183 struct wpa_driver_nl80211_data *drv;
184 struct i802_bss *next;
185 int ifindex;
186 char ifname[IFNAMSIZ + 1];
187 char brname[IFNAMSIZ];
188 unsigned int beacon_set:1;
189 unsigned int added_if_into_bridge:1;
190 unsigned int added_bridge:1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700191 unsigned int in_deinit:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800192
193 u8 addr[ETH_ALEN];
194
195 int freq;
196
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800197 void *ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198 struct nl_handle *nl_preq, *nl_mgmt;
199 struct nl_cb *nl_cb;
200
201 struct nl80211_wiphy_data *wiphy_data;
202 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203};
204
205struct wpa_driver_nl80211_data {
206 struct nl80211_global *global;
207 struct dl_list list;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 struct dl_list wiphy_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 char phyname[32];
210 void *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 int ifindex;
212 int if_removed;
213 int if_disabled;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800214 int ignore_if_down_event;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 struct rfkill_data *rfkill;
216 struct wpa_driver_capa capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700217 u8 *extended_capa, *extended_capa_mask;
218 unsigned int extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 int has_capability;
220
221 int operstate;
222
223 int scan_complete_events;
224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226
227 u8 auth_bssid[ETH_ALEN];
228 u8 bssid[ETH_ALEN];
229 int associated;
230 u8 ssid[32];
231 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800232 enum nl80211_iftype nlmode;
233 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 unsigned int assoc_freq;
235
236 int monitor_sock;
237 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800242 unsigned int in_interface_list:1;
243 unsigned int device_ap_sme:1;
244 unsigned int poll_command_supported:1;
245 unsigned int data_tx_status:1;
246 unsigned int scan_for_auth:1;
247 unsigned int retry_auth:1;
248 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800249 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250
251 u64 remain_on_chan_cookie;
252 u64 send_action_cookie;
253
254 unsigned int last_mgmt_freq;
255
256 struct wpa_driver_scan_filter *filter_ssids;
257 size_t num_filter_ssids;
258
259 struct i802_bss first_bss;
260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800261 int eapol_tx_sock;
262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263#ifdef HOSTAPD
264 int eapol_sock; /* socket for EAPOL frames */
265
266 int default_if_indices[16];
267 int *if_indices;
268 int num_if_indices;
269
270 int last_freq;
271 int last_freq_ht;
272#endif /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273
274 /* From failed authentication command */
275 int auth_freq;
276 u8 auth_bssid_[ETH_ALEN];
277 u8 auth_ssid[32];
278 size_t auth_ssid_len;
279 int auth_alg;
280 u8 *auth_ie;
281 size_t auth_ie_len;
282 u8 auth_wep_key[4][16];
283 size_t auth_wep_key_len[4];
284 int auth_wep_tx_keyidx;
285 int auth_local_state_change;
286 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287};
288
289
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800290static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
292 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800293static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
294 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295static int
296wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
297static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
298 const u8 *addr, int cmd, u16 reason_code,
299 int local_state_change);
300static void nl80211_remove_monitor_interface(
301 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800302static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800304 const u8 *buf, size_t buf_len, u64 *cookie,
305 int no_cck, int no_ack, int offchanok);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800306static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
307 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308#ifdef ANDROID
309static int android_pno_start(struct i802_bss *bss,
310 struct wpa_driver_scan_params *params);
311static int android_pno_stop(struct i802_bss *bss);
312#endif /* ANDROID */
313#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700314int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700316int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
317int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
318 const struct wpabuf *proberesp,
319 const struct wpabuf *assocresp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322#ifdef HOSTAPD
323static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
324static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
325static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800326static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 enum wpa_driver_if_type type,
328 const char *ifname);
329#else /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
331{
332}
333
334static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
335{
336}
337
338static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339{
340 return 0;
341}
342#endif /* HOSTAPD */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700343#ifdef ANDROID
344extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
345 size_t buf_len);
346#endif
347
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800348static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
349 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
351 int ifindex, int disabled);
352
353static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354static int wpa_driver_nl80211_authenticate_retry(
355 struct wpa_driver_nl80211_data *drv);
356
357
358static int is_ap_interface(enum nl80211_iftype nlmode)
359{
360 return (nlmode == NL80211_IFTYPE_AP ||
361 nlmode == NL80211_IFTYPE_P2P_GO);
362}
363
364
365static int is_sta_interface(enum nl80211_iftype nlmode)
366{
367 return (nlmode == NL80211_IFTYPE_STATION ||
368 nlmode == NL80211_IFTYPE_P2P_CLIENT);
369}
370
371
372static int is_p2p_interface(enum nl80211_iftype nlmode)
373{
374 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
375 nlmode == NL80211_IFTYPE_P2P_GO);
376}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377
378
Jouni Malinen87fd2792011-05-16 18:35:42 +0300379struct nl80211_bss_info_arg {
380 struct wpa_driver_nl80211_data *drv;
381 struct wpa_scan_results *res;
382 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300384};
385
386static int bss_info_handler(struct nl_msg *msg, void *arg);
387
388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389/* nl80211 code */
390static int ack_handler(struct nl_msg *msg, void *arg)
391{
392 int *err = arg;
393 *err = 0;
394 return NL_STOP;
395}
396
397static int finish_handler(struct nl_msg *msg, void *arg)
398{
399 int *ret = arg;
400 *ret = 0;
401 return NL_SKIP;
402}
403
404static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
405 void *arg)
406{
407 int *ret = arg;
408 *ret = err->error;
409 return NL_SKIP;
410}
411
412
413static int no_seq_check(struct nl_msg *msg, void *arg)
414{
415 return NL_OK;
416}
417
418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800419static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 struct nl_handle *nl_handle, struct nl_msg *msg,
421 int (*valid_handler)(struct nl_msg *, void *),
422 void *valid_data)
423{
424 struct nl_cb *cb;
425 int err = -ENOMEM;
426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800427 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 if (!cb)
429 goto out;
430
431 err = nl_send_auto_complete(nl_handle, msg);
432 if (err < 0)
433 goto out;
434
435 err = 1;
436
437 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
438 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
439 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
440
441 if (valid_handler)
442 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
443 valid_handler, valid_data);
444
445 while (err > 0)
446 nl_recvmsgs(nl_handle, cb);
447 out:
448 nl_cb_put(cb);
449 nlmsg_free(msg);
450 return err;
451}
452
453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454static int send_and_recv_msgs_global(struct nl80211_global *global,
455 struct nl_msg *msg,
456 int (*valid_handler)(struct nl_msg *, void *),
457 void *valid_data)
458{
459 return send_and_recv(global, global->nl, msg, valid_handler,
460 valid_data);
461}
462
Dmitry Shmidt04949592012-07-19 12:16:46 -0700463
Jouni Malinen80da0422012-08-09 15:29:25 -0700464#ifndef ANDROID
465static
466#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700467int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 struct nl_msg *msg,
469 int (*valid_handler)(struct nl_msg *, void *),
470 void *valid_data)
471{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800472 return send_and_recv(drv->global, drv->global->nl, msg,
473 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474}
475
476
477struct family_data {
478 const char *group;
479 int id;
480};
481
482
483static int family_handler(struct nl_msg *msg, void *arg)
484{
485 struct family_data *res = arg;
486 struct nlattr *tb[CTRL_ATTR_MAX + 1];
487 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
488 struct nlattr *mcgrp;
489 int i;
490
491 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
492 genlmsg_attrlen(gnlh, 0), NULL);
493 if (!tb[CTRL_ATTR_MCAST_GROUPS])
494 return NL_SKIP;
495
496 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
497 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
498 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
499 nla_len(mcgrp), NULL);
500 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
501 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
502 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
503 res->group,
504 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
505 continue;
506 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
507 break;
508 };
509
510 return NL_SKIP;
511}
512
513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 const char *family, const char *group)
516{
517 struct nl_msg *msg;
518 int ret = -1;
519 struct family_data res = { group, -ENOENT };
520
521 msg = nlmsg_alloc();
522 if (!msg)
523 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800524 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 0, 0, CTRL_CMD_GETFAMILY, 0);
526 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
527
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800528 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 msg = NULL;
530 if (ret == 0)
531 ret = res.id;
532
533nla_put_failure:
534 nlmsg_free(msg);
535 return ret;
536}
537
538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800539static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
540 struct nl_msg *msg, int flags, uint8_t cmd)
541{
542 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
543 0, flags, cmd, 0);
544}
545
546
547struct wiphy_idx_data {
548 int wiphy_idx;
549};
550
551
552static int netdev_info_handler(struct nl_msg *msg, void *arg)
553{
554 struct nlattr *tb[NL80211_ATTR_MAX + 1];
555 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
556 struct wiphy_idx_data *info = arg;
557
558 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
559 genlmsg_attrlen(gnlh, 0), NULL);
560
561 if (tb[NL80211_ATTR_WIPHY])
562 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
563
564 return NL_SKIP;
565}
566
567
568static int nl80211_get_wiphy_index(struct i802_bss *bss)
569{
570 struct nl_msg *msg;
571 struct wiphy_idx_data data = {
572 .wiphy_idx = -1,
573 };
574
575 msg = nlmsg_alloc();
576 if (!msg)
577 return -1;
578
579 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
580
581 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
582
583 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
584 return data.wiphy_idx;
585 msg = NULL;
586nla_put_failure:
587 nlmsg_free(msg);
588 return -1;
589}
590
591
592static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
593 struct nl80211_wiphy_data *w)
594{
595 struct nl_msg *msg;
596 int ret = -1;
597
598 msg = nlmsg_alloc();
599 if (!msg)
600 return -1;
601
602 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
603
604 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
605
606 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
607 msg = NULL;
608 if (ret) {
609 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
610 "failed: ret=%d (%s)",
611 ret, strerror(-ret));
612 goto nla_put_failure;
613 }
614 ret = 0;
615nla_put_failure:
616 nlmsg_free(msg);
617 return ret;
618}
619
620
621static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
622{
623 struct nl80211_wiphy_data *w = eloop_ctx;
624
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800625 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626
627 nl_recvmsgs(handle, w->nl_cb);
628}
629
630
631static int process_beacon_event(struct nl_msg *msg, void *arg)
632{
633 struct nl80211_wiphy_data *w = arg;
634 struct wpa_driver_nl80211_data *drv;
635 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
636 struct nlattr *tb[NL80211_ATTR_MAX + 1];
637 union wpa_event_data event;
638
639 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
640 genlmsg_attrlen(gnlh, 0), NULL);
641
642 if (gnlh->cmd != NL80211_CMD_FRAME) {
643 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
644 gnlh->cmd);
645 return NL_SKIP;
646 }
647
648 if (!tb[NL80211_ATTR_FRAME])
649 return NL_SKIP;
650
651 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
652 wiphy_list) {
653 os_memset(&event, 0, sizeof(event));
654 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
655 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
656 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
657 }
658
659 return NL_SKIP;
660}
661
662
663static struct nl80211_wiphy_data *
664nl80211_get_wiphy_data_ap(struct i802_bss *bss)
665{
666 static DEFINE_DL_LIST(nl80211_wiphys);
667 struct nl80211_wiphy_data *w;
668 int wiphy_idx, found = 0;
669 struct i802_bss *tmp_bss;
670
671 if (bss->wiphy_data != NULL)
672 return bss->wiphy_data;
673
674 wiphy_idx = nl80211_get_wiphy_index(bss);
675
676 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
677 if (w->wiphy_idx == wiphy_idx)
678 goto add;
679 }
680
681 /* alloc new one */
682 w = os_zalloc(sizeof(*w));
683 if (w == NULL)
684 return NULL;
685 w->wiphy_idx = wiphy_idx;
686 dl_list_init(&w->bsss);
687 dl_list_init(&w->drvs);
688
689 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
690 if (!w->nl_cb) {
691 os_free(w);
692 return NULL;
693 }
694 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
695 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
696 w);
697
698 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
699 "wiphy beacons");
700 if (w->nl_beacons == NULL) {
701 os_free(w);
702 return NULL;
703 }
704
705 if (nl80211_register_beacons(bss->drv, w)) {
706 nl_destroy_handles(&w->nl_beacons);
707 os_free(w);
708 return NULL;
709 }
710
711 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
712 nl80211_recv_beacons, w, w->nl_beacons);
713
714 dl_list_add(&nl80211_wiphys, &w->list);
715
716add:
717 /* drv entry for this bss already there? */
718 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
719 if (tmp_bss->drv == bss->drv) {
720 found = 1;
721 break;
722 }
723 }
724 /* if not add it */
725 if (!found)
726 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
727
728 dl_list_add(&w->bsss, &bss->wiphy_list);
729 bss->wiphy_data = w;
730 return w;
731}
732
733
734static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
735{
736 struct nl80211_wiphy_data *w = bss->wiphy_data;
737 struct i802_bss *tmp_bss;
738 int found = 0;
739
740 if (w == NULL)
741 return;
742 bss->wiphy_data = NULL;
743 dl_list_del(&bss->wiphy_list);
744
745 /* still any for this drv present? */
746 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
747 if (tmp_bss->drv == bss->drv) {
748 found = 1;
749 break;
750 }
751 }
752 /* if not remove it */
753 if (!found)
754 dl_list_del(&bss->drv->wiphy_list);
755
756 if (!dl_list_empty(&w->bsss))
757 return;
758
759 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
760
761 nl_cb_put(w->nl_cb);
762 nl_destroy_handles(&w->nl_beacons);
763 dl_list_del(&w->list);
764 os_free(w);
765}
766
767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
769{
770 struct i802_bss *bss = priv;
771 struct wpa_driver_nl80211_data *drv = bss->drv;
772 if (!drv->associated)
773 return -1;
774 os_memcpy(bssid, drv->bssid, ETH_ALEN);
775 return 0;
776}
777
778
779static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
780{
781 struct i802_bss *bss = priv;
782 struct wpa_driver_nl80211_data *drv = bss->drv;
783 if (!drv->associated)
784 return -1;
785 os_memcpy(ssid, drv->ssid, drv->ssid_len);
786 return drv->ssid_len;
787}
788
789
790static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
791 char *buf, size_t len, int del)
792{
793 union wpa_event_data event;
794
795 os_memset(&event, 0, sizeof(event));
796 if (len > sizeof(event.interface_status.ifname))
797 len = sizeof(event.interface_status.ifname) - 1;
798 os_memcpy(event.interface_status.ifname, buf, len);
799 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
800 EVENT_INTERFACE_ADDED;
801
802 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
803 del ? "DEL" : "NEW",
804 event.interface_status.ifname,
805 del ? "removed" : "added");
806
807 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700808 if (del) {
809 if (drv->if_removed) {
810 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
811 "already set - ignore event");
812 return;
813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 drv->if_removed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700815 } else {
816 if (if_nametoindex(drv->first_bss.ifname) == 0) {
817 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
818 "does not exist - ignore "
819 "RTM_NEWLINK",
820 drv->first_bss.ifname);
821 return;
822 }
823 if (!drv->if_removed) {
824 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
825 "already cleared - ignore event");
826 return;
827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 drv->if_removed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700829 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 }
831
832 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
833}
834
835
836static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
837 u8 *buf, size_t len)
838{
839 int attrlen, rta_len;
840 struct rtattr *attr;
841
842 attrlen = len;
843 attr = (struct rtattr *) buf;
844
845 rta_len = RTA_ALIGN(sizeof(struct rtattr));
846 while (RTA_OK(attr, attrlen)) {
847 if (attr->rta_type == IFLA_IFNAME) {
848 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
849 == 0)
850 return 1;
851 else
852 break;
853 }
854 attr = RTA_NEXT(attr, attrlen);
855 }
856
857 return 0;
858}
859
860
861static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
862 int ifindex, u8 *buf, size_t len)
863{
864 if (drv->ifindex == ifindex)
865 return 1;
866
867 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
868 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
869 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
870 "interface");
871 wpa_driver_nl80211_finish_drv_init(drv);
872 return 1;
873 }
874
875 return 0;
876}
877
878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879static struct wpa_driver_nl80211_data *
880nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
881{
882 struct wpa_driver_nl80211_data *drv;
883 dl_list_for_each(drv, &global->interfaces,
884 struct wpa_driver_nl80211_data, list) {
885 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
886 have_ifidx(drv, idx))
887 return drv;
888 }
889 return NULL;
890}
891
892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
894 struct ifinfomsg *ifi,
895 u8 *buf, size_t len)
896{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897 struct nl80211_global *global = ctx;
898 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 int attrlen, rta_len;
900 struct rtattr *attr;
901 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 char namebuf[IFNAMSIZ];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
905 if (!drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
907 "ifindex %d", ifi->ifi_index);
908 return;
909 }
910
911 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
912 "(%s%s%s%s)",
913 drv->operstate, ifi->ifi_flags,
914 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
915 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
916 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
917 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
918
919 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800920 if (if_indextoname(ifi->ifi_index, namebuf) &&
921 linux_iface_up(drv->global->ioctl_sock,
922 drv->first_bss.ifname) > 0) {
923 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
924 "event since interface %s is up", namebuf);
925 return;
926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800928 if (drv->ignore_if_down_event) {
929 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
930 "event generated by mode change");
931 drv->ignore_if_down_event = 0;
932 } else {
933 drv->if_disabled = 1;
934 wpa_supplicant_event(drv->ctx,
935 EVENT_INTERFACE_DISABLED, NULL);
936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 }
938
939 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 if (if_indextoname(ifi->ifi_index, namebuf) &&
941 linux_iface_up(drv->global->ioctl_sock,
942 drv->first_bss.ifname) == 0) {
943 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
944 "event since interface %s is down",
945 namebuf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700946 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
947 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
948 "event since interface %s does not exist",
949 drv->first_bss.ifname);
950 } else if (drv->if_removed) {
951 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
952 "event since interface %s is marked "
953 "removed", drv->first_bss.ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800954 } else {
955 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
956 drv->if_disabled = 0;
957 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
958 NULL);
959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 }
961
962 /*
963 * Some drivers send the association event before the operup event--in
964 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
965 * fails. This will hit us when wpa_supplicant does not need to do
966 * IEEE 802.1X authentication
967 */
968 if (drv->operstate == 1 &&
969 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
970 !(ifi->ifi_flags & IFF_RUNNING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800971 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 -1, IF_OPER_UP);
973
974 attrlen = len;
975 attr = (struct rtattr *) buf;
976 rta_len = RTA_ALIGN(sizeof(struct rtattr));
977 while (RTA_OK(attr, attrlen)) {
978 if (attr->rta_type == IFLA_IFNAME) {
979 wpa_driver_nl80211_event_link(
980 drv,
981 ((char *) attr) + rta_len,
982 attr->rta_len - rta_len, 0);
983 } else if (attr->rta_type == IFLA_MASTER)
984 brid = nla_get_u32((struct nlattr *) attr);
985 attr = RTA_NEXT(attr, attrlen);
986 }
987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 if (ifi->ifi_family == AF_BRIDGE && brid) {
989 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 if_indextoname(brid, namebuf);
991 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
992 brid, namebuf);
993 add_ifidx(drv, brid);
994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995}
996
997
998static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
999 struct ifinfomsg *ifi,
1000 u8 *buf, size_t len)
1001{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 struct nl80211_global *global = ctx;
1003 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 int attrlen, rta_len;
1005 struct rtattr *attr;
1006 u32 brid = 0;
1007
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001008 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1009 if (!drv) {
1010 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1011 "foreign ifindex %d", ifi->ifi_index);
1012 return;
1013 }
1014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 attrlen = len;
1016 attr = (struct rtattr *) buf;
1017
1018 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1019 while (RTA_OK(attr, attrlen)) {
1020 if (attr->rta_type == IFLA_IFNAME) {
1021 wpa_driver_nl80211_event_link(
1022 drv,
1023 ((char *) attr) + rta_len,
1024 attr->rta_len - rta_len, 1);
1025 } else if (attr->rta_type == IFLA_MASTER)
1026 brid = nla_get_u32((struct nlattr *) attr);
1027 attr = RTA_NEXT(attr, attrlen);
1028 }
1029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 if (ifi->ifi_family == AF_BRIDGE && brid) {
1031 /* device has been removed from bridge */
1032 char namebuf[IFNAMSIZ];
1033 if_indextoname(brid, namebuf);
1034 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1035 "%s", brid, namebuf);
1036 del_ifidx(drv, brid);
1037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038}
1039
1040
1041static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1042 const u8 *frame, size_t len)
1043{
1044 const struct ieee80211_mgmt *mgmt;
1045 union wpa_event_data event;
1046
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001047 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 mgmt = (const struct ieee80211_mgmt *) frame;
1049 if (len < 24 + sizeof(mgmt->u.auth)) {
1050 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1051 "frame");
1052 return;
1053 }
1054
1055 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1056 os_memset(&event, 0, sizeof(event));
1057 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1058 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001059 event.auth.auth_transaction =
1060 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1062 if (len > 24 + sizeof(mgmt->u.auth)) {
1063 event.auth.ies = mgmt->u.auth.variable;
1064 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1065 }
1066
1067 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1068}
1069
1070
Jouni Malinen87fd2792011-05-16 18:35:42 +03001071static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1072{
1073 struct nl_msg *msg;
1074 int ret;
1075 struct nl80211_bss_info_arg arg;
1076
1077 os_memset(&arg, 0, sizeof(arg));
1078 msg = nlmsg_alloc();
1079 if (!msg)
1080 goto nla_put_failure;
1081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001083 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1084
1085 arg.drv = drv;
1086 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1087 msg = NULL;
1088 if (ret == 0) {
1089 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1090 "associated BSS from scan results: %u MHz",
1091 arg.assoc_freq);
1092 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1093 }
1094 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1095 "(%s)", ret, strerror(-ret));
1096nla_put_failure:
1097 nlmsg_free(msg);
1098 return drv->assoc_freq;
1099}
1100
1101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1103 const u8 *frame, size_t len)
1104{
1105 const struct ieee80211_mgmt *mgmt;
1106 union wpa_event_data event;
1107 u16 status;
1108
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001109 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 mgmt = (const struct ieee80211_mgmt *) frame;
1111 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1112 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1113 "frame");
1114 return;
1115 }
1116
1117 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1118 if (status != WLAN_STATUS_SUCCESS) {
1119 os_memset(&event, 0, sizeof(event));
1120 event.assoc_reject.bssid = mgmt->bssid;
1121 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1122 event.assoc_reject.resp_ies =
1123 (u8 *) mgmt->u.assoc_resp.variable;
1124 event.assoc_reject.resp_ies_len =
1125 len - 24 - sizeof(mgmt->u.assoc_resp);
1126 }
1127 event.assoc_reject.status_code = status;
1128
1129 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1130 return;
1131 }
1132
1133 drv->associated = 1;
1134 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1135
1136 os_memset(&event, 0, sizeof(event));
1137 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1138 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1139 event.assoc_info.resp_ies_len =
1140 len - 24 - sizeof(mgmt->u.assoc_resp);
1141 }
1142
1143 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1146}
1147
1148
1149static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1150 enum nl80211_commands cmd, struct nlattr *status,
1151 struct nlattr *addr, struct nlattr *req_ie,
1152 struct nlattr *resp_ie)
1153{
1154 union wpa_event_data event;
1155
1156 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1157 /*
1158 * Avoid reporting two association events that would confuse
1159 * the core code.
1160 */
1161 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1162 "when using userspace SME", cmd);
1163 return;
1164 }
1165
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001166 if (cmd == NL80211_CMD_CONNECT)
1167 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1168 else if (cmd == NL80211_CMD_ROAM)
1169 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 os_memset(&event, 0, sizeof(event));
1172 if (cmd == NL80211_CMD_CONNECT &&
1173 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1174 if (addr)
1175 event.assoc_reject.bssid = nla_data(addr);
1176 if (resp_ie) {
1177 event.assoc_reject.resp_ies = nla_data(resp_ie);
1178 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1179 }
1180 event.assoc_reject.status_code = nla_get_u16(status);
1181 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1182 return;
1183 }
1184
1185 drv->associated = 1;
1186 if (addr)
1187 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1188
1189 if (req_ie) {
1190 event.assoc_info.req_ies = nla_data(req_ie);
1191 event.assoc_info.req_ies_len = nla_len(req_ie);
1192 }
1193 if (resp_ie) {
1194 event.assoc_info.resp_ies = nla_data(resp_ie);
1195 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1196 }
1197
Jouni Malinen87fd2792011-05-16 18:35:42 +03001198 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1201}
1202
1203
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001204static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001205 struct nlattr *reason, struct nlattr *addr,
1206 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207{
1208 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001209 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210
1211 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1212 /*
1213 * Avoid reporting two disassociation events that could
1214 * confuse the core code.
1215 */
1216 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1217 "event when using userspace SME");
1218 return;
1219 }
1220
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001221 if (drv->ignore_next_local_disconnect) {
1222 drv->ignore_next_local_disconnect = 0;
1223 if (locally_generated) {
1224 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1225 "event triggered during reassociation");
1226 return;
1227 }
1228 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1229 "disconnect but got another disconnect "
1230 "event first");
1231 }
1232
1233 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 drv->associated = 0;
1235 os_memset(&data, 0, sizeof(data));
1236 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001237 data.deauth_info.reason_code = nla_get_u16(reason);
1238 data.deauth_info.locally_generated = by_ap == NULL;
1239 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1240}
1241
1242
1243static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1244 struct nlattr *freq, struct nlattr *type)
1245{
1246 union wpa_event_data data;
1247 int ht_enabled = 1;
1248 int chan_offset = 0;
1249
1250 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1251
1252 if (!freq || !type)
1253 return;
1254
1255 switch (nla_get_u32(type)) {
1256 case NL80211_CHAN_NO_HT:
1257 ht_enabled = 0;
1258 break;
1259 case NL80211_CHAN_HT20:
1260 break;
1261 case NL80211_CHAN_HT40PLUS:
1262 chan_offset = 1;
1263 break;
1264 case NL80211_CHAN_HT40MINUS:
1265 chan_offset = -1;
1266 break;
1267 }
1268
1269 data.ch_switch.freq = nla_get_u32(freq);
1270 data.ch_switch.ht_enabled = ht_enabled;
1271 data.ch_switch.ch_offset = chan_offset;
1272
1273 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274}
1275
1276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1278 enum nl80211_commands cmd, struct nlattr *addr)
1279{
1280 union wpa_event_data event;
1281 enum wpa_event_type ev;
1282
1283 if (nla_len(addr) != ETH_ALEN)
1284 return;
1285
1286 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1287 cmd, MAC2STR((u8 *) nla_data(addr)));
1288
1289 if (cmd == NL80211_CMD_AUTHENTICATE)
1290 ev = EVENT_AUTH_TIMED_OUT;
1291 else if (cmd == NL80211_CMD_ASSOCIATE)
1292 ev = EVENT_ASSOC_TIMED_OUT;
1293 else
1294 return;
1295
1296 os_memset(&event, 0, sizeof(event));
1297 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1298 wpa_supplicant_event(drv->ctx, ev, &event);
1299}
1300
1301
1302static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001303 struct nlattr *freq, struct nlattr *sig,
1304 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305{
1306 const struct ieee80211_mgmt *mgmt;
1307 union wpa_event_data event;
1308 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001309 int ssi_signal = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001311 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 mgmt = (const struct ieee80211_mgmt *) frame;
1313 if (len < 24) {
1314 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1315 return;
1316 }
1317
1318 fc = le_to_host16(mgmt->frame_control);
1319 stype = WLAN_FC_GET_STYPE(fc);
1320
Dmitry Shmidt04949592012-07-19 12:16:46 -07001321 if (sig)
1322 ssi_signal = (s32) nla_get_u32(sig);
1323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 os_memset(&event, 0, sizeof(event));
1325 if (freq) {
1326 event.rx_action.freq = nla_get_u32(freq);
1327 drv->last_mgmt_freq = event.rx_action.freq;
1328 }
1329 if (stype == WLAN_FC_STYPE_ACTION) {
1330 event.rx_action.da = mgmt->da;
1331 event.rx_action.sa = mgmt->sa;
1332 event.rx_action.bssid = mgmt->bssid;
1333 event.rx_action.category = mgmt->u.action.category;
1334 event.rx_action.data = &mgmt->u.action.category + 1;
1335 event.rx_action.len = frame + len - event.rx_action.data;
1336 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1337 } else {
1338 event.rx_mgmt.frame = frame;
1339 event.rx_mgmt.frame_len = len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001340 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1342 }
1343}
1344
1345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1347 struct nlattr *cookie, const u8 *frame,
1348 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349{
1350 union wpa_event_data event;
1351 const struct ieee80211_hdr *hdr;
1352 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001354 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 if (!is_ap_interface(drv->nlmode)) {
1356 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001358 if (!cookie)
1359 return;
1360
1361 cookie_val = nla_get_u64(cookie);
1362 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1363 " cookie=0%llx%s (ack=%d)",
1364 (long long unsigned int) cookie_val,
1365 cookie_val == drv->send_action_cookie ?
1366 " (match)" : " (unknown)", ack != NULL);
1367 if (cookie_val != drv->send_action_cookie)
1368 return;
1369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370
1371 hdr = (const struct ieee80211_hdr *) frame;
1372 fc = le_to_host16(hdr->frame_control);
1373
1374 os_memset(&event, 0, sizeof(event));
1375 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1376 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1377 event.tx_status.dst = hdr->addr1;
1378 event.tx_status.data = frame;
1379 event.tx_status.data_len = len;
1380 event.tx_status.ack = ack != NULL;
1381 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1382}
1383
1384
1385static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1386 enum wpa_event_type type,
1387 const u8 *frame, size_t len)
1388{
1389 const struct ieee80211_mgmt *mgmt;
1390 union wpa_event_data event;
1391 const u8 *bssid = NULL;
1392 u16 reason_code = 0;
1393
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001394 if (type == EVENT_DEAUTH)
1395 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1396 else
1397 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 mgmt = (const struct ieee80211_mgmt *) frame;
1400 if (len >= 24) {
1401 bssid = mgmt->bssid;
1402
1403 if (drv->associated != 0 &&
1404 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1405 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1406 /*
1407 * We have presumably received this deauth as a
1408 * response to a clear_state_mismatch() outgoing
1409 * deauth. Don't let it take us offline!
1410 */
1411 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1412 "from Unknown BSSID " MACSTR " -- ignoring",
1413 MAC2STR(bssid));
1414 return;
1415 }
1416 }
1417
1418 drv->associated = 0;
1419 os_memset(&event, 0, sizeof(event));
1420
1421 /* Note: Same offset for Reason Code in both frame subtypes */
1422 if (len >= 24 + sizeof(mgmt->u.deauth))
1423 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1424
1425 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001426 event.disassoc_info.locally_generated =
1427 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 event.disassoc_info.addr = bssid;
1429 event.disassoc_info.reason_code = reason_code;
1430 if (frame + len > mgmt->u.disassoc.variable) {
1431 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1432 event.disassoc_info.ie_len = frame + len -
1433 mgmt->u.disassoc.variable;
1434 }
1435 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001436 event.deauth_info.locally_generated =
1437 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 event.deauth_info.addr = bssid;
1439 event.deauth_info.reason_code = reason_code;
1440 if (frame + len > mgmt->u.deauth.variable) {
1441 event.deauth_info.ie = mgmt->u.deauth.variable;
1442 event.deauth_info.ie_len = frame + len -
1443 mgmt->u.deauth.variable;
1444 }
1445 }
1446
1447 wpa_supplicant_event(drv->ctx, type, &event);
1448}
1449
1450
1451static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1452 enum wpa_event_type type,
1453 const u8 *frame, size_t len)
1454{
1455 const struct ieee80211_mgmt *mgmt;
1456 union wpa_event_data event;
1457 u16 reason_code = 0;
1458
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001459 if (type == EVENT_UNPROT_DEAUTH)
1460 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1461 else
1462 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 if (len < 24)
1465 return;
1466
1467 mgmt = (const struct ieee80211_mgmt *) frame;
1468
1469 os_memset(&event, 0, sizeof(event));
1470 /* Note: Same offset for Reason Code in both frame subtypes */
1471 if (len >= 24 + sizeof(mgmt->u.deauth))
1472 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1473
1474 if (type == EVENT_UNPROT_DISASSOC) {
1475 event.unprot_disassoc.sa = mgmt->sa;
1476 event.unprot_disassoc.da = mgmt->da;
1477 event.unprot_disassoc.reason_code = reason_code;
1478 } else {
1479 event.unprot_deauth.sa = mgmt->sa;
1480 event.unprot_deauth.da = mgmt->da;
1481 event.unprot_deauth.reason_code = reason_code;
1482 }
1483
1484 wpa_supplicant_event(drv->ctx, type, &event);
1485}
1486
1487
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001488static void mlme_event(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 enum nl80211_commands cmd, struct nlattr *frame,
1490 struct nlattr *addr, struct nlattr *timed_out,
1491 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001492 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001494 struct wpa_driver_nl80211_data *drv = bss->drv;
1495 const u8 *data;
1496 size_t len;
1497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 if (timed_out && addr) {
1499 mlme_timeout_event(drv, cmd, addr);
1500 return;
1501 }
1502
1503 if (frame == NULL) {
1504 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1505 "data", cmd);
1506 return;
1507 }
1508
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001509 data = nla_data(frame);
1510 len = nla_len(frame);
1511 if (len < 4 + ETH_ALEN) {
1512 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d on %s(" MACSTR
1513 ") - too short",
1514 cmd, bss->ifname, MAC2STR(bss->addr));
1515 return;
1516 }
1517 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d on %s(" MACSTR ") A1="
1518 MACSTR, cmd, bss->ifname, MAC2STR(bss->addr),
1519 MAC2STR(data + 4));
1520 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
1521 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0) {
1522 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1523 "for foreign address", bss->ifname);
1524 return;
1525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1527 nla_data(frame), nla_len(frame));
1528
1529 switch (cmd) {
1530 case NL80211_CMD_AUTHENTICATE:
1531 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1532 break;
1533 case NL80211_CMD_ASSOCIATE:
1534 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1535 break;
1536 case NL80211_CMD_DEAUTHENTICATE:
1537 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1538 nla_data(frame), nla_len(frame));
1539 break;
1540 case NL80211_CMD_DISASSOCIATE:
1541 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1542 nla_data(frame), nla_len(frame));
1543 break;
1544 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001545 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1546 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 break;
1548 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1550 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 break;
1552 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1553 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1554 nla_data(frame), nla_len(frame));
1555 break;
1556 case NL80211_CMD_UNPROT_DISASSOCIATE:
1557 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1558 nla_data(frame), nla_len(frame));
1559 break;
1560 default:
1561 break;
1562 }
1563}
1564
1565
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001566static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 struct nlattr *tb[])
1568{
1569 union wpa_event_data data;
1570
1571 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1572 os_memset(&data, 0, sizeof(data));
1573 if (tb[NL80211_ATTR_MAC]) {
1574 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1575 nla_data(tb[NL80211_ATTR_MAC]),
1576 nla_len(tb[NL80211_ATTR_MAC]));
1577 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1578 }
1579 if (tb[NL80211_ATTR_KEY_SEQ]) {
1580 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1581 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1582 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1583 }
1584 if (tb[NL80211_ATTR_KEY_TYPE]) {
1585 enum nl80211_key_type key_type =
1586 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1587 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1588 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1589 data.michael_mic_failure.unicast = 1;
1590 } else
1591 data.michael_mic_failure.unicast = 1;
1592
1593 if (tb[NL80211_ATTR_KEY_IDX]) {
1594 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1595 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1596 }
1597
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001598 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599}
1600
1601
1602static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1603 struct nlattr *tb[])
1604{
1605 if (tb[NL80211_ATTR_MAC] == NULL) {
1606 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1607 "event");
1608 return;
1609 }
1610 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1611 drv->associated = 1;
1612 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1613 MAC2STR(drv->bssid));
1614
1615 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1616}
1617
1618
1619static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1620 int cancel_event, struct nlattr *tb[])
1621{
1622 unsigned int freq, chan_type, duration;
1623 union wpa_event_data data;
1624 u64 cookie;
1625
1626 if (tb[NL80211_ATTR_WIPHY_FREQ])
1627 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1628 else
1629 freq = 0;
1630
1631 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1632 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1633 else
1634 chan_type = 0;
1635
1636 if (tb[NL80211_ATTR_DURATION])
1637 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1638 else
1639 duration = 0;
1640
1641 if (tb[NL80211_ATTR_COOKIE])
1642 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1643 else
1644 cookie = 0;
1645
1646 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1647 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1648 cancel_event, freq, chan_type, duration,
1649 (long long unsigned int) cookie,
1650 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1651
1652 if (cookie != drv->remain_on_chan_cookie)
1653 return; /* not for us */
1654
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655 if (cancel_event)
1656 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657
1658 os_memset(&data, 0, sizeof(data));
1659 data.remain_on_channel.freq = freq;
1660 data.remain_on_channel.duration = duration;
1661 wpa_supplicant_event(drv->ctx, cancel_event ?
1662 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1663 EVENT_REMAIN_ON_CHANNEL, &data);
1664}
1665
1666
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001667static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1668 struct nlattr *tb[])
1669{
1670 union wpa_event_data data;
1671
1672 os_memset(&data, 0, sizeof(data));
1673
1674 if (tb[NL80211_ATTR_IE]) {
1675 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1676 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1677 }
1678
1679 if (tb[NL80211_ATTR_IE_RIC]) {
1680 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1681 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1682 }
1683
1684 if (tb[NL80211_ATTR_MAC])
1685 os_memcpy(data.ft_ies.target_ap,
1686 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1687
1688 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1689 MAC2STR(data.ft_ies.target_ap));
1690
1691 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1692}
1693
1694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1696 struct nlattr *tb[])
1697{
1698 union wpa_event_data event;
1699 struct nlattr *nl;
1700 int rem;
1701 struct scan_info *info;
1702#define MAX_REPORT_FREQS 50
1703 int freqs[MAX_REPORT_FREQS];
1704 int num_freqs = 0;
1705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001706 if (drv->scan_for_auth) {
1707 drv->scan_for_auth = 0;
1708 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1709 "cfg80211 BSS entry");
1710 wpa_driver_nl80211_authenticate_retry(drv);
1711 return;
1712 }
1713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 os_memset(&event, 0, sizeof(event));
1715 info = &event.scan_info;
1716 info->aborted = aborted;
1717
1718 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1719 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1720 struct wpa_driver_scan_ssid *s =
1721 &info->ssids[info->num_ssids];
1722 s->ssid = nla_data(nl);
1723 s->ssid_len = nla_len(nl);
1724 info->num_ssids++;
1725 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1726 break;
1727 }
1728 }
1729 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1730 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1731 {
1732 freqs[num_freqs] = nla_get_u32(nl);
1733 num_freqs++;
1734 if (num_freqs == MAX_REPORT_FREQS - 1)
1735 break;
1736 }
1737 info->freqs = freqs;
1738 info->num_freqs = num_freqs;
1739 }
1740 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1741}
1742
1743
1744static int get_link_signal(struct nl_msg *msg, void *arg)
1745{
1746 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1747 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1748 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1749 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1750 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1751 };
1752 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1753 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1754 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1755 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1756 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1757 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1758 };
1759 struct wpa_signal_info *sig_change = arg;
1760
1761 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1762 genlmsg_attrlen(gnlh, 0), NULL);
1763 if (!tb[NL80211_ATTR_STA_INFO] ||
1764 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1765 tb[NL80211_ATTR_STA_INFO], policy))
1766 return NL_SKIP;
1767 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1768 return NL_SKIP;
1769
1770 sig_change->current_signal =
1771 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1772
1773 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1774 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1775 sinfo[NL80211_STA_INFO_TX_BITRATE],
1776 rate_policy)) {
1777 sig_change->current_txrate = 0;
1778 } else {
1779 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1780 sig_change->current_txrate =
1781 nla_get_u16(rinfo[
1782 NL80211_RATE_INFO_BITRATE]) * 100;
1783 }
1784 }
1785 }
1786
1787 return NL_SKIP;
1788}
1789
1790
1791static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1792 struct wpa_signal_info *sig)
1793{
1794 struct nl_msg *msg;
1795
1796 sig->current_signal = -9999;
1797 sig->current_txrate = 0;
1798
1799 msg = nlmsg_alloc();
1800 if (!msg)
1801 return -ENOMEM;
1802
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804
1805 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1806 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1807
1808 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1809 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001810 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 return -ENOBUFS;
1812}
1813
1814
1815static int get_link_noise(struct nl_msg *msg, void *arg)
1816{
1817 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1818 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1819 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1820 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1821 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1822 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1823 };
1824 struct wpa_signal_info *sig_change = arg;
1825
1826 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1827 genlmsg_attrlen(gnlh, 0), NULL);
1828
1829 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1830 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1831 return NL_SKIP;
1832 }
1833
1834 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1835 tb[NL80211_ATTR_SURVEY_INFO],
1836 survey_policy)) {
1837 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1838 "attributes!");
1839 return NL_SKIP;
1840 }
1841
1842 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1843 return NL_SKIP;
1844
1845 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1846 sig_change->frequency)
1847 return NL_SKIP;
1848
1849 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1850 return NL_SKIP;
1851
1852 sig_change->current_noise =
1853 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1854
1855 return NL_SKIP;
1856}
1857
1858
1859static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1860 struct wpa_signal_info *sig_change)
1861{
1862 struct nl_msg *msg;
1863
1864 sig_change->current_noise = 9999;
1865 sig_change->frequency = drv->assoc_freq;
1866
1867 msg = nlmsg_alloc();
1868 if (!msg)
1869 return -ENOMEM;
1870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872
1873 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1874
1875 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1876 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877 nlmsg_free(msg);
1878 return -ENOBUFS;
1879}
1880
1881
1882static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1883{
1884 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1885 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1886 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1887 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1888 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1889 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1890 };
1891 struct wpa_scan_results *scan_results = arg;
1892 struct wpa_scan_res *scan_res;
1893 size_t i;
1894
1895 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1896 genlmsg_attrlen(gnlh, 0), NULL);
1897
1898 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1899 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1900 return NL_SKIP;
1901 }
1902
1903 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1904 tb[NL80211_ATTR_SURVEY_INFO],
1905 survey_policy)) {
1906 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1907 "attributes");
1908 return NL_SKIP;
1909 }
1910
1911 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1912 return NL_SKIP;
1913
1914 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1915 return NL_SKIP;
1916
1917 for (i = 0; i < scan_results->num; ++i) {
1918 scan_res = scan_results->res[i];
1919 if (!scan_res)
1920 continue;
1921 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1922 scan_res->freq)
1923 continue;
1924 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1925 continue;
1926 scan_res->noise = (s8)
1927 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1928 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1929 }
1930
1931 return NL_SKIP;
1932}
1933
1934
1935static int nl80211_get_noise_for_scan_results(
1936 struct wpa_driver_nl80211_data *drv,
1937 struct wpa_scan_results *scan_res)
1938{
1939 struct nl_msg *msg;
1940
1941 msg = nlmsg_alloc();
1942 if (!msg)
1943 return -ENOMEM;
1944
1945 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1946
1947 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1948
1949 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1950 scan_res);
1951 nla_put_failure:
1952 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 return -ENOBUFS;
1954}
1955
1956
1957static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1958 struct nlattr *tb[])
1959{
1960 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1961 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1962 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1963 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1964 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1965 };
1966 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1967 enum nl80211_cqm_rssi_threshold_event event;
1968 union wpa_event_data ed;
1969 struct wpa_signal_info sig;
1970 int res;
1971
1972 if (tb[NL80211_ATTR_CQM] == NULL ||
1973 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1974 cqm_policy)) {
1975 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1976 return;
1977 }
1978
1979 os_memset(&ed, 0, sizeof(ed));
1980
1981 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1982 if (!tb[NL80211_ATTR_MAC])
1983 return;
1984 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1985 ETH_ALEN);
1986 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1987 return;
1988 }
1989
1990 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1991 return;
1992 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1993
1994 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1995 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1996 "event: RSSI high");
1997 ed.signal_change.above_threshold = 1;
1998 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1999 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2000 "event: RSSI low");
2001 ed.signal_change.above_threshold = 0;
2002 } else
2003 return;
2004
2005 res = nl80211_get_link_signal(drv, &sig);
2006 if (res == 0) {
2007 ed.signal_change.current_signal = sig.current_signal;
2008 ed.signal_change.current_txrate = sig.current_txrate;
2009 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2010 sig.current_signal, sig.current_txrate);
2011 }
2012
2013 res = nl80211_get_link_noise(drv, &sig);
2014 if (res == 0) {
2015 ed.signal_change.current_noise = sig.current_noise;
2016 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2017 sig.current_noise);
2018 }
2019
2020 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2021}
2022
2023
2024static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2025 struct nlattr **tb)
2026{
2027 u8 *addr;
2028 union wpa_event_data data;
2029
2030 if (tb[NL80211_ATTR_MAC] == NULL)
2031 return;
2032 addr = nla_data(tb[NL80211_ATTR_MAC]);
2033 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002034
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002035 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002036 u8 *ies = NULL;
2037 size_t ies_len = 0;
2038 if (tb[NL80211_ATTR_IE]) {
2039 ies = nla_data(tb[NL80211_ATTR_IE]);
2040 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2041 }
2042 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2043 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2044 return;
2045 }
2046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2048 return;
2049
2050 os_memset(&data, 0, sizeof(data));
2051 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2052 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2053}
2054
2055
2056static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2057 struct nlattr **tb)
2058{
2059 u8 *addr;
2060 union wpa_event_data data;
2061
2062 if (tb[NL80211_ATTR_MAC] == NULL)
2063 return;
2064 addr = nla_data(tb[NL80211_ATTR_MAC]);
2065 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2066 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002069 drv_event_disassoc(drv->ctx, addr);
2070 return;
2071 }
2072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2074 return;
2075
2076 os_memset(&data, 0, sizeof(data));
2077 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2078 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2079}
2080
2081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2083 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002085 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2086 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2087 [NL80211_REKEY_DATA_KEK] = {
2088 .minlen = NL80211_KEK_LEN,
2089 .maxlen = NL80211_KEK_LEN,
2090 },
2091 [NL80211_REKEY_DATA_KCK] = {
2092 .minlen = NL80211_KCK_LEN,
2093 .maxlen = NL80211_KCK_LEN,
2094 },
2095 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2096 .minlen = NL80211_REPLAY_CTR_LEN,
2097 .maxlen = NL80211_REPLAY_CTR_LEN,
2098 },
2099 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 union wpa_event_data data;
2101
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002102 if (!tb[NL80211_ATTR_MAC])
2103 return;
2104 if (!tb[NL80211_ATTR_REKEY_DATA])
2105 return;
2106 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2107 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2108 return;
2109 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2110 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 os_memset(&data, 0, sizeof(data));
2113 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2114 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2115 MAC2STR(data.driver_gtk_rekey.bssid));
2116 data.driver_gtk_rekey.replay_ctr =
2117 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2118 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2119 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2120 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2121}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123
2124static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2125 struct nlattr **tb)
2126{
2127 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2128 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2129 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2130 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2131 .minlen = ETH_ALEN,
2132 .maxlen = ETH_ALEN,
2133 },
2134 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2135 };
2136 union wpa_event_data data;
2137
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002138 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2141 return;
2142 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2143 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2144 return;
2145 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2146 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2147 return;
2148
2149 os_memset(&data, 0, sizeof(data));
2150 os_memcpy(data.pmkid_candidate.bssid,
2151 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2152 data.pmkid_candidate.index =
2153 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2154 data.pmkid_candidate.preauth =
2155 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2156 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2157}
2158
2159
2160static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2161 struct nlattr **tb)
2162{
2163 union wpa_event_data data;
2164
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002165 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002167 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2168 return;
2169
2170 os_memset(&data, 0, sizeof(data));
2171 os_memcpy(data.client_poll.addr,
2172 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2173
2174 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2175}
2176
2177
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002178static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2179 struct nlattr **tb)
2180{
2181 union wpa_event_data data;
2182
2183 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2184
2185 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2186 return;
2187
2188 os_memset(&data, 0, sizeof(data));
2189 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2190 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2191 case NL80211_TDLS_SETUP:
2192 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2193 MACSTR, MAC2STR(data.tdls.peer));
2194 data.tdls.oper = TDLS_REQUEST_SETUP;
2195 break;
2196 case NL80211_TDLS_TEARDOWN:
2197 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2198 MACSTR, MAC2STR(data.tdls.peer));
2199 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2200 break;
2201 default:
2202 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2203 "event");
2204 return;
2205 }
2206 if (tb[NL80211_ATTR_REASON_CODE]) {
2207 data.tdls.reason_code =
2208 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2209 }
2210
2211 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2212}
2213
2214
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002215static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2216 struct nlattr **tb)
2217{
2218 union wpa_event_data data;
2219 u32 reason;
2220
2221 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2222
2223 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2224 return;
2225
2226 os_memset(&data, 0, sizeof(data));
2227 os_memcpy(data.connect_failed_reason.addr,
2228 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2229
2230 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2231 switch (reason) {
2232 case NL80211_CONN_FAIL_MAX_CLIENTS:
2233 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2234 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2235 break;
2236 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2237 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2238 " tried to connect",
2239 MAC2STR(data.connect_failed_reason.addr));
2240 data.connect_failed_reason.code = BLOCKED_CLIENT;
2241 break;
2242 default:
2243 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2244 "%u", reason);
2245 return;
2246 }
2247
2248 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2249}
2250
2251
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002252static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2253 int wds)
2254{
2255 struct wpa_driver_nl80211_data *drv = bss->drv;
2256 union wpa_event_data event;
2257
2258 if (!tb[NL80211_ATTR_MAC])
2259 return;
2260
2261 os_memset(&event, 0, sizeof(event));
2262 event.rx_from_unknown.bssid = bss->addr;
2263 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2264 event.rx_from_unknown.wds = wds;
2265
2266 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2267}
2268
2269
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002270static void do_process_drv_event(struct i802_bss *bss, int cmd,
2271 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002272{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002273 struct wpa_driver_nl80211_data *drv = bss->drv;
2274
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002275 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2276 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2277 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002279 drv->ap_scan_as_station);
2280 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 }
2282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002285 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002287 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002288 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002289 break;
2290 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002291 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2293 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002295 wpa_dbg(drv->ctx, MSG_DEBUG,
2296 "nl80211: New scan results available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 drv->scan_complete_events = 1;
2298 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2299 drv->ctx);
2300 send_scan_event(drv, 0, tb);
2301 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002302 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002303 wpa_dbg(drv->ctx, MSG_DEBUG,
2304 "nl80211: New sched scan results available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002305 send_scan_event(drv, 0, tb);
2306 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002308 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 /*
2310 * Need to indicate that scan results are available in order
2311 * not to make wpa_supplicant stop its scanning.
2312 */
2313 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2314 drv->ctx);
2315 send_scan_event(drv, 1, tb);
2316 break;
2317 case NL80211_CMD_AUTHENTICATE:
2318 case NL80211_CMD_ASSOCIATE:
2319 case NL80211_CMD_DEAUTHENTICATE:
2320 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 case NL80211_CMD_FRAME_TX_STATUS:
2322 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2323 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002324 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2326 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002327 tb[NL80211_ATTR_COOKIE],
2328 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 break;
2330 case NL80211_CMD_CONNECT:
2331 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002332 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 tb[NL80211_ATTR_STATUS_CODE],
2334 tb[NL80211_ATTR_MAC],
2335 tb[NL80211_ATTR_REQ_IE],
2336 tb[NL80211_ATTR_RESP_IE]);
2337 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002338 case NL80211_CMD_CH_SWITCH_NOTIFY:
2339 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2340 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2341 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002343 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002344 tb[NL80211_ATTR_MAC],
2345 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 break;
2347 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002348 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 break;
2350 case NL80211_CMD_JOIN_IBSS:
2351 mlme_event_join_ibss(drv, tb);
2352 break;
2353 case NL80211_CMD_REMAIN_ON_CHANNEL:
2354 mlme_event_remain_on_channel(drv, 0, tb);
2355 break;
2356 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2357 mlme_event_remain_on_channel(drv, 1, tb);
2358 break;
2359 case NL80211_CMD_NOTIFY_CQM:
2360 nl80211_cqm_event(drv, tb);
2361 break;
2362 case NL80211_CMD_REG_CHANGE:
2363 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2364 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2365 NULL);
2366 break;
2367 case NL80211_CMD_REG_BEACON_HINT:
2368 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2369 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2370 NULL);
2371 break;
2372 case NL80211_CMD_NEW_STATION:
2373 nl80211_new_station_event(drv, tb);
2374 break;
2375 case NL80211_CMD_DEL_STATION:
2376 nl80211_del_station_event(drv, tb);
2377 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002378 case NL80211_CMD_SET_REKEY_OFFLOAD:
2379 nl80211_rekey_offload_event(drv, tb);
2380 break;
2381 case NL80211_CMD_PMKSA_CANDIDATE:
2382 nl80211_pmksa_candidate_event(drv, tb);
2383 break;
2384 case NL80211_CMD_PROBE_CLIENT:
2385 nl80211_client_probe_event(drv, tb);
2386 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002387 case NL80211_CMD_TDLS_OPER:
2388 nl80211_tdls_oper_event(drv, tb);
2389 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002390 case NL80211_CMD_CONN_FAILED:
2391 nl80211_connect_failed_event(drv, tb);
2392 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002393 case NL80211_CMD_FT_EVENT:
2394 mlme_event_ft_event(drv, tb);
2395 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002396 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002397 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2398 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002399 break;
2400 }
2401}
2402
2403
2404static int process_drv_event(struct nl_msg *msg, void *arg)
2405{
2406 struct wpa_driver_nl80211_data *drv = arg;
2407 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2408 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002409 struct i802_bss *bss;
2410 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002411
2412 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2413 genlmsg_attrlen(gnlh, 0), NULL);
2414
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002415 if (tb[NL80211_ATTR_IFINDEX])
2416 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2417
2418 for (bss = &drv->first_bss; bss; bss = bss->next) {
2419 if (ifidx == -1 || ifidx == bss->ifindex) {
2420 do_process_drv_event(bss, gnlh->cmd, tb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002421 return NL_SKIP;
2422 }
2423 }
2424
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002425 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d) for foreign "
2426 "interface (ifindex %d)", gnlh->cmd, ifidx);
2427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428 return NL_SKIP;
2429}
2430
2431
2432static int process_global_event(struct nl_msg *msg, void *arg)
2433{
2434 struct nl80211_global *global = arg;
2435 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2436 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002437 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002439 struct i802_bss *bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002440
2441 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2442 genlmsg_attrlen(gnlh, 0), NULL);
2443
2444 if (tb[NL80211_ATTR_IFINDEX])
2445 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2446
Dmitry Shmidt04949592012-07-19 12:16:46 -07002447 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2448 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002449 for (bss = &drv->first_bss; bss; bss = bss->next) {
2450 if (ifidx == -1 || ifidx == bss->ifindex) {
2451 do_process_drv_event(bss, gnlh->cmd, tb);
2452 return NL_SKIP;
2453 }
2454 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002455 }
2456
2457 return NL_SKIP;
2458}
2459
2460
2461static int process_bss_event(struct nl_msg *msg, void *arg)
2462{
2463 struct i802_bss *bss = arg;
2464 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2465 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2466
2467 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2468 genlmsg_attrlen(gnlh, 0), NULL);
2469
2470 switch (gnlh->cmd) {
2471 case NL80211_CMD_FRAME:
2472 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002473 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002474 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2475 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002476 tb[NL80211_ATTR_COOKIE],
2477 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002478 break;
2479 case NL80211_CMD_UNEXPECTED_FRAME:
2480 nl80211_spurious_frame(bss, tb, 0);
2481 break;
2482 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2483 nl80211_spurious_frame(bss, tb, 1);
2484 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 default:
2486 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2487 "(cmd=%d)", gnlh->cmd);
2488 break;
2489 }
2490
2491 return NL_SKIP;
2492}
2493
2494
2495static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2496 void *handle)
2497{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002500 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 nl_recvmsgs(handle, cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503}
2504
2505
2506/**
2507 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2508 * @priv: driver_nl80211 private data
2509 * @alpha2_arg: country to which to switch to
2510 * Returns: 0 on success, -1 on failure
2511 *
2512 * This asks nl80211 to set the regulatory domain for given
2513 * country ISO / IEC alpha2.
2514 */
2515static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2516{
2517 struct i802_bss *bss = priv;
2518 struct wpa_driver_nl80211_data *drv = bss->drv;
2519 char alpha2[3];
2520 struct nl_msg *msg;
2521
2522 msg = nlmsg_alloc();
2523 if (!msg)
2524 return -ENOMEM;
2525
2526 alpha2[0] = alpha2_arg[0];
2527 alpha2[1] = alpha2_arg[1];
2528 alpha2[2] = '\0';
2529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531
2532 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2533 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2534 return -EINVAL;
2535 return 0;
2536nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 return -EINVAL;
2539}
2540
2541
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002542static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2543{
2544 u32 *feat = arg;
2545 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2546 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2547
2548 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2549 genlmsg_attrlen(gnlh, 0), NULL);
2550
2551 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2552 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2553
2554 return NL_SKIP;
2555}
2556
2557
2558static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2559{
2560 u32 feat = 0;
2561 struct nl_msg *msg;
2562
2563 msg = nlmsg_alloc();
2564 if (!msg)
2565 goto nla_put_failure;
2566
2567 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2568 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2569 return feat;
2570
2571 msg = NULL;
2572nla_put_failure:
2573 nlmsg_free(msg);
2574 return 0;
2575}
2576
2577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578struct wiphy_info_data {
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002579 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580 struct wpa_driver_capa *capa;
2581
2582 unsigned int error:1;
2583 unsigned int device_ap_sme:1;
2584 unsigned int poll_command_supported:1;
2585 unsigned int data_tx_status:1;
2586 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002587 unsigned int auth_supported:1;
2588 unsigned int connect_supported:1;
2589 unsigned int p2p_go_supported:1;
2590 unsigned int p2p_client_supported:1;
2591 unsigned int p2p_concurrent:1;
2592 unsigned int p2p_multichan_concurrent:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593};
2594
2595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002596static unsigned int probe_resp_offload_support(int supp_protocols)
2597{
2598 unsigned int prot = 0;
2599
2600 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2601 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2602 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2603 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2604 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2605 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2606 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2607 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2608
2609 return prot;
2610}
2611
2612
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002613static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
2614 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002616 struct nlattr *nl_mode;
2617 int i;
2618
2619 if (tb == NULL)
2620 return;
2621
2622 nla_for_each_nested(nl_mode, tb, i) {
2623 switch (nla_type(nl_mode)) {
2624 case NL80211_IFTYPE_AP:
2625 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
2626 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002627 case NL80211_IFTYPE_ADHOC:
2628 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
2629 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002630 case NL80211_IFTYPE_P2P_GO:
2631 info->p2p_go_supported = 1;
2632 break;
2633 case NL80211_IFTYPE_P2P_CLIENT:
2634 info->p2p_client_supported = 1;
2635 break;
2636 case NL80211_IFTYPE_MONITOR:
2637 info->monitor_supported = 1;
2638 break;
2639 }
2640 }
2641}
2642
2643
2644static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
2645 struct nlattr *nl_combi)
2646{
2647 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2648 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2649 struct nlattr *nl_limit, *nl_mode;
2650 int err, rem_limit, rem_mode;
2651 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652 static struct nla_policy
2653 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2654 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2655 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2656 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2657 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2658 },
2659 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2660 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2661 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2662 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002664 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2665 nl_combi, iface_combination_policy);
2666 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2667 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2668 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2669 return 0; /* broken combination */
2670
2671 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
2672 rem_limit) {
2673 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
2674 nl_limit, iface_limit_policy);
2675 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2676 return 0; /* broken combination */
2677
2678 nla_for_each_nested(nl_mode,
2679 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2680 rem_mode) {
2681 int ift = nla_type(nl_mode);
2682 if (ift == NL80211_IFTYPE_P2P_GO ||
2683 ift == NL80211_IFTYPE_P2P_CLIENT)
2684 combination_has_p2p = 1;
2685 if (ift == NL80211_IFTYPE_STATION)
2686 combination_has_mgd = 1;
2687 }
2688 if (combination_has_p2p && combination_has_mgd)
2689 break;
2690 }
2691
2692 if (combination_has_p2p && combination_has_mgd) {
2693 info->p2p_concurrent = 1;
2694 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
2695 info->p2p_multichan_concurrent = 1;
2696 return 1;
2697 }
2698
2699 return 0;
2700}
2701
2702
2703static void wiphy_info_iface_comb(struct wiphy_info_data *info,
2704 struct nlattr *tb)
2705{
2706 struct nlattr *nl_combi;
2707 int rem_combi;
2708
2709 if (tb == NULL)
2710 return;
2711
2712 nla_for_each_nested(nl_combi, tb, rem_combi) {
2713 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
2714 break;
2715 }
2716}
2717
2718
2719static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
2720 struct nlattr *tb)
2721{
2722 struct nlattr *nl_cmd;
2723 int i;
2724
2725 if (tb == NULL)
2726 return;
2727
2728 nla_for_each_nested(nl_cmd, tb, i) {
2729 switch (nla_get_u32(nl_cmd)) {
2730 case NL80211_CMD_AUTHENTICATE:
2731 info->auth_supported = 1;
2732 break;
2733 case NL80211_CMD_CONNECT:
2734 info->connect_supported = 1;
2735 break;
2736 case NL80211_CMD_START_SCHED_SCAN:
2737 info->capa->sched_scan_supported = 1;
2738 break;
2739 case NL80211_CMD_PROBE_CLIENT:
2740 info->poll_command_supported = 1;
2741 break;
2742 }
2743 }
2744}
2745
2746
2747static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
2748 struct nlattr *tb)
2749{
2750 /* default to 5000 since early versions of mac80211 don't set it */
2751 capa->max_remain_on_chan = 5000;
2752
2753 if (tb)
2754 capa->max_remain_on_chan = nla_get_u32(tb);
2755}
2756
2757
2758static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
2759 struct nlattr *ext_setup)
2760{
2761 if (tdls == NULL)
2762 return;
2763
2764 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2765 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2766
2767 if (ext_setup) {
2768 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2769 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2770 }
2771}
2772
2773
2774static void wiphy_info_feature_flags(struct wiphy_info_data *info,
2775 struct nlattr *tb)
2776{
2777 u32 flags;
2778 struct wpa_driver_capa *capa = info->capa;
2779
2780 if (tb == NULL)
2781 return;
2782
2783 flags = nla_get_u32(tb);
2784
2785 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2786 info->data_tx_status = 1;
2787
2788 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
2789 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
2790
2791 if (flags & NL80211_FEATURE_SAE)
2792 capa->flags |= WPA_DRIVER_FLAGS_SAE;
2793
2794 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
2795 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
2796}
2797
2798
2799static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
2800 struct nlattr *tb)
2801{
2802 u32 protocols;
2803
2804 if (tb == NULL)
2805 return;
2806
2807 protocols = nla_get_u32(tb);
2808 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
2809 "mode");
2810 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2811 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
2812}
2813
2814
2815static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2816{
2817 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2818 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2819 struct wiphy_info_data *info = arg;
2820 struct wpa_driver_capa *capa = info->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002821 struct wpa_driver_nl80211_data *drv = info->drv;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2824 genlmsg_attrlen(gnlh, 0), NULL);
2825
2826 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002827 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002830 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
2831 capa->max_sched_scan_ssids =
2832 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2833
2834 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
2835 capa->max_match_sets =
2836 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2837
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002838 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
2839 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
2840 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2843 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2844 "off-channel TX");
2845 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2846 }
2847
2848 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2849 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2850 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2851 }
2852
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002853 wiphy_info_max_roc(capa,
2854 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002855
2856 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2857 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002859 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
2860 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002862 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2863 info->device_ap_sme = 1;
2864
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002865 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
2866 wiphy_info_probe_resp_offload(capa,
2867 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002868
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002869 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
2870 drv->extended_capa == NULL) {
2871 drv->extended_capa =
2872 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2873 if (drv->extended_capa) {
2874 os_memcpy(drv->extended_capa,
2875 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
2876 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2877 drv->extended_capa_len =
2878 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
2879 }
2880 drv->extended_capa_mask =
2881 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2882 if (drv->extended_capa_mask) {
2883 os_memcpy(drv->extended_capa_mask,
2884 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
2885 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
2886 } else {
2887 os_free(drv->extended_capa);
2888 drv->extended_capa = NULL;
2889 drv->extended_capa_len = 0;
2890 }
2891 }
2892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 return NL_SKIP;
2894}
2895
2896
2897static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2898 struct wiphy_info_data *info)
2899{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002900 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 struct nl_msg *msg;
2902
2903 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002904 info->capa = &drv->capa;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002905 info->drv = drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906
2907 msg = nlmsg_alloc();
2908 if (!msg)
2909 return -1;
2910
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002911 feat = get_nl80211_protocol_features(drv);
2912 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
2913 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
2914 else
2915 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002917 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2919
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002920 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
2921 return -1;
2922
2923 if (info->auth_supported)
2924 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
2925 else if (!info->connect_supported) {
2926 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2927 "authentication/association or connect commands");
2928 info->error = 1;
2929 }
2930
2931 if (info->p2p_go_supported && info->p2p_client_supported)
2932 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2933 if (info->p2p_concurrent) {
2934 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2935 "interface (driver advertised support)");
2936 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2937 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2938 }
2939 if (info->p2p_multichan_concurrent) {
2940 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
2941 "concurrent (driver advertised support)");
2942 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
2943 }
2944 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945nla_put_failure:
2946 nlmsg_free(msg);
2947 return -1;
2948}
2949
2950
2951static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2952{
2953 struct wiphy_info_data info;
2954 if (wpa_driver_nl80211_get_info(drv, &info))
2955 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002956
2957 if (info.error)
2958 return -1;
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 drv->has_capability = 1;
2961 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2962 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2963 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2964 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2965 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2966 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2967 WPA_DRIVER_CAPA_ENC_WEP104 |
2968 WPA_DRIVER_CAPA_ENC_TKIP |
2969 WPA_DRIVER_CAPA_ENC_CCMP;
2970 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2971 WPA_DRIVER_AUTH_SHARED |
2972 WPA_DRIVER_AUTH_LEAP;
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2975 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002977
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002978 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002979 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002980
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981 /*
2982 * No AP SME is currently assumed to also indicate no AP MLME
2983 * in the driver/firmware.
2984 */
2985 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
2986 }
2987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002988 drv->device_ap_sme = info.device_ap_sme;
2989 drv->poll_command_supported = info.poll_command_supported;
2990 drv->data_tx_status = info.data_tx_status;
2991
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992#ifdef ANDROID_P2P
2993 if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
2994 /* Driver is new enough to support monitorless mode*/
2995 wpa_printf(MSG_DEBUG, "nl80211: Driver is new "
2996 "enough to support monitor-less mode");
2997 drv->use_monitor = 0;
2998 }
2999#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003000 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003001 * If poll command and tx status are supported, mac80211 is new enough
3002 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003003 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003004 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003005#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003006
3007 if (drv->device_ap_sme && drv->use_monitor) {
3008 /*
3009 * Non-mac80211 drivers may not support monitor interface.
3010 * Make sure we do not get stuck with incorrect capability here
3011 * by explicitly testing this.
3012 */
3013 if (!info.monitor_supported) {
3014 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3015 "with device_ap_sme since no monitor mode "
3016 "support detected");
3017 drv->use_monitor = 0;
3018 }
3019 }
3020
3021 /*
3022 * If we aren't going to use monitor interfaces, but the
3023 * driver doesn't support data TX status, we won't get TX
3024 * status for EAPOL frames.
3025 */
3026 if (!drv->use_monitor && !info.data_tx_status)
3027 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028
3029 return 0;
3030}
3031
3032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003033#ifdef ANDROID
3034static int android_genl_ctrl_resolve(struct nl_handle *handle,
3035 const char *name)
3036{
3037 /*
3038 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3039 * need to work around that.
3040 */
3041 struct nl_cache *cache = NULL;
3042 struct genl_family *nl80211 = NULL;
3043 int id = -1;
3044
3045 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3046 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3047 "netlink cache");
3048 goto fail;
3049 }
3050
3051 nl80211 = genl_ctrl_search_by_name(cache, name);
3052 if (nl80211 == NULL)
3053 goto fail;
3054
3055 id = genl_family_get_id(nl80211);
3056
3057fail:
3058 if (nl80211)
3059 genl_family_put(nl80211);
3060 if (cache)
3061 nl_cache_free(cache);
3062
3063 return id;
3064}
3065#define genl_ctrl_resolve android_genl_ctrl_resolve
3066#endif /* ANDROID */
3067
3068
3069static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070{
3071 int ret;
3072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003073 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3074 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3076 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003077 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 }
3079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003080 global->nl = nl_create_handle(global->nl_cb, "nl");
3081 if (global->nl == NULL)
3082 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003084 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3085 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3087 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003088 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 }
3090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003091 global->nl_event = nl_create_handle(global->nl_cb, "event");
3092 if (global->nl_event == NULL)
3093 goto err;
3094
3095 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003097 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 if (ret < 0) {
3099 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3100 "membership for scan events: %d (%s)",
3101 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003102 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 }
3104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003105 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003107 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108 if (ret < 0) {
3109 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3110 "membership for mlme events: %d (%s)",
3111 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003112 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 }
3114
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003115 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003117 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 if (ret < 0) {
3119 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3120 "membership for regulatory events: %d (%s)",
3121 ret, strerror(-ret));
3122 /* Continue without regulatory events */
3123 }
3124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003125 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3126 no_seq_check, NULL);
3127 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3128 process_global_event, global);
3129
3130 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
3131 wpa_driver_nl80211_event_receive,
3132 global->nl_cb, global->nl_event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133
3134 return 0;
3135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003136err:
3137 nl_destroy_handles(&global->nl_event);
3138 nl_destroy_handles(&global->nl);
3139 nl_cb_put(global->nl_cb);
3140 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 return -1;
3142}
3143
3144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3146{
3147 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3148 if (!drv->nl_cb) {
3149 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3150 return -1;
3151 }
3152
3153 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3154 no_seq_check, NULL);
3155 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3156 process_drv_event, drv);
3157
3158 return 0;
3159}
3160
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3163{
3164 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3165 /*
3166 * This may be for any interface; use ifdown event to disable
3167 * interface.
3168 */
3169}
3170
3171
3172static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3173{
3174 struct wpa_driver_nl80211_data *drv = ctx;
3175 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003176 if (linux_set_iface_flags(drv->global->ioctl_sock,
3177 drv->first_bss.ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3179 "after rfkill unblock");
3180 return;
3181 }
3182 /* rtnetlink ifup handler will report interface as enabled */
3183}
3184
3185
3186static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
3187{
3188 /* Find phy (radio) to which this interface belongs */
3189 char buf[90], *pos;
3190 int f, rv;
3191
3192 drv->phyname[0] = '\0';
3193 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
3194 drv->first_bss.ifname);
3195 f = open(buf, O_RDONLY);
3196 if (f < 0) {
3197 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
3198 buf, strerror(errno));
3199 return;
3200 }
3201
3202 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
3203 close(f);
3204 if (rv < 0) {
3205 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
3206 buf, strerror(errno));
3207 return;
3208 }
3209
3210 drv->phyname[rv] = '\0';
3211 pos = os_strchr(drv->phyname, '\n');
3212 if (pos)
3213 *pos = '\0';
3214 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3215 drv->first_bss.ifname, drv->phyname);
3216}
3217
3218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003219static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3220 void *eloop_ctx,
3221 void *handle)
3222{
3223 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3224 u8 data[2048];
3225 struct msghdr msg;
3226 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003227 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003228 struct cmsghdr *cmsg;
3229 int res, found_ee = 0, found_wifi = 0, acked = 0;
3230 union wpa_event_data event;
3231
3232 memset(&msg, 0, sizeof(msg));
3233 msg.msg_iov = &entry;
3234 msg.msg_iovlen = 1;
3235 entry.iov_base = data;
3236 entry.iov_len = sizeof(data);
3237 msg.msg_control = &control;
3238 msg.msg_controllen = sizeof(control);
3239
3240 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3241 /* if error or not fitting 802.3 header, return */
3242 if (res < 14)
3243 return;
3244
3245 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3246 {
3247 if (cmsg->cmsg_level == SOL_SOCKET &&
3248 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3249 int *ack;
3250
3251 found_wifi = 1;
3252 ack = (void *)CMSG_DATA(cmsg);
3253 acked = *ack;
3254 }
3255
3256 if (cmsg->cmsg_level == SOL_PACKET &&
3257 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3258 struct sock_extended_err *err =
3259 (struct sock_extended_err *)CMSG_DATA(cmsg);
3260
3261 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3262 found_ee = 1;
3263 }
3264 }
3265
3266 if (!found_ee || !found_wifi)
3267 return;
3268
3269 memset(&event, 0, sizeof(event));
3270 event.eapol_tx_status.dst = data;
3271 event.eapol_tx_status.data = data + 14;
3272 event.eapol_tx_status.data_len = res - 14;
3273 event.eapol_tx_status.ack = acked;
3274 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3275}
3276
3277
3278static int nl80211_init_bss(struct i802_bss *bss)
3279{
3280 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3281 if (!bss->nl_cb)
3282 return -1;
3283
3284 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3285 no_seq_check, NULL);
3286 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3287 process_bss_event, bss);
3288
3289 return 0;
3290}
3291
3292
3293static void nl80211_destroy_bss(struct i802_bss *bss)
3294{
3295 nl_cb_put(bss->nl_cb);
3296 bss->nl_cb = NULL;
3297}
3298
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300/**
3301 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3302 * @ctx: context to be used when calling wpa_supplicant functions,
3303 * e.g., wpa_supplicant_event()
3304 * @ifname: interface name, e.g., wlan0
3305 * @global_priv: private driver global data from global_init()
3306 * Returns: Pointer to private data, %NULL on failure
3307 */
3308static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3309 void *global_priv)
3310{
3311 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 struct rfkill_config *rcfg;
3313 struct i802_bss *bss;
3314
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003315 if (global_priv == NULL)
3316 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 drv = os_zalloc(sizeof(*drv));
3318 if (drv == NULL)
3319 return NULL;
3320 drv->global = global_priv;
3321 drv->ctx = ctx;
3322 bss = &drv->first_bss;
3323 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003324 bss->ctx = ctx;
3325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
3327 drv->monitor_ifidx = -1;
3328 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003329 drv->eapol_tx_sock = -1;
3330 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331
3332 if (wpa_driver_nl80211_init_nl(drv)) {
3333 os_free(drv);
3334 return NULL;
3335 }
3336
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003337 if (nl80211_init_bss(bss))
3338 goto failed;
3339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 nl80211_get_phy_name(drv);
3341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 rcfg = os_zalloc(sizeof(*rcfg));
3343 if (rcfg == NULL)
3344 goto failed;
3345 rcfg->ctx = drv;
3346 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3347 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3348 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3349 drv->rfkill = rfkill_init(rcfg);
3350 if (drv->rfkill == NULL) {
3351 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3352 os_free(rcfg);
3353 }
3354
3355 if (wpa_driver_nl80211_finish_drv_init(drv))
3356 goto failed;
3357
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003358 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3359 if (drv->eapol_tx_sock < 0)
3360 goto failed;
3361
3362 if (drv->data_tx_status) {
3363 int enabled = 1;
3364
3365 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3366 &enabled, sizeof(enabled)) < 0) {
3367 wpa_printf(MSG_DEBUG,
3368 "nl80211: wifi status sockopt failed\n");
3369 drv->data_tx_status = 0;
3370 if (!drv->use_monitor)
3371 drv->capa.flags &=
3372 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3373 } else {
3374 eloop_register_read_sock(drv->eapol_tx_sock,
3375 wpa_driver_nl80211_handle_eapol_tx_status,
3376 drv, NULL);
3377 }
3378 }
3379
3380 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003382 drv->in_interface_list = 1;
3383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384
3385 return bss;
3386
3387failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003388 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389 return NULL;
3390}
3391
3392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003393static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 struct nl_handle *nl_handle,
3395 u16 type, const u8 *match, size_t match_len)
3396{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003397 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 struct nl_msg *msg;
3399 int ret = -1;
3400
3401 msg = nlmsg_alloc();
3402 if (!msg)
3403 return -1;
3404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3406 type, nl_handle);
3407 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3408 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003410 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3411
3412 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3414 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3415
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003416 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417 msg = NULL;
3418 if (ret) {
3419 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3420 "failed (type=%u): ret=%d (%s)",
3421 type, ret, strerror(-ret));
3422 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3423 match, match_len);
3424 goto nla_put_failure;
3425 }
3426 ret = 0;
3427nla_put_failure:
3428 nlmsg_free(msg);
3429 return ret;
3430}
3431
3432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003433static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3434{
3435 struct wpa_driver_nl80211_data *drv = bss->drv;
3436
3437 if (bss->nl_mgmt) {
3438 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3439 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3440 return -1;
3441 }
3442
3443 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3444 if (bss->nl_mgmt == NULL)
3445 return -1;
3446
3447 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3448 wpa_driver_nl80211_event_receive, bss->nl_cb,
3449 bss->nl_mgmt);
3450
3451 return 0;
3452}
3453
3454
3455static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 const u8 *match, size_t match_len)
3457{
3458 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003459 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 type, match, match_len);
3461}
3462
3463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003464static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 struct wpa_driver_nl80211_data *drv = bss->drv;
3467
3468 if (nl80211_alloc_mgmt_handle(bss))
3469 return -1;
3470 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3471 "handle %p", bss->nl_mgmt);
3472
3473#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003474 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003475 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 return -1;
3477 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003478 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 return -1;
3480 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003481 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 return -1;
3483 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003484 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003486#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3487#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003489 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3491 6) < 0)
3492 return -1;
3493 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003494 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 (u8 *) "\x7f\x50\x6f\x9a\x09",
3496 5) < 0)
3497 return -1;
3498#endif /* CONFIG_P2P */
3499#ifdef CONFIG_IEEE80211W
3500 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003501 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 return -1;
3503#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003504#ifdef CONFIG_TDLS
3505 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3506 /* TDLS Discovery Response */
3507 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3508 0)
3509 return -1;
3510 }
3511#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512
3513 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003514 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 return -1;
3516 else
3517 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3518 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003520 /* WNM - BSS Transition Management Request */
3521 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3522 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003523 /* WNM-Sleep Mode Response */
3524 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3525 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003527 return 0;
3528}
3529
3530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003531static int nl80211_register_spurious_class3(struct i802_bss *bss)
3532{
3533 struct wpa_driver_nl80211_data *drv = bss->drv;
3534 struct nl_msg *msg;
3535 int ret = -1;
3536
3537 msg = nlmsg_alloc();
3538 if (!msg)
3539 return -1;
3540
3541 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3542
3543 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3544
3545 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3546 msg = NULL;
3547 if (ret) {
3548 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3549 "failed: ret=%d (%s)",
3550 ret, strerror(-ret));
3551 goto nla_put_failure;
3552 }
3553 ret = 0;
3554nla_put_failure:
3555 nlmsg_free(msg);
3556 return ret;
3557}
3558
3559
3560static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3561{
3562 static const int stypes[] = {
3563 WLAN_FC_STYPE_AUTH,
3564 WLAN_FC_STYPE_ASSOC_REQ,
3565 WLAN_FC_STYPE_REASSOC_REQ,
3566 WLAN_FC_STYPE_DISASSOC,
3567 WLAN_FC_STYPE_DEAUTH,
3568 WLAN_FC_STYPE_ACTION,
3569 WLAN_FC_STYPE_PROBE_REQ,
3570/* Beacon doesn't work as mac80211 doesn't currently allow
3571 * it, but it wouldn't really be the right thing anyway as
3572 * it isn't per interface ... maybe just dump the scan
3573 * results periodically for OLBC?
3574 */
3575// WLAN_FC_STYPE_BEACON,
3576 };
3577 unsigned int i;
3578
3579 if (nl80211_alloc_mgmt_handle(bss))
3580 return -1;
3581 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3582 "handle %p", bss->nl_mgmt);
3583
3584 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3585 if (nl80211_register_frame(bss, bss->nl_mgmt,
3586 (WLAN_FC_TYPE_MGMT << 2) |
3587 (stypes[i] << 4),
3588 NULL, 0) < 0) {
3589 goto out_err;
3590 }
3591 }
3592
3593 if (nl80211_register_spurious_class3(bss))
3594 goto out_err;
3595
3596 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3597 goto out_err;
3598
3599 return 0;
3600
3601out_err:
3602 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3603 nl_destroy_handles(&bss->nl_mgmt);
3604 return -1;
3605}
3606
3607
3608static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3609{
3610 if (nl80211_alloc_mgmt_handle(bss))
3611 return -1;
3612 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3613 "handle %p (device SME)", bss->nl_mgmt);
3614
3615 if (nl80211_register_frame(bss, bss->nl_mgmt,
3616 (WLAN_FC_TYPE_MGMT << 2) |
3617 (WLAN_FC_STYPE_ACTION << 4),
3618 NULL, 0) < 0)
3619 goto out_err;
3620
3621 return 0;
3622
3623out_err:
3624 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3625 nl_destroy_handles(&bss->nl_mgmt);
3626 return -1;
3627}
3628
3629
3630static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3631{
3632 if (bss->nl_mgmt == NULL)
3633 return;
3634 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3635 "(%s)", bss->nl_mgmt, reason);
3636 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3637 nl_destroy_handles(&bss->nl_mgmt);
3638
3639 nl80211_put_wiphy_data_ap(bss);
3640}
3641
3642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3644{
3645 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3646}
3647
3648
3649static int
3650wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3651{
3652 struct i802_bss *bss = &drv->first_bss;
3653 int send_rfkill_event = 0;
3654
3655 drv->ifindex = if_nametoindex(bss->ifname);
3656 drv->first_bss.ifindex = drv->ifindex;
3657
3658#ifndef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003659 /*
3660 * Make sure the interface starts up in station mode unless this is a
3661 * dynamically added interface (e.g., P2P) that was already configured
3662 * with proper iftype.
3663 */
3664 if (drv->ifindex != drv->global->if_add_ifindex &&
3665 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
3666 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 "use managed mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003668 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 }
3670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003671 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 if (rfkill_is_blocked(drv->rfkill)) {
3673 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3674 "interface '%s' due to rfkill",
3675 bss->ifname);
3676 drv->if_disabled = 1;
3677 send_rfkill_event = 1;
3678 } else {
3679 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3680 "interface '%s' UP", bss->ifname);
3681 return -1;
3682 }
3683 }
3684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003685 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 1, IF_OPER_DORMANT);
3687#endif /* HOSTAPD */
3688
3689 if (wpa_driver_nl80211_capa(drv))
3690 return -1;
3691
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003692 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3693 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 return -1;
3695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 if (send_rfkill_event) {
3697 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3698 drv, drv->ctx);
3699 }
3700
3701 return 0;
3702}
3703
3704
3705static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3706{
3707 struct nl_msg *msg;
3708
3709 msg = nlmsg_alloc();
3710 if (!msg)
3711 return -ENOMEM;
3712
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003713 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3715
3716 return send_and_recv_msgs(drv, msg, NULL, NULL);
3717 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003718 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 return -ENOBUFS;
3720}
3721
3722
3723/**
3724 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003725 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 *
3727 * Shut down driver interface and processing of driver events. Free
3728 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3729 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003730static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 struct wpa_driver_nl80211_data *drv = bss->drv;
3733
Dmitry Shmidt04949592012-07-19 12:16:46 -07003734 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003735 if (drv->data_tx_status)
3736 eloop_unregister_read_sock(drv->eapol_tx_sock);
3737 if (drv->eapol_tx_sock >= 0)
3738 close(drv->eapol_tx_sock);
3739
3740 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003741 wpa_driver_nl80211_probe_req_report(bss, 0);
3742 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003743 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3744 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3746 "interface %s from bridge %s: %s",
3747 bss->ifname, bss->brname, strerror(errno));
3748 }
3749 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003750 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3752 "bridge %s: %s",
3753 bss->brname, strerror(errno));
3754 }
3755
3756 nl80211_remove_monitor_interface(drv);
3757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003758 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 wpa_driver_nl80211_del_beacon(drv);
3760
3761#ifdef HOSTAPD
3762 if (drv->last_freq_ht) {
3763 /* Clear HT flags from the driver */
3764 struct hostapd_freq_params freq;
3765 os_memset(&freq, 0, sizeof(freq));
3766 freq.freq = drv->last_freq;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003767 wpa_driver_nl80211_set_freq(bss, &freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 }
3769
3770 if (drv->eapol_sock >= 0) {
3771 eloop_unregister_read_sock(drv->eapol_sock);
3772 close(drv->eapol_sock);
3773 }
3774
3775 if (drv->if_indices != drv->default_if_indices)
3776 os_free(drv->if_indices);
3777#endif /* HOSTAPD */
3778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003779 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3781
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003782 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3783 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784 rfkill_deinit(drv->rfkill);
3785
3786 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003788 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
3789 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
3790 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 nl_cb_put(drv->nl_cb);
3793
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003794 nl80211_destroy_bss(&drv->first_bss);
3795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 os_free(drv->filter_ssids);
3797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003798 os_free(drv->auth_ie);
3799
3800 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 dl_list_del(&drv->list);
3802
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003803 os_free(drv->extended_capa);
3804 os_free(drv->extended_capa_mask);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805 os_free(drv);
3806}
3807
3808
3809/**
3810 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
3811 * @eloop_ctx: Driver private data
3812 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3813 *
3814 * This function can be used as registered timeout when starting a scan to
3815 * generate a scan completed event if the driver does not report this.
3816 */
3817static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3818{
3819 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003822 drv->ap_scan_as_station);
3823 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 }
3825 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3826 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3827}
3828
3829
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003830static struct nl_msg *
3831nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
3832 struct wpa_driver_scan_params *params)
3833{
3834 struct nl_msg *msg;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003835 size_t i;
3836
3837 msg = nlmsg_alloc();
3838 if (!msg)
3839 return NULL;
3840
3841 nl80211_cmd(drv, msg, 0, cmd);
3842
3843 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0)
3844 goto fail;
3845
3846 if (params->num_ssids) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003847 struct nlattr *ssids;
3848
3849 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003850 if (ssids == NULL)
3851 goto fail;
3852 for (i = 0; i < params->num_ssids; i++) {
3853 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3854 params->ssids[i].ssid,
3855 params->ssids[i].ssid_len);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003856 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
3857 params->ssids[i].ssid) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003858 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003860 nla_nest_end(msg, ssids);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003861 }
3862
3863 if (params->extra_ies) {
3864 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3865 params->extra_ies, params->extra_ies_len);
3866 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
3867 params->extra_ies) < 0)
3868 goto fail;
3869 }
3870
3871 if (params->freqs) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003872 struct nlattr *freqs;
3873 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874 if (freqs == NULL)
3875 goto fail;
3876 for (i = 0; params->freqs[i]; i++) {
3877 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3878 "MHz", params->freqs[i]);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003879 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003881 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003882 nla_nest_end(msg, freqs);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003883 }
3884
3885 os_free(drv->filter_ssids);
3886 drv->filter_ssids = params->filter_ssids;
3887 params->filter_ssids = NULL;
3888 drv->num_filter_ssids = params->num_filter_ssids;
3889
3890 return msg;
3891
3892fail:
3893 nlmsg_free(msg);
3894 return NULL;
3895}
3896
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898/**
3899 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003900 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 * @params: Scan parameters
3902 * Returns: 0 on success, -1 on failure
3903 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003904static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 struct wpa_driver_scan_params *params)
3906{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003908 int ret = -1, timeout;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003909 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003911 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003912 drv->scan_for_auth = 0;
3913
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003914 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params);
3915 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003918 if (params->p2p_probe) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003919 struct nlattr *rates;
3920
Dmitry Shmidt04949592012-07-19 12:16:46 -07003921 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3922
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003923 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003924 if (rates == NULL)
3925 goto nla_put_failure;
3926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 /*
3928 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3929 * by masking out everything else apart from the OFDM rates 6,
3930 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3931 * rates are left enabled.
3932 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003933 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003934 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003935 nla_nest_end(msg, rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003936
3937 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
3938 }
3939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003940 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3941 msg = NULL;
3942 if (ret) {
3943 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3944 "(%s)", ret, strerror(-ret));
3945#ifdef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003946 if (is_ap_interface(drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 /*
3948 * mac80211 does not allow scan requests in AP mode, so
3949 * try to do this in station mode.
3950 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003951 if (wpa_driver_nl80211_set_mode(
3952 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 goto nla_put_failure;
3954
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003955 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003956 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957 goto nla_put_failure;
3958 }
3959
3960 /* Restore AP mode when processing scan results */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003961 drv->ap_scan_as_station = drv->nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 ret = 0;
3963 } else
3964 goto nla_put_failure;
3965#else /* HOSTAPD */
3966 goto nla_put_failure;
3967#endif /* HOSTAPD */
3968 }
3969
3970 /* Not all drivers generate "scan completed" wireless event, so try to
3971 * read results after a timeout. */
3972 timeout = 10;
3973 if (drv->scan_complete_events) {
3974 /*
3975 * The driver seems to deliver events to notify when scan is
3976 * complete, so use longer timeout to avoid race conditions
3977 * with scanning and following association request.
3978 */
3979 timeout = 30;
3980 }
3981 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3982 "seconds", ret, timeout);
3983 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3984 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3985 drv, drv->ctx);
3986
3987nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003989 return ret;
3990}
3991
3992
3993/**
3994 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3995 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3996 * @params: Scan parameters
3997 * @interval: Interval between scan cycles in milliseconds
3998 * Returns: 0 on success, -1 on failure or if not supported
3999 */
4000static int wpa_driver_nl80211_sched_scan(void *priv,
4001 struct wpa_driver_scan_params *params,
4002 u32 interval)
4003{
4004 struct i802_bss *bss = priv;
4005 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004006 int ret = -1;
4007 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004008 size_t i;
4009
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004010 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4011
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004012#ifdef ANDROID
4013 if (!drv->capa.sched_scan_supported)
4014 return android_pno_start(bss, params);
4015#endif /* ANDROID */
4016
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004017 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params);
4018 if (!msg)
4019 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004020
4021 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4022
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004023 if ((drv->num_filter_ssids &&
4024 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4025 params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004026 struct nlattr *match_sets;
4027 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028 if (match_sets == NULL)
4029 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004030
4031 for (i = 0; i < drv->num_filter_ssids; i++) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004032 struct nlattr *match_set_ssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004033 wpa_hexdump_ascii(MSG_MSGDUMP,
4034 "nl80211: Sched scan filter SSID",
4035 drv->filter_ssids[i].ssid,
4036 drv->filter_ssids[i].ssid_len);
4037
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004038 match_set_ssid = nla_nest_start(msg, i + 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004039 if (match_set_ssid == NULL)
4040 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004041 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004042 drv->filter_ssids[i].ssid_len,
4043 drv->filter_ssids[i].ssid);
4044
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004045 nla_nest_end(msg, match_set_ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 }
4047
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004048 if (params->filter_rssi) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004049 struct nlattr *match_set_rssi;
4050 match_set_rssi = nla_nest_start(msg, 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004051 if (match_set_rssi == NULL)
4052 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004053 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004054 params->filter_rssi);
4055 wpa_printf(MSG_MSGDUMP,
4056 "nl80211: Sched scan RSSI filter %d dBm",
4057 params->filter_rssi);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004058 nla_nest_end(msg, match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004059 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004060
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004061 nla_nest_end(msg, match_sets);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004062 }
4063
4064 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4065
4066 /* TODO: if we get an error here, we should fall back to normal scan */
4067
4068 msg = NULL;
4069 if (ret) {
4070 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4071 "ret=%d (%s)", ret, strerror(-ret));
4072 goto nla_put_failure;
4073 }
4074
4075 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4076 "scan interval %d msec", ret, interval);
4077
4078nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004079 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004080 return ret;
4081}
4082
4083
4084/**
4085 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4086 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4087 * Returns: 0 on success, -1 on failure or if not supported
4088 */
4089static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4090{
4091 struct i802_bss *bss = priv;
4092 struct wpa_driver_nl80211_data *drv = bss->drv;
4093 int ret = 0;
4094 struct nl_msg *msg;
4095
4096#ifdef ANDROID
4097 if (!drv->capa.sched_scan_supported)
4098 return android_pno_stop(bss);
4099#endif /* ANDROID */
4100
4101 msg = nlmsg_alloc();
4102 if (!msg)
4103 return -1;
4104
4105 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4106
4107 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4108
4109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4110 msg = NULL;
4111 if (ret) {
4112 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4113 "ret=%d (%s)", ret, strerror(-ret));
4114 goto nla_put_failure;
4115 }
4116
4117 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4118
4119nla_put_failure:
4120 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121 return ret;
4122}
4123
4124
4125static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4126{
4127 const u8 *end, *pos;
4128
4129 if (ies == NULL)
4130 return NULL;
4131
4132 pos = ies;
4133 end = ies + ies_len;
4134
4135 while (pos + 1 < end) {
4136 if (pos + 2 + pos[1] > end)
4137 break;
4138 if (pos[0] == ie)
4139 return pos;
4140 pos += 2 + pos[1];
4141 }
4142
4143 return NULL;
4144}
4145
4146
4147static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4148 const u8 *ie, size_t ie_len)
4149{
4150 const u8 *ssid;
4151 size_t i;
4152
4153 if (drv->filter_ssids == NULL)
4154 return 0;
4155
4156 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4157 if (ssid == NULL)
4158 return 1;
4159
4160 for (i = 0; i < drv->num_filter_ssids; i++) {
4161 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4162 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4163 0)
4164 return 0;
4165 }
4166
4167 return 1;
4168}
4169
4170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171static int bss_info_handler(struct nl_msg *msg, void *arg)
4172{
4173 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4174 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4175 struct nlattr *bss[NL80211_BSS_MAX + 1];
4176 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4177 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4178 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4179 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4180 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4181 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4182 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4183 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4184 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
4185 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
4186 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
4187 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
4188 };
4189 struct nl80211_bss_info_arg *_arg = arg;
4190 struct wpa_scan_results *res = _arg->res;
4191 struct wpa_scan_res **tmp;
4192 struct wpa_scan_res *r;
4193 const u8 *ie, *beacon_ie;
4194 size_t ie_len, beacon_ie_len;
4195 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004196 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197
4198 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4199 genlmsg_attrlen(gnlh, 0), NULL);
4200 if (!tb[NL80211_ATTR_BSS])
4201 return NL_SKIP;
4202 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4203 bss_policy))
4204 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004205 if (bss[NL80211_BSS_STATUS]) {
4206 enum nl80211_bss_status status;
4207 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4208 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4209 bss[NL80211_BSS_FREQUENCY]) {
4210 _arg->assoc_freq =
4211 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4212 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4213 _arg->assoc_freq);
4214 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004215 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4216 bss[NL80211_BSS_BSSID]) {
4217 os_memcpy(_arg->assoc_bssid,
4218 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4219 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4220 MACSTR, MAC2STR(_arg->assoc_bssid));
4221 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03004222 }
4223 if (!res)
4224 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4226 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4227 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4228 } else {
4229 ie = NULL;
4230 ie_len = 0;
4231 }
4232 if (bss[NL80211_BSS_BEACON_IES]) {
4233 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4234 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4235 } else {
4236 beacon_ie = NULL;
4237 beacon_ie_len = 0;
4238 }
4239
4240 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4241 ie ? ie_len : beacon_ie_len))
4242 return NL_SKIP;
4243
4244 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
4245 if (r == NULL)
4246 return NL_SKIP;
4247 if (bss[NL80211_BSS_BSSID])
4248 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4249 ETH_ALEN);
4250 if (bss[NL80211_BSS_FREQUENCY])
4251 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4252 if (bss[NL80211_BSS_BEACON_INTERVAL])
4253 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4254 if (bss[NL80211_BSS_CAPABILITY])
4255 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
4256 r->flags |= WPA_SCAN_NOISE_INVALID;
4257 if (bss[NL80211_BSS_SIGNAL_MBM]) {
4258 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
4259 r->level /= 100; /* mBm to dBm */
4260 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4261 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4262 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004263 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 } else
4265 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
4266 if (bss[NL80211_BSS_TSF])
4267 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
4268 if (bss[NL80211_BSS_SEEN_MS_AGO])
4269 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
4270 r->ie_len = ie_len;
4271 pos = (u8 *) (r + 1);
4272 if (ie) {
4273 os_memcpy(pos, ie, ie_len);
4274 pos += ie_len;
4275 }
4276 r->beacon_ie_len = beacon_ie_len;
4277 if (beacon_ie)
4278 os_memcpy(pos, beacon_ie, beacon_ie_len);
4279
4280 if (bss[NL80211_BSS_STATUS]) {
4281 enum nl80211_bss_status status;
4282 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4283 switch (status) {
4284 case NL80211_BSS_STATUS_AUTHENTICATED:
4285 r->flags |= WPA_SCAN_AUTHENTICATED;
4286 break;
4287 case NL80211_BSS_STATUS_ASSOCIATED:
4288 r->flags |= WPA_SCAN_ASSOCIATED;
4289 break;
4290 default:
4291 break;
4292 }
4293 }
4294
Jouni Malinen87fd2792011-05-16 18:35:42 +03004295 /*
4296 * cfg80211 maintains separate BSS table entries for APs if the same
4297 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4298 * not use frequency as a separate key in the BSS table, so filter out
4299 * duplicated entries. Prefer associated BSS entry in such a case in
4300 * order to get the correct frequency into the BSS table.
4301 */
4302 for (i = 0; i < res->num; i++) {
4303 const u8 *s1, *s2;
4304 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4305 continue;
4306
4307 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4308 res->res[i]->ie_len, WLAN_EID_SSID);
4309 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4310 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4311 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4312 continue;
4313
4314 /* Same BSSID,SSID was already included in scan results */
4315 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4316 "for " MACSTR, MAC2STR(r->bssid));
4317
4318 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4319 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4320 os_free(res->res[i]);
4321 res->res[i] = r;
4322 } else
4323 os_free(r);
4324 return NL_SKIP;
4325 }
4326
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004327 tmp = os_realloc_array(res->res, res->num + 1,
4328 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329 if (tmp == NULL) {
4330 os_free(r);
4331 return NL_SKIP;
4332 }
4333 tmp[res->num++] = r;
4334 res->res = tmp;
4335
4336 return NL_SKIP;
4337}
4338
4339
4340static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4341 const u8 *addr)
4342{
4343 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4344 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4345 "mismatch (" MACSTR ")", MAC2STR(addr));
4346 wpa_driver_nl80211_mlme(drv, addr,
4347 NL80211_CMD_DEAUTHENTICATE,
4348 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4349 }
4350}
4351
4352
4353static void wpa_driver_nl80211_check_bss_status(
4354 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4355{
4356 size_t i;
4357
4358 for (i = 0; i < res->num; i++) {
4359 struct wpa_scan_res *r = res->res[i];
4360 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4361 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4362 "indicates BSS status with " MACSTR
4363 " as authenticated",
4364 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4367 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4368 0) {
4369 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4370 " in local state (auth=" MACSTR
4371 " assoc=" MACSTR ")",
4372 MAC2STR(drv->auth_bssid),
4373 MAC2STR(drv->bssid));
4374 clear_state_mismatch(drv, r->bssid);
4375 }
4376 }
4377
4378 if (r->flags & WPA_SCAN_ASSOCIATED) {
4379 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4380 "indicate BSS status with " MACSTR
4381 " as associated",
4382 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 !drv->associated) {
4385 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4386 "(not associated) does not match "
4387 "with BSS state");
4388 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004389 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4391 0) {
4392 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4393 "(associated with " MACSTR ") does "
4394 "not match with BSS state",
4395 MAC2STR(drv->bssid));
4396 clear_state_mismatch(drv, r->bssid);
4397 clear_state_mismatch(drv, drv->bssid);
4398 }
4399 }
4400 }
4401}
4402
4403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404static struct wpa_scan_results *
4405nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4406{
4407 struct nl_msg *msg;
4408 struct wpa_scan_results *res;
4409 int ret;
4410 struct nl80211_bss_info_arg arg;
4411
4412 res = os_zalloc(sizeof(*res));
4413 if (res == NULL)
4414 return NULL;
4415 msg = nlmsg_alloc();
4416 if (!msg)
4417 goto nla_put_failure;
4418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004419 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4421
4422 arg.drv = drv;
4423 arg.res = res;
4424 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4425 msg = NULL;
4426 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004427 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4428 "BSSes)", (unsigned long) res->num);
4429 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 return res;
4431 }
4432 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4433 "(%s)", ret, strerror(-ret));
4434nla_put_failure:
4435 nlmsg_free(msg);
4436 wpa_scan_results_free(res);
4437 return NULL;
4438}
4439
4440
4441/**
4442 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4443 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4444 * Returns: Scan results on success, -1 on failure
4445 */
4446static struct wpa_scan_results *
4447wpa_driver_nl80211_get_scan_results(void *priv)
4448{
4449 struct i802_bss *bss = priv;
4450 struct wpa_driver_nl80211_data *drv = bss->drv;
4451 struct wpa_scan_results *res;
4452
4453 res = nl80211_get_scan_results(drv);
4454 if (res)
4455 wpa_driver_nl80211_check_bss_status(drv, res);
4456 return res;
4457}
4458
4459
4460static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4461{
4462 struct wpa_scan_results *res;
4463 size_t i;
4464
4465 res = nl80211_get_scan_results(drv);
4466 if (res == NULL) {
4467 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4468 return;
4469 }
4470
4471 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4472 for (i = 0; i < res->num; i++) {
4473 struct wpa_scan_res *r = res->res[i];
4474 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4475 (int) i, (int) res->num, MAC2STR(r->bssid),
4476 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4477 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4478 }
4479
4480 wpa_scan_results_free(res);
4481}
4482
4483
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004484static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 enum wpa_alg alg, const u8 *addr,
4486 int key_idx, int set_tx,
4487 const u8 *seq, size_t seq_len,
4488 const u8 *key, size_t key_len)
4489{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 struct wpa_driver_nl80211_data *drv = bss->drv;
4491 int ifindex = if_nametoindex(ifname);
4492 struct nl_msg *msg;
4493 int ret;
4494
4495 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4496 "set_tx=%d seq_len=%lu key_len=%lu",
4497 __func__, ifindex, alg, addr, key_idx, set_tx,
4498 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004499#ifdef CONFIG_TDLS
4500 if (key_idx == -1)
4501 key_idx = 0;
4502#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503
4504 msg = nlmsg_alloc();
4505 if (!msg)
4506 return -ENOMEM;
4507
4508 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004509 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004511 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4513 switch (alg) {
4514 case WPA_ALG_WEP:
4515 if (key_len == 5)
4516 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4517 WLAN_CIPHER_SUITE_WEP40);
4518 else
4519 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4520 WLAN_CIPHER_SUITE_WEP104);
4521 break;
4522 case WPA_ALG_TKIP:
4523 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4524 WLAN_CIPHER_SUITE_TKIP);
4525 break;
4526 case WPA_ALG_CCMP:
4527 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4528 WLAN_CIPHER_SUITE_CCMP);
4529 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004530 case WPA_ALG_GCMP:
4531 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4532 WLAN_CIPHER_SUITE_GCMP);
4533 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534 case WPA_ALG_IGTK:
4535 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4536 WLAN_CIPHER_SUITE_AES_CMAC);
4537 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004538 case WPA_ALG_SMS4:
4539 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4540 WLAN_CIPHER_SUITE_SMS4);
4541 break;
4542 case WPA_ALG_KRK:
4543 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4544 WLAN_CIPHER_SUITE_KRK);
4545 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546 default:
4547 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4548 "algorithm %d", __func__, alg);
4549 nlmsg_free(msg);
4550 return -1;
4551 }
4552 }
4553
4554 if (seq && seq_len)
4555 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4556
4557 if (addr && !is_broadcast_ether_addr(addr)) {
4558 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4559 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4560
4561 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4562 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4563 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4564 NL80211_KEYTYPE_GROUP);
4565 }
4566 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004567 struct nlattr *types;
4568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004570
4571 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 if (!types)
4573 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004574 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4575 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576 }
4577 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4578 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4579
4580 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4581 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4582 ret = 0;
4583 if (ret)
4584 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4585 ret, strerror(-ret));
4586
4587 /*
4588 * If we failed or don't need to set the default TX key (below),
4589 * we're done here.
4590 */
4591 if (ret || !set_tx || alg == WPA_ALG_NONE)
4592 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004593 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 !is_broadcast_ether_addr(addr))
4595 return ret;
4596
4597 msg = nlmsg_alloc();
4598 if (!msg)
4599 return -ENOMEM;
4600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004601 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4603 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4604 if (alg == WPA_ALG_IGTK)
4605 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4606 else
4607 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4608 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004609 struct nlattr *types;
4610
4611 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 if (!types)
4613 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004614 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4615 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004617 struct nlattr *types;
4618
4619 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 if (!types)
4621 goto nla_put_failure;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004622 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4623 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624 }
4625
4626 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4627 if (ret == -ENOENT)
4628 ret = 0;
4629 if (ret)
4630 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4631 "err=%d %s)", ret, strerror(-ret));
4632 return ret;
4633
4634nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004635 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 return -ENOBUFS;
4637}
4638
4639
4640static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4641 int key_idx, int defkey,
4642 const u8 *seq, size_t seq_len,
4643 const u8 *key, size_t key_len)
4644{
4645 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4646 if (!key_attr)
4647 return -1;
4648
4649 if (defkey && alg == WPA_ALG_IGTK)
4650 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4651 else if (defkey)
4652 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4653
4654 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4655
4656 switch (alg) {
4657 case WPA_ALG_WEP:
4658 if (key_len == 5)
4659 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4660 WLAN_CIPHER_SUITE_WEP40);
4661 else
4662 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4663 WLAN_CIPHER_SUITE_WEP104);
4664 break;
4665 case WPA_ALG_TKIP:
4666 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4667 break;
4668 case WPA_ALG_CCMP:
4669 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4670 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004671 case WPA_ALG_GCMP:
4672 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
4673 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 case WPA_ALG_IGTK:
4675 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4676 WLAN_CIPHER_SUITE_AES_CMAC);
4677 break;
4678 default:
4679 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4680 "algorithm %d", __func__, alg);
4681 return -1;
4682 }
4683
4684 if (seq && seq_len)
4685 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4686
4687 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4688
4689 nla_nest_end(msg, key_attr);
4690
4691 return 0;
4692 nla_put_failure:
4693 return -1;
4694}
4695
4696
4697static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4698 struct nl_msg *msg)
4699{
4700 int i, privacy = 0;
4701 struct nlattr *nl_keys, *nl_key;
4702
4703 for (i = 0; i < 4; i++) {
4704 if (!params->wep_key[i])
4705 continue;
4706 privacy = 1;
4707 break;
4708 }
4709 if (params->wps == WPS_MODE_PRIVACY)
4710 privacy = 1;
4711 if (params->pairwise_suite &&
4712 params->pairwise_suite != WPA_CIPHER_NONE)
4713 privacy = 1;
4714
4715 if (!privacy)
4716 return 0;
4717
4718 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4719
4720 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4721 if (!nl_keys)
4722 goto nla_put_failure;
4723
4724 for (i = 0; i < 4; i++) {
4725 if (!params->wep_key[i])
4726 continue;
4727
4728 nl_key = nla_nest_start(msg, i);
4729 if (!nl_key)
4730 goto nla_put_failure;
4731
4732 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4733 params->wep_key[i]);
4734 if (params->wep_key_len[i] == 5)
4735 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4736 WLAN_CIPHER_SUITE_WEP40);
4737 else
4738 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4739 WLAN_CIPHER_SUITE_WEP104);
4740
4741 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4742
4743 if (i == params->wep_tx_keyidx)
4744 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4745
4746 nla_nest_end(msg, nl_key);
4747 }
4748 nla_nest_end(msg, nl_keys);
4749
4750 return 0;
4751
4752nla_put_failure:
4753 return -ENOBUFS;
4754}
4755
4756
4757static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
4758 const u8 *addr, int cmd, u16 reason_code,
4759 int local_state_change)
4760{
4761 int ret = -1;
4762 struct nl_msg *msg;
4763
4764 msg = nlmsg_alloc();
4765 if (!msg)
4766 return -1;
4767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769
4770 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4771 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004772 if (addr)
4773 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774 if (local_state_change)
4775 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4776
4777 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4778 msg = NULL;
4779 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004780 wpa_dbg(drv->ctx, MSG_DEBUG,
4781 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4782 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 goto nla_put_failure;
4784 }
4785 ret = 0;
4786
4787nla_put_failure:
4788 nlmsg_free(msg);
4789 return ret;
4790}
4791
4792
4793static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004794 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004796 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797 drv->associated = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004798 drv->ignore_next_local_disconnect = 0;
4799 /* Disconnect command doesn't need BSSID - it uses cached value */
4800 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 reason_code, 0);
4802}
4803
4804
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004805static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
4806 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004808 struct wpa_driver_nl80211_data *drv = bss->drv;
4809 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004810 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4812 __func__, MAC2STR(addr), reason_code);
4813 drv->associated = 0;
4814 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4815 return nl80211_leave_ibss(drv);
4816 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
4817 reason_code, 0);
4818}
4819
4820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004821static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4822 struct wpa_driver_auth_params *params)
4823{
4824 int i;
4825
4826 drv->auth_freq = params->freq;
4827 drv->auth_alg = params->auth_alg;
4828 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4829 drv->auth_local_state_change = params->local_state_change;
4830 drv->auth_p2p = params->p2p;
4831
4832 if (params->bssid)
4833 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4834 else
4835 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4836
4837 if (params->ssid) {
4838 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4839 drv->auth_ssid_len = params->ssid_len;
4840 } else
4841 drv->auth_ssid_len = 0;
4842
4843
4844 os_free(drv->auth_ie);
4845 drv->auth_ie = NULL;
4846 drv->auth_ie_len = 0;
4847 if (params->ie) {
4848 drv->auth_ie = os_malloc(params->ie_len);
4849 if (drv->auth_ie) {
4850 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4851 drv->auth_ie_len = params->ie_len;
4852 }
4853 }
4854
4855 for (i = 0; i < 4; i++) {
4856 if (params->wep_key[i] && params->wep_key_len[i] &&
4857 params->wep_key_len[i] <= 16) {
4858 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4859 params->wep_key_len[i]);
4860 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4861 } else
4862 drv->auth_wep_key_len[i] = 0;
4863 }
4864}
4865
4866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004868 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 struct wpa_driver_nl80211_data *drv = bss->drv;
4871 int ret = -1, i;
4872 struct nl_msg *msg;
4873 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004874 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004876 int is_retry;
4877
4878 is_retry = drv->retry_auth;
4879 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880
4881 drv->associated = 0;
4882 os_memset(drv->auth_bssid, 0, ETH_ALEN);
4883 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004884 nlmode = params->p2p ?
4885 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4886 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004887 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 return -1;
4889
4890retry:
4891 msg = nlmsg_alloc();
4892 if (!msg)
4893 return -1;
4894
4895 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4896 drv->ifindex);
4897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004898 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899
4900 for (i = 0; i < 4; i++) {
4901 if (!params->wep_key[i])
4902 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004903 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 NULL, i,
4905 i == params->wep_tx_keyidx, NULL, 0,
4906 params->wep_key[i],
4907 params->wep_key_len[i]);
4908 if (params->wep_tx_keyidx != i)
4909 continue;
4910 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4911 params->wep_key[i], params->wep_key_len[i])) {
4912 nlmsg_free(msg);
4913 return -1;
4914 }
4915 }
4916
4917 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4918 if (params->bssid) {
4919 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4920 MAC2STR(params->bssid));
4921 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4922 }
4923 if (params->freq) {
4924 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4925 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4926 }
4927 if (params->ssid) {
4928 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4929 params->ssid, params->ssid_len);
4930 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4931 params->ssid);
4932 }
4933 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4934 if (params->ie)
4935 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004936 if (params->sae_data) {
4937 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
4938 params->sae_data_len);
4939 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
4940 params->sae_data);
4941 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4943 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4944 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4945 type = NL80211_AUTHTYPE_SHARED_KEY;
4946 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4947 type = NL80211_AUTHTYPE_NETWORK_EAP;
4948 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4949 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004950 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
4951 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 else
4953 goto nla_put_failure;
4954 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4955 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4956 if (params->local_state_change) {
4957 wpa_printf(MSG_DEBUG, " * Local state change only");
4958 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4959 }
4960
4961 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4962 msg = NULL;
4963 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004964 wpa_dbg(drv->ctx, MSG_DEBUG,
4965 "nl80211: MLME command failed (auth): ret=%d (%s)",
4966 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 count++;
4968 if (ret == -EALREADY && count == 1 && params->bssid &&
4969 !params->local_state_change) {
4970 /*
4971 * mac80211 does not currently accept new
4972 * authentication if we are already authenticated. As a
4973 * workaround, force deauthentication and try again.
4974 */
4975 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4976 "after forced deauthentication");
4977 wpa_driver_nl80211_deauthenticate(
4978 bss, params->bssid,
4979 WLAN_REASON_PREV_AUTH_NOT_VALID);
4980 nlmsg_free(msg);
4981 goto retry;
4982 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004983
4984 if (ret == -ENOENT && params->freq && !is_retry) {
4985 /*
4986 * cfg80211 has likely expired the BSS entry even
4987 * though it was previously available in our internal
4988 * BSS table. To recover quickly, start a single
4989 * channel scan on the specified channel.
4990 */
4991 struct wpa_driver_scan_params scan;
4992 int freqs[2];
4993
4994 os_memset(&scan, 0, sizeof(scan));
4995 scan.num_ssids = 1;
4996 if (params->ssid) {
4997 scan.ssids[0].ssid = params->ssid;
4998 scan.ssids[0].ssid_len = params->ssid_len;
4999 }
5000 freqs[0] = params->freq;
5001 freqs[1] = 0;
5002 scan.freqs = freqs;
5003 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5004 "channel scan to refresh cfg80211 BSS "
5005 "entry");
5006 ret = wpa_driver_nl80211_scan(bss, &scan);
5007 if (ret == 0) {
5008 nl80211_copy_auth_params(drv, params);
5009 drv->scan_for_auth = 1;
5010 }
5011 } else if (is_retry) {
5012 /*
5013 * Need to indicate this with an event since the return
5014 * value from the retry is not delivered to core code.
5015 */
5016 union wpa_event_data event;
5017 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5018 "failed");
5019 os_memset(&event, 0, sizeof(event));
5020 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5021 ETH_ALEN);
5022 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5023 &event);
5024 }
5025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 goto nla_put_failure;
5027 }
5028 ret = 0;
5029 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5030 "successfully");
5031
5032nla_put_failure:
5033 nlmsg_free(msg);
5034 return ret;
5035}
5036
5037
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005038static int wpa_driver_nl80211_authenticate_retry(
5039 struct wpa_driver_nl80211_data *drv)
5040{
5041 struct wpa_driver_auth_params params;
5042 struct i802_bss *bss = &drv->first_bss;
5043 int i;
5044
5045 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5046
5047 os_memset(&params, 0, sizeof(params));
5048 params.freq = drv->auth_freq;
5049 params.auth_alg = drv->auth_alg;
5050 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5051 params.local_state_change = drv->auth_local_state_change;
5052 params.p2p = drv->auth_p2p;
5053
5054 if (!is_zero_ether_addr(drv->auth_bssid_))
5055 params.bssid = drv->auth_bssid_;
5056
5057 if (drv->auth_ssid_len) {
5058 params.ssid = drv->auth_ssid;
5059 params.ssid_len = drv->auth_ssid_len;
5060 }
5061
5062 params.ie = drv->auth_ie;
5063 params.ie_len = drv->auth_ie_len;
5064
5065 for (i = 0; i < 4; i++) {
5066 if (drv->auth_wep_key_len[i]) {
5067 params.wep_key[i] = drv->auth_wep_key[i];
5068 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5069 }
5070 }
5071
5072 drv->retry_auth = 1;
5073 return wpa_driver_nl80211_authenticate(bss, &params);
5074}
5075
5076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077struct phy_info_arg {
5078 u16 *num_modes;
5079 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005080 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081};
5082
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005083static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5084 struct nlattr *ampdu_factor,
5085 struct nlattr *ampdu_density,
5086 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005088 if (capa)
5089 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005091 if (ampdu_factor)
5092 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005094 if (ampdu_density)
5095 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5096
5097 if (mcs_set && nla_len(mcs_set) >= 16) {
5098 u8 *mcs;
5099 mcs = nla_data(mcs_set);
5100 os_memcpy(mode->mcs_set, mcs, 16);
5101 }
5102}
5103
5104
5105static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5106 struct nlattr *capa,
5107 struct nlattr *mcs_set)
5108{
5109 if (capa)
5110 mode->vht_capab = nla_get_u32(capa);
5111
5112 if (mcs_set && nla_len(mcs_set) >= 8) {
5113 u8 *mcs;
5114 mcs = nla_data(mcs_set);
5115 os_memcpy(mode->vht_mcs_set, mcs, 8);
5116 }
5117}
5118
5119
5120static void phy_info_freq(struct hostapd_hw_modes *mode,
5121 struct hostapd_channel_data *chan,
5122 struct nlattr *tb_freq[])
5123{
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005124 enum hostapd_hw_mode m;
5125
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005126 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5127 chan->flag = 0;
5128
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005129 if (chan->freq < 4000)
5130 m = HOSTAPD_MODE_IEEE80211B;
5131 else if (chan->freq > 50000)
5132 m = HOSTAPD_MODE_IEEE80211AD;
5133 else
5134 m = HOSTAPD_MODE_IEEE80211A;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005135
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005136 switch (m) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005137 case HOSTAPD_MODE_IEEE80211AD:
5138 chan->chan = (chan->freq - 56160) / 2160;
5139 break;
5140 case HOSTAPD_MODE_IEEE80211A:
5141 chan->chan = chan->freq / 5 - 1000;
5142 break;
5143 case HOSTAPD_MODE_IEEE80211B:
5144 case HOSTAPD_MODE_IEEE80211G:
5145 if (chan->freq == 2484)
5146 chan->chan = 14;
5147 else
5148 chan->chan = (chan->freq - 2407) / 5;
5149 break;
5150 default:
5151 break;
5152 }
5153
5154 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5155 chan->flag |= HOSTAPD_CHAN_DISABLED;
5156 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5157 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5158 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5159 chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5160 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5161 chan->flag |= HOSTAPD_CHAN_RADAR;
5162
5163 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5164 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5165 chan->max_tx_power = nla_get_u32(
5166 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
5167}
5168
5169
5170static int phy_info_freqs(struct phy_info_arg *phy_info,
5171 struct hostapd_hw_modes *mode, struct nlattr *tb)
5172{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5174 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5175 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5176 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5177 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5178 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5179 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
5180 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005181 int new_channels = 0;
5182 struct hostapd_channel_data *channel;
5183 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005185 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005187 if (tb == NULL)
5188 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005190 nla_for_each_nested(nl_freq, tb, rem_freq) {
5191 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5192 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5193 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5194 continue;
5195 new_channels++;
5196 }
5197
5198 channel = os_realloc_array(mode->channels,
5199 mode->num_channels + new_channels,
5200 sizeof(struct hostapd_channel_data));
5201 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202 return NL_SKIP;
5203
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005204 mode->channels = channel;
5205 mode->num_channels += new_channels;
5206
5207 idx = phy_info->last_chan_idx;
5208
5209 nla_for_each_nested(nl_freq, tb, rem_freq) {
5210 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5211 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5212 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5213 continue;
5214 phy_info_freq(mode, &mode->channels[idx], tb_freq);
5215 idx++;
5216 }
5217 phy_info->last_chan_idx = idx;
5218
5219 return NL_OK;
5220}
5221
5222
5223static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5224{
5225 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5226 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
5227 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5228 { .type = NLA_FLAG },
5229 };
5230 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
5231 struct nlattr *nl_rate;
5232 int rem_rate, idx;
5233
5234 if (tb == NULL)
5235 return NL_OK;
5236
5237 nla_for_each_nested(nl_rate, tb, rem_rate) {
5238 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5239 nla_data(nl_rate), nla_len(nl_rate),
5240 rate_policy);
5241 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5242 continue;
5243 mode->num_rates++;
5244 }
5245
5246 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5247 if (!mode->rates)
5248 return NL_SKIP;
5249
5250 idx = 0;
5251
5252 nla_for_each_nested(nl_rate, tb, rem_rate) {
5253 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5254 nla_data(nl_rate), nla_len(nl_rate),
5255 rate_policy);
5256 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5257 continue;
5258 mode->rates[idx] = nla_get_u32(
5259 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005260 idx++;
5261 }
5262
5263 return NL_OK;
5264}
5265
5266
5267static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5268{
5269 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
5270 struct hostapd_hw_modes *mode;
5271 int ret;
5272
5273 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005274 mode = os_realloc_array(phy_info->modes,
5275 *phy_info->num_modes + 1,
5276 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 if (!mode)
5278 return NL_SKIP;
5279 phy_info->modes = mode;
5280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005282 os_memset(mode, 0, sizeof(*mode));
5283 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005284 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005286 phy_info->last_mode = nl_band->nla_type;
5287 phy_info->last_chan_idx = 0;
5288 } else
5289 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005291 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5292 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005293
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005294 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5295 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5296 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5297 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5298 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5299 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5300 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5301 if (ret != NL_OK)
5302 return ret;
5303 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5304 if (ret != NL_OK)
5305 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005307 return NL_OK;
5308}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005311static int phy_info_handler(struct nl_msg *msg, void *arg)
5312{
5313 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5314 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5315 struct phy_info_arg *phy_info = arg;
5316 struct nlattr *nl_band;
5317 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005319 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5320 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005321
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005322 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5323 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005324
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005325 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5326 {
5327 int res = phy_info_band(phy_info, nl_band);
5328 if (res != NL_OK)
5329 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330 }
5331
5332 return NL_SKIP;
5333}
5334
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005337wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5338 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339{
5340 u16 m;
5341 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5342 int i, mode11g_idx = -1;
5343
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005344 /* heuristic to set up modes */
5345 for (m = 0; m < *num_modes; m++) {
5346 if (!modes[m].num_channels)
5347 continue;
5348 if (modes[m].channels[0].freq < 4000) {
5349 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5350 for (i = 0; i < modes[m].num_rates; i++) {
5351 if (modes[m].rates[i] > 200) {
5352 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5353 break;
5354 }
5355 }
5356 } else if (modes[m].channels[0].freq > 50000)
5357 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5358 else
5359 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5360 }
5361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362 /* If only 802.11g mode is included, use it to construct matching
5363 * 802.11b mode data. */
5364
5365 for (m = 0; m < *num_modes; m++) {
5366 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5367 return modes; /* 802.11b already included */
5368 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5369 mode11g_idx = m;
5370 }
5371
5372 if (mode11g_idx < 0)
5373 return modes; /* 2.4 GHz band not supported at all */
5374
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005375 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005376 if (nmodes == NULL)
5377 return modes; /* Could not add 802.11b mode */
5378
5379 mode = &nmodes[*num_modes];
5380 os_memset(mode, 0, sizeof(*mode));
5381 (*num_modes)++;
5382 modes = nmodes;
5383
5384 mode->mode = HOSTAPD_MODE_IEEE80211B;
5385
5386 mode11g = &modes[mode11g_idx];
5387 mode->num_channels = mode11g->num_channels;
5388 mode->channels = os_malloc(mode11g->num_channels *
5389 sizeof(struct hostapd_channel_data));
5390 if (mode->channels == NULL) {
5391 (*num_modes)--;
5392 return modes; /* Could not add 802.11b mode */
5393 }
5394 os_memcpy(mode->channels, mode11g->channels,
5395 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5396
5397 mode->num_rates = 0;
5398 mode->rates = os_malloc(4 * sizeof(int));
5399 if (mode->rates == NULL) {
5400 os_free(mode->channels);
5401 (*num_modes)--;
5402 return modes; /* Could not add 802.11b mode */
5403 }
5404
5405 for (i = 0; i < mode11g->num_rates; i++) {
5406 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5407 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
5408 continue;
5409 mode->rates[mode->num_rates] = mode11g->rates[i];
5410 mode->num_rates++;
5411 if (mode->num_rates == 4)
5412 break;
5413 }
5414
5415 if (mode->num_rates == 0) {
5416 os_free(mode->channels);
5417 os_free(mode->rates);
5418 (*num_modes)--;
5419 return modes; /* No 802.11b rates */
5420 }
5421
5422 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5423 "information");
5424
5425 return modes;
5426}
5427
5428
5429static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5430 int end)
5431{
5432 int c;
5433
5434 for (c = 0; c < mode->num_channels; c++) {
5435 struct hostapd_channel_data *chan = &mode->channels[c];
5436 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5437 chan->flag |= HOSTAPD_CHAN_HT40;
5438 }
5439}
5440
5441
5442static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5443 int end)
5444{
5445 int c;
5446
5447 for (c = 0; c < mode->num_channels; c++) {
5448 struct hostapd_channel_data *chan = &mode->channels[c];
5449 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5450 continue;
5451 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5452 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5453 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5454 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5455 }
5456}
5457
5458
5459static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5460 struct phy_info_arg *results)
5461{
5462 u32 start, end, max_bw;
5463 u16 m;
5464
5465 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5466 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5467 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5468 return;
5469
5470 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5471 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5472 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5473
5474 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5475 start, end, max_bw);
5476 if (max_bw < 40)
5477 return;
5478
5479 for (m = 0; m < *results->num_modes; m++) {
5480 if (!(results->modes[m].ht_capab &
5481 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5482 continue;
5483 nl80211_set_ht40_mode(&results->modes[m], start, end);
5484 }
5485}
5486
5487
5488static void nl80211_reg_rule_sec(struct nlattr *tb[],
5489 struct phy_info_arg *results)
5490{
5491 u32 start, end, max_bw;
5492 u16 m;
5493
5494 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5495 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5496 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5497 return;
5498
5499 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5500 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5501 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5502
5503 if (max_bw < 20)
5504 return;
5505
5506 for (m = 0; m < *results->num_modes; m++) {
5507 if (!(results->modes[m].ht_capab &
5508 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5509 continue;
5510 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5511 }
5512}
5513
5514
5515static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5516{
5517 struct phy_info_arg *results = arg;
5518 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5519 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5520 struct nlattr *nl_rule;
5521 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5522 int rem_rule;
5523 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5524 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5525 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5526 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5527 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5528 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5529 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5530 };
5531
5532 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5533 genlmsg_attrlen(gnlh, 0), NULL);
5534 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5535 !tb_msg[NL80211_ATTR_REG_RULES]) {
5536 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5537 "available");
5538 return NL_SKIP;
5539 }
5540
5541 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5542 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5543
5544 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5545 {
5546 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5547 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5548 nl80211_reg_rule_ht40(tb_rule, results);
5549 }
5550
5551 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5552 {
5553 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5554 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5555 nl80211_reg_rule_sec(tb_rule, results);
5556 }
5557
5558 return NL_SKIP;
5559}
5560
5561
5562static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5563 struct phy_info_arg *results)
5564{
5565 struct nl_msg *msg;
5566
5567 msg = nlmsg_alloc();
5568 if (!msg)
5569 return -ENOMEM;
5570
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005571 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5573}
5574
5575
5576static struct hostapd_hw_modes *
5577wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5578{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005579 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 struct i802_bss *bss = priv;
5581 struct wpa_driver_nl80211_data *drv = bss->drv;
5582 struct nl_msg *msg;
5583 struct phy_info_arg result = {
5584 .num_modes = num_modes,
5585 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005586 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587 };
5588
5589 *num_modes = 0;
5590 *flags = 0;
5591
5592 msg = nlmsg_alloc();
5593 if (!msg)
5594 return NULL;
5595
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005596 feat = get_nl80211_protocol_features(drv);
5597 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
5598 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
5599 else
5600 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005602 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5604
5605 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5606 nl80211_set_ht40_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005607 return wpa_driver_nl80211_postprocess_modes(result.modes,
5608 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005610 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005612 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 return NULL;
5614}
5615
5616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005617static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5618 const void *data, size_t len,
5619 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005620{
5621 __u8 rtap_hdr[] = {
5622 0x00, 0x00, /* radiotap version */
5623 0x0e, 0x00, /* radiotap length */
5624 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5625 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5626 0x00, /* padding */
5627 0x00, 0x00, /* RX and TX flags to indicate that */
5628 0x00, 0x00, /* this is the injected frame directly */
5629 };
5630 struct iovec iov[2] = {
5631 {
5632 .iov_base = &rtap_hdr,
5633 .iov_len = sizeof(rtap_hdr),
5634 },
5635 {
5636 .iov_base = (void *) data,
5637 .iov_len = len,
5638 }
5639 };
5640 struct msghdr msg = {
5641 .msg_name = NULL,
5642 .msg_namelen = 0,
5643 .msg_iov = iov,
5644 .msg_iovlen = 2,
5645 .msg_control = NULL,
5646 .msg_controllen = 0,
5647 .msg_flags = 0,
5648 };
5649 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005650 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651
5652 if (encrypt)
5653 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5654
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005655 if (drv->monitor_sock < 0) {
5656 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5657 "for %s", __func__);
5658 return -1;
5659 }
5660
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005661 if (noack)
5662 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005663 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665 res = sendmsg(drv->monitor_sock, &msg, 0);
5666 if (res < 0) {
5667 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5668 return -1;
5669 }
5670 return 0;
5671}
5672
5673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005674static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5675 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005676 int encrypt, int noack,
5677 unsigned int freq, int no_cck,
5678 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005679{
5680 struct wpa_driver_nl80211_data *drv = bss->drv;
5681 u64 cookie;
5682
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005683 if (freq == 0)
5684 freq = bss->freq;
5685
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686 if (drv->use_monitor)
5687 return wpa_driver_nl80211_send_mntr(drv, data, len,
5688 encrypt, noack);
5689
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005690 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5691 &cookie, no_cck, noack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005692}
5693
5694
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005695static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
5696 size_t data_len, int noack,
5697 unsigned int freq, int no_cck,
5698 int offchanok,
5699 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701 struct wpa_driver_nl80211_data *drv = bss->drv;
5702 struct ieee80211_mgmt *mgmt;
5703 int encrypt = 1;
5704 u16 fc;
5705
5706 mgmt = (struct ieee80211_mgmt *) data;
5707 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005708
5709 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5711 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5712 /*
5713 * The use of last_mgmt_freq is a bit of a hack,
5714 * but it works due to the single-threaded nature
5715 * of wpa_supplicant.
5716 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005717 if (freq == 0)
5718 freq = drv->last_mgmt_freq;
5719 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005720 data, data_len, NULL, 1, noack,
5721 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005723
5724 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005725 if (freq == 0)
5726 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005727 return nl80211_send_frame_cmd(bss, freq,
5728 (int) freq == bss->freq ? 0 :
5729 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005730 data, data_len,
5731 &drv->send_action_cookie,
5732 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005733 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07005734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005735 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5736 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5737 /*
5738 * Only one of the authentication frame types is encrypted.
5739 * In order for static WEP encryption to work properly (i.e.,
5740 * to not encrypt the frame), we need to tell mac80211 about
5741 * the frames that must not be encrypted.
5742 */
5743 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5744 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
5745 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5746 encrypt = 0;
5747 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005748
5749 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005750 noack, freq, no_cck, offchanok,
5751 wait_time);
5752}
5753
5754
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005755static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
5756 int slot, int ht_opmode, int ap_isolate,
5757 int *basic_rates)
5758{
5759 struct wpa_driver_nl80211_data *drv = bss->drv;
5760 struct nl_msg *msg;
5761
5762 msg = nlmsg_alloc();
5763 if (!msg)
5764 return -ENOMEM;
5765
5766 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5767
5768 if (cts >= 0)
5769 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5770 if (preamble >= 0)
5771 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5772 if (slot >= 0)
5773 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5774 if (ht_opmode >= 0)
5775 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5776 if (ap_isolate >= 0)
5777 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
5778
5779 if (basic_rates) {
5780 u8 rates[NL80211_MAX_SUPP_RATES];
5781 u8 rates_len = 0;
5782 int i;
5783
5784 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5785 i++)
5786 rates[rates_len++] = basic_rates[i] / 5;
5787
5788 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5789 }
5790
5791 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5792
5793 return send_and_recv_msgs(drv, msg, NULL, NULL);
5794 nla_put_failure:
5795 nlmsg_free(msg);
5796 return -ENOBUFS;
5797}
5798
5799
5800static int wpa_driver_nl80211_set_ap(void *priv,
5801 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802{
5803 struct i802_bss *bss = priv;
5804 struct wpa_driver_nl80211_data *drv = bss->drv;
5805 struct nl_msg *msg;
5806 u8 cmd = NL80211_CMD_NEW_BEACON;
5807 int ret;
5808 int beacon_set;
5809 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005810 int num_suites;
5811 u32 suites[10];
5812 u32 ver;
5813
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005814 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815
5816 msg = nlmsg_alloc();
5817 if (!msg)
5818 return -ENOMEM;
5819
5820 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5821 beacon_set);
5822 if (beacon_set)
5823 cmd = NL80211_CMD_SET_BEACON;
5824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005825 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005826 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5827 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005828 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005829 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5830 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005831 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005832 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005834 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005835 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005836 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005837 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005838 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
5839 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005840 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5841 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005842 if (params->proberesp && params->proberesp_len) {
5843 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5844 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5846 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005847 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005848 switch (params->hide_ssid) {
5849 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005850 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005851 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5852 NL80211_HIDDEN_SSID_NOT_IN_USE);
5853 break;
5854 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005855 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005856 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5857 NL80211_HIDDEN_SSID_ZERO_LEN);
5858 break;
5859 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005860 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005861 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5862 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5863 break;
5864 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005865 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005866 if (params->privacy)
5867 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005868 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005869 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5870 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5871 /* Leave out the attribute */
5872 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5873 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5874 NL80211_AUTHTYPE_SHARED_KEY);
5875 else
5876 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5877 NL80211_AUTHTYPE_OPEN_SYSTEM);
5878
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005879 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005880 ver = 0;
5881 if (params->wpa_version & WPA_PROTO_WPA)
5882 ver |= NL80211_WPA_VERSION_1;
5883 if (params->wpa_version & WPA_PROTO_RSN)
5884 ver |= NL80211_WPA_VERSION_2;
5885 if (ver)
5886 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5887
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005888 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5889 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005890 num_suites = 0;
5891 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5892 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5893 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5894 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5895 if (num_suites) {
5896 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5897 num_suites * sizeof(u32), suites);
5898 }
5899
5900 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5901 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5902 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5903
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005904 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5905 params->pairwise_ciphers);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005906 num_suites = 0;
5907 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5908 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005909 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
5910 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005911 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5912 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5913 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5914 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5915 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5916 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5917 if (num_suites) {
5918 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5919 num_suites * sizeof(u32), suites);
5920 }
5921
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005922 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5923 params->group_cipher);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005924 switch (params->group_cipher) {
5925 case WPA_CIPHER_CCMP:
5926 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5927 WLAN_CIPHER_SUITE_CCMP);
5928 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005929 case WPA_CIPHER_GCMP:
5930 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5931 WLAN_CIPHER_SUITE_GCMP);
5932 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005933 case WPA_CIPHER_TKIP:
5934 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5935 WLAN_CIPHER_SUITE_TKIP);
5936 break;
5937 case WPA_CIPHER_WEP104:
5938 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5939 WLAN_CIPHER_SUITE_WEP104);
5940 break;
5941 case WPA_CIPHER_WEP40:
5942 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5943 WLAN_CIPHER_SUITE_WEP40);
5944 break;
5945 }
5946
5947 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005948 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5949 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005950 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5951 wpabuf_head(params->beacon_ies));
5952 }
5953 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005954 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5955 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005956 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5957 wpabuf_len(params->proberesp_ies),
5958 wpabuf_head(params->proberesp_ies));
5959 }
5960 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005961 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5962 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005963 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5964 wpabuf_len(params->assocresp_ies),
5965 wpabuf_head(params->assocresp_ies));
5966 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005967
Dmitry Shmidt04949592012-07-19 12:16:46 -07005968 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005969 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5970 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005971 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5972 params->ap_max_inactivity);
5973 }
5974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5976 if (ret) {
5977 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5978 ret, strerror(-ret));
5979 } else {
5980 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005981 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5982 params->short_slot_time, params->ht_opmode,
5983 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 }
5985 return ret;
5986 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005987 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988 return -ENOBUFS;
5989}
5990
5991
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005992static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005993 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005995 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996 struct nl_msg *msg;
5997 int ret;
5998
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005999 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
6000 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6001 freq->freq, freq->ht_enabled, freq->vht_enabled,
6002 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006003 msg = nlmsg_alloc();
6004 if (!msg)
6005 return -1;
6006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006007 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006008
6009 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006010 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
6011 if (freq->vht_enabled) {
6012 switch (freq->bandwidth) {
6013 case 20:
6014 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6015 NL80211_CHAN_WIDTH_20);
6016 break;
6017 case 40:
6018 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6019 NL80211_CHAN_WIDTH_40);
6020 break;
6021 case 80:
6022 if (freq->center_freq2)
6023 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6024 NL80211_CHAN_WIDTH_80P80);
6025 else
6026 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6027 NL80211_CHAN_WIDTH_80);
6028 break;
6029 case 160:
6030 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6031 NL80211_CHAN_WIDTH_160);
6032 break;
6033 default:
6034 return -1;
6035 }
6036 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6037 if (freq->center_freq2)
6038 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6039 freq->center_freq2);
6040 } else if (freq->ht_enabled) {
6041 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 case -1:
6043 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6044 NL80211_CHAN_HT40MINUS);
6045 break;
6046 case 1:
6047 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6048 NL80211_CHAN_HT40PLUS);
6049 break;
6050 default:
6051 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6052 NL80211_CHAN_HT20);
6053 break;
6054 }
6055 }
6056
6057 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006058 msg = NULL;
6059 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006060 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006064 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006066 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 return -1;
6068}
6069
6070
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006071static u32 sta_flags_nl80211(int flags)
6072{
6073 u32 f = 0;
6074
6075 if (flags & WPA_STA_AUTHORIZED)
6076 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6077 if (flags & WPA_STA_WMM)
6078 f |= BIT(NL80211_STA_FLAG_WME);
6079 if (flags & WPA_STA_SHORT_PREAMBLE)
6080 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6081 if (flags & WPA_STA_MFP)
6082 f |= BIT(NL80211_STA_FLAG_MFP);
6083 if (flags & WPA_STA_TDLS_PEER)
6084 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
6085
6086 return f;
6087}
6088
6089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090static int wpa_driver_nl80211_sta_add(void *priv,
6091 struct hostapd_sta_add_params *params)
6092{
6093 struct i802_bss *bss = priv;
6094 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006095 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 int ret = -ENOBUFS;
6098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006099 if ((params->flags & WPA_STA_TDLS_PEER) &&
6100 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6101 return -EOPNOTSUPP;
6102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006103 msg = nlmsg_alloc();
6104 if (!msg)
6105 return -ENOMEM;
6106
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006107 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6108 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006109 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6110 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111
6112 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6113 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006114 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6115 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006116 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
6117 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006118 if (!params->set) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006119 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006120 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006121 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
6122 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006123 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6124 params->listen_interval);
6125 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006127 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
6128 (u8 *) params->ht_capabilities,
6129 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
6131 sizeof(*params->ht_capabilities),
6132 params->ht_capabilities);
6133 }
6134
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006135 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006136 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
6137 (u8 *) params->vht_capabilities,
6138 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006139 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6140 sizeof(*params->vht_capabilities),
6141 params->vht_capabilities);
6142 }
6143
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006144 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
6145 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6146
6147 if (params->ext_capab) {
6148 wpa_hexdump(MSG_DEBUG, " * ext_capab",
6149 params->ext_capab, params->ext_capab_len);
6150 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6151 params->ext_capab_len, params->ext_capab);
6152 }
6153
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006154 os_memset(&upd, 0, sizeof(upd));
6155 upd.mask = sta_flags_nl80211(params->flags);
6156 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006157 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
6158 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006159 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6160
6161 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006162 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
6163
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006164 if (!wme)
6165 goto nla_put_failure;
6166
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006167 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006168 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006169 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006170 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006171 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006172 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006173 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174 }
6175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006177 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006179 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6180 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6181 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006182 if (ret == -EEXIST)
6183 ret = 0;
6184 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006185 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006186 return ret;
6187}
6188
6189
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006190static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006192 struct wpa_driver_nl80211_data *drv = bss->drv;
6193 struct nl_msg *msg;
6194 int ret;
6195
6196 msg = nlmsg_alloc();
6197 if (!msg)
6198 return -ENOMEM;
6199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006200 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201
6202 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6203 if_nametoindex(bss->ifname));
6204 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6205
6206 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6207 if (ret == -ENOENT)
6208 return 0;
6209 return ret;
6210 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006211 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 return -ENOBUFS;
6213}
6214
6215
6216static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6217 int ifidx)
6218{
6219 struct nl_msg *msg;
6220
6221 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223 /* stop listening for EAPOL on this interface */
6224 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225
6226 msg = nlmsg_alloc();
6227 if (!msg)
6228 goto nla_put_failure;
6229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006230 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006231 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6232
6233 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6234 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006235 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006237 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6239}
6240
6241
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006242static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6243{
6244 switch (mode) {
6245 case NL80211_IFTYPE_ADHOC:
6246 return "ADHOC";
6247 case NL80211_IFTYPE_STATION:
6248 return "STATION";
6249 case NL80211_IFTYPE_AP:
6250 return "AP";
6251 case NL80211_IFTYPE_MONITOR:
6252 return "MONITOR";
6253 case NL80211_IFTYPE_P2P_CLIENT:
6254 return "P2P_CLIENT";
6255 case NL80211_IFTYPE_P2P_GO:
6256 return "P2P_GO";
6257 default:
6258 return "unknown";
6259 }
6260}
6261
6262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6264 const char *ifname,
6265 enum nl80211_iftype iftype,
6266 const u8 *addr, int wds)
6267{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006268 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269 int ifidx;
6270 int ret = -ENOBUFS;
6271
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006272 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6273 iftype, nl80211_iftype_str(iftype));
6274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275 msg = nlmsg_alloc();
6276 if (!msg)
6277 return -1;
6278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006279 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6281 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6282 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6283
6284 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006285 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006286
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006287 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 if (!flags)
6289 goto nla_put_failure;
6290
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006291 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006293 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294 } else if (wds) {
6295 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
6296 }
6297
6298 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006299 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300 if (ret) {
6301 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006302 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6304 ifname, ret, strerror(-ret));
6305 return ret;
6306 }
6307
6308 ifidx = if_nametoindex(ifname);
6309 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6310 ifname, ifidx);
6311
6312 if (ifidx <= 0)
6313 return -1;
6314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006315 /* start listening for EAPOL on this interface */
6316 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317
6318 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006319 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006320 nl80211_remove_iface(drv, ifidx);
6321 return -1;
6322 }
6323
6324 return ifidx;
6325}
6326
6327
6328static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6329 const char *ifname, enum nl80211_iftype iftype,
6330 const u8 *addr, int wds)
6331{
6332 int ret;
6333
6334 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
6335
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006336 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006337 if (ret == -ENFILE && if_nametoindex(ifname)) {
6338 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6339
6340 /* Try to remove the interface that was already there. */
6341 nl80211_remove_iface(drv, if_nametoindex(ifname));
6342
6343 /* Try to create the interface again */
6344 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6345 wds);
6346 }
6347
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006348 if (ret >= 0 && is_p2p_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349 nl80211_disable_11b_rates(drv, ret, 1);
6350
6351 return ret;
6352}
6353
6354
6355static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6356{
6357 struct ieee80211_hdr *hdr;
6358 u16 fc;
6359 union wpa_event_data event;
6360
6361 hdr = (struct ieee80211_hdr *) buf;
6362 fc = le_to_host16(hdr->frame_control);
6363
6364 os_memset(&event, 0, sizeof(event));
6365 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6366 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6367 event.tx_status.dst = hdr->addr1;
6368 event.tx_status.data = buf;
6369 event.tx_status.data_len = len;
6370 event.tx_status.ack = ok;
6371 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
6372}
6373
6374
6375static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
6376 u8 *buf, size_t len)
6377{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006378 struct ieee80211_hdr *hdr = (void *)buf;
6379 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006381
6382 if (len < sizeof(*hdr))
6383 return;
6384
6385 fc = le_to_host16(hdr->frame_control);
6386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006387 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006388 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6389 event.rx_from_unknown.addr = hdr->addr2;
6390 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6391 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006392 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
6393}
6394
6395
6396static void handle_frame(struct wpa_driver_nl80211_data *drv,
6397 u8 *buf, size_t len, int datarate, int ssi_signal)
6398{
6399 struct ieee80211_hdr *hdr;
6400 u16 fc;
6401 union wpa_event_data event;
6402
6403 hdr = (struct ieee80211_hdr *) buf;
6404 fc = le_to_host16(hdr->frame_control);
6405
6406 switch (WLAN_FC_GET_TYPE(fc)) {
6407 case WLAN_FC_TYPE_MGMT:
6408 os_memset(&event, 0, sizeof(event));
6409 event.rx_mgmt.frame = buf;
6410 event.rx_mgmt.frame_len = len;
6411 event.rx_mgmt.datarate = datarate;
6412 event.rx_mgmt.ssi_signal = ssi_signal;
6413 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
6414 break;
6415 case WLAN_FC_TYPE_CTRL:
6416 /* can only get here with PS-Poll frames */
6417 wpa_printf(MSG_DEBUG, "CTRL");
6418 from_unknown_sta(drv, buf, len);
6419 break;
6420 case WLAN_FC_TYPE_DATA:
6421 from_unknown_sta(drv, buf, len);
6422 break;
6423 }
6424}
6425
6426
6427static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6428{
6429 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6430 int len;
6431 unsigned char buf[3000];
6432 struct ieee80211_radiotap_iterator iter;
6433 int ret;
6434 int datarate = 0, ssi_signal = 0;
6435 int injected = 0, failed = 0, rxflags = 0;
6436
6437 len = recv(sock, buf, sizeof(buf), 0);
6438 if (len < 0) {
6439 perror("recv");
6440 return;
6441 }
6442
6443 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6444 printf("received invalid radiotap frame\n");
6445 return;
6446 }
6447
6448 while (1) {
6449 ret = ieee80211_radiotap_iterator_next(&iter);
6450 if (ret == -ENOENT)
6451 break;
6452 if (ret) {
6453 printf("received invalid radiotap frame (%d)\n", ret);
6454 return;
6455 }
6456 switch (iter.this_arg_index) {
6457 case IEEE80211_RADIOTAP_FLAGS:
6458 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6459 len -= 4;
6460 break;
6461 case IEEE80211_RADIOTAP_RX_FLAGS:
6462 rxflags = 1;
6463 break;
6464 case IEEE80211_RADIOTAP_TX_FLAGS:
6465 injected = 1;
6466 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6467 IEEE80211_RADIOTAP_F_TX_FAIL;
6468 break;
6469 case IEEE80211_RADIOTAP_DATA_RETRIES:
6470 break;
6471 case IEEE80211_RADIOTAP_CHANNEL:
6472 /* TODO: convert from freq/flags to channel number */
6473 break;
6474 case IEEE80211_RADIOTAP_RATE:
6475 datarate = *iter.this_arg * 5;
6476 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006477 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6478 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006479 break;
6480 }
6481 }
6482
6483 if (rxflags && injected)
6484 return;
6485
6486 if (!injected)
6487 handle_frame(drv, buf + iter.max_length,
6488 len - iter.max_length, datarate, ssi_signal);
6489 else
6490 handle_tx_callback(drv->ctx, buf + iter.max_length,
6491 len - iter.max_length, !failed);
6492}
6493
6494
6495/*
6496 * we post-process the filter code later and rewrite
6497 * this to the offset to the last instruction
6498 */
6499#define PASS 0xFF
6500#define FAIL 0xFE
6501
6502static struct sock_filter msock_filter_insns[] = {
6503 /*
6504 * do a little-endian load of the radiotap length field
6505 */
6506 /* load lower byte into A */
6507 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6508 /* put it into X (== index register) */
6509 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6510 /* load upper byte into A */
6511 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6512 /* left-shift it by 8 */
6513 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6514 /* or with X */
6515 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6516 /* put result into X */
6517 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6518
6519 /*
6520 * Allow management frames through, this also gives us those
6521 * management frames that we sent ourselves with status
6522 */
6523 /* load the lower byte of the IEEE 802.11 frame control field */
6524 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6525 /* mask off frame type and version */
6526 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6527 /* accept frame if it's both 0, fall through otherwise */
6528 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6529
6530 /*
6531 * TODO: add a bit to radiotap RX flags that indicates
6532 * that the sending station is not associated, then
6533 * add a filter here that filters on our DA and that flag
6534 * to allow us to deauth frames to that bad station.
6535 *
6536 * For now allow all To DS data frames through.
6537 */
6538 /* load the IEEE 802.11 frame control field */
6539 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6540 /* mask off frame type, version and DS status */
6541 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6542 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6543 */
6544 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6545
6546#if 0
6547 /*
6548 * drop non-data frames
6549 */
6550 /* load the lower byte of the frame control field */
6551 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6552 /* mask off QoS bit */
6553 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6554 /* drop non-data frames */
6555 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
6556#endif
6557 /* load the upper byte of the frame control field */
6558 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
6559 /* mask off toDS/fromDS */
6560 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
6561 /* accept WDS frames */
6562 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
6563
6564 /*
6565 * add header length to index
6566 */
6567 /* load the lower byte of the frame control field */
6568 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6569 /* mask off QoS bit */
6570 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6571 /* right shift it by 6 to give 0 or 2 */
6572 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6573 /* add data frame header length */
6574 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6575 /* add index, was start of 802.11 header */
6576 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6577 /* move to index, now start of LL header */
6578 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6579
6580 /*
6581 * Accept empty data frames, we use those for
6582 * polling activity.
6583 */
6584 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6585 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6586
6587 /*
6588 * Accept EAPOL frames
6589 */
6590 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6591 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6592 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6593 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6594
6595 /* keep these last two statements or change the code below */
6596 /* return 0 == "DROP" */
6597 BPF_STMT(BPF_RET | BPF_K, 0),
6598 /* return ~0 == "keep all" */
6599 BPF_STMT(BPF_RET | BPF_K, ~0),
6600};
6601
6602static struct sock_fprog msock_filter = {
6603 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6604 .filter = msock_filter_insns,
6605};
6606
6607
6608static int add_monitor_filter(int s)
6609{
6610 int idx;
6611
6612 /* rewrite all PASS/FAIL jump offsets */
6613 for (idx = 0; idx < msock_filter.len; idx++) {
6614 struct sock_filter *insn = &msock_filter_insns[idx];
6615
6616 if (BPF_CLASS(insn->code) == BPF_JMP) {
6617 if (insn->code == (BPF_JMP|BPF_JA)) {
6618 if (insn->k == PASS)
6619 insn->k = msock_filter.len - idx - 2;
6620 else if (insn->k == FAIL)
6621 insn->k = msock_filter.len - idx - 3;
6622 }
6623
6624 if (insn->jt == PASS)
6625 insn->jt = msock_filter.len - idx - 2;
6626 else if (insn->jt == FAIL)
6627 insn->jt = msock_filter.len - idx - 3;
6628
6629 if (insn->jf == PASS)
6630 insn->jf = msock_filter.len - idx - 2;
6631 else if (insn->jf == FAIL)
6632 insn->jf = msock_filter.len - idx - 3;
6633 }
6634 }
6635
6636 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6637 &msock_filter, sizeof(msock_filter))) {
6638 perror("SO_ATTACH_FILTER");
6639 return -1;
6640 }
6641
6642 return 0;
6643}
6644
6645
6646static void nl80211_remove_monitor_interface(
6647 struct wpa_driver_nl80211_data *drv)
6648{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006649 drv->monitor_refcount--;
6650 if (drv->monitor_refcount > 0)
6651 return;
6652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006653 if (drv->monitor_ifidx >= 0) {
6654 nl80211_remove_iface(drv, drv->monitor_ifidx);
6655 drv->monitor_ifidx = -1;
6656 }
6657 if (drv->monitor_sock >= 0) {
6658 eloop_unregister_read_sock(drv->monitor_sock);
6659 close(drv->monitor_sock);
6660 drv->monitor_sock = -1;
6661 }
6662}
6663
6664
6665static int
6666nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6667{
6668 char buf[IFNAMSIZ];
6669 struct sockaddr_ll ll;
6670 int optval;
6671 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006672
6673 if (drv->monitor_ifidx >= 0) {
6674 drv->monitor_refcount++;
6675 return 0;
6676 }
6677
6678 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6679 /*
6680 * P2P interface name is of the format p2p-%s-%d. For monitor
6681 * interface name corresponding to P2P GO, replace "p2p-" with
6682 * "mon-" to retain the same interface name length and to
6683 * indicate that it is a monitor interface.
6684 */
6685 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6686 } else {
6687 /* Non-P2P interface with AP functionality. */
6688 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6689 }
6690
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006691 buf[IFNAMSIZ - 1] = '\0';
6692
6693 drv->monitor_ifidx =
6694 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6695 0);
6696
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006697 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006698 /*
6699 * This is backward compatibility for a few versions of
6700 * the kernel only that didn't advertise the right
6701 * attributes for the only driver that then supported
6702 * AP mode w/o monitor -- ath6kl.
6703 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006704 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6705 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006706 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006707 }
6708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006709 if (drv->monitor_ifidx < 0)
6710 return -1;
6711
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006712 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006713 goto error;
6714
6715 memset(&ll, 0, sizeof(ll));
6716 ll.sll_family = AF_PACKET;
6717 ll.sll_ifindex = drv->monitor_ifidx;
6718 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6719 if (drv->monitor_sock < 0) {
6720 perror("socket[PF_PACKET,SOCK_RAW]");
6721 goto error;
6722 }
6723
6724 if (add_monitor_filter(drv->monitor_sock)) {
6725 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6726 "interface; do filtering in user space");
6727 /* This works, but will cost in performance. */
6728 }
6729
6730 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6731 perror("monitor socket bind");
6732 goto error;
6733 }
6734
6735 optlen = sizeof(optval);
6736 optval = 20;
6737 if (setsockopt
6738 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6739 perror("Failed to set socket priority");
6740 goto error;
6741 }
6742
6743 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6744 drv, NULL)) {
6745 printf("Could not register monitor read socket\n");
6746 goto error;
6747 }
6748
6749 return 0;
6750 error:
6751 nl80211_remove_monitor_interface(drv);
6752 return -1;
6753}
6754
6755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006756static int nl80211_setup_ap(struct i802_bss *bss)
6757{
6758 struct wpa_driver_nl80211_data *drv = bss->drv;
6759
6760 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6761 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6762
6763 /*
6764 * Disable Probe Request reporting unless we need it in this way for
6765 * devices that include the AP SME, in the other case (unless using
6766 * monitor iface) we'll get it through the nl_mgmt socket instead.
6767 */
6768 if (!drv->device_ap_sme)
6769 wpa_driver_nl80211_probe_req_report(bss, 0);
6770
6771 if (!drv->device_ap_sme && !drv->use_monitor)
6772 if (nl80211_mgmt_subscribe_ap(bss))
6773 return -1;
6774
6775 if (drv->device_ap_sme && !drv->use_monitor)
6776 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6777 return -1;
6778
6779 if (!drv->device_ap_sme && drv->use_monitor &&
6780 nl80211_create_monitor_interface(drv) &&
6781 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006782 return -1;
6783
6784#ifdef ANDROID_P2P
6785 if (drv->device_ap_sme && drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006786 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6787 return -1;
6788
6789 if (drv->use_monitor &&
6790 nl80211_create_monitor_interface(drv))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006791 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006792#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006793
6794 if (drv->device_ap_sme &&
6795 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6796 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6797 "Probe Request frame reporting in AP mode");
6798 /* Try to survive without this */
6799 }
6800
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006801 return 0;
6802}
6803
6804
6805static void nl80211_teardown_ap(struct i802_bss *bss)
6806{
6807 struct wpa_driver_nl80211_data *drv = bss->drv;
6808
6809 if (drv->device_ap_sme) {
6810 wpa_driver_nl80211_probe_req_report(bss, 0);
6811 if (!drv->use_monitor)
6812 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6813 } else if (drv->use_monitor)
6814 nl80211_remove_monitor_interface(drv);
6815 else
6816 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6817
6818 bss->beacon_set = 0;
6819}
6820
6821
6822static int nl80211_send_eapol_data(struct i802_bss *bss,
6823 const u8 *addr, const u8 *data,
6824 size_t data_len)
6825{
6826 struct sockaddr_ll ll;
6827 int ret;
6828
6829 if (bss->drv->eapol_tx_sock < 0) {
6830 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6831 return -1;
6832 }
6833
6834 os_memset(&ll, 0, sizeof(ll));
6835 ll.sll_family = AF_PACKET;
6836 ll.sll_ifindex = bss->ifindex;
6837 ll.sll_protocol = htons(ETH_P_PAE);
6838 ll.sll_halen = ETH_ALEN;
6839 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6840 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6841 (struct sockaddr *) &ll, sizeof(ll));
6842 if (ret < 0)
6843 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6844 strerror(errno));
6845
6846 return ret;
6847}
6848
6849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6851
6852static int wpa_driver_nl80211_hapd_send_eapol(
6853 void *priv, const u8 *addr, const u8 *data,
6854 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6855{
6856 struct i802_bss *bss = priv;
6857 struct wpa_driver_nl80211_data *drv = bss->drv;
6858 struct ieee80211_hdr *hdr;
6859 size_t len;
6860 u8 *pos;
6861 int res;
6862 int qos = flags & WPA_STA_WMM;
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006863#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006864 if (drv->device_ap_sme || !drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006865#else
6866 if (drv->device_ap_sme && !drv->use_monitor)
6867#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006868 return nl80211_send_eapol_data(bss, addr, data, data_len);
6869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006870 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6871 data_len;
6872 hdr = os_zalloc(len);
6873 if (hdr == NULL) {
6874 printf("malloc() failed for i802_send_data(len=%lu)\n",
6875 (unsigned long) len);
6876 return -1;
6877 }
6878
6879 hdr->frame_control =
6880 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6881 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6882 if (encrypt)
6883 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6884 if (qos) {
6885 hdr->frame_control |=
6886 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6887 }
6888
6889 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6890 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6891 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6892 pos = (u8 *) (hdr + 1);
6893
6894 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006895 /* Set highest priority in QoS header */
6896 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897 pos[1] = 0;
6898 pos += 2;
6899 }
6900
6901 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6902 pos += sizeof(rfc1042_header);
6903 WPA_PUT_BE16(pos, ETH_P_PAE);
6904 pos += 2;
6905 memcpy(pos, data, data_len);
6906
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006907 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6908 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006909 if (res < 0) {
6910 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6911 "failed: %d (%s)",
6912 (unsigned long) len, errno, strerror(errno));
6913 }
6914 os_free(hdr);
6915
6916 return res;
6917}
6918
6919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006920static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6921 int total_flags,
6922 int flags_or, int flags_and)
6923{
6924 struct i802_bss *bss = priv;
6925 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006926 struct nl_msg *msg;
6927 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928 struct nl80211_sta_flag_update upd;
6929
6930 msg = nlmsg_alloc();
6931 if (!msg)
6932 return -ENOMEM;
6933
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006934 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935
6936 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6937 if_nametoindex(bss->ifname));
6938 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6939
6940 /*
6941 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6942 * can be removed eventually.
6943 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006944 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
6945 if (!flags)
6946 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006947 if (total_flags & WPA_STA_AUTHORIZED)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006948 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949
6950 if (total_flags & WPA_STA_WMM)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006951 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952
6953 if (total_flags & WPA_STA_SHORT_PREAMBLE)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006954 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955
6956 if (total_flags & WPA_STA_MFP)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006957 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006958
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006959 if (total_flags & WPA_STA_TDLS_PEER)
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006960 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006961
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006962 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006963
6964 os_memset(&upd, 0, sizeof(upd));
6965 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6966 upd.set = sta_flags_nl80211(flags_or);
6967 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006969 return send_and_recv_msgs(drv, msg, NULL, NULL);
6970 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006971 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 return -ENOBUFS;
6973}
6974
6975
6976static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6977 struct wpa_driver_associate_params *params)
6978{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006979 enum nl80211_iftype nlmode, old_mode;
6980 struct hostapd_freq_params freq = {
6981 .freq = params->freq,
6982 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006983
6984 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006985 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6986 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006987 nlmode = NL80211_IFTYPE_P2P_GO;
6988 } else
6989 nlmode = NL80211_IFTYPE_AP;
6990
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006991 old_mode = drv->nlmode;
6992 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
6993 nl80211_remove_monitor_interface(drv);
6994 return -1;
6995 }
6996
6997 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
6998 if (old_mode != nlmode)
6999 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000 nl80211_remove_monitor_interface(drv);
7001 return -1;
7002 }
7003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004 return 0;
7005}
7006
7007
7008static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7009{
7010 struct nl_msg *msg;
7011 int ret = -1;
7012
7013 msg = nlmsg_alloc();
7014 if (!msg)
7015 return -1;
7016
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007017 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007018 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7019 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7020 msg = NULL;
7021 if (ret) {
7022 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7023 "(%s)", ret, strerror(-ret));
7024 goto nla_put_failure;
7025 }
7026
7027 ret = 0;
7028 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7029
7030nla_put_failure:
7031 nlmsg_free(msg);
7032 return ret;
7033}
7034
7035
7036static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7037 struct wpa_driver_associate_params *params)
7038{
7039 struct nl_msg *msg;
7040 int ret = -1;
7041 int count = 0;
7042
7043 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7044
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007045 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7046 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7048 "IBSS mode");
7049 return -1;
7050 }
7051
7052retry:
7053 msg = nlmsg_alloc();
7054 if (!msg)
7055 return -1;
7056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007057 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007058 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7059
7060 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7061 goto nla_put_failure;
7062
7063 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7064 params->ssid, params->ssid_len);
7065 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7066 params->ssid);
7067 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7068 drv->ssid_len = params->ssid_len;
7069
7070 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7071 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7072
7073 ret = nl80211_set_conn_keys(params, msg);
7074 if (ret)
7075 goto nla_put_failure;
7076
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007077 if (params->bssid && params->fixed_bssid) {
7078 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
7079 MAC2STR(params->bssid));
7080 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7081 }
7082
7083 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7084 params->key_mgmt_suite == KEY_MGMT_PSK ||
7085 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7086 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7087 wpa_printf(MSG_DEBUG, " * control port");
7088 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7089 }
7090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007091 if (params->wpa_ie) {
7092 wpa_hexdump(MSG_DEBUG,
7093 " * Extra IEs for Beacon/Probe Response frames",
7094 params->wpa_ie, params->wpa_ie_len);
7095 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7096 params->wpa_ie);
7097 }
7098
7099 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7100 msg = NULL;
7101 if (ret) {
7102 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7103 ret, strerror(-ret));
7104 count++;
7105 if (ret == -EALREADY && count == 1) {
7106 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7107 "forced leave");
7108 nl80211_leave_ibss(drv);
7109 nlmsg_free(msg);
7110 goto retry;
7111 }
7112
7113 goto nla_put_failure;
7114 }
7115 ret = 0;
7116 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7117
7118nla_put_failure:
7119 nlmsg_free(msg);
7120 return ret;
7121}
7122
7123
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007124static int wpa_driver_nl80211_try_connect(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007125 struct wpa_driver_nl80211_data *drv,
7126 struct wpa_driver_associate_params *params)
7127{
7128 struct nl_msg *msg;
7129 enum nl80211_auth_type type;
7130 int ret = 0;
7131 int algs;
7132
7133 msg = nlmsg_alloc();
7134 if (!msg)
7135 return -1;
7136
7137 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007138 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007139
7140 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7141 if (params->bssid) {
7142 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7143 MAC2STR(params->bssid));
7144 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7145 }
7146 if (params->freq) {
7147 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7148 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7149 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007150 if (params->bg_scan_period >= 0) {
7151 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7152 params->bg_scan_period);
7153 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7154 params->bg_scan_period);
7155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156 if (params->ssid) {
7157 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7158 params->ssid, params->ssid_len);
7159 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7160 params->ssid);
7161 if (params->ssid_len > sizeof(drv->ssid))
7162 goto nla_put_failure;
7163 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7164 drv->ssid_len = params->ssid_len;
7165 }
7166 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7167 if (params->wpa_ie)
7168 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7169 params->wpa_ie);
7170
7171 algs = 0;
7172 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7173 algs++;
7174 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7175 algs++;
7176 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7177 algs++;
7178 if (algs > 1) {
7179 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7180 "selection");
7181 goto skip_auth_type;
7182 }
7183
7184 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7185 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
7186 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7187 type = NL80211_AUTHTYPE_SHARED_KEY;
7188 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7189 type = NL80211_AUTHTYPE_NETWORK_EAP;
7190 else if (params->auth_alg & WPA_AUTH_ALG_FT)
7191 type = NL80211_AUTHTYPE_FT;
7192 else
7193 goto nla_put_failure;
7194
7195 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
7196 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7197
7198skip_auth_type:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007199 if (params->wpa_proto) {
7200 enum nl80211_wpa_versions ver = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007202 if (params->wpa_proto & WPA_PROTO_WPA)
7203 ver |= NL80211_WPA_VERSION_1;
7204 if (params->wpa_proto & WPA_PROTO_RSN)
7205 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007207 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007208 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7209 }
7210
7211 if (params->pairwise_suite != CIPHER_NONE) {
7212 int cipher;
7213
7214 switch (params->pairwise_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007215 case CIPHER_SMS4:
7216 cipher = WLAN_CIPHER_SUITE_SMS4;
7217 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007218 case CIPHER_WEP40:
7219 cipher = WLAN_CIPHER_SUITE_WEP40;
7220 break;
7221 case CIPHER_WEP104:
7222 cipher = WLAN_CIPHER_SUITE_WEP104;
7223 break;
7224 case CIPHER_CCMP:
7225 cipher = WLAN_CIPHER_SUITE_CCMP;
7226 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007227 case CIPHER_GCMP:
7228 cipher = WLAN_CIPHER_SUITE_GCMP;
7229 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007230 case CIPHER_TKIP:
7231 default:
7232 cipher = WLAN_CIPHER_SUITE_TKIP;
7233 break;
7234 }
7235 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7236 }
7237
7238 if (params->group_suite != CIPHER_NONE) {
7239 int cipher;
7240
7241 switch (params->group_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007242 case CIPHER_SMS4:
7243 cipher = WLAN_CIPHER_SUITE_SMS4;
7244 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007245 case CIPHER_WEP40:
7246 cipher = WLAN_CIPHER_SUITE_WEP40;
7247 break;
7248 case CIPHER_WEP104:
7249 cipher = WLAN_CIPHER_SUITE_WEP104;
7250 break;
7251 case CIPHER_CCMP:
7252 cipher = WLAN_CIPHER_SUITE_CCMP;
7253 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007254 case CIPHER_GCMP:
7255 cipher = WLAN_CIPHER_SUITE_GCMP;
7256 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007257 case CIPHER_TKIP:
7258 default:
7259 cipher = WLAN_CIPHER_SUITE_TKIP;
7260 break;
7261 }
7262 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7263 }
7264
7265 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007266 params->key_mgmt_suite == KEY_MGMT_PSK ||
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007267 params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7268 params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007269 params->key_mgmt_suite == KEY_MGMT_CCKM) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007270 int mgmt = WLAN_AKM_SUITE_PSK;
7271
7272 switch (params->key_mgmt_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007273 case KEY_MGMT_CCKM:
7274 mgmt = WLAN_AKM_SUITE_CCKM;
7275 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007276 case KEY_MGMT_802_1X:
7277 mgmt = WLAN_AKM_SUITE_8021X;
7278 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007279 case KEY_MGMT_FT_802_1X:
7280 mgmt = WLAN_AKM_SUITE_FT_8021X;
7281 break;
7282 case KEY_MGMT_FT_PSK:
7283 mgmt = WLAN_AKM_SUITE_FT_PSK;
7284 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285 case KEY_MGMT_PSK:
7286 default:
7287 mgmt = WLAN_AKM_SUITE_PSK;
7288 break;
7289 }
7290 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7291 }
7292
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007293#ifdef CONFIG_IEEE80211W
7294 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7295 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7296#endif /* CONFIG_IEEE80211W */
7297
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007298 if (params->disable_ht)
7299 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7300
7301 if (params->htcaps && params->htcaps_mask) {
7302 int sz = sizeof(struct ieee80211_ht_capabilities);
7303 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7304 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7305 params->htcaps_mask);
7306 }
7307
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007308#ifdef CONFIG_VHT_OVERRIDES
7309 if (params->disable_vht) {
7310 wpa_printf(MSG_DEBUG, " * VHT disabled");
7311 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7312 }
7313
7314 if (params->vhtcaps && params->vhtcaps_mask) {
7315 int sz = sizeof(struct ieee80211_vht_capabilities);
7316 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7317 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7318 params->vhtcaps_mask);
7319 }
7320#endif /* CONFIG_VHT_OVERRIDES */
7321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007322 ret = nl80211_set_conn_keys(params, msg);
7323 if (ret)
7324 goto nla_put_failure;
7325
7326 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7327 msg = NULL;
7328 if (ret) {
7329 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7330 "(%s)", ret, strerror(-ret));
7331 goto nla_put_failure;
7332 }
7333 ret = 0;
7334 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7335
7336nla_put_failure:
7337 nlmsg_free(msg);
7338 return ret;
7339
7340}
7341
7342
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007343static int wpa_driver_nl80211_connect(
7344 struct wpa_driver_nl80211_data *drv,
7345 struct wpa_driver_associate_params *params)
7346{
7347 int ret = wpa_driver_nl80211_try_connect(drv, params);
7348 if (ret == -EALREADY) {
7349 /*
7350 * cfg80211 does not currently accept new connections if
7351 * we are already connected. As a workaround, force
7352 * disconnection and try again.
7353 */
7354 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7355 "disconnecting before reassociation "
7356 "attempt");
7357 if (wpa_driver_nl80211_disconnect(
7358 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7359 return -1;
7360 /* Ignore the next local disconnect message. */
7361 drv->ignore_next_local_disconnect = 1;
7362 ret = wpa_driver_nl80211_try_connect(drv, params);
7363 }
7364 return ret;
7365}
7366
7367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007368static int wpa_driver_nl80211_associate(
7369 void *priv, struct wpa_driver_associate_params *params)
7370{
7371 struct i802_bss *bss = priv;
7372 struct wpa_driver_nl80211_data *drv = bss->drv;
7373 int ret = -1;
7374 struct nl_msg *msg;
7375
7376 if (params->mode == IEEE80211_MODE_AP)
7377 return wpa_driver_nl80211_ap(drv, params);
7378
7379 if (params->mode == IEEE80211_MODE_IBSS)
7380 return wpa_driver_nl80211_ibss(drv, params);
7381
7382 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007383 enum nl80211_iftype nlmode = params->p2p ?
7384 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7385
7386 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007387 return -1;
7388 return wpa_driver_nl80211_connect(drv, params);
7389 }
7390
7391 drv->associated = 0;
7392
7393 msg = nlmsg_alloc();
7394 if (!msg)
7395 return -1;
7396
7397 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7398 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007399 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007400
7401 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7402 if (params->bssid) {
7403 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7404 MAC2STR(params->bssid));
7405 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7406 }
7407 if (params->freq) {
7408 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7409 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7410 drv->assoc_freq = params->freq;
7411 } else
7412 drv->assoc_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007413 if (params->bg_scan_period >= 0) {
7414 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7415 params->bg_scan_period);
7416 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7417 params->bg_scan_period);
7418 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007419 if (params->ssid) {
7420 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7421 params->ssid, params->ssid_len);
7422 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7423 params->ssid);
7424 if (params->ssid_len > sizeof(drv->ssid))
7425 goto nla_put_failure;
7426 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7427 drv->ssid_len = params->ssid_len;
7428 }
7429 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7430 if (params->wpa_ie)
7431 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7432 params->wpa_ie);
7433
7434 if (params->pairwise_suite != CIPHER_NONE) {
7435 int cipher;
7436
7437 switch (params->pairwise_suite) {
7438 case CIPHER_WEP40:
7439 cipher = WLAN_CIPHER_SUITE_WEP40;
7440 break;
7441 case CIPHER_WEP104:
7442 cipher = WLAN_CIPHER_SUITE_WEP104;
7443 break;
7444 case CIPHER_CCMP:
7445 cipher = WLAN_CIPHER_SUITE_CCMP;
7446 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007447 case CIPHER_GCMP:
7448 cipher = WLAN_CIPHER_SUITE_GCMP;
7449 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007450 case CIPHER_TKIP:
7451 default:
7452 cipher = WLAN_CIPHER_SUITE_TKIP;
7453 break;
7454 }
7455 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
7456 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7457 }
7458
7459 if (params->group_suite != CIPHER_NONE) {
7460 int cipher;
7461
7462 switch (params->group_suite) {
7463 case CIPHER_WEP40:
7464 cipher = WLAN_CIPHER_SUITE_WEP40;
7465 break;
7466 case CIPHER_WEP104:
7467 cipher = WLAN_CIPHER_SUITE_WEP104;
7468 break;
7469 case CIPHER_CCMP:
7470 cipher = WLAN_CIPHER_SUITE_CCMP;
7471 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007472 case CIPHER_GCMP:
7473 cipher = WLAN_CIPHER_SUITE_GCMP;
7474 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007475 case CIPHER_TKIP:
7476 default:
7477 cipher = WLAN_CIPHER_SUITE_TKIP;
7478 break;
7479 }
7480 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
7481 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7482 }
7483
7484#ifdef CONFIG_IEEE80211W
7485 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7486 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7487#endif /* CONFIG_IEEE80211W */
7488
7489 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7490
7491 if (params->prev_bssid) {
7492 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7493 MAC2STR(params->prev_bssid));
7494 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7495 params->prev_bssid);
7496 }
7497
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007498 if (params->disable_ht)
7499 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7500
7501 if (params->htcaps && params->htcaps_mask) {
7502 int sz = sizeof(struct ieee80211_ht_capabilities);
7503 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7504 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7505 params->htcaps_mask);
7506 }
7507
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007508#ifdef CONFIG_VHT_OVERRIDES
7509 if (params->disable_vht) {
7510 wpa_printf(MSG_DEBUG, " * VHT disabled");
7511 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7512 }
7513
7514 if (params->vhtcaps && params->vhtcaps_mask) {
7515 int sz = sizeof(struct ieee80211_vht_capabilities);
7516 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7517 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7518 params->vhtcaps_mask);
7519 }
7520#endif /* CONFIG_VHT_OVERRIDES */
7521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522 if (params->p2p)
7523 wpa_printf(MSG_DEBUG, " * P2P group");
7524
7525 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7526 msg = NULL;
7527 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007528 wpa_dbg(drv->ctx, MSG_DEBUG,
7529 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7530 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531 nl80211_dump_scan(drv);
7532 goto nla_put_failure;
7533 }
7534 ret = 0;
7535 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7536 "successfully");
7537
7538nla_put_failure:
7539 nlmsg_free(msg);
7540 return ret;
7541}
7542
7543
7544static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007545 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007546{
7547 struct nl_msg *msg;
7548 int ret = -ENOBUFS;
7549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007550 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7551 ifindex, mode, nl80211_iftype_str(mode));
7552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 msg = nlmsg_alloc();
7554 if (!msg)
7555 return -ENOMEM;
7556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007557 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7559 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7560
7561 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007562 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563 if (!ret)
7564 return 0;
7565nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007566 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007567 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7568 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7569 return ret;
7570}
7571
7572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007573static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7574 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007576 struct wpa_driver_nl80211_data *drv = bss->drv;
7577 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007578 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007579 int was_ap = is_ap_interface(drv->nlmode);
7580 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007582 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7583 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007584 drv->nlmode = nlmode;
7585 ret = 0;
7586 goto done;
7587 }
7588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007589 if (res == -ENODEV)
7590 return -1;
7591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592 if (nlmode == drv->nlmode) {
7593 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7594 "requested mode - ignore error");
7595 ret = 0;
7596 goto done; /* Already in the requested mode */
7597 }
7598
7599 /* mac80211 doesn't allow mode changes while the device is up, so
7600 * take the device down, try to set the mode again, and bring the
7601 * device back up.
7602 */
7603 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7604 "interface down");
7605 for (i = 0; i < 10; i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007606 res = linux_set_iface_flags(drv->global->ioctl_sock,
7607 bss->ifname, 0);
7608 if (res == -EACCES || res == -ENODEV)
7609 break;
7610 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007611 /* Try to set the mode again while the interface is
7612 * down */
7613 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007614 if (ret == -EACCES)
7615 break;
7616 res = linux_set_iface_flags(drv->global->ioctl_sock,
7617 bss->ifname, 1);
7618 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007619 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007620 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621 break;
7622 } else
7623 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7624 "interface down");
7625 os_sleep(0, 100000);
7626 }
7627
7628 if (!ret) {
7629 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7630 "interface is down");
7631 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007632 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633 }
7634
7635done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007636 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7638 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007639 return ret;
7640 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007641
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007642 if (is_p2p_interface(nlmode))
7643 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
7644 else if (drv->disabled_11b_rates)
7645 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
7646
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007647 if (is_ap_interface(nlmode)) {
7648 nl80211_mgmt_unsubscribe(bss, "start AP");
7649 /* Setup additional AP mode functionality if needed */
7650 if (nl80211_setup_ap(bss))
7651 return -1;
7652 } else if (was_ap) {
7653 /* Remove additional AP mode functionality */
7654 nl80211_teardown_ap(bss);
7655 } else {
7656 nl80211_mgmt_unsubscribe(bss, "mode change");
7657 }
7658
Dmitry Shmidt04949592012-07-19 12:16:46 -07007659 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007660 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7661 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7662 "frame processing - ignore for now");
7663
7664 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007665}
7666
7667
7668static int wpa_driver_nl80211_get_capa(void *priv,
7669 struct wpa_driver_capa *capa)
7670{
7671 struct i802_bss *bss = priv;
7672 struct wpa_driver_nl80211_data *drv = bss->drv;
7673 if (!drv->has_capability)
7674 return -1;
7675 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007676 if (drv->extended_capa && drv->extended_capa_mask) {
7677 capa->extended_capa = drv->extended_capa;
7678 capa->extended_capa_mask = drv->extended_capa_mask;
7679 capa->extended_capa_len = drv->extended_capa_len;
7680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681 return 0;
7682}
7683
7684
7685static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7686{
7687 struct i802_bss *bss = priv;
7688 struct wpa_driver_nl80211_data *drv = bss->drv;
7689
7690 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7691 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7692 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007693 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007694 state ? IF_OPER_UP : IF_OPER_DORMANT);
7695}
7696
7697
7698static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7699{
7700 struct i802_bss *bss = priv;
7701 struct wpa_driver_nl80211_data *drv = bss->drv;
7702 struct nl_msg *msg;
7703 struct nl80211_sta_flag_update upd;
7704
7705 msg = nlmsg_alloc();
7706 if (!msg)
7707 return -ENOMEM;
7708
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007709 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710
7711 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7712 if_nametoindex(bss->ifname));
7713 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7714
7715 os_memset(&upd, 0, sizeof(upd));
7716 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7717 if (authorized)
7718 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7719 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7720
7721 return send_and_recv_msgs(drv, msg, NULL, NULL);
7722 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007723 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724 return -ENOBUFS;
7725}
7726
7727
Jouni Malinen75ecf522011-06-27 15:19:46 -07007728/* Set kernel driver on given frequency (MHz) */
7729static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007730{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007731 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007732 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007733}
7734
7735
Jouni Malinen75ecf522011-06-27 15:19:46 -07007736#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737
7738static inline int min_int(int a, int b)
7739{
7740 if (a < b)
7741 return a;
7742 return b;
7743}
7744
7745
7746static int get_key_handler(struct nl_msg *msg, void *arg)
7747{
7748 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7749 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7750
7751 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7752 genlmsg_attrlen(gnlh, 0), NULL);
7753
7754 /*
7755 * TODO: validate the key index and mac address!
7756 * Otherwise, there's a race condition as soon as
7757 * the kernel starts sending key notifications.
7758 */
7759
7760 if (tb[NL80211_ATTR_KEY_SEQ])
7761 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7762 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7763 return NL_SKIP;
7764}
7765
7766
7767static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7768 int idx, u8 *seq)
7769{
7770 struct i802_bss *bss = priv;
7771 struct wpa_driver_nl80211_data *drv = bss->drv;
7772 struct nl_msg *msg;
7773
7774 msg = nlmsg_alloc();
7775 if (!msg)
7776 return -ENOMEM;
7777
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007778 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007779
7780 if (addr)
7781 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7782 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7783 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7784
7785 memset(seq, 0, 6);
7786
7787 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7788 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007789 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007790 return -ENOBUFS;
7791}
7792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793
7794static int i802_set_rts(void *priv, int rts)
7795{
7796 struct i802_bss *bss = priv;
7797 struct wpa_driver_nl80211_data *drv = bss->drv;
7798 struct nl_msg *msg;
7799 int ret = -ENOBUFS;
7800 u32 val;
7801
7802 msg = nlmsg_alloc();
7803 if (!msg)
7804 return -ENOMEM;
7805
7806 if (rts >= 2347)
7807 val = (u32) -1;
7808 else
7809 val = rts;
7810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007811 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7813 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7814
7815 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007816 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007817 if (!ret)
7818 return 0;
7819nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007820 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7822 "%d (%s)", rts, ret, strerror(-ret));
7823 return ret;
7824}
7825
7826
7827static int i802_set_frag(void *priv, int frag)
7828{
7829 struct i802_bss *bss = priv;
7830 struct wpa_driver_nl80211_data *drv = bss->drv;
7831 struct nl_msg *msg;
7832 int ret = -ENOBUFS;
7833 u32 val;
7834
7835 msg = nlmsg_alloc();
7836 if (!msg)
7837 return -ENOMEM;
7838
7839 if (frag >= 2346)
7840 val = (u32) -1;
7841 else
7842 val = frag;
7843
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007844 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007845 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7846 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7847
7848 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007849 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007850 if (!ret)
7851 return 0;
7852nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007853 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007854 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7855 "%d: %d (%s)", frag, ret, strerror(-ret));
7856 return ret;
7857}
7858
7859
7860static int i802_flush(void *priv)
7861{
7862 struct i802_bss *bss = priv;
7863 struct wpa_driver_nl80211_data *drv = bss->drv;
7864 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007865 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007866
7867 msg = nlmsg_alloc();
7868 if (!msg)
7869 return -1;
7870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007871 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007872
7873 /*
7874 * XXX: FIX! this needs to flush all VLANs too
7875 */
7876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7877 if_nametoindex(bss->ifname));
7878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007879 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7880 if (res) {
7881 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7882 "(%s)", res, strerror(-res));
7883 }
7884 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007885 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007886 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007887 return -ENOBUFS;
7888}
7889
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007890#endif /* HOSTAPD || CONFIG_AP */
7891
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007892
7893static int get_sta_handler(struct nl_msg *msg, void *arg)
7894{
7895 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7896 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7897 struct hostap_sta_driver_data *data = arg;
7898 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7899 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7900 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7901 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7902 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7903 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7904 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007905 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007906 };
7907
7908 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7909 genlmsg_attrlen(gnlh, 0), NULL);
7910
7911 /*
7912 * TODO: validate the interface and mac address!
7913 * Otherwise, there's a race condition as soon as
7914 * the kernel starts sending station notifications.
7915 */
7916
7917 if (!tb[NL80211_ATTR_STA_INFO]) {
7918 wpa_printf(MSG_DEBUG, "sta stats missing!");
7919 return NL_SKIP;
7920 }
7921 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7922 tb[NL80211_ATTR_STA_INFO],
7923 stats_policy)) {
7924 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7925 return NL_SKIP;
7926 }
7927
7928 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7929 data->inactive_msec =
7930 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7931 if (stats[NL80211_STA_INFO_RX_BYTES])
7932 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7933 if (stats[NL80211_STA_INFO_TX_BYTES])
7934 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7935 if (stats[NL80211_STA_INFO_RX_PACKETS])
7936 data->rx_packets =
7937 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7938 if (stats[NL80211_STA_INFO_TX_PACKETS])
7939 data->tx_packets =
7940 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007941 if (stats[NL80211_STA_INFO_TX_FAILED])
7942 data->tx_retry_failed =
7943 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007944
7945 return NL_SKIP;
7946}
7947
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007948static int i802_read_sta_data(struct i802_bss *bss,
7949 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950 const u8 *addr)
7951{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007952 struct wpa_driver_nl80211_data *drv = bss->drv;
7953 struct nl_msg *msg;
7954
7955 os_memset(data, 0, sizeof(*data));
7956 msg = nlmsg_alloc();
7957 if (!msg)
7958 return -ENOMEM;
7959
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007960 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007961
7962 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7963 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7964
7965 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7966 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007967 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007968 return -ENOBUFS;
7969}
7970
7971
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007972#if defined(HOSTAPD) || defined(CONFIG_AP)
7973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007974static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7975 int cw_min, int cw_max, int burst_time)
7976{
7977 struct i802_bss *bss = priv;
7978 struct wpa_driver_nl80211_data *drv = bss->drv;
7979 struct nl_msg *msg;
7980 struct nlattr *txq, *params;
7981
7982 msg = nlmsg_alloc();
7983 if (!msg)
7984 return -1;
7985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007987
7988 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7989
7990 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7991 if (!txq)
7992 goto nla_put_failure;
7993
7994 /* We are only sending parameters for a single TXQ at a time */
7995 params = nla_nest_start(msg, 1);
7996 if (!params)
7997 goto nla_put_failure;
7998
7999 switch (queue) {
8000 case 0:
8001 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8002 break;
8003 case 1:
8004 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8005 break;
8006 case 2:
8007 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8008 break;
8009 case 3:
8010 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8011 break;
8012 }
8013 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8014 * 32 usec, so need to convert the value here. */
8015 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8016 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8017 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8018 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8019
8020 nla_nest_end(msg, params);
8021
8022 nla_nest_end(msg, txq);
8023
8024 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8025 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008026 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008027 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008028 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008029 return -1;
8030}
8031
8032
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008033static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008034 const char *ifname, int vlan_id)
8035{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008036 struct wpa_driver_nl80211_data *drv = bss->drv;
8037 struct nl_msg *msg;
8038 int ret = -ENOBUFS;
8039
8040 msg = nlmsg_alloc();
8041 if (!msg)
8042 return -ENOMEM;
8043
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008044 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045
8046 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8047 if_nametoindex(bss->ifname));
8048 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8049 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8050 if_nametoindex(ifname));
8051
8052 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008053 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008054 if (ret < 0) {
8055 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8056 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8057 MAC2STR(addr), ifname, vlan_id, ret,
8058 strerror(-ret));
8059 }
8060 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008061 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008062 return ret;
8063}
8064
8065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008066static int i802_get_inact_sec(void *priv, const u8 *addr)
8067{
8068 struct hostap_sta_driver_data data;
8069 int ret;
8070
8071 data.inactive_msec = (unsigned long) -1;
8072 ret = i802_read_sta_data(priv, &data, addr);
8073 if (ret || data.inactive_msec == (unsigned long) -1)
8074 return -1;
8075 return data.inactive_msec / 1000;
8076}
8077
8078
8079static int i802_sta_clear_stats(void *priv, const u8 *addr)
8080{
8081#if 0
8082 /* TODO */
8083#endif
8084 return 0;
8085}
8086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008087
8088static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8089 int reason)
8090{
8091 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008092 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008093 struct ieee80211_mgmt mgmt;
8094
Dmitry Shmidt04949592012-07-19 12:16:46 -07008095 if (drv->device_ap_sme)
8096 return wpa_driver_nl80211_sta_remove(bss, addr);
8097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008098 memset(&mgmt, 0, sizeof(mgmt));
8099 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8100 WLAN_FC_STYPE_DEAUTH);
8101 memcpy(mgmt.da, addr, ETH_ALEN);
8102 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8103 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8104 mgmt.u.deauth.reason_code = host_to_le16(reason);
8105 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8106 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008107 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8108 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008109}
8110
8111
8112static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8113 int reason)
8114{
8115 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008116 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008117 struct ieee80211_mgmt mgmt;
8118
Dmitry Shmidt04949592012-07-19 12:16:46 -07008119 if (drv->device_ap_sme)
8120 return wpa_driver_nl80211_sta_remove(bss, addr);
8121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008122 memset(&mgmt, 0, sizeof(mgmt));
8123 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8124 WLAN_FC_STYPE_DISASSOC);
8125 memcpy(mgmt.da, addr, ETH_ALEN);
8126 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8127 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8128 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8129 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8130 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008131 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8132 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008133}
8134
8135#endif /* HOSTAPD || CONFIG_AP */
8136
8137#ifdef HOSTAPD
8138
Jouni Malinen75ecf522011-06-27 15:19:46 -07008139static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8140{
8141 int i;
8142 int *old;
8143
8144 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8145 ifidx);
8146 for (i = 0; i < drv->num_if_indices; i++) {
8147 if (drv->if_indices[i] == 0) {
8148 drv->if_indices[i] = ifidx;
8149 return;
8150 }
8151 }
8152
8153 if (drv->if_indices != drv->default_if_indices)
8154 old = drv->if_indices;
8155 else
8156 old = NULL;
8157
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008158 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8159 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008160 if (!drv->if_indices) {
8161 if (!old)
8162 drv->if_indices = drv->default_if_indices;
8163 else
8164 drv->if_indices = old;
8165 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8166 "interfaces");
8167 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8168 return;
8169 } else if (!old)
8170 os_memcpy(drv->if_indices, drv->default_if_indices,
8171 sizeof(drv->default_if_indices));
8172 drv->if_indices[drv->num_if_indices] = ifidx;
8173 drv->num_if_indices++;
8174}
8175
8176
8177static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8178{
8179 int i;
8180
8181 for (i = 0; i < drv->num_if_indices; i++) {
8182 if (drv->if_indices[i] == ifidx) {
8183 drv->if_indices[i] = 0;
8184 break;
8185 }
8186 }
8187}
8188
8189
8190static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8191{
8192 int i;
8193
8194 for (i = 0; i < drv->num_if_indices; i++)
8195 if (drv->if_indices[i] == ifidx)
8196 return 1;
8197
8198 return 0;
8199}
8200
8201
8202static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8203 const char *bridge_ifname)
8204{
8205 struct i802_bss *bss = priv;
8206 struct wpa_driver_nl80211_data *drv = bss->drv;
8207 char name[IFNAMSIZ + 1];
8208
8209 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8210 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8211 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8212 if (val) {
8213 if (!if_nametoindex(name)) {
8214 if (nl80211_create_iface(drv, name,
8215 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008216 bss->addr, 1) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008217 return -1;
8218 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008219 linux_br_add_if(drv->global->ioctl_sock,
8220 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008221 return -1;
8222 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008223 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8224 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8225 "interface %s up", name);
8226 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008227 return i802_set_sta_vlan(priv, addr, name, 0);
8228 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008229 if (bridge_ifname)
8230 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8231 name);
8232
Jouni Malinen75ecf522011-06-27 15:19:46 -07008233 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8234 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8235 name);
8236 }
8237}
8238
8239
8240static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8241{
8242 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8243 struct sockaddr_ll lladdr;
8244 unsigned char buf[3000];
8245 int len;
8246 socklen_t fromlen = sizeof(lladdr);
8247
8248 len = recvfrom(sock, buf, sizeof(buf), 0,
8249 (struct sockaddr *)&lladdr, &fromlen);
8250 if (len < 0) {
8251 perror("recv");
8252 return;
8253 }
8254
8255 if (have_ifidx(drv, lladdr.sll_ifindex))
8256 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8257}
8258
8259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008260static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8261 struct i802_bss *bss,
8262 const char *brname, const char *ifname)
8263{
8264 int ifindex;
8265 char in_br[IFNAMSIZ];
8266
8267 os_strlcpy(bss->brname, brname, IFNAMSIZ);
8268 ifindex = if_nametoindex(brname);
8269 if (ifindex == 0) {
8270 /*
8271 * Bridge was configured, but the bridge device does
8272 * not exist. Try to add it now.
8273 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008274 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008275 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8276 "bridge interface %s: %s",
8277 brname, strerror(errno));
8278 return -1;
8279 }
8280 bss->added_bridge = 1;
8281 add_ifidx(drv, if_nametoindex(brname));
8282 }
8283
8284 if (linux_br_get(in_br, ifname) == 0) {
8285 if (os_strcmp(in_br, brname) == 0)
8286 return 0; /* already in the bridge */
8287
8288 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8289 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008290 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8291 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008292 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8293 "remove interface %s from bridge "
8294 "%s: %s",
8295 ifname, brname, strerror(errno));
8296 return -1;
8297 }
8298 }
8299
8300 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8301 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008302 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008303 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8304 "into bridge %s: %s",
8305 ifname, brname, strerror(errno));
8306 return -1;
8307 }
8308 bss->added_if_into_bridge = 1;
8309
8310 return 0;
8311}
8312
8313
8314static void *i802_init(struct hostapd_data *hapd,
8315 struct wpa_init_params *params)
8316{
8317 struct wpa_driver_nl80211_data *drv;
8318 struct i802_bss *bss;
8319 size_t i;
8320 char brname[IFNAMSIZ];
8321 int ifindex, br_ifindex;
8322 int br_added = 0;
8323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008324 bss = wpa_driver_nl80211_init(hapd, params->ifname,
8325 params->global_priv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008326 if (bss == NULL)
8327 return NULL;
8328
8329 drv = bss->drv;
8330 drv->nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008331 drv->eapol_sock = -1;
8332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008333 if (linux_br_get(brname, params->ifname) == 0) {
8334 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8335 params->ifname, brname);
8336 br_ifindex = if_nametoindex(brname);
8337 } else {
8338 brname[0] = '\0';
8339 br_ifindex = 0;
8340 }
8341
8342 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8343 drv->if_indices = drv->default_if_indices;
8344 for (i = 0; i < params->num_bridge; i++) {
8345 if (params->bridge[i]) {
8346 ifindex = if_nametoindex(params->bridge[i]);
8347 if (ifindex)
8348 add_ifidx(drv, ifindex);
8349 if (ifindex == br_ifindex)
8350 br_added = 1;
8351 }
8352 }
8353 if (!br_added && br_ifindex &&
8354 (params->num_bridge == 0 || !params->bridge[0]))
8355 add_ifidx(drv, br_ifindex);
8356
8357 /* start listening for EAPOL on the default AP interface */
8358 add_ifidx(drv, drv->ifindex);
8359
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008360 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008361 goto failed;
8362
8363 if (params->bssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008364 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008365 params->bssid))
8366 goto failed;
8367 }
8368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008369 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008370 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
8371 "into AP mode", bss->ifname);
8372 goto failed;
8373 }
8374
8375 if (params->num_bridge && params->bridge[0] &&
8376 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
8377 goto failed;
8378
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008379 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008380 goto failed;
8381
8382 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8383 if (drv->eapol_sock < 0) {
8384 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
8385 goto failed;
8386 }
8387
8388 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8389 {
8390 printf("Could not register read socket for eapol\n");
8391 goto failed;
8392 }
8393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008394 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8395 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008396 goto failed;
8397
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008398 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008400 return bss;
8401
8402failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008403 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008404 return NULL;
8405}
8406
8407
8408static void i802_deinit(void *priv)
8409{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008410 struct i802_bss *bss = priv;
8411 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008412}
8413
8414#endif /* HOSTAPD */
8415
8416
8417static enum nl80211_iftype wpa_driver_nl80211_if_type(
8418 enum wpa_driver_if_type type)
8419{
8420 switch (type) {
8421 case WPA_IF_STATION:
8422 return NL80211_IFTYPE_STATION;
8423 case WPA_IF_P2P_CLIENT:
8424 case WPA_IF_P2P_GROUP:
8425 return NL80211_IFTYPE_P2P_CLIENT;
8426 case WPA_IF_AP_VLAN:
8427 return NL80211_IFTYPE_AP_VLAN;
8428 case WPA_IF_AP_BSS:
8429 return NL80211_IFTYPE_AP;
8430 case WPA_IF_P2P_GO:
8431 return NL80211_IFTYPE_P2P_GO;
8432 }
8433 return -1;
8434}
8435
8436
8437#ifdef CONFIG_P2P
8438
8439static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8440{
8441 struct wpa_driver_nl80211_data *drv;
8442 dl_list_for_each(drv, &global->interfaces,
8443 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008444 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008445 return 1;
8446 }
8447 return 0;
8448}
8449
8450
8451static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8452 u8 *new_addr)
8453{
8454 unsigned int idx;
8455
8456 if (!drv->global)
8457 return -1;
8458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008459 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008460 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008461 new_addr[0] = drv->first_bss.addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462 new_addr[0] ^= idx << 2;
8463 if (!nl80211_addr_in_use(drv->global, new_addr))
8464 break;
8465 }
8466 if (idx == 64)
8467 return -1;
8468
8469 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8470 MACSTR, MAC2STR(new_addr));
8471
8472 return 0;
8473}
8474
8475#endif /* CONFIG_P2P */
8476
8477
8478static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8479 const char *ifname, const u8 *addr,
8480 void *bss_ctx, void **drv_priv,
8481 char *force_ifname, u8 *if_addr,
8482 const char *bridge)
8483{
8484 struct i802_bss *bss = priv;
8485 struct wpa_driver_nl80211_data *drv = bss->drv;
8486 int ifidx;
8487#ifdef HOSTAPD
8488 struct i802_bss *new_bss = NULL;
8489
8490 if (type == WPA_IF_AP_BSS) {
8491 new_bss = os_zalloc(sizeof(*new_bss));
8492 if (new_bss == NULL)
8493 return -1;
8494 }
8495#endif /* HOSTAPD */
8496
8497 if (addr)
8498 os_memcpy(if_addr, addr, ETH_ALEN);
8499 ifidx = nl80211_create_iface(drv, ifname,
8500 wpa_driver_nl80211_if_type(type), addr,
8501 0);
8502 if (ifidx < 0) {
8503#ifdef HOSTAPD
8504 os_free(new_bss);
8505#endif /* HOSTAPD */
8506 return -1;
8507 }
8508
8509 if (!addr &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008510 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8511 if_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008512 nl80211_remove_iface(drv, ifidx);
8513 return -1;
8514 }
8515
8516#ifdef CONFIG_P2P
8517 if (!addr &&
8518 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8519 type == WPA_IF_P2P_GO)) {
8520 /* Enforce unique P2P Interface Address */
8521 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008523 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8524 own_addr) < 0 ||
8525 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8526 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008527 nl80211_remove_iface(drv, ifidx);
8528 return -1;
8529 }
8530 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8531 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8532 "for P2P group interface");
8533 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
8534 nl80211_remove_iface(drv, ifidx);
8535 return -1;
8536 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008537 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008538 new_addr) < 0) {
8539 nl80211_remove_iface(drv, ifidx);
8540 return -1;
8541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008542 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008543 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008544 }
8545#endif /* CONFIG_P2P */
8546
8547#ifdef HOSTAPD
8548 if (bridge &&
8549 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8550 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8551 "interface %s to a bridge %s", ifname, bridge);
8552 nl80211_remove_iface(drv, ifidx);
8553 os_free(new_bss);
8554 return -1;
8555 }
8556
8557 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008558 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8559 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008560 nl80211_remove_iface(drv, ifidx);
8561 os_free(new_bss);
8562 return -1;
8563 }
8564 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008565 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008566 new_bss->ifindex = ifidx;
8567 new_bss->drv = drv;
8568 new_bss->next = drv->first_bss.next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008569 new_bss->freq = drv->first_bss.freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008570 new_bss->ctx = bss_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008571 drv->first_bss.next = new_bss;
8572 if (drv_priv)
8573 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008574 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008575
8576 /* Subscribe management frames for this WPA_IF_AP_BSS */
8577 if (nl80211_setup_ap(new_bss))
8578 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008579 }
8580#endif /* HOSTAPD */
8581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008582 if (drv->global)
8583 drv->global->if_add_ifindex = ifidx;
8584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008585 return 0;
8586}
8587
8588
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008589static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008590 enum wpa_driver_if_type type,
8591 const char *ifname)
8592{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008593 struct wpa_driver_nl80211_data *drv = bss->drv;
8594 int ifindex = if_nametoindex(ifname);
8595
8596 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8597 __func__, type, ifname, ifindex);
8598 if (ifindex <= 0)
8599 return -1;
8600
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008601 nl80211_remove_iface(drv, ifindex);
8602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008603#ifdef HOSTAPD
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008604 if (type != WPA_IF_AP_BSS)
8605 return 0;
8606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008607 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008608 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8609 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008610 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8611 "interface %s from bridge %s: %s",
8612 bss->ifname, bss->brname, strerror(errno));
8613 }
8614 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008615 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008616 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8617 "bridge %s: %s",
8618 bss->brname, strerror(errno));
8619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008620
8621 if (bss != &drv->first_bss) {
8622 struct i802_bss *tbss;
8623
8624 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8625 if (tbss->next == bss) {
8626 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008627 /* Unsubscribe management frames */
8628 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008629 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008630 os_free(bss);
8631 bss = NULL;
8632 break;
8633 }
8634 }
8635 if (bss)
8636 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8637 "BSS %p in the list", __func__, bss);
8638 }
8639#endif /* HOSTAPD */
8640
8641 return 0;
8642}
8643
8644
8645static int cookie_handler(struct nl_msg *msg, void *arg)
8646{
8647 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8648 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8649 u64 *cookie = arg;
8650 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8651 genlmsg_attrlen(gnlh, 0), NULL);
8652 if (tb[NL80211_ATTR_COOKIE])
8653 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8654 return NL_SKIP;
8655}
8656
8657
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008658static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008659 unsigned int freq, unsigned int wait,
8660 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008661 u64 *cookie_out, int no_cck, int no_ack,
8662 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008663{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008664 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008665 struct nl_msg *msg;
8666 u64 cookie;
8667 int ret = -1;
8668
8669 msg = nlmsg_alloc();
8670 if (!msg)
8671 return -1;
8672
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008673 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07008674 "no_ack=%d offchanok=%d",
8675 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008676 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008677
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008678 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008679 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008680 if (wait)
8681 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008682 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008683 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
8684 if (no_cck)
8685 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
8686 if (no_ack)
8687 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
8688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008689 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8690
8691 cookie = 0;
8692 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8693 msg = NULL;
8694 if (ret) {
8695 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008696 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8697 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008698 goto nla_put_failure;
8699 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07008700 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008701 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8702 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008703
8704 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008705 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008706
8707nla_put_failure:
8708 nlmsg_free(msg);
8709 return ret;
8710}
8711
8712
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008713static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
8714 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008715 unsigned int wait_time,
8716 const u8 *dst, const u8 *src,
8717 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008718 const u8 *data, size_t data_len,
8719 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008720{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008721 struct wpa_driver_nl80211_data *drv = bss->drv;
8722 int ret = -1;
8723 u8 *buf;
8724 struct ieee80211_hdr *hdr;
8725
8726 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008727 "freq=%u MHz wait=%d ms no_cck=%d)",
8728 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008729
8730 buf = os_zalloc(24 + data_len);
8731 if (buf == NULL)
8732 return ret;
8733 os_memcpy(buf + 24, data, data_len);
8734 hdr = (struct ieee80211_hdr *) buf;
8735 hdr->frame_control =
8736 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8737 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8738 os_memcpy(hdr->addr2, src, ETH_ALEN);
8739 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008741 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008742 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
8743 0, freq, no_cck, 1,
8744 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008745 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008746 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008747 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008748 &drv->send_action_cookie,
8749 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008750
8751 os_free(buf);
8752 return ret;
8753}
8754
8755
8756static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8757{
8758 struct i802_bss *bss = priv;
8759 struct wpa_driver_nl80211_data *drv = bss->drv;
8760 struct nl_msg *msg;
8761 int ret;
8762
8763 msg = nlmsg_alloc();
8764 if (!msg)
8765 return;
8766
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008767 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
8768 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008769 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008770
8771 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8772 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8773
8774 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8775 msg = NULL;
8776 if (ret)
8777 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8778 "(%s)", ret, strerror(-ret));
8779
8780 nla_put_failure:
8781 nlmsg_free(msg);
8782}
8783
8784
8785static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8786 unsigned int duration)
8787{
8788 struct i802_bss *bss = priv;
8789 struct wpa_driver_nl80211_data *drv = bss->drv;
8790 struct nl_msg *msg;
8791 int ret;
8792 u64 cookie;
8793
8794 msg = nlmsg_alloc();
8795 if (!msg)
8796 return -1;
8797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008798 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008799
8800 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8801 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8802 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8803
8804 cookie = 0;
8805 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008806 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008807 if (ret == 0) {
8808 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8809 "0x%llx for freq=%u MHz duration=%u",
8810 (long long unsigned int) cookie, freq, duration);
8811 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008812 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008813 return 0;
8814 }
8815 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8816 "(freq=%d duration=%u): %d (%s)",
8817 freq, duration, ret, strerror(-ret));
8818nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008819 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008820 return -1;
8821}
8822
8823
8824static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8825{
8826 struct i802_bss *bss = priv;
8827 struct wpa_driver_nl80211_data *drv = bss->drv;
8828 struct nl_msg *msg;
8829 int ret;
8830
8831 if (!drv->pending_remain_on_chan) {
8832 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8833 "to cancel");
8834 return -1;
8835 }
8836
8837 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8838 "0x%llx",
8839 (long long unsigned int) drv->remain_on_chan_cookie);
8840
8841 msg = nlmsg_alloc();
8842 if (!msg)
8843 return -1;
8844
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008845 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008846
8847 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8848 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8849
8850 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008851 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008852 if (ret == 0)
8853 return 0;
8854 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8855 "%d (%s)", ret, strerror(-ret));
8856nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008857 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008858 return -1;
8859}
8860
8861
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008862static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008863{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008864 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008866 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008867 if (bss->nl_preq && drv->device_ap_sme &&
8868 is_ap_interface(drv->nlmode)) {
8869 /*
8870 * Do not disable Probe Request reporting that was
8871 * enabled in nl80211_setup_ap().
8872 */
8873 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8874 "Probe Request reporting nl_preq=%p while "
8875 "in AP mode", bss->nl_preq);
8876 } else if (bss->nl_preq) {
8877 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8878 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008879 eloop_unregister_read_sock(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008880 nl_socket_get_fd(bss->nl_preq));
8881 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008882 }
8883 return 0;
8884 }
8885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008886 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008887 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008888 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008889 return 0;
8890 }
8891
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008892 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8893 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008894 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008895 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8896 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008898 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008899 (WLAN_FC_TYPE_MGMT << 2) |
8900 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008901 NULL, 0) < 0)
8902 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07008903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008904 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8905 wpa_driver_nl80211_event_receive, bss->nl_cb,
8906 bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008907
8908 return 0;
8909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008910 out_err:
8911 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008912 return -1;
8913}
8914
8915
8916static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8917 int ifindex, int disabled)
8918{
8919 struct nl_msg *msg;
8920 struct nlattr *bands, *band;
8921 int ret;
8922
8923 msg = nlmsg_alloc();
8924 if (!msg)
8925 return -1;
8926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008927 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008928 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8929
8930 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8931 if (!bands)
8932 goto nla_put_failure;
8933
8934 /*
8935 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8936 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8937 * rates. All 5 GHz rates are left enabled.
8938 */
8939 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8940 if (!band)
8941 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008942 if (disabled) {
8943 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8944 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008946 nla_nest_end(msg, band);
8947
8948 nla_nest_end(msg, bands);
8949
8950 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8951 msg = NULL;
8952 if (ret) {
8953 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8954 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008955 } else
8956 drv->disabled_11b_rates = disabled;
8957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008958 return ret;
8959
8960nla_put_failure:
8961 nlmsg_free(msg);
8962 return -1;
8963}
8964
8965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008966static int wpa_driver_nl80211_deinit_ap(void *priv)
8967{
8968 struct i802_bss *bss = priv;
8969 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008970 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008971 return -1;
8972 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008973 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008974}
8975
8976
Dmitry Shmidt04949592012-07-19 12:16:46 -07008977static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8978{
8979 struct i802_bss *bss = priv;
8980 struct wpa_driver_nl80211_data *drv = bss->drv;
8981 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8982 return -1;
8983 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8984}
8985
8986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008987static void wpa_driver_nl80211_resume(void *priv)
8988{
8989 struct i802_bss *bss = priv;
8990 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008991 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008992 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8993 "resume event");
8994 }
8995}
8996
8997
8998static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8999 const u8 *ies, size_t ies_len)
9000{
9001 struct i802_bss *bss = priv;
9002 struct wpa_driver_nl80211_data *drv = bss->drv;
9003 int ret;
9004 u8 *data, *pos;
9005 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009006 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009007
9008 if (action != 1) {
9009 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9010 "action %d", action);
9011 return -1;
9012 }
9013
9014 /*
9015 * Action frame payload:
9016 * Category[1] = 6 (Fast BSS Transition)
9017 * Action[1] = 1 (Fast BSS Transition Request)
9018 * STA Address
9019 * Target AP Address
9020 * FT IEs
9021 */
9022
9023 data_len = 2 + 2 * ETH_ALEN + ies_len;
9024 data = os_malloc(data_len);
9025 if (data == NULL)
9026 return -1;
9027 pos = data;
9028 *pos++ = 0x06; /* FT Action category */
9029 *pos++ = action;
9030 os_memcpy(pos, own_addr, ETH_ALEN);
9031 pos += ETH_ALEN;
9032 os_memcpy(pos, target_ap, ETH_ALEN);
9033 pos += ETH_ALEN;
9034 os_memcpy(pos, ies, ies_len);
9035
9036 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9037 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009038 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009039 os_free(data);
9040
9041 return ret;
9042}
9043
9044
9045static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9046{
9047 struct i802_bss *bss = priv;
9048 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009049 struct nl_msg *msg;
9050 struct nlattr *cqm;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009051 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009052
9053 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9054 "hysteresis=%d", threshold, hysteresis);
9055
9056 msg = nlmsg_alloc();
9057 if (!msg)
9058 return -1;
9059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009060 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009061
9062 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9063
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009064 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009065 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009066 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009067
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07009068 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9069 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
9070 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009071
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009072 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009073 msg = NULL;
9074
9075nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009076 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009077 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009078}
9079
9080
9081static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9082{
9083 struct i802_bss *bss = priv;
9084 struct wpa_driver_nl80211_data *drv = bss->drv;
9085 int res;
9086
9087 os_memset(si, 0, sizeof(*si));
9088 res = nl80211_get_link_signal(drv, si);
9089 if (res != 0)
9090 return res;
9091
9092 return nl80211_get_link_noise(drv, si);
9093}
9094
9095
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009096static int wpa_driver_nl80211_shared_freq(void *priv)
9097{
9098 struct i802_bss *bss = priv;
9099 struct wpa_driver_nl80211_data *drv = bss->drv;
9100 struct wpa_driver_nl80211_data *driver;
9101 int freq = 0;
9102
9103 /*
9104 * If the same PHY is in connected state with some other interface,
9105 * then retrieve the assoc freq.
9106 */
9107 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9108 drv->phyname);
9109
9110 dl_list_for_each(driver, &drv->global->interfaces,
9111 struct wpa_driver_nl80211_data, list) {
9112 if (drv == driver ||
9113 os_strcmp(drv->phyname, driver->phyname) != 0 ||
9114#ifdef ANDROID_P2P
9115 (!driver->associated && !is_ap_interface(driver->nlmode)))
9116#else
9117 !driver->associated)
9118#endif
9119 continue;
9120
9121 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9122 MACSTR,
9123 driver->phyname, driver->first_bss.ifname,
9124 MAC2STR(driver->first_bss.addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07009125 if (is_ap_interface(driver->nlmode))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009126 freq = driver->first_bss.freq;
9127 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009128 freq = nl80211_get_assoc_freq(driver);
9129 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9130 drv->phyname, freq);
9131 }
9132
9133 if (!freq)
9134 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9135 "PHY (%s) in associated state", drv->phyname);
9136
9137 return freq;
9138}
9139
9140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009141static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9142 int encrypt)
9143{
9144 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009145 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9146 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009147}
9148
9149
9150static int nl80211_set_param(void *priv, const char *param)
9151{
9152 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9153 if (param == NULL)
9154 return 0;
9155
9156#ifdef CONFIG_P2P
9157 if (os_strstr(param, "use_p2p_group_interface=1")) {
9158 struct i802_bss *bss = priv;
9159 struct wpa_driver_nl80211_data *drv = bss->drv;
9160
9161 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9162 "interface");
9163 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9164 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9165 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009166#ifdef ANDROID_P2P
9167 if(os_strstr(param, "use_multi_chan_concurrent=1")) {
9168 struct i802_bss *bss = priv;
9169 struct wpa_driver_nl80211_data *drv = bss->drv;
9170 wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel "
9171 "concurrency");
9172 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
9173 }
9174#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009175#endif /* CONFIG_P2P */
9176
9177 return 0;
9178}
9179
9180
9181static void * nl80211_global_init(void)
9182{
9183 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009184 struct netlink_config *cfg;
9185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009186 global = os_zalloc(sizeof(*global));
9187 if (global == NULL)
9188 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009189 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009190 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009191 global->if_add_ifindex = -1;
9192
9193 cfg = os_zalloc(sizeof(*cfg));
9194 if (cfg == NULL)
9195 goto err;
9196
9197 cfg->ctx = global;
9198 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9199 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9200 global->netlink = netlink_init(cfg);
9201 if (global->netlink == NULL) {
9202 os_free(cfg);
9203 goto err;
9204 }
9205
9206 if (wpa_driver_nl80211_init_nl_global(global) < 0)
9207 goto err;
9208
9209 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
9210 if (global->ioctl_sock < 0) {
9211 perror("socket(PF_INET,SOCK_DGRAM)");
9212 goto err;
9213 }
9214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009215 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009216
9217err:
9218 nl80211_global_deinit(global);
9219 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009220}
9221
9222
9223static void nl80211_global_deinit(void *priv)
9224{
9225 struct nl80211_global *global = priv;
9226 if (global == NULL)
9227 return;
9228 if (!dl_list_empty(&global->interfaces)) {
9229 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
9230 "nl80211_global_deinit",
9231 dl_list_len(&global->interfaces));
9232 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009233
9234 if (global->netlink)
9235 netlink_deinit(global->netlink);
9236
9237 nl_destroy_handles(&global->nl);
9238
9239 if (global->nl_event) {
9240 eloop_unregister_read_sock(
9241 nl_socket_get_fd(global->nl_event));
9242 nl_destroy_handles(&global->nl_event);
9243 }
9244
9245 nl_cb_put(global->nl_cb);
9246
9247 if (global->ioctl_sock >= 0)
9248 close(global->ioctl_sock);
9249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009250 os_free(global);
9251}
9252
9253
9254static const char * nl80211_get_radio_name(void *priv)
9255{
9256 struct i802_bss *bss = priv;
9257 struct wpa_driver_nl80211_data *drv = bss->drv;
9258 return drv->phyname;
9259}
9260
9261
Jouni Malinen75ecf522011-06-27 15:19:46 -07009262static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
9263 const u8 *pmkid)
9264{
9265 struct nl_msg *msg;
9266
9267 msg = nlmsg_alloc();
9268 if (!msg)
9269 return -ENOMEM;
9270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009271 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009272
9273 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9274 if (pmkid)
9275 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9276 if (bssid)
9277 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9278
9279 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9280 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009281 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009282 return -ENOBUFS;
9283}
9284
9285
9286static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9287{
9288 struct i802_bss *bss = priv;
9289 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9290 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9291}
9292
9293
9294static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9295{
9296 struct i802_bss *bss = priv;
9297 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9298 MAC2STR(bssid));
9299 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9300}
9301
9302
9303static int nl80211_flush_pmkid(void *priv)
9304{
9305 struct i802_bss *bss = priv;
9306 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9307 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9308}
9309
9310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009311static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9312 const u8 *replay_ctr)
9313{
9314 struct i802_bss *bss = priv;
9315 struct wpa_driver_nl80211_data *drv = bss->drv;
9316 struct nlattr *replay_nested;
9317 struct nl_msg *msg;
9318
9319 msg = nlmsg_alloc();
9320 if (!msg)
9321 return;
9322
9323 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9324
9325 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9326
9327 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9328 if (!replay_nested)
9329 goto nla_put_failure;
9330
9331 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9332 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9333 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9334 replay_ctr);
9335
9336 nla_nest_end(msg, replay_nested);
9337
9338 send_and_recv_msgs(drv, msg, NULL, NULL);
9339 return;
9340 nla_put_failure:
9341 nlmsg_free(msg);
9342}
9343
9344
9345static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9346 const u8 *addr, int qos)
9347{
9348 /* send data frame to poll STA and check whether
9349 * this frame is ACKed */
9350 struct {
9351 struct ieee80211_hdr hdr;
9352 u16 qos_ctl;
9353 } STRUCT_PACKED nulldata;
9354 size_t size;
9355
9356 /* Send data frame to poll STA and check whether this frame is ACKed */
9357
9358 os_memset(&nulldata, 0, sizeof(nulldata));
9359
9360 if (qos) {
9361 nulldata.hdr.frame_control =
9362 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9363 WLAN_FC_STYPE_QOS_NULL);
9364 size = sizeof(nulldata);
9365 } else {
9366 nulldata.hdr.frame_control =
9367 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9368 WLAN_FC_STYPE_NULLFUNC);
9369 size = sizeof(struct ieee80211_hdr);
9370 }
9371
9372 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9373 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9374 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9375 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9376
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009377 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9378 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009379 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9380 "send poll frame");
9381}
9382
9383static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9384 int qos)
9385{
9386 struct i802_bss *bss = priv;
9387 struct wpa_driver_nl80211_data *drv = bss->drv;
9388 struct nl_msg *msg;
9389
9390 if (!drv->poll_command_supported) {
9391 nl80211_send_null_frame(bss, own_addr, addr, qos);
9392 return;
9393 }
9394
9395 msg = nlmsg_alloc();
9396 if (!msg)
9397 return;
9398
9399 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9400
9401 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9402 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9403
9404 send_and_recv_msgs(drv, msg, NULL, NULL);
9405 return;
9406 nla_put_failure:
9407 nlmsg_free(msg);
9408}
9409
9410
9411static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9412{
9413 struct nl_msg *msg;
9414
9415 msg = nlmsg_alloc();
9416 if (!msg)
9417 return -ENOMEM;
9418
9419 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9421 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9422 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9423 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9424nla_put_failure:
9425 nlmsg_free(msg);
9426 return -ENOBUFS;
9427}
9428
9429
9430static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9431 int ctwindow)
9432{
9433 struct i802_bss *bss = priv;
9434
9435 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9436 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9437
9438 if (opp_ps != -1 || ctwindow != -1)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009439#ifdef ANDROID_P2P
9440 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
9441#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009442 return -1; /* Not yet supported */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009443#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009444
9445 if (legacy_ps == -1)
9446 return 0;
9447 if (legacy_ps != 0 && legacy_ps != 1)
9448 return -1; /* Not yet supported */
9449
9450 return nl80211_set_power_save(bss, legacy_ps);
9451}
9452
9453
9454#ifdef CONFIG_TDLS
9455
9456static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9457 u8 dialog_token, u16 status_code,
9458 const u8 *buf, size_t len)
9459{
9460 struct i802_bss *bss = priv;
9461 struct wpa_driver_nl80211_data *drv = bss->drv;
9462 struct nl_msg *msg;
9463
9464 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9465 return -EOPNOTSUPP;
9466
9467 if (!dst)
9468 return -EINVAL;
9469
9470 msg = nlmsg_alloc();
9471 if (!msg)
9472 return -ENOMEM;
9473
9474 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9475 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9476 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9477 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9478 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9479 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9480 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9481
9482 return send_and_recv_msgs(drv, msg, NULL, NULL);
9483
9484nla_put_failure:
9485 nlmsg_free(msg);
9486 return -ENOBUFS;
9487}
9488
9489
9490static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9491{
9492 struct i802_bss *bss = priv;
9493 struct wpa_driver_nl80211_data *drv = bss->drv;
9494 struct nl_msg *msg;
9495 enum nl80211_tdls_operation nl80211_oper;
9496
9497 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9498 return -EOPNOTSUPP;
9499
9500 switch (oper) {
9501 case TDLS_DISCOVERY_REQ:
9502 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9503 break;
9504 case TDLS_SETUP:
9505 nl80211_oper = NL80211_TDLS_SETUP;
9506 break;
9507 case TDLS_TEARDOWN:
9508 nl80211_oper = NL80211_TDLS_TEARDOWN;
9509 break;
9510 case TDLS_ENABLE_LINK:
9511 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9512 break;
9513 case TDLS_DISABLE_LINK:
9514 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9515 break;
9516 case TDLS_ENABLE:
9517 return 0;
9518 case TDLS_DISABLE:
9519 return 0;
9520 default:
9521 return -EINVAL;
9522 }
9523
9524 msg = nlmsg_alloc();
9525 if (!msg)
9526 return -ENOMEM;
9527
9528 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9529 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9530 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9531 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9532
9533 return send_and_recv_msgs(drv, msg, NULL, NULL);
9534
9535nla_put_failure:
9536 nlmsg_free(msg);
9537 return -ENOBUFS;
9538}
9539
9540#endif /* CONFIG TDLS */
9541
9542
9543#ifdef ANDROID
9544
9545typedef struct android_wifi_priv_cmd {
9546 char *buf;
9547 int used_len;
9548 int total_len;
9549} android_wifi_priv_cmd;
9550
9551static int drv_errors = 0;
9552
9553static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9554{
9555 drv_errors++;
9556 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9557 drv_errors = 0;
9558 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9559 }
9560}
9561
9562
9563static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9564{
9565 struct wpa_driver_nl80211_data *drv = bss->drv;
9566 struct ifreq ifr;
9567 android_wifi_priv_cmd priv_cmd;
9568 char buf[MAX_DRV_CMD_SIZE];
9569 int ret;
9570
9571 os_memset(&ifr, 0, sizeof(ifr));
9572 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9573 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9574
9575 os_memset(buf, 0, sizeof(buf));
9576 os_strlcpy(buf, cmd, sizeof(buf));
9577
9578 priv_cmd.buf = buf;
9579 priv_cmd.used_len = sizeof(buf);
9580 priv_cmd.total_len = sizeof(buf);
9581 ifr.ifr_data = &priv_cmd;
9582
9583 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9584 if (ret < 0) {
9585 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9586 __func__);
9587 wpa_driver_send_hang_msg(drv);
9588 return ret;
9589 }
9590
9591 drv_errors = 0;
9592 return 0;
9593}
9594
9595
9596static int android_pno_start(struct i802_bss *bss,
9597 struct wpa_driver_scan_params *params)
9598{
9599 struct wpa_driver_nl80211_data *drv = bss->drv;
9600 struct ifreq ifr;
9601 android_wifi_priv_cmd priv_cmd;
9602 int ret = 0, i = 0, bp;
9603 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9604
9605 bp = WEXT_PNOSETUP_HEADER_SIZE;
9606 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9607 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9608 buf[bp++] = WEXT_PNO_TLV_VERSION;
9609 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9610 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9611
9612 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9613 /* Check that there is enough space needed for 1 more SSID, the
9614 * other sections and null termination */
9615 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9616 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9617 break;
9618 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
9619 params->ssids[i].ssid,
9620 params->ssids[i].ssid_len);
9621 buf[bp++] = WEXT_PNO_SSID_SECTION;
9622 buf[bp++] = params->ssids[i].ssid_len;
9623 os_memcpy(&buf[bp], params->ssids[i].ssid,
9624 params->ssids[i].ssid_len);
9625 bp += params->ssids[i].ssid_len;
9626 i++;
9627 }
9628
9629 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9630 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9631 WEXT_PNO_SCAN_INTERVAL);
9632 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9633
9634 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9635 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9636 WEXT_PNO_REPEAT);
9637 bp += WEXT_PNO_REPEAT_LENGTH;
9638
9639 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9640 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9641 WEXT_PNO_MAX_REPEAT);
9642 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9643
9644 memset(&ifr, 0, sizeof(ifr));
9645 memset(&priv_cmd, 0, sizeof(priv_cmd));
9646 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9647
9648 priv_cmd.buf = buf;
9649 priv_cmd.used_len = bp;
9650 priv_cmd.total_len = bp;
9651 ifr.ifr_data = &priv_cmd;
9652
9653 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9654
9655 if (ret < 0) {
9656 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9657 ret);
9658 wpa_driver_send_hang_msg(drv);
9659 return ret;
9660 }
9661
9662 drv_errors = 0;
9663
9664 return android_priv_cmd(bss, "PNOFORCE 1");
9665}
9666
9667
9668static int android_pno_stop(struct i802_bss *bss)
9669{
9670 return android_priv_cmd(bss, "PNOFORCE 0");
9671}
9672
9673#endif /* ANDROID */
9674
9675
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009676static int driver_nl80211_set_key(const char *ifname, void *priv,
9677 enum wpa_alg alg, const u8 *addr,
9678 int key_idx, int set_tx,
9679 const u8 *seq, size_t seq_len,
9680 const u8 *key, size_t key_len)
9681{
9682 struct i802_bss *bss = priv;
9683 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
9684 set_tx, seq, seq_len, key, key_len);
9685}
9686
9687
9688static int driver_nl80211_scan2(void *priv,
9689 struct wpa_driver_scan_params *params)
9690{
9691 struct i802_bss *bss = priv;
9692 return wpa_driver_nl80211_scan(bss, params);
9693}
9694
9695
9696static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
9697 int reason_code)
9698{
9699 struct i802_bss *bss = priv;
9700 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
9701}
9702
9703
9704static int driver_nl80211_authenticate(void *priv,
9705 struct wpa_driver_auth_params *params)
9706{
9707 struct i802_bss *bss = priv;
9708 return wpa_driver_nl80211_authenticate(bss, params);
9709}
9710
9711
9712static void driver_nl80211_deinit(void *priv)
9713{
9714 struct i802_bss *bss = priv;
9715 wpa_driver_nl80211_deinit(bss);
9716}
9717
9718
9719static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9720 const char *ifname)
9721{
9722 struct i802_bss *bss = priv;
9723 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9724}
9725
9726
9727static int driver_nl80211_send_mlme(void *priv, const u8 *data,
9728 size_t data_len, int noack)
9729{
9730 struct i802_bss *bss = priv;
9731 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
9732 0, 0, 0, 0);
9733}
9734
9735
9736static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9737{
9738 struct i802_bss *bss = priv;
9739 return wpa_driver_nl80211_sta_remove(bss, addr);
9740}
9741
9742
9743#if defined(HOSTAPD) || defined(CONFIG_AP)
9744static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9745 const char *ifname, int vlan_id)
9746{
9747 struct i802_bss *bss = priv;
9748 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9749}
9750#endif /* HOSTAPD || CONFIG_AP */
9751
9752
9753static int driver_nl80211_read_sta_data(void *priv,
9754 struct hostap_sta_driver_data *data,
9755 const u8 *addr)
9756{
9757 struct i802_bss *bss = priv;
9758 return i802_read_sta_data(bss, data, addr);
9759}
9760
9761
9762static int driver_nl80211_send_action(void *priv, unsigned int freq,
9763 unsigned int wait_time,
9764 const u8 *dst, const u8 *src,
9765 const u8 *bssid,
9766 const u8 *data, size_t data_len,
9767 int no_cck)
9768{
9769 struct i802_bss *bss = priv;
9770 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9771 bssid, data, data_len, no_cck);
9772}
9773
9774
9775static int driver_nl80211_probe_req_report(void *priv, int report)
9776{
9777 struct i802_bss *bss = priv;
9778 return wpa_driver_nl80211_probe_req_report(bss, report);
9779}
9780
9781
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009782static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9783 const u8 *ies, size_t ies_len)
9784{
9785 int ret;
9786 struct nl_msg *msg;
9787 struct i802_bss *bss = priv;
9788 struct wpa_driver_nl80211_data *drv = bss->drv;
9789 u16 mdid = WPA_GET_LE16(md);
9790
9791 msg = nlmsg_alloc();
9792 if (!msg)
9793 return -ENOMEM;
9794
9795 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9796 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
9797 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9798 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
9799 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
9800
9801 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9802 if (ret) {
9803 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9804 "err=%d (%s)", ret, strerror(-ret));
9805 }
9806
9807 return ret;
9808
9809nla_put_failure:
9810 nlmsg_free(msg);
9811 return -ENOBUFS;
9812}
9813
9814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009815const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9816 .name = "nl80211",
9817 .desc = "Linux nl80211/cfg80211",
9818 .get_bssid = wpa_driver_nl80211_get_bssid,
9819 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009820 .set_key = driver_nl80211_set_key,
9821 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009822 .sched_scan = wpa_driver_nl80211_sched_scan,
9823 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009824 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009825 .deauthenticate = driver_nl80211_deauthenticate,
9826 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009827 .associate = wpa_driver_nl80211_associate,
9828 .global_init = nl80211_global_init,
9829 .global_deinit = nl80211_global_deinit,
9830 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009831 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009832 .get_capa = wpa_driver_nl80211_get_capa,
9833 .set_operstate = wpa_driver_nl80211_set_operstate,
9834 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9835 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009836 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009837 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009838 .if_remove = driver_nl80211_if_remove,
9839 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009840 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
9841 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009842 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009843 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9844 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
9845#ifdef HOSTAPD
9846 .hapd_init = i802_init,
9847 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009848 .set_wds_sta = i802_set_wds_sta,
9849#endif /* HOSTAPD */
9850#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009851 .get_seqnum = i802_get_seqnum,
9852 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009853 .get_inact_sec = i802_get_inact_sec,
9854 .sta_clear_stats = i802_sta_clear_stats,
9855 .set_rts = i802_set_rts,
9856 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009857 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009858 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009859 .sta_deauth = i802_sta_deauth,
9860 .sta_disassoc = i802_sta_disassoc,
9861#endif /* HOSTAPD || CONFIG_AP */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009862 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009863 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009864 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009865 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9866 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9867 .cancel_remain_on_channel =
9868 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009869 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009870 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009871 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009872 .resume = wpa_driver_nl80211_resume,
9873 .send_ft_action = nl80211_send_ft_action,
9874 .signal_monitor = nl80211_signal_monitor,
9875 .signal_poll = nl80211_signal_poll,
9876 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009877 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009878 .set_param = nl80211_set_param,
9879 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009880 .add_pmkid = nl80211_add_pmkid,
9881 .remove_pmkid = nl80211_remove_pmkid,
9882 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009883 .set_rekey_info = nl80211_set_rekey_info,
9884 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009885 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009886#ifdef CONFIG_TDLS
9887 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9888 .tdls_oper = nl80211_tdls_oper,
9889#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009890 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009891#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009892 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009893 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009894 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
9895#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009896#ifdef ANDROID
9897 .driver_cmd = wpa_driver_nl80211_driver_cmd,
9898#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009899};