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