]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
nl80211: Support splitting wiphy information in dumps
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
CommitLineData
3f5285e8 1/*
c5121837 2 * Driver interaction with Linux nl80211/cfg80211
3d9975d5 3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
072ad14c
JM
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
58f6fbe0 7 * Copyright (c) 2009-2010, Atheros Communications
3f5285e8 8 *
0f3d578e
JM
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
3f5285e8
JM
11 */
12
13#include "includes.h"
14#include <sys/ioctl.h>
6859f1cb
BG
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
37b7d082 18#include <net/if.h>
3f5285e8
JM
19#include <netlink/genl/genl.h>
20#include <netlink/genl/family.h>
21#include <netlink/genl/ctrl.h>
8602b0f2 22#include <linux/rtnetlink.h>
1b648c7e
JM
23#include <netpacket/packet.h>
24#include <linux/filter.h>
32ab4855 25#include <linux/errqueue.h>
7e45830a 26#include "nl80211_copy.h"
625f587b 27
3f5285e8 28#include "common.h"
1b648c7e 29#include "eloop.h"
f2ed8023 30#include "utils/list.h"
1b648c7e 31#include "common/ieee802_11_defs.h"
9b90955e 32#include "common/ieee802_11_common.h"
f10bfc9a 33#include "l2_packet/l2_packet.h"
e2d02c29 34#include "netlink.h"
34f2f814 35#include "linux_ioctl.h"
e2d02c29
JM
36#include "radiotap.h"
37#include "radiotap_iter.h"
8401a6b0 38#include "rfkill.h"
1b648c7e 39#include "driver.h"
0915d02c 40
32ab4855
JB
41#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
216eede8
DS
61#ifdef ANDROID
62#include "android_drv.h"
df2f9ec6
JM
63
64/* system/core/libnl_2 in AOSP does not include nla_put_u32() */
65int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
66{
67 return nla_put(msg, attrtype, sizeof(uint32_t), &value);
68}
216eede8 69#endif /* ANDROID */
c5121837
JM
70#ifdef CONFIG_LIBNL20
71/* libnl 2.0 compatibility code */
2e8eac2d 72#define nl_handle nl_sock
a65a9aed
JB
73#define nl80211_handle_alloc nl_socket_alloc_cb
74#define nl80211_handle_destroy nl_socket_free
75#else
76/*
77 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
78 * but when you free a socket again it will mess up its bitmap and
79 * and use the wrong number the next time it needs a socket ID.
80 * Therefore, we wrap the handle alloc/destroy and add our own pid
81 * accounting.
82 */
83static uint32_t port_bitmap[32] = { 0 };
84
85static struct nl_handle *nl80211_handle_alloc(void *cb)
86{
87 struct nl_handle *handle;
88 uint32_t pid = getpid() & 0x3FFFFF;
89 int i;
90
91 handle = nl_handle_alloc_cb(cb);
92
93 for (i = 0; i < 1024; i++) {
94 if (port_bitmap[i / 32] & (1 << (i % 32)))
95 continue;
96 port_bitmap[i / 32] |= 1 << (i % 32);
97 pid += i << 22;
98 break;
99 }
100
101 nl_socket_set_local_port(handle, pid);
102
103 return handle;
104}
105
106static void nl80211_handle_destroy(struct nl_handle *handle)
107{
108 uint32_t port = nl_socket_get_local_port(handle);
109
110 port >>= 22;
111 port_bitmap[port / 32] &= ~(1 << (port % 32));
112
113 nl_handle_destroy(handle);
114}
c5121837
JM
115#endif /* CONFIG_LIBNL20 */
116
c5121837 117
481234cf 118static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
a92dfde8 119{
481234cf 120 struct nl_handle *handle;
a92dfde8 121
481234cf
JM
122 handle = nl80211_handle_alloc(cb);
123 if (handle == NULL) {
a92dfde8
JB
124 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
125 "callbacks (%s)", dbg);
481234cf 126 return NULL;
a92dfde8
JB
127 }
128
481234cf 129 if (genl_connect(handle)) {
a92dfde8
JB
130 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
131 "netlink (%s)", dbg);
481234cf
JM
132 nl80211_handle_destroy(handle);
133 return NULL;
a92dfde8
JB
134 }
135
481234cf 136 return handle;
a92dfde8
JB
137}
138
139
481234cf 140static void nl_destroy_handles(struct nl_handle **handle)
a92dfde8 141{
481234cf 142 if (*handle == NULL)
a92dfde8 143 return;
481234cf
JM
144 nl80211_handle_destroy(*handle);
145 *handle = NULL;
a92dfde8
JB
146}
147
148
3f5285e8
JM
149#ifndef IFF_LOWER_UP
150#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
151#endif
152#ifndef IFF_DORMANT
153#define IFF_DORMANT 0x20000 /* driver signals dormant */
154#endif
155
156#ifndef IF_OPER_DORMANT
157#define IF_OPER_DORMANT 5
158#endif
159#ifndef IF_OPER_UP
160#define IF_OPER_UP 6
161#endif
162
f2ed8023
JM
163struct nl80211_global {
164 struct dl_list interfaces;
ff6a158b 165 int if_add_ifindex;
36d84860 166 struct netlink_data *netlink;
2a7b66f5 167 struct nl_cb *nl_cb;
481234cf 168 struct nl_handle *nl;
335d42b1 169 int nl80211_id;
c81eff1a 170 int ioctl_sock; /* socket for ioctl() use */
d6c9aab8 171
481234cf 172 struct nl_handle *nl_event;
f2ed8023
JM
173};
174
e32ad281
JB
175struct nl80211_wiphy_data {
176 struct dl_list list;
177 struct dl_list bsss;
178 struct dl_list drvs;
179
481234cf 180 struct nl_handle *nl_beacons;
e32ad281
JB
181 struct nl_cb *nl_cb;
182
183 int wiphy_idx;
184};
185
36d84860
BG
186static void nl80211_global_deinit(void *priv);
187
c5121837 188struct i802_bss {
a2e40bb6 189 struct wpa_driver_nl80211_data *drv;
c5121837 190 struct i802_bss *next;
b4fd6fab 191 int ifindex;
a2e40bb6 192 char ifname[IFNAMSIZ + 1];
e17a2477 193 char brname[IFNAMSIZ];
c5121837 194 unsigned int beacon_set:1;
e17a2477
JM
195 unsigned int added_if_into_bridge:1;
196 unsigned int added_bridge:1;
873d0fcf 197 unsigned int in_deinit:1;
221a59c9 198
341eebee
JB
199 u8 addr[ETH_ALEN];
200
e4fb2167
JB
201 int freq;
202
a5e1eb20 203 void *ctx;
481234cf 204 struct nl_handle *nl_preq, *nl_mgmt;
cc7a48d1 205 struct nl_cb *nl_cb;
e32ad281
JB
206
207 struct nl80211_wiphy_data *wiphy_data;
208 struct dl_list wiphy_list;
c5121837 209};
3f5285e8
JM
210
211struct wpa_driver_nl80211_data {
f2ed8023
JM
212 struct nl80211_global *global;
213 struct dl_list list;
e32ad281 214 struct dl_list wiphy_list;
6859f1cb 215 char phyname[32];
3f5285e8 216 void *ctx;
3f5285e8 217 int ifindex;
7524cfb1 218 int if_removed;
a63063b4 219 int if_disabled;
7d9c3698 220 int ignore_if_down_event;
8401a6b0 221 struct rfkill_data *rfkill;
c2a04078
JM
222 struct wpa_driver_capa capa;
223 int has_capability;
c2a04078 224
3f5285e8
JM
225 int operstate;
226
3f5285e8
JM
227 int scan_complete_events;
228
1afc986d 229 struct nl_cb *nl_cb;
1c873584 230
e6b8efeb 231 u8 auth_bssid[ETH_ALEN];
c2a04078
JM
232 u8 bssid[ETH_ALEN];
233 int associated;
fd05d64e
JM
234 u8 ssid[32];
235 size_t ssid_len;
b1f625e0
EP
236 enum nl80211_iftype nlmode;
237 enum nl80211_iftype ap_scan_as_station;
4832ecd7 238 unsigned int assoc_freq;
d2440ba0 239
0915d02c
JM
240 int monitor_sock;
241 int monitor_ifidx;
3fd1cefb 242 int monitor_refcount;
7da3abe7 243
b3af99d2 244 unsigned int disabled_11b_rates:1;
55777702 245 unsigned int pending_remain_on_chan:1;
dac12351 246 unsigned int in_interface_list:1;
61cbe2ff 247 unsigned int device_ap_sme:1;
39718852 248 unsigned int poll_command_supported:1;
32ab4855 249 unsigned int data_tx_status:1;
536fd62d
JM
250 unsigned int scan_for_auth:1;
251 unsigned int retry_auth:1;
a11241fa 252 unsigned int use_monitor:1;
a8c5b43a 253 unsigned int ignore_next_local_disconnect:1;
55777702
JM
254
255 u64 remain_on_chan_cookie;
58f6fbe0 256 u64 send_action_cookie;
c5121837 257
5582a5d1
JB
258 unsigned int last_mgmt_freq;
259
3812464c
JM
260 struct wpa_driver_scan_filter *filter_ssids;
261 size_t num_filter_ssids;
262
a2e40bb6
FF
263 struct i802_bss first_bss;
264
d12dab4c 265 int eapol_tx_sock;
f10bfc9a 266
c5121837 267#ifdef HOSTAPD
c5121837 268 int eapol_sock; /* socket for EAPOL frames */
c5121837
JM
269
270 int default_if_indices[16];
271 int *if_indices;
272 int num_if_indices;
273
c5121837
JM
274 int last_freq;
275 int last_freq_ht;
c5121837 276#endif /* HOSTAPD */
536fd62d
JM
277
278 /* From failed authentication command */
279 int auth_freq;
280 u8 auth_bssid_[ETH_ALEN];
281 u8 auth_ssid[32];
282 size_t auth_ssid_len;
283 int auth_alg;
284 u8 *auth_ie;
285 size_t auth_ie_len;
286 u8 auth_wep_key[4][16];
287 size_t auth_wep_key_len[4];
288 int auth_wep_tx_keyidx;
289 int auth_local_state_change;
290 int auth_p2p;
3f5285e8
JM
291};
292
293
9ebce9c5 294static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
3f5285e8
JM
295static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
296 void *timeout_ctx);
b1f625e0
EP
297static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
298 enum nl80211_iftype nlmode);
362f781e 299static int
7524cfb1 300wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
d72aad94 301static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
302 const u8 *addr, int cmd, u16 reason_code,
303 int local_state_change);
460456f8
JM
304static void nl80211_remove_monitor_interface(
305 struct wpa_driver_nl80211_data *drv);
88df0ef7 306static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f 307 unsigned int freq, unsigned int wait,
b106173a 308 const u8 *buf, size_t buf_len, u64 *cookie,
88df0ef7 309 int no_cck, int no_ack, int offchanok);
9ebce9c5
JM
310static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
311 int report);
216eede8
DS
312#ifdef ANDROID
313static int android_pno_start(struct i802_bss *bss,
314 struct wpa_driver_scan_params *params);
315static int android_pno_stop(struct i802_bss *bss);
316#endif /* ANDROID */
0915d02c 317
072ad14c 318#ifdef HOSTAPD
2135f224
JM
319static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
320static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
97cfcf64 321static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
9ebce9c5 322static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
fbbfcbac
FF
323 enum wpa_driver_if_type type,
324 const char *ifname);
37eb8da9 325#else /* HOSTAPD */
bd2df892
JM
326static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
327{
328}
329
330static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
331{
332}
333
334static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
37eb8da9
JM
335{
336 return 0;
337}
072ad14c
JM
338#endif /* HOSTAPD */
339
9ebce9c5
JM
340static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
341 struct hostapd_freq_params *freq);
4e5cb1a3
JM
342static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
343 int ifindex, int disabled);
504e905c 344
21bdbe38 345static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
536fd62d
JM
346static int wpa_driver_nl80211_authenticate_retry(
347 struct wpa_driver_nl80211_data *drv);
21bdbe38 348
3f5285e8 349
b1f625e0
EP
350static int is_ap_interface(enum nl80211_iftype nlmode)
351{
352 return (nlmode == NL80211_IFTYPE_AP ||
353 nlmode == NL80211_IFTYPE_P2P_GO);
354}
355
356
357static int is_sta_interface(enum nl80211_iftype nlmode)
358{
359 return (nlmode == NL80211_IFTYPE_STATION ||
360 nlmode == NL80211_IFTYPE_P2P_CLIENT);
361}
362
363
b3af99d2
JM
364static int is_p2p_interface(enum nl80211_iftype nlmode)
365{
366 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
367 nlmode == NL80211_IFTYPE_P2P_GO);
368}
369
370
f5a8d422
JM
371struct nl80211_bss_info_arg {
372 struct wpa_driver_nl80211_data *drv;
373 struct wpa_scan_results *res;
374 unsigned int assoc_freq;
20f5a4c2 375 u8 assoc_bssid[ETH_ALEN];
f5a8d422
JM
376};
377
378static int bss_info_handler(struct nl_msg *msg, void *arg);
379
380
6241fcb1
JM
381/* nl80211 code */
382static int ack_handler(struct nl_msg *msg, void *arg)
383{
384 int *err = arg;
385 *err = 0;
386 return NL_STOP;
387}
388
389static int finish_handler(struct nl_msg *msg, void *arg)
390{
8e8df255
JM
391 int *ret = arg;
392 *ret = 0;
6241fcb1
JM
393 return NL_SKIP;
394}
395
396static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
397 void *arg)
398{
399 int *ret = arg;
400 *ret = err->error;
401 return NL_SKIP;
402}
403
5b7b85f6
JM
404
405static int no_seq_check(struct nl_msg *msg, void *arg)
406{
407 return NL_OK;
408}
409
410
d6c9aab8 411static int send_and_recv(struct nl80211_global *global,
58f6fbe0
JM
412 struct nl_handle *nl_handle, struct nl_msg *msg,
413 int (*valid_handler)(struct nl_msg *, void *),
414 void *valid_data)
6241fcb1
JM
415{
416 struct nl_cb *cb;
417 int err = -ENOMEM;
418
d6c9aab8 419 cb = nl_cb_clone(global->nl_cb);
6241fcb1
JM
420 if (!cb)
421 goto out;
422
58f6fbe0 423 err = nl_send_auto_complete(nl_handle, msg);
6241fcb1
JM
424 if (err < 0)
425 goto out;
426
427 err = 1;
428
429 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
8e8df255 430 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6241fcb1
JM
431 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
432
433 if (valid_handler)
434 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
435 valid_handler, valid_data);
436
437 while (err > 0)
58f6fbe0 438 nl_recvmsgs(nl_handle, cb);
6241fcb1
JM
439 out:
440 nl_cb_put(cb);
441 nlmsg_free(msg);
442 return err;
443}
444
445
d6c9aab8
JB
446static int send_and_recv_msgs_global(struct nl80211_global *global,
447 struct nl_msg *msg,
448 int (*valid_handler)(struct nl_msg *, void *),
449 void *valid_data)
450{
481234cf 451 return send_and_recv(global, global->nl, msg, valid_handler,
d6c9aab8
JB
452 valid_data);
453}
454
455
58f6fbe0
JM
456static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
457 struct nl_msg *msg,
458 int (*valid_handler)(struct nl_msg *, void *),
459 void *valid_data)
460{
481234cf 461 return send_and_recv(drv->global, drv->global->nl, msg,
d6c9aab8 462 valid_handler, valid_data);
58f6fbe0
JM
463}
464
465
97865538
JM
466struct family_data {
467 const char *group;
468 int id;
469};
470
471
472static int family_handler(struct nl_msg *msg, void *arg)
473{
474 struct family_data *res = arg;
475 struct nlattr *tb[CTRL_ATTR_MAX + 1];
476 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
477 struct nlattr *mcgrp;
478 int i;
479
480 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
481 genlmsg_attrlen(gnlh, 0), NULL);
482 if (!tb[CTRL_ATTR_MCAST_GROUPS])
483 return NL_SKIP;
484
485 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
486 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
487 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
488 nla_len(mcgrp), NULL);
489 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
490 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
491 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
492 res->group,
493 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
494 continue;
495 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
496 break;
497 };
498
499 return NL_SKIP;
500}
501
502
d6c9aab8 503static int nl_get_multicast_id(struct nl80211_global *global,
97865538
JM
504 const char *family, const char *group)
505{
506 struct nl_msg *msg;
507 int ret = -1;
508 struct family_data res = { group, -ENOENT };
509
510 msg = nlmsg_alloc();
511 if (!msg)
512 return -ENOMEM;
481234cf 513 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
97865538
JM
514 0, 0, CTRL_CMD_GETFAMILY, 0);
515 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
516
d6c9aab8 517 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
97865538
JM
518 msg = NULL;
519 if (ret == 0)
520 ret = res.id;
521
522nla_put_failure:
523 nlmsg_free(msg);
524 return ret;
525}
526
527
9fb04070
JM
528static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
529 struct nl_msg *msg, int flags, uint8_t cmd)
530{
335d42b1 531 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
276e2d67 532 0, flags, cmd, 0);
9fb04070
JM
533}
534
535
e32ad281
JB
536struct wiphy_idx_data {
537 int wiphy_idx;
538};
539
540
541static int netdev_info_handler(struct nl_msg *msg, void *arg)
542{
543 struct nlattr *tb[NL80211_ATTR_MAX + 1];
544 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
545 struct wiphy_idx_data *info = arg;
546
547 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
548 genlmsg_attrlen(gnlh, 0), NULL);
549
550 if (tb[NL80211_ATTR_WIPHY])
551 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
552
553 return NL_SKIP;
554}
555
556
557static int nl80211_get_wiphy_index(struct i802_bss *bss)
558{
559 struct nl_msg *msg;
560 struct wiphy_idx_data data = {
561 .wiphy_idx = -1,
562 };
563
564 msg = nlmsg_alloc();
565 if (!msg)
566 return -1;
567
568 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
569
570 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
571
572 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
573 return data.wiphy_idx;
574 msg = NULL;
575nla_put_failure:
576 nlmsg_free(msg);
577 return -1;
578}
579
580
581static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
582 struct nl80211_wiphy_data *w)
583{
584 struct nl_msg *msg;
585 int ret = -1;
586
587 msg = nlmsg_alloc();
588 if (!msg)
589 return -1;
590
591 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
592
593 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
594
481234cf 595 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
e32ad281
JB
596 msg = NULL;
597 if (ret) {
598 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
599 "failed: ret=%d (%s)",
600 ret, strerror(-ret));
601 goto nla_put_failure;
602 }
603 ret = 0;
604nla_put_failure:
605 nlmsg_free(msg);
606 return ret;
607}
608
609
610static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
611{
612 struct nl80211_wiphy_data *w = eloop_ctx;
613
ee9fc67a 614 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
e32ad281
JB
615
616 nl_recvmsgs(handle, w->nl_cb);
617}
618
619
620static int process_beacon_event(struct nl_msg *msg, void *arg)
621{
622 struct nl80211_wiphy_data *w = arg;
623 struct wpa_driver_nl80211_data *drv;
624 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
625 struct nlattr *tb[NL80211_ATTR_MAX + 1];
626 union wpa_event_data event;
627
628 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
629 genlmsg_attrlen(gnlh, 0), NULL);
630
631 if (gnlh->cmd != NL80211_CMD_FRAME) {
632 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
633 gnlh->cmd);
634 return NL_SKIP;
635 }
636
637 if (!tb[NL80211_ATTR_FRAME])
638 return NL_SKIP;
639
640 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
641 wiphy_list) {
642 os_memset(&event, 0, sizeof(event));
643 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
644 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
645 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
646 }
647
648 return NL_SKIP;
649}
650
651
652static struct nl80211_wiphy_data *
653nl80211_get_wiphy_data_ap(struct i802_bss *bss)
654{
655 static DEFINE_DL_LIST(nl80211_wiphys);
656 struct nl80211_wiphy_data *w;
657 int wiphy_idx, found = 0;
658 struct i802_bss *tmp_bss;
659
660 if (bss->wiphy_data != NULL)
661 return bss->wiphy_data;
662
663 wiphy_idx = nl80211_get_wiphy_index(bss);
664
665 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
666 if (w->wiphy_idx == wiphy_idx)
667 goto add;
668 }
669
670 /* alloc new one */
671 w = os_zalloc(sizeof(*w));
672 if (w == NULL)
673 return NULL;
674 w->wiphy_idx = wiphy_idx;
675 dl_list_init(&w->bsss);
676 dl_list_init(&w->drvs);
677
678 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
679 if (!w->nl_cb) {
680 os_free(w);
681 return NULL;
682 }
683 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
684 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
685 w);
686
481234cf
JM
687 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
688 "wiphy beacons");
689 if (w->nl_beacons == NULL) {
e32ad281
JB
690 os_free(w);
691 return NULL;
692 }
693
694 if (nl80211_register_beacons(bss->drv, w)) {
695 nl_destroy_handles(&w->nl_beacons);
696 os_free(w);
697 return NULL;
698 }
699
481234cf
JM
700 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
701 nl80211_recv_beacons, w, w->nl_beacons);
e32ad281
JB
702
703 dl_list_add(&nl80211_wiphys, &w->list);
704
705add:
706 /* drv entry for this bss already there? */
707 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
708 if (tmp_bss->drv == bss->drv) {
709 found = 1;
710 break;
711 }
712 }
713 /* if not add it */
714 if (!found)
715 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
716
717 dl_list_add(&w->bsss, &bss->wiphy_list);
718 bss->wiphy_data = w;
719 return w;
720}
721
722
723static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
724{
725 struct nl80211_wiphy_data *w = bss->wiphy_data;
726 struct i802_bss *tmp_bss;
727 int found = 0;
728
729 if (w == NULL)
730 return;
731 bss->wiphy_data = NULL;
732 dl_list_del(&bss->wiphy_list);
733
734 /* still any for this drv present? */
735 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
736 if (tmp_bss->drv == bss->drv) {
737 found = 1;
738 break;
739 }
740 }
741 /* if not remove it */
742 if (!found)
743 dl_list_del(&bss->drv->wiphy_list);
744
745 if (!dl_list_empty(&w->bsss))
746 return;
747
481234cf 748 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
e32ad281
JB
749
750 nl_cb_put(w->nl_cb);
751 nl_destroy_handles(&w->nl_beacons);
752 dl_list_del(&w->list);
753 os_free(w);
754}
755
756
3f5285e8
JM
757static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
758{
a2e40bb6
FF
759 struct i802_bss *bss = priv;
760 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
761 if (!drv->associated)
762 return -1;
763 os_memcpy(bssid, drv->bssid, ETH_ALEN);
764 return 0;
3f5285e8
JM
765}
766
767
3f5285e8
JM
768static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
769{
a2e40bb6
FF
770 struct i802_bss *bss = priv;
771 struct wpa_driver_nl80211_data *drv = bss->drv;
fd05d64e
JM
772 if (!drv->associated)
773 return -1;
774 os_memcpy(ssid, drv->ssid, drv->ssid_len);
775 return drv->ssid_len;
3f5285e8
JM
776}
777
778
7524cfb1 779static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
08063178 780 char *buf, size_t len, int del)
3f5285e8
JM
781{
782 union wpa_event_data event;
783
784 os_memset(&event, 0, sizeof(event));
785 if (len > sizeof(event.interface_status.ifname))
786 len = sizeof(event.interface_status.ifname) - 1;
787 os_memcpy(event.interface_status.ifname, buf, len);
788 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
789 EVENT_INTERFACE_ADDED;
790
791 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
792 del ? "DEL" : "NEW",
793 event.interface_status.ifname,
794 del ? "removed" : "added");
795
a2e40bb6 796 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
d1f4942b
JM
797 if (del) {
798 if (drv->if_removed) {
799 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
800 "already set - ignore event");
801 return;
802 }
7524cfb1 803 drv->if_removed = 1;
d1f4942b
JM
804 } else {
805 if (if_nametoindex(drv->first_bss.ifname) == 0) {
806 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
807 "does not exist - ignore "
808 "RTM_NEWLINK",
809 drv->first_bss.ifname);
810 return;
811 }
812 if (!drv->if_removed) {
813 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
814 "already cleared - ignore event");
815 return;
816 }
7524cfb1 817 drv->if_removed = 0;
d1f4942b 818 }
7524cfb1
JM
819 }
820
08063178 821 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
3f5285e8
JM
822}
823
824
7524cfb1 825static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
62d680c3 826 u8 *buf, size_t len)
7524cfb1 827{
62d680c3 828 int attrlen, rta_len;
7524cfb1
JM
829 struct rtattr *attr;
830
62d680c3
JM
831 attrlen = len;
832 attr = (struct rtattr *) buf;
7524cfb1
JM
833
834 rta_len = RTA_ALIGN(sizeof(struct rtattr));
835 while (RTA_OK(attr, attrlen)) {
836 if (attr->rta_type == IFLA_IFNAME) {
a2e40bb6 837 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
7524cfb1
JM
838 == 0)
839 return 1;
840 else
841 break;
842 }
843 attr = RTA_NEXT(attr, attrlen);
844 }
845
846 return 0;
847}
848
849
850static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
62d680c3 851 int ifindex, u8 *buf, size_t len)
7524cfb1
JM
852{
853 if (drv->ifindex == ifindex)
854 return 1;
855
62d680c3 856 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
a2e40bb6 857 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
7524cfb1
JM
858 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
859 "interface");
860 wpa_driver_nl80211_finish_drv_init(drv);
861 return 1;
862 }
863
864 return 0;
865}
866
867
36d84860
BG
868static struct wpa_driver_nl80211_data *
869nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
870{
871 struct wpa_driver_nl80211_data *drv;
872 dl_list_for_each(drv, &global->interfaces,
873 struct wpa_driver_nl80211_data, list) {
874 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
875 have_ifidx(drv, idx))
876 return drv;
877 }
878 return NULL;
879}
880
881
62d680c3
JM
882static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
883 struct ifinfomsg *ifi,
884 u8 *buf, size_t len)
3f5285e8 885{
36d84860
BG
886 struct nl80211_global *global = ctx;
887 struct wpa_driver_nl80211_data *drv;
62d680c3
JM
888 int attrlen, rta_len;
889 struct rtattr *attr;
97cfcf64 890 u32 brid = 0;
aef85ba2 891 char namebuf[IFNAMSIZ];
3f5285e8 892
36d84860
BG
893 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
894 if (!drv) {
97cfcf64
B
895 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
896 "ifindex %d", ifi->ifi_index);
3f5285e8
JM
897 return;
898 }
899
900 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
901 "(%s%s%s%s)",
902 drv->operstate, ifi->ifi_flags,
903 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
904 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
905 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
906 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
a63063b4
JM
907
908 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
59d24925
JM
909 if (if_indextoname(ifi->ifi_index, namebuf) &&
910 linux_iface_up(drv->global->ioctl_sock,
911 drv->first_bss.ifname) > 0) {
912 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
913 "event since interface %s is up", namebuf);
914 return;
915 }
a63063b4 916 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
7d9c3698
JM
917 if (drv->ignore_if_down_event) {
918 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
919 "event generated by mode change");
920 drv->ignore_if_down_event = 0;
921 } else {
922 drv->if_disabled = 1;
923 wpa_supplicant_event(drv->ctx,
924 EVENT_INTERFACE_DISABLED, NULL);
925 }
a63063b4
JM
926 }
927
928 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
aef85ba2
JM
929 if (if_indextoname(ifi->ifi_index, namebuf) &&
930 linux_iface_up(drv->global->ioctl_sock,
931 drv->first_bss.ifname) == 0) {
932 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
933 "event since interface %s is down",
934 namebuf);
d1f4942b
JM
935 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
936 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
937 "event since interface %s does not exist",
938 drv->first_bss.ifname);
939 } else if (drv->if_removed) {
940 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
941 "event since interface %s is marked "
942 "removed", drv->first_bss.ifname);
aef85ba2
JM
943 } else {
944 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
945 drv->if_disabled = 0;
946 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
947 NULL);
948 }
a63063b4
JM
949 }
950
3f5285e8
JM
951 /*
952 * Some drivers send the association event before the operup event--in
953 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
954 * fails. This will hit us when wpa_supplicant does not need to do
955 * IEEE 802.1X authentication
956 */
957 if (drv->operstate == 1 &&
958 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
959 !(ifi->ifi_flags & IFF_RUNNING))
36d84860 960 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 961 -1, IF_OPER_UP);
3f5285e8 962
62d680c3
JM
963 attrlen = len;
964 attr = (struct rtattr *) buf;
3f5285e8
JM
965 rta_len = RTA_ALIGN(sizeof(struct rtattr));
966 while (RTA_OK(attr, attrlen)) {
d8816397 967 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 968 wpa_driver_nl80211_event_link(
08063178 969 drv,
7524cfb1
JM
970 ((char *) attr) + rta_len,
971 attr->rta_len - rta_len, 0);
97cfcf64
B
972 } else if (attr->rta_type == IFLA_MASTER)
973 brid = nla_get_u32((struct nlattr *) attr);
3f5285e8
JM
974 attr = RTA_NEXT(attr, attrlen);
975 }
97cfcf64
B
976
977 if (ifi->ifi_family == AF_BRIDGE && brid) {
978 /* device has been added to bridge */
97cfcf64
B
979 if_indextoname(brid, namebuf);
980 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
981 brid, namebuf);
982 add_ifidx(drv, brid);
983 }
3f5285e8
JM
984}
985
986
62d680c3
JM
987static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
988 struct ifinfomsg *ifi,
989 u8 *buf, size_t len)
3f5285e8 990{
36d84860
BG
991 struct nl80211_global *global = ctx;
992 struct wpa_driver_nl80211_data *drv;
62d680c3
JM
993 int attrlen, rta_len;
994 struct rtattr *attr;
97cfcf64 995 u32 brid = 0;
3f5285e8 996
36d84860
BG
997 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
998 if (!drv) {
999 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1000 "foreign ifindex %d", ifi->ifi_index);
1001 return;
1002 }
1003
62d680c3
JM
1004 attrlen = len;
1005 attr = (struct rtattr *) buf;
3f5285e8
JM
1006
1007 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1008 while (RTA_OK(attr, attrlen)) {
1009 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 1010 wpa_driver_nl80211_event_link(
08063178 1011 drv,
7524cfb1
JM
1012 ((char *) attr) + rta_len,
1013 attr->rta_len - rta_len, 1);
97cfcf64
B
1014 } else if (attr->rta_type == IFLA_MASTER)
1015 brid = nla_get_u32((struct nlattr *) attr);
3f5285e8
JM
1016 attr = RTA_NEXT(attr, attrlen);
1017 }
97cfcf64
B
1018
1019 if (ifi->ifi_family == AF_BRIDGE && brid) {
1020 /* device has been removed from bridge */
1021 char namebuf[IFNAMSIZ];
1022 if_indextoname(brid, namebuf);
1023 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1024 "%s", brid, namebuf);
1025 del_ifidx(drv, brid);
1026 }
3f5285e8
JM
1027}
1028
1029
c2a04078
JM
1030static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1031 const u8 *frame, size_t len)
1032{
1033 const struct ieee80211_mgmt *mgmt;
1034 union wpa_event_data event;
1035
7d81932d 1036 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
c2a04078
JM
1037 mgmt = (const struct ieee80211_mgmt *) frame;
1038 if (len < 24 + sizeof(mgmt->u.auth)) {
1039 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1040 "frame");
1041 return;
1042 }
1043
e6b8efeb 1044 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
c2a04078
JM
1045 os_memset(&event, 0, sizeof(event));
1046 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1047 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
569fed90
JM
1048 event.auth.auth_transaction =
1049 le_to_host16(mgmt->u.auth.auth_transaction);
c2a04078
JM
1050 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1051 if (len > 24 + sizeof(mgmt->u.auth)) {
1052 event.auth.ies = mgmt->u.auth.variable;
1053 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1054 }
1055
1056 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1057}
1058
1059
f5a8d422
JM
1060static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1061{
1062 struct nl_msg *msg;
1063 int ret;
1064 struct nl80211_bss_info_arg arg;
1065
1066 os_memset(&arg, 0, sizeof(arg));
1067 msg = nlmsg_alloc();
1068 if (!msg)
1069 goto nla_put_failure;
1070
9fb04070 1071 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
f5a8d422
JM
1072 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1073
1074 arg.drv = drv;
1075 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1076 msg = NULL;
1077 if (ret == 0) {
1078 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1079 "associated BSS from scan results: %u MHz",
1080 arg.assoc_freq);
1081 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1082 }
1083 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1084 "(%s)", ret, strerror(-ret));
1085nla_put_failure:
1086 nlmsg_free(msg);
1087 return drv->assoc_freq;
1088}
1089
1090
c2a04078
JM
1091static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1092 const u8 *frame, size_t len)
1093{
1094 const struct ieee80211_mgmt *mgmt;
1095 union wpa_event_data event;
1096 u16 status;
1097
7d81932d 1098 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
c2a04078
JM
1099 mgmt = (const struct ieee80211_mgmt *) frame;
1100 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1101 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1102 "frame");
1103 return;
1104 }
1105
1106 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1107 if (status != WLAN_STATUS_SUCCESS) {
efa46078 1108 os_memset(&event, 0, sizeof(event));
59ddf221 1109 event.assoc_reject.bssid = mgmt->bssid;
efa46078
JM
1110 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1111 event.assoc_reject.resp_ies =
1112 (u8 *) mgmt->u.assoc_resp.variable;
1113 event.assoc_reject.resp_ies_len =
1114 len - 24 - sizeof(mgmt->u.assoc_resp);
1115 }
1116 event.assoc_reject.status_code = status;
1117
1118 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
c2a04078
JM
1119 return;
1120 }
1121
1122 drv->associated = 1;
1123 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1124
1125 os_memset(&event, 0, sizeof(event));
1126 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1127 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1128 event.assoc_info.resp_ies_len =
efa46078 1129 len - 24 - sizeof(mgmt->u.assoc_resp);
c2a04078
JM
1130 }
1131
4832ecd7
JM
1132 event.assoc_info.freq = drv->assoc_freq;
1133
c2a04078
JM
1134 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1135}
1136
c1bb3e0a 1137
da72a1c1
ZY
1138static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1139 enum nl80211_commands cmd, struct nlattr *status,
1140 struct nlattr *addr, struct nlattr *req_ie,
1141 struct nlattr *resp_ie)
1142{
1143 union wpa_event_data event;
1144
7da2c527
JM
1145 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1146 /*
1147 * Avoid reporting two association events that would confuse
1148 * the core code.
1149 */
1150 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1151 "when using userspace SME", cmd);
1152 return;
1153 }
1154
7d81932d
JM
1155 if (cmd == NL80211_CMD_CONNECT)
1156 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1157 else if (cmd == NL80211_CMD_ROAM)
1158 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1159
da72a1c1
ZY
1160 os_memset(&event, 0, sizeof(event));
1161 if (cmd == NL80211_CMD_CONNECT &&
1162 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
df89c1c8
JM
1163 if (addr)
1164 event.assoc_reject.bssid = nla_data(addr);
da72a1c1
ZY
1165 if (resp_ie) {
1166 event.assoc_reject.resp_ies = nla_data(resp_ie);
1167 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1168 }
1169 event.assoc_reject.status_code = nla_get_u16(status);
1170 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1171 return;
1172 }
1173
1174 drv->associated = 1;
1175 if (addr)
1176 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1177
1178 if (req_ie) {
1179 event.assoc_info.req_ies = nla_data(req_ie);
1180 event.assoc_info.req_ies_len = nla_len(req_ie);
1181 }
1182 if (resp_ie) {
1183 event.assoc_info.resp_ies = nla_data(resp_ie);
1184 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1185 }
1186
f5a8d422
JM
1187 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1188
da72a1c1
ZY
1189 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1190}
c2a04078 1191
c1bb3e0a 1192
20f5a4c2 1193static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
3d9975d5
JM
1194 struct nlattr *reason, struct nlattr *addr,
1195 struct nlattr *by_ap)
20f5a4c2
JM
1196{
1197 union wpa_event_data data;
a8c5b43a 1198 unsigned int locally_generated = by_ap == NULL;
20f5a4c2
JM
1199
1200 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1201 /*
1202 * Avoid reporting two disassociation events that could
1203 * confuse the core code.
1204 */
1205 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1206 "event when using userspace SME");
1207 return;
1208 }
1209
a8c5b43a
CW
1210 if (drv->ignore_next_local_disconnect) {
1211 drv->ignore_next_local_disconnect = 0;
1212 if (locally_generated) {
1213 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1214 "event triggered during reassociation");
1215 return;
1216 }
1217 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1218 "disconnect but got another disconnect "
1219 "event first");
1220 }
1221
7d81932d 1222 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
20f5a4c2
JM
1223 drv->associated = 0;
1224 os_memset(&data, 0, sizeof(data));
1225 if (reason)
2e9f078c
JM
1226 data.deauth_info.reason_code = nla_get_u16(reason);
1227 data.deauth_info.locally_generated = by_ap == NULL;
1228 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
20f5a4c2
JM
1229}
1230
1231
1b487b8b
TP
1232static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1233 struct nlattr *freq, struct nlattr *type)
1234{
1235 union wpa_event_data data;
1236 int ht_enabled = 1;
1237 int chan_offset = 0;
1238
1239 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1240
1241 if (!freq || !type)
1242 return;
1243
1244 switch (nla_get_u32(type)) {
1245 case NL80211_CHAN_NO_HT:
1246 ht_enabled = 0;
1247 break;
1248 case NL80211_CHAN_HT20:
1249 break;
1250 case NL80211_CHAN_HT40PLUS:
1251 chan_offset = 1;
1252 break;
1253 case NL80211_CHAN_HT40MINUS:
1254 chan_offset = -1;
1255 break;
1256 }
1257
1258 data.ch_switch.freq = nla_get_u32(freq);
1259 data.ch_switch.ht_enabled = ht_enabled;
1260 data.ch_switch.ch_offset = chan_offset;
1261
1262 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
1263}
1264
1265
da1fb17c
JM
1266static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1267 enum nl80211_commands cmd, struct nlattr *addr)
1268{
1269 union wpa_event_data event;
1270 enum wpa_event_type ev;
1271
1272 if (nla_len(addr) != ETH_ALEN)
1273 return;
1274
1275 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1276 cmd, MAC2STR((u8 *) nla_data(addr)));
1277
1278 if (cmd == NL80211_CMD_AUTHENTICATE)
1279 ev = EVENT_AUTH_TIMED_OUT;
1280 else if (cmd == NL80211_CMD_ASSOCIATE)
1281 ev = EVENT_ASSOC_TIMED_OUT;
1282 else
1283 return;
1284
1285 os_memset(&event, 0, sizeof(event));
1286 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1287 wpa_supplicant_event(drv->ctx, ev, &event);
1288}
1289
1290
5582a5d1 1291static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
da873dbb
JB
1292 struct nlattr *freq, struct nlattr *sig,
1293 const u8 *frame, size_t len)
58f6fbe0
JM
1294{
1295 const struct ieee80211_mgmt *mgmt;
1296 union wpa_event_data event;
1297 u16 fc, stype;
da873dbb 1298 int ssi_signal = 0;
58f6fbe0 1299
7d81932d 1300 wpa_printf(MSG_DEBUG, "nl80211: Frame event");
58f6fbe0
JM
1301 mgmt = (const struct ieee80211_mgmt *) frame;
1302 if (len < 24) {
1303 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1304 return;
1305 }
1306
1307 fc = le_to_host16(mgmt->frame_control);
1308 stype = WLAN_FC_GET_STYPE(fc);
1309
da873dbb
JB
1310 if (sig)
1311 ssi_signal = (s32) nla_get_u32(sig);
1312
58f6fbe0 1313 os_memset(&event, 0, sizeof(event));
5582a5d1 1314 if (freq) {
58f6fbe0 1315 event.rx_action.freq = nla_get_u32(freq);
5582a5d1
JB
1316 drv->last_mgmt_freq = event.rx_action.freq;
1317 }
1318 if (stype == WLAN_FC_STYPE_ACTION) {
1319 event.rx_action.da = mgmt->da;
1320 event.rx_action.sa = mgmt->sa;
1321 event.rx_action.bssid = mgmt->bssid;
1322 event.rx_action.category = mgmt->u.action.category;
1323 event.rx_action.data = &mgmt->u.action.category + 1;
1324 event.rx_action.len = frame + len - event.rx_action.data;
1325 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1326 } else {
1327 event.rx_mgmt.frame = frame;
1328 event.rx_mgmt.frame_len = len;
da873dbb 1329 event.rx_mgmt.ssi_signal = ssi_signal;
5582a5d1
JB
1330 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1331 }
58f6fbe0
JM
1332}
1333
1334
a11241fa
JB
1335static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1336 struct nlattr *cookie, const u8 *frame,
1337 size_t len, struct nlattr *ack)
58f6fbe0
JM
1338{
1339 union wpa_event_data event;
1340 const struct ieee80211_hdr *hdr;
1341 u16 fc;
58f6fbe0 1342
7d81932d 1343 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
a11241fa
JB
1344 if (!is_ap_interface(drv->nlmode)) {
1345 u64 cookie_val;
58f6fbe0 1346
a11241fa
JB
1347 if (!cookie)
1348 return;
1349
1350 cookie_val = nla_get_u64(cookie);
1351 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1352 " cookie=0%llx%s (ack=%d)",
1353 (long long unsigned int) cookie_val,
1354 cookie_val == drv->send_action_cookie ?
1355 " (match)" : " (unknown)", ack != NULL);
1356 if (cookie_val != drv->send_action_cookie)
1357 return;
1358 }
58f6fbe0
JM
1359
1360 hdr = (const struct ieee80211_hdr *) frame;
1361 fc = le_to_host16(hdr->frame_control);
1362
1363 os_memset(&event, 0, sizeof(event));
1364 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1365 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1366 event.tx_status.dst = hdr->addr1;
1367 event.tx_status.data = frame;
1368 event.tx_status.data_len = len;
1369 event.tx_status.ack = ack != NULL;
1370 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1371}
1372
1373
0544b242
JM
1374static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1375 enum wpa_event_type type,
1376 const u8 *frame, size_t len)
1377{
1378 const struct ieee80211_mgmt *mgmt;
1379 union wpa_event_data event;
1380 const u8 *bssid = NULL;
1381 u16 reason_code = 0;
1382
7d81932d
JM
1383 if (type == EVENT_DEAUTH)
1384 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1385 else
1386 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1387
cb30b297
PS
1388 mgmt = (const struct ieee80211_mgmt *) frame;
1389 if (len >= 24) {
1390 bssid = mgmt->bssid;
1391
1392 if (drv->associated != 0 &&
1393 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1394 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1395 /*
1396 * We have presumably received this deauth as a
1397 * response to a clear_state_mismatch() outgoing
1398 * deauth. Don't let it take us offline!
1399 */
1400 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1401 "from Unknown BSSID " MACSTR " -- ignoring",
1402 MAC2STR(bssid));
1403 return;
1404 }
1405 }
1406
0544b242
JM
1407 drv->associated = 0;
1408 os_memset(&event, 0, sizeof(event));
1409
0544b242
JM
1410 /* Note: Same offset for Reason Code in both frame subtypes */
1411 if (len >= 24 + sizeof(mgmt->u.deauth))
1412 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1413
1414 if (type == EVENT_DISASSOC) {
3d9975d5
JM
1415 event.disassoc_info.locally_generated =
1416 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
0544b242
JM
1417 event.disassoc_info.addr = bssid;
1418 event.disassoc_info.reason_code = reason_code;
046b26a2
JM
1419 if (frame + len > mgmt->u.disassoc.variable) {
1420 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1421 event.disassoc_info.ie_len = frame + len -
1422 mgmt->u.disassoc.variable;
1423 }
0544b242 1424 } else {
3d9975d5
JM
1425 event.deauth_info.locally_generated =
1426 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
0544b242
JM
1427 event.deauth_info.addr = bssid;
1428 event.deauth_info.reason_code = reason_code;
046b26a2
JM
1429 if (frame + len > mgmt->u.deauth.variable) {
1430 event.deauth_info.ie = mgmt->u.deauth.variable;
1431 event.deauth_info.ie_len = frame + len -
1432 mgmt->u.deauth.variable;
1433 }
0544b242
JM
1434 }
1435
1436 wpa_supplicant_event(drv->ctx, type, &event);
1437}
1438
1439
7d878ca7
JM
1440static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1441 enum wpa_event_type type,
1442 const u8 *frame, size_t len)
1443{
1444 const struct ieee80211_mgmt *mgmt;
1445 union wpa_event_data event;
1446 u16 reason_code = 0;
1447
7d81932d
JM
1448 if (type == EVENT_UNPROT_DEAUTH)
1449 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1450 else
1451 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1452
7d878ca7
JM
1453 if (len < 24)
1454 return;
1455
1456 mgmt = (const struct ieee80211_mgmt *) frame;
1457
1458 os_memset(&event, 0, sizeof(event));
1459 /* Note: Same offset for Reason Code in both frame subtypes */
1460 if (len >= 24 + sizeof(mgmt->u.deauth))
1461 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1462
1463 if (type == EVENT_UNPROT_DISASSOC) {
1464 event.unprot_disassoc.sa = mgmt->sa;
1465 event.unprot_disassoc.da = mgmt->da;
1466 event.unprot_disassoc.reason_code = reason_code;
1467 } else {
1468 event.unprot_deauth.sa = mgmt->sa;
1469 event.unprot_deauth.da = mgmt->da;
1470 event.unprot_deauth.reason_code = reason_code;
1471 }
1472
1473 wpa_supplicant_event(drv->ctx, type, &event);
1474}
1475
1476
c2a04078 1477static void mlme_event(struct wpa_driver_nl80211_data *drv,
da1fb17c 1478 enum nl80211_commands cmd, struct nlattr *frame,
58f6fbe0
JM
1479 struct nlattr *addr, struct nlattr *timed_out,
1480 struct nlattr *freq, struct nlattr *ack,
da873dbb 1481 struct nlattr *cookie, struct nlattr *sig)
c2a04078 1482{
da1fb17c
JM
1483 if (timed_out && addr) {
1484 mlme_timeout_event(drv, cmd, addr);
1485 return;
1486 }
1487
c2a04078
JM
1488 if (frame == NULL) {
1489 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1490 "data", cmd);
1491 return;
1492 }
1493
1494 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1495 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1496 nla_data(frame), nla_len(frame));
1497
1498 switch (cmd) {
1499 case NL80211_CMD_AUTHENTICATE:
1500 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1501 break;
1502 case NL80211_CMD_ASSOCIATE:
1503 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1504 break;
1505 case NL80211_CMD_DEAUTHENTICATE:
0544b242
JM
1506 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1507 nla_data(frame), nla_len(frame));
c2a04078
JM
1508 break;
1509 case NL80211_CMD_DISASSOCIATE:
0544b242
JM
1510 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1511 nla_data(frame), nla_len(frame));
c2a04078 1512 break;
bd94971e 1513 case NL80211_CMD_FRAME:
da873dbb
JB
1514 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1515 nla_len(frame));
58f6fbe0 1516 break;
bd94971e 1517 case NL80211_CMD_FRAME_TX_STATUS:
a11241fa
JB
1518 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1519 nla_len(frame), ack);
58f6fbe0 1520 break;
7d878ca7
JM
1521 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1522 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1523 nla_data(frame), nla_len(frame));
1524 break;
1525 case NL80211_CMD_UNPROT_DISASSOCIATE:
1526 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1527 nla_data(frame), nla_len(frame));
1528 break;
c2a04078
JM
1529 default:
1530 break;
1531 }
1532}
1533
1534
a5e1eb20 1535static void mlme_event_michael_mic_failure(struct i802_bss *bss,
35583f3f
JM
1536 struct nlattr *tb[])
1537{
1538 union wpa_event_data data;
1539
1540 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1541 os_memset(&data, 0, sizeof(data));
1542 if (tb[NL80211_ATTR_MAC]) {
1543 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1544 nla_data(tb[NL80211_ATTR_MAC]),
1545 nla_len(tb[NL80211_ATTR_MAC]));
ad1e68e6 1546 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
35583f3f
JM
1547 }
1548 if (tb[NL80211_ATTR_KEY_SEQ]) {
1549 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1550 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1551 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1552 }
1553 if (tb[NL80211_ATTR_KEY_TYPE]) {
1554 enum nl80211_key_type key_type =
1555 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1556 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1557 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1558 data.michael_mic_failure.unicast = 1;
1559 } else
1560 data.michael_mic_failure.unicast = 1;
1561
1562 if (tb[NL80211_ATTR_KEY_IDX]) {
1563 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1564 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1565 }
1566
a5e1eb20 1567 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
35583f3f
JM
1568}
1569
1570
5cc4d64b
JM
1571static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1572 struct nlattr *tb[])
1573{
1574 if (tb[NL80211_ATTR_MAC] == NULL) {
1575 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1576 "event");
1577 return;
1578 }
1579 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1580 drv->associated = 1;
1581 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1582 MAC2STR(drv->bssid));
1583
1584 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1585}
1586
1587
55777702
JM
1588static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1589 int cancel_event, struct nlattr *tb[])
1590{
1591 unsigned int freq, chan_type, duration;
1592 union wpa_event_data data;
1593 u64 cookie;
1594
1595 if (tb[NL80211_ATTR_WIPHY_FREQ])
1596 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1597 else
1598 freq = 0;
1599
1600 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1601 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1602 else
1603 chan_type = 0;
1604
1605 if (tb[NL80211_ATTR_DURATION])
1606 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1607 else
1608 duration = 0;
1609
1610 if (tb[NL80211_ATTR_COOKIE])
1611 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1612 else
1613 cookie = 0;
1614
1615 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1616 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1617 cancel_event, freq, chan_type, duration,
1618 (long long unsigned int) cookie,
1619 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1620
1621 if (cookie != drv->remain_on_chan_cookie)
1622 return; /* not for us */
1623
531f0331
JB
1624 if (cancel_event)
1625 drv->pending_remain_on_chan = 0;
55777702
JM
1626
1627 os_memset(&data, 0, sizeof(data));
1628 data.remain_on_channel.freq = freq;
1629 data.remain_on_channel.duration = duration;
1630 wpa_supplicant_event(drv->ctx, cancel_event ?
1631 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1632 EVENT_REMAIN_ON_CHANNEL, &data);
1633}
1634
1635
8d923a4a
JM
1636static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1637 struct nlattr *tb[])
1638{
1639 union wpa_event_data event;
1640 struct nlattr *nl;
1641 int rem;
1642 struct scan_info *info;
1643#define MAX_REPORT_FREQS 50
1644 int freqs[MAX_REPORT_FREQS];
1645 int num_freqs = 0;
1646
536fd62d
JM
1647 if (drv->scan_for_auth) {
1648 drv->scan_for_auth = 0;
1649 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1650 "cfg80211 BSS entry");
1651 wpa_driver_nl80211_authenticate_retry(drv);
1652 return;
1653 }
1654
8d923a4a
JM
1655 os_memset(&event, 0, sizeof(event));
1656 info = &event.scan_info;
1657 info->aborted = aborted;
1658
1659 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1660 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1661 struct wpa_driver_scan_ssid *s =
1662 &info->ssids[info->num_ssids];
1663 s->ssid = nla_data(nl);
1664 s->ssid_len = nla_len(nl);
1665 info->num_ssids++;
1666 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1667 break;
1668 }
1669 }
1670 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1671 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1672 {
1673 freqs[num_freqs] = nla_get_u32(nl);
1674 num_freqs++;
1675 if (num_freqs == MAX_REPORT_FREQS - 1)
1676 break;
1677 }
1678 info->freqs = freqs;
1679 info->num_freqs = num_freqs;
1680 }
1681 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1682}
1683
1684
60a972a6
JM
1685static int get_link_signal(struct nl_msg *msg, void *arg)
1686{
1687 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1688 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1689 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1690 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1691 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1692 };
7ee35bf3
PS
1693 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1694 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1695 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1696 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1697 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1698 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1699 };
1c5c7273 1700 struct wpa_signal_info *sig_change = arg;
60a972a6
JM
1701
1702 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1703 genlmsg_attrlen(gnlh, 0), NULL);
1704 if (!tb[NL80211_ATTR_STA_INFO] ||
1705 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1706 tb[NL80211_ATTR_STA_INFO], policy))
1707 return NL_SKIP;
1708 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1709 return NL_SKIP;
1710
7ee35bf3
PS
1711 sig_change->current_signal =
1712 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1713
1714 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1715 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1716 sinfo[NL80211_STA_INFO_TX_BITRATE],
1717 rate_policy)) {
1718 sig_change->current_txrate = 0;
1719 } else {
1720 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1721 sig_change->current_txrate =
1722 nla_get_u16(rinfo[
1723 NL80211_RATE_INFO_BITRATE]) * 100;
1724 }
1725 }
1726 }
1727
60a972a6
JM
1728 return NL_SKIP;
1729}
1730
1731
1732static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1c5c7273 1733 struct wpa_signal_info *sig)
60a972a6
JM
1734{
1735 struct nl_msg *msg;
1736
7ee35bf3
PS
1737 sig->current_signal = -9999;
1738 sig->current_txrate = 0;
60a972a6
JM
1739
1740 msg = nlmsg_alloc();
1741 if (!msg)
1742 return -ENOMEM;
1743
9fb04070 1744 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
60a972a6
JM
1745
1746 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1747 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1748
1749 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1750 nla_put_failure:
5883168a 1751 nlmsg_free(msg);
60a972a6
JM
1752 return -ENOBUFS;
1753}
1754
1755
7ee35bf3
PS
1756static int get_link_noise(struct nl_msg *msg, void *arg)
1757{
1758 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1759 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1760 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1761 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1762 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1763 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1764 };
1c5c7273 1765 struct wpa_signal_info *sig_change = arg;
7ee35bf3
PS
1766
1767 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1768 genlmsg_attrlen(gnlh, 0), NULL);
1769
1770 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1771 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1772 return NL_SKIP;
1773 }
1774
1775 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1776 tb[NL80211_ATTR_SURVEY_INFO],
1777 survey_policy)) {
1778 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1779 "attributes!");
1780 return NL_SKIP;
1781 }
1782
1783 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1784 return NL_SKIP;
1785
1786 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1787 sig_change->frequency)
1788 return NL_SKIP;
1789
1790 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1791 return NL_SKIP;
1792
1793 sig_change->current_noise =
1794 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1795
1796 return NL_SKIP;
1797}
1798
1799
1800static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1c5c7273 1801 struct wpa_signal_info *sig_change)
7ee35bf3
PS
1802{
1803 struct nl_msg *msg;
1804
1805 sig_change->current_noise = 9999;
1806 sig_change->frequency = drv->assoc_freq;
1807
1808 msg = nlmsg_alloc();
1809 if (!msg)
1810 return -ENOMEM;
1811
9fb04070 1812 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
7ee35bf3
PS
1813
1814 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1815
1816 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1817 nla_put_failure:
5883168a 1818 nlmsg_free(msg);
7ee35bf3
PS
1819 return -ENOBUFS;
1820}
1821
1822
577db0ae
GM
1823static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1824{
1825 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1826 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1827 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1828 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1829 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1830 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1831 };
1832 struct wpa_scan_results *scan_results = arg;
1833 struct wpa_scan_res *scan_res;
1834 size_t i;
1835
1836 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1837 genlmsg_attrlen(gnlh, 0), NULL);
1838
1839 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1840 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1841 return NL_SKIP;
1842 }
1843
1844 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1845 tb[NL80211_ATTR_SURVEY_INFO],
1846 survey_policy)) {
1847 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1848 "attributes");
1849 return NL_SKIP;
1850 }
1851
1852 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1853 return NL_SKIP;
1854
1855 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1856 return NL_SKIP;
1857
1858 for (i = 0; i < scan_results->num; ++i) {
1859 scan_res = scan_results->res[i];
1860 if (!scan_res)
1861 continue;
1862 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1863 scan_res->freq)
1864 continue;
1865 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1866 continue;
1867 scan_res->noise = (s8)
1868 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1869 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1870 }
1871
1872 return NL_SKIP;
1873}
1874
1875
1876static int nl80211_get_noise_for_scan_results(
1877 struct wpa_driver_nl80211_data *drv,
1878 struct wpa_scan_results *scan_res)
1879{
1880 struct nl_msg *msg;
1881
1882 msg = nlmsg_alloc();
1883 if (!msg)
1884 return -ENOMEM;
1885
1886 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1887
1888 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1889
1890 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1891 scan_res);
1892 nla_put_failure:
9e088e74 1893 nlmsg_free(msg);
577db0ae
GM
1894 return -ENOBUFS;
1895}
1896
1897
93910401
JM
1898static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1899 struct nlattr *tb[])
1900{
1901 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1902 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1903 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1904 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
0d7e5a3a 1905 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
93910401
JM
1906 };
1907 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1908 enum nl80211_cqm_rssi_threshold_event event;
b625473c 1909 union wpa_event_data ed;
1c5c7273 1910 struct wpa_signal_info sig;
7ee35bf3 1911 int res;
93910401
JM
1912
1913 if (tb[NL80211_ATTR_CQM] == NULL ||
1914 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1915 cqm_policy)) {
1916 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1917 return;
1918 }
1919
0d7e5a3a
JB
1920 os_memset(&ed, 0, sizeof(ed));
1921
1922 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1923 if (!tb[NL80211_ATTR_MAC])
1924 return;
1925 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1926 ETH_ALEN);
1927 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1928 return;
1929 }
1930
93910401
JM
1931 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1932 return;
1933 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
b625473c 1934
93910401
JM
1935 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1936 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1937 "event: RSSI high");
b625473c 1938 ed.signal_change.above_threshold = 1;
93910401
JM
1939 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1940 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1941 "event: RSSI low");
b625473c
JM
1942 ed.signal_change.above_threshold = 0;
1943 } else
1944 return;
1945
60a972a6
JM
1946 res = nl80211_get_link_signal(drv, &sig);
1947 if (res == 0) {
7ee35bf3
PS
1948 ed.signal_change.current_signal = sig.current_signal;
1949 ed.signal_change.current_txrate = sig.current_txrate;
1950 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1951 sig.current_signal, sig.current_txrate);
1952 }
1953
1954 res = nl80211_get_link_noise(drv, &sig);
1955 if (res == 0) {
1956 ed.signal_change.current_noise = sig.current_noise;
1957 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1958 sig.current_noise);
60a972a6
JM
1959 }
1960
b625473c 1961 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
93910401
JM
1962}
1963
1964
18d2ba08
JM
1965static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1966 struct nlattr **tb)
1967{
1968 u8 *addr;
1969 union wpa_event_data data;
1970
1971 if (tb[NL80211_ATTR_MAC] == NULL)
1972 return;
1973 addr = nla_data(tb[NL80211_ATTR_MAC]);
1974 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
5f310a9e 1975
61cbe2ff 1976 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
1977 u8 *ies = NULL;
1978 size_t ies_len = 0;
1979 if (tb[NL80211_ATTR_IE]) {
1980 ies = nla_data(tb[NL80211_ATTR_IE]);
1981 ies_len = nla_len(tb[NL80211_ATTR_IE]);
1982 }
1983 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1984 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1985 return;
1986 }
1987
18d2ba08
JM
1988 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1989 return;
1990
1991 os_memset(&data, 0, sizeof(data));
1992 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1993 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1994}
1995
1996
ef985058
JM
1997static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1998 struct nlattr **tb)
1999{
2000 u8 *addr;
2001 union wpa_event_data data;
2002
2003 if (tb[NL80211_ATTR_MAC] == NULL)
2004 return;
2005 addr = nla_data(tb[NL80211_ATTR_MAC]);
2006 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2007 MAC2STR(addr));
5f310a9e 2008
61cbe2ff 2009 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
5f310a9e
JM
2010 drv_event_disassoc(drv->ctx, addr);
2011 return;
2012 }
2013
ef985058
JM
2014 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2015 return;
2016
2017 os_memset(&data, 0, sizeof(data));
2018 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2019 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2020}
2021
2022
b14a210c
JB
2023static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2024 struct nlattr **tb)
2025{
2026 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2027 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2028 [NL80211_REKEY_DATA_KEK] = {
2029 .minlen = NL80211_KEK_LEN,
2030 .maxlen = NL80211_KEK_LEN,
2031 },
2032 [NL80211_REKEY_DATA_KCK] = {
2033 .minlen = NL80211_KCK_LEN,
2034 .maxlen = NL80211_KCK_LEN,
2035 },
2036 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2037 .minlen = NL80211_REPLAY_CTR_LEN,
2038 .maxlen = NL80211_REPLAY_CTR_LEN,
2039 },
2040 };
2041 union wpa_event_data data;
2042
2043 if (!tb[NL80211_ATTR_MAC])
2044 return;
2045 if (!tb[NL80211_ATTR_REKEY_DATA])
2046 return;
2047 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2048 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2049 return;
2050 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2051 return;
2052
2053 os_memset(&data, 0, sizeof(data));
2054 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2055 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2056 MAC2STR(data.driver_gtk_rekey.bssid));
2057 data.driver_gtk_rekey.replay_ctr =
2058 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2059 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2060 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2061 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2062}
2063
2064
c36d5242
JM
2065static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2066 struct nlattr **tb)
2067{
2068 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2069 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2070 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2071 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2072 .minlen = ETH_ALEN,
2073 .maxlen = ETH_ALEN,
2074 },
2075 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2076 };
2077 union wpa_event_data data;
2078
7d81932d
JM
2079 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2080
c36d5242
JM
2081 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2082 return;
2083 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2084 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2085 return;
2086 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2087 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2088 return;
2089
2090 os_memset(&data, 0, sizeof(data));
2091 os_memcpy(data.pmkid_candidate.bssid,
2092 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2093 data.pmkid_candidate.index =
2094 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2095 data.pmkid_candidate.preauth =
2096 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2097 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2098}
2099
2100
39718852
JB
2101static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2102 struct nlattr **tb)
2103{
2104 union wpa_event_data data;
2105
7d81932d
JM
2106 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2107
39718852
JB
2108 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2109 return;
2110
2111 os_memset(&data, 0, sizeof(data));
2112 os_memcpy(data.client_poll.addr,
2113 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2114
2115 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2116}
2117
2118
6201b052
JM
2119static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2120 struct nlattr **tb)
2121{
2122 union wpa_event_data data;
2123
2124 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2125
2126 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2127 return;
2128
2129 os_memset(&data, 0, sizeof(data));
2130 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2131 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2132 case NL80211_TDLS_SETUP:
2133 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2134 MACSTR, MAC2STR(data.tdls.peer));
2135 data.tdls.oper = TDLS_REQUEST_SETUP;
2136 break;
2137 case NL80211_TDLS_TEARDOWN:
2138 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2139 MACSTR, MAC2STR(data.tdls.peer));
2140 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2141 break;
2142 default:
2143 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2144 "event");
2145 return;
2146 }
2147 if (tb[NL80211_ATTR_REASON_CODE]) {
2148 data.tdls.reason_code =
2149 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2150 }
2151
2152 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2153}
2154
2155
3140803b
RM
2156static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2157 struct nlattr **tb)
2158{
2159 union wpa_event_data data;
2160 u32 reason;
2161
2162 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2163
2164 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2165 return;
2166
2167 os_memset(&data, 0, sizeof(data));
2168 os_memcpy(data.connect_failed_reason.addr,
2169 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2170
2171 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2172 switch (reason) {
2173 case NL80211_CONN_FAIL_MAX_CLIENTS:
2174 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2175 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2176 break;
2177 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2178 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2179 " tried to connect",
2180 MAC2STR(data.connect_failed_reason.addr));
2181 data.connect_failed_reason.code = BLOCKED_CLIENT;
2182 break;
2183 default:
2184 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2185 "%u", reason);
2186 return;
2187 }
2188
2189 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2190}
2191
2192
3088e4e5
JB
2193static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2194 int wds)
02bb32c3
JB
2195{
2196 struct wpa_driver_nl80211_data *drv = bss->drv;
2197 union wpa_event_data event;
02bb32c3
JB
2198
2199 if (!tb[NL80211_ATTR_MAC])
2200 return;
2201
02bb32c3 2202 os_memset(&event, 0, sizeof(event));
341eebee 2203 event.rx_from_unknown.bssid = bss->addr;
02bb32c3 2204 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
3088e4e5 2205 event.rx_from_unknown.wds = wds;
02bb32c3
JB
2206
2207 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2208}
2209
2210
a5e1eb20
SE
2211static void do_process_drv_event(struct i802_bss *bss, int cmd,
2212 struct nlattr **tb)
97865538 2213{
a5e1eb20
SE
2214 struct wpa_driver_nl80211_data *drv = bss->drv;
2215
b1f625e0 2216 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
d6c9aab8
JB
2217 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2218 cmd == NL80211_CMD_SCAN_ABORTED)) {
a2e40bb6 2219 wpa_driver_nl80211_set_mode(&drv->first_bss,
b1f625e0
EP
2220 drv->ap_scan_as_station);
2221 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6
JM
2222 }
2223
d6c9aab8 2224 switch (cmd) {
d942a79e
JM
2225 case NL80211_CMD_TRIGGER_SCAN:
2226 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
2227 break;
d21c63b9
LC
2228 case NL80211_CMD_START_SCHED_SCAN:
2229 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
2230 break;
2231 case NL80211_CMD_SCHED_SCAN_STOPPED:
2232 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
2233 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2234 break;
97865538
JM
2235 case NL80211_CMD_NEW_SCAN_RESULTS:
2236 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
2237 drv->scan_complete_events = 1;
2238 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2239 drv->ctx);
8d923a4a 2240 send_scan_event(drv, 0, tb);
97865538 2241 break;
d21c63b9
LC
2242 case NL80211_CMD_SCHED_SCAN_RESULTS:
2243 wpa_printf(MSG_DEBUG,
2244 "nl80211: New sched scan results available");
2245 send_scan_event(drv, 0, tb);
2246 break;
97865538
JM
2247 case NL80211_CMD_SCAN_ABORTED:
2248 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
2249 /*
2250 * Need to indicate that scan results are available in order
2251 * not to make wpa_supplicant stop its scanning.
2252 */
2253 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2254 drv->ctx);
8d923a4a 2255 send_scan_event(drv, 1, tb);
97865538 2256 break;
c2a04078
JM
2257 case NL80211_CMD_AUTHENTICATE:
2258 case NL80211_CMD_ASSOCIATE:
2259 case NL80211_CMD_DEAUTHENTICATE:
2260 case NL80211_CMD_DISASSOCIATE:
bd94971e 2261 case NL80211_CMD_FRAME_TX_STATUS:
7d878ca7
JM
2262 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2263 case NL80211_CMD_UNPROT_DISASSOCIATE:
d6c9aab8 2264 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
58f6fbe0
JM
2265 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2266 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
da873dbb
JB
2267 tb[NL80211_ATTR_COOKIE],
2268 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
c2a04078 2269 break;
da72a1c1
ZY
2270 case NL80211_CMD_CONNECT:
2271 case NL80211_CMD_ROAM:
d6c9aab8 2272 mlme_event_connect(drv, cmd,
da72a1c1
ZY
2273 tb[NL80211_ATTR_STATUS_CODE],
2274 tb[NL80211_ATTR_MAC],
2275 tb[NL80211_ATTR_REQ_IE],
2276 tb[NL80211_ATTR_RESP_IE]);
2277 break;
1b487b8b
TP
2278 case NL80211_CMD_CH_SWITCH_NOTIFY:
2279 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2280 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2281 break;
da72a1c1 2282 case NL80211_CMD_DISCONNECT:
20f5a4c2 2283 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
3d9975d5
JM
2284 tb[NL80211_ATTR_MAC],
2285 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
da72a1c1 2286 break;
35583f3f 2287 case NL80211_CMD_MICHAEL_MIC_FAILURE:
a5e1eb20 2288 mlme_event_michael_mic_failure(bss, tb);
35583f3f 2289 break;
5cc4d64b
JM
2290 case NL80211_CMD_JOIN_IBSS:
2291 mlme_event_join_ibss(drv, tb);
2292 break;
55777702
JM
2293 case NL80211_CMD_REMAIN_ON_CHANNEL:
2294 mlme_event_remain_on_channel(drv, 0, tb);
2295 break;
2296 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2297 mlme_event_remain_on_channel(drv, 1, tb);
2298 break;
93910401
JM
2299 case NL80211_CMD_NOTIFY_CQM:
2300 nl80211_cqm_event(drv, tb);
2301 break;
33c5deb8
JM
2302 case NL80211_CMD_REG_CHANGE:
2303 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2304 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2305 NULL);
2306 break;
2307 case NL80211_CMD_REG_BEACON_HINT:
2308 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2309 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2310 NULL);
2311 break;
18d2ba08
JM
2312 case NL80211_CMD_NEW_STATION:
2313 nl80211_new_station_event(drv, tb);
2314 break;
ef985058
JM
2315 case NL80211_CMD_DEL_STATION:
2316 nl80211_del_station_event(drv, tb);
2317 break;
b14a210c
JB
2318 case NL80211_CMD_SET_REKEY_OFFLOAD:
2319 nl80211_rekey_offload_event(drv, tb);
2320 break;
c36d5242
JM
2321 case NL80211_CMD_PMKSA_CANDIDATE:
2322 nl80211_pmksa_candidate_event(drv, tb);
2323 break;
39718852
JB
2324 case NL80211_CMD_PROBE_CLIENT:
2325 nl80211_client_probe_event(drv, tb);
2326 break;
6201b052
JM
2327 case NL80211_CMD_TDLS_OPER:
2328 nl80211_tdls_oper_event(drv, tb);
2329 break;
3140803b
RM
2330 case NL80211_CMD_CONN_FAILED:
2331 nl80211_connect_failed_event(drv, tb);
2332 break;
97865538 2333 default:
c2a04078 2334 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
d6c9aab8 2335 "(cmd=%d)", cmd);
97865538
JM
2336 break;
2337 }
d6c9aab8
JB
2338}
2339
2340
2341static int process_drv_event(struct nl_msg *msg, void *arg)
2342{
2343 struct wpa_driver_nl80211_data *drv = arg;
2344 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2345 struct nlattr *tb[NL80211_ATTR_MAX + 1];
a5e1eb20
SE
2346 struct i802_bss *bss;
2347 int ifidx = -1;
d6c9aab8
JB
2348
2349 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2350 genlmsg_attrlen(gnlh, 0), NULL);
2351
a5e1eb20
SE
2352 if (tb[NL80211_ATTR_IFINDEX])
2353 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2354
2355 for (bss = &drv->first_bss; bss; bss = bss->next) {
2356 if (ifidx == -1 || ifidx == bss->ifindex) {
2357 do_process_drv_event(bss, gnlh->cmd, tb);
d6c9aab8
JB
2358 return NL_SKIP;
2359 }
2360 }
2361
a5e1eb20
SE
2362 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d) for foreign "
2363 "interface (ifindex %d)", gnlh->cmd, ifidx);
2364
d6c9aab8
JB
2365 return NL_SKIP;
2366}
2367
2368
2369static int process_global_event(struct nl_msg *msg, void *arg)
2370{
2371 struct nl80211_global *global = arg;
2372 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2373 struct nlattr *tb[NL80211_ATTR_MAX + 1];
24b5bd8b 2374 struct wpa_driver_nl80211_data *drv, *tmp;
d6c9aab8 2375 int ifidx = -1;
a5e1eb20 2376 struct i802_bss *bss;
d6c9aab8
JB
2377
2378 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2379 genlmsg_attrlen(gnlh, 0), NULL);
2380
2381 if (tb[NL80211_ATTR_IFINDEX])
2382 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2383
24b5bd8b
JB
2384 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2385 struct wpa_driver_nl80211_data, list) {
a5e1eb20
SE
2386 for (bss = &drv->first_bss; bss; bss = bss->next) {
2387 if (ifidx == -1 || ifidx == bss->ifindex) {
2388 do_process_drv_event(bss, gnlh->cmd, tb);
2389 return NL_SKIP;
2390 }
2391 }
d6c9aab8 2392 }
97865538
JM
2393
2394 return NL_SKIP;
2395}
2396
2397
cc7a48d1
JB
2398static int process_bss_event(struct nl_msg *msg, void *arg)
2399{
a11241fa 2400 struct i802_bss *bss = arg;
cc7a48d1
JB
2401 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2402 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2403
2404 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2405 genlmsg_attrlen(gnlh, 0), NULL);
2406
2407 switch (gnlh->cmd) {
a11241fa
JB
2408 case NL80211_CMD_FRAME:
2409 case NL80211_CMD_FRAME_TX_STATUS:
2410 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2411 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2412 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
da873dbb
JB
2413 tb[NL80211_ATTR_COOKIE],
2414 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
a11241fa 2415 break;
02bb32c3 2416 case NL80211_CMD_UNEXPECTED_FRAME:
3088e4e5
JB
2417 nl80211_spurious_frame(bss, tb, 0);
2418 break;
2419 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2420 nl80211_spurious_frame(bss, tb, 1);
02bb32c3 2421 break;
cc7a48d1
JB
2422 default:
2423 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2424 "(cmd=%d)", gnlh->cmd);
2425 break;
2426 }
2427
2428 return NL_SKIP;
2429}
2430
2431
97865538 2432static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
5582a5d1 2433 void *handle)
97865538 2434{
a4ae123c 2435 struct nl_cb *cb = eloop_ctx;
97865538
JM
2436
2437 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
2438
a4ae123c 2439 nl_recvmsgs(handle, cb);
97865538
JM
2440}
2441
2442
6d158490
LR
2443/**
2444 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2445 * @priv: driver_nl80211 private data
2446 * @alpha2_arg: country to which to switch to
2447 * Returns: 0 on success, -1 on failure
2448 *
2449 * This asks nl80211 to set the regulatory domain for given
2450 * country ISO / IEC alpha2.
2451 */
2452static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2453{
a2e40bb6
FF
2454 struct i802_bss *bss = priv;
2455 struct wpa_driver_nl80211_data *drv = bss->drv;
6d158490
LR
2456 char alpha2[3];
2457 struct nl_msg *msg;
2458
2459 msg = nlmsg_alloc();
2460 if (!msg)
e785c2ba 2461 return -ENOMEM;
6d158490
LR
2462
2463 alpha2[0] = alpha2_arg[0];
2464 alpha2[1] = alpha2_arg[1];
2465 alpha2[2] = '\0';
2466
9fb04070 2467 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
6d158490
LR
2468
2469 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2470 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2471 return -EINVAL;
2472 return 0;
2473nla_put_failure:
5883168a 2474 nlmsg_free(msg);
6d158490
LR
2475 return -EINVAL;
2476}
2477
2478
43245552
DJ
2479static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2480{
2481 u32 *feat = arg;
2482 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2483 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2484
2485 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2486 genlmsg_attrlen(gnlh, 0), NULL);
2487
2488 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2489 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2490
2491 return NL_SKIP;
2492}
2493
2494
2495static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2496{
2497 u32 feat = 0;
2498 struct nl_msg *msg;
2499
2500 msg = nlmsg_alloc();
2501 if (!msg)
2502 goto nla_put_failure;
2503
2504 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2505 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2506 return feat;
2507
2508 msg = NULL;
2509nla_put_failure:
2510 nlmsg_free(msg);
2511 return 0;
2512}
2513
2514
80bc75f1 2515struct wiphy_info_data {
e8b5e24e
JB
2516 struct wpa_driver_capa *capa;
2517
2518 unsigned int error:1;
61cbe2ff 2519 unsigned int device_ap_sme:1;
39718852 2520 unsigned int poll_command_supported:1;
32ab4855 2521 unsigned int data_tx_status:1;
536062f2 2522 unsigned int monitor_supported:1;
43245552
DJ
2523 unsigned int auth_supported:1;
2524 unsigned int connect_supported:1;
2525 unsigned int p2p_go_supported:1;
2526 unsigned int p2p_client_supported:1;
2527 unsigned int p2p_concurrent:1;
2528 unsigned int p2p_multichan_concurrent:1;
80bc75f1
JM
2529};
2530
2531
562c9d97
AN
2532static unsigned int probe_resp_offload_support(int supp_protocols)
2533{
2534 unsigned int prot = 0;
2535
2536 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2537 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2538 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2539 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2540 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2541 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2542 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2543 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2544
2545 return prot;
2546}
2547
2548
80bc75f1
JM
2549static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2550{
2551 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2552 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2553 struct wiphy_info_data *info = arg;
e8b5e24e 2554 struct wpa_driver_capa *capa = info->capa;
7626850d
JB
2555 static struct nla_policy
2556 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2557 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2558 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2559 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2560 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2561 },
2562 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2563 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2564 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2565 };
80bc75f1
JM
2566
2567 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2568 genlmsg_attrlen(gnlh, 0), NULL);
2569
2570 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
e8b5e24e 2571 capa->max_scan_ssids =
80bc75f1
JM
2572 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2573
d21c63b9 2574 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
e8b5e24e 2575 capa->max_sched_scan_ssids =
d21c63b9
LC
2576 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2577
bd525934 2578 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
e8b5e24e 2579 capa->max_match_sets =
bd525934
LC
2580 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2581
1581b38b
JM
2582 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
2583 struct nlattr *nl_mode;
2584 int i;
2585 nla_for_each_nested(nl_mode,
2586 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
58708b3b 2587 switch (nla_type(nl_mode)) {
9f51b113 2588 case NL80211_IFTYPE_AP:
e8b5e24e 2589 capa->flags |= WPA_DRIVER_FLAGS_AP;
1581b38b 2590 break;
9f51b113 2591 case NL80211_IFTYPE_P2P_GO:
43245552 2592 info->p2p_go_supported = 1;
9f51b113
JB
2593 break;
2594 case NL80211_IFTYPE_P2P_CLIENT:
43245552 2595 info->p2p_client_supported = 1;
9f51b113 2596 break;
536062f2
JM
2597 case NL80211_IFTYPE_MONITOR:
2598 info->monitor_supported = 1;
2599 break;
1581b38b
JM
2600 }
2601 }
2602 }
2603
7626850d
JB
2604 if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
2605 struct nlattr *nl_combi;
2606 int rem_combi;
2607
2608 nla_for_each_nested(nl_combi,
2609 tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
2610 rem_combi) {
2611 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2612 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2613 struct nlattr *nl_limit, *nl_mode;
2614 int err, rem_limit, rem_mode;
2615 int combination_has_p2p = 0, combination_has_mgd = 0;
2616
2617 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2618 nl_combi,
2619 iface_combination_policy);
2620 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2621 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2622 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2623 goto broken_combination;
2624
2625 nla_for_each_nested(nl_limit,
2626 tb_comb[NL80211_IFACE_COMB_LIMITS],
2627 rem_limit) {
2628 err = nla_parse_nested(tb_limit,
2629 MAX_NL80211_IFACE_LIMIT,
2630 nl_limit,
2631 iface_limit_policy);
2632 if (err ||
2633 !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2634 goto broken_combination;
2635
2636 nla_for_each_nested(
2637 nl_mode,
2638 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2639 rem_mode) {
2640 int ift = nla_type(nl_mode);
2641 if (ift == NL80211_IFTYPE_P2P_GO ||
2642 ift == NL80211_IFTYPE_P2P_CLIENT)
2643 combination_has_p2p = 1;
2644 if (ift == NL80211_IFTYPE_STATION)
2645 combination_has_mgd = 1;
2646 }
2647 if (combination_has_p2p && combination_has_mgd)
2648 break;
2649 }
2650
2651 if (combination_has_p2p && combination_has_mgd) {
43245552 2652 info->p2p_concurrent = 1;
e3e234fa 2653 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
43245552 2654 info->p2p_multichan_concurrent = 1;
7626850d
JB
2655 break;
2656 }
2657
2658broken_combination:
2659 ;
2660 }
2661 }
2662
93d11400
ZY
2663 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
2664 struct nlattr *nl_cmd;
2665 int i;
2666
2667 nla_for_each_nested(nl_cmd,
2668 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
e8b5e24e
JB
2669 switch (nla_get_u32(nl_cmd)) {
2670 case NL80211_CMD_AUTHENTICATE:
43245552 2671 info->auth_supported = 1;
e8b5e24e
JB
2672 break;
2673 case NL80211_CMD_CONNECT:
43245552 2674 info->connect_supported = 1;
e8b5e24e
JB
2675 break;
2676 case NL80211_CMD_START_SCHED_SCAN:
2677 capa->sched_scan_supported = 1;
2678 break;
39718852
JB
2679 case NL80211_CMD_PROBE_CLIENT:
2680 info->poll_command_supported = 1;
2681 break;
e8b5e24e 2682 }
93d11400
ZY
2683 }
2684 }
2685
e8b5e24e
JB
2686 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2687 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2688 "off-channel TX");
2689 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2690 }
2691
2692 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2693 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2694 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2695 }
5dfca53f 2696
e8b5e24e
JB
2697 /* default to 5000 since early versions of mac80211 don't set it */
2698 capa->max_remain_on_chan = 5000;
004ba773 2699
70619a5d
EP
2700 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2701 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
2702
89e07afb 2703 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
e8b5e24e 2704 capa->max_remain_on_chan =
89e07afb
JB
2705 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
2706
03ea1786
AN
2707 if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
2708 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2709 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2710
2711 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
2712 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2713 capa->flags |=
2714 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2715 }
2716 }
2717
61cbe2ff
JB
2718 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2719 info->device_ap_sme = 1;
2720
32ab4855
JB
2721 if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
2722 u32 flags = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
2723
2724 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2725 info->data_tx_status = 1;
a0133ee1
VT
2726
2727 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
2728 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
569fed90
JM
2729
2730 if (flags & NL80211_FEATURE_SAE)
2731 capa->flags |= WPA_DRIVER_FLAGS_SAE;
368b1957
AK
2732
2733 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
2734 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
32ab4855
JB
2735 }
2736
562c9d97
AN
2737 if (tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]) {
2738 int protocols =
2739 nla_get_u32(tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
2740 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response "
2741 "offload in AP mode");
2742 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2743 capa->probe_resp_offloads =
2744 probe_resp_offload_support(protocols);
2745 }
2746
80bc75f1
JM
2747 return NL_SKIP;
2748}
2749
2750
2751static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2752 struct wiphy_info_data *info)
2753{
43245552 2754 u32 feat;
80bc75f1
JM
2755 struct nl_msg *msg;
2756
2757 os_memset(info, 0, sizeof(*info));
e8b5e24e 2758 info->capa = &drv->capa;
89e07afb 2759
80bc75f1
JM
2760 msg = nlmsg_alloc();
2761 if (!msg)
2762 return -1;
2763
43245552
DJ
2764 feat = get_nl80211_protocol_features(drv);
2765 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
2766 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
2767 else
2768 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
80bc75f1 2769
43245552 2770 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
a2e40bb6 2771 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
80bc75f1 2772
43245552
DJ
2773 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
2774 return -1;
2775
2776 if (info->auth_supported)
2777 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
2778 else if (!info->connect_supported) {
2779 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2780 "authentication/association or connect commands");
2781 info->error = 1;
2782 }
2783
2784 if (info->p2p_go_supported && info->p2p_client_supported)
2785 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2786 if (info->p2p_concurrent) {
2787 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2788 "interface (driver advertised support)");
2789 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2790 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2791 }
2792 if (info->p2p_multichan_concurrent) {
2793 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
2794 "concurrent (driver advertised support)");
2795 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
2796 }
2797 return 0;
80bc75f1
JM
2798nla_put_failure:
2799 nlmsg_free(msg);
2800 return -1;
2801}
2802
2803
93d11400 2804static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
80bc75f1
JM
2805{
2806 struct wiphy_info_data info;
2807 if (wpa_driver_nl80211_get_info(drv, &info))
93d11400 2808 return -1;
e8b5e24e
JB
2809
2810 if (info.error)
2811 return -1;
2812
80bc75f1 2813 drv->has_capability = 1;
1b2a72e8
JM
2814 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2815 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2816 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2817 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2818 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2819 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2820 WPA_DRIVER_CAPA_ENC_WEP104 |
2821 WPA_DRIVER_CAPA_ENC_TKIP |
2822 WPA_DRIVER_CAPA_ENC_CCMP;
291b6068
JM
2823 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2824 WPA_DRIVER_AUTH_SHARED |
2825 WPA_DRIVER_AUTH_LEAP;
1b2a72e8 2826
871f4dd0 2827 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
0194fedb 2828 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2fee890a 2829 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
e1bd4e19 2830
e5091674 2831 if (!info.device_ap_sme) {
e1bd4e19 2832 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
0194fedb 2833
e5091674
JM
2834 /*
2835 * No AP SME is currently assumed to also indicate no AP MLME
2836 * in the driver/firmware.
2837 */
2838 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
2839 }
2840
61cbe2ff 2841 drv->device_ap_sme = info.device_ap_sme;
39718852 2842 drv->poll_command_supported = info.poll_command_supported;
32ab4855 2843 drv->data_tx_status = info.data_tx_status;
61cbe2ff 2844
a11241fa 2845 /*
73a3c6ff
FF
2846 * If poll command and tx status are supported, mac80211 is new enough
2847 * to have everything we need to not need monitor interfaces.
a11241fa 2848 */
73a3c6ff 2849 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
a11241fa 2850
536062f2
JM
2851 if (drv->device_ap_sme && drv->use_monitor) {
2852 /*
2853 * Non-mac80211 drivers may not support monitor interface.
2854 * Make sure we do not get stuck with incorrect capability here
2855 * by explicitly testing this.
2856 */
2857 if (!info.monitor_supported) {
2858 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
2859 "with device_ap_sme since no monitor mode "
2860 "support detected");
2861 drv->use_monitor = 0;
2862 }
2863 }
2864
a11241fa
JB
2865 /*
2866 * If we aren't going to use monitor interfaces, but the
2867 * driver doesn't support data TX status, we won't get TX
2868 * status for EAPOL frames.
2869 */
2870 if (!drv->use_monitor && !info.data_tx_status)
2871 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2872
93d11400 2873 return 0;
80bc75f1
JM
2874}
2875
2876
b088cf82
JM
2877#ifdef ANDROID
2878static int android_genl_ctrl_resolve(struct nl_handle *handle,
2879 const char *name)
2880{
2881 /*
2882 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
2883 * need to work around that.
2884 */
2885 struct nl_cache *cache = NULL;
2886 struct genl_family *nl80211 = NULL;
2887 int id = -1;
2888
2889 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
2890 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
2891 "netlink cache");
2892 goto fail;
2893 }
2894
2895 nl80211 = genl_ctrl_search_by_name(cache, name);
2896 if (nl80211 == NULL)
2897 goto fail;
2898
2899 id = genl_family_get_id(nl80211);
2900
2901fail:
2902 if (nl80211)
2903 genl_family_put(nl80211);
2904 if (cache)
2905 nl_cache_free(cache);
2906
2907 return id;
2908}
2909#define genl_ctrl_resolve android_genl_ctrl_resolve
2910#endif /* ANDROID */
2911
2912
2a7b66f5
BG
2913static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
2914{
d6c9aab8
JB
2915 int ret;
2916
2a7b66f5
BG
2917 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2918 if (global->nl_cb == NULL) {
2919 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2920 "callbacks");
2921 return -1;
2922 }
2923
481234cf
JM
2924 global->nl = nl_create_handle(global->nl_cb, "nl");
2925 if (global->nl == NULL)
d6c9aab8 2926 goto err;
276e2d67 2927
481234cf 2928 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
335d42b1 2929 if (global->nl80211_id < 0) {
276e2d67
BG
2930 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2931 "found");
d6c9aab8 2932 goto err;
276e2d67
BG
2933 }
2934
481234cf
JM
2935 global->nl_event = nl_create_handle(global->nl_cb, "event");
2936 if (global->nl_event == NULL)
d6c9aab8 2937 goto err;
9fff9fdc 2938
d6c9aab8 2939 ret = nl_get_multicast_id(global, "nl80211", "scan");
97865538 2940 if (ret >= 0)
481234cf 2941 ret = nl_socket_add_membership(global->nl_event, ret);
97865538
JM
2942 if (ret < 0) {
2943 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2944 "membership for scan events: %d (%s)",
2945 ret, strerror(-ret));
d6c9aab8 2946 goto err;
97865538 2947 }
c2a04078 2948
d6c9aab8 2949 ret = nl_get_multicast_id(global, "nl80211", "mlme");
c2a04078 2950 if (ret >= 0)
481234cf 2951 ret = nl_socket_add_membership(global->nl_event, ret);
c2a04078
JM
2952 if (ret < 0) {
2953 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2954 "membership for mlme events: %d (%s)",
2955 ret, strerror(-ret));
d6c9aab8 2956 goto err;
c2a04078 2957 }
c2a04078 2958
d6c9aab8 2959 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
33c5deb8 2960 if (ret >= 0)
481234cf 2961 ret = nl_socket_add_membership(global->nl_event, ret);
33c5deb8
JM
2962 if (ret < 0) {
2963 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2964 "membership for regulatory events: %d (%s)",
2965 ret, strerror(-ret));
2966 /* Continue without regulatory events */
2967 }
2968
d6c9aab8
JB
2969 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2970 no_seq_check, NULL);
2971 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2972 process_global_event, global);
2973
481234cf 2974 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
d6c9aab8 2975 wpa_driver_nl80211_event_receive,
481234cf 2976 global->nl_cb, global->nl_event);
d6c9aab8
JB
2977
2978 return 0;
2979
2980err:
2981 nl_destroy_handles(&global->nl_event);
2982 nl_destroy_handles(&global->nl);
2983 nl_cb_put(global->nl_cb);
671a5039 2984 global->nl_cb = NULL;
d6c9aab8
JB
2985 return -1;
2986}
2987
2988
2989static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2990{
1afc986d
JB
2991 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2992 if (!drv->nl_cb) {
2993 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
d6c9aab8 2994 return -1;
1afc986d
JB
2995 }
2996
2997 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2998 no_seq_check, NULL);
f06aedd9
JB
2999 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3000 process_drv_event, drv);
1afc986d 3001
9fff9fdc 3002 return 0;
9fff9fdc
JM
3003}
3004
3005
8401a6b0
JM
3006static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3007{
8401a6b0 3008 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
a63063b4
JM
3009 /*
3010 * This may be for any interface; use ifdown event to disable
3011 * interface.
3012 */
8401a6b0
JM
3013}
3014
3015
3016static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3017{
3018 struct wpa_driver_nl80211_data *drv = ctx;
3019 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
c81eff1a
BG
3020 if (linux_set_iface_flags(drv->global->ioctl_sock,
3021 drv->first_bss.ifname, 1)) {
8401a6b0
JM
3022 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3023 "after rfkill unblock");
3024 return;
3025 }
a63063b4 3026 /* rtnetlink ifup handler will report interface as enabled */
8401a6b0
JM
3027}
3028
3029
6859f1cb
BG
3030static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
3031{
3032 /* Find phy (radio) to which this interface belongs */
3033 char buf[90], *pos;
3034 int f, rv;
3035
3036 drv->phyname[0] = '\0';
3037 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
3038 drv->first_bss.ifname);
3039 f = open(buf, O_RDONLY);
3040 if (f < 0) {
3041 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
3042 buf, strerror(errno));
3043 return;
3044 }
3045
3046 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
3047 close(f);
3048 if (rv < 0) {
3049 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
3050 buf, strerror(errno));
3051 return;
3052 }
3053
3054 drv->phyname[rv] = '\0';
3055 pos = os_strchr(drv->phyname, '\n');
3056 if (pos)
3057 *pos = '\0';
3058 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3059 drv->first_bss.ifname, drv->phyname);
3060}
3061
3062
32ab4855
JB
3063static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3064 void *eloop_ctx,
3065 void *handle)
3066{
3067 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3068 u8 data[2048];
3069 struct msghdr msg;
3070 struct iovec entry;
cad0f50e 3071 u8 control[512];
32ab4855
JB
3072 struct cmsghdr *cmsg;
3073 int res, found_ee = 0, found_wifi = 0, acked = 0;
3074 union wpa_event_data event;
3075
3076 memset(&msg, 0, sizeof(msg));
3077 msg.msg_iov = &entry;
3078 msg.msg_iovlen = 1;
3079 entry.iov_base = data;
3080 entry.iov_len = sizeof(data);
3081 msg.msg_control = &control;
3082 msg.msg_controllen = sizeof(control);
3083
3084 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3085 /* if error or not fitting 802.3 header, return */
3086 if (res < 14)
3087 return;
3088
3089 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3090 {
3091 if (cmsg->cmsg_level == SOL_SOCKET &&
3092 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3093 int *ack;
3094
3095 found_wifi = 1;
3096 ack = (void *)CMSG_DATA(cmsg);
3097 acked = *ack;
3098 }
3099
3100 if (cmsg->cmsg_level == SOL_PACKET &&
3101 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3102 struct sock_extended_err *err =
3103 (struct sock_extended_err *)CMSG_DATA(cmsg);
3104
3105 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3106 found_ee = 1;
3107 }
3108 }
3109
3110 if (!found_ee || !found_wifi)
3111 return;
3112
3113 memset(&event, 0, sizeof(event));
3114 event.eapol_tx_status.dst = data;
3115 event.eapol_tx_status.data = data + 14;
3116 event.eapol_tx_status.data_len = res - 14;
3117 event.eapol_tx_status.ack = acked;
3118 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3119}
3120
3121
cc7a48d1
JB
3122static int nl80211_init_bss(struct i802_bss *bss)
3123{
3124 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3125 if (!bss->nl_cb)
3126 return -1;
3127
3128 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3129 no_seq_check, NULL);
3130 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3131 process_bss_event, bss);
3132
3133 return 0;
3134}
3135
3136
3137static void nl80211_destroy_bss(struct i802_bss *bss)
3138{
3139 nl_cb_put(bss->nl_cb);
3140 bss->nl_cb = NULL;
3141}
3142
3143
9fff9fdc
JM
3144/**
3145 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3146 * @ctx: context to be used when calling wpa_supplicant functions,
3147 * e.g., wpa_supplicant_event()
3148 * @ifname: interface name, e.g., wlan0
f2ed8023 3149 * @global_priv: private driver global data from global_init()
9fff9fdc
JM
3150 * Returns: Pointer to private data, %NULL on failure
3151 */
f2ed8023
JM
3152static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3153 void *global_priv)
9fff9fdc 3154{
9fff9fdc 3155 struct wpa_driver_nl80211_data *drv;
8401a6b0 3156 struct rfkill_config *rcfg;
a2e40bb6 3157 struct i802_bss *bss;
9fff9fdc 3158
a5c696ad
JM
3159 if (global_priv == NULL)
3160 return NULL;
9fff9fdc
JM
3161 drv = os_zalloc(sizeof(*drv));
3162 if (drv == NULL)
3163 return NULL;
f2ed8023 3164 drv->global = global_priv;
9fff9fdc 3165 drv->ctx = ctx;
a2e40bb6
FF
3166 bss = &drv->first_bss;
3167 bss->drv = drv;
a5e1eb20
SE
3168 bss->ctx = ctx;
3169
a2e40bb6 3170 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
9fff9fdc
JM
3171 drv->monitor_ifidx = -1;
3172 drv->monitor_sock = -1;
d12dab4c 3173 drv->eapol_tx_sock = -1;
b1f625e0 3174 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
9fff9fdc 3175
ba2d0d7d 3176 if (wpa_driver_nl80211_init_nl(drv)) {
bbaf0837
JM
3177 os_free(drv);
3178 return NULL;
3179 }
9fff9fdc 3180
cc7a48d1
JB
3181 if (nl80211_init_bss(bss))
3182 goto failed;
3183
6859f1cb
BG
3184 nl80211_get_phy_name(drv);
3185
8401a6b0
JM
3186 rcfg = os_zalloc(sizeof(*rcfg));
3187 if (rcfg == NULL)
3188 goto failed;
3189 rcfg->ctx = drv;
3190 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3191 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3192 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3193 drv->rfkill = rfkill_init(rcfg);
52169389 3194 if (drv->rfkill == NULL) {
8401a6b0 3195 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
52169389
JM
3196 os_free(rcfg);
3197 }
8401a6b0 3198
08063178 3199 if (wpa_driver_nl80211_finish_drv_init(drv))
bbaf0837 3200 goto failed;
7524cfb1 3201
d12dab4c 3202 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
32ab4855
JB
3203 if (drv->eapol_tx_sock < 0)
3204 goto failed;
3205
3206 if (drv->data_tx_status) {
3207 int enabled = 1;
3208
3209 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3210 &enabled, sizeof(enabled)) < 0) {
3211 wpa_printf(MSG_DEBUG,
3212 "nl80211: wifi status sockopt failed\n");
3213 drv->data_tx_status = 0;
a11241fa
JB
3214 if (!drv->use_monitor)
3215 drv->capa.flags &=
3216 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
32ab4855
JB
3217 } else {
3218 eloop_register_read_sock(drv->eapol_tx_sock,
3219 wpa_driver_nl80211_handle_eapol_tx_status,
3220 drv, NULL);
3221 }
3222 }
f10bfc9a 3223
dac12351 3224 if (drv->global) {
c4bb8817 3225 dl_list_add(&drv->global->interfaces, &drv->list);
dac12351
BG
3226 drv->in_interface_list = 1;
3227 }
c4bb8817 3228
a2e40bb6 3229 return bss;
7524cfb1 3230
bbaf0837 3231failed:
dac12351 3232 wpa_driver_nl80211_deinit(bss);
7524cfb1
JM
3233 return NULL;
3234}
3235
3236
a11241fa 3237static int nl80211_register_frame(struct i802_bss *bss,
5582a5d1 3238 struct nl_handle *nl_handle,
bd94971e 3239 u16 type, const u8 *match, size_t match_len)
58f6fbe0 3240{
a11241fa 3241 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0
JM
3242 struct nl_msg *msg;
3243 int ret = -1;
3244
3245 msg = nlmsg_alloc();
3246 if (!msg)
3247 return -1;
3248
36488c05
JM
3249 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3250 type, nl_handle);
3251 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3252 match, match_len);
3253
9fb04070 3254 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
58f6fbe0 3255
a11241fa 3256 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
bd94971e 3257 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
58f6fbe0
JM
3258 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3259
d6c9aab8 3260 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
58f6fbe0
JM
3261 msg = NULL;
3262 if (ret) {
5582a5d1
JB
3263 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3264 "failed (type=%u): ret=%d (%s)",
3265 type, ret, strerror(-ret));
3266 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
58f6fbe0
JM
3267 match, match_len);
3268 goto nla_put_failure;
3269 }
3270 ret = 0;
3271nla_put_failure:
3272 nlmsg_free(msg);
3273 return ret;
3274}
3275
3276
a11241fa
JB
3277static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3278{
3279 struct wpa_driver_nl80211_data *drv = bss->drv;
3280
481234cf 3281 if (bss->nl_mgmt) {
a11241fa 3282 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
36488c05 3283 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
a11241fa
JB
3284 return -1;
3285 }
3286
481234cf
JM
3287 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3288 if (bss->nl_mgmt == NULL)
a11241fa
JB
3289 return -1;
3290
481234cf 3291 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
a11241fa 3292 wpa_driver_nl80211_event_receive, bss->nl_cb,
481234cf 3293 bss->nl_mgmt);
a11241fa
JB
3294
3295 return 0;
3296}
3297
3298
3299static int nl80211_register_action_frame(struct i802_bss *bss,
bd94971e
JB
3300 const u8 *match, size_t match_len)
3301{
3302 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
481234cf 3303 return nl80211_register_frame(bss, bss->nl_mgmt,
5582a5d1 3304 type, match, match_len);
bd94971e
JB
3305}
3306
3307
a11241fa 3308static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
58f6fbe0 3309{
a11241fa
JB
3310 struct wpa_driver_nl80211_data *drv = bss->drv;
3311
3312 if (nl80211_alloc_mgmt_handle(bss))
3313 return -1;
36488c05
JM
3314 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3315 "handle %p", bss->nl_mgmt);
a11241fa 3316
4fe9fa0d 3317#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
046b26a2 3318 /* GAS Initial Request */
a11241fa 3319 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
046b26a2
JM
3320 return -1;
3321 /* GAS Initial Response */
a11241fa 3322 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
046b26a2 3323 return -1;
18708aad 3324 /* GAS Comeback Request */
a11241fa 3325 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
18708aad
JM
3326 return -1;
3327 /* GAS Comeback Response */
a11241fa 3328 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
18708aad 3329 return -1;
4fe9fa0d
JM
3330#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3331#ifdef CONFIG_P2P
046b26a2 3332 /* P2P Public Action */
a11241fa 3333 if (nl80211_register_action_frame(bss,
046b26a2
JM
3334 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3335 6) < 0)
3336 return -1;
3337 /* P2P Action */
a11241fa 3338 if (nl80211_register_action_frame(bss,
046b26a2
JM
3339 (u8 *) "\x7f\x50\x6f\x9a\x09",
3340 5) < 0)
3341 return -1;
3342#endif /* CONFIG_P2P */
7d878ca7
JM
3343#ifdef CONFIG_IEEE80211W
3344 /* SA Query Response */
a11241fa 3345 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
7d878ca7
JM
3346 return -1;
3347#endif /* CONFIG_IEEE80211W */
35287637
AN
3348#ifdef CONFIG_TDLS
3349 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3350 /* TDLS Discovery Response */
aa543c0c 3351 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
35287637
AN
3352 0)
3353 return -1;
3354 }
3355#endif /* CONFIG_TDLS */
046b26a2 3356
7b90c16a 3357 /* FT Action frames */
a11241fa 3358 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
7b90c16a
JM
3359 return -1;
3360 else
3361 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3362 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3363
71269b37 3364 /* WNM - BSS Transition Management Request */
a11241fa
JB
3365 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3366 return -1;
bd896433
JM
3367 /* WNM-Sleep Mode Response */
3368 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3369 return -1;
a11241fa
JB
3370
3371 return 0;
3372}
3373
3374
02bb32c3
JB
3375static int nl80211_register_spurious_class3(struct i802_bss *bss)
3376{
3377 struct wpa_driver_nl80211_data *drv = bss->drv;
3378 struct nl_msg *msg;
3379 int ret = -1;
3380
3381 msg = nlmsg_alloc();
3382 if (!msg)
3383 return -1;
3384
3385 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3386
3387 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3388
481234cf 3389 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
3390 msg = NULL;
3391 if (ret) {
3392 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3393 "failed: ret=%d (%s)",
3394 ret, strerror(-ret));
3395 goto nla_put_failure;
3396 }
3397 ret = 0;
3398nla_put_failure:
3399 nlmsg_free(msg);
3400 return ret;
3401}
3402
3403
a11241fa
JB
3404static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3405{
3406 static const int stypes[] = {
3407 WLAN_FC_STYPE_AUTH,
3408 WLAN_FC_STYPE_ASSOC_REQ,
3409 WLAN_FC_STYPE_REASSOC_REQ,
3410 WLAN_FC_STYPE_DISASSOC,
3411 WLAN_FC_STYPE_DEAUTH,
3412 WLAN_FC_STYPE_ACTION,
3413 WLAN_FC_STYPE_PROBE_REQ,
3414/* Beacon doesn't work as mac80211 doesn't currently allow
3415 * it, but it wouldn't really be the right thing anyway as
3416 * it isn't per interface ... maybe just dump the scan
3417 * results periodically for OLBC?
3418 */
3419// WLAN_FC_STYPE_BEACON,
3420 };
3421 unsigned int i;
3422
3423 if (nl80211_alloc_mgmt_handle(bss))
71269b37 3424 return -1;
36488c05
JM
3425 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3426 "handle %p", bss->nl_mgmt);
71269b37 3427
a11241fa 3428 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
481234cf 3429 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
3430 (WLAN_FC_TYPE_MGMT << 2) |
3431 (stypes[i] << 4),
3432 NULL, 0) < 0) {
3433 goto out_err;
3434 }
3435 }
3436
02bb32c3
JB
3437 if (nl80211_register_spurious_class3(bss))
3438 goto out_err;
3439
e32ad281
JB
3440 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3441 goto out_err;
3442
58f6fbe0 3443 return 0;
a11241fa
JB
3444
3445out_err:
481234cf 3446 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
a11241fa
JB
3447 nl_destroy_handles(&bss->nl_mgmt);
3448 return -1;
3449}
3450
3451
a6cc0602
JM
3452static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3453{
3454 if (nl80211_alloc_mgmt_handle(bss))
3455 return -1;
3456 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3457 "handle %p (device SME)", bss->nl_mgmt);
3458
3459 if (nl80211_register_frame(bss, bss->nl_mgmt,
3460 (WLAN_FC_TYPE_MGMT << 2) |
3461 (WLAN_FC_STYPE_ACTION << 4),
3462 NULL, 0) < 0)
3463 goto out_err;
3464
3465 return 0;
3466
3467out_err:
3468 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3469 nl_destroy_handles(&bss->nl_mgmt);
3470 return -1;
3471}
3472
3473
36488c05 3474static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 3475{
481234cf 3476 if (bss->nl_mgmt == NULL)
a11241fa 3477 return;
36488c05
JM
3478 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3479 "(%s)", bss->nl_mgmt, reason);
481234cf 3480 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
a11241fa 3481 nl_destroy_handles(&bss->nl_mgmt);
e32ad281
JB
3482
3483 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
3484}
3485
3486
8401a6b0
JM
3487static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3488{
3489 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3490}
3491
3492
362f781e 3493static int
7524cfb1
JM
3494wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3495{
a2e40bb6 3496 struct i802_bss *bss = &drv->first_bss;
8401a6b0 3497 int send_rfkill_event = 0;
a2e40bb6
FF
3498
3499 drv->ifindex = if_nametoindex(bss->ifname);
3500 drv->first_bss.ifindex = drv->ifindex;
a87c9d96 3501
bbaf0837 3502#ifndef HOSTAPD
ff6a158b
JM
3503 /*
3504 * Make sure the interface starts up in station mode unless this is a
3505 * dynamically added interface (e.g., P2P) that was already configured
3506 * with proper iftype.
3507 */
a5c696ad 3508 if (drv->ifindex != drv->global->if_add_ifindex &&
b1f625e0 3509 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
6e8183d7 3510 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
a87c9d96 3511 "use managed mode");
6e8183d7 3512 return -1;
a87c9d96
JM
3513 }
3514
c81eff1a 3515 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
8401a6b0
JM
3516 if (rfkill_is_blocked(drv->rfkill)) {
3517 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3518 "interface '%s' due to rfkill",
3519 bss->ifname);
a63063b4 3520 drv->if_disabled = 1;
8401a6b0
JM
3521 send_rfkill_event = 1;
3522 } else {
3523 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3524 "interface '%s' UP", bss->ifname);
3525 return -1;
3526 }
362f781e 3527 }
3f5285e8 3528
36d84860 3529 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
e2d02c29 3530 1, IF_OPER_DORMANT);
bbaf0837 3531#endif /* HOSTAPD */
362f781e 3532
2fee890a
JM
3533 if (wpa_driver_nl80211_capa(drv))
3534 return -1;
3535
c81eff1a 3536 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
341eebee 3537 bss->addr))
2136f480 3538 return -1;
f2ed8023 3539
8401a6b0
JM
3540 if (send_rfkill_event) {
3541 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3542 drv, drv->ctx);
3543 }
3544
362f781e 3545 return 0;
3f5285e8
JM
3546}
3547
3548
8a27af5c
JM
3549static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3550{
3551 struct nl_msg *msg;
3552
3553 msg = nlmsg_alloc();
3554 if (!msg)
3555 return -ENOMEM;
3556
9fb04070 3557 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
8a27af5c
JM
3558 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3559
3560 return send_and_recv_msgs(drv, msg, NULL, NULL);
3561 nla_put_failure:
5883168a 3562 nlmsg_free(msg);
8a27af5c
JM
3563 return -ENOBUFS;
3564}
8a27af5c
JM
3565
3566
3f5285e8 3567/**
7e5ba1b9 3568 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
9ebce9c5 3569 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
3570 *
3571 * Shut down driver interface and processing of driver events. Free
3572 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3573 */
9ebce9c5 3574static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
3f5285e8 3575{
a2e40bb6 3576 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 3577
873d0fcf 3578 bss->in_deinit = 1;
32ab4855
JB
3579 if (drv->data_tx_status)
3580 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
3581 if (drv->eapol_tx_sock >= 0)
3582 close(drv->eapol_tx_sock);
f10bfc9a 3583
481234cf 3584 if (bss->nl_preq)
5582a5d1 3585 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 3586 if (bss->added_if_into_bridge) {
c81eff1a
BG
3587 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3588 bss->ifname) < 0)
94627f6c
JM
3589 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3590 "interface %s from bridge %s: %s",
e17a2477 3591 bss->ifname, bss->brname, strerror(errno));
94627f6c 3592 }
e17a2477 3593 if (bss->added_bridge) {
c81eff1a 3594 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
3595 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3596 "bridge %s: %s",
e17a2477 3597 bss->brname, strerror(errno));
94627f6c
JM
3598 }
3599
460456f8 3600 nl80211_remove_monitor_interface(drv);
8a27af5c 3601
b1f625e0 3602 if (is_ap_interface(drv->nlmode))
8a27af5c 3603 wpa_driver_nl80211_del_beacon(drv);
0915d02c 3604
bbaf0837
JM
3605#ifdef HOSTAPD
3606 if (drv->last_freq_ht) {
3607 /* Clear HT flags from the driver */
3608 struct hostapd_freq_params freq;
3609 os_memset(&freq, 0, sizeof(freq));
3610 freq.freq = drv->last_freq;
9ebce9c5 3611 wpa_driver_nl80211_set_freq(bss, &freq);
bbaf0837
JM
3612 }
3613
bbaf0837
JM
3614 if (drv->eapol_sock >= 0) {
3615 eloop_unregister_read_sock(drv->eapol_sock);
3616 close(drv->eapol_sock);
3617 }
3618
3619 if (drv->if_indices != drv->default_if_indices)
3620 os_free(drv->if_indices);
1b648c7e 3621#endif /* HOSTAPD */
3f5285e8 3622
b3af99d2 3623 if (drv->disabled_11b_rates)
4e5cb1a3
JM
3624 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3625
36d84860
BG
3626 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3627 IF_OPER_UP);
8401a6b0 3628 rfkill_deinit(drv->rfkill);
3f5285e8 3629
bbaf0837
JM
3630 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3631
c81eff1a 3632 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
b1f625e0 3633 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
36488c05 3634 nl80211_mgmt_unsubscribe(bss, "deinit");
3f5285e8 3635
1afc986d 3636 nl_cb_put(drv->nl_cb);
3f5285e8 3637
cc7a48d1
JB
3638 nl80211_destroy_bss(&drv->first_bss);
3639
3812464c
JM
3640 os_free(drv->filter_ssids);
3641
536fd62d
JM
3642 os_free(drv->auth_ie);
3643
dac12351 3644 if (drv->in_interface_list)
f2ed8023
JM
3645 dl_list_del(&drv->list);
3646
3f5285e8
JM
3647 os_free(drv);
3648}
3649
3650
3651/**
3652 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
ad1e68e6 3653 * @eloop_ctx: Driver private data
3f5285e8
JM
3654 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3655 *
3656 * This function can be used as registered timeout when starting a scan to
3657 * generate a scan completed event if the driver does not report this.
3658 */
3659static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3660{
ad1e68e6 3661 struct wpa_driver_nl80211_data *drv = eloop_ctx;
b1f625e0 3662 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
a2e40bb6 3663 wpa_driver_nl80211_set_mode(&drv->first_bss,
b1f625e0
EP
3664 drv->ap_scan_as_station);
3665 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
ad1e68e6 3666 }
3f5285e8
JM
3667 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3668 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3669}
3670
3671
95ac3bf4
JM
3672static struct nl_msg *
3673nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
3674 struct wpa_driver_scan_params *params)
3f5285e8 3675{
95ac3bf4
JM
3676 struct nl_msg *msg;
3677 int err;
6a1063e0 3678 size_t i;
3f5285e8 3679
0e75527f 3680 msg = nlmsg_alloc();
f0494d0f 3681 if (!msg)
95ac3bf4 3682 return NULL;
3812464c 3683
95ac3bf4 3684 nl80211_cmd(drv, msg, 0, cmd);
0e75527f 3685
95ac3bf4
JM
3686 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) < 0)
3687 goto fail;
3f5285e8 3688
f0494d0f 3689 if (params->num_ssids) {
95ac3bf4 3690 struct nl_msg *ssids = nlmsg_alloc();
f0494d0f 3691 if (ssids == NULL)
95ac3bf4 3692 goto fail;
f0494d0f
JM
3693 for (i = 0; i < params->num_ssids; i++) {
3694 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3695 params->ssids[i].ssid,
3696 params->ssids[i].ssid_len);
95ac3bf4
JM
3697 if (nla_put(ssids, i + 1, params->ssids[i].ssid_len,
3698 params->ssids[i].ssid) < 0) {
3699 nlmsg_free(ssids);
3700 goto fail;
3701 }
f0494d0f 3702 }
95ac3bf4
JM
3703 err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3704 nlmsg_free(ssids);
3705 if (err < 0)
3706 goto fail;
3f5285e8
JM
3707 }
3708
d173df52 3709 if (params->extra_ies) {
3b1c7bfd
JM
3710 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3711 params->extra_ies, params->extra_ies_len);
95ac3bf4
JM
3712 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
3713 params->extra_ies) < 0)
3714 goto fail;
d173df52
JM
3715 }
3716
d3a98225 3717 if (params->freqs) {
95ac3bf4 3718 struct nl_msg *freqs = nlmsg_alloc();
f0494d0f 3719 if (freqs == NULL)
95ac3bf4 3720 goto fail;
9fad706c
JM
3721 for (i = 0; params->freqs[i]; i++) {
3722 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3723 "MHz", params->freqs[i]);
95ac3bf4
JM
3724 if (nla_put_u32(freqs, i + 1, params->freqs[i]) < 0) {
3725 nlmsg_free(freqs);
3726 goto fail;
3727 }
9fad706c 3728 }
95ac3bf4
JM
3729 err = nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES,
3730 freqs);
3731 nlmsg_free(freqs);
3732 if (err < 0)
3733 goto fail;
d3a98225
JM
3734 }
3735
95ac3bf4
JM
3736 os_free(drv->filter_ssids);
3737 drv->filter_ssids = params->filter_ssids;
3738 params->filter_ssids = NULL;
3739 drv->num_filter_ssids = params->num_filter_ssids;
3740
3741 return msg;
3742
3743fail:
3744 nlmsg_free(msg);
3745 return NULL;
3746}
3747
3748
3749/**
3750 * wpa_driver_nl80211_scan - Request the driver to initiate scan
9ebce9c5 3751 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
95ac3bf4
JM
3752 * @params: Scan parameters
3753 * Returns: 0 on success, -1 on failure
3754 */
9ebce9c5 3755static int wpa_driver_nl80211_scan(struct i802_bss *bss,
95ac3bf4
JM
3756 struct wpa_driver_scan_params *params)
3757{
95ac3bf4
JM
3758 struct wpa_driver_nl80211_data *drv = bss->drv;
3759 int ret = -1, timeout;
3760 struct nl_msg *msg, *rates = NULL;
3761
3762 drv->scan_for_auth = 0;
3763
3764 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params);
3765 if (!msg)
3766 return -1;
3767
47185fc7 3768 if (params->p2p_probe) {
8a6a1e1b
JM
3769 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3770
f0494d0f
JM
3771 rates = nlmsg_alloc();
3772 if (rates == NULL)
3773 goto nla_put_failure;
3774
47185fc7
RM
3775 /*
3776 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3777 * by masking out everything else apart from the OFDM rates 6,
3778 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3779 * rates are left enabled.
3780 */
3781 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
3782 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
f0494d0f
JM
3783 if (nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates) <
3784 0)
3785 goto nla_put_failure;
970fa12e
RM
3786
3787 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
47185fc7
RM
3788 }
3789
0e75527f
JM
3790 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3791 msg = NULL;
3792 if (ret) {
3793 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3794 "(%s)", ret, strerror(-ret));
ad1e68e6 3795#ifdef HOSTAPD
b1f625e0 3796 if (is_ap_interface(drv->nlmode)) {
ad1e68e6
JM
3797 /*
3798 * mac80211 does not allow scan requests in AP mode, so
3799 * try to do this in station mode.
3800 */
b1f625e0
EP
3801 if (wpa_driver_nl80211_set_mode(
3802 bss, NL80211_IFTYPE_STATION))
ad1e68e6
JM
3803 goto nla_put_failure;
3804
085b29f1 3805 if (wpa_driver_nl80211_scan(bss, params)) {
b1f625e0 3806 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
ad1e68e6
JM
3807 goto nla_put_failure;
3808 }
3809
3810 /* Restore AP mode when processing scan results */
b1f625e0 3811 drv->ap_scan_as_station = drv->nlmode;
ad1e68e6
JM
3812 ret = 0;
3813 } else
3814 goto nla_put_failure;
3815#else /* HOSTAPD */
0e75527f 3816 goto nla_put_failure;
ad1e68e6 3817#endif /* HOSTAPD */
3f5285e8
JM
3818 }
3819
3820 /* Not all drivers generate "scan completed" wireless event, so try to
3821 * read results after a timeout. */
0e75527f 3822 timeout = 10;
3f5285e8
JM
3823 if (drv->scan_complete_events) {
3824 /*
d173df52
JM
3825 * The driver seems to deliver events to notify when scan is
3826 * complete, so use longer timeout to avoid race conditions
3827 * with scanning and following association request.
3f5285e8
JM
3828 */
3829 timeout = 30;
3830 }
3831 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3832 "seconds", ret, timeout);
3833 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
0e75527f
JM
3834 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3835 drv, drv->ctx);
3f5285e8 3836
0e75527f 3837nla_put_failure:
0e75527f 3838 nlmsg_free(msg);
47185fc7 3839 nlmsg_free(rates);
3f5285e8
JM
3840 return ret;
3841}
3842
3843
d21c63b9
LC
3844/**
3845 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3846 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3847 * @params: Scan parameters
3848 * @interval: Interval between scan cycles in milliseconds
3849 * Returns: 0 on success, -1 on failure or if not supported
3850 */
3851static int wpa_driver_nl80211_sched_scan(void *priv,
3852 struct wpa_driver_scan_params *params,
3853 u32 interval)
3854{
3855 struct i802_bss *bss = priv;
3856 struct wpa_driver_nl80211_data *drv = bss->drv;
6afbc3d6 3857 int ret = -1;
95ac3bf4 3858 struct nl_msg *msg;
6afbc3d6
JM
3859 struct nl_msg *match_set_ssid = NULL, *match_sets = NULL;
3860 struct nl_msg *match_set_rssi = NULL;
d21c63b9
LC
3861 size_t i;
3862
216eede8
DS
3863#ifdef ANDROID
3864 if (!drv->capa.sched_scan_supported)
3865 return android_pno_start(bss, params);
3866#endif /* ANDROID */
3867
95ac3bf4 3868 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params);
6afbc3d6
JM
3869 if (!msg)
3870 goto nla_put_failure;
d21c63b9 3871
d21c63b9
LC
3872 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
3873
bf8d6d24
TP
3874 if ((drv->num_filter_ssids &&
3875 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
3876 params->filter_rssi) {
bd525934 3877 match_sets = nlmsg_alloc();
6afbc3d6
JM
3878 if (match_sets == NULL)
3879 goto nla_put_failure;
bd525934
LC
3880
3881 for (i = 0; i < drv->num_filter_ssids; i++) {
3882 wpa_hexdump_ascii(MSG_MSGDUMP,
3883 "nl80211: Sched scan filter SSID",
3884 drv->filter_ssids[i].ssid,
3885 drv->filter_ssids[i].ssid_len);
3886
3887 match_set_ssid = nlmsg_alloc();
6afbc3d6
JM
3888 if (match_set_ssid == NULL)
3889 goto nla_put_failure;
3890 NLA_PUT(match_set_ssid,
bd525934
LC
3891 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
3892 drv->filter_ssids[i].ssid_len,
3893 drv->filter_ssids[i].ssid);
3894
6afbc3d6
JM
3895 if (nla_put_nested(match_sets, i + 1, match_set_ssid) <
3896 0)
3897 goto nla_put_failure;
bd525934
LC
3898 }
3899
bf8d6d24
TP
3900 if (params->filter_rssi) {
3901 match_set_rssi = nlmsg_alloc();
6afbc3d6
JM
3902 if (match_set_rssi == NULL)
3903 goto nla_put_failure;
bf8d6d24
TP
3904 NLA_PUT_U32(match_set_rssi,
3905 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
3906 params->filter_rssi);
3907 wpa_printf(MSG_MSGDUMP,
3908 "nl80211: Sched scan RSSI filter %d dBm",
3909 params->filter_rssi);
6afbc3d6
JM
3910 if (nla_put_nested(match_sets, 0, match_set_rssi) < 0)
3911 goto nla_put_failure;
bf8d6d24
TP
3912 }
3913
6afbc3d6
JM
3914 if (nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
3915 match_sets) < 0)
3916 goto nla_put_failure;
bd525934
LC
3917 }
3918
d21c63b9
LC
3919 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3920
3921 /* TODO: if we get an error here, we should fall back to normal scan */
3922
3923 msg = NULL;
3924 if (ret) {
3925 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
3926 "ret=%d (%s)", ret, strerror(-ret));
3927 goto nla_put_failure;
3928 }
3929
3930 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
3931 "scan interval %d msec", ret, interval);
3932
3933nla_put_failure:
6afbc3d6
JM
3934 nlmsg_free(match_set_ssid);
3935 nlmsg_free(match_sets);
3936 nlmsg_free(match_set_rssi);
d21c63b9 3937 nlmsg_free(msg);
d21c63b9
LC
3938 return ret;
3939}
3940
3941
3942/**
3943 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
3944 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3945 * Returns: 0 on success, -1 on failure or if not supported
3946 */
3947static int wpa_driver_nl80211_stop_sched_scan(void *priv)
3948{
3949 struct i802_bss *bss = priv;
3950 struct wpa_driver_nl80211_data *drv = bss->drv;
3951 int ret = 0;
3952 struct nl_msg *msg;
3953
216eede8
DS
3954#ifdef ANDROID
3955 if (!drv->capa.sched_scan_supported)
3956 return android_pno_stop(bss);
3957#endif /* ANDROID */
3958
d21c63b9
LC
3959 msg = nlmsg_alloc();
3960 if (!msg)
3961 return -1;
3962
9fb04070 3963 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
d21c63b9
LC
3964
3965 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3966
3967 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3968 msg = NULL;
3969 if (ret) {
3970 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
3971 "ret=%d (%s)", ret, strerror(-ret));
3972 goto nla_put_failure;
3973 }
3974
3975 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
3976
3977nla_put_failure:
3978 nlmsg_free(msg);
3979 return ret;
3980}
3981
3982
3812464c
JM
3983static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
3984{
3985 const u8 *end, *pos;
3986
3987 if (ies == NULL)
3988 return NULL;
3989
3990 pos = ies;
3991 end = ies + ies_len;
3992
3993 while (pos + 1 < end) {
3994 if (pos + 2 + pos[1] > end)
3995 break;
3996 if (pos[0] == ie)
3997 return pos;
3998 pos += 2 + pos[1];
3999 }
4000
4001 return NULL;
4002}
4003
4004
4005static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4006 const u8 *ie, size_t ie_len)
4007{
4008 const u8 *ssid;
4009 size_t i;
4010
4011 if (drv->filter_ssids == NULL)
4012 return 0;
4013
4014 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4015 if (ssid == NULL)
4016 return 1;
4017
4018 for (i = 0; i < drv->num_filter_ssids; i++) {
4019 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4020 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4021 0)
4022 return 0;
4023 }
4024
4025 return 1;
4026}
4027
4028
b3db1e1c 4029static int bss_info_handler(struct nl_msg *msg, void *arg)
3f5285e8 4030{
b3db1e1c
JM
4031 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4032 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4033 struct nlattr *bss[NL80211_BSS_MAX + 1];
4034 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4035 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4036 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4037 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4038 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4039 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4040 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4041 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4042 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
e6b8efeb 4043 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
b3ad11bb 4044 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
8c090654 4045 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
b3db1e1c 4046 };
3812464c
JM
4047 struct nl80211_bss_info_arg *_arg = arg;
4048 struct wpa_scan_results *res = _arg->res;
3f5285e8
JM
4049 struct wpa_scan_res **tmp;
4050 struct wpa_scan_res *r;
8c090654
JM
4051 const u8 *ie, *beacon_ie;
4052 size_t ie_len, beacon_ie_len;
4053 u8 *pos;
46957a9b 4054 size_t i;
3f5285e8 4055
b3db1e1c
JM
4056 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4057 genlmsg_attrlen(gnlh, 0), NULL);
4058 if (!tb[NL80211_ATTR_BSS])
4059 return NL_SKIP;
4060 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4061 bss_policy))
4062 return NL_SKIP;
f5a8d422
JM
4063 if (bss[NL80211_BSS_STATUS]) {
4064 enum nl80211_bss_status status;
4065 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4066 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4067 bss[NL80211_BSS_FREQUENCY]) {
4068 _arg->assoc_freq =
4069 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4070 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4071 _arg->assoc_freq);
4072 }
20f5a4c2
JM
4073 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4074 bss[NL80211_BSS_BSSID]) {
4075 os_memcpy(_arg->assoc_bssid,
4076 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4077 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4078 MACSTR, MAC2STR(_arg->assoc_bssid));
4079 }
f5a8d422
JM
4080 }
4081 if (!res)
4082 return NL_SKIP;
b3db1e1c
JM
4083 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4084 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4085 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4086 } else {
4087 ie = NULL;
4088 ie_len = 0;
4089 }
8c090654
JM
4090 if (bss[NL80211_BSS_BEACON_IES]) {
4091 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4092 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4093 } else {
4094 beacon_ie = NULL;
4095 beacon_ie_len = 0;
4096 }
3f5285e8 4097
3812464c
JM
4098 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4099 ie ? ie_len : beacon_ie_len))
4100 return NL_SKIP;
4101
8c090654 4102 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3f5285e8 4103 if (r == NULL)
b3db1e1c
JM
4104 return NL_SKIP;
4105 if (bss[NL80211_BSS_BSSID])
4106 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4107 ETH_ALEN);
4108 if (bss[NL80211_BSS_FREQUENCY])
4109 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4110 if (bss[NL80211_BSS_BEACON_INTERVAL])
4111 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4112 if (bss[NL80211_BSS_CAPABILITY])
4113 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
7c2849d2
JM
4114 r->flags |= WPA_SCAN_NOISE_INVALID;
4115 if (bss[NL80211_BSS_SIGNAL_MBM]) {
b3db1e1c 4116 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
7c2849d2
JM
4117 r->level /= 100; /* mBm to dBm */
4118 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4119 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4120 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
98ac6763 4121 r->flags |= WPA_SCAN_QUAL_INVALID;
7c2849d2
JM
4122 } else
4123 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
b3db1e1c
JM
4124 if (bss[NL80211_BSS_TSF])
4125 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
b3ad11bb
JM
4126 if (bss[NL80211_BSS_SEEN_MS_AGO])
4127 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
b3db1e1c 4128 r->ie_len = ie_len;
8c090654
JM
4129 pos = (u8 *) (r + 1);
4130 if (ie) {
4131 os_memcpy(pos, ie, ie_len);
4132 pos += ie_len;
4133 }
4134 r->beacon_ie_len = beacon_ie_len;
4135 if (beacon_ie)
4136 os_memcpy(pos, beacon_ie, beacon_ie_len);
3f5285e8 4137
e6b8efeb
JM
4138 if (bss[NL80211_BSS_STATUS]) {
4139 enum nl80211_bss_status status;
4140 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4141 switch (status) {
4142 case NL80211_BSS_STATUS_AUTHENTICATED:
4143 r->flags |= WPA_SCAN_AUTHENTICATED;
4144 break;
4145 case NL80211_BSS_STATUS_ASSOCIATED:
4146 r->flags |= WPA_SCAN_ASSOCIATED;
4147 break;
4148 default:
4149 break;
4150 }
4151 }
4152
46957a9b
JM
4153 /*
4154 * cfg80211 maintains separate BSS table entries for APs if the same
4155 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4156 * not use frequency as a separate key in the BSS table, so filter out
4157 * duplicated entries. Prefer associated BSS entry in such a case in
4158 * order to get the correct frequency into the BSS table.
4159 */
4160 for (i = 0; i < res->num; i++) {
4161 const u8 *s1, *s2;
4162 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4163 continue;
4164
4165 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4166 res->res[i]->ie_len, WLAN_EID_SSID);
4167 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4168 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4169 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4170 continue;
4171
4172 /* Same BSSID,SSID was already included in scan results */
4173 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4174 "for " MACSTR, MAC2STR(r->bssid));
4175
4176 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4177 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4178 os_free(res->res[i]);
4179 res->res[i] = r;
4180 } else
4181 os_free(r);
4182 return NL_SKIP;
4183 }
4184
067ffa26
JM
4185 tmp = os_realloc_array(res->res, res->num + 1,
4186 sizeof(struct wpa_scan_res *));
3f5285e8
JM
4187 if (tmp == NULL) {
4188 os_free(r);
b3db1e1c 4189 return NL_SKIP;
3f5285e8
JM
4190 }
4191 tmp[res->num++] = r;
4192 res->res = tmp;
b3db1e1c
JM
4193
4194 return NL_SKIP;
3f5285e8 4195}
b3db1e1c 4196
3f5285e8 4197
d72aad94
JM
4198static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4199 const u8 *addr)
4200{
4201 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4202 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
582507be 4203 "mismatch (" MACSTR ")", MAC2STR(addr));
d72aad94
JM
4204 wpa_driver_nl80211_mlme(drv, addr,
4205 NL80211_CMD_DEAUTHENTICATE,
77339912 4206 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
d72aad94
JM
4207 }
4208}
4209
4210
e6b8efeb
JM
4211static void wpa_driver_nl80211_check_bss_status(
4212 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4213{
4214 size_t i;
4215
4216 for (i = 0; i < res->num; i++) {
4217 struct wpa_scan_res *r = res->res[i];
4218 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4219 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4220 "indicates BSS status with " MACSTR
4221 " as authenticated",
4222 MAC2STR(r->bssid));
b1f625e0 4223 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
4224 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4225 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4226 0) {
4227 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4228 " in local state (auth=" MACSTR
4229 " assoc=" MACSTR ")",
4230 MAC2STR(drv->auth_bssid),
4231 MAC2STR(drv->bssid));
582507be 4232 clear_state_mismatch(drv, r->bssid);
e6b8efeb
JM
4233 }
4234 }
4235
4236 if (r->flags & WPA_SCAN_ASSOCIATED) {
4237 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4238 "indicate BSS status with " MACSTR
4239 " as associated",
4240 MAC2STR(r->bssid));
b1f625e0 4241 if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
4242 !drv->associated) {
4243 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4244 "(not associated) does not match "
4245 "with BSS state");
d72aad94 4246 clear_state_mismatch(drv, r->bssid);
b1f625e0 4247 } else if (is_sta_interface(drv->nlmode) &&
e6b8efeb
JM
4248 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4249 0) {
4250 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4251 "(associated with " MACSTR ") does "
4252 "not match with BSS state",
d72aad94
JM
4253 MAC2STR(drv->bssid));
4254 clear_state_mismatch(drv, r->bssid);
4255 clear_state_mismatch(drv, drv->bssid);
e6b8efeb
JM
4256 }
4257 }
4258 }
4259}
4260
4261
7e5ba1b9 4262static struct wpa_scan_results *
8856462d 4263nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3f5285e8 4264{
b3db1e1c 4265 struct nl_msg *msg;
3f5285e8 4266 struct wpa_scan_results *res;
b3db1e1c 4267 int ret;
3812464c 4268 struct nl80211_bss_info_arg arg;
3f5285e8
JM
4269
4270 res = os_zalloc(sizeof(*res));
b3db1e1c 4271 if (res == NULL)
8e2c104f 4272 return NULL;
b3db1e1c
JM
4273 msg = nlmsg_alloc();
4274 if (!msg)
4275 goto nla_put_failure;
3f5285e8 4276
9fb04070 4277 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
b3db1e1c 4278 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3f5285e8 4279
3812464c
JM
4280 arg.drv = drv;
4281 arg.res = res;
4282 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
b3db1e1c
JM
4283 msg = NULL;
4284 if (ret == 0) {
577db0ae
GM
4285 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4286 "BSSes)", (unsigned long) res->num);
4287 nl80211_get_noise_for_scan_results(drv, res);
b3db1e1c 4288 return res;
3f5285e8 4289 }
b3db1e1c
JM
4290 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4291 "(%s)", ret, strerror(-ret));
4292nla_put_failure:
4293 nlmsg_free(msg);
4294 wpa_scan_results_free(res);
4295 return NULL;
3f5285e8
JM
4296}
4297
4298
8856462d
JM
4299/**
4300 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4301 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4302 * Returns: Scan results on success, -1 on failure
4303 */
4304static struct wpa_scan_results *
4305wpa_driver_nl80211_get_scan_results(void *priv)
4306{
a2e40bb6
FF
4307 struct i802_bss *bss = priv;
4308 struct wpa_driver_nl80211_data *drv = bss->drv;
8856462d
JM
4309 struct wpa_scan_results *res;
4310
4311 res = nl80211_get_scan_results(drv);
4312 if (res)
4313 wpa_driver_nl80211_check_bss_status(drv, res);
4314 return res;
4315}
4316
4317
4318static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4319{
4320 struct wpa_scan_results *res;
4321 size_t i;
4322
4323 res = nl80211_get_scan_results(drv);
4324 if (res == NULL) {
4325 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4326 return;
4327 }
4328
4329 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4330 for (i = 0; i < res->num; i++) {
4331 struct wpa_scan_res *r = res->res[i];
4332 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4333 (int) i, (int) res->num, MAC2STR(r->bssid),
4334 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4335 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4336 }
4337
4338 wpa_scan_results_free(res);
4339}
4340
4341
9ebce9c5 4342static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
71934751
JM
4343 enum wpa_alg alg, const u8 *addr,
4344 int key_idx, int set_tx,
642187d6
JM
4345 const u8 *seq, size_t seq_len,
4346 const u8 *key, size_t key_len)
3f5285e8 4347{
a2e40bb6 4348 struct wpa_driver_nl80211_data *drv = bss->drv;
642187d6 4349 int ifindex = if_nametoindex(ifname);
3f5285e8 4350 struct nl_msg *msg;
1ad1cdc2 4351 int ret;
3f5285e8 4352
1ad1cdc2
JM
4353 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4354 "set_tx=%d seq_len=%lu key_len=%lu",
4355 __func__, ifindex, alg, addr, key_idx, set_tx,
3f5285e8 4356 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185
JM
4357#ifdef CONFIG_TDLS
4358 if (key_idx == -1)
4359 key_idx = 0;
4360#endif /* CONFIG_TDLS */
3f5285e8
JM
4361
4362 msg = nlmsg_alloc();
1ad1cdc2
JM
4363 if (!msg)
4364 return -ENOMEM;
3f5285e8
JM
4365
4366 if (alg == WPA_ALG_NONE) {
9fb04070 4367 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3f5285e8 4368 } else {
9fb04070 4369 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3f5285e8 4370 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
d723bab4
JM
4371 switch (alg) {
4372 case WPA_ALG_WEP:
4373 if (key_len == 5)
4374 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4375 WLAN_CIPHER_SUITE_WEP40);
4376 else
4377 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4378 WLAN_CIPHER_SUITE_WEP104);
4379 break;
4380 case WPA_ALG_TKIP:
4381 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4382 WLAN_CIPHER_SUITE_TKIP);
4383 break;
4384 case WPA_ALG_CCMP:
4385 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4386 WLAN_CIPHER_SUITE_CCMP);
4387 break;
eb7719ff
JM
4388 case WPA_ALG_GCMP:
4389 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4390 WLAN_CIPHER_SUITE_GCMP);
4391 break;
d723bab4
JM
4392 case WPA_ALG_IGTK:
4393 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4394 WLAN_CIPHER_SUITE_AES_CMAC);
4395 break;
369c8d7b
JM
4396 case WPA_ALG_SMS4:
4397 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4398 WLAN_CIPHER_SUITE_SMS4);
4399 break;
4400 case WPA_ALG_KRK:
4401 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4402 WLAN_CIPHER_SUITE_KRK);
4403 break;
d723bab4
JM
4404 default:
4405 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4406 "algorithm %d", __func__, alg);
3f5285e8
JM
4407 nlmsg_free(msg);
4408 return -1;
4409 }
4410 }
4411
849ef835 4412 if (seq && seq_len)
1ad1cdc2
JM
4413 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4414
0382097e 4415 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8
JM
4416 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4417 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
89c38e32
JM
4418
4419 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4420 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4421 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4422 NL80211_KEYTYPE_GROUP);
4423 }
60ea8187
JM
4424 } else if (addr && is_broadcast_ether_addr(addr)) {
4425 struct nl_msg *types;
4426 int err;
4427 wpa_printf(MSG_DEBUG, " broadcast key");
4428 types = nlmsg_alloc();
4429 if (!types)
4430 goto nla_put_failure;
4431 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4432 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4433 types);
4434 nlmsg_free(types);
4435 if (err)
4436 goto nla_put_failure;
3f5285e8
JM
4437 }
4438 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1ad1cdc2 4439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3f5285e8 4440
1ad1cdc2 4441 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
15664ad0 4442 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
4443 ret = 0;
4444 if (ret)
4445 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4446 ret, strerror(-ret));
3f5285e8 4447
1ad1cdc2
JM
4448 /*
4449 * If we failed or don't need to set the default TX key (below),
4450 * we're done here.
4451 */
4452 if (ret || !set_tx || alg == WPA_ALG_NONE)
4453 return ret;
b1f625e0 4454 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 4455 !is_broadcast_ether_addr(addr))
de12717a 4456 return ret;
3f5285e8 4457
1ad1cdc2
JM
4458 msg = nlmsg_alloc();
4459 if (!msg)
4460 return -ENOMEM;
3f5285e8 4461
9fb04070 4462 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
1ad1cdc2
JM
4463 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4464 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4465 if (alg == WPA_ALG_IGTK)
4466 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4467 else
4468 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
60ea8187
JM
4469 if (addr && is_broadcast_ether_addr(addr)) {
4470 struct nl_msg *types;
4471 int err;
4472 types = nlmsg_alloc();
4473 if (!types)
4474 goto nla_put_failure;
4475 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4476 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4477 types);
4478 nlmsg_free(types);
4479 if (err)
4480 goto nla_put_failure;
4481 } else if (addr) {
4482 struct nl_msg *types;
4483 int err;
4484 types = nlmsg_alloc();
4485 if (!types)
4486 goto nla_put_failure;
4487 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4488 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4489 types);
4490 nlmsg_free(types);
4491 if (err)
4492 goto nla_put_failure;
4493 }
3f5285e8 4494
1ad1cdc2
JM
4495 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4496 if (ret == -ENOENT)
4497 ret = 0;
4498 if (ret)
4499 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4500 "err=%d %s)", ret, strerror(-ret));
4501 return ret;
3f5285e8
JM
4502
4503nla_put_failure:
5883168a 4504 nlmsg_free(msg);
6241fcb1 4505 return -ENOBUFS;
3f5285e8
JM
4506}
4507
4508
71934751 4509static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
4510 int key_idx, int defkey,
4511 const u8 *seq, size_t seq_len,
4512 const u8 *key, size_t key_len)
4513{
4514 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4515 if (!key_attr)
4516 return -1;
4517
4518 if (defkey && alg == WPA_ALG_IGTK)
4519 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4520 else if (defkey)
4521 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4522
4523 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4524
d723bab4
JM
4525 switch (alg) {
4526 case WPA_ALG_WEP:
4527 if (key_len == 5)
2aa5f847
JM
4528 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4529 WLAN_CIPHER_SUITE_WEP40);
d723bab4 4530 else
2aa5f847
JM
4531 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4532 WLAN_CIPHER_SUITE_WEP104);
d723bab4
JM
4533 break;
4534 case WPA_ALG_TKIP:
2aa5f847 4535 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
d723bab4
JM
4536 break;
4537 case WPA_ALG_CCMP:
2aa5f847 4538 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
d723bab4 4539 break;
eb7719ff
JM
4540 case WPA_ALG_GCMP:
4541 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
4542 break;
d723bab4 4543 case WPA_ALG_IGTK:
2aa5f847
JM
4544 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4545 WLAN_CIPHER_SUITE_AES_CMAC);
d723bab4
JM
4546 break;
4547 default:
4548 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4549 "algorithm %d", __func__, alg);
0194fedb 4550 return -1;
d723bab4 4551 }
0194fedb
JB
4552
4553 if (seq && seq_len)
4554 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4555
4556 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4557
4558 nla_nest_end(msg, key_attr);
4559
4560 return 0;
4561 nla_put_failure:
4562 return -1;
4563}
4564
c811d5bc 4565
cfaab580
ZY
4566static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4567 struct nl_msg *msg)
4568{
4569 int i, privacy = 0;
4570 struct nlattr *nl_keys, *nl_key;
4571
4572 for (i = 0; i < 4; i++) {
4573 if (!params->wep_key[i])
4574 continue;
4575 privacy = 1;
4576 break;
4577 }
ce04af5a
JM
4578 if (params->wps == WPS_MODE_PRIVACY)
4579 privacy = 1;
4580 if (params->pairwise_suite &&
4581 params->pairwise_suite != WPA_CIPHER_NONE)
4582 privacy = 1;
4583
cfaab580
ZY
4584 if (!privacy)
4585 return 0;
4586
4587 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4588
4589 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4590 if (!nl_keys)
4591 goto nla_put_failure;
4592
4593 for (i = 0; i < 4; i++) {
4594 if (!params->wep_key[i])
4595 continue;
4596
4597 nl_key = nla_nest_start(msg, i);
4598 if (!nl_key)
4599 goto nla_put_failure;
4600
4601 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4602 params->wep_key[i]);
4603 if (params->wep_key_len[i] == 5)
4604 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4605 WLAN_CIPHER_SUITE_WEP40);
4606 else
4607 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4608 WLAN_CIPHER_SUITE_WEP104);
4609
4610 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4611
4612 if (i == params->wep_tx_keyidx)
4613 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4614
4615 nla_nest_end(msg, nl_key);
4616 }
4617 nla_nest_end(msg, nl_keys);
4618
4619 return 0;
4620
4621nla_put_failure:
4622 return -ENOBUFS;
4623}
4624
4625
c2a04078 4626static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
77339912
JM
4627 const u8 *addr, int cmd, u16 reason_code,
4628 int local_state_change)
c2a04078
JM
4629{
4630 int ret = -1;
4631 struct nl_msg *msg;
4632
4633 msg = nlmsg_alloc();
4634 if (!msg)
4635 return -1;
4636
9fb04070 4637 nl80211_cmd(drv, msg, 0, cmd);
c2a04078
JM
4638
4639 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4640 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
817762d9
MI
4641 if (addr)
4642 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
77339912
JM
4643 if (local_state_change)
4644 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
c2a04078
JM
4645
4646 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4647 msg = NULL;
4648 if (ret) {
3b7ea880
BG
4649 wpa_dbg(drv->ctx, MSG_DEBUG,
4650 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4651 reason_code, ret, strerror(-ret));
c2a04078
JM
4652 goto nla_put_failure;
4653 }
4654 ret = 0;
4655
4656nla_put_failure:
4657 nlmsg_free(msg);
4658 return ret;
4659}
3f5285e8
JM
4660
4661
cfaab580 4662static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
817762d9 4663 int reason_code)
cfaab580 4664{
817762d9 4665 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
cfaab580 4666 drv->associated = 0;
a8c5b43a 4667 drv->ignore_next_local_disconnect = 0;
817762d9
MI
4668 /* Disconnect command doesn't need BSSID - it uses cached value */
4669 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
77339912 4670 reason_code, 0);
cfaab580
ZY
4671}
4672
4673
9ebce9c5
JM
4674static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
4675 const u8 *addr, int reason_code)
3f5285e8 4676{
a2e40bb6 4677 struct wpa_driver_nl80211_data *drv = bss->drv;
cfaab580 4678 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
817762d9 4679 return wpa_driver_nl80211_disconnect(drv, reason_code);
2e75a2b3
JM
4680 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4681 __func__, MAC2STR(addr), reason_code);
13405f35 4682 drv->associated = 0;
21bdbe38
JM
4683 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4684 return nl80211_leave_ibss(drv);
c2a04078 4685 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
77339912 4686 reason_code, 0);
3f5285e8
JM
4687}
4688
4689
536fd62d
JM
4690static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4691 struct wpa_driver_auth_params *params)
4692{
4693 int i;
4694
4695 drv->auth_freq = params->freq;
4696 drv->auth_alg = params->auth_alg;
4697 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4698 drv->auth_local_state_change = params->local_state_change;
4699 drv->auth_p2p = params->p2p;
4700
4701 if (params->bssid)
4702 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4703 else
4704 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4705
4706 if (params->ssid) {
4707 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4708 drv->auth_ssid_len = params->ssid_len;
4709 } else
4710 drv->auth_ssid_len = 0;
4711
4712
4713 os_free(drv->auth_ie);
4714 drv->auth_ie = NULL;
4715 drv->auth_ie_len = 0;
4716 if (params->ie) {
4717 drv->auth_ie = os_malloc(params->ie_len);
4718 if (drv->auth_ie) {
4719 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4720 drv->auth_ie_len = params->ie_len;
4721 }
4722 }
4723
4724 for (i = 0; i < 4; i++) {
4725 if (params->wep_key[i] && params->wep_key_len[i] &&
4726 params->wep_key_len[i] <= 16) {
4727 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4728 params->wep_key_len[i]);
4729 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4730 } else
4731 drv->auth_wep_key_len[i] = 0;
4732 }
4733}
4734
4735
c2a04078 4736static int wpa_driver_nl80211_authenticate(
9ebce9c5 4737 struct i802_bss *bss, struct wpa_driver_auth_params *params)
c2a04078 4738{
a2e40bb6 4739 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 4740 int ret = -1, i;
c2a04078
JM
4741 struct nl_msg *msg;
4742 enum nl80211_auth_type type;
2f4f73b1 4743 enum nl80211_iftype nlmode;
6d6f4bb8 4744 int count = 0;
536fd62d
JM
4745 int is_retry;
4746
4747 is_retry = drv->retry_auth;
4748 drv->retry_auth = 0;
c2a04078
JM
4749
4750 drv->associated = 0;
e6b8efeb 4751 os_memset(drv->auth_bssid, 0, ETH_ALEN);
af473088 4752 /* FIX: IBSS mode */
2f4f73b1
EP
4753 nlmode = params->p2p ?
4754 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4755 if (drv->nlmode != nlmode &&
9ebce9c5 4756 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
4a867032
JM
4757 return -1;
4758
6d6f4bb8 4759retry:
c2a04078
JM
4760 msg = nlmsg_alloc();
4761 if (!msg)
4762 return -1;
4763
4764 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4765 drv->ifindex);
a0b2f99b 4766
9fb04070 4767 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
0194fedb 4768
a0b2f99b
JM
4769 for (i = 0; i < 4; i++) {
4770 if (!params->wep_key[i])
4771 continue;
9ebce9c5 4772 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2ea2fcc7 4773 NULL, i,
a0b2f99b
JM
4774 i == params->wep_tx_keyidx, NULL, 0,
4775 params->wep_key[i],
4776 params->wep_key_len[i]);
0194fedb
JB
4777 if (params->wep_tx_keyidx != i)
4778 continue;
4779 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4780 params->wep_key[i], params->wep_key_len[i])) {
4781 nlmsg_free(msg);
4782 return -1;
4783 }
a0b2f99b
JM
4784 }
4785
c2a04078
JM
4786 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4787 if (params->bssid) {
4788 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4789 MAC2STR(params->bssid));
4790 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4791 }
4792 if (params->freq) {
4793 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4794 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4795 }
4796 if (params->ssid) {
4797 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4798 params->ssid, params->ssid_len);
4799 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4800 params->ssid);
4801 }
4802 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4803 if (params->ie)
4804 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
569fed90
JM
4805 if (params->sae_data) {
4806 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
4807 params->sae_data_len);
4808 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
4809 params->sae_data);
4810 }
abd9fafa 4811 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
c2a04078 4812 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 4813 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
c2a04078 4814 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 4815 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
c2a04078 4816 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 4817 else if (params->auth_alg & WPA_AUTH_ALG_FT)
c2a04078 4818 type = NL80211_AUTHTYPE_FT;
569fed90
JM
4819 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
4820 type = NL80211_AUTHTYPE_SAE;
c2a04078
JM
4821 else
4822 goto nla_put_failure;
4823 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4824 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
77339912
JM
4825 if (params->local_state_change) {
4826 wpa_printf(MSG_DEBUG, " * Local state change only");
4827 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4828 }
c2a04078
JM
4829
4830 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4831 msg = NULL;
4832 if (ret) {
3b7ea880
BG
4833 wpa_dbg(drv->ctx, MSG_DEBUG,
4834 "nl80211: MLME command failed (auth): ret=%d (%s)",
4835 ret, strerror(-ret));
6d6f4bb8 4836 count++;
77339912
JM
4837 if (ret == -EALREADY && count == 1 && params->bssid &&
4838 !params->local_state_change) {
6d6f4bb8
JM
4839 /*
4840 * mac80211 does not currently accept new
4841 * authentication if we are already authenticated. As a
4842 * workaround, force deauthentication and try again.
4843 */
4844 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4845 "after forced deauthentication");
4846 wpa_driver_nl80211_deauthenticate(
5205c4f9 4847 bss, params->bssid,
6d6f4bb8
JM
4848 WLAN_REASON_PREV_AUTH_NOT_VALID);
4849 nlmsg_free(msg);
4850 goto retry;
4851 }
536fd62d
JM
4852
4853 if (ret == -ENOENT && params->freq && !is_retry) {
4854 /*
4855 * cfg80211 has likely expired the BSS entry even
4856 * though it was previously available in our internal
4857 * BSS table. To recover quickly, start a single
4858 * channel scan on the specified channel.
4859 */
4860 struct wpa_driver_scan_params scan;
4861 int freqs[2];
4862
4863 os_memset(&scan, 0, sizeof(scan));
4864 scan.num_ssids = 1;
4865 if (params->ssid) {
4866 scan.ssids[0].ssid = params->ssid;
4867 scan.ssids[0].ssid_len = params->ssid_len;
4868 }
4869 freqs[0] = params->freq;
4870 freqs[1] = 0;
4871 scan.freqs = freqs;
4872 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4873 "channel scan to refresh cfg80211 BSS "
4874 "entry");
4875 ret = wpa_driver_nl80211_scan(bss, &scan);
4876 if (ret == 0) {
4877 nl80211_copy_auth_params(drv, params);
4878 drv->scan_for_auth = 1;
4879 }
8c3ba078
JM
4880 } else if (is_retry) {
4881 /*
4882 * Need to indicate this with an event since the return
4883 * value from the retry is not delivered to core code.
4884 */
4885 union wpa_event_data event;
4886 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4887 "failed");
4888 os_memset(&event, 0, sizeof(event));
4889 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4890 ETH_ALEN);
4891 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4892 &event);
536fd62d
JM
4893 }
4894
c2a04078
JM
4895 goto nla_put_failure;
4896 }
4897 ret = 0;
4898 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
4899 "successfully");
4900
4901nla_put_failure:
4902 nlmsg_free(msg);
4903 return ret;
4904}
4905
4906
536fd62d
JM
4907static int wpa_driver_nl80211_authenticate_retry(
4908 struct wpa_driver_nl80211_data *drv)
4909{
4910 struct wpa_driver_auth_params params;
4911 struct i802_bss *bss = &drv->first_bss;
4912 int i;
4913
4914 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4915
4916 os_memset(&params, 0, sizeof(params));
4917 params.freq = drv->auth_freq;
4918 params.auth_alg = drv->auth_alg;
4919 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4920 params.local_state_change = drv->auth_local_state_change;
4921 params.p2p = drv->auth_p2p;
4922
4923 if (!is_zero_ether_addr(drv->auth_bssid_))
4924 params.bssid = drv->auth_bssid_;
4925
4926 if (drv->auth_ssid_len) {
4927 params.ssid = drv->auth_ssid;
4928 params.ssid_len = drv->auth_ssid_len;
4929 }
4930
4931 params.ie = drv->auth_ie;
4932 params.ie_len = drv->auth_ie_len;
4933
4934 for (i = 0; i < 4; i++) {
4935 if (drv->auth_wep_key_len[i]) {
4936 params.wep_key[i] = drv->auth_wep_key[i];
4937 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4938 }
4939 }
4940
4941 drv->retry_auth = 1;
4942 return wpa_driver_nl80211_authenticate(bss, &params);
4943}
4944
4945
282d5590
JM
4946struct phy_info_arg {
4947 u16 *num_modes;
4948 struct hostapd_hw_modes *modes;
43245552 4949 int last_mode, last_chan_idx;
282d5590
JM
4950};
4951
4952static int phy_info_handler(struct nl_msg *msg, void *arg)
4953{
4954 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4955 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4956 struct phy_info_arg *phy_info = arg;
4957
4958 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
4959
4960 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
4961 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4962 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
4963 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
4964 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
4965 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
4966 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
4967 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
4968 };
4969
4970 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
4971 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
4972 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
4973 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
4974 };
4975
4976 struct nlattr *nl_band;
4977 struct nlattr *nl_freq;
4978 struct nlattr *nl_rate;
4979 int rem_band, rem_freq, rem_rate;
4980 struct hostapd_hw_modes *mode;
43245552 4981 int idx;
282d5590
JM
4982
4983 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4984 genlmsg_attrlen(gnlh, 0), NULL);
4985
4986 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
4987 return NL_SKIP;
4988
4989 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
43245552
DJ
4990 if (phy_info->last_mode != nl_band->nla_type) {
4991 mode = os_realloc_array(phy_info->modes,
4992 *phy_info->num_modes + 1,
4993 sizeof(*mode));
4994 if (!mode)
4995 return NL_SKIP;
4996 phy_info->modes = mode;
4997
4998 mode = &phy_info->modes[*(phy_info->num_modes)];
4999 os_memset(mode, 0, sizeof(*mode));
5000 mode->mode = NUM_HOSTAPD_MODES;
5001 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
5002 *(phy_info->num_modes) += 1;
5003 phy_info->last_mode = nl_band->nla_type;
5004 phy_info->last_chan_idx = 0;
5005 } else
5006 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
282d5590
JM
5007
5008 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5009 nla_len(nl_band), NULL);
5010
5011 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
5012 mode->ht_capab = nla_get_u16(
5013 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
5014 }
5015
be8eb8ab
JM
5016 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
5017 mode->a_mpdu_params |= nla_get_u8(
5018 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
5019 0x03;
5020 }
5021
5022 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
5023 mode->a_mpdu_params |= nla_get_u8(
5024 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
5025 2;
5026 }
5027
08eb154d
JM
5028 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
5029 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
5030 u8 *mcs;
5031 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5032 os_memcpy(mode->mcs_set, mcs, 16);
5033 }
5034
990933fb
MP
5035 if (tb_band[NL80211_BAND_ATTR_VHT_CAPA]) {
5036 mode->vht_capab = nla_get_u32(
5037 tb_band[NL80211_BAND_ATTR_VHT_CAPA]);
5038 }
5039
5040 if (tb_band[NL80211_BAND_ATTR_VHT_MCS_SET] &&
5041 nla_len(tb_band[NL80211_BAND_ATTR_VHT_MCS_SET])) {
5042 u8 *mcs;
5043 mcs = nla_data(tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5044 os_memcpy(mode->vht_mcs_set, mcs, 8);
5045 }
5046
43245552
DJ
5047 if (tb_band[NL80211_BAND_ATTR_FREQS]) {
5048 int new_channels = 0;
5049 struct hostapd_channel_data *channel;
5050 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
5051 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5052 nla_data(nl_freq),
5053 nla_len(nl_freq), freq_policy);
5054 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5055 continue;
5056 new_channels++;
5057 }
282d5590 5058
43245552
DJ
5059 channel = os_realloc_array(mode->channels,
5060 mode->num_channels + new_channels,
5061 sizeof(struct hostapd_channel_data));
282d5590 5062
43245552
DJ
5063 if (!channel)
5064 return NL_SKIP;
282d5590 5065
43245552
DJ
5066 mode->channels = channel;
5067 mode->num_channels += new_channels;
5068
5069 idx = phy_info->last_chan_idx;
5070
5071 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
5072 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
5073 nla_len(nl_freq), freq_policy);
5074 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5075 continue;
5076
5077 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5078 mode->channels[idx].flag = 0;
5079
5080 /* mode is not set */
5081 if (mode->mode >= NUM_HOSTAPD_MODES) {
5082 /* crude heuristic */
5083 if (mode->channels[idx].freq < 4000)
5084 mode->mode = HOSTAPD_MODE_IEEE80211B;
5085 else if (mode->channels[idx].freq > 50000)
5086 mode->mode = HOSTAPD_MODE_IEEE80211AD;
5087 else
5088 mode->mode = HOSTAPD_MODE_IEEE80211A;
5089 }
282d5590 5090
43245552
DJ
5091 switch (mode->mode) {
5092 case HOSTAPD_MODE_IEEE80211AD:
7829894c 5093 mode->channels[idx].chan =
43245552
DJ
5094 (mode->channels[idx].freq - 56160) /
5095 2160;
5096 break;
5097 case HOSTAPD_MODE_IEEE80211A:
5098 mode->channels[idx].chan =
5099 mode->channels[idx].freq / 5 - 1000;
5100 break;
5101 case HOSTAPD_MODE_IEEE80211B:
5102 case HOSTAPD_MODE_IEEE80211G:
5103 if (mode->channels[idx].freq == 2484)
5104 mode->channels[idx].chan = 14;
5105 else
5106 mode->channels[idx].chan =
5107 (mode->channels[idx].freq -
5108 2407) / 5;
5109 break;
5110 default:
5111 break;
5112 }
282d5590 5113
43245552
DJ
5114 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5115 mode->channels[idx].flag |=
5116 HOSTAPD_CHAN_DISABLED;
5117 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5118 mode->channels[idx].flag |=
5119 HOSTAPD_CHAN_PASSIVE_SCAN;
5120 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5121 mode->channels[idx].flag |=
5122 HOSTAPD_CHAN_NO_IBSS;
5123 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5124 mode->channels[idx].flag |=
5125 HOSTAPD_CHAN_RADAR;
5126
5127 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5128 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5129 mode->channels[idx].max_tx_power =
5130 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
5131
5132 idx++;
5133 }
5134 phy_info->last_chan_idx = idx;
282d5590
JM
5135 }
5136
43245552
DJ
5137 if (tb_band[NL80211_BAND_ATTR_RATES]) {
5138 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
5139 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
5140 nla_len(nl_rate), rate_policy);
5141 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5142 continue;
5143 mode->num_rates++;
5144 }
282d5590 5145
43245552
DJ
5146 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5147 if (!mode->rates)
5148 return NL_SKIP;
282d5590 5149
43245552 5150 idx = 0;
282d5590 5151
43245552
DJ
5152 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
5153 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
5154 nla_len(nl_rate), rate_policy);
5155 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5156 continue;
5157 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
282d5590 5158
43245552
DJ
5159 /* crude heuristic */
5160 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
5161 mode->rates[idx] > 200)
5162 mode->mode = HOSTAPD_MODE_IEEE80211G;
282d5590 5163
43245552
DJ
5164 idx++;
5165 }
282d5590
JM
5166 }
5167 }
5168
5169 return NL_SKIP;
5170}
5171
5172static struct hostapd_hw_modes *
5173wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
5174{
5175 u16 m;
5176 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5177 int i, mode11g_idx = -1;
5178
5179 /* If only 802.11g mode is included, use it to construct matching
5180 * 802.11b mode data. */
5181
5182 for (m = 0; m < *num_modes; m++) {
5183 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5184 return modes; /* 802.11b already included */
5185 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5186 mode11g_idx = m;
5187 }
5188
5189 if (mode11g_idx < 0)
5190 return modes; /* 2.4 GHz band not supported at all */
5191
067ffa26 5192 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
282d5590
JM
5193 if (nmodes == NULL)
5194 return modes; /* Could not add 802.11b mode */
5195
5196 mode = &nmodes[*num_modes];
5197 os_memset(mode, 0, sizeof(*mode));
5198 (*num_modes)++;
5199 modes = nmodes;
5200
5201 mode->mode = HOSTAPD_MODE_IEEE80211B;
5202
5203 mode11g = &modes[mode11g_idx];
5204 mode->num_channels = mode11g->num_channels;
5205 mode->channels = os_malloc(mode11g->num_channels *
5206 sizeof(struct hostapd_channel_data));
5207 if (mode->channels == NULL) {
5208 (*num_modes)--;
5209 return modes; /* Could not add 802.11b mode */
5210 }
5211 os_memcpy(mode->channels, mode11g->channels,
5212 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5213
5214 mode->num_rates = 0;
fb7842aa 5215 mode->rates = os_malloc(4 * sizeof(int));
282d5590
JM
5216 if (mode->rates == NULL) {
5217 os_free(mode->channels);
5218 (*num_modes)--;
5219 return modes; /* Could not add 802.11b mode */
5220 }
5221
5222 for (i = 0; i < mode11g->num_rates; i++) {
fb7842aa
JM
5223 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5224 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
282d5590
JM
5225 continue;
5226 mode->rates[mode->num_rates] = mode11g->rates[i];
5227 mode->num_rates++;
5228 if (mode->num_rates == 4)
5229 break;
5230 }
5231
5232 if (mode->num_rates == 0) {
5233 os_free(mode->channels);
5234 os_free(mode->rates);
5235 (*num_modes)--;
5236 return modes; /* No 802.11b rates */
5237 }
5238
5239 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5240 "information");
5241
5242 return modes;
5243}
5244
5245
d8e66e80
JM
5246static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5247 int end)
5248{
5249 int c;
5250
5251 for (c = 0; c < mode->num_channels; c++) {
5252 struct hostapd_channel_data *chan = &mode->channels[c];
5253 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5254 chan->flag |= HOSTAPD_CHAN_HT40;
5255 }
5256}
5257
5258
5259static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5260 int end)
5261{
5262 int c;
5263
5264 for (c = 0; c < mode->num_channels; c++) {
5265 struct hostapd_channel_data *chan = &mode->channels[c];
5266 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5267 continue;
5268 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5269 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5270 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5271 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5272 }
5273}
5274
5275
5276static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5277 struct phy_info_arg *results)
5278{
5279 u32 start, end, max_bw;
5280 u16 m;
5281
5282 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5283 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5284 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5285 return;
5286
5287 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5288 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5289 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5290
5291 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5292 start, end, max_bw);
5293 if (max_bw < 40)
5294 return;
5295
5296 for (m = 0; m < *results->num_modes; m++) {
5297 if (!(results->modes[m].ht_capab &
5298 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5299 continue;
5300 nl80211_set_ht40_mode(&results->modes[m], start, end);
5301 }
5302}
5303
5304
5305static void nl80211_reg_rule_sec(struct nlattr *tb[],
5306 struct phy_info_arg *results)
5307{
5308 u32 start, end, max_bw;
5309 u16 m;
5310
5311 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5312 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5313 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5314 return;
5315
5316 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5317 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5318 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5319
5320 if (max_bw < 20)
5321 return;
5322
5323 for (m = 0; m < *results->num_modes; m++) {
5324 if (!(results->modes[m].ht_capab &
5325 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5326 continue;
5327 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5328 }
5329}
5330
5331
5332static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5333{
5334 struct phy_info_arg *results = arg;
5335 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5336 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5337 struct nlattr *nl_rule;
5338 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5339 int rem_rule;
5340 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5341 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5342 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5343 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5344 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5345 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5346 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5347 };
5348
5349 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5350 genlmsg_attrlen(gnlh, 0), NULL);
5351 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5352 !tb_msg[NL80211_ATTR_REG_RULES]) {
5353 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5354 "available");
5355 return NL_SKIP;
5356 }
5357
5358 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5359 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5360
5361 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5362 {
5363 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5364 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5365 nl80211_reg_rule_ht40(tb_rule, results);
5366 }
5367
5368 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5369 {
5370 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5371 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5372 nl80211_reg_rule_sec(tb_rule, results);
5373 }
5374
5375 return NL_SKIP;
5376}
5377
5378
5379static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5380 struct phy_info_arg *results)
5381{
5382 struct nl_msg *msg;
5383
5384 msg = nlmsg_alloc();
5385 if (!msg)
5386 return -ENOMEM;
5387
9fb04070 5388 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
d8e66e80
JM
5389 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5390}
5391
5392
282d5590
JM
5393static struct hostapd_hw_modes *
5394wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5395{
43245552 5396 u32 feat;
a2e40bb6
FF
5397 struct i802_bss *bss = priv;
5398 struct wpa_driver_nl80211_data *drv = bss->drv;
282d5590
JM
5399 struct nl_msg *msg;
5400 struct phy_info_arg result = {
5401 .num_modes = num_modes,
5402 .modes = NULL,
43245552 5403 .last_mode = -1,
282d5590
JM
5404 };
5405
5406 *num_modes = 0;
5407 *flags = 0;
5408
5409 msg = nlmsg_alloc();
5410 if (!msg)
5411 return NULL;
5412
43245552
DJ
5413 feat = get_nl80211_protocol_features(drv);
5414 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
5415 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
5416 else
5417 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
282d5590 5418
43245552 5419 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
282d5590
JM
5420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5421
d8e66e80
JM
5422 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5423 nl80211_set_ht40_flags(drv, &result);
282d5590 5424 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
d8e66e80 5425 }
5883168a 5426 msg = NULL;
282d5590 5427 nla_put_failure:
5883168a 5428 nlmsg_free(msg);
282d5590
JM
5429 return NULL;
5430}
5431
5432
a11241fa
JB
5433static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5434 const void *data, size_t len,
5435 int encrypt, int noack)
2c2010ac
JM
5436{
5437 __u8 rtap_hdr[] = {
5438 0x00, 0x00, /* radiotap version */
5439 0x0e, 0x00, /* radiotap length */
5440 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5441 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5442 0x00, /* padding */
5443 0x00, 0x00, /* RX and TX flags to indicate that */
5444 0x00, 0x00, /* this is the injected frame directly */
5445 };
5446 struct iovec iov[2] = {
5447 {
5448 .iov_base = &rtap_hdr,
5449 .iov_len = sizeof(rtap_hdr),
5450 },
5451 {
5452 .iov_base = (void *) data,
5453 .iov_len = len,
5454 }
5455 };
5456 struct msghdr msg = {
5457 .msg_name = NULL,
5458 .msg_namelen = 0,
5459 .msg_iov = iov,
5460 .msg_iovlen = 2,
5461 .msg_control = NULL,
5462 .msg_controllen = 0,
5463 .msg_flags = 0,
5464 };
ebbec8b2 5465 int res;
fab25336 5466 u16 txflags = 0;
2c2010ac
JM
5467
5468 if (encrypt)
5469 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5470
866af8b6
JM
5471 if (drv->monitor_sock < 0) {
5472 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5473 "for %s", __func__);
5474 return -1;
5475 }
5476
fab25336
HS
5477 if (noack)
5478 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
9d7a63dc 5479 WPA_PUT_LE16(&rtap_hdr[12], txflags);
fab25336 5480
ebbec8b2
JM
5481 res = sendmsg(drv->monitor_sock, &msg, 0);
5482 if (res < 0) {
5483 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5484 return -1;
5485 }
5486 return 0;
2c2010ac
JM
5487}
5488
5489
a11241fa
JB
5490static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5491 const void *data, size_t len,
55231068
JM
5492 int encrypt, int noack,
5493 unsigned int freq, int no_cck,
5494 int offchanok, unsigned int wait_time)
a11241fa
JB
5495{
5496 struct wpa_driver_nl80211_data *drv = bss->drv;
5497 u64 cookie;
5498
55231068
JM
5499 if (freq == 0)
5500 freq = bss->freq;
5501
a11241fa
JB
5502 if (drv->use_monitor)
5503 return wpa_driver_nl80211_send_mntr(drv, data, len,
5504 encrypt, noack);
5505
55231068
JM
5506 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5507 &cookie, no_cck, noack, offchanok);
a11241fa
JB
5508}
5509
5510
9ebce9c5
JM
5511static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
5512 size_t data_len, int noack,
5513 unsigned int freq, int no_cck,
5514 int offchanok,
5515 unsigned int wait_time)
2c2010ac 5516{
a2e40bb6 5517 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 5518 struct ieee80211_mgmt *mgmt;
7a47d567 5519 int encrypt = 1;
2c2010ac
JM
5520 u16 fc;
5521
5522 mgmt = (struct ieee80211_mgmt *) data;
5523 fc = le_to_host16(mgmt->frame_control);
5524
b1f625e0 5525 if (is_sta_interface(drv->nlmode) &&
5582a5d1
JB
5526 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5527 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5528 /*
5529 * The use of last_mgmt_freq is a bit of a hack,
5530 * but it works due to the single-threaded nature
5531 * of wpa_supplicant.
5532 */
55231068
JM
5533 if (freq == 0)
5534 freq = drv->last_mgmt_freq;
5535 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7
JB
5536 data, data_len, NULL, 1, noack,
5537 1);
5582a5d1
JB
5538 }
5539
61cbe2ff 5540 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
55231068
JM
5541 if (freq == 0)
5542 freq = bss->freq;
b5671498
JM
5543 return nl80211_send_frame_cmd(bss, freq,
5544 (int) freq == bss->freq ? 0 :
5545 wait_time,
d8d6b32e
DG
5546 data, data_len,
5547 &drv->send_action_cookie,
55231068 5548 no_cck, noack, offchanok);
86957e62
JM
5549 }
5550
2c2010ac
JM
5551 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5552 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5553 /*
5554 * Only one of the authentication frame types is encrypted.
5555 * In order for static WEP encryption to work properly (i.e.,
5556 * to not encrypt the frame), we need to tell mac80211 about
5557 * the frames that must not be encrypted.
5558 */
5559 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5560 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
5561 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5562 encrypt = 0;
2c2010ac
JM
5563 }
5564
a11241fa 5565 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068
JM
5566 noack, freq, no_cck, offchanok,
5567 wait_time);
5568}
5569
5570
31357268 5571static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47
JM
5572 int slot, int ht_opmode, int ap_isolate,
5573 int *basic_rates)
31357268
JM
5574{
5575 struct wpa_driver_nl80211_data *drv = bss->drv;
5576 struct nl_msg *msg;
5577
5578 msg = nlmsg_alloc();
5579 if (!msg)
5580 return -ENOMEM;
5581
9fb04070 5582 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
31357268
JM
5583
5584 if (cts >= 0)
5585 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5586 if (preamble >= 0)
5587 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5588 if (slot >= 0)
5589 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5590 if (ht_opmode >= 0)
5591 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
d03e8d11
JM
5592 if (ap_isolate >= 0)
5593 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
e5693c47
JM
5594
5595 if (basic_rates) {
5596 u8 rates[NL80211_MAX_SUPP_RATES];
5597 u8 rates_len = 0;
5598 int i;
5599
5600 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5601 i++)
5602 rates[rates_len++] = basic_rates[i] / 5;
5603
5604 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5605 }
5606
31357268
JM
5607 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5608
5609 return send_and_recv_msgs(drv, msg, NULL, NULL);
5610 nla_put_failure:
5883168a 5611 nlmsg_free(msg);
31357268
JM
5612 return -ENOBUFS;
5613}
5614
5615
19c3b566
JM
5616static int wpa_driver_nl80211_set_ap(void *priv,
5617 struct wpa_driver_ap_params *params)
d2440ba0 5618{
a2e40bb6
FF
5619 struct i802_bss *bss = priv;
5620 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
5621 struct nl_msg *msg;
5622 u8 cmd = NL80211_CMD_NEW_BEACON;
5623 int ret;
b4fd6fab 5624 int beacon_set;
8b897f5a 5625 int ifindex = if_nametoindex(bss->ifname);
b11d1d64
JM
5626 int num_suites;
5627 u32 suites[10];
5628 u32 ver;
b4fd6fab 5629
b4fd6fab 5630 beacon_set = bss->beacon_set;
d2440ba0
JM
5631
5632 msg = nlmsg_alloc();
5633 if (!msg)
5634 return -ENOMEM;
5635
5636 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
5637 beacon_set);
5638 if (beacon_set)
d2440ba0
JM
5639 cmd = NL80211_CMD_SET_BEACON;
5640
9fb04070 5641 nl80211_cmd(drv, msg, 0, cmd);
19c3b566
JM
5642 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
5643 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
b4fd6fab 5644 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
19c3b566
JM
5645 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
5646 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
ccb941e6
JM
5647 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5648 params->ssid);
5ed33546
AN
5649 if (params->proberesp && params->proberesp_len)
5650 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5651 params->proberesp);
97a7a0b5
JM
5652 switch (params->hide_ssid) {
5653 case NO_SSID_HIDING:
5654 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5655 NL80211_HIDDEN_SSID_NOT_IN_USE);
5656 break;
5657 case HIDDEN_SSID_ZERO_LEN:
5658 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5659 NL80211_HIDDEN_SSID_ZERO_LEN);
5660 break;
5661 case HIDDEN_SSID_ZERO_CONTENTS:
5662 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5663 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5664 break;
5665 }
b11d1d64
JM
5666 if (params->privacy)
5667 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5668 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5669 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5670 /* Leave out the attribute */
5671 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5672 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5673 NL80211_AUTHTYPE_SHARED_KEY);
5674 else
5675 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5676 NL80211_AUTHTYPE_OPEN_SYSTEM);
5677
5678 ver = 0;
5679 if (params->wpa_version & WPA_PROTO_WPA)
5680 ver |= NL80211_WPA_VERSION_1;
5681 if (params->wpa_version & WPA_PROTO_RSN)
5682 ver |= NL80211_WPA_VERSION_2;
5683 if (ver)
5684 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5685
5686 num_suites = 0;
5687 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5688 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5689 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5690 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5691 if (num_suites) {
5692 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5693 num_suites * sizeof(u32), suites);
5694 }
5695
9f12614b
JB
5696 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5697 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5698 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5699
b11d1d64
JM
5700 num_suites = 0;
5701 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5702 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
eb7719ff
JM
5703 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
5704 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
b11d1d64
JM
5705 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5706 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5707 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5708 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5709 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5710 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5711 if (num_suites) {
5712 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5713 num_suites * sizeof(u32), suites);
5714 }
5715
5716 switch (params->group_cipher) {
5717 case WPA_CIPHER_CCMP:
5718 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5719 WLAN_CIPHER_SUITE_CCMP);
5720 break;
eb7719ff
JM
5721 case WPA_CIPHER_GCMP:
5722 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5723 WLAN_CIPHER_SUITE_GCMP);
5724 break;
b11d1d64
JM
5725 case WPA_CIPHER_TKIP:
5726 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5727 WLAN_CIPHER_SUITE_TKIP);
5728 break;
5729 case WPA_CIPHER_WEP104:
5730 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5731 WLAN_CIPHER_SUITE_WEP104);
5732 break;
5733 case WPA_CIPHER_WEP40:
5734 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5735 WLAN_CIPHER_SUITE_WEP40);
5736 break;
5737 }
d2440ba0 5738
fb91db56
JM
5739 if (params->beacon_ies) {
5740 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5741 wpabuf_head(params->beacon_ies));
5742 }
5743 if (params->proberesp_ies) {
5744 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5745 wpabuf_len(params->proberesp_ies),
5746 wpabuf_head(params->proberesp_ies));
5747 }
5748 if (params->assocresp_ies) {
5749 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5750 wpabuf_len(params->assocresp_ies),
5751 wpabuf_head(params->assocresp_ies));
5752 }
5753
a0133ee1
VT
5754 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
5755 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
5756 params->ap_max_inactivity);
5757 }
5758
d2440ba0
JM
5759 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5760 if (ret) {
5761 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5762 ret, strerror(-ret));
b4fd6fab 5763 } else {
b4fd6fab 5764 bss->beacon_set = 1;
31357268 5765 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 5766 params->short_slot_time, params->ht_opmode,
e5693c47 5767 params->isolate, params->basic_rates);
b4fd6fab 5768 }
d2440ba0
JM
5769 return ret;
5770 nla_put_failure:
5883168a 5771 nlmsg_free(msg);
d2440ba0
JM
5772 return -ENOBUFS;
5773}
5774
5775
e4fb2167 5776static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
89b800d7 5777 struct hostapd_freq_params *freq)
1581b38b 5778{
e4fb2167 5779 struct wpa_driver_nl80211_data *drv = bss->drv;
1581b38b 5780 struct nl_msg *msg;
d2440ba0 5781 int ret;
1581b38b 5782
89b800d7
JB
5783 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
5784 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
5785 freq->freq, freq->ht_enabled, freq->vht_enabled,
5786 freq->bandwidth, freq->center_freq1, freq->center_freq2);
1581b38b
JM
5787 msg = nlmsg_alloc();
5788 if (!msg)
5789 return -1;
5790
9fb04070 5791 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
1581b38b
JM
5792
5793 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
89b800d7
JB
5794 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
5795 if (freq->vht_enabled) {
5796 switch (freq->bandwidth) {
5797 case 20:
5798 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5799 NL80211_CHAN_WIDTH_20);
5800 break;
5801 case 40:
5802 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5803 NL80211_CHAN_WIDTH_40);
5804 break;
5805 case 80:
5806 if (freq->center_freq2)
5807 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5808 NL80211_CHAN_WIDTH_80P80);
5809 else
5810 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5811 NL80211_CHAN_WIDTH_80);
5812 break;
5813 case 160:
5814 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
5815 NL80211_CHAN_WIDTH_160);
5816 break;
5817 default:
5818 return -1;
5819 }
5820 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
5821 if (freq->center_freq2)
5822 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
5823 freq->center_freq2);
5824 } else if (freq->ht_enabled) {
5825 switch (freq->sec_channel_offset) {
f019981a
JM
5826 case -1:
5827 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5828 NL80211_CHAN_HT40MINUS);
5829 break;
5830 case 1:
5831 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5832 NL80211_CHAN_HT40PLUS);
5833 break;
5834 default:
5835 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5836 NL80211_CHAN_HT20);
5837 break;
5838 }
5839 }
1581b38b 5840
d2440ba0 5841 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5842 msg = NULL;
e4fb2167 5843 if (ret == 0) {
89b800d7 5844 bss->freq = freq->freq;
1581b38b 5845 return 0;
e4fb2167 5846 }
f019981a 5847 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 5848 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b 5849nla_put_failure:
5883168a 5850 nlmsg_free(msg);
1581b38b
JM
5851 return -1;
5852}
5853
0f4e8b4f 5854
95ab6063
AN
5855static u32 sta_flags_nl80211(int flags)
5856{
5857 u32 f = 0;
5858
5859 if (flags & WPA_STA_AUTHORIZED)
5860 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5861 if (flags & WPA_STA_WMM)
5862 f |= BIT(NL80211_STA_FLAG_WME);
5863 if (flags & WPA_STA_SHORT_PREAMBLE)
5864 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5865 if (flags & WPA_STA_MFP)
5866 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
5867 if (flags & WPA_STA_TDLS_PEER)
5868 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
95ab6063
AN
5869
5870 return f;
5871}
5872
5873
62847751 5874static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
5875 struct hostapd_sta_add_params *params)
5876{
a2e40bb6
FF
5877 struct i802_bss *bss = priv;
5878 struct wpa_driver_nl80211_data *drv = bss->drv;
774bfa62 5879 struct nl_msg *msg, *wme = NULL;
95ab6063 5880 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
5881 int ret = -ENOBUFS;
5882
45b722f1
AN
5883 if ((params->flags & WPA_STA_TDLS_PEER) &&
5884 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5885 return -EOPNOTSUPP;
5886
0f4e8b4f
JM
5887 msg = nlmsg_alloc();
5888 if (!msg)
5889 return -ENOMEM;
5890
e4dea253
JM
5891 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
5892 params->set ? "Set" : "Add", MAC2STR(params->addr));
45b722f1
AN
5893 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
5894 NL80211_CMD_NEW_STATION);
0f4e8b4f 5895
62847751 5896 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
0f4e8b4f 5897 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
0f4e8b4f
JM
5898 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
5899 params->supp_rates);
e4dea253
JM
5900 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
5901 params->supp_rates_len);
45b722f1 5902 if (!params->set) {
e4dea253 5903 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
45b722f1 5904 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
e4dea253
JM
5905 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
5906 params->listen_interval);
45b722f1
AN
5907 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5908 params->listen_interval);
5909 }
0f4e8b4f 5910 if (params->ht_capabilities) {
e4dea253
JM
5911 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
5912 (u8 *) params->ht_capabilities,
5913 sizeof(*params->ht_capabilities));
0f4e8b4f 5914 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
fc4e2d95
JM
5915 sizeof(*params->ht_capabilities),
5916 params->ht_capabilities);
0f4e8b4f 5917 }
0f4e8b4f 5918
05a8d422 5919 if (params->vht_capabilities) {
e4dea253
JM
5920 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
5921 (u8 *) params->vht_capabilities,
5922 sizeof(*params->vht_capabilities));
05a8d422
JB
5923 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
5924 sizeof(*params->vht_capabilities),
5925 params->vht_capabilities);
5926 }
5927
122d16f2
SD
5928 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
5929 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
5930
5931 if (params->ext_capab) {
5932 wpa_hexdump(MSG_DEBUG, " * ext_capab",
5933 params->ext_capab, params->ext_capab_len);
5934 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
5935 params->ext_capab_len, params->ext_capab);
5936 }
5937
95ab6063
AN
5938 os_memset(&upd, 0, sizeof(upd));
5939 upd.mask = sta_flags_nl80211(params->flags);
5940 upd.set = upd.mask;
e4dea253
JM
5941 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
5942 upd.set, upd.mask);
95ab6063
AN
5943 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5944
774bfa62
EP
5945 if (params->flags & WPA_STA_WMM) {
5946 wme = nlmsg_alloc();
5947 if (!wme)
5948 goto nla_put_failure;
5949
e4dea253 5950 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
774bfa62 5951 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
5d061637
JY
5952 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
5953 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
bdaf1748 5954 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
5d061637 5955 WMM_QOSINFO_STA_SP_MASK);
f0494d0f
JM
5956 if (nla_put_nested(msg, NL80211_ATTR_STA_WME, wme) < 0)
5957 goto nla_put_failure;
774bfa62
EP
5958 }
5959
0f4e8b4f 5960 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 5961 msg = NULL;
0f4e8b4f 5962 if (ret)
45b722f1
AN
5963 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
5964 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
5965 strerror(-ret));
0f4e8b4f
JM
5966 if (ret == -EEXIST)
5967 ret = 0;
5968 nla_put_failure:
5d061637 5969 nlmsg_free(wme);
5883168a 5970 nlmsg_free(msg);
0f4e8b4f
JM
5971 return ret;
5972}
5973
5974
9ebce9c5 5975static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
0f4e8b4f 5976{
a2e40bb6 5977 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
5978 struct nl_msg *msg;
5979 int ret;
5980
5981 msg = nlmsg_alloc();
5982 if (!msg)
5983 return -ENOMEM;
5984
9fb04070 5985 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
0f4e8b4f
JM
5986
5987 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 5988 if_nametoindex(bss->ifname));
0f4e8b4f
JM
5989 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5990
5991 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5992 if (ret == -ENOENT)
5993 return 0;
5994 return ret;
5995 nla_put_failure:
5883168a 5996 nlmsg_free(msg);
0f4e8b4f
JM
5997 return -ENOBUFS;
5998}
5999
1581b38b 6000
0915d02c
JM
6001static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6002 int ifidx)
6003{
6004 struct nl_msg *msg;
6005
c6e8e8e4
JM
6006 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6007
2135f224
JM
6008 /* stop listening for EAPOL on this interface */
6009 del_ifidx(drv, ifidx);
2135f224 6010
0915d02c
JM
6011 msg = nlmsg_alloc();
6012 if (!msg)
6013 goto nla_put_failure;
6014
9fb04070 6015 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
6016 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6017
6018 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6019 return;
5883168a 6020 msg = NULL;
0915d02c 6021 nla_put_failure:
5883168a 6022 nlmsg_free(msg);
e748062b 6023 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
6024}
6025
6026
a1922f93
JM
6027static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6028{
6029 switch (mode) {
6030 case NL80211_IFTYPE_ADHOC:
6031 return "ADHOC";
6032 case NL80211_IFTYPE_STATION:
6033 return "STATION";
6034 case NL80211_IFTYPE_AP:
6035 return "AP";
6036 case NL80211_IFTYPE_MONITOR:
6037 return "MONITOR";
6038 case NL80211_IFTYPE_P2P_CLIENT:
6039 return "P2P_CLIENT";
6040 case NL80211_IFTYPE_P2P_GO:
6041 return "P2P_GO";
6042 default:
6043 return "unknown";
6044 }
6045}
6046
6047
a35187e7
KH
6048static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6049 const char *ifname,
6050 enum nl80211_iftype iftype,
fbbfcbac 6051 const u8 *addr, int wds)
0915d02c
JM
6052{
6053 struct nl_msg *msg, *flags = NULL;
6054 int ifidx;
6055 int ret = -ENOBUFS;
6056
a1922f93
JM
6057 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6058 iftype, nl80211_iftype_str(iftype));
6059
0915d02c
JM
6060 msg = nlmsg_alloc();
6061 if (!msg)
6062 return -1;
6063
9fb04070 6064 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
0915d02c
JM
6065 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6066 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6067 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6068
6069 if (iftype == NL80211_IFTYPE_MONITOR) {
6070 int err;
6071
6072 flags = nlmsg_alloc();
6073 if (!flags)
6074 goto nla_put_failure;
6075
6076 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
6077
6078 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
6079
6080 nlmsg_free(flags);
6081
6082 if (err)
6083 goto nla_put_failure;
fbbfcbac
FF
6084 } else if (wds) {
6085 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
0915d02c
JM
6086 }
6087
6088 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 6089 msg = NULL;
0915d02c
JM
6090 if (ret) {
6091 nla_put_failure:
5883168a 6092 nlmsg_free(msg);
a35187e7
KH
6093 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6094 ifname, ret, strerror(-ret));
0915d02c
JM
6095 return ret;
6096 }
6097
6098 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
6099 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6100 ifname, ifidx);
0915d02c
JM
6101
6102 if (ifidx <= 0)
6103 return -1;
6104
2135f224
JM
6105 /* start listening for EAPOL on this interface */
6106 add_ifidx(drv, ifidx);
6107
7bfc47c3 6108 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 6109 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
6110 nl80211_remove_iface(drv, ifidx);
6111 return -1;
2135f224 6112 }
2135f224 6113
0915d02c
JM
6114 return ifidx;
6115}
22a7c9d7
JM
6116
6117
a35187e7
KH
6118static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6119 const char *ifname, enum nl80211_iftype iftype,
fbbfcbac 6120 const u8 *addr, int wds)
a35187e7
KH
6121{
6122 int ret;
6123
fbbfcbac 6124 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
a35187e7 6125
ffbf1eaa 6126 /* if error occurred and interface exists already */
a35187e7
KH
6127 if (ret == -ENFILE && if_nametoindex(ifname)) {
6128 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6129
6130 /* Try to remove the interface that was already there. */
6131 nl80211_remove_iface(drv, if_nametoindex(ifname));
6132
6133 /* Try to create the interface again */
fbbfcbac
FF
6134 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6135 wds);
a35187e7
KH
6136 }
6137
b3af99d2 6138 if (ret >= 0 && is_p2p_interface(iftype))
4e5cb1a3
JM
6139 nl80211_disable_11b_rates(drv, ret, 1);
6140
a35187e7
KH
6141 return ret;
6142}
0915d02c 6143
2135f224 6144
0915d02c
JM
6145static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6146{
6147 struct ieee80211_hdr *hdr;
f8b1f695
JM
6148 u16 fc;
6149 union wpa_event_data event;
0915d02c
JM
6150
6151 hdr = (struct ieee80211_hdr *) buf;
6152 fc = le_to_host16(hdr->frame_control);
6153
f8b1f695
JM
6154 os_memset(&event, 0, sizeof(event));
6155 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6156 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6157 event.tx_status.dst = hdr->addr1;
6158 event.tx_status.data = buf;
6159 event.tx_status.data_len = len;
6160 event.tx_status.ack = ok;
6161 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
0915d02c
JM
6162}
6163
6164
4b9841d3 6165static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
0d9fc3d8 6166 u8 *buf, size_t len)
0915d02c 6167{
9b90955e
JB
6168 struct ieee80211_hdr *hdr = (void *)buf;
6169 u16 fc;
f8b1f695 6170 union wpa_event_data event;
9b90955e
JB
6171
6172 if (len < sizeof(*hdr))
6173 return;
6174
6175 fc = le_to_host16(hdr->frame_control);
6176
f8b1f695 6177 os_memset(&event, 0, sizeof(event));
9b90955e
JB
6178 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6179 event.rx_from_unknown.addr = hdr->addr2;
6180 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6181 (WLAN_FC_FROMDS | WLAN_FC_TODS);
f8b1f695 6182 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4b9841d3 6183}
0915d02c 6184
4b9841d3
JM
6185
6186static void handle_frame(struct wpa_driver_nl80211_data *drv,
2a8b7416 6187 u8 *buf, size_t len, int datarate, int ssi_signal)
4b9841d3
JM
6188{
6189 struct ieee80211_hdr *hdr;
f8b1f695
JM
6190 u16 fc;
6191 union wpa_event_data event;
0915d02c
JM
6192
6193 hdr = (struct ieee80211_hdr *) buf;
6194 fc = le_to_host16(hdr->frame_control);
0915d02c 6195
4b9841d3 6196 switch (WLAN_FC_GET_TYPE(fc)) {
0915d02c 6197 case WLAN_FC_TYPE_MGMT:
f8b1f695
JM
6198 os_memset(&event, 0, sizeof(event));
6199 event.rx_mgmt.frame = buf;
6200 event.rx_mgmt.frame_len = len;
2a8b7416
JM
6201 event.rx_mgmt.datarate = datarate;
6202 event.rx_mgmt.ssi_signal = ssi_signal;
f8b1f695 6203 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
0915d02c
JM
6204 break;
6205 case WLAN_FC_TYPE_CTRL:
6206 /* can only get here with PS-Poll frames */
6207 wpa_printf(MSG_DEBUG, "CTRL");
0d9fc3d8 6208 from_unknown_sta(drv, buf, len);
0915d02c
JM
6209 break;
6210 case WLAN_FC_TYPE_DATA:
0d9fc3d8 6211 from_unknown_sta(drv, buf, len);
0915d02c
JM
6212 break;
6213 }
6214}
6215
6216
6217static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6218{
6219 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6220 int len;
6221 unsigned char buf[3000];
6222 struct ieee80211_radiotap_iterator iter;
6223 int ret;
2a8b7416 6224 int datarate = 0, ssi_signal = 0;
4b9841d3 6225 int injected = 0, failed = 0, rxflags = 0;
0915d02c
JM
6226
6227 len = recv(sock, buf, sizeof(buf), 0);
6228 if (len < 0) {
6229 perror("recv");
6230 return;
6231 }
6232
6233 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6234 printf("received invalid radiotap frame\n");
6235 return;
6236 }
6237
0915d02c
JM
6238 while (1) {
6239 ret = ieee80211_radiotap_iterator_next(&iter);
6240 if (ret == -ENOENT)
6241 break;
6242 if (ret) {
6243 printf("received invalid radiotap frame (%d)\n", ret);
6244 return;
6245 }
6246 switch (iter.this_arg_index) {
6247 case IEEE80211_RADIOTAP_FLAGS:
6248 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6249 len -= 4;
6250 break;
6251 case IEEE80211_RADIOTAP_RX_FLAGS:
6252 rxflags = 1;
6253 break;
6254 case IEEE80211_RADIOTAP_TX_FLAGS:
6255 injected = 1;
6256 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6257 IEEE80211_RADIOTAP_F_TX_FAIL;
6258 break;
6259 case IEEE80211_RADIOTAP_DATA_RETRIES:
6260 break;
6261 case IEEE80211_RADIOTAP_CHANNEL:
2a8b7416 6262 /* TODO: convert from freq/flags to channel number */
0915d02c
JM
6263 break;
6264 case IEEE80211_RADIOTAP_RATE:
2a8b7416 6265 datarate = *iter.this_arg * 5;
0915d02c 6266 break;
baf513d6
JB
6267 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6268 ssi_signal = (s8) *iter.this_arg;
0915d02c
JM
6269 break;
6270 }
6271 }
6272
6273 if (rxflags && injected)
6274 return;
6275
6276 if (!injected)
4b9841d3 6277 handle_frame(drv, buf + iter.max_length,
2a8b7416 6278 len - iter.max_length, datarate, ssi_signal);
0915d02c 6279 else
4b9841d3
JM
6280 handle_tx_callback(drv->ctx, buf + iter.max_length,
6281 len - iter.max_length, !failed);
0915d02c
JM
6282}
6283
6284
6285/*
6286 * we post-process the filter code later and rewrite
6287 * this to the offset to the last instruction
6288 */
6289#define PASS 0xFF
6290#define FAIL 0xFE
6291
6292static struct sock_filter msock_filter_insns[] = {
6293 /*
6294 * do a little-endian load of the radiotap length field
6295 */
6296 /* load lower byte into A */
6297 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6298 /* put it into X (== index register) */
6299 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6300 /* load upper byte into A */
6301 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6302 /* left-shift it by 8 */
6303 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6304 /* or with X */
6305 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6306 /* put result into X */
6307 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6308
6309 /*
6310 * Allow management frames through, this also gives us those
6311 * management frames that we sent ourselves with status
6312 */
6313 /* load the lower byte of the IEEE 802.11 frame control field */
6314 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6315 /* mask off frame type and version */
6316 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6317 /* accept frame if it's both 0, fall through otherwise */
6318 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6319
6320 /*
6321 * TODO: add a bit to radiotap RX flags that indicates
6322 * that the sending station is not associated, then
6323 * add a filter here that filters on our DA and that flag
6324 * to allow us to deauth frames to that bad station.
6325 *
65ae1afd 6326 * For now allow all To DS data frames through.
0915d02c 6327 */
65ae1afd
HS
6328 /* load the IEEE 802.11 frame control field */
6329 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6330 /* mask off frame type, version and DS status */
6331 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6332 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6333 */
6334 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
0915d02c
JM
6335
6336#if 0
6337 /*
fbbfcbac 6338 * drop non-data frames
0915d02c
JM
6339 */
6340 /* load the lower byte of the frame control field */
6341 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6342 /* mask off QoS bit */
6343 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6344 /* drop non-data frames */
6345 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
fbbfcbac 6346#endif
0915d02c 6347 /* load the upper byte of the frame control field */
fbbfcbac 6348 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
0915d02c
JM
6349 /* mask off toDS/fromDS */
6350 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
fbbfcbac
FF
6351 /* accept WDS frames */
6352 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
0915d02c
JM
6353
6354 /*
6355 * add header length to index
6356 */
6357 /* load the lower byte of the frame control field */
6358 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6359 /* mask off QoS bit */
6360 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6361 /* right shift it by 6 to give 0 or 2 */
6362 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6363 /* add data frame header length */
6364 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6365 /* add index, was start of 802.11 header */
6366 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6367 /* move to index, now start of LL header */
6368 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6369
6370 /*
6371 * Accept empty data frames, we use those for
6372 * polling activity.
6373 */
6374 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6375 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6376
6377 /*
6378 * Accept EAPOL frames
6379 */
6380 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6381 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6382 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6383 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6384
6385 /* keep these last two statements or change the code below */
6386 /* return 0 == "DROP" */
6387 BPF_STMT(BPF_RET | BPF_K, 0),
6388 /* return ~0 == "keep all" */
6389 BPF_STMT(BPF_RET | BPF_K, ~0),
6390};
6391
6392static struct sock_fprog msock_filter = {
6393 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6394 .filter = msock_filter_insns,
6395};
6396
6397
6398static int add_monitor_filter(int s)
6399{
6400 int idx;
6401
6402 /* rewrite all PASS/FAIL jump offsets */
6403 for (idx = 0; idx < msock_filter.len; idx++) {
6404 struct sock_filter *insn = &msock_filter_insns[idx];
6405
6406 if (BPF_CLASS(insn->code) == BPF_JMP) {
6407 if (insn->code == (BPF_JMP|BPF_JA)) {
6408 if (insn->k == PASS)
6409 insn->k = msock_filter.len - idx - 2;
6410 else if (insn->k == FAIL)
6411 insn->k = msock_filter.len - idx - 3;
6412 }
6413
6414 if (insn->jt == PASS)
6415 insn->jt = msock_filter.len - idx - 2;
6416 else if (insn->jt == FAIL)
6417 insn->jt = msock_filter.len - idx - 3;
6418
6419 if (insn->jf == PASS)
6420 insn->jf = msock_filter.len - idx - 2;
6421 else if (insn->jf == FAIL)
6422 insn->jf = msock_filter.len - idx - 3;
6423 }
6424 }
6425
6426 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6427 &msock_filter, sizeof(msock_filter))) {
6428 perror("SO_ATTACH_FILTER");
6429 return -1;
6430 }
6431
6432 return 0;
6433}
6434
6435
460456f8
JM
6436static void nl80211_remove_monitor_interface(
6437 struct wpa_driver_nl80211_data *drv)
6438{
3fd1cefb
JB
6439 drv->monitor_refcount--;
6440 if (drv->monitor_refcount > 0)
6441 return;
6442
460456f8
JM
6443 if (drv->monitor_ifidx >= 0) {
6444 nl80211_remove_iface(drv, drv->monitor_ifidx);
6445 drv->monitor_ifidx = -1;
6446 }
504e905c
JM
6447 if (drv->monitor_sock >= 0) {
6448 eloop_unregister_read_sock(drv->monitor_sock);
6449 close(drv->monitor_sock);
6450 drv->monitor_sock = -1;
6451 }
460456f8
JM
6452}
6453
6454
0915d02c
JM
6455static int
6456nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6457{
6458 char buf[IFNAMSIZ];
6459 struct sockaddr_ll ll;
6460 int optval;
6461 socklen_t optlen;
0915d02c 6462
3fd1cefb
JB
6463 if (drv->monitor_ifidx >= 0) {
6464 drv->monitor_refcount++;
6465 return 0;
6466 }
6467
6758b167
JJ
6468 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6469 /*
6470 * P2P interface name is of the format p2p-%s-%d. For monitor
6471 * interface name corresponding to P2P GO, replace "p2p-" with
6472 * "mon-" to retain the same interface name length and to
6473 * indicate that it is a monitor interface.
6474 */
6475 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6476 } else {
6477 /* Non-P2P interface with AP functionality. */
6478 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6479 }
6480
0915d02c
JM
6481 buf[IFNAMSIZ - 1] = '\0';
6482
6483 drv->monitor_ifidx =
fbbfcbac
FF
6484 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6485 0);
0915d02c 6486
866af8b6 6487 if (drv->monitor_ifidx == -EOPNOTSUPP) {
61cbe2ff
JB
6488 /*
6489 * This is backward compatibility for a few versions of
6490 * the kernel only that didn't advertise the right
6491 * attributes for the only driver that then supported
6492 * AP mode w/o monitor -- ath6kl.
6493 */
866af8b6
JM
6494 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6495 "monitor interface type - try to run without it");
61cbe2ff 6496 drv->device_ap_sme = 1;
866af8b6
JM
6497 }
6498
0915d02c
JM
6499 if (drv->monitor_ifidx < 0)
6500 return -1;
6501
c81eff1a 6502 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
0915d02c 6503 goto error;
0915d02c
JM
6504
6505 memset(&ll, 0, sizeof(ll));
6506 ll.sll_family = AF_PACKET;
6507 ll.sll_ifindex = drv->monitor_ifidx;
6508 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6509 if (drv->monitor_sock < 0) {
6510 perror("socket[PF_PACKET,SOCK_RAW]");
6511 goto error;
6512 }
6513
6514 if (add_monitor_filter(drv->monitor_sock)) {
6515 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6516 "interface; do filtering in user space");
6517 /* This works, but will cost in performance. */
6518 }
6519
2135f224 6520 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
0915d02c
JM
6521 perror("monitor socket bind");
6522 goto error;
6523 }
6524
6525 optlen = sizeof(optval);
6526 optval = 20;
6527 if (setsockopt
6528 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6529 perror("Failed to set socket priority");
6530 goto error;
6531 }
6532
6533 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6534 drv, NULL)) {
6535 printf("Could not register monitor read socket\n");
6536 goto error;
6537 }
6538
6539 return 0;
6540 error:
460456f8 6541 nl80211_remove_monitor_interface(drv);
0915d02c
JM
6542 return -1;
6543}
6544
db149ac9 6545
3fd1cefb
JB
6546static int nl80211_setup_ap(struct i802_bss *bss)
6547{
6548 struct wpa_driver_nl80211_data *drv = bss->drv;
6549
36488c05
JM
6550 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6551 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6552
a11241fa
JB
6553 /*
6554 * Disable Probe Request reporting unless we need it in this way for
6555 * devices that include the AP SME, in the other case (unless using
6556 * monitor iface) we'll get it through the nl_mgmt socket instead.
6557 */
6558 if (!drv->device_ap_sme)
6559 wpa_driver_nl80211_probe_req_report(bss, 0);
6560
6561 if (!drv->device_ap_sme && !drv->use_monitor)
6562 if (nl80211_mgmt_subscribe_ap(bss))
6563 return -1;
6564
a6cc0602
JM
6565 if (drv->device_ap_sme && !drv->use_monitor)
6566 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6567 return -1;
6568
a11241fa 6569 if (!drv->device_ap_sme && drv->use_monitor &&
3fd1cefb
JB
6570 nl80211_create_monitor_interface(drv) &&
6571 !drv->device_ap_sme)
6572 return -1;
6573
6574 if (drv->device_ap_sme &&
6575 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6576 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6577 "Probe Request frame reporting in AP mode");
6578 /* Try to survive without this */
6579 }
6580
6581 return 0;
6582}
6583
6584
6585static void nl80211_teardown_ap(struct i802_bss *bss)
6586{
6587 struct wpa_driver_nl80211_data *drv = bss->drv;
6588
a6cc0602 6589 if (drv->device_ap_sme) {
3fd1cefb 6590 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
6591 if (!drv->use_monitor)
6592 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6593 } else if (drv->use_monitor)
3fd1cefb 6594 nl80211_remove_monitor_interface(drv);
a11241fa 6595 else
36488c05 6596 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 6597
3fd1cefb
JB
6598 bss->beacon_set = 0;
6599}
6600
6601
f10bfc9a
JM
6602static int nl80211_send_eapol_data(struct i802_bss *bss,
6603 const u8 *addr, const u8 *data,
d12dab4c 6604 size_t data_len)
f10bfc9a 6605{
d12dab4c
JB
6606 struct sockaddr_ll ll;
6607 int ret;
6608
6609 if (bss->drv->eapol_tx_sock < 0) {
6610 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
6611 return -1;
6612 }
6613
d12dab4c
JB
6614 os_memset(&ll, 0, sizeof(ll));
6615 ll.sll_family = AF_PACKET;
6616 ll.sll_ifindex = bss->ifindex;
6617 ll.sll_protocol = htons(ETH_P_PAE);
6618 ll.sll_halen = ETH_ALEN;
6619 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6620 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6621 (struct sockaddr *) &ll, sizeof(ll));
6622 if (ret < 0)
6623 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6624 strerror(errno));
6625
6626 return ret;
f10bfc9a 6627}
5fb1a232 6628
f10bfc9a 6629
db149ac9
JM
6630static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6631
6632static int wpa_driver_nl80211_hapd_send_eapol(
6633 void *priv, const u8 *addr, const u8 *data,
4378fc14 6634 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 6635{
a2e40bb6
FF
6636 struct i802_bss *bss = priv;
6637 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
6638 struct ieee80211_hdr *hdr;
6639 size_t len;
6640 u8 *pos;
6641 int res;
4378fc14 6642 int qos = flags & WPA_STA_WMM;
db149ac9 6643
a11241fa 6644 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 6645 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 6646
db149ac9
JM
6647 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6648 data_len;
6649 hdr = os_zalloc(len);
6650 if (hdr == NULL) {
6651 printf("malloc() failed for i802_send_data(len=%lu)\n",
6652 (unsigned long) len);
6653 return -1;
6654 }
6655
6656 hdr->frame_control =
6657 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6658 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6659 if (encrypt)
6660 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
6661 if (qos) {
6662 hdr->frame_control |=
6663 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6664 }
db149ac9
JM
6665
6666 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6667 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6668 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6669 pos = (u8 *) (hdr + 1);
6670
db149ac9 6671 if (qos) {
92d521d8
FF
6672 /* Set highest priority in QoS header */
6673 pos[0] = 7;
db149ac9
JM
6674 pos[1] = 0;
6675 pos += 2;
6676 }
db149ac9
JM
6677
6678 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6679 pos += sizeof(rfc1042_header);
6680 WPA_PUT_BE16(pos, ETH_P_PAE);
6681 pos += 2;
6682 memcpy(pos, data, data_len);
6683
55231068
JM
6684 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6685 0, 0, 0, 0);
db149ac9
JM
6686 if (res < 0) {
6687 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6688 "failed: %d (%s)",
6689 (unsigned long) len, errno, strerror(errno));
6690 }
7bf12757 6691 os_free(hdr);
db149ac9
JM
6692
6693 return res;
6694}
6695
a8d6ffa4 6696
3234cba4
JM
6697static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6698 int total_flags,
4c32757d 6699 int flags_or, int flags_and)
a8d6ffa4 6700{
a2e40bb6
FF
6701 struct i802_bss *bss = priv;
6702 struct wpa_driver_nl80211_data *drv = bss->drv;
a8d6ffa4 6703 struct nl_msg *msg, *flags = NULL;
7e76ee9c 6704 struct nl80211_sta_flag_update upd;
a8d6ffa4
JM
6705
6706 msg = nlmsg_alloc();
6707 if (!msg)
6708 return -ENOMEM;
6709
6710 flags = nlmsg_alloc();
6711 if (!flags) {
6712 nlmsg_free(msg);
6713 return -ENOMEM;
6714 }
6715
9fb04070 6716 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
a8d6ffa4
JM
6717
6718 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3234cba4 6719 if_nametoindex(bss->ifname));
a8d6ffa4
JM
6720 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6721
7e76ee9c
JM
6722 /*
6723 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6724 * can be removed eventually.
6725 */
0de39516 6726 if (total_flags & WPA_STA_AUTHORIZED)
a8d6ffa4
JM
6727 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
6728
0de39516 6729 if (total_flags & WPA_STA_WMM)
a8d6ffa4
JM
6730 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
6731
0de39516 6732 if (total_flags & WPA_STA_SHORT_PREAMBLE)
a8d6ffa4
JM
6733 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
6734
0de39516 6735 if (total_flags & WPA_STA_MFP)
a8d6ffa4
JM
6736 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
6737
45b722f1
AN
6738 if (total_flags & WPA_STA_TDLS_PEER)
6739 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
6740
a8d6ffa4
JM
6741 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
6742 goto nla_put_failure;
6743
7e76ee9c
JM
6744 os_memset(&upd, 0, sizeof(upd));
6745 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6746 upd.set = sta_flags_nl80211(flags_or);
6747 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6748
a8d6ffa4
JM
6749 nlmsg_free(flags);
6750
6751 return send_and_recv_msgs(drv, msg, NULL, NULL);
6752 nla_put_failure:
5883168a 6753 nlmsg_free(msg);
a8d6ffa4
JM
6754 nlmsg_free(flags);
6755 return -ENOBUFS;
6756}
6757
0915d02c 6758
1581b38b
JM
6759static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6760 struct wpa_driver_associate_params *params)
6761{
708bc8e0 6762 enum nl80211_iftype nlmode, old_mode;
89b800d7
JB
6763 struct hostapd_freq_params freq = {
6764 .freq = params->freq,
6765 };
b1f625e0
EP
6766
6767 if (params->p2p) {
046b26a2
JM
6768 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6769 "group (GO)");
b1f625e0
EP
6770 nlmode = NL80211_IFTYPE_P2P_GO;
6771 } else
6772 nlmode = NL80211_IFTYPE_AP;
6773
708bc8e0
JM
6774 old_mode = drv->nlmode;
6775 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
6776 nl80211_remove_monitor_interface(drv);
6777 return -1;
6778 }
6779
89b800d7 6780 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
708bc8e0
JM
6781 if (old_mode != nlmode)
6782 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
460456f8 6783 nl80211_remove_monitor_interface(drv);
1581b38b 6784 return -1;
0915d02c 6785 }
1581b38b 6786
1581b38b
JM
6787 return 0;
6788}
1581b38b
JM
6789
6790
5cc4d64b
JM
6791static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
6792{
6793 struct nl_msg *msg;
6794 int ret = -1;
6795
6796 msg = nlmsg_alloc();
6797 if (!msg)
6798 return -1;
6799
9fb04070 6800 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b
JM
6801 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6802 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6803 msg = NULL;
6804 if (ret) {
6805 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6806 "(%s)", ret, strerror(-ret));
6807 goto nla_put_failure;
6808 }
6809
6810 ret = 0;
6811 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
6812
6813nla_put_failure:
6814 nlmsg_free(msg);
6815 return ret;
6816}
6817
6818
6819static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6820 struct wpa_driver_associate_params *params)
6821{
6822 struct nl_msg *msg;
6823 int ret = -1;
6824 int count = 0;
6825
6826 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6827
b1f625e0
EP
6828 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
6829 NL80211_IFTYPE_ADHOC)) {
5cc4d64b
JM
6830 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6831 "IBSS mode");
6832 return -1;
6833 }
6834
6835retry:
6836 msg = nlmsg_alloc();
6837 if (!msg)
6838 return -1;
6839
9fb04070 6840 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5cc4d64b
JM
6841 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6842
6843 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6844 goto nla_put_failure;
6845
6846 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6847 params->ssid, params->ssid_len);
6848 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6849 params->ssid);
6850 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6851 drv->ssid_len = params->ssid_len;
6852
6853 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6854 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6855
6856 ret = nl80211_set_conn_keys(params, msg);
6857 if (ret)
6858 goto nla_put_failure;
6859
913e3cf7
NC
6860 if (params->bssid && params->fixed_bssid) {
6861 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6862 MAC2STR(params->bssid));
6863 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6864 }
6865
e640888c
AQ
6866 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6867 params->key_mgmt_suite == KEY_MGMT_PSK ||
6868 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
6869 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
6870 wpa_printf(MSG_DEBUG, " * control port");
6871 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6872 }
6873
a95795ad
JM
6874 if (params->wpa_ie) {
6875 wpa_hexdump(MSG_DEBUG,
6876 " * Extra IEs for Beacon/Probe Response frames",
6877 params->wpa_ie, params->wpa_ie_len);
6878 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6879 params->wpa_ie);
6880 }
6881
5cc4d64b
JM
6882 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6883 msg = NULL;
6884 if (ret) {
6885 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6886 ret, strerror(-ret));
6887 count++;
6888 if (ret == -EALREADY && count == 1) {
6889 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6890 "forced leave");
6891 nl80211_leave_ibss(drv);
6892 nlmsg_free(msg);
6893 goto retry;
6894 }
6895
6896 goto nla_put_failure;
6897 }
6898 ret = 0;
6899 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
6900
6901nla_put_failure:
6902 nlmsg_free(msg);
6903 return ret;
6904}
6905
6906
a8c5b43a 6907static int wpa_driver_nl80211_try_connect(
cfaab580
ZY
6908 struct wpa_driver_nl80211_data *drv,
6909 struct wpa_driver_associate_params *params)
6910{
6911 struct nl_msg *msg;
6912 enum nl80211_auth_type type;
6913 int ret = 0;
3f360238 6914 int algs;
cfaab580
ZY
6915
6916 msg = nlmsg_alloc();
6917 if (!msg)
6918 return -1;
6919
6920 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
9fb04070 6921 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
cfaab580
ZY
6922
6923 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6924 if (params->bssid) {
6925 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6926 MAC2STR(params->bssid));
6927 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6928 }
6929 if (params->freq) {
6930 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6931 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6932 }
1f6c0ab8
BS
6933 if (params->bg_scan_period >= 0) {
6934 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
6935 params->bg_scan_period);
6936 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
6937 params->bg_scan_period);
6938 }
cfaab580
ZY
6939 if (params->ssid) {
6940 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6941 params->ssid, params->ssid_len);
6942 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6943 params->ssid);
6944 if (params->ssid_len > sizeof(drv->ssid))
6945 goto nla_put_failure;
6946 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6947 drv->ssid_len = params->ssid_len;
6948 }
6949 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6950 if (params->wpa_ie)
6951 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6952 params->wpa_ie);
6953
3f360238
JM
6954 algs = 0;
6955 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6956 algs++;
6957 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6958 algs++;
6959 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6960 algs++;
6961 if (algs > 1) {
6962 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
6963 "selection");
6964 goto skip_auth_type;
6965 }
6966
abd9fafa 6967 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
cfaab580 6968 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 6969 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
cfaab580 6970 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 6971 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
cfaab580 6972 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 6973 else if (params->auth_alg & WPA_AUTH_ALG_FT)
cfaab580
ZY
6974 type = NL80211_AUTHTYPE_FT;
6975 else
6976 goto nla_put_failure;
6977
6978 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6979 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6980
3f360238 6981skip_auth_type:
64fa840a
JM
6982 if (params->wpa_proto) {
6983 enum nl80211_wpa_versions ver = 0;
cfaab580 6984
64fa840a
JM
6985 if (params->wpa_proto & WPA_PROTO_WPA)
6986 ver |= NL80211_WPA_VERSION_1;
6987 if (params->wpa_proto & WPA_PROTO_RSN)
6988 ver |= NL80211_WPA_VERSION_2;
cfaab580 6989
64fa840a 6990 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
cfaab580
ZY
6991 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6992 }
6993
6994 if (params->pairwise_suite != CIPHER_NONE) {
c1bb3e0a 6995 int cipher;
cfaab580
ZY
6996
6997 switch (params->pairwise_suite) {
369c8d7b
JM
6998 case CIPHER_SMS4:
6999 cipher = WLAN_CIPHER_SUITE_SMS4;
7000 break;
cfaab580
ZY
7001 case CIPHER_WEP40:
7002 cipher = WLAN_CIPHER_SUITE_WEP40;
7003 break;
7004 case CIPHER_WEP104:
7005 cipher = WLAN_CIPHER_SUITE_WEP104;
7006 break;
7007 case CIPHER_CCMP:
7008 cipher = WLAN_CIPHER_SUITE_CCMP;
7009 break;
eb7719ff
JM
7010 case CIPHER_GCMP:
7011 cipher = WLAN_CIPHER_SUITE_GCMP;
7012 break;
cfaab580
ZY
7013 case CIPHER_TKIP:
7014 default:
7015 cipher = WLAN_CIPHER_SUITE_TKIP;
7016 break;
7017 }
7018 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7019 }
7020
7021 if (params->group_suite != CIPHER_NONE) {
c1bb3e0a 7022 int cipher;
cfaab580
ZY
7023
7024 switch (params->group_suite) {
369c8d7b
JM
7025 case CIPHER_SMS4:
7026 cipher = WLAN_CIPHER_SUITE_SMS4;
7027 break;
cfaab580
ZY
7028 case CIPHER_WEP40:
7029 cipher = WLAN_CIPHER_SUITE_WEP40;
7030 break;
7031 case CIPHER_WEP104:
7032 cipher = WLAN_CIPHER_SUITE_WEP104;
7033 break;
7034 case CIPHER_CCMP:
7035 cipher = WLAN_CIPHER_SUITE_CCMP;
7036 break;
eb7719ff
JM
7037 case CIPHER_GCMP:
7038 cipher = WLAN_CIPHER_SUITE_GCMP;
7039 break;
cfaab580
ZY
7040 case CIPHER_TKIP:
7041 default:
7042 cipher = WLAN_CIPHER_SUITE_TKIP;
7043 break;
7044 }
7045 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7046 }
7047
7048 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
369c8d7b
JM
7049 params->key_mgmt_suite == KEY_MGMT_PSK ||
7050 params->key_mgmt_suite == KEY_MGMT_CCKM) {
cfaab580
ZY
7051 int mgmt = WLAN_AKM_SUITE_PSK;
7052
7053 switch (params->key_mgmt_suite) {
369c8d7b
JM
7054 case KEY_MGMT_CCKM:
7055 mgmt = WLAN_AKM_SUITE_CCKM;
7056 break;
cfaab580
ZY
7057 case KEY_MGMT_802_1X:
7058 mgmt = WLAN_AKM_SUITE_8021X;
7059 break;
7060 case KEY_MGMT_PSK:
7061 default:
7062 mgmt = WLAN_AKM_SUITE_PSK;
7063 break;
7064 }
7065 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7066 }
7067
8b706a99
JM
7068#ifdef CONFIG_IEEE80211W
7069 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7070 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7071#endif /* CONFIG_IEEE80211W */
7072
80e8a5ee
BG
7073 if (params->disable_ht)
7074 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7075
7076 if (params->htcaps && params->htcaps_mask) {
7077 int sz = sizeof(struct ieee80211_ht_capabilities);
7078 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7079 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7080 params->htcaps_mask);
7081 }
7082
cfaab580
ZY
7083 ret = nl80211_set_conn_keys(params, msg);
7084 if (ret)
7085 goto nla_put_failure;
7086
7087 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7088 msg = NULL;
7089 if (ret) {
7090 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7091 "(%s)", ret, strerror(-ret));
7092 goto nla_put_failure;
7093 }
7094 ret = 0;
7095 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7096
7097nla_put_failure:
7098 nlmsg_free(msg);
7099 return ret;
7100
7101}
7102
7103
a8c5b43a
CW
7104static int wpa_driver_nl80211_connect(
7105 struct wpa_driver_nl80211_data *drv,
7106 struct wpa_driver_associate_params *params)
7107{
7108 int ret = wpa_driver_nl80211_try_connect(drv, params);
7109 if (ret == -EALREADY) {
7110 /*
7111 * cfg80211 does not currently accept new connections if
7112 * we are already connected. As a workaround, force
7113 * disconnection and try again.
7114 */
7115 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7116 "disconnecting before reassociation "
7117 "attempt");
7118 if (wpa_driver_nl80211_disconnect(
7119 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7120 return -1;
7121 /* Ignore the next local disconnect message. */
7122 drv->ignore_next_local_disconnect = 1;
7123 ret = wpa_driver_nl80211_try_connect(drv, params);
7124 }
7125 return ret;
7126}
7127
7128
c2a04078
JM
7129static int wpa_driver_nl80211_associate(
7130 void *priv, struct wpa_driver_associate_params *params)
7131{
a2e40bb6
FF
7132 struct i802_bss *bss = priv;
7133 struct wpa_driver_nl80211_data *drv = bss->drv;
c2a04078
JM
7134 int ret = -1;
7135 struct nl_msg *msg;
7136
5cc4d64b 7137 if (params->mode == IEEE80211_MODE_AP)
1581b38b 7138 return wpa_driver_nl80211_ap(drv, params);
1581b38b 7139
5cc4d64b
JM
7140 if (params->mode == IEEE80211_MODE_IBSS)
7141 return wpa_driver_nl80211_ibss(drv, params);
7142
4a867032 7143 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
7144 enum nl80211_iftype nlmode = params->p2p ?
7145 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7146
7147 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 7148 return -1;
cfaab580 7149 return wpa_driver_nl80211_connect(drv, params);
4a867032 7150 }
cfaab580 7151
c2a04078
JM
7152 drv->associated = 0;
7153
7154 msg = nlmsg_alloc();
7155 if (!msg)
7156 return -1;
7157
7158 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7159 drv->ifindex);
9fb04070 7160 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
c2a04078
JM
7161
7162 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7163 if (params->bssid) {
7164 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7165 MAC2STR(params->bssid));
7166 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7167 }
7168 if (params->freq) {
7169 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7170 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4832ecd7
JM
7171 drv->assoc_freq = params->freq;
7172 } else
7173 drv->assoc_freq = 0;
1f6c0ab8
BS
7174 if (params->bg_scan_period >= 0) {
7175 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7176 params->bg_scan_period);
7177 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7178 params->bg_scan_period);
7179 }
c2a04078
JM
7180 if (params->ssid) {
7181 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7182 params->ssid, params->ssid_len);
7183 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7184 params->ssid);
fd05d64e
JM
7185 if (params->ssid_len > sizeof(drv->ssid))
7186 goto nla_put_failure;
7187 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7188 drv->ssid_len = params->ssid_len;
c2a04078
JM
7189 }
7190 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7191 if (params->wpa_ie)
7192 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7193 params->wpa_ie);
7194
aca01605
JM
7195 if (params->pairwise_suite != CIPHER_NONE) {
7196 int cipher;
7197
7198 switch (params->pairwise_suite) {
7199 case CIPHER_WEP40:
7200 cipher = WLAN_CIPHER_SUITE_WEP40;
7201 break;
7202 case CIPHER_WEP104:
7203 cipher = WLAN_CIPHER_SUITE_WEP104;
7204 break;
7205 case CIPHER_CCMP:
7206 cipher = WLAN_CIPHER_SUITE_CCMP;
7207 break;
eb7719ff
JM
7208 case CIPHER_GCMP:
7209 cipher = WLAN_CIPHER_SUITE_GCMP;
7210 break;
aca01605
JM
7211 case CIPHER_TKIP:
7212 default:
7213 cipher = WLAN_CIPHER_SUITE_TKIP;
7214 break;
7215 }
79c3124c 7216 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
aca01605
JM
7217 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7218 }
7219
7220 if (params->group_suite != CIPHER_NONE) {
7221 int cipher;
7222
7223 switch (params->group_suite) {
7224 case CIPHER_WEP40:
7225 cipher = WLAN_CIPHER_SUITE_WEP40;
7226 break;
7227 case CIPHER_WEP104:
7228 cipher = WLAN_CIPHER_SUITE_WEP104;
7229 break;
7230 case CIPHER_CCMP:
7231 cipher = WLAN_CIPHER_SUITE_CCMP;
7232 break;
eb7719ff
JM
7233 case CIPHER_GCMP:
7234 cipher = WLAN_CIPHER_SUITE_GCMP;
7235 break;
aca01605
JM
7236 case CIPHER_TKIP:
7237 default:
7238 cipher = WLAN_CIPHER_SUITE_TKIP;
7239 break;
7240 }
79c3124c 7241 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
aca01605
JM
7242 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7243 }
7244
e572fa33
JM
7245#ifdef CONFIG_IEEE80211W
7246 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7247 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7248#endif /* CONFIG_IEEE80211W */
7249
01652550
JM
7250 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7251
62fa124c
JM
7252 if (params->prev_bssid) {
7253 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7254 MAC2STR(params->prev_bssid));
7255 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7256 params->prev_bssid);
7257 }
7258
80e8a5ee
BG
7259 if (params->disable_ht)
7260 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7261
7262 if (params->htcaps && params->htcaps_mask) {
7263 int sz = sizeof(struct ieee80211_ht_capabilities);
7264 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7265 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7266 params->htcaps_mask);
7267 }
7268
046b26a2
JM
7269 if (params->p2p)
7270 wpa_printf(MSG_DEBUG, " * P2P group");
7271
c2a04078
JM
7272 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7273 msg = NULL;
7274 if (ret) {
3b7ea880
BG
7275 wpa_dbg(drv->ctx, MSG_DEBUG,
7276 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7277 ret, strerror(-ret));
8856462d 7278 nl80211_dump_scan(drv);
c2a04078
JM
7279 goto nla_put_failure;
7280 }
7281 ret = 0;
7282 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7283 "successfully");
7284
7285nla_put_failure:
7286 nlmsg_free(msg);
7287 return ret;
7288}
3f5285e8
JM
7289
7290
ad1e68e6 7291static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 7292 int ifindex, enum nl80211_iftype mode)
3f5285e8 7293{
3f5285e8 7294 struct nl_msg *msg;
ad1e68e6
JM
7295 int ret = -ENOBUFS;
7296
a1922f93
JM
7297 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7298 ifindex, mode, nl80211_iftype_str(mode));
7299
ad1e68e6
JM
7300 msg = nlmsg_alloc();
7301 if (!msg)
7302 return -ENOMEM;
7303
9fb04070 7304 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
ad1e68e6
JM
7305 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7306 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7307
7308 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7309 msg = NULL;
ad1e68e6
JM
7310 if (!ret)
7311 return 0;
7312nla_put_failure:
5883168a 7313 nlmsg_free(msg);
ad1e68e6
JM
7314 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7315 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7316 return ret;
7317}
7318
7319
b1f625e0
EP
7320static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7321 enum nl80211_iftype nlmode)
ad1e68e6 7322{
a2e40bb6 7323 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 7324 int ret = -1;
26af9dca 7325 int i;
86957e62 7326 int was_ap = is_ap_interface(drv->nlmode);
671a5039 7327 int res;
1581b38b 7328
671a5039
JM
7329 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7330 if (res == 0) {
ad1e68e6 7331 drv->nlmode = nlmode;
460456f8
JM
7332 ret = 0;
7333 goto done;
ad1e68e6 7334 }
3f5285e8 7335
671a5039
JM
7336 if (res == -ENODEV)
7337 return -1;
7338
460456f8 7339 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
7340 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7341 "requested mode - ignore error");
460456f8
JM
7342 ret = 0;
7343 goto done; /* Already in the requested mode */
7344 }
3f5285e8 7345
3f5285e8
JM
7346 /* mac80211 doesn't allow mode changes while the device is up, so
7347 * take the device down, try to set the mode again, and bring the
7348 * device back up.
7349 */
26af9dca
JM
7350 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7351 "interface down");
7352 for (i = 0; i < 10; i++) {
c81eff1a
BG
7353 res = linux_set_iface_flags(drv->global->ioctl_sock,
7354 bss->ifname, 0);
6e8183d7
JM
7355 if (res == -EACCES || res == -ENODEV)
7356 break;
7357 if (res == 0) {
26af9dca
JM
7358 /* Try to set the mode again while the interface is
7359 * down */
7360 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
6e8183d7
JM
7361 if (ret == -EACCES)
7362 break;
c81eff1a 7363 res = linux_set_iface_flags(drv->global->ioctl_sock,
6e8183d7
JM
7364 bss->ifname, 1);
7365 if (res && !ret)
26af9dca 7366 ret = -1;
6e8183d7 7367 else if (ret != -EBUSY)
26af9dca
JM
7368 break;
7369 } else
7370 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7371 "interface down");
7372 os_sleep(0, 100000);
3f5285e8
JM
7373 }
7374
c6e8e8e4
JM
7375 if (!ret) {
7376 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7377 "interface is down");
ad1e68e6 7378 drv->nlmode = nlmode;
7d9c3698 7379 drv->ignore_if_down_event = 1;
c6e8e8e4 7380 }
ad1e68e6 7381
460456f8 7382done:
3fd1cefb
JB
7383 if (ret) {
7384 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7385 "from %d failed", nlmode, drv->nlmode);
7386 return ret;
7387 }
7388
edb9bfba
RM
7389 if (is_p2p_interface(nlmode))
7390 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1d0c6fb1
JM
7391 else if (drv->disabled_11b_rates)
7392 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
edb9bfba 7393
3fd1cefb 7394 if (is_ap_interface(nlmode)) {
36488c05 7395 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 7396 /* Setup additional AP mode functionality if needed */
3fd1cefb 7397 if (nl80211_setup_ap(bss))
460456f8 7398 return -1;
3fd1cefb 7399 } else if (was_ap) {
460456f8 7400 /* Remove additional AP mode functionality */
3fd1cefb 7401 nl80211_teardown_ap(bss);
a11241fa 7402 } else {
36488c05 7403 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 7404 }
460456f8 7405
873d0fcf 7406 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
a11241fa
JB
7407 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7408 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7409 "frame processing - ignore for now");
08359050 7410
3fd1cefb 7411 return 0;
3f5285e8
JM
7412}
7413
7414
3f5285e8
JM
7415static int wpa_driver_nl80211_get_capa(void *priv,
7416 struct wpa_driver_capa *capa)
7417{
a2e40bb6
FF
7418 struct i802_bss *bss = priv;
7419 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
7420 if (!drv->has_capability)
7421 return -1;
7422 os_memcpy(capa, &drv->capa, sizeof(*capa));
7423 return 0;
7424}
7425
7426
7427static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7428{
a2e40bb6
FF
7429 struct i802_bss *bss = priv;
7430 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8
JM
7431
7432 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7433 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7434 drv->operstate = state;
36d84860 7435 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 7436 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
7437}
7438
01652550
JM
7439
7440static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7441{
a2e40bb6
FF
7442 struct i802_bss *bss = priv;
7443 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
7444 struct nl_msg *msg;
7445 struct nl80211_sta_flag_update upd;
7446
7447 msg = nlmsg_alloc();
7448 if (!msg)
7449 return -ENOMEM;
7450
9fb04070 7451 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
01652550
JM
7452
7453 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7454 if_nametoindex(bss->ifname));
01652550
JM
7455 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7456
7457 os_memset(&upd, 0, sizeof(upd));
7458 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7459 if (authorized)
7460 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7461 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7462
7463 return send_and_recv_msgs(drv, msg, NULL, NULL);
7464 nla_put_failure:
5883168a 7465 nlmsg_free(msg);
01652550
JM
7466 return -ENOBUFS;
7467}
7468
3f5285e8 7469
f07ead6a
JM
7470/* Set kernel driver on given frequency (MHz) */
7471static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 7472{
f07ead6a 7473 struct i802_bss *bss = priv;
89b800d7 7474 return wpa_driver_nl80211_set_freq(bss, freq);
c5121837
JM
7475}
7476
f7b3920c
JM
7477
7478#if defined(HOSTAPD) || defined(CONFIG_AP)
c5121837 7479
c5121837
JM
7480static inline int min_int(int a, int b)
7481{
7482 if (a < b)
7483 return a;
7484 return b;
7485}
7486
7487
7488static int get_key_handler(struct nl_msg *msg, void *arg)
7489{
7490 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7491 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7492
7493 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7494 genlmsg_attrlen(gnlh, 0), NULL);
7495
7496 /*
7497 * TODO: validate the key index and mac address!
7498 * Otherwise, there's a race condition as soon as
7499 * the kernel starts sending key notifications.
7500 */
7501
7502 if (tb[NL80211_ATTR_KEY_SEQ])
7503 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7504 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7505 return NL_SKIP;
7506}
7507
7508
7509static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7510 int idx, u8 *seq)
7511{
a2e40bb6
FF
7512 struct i802_bss *bss = priv;
7513 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7514 struct nl_msg *msg;
7515
7516 msg = nlmsg_alloc();
7517 if (!msg)
7518 return -ENOMEM;
7519
9fb04070 7520 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
c5121837
JM
7521
7522 if (addr)
7523 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7524 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7525 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7526
7527 memset(seq, 0, 6);
7528
7529 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7530 nla_put_failure:
9e088e74 7531 nlmsg_free(msg);
c5121837
JM
7532 return -ENOBUFS;
7533}
7534
7535
c5121837
JM
7536static int i802_set_rts(void *priv, int rts)
7537{
a2e40bb6
FF
7538 struct i802_bss *bss = priv;
7539 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
7540 struct nl_msg *msg;
7541 int ret = -ENOBUFS;
7542 u32 val;
c5121837 7543
ad649451
JM
7544 msg = nlmsg_alloc();
7545 if (!msg)
7546 return -ENOMEM;
c5121837 7547
ad649451
JM
7548 if (rts >= 2347)
7549 val = (u32) -1;
7550 else
7551 val = rts;
c5121837 7552
9fb04070 7553 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
7554 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7555 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7556
7557 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7558 msg = NULL;
ad649451
JM
7559 if (!ret)
7560 return 0;
7561nla_put_failure:
5883168a 7562 nlmsg_free(msg);
ad649451
JM
7563 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7564 "%d (%s)", rts, ret, strerror(-ret));
7565 return ret;
c5121837
JM
7566}
7567
7568
7569static int i802_set_frag(void *priv, int frag)
7570{
a2e40bb6
FF
7571 struct i802_bss *bss = priv;
7572 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451
JM
7573 struct nl_msg *msg;
7574 int ret = -ENOBUFS;
7575 u32 val;
c5121837 7576
ad649451
JM
7577 msg = nlmsg_alloc();
7578 if (!msg)
7579 return -ENOMEM;
c5121837 7580
ad649451
JM
7581 if (frag >= 2346)
7582 val = (u32) -1;
7583 else
7584 val = frag;
c5121837 7585
9fb04070 7586 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
ad649451
JM
7587 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7588 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7589
7590 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 7591 msg = NULL;
ad649451
JM
7592 if (!ret)
7593 return 0;
7594nla_put_failure:
5883168a 7595 nlmsg_free(msg);
ad649451
JM
7596 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7597 "%d: %d (%s)", frag, ret, strerror(-ret));
7598 return ret;
c5121837
JM
7599}
7600
7601
c5121837
JM
7602static int i802_flush(void *priv)
7603{
a2e40bb6
FF
7604 struct i802_bss *bss = priv;
7605 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 7606 struct nl_msg *msg;
82554b10 7607 int res;
c5121837
JM
7608
7609 msg = nlmsg_alloc();
7610 if (!msg)
7611 return -1;
7612
9fb04070 7613 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
c5121837
JM
7614
7615 /*
7616 * XXX: FIX! this needs to flush all VLANs too
7617 */
7618 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7619 if_nametoindex(bss->ifname));
c5121837 7620
82554b10
JM
7621 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7622 if (res) {
7623 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7624 "(%s)", res, strerror(-res));
7625 }
7626 return res;
c5121837 7627 nla_put_failure:
9e088e74 7628 nlmsg_free(msg);
c5121837
JM
7629 return -ENOBUFS;
7630}
7631
d732463c
JM
7632#endif /* HOSTAPD || CONFIG_AP */
7633
c5121837
JM
7634
7635static int get_sta_handler(struct nl_msg *msg, void *arg)
7636{
7637 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7638 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7639 struct hostap_sta_driver_data *data = arg;
7640 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7641 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7642 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7643 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7644 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7645 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7646 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 7647 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
c5121837
JM
7648 };
7649
7650 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7651 genlmsg_attrlen(gnlh, 0), NULL);
7652
7653 /*
7654 * TODO: validate the interface and mac address!
7655 * Otherwise, there's a race condition as soon as
7656 * the kernel starts sending station notifications.
7657 */
7658
7659 if (!tb[NL80211_ATTR_STA_INFO]) {
7660 wpa_printf(MSG_DEBUG, "sta stats missing!");
7661 return NL_SKIP;
7662 }
7663 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7664 tb[NL80211_ATTR_STA_INFO],
7665 stats_policy)) {
7666 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7667 return NL_SKIP;
7668 }
7669
7670 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7671 data->inactive_msec =
7672 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7673 if (stats[NL80211_STA_INFO_RX_BYTES])
7674 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7675 if (stats[NL80211_STA_INFO_TX_BYTES])
7676 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7677 if (stats[NL80211_STA_INFO_RX_PACKETS])
7678 data->rx_packets =
7679 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7680 if (stats[NL80211_STA_INFO_TX_PACKETS])
7681 data->tx_packets =
7682 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
7683 if (stats[NL80211_STA_INFO_TX_FAILED])
7684 data->tx_retry_failed =
7685 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
c5121837
JM
7686
7687 return NL_SKIP;
7688}
7689
9ebce9c5
JM
7690static int i802_read_sta_data(struct i802_bss *bss,
7691 struct hostap_sta_driver_data *data,
c5121837
JM
7692 const u8 *addr)
7693{
a2e40bb6 7694 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7695 struct nl_msg *msg;
7696
7697 os_memset(data, 0, sizeof(*data));
7698 msg = nlmsg_alloc();
7699 if (!msg)
7700 return -ENOMEM;
7701
9fb04070 7702 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
c5121837
JM
7703
7704 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
a2e40bb6 7705 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
7706
7707 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7708 nla_put_failure:
9e088e74 7709 nlmsg_free(msg);
c5121837
JM
7710 return -ENOBUFS;
7711}
7712
7713
d732463c
JM
7714#if defined(HOSTAPD) || defined(CONFIG_AP)
7715
c5121837
JM
7716static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7717 int cw_min, int cw_max, int burst_time)
7718{
a2e40bb6
FF
7719 struct i802_bss *bss = priv;
7720 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7721 struct nl_msg *msg;
7722 struct nlattr *txq, *params;
7723
7724 msg = nlmsg_alloc();
7725 if (!msg)
7726 return -1;
7727
9fb04070 7728 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
c5121837 7729
a2e40bb6 7730 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
c5121837
JM
7731
7732 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7733 if (!txq)
7734 goto nla_put_failure;
7735
7736 /* We are only sending parameters for a single TXQ at a time */
7737 params = nla_nest_start(msg, 1);
7738 if (!params)
7739 goto nla_put_failure;
7740
7e3c1781
JM
7741 switch (queue) {
7742 case 0:
7743 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7744 break;
7745 case 1:
7746 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7747 break;
7748 case 2:
7749 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7750 break;
7751 case 3:
7752 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7753 break;
7754 }
c5121837
JM
7755 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
7756 * 32 usec, so need to convert the value here. */
7757 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
7758 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
7759 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
7760 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
7761
7762 nla_nest_end(msg, params);
7763
7764 nla_nest_end(msg, txq);
7765
7766 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7767 return 0;
9e088e74 7768 msg = NULL;
c5121837 7769 nla_put_failure:
9e088e74 7770 nlmsg_free(msg);
c5121837
JM
7771 return -1;
7772}
7773
7774
9ebce9c5 7775static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
7776 const char *ifname, int vlan_id)
7777{
a2e40bb6 7778 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 7779 struct nl_msg *msg;
cd1d72c1 7780 int ret = -ENOBUFS;
c5121837
JM
7781
7782 msg = nlmsg_alloc();
7783 if (!msg)
7784 return -ENOMEM;
7785
9fb04070 7786 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
c5121837
JM
7787
7788 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
a2e40bb6 7789 if_nametoindex(bss->ifname));
c5121837 7790 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1c766b09 7791 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
c5121837
JM
7792 if_nametoindex(ifname));
7793
cd1d72c1 7794 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9e088e74 7795 msg = NULL;
cd1d72c1
JM
7796 if (ret < 0) {
7797 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
7798 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
7799 MAC2STR(addr), ifname, vlan_id, ret,
7800 strerror(-ret));
7801 }
c5121837 7802 nla_put_failure:
9e088e74 7803 nlmsg_free(msg);
cd1d72c1 7804 return ret;
c5121837
JM
7805}
7806
fbbfcbac 7807
c5121837
JM
7808static int i802_get_inact_sec(void *priv, const u8 *addr)
7809{
7810 struct hostap_sta_driver_data data;
7811 int ret;
7812
7813 data.inactive_msec = (unsigned long) -1;
7814 ret = i802_read_sta_data(priv, &data, addr);
7815 if (ret || data.inactive_msec == (unsigned long) -1)
7816 return -1;
7817 return data.inactive_msec / 1000;
7818}
7819
7820
7821static int i802_sta_clear_stats(void *priv, const u8 *addr)
7822{
7823#if 0
7824 /* TODO */
7825#endif
7826 return 0;
7827}
7828
7829
731723a5
JM
7830static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
7831 int reason)
c5121837 7832{
a2e40bb6 7833 struct i802_bss *bss = priv;
e1bd4e19 7834 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7835 struct ieee80211_mgmt mgmt;
7836
e1bd4e19
AT
7837 if (drv->device_ap_sme)
7838 return wpa_driver_nl80211_sta_remove(bss, addr);
7839
c5121837
JM
7840 memset(&mgmt, 0, sizeof(mgmt));
7841 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7842 WLAN_FC_STYPE_DEAUTH);
7843 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
7844 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7845 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 7846 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 7847 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 7848 IEEE80211_HDRLEN +
9ebce9c5
JM
7849 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
7850 0);
c5121837
JM
7851}
7852
7853
731723a5
JM
7854static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
7855 int reason)
c5121837 7856{
a2e40bb6 7857 struct i802_bss *bss = priv;
e1bd4e19 7858 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
7859 struct ieee80211_mgmt mgmt;
7860
e1bd4e19
AT
7861 if (drv->device_ap_sme)
7862 return wpa_driver_nl80211_sta_remove(bss, addr);
7863
c5121837
JM
7864 memset(&mgmt, 0, sizeof(mgmt));
7865 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7866 WLAN_FC_STYPE_DISASSOC);
7867 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
7868 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7869 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 7870 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 7871 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 7872 IEEE80211_HDRLEN +
9ebce9c5
JM
7873 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
7874 0);
c5121837
JM
7875}
7876
ee7ab173
JB
7877#endif /* HOSTAPD || CONFIG_AP */
7878
7879#ifdef HOSTAPD
c5121837 7880
f07ead6a
JM
7881static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7882{
7883 int i;
7884 int *old;
7885
7886 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
7887 ifidx);
7888 for (i = 0; i < drv->num_if_indices; i++) {
7889 if (drv->if_indices[i] == 0) {
7890 drv->if_indices[i] = ifidx;
7891 return;
7892 }
7893 }
7894
7895 if (drv->if_indices != drv->default_if_indices)
7896 old = drv->if_indices;
7897 else
7898 old = NULL;
7899
067ffa26
JM
7900 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
7901 sizeof(int));
f07ead6a
JM
7902 if (!drv->if_indices) {
7903 if (!old)
7904 drv->if_indices = drv->default_if_indices;
7905 else
7906 drv->if_indices = old;
7907 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7908 "interfaces");
7909 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7910 return;
7911 } else if (!old)
7912 os_memcpy(drv->if_indices, drv->default_if_indices,
7913 sizeof(drv->default_if_indices));
7914 drv->if_indices[drv->num_if_indices] = ifidx;
7915 drv->num_if_indices++;
7916}
7917
7918
7919static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7920{
7921 int i;
7922
7923 for (i = 0; i < drv->num_if_indices; i++) {
7924 if (drv->if_indices[i] == ifidx) {
7925 drv->if_indices[i] = 0;
7926 break;
7927 }
7928 }
7929}
7930
7931
7932static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7933{
7934 int i;
7935
7936 for (i = 0; i < drv->num_if_indices; i++)
7937 if (drv->if_indices[i] == ifidx)
7938 return 1;
7939
7940 return 0;
7941}
7942
7943
7944static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
7945 const char *bridge_ifname)
7946{
7947 struct i802_bss *bss = priv;
7948 struct wpa_driver_nl80211_data *drv = bss->drv;
7949 char name[IFNAMSIZ + 1];
7950
7951 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7952 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7953 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7954 if (val) {
7955 if (!if_nametoindex(name)) {
7956 if (nl80211_create_iface(drv, name,
7957 NL80211_IFTYPE_AP_VLAN,
c3e3a5b9 7958 bss->addr, 1) < 0)
f07ead6a
JM
7959 return -1;
7960 if (bridge_ifname &&
c81eff1a
BG
7961 linux_br_add_if(drv->global->ioctl_sock,
7962 bridge_ifname, name) < 0)
f07ead6a
JM
7963 return -1;
7964 }
75227f3a
JM
7965 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
7966 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
7967 "interface %s up", name);
7968 }
f07ead6a
JM
7969 return i802_set_sta_vlan(priv, addr, name, 0);
7970 } else {
c34e618d
FF
7971 if (bridge_ifname)
7972 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
7973 name);
7974
f07ead6a
JM
7975 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
7976 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
7977 name);
7978 }
7979}
7980
7981
7982static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7983{
7984 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7985 struct sockaddr_ll lladdr;
7986 unsigned char buf[3000];
7987 int len;
7988 socklen_t fromlen = sizeof(lladdr);
7989
7990 len = recvfrom(sock, buf, sizeof(buf), 0,
7991 (struct sockaddr *)&lladdr, &fromlen);
7992 if (len < 0) {
7993 perror("recv");
7994 return;
7995 }
7996
7997 if (have_ifidx(drv, lladdr.sll_ifindex))
7998 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7999}
8000
8001
94627f6c 8002static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 8003 struct i802_bss *bss,
94627f6c
JM
8004 const char *brname, const char *ifname)
8005{
8006 int ifindex;
8007 char in_br[IFNAMSIZ];
8008
e17a2477 8009 os_strlcpy(bss->brname, brname, IFNAMSIZ);
94627f6c
JM
8010 ifindex = if_nametoindex(brname);
8011 if (ifindex == 0) {
8012 /*
8013 * Bridge was configured, but the bridge device does
8014 * not exist. Try to add it now.
8015 */
c81eff1a 8016 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
8017 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8018 "bridge interface %s: %s",
8019 brname, strerror(errno));
8020 return -1;
8021 }
e17a2477 8022 bss->added_bridge = 1;
94627f6c
JM
8023 add_ifidx(drv, if_nametoindex(brname));
8024 }
8025
8026 if (linux_br_get(in_br, ifname) == 0) {
8027 if (os_strcmp(in_br, brname) == 0)
8028 return 0; /* already in the bridge */
8029
8030 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8031 "bridge %s", ifname, in_br);
c81eff1a
BG
8032 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8033 0) {
94627f6c
JM
8034 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8035 "remove interface %s from bridge "
8036 "%s: %s",
8037 ifname, brname, strerror(errno));
8038 return -1;
8039 }
8040 }
8041
8042 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8043 ifname, brname);
c81eff1a 8044 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
8045 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8046 "into bridge %s: %s",
8047 ifname, brname, strerror(errno));
8048 return -1;
8049 }
e17a2477 8050 bss->added_if_into_bridge = 1;
94627f6c
JM
8051
8052 return 0;
8053}
8054
8055
92f475b4
JM
8056static void *i802_init(struct hostapd_data *hapd,
8057 struct wpa_init_params *params)
c5121837
JM
8058{
8059 struct wpa_driver_nl80211_data *drv;
a2e40bb6 8060 struct i802_bss *bss;
c5121837 8061 size_t i;
94627f6c
JM
8062 char brname[IFNAMSIZ];
8063 int ifindex, br_ifindex;
8064 int br_added = 0;
c5121837 8065
4b24282a
JM
8066 bss = wpa_driver_nl80211_init(hapd, params->ifname,
8067 params->global_priv);
a2e40bb6 8068 if (bss == NULL)
c5121837 8069 return NULL;
c5121837 8070
a2e40bb6 8071 drv = bss->drv;
0382097e 8072 drv->nlmode = NL80211_IFTYPE_AP;
7635bfb0
JB
8073 drv->eapol_sock = -1;
8074
94627f6c
JM
8075 if (linux_br_get(brname, params->ifname) == 0) {
8076 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8077 params->ifname, brname);
8078 br_ifindex = if_nametoindex(brname);
8079 } else {
8080 brname[0] = '\0';
8081 br_ifindex = 0;
8082 }
8083
c5121837
JM
8084 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8085 drv->if_indices = drv->default_if_indices;
92f475b4 8086 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
8087 if (params->bridge[i]) {
8088 ifindex = if_nametoindex(params->bridge[i]);
8089 if (ifindex)
8090 add_ifidx(drv, ifindex);
8091 if (ifindex == br_ifindex)
8092 br_added = 1;
8093 }
c5121837 8094 }
94627f6c
JM
8095 if (!br_added && br_ifindex &&
8096 (params->num_bridge == 0 || !params->bridge[0]))
8097 add_ifidx(drv, br_ifindex);
c5121837 8098
ad1e68e6
JM
8099 /* start listening for EAPOL on the default AP interface */
8100 add_ifidx(drv, drv->ifindex);
8101
c81eff1a 8102 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
6980c191 8103 goto failed;
ad1e68e6 8104
6980c191 8105 if (params->bssid) {
c81eff1a 8106 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2ac9688e 8107 params->bssid))
460456f8
JM
8108 goto failed;
8109 }
ad1e68e6 8110
b1f625e0 8111 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
ad1e68e6 8112 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
a2e40bb6 8113 "into AP mode", bss->ifname);
bbaf0837 8114 goto failed;
ad1e68e6
JM
8115 }
8116
94627f6c 8117 if (params->num_bridge && params->bridge[0] &&
e17a2477 8118 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
94627f6c
JM
8119 goto failed;
8120
c81eff1a 8121 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
bbaf0837 8122 goto failed;
ad1e68e6
JM
8123
8124 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8125 if (drv->eapol_sock < 0) {
8126 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
bbaf0837 8127 goto failed;
ad1e68e6
JM
8128 }
8129
8130 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8131 {
8132 printf("Could not register read socket for eapol\n");
c5121837 8133 goto failed;
ad1e68e6
JM
8134 }
8135
c81eff1a
BG
8136 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8137 params->own_addr))
bbaf0837 8138 goto failed;
c5121837 8139
341eebee
JB
8140 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8141
a2e40bb6 8142 return bss;
c5121837
JM
8143
8144failed:
7635bfb0 8145 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
8146 return NULL;
8147}
c5121837 8148
c5121837 8149
bbaf0837
JM
8150static void i802_deinit(void *priv)
8151{
9ebce9c5
JM
8152 struct i802_bss *bss = priv;
8153 wpa_driver_nl80211_deinit(bss);
c5121837
JM
8154}
8155
8156#endif /* HOSTAPD */
8157
8158
22a7c9d7
JM
8159static enum nl80211_iftype wpa_driver_nl80211_if_type(
8160 enum wpa_driver_if_type type)
8161{
8162 switch (type) {
8163 case WPA_IF_STATION:
9f51b113 8164 return NL80211_IFTYPE_STATION;
75bde05d
JM
8165 case WPA_IF_P2P_CLIENT:
8166 case WPA_IF_P2P_GROUP:
9f51b113 8167 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
8168 case WPA_IF_AP_VLAN:
8169 return NL80211_IFTYPE_AP_VLAN;
8170 case WPA_IF_AP_BSS:
8171 return NL80211_IFTYPE_AP;
9f51b113
JB
8172 case WPA_IF_P2P_GO:
8173 return NL80211_IFTYPE_P2P_GO;
22a7c9d7
JM
8174 }
8175 return -1;
8176}
8177
8178
482856c8
JM
8179#ifdef CONFIG_P2P
8180
f2ed8023
JM
8181static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8182{
8183 struct wpa_driver_nl80211_data *drv;
8184 dl_list_for_each(drv, &global->interfaces,
8185 struct wpa_driver_nl80211_data, list) {
341eebee 8186 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
f2ed8023
JM
8187 return 1;
8188 }
8189 return 0;
8190}
8191
8192
8193static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8194 u8 *new_addr)
8195{
8196 unsigned int idx;
8197
8198 if (!drv->global)
8199 return -1;
8200
341eebee 8201 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
f2ed8023 8202 for (idx = 0; idx < 64; idx++) {
341eebee 8203 new_addr[0] = drv->first_bss.addr[0] | 0x02;
f2ed8023
JM
8204 new_addr[0] ^= idx << 2;
8205 if (!nl80211_addr_in_use(drv->global, new_addr))
8206 break;
8207 }
8208 if (idx == 64)
8209 return -1;
8210
8211 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8212 MACSTR, MAC2STR(new_addr));
8213
8214 return 0;
8215}
8216
482856c8
JM
8217#endif /* CONFIG_P2P */
8218
f2ed8023 8219
7ab68865 8220static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 8221 const char *ifname, const u8 *addr,
f3585c8a 8222 void *bss_ctx, void **drv_priv,
e17a2477
JM
8223 char *force_ifname, u8 *if_addr,
8224 const char *bridge)
22a7c9d7 8225{
a2e40bb6
FF
8226 struct i802_bss *bss = priv;
8227 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
8228 int ifidx;
8229#ifdef HOSTAPD
a2e40bb6 8230 struct i802_bss *new_bss = NULL;
22a7c9d7
JM
8231
8232 if (type == WPA_IF_AP_BSS) {
a2e40bb6
FF
8233 new_bss = os_zalloc(sizeof(*new_bss));
8234 if (new_bss == NULL)
22a7c9d7
JM
8235 return -1;
8236 }
8237#endif /* HOSTAPD */
8238
f3585c8a
JM
8239 if (addr)
8240 os_memcpy(if_addr, addr, ETH_ALEN);
22a7c9d7 8241 ifidx = nl80211_create_iface(drv, ifname,
fbbfcbac
FF
8242 wpa_driver_nl80211_if_type(type), addr,
8243 0);
22a7c9d7
JM
8244 if (ifidx < 0) {
8245#ifdef HOSTAPD
a2e40bb6 8246 os_free(new_bss);
22a7c9d7
JM
8247#endif /* HOSTAPD */
8248 return -1;
8249 }
8250
f3585c8a 8251 if (!addr &&
c81eff1a
BG
8252 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8253 if_addr) < 0) {
c55f774d 8254 nl80211_remove_iface(drv, ifidx);
f3585c8a 8255 return -1;
c55f774d
JM
8256 }
8257
8258#ifdef CONFIG_P2P
8259 if (!addr &&
8260 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8261 type == WPA_IF_P2P_GO)) {
8262 /* Enforce unique P2P Interface Address */
8263 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8264
c81eff1a
BG
8265 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8266 own_addr) < 0 ||
8267 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8268 new_addr) < 0) {
c55f774d
JM
8269 nl80211_remove_iface(drv, ifidx);
8270 return -1;
8271 }
8272 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8273 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8274 "for P2P group interface");
f2ed8023 8275 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
c55f774d
JM
8276 nl80211_remove_iface(drv, ifidx);
8277 return -1;
8278 }
c81eff1a 8279 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d
JM
8280 new_addr) < 0) {
8281 nl80211_remove_iface(drv, ifidx);
8282 return -1;
8283 }
c55f774d 8284 }
f67eeb5c 8285 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d
JM
8286 }
8287#endif /* CONFIG_P2P */
f3585c8a 8288
22a7c9d7 8289#ifdef HOSTAPD
e17a2477
JM
8290 if (bridge &&
8291 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8292 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8293 "interface %s to a bridge %s", ifname, bridge);
8294 nl80211_remove_iface(drv, ifidx);
8295 os_free(new_bss);
8296 return -1;
8297 }
8298
22a7c9d7 8299 if (type == WPA_IF_AP_BSS) {
c81eff1a
BG
8300 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8301 {
34f2f814 8302 nl80211_remove_iface(drv, ifidx);
07179987 8303 os_free(new_bss);
22a7c9d7
JM
8304 return -1;
8305 }
a2e40bb6 8306 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 8307 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
8308 new_bss->ifindex = ifidx;
8309 new_bss->drv = drv;
8310 new_bss->next = drv->first_bss.next;
a7a6af4c 8311 new_bss->freq = drv->first_bss.freq;
a5e1eb20 8312 new_bss->ctx = bss_ctx;
a2e40bb6
FF
8313 drv->first_bss.next = new_bss;
8314 if (drv_priv)
8315 *drv_priv = new_bss;
cc7a48d1 8316 nl80211_init_bss(new_bss);
3dd1d890
YAP
8317
8318 /* Subscribe management frames for this WPA_IF_AP_BSS */
8319 if (nl80211_setup_ap(new_bss))
8320 return -1;
22a7c9d7
JM
8321 }
8322#endif /* HOSTAPD */
8323
ff6a158b
JM
8324 if (drv->global)
8325 drv->global->if_add_ifindex = ifidx;
8326
22a7c9d7
JM
8327 return 0;
8328}
8329
8330
9ebce9c5 8331static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
8332 enum wpa_driver_if_type type,
8333 const char *ifname)
8334{
a2e40bb6 8335 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
8336 int ifindex = if_nametoindex(ifname);
8337
e748062b
JM
8338 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8339 __func__, type, ifname, ifindex);
8340 if (ifindex <= 0)
8341 return -1;
e17a2477 8342
c34e618d
FF
8343 nl80211_remove_iface(drv, ifindex);
8344
e17a2477 8345#ifdef HOSTAPD
c34e618d
FF
8346 if (type != WPA_IF_AP_BSS)
8347 return 0;
8348
e17a2477 8349 if (bss->added_if_into_bridge) {
c81eff1a
BG
8350 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8351 bss->ifname) < 0)
e17a2477
JM
8352 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8353 "interface %s from bridge %s: %s",
8354 bss->ifname, bss->brname, strerror(errno));
8355 }
8356 if (bss->added_bridge) {
c81eff1a 8357 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
8358 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8359 "bridge %s: %s",
8360 bss->brname, strerror(errno));
8361 }
a2e40bb6
FF
8362
8363 if (bss != &drv->first_bss) {
8546ea19 8364 struct i802_bss *tbss;
a2e40bb6 8365
8546ea19
JM
8366 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8367 if (tbss->next == bss) {
8368 tbss->next = bss->next;
3dd1d890
YAP
8369 /* Unsubscribe management frames */
8370 nl80211_teardown_ap(bss);
cc7a48d1 8371 nl80211_destroy_bss(bss);
8546ea19
JM
8372 os_free(bss);
8373 bss = NULL;
8374 break;
8375 }
22a7c9d7 8376 }
8546ea19
JM
8377 if (bss)
8378 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8379 "BSS %p in the list", __func__, bss);
22a7c9d7
JM
8380 }
8381#endif /* HOSTAPD */
8382
8383 return 0;
8384}
8385
8386
55777702
JM
8387static int cookie_handler(struct nl_msg *msg, void *arg)
8388{
8389 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8390 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8391 u64 *cookie = arg;
8392 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8393 genlmsg_attrlen(gnlh, 0), NULL);
8394 if (tb[NL80211_ATTR_COOKIE])
8395 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8396 return NL_SKIP;
8397}
8398
8399
88df0ef7 8400static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
8401 unsigned int freq, unsigned int wait,
8402 const u8 *buf, size_t buf_len,
88df0ef7
JB
8403 u64 *cookie_out, int no_cck, int no_ack,
8404 int offchanok)
9884f9cc 8405{
88df0ef7 8406 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
8407 struct nl_msg *msg;
8408 u64 cookie;
8409 int ret = -1;
8410
8411 msg = nlmsg_alloc();
8412 if (!msg)
8413 return -1;
8414
2e3e4566
JM
8415 wpa_printf(MSG_DEBUG, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
8416 "no_ack=%d offchanok=%d",
8417 freq, wait, no_cck, no_ack, offchanok);
9fb04070 8418 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9884f9cc 8419
88df0ef7 8420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9884f9cc 8421 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9db931ed
JM
8422 if (wait)
8423 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
b9fd8ce8 8424 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
88df0ef7 8425 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
b106173a
JM
8426 if (no_cck)
8427 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
ddc53271
JM
8428 if (no_ack)
8429 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
970fa12e 8430
9884f9cc
JB
8431 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8432
8433 cookie = 0;
8434 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8435 msg = NULL;
8436 if (ret) {
8437 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
8438 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8439 freq, wait);
9884f9cc
JB
8440 goto nla_put_failure;
8441 }
ddc53271
JM
8442 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
8443 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8444 (long long unsigned int) cookie);
9884f9cc
JB
8445
8446 if (cookie_out)
ddc53271 8447 *cookie_out = no_ack ? (u64) -1 : cookie;
9884f9cc
JB
8448
8449nla_put_failure:
8450 nlmsg_free(msg);
8451 return ret;
8452}
8453
8454
9ebce9c5
JM
8455static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
8456 unsigned int freq,
190b9062 8457 unsigned int wait_time,
58f6fbe0
JM
8458 const u8 *dst, const u8 *src,
8459 const u8 *bssid,
b106173a
JM
8460 const u8 *data, size_t data_len,
8461 int no_cck)
58f6fbe0 8462{
a2e40bb6 8463 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 8464 int ret = -1;
58f6fbe0
JM
8465 u8 *buf;
8466 struct ieee80211_hdr *hdr;
58f6fbe0 8467
5dfca53f 8468 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
8469 "freq=%u MHz wait=%d ms no_cck=%d)",
8470 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
8471
8472 buf = os_zalloc(24 + data_len);
8473 if (buf == NULL)
8474 return ret;
8475 os_memcpy(buf + 24, data, data_len);
8476 hdr = (struct ieee80211_hdr *) buf;
8477 hdr->frame_control =
8478 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8479 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8480 os_memcpy(hdr->addr2, src, ETH_ALEN);
8481 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8482
b1f625e0 8483 if (is_ap_interface(drv->nlmode))
9ebce9c5
JM
8484 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
8485 0, freq, no_cck, 1,
8486 wait_time);
9884f9cc 8487 else
88df0ef7 8488 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 8489 24 + data_len,
b106173a 8490 &drv->send_action_cookie,
88df0ef7 8491 no_cck, 0, 1);
58f6fbe0 8492
f8bf1421 8493 os_free(buf);
58f6fbe0
JM
8494 return ret;
8495}
8496
8497
5dfca53f
JB
8498static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8499{
8500 struct i802_bss *bss = priv;
8501 struct wpa_driver_nl80211_data *drv = bss->drv;
8502 struct nl_msg *msg;
8503 int ret;
8504
8505 msg = nlmsg_alloc();
8506 if (!msg)
8507 return;
8508
316a9e4d
JM
8509 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
8510 (long long unsigned int) drv->send_action_cookie);
9fb04070 8511 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
5dfca53f
JB
8512
8513 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8514 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8515
8516 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8517 msg = NULL;
8518 if (ret)
8519 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8520 "(%s)", ret, strerror(-ret));
8521
8522 nla_put_failure:
8523 nlmsg_free(msg);
8524}
8525
8526
55777702
JM
8527static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8528 unsigned int duration)
8529{
a2e40bb6
FF
8530 struct i802_bss *bss = priv;
8531 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
8532 struct nl_msg *msg;
8533 int ret;
8534 u64 cookie;
8535
8536 msg = nlmsg_alloc();
8537 if (!msg)
8538 return -1;
8539
9fb04070 8540 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
55777702
JM
8541
8542 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8543 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8544 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8545
8546 cookie = 0;
8547 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5883168a 8548 msg = NULL;
55777702
JM
8549 if (ret == 0) {
8550 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8551 "0x%llx for freq=%u MHz duration=%u",
8552 (long long unsigned int) cookie, freq, duration);
8553 drv->remain_on_chan_cookie = cookie;
531f0331 8554 drv->pending_remain_on_chan = 1;
55777702
JM
8555 return 0;
8556 }
8557 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
8558 "(freq=%d duration=%u): %d (%s)",
8559 freq, duration, ret, strerror(-ret));
55777702 8560nla_put_failure:
5883168a 8561 nlmsg_free(msg);
55777702
JM
8562 return -1;
8563}
8564
8565
8566static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8567{
a2e40bb6
FF
8568 struct i802_bss *bss = priv;
8569 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
8570 struct nl_msg *msg;
8571 int ret;
8572
8573 if (!drv->pending_remain_on_chan) {
8574 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8575 "to cancel");
8576 return -1;
8577 }
8578
8579 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8580 "0x%llx",
8581 (long long unsigned int) drv->remain_on_chan_cookie);
8582
8583 msg = nlmsg_alloc();
8584 if (!msg)
8585 return -1;
8586
9fb04070 8587 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
55777702
JM
8588
8589 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8590 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8591
8592 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 8593 msg = NULL;
55777702
JM
8594 if (ret == 0)
8595 return 0;
8596 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8597 "%d (%s)", ret, strerror(-ret));
8598nla_put_failure:
5883168a 8599 nlmsg_free(msg);
55777702
JM
8600 return -1;
8601}
8602
8603
9ebce9c5 8604static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 8605{
a2e40bb6 8606 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 8607
5582a5d1 8608 if (!report) {
0d891981
JM
8609 if (bss->nl_preq && drv->device_ap_sme &&
8610 is_ap_interface(drv->nlmode)) {
8611 /*
8612 * Do not disable Probe Request reporting that was
8613 * enabled in nl80211_setup_ap().
8614 */
8615 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8616 "Probe Request reporting nl_preq=%p while "
8617 "in AP mode", bss->nl_preq);
8618 } else if (bss->nl_preq) {
36488c05
JM
8619 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8620 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 8621 eloop_unregister_read_sock(
481234cf 8622 nl_socket_get_fd(bss->nl_preq));
221a59c9 8623 nl_destroy_handles(&bss->nl_preq);
5582a5d1
JB
8624 }
8625 return 0;
8626 }
8627
481234cf 8628 if (bss->nl_preq) {
5582a5d1 8629 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 8630 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
8631 return 0;
8632 }
8633
481234cf
JM
8634 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8635 if (bss->nl_preq == NULL)
5582a5d1 8636 return -1;
36488c05
JM
8637 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8638 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 8639
481234cf 8640 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
8641 (WLAN_FC_TYPE_MGMT << 2) |
8642 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
8643 NULL, 0) < 0)
8644 goto out_err;
5582a5d1 8645
481234cf 8646 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
a11241fa 8647 wpa_driver_nl80211_event_receive, bss->nl_cb,
481234cf 8648 bss->nl_preq);
5582a5d1 8649
504e905c 8650 return 0;
5582a5d1 8651
a92dfde8 8652 out_err:
221a59c9 8653 nl_destroy_handles(&bss->nl_preq);
5582a5d1 8654 return -1;
504e905c
JM
8655}
8656
8657
4e5cb1a3
JM
8658static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8659 int ifindex, int disabled)
8660{
8661 struct nl_msg *msg;
8662 struct nlattr *bands, *band;
8663 int ret;
8664
8665 msg = nlmsg_alloc();
8666 if (!msg)
8667 return -1;
8668
9fb04070 8669 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
8670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8671
8672 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8673 if (!bands)
8674 goto nla_put_failure;
8675
8676 /*
8677 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8678 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8679 * rates. All 5 GHz rates are left enabled.
8680 */
8681 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8682 if (!band)
8683 goto nla_put_failure;
1dea5882
JM
8684 if (disabled) {
8685 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8686 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8687 }
4e5cb1a3
JM
8688 nla_nest_end(msg, band);
8689
8690 nla_nest_end(msg, bands);
8691
8692 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8693 msg = NULL;
8694 if (ret) {
8695 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8696 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
8697 } else
8698 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
8699
8700 return ret;
8701
8702nla_put_failure:
8703 nlmsg_free(msg);
8704 return -1;
8705}
8706
8707
af473088
JM
8708static int wpa_driver_nl80211_deinit_ap(void *priv)
8709{
a2e40bb6
FF
8710 struct i802_bss *bss = priv;
8711 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 8712 if (!is_ap_interface(drv->nlmode))
af473088
JM
8713 return -1;
8714 wpa_driver_nl80211_del_beacon(drv);
b1f625e0 8715 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
8716}
8717
8718
3c29244e
EP
8719static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8720{
8721 struct i802_bss *bss = priv;
8722 struct wpa_driver_nl80211_data *drv = bss->drv;
8723 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8724 return -1;
8725 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8726}
8727
8728
207ef3fb
JM
8729static void wpa_driver_nl80211_resume(void *priv)
8730{
a2e40bb6
FF
8731 struct i802_bss *bss = priv;
8732 struct wpa_driver_nl80211_data *drv = bss->drv;
c81eff1a 8733 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
207ef3fb
JM
8734 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8735 "resume event");
8736 }
8737}
8738
8739
7b90c16a
JM
8740static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8741 const u8 *ies, size_t ies_len)
8742{
8743 struct i802_bss *bss = priv;
8744 struct wpa_driver_nl80211_data *drv = bss->drv;
8745 int ret;
8746 u8 *data, *pos;
8747 size_t data_len;
341eebee 8748 const u8 *own_addr = bss->addr;
7b90c16a
JM
8749
8750 if (action != 1) {
8751 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8752 "action %d", action);
8753 return -1;
8754 }
8755
8756 /*
8757 * Action frame payload:
8758 * Category[1] = 6 (Fast BSS Transition)
8759 * Action[1] = 1 (Fast BSS Transition Request)
8760 * STA Address
8761 * Target AP Address
8762 * FT IEs
8763 */
8764
73fc617d
JM
8765 data_len = 2 + 2 * ETH_ALEN + ies_len;
8766 data = os_malloc(data_len);
7b90c16a
JM
8767 if (data == NULL)
8768 return -1;
8769 pos = data;
8770 *pos++ = 0x06; /* FT Action category */
8771 *pos++ = action;
8772 os_memcpy(pos, own_addr, ETH_ALEN);
8773 pos += ETH_ALEN;
8774 os_memcpy(pos, target_ap, ETH_ALEN);
8775 pos += ETH_ALEN;
8776 os_memcpy(pos, ies, ies_len);
8777
190b9062
JB
8778 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
8779 drv->bssid, own_addr, drv->bssid,
b106173a 8780 data, data_len, 0);
7b90c16a
JM
8781 os_free(data);
8782
8783 return ret;
8784}
8785
8786
b625473c
JM
8787static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8788{
8789 struct i802_bss *bss = priv;
8790 struct wpa_driver_nl80211_data *drv = bss->drv;
8791 struct nl_msg *msg, *cqm = NULL;
f0494d0f 8792 int ret = -1;
b625473c
JM
8793
8794 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8795 "hysteresis=%d", threshold, hysteresis);
8796
8797 msg = nlmsg_alloc();
8798 if (!msg)
8799 return -1;
8800
9fb04070 8801 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
b625473c
JM
8802
8803 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8804
8805 cqm = nlmsg_alloc();
8806 if (cqm == NULL)
21270bb4 8807 goto nla_put_failure;
b625473c
JM
8808
8809 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
8810 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
f0494d0f
JM
8811 if (nla_put_nested(msg, NL80211_ATTR_CQM, cqm) < 0)
8812 goto nla_put_failure;
21270bb4 8813
f0494d0f 8814 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
8815 msg = NULL;
8816
8817nla_put_failure:
5883168a 8818 nlmsg_free(cqm);
b625473c 8819 nlmsg_free(msg);
f0494d0f 8820 return ret;
b625473c
JM
8821}
8822
8823
1c5c7273
PS
8824static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8825{
8826 struct i802_bss *bss = priv;
8827 struct wpa_driver_nl80211_data *drv = bss->drv;
8828 int res;
8829
8830 os_memset(si, 0, sizeof(*si));
8831 res = nl80211_get_link_signal(drv, si);
8832 if (res != 0)
8833 return res;
8834
8835 return nl80211_get_link_noise(drv, si);
8836}
8837
8838
57ebba59
JJ
8839static int wpa_driver_nl80211_shared_freq(void *priv)
8840{
8841 struct i802_bss *bss = priv;
8842 struct wpa_driver_nl80211_data *drv = bss->drv;
8843 struct wpa_driver_nl80211_data *driver;
8844 int freq = 0;
8845
8846 /*
8847 * If the same PHY is in connected state with some other interface,
8848 * then retrieve the assoc freq.
8849 */
8850 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
8851 drv->phyname);
8852
8853 dl_list_for_each(driver, &drv->global->interfaces,
8854 struct wpa_driver_nl80211_data, list) {
8855 if (drv == driver ||
8856 os_strcmp(drv->phyname, driver->phyname) != 0 ||
8857 !driver->associated)
8858 continue;
8859
8860 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
8861 MACSTR,
8862 driver->phyname, driver->first_bss.ifname,
341eebee 8863 MAC2STR(driver->first_bss.addr));
d3bd0f05
JJ
8864 if (is_ap_interface(driver->nlmode))
8865 freq = driver->first_bss.freq;
8866 else
8867 freq = nl80211_get_assoc_freq(driver);
57ebba59
JJ
8868 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
8869 drv->phyname, freq);
8870 }
8871
8872 if (!freq)
8873 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
8874 "PHY (%s) in associated state", drv->phyname);
8875
8876 return freq;
8877}
8878
8879
b91ab76e
JM
8880static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
8881 int encrypt)
8882{
8883 struct i802_bss *bss = priv;
55231068
JM
8884 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
8885 0, 0, 0, 0);
b91ab76e
JM
8886}
8887
8888
c55f774d
JM
8889static int nl80211_set_param(void *priv, const char *param)
8890{
c55f774d
JM
8891 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
8892 if (param == NULL)
8893 return 0;
8894
8895#ifdef CONFIG_P2P
8896 if (os_strstr(param, "use_p2p_group_interface=1")) {
482856c8
JM
8897 struct i802_bss *bss = priv;
8898 struct wpa_driver_nl80211_data *drv = bss->drv;
8899
c55f774d
JM
8900 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8901 "interface");
8902 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8903 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8904 }
8905#endif /* CONFIG_P2P */
8906
8907 return 0;
8908}
8909
8910
f2ed8023
JM
8911static void * nl80211_global_init(void)
8912{
8913 struct nl80211_global *global;
36d84860
BG
8914 struct netlink_config *cfg;
8915
f2ed8023
JM
8916 global = os_zalloc(sizeof(*global));
8917 if (global == NULL)
8918 return NULL;
c81eff1a 8919 global->ioctl_sock = -1;
f2ed8023 8920 dl_list_init(&global->interfaces);
ff6a158b 8921 global->if_add_ifindex = -1;
36d84860
BG
8922
8923 cfg = os_zalloc(sizeof(*cfg));
8924 if (cfg == NULL)
8925 goto err;
8926
8927 cfg->ctx = global;
8928 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8929 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8930 global->netlink = netlink_init(cfg);
8931 if (global->netlink == NULL) {
8932 os_free(cfg);
8933 goto err;
8934 }
8935
2a7b66f5
BG
8936 if (wpa_driver_nl80211_init_nl_global(global) < 0)
8937 goto err;
8938
c81eff1a
BG
8939 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8940 if (global->ioctl_sock < 0) {
8941 perror("socket(PF_INET,SOCK_DGRAM)");
8942 goto err;
8943 }
8944
f2ed8023 8945 return global;
36d84860
BG
8946
8947err:
8948 nl80211_global_deinit(global);
8949 return NULL;
f2ed8023
JM
8950}
8951
8952
8953static void nl80211_global_deinit(void *priv)
8954{
8955 struct nl80211_global *global = priv;
8956 if (global == NULL)
8957 return;
8958 if (!dl_list_empty(&global->interfaces)) {
8959 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8960 "nl80211_global_deinit",
8961 dl_list_len(&global->interfaces));
8962 }
36d84860
BG
8963
8964 if (global->netlink)
8965 netlink_deinit(global->netlink);
8966
276e2d67
BG
8967 nl_destroy_handles(&global->nl);
8968
481234cf 8969 if (global->nl_event) {
d6c9aab8 8970 eloop_unregister_read_sock(
481234cf 8971 nl_socket_get_fd(global->nl_event));
d6c9aab8
JB
8972 nl_destroy_handles(&global->nl_event);
8973 }
8974
8975 nl_cb_put(global->nl_cb);
2a7b66f5 8976
c81eff1a
BG
8977 if (global->ioctl_sock >= 0)
8978 close(global->ioctl_sock);
8979
f2ed8023
JM
8980 os_free(global);
8981}
8982
8983
6859f1cb
BG
8984static const char * nl80211_get_radio_name(void *priv)
8985{
8986 struct i802_bss *bss = priv;
8987 struct wpa_driver_nl80211_data *drv = bss->drv;
8988 return drv->phyname;
8989}
8990
8991
a6efc65d
JM
8992static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
8993 const u8 *pmkid)
8994{
8995 struct nl_msg *msg;
8996
8997 msg = nlmsg_alloc();
8998 if (!msg)
8999 return -ENOMEM;
9000
9fb04070 9001 nl80211_cmd(bss->drv, msg, 0, cmd);
a6efc65d
JM
9002
9003 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9004 if (pmkid)
9005 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9006 if (bssid)
9007 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9008
9009 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9010 nla_put_failure:
9e088e74 9011 nlmsg_free(msg);
a6efc65d
JM
9012 return -ENOBUFS;
9013}
9014
9015
9016static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9017{
9018 struct i802_bss *bss = priv;
9019 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9020 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9021}
9022
9023
9024static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9025{
9026 struct i802_bss *bss = priv;
9027 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9028 MAC2STR(bssid));
9029 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9030}
9031
9032
9033static int nl80211_flush_pmkid(void *priv)
9034{
9035 struct i802_bss *bss = priv;
9036 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9037 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9038}
9039
9040
b14a210c
JB
9041static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9042 const u8 *replay_ctr)
9043{
9044 struct i802_bss *bss = priv;
9045 struct wpa_driver_nl80211_data *drv = bss->drv;
9046 struct nlattr *replay_nested;
9047 struct nl_msg *msg;
9048
9049 msg = nlmsg_alloc();
9050 if (!msg)
9051 return;
9052
9fb04070 9053 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
b14a210c
JB
9054
9055 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9056
9057 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9058 if (!replay_nested)
9059 goto nla_put_failure;
9060
9061 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9062 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9063 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9064 replay_ctr);
9065
9066 nla_nest_end(msg, replay_nested);
9067
9068 send_and_recv_msgs(drv, msg, NULL, NULL);
9069 return;
9070 nla_put_failure:
9071 nlmsg_free(msg);
9072}
9073
9074
39718852
JB
9075static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9076 const u8 *addr, int qos)
bcf24348 9077{
39718852
JB
9078 /* send data frame to poll STA and check whether
9079 * this frame is ACKed */
bcf24348
JB
9080 struct {
9081 struct ieee80211_hdr hdr;
9082 u16 qos_ctl;
9083 } STRUCT_PACKED nulldata;
9084 size_t size;
9085
9086 /* Send data frame to poll STA and check whether this frame is ACKed */
9087
9088 os_memset(&nulldata, 0, sizeof(nulldata));
9089
9090 if (qos) {
9091 nulldata.hdr.frame_control =
9092 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9093 WLAN_FC_STYPE_QOS_NULL);
9094 size = sizeof(nulldata);
9095 } else {
9096 nulldata.hdr.frame_control =
9097 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9098 WLAN_FC_STYPE_NULLFUNC);
9099 size = sizeof(struct ieee80211_hdr);
9100 }
9101
9102 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9103 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9104 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9105 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9106
9ebce9c5
JM
9107 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9108 0, 0) < 0)
bcf24348
JB
9109 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9110 "send poll frame");
9111}
9112
39718852
JB
9113static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9114 int qos)
9115{
9116 struct i802_bss *bss = priv;
9117 struct wpa_driver_nl80211_data *drv = bss->drv;
9118 struct nl_msg *msg;
9119
9120 if (!drv->poll_command_supported) {
9121 nl80211_send_null_frame(bss, own_addr, addr, qos);
9122 return;
9123 }
9124
9125 msg = nlmsg_alloc();
9126 if (!msg)
9127 return;
9128
9129 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9130
9131 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9132 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9133
9134 send_and_recv_msgs(drv, msg, NULL, NULL);
9135 return;
9136 nla_put_failure:
9137 nlmsg_free(msg);
9138}
9139
bcf24348 9140
29f338af
JM
9141static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9142{
9143 struct nl_msg *msg;
9144
9145 msg = nlmsg_alloc();
9146 if (!msg)
9147 return -ENOMEM;
9148
9149 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9150 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9151 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9152 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9153 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9154nla_put_failure:
9155 nlmsg_free(msg);
9156 return -ENOBUFS;
9157}
9158
9159
9160static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9161 int ctwindow)
9162{
9163 struct i802_bss *bss = priv;
9164
9165 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9166 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9167
9168 if (opp_ps != -1 || ctwindow != -1)
9169 return -1; /* Not yet supported */
9170
9171 if (legacy_ps == -1)
9172 return 0;
9173 if (legacy_ps != 0 && legacy_ps != 1)
9174 return -1; /* Not yet supported */
9175
9176 return nl80211_set_power_save(bss, legacy_ps);
9177}
9178
9179
03ea1786
AN
9180#ifdef CONFIG_TDLS
9181
9182static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9183 u8 dialog_token, u16 status_code,
9184 const u8 *buf, size_t len)
9185{
9186 struct i802_bss *bss = priv;
9187 struct wpa_driver_nl80211_data *drv = bss->drv;
9188 struct nl_msg *msg;
9189
9190 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9191 return -EOPNOTSUPP;
9192
9193 if (!dst)
9194 return -EINVAL;
9195
9196 msg = nlmsg_alloc();
9197 if (!msg)
9198 return -ENOMEM;
9199
9200 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9201 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9202 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9203 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9204 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9205 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9206 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9207
9208 return send_and_recv_msgs(drv, msg, NULL, NULL);
9209
9210nla_put_failure:
9211 nlmsg_free(msg);
9212 return -ENOBUFS;
9213}
9214
9215
9216static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9217{
9218 struct i802_bss *bss = priv;
9219 struct wpa_driver_nl80211_data *drv = bss->drv;
9220 struct nl_msg *msg;
9221 enum nl80211_tdls_operation nl80211_oper;
9222
9223 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9224 return -EOPNOTSUPP;
9225
9226 switch (oper) {
9227 case TDLS_DISCOVERY_REQ:
9228 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9229 break;
9230 case TDLS_SETUP:
9231 nl80211_oper = NL80211_TDLS_SETUP;
9232 break;
9233 case TDLS_TEARDOWN:
9234 nl80211_oper = NL80211_TDLS_TEARDOWN;
9235 break;
9236 case TDLS_ENABLE_LINK:
9237 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9238 break;
9239 case TDLS_DISABLE_LINK:
9240 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9241 break;
9242 case TDLS_ENABLE:
9243 return 0;
9244 case TDLS_DISABLE:
9245 return 0;
9246 default:
9247 return -EINVAL;
9248 }
9249
9250 msg = nlmsg_alloc();
9251 if (!msg)
9252 return -ENOMEM;
9253
9254 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9255 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9256 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9257 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9258
9259 return send_and_recv_msgs(drv, msg, NULL, NULL);
9260
9261nla_put_failure:
9262 nlmsg_free(msg);
9263 return -ENOBUFS;
9264}
9265
9266#endif /* CONFIG TDLS */
9267
9268
216eede8
DS
9269#ifdef ANDROID
9270
9271typedef struct android_wifi_priv_cmd {
9272 char *buf;
9273 int used_len;
9274 int total_len;
9275} android_wifi_priv_cmd;
9276
9277static int drv_errors = 0;
9278
9279static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9280{
9281 drv_errors++;
9282 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9283 drv_errors = 0;
9284 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9285 }
9286}
9287
9288
9289static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9290{
9291 struct wpa_driver_nl80211_data *drv = bss->drv;
9292 struct ifreq ifr;
9293 android_wifi_priv_cmd priv_cmd;
9294 char buf[MAX_DRV_CMD_SIZE];
9295 int ret;
9296
9297 os_memset(&ifr, 0, sizeof(ifr));
9298 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9299 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9300
9301 os_memset(buf, 0, sizeof(buf));
9302 os_strlcpy(buf, cmd, sizeof(buf));
9303
9304 priv_cmd.buf = buf;
9305 priv_cmd.used_len = sizeof(buf);
9306 priv_cmd.total_len = sizeof(buf);
9307 ifr.ifr_data = &priv_cmd;
9308
9309 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9310 if (ret < 0) {
9311 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9312 __func__);
9313 wpa_driver_send_hang_msg(drv);
9314 return ret;
9315 }
9316
9317 drv_errors = 0;
9318 return 0;
9319}
9320
9321
9322static int android_pno_start(struct i802_bss *bss,
9323 struct wpa_driver_scan_params *params)
9324{
9325 struct wpa_driver_nl80211_data *drv = bss->drv;
9326 struct ifreq ifr;
9327 android_wifi_priv_cmd priv_cmd;
9328 int ret = 0, i = 0, bp;
9329 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9330
9331 bp = WEXT_PNOSETUP_HEADER_SIZE;
9332 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9333 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9334 buf[bp++] = WEXT_PNO_TLV_VERSION;
9335 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9336 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9337
9338 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9339 /* Check that there is enough space needed for 1 more SSID, the
9340 * other sections and null termination */
9341 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9342 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9343 break;
9344 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
a97bde0a
JM
9345 params->ssids[i].ssid,
9346 params->ssids[i].ssid_len);
216eede8
DS
9347 buf[bp++] = WEXT_PNO_SSID_SECTION;
9348 buf[bp++] = params->ssids[i].ssid_len;
9349 os_memcpy(&buf[bp], params->ssids[i].ssid,
9350 params->ssids[i].ssid_len);
9351 bp += params->ssids[i].ssid_len;
9352 i++;
9353 }
9354
9355 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9356 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9357 WEXT_PNO_SCAN_INTERVAL);
9358 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9359
9360 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9361 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9362 WEXT_PNO_REPEAT);
9363 bp += WEXT_PNO_REPEAT_LENGTH;
9364
9365 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9366 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9367 WEXT_PNO_MAX_REPEAT);
9368 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9369
9370 memset(&ifr, 0, sizeof(ifr));
9371 memset(&priv_cmd, 0, sizeof(priv_cmd));
9372 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9373
9374 priv_cmd.buf = buf;
9375 priv_cmd.used_len = bp;
9376 priv_cmd.total_len = bp;
9377 ifr.ifr_data = &priv_cmd;
9378
9379 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9380
9381 if (ret < 0) {
9382 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9383 ret);
9384 wpa_driver_send_hang_msg(drv);
9385 return ret;
9386 }
9387
9388 drv_errors = 0;
9389
9390 return android_priv_cmd(bss, "PNOFORCE 1");
9391}
9392
9393
9394static int android_pno_stop(struct i802_bss *bss)
9395{
9396 return android_priv_cmd(bss, "PNOFORCE 0");
9397}
9398
9399#endif /* ANDROID */
9400
9401
9ebce9c5
JM
9402static int driver_nl80211_set_key(const char *ifname, void *priv,
9403 enum wpa_alg alg, const u8 *addr,
9404 int key_idx, int set_tx,
9405 const u8 *seq, size_t seq_len,
9406 const u8 *key, size_t key_len)
9407{
9408 struct i802_bss *bss = priv;
9409 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
9410 set_tx, seq, seq_len, key, key_len);
9411}
9412
9413
9414static int driver_nl80211_scan2(void *priv,
9415 struct wpa_driver_scan_params *params)
9416{
9417 struct i802_bss *bss = priv;
9418 return wpa_driver_nl80211_scan(bss, params);
9419}
9420
9421
9422static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
9423 int reason_code)
9424{
9425 struct i802_bss *bss = priv;
9426 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
9427}
9428
9429
9430static int driver_nl80211_authenticate(void *priv,
9431 struct wpa_driver_auth_params *params)
9432{
9433 struct i802_bss *bss = priv;
9434 return wpa_driver_nl80211_authenticate(bss, params);
9435}
9436
9437
9438static void driver_nl80211_deinit(void *priv)
9439{
9440 struct i802_bss *bss = priv;
9441 wpa_driver_nl80211_deinit(bss);
9442}
9443
9444
9445static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
9446 const char *ifname)
9447{
9448 struct i802_bss *bss = priv;
9449 return wpa_driver_nl80211_if_remove(bss, type, ifname);
9450}
9451
9452
9453static int driver_nl80211_send_mlme(void *priv, const u8 *data,
9454 size_t data_len, int noack)
9455{
9456 struct i802_bss *bss = priv;
9457 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
9458 0, 0, 0, 0);
9459}
9460
9461
9462static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
9463{
9464 struct i802_bss *bss = priv;
9465 return wpa_driver_nl80211_sta_remove(bss, addr);
9466}
9467
9468
9469#if defined(HOSTAPD) || defined(CONFIG_AP)
9470static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
9471 const char *ifname, int vlan_id)
9472{
9473 struct i802_bss *bss = priv;
9474 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
9475}
9476#endif /* HOSTAPD || CONFIG_AP */
9477
9478
9479static int driver_nl80211_read_sta_data(void *priv,
9480 struct hostap_sta_driver_data *data,
9481 const u8 *addr)
9482{
9483 struct i802_bss *bss = priv;
9484 return i802_read_sta_data(bss, data, addr);
9485}
9486
9487
9488static int driver_nl80211_send_action(void *priv, unsigned int freq,
9489 unsigned int wait_time,
9490 const u8 *dst, const u8 *src,
9491 const u8 *bssid,
9492 const u8 *data, size_t data_len,
9493 int no_cck)
9494{
9495 struct i802_bss *bss = priv;
9496 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
9497 bssid, data, data_len, no_cck);
9498}
9499
9500
9501static int driver_nl80211_probe_req_report(void *priv, int report)
9502{
9503 struct i802_bss *bss = priv;
9504 return wpa_driver_nl80211_probe_req_report(bss, report);
9505}
9506
9507
3f5285e8
JM
9508const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9509 .name = "nl80211",
9510 .desc = "Linux nl80211/cfg80211",
9511 .get_bssid = wpa_driver_nl80211_get_bssid,
9512 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
9513 .set_key = driver_nl80211_set_key,
9514 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
9515 .sched_scan = wpa_driver_nl80211_sched_scan,
9516 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 9517 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9ebce9c5
JM
9518 .deauthenticate = driver_nl80211_deauthenticate,
9519 .authenticate = driver_nl80211_authenticate,
3f5285e8 9520 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
9521 .global_init = nl80211_global_init,
9522 .global_deinit = nl80211_global_deinit,
9523 .init2 = wpa_driver_nl80211_init,
9ebce9c5 9524 .deinit = driver_nl80211_deinit,
3f5285e8
JM
9525 .get_capa = wpa_driver_nl80211_get_capa,
9526 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 9527 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 9528 .set_country = wpa_driver_nl80211_set_country,
19c3b566 9529 .set_ap = wpa_driver_nl80211_set_ap,
22a7c9d7 9530 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
9531 .if_remove = driver_nl80211_if_remove,
9532 .send_mlme = driver_nl80211_send_mlme,
c3965310 9533 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
0f4e8b4f 9534 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 9535 .sta_remove = driver_nl80211_sta_remove,
db149ac9 9536 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 9537 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837
JM
9538#ifdef HOSTAPD
9539 .hapd_init = i802_init,
c5121837 9540 .hapd_deinit = i802_deinit,
f7b3920c
JM
9541 .set_wds_sta = i802_set_wds_sta,
9542#endif /* HOSTAPD */
9543#if defined(HOSTAPD) || defined(CONFIG_AP)
c5121837
JM
9544 .get_seqnum = i802_get_seqnum,
9545 .flush = i802_flush,
c5121837
JM
9546 .get_inact_sec = i802_get_inact_sec,
9547 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
9548 .set_rts = i802_set_rts,
9549 .set_frag = i802_set_frag,
c5121837 9550 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 9551 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
9552 .sta_deauth = i802_sta_deauth,
9553 .sta_disassoc = i802_sta_disassoc,
9554#endif /* HOSTAPD || CONFIG_AP */
9ebce9c5 9555 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 9556 .set_freq = i802_set_freq,
9ebce9c5 9557 .send_action = driver_nl80211_send_action,
5dfca53f 9558 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
9559 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9560 .cancel_remain_on_channel =
9561 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 9562 .probe_req_report = driver_nl80211_probe_req_report,
af473088 9563 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 9564 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 9565 .resume = wpa_driver_nl80211_resume,
7b90c16a 9566 .send_ft_action = nl80211_send_ft_action,
b625473c 9567 .signal_monitor = nl80211_signal_monitor,
1c5c7273 9568 .signal_poll = nl80211_signal_poll,
b91ab76e 9569 .send_frame = nl80211_send_frame,
57ebba59 9570 .shared_freq = wpa_driver_nl80211_shared_freq,
c55f774d 9571 .set_param = nl80211_set_param,
6859f1cb 9572 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
9573 .add_pmkid = nl80211_add_pmkid,
9574 .remove_pmkid = nl80211_remove_pmkid,
9575 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 9576 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 9577 .poll_client = nl80211_poll_client,
29f338af 9578 .set_p2p_powersave = nl80211_set_p2p_powersave,
03ea1786
AN
9579#ifdef CONFIG_TDLS
9580 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9581 .tdls_oper = nl80211_tdls_oper,
9582#endif /* CONFIG_TDLS */
3f5285e8 9583};