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