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