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