blob: c2b9fffbb6893146e2b78ebefd7db7af21b9aecb [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;
217 int has_capability;
218
219 int operstate;
220
221 int scan_complete_events;
222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 struct nl_cb *nl_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224
225 u8 auth_bssid[ETH_ALEN];
226 u8 bssid[ETH_ALEN];
227 int associated;
228 u8 ssid[32];
229 size_t ssid_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 enum nl80211_iftype nlmode;
231 enum nl80211_iftype ap_scan_as_station;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 unsigned int assoc_freq;
233
234 int monitor_sock;
235 int monitor_ifidx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236 int monitor_refcount;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238 unsigned int disabled_11b_rates:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 unsigned int pending_remain_on_chan:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800240 unsigned int in_interface_list:1;
241 unsigned int device_ap_sme:1;
242 unsigned int poll_command_supported:1;
243 unsigned int data_tx_status:1;
244 unsigned int scan_for_auth:1;
245 unsigned int retry_auth:1;
246 unsigned int use_monitor:1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800247 unsigned int ignore_next_local_disconnect:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 u64 remain_on_chan_cookie;
250 u64 send_action_cookie;
251
252 unsigned int last_mgmt_freq;
253
254 struct wpa_driver_scan_filter *filter_ssids;
255 size_t num_filter_ssids;
256
257 struct i802_bss first_bss;
258
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800259 int eapol_tx_sock;
260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261#ifdef HOSTAPD
262 int eapol_sock; /* socket for EAPOL frames */
263
264 int default_if_indices[16];
265 int *if_indices;
266 int num_if_indices;
267
268 int last_freq;
269 int last_freq_ht;
270#endif /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800271
272 /* From failed authentication command */
273 int auth_freq;
274 u8 auth_bssid_[ETH_ALEN];
275 u8 auth_ssid[32];
276 size_t auth_ssid_len;
277 int auth_alg;
278 u8 *auth_ie;
279 size_t auth_ie_len;
280 u8 auth_wep_key[4][16];
281 size_t auth_wep_key_len[4];
282 int auth_wep_tx_keyidx;
283 int auth_local_state_change;
284 int auth_p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285};
286
287
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800288static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
290 void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
292 enum nl80211_iftype nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293static int
294wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
295static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
296 const u8 *addr, int cmd, u16 reason_code,
297 int local_state_change);
298static void nl80211_remove_monitor_interface(
299 struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800300static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800302 const u8 *buf, size_t buf_len, u64 *cookie,
303 int no_cck, int no_ack, int offchanok);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800304static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
305 int report);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306#ifdef ANDROID
307static int android_pno_start(struct i802_bss *bss,
308 struct wpa_driver_scan_params *params);
309static int android_pno_stop(struct i802_bss *bss);
310#endif /* ANDROID */
311#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700312int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700314int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
315int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
316 const struct wpabuf *proberesp,
317 const struct wpabuf *assocresp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800319#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320#ifdef HOSTAPD
321static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
322static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
323static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800324static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 enum wpa_driver_if_type type,
326 const char *ifname);
327#else /* HOSTAPD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800328static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
329{
330}
331
332static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
333{
334}
335
336static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337{
338 return 0;
339}
340#endif /* HOSTAPD */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700341#ifdef ANDROID
342extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
343 size_t buf_len);
344#endif
345
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800346static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
347 struct hostapd_freq_params *freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
349 int ifindex, int disabled);
350
351static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800352static int wpa_driver_nl80211_authenticate_retry(
353 struct wpa_driver_nl80211_data *drv);
354
355
356static int is_ap_interface(enum nl80211_iftype nlmode)
357{
358 return (nlmode == NL80211_IFTYPE_AP ||
359 nlmode == NL80211_IFTYPE_P2P_GO);
360}
361
362
363static int is_sta_interface(enum nl80211_iftype nlmode)
364{
365 return (nlmode == NL80211_IFTYPE_STATION ||
366 nlmode == NL80211_IFTYPE_P2P_CLIENT);
367}
368
369
370static int is_p2p_interface(enum nl80211_iftype nlmode)
371{
372 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
373 nlmode == NL80211_IFTYPE_P2P_GO);
374}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376
Jouni Malinen87fd2792011-05-16 18:35:42 +0300377struct nl80211_bss_info_arg {
378 struct wpa_driver_nl80211_data *drv;
379 struct wpa_scan_results *res;
380 unsigned int assoc_freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381 u8 assoc_bssid[ETH_ALEN];
Jouni Malinen87fd2792011-05-16 18:35:42 +0300382};
383
384static int bss_info_handler(struct nl_msg *msg, void *arg);
385
386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387/* nl80211 code */
388static int ack_handler(struct nl_msg *msg, void *arg)
389{
390 int *err = arg;
391 *err = 0;
392 return NL_STOP;
393}
394
395static int finish_handler(struct nl_msg *msg, void *arg)
396{
397 int *ret = arg;
398 *ret = 0;
399 return NL_SKIP;
400}
401
402static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
403 void *arg)
404{
405 int *ret = arg;
406 *ret = err->error;
407 return NL_SKIP;
408}
409
410
411static int no_seq_check(struct nl_msg *msg, void *arg)
412{
413 return NL_OK;
414}
415
416
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800417static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 struct nl_handle *nl_handle, struct nl_msg *msg,
419 int (*valid_handler)(struct nl_msg *, void *),
420 void *valid_data)
421{
422 struct nl_cb *cb;
423 int err = -ENOMEM;
424
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800425 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 if (!cb)
427 goto out;
428
429 err = nl_send_auto_complete(nl_handle, msg);
430 if (err < 0)
431 goto out;
432
433 err = 1;
434
435 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
436 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
437 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
438
439 if (valid_handler)
440 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
441 valid_handler, valid_data);
442
443 while (err > 0)
444 nl_recvmsgs(nl_handle, cb);
445 out:
446 nl_cb_put(cb);
447 nlmsg_free(msg);
448 return err;
449}
450
451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800452static int send_and_recv_msgs_global(struct nl80211_global *global,
453 struct nl_msg *msg,
454 int (*valid_handler)(struct nl_msg *, void *),
455 void *valid_data)
456{
457 return send_and_recv(global, global->nl, msg, valid_handler,
458 valid_data);
459}
460
Dmitry Shmidt04949592012-07-19 12:16:46 -0700461
Jouni Malinen80da0422012-08-09 15:29:25 -0700462#ifndef ANDROID
463static
464#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700465int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 struct nl_msg *msg,
467 int (*valid_handler)(struct nl_msg *, void *),
468 void *valid_data)
469{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800470 return send_and_recv(drv->global, drv->global->nl, msg,
471 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472}
473
474
475struct family_data {
476 const char *group;
477 int id;
478};
479
480
481static int family_handler(struct nl_msg *msg, void *arg)
482{
483 struct family_data *res = arg;
484 struct nlattr *tb[CTRL_ATTR_MAX + 1];
485 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
486 struct nlattr *mcgrp;
487 int i;
488
489 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
490 genlmsg_attrlen(gnlh, 0), NULL);
491 if (!tb[CTRL_ATTR_MCAST_GROUPS])
492 return NL_SKIP;
493
494 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
495 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
496 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
497 nla_len(mcgrp), NULL);
498 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
499 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
500 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
501 res->group,
502 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
503 continue;
504 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
505 break;
506 };
507
508 return NL_SKIP;
509}
510
511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800512static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 const char *family, const char *group)
514{
515 struct nl_msg *msg;
516 int ret = -1;
517 struct family_data res = { group, -ENOENT };
518
519 msg = nlmsg_alloc();
520 if (!msg)
521 return -ENOMEM;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 0, 0, CTRL_CMD_GETFAMILY, 0);
524 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
525
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800526 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 msg = NULL;
528 if (ret == 0)
529 ret = res.id;
530
531nla_put_failure:
532 nlmsg_free(msg);
533 return ret;
534}
535
536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800537static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
538 struct nl_msg *msg, int flags, uint8_t cmd)
539{
540 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
541 0, flags, cmd, 0);
542}
543
544
545struct wiphy_idx_data {
546 int wiphy_idx;
547};
548
549
550static int netdev_info_handler(struct nl_msg *msg, void *arg)
551{
552 struct nlattr *tb[NL80211_ATTR_MAX + 1];
553 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
554 struct wiphy_idx_data *info = arg;
555
556 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
557 genlmsg_attrlen(gnlh, 0), NULL);
558
559 if (tb[NL80211_ATTR_WIPHY])
560 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
561
562 return NL_SKIP;
563}
564
565
566static int nl80211_get_wiphy_index(struct i802_bss *bss)
567{
568 struct nl_msg *msg;
569 struct wiphy_idx_data data = {
570 .wiphy_idx = -1,
571 };
572
573 msg = nlmsg_alloc();
574 if (!msg)
575 return -1;
576
577 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
578
579 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
580
581 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
582 return data.wiphy_idx;
583 msg = NULL;
584nla_put_failure:
585 nlmsg_free(msg);
586 return -1;
587}
588
589
590static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
591 struct nl80211_wiphy_data *w)
592{
593 struct nl_msg *msg;
594 int ret = -1;
595
596 msg = nlmsg_alloc();
597 if (!msg)
598 return -1;
599
600 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
601
602 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
603
604 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
605 msg = NULL;
606 if (ret) {
607 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
608 "failed: ret=%d (%s)",
609 ret, strerror(-ret));
610 goto nla_put_failure;
611 }
612 ret = 0;
613nla_put_failure:
614 nlmsg_free(msg);
615 return ret;
616}
617
618
619static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
620{
621 struct nl80211_wiphy_data *w = eloop_ctx;
622
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800623 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624
625 nl_recvmsgs(handle, w->nl_cb);
626}
627
628
629static int process_beacon_event(struct nl_msg *msg, void *arg)
630{
631 struct nl80211_wiphy_data *w = arg;
632 struct wpa_driver_nl80211_data *drv;
633 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
634 struct nlattr *tb[NL80211_ATTR_MAX + 1];
635 union wpa_event_data event;
636
637 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
638 genlmsg_attrlen(gnlh, 0), NULL);
639
640 if (gnlh->cmd != NL80211_CMD_FRAME) {
641 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
642 gnlh->cmd);
643 return NL_SKIP;
644 }
645
646 if (!tb[NL80211_ATTR_FRAME])
647 return NL_SKIP;
648
649 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
650 wiphy_list) {
651 os_memset(&event, 0, sizeof(event));
652 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
653 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
654 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
655 }
656
657 return NL_SKIP;
658}
659
660
661static struct nl80211_wiphy_data *
662nl80211_get_wiphy_data_ap(struct i802_bss *bss)
663{
664 static DEFINE_DL_LIST(nl80211_wiphys);
665 struct nl80211_wiphy_data *w;
666 int wiphy_idx, found = 0;
667 struct i802_bss *tmp_bss;
668
669 if (bss->wiphy_data != NULL)
670 return bss->wiphy_data;
671
672 wiphy_idx = nl80211_get_wiphy_index(bss);
673
674 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
675 if (w->wiphy_idx == wiphy_idx)
676 goto add;
677 }
678
679 /* alloc new one */
680 w = os_zalloc(sizeof(*w));
681 if (w == NULL)
682 return NULL;
683 w->wiphy_idx = wiphy_idx;
684 dl_list_init(&w->bsss);
685 dl_list_init(&w->drvs);
686
687 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
688 if (!w->nl_cb) {
689 os_free(w);
690 return NULL;
691 }
692 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
693 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
694 w);
695
696 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
697 "wiphy beacons");
698 if (w->nl_beacons == NULL) {
699 os_free(w);
700 return NULL;
701 }
702
703 if (nl80211_register_beacons(bss->drv, w)) {
704 nl_destroy_handles(&w->nl_beacons);
705 os_free(w);
706 return NULL;
707 }
708
709 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
710 nl80211_recv_beacons, w, w->nl_beacons);
711
712 dl_list_add(&nl80211_wiphys, &w->list);
713
714add:
715 /* drv entry for this bss already there? */
716 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
717 if (tmp_bss->drv == bss->drv) {
718 found = 1;
719 break;
720 }
721 }
722 /* if not add it */
723 if (!found)
724 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
725
726 dl_list_add(&w->bsss, &bss->wiphy_list);
727 bss->wiphy_data = w;
728 return w;
729}
730
731
732static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
733{
734 struct nl80211_wiphy_data *w = bss->wiphy_data;
735 struct i802_bss *tmp_bss;
736 int found = 0;
737
738 if (w == NULL)
739 return;
740 bss->wiphy_data = NULL;
741 dl_list_del(&bss->wiphy_list);
742
743 /* still any for this drv present? */
744 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
745 if (tmp_bss->drv == bss->drv) {
746 found = 1;
747 break;
748 }
749 }
750 /* if not remove it */
751 if (!found)
752 dl_list_del(&bss->drv->wiphy_list);
753
754 if (!dl_list_empty(&w->bsss))
755 return;
756
757 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
758
759 nl_cb_put(w->nl_cb);
760 nl_destroy_handles(&w->nl_beacons);
761 dl_list_del(&w->list);
762 os_free(w);
763}
764
765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
767{
768 struct i802_bss *bss = priv;
769 struct wpa_driver_nl80211_data *drv = bss->drv;
770 if (!drv->associated)
771 return -1;
772 os_memcpy(bssid, drv->bssid, ETH_ALEN);
773 return 0;
774}
775
776
777static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
778{
779 struct i802_bss *bss = priv;
780 struct wpa_driver_nl80211_data *drv = bss->drv;
781 if (!drv->associated)
782 return -1;
783 os_memcpy(ssid, drv->ssid, drv->ssid_len);
784 return drv->ssid_len;
785}
786
787
788static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
789 char *buf, size_t len, int del)
790{
791 union wpa_event_data event;
792
793 os_memset(&event, 0, sizeof(event));
794 if (len > sizeof(event.interface_status.ifname))
795 len = sizeof(event.interface_status.ifname) - 1;
796 os_memcpy(event.interface_status.ifname, buf, len);
797 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
798 EVENT_INTERFACE_ADDED;
799
800 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
801 del ? "DEL" : "NEW",
802 event.interface_status.ifname,
803 del ? "removed" : "added");
804
805 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700806 if (del) {
807 if (drv->if_removed) {
808 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
809 "already set - ignore event");
810 return;
811 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 drv->if_removed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813 } else {
814 if (if_nametoindex(drv->first_bss.ifname) == 0) {
815 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
816 "does not exist - ignore "
817 "RTM_NEWLINK",
818 drv->first_bss.ifname);
819 return;
820 }
821 if (!drv->if_removed) {
822 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
823 "already cleared - ignore event");
824 return;
825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 drv->if_removed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 }
829
830 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
831}
832
833
834static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
835 u8 *buf, size_t len)
836{
837 int attrlen, rta_len;
838 struct rtattr *attr;
839
840 attrlen = len;
841 attr = (struct rtattr *) buf;
842
843 rta_len = RTA_ALIGN(sizeof(struct rtattr));
844 while (RTA_OK(attr, attrlen)) {
845 if (attr->rta_type == IFLA_IFNAME) {
846 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
847 == 0)
848 return 1;
849 else
850 break;
851 }
852 attr = RTA_NEXT(attr, attrlen);
853 }
854
855 return 0;
856}
857
858
859static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
860 int ifindex, u8 *buf, size_t len)
861{
862 if (drv->ifindex == ifindex)
863 return 1;
864
865 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
866 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
867 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
868 "interface");
869 wpa_driver_nl80211_finish_drv_init(drv);
870 return 1;
871 }
872
873 return 0;
874}
875
876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877static struct wpa_driver_nl80211_data *
878nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
879{
880 struct wpa_driver_nl80211_data *drv;
881 dl_list_for_each(drv, &global->interfaces,
882 struct wpa_driver_nl80211_data, list) {
883 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
884 have_ifidx(drv, idx))
885 return drv;
886 }
887 return NULL;
888}
889
890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
892 struct ifinfomsg *ifi,
893 u8 *buf, size_t len)
894{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895 struct nl80211_global *global = ctx;
896 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 int attrlen, rta_len;
898 struct rtattr *attr;
899 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800900 char namebuf[IFNAMSIZ];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
903 if (!drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
905 "ifindex %d", ifi->ifi_index);
906 return;
907 }
908
909 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
910 "(%s%s%s%s)",
911 drv->operstate, ifi->ifi_flags,
912 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
913 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
914 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
915 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
916
917 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800918 if (if_indextoname(ifi->ifi_index, namebuf) &&
919 linux_iface_up(drv->global->ioctl_sock,
920 drv->first_bss.ifname) > 0) {
921 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
922 "event since interface %s is up", namebuf);
923 return;
924 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800926 if (drv->ignore_if_down_event) {
927 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
928 "event generated by mode change");
929 drv->ignore_if_down_event = 0;
930 } else {
931 drv->if_disabled = 1;
932 wpa_supplicant_event(drv->ctx,
933 EVENT_INTERFACE_DISABLED, NULL);
934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 }
936
937 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800938 if (if_indextoname(ifi->ifi_index, namebuf) &&
939 linux_iface_up(drv->global->ioctl_sock,
940 drv->first_bss.ifname) == 0) {
941 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
942 "event since interface %s is down",
943 namebuf);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700944 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
945 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
946 "event since interface %s does not exist",
947 drv->first_bss.ifname);
948 } else if (drv->if_removed) {
949 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
950 "event since interface %s is marked "
951 "removed", drv->first_bss.ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800952 } else {
953 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
954 drv->if_disabled = 0;
955 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
956 NULL);
957 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 }
959
960 /*
961 * Some drivers send the association event before the operup event--in
962 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
963 * fails. This will hit us when wpa_supplicant does not need to do
964 * IEEE 802.1X authentication
965 */
966 if (drv->operstate == 1 &&
967 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
968 !(ifi->ifi_flags & IFF_RUNNING))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800969 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 -1, IF_OPER_UP);
971
972 attrlen = len;
973 attr = (struct rtattr *) buf;
974 rta_len = RTA_ALIGN(sizeof(struct rtattr));
975 while (RTA_OK(attr, attrlen)) {
976 if (attr->rta_type == IFLA_IFNAME) {
977 wpa_driver_nl80211_event_link(
978 drv,
979 ((char *) attr) + rta_len,
980 attr->rta_len - rta_len, 0);
981 } else if (attr->rta_type == IFLA_MASTER)
982 brid = nla_get_u32((struct nlattr *) attr);
983 attr = RTA_NEXT(attr, attrlen);
984 }
985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 if (ifi->ifi_family == AF_BRIDGE && brid) {
987 /* device has been added to bridge */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 if_indextoname(brid, namebuf);
989 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
990 brid, namebuf);
991 add_ifidx(drv, brid);
992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993}
994
995
996static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
997 struct ifinfomsg *ifi,
998 u8 *buf, size_t len)
999{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 struct nl80211_global *global = ctx;
1001 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 int attrlen, rta_len;
1003 struct rtattr *attr;
1004 u32 brid = 0;
1005
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001006 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1007 if (!drv) {
1008 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1009 "foreign ifindex %d", ifi->ifi_index);
1010 return;
1011 }
1012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 attrlen = len;
1014 attr = (struct rtattr *) buf;
1015
1016 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1017 while (RTA_OK(attr, attrlen)) {
1018 if (attr->rta_type == IFLA_IFNAME) {
1019 wpa_driver_nl80211_event_link(
1020 drv,
1021 ((char *) attr) + rta_len,
1022 attr->rta_len - rta_len, 1);
1023 } else if (attr->rta_type == IFLA_MASTER)
1024 brid = nla_get_u32((struct nlattr *) attr);
1025 attr = RTA_NEXT(attr, attrlen);
1026 }
1027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 if (ifi->ifi_family == AF_BRIDGE && brid) {
1029 /* device has been removed from bridge */
1030 char namebuf[IFNAMSIZ];
1031 if_indextoname(brid, namebuf);
1032 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1033 "%s", brid, namebuf);
1034 del_ifidx(drv, brid);
1035 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036}
1037
1038
1039static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1040 const u8 *frame, size_t len)
1041{
1042 const struct ieee80211_mgmt *mgmt;
1043 union wpa_event_data event;
1044
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001045 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 mgmt = (const struct ieee80211_mgmt *) frame;
1047 if (len < 24 + sizeof(mgmt->u.auth)) {
1048 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1049 "frame");
1050 return;
1051 }
1052
1053 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1054 os_memset(&event, 0, sizeof(event));
1055 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1056 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001057 event.auth.auth_transaction =
1058 le_to_host16(mgmt->u.auth.auth_transaction);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1060 if (len > 24 + sizeof(mgmt->u.auth)) {
1061 event.auth.ies = mgmt->u.auth.variable;
1062 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1063 }
1064
1065 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1066}
1067
1068
Jouni Malinen87fd2792011-05-16 18:35:42 +03001069static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1070{
1071 struct nl_msg *msg;
1072 int ret;
1073 struct nl80211_bss_info_arg arg;
1074
1075 os_memset(&arg, 0, sizeof(arg));
1076 msg = nlmsg_alloc();
1077 if (!msg)
1078 goto nla_put_failure;
1079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001081 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1082
1083 arg.drv = drv;
1084 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1085 msg = NULL;
1086 if (ret == 0) {
1087 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1088 "associated BSS from scan results: %u MHz",
1089 arg.assoc_freq);
1090 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1091 }
1092 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1093 "(%s)", ret, strerror(-ret));
1094nla_put_failure:
1095 nlmsg_free(msg);
1096 return drv->assoc_freq;
1097}
1098
1099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1101 const u8 *frame, size_t len)
1102{
1103 const struct ieee80211_mgmt *mgmt;
1104 union wpa_event_data event;
1105 u16 status;
1106
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001107 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 mgmt = (const struct ieee80211_mgmt *) frame;
1109 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1110 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1111 "frame");
1112 return;
1113 }
1114
1115 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1116 if (status != WLAN_STATUS_SUCCESS) {
1117 os_memset(&event, 0, sizeof(event));
1118 event.assoc_reject.bssid = mgmt->bssid;
1119 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1120 event.assoc_reject.resp_ies =
1121 (u8 *) mgmt->u.assoc_resp.variable;
1122 event.assoc_reject.resp_ies_len =
1123 len - 24 - sizeof(mgmt->u.assoc_resp);
1124 }
1125 event.assoc_reject.status_code = status;
1126
1127 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1128 return;
1129 }
1130
1131 drv->associated = 1;
1132 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1133
1134 os_memset(&event, 0, sizeof(event));
1135 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1136 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1137 event.assoc_info.resp_ies_len =
1138 len - 24 - sizeof(mgmt->u.assoc_resp);
1139 }
1140
1141 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1144}
1145
1146
1147static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1148 enum nl80211_commands cmd, struct nlattr *status,
1149 struct nlattr *addr, struct nlattr *req_ie,
1150 struct nlattr *resp_ie)
1151{
1152 union wpa_event_data event;
1153
1154 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1155 /*
1156 * Avoid reporting two association events that would confuse
1157 * the core code.
1158 */
1159 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1160 "when using userspace SME", cmd);
1161 return;
1162 }
1163
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001164 if (cmd == NL80211_CMD_CONNECT)
1165 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1166 else if (cmd == NL80211_CMD_ROAM)
1167 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 os_memset(&event, 0, sizeof(event));
1170 if (cmd == NL80211_CMD_CONNECT &&
1171 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1172 if (addr)
1173 event.assoc_reject.bssid = nla_data(addr);
1174 if (resp_ie) {
1175 event.assoc_reject.resp_ies = nla_data(resp_ie);
1176 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1177 }
1178 event.assoc_reject.status_code = nla_get_u16(status);
1179 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1180 return;
1181 }
1182
1183 drv->associated = 1;
1184 if (addr)
1185 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1186
1187 if (req_ie) {
1188 event.assoc_info.req_ies = nla_data(req_ie);
1189 event.assoc_info.req_ies_len = nla_len(req_ie);
1190 }
1191 if (resp_ie) {
1192 event.assoc_info.resp_ies = nla_data(resp_ie);
1193 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1194 }
1195
Jouni Malinen87fd2792011-05-16 18:35:42 +03001196 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1199}
1200
1201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001202static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001203 struct nlattr *reason, struct nlattr *addr,
1204 struct nlattr *by_ap)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001205{
1206 union wpa_event_data data;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001207 unsigned int locally_generated = by_ap == NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001208
1209 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1210 /*
1211 * Avoid reporting two disassociation events that could
1212 * confuse the core code.
1213 */
1214 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1215 "event when using userspace SME");
1216 return;
1217 }
1218
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001219 if (drv->ignore_next_local_disconnect) {
1220 drv->ignore_next_local_disconnect = 0;
1221 if (locally_generated) {
1222 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1223 "event triggered during reassociation");
1224 return;
1225 }
1226 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1227 "disconnect but got another disconnect "
1228 "event first");
1229 }
1230
1231 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 drv->associated = 0;
1233 os_memset(&data, 0, sizeof(data));
1234 if (reason)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001235 data.deauth_info.reason_code = nla_get_u16(reason);
1236 data.deauth_info.locally_generated = by_ap == NULL;
1237 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1238}
1239
1240
1241static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1242 struct nlattr *freq, struct nlattr *type)
1243{
1244 union wpa_event_data data;
1245 int ht_enabled = 1;
1246 int chan_offset = 0;
1247
1248 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1249
1250 if (!freq || !type)
1251 return;
1252
1253 switch (nla_get_u32(type)) {
1254 case NL80211_CHAN_NO_HT:
1255 ht_enabled = 0;
1256 break;
1257 case NL80211_CHAN_HT20:
1258 break;
1259 case NL80211_CHAN_HT40PLUS:
1260 chan_offset = 1;
1261 break;
1262 case NL80211_CHAN_HT40MINUS:
1263 chan_offset = -1;
1264 break;
1265 }
1266
1267 data.ch_switch.freq = nla_get_u32(freq);
1268 data.ch_switch.ht_enabled = ht_enabled;
1269 data.ch_switch.ch_offset = chan_offset;
1270
1271 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272}
1273
1274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1276 enum nl80211_commands cmd, struct nlattr *addr)
1277{
1278 union wpa_event_data event;
1279 enum wpa_event_type ev;
1280
1281 if (nla_len(addr) != ETH_ALEN)
1282 return;
1283
1284 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1285 cmd, MAC2STR((u8 *) nla_data(addr)));
1286
1287 if (cmd == NL80211_CMD_AUTHENTICATE)
1288 ev = EVENT_AUTH_TIMED_OUT;
1289 else if (cmd == NL80211_CMD_ASSOCIATE)
1290 ev = EVENT_ASSOC_TIMED_OUT;
1291 else
1292 return;
1293
1294 os_memset(&event, 0, sizeof(event));
1295 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1296 wpa_supplicant_event(drv->ctx, ev, &event);
1297}
1298
1299
1300static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001301 struct nlattr *freq, struct nlattr *sig,
1302 const u8 *frame, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303{
1304 const struct ieee80211_mgmt *mgmt;
1305 union wpa_event_data event;
1306 u16 fc, stype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001307 int ssi_signal = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001309 wpa_printf(MSG_DEBUG, "nl80211: Frame event");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 mgmt = (const struct ieee80211_mgmt *) frame;
1311 if (len < 24) {
1312 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1313 return;
1314 }
1315
1316 fc = le_to_host16(mgmt->frame_control);
1317 stype = WLAN_FC_GET_STYPE(fc);
1318
Dmitry Shmidt04949592012-07-19 12:16:46 -07001319 if (sig)
1320 ssi_signal = (s32) nla_get_u32(sig);
1321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 os_memset(&event, 0, sizeof(event));
1323 if (freq) {
1324 event.rx_action.freq = nla_get_u32(freq);
1325 drv->last_mgmt_freq = event.rx_action.freq;
1326 }
1327 if (stype == WLAN_FC_STYPE_ACTION) {
1328 event.rx_action.da = mgmt->da;
1329 event.rx_action.sa = mgmt->sa;
1330 event.rx_action.bssid = mgmt->bssid;
1331 event.rx_action.category = mgmt->u.action.category;
1332 event.rx_action.data = &mgmt->u.action.category + 1;
1333 event.rx_action.len = frame + len - event.rx_action.data;
1334 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1335 } else {
1336 event.rx_mgmt.frame = frame;
1337 event.rx_mgmt.frame_len = len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001338 event.rx_mgmt.ssi_signal = ssi_signal;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1340 }
1341}
1342
1343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001344static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1345 struct nlattr *cookie, const u8 *frame,
1346 size_t len, struct nlattr *ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347{
1348 union wpa_event_data event;
1349 const struct ieee80211_hdr *hdr;
1350 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001352 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001353 if (!is_ap_interface(drv->nlmode)) {
1354 u64 cookie_val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001356 if (!cookie)
1357 return;
1358
1359 cookie_val = nla_get_u64(cookie);
1360 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1361 " cookie=0%llx%s (ack=%d)",
1362 (long long unsigned int) cookie_val,
1363 cookie_val == drv->send_action_cookie ?
1364 " (match)" : " (unknown)", ack != NULL);
1365 if (cookie_val != drv->send_action_cookie)
1366 return;
1367 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368
1369 hdr = (const struct ieee80211_hdr *) frame;
1370 fc = le_to_host16(hdr->frame_control);
1371
1372 os_memset(&event, 0, sizeof(event));
1373 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1374 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1375 event.tx_status.dst = hdr->addr1;
1376 event.tx_status.data = frame;
1377 event.tx_status.data_len = len;
1378 event.tx_status.ack = ack != NULL;
1379 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1380}
1381
1382
1383static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1384 enum wpa_event_type type,
1385 const u8 *frame, size_t len)
1386{
1387 const struct ieee80211_mgmt *mgmt;
1388 union wpa_event_data event;
1389 const u8 *bssid = NULL;
1390 u16 reason_code = 0;
1391
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001392 if (type == EVENT_DEAUTH)
1393 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1394 else
1395 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 mgmt = (const struct ieee80211_mgmt *) frame;
1398 if (len >= 24) {
1399 bssid = mgmt->bssid;
1400
1401 if (drv->associated != 0 &&
1402 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1403 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1404 /*
1405 * We have presumably received this deauth as a
1406 * response to a clear_state_mismatch() outgoing
1407 * deauth. Don't let it take us offline!
1408 */
1409 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1410 "from Unknown BSSID " MACSTR " -- ignoring",
1411 MAC2STR(bssid));
1412 return;
1413 }
1414 }
1415
1416 drv->associated = 0;
1417 os_memset(&event, 0, sizeof(event));
1418
1419 /* Note: Same offset for Reason Code in both frame subtypes */
1420 if (len >= 24 + sizeof(mgmt->u.deauth))
1421 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1422
1423 if (type == EVENT_DISASSOC) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001424 event.disassoc_info.locally_generated =
1425 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 event.disassoc_info.addr = bssid;
1427 event.disassoc_info.reason_code = reason_code;
1428 if (frame + len > mgmt->u.disassoc.variable) {
1429 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1430 event.disassoc_info.ie_len = frame + len -
1431 mgmt->u.disassoc.variable;
1432 }
1433 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001434 event.deauth_info.locally_generated =
1435 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436 event.deauth_info.addr = bssid;
1437 event.deauth_info.reason_code = reason_code;
1438 if (frame + len > mgmt->u.deauth.variable) {
1439 event.deauth_info.ie = mgmt->u.deauth.variable;
1440 event.deauth_info.ie_len = frame + len -
1441 mgmt->u.deauth.variable;
1442 }
1443 }
1444
1445 wpa_supplicant_event(drv->ctx, type, &event);
1446}
1447
1448
1449static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1450 enum wpa_event_type type,
1451 const u8 *frame, size_t len)
1452{
1453 const struct ieee80211_mgmt *mgmt;
1454 union wpa_event_data event;
1455 u16 reason_code = 0;
1456
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001457 if (type == EVENT_UNPROT_DEAUTH)
1458 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1459 else
1460 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 if (len < 24)
1463 return;
1464
1465 mgmt = (const struct ieee80211_mgmt *) frame;
1466
1467 os_memset(&event, 0, sizeof(event));
1468 /* Note: Same offset for Reason Code in both frame subtypes */
1469 if (len >= 24 + sizeof(mgmt->u.deauth))
1470 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1471
1472 if (type == EVENT_UNPROT_DISASSOC) {
1473 event.unprot_disassoc.sa = mgmt->sa;
1474 event.unprot_disassoc.da = mgmt->da;
1475 event.unprot_disassoc.reason_code = reason_code;
1476 } else {
1477 event.unprot_deauth.sa = mgmt->sa;
1478 event.unprot_deauth.da = mgmt->da;
1479 event.unprot_deauth.reason_code = reason_code;
1480 }
1481
1482 wpa_supplicant_event(drv->ctx, type, &event);
1483}
1484
1485
1486static void mlme_event(struct wpa_driver_nl80211_data *drv,
1487 enum nl80211_commands cmd, struct nlattr *frame,
1488 struct nlattr *addr, struct nlattr *timed_out,
1489 struct nlattr *freq, struct nlattr *ack,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001490 struct nlattr *cookie, struct nlattr *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491{
1492 if (timed_out && addr) {
1493 mlme_timeout_event(drv, cmd, addr);
1494 return;
1495 }
1496
1497 if (frame == NULL) {
1498 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1499 "data", cmd);
1500 return;
1501 }
1502
1503 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1504 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1505 nla_data(frame), nla_len(frame));
1506
1507 switch (cmd) {
1508 case NL80211_CMD_AUTHENTICATE:
1509 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1510 break;
1511 case NL80211_CMD_ASSOCIATE:
1512 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1513 break;
1514 case NL80211_CMD_DEAUTHENTICATE:
1515 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1516 nla_data(frame), nla_len(frame));
1517 break;
1518 case NL80211_CMD_DISASSOCIATE:
1519 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1520 nla_data(frame), nla_len(frame));
1521 break;
1522 case NL80211_CMD_FRAME:
Dmitry Shmidt04949592012-07-19 12:16:46 -07001523 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1524 nla_len(frame));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525 break;
1526 case NL80211_CMD_FRAME_TX_STATUS:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001527 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1528 nla_len(frame), ack);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 break;
1530 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1531 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1532 nla_data(frame), nla_len(frame));
1533 break;
1534 case NL80211_CMD_UNPROT_DISASSOCIATE:
1535 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1536 nla_data(frame), nla_len(frame));
1537 break;
1538 default:
1539 break;
1540 }
1541}
1542
1543
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001544static void mlme_event_michael_mic_failure(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 struct nlattr *tb[])
1546{
1547 union wpa_event_data data;
1548
1549 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1550 os_memset(&data, 0, sizeof(data));
1551 if (tb[NL80211_ATTR_MAC]) {
1552 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1553 nla_data(tb[NL80211_ATTR_MAC]),
1554 nla_len(tb[NL80211_ATTR_MAC]));
1555 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1556 }
1557 if (tb[NL80211_ATTR_KEY_SEQ]) {
1558 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1559 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1560 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1561 }
1562 if (tb[NL80211_ATTR_KEY_TYPE]) {
1563 enum nl80211_key_type key_type =
1564 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1565 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1566 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1567 data.michael_mic_failure.unicast = 1;
1568 } else
1569 data.michael_mic_failure.unicast = 1;
1570
1571 if (tb[NL80211_ATTR_KEY_IDX]) {
1572 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1573 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1574 }
1575
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001576 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577}
1578
1579
1580static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1581 struct nlattr *tb[])
1582{
1583 if (tb[NL80211_ATTR_MAC] == NULL) {
1584 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1585 "event");
1586 return;
1587 }
1588 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1589 drv->associated = 1;
1590 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1591 MAC2STR(drv->bssid));
1592
1593 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1594}
1595
1596
1597static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1598 int cancel_event, struct nlattr *tb[])
1599{
1600 unsigned int freq, chan_type, duration;
1601 union wpa_event_data data;
1602 u64 cookie;
1603
1604 if (tb[NL80211_ATTR_WIPHY_FREQ])
1605 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1606 else
1607 freq = 0;
1608
1609 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1610 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1611 else
1612 chan_type = 0;
1613
1614 if (tb[NL80211_ATTR_DURATION])
1615 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1616 else
1617 duration = 0;
1618
1619 if (tb[NL80211_ATTR_COOKIE])
1620 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1621 else
1622 cookie = 0;
1623
1624 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1625 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1626 cancel_event, freq, chan_type, duration,
1627 (long long unsigned int) cookie,
1628 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1629
1630 if (cookie != drv->remain_on_chan_cookie)
1631 return; /* not for us */
1632
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001633 if (cancel_event)
1634 drv->pending_remain_on_chan = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635
1636 os_memset(&data, 0, sizeof(data));
1637 data.remain_on_channel.freq = freq;
1638 data.remain_on_channel.duration = duration;
1639 wpa_supplicant_event(drv->ctx, cancel_event ?
1640 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1641 EVENT_REMAIN_ON_CHANNEL, &data);
1642}
1643
1644
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001645static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1646 struct nlattr *tb[])
1647{
1648 union wpa_event_data data;
1649
1650 os_memset(&data, 0, sizeof(data));
1651
1652 if (tb[NL80211_ATTR_IE]) {
1653 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1654 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1655 }
1656
1657 if (tb[NL80211_ATTR_IE_RIC]) {
1658 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1659 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1660 }
1661
1662 if (tb[NL80211_ATTR_MAC])
1663 os_memcpy(data.ft_ies.target_ap,
1664 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1665
1666 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1667 MAC2STR(data.ft_ies.target_ap));
1668
1669 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1670}
1671
1672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1674 struct nlattr *tb[])
1675{
1676 union wpa_event_data event;
1677 struct nlattr *nl;
1678 int rem;
1679 struct scan_info *info;
1680#define MAX_REPORT_FREQS 50
1681 int freqs[MAX_REPORT_FREQS];
1682 int num_freqs = 0;
1683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684 if (drv->scan_for_auth) {
1685 drv->scan_for_auth = 0;
1686 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1687 "cfg80211 BSS entry");
1688 wpa_driver_nl80211_authenticate_retry(drv);
1689 return;
1690 }
1691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 os_memset(&event, 0, sizeof(event));
1693 info = &event.scan_info;
1694 info->aborted = aborted;
1695
1696 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1697 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1698 struct wpa_driver_scan_ssid *s =
1699 &info->ssids[info->num_ssids];
1700 s->ssid = nla_data(nl);
1701 s->ssid_len = nla_len(nl);
1702 info->num_ssids++;
1703 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1704 break;
1705 }
1706 }
1707 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1708 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1709 {
1710 freqs[num_freqs] = nla_get_u32(nl);
1711 num_freqs++;
1712 if (num_freqs == MAX_REPORT_FREQS - 1)
1713 break;
1714 }
1715 info->freqs = freqs;
1716 info->num_freqs = num_freqs;
1717 }
1718 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1719}
1720
1721
1722static int get_link_signal(struct nl_msg *msg, void *arg)
1723{
1724 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1725 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1726 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1727 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1728 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1729 };
1730 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1731 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1732 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1733 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1734 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1735 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1736 };
1737 struct wpa_signal_info *sig_change = arg;
1738
1739 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1740 genlmsg_attrlen(gnlh, 0), NULL);
1741 if (!tb[NL80211_ATTR_STA_INFO] ||
1742 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1743 tb[NL80211_ATTR_STA_INFO], policy))
1744 return NL_SKIP;
1745 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1746 return NL_SKIP;
1747
1748 sig_change->current_signal =
1749 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1750
1751 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1752 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1753 sinfo[NL80211_STA_INFO_TX_BITRATE],
1754 rate_policy)) {
1755 sig_change->current_txrate = 0;
1756 } else {
1757 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1758 sig_change->current_txrate =
1759 nla_get_u16(rinfo[
1760 NL80211_RATE_INFO_BITRATE]) * 100;
1761 }
1762 }
1763 }
1764
1765 return NL_SKIP;
1766}
1767
1768
1769static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1770 struct wpa_signal_info *sig)
1771{
1772 struct nl_msg *msg;
1773
1774 sig->current_signal = -9999;
1775 sig->current_txrate = 0;
1776
1777 msg = nlmsg_alloc();
1778 if (!msg)
1779 return -ENOMEM;
1780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001781 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782
1783 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1784 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1785
1786 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1787 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001788 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 return -ENOBUFS;
1790}
1791
1792
1793static int get_link_noise(struct nl_msg *msg, void *arg)
1794{
1795 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1796 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1797 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1798 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1799 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1800 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1801 };
1802 struct wpa_signal_info *sig_change = arg;
1803
1804 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1805 genlmsg_attrlen(gnlh, 0), NULL);
1806
1807 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1808 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1809 return NL_SKIP;
1810 }
1811
1812 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1813 tb[NL80211_ATTR_SURVEY_INFO],
1814 survey_policy)) {
1815 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1816 "attributes!");
1817 return NL_SKIP;
1818 }
1819
1820 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1821 return NL_SKIP;
1822
1823 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1824 sig_change->frequency)
1825 return NL_SKIP;
1826
1827 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1828 return NL_SKIP;
1829
1830 sig_change->current_noise =
1831 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1832
1833 return NL_SKIP;
1834}
1835
1836
1837static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1838 struct wpa_signal_info *sig_change)
1839{
1840 struct nl_msg *msg;
1841
1842 sig_change->current_noise = 9999;
1843 sig_change->frequency = drv->assoc_freq;
1844
1845 msg = nlmsg_alloc();
1846 if (!msg)
1847 return -ENOMEM;
1848
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001849 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850
1851 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1852
1853 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1854 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001855 nlmsg_free(msg);
1856 return -ENOBUFS;
1857}
1858
1859
1860static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1861{
1862 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1863 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1864 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1865 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1866 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1867 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1868 };
1869 struct wpa_scan_results *scan_results = arg;
1870 struct wpa_scan_res *scan_res;
1871 size_t i;
1872
1873 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1874 genlmsg_attrlen(gnlh, 0), NULL);
1875
1876 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1877 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1878 return NL_SKIP;
1879 }
1880
1881 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1882 tb[NL80211_ATTR_SURVEY_INFO],
1883 survey_policy)) {
1884 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1885 "attributes");
1886 return NL_SKIP;
1887 }
1888
1889 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1890 return NL_SKIP;
1891
1892 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1893 return NL_SKIP;
1894
1895 for (i = 0; i < scan_results->num; ++i) {
1896 scan_res = scan_results->res[i];
1897 if (!scan_res)
1898 continue;
1899 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1900 scan_res->freq)
1901 continue;
1902 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1903 continue;
1904 scan_res->noise = (s8)
1905 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1906 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1907 }
1908
1909 return NL_SKIP;
1910}
1911
1912
1913static int nl80211_get_noise_for_scan_results(
1914 struct wpa_driver_nl80211_data *drv,
1915 struct wpa_scan_results *scan_res)
1916{
1917 struct nl_msg *msg;
1918
1919 msg = nlmsg_alloc();
1920 if (!msg)
1921 return -ENOMEM;
1922
1923 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1924
1925 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1926
1927 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1928 scan_res);
1929 nla_put_failure:
1930 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 return -ENOBUFS;
1932}
1933
1934
1935static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1936 struct nlattr *tb[])
1937{
1938 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1939 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1940 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1941 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1942 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1943 };
1944 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1945 enum nl80211_cqm_rssi_threshold_event event;
1946 union wpa_event_data ed;
1947 struct wpa_signal_info sig;
1948 int res;
1949
1950 if (tb[NL80211_ATTR_CQM] == NULL ||
1951 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1952 cqm_policy)) {
1953 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1954 return;
1955 }
1956
1957 os_memset(&ed, 0, sizeof(ed));
1958
1959 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1960 if (!tb[NL80211_ATTR_MAC])
1961 return;
1962 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1963 ETH_ALEN);
1964 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1965 return;
1966 }
1967
1968 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1969 return;
1970 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1971
1972 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1973 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1974 "event: RSSI high");
1975 ed.signal_change.above_threshold = 1;
1976 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1977 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1978 "event: RSSI low");
1979 ed.signal_change.above_threshold = 0;
1980 } else
1981 return;
1982
1983 res = nl80211_get_link_signal(drv, &sig);
1984 if (res == 0) {
1985 ed.signal_change.current_signal = sig.current_signal;
1986 ed.signal_change.current_txrate = sig.current_txrate;
1987 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1988 sig.current_signal, sig.current_txrate);
1989 }
1990
1991 res = nl80211_get_link_noise(drv, &sig);
1992 if (res == 0) {
1993 ed.signal_change.current_noise = sig.current_noise;
1994 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1995 sig.current_noise);
1996 }
1997
1998 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1999}
2000
2001
2002static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2003 struct nlattr **tb)
2004{
2005 u8 *addr;
2006 union wpa_event_data data;
2007
2008 if (tb[NL80211_ATTR_MAC] == NULL)
2009 return;
2010 addr = nla_data(tb[NL80211_ATTR_MAC]);
2011 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002012
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002013 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002014 u8 *ies = NULL;
2015 size_t ies_len = 0;
2016 if (tb[NL80211_ATTR_IE]) {
2017 ies = nla_data(tb[NL80211_ATTR_IE]);
2018 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2019 }
2020 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2021 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2022 return;
2023 }
2024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2026 return;
2027
2028 os_memset(&data, 0, sizeof(data));
2029 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2030 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2031}
2032
2033
2034static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2035 struct nlattr **tb)
2036{
2037 u8 *addr;
2038 union wpa_event_data data;
2039
2040 if (tb[NL80211_ATTR_MAC] == NULL)
2041 return;
2042 addr = nla_data(tb[NL80211_ATTR_MAC]);
2043 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2044 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002046 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002047 drv_event_disassoc(drv->ctx, addr);
2048 return;
2049 }
2050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2052 return;
2053
2054 os_memset(&data, 0, sizeof(data));
2055 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2056 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2057}
2058
2059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2061 struct nlattr **tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002063 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2064 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2065 [NL80211_REKEY_DATA_KEK] = {
2066 .minlen = NL80211_KEK_LEN,
2067 .maxlen = NL80211_KEK_LEN,
2068 },
2069 [NL80211_REKEY_DATA_KCK] = {
2070 .minlen = NL80211_KCK_LEN,
2071 .maxlen = NL80211_KCK_LEN,
2072 },
2073 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2074 .minlen = NL80211_REPLAY_CTR_LEN,
2075 .maxlen = NL80211_REPLAY_CTR_LEN,
2076 },
2077 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 union wpa_event_data data;
2079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002080 if (!tb[NL80211_ATTR_MAC])
2081 return;
2082 if (!tb[NL80211_ATTR_REKEY_DATA])
2083 return;
2084 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2085 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2086 return;
2087 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2088 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 os_memset(&data, 0, sizeof(data));
2091 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2092 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2093 MAC2STR(data.driver_gtk_rekey.bssid));
2094 data.driver_gtk_rekey.replay_ctr =
2095 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2096 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2097 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2098 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2099}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101
2102static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2103 struct nlattr **tb)
2104{
2105 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2106 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2107 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2108 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2109 .minlen = ETH_ALEN,
2110 .maxlen = ETH_ALEN,
2111 },
2112 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2113 };
2114 union wpa_event_data data;
2115
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002116 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2117
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2119 return;
2120 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2121 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2122 return;
2123 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2124 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2125 return;
2126
2127 os_memset(&data, 0, sizeof(data));
2128 os_memcpy(data.pmkid_candidate.bssid,
2129 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2130 data.pmkid_candidate.index =
2131 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2132 data.pmkid_candidate.preauth =
2133 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2134 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2135}
2136
2137
2138static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2139 struct nlattr **tb)
2140{
2141 union wpa_event_data data;
2142
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002143 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002145 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2146 return;
2147
2148 os_memset(&data, 0, sizeof(data));
2149 os_memcpy(data.client_poll.addr,
2150 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2151
2152 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2153}
2154
2155
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002156static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2157 struct nlattr **tb)
2158{
2159 union wpa_event_data data;
2160
2161 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2162
2163 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2164 return;
2165
2166 os_memset(&data, 0, sizeof(data));
2167 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2168 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2169 case NL80211_TDLS_SETUP:
2170 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2171 MACSTR, MAC2STR(data.tdls.peer));
2172 data.tdls.oper = TDLS_REQUEST_SETUP;
2173 break;
2174 case NL80211_TDLS_TEARDOWN:
2175 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2176 MACSTR, MAC2STR(data.tdls.peer));
2177 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2178 break;
2179 default:
2180 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2181 "event");
2182 return;
2183 }
2184 if (tb[NL80211_ATTR_REASON_CODE]) {
2185 data.tdls.reason_code =
2186 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2187 }
2188
2189 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2190}
2191
2192
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002193static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2194 struct nlattr **tb)
2195{
2196 union wpa_event_data data;
2197 u32 reason;
2198
2199 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2200
2201 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2202 return;
2203
2204 os_memset(&data, 0, sizeof(data));
2205 os_memcpy(data.connect_failed_reason.addr,
2206 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2207
2208 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2209 switch (reason) {
2210 case NL80211_CONN_FAIL_MAX_CLIENTS:
2211 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2212 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2213 break;
2214 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2215 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2216 " tried to connect",
2217 MAC2STR(data.connect_failed_reason.addr));
2218 data.connect_failed_reason.code = BLOCKED_CLIENT;
2219 break;
2220 default:
2221 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2222 "%u", reason);
2223 return;
2224 }
2225
2226 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2227}
2228
2229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002230static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2231 int wds)
2232{
2233 struct wpa_driver_nl80211_data *drv = bss->drv;
2234 union wpa_event_data event;
2235
2236 if (!tb[NL80211_ATTR_MAC])
2237 return;
2238
2239 os_memset(&event, 0, sizeof(event));
2240 event.rx_from_unknown.bssid = bss->addr;
2241 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2242 event.rx_from_unknown.wds = wds;
2243
2244 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2245}
2246
2247
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002248static void do_process_drv_event(struct i802_bss *bss, int cmd,
2249 struct nlattr **tb)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002251 struct wpa_driver_nl80211_data *drv = bss->drv;
2252
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002253 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2254 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2255 cmd == NL80211_CMD_SCAN_ABORTED)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002257 drv->ap_scan_as_station);
2258 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 }
2260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002261 switch (cmd) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 case NL80211_CMD_TRIGGER_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002263 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002265 case NL80211_CMD_START_SCHED_SCAN:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002266 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 break;
2268 case NL80211_CMD_SCHED_SCAN_STOPPED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002269 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2271 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 case NL80211_CMD_NEW_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002273 wpa_dbg(drv->ctx, MSG_DEBUG,
2274 "nl80211: New scan results available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 drv->scan_complete_events = 1;
2276 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2277 drv->ctx);
2278 send_scan_event(drv, 0, tb);
2279 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002280 case NL80211_CMD_SCHED_SCAN_RESULTS:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002281 wpa_dbg(drv->ctx, MSG_DEBUG,
2282 "nl80211: New sched scan results available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283 send_scan_event(drv, 0, tb);
2284 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 case NL80211_CMD_SCAN_ABORTED:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002286 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 /*
2288 * Need to indicate that scan results are available in order
2289 * not to make wpa_supplicant stop its scanning.
2290 */
2291 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2292 drv->ctx);
2293 send_scan_event(drv, 1, tb);
2294 break;
2295 case NL80211_CMD_AUTHENTICATE:
2296 case NL80211_CMD_ASSOCIATE:
2297 case NL80211_CMD_DEAUTHENTICATE:
2298 case NL80211_CMD_DISASSOCIATE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 case NL80211_CMD_FRAME_TX_STATUS:
2300 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2301 case NL80211_CMD_UNPROT_DISASSOCIATE:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002302 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2304 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002305 tb[NL80211_ATTR_COOKIE],
2306 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 break;
2308 case NL80211_CMD_CONNECT:
2309 case NL80211_CMD_ROAM:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002310 mlme_event_connect(drv, cmd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 tb[NL80211_ATTR_STATUS_CODE],
2312 tb[NL80211_ATTR_MAC],
2313 tb[NL80211_ATTR_REQ_IE],
2314 tb[NL80211_ATTR_RESP_IE]);
2315 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002316 case NL80211_CMD_CH_SWITCH_NOTIFY:
2317 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2318 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2319 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 case NL80211_CMD_DISCONNECT:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002321 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002322 tb[NL80211_ATTR_MAC],
2323 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 break;
2325 case NL80211_CMD_MICHAEL_MIC_FAILURE:
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002326 mlme_event_michael_mic_failure(bss, tb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 break;
2328 case NL80211_CMD_JOIN_IBSS:
2329 mlme_event_join_ibss(drv, tb);
2330 break;
2331 case NL80211_CMD_REMAIN_ON_CHANNEL:
2332 mlme_event_remain_on_channel(drv, 0, tb);
2333 break;
2334 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2335 mlme_event_remain_on_channel(drv, 1, tb);
2336 break;
2337 case NL80211_CMD_NOTIFY_CQM:
2338 nl80211_cqm_event(drv, tb);
2339 break;
2340 case NL80211_CMD_REG_CHANGE:
2341 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2342 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2343 NULL);
2344 break;
2345 case NL80211_CMD_REG_BEACON_HINT:
2346 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2347 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2348 NULL);
2349 break;
2350 case NL80211_CMD_NEW_STATION:
2351 nl80211_new_station_event(drv, tb);
2352 break;
2353 case NL80211_CMD_DEL_STATION:
2354 nl80211_del_station_event(drv, tb);
2355 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002356 case NL80211_CMD_SET_REKEY_OFFLOAD:
2357 nl80211_rekey_offload_event(drv, tb);
2358 break;
2359 case NL80211_CMD_PMKSA_CANDIDATE:
2360 nl80211_pmksa_candidate_event(drv, tb);
2361 break;
2362 case NL80211_CMD_PROBE_CLIENT:
2363 nl80211_client_probe_event(drv, tb);
2364 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002365 case NL80211_CMD_TDLS_OPER:
2366 nl80211_tdls_oper_event(drv, tb);
2367 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002368 case NL80211_CMD_CONN_FAILED:
2369 nl80211_connect_failed_event(drv, tb);
2370 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002371 case NL80211_CMD_FT_EVENT:
2372 mlme_event_ft_event(drv, tb);
2373 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 default:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002375 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2376 "(cmd=%d)", cmd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377 break;
2378 }
2379}
2380
2381
2382static int process_drv_event(struct nl_msg *msg, void *arg)
2383{
2384 struct wpa_driver_nl80211_data *drv = arg;
2385 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2386 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002387 struct i802_bss *bss;
2388 int ifidx = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002389
2390 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2391 genlmsg_attrlen(gnlh, 0), NULL);
2392
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002393 if (tb[NL80211_ATTR_IFINDEX])
2394 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2395
2396 for (bss = &drv->first_bss; bss; bss = bss->next) {
2397 if (ifidx == -1 || ifidx == bss->ifindex) {
2398 do_process_drv_event(bss, gnlh->cmd, tb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002399 return NL_SKIP;
2400 }
2401 }
2402
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002403 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d) for foreign "
2404 "interface (ifindex %d)", gnlh->cmd, ifidx);
2405
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002406 return NL_SKIP;
2407}
2408
2409
2410static int process_global_event(struct nl_msg *msg, void *arg)
2411{
2412 struct nl80211_global *global = arg;
2413 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2414 struct nlattr *tb[NL80211_ATTR_MAX + 1];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002415 struct wpa_driver_nl80211_data *drv, *tmp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002416 int ifidx = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002417 struct i802_bss *bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002418
2419 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2420 genlmsg_attrlen(gnlh, 0), NULL);
2421
2422 if (tb[NL80211_ATTR_IFINDEX])
2423 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2424
Dmitry Shmidt04949592012-07-19 12:16:46 -07002425 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2426 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002427 for (bss = &drv->first_bss; bss; bss = bss->next) {
2428 if (ifidx == -1 || ifidx == bss->ifindex) {
2429 do_process_drv_event(bss, gnlh->cmd, tb);
2430 return NL_SKIP;
2431 }
2432 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002433 }
2434
2435 return NL_SKIP;
2436}
2437
2438
2439static int process_bss_event(struct nl_msg *msg, void *arg)
2440{
2441 struct i802_bss *bss = arg;
2442 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2443 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2444
2445 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2446 genlmsg_attrlen(gnlh, 0), NULL);
2447
2448 switch (gnlh->cmd) {
2449 case NL80211_CMD_FRAME:
2450 case NL80211_CMD_FRAME_TX_STATUS:
2451 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2452 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2453 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
Dmitry Shmidt04949592012-07-19 12:16:46 -07002454 tb[NL80211_ATTR_COOKIE],
2455 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002456 break;
2457 case NL80211_CMD_UNEXPECTED_FRAME:
2458 nl80211_spurious_frame(bss, tb, 0);
2459 break;
2460 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2461 nl80211_spurious_frame(bss, tb, 1);
2462 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 default:
2464 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2465 "(cmd=%d)", gnlh->cmd);
2466 break;
2467 }
2468
2469 return NL_SKIP;
2470}
2471
2472
2473static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2474 void *handle)
2475{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002476 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477
2478 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
2479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 nl_recvmsgs(handle, cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481}
2482
2483
2484/**
2485 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2486 * @priv: driver_nl80211 private data
2487 * @alpha2_arg: country to which to switch to
2488 * Returns: 0 on success, -1 on failure
2489 *
2490 * This asks nl80211 to set the regulatory domain for given
2491 * country ISO / IEC alpha2.
2492 */
2493static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2494{
2495 struct i802_bss *bss = priv;
2496 struct wpa_driver_nl80211_data *drv = bss->drv;
2497 char alpha2[3];
2498 struct nl_msg *msg;
2499
2500 msg = nlmsg_alloc();
2501 if (!msg)
2502 return -ENOMEM;
2503
2504 alpha2[0] = alpha2_arg[0];
2505 alpha2[1] = alpha2_arg[1];
2506 alpha2[2] = '\0';
2507
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002508 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509
2510 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2511 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2512 return -EINVAL;
2513 return 0;
2514nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002515 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 return -EINVAL;
2517}
2518
2519
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002520static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2521{
2522 u32 *feat = arg;
2523 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2524 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2525
2526 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2527 genlmsg_attrlen(gnlh, 0), NULL);
2528
2529 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2530 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2531
2532 return NL_SKIP;
2533}
2534
2535
2536static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2537{
2538 u32 feat = 0;
2539 struct nl_msg *msg;
2540
2541 msg = nlmsg_alloc();
2542 if (!msg)
2543 goto nla_put_failure;
2544
2545 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2546 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2547 return feat;
2548
2549 msg = NULL;
2550nla_put_failure:
2551 nlmsg_free(msg);
2552 return 0;
2553}
2554
2555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556struct wiphy_info_data {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002557 struct wpa_driver_capa *capa;
2558
2559 unsigned int error:1;
2560 unsigned int device_ap_sme:1;
2561 unsigned int poll_command_supported:1;
2562 unsigned int data_tx_status:1;
2563 unsigned int monitor_supported:1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002564 unsigned int auth_supported:1;
2565 unsigned int connect_supported:1;
2566 unsigned int p2p_go_supported:1;
2567 unsigned int p2p_client_supported:1;
2568 unsigned int p2p_concurrent:1;
2569 unsigned int p2p_multichan_concurrent:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570};
2571
2572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002573static unsigned int probe_resp_offload_support(int supp_protocols)
2574{
2575 unsigned int prot = 0;
2576
2577 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2578 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2579 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2580 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2581 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2582 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2583 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2584 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2585
2586 return prot;
2587}
2588
2589
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002590static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
2591 struct nlattr *tb)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002593 struct nlattr *nl_mode;
2594 int i;
2595
2596 if (tb == NULL)
2597 return;
2598
2599 nla_for_each_nested(nl_mode, tb, i) {
2600 switch (nla_type(nl_mode)) {
2601 case NL80211_IFTYPE_AP:
2602 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
2603 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002604 case NL80211_IFTYPE_ADHOC:
2605 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
2606 break;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002607 case NL80211_IFTYPE_P2P_GO:
2608 info->p2p_go_supported = 1;
2609 break;
2610 case NL80211_IFTYPE_P2P_CLIENT:
2611 info->p2p_client_supported = 1;
2612 break;
2613 case NL80211_IFTYPE_MONITOR:
2614 info->monitor_supported = 1;
2615 break;
2616 }
2617 }
2618}
2619
2620
2621static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
2622 struct nlattr *nl_combi)
2623{
2624 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2625 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2626 struct nlattr *nl_limit, *nl_mode;
2627 int err, rem_limit, rem_mode;
2628 int combination_has_p2p = 0, combination_has_mgd = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002629 static struct nla_policy
2630 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2631 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2632 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2633 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2634 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2635 },
2636 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2637 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2638 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2639 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002641 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2642 nl_combi, iface_combination_policy);
2643 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2644 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2645 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2646 return 0; /* broken combination */
2647
2648 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
2649 rem_limit) {
2650 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
2651 nl_limit, iface_limit_policy);
2652 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2653 return 0; /* broken combination */
2654
2655 nla_for_each_nested(nl_mode,
2656 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2657 rem_mode) {
2658 int ift = nla_type(nl_mode);
2659 if (ift == NL80211_IFTYPE_P2P_GO ||
2660 ift == NL80211_IFTYPE_P2P_CLIENT)
2661 combination_has_p2p = 1;
2662 if (ift == NL80211_IFTYPE_STATION)
2663 combination_has_mgd = 1;
2664 }
2665 if (combination_has_p2p && combination_has_mgd)
2666 break;
2667 }
2668
2669 if (combination_has_p2p && combination_has_mgd) {
2670 info->p2p_concurrent = 1;
2671 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
2672 info->p2p_multichan_concurrent = 1;
2673 return 1;
2674 }
2675
2676 return 0;
2677}
2678
2679
2680static void wiphy_info_iface_comb(struct wiphy_info_data *info,
2681 struct nlattr *tb)
2682{
2683 struct nlattr *nl_combi;
2684 int rem_combi;
2685
2686 if (tb == NULL)
2687 return;
2688
2689 nla_for_each_nested(nl_combi, tb, rem_combi) {
2690 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
2691 break;
2692 }
2693}
2694
2695
2696static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
2697 struct nlattr *tb)
2698{
2699 struct nlattr *nl_cmd;
2700 int i;
2701
2702 if (tb == NULL)
2703 return;
2704
2705 nla_for_each_nested(nl_cmd, tb, i) {
2706 switch (nla_get_u32(nl_cmd)) {
2707 case NL80211_CMD_AUTHENTICATE:
2708 info->auth_supported = 1;
2709 break;
2710 case NL80211_CMD_CONNECT:
2711 info->connect_supported = 1;
2712 break;
2713 case NL80211_CMD_START_SCHED_SCAN:
2714 info->capa->sched_scan_supported = 1;
2715 break;
2716 case NL80211_CMD_PROBE_CLIENT:
2717 info->poll_command_supported = 1;
2718 break;
2719 }
2720 }
2721}
2722
2723
2724static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
2725 struct nlattr *tb)
2726{
2727 /* default to 5000 since early versions of mac80211 don't set it */
2728 capa->max_remain_on_chan = 5000;
2729
2730 if (tb)
2731 capa->max_remain_on_chan = nla_get_u32(tb);
2732}
2733
2734
2735static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
2736 struct nlattr *ext_setup)
2737{
2738 if (tdls == NULL)
2739 return;
2740
2741 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2742 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2743
2744 if (ext_setup) {
2745 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2746 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2747 }
2748}
2749
2750
2751static void wiphy_info_feature_flags(struct wiphy_info_data *info,
2752 struct nlattr *tb)
2753{
2754 u32 flags;
2755 struct wpa_driver_capa *capa = info->capa;
2756
2757 if (tb == NULL)
2758 return;
2759
2760 flags = nla_get_u32(tb);
2761
2762 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2763 info->data_tx_status = 1;
2764
2765 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
2766 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
2767
2768 if (flags & NL80211_FEATURE_SAE)
2769 capa->flags |= WPA_DRIVER_FLAGS_SAE;
2770
2771 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
2772 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
2773}
2774
2775
2776static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
2777 struct nlattr *tb)
2778{
2779 u32 protocols;
2780
2781 if (tb == NULL)
2782 return;
2783
2784 protocols = nla_get_u32(tb);
2785 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
2786 "mode");
2787 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2788 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
2789}
2790
2791
2792static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2793{
2794 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2795 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2796 struct wiphy_info_data *info = arg;
2797 struct wpa_driver_capa *capa = info->capa;
2798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2800 genlmsg_attrlen(gnlh, 0), NULL);
2801
2802 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002803 capa->max_scan_ssids =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2805
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002806 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
2807 capa->max_sched_scan_ssids =
2808 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2809
2810 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
2811 capa->max_match_sets =
2812 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2813
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002814 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
2815 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
2816 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2819 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2820 "off-channel TX");
2821 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2822 }
2823
2824 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2825 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2826 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2827 }
2828
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002829 wiphy_info_max_roc(capa,
2830 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002831
2832 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2833 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002835 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
2836 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2839 info->device_ap_sme = 1;
2840
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002841 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
2842 wiphy_info_probe_resp_offload(capa,
2843 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845 return NL_SKIP;
2846}
2847
2848
2849static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2850 struct wiphy_info_data *info)
2851{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002852 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 struct nl_msg *msg;
2854
2855 os_memset(info, 0, sizeof(*info));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002856 info->capa = &drv->capa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857
2858 msg = nlmsg_alloc();
2859 if (!msg)
2860 return -1;
2861
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002862 feat = get_nl80211_protocol_features(drv);
2863 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
2864 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
2865 else
2866 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002868 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2870
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002871 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
2872 return -1;
2873
2874 if (info->auth_supported)
2875 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
2876 else if (!info->connect_supported) {
2877 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2878 "authentication/association or connect commands");
2879 info->error = 1;
2880 }
2881
2882 if (info->p2p_go_supported && info->p2p_client_supported)
2883 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2884 if (info->p2p_concurrent) {
2885 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2886 "interface (driver advertised support)");
2887 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2888 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2889 }
2890 if (info->p2p_multichan_concurrent) {
2891 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
2892 "concurrent (driver advertised support)");
2893 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
2894 }
2895 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896nla_put_failure:
2897 nlmsg_free(msg);
2898 return -1;
2899}
2900
2901
2902static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2903{
2904 struct wiphy_info_data info;
2905 if (wpa_driver_nl80211_get_info(drv, &info))
2906 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002907
2908 if (info.error)
2909 return -1;
2910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 drv->has_capability = 1;
2912 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2913 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2914 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2915 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2916 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2917 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2918 WPA_DRIVER_CAPA_ENC_WEP104 |
2919 WPA_DRIVER_CAPA_ENC_TKIP |
2920 WPA_DRIVER_CAPA_ENC_CCMP;
2921 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2922 WPA_DRIVER_AUTH_SHARED |
2923 WPA_DRIVER_AUTH_LEAP;
2924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2926 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07002928
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002929 if (!info.device_ap_sme) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002930 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002931
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002932 /*
2933 * No AP SME is currently assumed to also indicate no AP MLME
2934 * in the driver/firmware.
2935 */
2936 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
2937 }
2938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 drv->device_ap_sme = info.device_ap_sme;
2940 drv->poll_command_supported = info.poll_command_supported;
2941 drv->data_tx_status = info.data_tx_status;
2942
Dmitry Shmidt04949592012-07-19 12:16:46 -07002943#ifdef ANDROID_P2P
2944 if(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
2945 /* Driver is new enough to support monitorless mode*/
2946 wpa_printf(MSG_DEBUG, "nl80211: Driver is new "
2947 "enough to support monitor-less mode");
2948 drv->use_monitor = 0;
2949 }
2950#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002951 /*
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002952 * If poll command and tx status are supported, mac80211 is new enough
2953 * to have everything we need to not need monitor interfaces.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002954 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002955 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957
2958 if (drv->device_ap_sme && drv->use_monitor) {
2959 /*
2960 * Non-mac80211 drivers may not support monitor interface.
2961 * Make sure we do not get stuck with incorrect capability here
2962 * by explicitly testing this.
2963 */
2964 if (!info.monitor_supported) {
2965 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
2966 "with device_ap_sme since no monitor mode "
2967 "support detected");
2968 drv->use_monitor = 0;
2969 }
2970 }
2971
2972 /*
2973 * If we aren't going to use monitor interfaces, but the
2974 * driver doesn't support data TX status, we won't get TX
2975 * status for EAPOL frames.
2976 */
2977 if (!drv->use_monitor && !info.data_tx_status)
2978 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979
2980 return 0;
2981}
2982
2983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002984#ifdef ANDROID
2985static int android_genl_ctrl_resolve(struct nl_handle *handle,
2986 const char *name)
2987{
2988 /*
2989 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
2990 * need to work around that.
2991 */
2992 struct nl_cache *cache = NULL;
2993 struct genl_family *nl80211 = NULL;
2994 int id = -1;
2995
2996 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
2997 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
2998 "netlink cache");
2999 goto fail;
3000 }
3001
3002 nl80211 = genl_ctrl_search_by_name(cache, name);
3003 if (nl80211 == NULL)
3004 goto fail;
3005
3006 id = genl_family_get_id(nl80211);
3007
3008fail:
3009 if (nl80211)
3010 genl_family_put(nl80211);
3011 if (cache)
3012 nl_cache_free(cache);
3013
3014 return id;
3015}
3016#define genl_ctrl_resolve android_genl_ctrl_resolve
3017#endif /* ANDROID */
3018
3019
3020static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021{
3022 int ret;
3023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003024 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3025 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3027 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003028 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 }
3030
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003031 global->nl = nl_create_handle(global->nl_cb, "nl");
3032 if (global->nl == NULL)
3033 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003035 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3036 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3038 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003039 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 }
3041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003042 global->nl_event = nl_create_handle(global->nl_cb, "event");
3043 if (global->nl_event == NULL)
3044 goto err;
3045
3046 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003048 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 if (ret < 0) {
3050 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3051 "membership for scan events: %d (%s)",
3052 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003053 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 }
3055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003056 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003058 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 if (ret < 0) {
3060 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3061 "membership for mlme events: %d (%s)",
3062 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003063 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 }
3065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003066 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003068 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 if (ret < 0) {
3070 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3071 "membership for regulatory events: %d (%s)",
3072 ret, strerror(-ret));
3073 /* Continue without regulatory events */
3074 }
3075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003076 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3077 no_seq_check, NULL);
3078 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3079 process_global_event, global);
3080
3081 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
3082 wpa_driver_nl80211_event_receive,
3083 global->nl_cb, global->nl_event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084
3085 return 0;
3086
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003087err:
3088 nl_destroy_handles(&global->nl_event);
3089 nl_destroy_handles(&global->nl);
3090 nl_cb_put(global->nl_cb);
3091 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 return -1;
3093}
3094
3095
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003096static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3097{
3098 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3099 if (!drv->nl_cb) {
3100 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3101 return -1;
3102 }
3103
3104 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3105 no_seq_check, NULL);
3106 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3107 process_drv_event, drv);
3108
3109 return 0;
3110}
3111
3112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3114{
3115 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3116 /*
3117 * This may be for any interface; use ifdown event to disable
3118 * interface.
3119 */
3120}
3121
3122
3123static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3124{
3125 struct wpa_driver_nl80211_data *drv = ctx;
3126 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003127 if (linux_set_iface_flags(drv->global->ioctl_sock,
3128 drv->first_bss.ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3130 "after rfkill unblock");
3131 return;
3132 }
3133 /* rtnetlink ifup handler will report interface as enabled */
3134}
3135
3136
3137static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
3138{
3139 /* Find phy (radio) to which this interface belongs */
3140 char buf[90], *pos;
3141 int f, rv;
3142
3143 drv->phyname[0] = '\0';
3144 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
3145 drv->first_bss.ifname);
3146 f = open(buf, O_RDONLY);
3147 if (f < 0) {
3148 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
3149 buf, strerror(errno));
3150 return;
3151 }
3152
3153 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
3154 close(f);
3155 if (rv < 0) {
3156 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
3157 buf, strerror(errno));
3158 return;
3159 }
3160
3161 drv->phyname[rv] = '\0';
3162 pos = os_strchr(drv->phyname, '\n');
3163 if (pos)
3164 *pos = '\0';
3165 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3166 drv->first_bss.ifname, drv->phyname);
3167}
3168
3169
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003170static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3171 void *eloop_ctx,
3172 void *handle)
3173{
3174 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3175 u8 data[2048];
3176 struct msghdr msg;
3177 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003178 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179 struct cmsghdr *cmsg;
3180 int res, found_ee = 0, found_wifi = 0, acked = 0;
3181 union wpa_event_data event;
3182
3183 memset(&msg, 0, sizeof(msg));
3184 msg.msg_iov = &entry;
3185 msg.msg_iovlen = 1;
3186 entry.iov_base = data;
3187 entry.iov_len = sizeof(data);
3188 msg.msg_control = &control;
3189 msg.msg_controllen = sizeof(control);
3190
3191 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3192 /* if error or not fitting 802.3 header, return */
3193 if (res < 14)
3194 return;
3195
3196 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3197 {
3198 if (cmsg->cmsg_level == SOL_SOCKET &&
3199 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3200 int *ack;
3201
3202 found_wifi = 1;
3203 ack = (void *)CMSG_DATA(cmsg);
3204 acked = *ack;
3205 }
3206
3207 if (cmsg->cmsg_level == SOL_PACKET &&
3208 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3209 struct sock_extended_err *err =
3210 (struct sock_extended_err *)CMSG_DATA(cmsg);
3211
3212 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3213 found_ee = 1;
3214 }
3215 }
3216
3217 if (!found_ee || !found_wifi)
3218 return;
3219
3220 memset(&event, 0, sizeof(event));
3221 event.eapol_tx_status.dst = data;
3222 event.eapol_tx_status.data = data + 14;
3223 event.eapol_tx_status.data_len = res - 14;
3224 event.eapol_tx_status.ack = acked;
3225 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3226}
3227
3228
3229static int nl80211_init_bss(struct i802_bss *bss)
3230{
3231 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3232 if (!bss->nl_cb)
3233 return -1;
3234
3235 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3236 no_seq_check, NULL);
3237 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3238 process_bss_event, bss);
3239
3240 return 0;
3241}
3242
3243
3244static void nl80211_destroy_bss(struct i802_bss *bss)
3245{
3246 nl_cb_put(bss->nl_cb);
3247 bss->nl_cb = NULL;
3248}
3249
3250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251/**
3252 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3253 * @ctx: context to be used when calling wpa_supplicant functions,
3254 * e.g., wpa_supplicant_event()
3255 * @ifname: interface name, e.g., wlan0
3256 * @global_priv: private driver global data from global_init()
3257 * Returns: Pointer to private data, %NULL on failure
3258 */
3259static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3260 void *global_priv)
3261{
3262 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 struct rfkill_config *rcfg;
3264 struct i802_bss *bss;
3265
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003266 if (global_priv == NULL)
3267 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 drv = os_zalloc(sizeof(*drv));
3269 if (drv == NULL)
3270 return NULL;
3271 drv->global = global_priv;
3272 drv->ctx = ctx;
3273 bss = &drv->first_bss;
3274 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003275 bss->ctx = ctx;
3276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
3278 drv->monitor_ifidx = -1;
3279 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003280 drv->eapol_tx_sock = -1;
3281 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282
3283 if (wpa_driver_nl80211_init_nl(drv)) {
3284 os_free(drv);
3285 return NULL;
3286 }
3287
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003288 if (nl80211_init_bss(bss))
3289 goto failed;
3290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 nl80211_get_phy_name(drv);
3292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 rcfg = os_zalloc(sizeof(*rcfg));
3294 if (rcfg == NULL)
3295 goto failed;
3296 rcfg->ctx = drv;
3297 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3298 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3299 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3300 drv->rfkill = rfkill_init(rcfg);
3301 if (drv->rfkill == NULL) {
3302 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3303 os_free(rcfg);
3304 }
3305
3306 if (wpa_driver_nl80211_finish_drv_init(drv))
3307 goto failed;
3308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3310 if (drv->eapol_tx_sock < 0)
3311 goto failed;
3312
3313 if (drv->data_tx_status) {
3314 int enabled = 1;
3315
3316 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3317 &enabled, sizeof(enabled)) < 0) {
3318 wpa_printf(MSG_DEBUG,
3319 "nl80211: wifi status sockopt failed\n");
3320 drv->data_tx_status = 0;
3321 if (!drv->use_monitor)
3322 drv->capa.flags &=
3323 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3324 } else {
3325 eloop_register_read_sock(drv->eapol_tx_sock,
3326 wpa_driver_nl80211_handle_eapol_tx_status,
3327 drv, NULL);
3328 }
3329 }
3330
3331 if (drv->global) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003333 drv->in_interface_list = 1;
3334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335
3336 return bss;
3337
3338failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003339 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 return NULL;
3341}
3342
3343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 struct nl_handle *nl_handle,
3346 u16 type, const u8 *match, size_t match_len)
3347{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003348 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 struct nl_msg *msg;
3350 int ret = -1;
3351
3352 msg = nlmsg_alloc();
3353 if (!msg)
3354 return -1;
3355
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003356 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3357 type, nl_handle);
3358 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3359 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003361 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3362
3363 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3365 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3366
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003367 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003368 msg = NULL;
3369 if (ret) {
3370 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3371 "failed (type=%u): ret=%d (%s)",
3372 type, ret, strerror(-ret));
3373 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3374 match, match_len);
3375 goto nla_put_failure;
3376 }
3377 ret = 0;
3378nla_put_failure:
3379 nlmsg_free(msg);
3380 return ret;
3381}
3382
3383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003384static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3385{
3386 struct wpa_driver_nl80211_data *drv = bss->drv;
3387
3388 if (bss->nl_mgmt) {
3389 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3390 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3391 return -1;
3392 }
3393
3394 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3395 if (bss->nl_mgmt == NULL)
3396 return -1;
3397
3398 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3399 wpa_driver_nl80211_event_receive, bss->nl_cb,
3400 bss->nl_mgmt);
3401
3402 return 0;
3403}
3404
3405
3406static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407 const u8 *match, size_t match_len)
3408{
3409 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003410 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 type, match, match_len);
3412}
3413
3414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003415static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003417 struct wpa_driver_nl80211_data *drv = bss->drv;
3418
3419 if (nl80211_alloc_mgmt_handle(bss))
3420 return -1;
3421 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3422 "handle %p", bss->nl_mgmt);
3423
3424#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003426 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 return -1;
3428 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003429 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430 return -1;
3431 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003432 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003433 return -1;
3434 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003435 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003437#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3438#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003440 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3442 6) < 0)
3443 return -1;
3444 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003445 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 (u8 *) "\x7f\x50\x6f\x9a\x09",
3447 5) < 0)
3448 return -1;
3449#endif /* CONFIG_P2P */
3450#ifdef CONFIG_IEEE80211W
3451 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003452 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 return -1;
3454#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003455#ifdef CONFIG_TDLS
3456 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3457 /* TDLS Discovery Response */
3458 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3459 0)
3460 return -1;
3461 }
3462#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463
3464 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003465 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003466 return -1;
3467 else
3468 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3469 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3470
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003471 /* WNM - BSS Transition Management Request */
3472 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3473 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003474 /* WNM-Sleep Mode Response */
3475 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3476 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478 return 0;
3479}
3480
3481
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003482static int nl80211_register_spurious_class3(struct i802_bss *bss)
3483{
3484 struct wpa_driver_nl80211_data *drv = bss->drv;
3485 struct nl_msg *msg;
3486 int ret = -1;
3487
3488 msg = nlmsg_alloc();
3489 if (!msg)
3490 return -1;
3491
3492 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3493
3494 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3495
3496 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3497 msg = NULL;
3498 if (ret) {
3499 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3500 "failed: ret=%d (%s)",
3501 ret, strerror(-ret));
3502 goto nla_put_failure;
3503 }
3504 ret = 0;
3505nla_put_failure:
3506 nlmsg_free(msg);
3507 return ret;
3508}
3509
3510
3511static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3512{
3513 static const int stypes[] = {
3514 WLAN_FC_STYPE_AUTH,
3515 WLAN_FC_STYPE_ASSOC_REQ,
3516 WLAN_FC_STYPE_REASSOC_REQ,
3517 WLAN_FC_STYPE_DISASSOC,
3518 WLAN_FC_STYPE_DEAUTH,
3519 WLAN_FC_STYPE_ACTION,
3520 WLAN_FC_STYPE_PROBE_REQ,
3521/* Beacon doesn't work as mac80211 doesn't currently allow
3522 * it, but it wouldn't really be the right thing anyway as
3523 * it isn't per interface ... maybe just dump the scan
3524 * results periodically for OLBC?
3525 */
3526// WLAN_FC_STYPE_BEACON,
3527 };
3528 unsigned int i;
3529
3530 if (nl80211_alloc_mgmt_handle(bss))
3531 return -1;
3532 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3533 "handle %p", bss->nl_mgmt);
3534
3535 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3536 if (nl80211_register_frame(bss, bss->nl_mgmt,
3537 (WLAN_FC_TYPE_MGMT << 2) |
3538 (stypes[i] << 4),
3539 NULL, 0) < 0) {
3540 goto out_err;
3541 }
3542 }
3543
3544 if (nl80211_register_spurious_class3(bss))
3545 goto out_err;
3546
3547 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3548 goto out_err;
3549
3550 return 0;
3551
3552out_err:
3553 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3554 nl_destroy_handles(&bss->nl_mgmt);
3555 return -1;
3556}
3557
3558
3559static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3560{
3561 if (nl80211_alloc_mgmt_handle(bss))
3562 return -1;
3563 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3564 "handle %p (device SME)", bss->nl_mgmt);
3565
3566 if (nl80211_register_frame(bss, bss->nl_mgmt,
3567 (WLAN_FC_TYPE_MGMT << 2) |
3568 (WLAN_FC_STYPE_ACTION << 4),
3569 NULL, 0) < 0)
3570 goto out_err;
3571
3572 return 0;
3573
3574out_err:
3575 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3576 nl_destroy_handles(&bss->nl_mgmt);
3577 return -1;
3578}
3579
3580
3581static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3582{
3583 if (bss->nl_mgmt == NULL)
3584 return;
3585 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3586 "(%s)", bss->nl_mgmt, reason);
3587 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3588 nl_destroy_handles(&bss->nl_mgmt);
3589
3590 nl80211_put_wiphy_data_ap(bss);
3591}
3592
3593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3595{
3596 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3597}
3598
3599
3600static int
3601wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3602{
3603 struct i802_bss *bss = &drv->first_bss;
3604 int send_rfkill_event = 0;
3605
3606 drv->ifindex = if_nametoindex(bss->ifname);
3607 drv->first_bss.ifindex = drv->ifindex;
3608
3609#ifndef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003610 /*
3611 * Make sure the interface starts up in station mode unless this is a
3612 * dynamically added interface (e.g., P2P) that was already configured
3613 * with proper iftype.
3614 */
3615 if (drv->ifindex != drv->global->if_add_ifindex &&
3616 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
3617 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 "use managed mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003619 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 }
3621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003622 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 if (rfkill_is_blocked(drv->rfkill)) {
3624 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3625 "interface '%s' due to rfkill",
3626 bss->ifname);
3627 drv->if_disabled = 1;
3628 send_rfkill_event = 1;
3629 } else {
3630 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3631 "interface '%s' UP", bss->ifname);
3632 return -1;
3633 }
3634 }
3635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003636 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 1, IF_OPER_DORMANT);
3638#endif /* HOSTAPD */
3639
3640 if (wpa_driver_nl80211_capa(drv))
3641 return -1;
3642
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003643 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3644 bss->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 return -1;
3646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647 if (send_rfkill_event) {
3648 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3649 drv, drv->ctx);
3650 }
3651
3652 return 0;
3653}
3654
3655
3656static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3657{
3658 struct nl_msg *msg;
3659
3660 msg = nlmsg_alloc();
3661 if (!msg)
3662 return -ENOMEM;
3663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003664 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3666
3667 return send_and_recv_msgs(drv, msg, NULL, NULL);
3668 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003669 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 return -ENOBUFS;
3671}
3672
3673
3674/**
3675 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003676 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677 *
3678 * Shut down driver interface and processing of driver events. Free
3679 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3680 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003681static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683 struct wpa_driver_nl80211_data *drv = bss->drv;
3684
Dmitry Shmidt04949592012-07-19 12:16:46 -07003685 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003686 if (drv->data_tx_status)
3687 eloop_unregister_read_sock(drv->eapol_tx_sock);
3688 if (drv->eapol_tx_sock >= 0)
3689 close(drv->eapol_tx_sock);
3690
3691 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 wpa_driver_nl80211_probe_req_report(bss, 0);
3693 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003694 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3695 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3697 "interface %s from bridge %s: %s",
3698 bss->ifname, bss->brname, strerror(errno));
3699 }
3700 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003701 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3703 "bridge %s: %s",
3704 bss->brname, strerror(errno));
3705 }
3706
3707 nl80211_remove_monitor_interface(drv);
3708
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003709 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710 wpa_driver_nl80211_del_beacon(drv);
3711
3712#ifdef HOSTAPD
3713 if (drv->last_freq_ht) {
3714 /* Clear HT flags from the driver */
3715 struct hostapd_freq_params freq;
3716 os_memset(&freq, 0, sizeof(freq));
3717 freq.freq = drv->last_freq;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003718 wpa_driver_nl80211_set_freq(bss, &freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 }
3720
3721 if (drv->eapol_sock >= 0) {
3722 eloop_unregister_read_sock(drv->eapol_sock);
3723 close(drv->eapol_sock);
3724 }
3725
3726 if (drv->if_indices != drv->default_if_indices)
3727 os_free(drv->if_indices);
3728#endif /* HOSTAPD */
3729
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003730 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003731 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3732
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003733 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3734 IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735 rfkill_deinit(drv->rfkill);
3736
3737 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003739 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
3740 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
3741 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 nl_cb_put(drv->nl_cb);
3744
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003745 nl80211_destroy_bss(&drv->first_bss);
3746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 os_free(drv->filter_ssids);
3748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003749 os_free(drv->auth_ie);
3750
3751 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 dl_list_del(&drv->list);
3753
3754 os_free(drv);
3755}
3756
3757
3758/**
3759 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
3760 * @eloop_ctx: Driver private data
3761 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3762 *
3763 * This function can be used as registered timeout when starting a scan to
3764 * generate a scan completed event if the driver does not report this.
3765 */
3766static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3767{
3768 struct wpa_driver_nl80211_data *drv = eloop_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003769 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770 wpa_driver_nl80211_set_mode(&drv->first_bss,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003771 drv->ap_scan_as_station);
3772 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 }
3774 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3775 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3776}
3777
3778
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003779static struct nl_msg *
3780nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
3781 struct wpa_driver_scan_params *params)
3782{
3783 struct nl_msg *msg;
3784 int err;
3785 size_t i;
3786
3787 msg = nlmsg_alloc();
3788 if (!msg)
3789 return NULL;
3790
3791 nl80211_cmd(drv, msg, 0, cmd);
3792
3793 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0)
3794 goto fail;
3795
3796 if (params->num_ssids) {
3797 struct nl_msg *ssids = nlmsg_alloc();
3798 if (ssids == NULL)
3799 goto fail;
3800 for (i = 0; i < params->num_ssids; i++) {
3801 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3802 params->ssids[i].ssid,
3803 params->ssids[i].ssid_len);
3804 if (nla_put(ssids, i + 1, params->ssids[i].ssid_len,
3805 params->ssids[i].ssid) < 0) {
3806 nlmsg_free(ssids);
3807 goto fail;
3808 }
3809 }
3810 err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3811 nlmsg_free(ssids);
3812 if (err < 0)
3813 goto fail;
3814 }
3815
3816 if (params->extra_ies) {
3817 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3818 params->extra_ies, params->extra_ies_len);
3819 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
3820 params->extra_ies) < 0)
3821 goto fail;
3822 }
3823
3824 if (params->freqs) {
3825 struct nl_msg *freqs = nlmsg_alloc();
3826 if (freqs == NULL)
3827 goto fail;
3828 for (i = 0; params->freqs[i]; i++) {
3829 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3830 "MHz", params->freqs[i]);
3831 if (nla_put_u32(freqs, i + 1, params->freqs[i]) < 0) {
3832 nlmsg_free(freqs);
3833 goto fail;
3834 }
3835 }
3836 err = nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES,
3837 freqs);
3838 nlmsg_free(freqs);
3839 if (err < 0)
3840 goto fail;
3841 }
3842
3843 os_free(drv->filter_ssids);
3844 drv->filter_ssids = params->filter_ssids;
3845 params->filter_ssids = NULL;
3846 drv->num_filter_ssids = params->num_filter_ssids;
3847
3848 return msg;
3849
3850fail:
3851 nlmsg_free(msg);
3852 return NULL;
3853}
3854
3855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856/**
3857 * wpa_driver_nl80211_scan - Request the driver to initiate scan
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003858 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859 * @params: Scan parameters
3860 * Returns: 0 on success, -1 on failure
3861 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003862static int wpa_driver_nl80211_scan(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 struct wpa_driver_scan_params *params)
3864{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003865 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003866 int ret = -1, timeout;
3867 struct nl_msg *msg, *rates = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003869 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003870 drv->scan_for_auth = 0;
3871
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params);
3873 if (!msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003876 if (params->p2p_probe) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003877 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3878
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003879 rates = nlmsg_alloc();
3880 if (rates == NULL)
3881 goto nla_put_failure;
3882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883 /*
3884 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3885 * by masking out everything else apart from the OFDM rates 6,
3886 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3887 * rates are left enabled.
3888 */
3889 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
3890 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003891 if (nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates) <
3892 0)
3893 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003894
3895 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
3896 }
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3899 msg = NULL;
3900 if (ret) {
3901 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3902 "(%s)", ret, strerror(-ret));
3903#ifdef HOSTAPD
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003904 if (is_ap_interface(drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 /*
3906 * mac80211 does not allow scan requests in AP mode, so
3907 * try to do this in station mode.
3908 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003909 if (wpa_driver_nl80211_set_mode(
3910 bss, NL80211_IFTYPE_STATION))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 goto nla_put_failure;
3912
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003913 if (wpa_driver_nl80211_scan(bss, params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003914 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915 goto nla_put_failure;
3916 }
3917
3918 /* Restore AP mode when processing scan results */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003919 drv->ap_scan_as_station = drv->nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920 ret = 0;
3921 } else
3922 goto nla_put_failure;
3923#else /* HOSTAPD */
3924 goto nla_put_failure;
3925#endif /* HOSTAPD */
3926 }
3927
3928 /* Not all drivers generate "scan completed" wireless event, so try to
3929 * read results after a timeout. */
3930 timeout = 10;
3931 if (drv->scan_complete_events) {
3932 /*
3933 * The driver seems to deliver events to notify when scan is
3934 * complete, so use longer timeout to avoid race conditions
3935 * with scanning and following association request.
3936 */
3937 timeout = 30;
3938 }
3939 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3940 "seconds", ret, timeout);
3941 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3942 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3943 drv, drv->ctx);
3944
3945nla_put_failure:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003947 nlmsg_free(rates);
3948 return ret;
3949}
3950
3951
3952/**
3953 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3954 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3955 * @params: Scan parameters
3956 * @interval: Interval between scan cycles in milliseconds
3957 * Returns: 0 on success, -1 on failure or if not supported
3958 */
3959static int wpa_driver_nl80211_sched_scan(void *priv,
3960 struct wpa_driver_scan_params *params,
3961 u32 interval)
3962{
3963 struct i802_bss *bss = priv;
3964 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003965 int ret = -1;
3966 struct nl_msg *msg;
3967 struct nl_msg *match_set_ssid = NULL, *match_sets = NULL;
3968 struct nl_msg *match_set_rssi = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003969 size_t i;
3970
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003971 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
3972
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003973#ifdef ANDROID
3974 if (!drv->capa.sched_scan_supported)
3975 return android_pno_start(bss, params);
3976#endif /* ANDROID */
3977
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003978 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params);
3979 if (!msg)
3980 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003981
3982 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
3983
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003984 if ((drv->num_filter_ssids &&
3985 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
3986 params->filter_rssi) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003987 match_sets = nlmsg_alloc();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003988 if (match_sets == NULL)
3989 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003990
3991 for (i = 0; i < drv->num_filter_ssids; i++) {
3992 wpa_hexdump_ascii(MSG_MSGDUMP,
3993 "nl80211: Sched scan filter SSID",
3994 drv->filter_ssids[i].ssid,
3995 drv->filter_ssids[i].ssid_len);
3996
3997 match_set_ssid = nlmsg_alloc();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003998 if (match_set_ssid == NULL)
3999 goto nla_put_failure;
4000 NLA_PUT(match_set_ssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004001 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
4002 drv->filter_ssids[i].ssid_len,
4003 drv->filter_ssids[i].ssid);
4004
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004005 if (nla_put_nested(match_sets, i + 1, match_set_ssid) <
4006 0)
4007 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004008 }
4009
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004010 if (params->filter_rssi) {
4011 match_set_rssi = nlmsg_alloc();
4012 if (match_set_rssi == NULL)
4013 goto nla_put_failure;
4014 NLA_PUT_U32(match_set_rssi,
4015 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
4016 params->filter_rssi);
4017 wpa_printf(MSG_MSGDUMP,
4018 "nl80211: Sched scan RSSI filter %d dBm",
4019 params->filter_rssi);
4020 if (nla_put_nested(match_sets, 0, match_set_rssi) < 0)
4021 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004022 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004023
4024 if (nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
4025 match_sets) < 0)
4026 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004027 }
4028
4029 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4030
4031 /* TODO: if we get an error here, we should fall back to normal scan */
4032
4033 msg = NULL;
4034 if (ret) {
4035 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4036 "ret=%d (%s)", ret, strerror(-ret));
4037 goto nla_put_failure;
4038 }
4039
4040 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4041 "scan interval %d msec", ret, interval);
4042
4043nla_put_failure:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004044 nlmsg_free(match_set_ssid);
4045 nlmsg_free(match_sets);
4046 nlmsg_free(match_set_rssi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004047 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004048 return ret;
4049}
4050
4051
4052/**
4053 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4054 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4055 * Returns: 0 on success, -1 on failure or if not supported
4056 */
4057static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4058{
4059 struct i802_bss *bss = priv;
4060 struct wpa_driver_nl80211_data *drv = bss->drv;
4061 int ret = 0;
4062 struct nl_msg *msg;
4063
4064#ifdef ANDROID
4065 if (!drv->capa.sched_scan_supported)
4066 return android_pno_stop(bss);
4067#endif /* ANDROID */
4068
4069 msg = nlmsg_alloc();
4070 if (!msg)
4071 return -1;
4072
4073 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4074
4075 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4076
4077 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4078 msg = NULL;
4079 if (ret) {
4080 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4081 "ret=%d (%s)", ret, strerror(-ret));
4082 goto nla_put_failure;
4083 }
4084
4085 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4086
4087nla_put_failure:
4088 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 return ret;
4090}
4091
4092
4093static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4094{
4095 const u8 *end, *pos;
4096
4097 if (ies == NULL)
4098 return NULL;
4099
4100 pos = ies;
4101 end = ies + ies_len;
4102
4103 while (pos + 1 < end) {
4104 if (pos + 2 + pos[1] > end)
4105 break;
4106 if (pos[0] == ie)
4107 return pos;
4108 pos += 2 + pos[1];
4109 }
4110
4111 return NULL;
4112}
4113
4114
4115static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4116 const u8 *ie, size_t ie_len)
4117{
4118 const u8 *ssid;
4119 size_t i;
4120
4121 if (drv->filter_ssids == NULL)
4122 return 0;
4123
4124 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4125 if (ssid == NULL)
4126 return 1;
4127
4128 for (i = 0; i < drv->num_filter_ssids; i++) {
4129 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4130 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4131 0)
4132 return 0;
4133 }
4134
4135 return 1;
4136}
4137
4138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139static int bss_info_handler(struct nl_msg *msg, void *arg)
4140{
4141 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4142 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4143 struct nlattr *bss[NL80211_BSS_MAX + 1];
4144 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4145 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4146 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4147 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4148 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4149 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4150 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4151 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4152 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
4153 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
4154 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
4155 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
4156 };
4157 struct nl80211_bss_info_arg *_arg = arg;
4158 struct wpa_scan_results *res = _arg->res;
4159 struct wpa_scan_res **tmp;
4160 struct wpa_scan_res *r;
4161 const u8 *ie, *beacon_ie;
4162 size_t ie_len, beacon_ie_len;
4163 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004164 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165
4166 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4167 genlmsg_attrlen(gnlh, 0), NULL);
4168 if (!tb[NL80211_ATTR_BSS])
4169 return NL_SKIP;
4170 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4171 bss_policy))
4172 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03004173 if (bss[NL80211_BSS_STATUS]) {
4174 enum nl80211_bss_status status;
4175 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4176 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4177 bss[NL80211_BSS_FREQUENCY]) {
4178 _arg->assoc_freq =
4179 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4180 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4181 _arg->assoc_freq);
4182 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004183 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4184 bss[NL80211_BSS_BSSID]) {
4185 os_memcpy(_arg->assoc_bssid,
4186 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4187 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4188 MACSTR, MAC2STR(_arg->assoc_bssid));
4189 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03004190 }
4191 if (!res)
4192 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4194 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4195 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4196 } else {
4197 ie = NULL;
4198 ie_len = 0;
4199 }
4200 if (bss[NL80211_BSS_BEACON_IES]) {
4201 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4202 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4203 } else {
4204 beacon_ie = NULL;
4205 beacon_ie_len = 0;
4206 }
4207
4208 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4209 ie ? ie_len : beacon_ie_len))
4210 return NL_SKIP;
4211
4212 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
4213 if (r == NULL)
4214 return NL_SKIP;
4215 if (bss[NL80211_BSS_BSSID])
4216 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4217 ETH_ALEN);
4218 if (bss[NL80211_BSS_FREQUENCY])
4219 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4220 if (bss[NL80211_BSS_BEACON_INTERVAL])
4221 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4222 if (bss[NL80211_BSS_CAPABILITY])
4223 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
4224 r->flags |= WPA_SCAN_NOISE_INVALID;
4225 if (bss[NL80211_BSS_SIGNAL_MBM]) {
4226 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
4227 r->level /= 100; /* mBm to dBm */
4228 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4229 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4230 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004231 r->flags |= WPA_SCAN_QUAL_INVALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232 } else
4233 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
4234 if (bss[NL80211_BSS_TSF])
4235 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
4236 if (bss[NL80211_BSS_SEEN_MS_AGO])
4237 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
4238 r->ie_len = ie_len;
4239 pos = (u8 *) (r + 1);
4240 if (ie) {
4241 os_memcpy(pos, ie, ie_len);
4242 pos += ie_len;
4243 }
4244 r->beacon_ie_len = beacon_ie_len;
4245 if (beacon_ie)
4246 os_memcpy(pos, beacon_ie, beacon_ie_len);
4247
4248 if (bss[NL80211_BSS_STATUS]) {
4249 enum nl80211_bss_status status;
4250 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4251 switch (status) {
4252 case NL80211_BSS_STATUS_AUTHENTICATED:
4253 r->flags |= WPA_SCAN_AUTHENTICATED;
4254 break;
4255 case NL80211_BSS_STATUS_ASSOCIATED:
4256 r->flags |= WPA_SCAN_ASSOCIATED;
4257 break;
4258 default:
4259 break;
4260 }
4261 }
4262
Jouni Malinen87fd2792011-05-16 18:35:42 +03004263 /*
4264 * cfg80211 maintains separate BSS table entries for APs if the same
4265 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4266 * not use frequency as a separate key in the BSS table, so filter out
4267 * duplicated entries. Prefer associated BSS entry in such a case in
4268 * order to get the correct frequency into the BSS table.
4269 */
4270 for (i = 0; i < res->num; i++) {
4271 const u8 *s1, *s2;
4272 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4273 continue;
4274
4275 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4276 res->res[i]->ie_len, WLAN_EID_SSID);
4277 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4278 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4279 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4280 continue;
4281
4282 /* Same BSSID,SSID was already included in scan results */
4283 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4284 "for " MACSTR, MAC2STR(r->bssid));
4285
4286 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4287 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4288 os_free(res->res[i]);
4289 res->res[i] = r;
4290 } else
4291 os_free(r);
4292 return NL_SKIP;
4293 }
4294
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004295 tmp = os_realloc_array(res->res, res->num + 1,
4296 sizeof(struct wpa_scan_res *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 if (tmp == NULL) {
4298 os_free(r);
4299 return NL_SKIP;
4300 }
4301 tmp[res->num++] = r;
4302 res->res = tmp;
4303
4304 return NL_SKIP;
4305}
4306
4307
4308static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4309 const u8 *addr)
4310{
4311 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4312 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4313 "mismatch (" MACSTR ")", MAC2STR(addr));
4314 wpa_driver_nl80211_mlme(drv, addr,
4315 NL80211_CMD_DEAUTHENTICATE,
4316 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4317 }
4318}
4319
4320
4321static void wpa_driver_nl80211_check_bss_status(
4322 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4323{
4324 size_t i;
4325
4326 for (i = 0; i < res->num; i++) {
4327 struct wpa_scan_res *r = res->res[i];
4328 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4329 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4330 "indicates BSS status with " MACSTR
4331 " as authenticated",
4332 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004333 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4335 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4336 0) {
4337 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4338 " in local state (auth=" MACSTR
4339 " assoc=" MACSTR ")",
4340 MAC2STR(drv->auth_bssid),
4341 MAC2STR(drv->bssid));
4342 clear_state_mismatch(drv, r->bssid);
4343 }
4344 }
4345
4346 if (r->flags & WPA_SCAN_ASSOCIATED) {
4347 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4348 "indicate BSS status with " MACSTR
4349 " as associated",
4350 MAC2STR(r->bssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004351 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 !drv->associated) {
4353 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4354 "(not associated) does not match "
4355 "with BSS state");
4356 clear_state_mismatch(drv, r->bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357 } else if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4359 0) {
4360 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4361 "(associated with " MACSTR ") does "
4362 "not match with BSS state",
4363 MAC2STR(drv->bssid));
4364 clear_state_mismatch(drv, r->bssid);
4365 clear_state_mismatch(drv, drv->bssid);
4366 }
4367 }
4368 }
4369}
4370
4371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372static struct wpa_scan_results *
4373nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4374{
4375 struct nl_msg *msg;
4376 struct wpa_scan_results *res;
4377 int ret;
4378 struct nl80211_bss_info_arg arg;
4379
4380 res = os_zalloc(sizeof(*res));
4381 if (res == NULL)
4382 return NULL;
4383 msg = nlmsg_alloc();
4384 if (!msg)
4385 goto nla_put_failure;
4386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004387 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4389
4390 arg.drv = drv;
4391 arg.res = res;
4392 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4393 msg = NULL;
4394 if (ret == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004395 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4396 "BSSes)", (unsigned long) res->num);
4397 nl80211_get_noise_for_scan_results(drv, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398 return res;
4399 }
4400 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4401 "(%s)", ret, strerror(-ret));
4402nla_put_failure:
4403 nlmsg_free(msg);
4404 wpa_scan_results_free(res);
4405 return NULL;
4406}
4407
4408
4409/**
4410 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4411 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4412 * Returns: Scan results on success, -1 on failure
4413 */
4414static struct wpa_scan_results *
4415wpa_driver_nl80211_get_scan_results(void *priv)
4416{
4417 struct i802_bss *bss = priv;
4418 struct wpa_driver_nl80211_data *drv = bss->drv;
4419 struct wpa_scan_results *res;
4420
4421 res = nl80211_get_scan_results(drv);
4422 if (res)
4423 wpa_driver_nl80211_check_bss_status(drv, res);
4424 return res;
4425}
4426
4427
4428static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4429{
4430 struct wpa_scan_results *res;
4431 size_t i;
4432
4433 res = nl80211_get_scan_results(drv);
4434 if (res == NULL) {
4435 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4436 return;
4437 }
4438
4439 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4440 for (i = 0; i < res->num; i++) {
4441 struct wpa_scan_res *r = res->res[i];
4442 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4443 (int) i, (int) res->num, MAC2STR(r->bssid),
4444 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4445 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4446 }
4447
4448 wpa_scan_results_free(res);
4449}
4450
4451
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004452static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453 enum wpa_alg alg, const u8 *addr,
4454 int key_idx, int set_tx,
4455 const u8 *seq, size_t seq_len,
4456 const u8 *key, size_t key_len)
4457{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 struct wpa_driver_nl80211_data *drv = bss->drv;
4459 int ifindex = if_nametoindex(ifname);
4460 struct nl_msg *msg;
4461 int ret;
4462
4463 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4464 "set_tx=%d seq_len=%lu key_len=%lu",
4465 __func__, ifindex, alg, addr, key_idx, set_tx,
4466 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004467#ifdef CONFIG_TDLS
4468 if (key_idx == -1)
4469 key_idx = 0;
4470#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471
4472 msg = nlmsg_alloc();
4473 if (!msg)
4474 return -ENOMEM;
4475
4476 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004477 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004479 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4481 switch (alg) {
4482 case WPA_ALG_WEP:
4483 if (key_len == 5)
4484 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4485 WLAN_CIPHER_SUITE_WEP40);
4486 else
4487 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4488 WLAN_CIPHER_SUITE_WEP104);
4489 break;
4490 case WPA_ALG_TKIP:
4491 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4492 WLAN_CIPHER_SUITE_TKIP);
4493 break;
4494 case WPA_ALG_CCMP:
4495 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4496 WLAN_CIPHER_SUITE_CCMP);
4497 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004498 case WPA_ALG_GCMP:
4499 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4500 WLAN_CIPHER_SUITE_GCMP);
4501 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502 case WPA_ALG_IGTK:
4503 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4504 WLAN_CIPHER_SUITE_AES_CMAC);
4505 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004506 case WPA_ALG_SMS4:
4507 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4508 WLAN_CIPHER_SUITE_SMS4);
4509 break;
4510 case WPA_ALG_KRK:
4511 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4512 WLAN_CIPHER_SUITE_KRK);
4513 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 default:
4515 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4516 "algorithm %d", __func__, alg);
4517 nlmsg_free(msg);
4518 return -1;
4519 }
4520 }
4521
4522 if (seq && seq_len)
4523 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4524
4525 if (addr && !is_broadcast_ether_addr(addr)) {
4526 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4527 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4528
4529 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4530 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4531 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4532 NL80211_KEYTYPE_GROUP);
4533 }
4534 } else if (addr && is_broadcast_ether_addr(addr)) {
4535 struct nl_msg *types;
4536 int err;
4537 wpa_printf(MSG_DEBUG, " broadcast key");
4538 types = nlmsg_alloc();
4539 if (!types)
4540 goto nla_put_failure;
4541 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4542 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4543 types);
4544 nlmsg_free(types);
4545 if (err)
4546 goto nla_put_failure;
4547 }
4548 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4549 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4550
4551 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4552 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4553 ret = 0;
4554 if (ret)
4555 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4556 ret, strerror(-ret));
4557
4558 /*
4559 * If we failed or don't need to set the default TX key (below),
4560 * we're done here.
4561 */
4562 if (ret || !set_tx || alg == WPA_ALG_NONE)
4563 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004564 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565 !is_broadcast_ether_addr(addr))
4566 return ret;
4567
4568 msg = nlmsg_alloc();
4569 if (!msg)
4570 return -ENOMEM;
4571
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004572 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4574 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4575 if (alg == WPA_ALG_IGTK)
4576 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4577 else
4578 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4579 if (addr && is_broadcast_ether_addr(addr)) {
4580 struct nl_msg *types;
4581 int err;
4582 types = nlmsg_alloc();
4583 if (!types)
4584 goto nla_put_failure;
4585 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4586 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4587 types);
4588 nlmsg_free(types);
4589 if (err)
4590 goto nla_put_failure;
4591 } else if (addr) {
4592 struct nl_msg *types;
4593 int err;
4594 types = nlmsg_alloc();
4595 if (!types)
4596 goto nla_put_failure;
4597 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4598 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4599 types);
4600 nlmsg_free(types);
4601 if (err)
4602 goto nla_put_failure;
4603 }
4604
4605 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4606 if (ret == -ENOENT)
4607 ret = 0;
4608 if (ret)
4609 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4610 "err=%d %s)", ret, strerror(-ret));
4611 return ret;
4612
4613nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004614 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 return -ENOBUFS;
4616}
4617
4618
4619static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4620 int key_idx, int defkey,
4621 const u8 *seq, size_t seq_len,
4622 const u8 *key, size_t key_len)
4623{
4624 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4625 if (!key_attr)
4626 return -1;
4627
4628 if (defkey && alg == WPA_ALG_IGTK)
4629 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4630 else if (defkey)
4631 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4632
4633 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4634
4635 switch (alg) {
4636 case WPA_ALG_WEP:
4637 if (key_len == 5)
4638 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4639 WLAN_CIPHER_SUITE_WEP40);
4640 else
4641 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4642 WLAN_CIPHER_SUITE_WEP104);
4643 break;
4644 case WPA_ALG_TKIP:
4645 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4646 break;
4647 case WPA_ALG_CCMP:
4648 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4649 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004650 case WPA_ALG_GCMP:
4651 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
4652 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653 case WPA_ALG_IGTK:
4654 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4655 WLAN_CIPHER_SUITE_AES_CMAC);
4656 break;
4657 default:
4658 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4659 "algorithm %d", __func__, alg);
4660 return -1;
4661 }
4662
4663 if (seq && seq_len)
4664 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4665
4666 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4667
4668 nla_nest_end(msg, key_attr);
4669
4670 return 0;
4671 nla_put_failure:
4672 return -1;
4673}
4674
4675
4676static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4677 struct nl_msg *msg)
4678{
4679 int i, privacy = 0;
4680 struct nlattr *nl_keys, *nl_key;
4681
4682 for (i = 0; i < 4; i++) {
4683 if (!params->wep_key[i])
4684 continue;
4685 privacy = 1;
4686 break;
4687 }
4688 if (params->wps == WPS_MODE_PRIVACY)
4689 privacy = 1;
4690 if (params->pairwise_suite &&
4691 params->pairwise_suite != WPA_CIPHER_NONE)
4692 privacy = 1;
4693
4694 if (!privacy)
4695 return 0;
4696
4697 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4698
4699 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4700 if (!nl_keys)
4701 goto nla_put_failure;
4702
4703 for (i = 0; i < 4; i++) {
4704 if (!params->wep_key[i])
4705 continue;
4706
4707 nl_key = nla_nest_start(msg, i);
4708 if (!nl_key)
4709 goto nla_put_failure;
4710
4711 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4712 params->wep_key[i]);
4713 if (params->wep_key_len[i] == 5)
4714 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4715 WLAN_CIPHER_SUITE_WEP40);
4716 else
4717 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4718 WLAN_CIPHER_SUITE_WEP104);
4719
4720 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4721
4722 if (i == params->wep_tx_keyidx)
4723 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4724
4725 nla_nest_end(msg, nl_key);
4726 }
4727 nla_nest_end(msg, nl_keys);
4728
4729 return 0;
4730
4731nla_put_failure:
4732 return -ENOBUFS;
4733}
4734
4735
4736static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
4737 const u8 *addr, int cmd, u16 reason_code,
4738 int local_state_change)
4739{
4740 int ret = -1;
4741 struct nl_msg *msg;
4742
4743 msg = nlmsg_alloc();
4744 if (!msg)
4745 return -1;
4746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004747 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748
4749 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4750 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004751 if (addr)
4752 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 if (local_state_change)
4754 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4755
4756 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4757 msg = NULL;
4758 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004759 wpa_dbg(drv->ctx, MSG_DEBUG,
4760 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4761 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 goto nla_put_failure;
4763 }
4764 ret = 0;
4765
4766nla_put_failure:
4767 nlmsg_free(msg);
4768 return ret;
4769}
4770
4771
4772static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004773 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004775 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776 drv->associated = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004777 drv->ignore_next_local_disconnect = 0;
4778 /* Disconnect command doesn't need BSSID - it uses cached value */
4779 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 reason_code, 0);
4781}
4782
4783
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004784static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
4785 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 struct wpa_driver_nl80211_data *drv = bss->drv;
4788 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004789 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4791 __func__, MAC2STR(addr), reason_code);
4792 drv->associated = 0;
4793 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4794 return nl80211_leave_ibss(drv);
4795 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
4796 reason_code, 0);
4797}
4798
4799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004800static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4801 struct wpa_driver_auth_params *params)
4802{
4803 int i;
4804
4805 drv->auth_freq = params->freq;
4806 drv->auth_alg = params->auth_alg;
4807 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4808 drv->auth_local_state_change = params->local_state_change;
4809 drv->auth_p2p = params->p2p;
4810
4811 if (params->bssid)
4812 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4813 else
4814 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4815
4816 if (params->ssid) {
4817 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4818 drv->auth_ssid_len = params->ssid_len;
4819 } else
4820 drv->auth_ssid_len = 0;
4821
4822
4823 os_free(drv->auth_ie);
4824 drv->auth_ie = NULL;
4825 drv->auth_ie_len = 0;
4826 if (params->ie) {
4827 drv->auth_ie = os_malloc(params->ie_len);
4828 if (drv->auth_ie) {
4829 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4830 drv->auth_ie_len = params->ie_len;
4831 }
4832 }
4833
4834 for (i = 0; i < 4; i++) {
4835 if (params->wep_key[i] && params->wep_key_len[i] &&
4836 params->wep_key_len[i] <= 16) {
4837 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4838 params->wep_key_len[i]);
4839 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4840 } else
4841 drv->auth_wep_key_len[i] = 0;
4842 }
4843}
4844
4845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004847 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 struct wpa_driver_nl80211_data *drv = bss->drv;
4850 int ret = -1, i;
4851 struct nl_msg *msg;
4852 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004853 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004855 int is_retry;
4856
4857 is_retry = drv->retry_auth;
4858 drv->retry_auth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859
4860 drv->associated = 0;
4861 os_memset(drv->auth_bssid, 0, ETH_ALEN);
4862 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004863 nlmode = params->p2p ?
4864 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4865 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004866 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 return -1;
4868
4869retry:
4870 msg = nlmsg_alloc();
4871 if (!msg)
4872 return -1;
4873
4874 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4875 drv->ifindex);
4876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004877 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878
4879 for (i = 0; i < 4; i++) {
4880 if (!params->wep_key[i])
4881 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004882 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 NULL, i,
4884 i == params->wep_tx_keyidx, NULL, 0,
4885 params->wep_key[i],
4886 params->wep_key_len[i]);
4887 if (params->wep_tx_keyidx != i)
4888 continue;
4889 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4890 params->wep_key[i], params->wep_key_len[i])) {
4891 nlmsg_free(msg);
4892 return -1;
4893 }
4894 }
4895
4896 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4897 if (params->bssid) {
4898 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4899 MAC2STR(params->bssid));
4900 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4901 }
4902 if (params->freq) {
4903 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4904 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4905 }
4906 if (params->ssid) {
4907 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4908 params->ssid, params->ssid_len);
4909 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4910 params->ssid);
4911 }
4912 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4913 if (params->ie)
4914 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004915 if (params->sae_data) {
4916 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
4917 params->sae_data_len);
4918 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
4919 params->sae_data);
4920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4922 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4923 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4924 type = NL80211_AUTHTYPE_SHARED_KEY;
4925 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4926 type = NL80211_AUTHTYPE_NETWORK_EAP;
4927 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4928 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004929 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
4930 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 else
4932 goto nla_put_failure;
4933 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4934 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4935 if (params->local_state_change) {
4936 wpa_printf(MSG_DEBUG, " * Local state change only");
4937 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4938 }
4939
4940 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4941 msg = NULL;
4942 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004943 wpa_dbg(drv->ctx, MSG_DEBUG,
4944 "nl80211: MLME command failed (auth): ret=%d (%s)",
4945 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946 count++;
4947 if (ret == -EALREADY && count == 1 && params->bssid &&
4948 !params->local_state_change) {
4949 /*
4950 * mac80211 does not currently accept new
4951 * authentication if we are already authenticated. As a
4952 * workaround, force deauthentication and try again.
4953 */
4954 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4955 "after forced deauthentication");
4956 wpa_driver_nl80211_deauthenticate(
4957 bss, params->bssid,
4958 WLAN_REASON_PREV_AUTH_NOT_VALID);
4959 nlmsg_free(msg);
4960 goto retry;
4961 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004962
4963 if (ret == -ENOENT && params->freq && !is_retry) {
4964 /*
4965 * cfg80211 has likely expired the BSS entry even
4966 * though it was previously available in our internal
4967 * BSS table. To recover quickly, start a single
4968 * channel scan on the specified channel.
4969 */
4970 struct wpa_driver_scan_params scan;
4971 int freqs[2];
4972
4973 os_memset(&scan, 0, sizeof(scan));
4974 scan.num_ssids = 1;
4975 if (params->ssid) {
4976 scan.ssids[0].ssid = params->ssid;
4977 scan.ssids[0].ssid_len = params->ssid_len;
4978 }
4979 freqs[0] = params->freq;
4980 freqs[1] = 0;
4981 scan.freqs = freqs;
4982 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4983 "channel scan to refresh cfg80211 BSS "
4984 "entry");
4985 ret = wpa_driver_nl80211_scan(bss, &scan);
4986 if (ret == 0) {
4987 nl80211_copy_auth_params(drv, params);
4988 drv->scan_for_auth = 1;
4989 }
4990 } else if (is_retry) {
4991 /*
4992 * Need to indicate this with an event since the return
4993 * value from the retry is not delivered to core code.
4994 */
4995 union wpa_event_data event;
4996 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4997 "failed");
4998 os_memset(&event, 0, sizeof(event));
4999 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5000 ETH_ALEN);
5001 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5002 &event);
5003 }
5004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 goto nla_put_failure;
5006 }
5007 ret = 0;
5008 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5009 "successfully");
5010
5011nla_put_failure:
5012 nlmsg_free(msg);
5013 return ret;
5014}
5015
5016
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005017static int wpa_driver_nl80211_authenticate_retry(
5018 struct wpa_driver_nl80211_data *drv)
5019{
5020 struct wpa_driver_auth_params params;
5021 struct i802_bss *bss = &drv->first_bss;
5022 int i;
5023
5024 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5025
5026 os_memset(&params, 0, sizeof(params));
5027 params.freq = drv->auth_freq;
5028 params.auth_alg = drv->auth_alg;
5029 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5030 params.local_state_change = drv->auth_local_state_change;
5031 params.p2p = drv->auth_p2p;
5032
5033 if (!is_zero_ether_addr(drv->auth_bssid_))
5034 params.bssid = drv->auth_bssid_;
5035
5036 if (drv->auth_ssid_len) {
5037 params.ssid = drv->auth_ssid;
5038 params.ssid_len = drv->auth_ssid_len;
5039 }
5040
5041 params.ie = drv->auth_ie;
5042 params.ie_len = drv->auth_ie_len;
5043
5044 for (i = 0; i < 4; i++) {
5045 if (drv->auth_wep_key_len[i]) {
5046 params.wep_key[i] = drv->auth_wep_key[i];
5047 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5048 }
5049 }
5050
5051 drv->retry_auth = 1;
5052 return wpa_driver_nl80211_authenticate(bss, &params);
5053}
5054
5055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056struct phy_info_arg {
5057 u16 *num_modes;
5058 struct hostapd_hw_modes *modes;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005059 int last_mode, last_chan_idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060};
5061
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005062static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5063 struct nlattr *ampdu_factor,
5064 struct nlattr *ampdu_density,
5065 struct nlattr *mcs_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005067 if (capa)
5068 mode->ht_capab = nla_get_u16(capa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005070 if (ampdu_factor)
5071 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005073 if (ampdu_density)
5074 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5075
5076 if (mcs_set && nla_len(mcs_set) >= 16) {
5077 u8 *mcs;
5078 mcs = nla_data(mcs_set);
5079 os_memcpy(mode->mcs_set, mcs, 16);
5080 }
5081}
5082
5083
5084static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5085 struct nlattr *capa,
5086 struct nlattr *mcs_set)
5087{
5088 if (capa)
5089 mode->vht_capab = nla_get_u32(capa);
5090
5091 if (mcs_set && nla_len(mcs_set) >= 8) {
5092 u8 *mcs;
5093 mcs = nla_data(mcs_set);
5094 os_memcpy(mode->vht_mcs_set, mcs, 8);
5095 }
5096}
5097
5098
5099static void phy_info_freq(struct hostapd_hw_modes *mode,
5100 struct hostapd_channel_data *chan,
5101 struct nlattr *tb_freq[])
5102{
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005103 enum hostapd_hw_mode m;
5104
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005105 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5106 chan->flag = 0;
5107
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005108 if (chan->freq < 4000)
5109 m = HOSTAPD_MODE_IEEE80211B;
5110 else if (chan->freq > 50000)
5111 m = HOSTAPD_MODE_IEEE80211AD;
5112 else
5113 m = HOSTAPD_MODE_IEEE80211A;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005114
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005115 switch (m) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005116 case HOSTAPD_MODE_IEEE80211AD:
5117 chan->chan = (chan->freq - 56160) / 2160;
5118 break;
5119 case HOSTAPD_MODE_IEEE80211A:
5120 chan->chan = chan->freq / 5 - 1000;
5121 break;
5122 case HOSTAPD_MODE_IEEE80211B:
5123 case HOSTAPD_MODE_IEEE80211G:
5124 if (chan->freq == 2484)
5125 chan->chan = 14;
5126 else
5127 chan->chan = (chan->freq - 2407) / 5;
5128 break;
5129 default:
5130 break;
5131 }
5132
5133 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5134 chan->flag |= HOSTAPD_CHAN_DISABLED;
5135 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5136 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5137 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5138 chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5139 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5140 chan->flag |= HOSTAPD_CHAN_RADAR;
5141
5142 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5143 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5144 chan->max_tx_power = nla_get_u32(
5145 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
5146}
5147
5148
5149static int phy_info_freqs(struct phy_info_arg *phy_info,
5150 struct hostapd_hw_modes *mode, struct nlattr *tb)
5151{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5153 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5154 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5155 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5156 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5157 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5158 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
5159 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005160 int new_channels = 0;
5161 struct hostapd_channel_data *channel;
5162 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 struct nlattr *nl_freq;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005164 int rem_freq, idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005166 if (tb == NULL)
5167 return NL_OK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005169 nla_for_each_nested(nl_freq, tb, rem_freq) {
5170 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5171 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5172 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5173 continue;
5174 new_channels++;
5175 }
5176
5177 channel = os_realloc_array(mode->channels,
5178 mode->num_channels + new_channels,
5179 sizeof(struct hostapd_channel_data));
5180 if (!channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181 return NL_SKIP;
5182
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005183 mode->channels = channel;
5184 mode->num_channels += new_channels;
5185
5186 idx = phy_info->last_chan_idx;
5187
5188 nla_for_each_nested(nl_freq, tb, rem_freq) {
5189 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5190 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5191 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5192 continue;
5193 phy_info_freq(mode, &mode->channels[idx], tb_freq);
5194 idx++;
5195 }
5196 phy_info->last_chan_idx = idx;
5197
5198 return NL_OK;
5199}
5200
5201
5202static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5203{
5204 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5205 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
5206 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5207 { .type = NLA_FLAG },
5208 };
5209 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
5210 struct nlattr *nl_rate;
5211 int rem_rate, idx;
5212
5213 if (tb == NULL)
5214 return NL_OK;
5215
5216 nla_for_each_nested(nl_rate, tb, rem_rate) {
5217 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5218 nla_data(nl_rate), nla_len(nl_rate),
5219 rate_policy);
5220 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5221 continue;
5222 mode->num_rates++;
5223 }
5224
5225 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5226 if (!mode->rates)
5227 return NL_SKIP;
5228
5229 idx = 0;
5230
5231 nla_for_each_nested(nl_rate, tb, rem_rate) {
5232 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5233 nla_data(nl_rate), nla_len(nl_rate),
5234 rate_policy);
5235 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5236 continue;
5237 mode->rates[idx] = nla_get_u32(
5238 tb_rate[NL80211_BITRATE_ATTR_RATE]);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005239 idx++;
5240 }
5241
5242 return NL_OK;
5243}
5244
5245
5246static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5247{
5248 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
5249 struct hostapd_hw_modes *mode;
5250 int ret;
5251
5252 if (phy_info->last_mode != nl_band->nla_type) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005253 mode = os_realloc_array(phy_info->modes,
5254 *phy_info->num_modes + 1,
5255 sizeof(*mode));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256 if (!mode)
5257 return NL_SKIP;
5258 phy_info->modes = mode;
5259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 mode = &phy_info->modes[*(phy_info->num_modes)];
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005261 os_memset(mode, 0, sizeof(*mode));
5262 mode->mode = NUM_HOSTAPD_MODES;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264 *(phy_info->num_modes) += 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005265 phy_info->last_mode = nl_band->nla_type;
5266 phy_info->last_chan_idx = 0;
5267 } else
5268 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005269
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005270 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5271 nla_len(nl_band), NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005273 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5274 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5275 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5276 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5277 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5278 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5279 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5280 if (ret != NL_OK)
5281 return ret;
5282 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5283 if (ret != NL_OK)
5284 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005286 return NL_OK;
5287}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005290static int phy_info_handler(struct nl_msg *msg, void *arg)
5291{
5292 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5293 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5294 struct phy_info_arg *phy_info = arg;
5295 struct nlattr *nl_band;
5296 int rem_band;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005298 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5299 genlmsg_attrlen(gnlh, 0), NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005300
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005301 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5302 return NL_SKIP;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005303
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005304 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5305 {
5306 int res = phy_info_band(phy_info, nl_band);
5307 if (res != NL_OK)
5308 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309 }
5310
5311 return NL_SKIP;
5312}
5313
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315static struct hostapd_hw_modes *
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005316wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5317 u16 *num_modes)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318{
5319 u16 m;
5320 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5321 int i, mode11g_idx = -1;
5322
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005323 /* heuristic to set up modes */
5324 for (m = 0; m < *num_modes; m++) {
5325 if (!modes[m].num_channels)
5326 continue;
5327 if (modes[m].channels[0].freq < 4000) {
5328 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5329 for (i = 0; i < modes[m].num_rates; i++) {
5330 if (modes[m].rates[i] > 200) {
5331 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5332 break;
5333 }
5334 }
5335 } else if (modes[m].channels[0].freq > 50000)
5336 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5337 else
5338 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5339 }
5340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341 /* If only 802.11g mode is included, use it to construct matching
5342 * 802.11b mode data. */
5343
5344 for (m = 0; m < *num_modes; m++) {
5345 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5346 return modes; /* 802.11b already included */
5347 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5348 mode11g_idx = m;
5349 }
5350
5351 if (mode11g_idx < 0)
5352 return modes; /* 2.4 GHz band not supported at all */
5353
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005354 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 if (nmodes == NULL)
5356 return modes; /* Could not add 802.11b mode */
5357
5358 mode = &nmodes[*num_modes];
5359 os_memset(mode, 0, sizeof(*mode));
5360 (*num_modes)++;
5361 modes = nmodes;
5362
5363 mode->mode = HOSTAPD_MODE_IEEE80211B;
5364
5365 mode11g = &modes[mode11g_idx];
5366 mode->num_channels = mode11g->num_channels;
5367 mode->channels = os_malloc(mode11g->num_channels *
5368 sizeof(struct hostapd_channel_data));
5369 if (mode->channels == NULL) {
5370 (*num_modes)--;
5371 return modes; /* Could not add 802.11b mode */
5372 }
5373 os_memcpy(mode->channels, mode11g->channels,
5374 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5375
5376 mode->num_rates = 0;
5377 mode->rates = os_malloc(4 * sizeof(int));
5378 if (mode->rates == NULL) {
5379 os_free(mode->channels);
5380 (*num_modes)--;
5381 return modes; /* Could not add 802.11b mode */
5382 }
5383
5384 for (i = 0; i < mode11g->num_rates; i++) {
5385 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5386 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
5387 continue;
5388 mode->rates[mode->num_rates] = mode11g->rates[i];
5389 mode->num_rates++;
5390 if (mode->num_rates == 4)
5391 break;
5392 }
5393
5394 if (mode->num_rates == 0) {
5395 os_free(mode->channels);
5396 os_free(mode->rates);
5397 (*num_modes)--;
5398 return modes; /* No 802.11b rates */
5399 }
5400
5401 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5402 "information");
5403
5404 return modes;
5405}
5406
5407
5408static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5409 int end)
5410{
5411 int c;
5412
5413 for (c = 0; c < mode->num_channels; c++) {
5414 struct hostapd_channel_data *chan = &mode->channels[c];
5415 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5416 chan->flag |= HOSTAPD_CHAN_HT40;
5417 }
5418}
5419
5420
5421static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5422 int end)
5423{
5424 int c;
5425
5426 for (c = 0; c < mode->num_channels; c++) {
5427 struct hostapd_channel_data *chan = &mode->channels[c];
5428 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5429 continue;
5430 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5431 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5432 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5433 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5434 }
5435}
5436
5437
5438static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5439 struct phy_info_arg *results)
5440{
5441 u32 start, end, max_bw;
5442 u16 m;
5443
5444 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5445 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5446 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5447 return;
5448
5449 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5450 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5451 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5452
5453 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5454 start, end, max_bw);
5455 if (max_bw < 40)
5456 return;
5457
5458 for (m = 0; m < *results->num_modes; m++) {
5459 if (!(results->modes[m].ht_capab &
5460 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5461 continue;
5462 nl80211_set_ht40_mode(&results->modes[m], start, end);
5463 }
5464}
5465
5466
5467static void nl80211_reg_rule_sec(struct nlattr *tb[],
5468 struct phy_info_arg *results)
5469{
5470 u32 start, end, max_bw;
5471 u16 m;
5472
5473 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5474 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5475 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5476 return;
5477
5478 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5479 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5480 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5481
5482 if (max_bw < 20)
5483 return;
5484
5485 for (m = 0; m < *results->num_modes; m++) {
5486 if (!(results->modes[m].ht_capab &
5487 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5488 continue;
5489 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5490 }
5491}
5492
5493
5494static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5495{
5496 struct phy_info_arg *results = arg;
5497 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5498 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5499 struct nlattr *nl_rule;
5500 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5501 int rem_rule;
5502 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5503 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5504 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5505 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5506 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5507 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5508 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5509 };
5510
5511 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5512 genlmsg_attrlen(gnlh, 0), NULL);
5513 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5514 !tb_msg[NL80211_ATTR_REG_RULES]) {
5515 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5516 "available");
5517 return NL_SKIP;
5518 }
5519
5520 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5521 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5522
5523 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5524 {
5525 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5526 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5527 nl80211_reg_rule_ht40(tb_rule, results);
5528 }
5529
5530 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5531 {
5532 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5533 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5534 nl80211_reg_rule_sec(tb_rule, results);
5535 }
5536
5537 return NL_SKIP;
5538}
5539
5540
5541static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5542 struct phy_info_arg *results)
5543{
5544 struct nl_msg *msg;
5545
5546 msg = nlmsg_alloc();
5547 if (!msg)
5548 return -ENOMEM;
5549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005550 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5552}
5553
5554
5555static struct hostapd_hw_modes *
5556wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5557{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005558 u32 feat;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005559 struct i802_bss *bss = priv;
5560 struct wpa_driver_nl80211_data *drv = bss->drv;
5561 struct nl_msg *msg;
5562 struct phy_info_arg result = {
5563 .num_modes = num_modes,
5564 .modes = NULL,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005565 .last_mode = -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566 };
5567
5568 *num_modes = 0;
5569 *flags = 0;
5570
5571 msg = nlmsg_alloc();
5572 if (!msg)
5573 return NULL;
5574
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005575 feat = get_nl80211_protocol_features(drv);
5576 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
5577 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
5578 else
5579 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580
Dmitry Shmidt2f023192013-03-12 12:44:17 -07005581 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5583
5584 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5585 nl80211_set_ht40_flags(drv, &result);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005586 return wpa_driver_nl80211_postprocess_modes(result.modes,
5587 num_modes);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005589 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005591 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 return NULL;
5593}
5594
5595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005596static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5597 const void *data, size_t len,
5598 int encrypt, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599{
5600 __u8 rtap_hdr[] = {
5601 0x00, 0x00, /* radiotap version */
5602 0x0e, 0x00, /* radiotap length */
5603 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5604 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5605 0x00, /* padding */
5606 0x00, 0x00, /* RX and TX flags to indicate that */
5607 0x00, 0x00, /* this is the injected frame directly */
5608 };
5609 struct iovec iov[2] = {
5610 {
5611 .iov_base = &rtap_hdr,
5612 .iov_len = sizeof(rtap_hdr),
5613 },
5614 {
5615 .iov_base = (void *) data,
5616 .iov_len = len,
5617 }
5618 };
5619 struct msghdr msg = {
5620 .msg_name = NULL,
5621 .msg_namelen = 0,
5622 .msg_iov = iov,
5623 .msg_iovlen = 2,
5624 .msg_control = NULL,
5625 .msg_controllen = 0,
5626 .msg_flags = 0,
5627 };
5628 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005629 u16 txflags = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630
5631 if (encrypt)
5632 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5633
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005634 if (drv->monitor_sock < 0) {
5635 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5636 "for %s", __func__);
5637 return -1;
5638 }
5639
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005640 if (noack)
5641 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005642 WPA_PUT_LE16(&rtap_hdr[12], txflags);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644 res = sendmsg(drv->monitor_sock, &msg, 0);
5645 if (res < 0) {
5646 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5647 return -1;
5648 }
5649 return 0;
5650}
5651
5652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005653static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5654 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005655 int encrypt, int noack,
5656 unsigned int freq, int no_cck,
5657 int offchanok, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005658{
5659 struct wpa_driver_nl80211_data *drv = bss->drv;
5660 u64 cookie;
5661
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005662 if (freq == 0)
5663 freq = bss->freq;
5664
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005665 if (drv->use_monitor)
5666 return wpa_driver_nl80211_send_mntr(drv, data, len,
5667 encrypt, noack);
5668
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005669 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5670 &cookie, no_cck, noack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005671}
5672
5673
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005674static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
5675 size_t data_len, int noack,
5676 unsigned int freq, int no_cck,
5677 int offchanok,
5678 unsigned int wait_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 struct wpa_driver_nl80211_data *drv = bss->drv;
5681 struct ieee80211_mgmt *mgmt;
5682 int encrypt = 1;
5683 u16 fc;
5684
5685 mgmt = (struct ieee80211_mgmt *) data;
5686 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005687
5688 if (is_sta_interface(drv->nlmode) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5690 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5691 /*
5692 * The use of last_mgmt_freq is a bit of a hack,
5693 * but it works due to the single-threaded nature
5694 * of wpa_supplicant.
5695 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005696 if (freq == 0)
5697 freq = drv->last_mgmt_freq;
5698 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005699 data, data_len, NULL, 1, noack,
5700 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005702
5703 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005704 if (freq == 0)
5705 freq = bss->freq;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005706 return nl80211_send_frame_cmd(bss, freq,
5707 (int) freq == bss->freq ? 0 :
5708 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005709 data, data_len,
5710 &drv->send_action_cookie,
5711 no_cck, noack, offchanok);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005712 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07005713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5715 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5716 /*
5717 * Only one of the authentication frame types is encrypted.
5718 * In order for static WEP encryption to work properly (i.e.,
5719 * to not encrypt the frame), we need to tell mac80211 about
5720 * the frames that must not be encrypted.
5721 */
5722 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5723 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
5724 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5725 encrypt = 0;
5726 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005727
5728 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005729 noack, freq, no_cck, offchanok,
5730 wait_time);
5731}
5732
5733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005734static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
5735 int slot, int ht_opmode, int ap_isolate,
5736 int *basic_rates)
5737{
5738 struct wpa_driver_nl80211_data *drv = bss->drv;
5739 struct nl_msg *msg;
5740
5741 msg = nlmsg_alloc();
5742 if (!msg)
5743 return -ENOMEM;
5744
5745 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5746
5747 if (cts >= 0)
5748 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5749 if (preamble >= 0)
5750 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5751 if (slot >= 0)
5752 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5753 if (ht_opmode >= 0)
5754 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5755 if (ap_isolate >= 0)
5756 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
5757
5758 if (basic_rates) {
5759 u8 rates[NL80211_MAX_SUPP_RATES];
5760 u8 rates_len = 0;
5761 int i;
5762
5763 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5764 i++)
5765 rates[rates_len++] = basic_rates[i] / 5;
5766
5767 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5768 }
5769
5770 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5771
5772 return send_and_recv_msgs(drv, msg, NULL, NULL);
5773 nla_put_failure:
5774 nlmsg_free(msg);
5775 return -ENOBUFS;
5776}
5777
5778
5779static int wpa_driver_nl80211_set_ap(void *priv,
5780 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781{
5782 struct i802_bss *bss = priv;
5783 struct wpa_driver_nl80211_data *drv = bss->drv;
5784 struct nl_msg *msg;
5785 u8 cmd = NL80211_CMD_NEW_BEACON;
5786 int ret;
5787 int beacon_set;
5788 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005789 int num_suites;
5790 u32 suites[10];
5791 u32 ver;
5792
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07005793 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794
5795 msg = nlmsg_alloc();
5796 if (!msg)
5797 return -ENOMEM;
5798
5799 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5800 beacon_set);
5801 if (beacon_set)
5802 cmd = NL80211_CMD_SET_BEACON;
5803
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005804 nl80211_cmd(drv, msg, 0, cmd);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005805 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
5806 params->head, params->head_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005807 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005808 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
5809 params->tail, params->tail_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005810 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005811 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005813 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005814 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005815 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005816 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005817 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
5818 params->ssid, params->ssid_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005819 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5820 params->ssid);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005821 if (params->proberesp && params->proberesp_len) {
5822 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
5823 params->proberesp, params->proberesp_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5825 params->proberesp);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005826 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005827 switch (params->hide_ssid) {
5828 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005829 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005830 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5831 NL80211_HIDDEN_SSID_NOT_IN_USE);
5832 break;
5833 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005834 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005835 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5836 NL80211_HIDDEN_SSID_ZERO_LEN);
5837 break;
5838 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005839 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005840 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5841 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5842 break;
5843 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005844 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845 if (params->privacy)
5846 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005847 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005848 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5849 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5850 /* Leave out the attribute */
5851 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5852 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5853 NL80211_AUTHTYPE_SHARED_KEY);
5854 else
5855 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5856 NL80211_AUTHTYPE_OPEN_SYSTEM);
5857
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005858 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005859 ver = 0;
5860 if (params->wpa_version & WPA_PROTO_WPA)
5861 ver |= NL80211_WPA_VERSION_1;
5862 if (params->wpa_version & WPA_PROTO_RSN)
5863 ver |= NL80211_WPA_VERSION_2;
5864 if (ver)
5865 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5866
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005867 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
5868 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005869 num_suites = 0;
5870 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5871 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5872 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5873 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5874 if (num_suites) {
5875 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5876 num_suites * sizeof(u32), suites);
5877 }
5878
5879 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5880 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5881 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5882
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005883 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
5884 params->pairwise_ciphers);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005885 num_suites = 0;
5886 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5887 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005888 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
5889 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005890 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5891 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5892 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5893 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5894 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5895 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5896 if (num_suites) {
5897 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5898 num_suites * sizeof(u32), suites);
5899 }
5900
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005901 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
5902 params->group_cipher);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005903 switch (params->group_cipher) {
5904 case WPA_CIPHER_CCMP:
5905 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5906 WLAN_CIPHER_SUITE_CCMP);
5907 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005908 case WPA_CIPHER_GCMP:
5909 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5910 WLAN_CIPHER_SUITE_GCMP);
5911 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005912 case WPA_CIPHER_TKIP:
5913 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5914 WLAN_CIPHER_SUITE_TKIP);
5915 break;
5916 case WPA_CIPHER_WEP104:
5917 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5918 WLAN_CIPHER_SUITE_WEP104);
5919 break;
5920 case WPA_CIPHER_WEP40:
5921 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5922 WLAN_CIPHER_SUITE_WEP40);
5923 break;
5924 }
5925
5926 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005927 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
5928 params->beacon_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005929 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5930 wpabuf_head(params->beacon_ies));
5931 }
5932 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005933 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
5934 params->proberesp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005935 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5936 wpabuf_len(params->proberesp_ies),
5937 wpabuf_head(params->proberesp_ies));
5938 }
5939 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005940 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
5941 params->assocresp_ies);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005942 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5943 wpabuf_len(params->assocresp_ies),
5944 wpabuf_head(params->assocresp_ies));
5945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005946
Dmitry Shmidt04949592012-07-19 12:16:46 -07005947 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005948 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
5949 params->ap_max_inactivity);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005950 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5951 params->ap_max_inactivity);
5952 }
5953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005954 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5955 if (ret) {
5956 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5957 ret, strerror(-ret));
5958 } else {
5959 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005960 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5961 params->short_slot_time, params->ht_opmode,
5962 params->isolate, params->basic_rates);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005963 }
5964 return ret;
5965 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005966 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005967 return -ENOBUFS;
5968}
5969
5970
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005971static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005972 struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005973{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005974 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975 struct nl_msg *msg;
5976 int ret;
5977
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005978 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
5979 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
5980 freq->freq, freq->ht_enabled, freq->vht_enabled,
5981 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 msg = nlmsg_alloc();
5983 if (!msg)
5984 return -1;
5985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005986 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987
5988 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005989 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
5990 if (freq->vht_enabled) {
5991 switch (freq->bandwidth) {
5992 case 20:
5993 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5994 NL80211_CHAN_WIDTH_20);
5995 break;
5996 case 40:
5997 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5998 NL80211_CHAN_WIDTH_40);
5999 break;
6000 case 80:
6001 if (freq->center_freq2)
6002 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6003 NL80211_CHAN_WIDTH_80P80);
6004 else
6005 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6006 NL80211_CHAN_WIDTH_80);
6007 break;
6008 case 160:
6009 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6010 NL80211_CHAN_WIDTH_160);
6011 break;
6012 default:
6013 return -1;
6014 }
6015 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6016 if (freq->center_freq2)
6017 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6018 freq->center_freq2);
6019 } else if (freq->ht_enabled) {
6020 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021 case -1:
6022 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6023 NL80211_CHAN_HT40MINUS);
6024 break;
6025 case 1:
6026 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6027 NL80211_CHAN_HT40PLUS);
6028 break;
6029 default:
6030 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6031 NL80211_CHAN_HT20);
6032 break;
6033 }
6034 }
6035
6036 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006037 msg = NULL;
6038 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006039 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006041 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006043 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006045 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 return -1;
6047}
6048
6049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006050static u32 sta_flags_nl80211(int flags)
6051{
6052 u32 f = 0;
6053
6054 if (flags & WPA_STA_AUTHORIZED)
6055 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6056 if (flags & WPA_STA_WMM)
6057 f |= BIT(NL80211_STA_FLAG_WME);
6058 if (flags & WPA_STA_SHORT_PREAMBLE)
6059 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6060 if (flags & WPA_STA_MFP)
6061 f |= BIT(NL80211_STA_FLAG_MFP);
6062 if (flags & WPA_STA_TDLS_PEER)
6063 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
6064
6065 return f;
6066}
6067
6068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069static int wpa_driver_nl80211_sta_add(void *priv,
6070 struct hostapd_sta_add_params *params)
6071{
6072 struct i802_bss *bss = priv;
6073 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006074 struct nl_msg *msg, *wme = NULL;
6075 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076 int ret = -ENOBUFS;
6077
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006078 if ((params->flags & WPA_STA_TDLS_PEER) &&
6079 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6080 return -EOPNOTSUPP;
6081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006082 msg = nlmsg_alloc();
6083 if (!msg)
6084 return -ENOMEM;
6085
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006086 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6087 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006088 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6089 NL80211_CMD_NEW_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090
6091 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6092 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6094 params->supp_rates);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006095 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
6096 params->supp_rates_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006097 if (!params->set) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006098 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006099 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006100 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
6101 params->listen_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006102 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6103 params->listen_interval);
6104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105 if (params->ht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006106 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
6107 (u8 *) params->ht_capabilities,
6108 sizeof(*params->ht_capabilities));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
6110 sizeof(*params->ht_capabilities),
6111 params->ht_capabilities);
6112 }
6113
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006114 if (params->vht_capabilities) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006115 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
6116 (u8 *) params->vht_capabilities,
6117 sizeof(*params->vht_capabilities));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006118 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6119 sizeof(*params->vht_capabilities),
6120 params->vht_capabilities);
6121 }
6122
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006123 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
6124 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6125
6126 if (params->ext_capab) {
6127 wpa_hexdump(MSG_DEBUG, " * ext_capab",
6128 params->ext_capab, params->ext_capab_len);
6129 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6130 params->ext_capab_len, params->ext_capab);
6131 }
6132
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006133 os_memset(&upd, 0, sizeof(upd));
6134 upd.mask = sta_flags_nl80211(params->flags);
6135 upd.set = upd.mask;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006136 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
6137 upd.set, upd.mask);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006138 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6139
6140 if (params->flags & WPA_STA_WMM) {
6141 wme = nlmsg_alloc();
6142 if (!wme)
6143 goto nla_put_failure;
6144
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006145 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006146 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
6147 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
6148 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006149 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006150 WMM_QOSINFO_STA_SP_MASK);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006151 if (nla_put_nested(msg, NL80211_ATTR_STA_WME, wme) < 0)
6152 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006153 }
6154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006155 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006156 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006157 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006158 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6159 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6160 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161 if (ret == -EEXIST)
6162 ret = 0;
6163 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006164 nlmsg_free(wme);
6165 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006166 return ret;
6167}
6168
6169
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006170static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006171{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 struct wpa_driver_nl80211_data *drv = bss->drv;
6173 struct nl_msg *msg;
6174 int ret;
6175
6176 msg = nlmsg_alloc();
6177 if (!msg)
6178 return -ENOMEM;
6179
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006180 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181
6182 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6183 if_nametoindex(bss->ifname));
6184 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6185
6186 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6187 if (ret == -ENOENT)
6188 return 0;
6189 return ret;
6190 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006192 return -ENOBUFS;
6193}
6194
6195
6196static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6197 int ifidx)
6198{
6199 struct nl_msg *msg;
6200
6201 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203 /* stop listening for EAPOL on this interface */
6204 del_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205
6206 msg = nlmsg_alloc();
6207 if (!msg)
6208 goto nla_put_failure;
6209
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006210 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6212
6213 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6214 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006215 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006217 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6219}
6220
6221
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006222static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6223{
6224 switch (mode) {
6225 case NL80211_IFTYPE_ADHOC:
6226 return "ADHOC";
6227 case NL80211_IFTYPE_STATION:
6228 return "STATION";
6229 case NL80211_IFTYPE_AP:
6230 return "AP";
6231 case NL80211_IFTYPE_MONITOR:
6232 return "MONITOR";
6233 case NL80211_IFTYPE_P2P_CLIENT:
6234 return "P2P_CLIENT";
6235 case NL80211_IFTYPE_P2P_GO:
6236 return "P2P_GO";
6237 default:
6238 return "unknown";
6239 }
6240}
6241
6242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6244 const char *ifname,
6245 enum nl80211_iftype iftype,
6246 const u8 *addr, int wds)
6247{
6248 struct nl_msg *msg, *flags = NULL;
6249 int ifidx;
6250 int ret = -ENOBUFS;
6251
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006252 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6253 iftype, nl80211_iftype_str(iftype));
6254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255 msg = nlmsg_alloc();
6256 if (!msg)
6257 return -1;
6258
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006259 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006260 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6261 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6262 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6263
6264 if (iftype == NL80211_IFTYPE_MONITOR) {
6265 int err;
6266
6267 flags = nlmsg_alloc();
6268 if (!flags)
6269 goto nla_put_failure;
6270
6271 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
6272
6273 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
6274
6275 nlmsg_free(flags);
6276
6277 if (err)
6278 goto nla_put_failure;
6279 } else if (wds) {
6280 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
6281 }
6282
6283 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006284 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006285 if (ret) {
6286 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006287 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6289 ifname, ret, strerror(-ret));
6290 return ret;
6291 }
6292
6293 ifidx = if_nametoindex(ifname);
6294 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6295 ifname, ifidx);
6296
6297 if (ifidx <= 0)
6298 return -1;
6299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300 /* start listening for EAPOL on this interface */
6301 add_ifidx(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302
6303 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006304 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006305 nl80211_remove_iface(drv, ifidx);
6306 return -1;
6307 }
6308
6309 return ifidx;
6310}
6311
6312
6313static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6314 const char *ifname, enum nl80211_iftype iftype,
6315 const u8 *addr, int wds)
6316{
6317 int ret;
6318
6319 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
6320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006321 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322 if (ret == -ENFILE && if_nametoindex(ifname)) {
6323 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6324
6325 /* Try to remove the interface that was already there. */
6326 nl80211_remove_iface(drv, if_nametoindex(ifname));
6327
6328 /* Try to create the interface again */
6329 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6330 wds);
6331 }
6332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006333 if (ret >= 0 && is_p2p_interface(iftype))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 nl80211_disable_11b_rates(drv, ret, 1);
6335
6336 return ret;
6337}
6338
6339
6340static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6341{
6342 struct ieee80211_hdr *hdr;
6343 u16 fc;
6344 union wpa_event_data event;
6345
6346 hdr = (struct ieee80211_hdr *) buf;
6347 fc = le_to_host16(hdr->frame_control);
6348
6349 os_memset(&event, 0, sizeof(event));
6350 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6351 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6352 event.tx_status.dst = hdr->addr1;
6353 event.tx_status.data = buf;
6354 event.tx_status.data_len = len;
6355 event.tx_status.ack = ok;
6356 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
6357}
6358
6359
6360static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
6361 u8 *buf, size_t len)
6362{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006363 struct ieee80211_hdr *hdr = (void *)buf;
6364 u16 fc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006365 union wpa_event_data event;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006366
6367 if (len < sizeof(*hdr))
6368 return;
6369
6370 fc = le_to_host16(hdr->frame_control);
6371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006372 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006373 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6374 event.rx_from_unknown.addr = hdr->addr2;
6375 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6376 (WLAN_FC_FROMDS | WLAN_FC_TODS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006377 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
6378}
6379
6380
6381static void handle_frame(struct wpa_driver_nl80211_data *drv,
6382 u8 *buf, size_t len, int datarate, int ssi_signal)
6383{
6384 struct ieee80211_hdr *hdr;
6385 u16 fc;
6386 union wpa_event_data event;
6387
6388 hdr = (struct ieee80211_hdr *) buf;
6389 fc = le_to_host16(hdr->frame_control);
6390
6391 switch (WLAN_FC_GET_TYPE(fc)) {
6392 case WLAN_FC_TYPE_MGMT:
6393 os_memset(&event, 0, sizeof(event));
6394 event.rx_mgmt.frame = buf;
6395 event.rx_mgmt.frame_len = len;
6396 event.rx_mgmt.datarate = datarate;
6397 event.rx_mgmt.ssi_signal = ssi_signal;
6398 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
6399 break;
6400 case WLAN_FC_TYPE_CTRL:
6401 /* can only get here with PS-Poll frames */
6402 wpa_printf(MSG_DEBUG, "CTRL");
6403 from_unknown_sta(drv, buf, len);
6404 break;
6405 case WLAN_FC_TYPE_DATA:
6406 from_unknown_sta(drv, buf, len);
6407 break;
6408 }
6409}
6410
6411
6412static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6413{
6414 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6415 int len;
6416 unsigned char buf[3000];
6417 struct ieee80211_radiotap_iterator iter;
6418 int ret;
6419 int datarate = 0, ssi_signal = 0;
6420 int injected = 0, failed = 0, rxflags = 0;
6421
6422 len = recv(sock, buf, sizeof(buf), 0);
6423 if (len < 0) {
6424 perror("recv");
6425 return;
6426 }
6427
6428 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6429 printf("received invalid radiotap frame\n");
6430 return;
6431 }
6432
6433 while (1) {
6434 ret = ieee80211_radiotap_iterator_next(&iter);
6435 if (ret == -ENOENT)
6436 break;
6437 if (ret) {
6438 printf("received invalid radiotap frame (%d)\n", ret);
6439 return;
6440 }
6441 switch (iter.this_arg_index) {
6442 case IEEE80211_RADIOTAP_FLAGS:
6443 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6444 len -= 4;
6445 break;
6446 case IEEE80211_RADIOTAP_RX_FLAGS:
6447 rxflags = 1;
6448 break;
6449 case IEEE80211_RADIOTAP_TX_FLAGS:
6450 injected = 1;
6451 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6452 IEEE80211_RADIOTAP_F_TX_FAIL;
6453 break;
6454 case IEEE80211_RADIOTAP_DATA_RETRIES:
6455 break;
6456 case IEEE80211_RADIOTAP_CHANNEL:
6457 /* TODO: convert from freq/flags to channel number */
6458 break;
6459 case IEEE80211_RADIOTAP_RATE:
6460 datarate = *iter.this_arg * 5;
6461 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006462 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6463 ssi_signal = (s8) *iter.this_arg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464 break;
6465 }
6466 }
6467
6468 if (rxflags && injected)
6469 return;
6470
6471 if (!injected)
6472 handle_frame(drv, buf + iter.max_length,
6473 len - iter.max_length, datarate, ssi_signal);
6474 else
6475 handle_tx_callback(drv->ctx, buf + iter.max_length,
6476 len - iter.max_length, !failed);
6477}
6478
6479
6480/*
6481 * we post-process the filter code later and rewrite
6482 * this to the offset to the last instruction
6483 */
6484#define PASS 0xFF
6485#define FAIL 0xFE
6486
6487static struct sock_filter msock_filter_insns[] = {
6488 /*
6489 * do a little-endian load of the radiotap length field
6490 */
6491 /* load lower byte into A */
6492 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6493 /* put it into X (== index register) */
6494 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6495 /* load upper byte into A */
6496 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6497 /* left-shift it by 8 */
6498 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6499 /* or with X */
6500 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6501 /* put result into X */
6502 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6503
6504 /*
6505 * Allow management frames through, this also gives us those
6506 * management frames that we sent ourselves with status
6507 */
6508 /* load the lower byte of the IEEE 802.11 frame control field */
6509 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6510 /* mask off frame type and version */
6511 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6512 /* accept frame if it's both 0, fall through otherwise */
6513 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6514
6515 /*
6516 * TODO: add a bit to radiotap RX flags that indicates
6517 * that the sending station is not associated, then
6518 * add a filter here that filters on our DA and that flag
6519 * to allow us to deauth frames to that bad station.
6520 *
6521 * For now allow all To DS data frames through.
6522 */
6523 /* load the IEEE 802.11 frame control field */
6524 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6525 /* mask off frame type, version and DS status */
6526 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6527 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6528 */
6529 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6530
6531#if 0
6532 /*
6533 * drop non-data frames
6534 */
6535 /* load the lower byte of the frame control field */
6536 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6537 /* mask off QoS bit */
6538 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6539 /* drop non-data frames */
6540 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
6541#endif
6542 /* load the upper byte of the frame control field */
6543 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
6544 /* mask off toDS/fromDS */
6545 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
6546 /* accept WDS frames */
6547 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
6548
6549 /*
6550 * add header length to index
6551 */
6552 /* load the lower byte of the frame control field */
6553 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6554 /* mask off QoS bit */
6555 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6556 /* right shift it by 6 to give 0 or 2 */
6557 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6558 /* add data frame header length */
6559 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6560 /* add index, was start of 802.11 header */
6561 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6562 /* move to index, now start of LL header */
6563 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6564
6565 /*
6566 * Accept empty data frames, we use those for
6567 * polling activity.
6568 */
6569 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6570 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6571
6572 /*
6573 * Accept EAPOL frames
6574 */
6575 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6576 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6577 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6578 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6579
6580 /* keep these last two statements or change the code below */
6581 /* return 0 == "DROP" */
6582 BPF_STMT(BPF_RET | BPF_K, 0),
6583 /* return ~0 == "keep all" */
6584 BPF_STMT(BPF_RET | BPF_K, ~0),
6585};
6586
6587static struct sock_fprog msock_filter = {
6588 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6589 .filter = msock_filter_insns,
6590};
6591
6592
6593static int add_monitor_filter(int s)
6594{
6595 int idx;
6596
6597 /* rewrite all PASS/FAIL jump offsets */
6598 for (idx = 0; idx < msock_filter.len; idx++) {
6599 struct sock_filter *insn = &msock_filter_insns[idx];
6600
6601 if (BPF_CLASS(insn->code) == BPF_JMP) {
6602 if (insn->code == (BPF_JMP|BPF_JA)) {
6603 if (insn->k == PASS)
6604 insn->k = msock_filter.len - idx - 2;
6605 else if (insn->k == FAIL)
6606 insn->k = msock_filter.len - idx - 3;
6607 }
6608
6609 if (insn->jt == PASS)
6610 insn->jt = msock_filter.len - idx - 2;
6611 else if (insn->jt == FAIL)
6612 insn->jt = msock_filter.len - idx - 3;
6613
6614 if (insn->jf == PASS)
6615 insn->jf = msock_filter.len - idx - 2;
6616 else if (insn->jf == FAIL)
6617 insn->jf = msock_filter.len - idx - 3;
6618 }
6619 }
6620
6621 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6622 &msock_filter, sizeof(msock_filter))) {
6623 perror("SO_ATTACH_FILTER");
6624 return -1;
6625 }
6626
6627 return 0;
6628}
6629
6630
6631static void nl80211_remove_monitor_interface(
6632 struct wpa_driver_nl80211_data *drv)
6633{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006634 drv->monitor_refcount--;
6635 if (drv->monitor_refcount > 0)
6636 return;
6637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006638 if (drv->monitor_ifidx >= 0) {
6639 nl80211_remove_iface(drv, drv->monitor_ifidx);
6640 drv->monitor_ifidx = -1;
6641 }
6642 if (drv->monitor_sock >= 0) {
6643 eloop_unregister_read_sock(drv->monitor_sock);
6644 close(drv->monitor_sock);
6645 drv->monitor_sock = -1;
6646 }
6647}
6648
6649
6650static int
6651nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6652{
6653 char buf[IFNAMSIZ];
6654 struct sockaddr_ll ll;
6655 int optval;
6656 socklen_t optlen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006657
6658 if (drv->monitor_ifidx >= 0) {
6659 drv->monitor_refcount++;
6660 return 0;
6661 }
6662
6663 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6664 /*
6665 * P2P interface name is of the format p2p-%s-%d. For monitor
6666 * interface name corresponding to P2P GO, replace "p2p-" with
6667 * "mon-" to retain the same interface name length and to
6668 * indicate that it is a monitor interface.
6669 */
6670 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6671 } else {
6672 /* Non-P2P interface with AP functionality. */
6673 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6674 }
6675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676 buf[IFNAMSIZ - 1] = '\0';
6677
6678 drv->monitor_ifidx =
6679 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6680 0);
6681
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006682 if (drv->monitor_ifidx == -EOPNOTSUPP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006683 /*
6684 * This is backward compatibility for a few versions of
6685 * the kernel only that didn't advertise the right
6686 * attributes for the only driver that then supported
6687 * AP mode w/o monitor -- ath6kl.
6688 */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006689 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6690 "monitor interface type - try to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006691 drv->device_ap_sme = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07006692 }
6693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 if (drv->monitor_ifidx < 0)
6695 return -1;
6696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006697 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006698 goto error;
6699
6700 memset(&ll, 0, sizeof(ll));
6701 ll.sll_family = AF_PACKET;
6702 ll.sll_ifindex = drv->monitor_ifidx;
6703 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6704 if (drv->monitor_sock < 0) {
6705 perror("socket[PF_PACKET,SOCK_RAW]");
6706 goto error;
6707 }
6708
6709 if (add_monitor_filter(drv->monitor_sock)) {
6710 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6711 "interface; do filtering in user space");
6712 /* This works, but will cost in performance. */
6713 }
6714
6715 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6716 perror("monitor socket bind");
6717 goto error;
6718 }
6719
6720 optlen = sizeof(optval);
6721 optval = 20;
6722 if (setsockopt
6723 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6724 perror("Failed to set socket priority");
6725 goto error;
6726 }
6727
6728 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6729 drv, NULL)) {
6730 printf("Could not register monitor read socket\n");
6731 goto error;
6732 }
6733
6734 return 0;
6735 error:
6736 nl80211_remove_monitor_interface(drv);
6737 return -1;
6738}
6739
6740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006741static int nl80211_setup_ap(struct i802_bss *bss)
6742{
6743 struct wpa_driver_nl80211_data *drv = bss->drv;
6744
6745 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6746 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6747
6748 /*
6749 * Disable Probe Request reporting unless we need it in this way for
6750 * devices that include the AP SME, in the other case (unless using
6751 * monitor iface) we'll get it through the nl_mgmt socket instead.
6752 */
6753 if (!drv->device_ap_sme)
6754 wpa_driver_nl80211_probe_req_report(bss, 0);
6755
6756 if (!drv->device_ap_sme && !drv->use_monitor)
6757 if (nl80211_mgmt_subscribe_ap(bss))
6758 return -1;
6759
6760 if (drv->device_ap_sme && !drv->use_monitor)
6761 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6762 return -1;
6763
6764 if (!drv->device_ap_sme && drv->use_monitor &&
6765 nl80211_create_monitor_interface(drv) &&
6766 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006767 return -1;
6768
6769#ifdef ANDROID_P2P
6770 if (drv->device_ap_sme && drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006771 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6772 return -1;
6773
6774 if (drv->use_monitor &&
6775 nl80211_create_monitor_interface(drv))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006776 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006777#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006778
6779 if (drv->device_ap_sme &&
6780 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6781 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6782 "Probe Request frame reporting in AP mode");
6783 /* Try to survive without this */
6784 }
6785
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006786 return 0;
6787}
6788
6789
6790static void nl80211_teardown_ap(struct i802_bss *bss)
6791{
6792 struct wpa_driver_nl80211_data *drv = bss->drv;
6793
6794 if (drv->device_ap_sme) {
6795 wpa_driver_nl80211_probe_req_report(bss, 0);
6796 if (!drv->use_monitor)
6797 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6798 } else if (drv->use_monitor)
6799 nl80211_remove_monitor_interface(drv);
6800 else
6801 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6802
6803 bss->beacon_set = 0;
6804}
6805
6806
6807static int nl80211_send_eapol_data(struct i802_bss *bss,
6808 const u8 *addr, const u8 *data,
6809 size_t data_len)
6810{
6811 struct sockaddr_ll ll;
6812 int ret;
6813
6814 if (bss->drv->eapol_tx_sock < 0) {
6815 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6816 return -1;
6817 }
6818
6819 os_memset(&ll, 0, sizeof(ll));
6820 ll.sll_family = AF_PACKET;
6821 ll.sll_ifindex = bss->ifindex;
6822 ll.sll_protocol = htons(ETH_P_PAE);
6823 ll.sll_halen = ETH_ALEN;
6824 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6825 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6826 (struct sockaddr *) &ll, sizeof(ll));
6827 if (ret < 0)
6828 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6829 strerror(errno));
6830
6831 return ret;
6832}
6833
6834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6836
6837static int wpa_driver_nl80211_hapd_send_eapol(
6838 void *priv, const u8 *addr, const u8 *data,
6839 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6840{
6841 struct i802_bss *bss = priv;
6842 struct wpa_driver_nl80211_data *drv = bss->drv;
6843 struct ieee80211_hdr *hdr;
6844 size_t len;
6845 u8 *pos;
6846 int res;
6847 int qos = flags & WPA_STA_WMM;
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006848#ifndef ANDROID_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006849 if (drv->device_ap_sme || !drv->use_monitor)
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07006850#else
6851 if (drv->device_ap_sme && !drv->use_monitor)
6852#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006853 return nl80211_send_eapol_data(bss, addr, data, data_len);
6854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6856 data_len;
6857 hdr = os_zalloc(len);
6858 if (hdr == NULL) {
6859 printf("malloc() failed for i802_send_data(len=%lu)\n",
6860 (unsigned long) len);
6861 return -1;
6862 }
6863
6864 hdr->frame_control =
6865 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6866 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6867 if (encrypt)
6868 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6869 if (qos) {
6870 hdr->frame_control |=
6871 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6872 }
6873
6874 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6875 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6876 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6877 pos = (u8 *) (hdr + 1);
6878
6879 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006880 /* Set highest priority in QoS header */
6881 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006882 pos[1] = 0;
6883 pos += 2;
6884 }
6885
6886 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6887 pos += sizeof(rfc1042_header);
6888 WPA_PUT_BE16(pos, ETH_P_PAE);
6889 pos += 2;
6890 memcpy(pos, data, data_len);
6891
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006892 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6893 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894 if (res < 0) {
6895 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6896 "failed: %d (%s)",
6897 (unsigned long) len, errno, strerror(errno));
6898 }
6899 os_free(hdr);
6900
6901 return res;
6902}
6903
6904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6906 int total_flags,
6907 int flags_or, int flags_and)
6908{
6909 struct i802_bss *bss = priv;
6910 struct wpa_driver_nl80211_data *drv = bss->drv;
6911 struct nl_msg *msg, *flags = NULL;
6912 struct nl80211_sta_flag_update upd;
6913
6914 msg = nlmsg_alloc();
6915 if (!msg)
6916 return -ENOMEM;
6917
6918 flags = nlmsg_alloc();
6919 if (!flags) {
6920 nlmsg_free(msg);
6921 return -ENOMEM;
6922 }
6923
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006924 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925
6926 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6927 if_nametoindex(bss->ifname));
6928 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6929
6930 /*
6931 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6932 * can be removed eventually.
6933 */
6934 if (total_flags & WPA_STA_AUTHORIZED)
6935 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
6936
6937 if (total_flags & WPA_STA_WMM)
6938 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
6939
6940 if (total_flags & WPA_STA_SHORT_PREAMBLE)
6941 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
6942
6943 if (total_flags & WPA_STA_MFP)
6944 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
6945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006946 if (total_flags & WPA_STA_TDLS_PEER)
6947 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
6948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
6950 goto nla_put_failure;
6951
6952 os_memset(&upd, 0, sizeof(upd));
6953 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6954 upd.set = sta_flags_nl80211(flags_or);
6955 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6956
6957 nlmsg_free(flags);
6958
6959 return send_and_recv_msgs(drv, msg, NULL, NULL);
6960 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006961 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962 nlmsg_free(flags);
6963 return -ENOBUFS;
6964}
6965
6966
6967static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6968 struct wpa_driver_associate_params *params)
6969{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006970 enum nl80211_iftype nlmode, old_mode;
6971 struct hostapd_freq_params freq = {
6972 .freq = params->freq,
6973 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006974
6975 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6977 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006978 nlmode = NL80211_IFTYPE_P2P_GO;
6979 } else
6980 nlmode = NL80211_IFTYPE_AP;
6981
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006982 old_mode = drv->nlmode;
6983 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
6984 nl80211_remove_monitor_interface(drv);
6985 return -1;
6986 }
6987
6988 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
6989 if (old_mode != nlmode)
6990 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991 nl80211_remove_monitor_interface(drv);
6992 return -1;
6993 }
6994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995 return 0;
6996}
6997
6998
6999static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7000{
7001 struct nl_msg *msg;
7002 int ret = -1;
7003
7004 msg = nlmsg_alloc();
7005 if (!msg)
7006 return -1;
7007
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007008 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007009 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7010 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7011 msg = NULL;
7012 if (ret) {
7013 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7014 "(%s)", ret, strerror(-ret));
7015 goto nla_put_failure;
7016 }
7017
7018 ret = 0;
7019 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7020
7021nla_put_failure:
7022 nlmsg_free(msg);
7023 return ret;
7024}
7025
7026
7027static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7028 struct wpa_driver_associate_params *params)
7029{
7030 struct nl_msg *msg;
7031 int ret = -1;
7032 int count = 0;
7033
7034 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007036 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7037 NL80211_IFTYPE_ADHOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7039 "IBSS mode");
7040 return -1;
7041 }
7042
7043retry:
7044 msg = nlmsg_alloc();
7045 if (!msg)
7046 return -1;
7047
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007048 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7050
7051 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7052 goto nla_put_failure;
7053
7054 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7055 params->ssid, params->ssid_len);
7056 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7057 params->ssid);
7058 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7059 drv->ssid_len = params->ssid_len;
7060
7061 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7062 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7063
7064 ret = nl80211_set_conn_keys(params, msg);
7065 if (ret)
7066 goto nla_put_failure;
7067
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007068 if (params->bssid && params->fixed_bssid) {
7069 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
7070 MAC2STR(params->bssid));
7071 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7072 }
7073
7074 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7075 params->key_mgmt_suite == KEY_MGMT_PSK ||
7076 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7077 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7078 wpa_printf(MSG_DEBUG, " * control port");
7079 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7080 }
7081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082 if (params->wpa_ie) {
7083 wpa_hexdump(MSG_DEBUG,
7084 " * Extra IEs for Beacon/Probe Response frames",
7085 params->wpa_ie, params->wpa_ie_len);
7086 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7087 params->wpa_ie);
7088 }
7089
7090 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7091 msg = NULL;
7092 if (ret) {
7093 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7094 ret, strerror(-ret));
7095 count++;
7096 if (ret == -EALREADY && count == 1) {
7097 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7098 "forced leave");
7099 nl80211_leave_ibss(drv);
7100 nlmsg_free(msg);
7101 goto retry;
7102 }
7103
7104 goto nla_put_failure;
7105 }
7106 ret = 0;
7107 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7108
7109nla_put_failure:
7110 nlmsg_free(msg);
7111 return ret;
7112}
7113
7114
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007115static int wpa_driver_nl80211_try_connect(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116 struct wpa_driver_nl80211_data *drv,
7117 struct wpa_driver_associate_params *params)
7118{
7119 struct nl_msg *msg;
7120 enum nl80211_auth_type type;
7121 int ret = 0;
7122 int algs;
7123
7124 msg = nlmsg_alloc();
7125 if (!msg)
7126 return -1;
7127
7128 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007129 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130
7131 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7132 if (params->bssid) {
7133 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7134 MAC2STR(params->bssid));
7135 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7136 }
7137 if (params->freq) {
7138 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7139 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7140 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007141 if (params->bg_scan_period >= 0) {
7142 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7143 params->bg_scan_period);
7144 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7145 params->bg_scan_period);
7146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147 if (params->ssid) {
7148 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7149 params->ssid, params->ssid_len);
7150 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7151 params->ssid);
7152 if (params->ssid_len > sizeof(drv->ssid))
7153 goto nla_put_failure;
7154 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7155 drv->ssid_len = params->ssid_len;
7156 }
7157 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7158 if (params->wpa_ie)
7159 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7160 params->wpa_ie);
7161
7162 algs = 0;
7163 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7164 algs++;
7165 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7166 algs++;
7167 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7168 algs++;
7169 if (algs > 1) {
7170 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7171 "selection");
7172 goto skip_auth_type;
7173 }
7174
7175 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7176 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
7177 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7178 type = NL80211_AUTHTYPE_SHARED_KEY;
7179 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7180 type = NL80211_AUTHTYPE_NETWORK_EAP;
7181 else if (params->auth_alg & WPA_AUTH_ALG_FT)
7182 type = NL80211_AUTHTYPE_FT;
7183 else
7184 goto nla_put_failure;
7185
7186 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
7187 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7188
7189skip_auth_type:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007190 if (params->wpa_proto) {
7191 enum nl80211_wpa_versions ver = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007193 if (params->wpa_proto & WPA_PROTO_WPA)
7194 ver |= NL80211_WPA_VERSION_1;
7195 if (params->wpa_proto & WPA_PROTO_RSN)
7196 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007198 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7200 }
7201
7202 if (params->pairwise_suite != CIPHER_NONE) {
7203 int cipher;
7204
7205 switch (params->pairwise_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007206 case CIPHER_SMS4:
7207 cipher = WLAN_CIPHER_SUITE_SMS4;
7208 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209 case CIPHER_WEP40:
7210 cipher = WLAN_CIPHER_SUITE_WEP40;
7211 break;
7212 case CIPHER_WEP104:
7213 cipher = WLAN_CIPHER_SUITE_WEP104;
7214 break;
7215 case CIPHER_CCMP:
7216 cipher = WLAN_CIPHER_SUITE_CCMP;
7217 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007218 case CIPHER_GCMP:
7219 cipher = WLAN_CIPHER_SUITE_GCMP;
7220 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221 case CIPHER_TKIP:
7222 default:
7223 cipher = WLAN_CIPHER_SUITE_TKIP;
7224 break;
7225 }
7226 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7227 }
7228
7229 if (params->group_suite != CIPHER_NONE) {
7230 int cipher;
7231
7232 switch (params->group_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007233 case CIPHER_SMS4:
7234 cipher = WLAN_CIPHER_SUITE_SMS4;
7235 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236 case CIPHER_WEP40:
7237 cipher = WLAN_CIPHER_SUITE_WEP40;
7238 break;
7239 case CIPHER_WEP104:
7240 cipher = WLAN_CIPHER_SUITE_WEP104;
7241 break;
7242 case CIPHER_CCMP:
7243 cipher = WLAN_CIPHER_SUITE_CCMP;
7244 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007245 case CIPHER_GCMP:
7246 cipher = WLAN_CIPHER_SUITE_GCMP;
7247 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248 case CIPHER_TKIP:
7249 default:
7250 cipher = WLAN_CIPHER_SUITE_TKIP;
7251 break;
7252 }
7253 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7254 }
7255
7256 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007257 params->key_mgmt_suite == KEY_MGMT_PSK ||
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007258 params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7259 params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007260 params->key_mgmt_suite == KEY_MGMT_CCKM) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007261 int mgmt = WLAN_AKM_SUITE_PSK;
7262
7263 switch (params->key_mgmt_suite) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007264 case KEY_MGMT_CCKM:
7265 mgmt = WLAN_AKM_SUITE_CCKM;
7266 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007267 case KEY_MGMT_802_1X:
7268 mgmt = WLAN_AKM_SUITE_8021X;
7269 break;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007270 case KEY_MGMT_FT_802_1X:
7271 mgmt = WLAN_AKM_SUITE_FT_8021X;
7272 break;
7273 case KEY_MGMT_FT_PSK:
7274 mgmt = WLAN_AKM_SUITE_FT_PSK;
7275 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007276 case KEY_MGMT_PSK:
7277 default:
7278 mgmt = WLAN_AKM_SUITE_PSK;
7279 break;
7280 }
7281 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7282 }
7283
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007284#ifdef CONFIG_IEEE80211W
7285 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7286 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7287#endif /* CONFIG_IEEE80211W */
7288
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007289 if (params->disable_ht)
7290 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7291
7292 if (params->htcaps && params->htcaps_mask) {
7293 int sz = sizeof(struct ieee80211_ht_capabilities);
7294 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7295 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7296 params->htcaps_mask);
7297 }
7298
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007299#ifdef CONFIG_VHT_OVERRIDES
7300 if (params->disable_vht) {
7301 wpa_printf(MSG_DEBUG, " * VHT disabled");
7302 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7303 }
7304
7305 if (params->vhtcaps && params->vhtcaps_mask) {
7306 int sz = sizeof(struct ieee80211_vht_capabilities);
7307 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7308 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7309 params->vhtcaps_mask);
7310 }
7311#endif /* CONFIG_VHT_OVERRIDES */
7312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007313 ret = nl80211_set_conn_keys(params, msg);
7314 if (ret)
7315 goto nla_put_failure;
7316
7317 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7318 msg = NULL;
7319 if (ret) {
7320 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7321 "(%s)", ret, strerror(-ret));
7322 goto nla_put_failure;
7323 }
7324 ret = 0;
7325 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7326
7327nla_put_failure:
7328 nlmsg_free(msg);
7329 return ret;
7330
7331}
7332
7333
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007334static int wpa_driver_nl80211_connect(
7335 struct wpa_driver_nl80211_data *drv,
7336 struct wpa_driver_associate_params *params)
7337{
7338 int ret = wpa_driver_nl80211_try_connect(drv, params);
7339 if (ret == -EALREADY) {
7340 /*
7341 * cfg80211 does not currently accept new connections if
7342 * we are already connected. As a workaround, force
7343 * disconnection and try again.
7344 */
7345 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7346 "disconnecting before reassociation "
7347 "attempt");
7348 if (wpa_driver_nl80211_disconnect(
7349 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7350 return -1;
7351 /* Ignore the next local disconnect message. */
7352 drv->ignore_next_local_disconnect = 1;
7353 ret = wpa_driver_nl80211_try_connect(drv, params);
7354 }
7355 return ret;
7356}
7357
7358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359static int wpa_driver_nl80211_associate(
7360 void *priv, struct wpa_driver_associate_params *params)
7361{
7362 struct i802_bss *bss = priv;
7363 struct wpa_driver_nl80211_data *drv = bss->drv;
7364 int ret = -1;
7365 struct nl_msg *msg;
7366
7367 if (params->mode == IEEE80211_MODE_AP)
7368 return wpa_driver_nl80211_ap(drv, params);
7369
7370 if (params->mode == IEEE80211_MODE_IBSS)
7371 return wpa_driver_nl80211_ibss(drv, params);
7372
7373 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007374 enum nl80211_iftype nlmode = params->p2p ?
7375 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7376
7377 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007378 return -1;
7379 return wpa_driver_nl80211_connect(drv, params);
7380 }
7381
7382 drv->associated = 0;
7383
7384 msg = nlmsg_alloc();
7385 if (!msg)
7386 return -1;
7387
7388 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7389 drv->ifindex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007390 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007391
7392 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7393 if (params->bssid) {
7394 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7395 MAC2STR(params->bssid));
7396 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7397 }
7398 if (params->freq) {
7399 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7400 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7401 drv->assoc_freq = params->freq;
7402 } else
7403 drv->assoc_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007404 if (params->bg_scan_period >= 0) {
7405 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7406 params->bg_scan_period);
7407 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7408 params->bg_scan_period);
7409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007410 if (params->ssid) {
7411 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7412 params->ssid, params->ssid_len);
7413 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7414 params->ssid);
7415 if (params->ssid_len > sizeof(drv->ssid))
7416 goto nla_put_failure;
7417 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7418 drv->ssid_len = params->ssid_len;
7419 }
7420 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7421 if (params->wpa_ie)
7422 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7423 params->wpa_ie);
7424
7425 if (params->pairwise_suite != CIPHER_NONE) {
7426 int cipher;
7427
7428 switch (params->pairwise_suite) {
7429 case CIPHER_WEP40:
7430 cipher = WLAN_CIPHER_SUITE_WEP40;
7431 break;
7432 case CIPHER_WEP104:
7433 cipher = WLAN_CIPHER_SUITE_WEP104;
7434 break;
7435 case CIPHER_CCMP:
7436 cipher = WLAN_CIPHER_SUITE_CCMP;
7437 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007438 case CIPHER_GCMP:
7439 cipher = WLAN_CIPHER_SUITE_GCMP;
7440 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441 case CIPHER_TKIP:
7442 default:
7443 cipher = WLAN_CIPHER_SUITE_TKIP;
7444 break;
7445 }
7446 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
7447 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7448 }
7449
7450 if (params->group_suite != CIPHER_NONE) {
7451 int cipher;
7452
7453 switch (params->group_suite) {
7454 case CIPHER_WEP40:
7455 cipher = WLAN_CIPHER_SUITE_WEP40;
7456 break;
7457 case CIPHER_WEP104:
7458 cipher = WLAN_CIPHER_SUITE_WEP104;
7459 break;
7460 case CIPHER_CCMP:
7461 cipher = WLAN_CIPHER_SUITE_CCMP;
7462 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007463 case CIPHER_GCMP:
7464 cipher = WLAN_CIPHER_SUITE_GCMP;
7465 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 case CIPHER_TKIP:
7467 default:
7468 cipher = WLAN_CIPHER_SUITE_TKIP;
7469 break;
7470 }
7471 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
7472 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7473 }
7474
7475#ifdef CONFIG_IEEE80211W
7476 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7477 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7478#endif /* CONFIG_IEEE80211W */
7479
7480 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7481
7482 if (params->prev_bssid) {
7483 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7484 MAC2STR(params->prev_bssid));
7485 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7486 params->prev_bssid);
7487 }
7488
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007489 if (params->disable_ht)
7490 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7491
7492 if (params->htcaps && params->htcaps_mask) {
7493 int sz = sizeof(struct ieee80211_ht_capabilities);
7494 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7495 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7496 params->htcaps_mask);
7497 }
7498
Dmitry Shmidt2f023192013-03-12 12:44:17 -07007499#ifdef CONFIG_VHT_OVERRIDES
7500 if (params->disable_vht) {
7501 wpa_printf(MSG_DEBUG, " * VHT disabled");
7502 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7503 }
7504
7505 if (params->vhtcaps && params->vhtcaps_mask) {
7506 int sz = sizeof(struct ieee80211_vht_capabilities);
7507 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7508 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7509 params->vhtcaps_mask);
7510 }
7511#endif /* CONFIG_VHT_OVERRIDES */
7512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007513 if (params->p2p)
7514 wpa_printf(MSG_DEBUG, " * P2P group");
7515
7516 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7517 msg = NULL;
7518 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007519 wpa_dbg(drv->ctx, MSG_DEBUG,
7520 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7521 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522 nl80211_dump_scan(drv);
7523 goto nla_put_failure;
7524 }
7525 ret = 0;
7526 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7527 "successfully");
7528
7529nla_put_failure:
7530 nlmsg_free(msg);
7531 return ret;
7532}
7533
7534
7535static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007536 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537{
7538 struct nl_msg *msg;
7539 int ret = -ENOBUFS;
7540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007541 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7542 ifindex, mode, nl80211_iftype_str(mode));
7543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544 msg = nlmsg_alloc();
7545 if (!msg)
7546 return -ENOMEM;
7547
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007548 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7550 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7551
7552 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007553 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554 if (!ret)
7555 return 0;
7556nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007557 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7559 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7560 return ret;
7561}
7562
7563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007564static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7565 enum nl80211_iftype nlmode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007566{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007567 struct wpa_driver_nl80211_data *drv = bss->drv;
7568 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007569 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007570 int was_ap = is_ap_interface(drv->nlmode);
7571 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007573 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7574 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575 drv->nlmode = nlmode;
7576 ret = 0;
7577 goto done;
7578 }
7579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007580 if (res == -ENODEV)
7581 return -1;
7582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007583 if (nlmode == drv->nlmode) {
7584 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7585 "requested mode - ignore error");
7586 ret = 0;
7587 goto done; /* Already in the requested mode */
7588 }
7589
7590 /* mac80211 doesn't allow mode changes while the device is up, so
7591 * take the device down, try to set the mode again, and bring the
7592 * device back up.
7593 */
7594 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7595 "interface down");
7596 for (i = 0; i < 10; i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007597 res = linux_set_iface_flags(drv->global->ioctl_sock,
7598 bss->ifname, 0);
7599 if (res == -EACCES || res == -ENODEV)
7600 break;
7601 if (res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007602 /* Try to set the mode again while the interface is
7603 * down */
7604 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007605 if (ret == -EACCES)
7606 break;
7607 res = linux_set_iface_flags(drv->global->ioctl_sock,
7608 bss->ifname, 1);
7609 if (res && !ret)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007610 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007611 else if (ret != -EBUSY)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007612 break;
7613 } else
7614 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7615 "interface down");
7616 os_sleep(0, 100000);
7617 }
7618
7619 if (!ret) {
7620 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7621 "interface is down");
7622 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007623 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007624 }
7625
7626done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007627 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007628 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7629 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007630 return ret;
7631 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007632
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007633 if (is_p2p_interface(nlmode))
7634 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
7635 else if (drv->disabled_11b_rates)
7636 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
7637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007638 if (is_ap_interface(nlmode)) {
7639 nl80211_mgmt_unsubscribe(bss, "start AP");
7640 /* Setup additional AP mode functionality if needed */
7641 if (nl80211_setup_ap(bss))
7642 return -1;
7643 } else if (was_ap) {
7644 /* Remove additional AP mode functionality */
7645 nl80211_teardown_ap(bss);
7646 } else {
7647 nl80211_mgmt_unsubscribe(bss, "mode change");
7648 }
7649
Dmitry Shmidt04949592012-07-19 12:16:46 -07007650 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007651 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7652 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7653 "frame processing - ignore for now");
7654
7655 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007656}
7657
7658
7659static int wpa_driver_nl80211_get_capa(void *priv,
7660 struct wpa_driver_capa *capa)
7661{
7662 struct i802_bss *bss = priv;
7663 struct wpa_driver_nl80211_data *drv = bss->drv;
7664 if (!drv->has_capability)
7665 return -1;
7666 os_memcpy(capa, &drv->capa, sizeof(*capa));
7667 return 0;
7668}
7669
7670
7671static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7672{
7673 struct i802_bss *bss = priv;
7674 struct wpa_driver_nl80211_data *drv = bss->drv;
7675
7676 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7677 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7678 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007679 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007680 state ? IF_OPER_UP : IF_OPER_DORMANT);
7681}
7682
7683
7684static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7685{
7686 struct i802_bss *bss = priv;
7687 struct wpa_driver_nl80211_data *drv = bss->drv;
7688 struct nl_msg *msg;
7689 struct nl80211_sta_flag_update upd;
7690
7691 msg = nlmsg_alloc();
7692 if (!msg)
7693 return -ENOMEM;
7694
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007695 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007696
7697 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7698 if_nametoindex(bss->ifname));
7699 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7700
7701 os_memset(&upd, 0, sizeof(upd));
7702 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7703 if (authorized)
7704 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7705 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7706
7707 return send_and_recv_msgs(drv, msg, NULL, NULL);
7708 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007709 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710 return -ENOBUFS;
7711}
7712
7713
Jouni Malinen75ecf522011-06-27 15:19:46 -07007714/* Set kernel driver on given frequency (MHz) */
7715static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007716{
Jouni Malinen75ecf522011-06-27 15:19:46 -07007717 struct i802_bss *bss = priv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007718 return wpa_driver_nl80211_set_freq(bss, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007719}
7720
7721
Jouni Malinen75ecf522011-06-27 15:19:46 -07007722#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723
7724static inline int min_int(int a, int b)
7725{
7726 if (a < b)
7727 return a;
7728 return b;
7729}
7730
7731
7732static int get_key_handler(struct nl_msg *msg, void *arg)
7733{
7734 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7735 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7736
7737 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7738 genlmsg_attrlen(gnlh, 0), NULL);
7739
7740 /*
7741 * TODO: validate the key index and mac address!
7742 * Otherwise, there's a race condition as soon as
7743 * the kernel starts sending key notifications.
7744 */
7745
7746 if (tb[NL80211_ATTR_KEY_SEQ])
7747 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7748 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7749 return NL_SKIP;
7750}
7751
7752
7753static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7754 int idx, u8 *seq)
7755{
7756 struct i802_bss *bss = priv;
7757 struct wpa_driver_nl80211_data *drv = bss->drv;
7758 struct nl_msg *msg;
7759
7760 msg = nlmsg_alloc();
7761 if (!msg)
7762 return -ENOMEM;
7763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007764 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007765
7766 if (addr)
7767 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7768 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7769 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7770
7771 memset(seq, 0, 6);
7772
7773 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7774 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007775 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007776 return -ENOBUFS;
7777}
7778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007779
7780static int i802_set_rts(void *priv, int rts)
7781{
7782 struct i802_bss *bss = priv;
7783 struct wpa_driver_nl80211_data *drv = bss->drv;
7784 struct nl_msg *msg;
7785 int ret = -ENOBUFS;
7786 u32 val;
7787
7788 msg = nlmsg_alloc();
7789 if (!msg)
7790 return -ENOMEM;
7791
7792 if (rts >= 2347)
7793 val = (u32) -1;
7794 else
7795 val = rts;
7796
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007797 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007798 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7799 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7800
7801 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007802 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 if (!ret)
7804 return 0;
7805nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007806 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007807 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7808 "%d (%s)", rts, ret, strerror(-ret));
7809 return ret;
7810}
7811
7812
7813static int i802_set_frag(void *priv, int frag)
7814{
7815 struct i802_bss *bss = priv;
7816 struct wpa_driver_nl80211_data *drv = bss->drv;
7817 struct nl_msg *msg;
7818 int ret = -ENOBUFS;
7819 u32 val;
7820
7821 msg = nlmsg_alloc();
7822 if (!msg)
7823 return -ENOMEM;
7824
7825 if (frag >= 2346)
7826 val = (u32) -1;
7827 else
7828 val = frag;
7829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007830 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007831 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7832 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7833
7834 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007835 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007836 if (!ret)
7837 return 0;
7838nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007839 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007840 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7841 "%d: %d (%s)", frag, ret, strerror(-ret));
7842 return ret;
7843}
7844
7845
7846static int i802_flush(void *priv)
7847{
7848 struct i802_bss *bss = priv;
7849 struct wpa_driver_nl80211_data *drv = bss->drv;
7850 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007851 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852
7853 msg = nlmsg_alloc();
7854 if (!msg)
7855 return -1;
7856
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007857 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858
7859 /*
7860 * XXX: FIX! this needs to flush all VLANs too
7861 */
7862 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7863 if_nametoindex(bss->ifname));
7864
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007865 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7866 if (res) {
7867 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7868 "(%s)", res, strerror(-res));
7869 }
7870 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007871 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007872 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007873 return -ENOBUFS;
7874}
7875
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007876#endif /* HOSTAPD || CONFIG_AP */
7877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007878
7879static int get_sta_handler(struct nl_msg *msg, void *arg)
7880{
7881 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7882 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7883 struct hostap_sta_driver_data *data = arg;
7884 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7885 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7886 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7887 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7888 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7889 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7890 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007891 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007892 };
7893
7894 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7895 genlmsg_attrlen(gnlh, 0), NULL);
7896
7897 /*
7898 * TODO: validate the interface and mac address!
7899 * Otherwise, there's a race condition as soon as
7900 * the kernel starts sending station notifications.
7901 */
7902
7903 if (!tb[NL80211_ATTR_STA_INFO]) {
7904 wpa_printf(MSG_DEBUG, "sta stats missing!");
7905 return NL_SKIP;
7906 }
7907 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7908 tb[NL80211_ATTR_STA_INFO],
7909 stats_policy)) {
7910 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7911 return NL_SKIP;
7912 }
7913
7914 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7915 data->inactive_msec =
7916 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7917 if (stats[NL80211_STA_INFO_RX_BYTES])
7918 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7919 if (stats[NL80211_STA_INFO_TX_BYTES])
7920 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7921 if (stats[NL80211_STA_INFO_RX_PACKETS])
7922 data->rx_packets =
7923 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7924 if (stats[NL80211_STA_INFO_TX_PACKETS])
7925 data->tx_packets =
7926 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007927 if (stats[NL80211_STA_INFO_TX_FAILED])
7928 data->tx_retry_failed =
7929 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007930
7931 return NL_SKIP;
7932}
7933
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007934static int i802_read_sta_data(struct i802_bss *bss,
7935 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936 const u8 *addr)
7937{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938 struct wpa_driver_nl80211_data *drv = bss->drv;
7939 struct nl_msg *msg;
7940
7941 os_memset(data, 0, sizeof(*data));
7942 msg = nlmsg_alloc();
7943 if (!msg)
7944 return -ENOMEM;
7945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007946 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007947
7948 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7949 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7950
7951 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7952 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007953 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007954 return -ENOBUFS;
7955}
7956
7957
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03007958#if defined(HOSTAPD) || defined(CONFIG_AP)
7959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007960static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7961 int cw_min, int cw_max, int burst_time)
7962{
7963 struct i802_bss *bss = priv;
7964 struct wpa_driver_nl80211_data *drv = bss->drv;
7965 struct nl_msg *msg;
7966 struct nlattr *txq, *params;
7967
7968 msg = nlmsg_alloc();
7969 if (!msg)
7970 return -1;
7971
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007972 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007973
7974 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7975
7976 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7977 if (!txq)
7978 goto nla_put_failure;
7979
7980 /* We are only sending parameters for a single TXQ at a time */
7981 params = nla_nest_start(msg, 1);
7982 if (!params)
7983 goto nla_put_failure;
7984
7985 switch (queue) {
7986 case 0:
7987 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7988 break;
7989 case 1:
7990 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7991 break;
7992 case 2:
7993 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7994 break;
7995 case 3:
7996 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7997 break;
7998 }
7999 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8000 * 32 usec, so need to convert the value here. */
8001 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8002 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8003 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8004 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8005
8006 nla_nest_end(msg, params);
8007
8008 nla_nest_end(msg, txq);
8009
8010 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8011 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008012 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008013 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008014 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008015 return -1;
8016}
8017
8018
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008019static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008020 const char *ifname, int vlan_id)
8021{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022 struct wpa_driver_nl80211_data *drv = bss->drv;
8023 struct nl_msg *msg;
8024 int ret = -ENOBUFS;
8025
8026 msg = nlmsg_alloc();
8027 if (!msg)
8028 return -ENOMEM;
8029
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008030 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031
8032 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8033 if_nametoindex(bss->ifname));
8034 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8035 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8036 if_nametoindex(ifname));
8037
8038 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008039 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008040 if (ret < 0) {
8041 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8042 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8043 MAC2STR(addr), ifname, vlan_id, ret,
8044 strerror(-ret));
8045 }
8046 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008047 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008048 return ret;
8049}
8050
8051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052static int i802_get_inact_sec(void *priv, const u8 *addr)
8053{
8054 struct hostap_sta_driver_data data;
8055 int ret;
8056
8057 data.inactive_msec = (unsigned long) -1;
8058 ret = i802_read_sta_data(priv, &data, addr);
8059 if (ret || data.inactive_msec == (unsigned long) -1)
8060 return -1;
8061 return data.inactive_msec / 1000;
8062}
8063
8064
8065static int i802_sta_clear_stats(void *priv, const u8 *addr)
8066{
8067#if 0
8068 /* TODO */
8069#endif
8070 return 0;
8071}
8072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008073
8074static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8075 int reason)
8076{
8077 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008078 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008079 struct ieee80211_mgmt mgmt;
8080
Dmitry Shmidt04949592012-07-19 12:16:46 -07008081 if (drv->device_ap_sme)
8082 return wpa_driver_nl80211_sta_remove(bss, addr);
8083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008084 memset(&mgmt, 0, sizeof(mgmt));
8085 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8086 WLAN_FC_STYPE_DEAUTH);
8087 memcpy(mgmt.da, addr, ETH_ALEN);
8088 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8089 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8090 mgmt.u.deauth.reason_code = host_to_le16(reason);
8091 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8092 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008093 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8094 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008095}
8096
8097
8098static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8099 int reason)
8100{
8101 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008102 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008103 struct ieee80211_mgmt mgmt;
8104
Dmitry Shmidt04949592012-07-19 12:16:46 -07008105 if (drv->device_ap_sme)
8106 return wpa_driver_nl80211_sta_remove(bss, addr);
8107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008108 memset(&mgmt, 0, sizeof(mgmt));
8109 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8110 WLAN_FC_STYPE_DISASSOC);
8111 memcpy(mgmt.da, addr, ETH_ALEN);
8112 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8113 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8114 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8115 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8116 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008117 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8118 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008119}
8120
8121#endif /* HOSTAPD || CONFIG_AP */
8122
8123#ifdef HOSTAPD
8124
Jouni Malinen75ecf522011-06-27 15:19:46 -07008125static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8126{
8127 int i;
8128 int *old;
8129
8130 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8131 ifidx);
8132 for (i = 0; i < drv->num_if_indices; i++) {
8133 if (drv->if_indices[i] == 0) {
8134 drv->if_indices[i] = ifidx;
8135 return;
8136 }
8137 }
8138
8139 if (drv->if_indices != drv->default_if_indices)
8140 old = drv->if_indices;
8141 else
8142 old = NULL;
8143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008144 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8145 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07008146 if (!drv->if_indices) {
8147 if (!old)
8148 drv->if_indices = drv->default_if_indices;
8149 else
8150 drv->if_indices = old;
8151 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8152 "interfaces");
8153 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8154 return;
8155 } else if (!old)
8156 os_memcpy(drv->if_indices, drv->default_if_indices,
8157 sizeof(drv->default_if_indices));
8158 drv->if_indices[drv->num_if_indices] = ifidx;
8159 drv->num_if_indices++;
8160}
8161
8162
8163static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8164{
8165 int i;
8166
8167 for (i = 0; i < drv->num_if_indices; i++) {
8168 if (drv->if_indices[i] == ifidx) {
8169 drv->if_indices[i] = 0;
8170 break;
8171 }
8172 }
8173}
8174
8175
8176static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8177{
8178 int i;
8179
8180 for (i = 0; i < drv->num_if_indices; i++)
8181 if (drv->if_indices[i] == ifidx)
8182 return 1;
8183
8184 return 0;
8185}
8186
8187
8188static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8189 const char *bridge_ifname)
8190{
8191 struct i802_bss *bss = priv;
8192 struct wpa_driver_nl80211_data *drv = bss->drv;
8193 char name[IFNAMSIZ + 1];
8194
8195 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8196 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8197 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8198 if (val) {
8199 if (!if_nametoindex(name)) {
8200 if (nl80211_create_iface(drv, name,
8201 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008202 bss->addr, 1) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008203 return -1;
8204 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008205 linux_br_add_if(drv->global->ioctl_sock,
8206 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07008207 return -1;
8208 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008209 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8210 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8211 "interface %s up", name);
8212 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07008213 return i802_set_sta_vlan(priv, addr, name, 0);
8214 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008215 if (bridge_ifname)
8216 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8217 name);
8218
Jouni Malinen75ecf522011-06-27 15:19:46 -07008219 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8220 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8221 name);
8222 }
8223}
8224
8225
8226static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8227{
8228 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8229 struct sockaddr_ll lladdr;
8230 unsigned char buf[3000];
8231 int len;
8232 socklen_t fromlen = sizeof(lladdr);
8233
8234 len = recvfrom(sock, buf, sizeof(buf), 0,
8235 (struct sockaddr *)&lladdr, &fromlen);
8236 if (len < 0) {
8237 perror("recv");
8238 return;
8239 }
8240
8241 if (have_ifidx(drv, lladdr.sll_ifindex))
8242 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8243}
8244
8245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008246static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8247 struct i802_bss *bss,
8248 const char *brname, const char *ifname)
8249{
8250 int ifindex;
8251 char in_br[IFNAMSIZ];
8252
8253 os_strlcpy(bss->brname, brname, IFNAMSIZ);
8254 ifindex = if_nametoindex(brname);
8255 if (ifindex == 0) {
8256 /*
8257 * Bridge was configured, but the bridge device does
8258 * not exist. Try to add it now.
8259 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008260 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008261 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8262 "bridge interface %s: %s",
8263 brname, strerror(errno));
8264 return -1;
8265 }
8266 bss->added_bridge = 1;
8267 add_ifidx(drv, if_nametoindex(brname));
8268 }
8269
8270 if (linux_br_get(in_br, ifname) == 0) {
8271 if (os_strcmp(in_br, brname) == 0)
8272 return 0; /* already in the bridge */
8273
8274 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8275 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008276 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8277 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008278 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8279 "remove interface %s from bridge "
8280 "%s: %s",
8281 ifname, brname, strerror(errno));
8282 return -1;
8283 }
8284 }
8285
8286 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8287 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008288 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008289 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8290 "into bridge %s: %s",
8291 ifname, brname, strerror(errno));
8292 return -1;
8293 }
8294 bss->added_if_into_bridge = 1;
8295
8296 return 0;
8297}
8298
8299
8300static void *i802_init(struct hostapd_data *hapd,
8301 struct wpa_init_params *params)
8302{
8303 struct wpa_driver_nl80211_data *drv;
8304 struct i802_bss *bss;
8305 size_t i;
8306 char brname[IFNAMSIZ];
8307 int ifindex, br_ifindex;
8308 int br_added = 0;
8309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008310 bss = wpa_driver_nl80211_init(hapd, params->ifname,
8311 params->global_priv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008312 if (bss == NULL)
8313 return NULL;
8314
8315 drv = bss->drv;
8316 drv->nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008317 drv->eapol_sock = -1;
8318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008319 if (linux_br_get(brname, params->ifname) == 0) {
8320 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8321 params->ifname, brname);
8322 br_ifindex = if_nametoindex(brname);
8323 } else {
8324 brname[0] = '\0';
8325 br_ifindex = 0;
8326 }
8327
8328 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8329 drv->if_indices = drv->default_if_indices;
8330 for (i = 0; i < params->num_bridge; i++) {
8331 if (params->bridge[i]) {
8332 ifindex = if_nametoindex(params->bridge[i]);
8333 if (ifindex)
8334 add_ifidx(drv, ifindex);
8335 if (ifindex == br_ifindex)
8336 br_added = 1;
8337 }
8338 }
8339 if (!br_added && br_ifindex &&
8340 (params->num_bridge == 0 || !params->bridge[0]))
8341 add_ifidx(drv, br_ifindex);
8342
8343 /* start listening for EAPOL on the default AP interface */
8344 add_ifidx(drv, drv->ifindex);
8345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008346 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008347 goto failed;
8348
8349 if (params->bssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008350 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008351 params->bssid))
8352 goto failed;
8353 }
8354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008355 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008356 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
8357 "into AP mode", bss->ifname);
8358 goto failed;
8359 }
8360
8361 if (params->num_bridge && params->bridge[0] &&
8362 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
8363 goto failed;
8364
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008365 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008366 goto failed;
8367
8368 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8369 if (drv->eapol_sock < 0) {
8370 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
8371 goto failed;
8372 }
8373
8374 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8375 {
8376 printf("Could not register read socket for eapol\n");
8377 goto failed;
8378 }
8379
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008380 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8381 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008382 goto failed;
8383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008384 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008386 return bss;
8387
8388failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008389 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008390 return NULL;
8391}
8392
8393
8394static void i802_deinit(void *priv)
8395{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008396 struct i802_bss *bss = priv;
8397 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008398}
8399
8400#endif /* HOSTAPD */
8401
8402
8403static enum nl80211_iftype wpa_driver_nl80211_if_type(
8404 enum wpa_driver_if_type type)
8405{
8406 switch (type) {
8407 case WPA_IF_STATION:
8408 return NL80211_IFTYPE_STATION;
8409 case WPA_IF_P2P_CLIENT:
8410 case WPA_IF_P2P_GROUP:
8411 return NL80211_IFTYPE_P2P_CLIENT;
8412 case WPA_IF_AP_VLAN:
8413 return NL80211_IFTYPE_AP_VLAN;
8414 case WPA_IF_AP_BSS:
8415 return NL80211_IFTYPE_AP;
8416 case WPA_IF_P2P_GO:
8417 return NL80211_IFTYPE_P2P_GO;
8418 }
8419 return -1;
8420}
8421
8422
8423#ifdef CONFIG_P2P
8424
8425static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8426{
8427 struct wpa_driver_nl80211_data *drv;
8428 dl_list_for_each(drv, &global->interfaces,
8429 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008430 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008431 return 1;
8432 }
8433 return 0;
8434}
8435
8436
8437static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8438 u8 *new_addr)
8439{
8440 unsigned int idx;
8441
8442 if (!drv->global)
8443 return -1;
8444
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008445 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008446 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008447 new_addr[0] = drv->first_bss.addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008448 new_addr[0] ^= idx << 2;
8449 if (!nl80211_addr_in_use(drv->global, new_addr))
8450 break;
8451 }
8452 if (idx == 64)
8453 return -1;
8454
8455 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8456 MACSTR, MAC2STR(new_addr));
8457
8458 return 0;
8459}
8460
8461#endif /* CONFIG_P2P */
8462
8463
8464static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8465 const char *ifname, const u8 *addr,
8466 void *bss_ctx, void **drv_priv,
8467 char *force_ifname, u8 *if_addr,
8468 const char *bridge)
8469{
8470 struct i802_bss *bss = priv;
8471 struct wpa_driver_nl80211_data *drv = bss->drv;
8472 int ifidx;
8473#ifdef HOSTAPD
8474 struct i802_bss *new_bss = NULL;
8475
8476 if (type == WPA_IF_AP_BSS) {
8477 new_bss = os_zalloc(sizeof(*new_bss));
8478 if (new_bss == NULL)
8479 return -1;
8480 }
8481#endif /* HOSTAPD */
8482
8483 if (addr)
8484 os_memcpy(if_addr, addr, ETH_ALEN);
8485 ifidx = nl80211_create_iface(drv, ifname,
8486 wpa_driver_nl80211_if_type(type), addr,
8487 0);
8488 if (ifidx < 0) {
8489#ifdef HOSTAPD
8490 os_free(new_bss);
8491#endif /* HOSTAPD */
8492 return -1;
8493 }
8494
8495 if (!addr &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008496 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8497 if_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008498 nl80211_remove_iface(drv, ifidx);
8499 return -1;
8500 }
8501
8502#ifdef CONFIG_P2P
8503 if (!addr &&
8504 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8505 type == WPA_IF_P2P_GO)) {
8506 /* Enforce unique P2P Interface Address */
8507 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008509 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8510 own_addr) < 0 ||
8511 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8512 new_addr) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008513 nl80211_remove_iface(drv, ifidx);
8514 return -1;
8515 }
8516 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8517 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8518 "for P2P group interface");
8519 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
8520 nl80211_remove_iface(drv, ifidx);
8521 return -1;
8522 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008523 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008524 new_addr) < 0) {
8525 nl80211_remove_iface(drv, ifidx);
8526 return -1;
8527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008528 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008529 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008530 }
8531#endif /* CONFIG_P2P */
8532
8533#ifdef HOSTAPD
8534 if (bridge &&
8535 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8536 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8537 "interface %s to a bridge %s", ifname, bridge);
8538 nl80211_remove_iface(drv, ifidx);
8539 os_free(new_bss);
8540 return -1;
8541 }
8542
8543 if (type == WPA_IF_AP_BSS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008544 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8545 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008546 nl80211_remove_iface(drv, ifidx);
8547 os_free(new_bss);
8548 return -1;
8549 }
8550 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008551 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008552 new_bss->ifindex = ifidx;
8553 new_bss->drv = drv;
8554 new_bss->next = drv->first_bss.next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008555 new_bss->freq = drv->first_bss.freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008556 new_bss->ctx = bss_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008557 drv->first_bss.next = new_bss;
8558 if (drv_priv)
8559 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008560 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008561
8562 /* Subscribe management frames for this WPA_IF_AP_BSS */
8563 if (nl80211_setup_ap(new_bss))
8564 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008565 }
8566#endif /* HOSTAPD */
8567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008568 if (drv->global)
8569 drv->global->if_add_ifindex = ifidx;
8570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008571 return 0;
8572}
8573
8574
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008575static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008576 enum wpa_driver_if_type type,
8577 const char *ifname)
8578{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008579 struct wpa_driver_nl80211_data *drv = bss->drv;
8580 int ifindex = if_nametoindex(ifname);
8581
8582 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8583 __func__, type, ifname, ifindex);
8584 if (ifindex <= 0)
8585 return -1;
8586
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008587 nl80211_remove_iface(drv, ifindex);
8588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008589#ifdef HOSTAPD
Dmitry Shmidtaa532512012-09-24 10:35:31 -07008590 if (type != WPA_IF_AP_BSS)
8591 return 0;
8592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008593 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008594 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8595 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008596 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8597 "interface %s from bridge %s: %s",
8598 bss->ifname, bss->brname, strerror(errno));
8599 }
8600 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008601 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008602 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8603 "bridge %s: %s",
8604 bss->brname, strerror(errno));
8605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008606
8607 if (bss != &drv->first_bss) {
8608 struct i802_bss *tbss;
8609
8610 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8611 if (tbss->next == bss) {
8612 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008613 /* Unsubscribe management frames */
8614 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008615 nl80211_destroy_bss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008616 os_free(bss);
8617 bss = NULL;
8618 break;
8619 }
8620 }
8621 if (bss)
8622 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8623 "BSS %p in the list", __func__, bss);
8624 }
8625#endif /* HOSTAPD */
8626
8627 return 0;
8628}
8629
8630
8631static int cookie_handler(struct nl_msg *msg, void *arg)
8632{
8633 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8634 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8635 u64 *cookie = arg;
8636 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8637 genlmsg_attrlen(gnlh, 0), NULL);
8638 if (tb[NL80211_ATTR_COOKIE])
8639 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8640 return NL_SKIP;
8641}
8642
8643
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008644static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008645 unsigned int freq, unsigned int wait,
8646 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008647 u64 *cookie_out, int no_cck, int no_ack,
8648 int offchanok)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008649{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008650 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008651 struct nl_msg *msg;
8652 u64 cookie;
8653 int ret = -1;
8654
8655 msg = nlmsg_alloc();
8656 if (!msg)
8657 return -1;
8658
Dmitry Shmidt04949592012-07-19 12:16:46 -07008659 wpa_printf(MSG_DEBUG, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
8660 "no_ack=%d offchanok=%d",
8661 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008662 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008664 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008665 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008666 if (wait)
8667 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008668 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008669 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
8670 if (no_cck)
8671 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
8672 if (no_ack)
8673 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
8674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8676
8677 cookie = 0;
8678 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8679 msg = NULL;
8680 if (ret) {
8681 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008682 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8683 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008684 goto nla_put_failure;
8685 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008686 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
8687 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8688 (long long unsigned int) cookie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008689
8690 if (cookie_out)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008691 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008692
8693nla_put_failure:
8694 nlmsg_free(msg);
8695 return ret;
8696}
8697
8698
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008699static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
8700 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008701 unsigned int wait_time,
8702 const u8 *dst, const u8 *src,
8703 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008704 const u8 *data, size_t data_len,
8705 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008706{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008707 struct wpa_driver_nl80211_data *drv = bss->drv;
8708 int ret = -1;
8709 u8 *buf;
8710 struct ieee80211_hdr *hdr;
8711
8712 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008713 "freq=%u MHz wait=%d ms no_cck=%d)",
8714 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008715
8716 buf = os_zalloc(24 + data_len);
8717 if (buf == NULL)
8718 return ret;
8719 os_memcpy(buf + 24, data, data_len);
8720 hdr = (struct ieee80211_hdr *) buf;
8721 hdr->frame_control =
8722 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8723 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8724 os_memcpy(hdr->addr2, src, ETH_ALEN);
8725 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8726
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008727 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008728 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
8729 0, freq, no_cck, 1,
8730 wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008731 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008732 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008733 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008734 &drv->send_action_cookie,
8735 no_cck, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008736
8737 os_free(buf);
8738 return ret;
8739}
8740
8741
8742static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8743{
8744 struct i802_bss *bss = priv;
8745 struct wpa_driver_nl80211_data *drv = bss->drv;
8746 struct nl_msg *msg;
8747 int ret;
8748
8749 msg = nlmsg_alloc();
8750 if (!msg)
8751 return;
8752
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008753 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
8754 (long long unsigned int) drv->send_action_cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008755 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008756
8757 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8758 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8759
8760 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8761 msg = NULL;
8762 if (ret)
8763 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8764 "(%s)", ret, strerror(-ret));
8765
8766 nla_put_failure:
8767 nlmsg_free(msg);
8768}
8769
8770
8771static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8772 unsigned int duration)
8773{
8774 struct i802_bss *bss = priv;
8775 struct wpa_driver_nl80211_data *drv = bss->drv;
8776 struct nl_msg *msg;
8777 int ret;
8778 u64 cookie;
8779
8780 msg = nlmsg_alloc();
8781 if (!msg)
8782 return -1;
8783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008784 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008785
8786 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8787 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8788 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8789
8790 cookie = 0;
8791 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008792 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008793 if (ret == 0) {
8794 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8795 "0x%llx for freq=%u MHz duration=%u",
8796 (long long unsigned int) cookie, freq, duration);
8797 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008798 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008799 return 0;
8800 }
8801 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8802 "(freq=%d duration=%u): %d (%s)",
8803 freq, duration, ret, strerror(-ret));
8804nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008805 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008806 return -1;
8807}
8808
8809
8810static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8811{
8812 struct i802_bss *bss = priv;
8813 struct wpa_driver_nl80211_data *drv = bss->drv;
8814 struct nl_msg *msg;
8815 int ret;
8816
8817 if (!drv->pending_remain_on_chan) {
8818 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8819 "to cancel");
8820 return -1;
8821 }
8822
8823 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8824 "0x%llx",
8825 (long long unsigned int) drv->remain_on_chan_cookie);
8826
8827 msg = nlmsg_alloc();
8828 if (!msg)
8829 return -1;
8830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008831 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008832
8833 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8834 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8835
8836 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008837 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008838 if (ret == 0)
8839 return 0;
8840 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8841 "%d (%s)", ret, strerror(-ret));
8842nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008843 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008844 return -1;
8845}
8846
8847
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008848static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008849{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008850 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07008851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008852 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008853 if (bss->nl_preq && drv->device_ap_sme &&
8854 is_ap_interface(drv->nlmode)) {
8855 /*
8856 * Do not disable Probe Request reporting that was
8857 * enabled in nl80211_setup_ap().
8858 */
8859 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8860 "Probe Request reporting nl_preq=%p while "
8861 "in AP mode", bss->nl_preq);
8862 } else if (bss->nl_preq) {
8863 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8864 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865 eloop_unregister_read_sock(
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008866 nl_socket_get_fd(bss->nl_preq));
8867 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008868 }
8869 return 0;
8870 }
8871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008872 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008873 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008874 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008875 return 0;
8876 }
8877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008878 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8879 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008880 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008881 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8882 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008884 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008885 (WLAN_FC_TYPE_MGMT << 2) |
8886 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008887 NULL, 0) < 0)
8888 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07008889
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008890 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8891 wpa_driver_nl80211_event_receive, bss->nl_cb,
8892 bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008893
8894 return 0;
8895
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008896 out_err:
8897 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008898 return -1;
8899}
8900
8901
8902static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8903 int ifindex, int disabled)
8904{
8905 struct nl_msg *msg;
8906 struct nlattr *bands, *band;
8907 int ret;
8908
8909 msg = nlmsg_alloc();
8910 if (!msg)
8911 return -1;
8912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008913 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008914 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8915
8916 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8917 if (!bands)
8918 goto nla_put_failure;
8919
8920 /*
8921 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8922 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8923 * rates. All 5 GHz rates are left enabled.
8924 */
8925 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8926 if (!band)
8927 goto nla_put_failure;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008928 if (disabled) {
8929 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8930 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8931 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008932 nla_nest_end(msg, band);
8933
8934 nla_nest_end(msg, bands);
8935
8936 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8937 msg = NULL;
8938 if (ret) {
8939 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8940 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008941 } else
8942 drv->disabled_11b_rates = disabled;
8943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008944 return ret;
8945
8946nla_put_failure:
8947 nlmsg_free(msg);
8948 return -1;
8949}
8950
8951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008952static int wpa_driver_nl80211_deinit_ap(void *priv)
8953{
8954 struct i802_bss *bss = priv;
8955 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008956 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008957 return -1;
8958 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008959 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008960}
8961
8962
Dmitry Shmidt04949592012-07-19 12:16:46 -07008963static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8964{
8965 struct i802_bss *bss = priv;
8966 struct wpa_driver_nl80211_data *drv = bss->drv;
8967 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8968 return -1;
8969 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8970}
8971
8972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008973static void wpa_driver_nl80211_resume(void *priv)
8974{
8975 struct i802_bss *bss = priv;
8976 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008977 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008978 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8979 "resume event");
8980 }
8981}
8982
8983
8984static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8985 const u8 *ies, size_t ies_len)
8986{
8987 struct i802_bss *bss = priv;
8988 struct wpa_driver_nl80211_data *drv = bss->drv;
8989 int ret;
8990 u8 *data, *pos;
8991 size_t data_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008992 const u8 *own_addr = bss->addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008993
8994 if (action != 1) {
8995 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8996 "action %d", action);
8997 return -1;
8998 }
8999
9000 /*
9001 * Action frame payload:
9002 * Category[1] = 6 (Fast BSS Transition)
9003 * Action[1] = 1 (Fast BSS Transition Request)
9004 * STA Address
9005 * Target AP Address
9006 * FT IEs
9007 */
9008
9009 data_len = 2 + 2 * ETH_ALEN + ies_len;
9010 data = os_malloc(data_len);
9011 if (data == NULL)
9012 return -1;
9013 pos = data;
9014 *pos++ = 0x06; /* FT Action category */
9015 *pos++ = action;
9016 os_memcpy(pos, own_addr, ETH_ALEN);
9017 pos += ETH_ALEN;
9018 os_memcpy(pos, target_ap, ETH_ALEN);
9019 pos += ETH_ALEN;
9020 os_memcpy(pos, ies, ies_len);
9021
9022 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9023 drv->bssid, own_addr, drv->bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009024 data, data_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009025 os_free(data);
9026
9027 return ret;
9028}
9029
9030
9031static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9032{
9033 struct i802_bss *bss = priv;
9034 struct wpa_driver_nl80211_data *drv = bss->drv;
9035 struct nl_msg *msg, *cqm = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009036 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009037
9038 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9039 "hysteresis=%d", threshold, hysteresis);
9040
9041 msg = nlmsg_alloc();
9042 if (!msg)
9043 return -1;
9044
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009045 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009046
9047 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9048
9049 cqm = nlmsg_alloc();
9050 if (cqm == NULL)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009051 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009052
9053 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9054 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009055 if (nla_put_nested(msg, NL80211_ATTR_CQM, cqm) < 0)
9056 goto nla_put_failure;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009057
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009058 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009059 msg = NULL;
9060
9061nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009062 nlmsg_free(cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009063 nlmsg_free(msg);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009064 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009065}
9066
9067
9068static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9069{
9070 struct i802_bss *bss = priv;
9071 struct wpa_driver_nl80211_data *drv = bss->drv;
9072 int res;
9073
9074 os_memset(si, 0, sizeof(*si));
9075 res = nl80211_get_link_signal(drv, si);
9076 if (res != 0)
9077 return res;
9078
9079 return nl80211_get_link_noise(drv, si);
9080}
9081
9082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009083static int wpa_driver_nl80211_shared_freq(void *priv)
9084{
9085 struct i802_bss *bss = priv;
9086 struct wpa_driver_nl80211_data *drv = bss->drv;
9087 struct wpa_driver_nl80211_data *driver;
9088 int freq = 0;
9089
9090 /*
9091 * If the same PHY is in connected state with some other interface,
9092 * then retrieve the assoc freq.
9093 */
9094 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9095 drv->phyname);
9096
9097 dl_list_for_each(driver, &drv->global->interfaces,
9098 struct wpa_driver_nl80211_data, list) {
9099 if (drv == driver ||
9100 os_strcmp(drv->phyname, driver->phyname) != 0 ||
9101#ifdef ANDROID_P2P
9102 (!driver->associated && !is_ap_interface(driver->nlmode)))
9103#else
9104 !driver->associated)
9105#endif
9106 continue;
9107
9108 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9109 MACSTR,
9110 driver->phyname, driver->first_bss.ifname,
9111 MAC2STR(driver->first_bss.addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07009112 if (is_ap_interface(driver->nlmode))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009113 freq = driver->first_bss.freq;
9114 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009115 freq = nl80211_get_assoc_freq(driver);
9116 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9117 drv->phyname, freq);
9118 }
9119
9120 if (!freq)
9121 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9122 "PHY (%s) in associated state", drv->phyname);
9123
9124 return freq;
9125}
9126
9127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009128static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9129 int encrypt)
9130{
9131 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009132 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9133 0, 0, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009134}
9135
9136
9137static int nl80211_set_param(void *priv, const char *param)
9138{
9139 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9140 if (param == NULL)
9141 return 0;
9142
9143#ifdef CONFIG_P2P
9144 if (os_strstr(param, "use_p2p_group_interface=1")) {
9145 struct i802_bss *bss = priv;
9146 struct wpa_driver_nl80211_data *drv = bss->drv;
9147
9148 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9149 "interface");
9150 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9151 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9152 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009153#ifdef ANDROID_P2P
9154 if(os_strstr(param, "use_multi_chan_concurrent=1")) {
9155 struct i802_bss *bss = priv;
9156 struct wpa_driver_nl80211_data *drv = bss->drv;
9157 wpa_printf(MSG_DEBUG, "nl80211: Use Multi channel "
9158 "concurrency");
9159 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
9160 }
9161#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009162#endif /* CONFIG_P2P */
9163
9164 return 0;
9165}
9166
9167
9168static void * nl80211_global_init(void)
9169{
9170 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009171 struct netlink_config *cfg;
9172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009173 global = os_zalloc(sizeof(*global));
9174 if (global == NULL)
9175 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009176 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009177 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009178 global->if_add_ifindex = -1;
9179
9180 cfg = os_zalloc(sizeof(*cfg));
9181 if (cfg == NULL)
9182 goto err;
9183
9184 cfg->ctx = global;
9185 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9186 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9187 global->netlink = netlink_init(cfg);
9188 if (global->netlink == NULL) {
9189 os_free(cfg);
9190 goto err;
9191 }
9192
9193 if (wpa_driver_nl80211_init_nl_global(global) < 0)
9194 goto err;
9195
9196 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
9197 if (global->ioctl_sock < 0) {
9198 perror("socket(PF_INET,SOCK_DGRAM)");
9199 goto err;
9200 }
9201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009202 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009203
9204err:
9205 nl80211_global_deinit(global);
9206 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009207}
9208
9209
9210static void nl80211_global_deinit(void *priv)
9211{
9212 struct nl80211_global *global = priv;
9213 if (global == NULL)
9214 return;
9215 if (!dl_list_empty(&global->interfaces)) {
9216 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
9217 "nl80211_global_deinit",
9218 dl_list_len(&global->interfaces));
9219 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009220
9221 if (global->netlink)
9222 netlink_deinit(global->netlink);
9223
9224 nl_destroy_handles(&global->nl);
9225
9226 if (global->nl_event) {
9227 eloop_unregister_read_sock(
9228 nl_socket_get_fd(global->nl_event));
9229 nl_destroy_handles(&global->nl_event);
9230 }
9231
9232 nl_cb_put(global->nl_cb);
9233
9234 if (global->ioctl_sock >= 0)
9235 close(global->ioctl_sock);
9236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009237 os_free(global);
9238}
9239
9240
9241static const char * nl80211_get_radio_name(void *priv)
9242{
9243 struct i802_bss *bss = priv;
9244 struct wpa_driver_nl80211_data *drv = bss->drv;
9245 return drv->phyname;
9246}
9247
9248
Jouni Malinen75ecf522011-06-27 15:19:46 -07009249static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
9250 const u8 *pmkid)
9251{
9252 struct nl_msg *msg;
9253
9254 msg = nlmsg_alloc();
9255 if (!msg)
9256 return -ENOMEM;
9257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009258 nl80211_cmd(bss->drv, msg, 0, cmd);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009259
9260 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9261 if (pmkid)
9262 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9263 if (bssid)
9264 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9265
9266 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9267 nla_put_failure:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009268 nlmsg_free(msg);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009269 return -ENOBUFS;
9270}
9271
9272
9273static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9274{
9275 struct i802_bss *bss = priv;
9276 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9277 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9278}
9279
9280
9281static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9282{
9283 struct i802_bss *bss = priv;
9284 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9285 MAC2STR(bssid));
9286 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9287}
9288
9289
9290static int nl80211_flush_pmkid(void *priv)
9291{
9292 struct i802_bss *bss = priv;
9293 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9294 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9295}
9296
9297
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009298static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9299 const u8 *replay_ctr)
9300{
9301 struct i802_bss *bss = priv;
9302 struct wpa_driver_nl80211_data *drv = bss->drv;
9303 struct nlattr *replay_nested;
9304 struct nl_msg *msg;
9305
9306 msg = nlmsg_alloc();
9307 if (!msg)
9308 return;
9309
9310 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9311
9312 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9313
9314 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9315 if (!replay_nested)
9316 goto nla_put_failure;
9317
9318 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9319 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9320 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9321 replay_ctr);
9322
9323 nla_nest_end(msg, replay_nested);
9324
9325 send_and_recv_msgs(drv, msg, NULL, NULL);
9326 return;
9327 nla_put_failure:
9328 nlmsg_free(msg);
9329}
9330
9331
9332static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9333 const u8 *addr, int qos)
9334{
9335 /* send data frame to poll STA and check whether
9336 * this frame is ACKed */
9337 struct {
9338 struct ieee80211_hdr hdr;
9339 u16 qos_ctl;
9340 } STRUCT_PACKED nulldata;
9341 size_t size;
9342
9343 /* Send data frame to poll STA and check whether this frame is ACKed */
9344
9345 os_memset(&nulldata, 0, sizeof(nulldata));
9346
9347 if (qos) {
9348 nulldata.hdr.frame_control =
9349 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9350 WLAN_FC_STYPE_QOS_NULL);
9351 size = sizeof(nulldata);
9352 } else {
9353 nulldata.hdr.frame_control =
9354 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9355 WLAN_FC_STYPE_NULLFUNC);
9356 size = sizeof(struct ieee80211_hdr);
9357 }
9358
9359 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9360 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9361 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9362 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9363
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009364 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9365 0, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009366 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9367 "send poll frame");
9368}
9369
9370static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9371 int qos)
9372{
9373 struct i802_bss *bss = priv;
9374 struct wpa_driver_nl80211_data *drv = bss->drv;
9375 struct nl_msg *msg;
9376
9377 if (!drv->poll_command_supported) {
9378 nl80211_send_null_frame(bss, own_addr, addr, qos);
9379 return;
9380 }
9381
9382 msg = nlmsg_alloc();
9383 if (!msg)
9384 return;
9385
9386 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9387
9388 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9389 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9390
9391 send_and_recv_msgs(drv, msg, NULL, NULL);
9392 return;
9393 nla_put_failure:
9394 nlmsg_free(msg);
9395}
9396
9397
9398static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9399{
9400 struct nl_msg *msg;
9401
9402 msg = nlmsg_alloc();
9403 if (!msg)
9404 return -ENOMEM;
9405
9406 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9407 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9408 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9409 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9410 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9411nla_put_failure:
9412 nlmsg_free(msg);
9413 return -ENOBUFS;
9414}
9415
9416
9417static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9418 int ctwindow)
9419{
9420 struct i802_bss *bss = priv;
9421
9422 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9423 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9424
9425 if (opp_ps != -1 || ctwindow != -1)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009426#ifdef ANDROID_P2P
9427 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
9428#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009429 return -1; /* Not yet supported */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009430#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009431
9432 if (legacy_ps == -1)
9433 return 0;
9434 if (legacy_ps != 0 && legacy_ps != 1)
9435 return -1; /* Not yet supported */
9436
9437 return nl80211_set_power_save(bss, legacy_ps);
9438}
9439
9440
9441#ifdef CONFIG_TDLS
9442
9443static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9444 u8 dialog_token, u16 status_code,
9445 const u8 *buf, size_t len)
9446{
9447 struct i802_bss *bss = priv;
9448 struct wpa_driver_nl80211_data *drv = bss->drv;
9449 struct nl_msg *msg;
9450
9451 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9452 return -EOPNOTSUPP;
9453
9454 if (!dst)
9455 return -EINVAL;
9456
9457 msg = nlmsg_alloc();
9458 if (!msg)
9459 return -ENOMEM;
9460
9461 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9462 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9463 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9464 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9465 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9466 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9467 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9468
9469 return send_and_recv_msgs(drv, msg, NULL, NULL);
9470
9471nla_put_failure:
9472 nlmsg_free(msg);
9473 return -ENOBUFS;
9474}
9475
9476
9477static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9478{
9479 struct i802_bss *bss = priv;
9480 struct wpa_driver_nl80211_data *drv = bss->drv;
9481 struct nl_msg *msg;
9482 enum nl80211_tdls_operation nl80211_oper;
9483
9484 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9485 return -EOPNOTSUPP;
9486
9487 switch (oper) {
9488 case TDLS_DISCOVERY_REQ:
9489 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9490 break;
9491 case TDLS_SETUP:
9492 nl80211_oper = NL80211_TDLS_SETUP;
9493 break;
9494 case TDLS_TEARDOWN:
9495 nl80211_oper = NL80211_TDLS_TEARDOWN;
9496 break;
9497 case TDLS_ENABLE_LINK:
9498 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9499 break;
9500 case TDLS_DISABLE_LINK:
9501 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9502 break;
9503 case TDLS_ENABLE:
9504 return 0;
9505 case TDLS_DISABLE:
9506 return 0;
9507 default:
9508 return -EINVAL;
9509 }
9510
9511 msg = nlmsg_alloc();
9512 if (!msg)
9513 return -ENOMEM;
9514
9515 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9516 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9517 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9518 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9519
9520 return send_and_recv_msgs(drv, msg, NULL, NULL);
9521
9522nla_put_failure:
9523 nlmsg_free(msg);
9524 return -ENOBUFS;
9525}
9526
9527#endif /* CONFIG TDLS */
9528
9529
9530#ifdef ANDROID
9531
9532typedef struct android_wifi_priv_cmd {
9533 char *buf;
9534 int used_len;
9535 int total_len;
9536} android_wifi_priv_cmd;
9537
9538static int drv_errors = 0;
9539
9540static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9541{
9542 drv_errors++;
9543 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9544 drv_errors = 0;
9545 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9546 }
9547}
9548
9549
9550static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9551{
9552 struct wpa_driver_nl80211_data *drv = bss->drv;
9553 struct ifreq ifr;
9554 android_wifi_priv_cmd priv_cmd;
9555 char buf[MAX_DRV_CMD_SIZE];
9556 int ret;
9557
9558 os_memset(&ifr, 0, sizeof(ifr));
9559 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9560 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9561
9562 os_memset(buf, 0, sizeof(buf));
9563 os_strlcpy(buf, cmd, sizeof(buf));
9564
9565 priv_cmd.buf = buf;
9566 priv_cmd.used_len = sizeof(buf);
9567 priv_cmd.total_len = sizeof(buf);
9568 ifr.ifr_data = &priv_cmd;
9569
9570 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9571 if (ret < 0) {
9572 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9573 __func__);
9574 wpa_driver_send_hang_msg(drv);
9575 return ret;
9576 }
9577
9578 drv_errors = 0;
9579 return 0;
9580}
9581
9582
9583static int android_pno_start(struct i802_bss *bss,
9584 struct wpa_driver_scan_params *params)
9585{
9586 struct wpa_driver_nl80211_data *drv = bss->drv;
9587 struct ifreq ifr;
9588 android_wifi_priv_cmd priv_cmd;
9589 int ret = 0, i = 0, bp;
9590 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9591
9592 bp = WEXT_PNOSETUP_HEADER_SIZE;
9593 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9594 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9595 buf[bp++] = WEXT_PNO_TLV_VERSION;
9596 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9597 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9598
9599 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9600 /* Check that there is enough space needed for 1 more SSID, the
9601 * other sections and null termination */
9602 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9603 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9604 break;
9605 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
9606 params->ssids[i].ssid,
9607 params->ssids[i].ssid_len);
9608 buf[bp++] = WEXT_PNO_SSID_SECTION;
9609 buf[bp++] = params->ssids[i].ssid_len;
9610 os_memcpy(&buf[bp], params->ssids[i].ssid,
9611 params->ssids[i].ssid_len);
9612 bp += params->ssids[i].ssid_len;
9613 i++;
9614 }
9615
9616 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9617 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9618 WEXT_PNO_SCAN_INTERVAL);
9619 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9620
9621 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9622 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9623 WEXT_PNO_REPEAT);
9624 bp += WEXT_PNO_REPEAT_LENGTH;
9625
9626 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9627 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9628 WEXT_PNO_MAX_REPEAT);
9629 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9630
9631 memset(&ifr, 0, sizeof(ifr));
9632 memset(&priv_cmd, 0, sizeof(priv_cmd));
9633 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9634
9635 priv_cmd.buf = buf;
9636 priv_cmd.used_len = bp;
9637 priv_cmd.total_len = bp;
9638 ifr.ifr_data = &priv_cmd;
9639
9640 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9641
9642 if (ret < 0) {
9643 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9644 ret);
9645 wpa_driver_send_hang_msg(drv);
9646 return ret;
9647 }
9648
9649 drv_errors = 0;
9650
9651 return android_priv_cmd(bss, "PNOFORCE 1");
9652}
9653
9654
9655static int android_pno_stop(struct i802_bss *bss)
9656{
9657 return android_priv_cmd(bss, "PNOFORCE 0");
9658}
9659
9660#endif /* ANDROID */
9661
9662
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009663static int driver_nl80211_set_key(const char *ifname, void *priv,
9664 enum wpa_alg alg, const u8 *addr,
9665 int key_idx, int set_tx,
9666 const u8 *seq, size_t seq_len,
9667 const u8 *key, size_t key_len)
9668{
9669 struct i802_bss *bss = priv;
9670 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
9671 set_tx, seq, seq_len, key, key_len);
9672}
9673
9674
9675static int driver_nl80211_scan2(void *priv,
9676 struct wpa_driver_scan_params *params)
9677{
9678 struct i802_bss *bss = priv;
9679 return wpa_driver_nl80211_scan(bss, params);
9680}
9681
9682
9683static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
9684 int reason_code)
9685{
9686 struct i802_bss *bss = priv;
9687 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
9688}
9689
9690
9691static int driver_nl80211_authenticate(void *priv,
9692 struct wpa_driver_auth_params *params)
9693{
9694 struct i802_bss *bss = priv;
9695 return wpa_driver_nl80211_authenticate(bss, params);
9696}
9697
9698
9699static void driver_nl80211_deinit(void *priv)
9700{
9701 struct i802_bss *bss = priv;
9702 wpa_driver_nl80211_deinit(bss);
9703}
9704
9705
9706static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9707 const char *ifname)
9708{
9709 struct i802_bss *bss = priv;
9710 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9711}
9712
9713
9714static int driver_nl80211_send_mlme(void *priv, const u8 *data,
9715 size_t data_len, int noack)
9716{
9717 struct i802_bss *bss = priv;
9718 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
9719 0, 0, 0, 0);
9720}
9721
9722
9723static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9724{
9725 struct i802_bss *bss = priv;
9726 return wpa_driver_nl80211_sta_remove(bss, addr);
9727}
9728
9729
9730#if defined(HOSTAPD) || defined(CONFIG_AP)
9731static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9732 const char *ifname, int vlan_id)
9733{
9734 struct i802_bss *bss = priv;
9735 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9736}
9737#endif /* HOSTAPD || CONFIG_AP */
9738
9739
9740static int driver_nl80211_read_sta_data(void *priv,
9741 struct hostap_sta_driver_data *data,
9742 const u8 *addr)
9743{
9744 struct i802_bss *bss = priv;
9745 return i802_read_sta_data(bss, data, addr);
9746}
9747
9748
9749static int driver_nl80211_send_action(void *priv, unsigned int freq,
9750 unsigned int wait_time,
9751 const u8 *dst, const u8 *src,
9752 const u8 *bssid,
9753 const u8 *data, size_t data_len,
9754 int no_cck)
9755{
9756 struct i802_bss *bss = priv;
9757 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9758 bssid, data, data_len, no_cck);
9759}
9760
9761
9762static int driver_nl80211_probe_req_report(void *priv, int report)
9763{
9764 struct i802_bss *bss = priv;
9765 return wpa_driver_nl80211_probe_req_report(bss, report);
9766}
9767
9768
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009769static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
9770 const u8 *ies, size_t ies_len)
9771{
9772 int ret;
9773 struct nl_msg *msg;
9774 struct i802_bss *bss = priv;
9775 struct wpa_driver_nl80211_data *drv = bss->drv;
9776 u16 mdid = WPA_GET_LE16(md);
9777
9778 msg = nlmsg_alloc();
9779 if (!msg)
9780 return -ENOMEM;
9781
9782 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9783 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
9784 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9785 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
9786 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
9787
9788 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9789 if (ret) {
9790 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
9791 "err=%d (%s)", ret, strerror(-ret));
9792 }
9793
9794 return ret;
9795
9796nla_put_failure:
9797 nlmsg_free(msg);
9798 return -ENOBUFS;
9799}
9800
9801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009802const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9803 .name = "nl80211",
9804 .desc = "Linux nl80211/cfg80211",
9805 .get_bssid = wpa_driver_nl80211_get_bssid,
9806 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009807 .set_key = driver_nl80211_set_key,
9808 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009809 .sched_scan = wpa_driver_nl80211_sched_scan,
9810 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009811 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009812 .deauthenticate = driver_nl80211_deauthenticate,
9813 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814 .associate = wpa_driver_nl80211_associate,
9815 .global_init = nl80211_global_init,
9816 .global_deinit = nl80211_global_deinit,
9817 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009818 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009819 .get_capa = wpa_driver_nl80211_get_capa,
9820 .set_operstate = wpa_driver_nl80211_set_operstate,
9821 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9822 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009823 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009824 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009825 .if_remove = driver_nl80211_if_remove,
9826 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009827 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
9828 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009829 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009830 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9831 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
9832#ifdef HOSTAPD
9833 .hapd_init = i802_init,
9834 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009835 .set_wds_sta = i802_set_wds_sta,
9836#endif /* HOSTAPD */
9837#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009838 .get_seqnum = i802_get_seqnum,
9839 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009840 .get_inact_sec = i802_get_inact_sec,
9841 .sta_clear_stats = i802_sta_clear_stats,
9842 .set_rts = i802_set_rts,
9843 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009844 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009845 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009846 .sta_deauth = i802_sta_deauth,
9847 .sta_disassoc = i802_sta_disassoc,
9848#endif /* HOSTAPD || CONFIG_AP */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009849 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009850 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009851 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009852 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9853 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9854 .cancel_remain_on_channel =
9855 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009856 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009857 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009858 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009859 .resume = wpa_driver_nl80211_resume,
9860 .send_ft_action = nl80211_send_ft_action,
9861 .signal_monitor = nl80211_signal_monitor,
9862 .signal_poll = nl80211_signal_poll,
9863 .send_frame = nl80211_send_frame,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009864 .shared_freq = wpa_driver_nl80211_shared_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009865 .set_param = nl80211_set_param,
9866 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009867 .add_pmkid = nl80211_add_pmkid,
9868 .remove_pmkid = nl80211_remove_pmkid,
9869 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009870 .set_rekey_info = nl80211_set_rekey_info,
9871 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009872 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009873#ifdef CONFIG_TDLS
9874 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9875 .tdls_oper = nl80211_tdls_oper,
9876#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009877 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009878#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009879 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009880 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009881 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
9882#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009883#ifdef ANDROID
9884 .driver_cmd = wpa_driver_nl80211_driver_cmd,
9885#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009886};