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