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