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