]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
dbus: Fix init-failure-detection
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
CommitLineData
3f5285e8 1/*
c5121837 2 * Driver interaction with Linux nl80211/cfg80211
072ad14c
JM
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
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>
7 * Copyright (c) 2009, 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>
21#include <net/if_arp.h>
37b7d082 22#include <net/if.h>
3f5285e8
JM
23#include <netlink/genl/genl.h>
24#include <netlink/genl/family.h>
25#include <netlink/genl/ctrl.h>
1b648c7e
JM
26#include <netpacket/packet.h>
27#include <linux/filter.h>
7e45830a 28#include "nl80211_copy.h"
625f587b 29
3f5285e8 30#include "common.h"
1b648c7e
JM
31#include "eloop.h"
32#include "common/ieee802_11_defs.h"
e2d02c29
JM
33#include "netlink.h"
34#include "radiotap.h"
35#include "radiotap_iter.h"
1b648c7e 36#include "driver.h"
0915d02c 37
c5121837
JM
38#ifdef CONFIG_LIBNL20
39/* libnl 2.0 compatibility code */
2e8eac2d 40#define nl_handle nl_sock
c5121837
JM
41#define nl_handle_alloc_cb nl_socket_alloc_cb
42#define nl_handle_destroy nl_socket_free
43#endif /* CONFIG_LIBNL20 */
44
c5121837 45
3f5285e8
JM
46#ifndef IFF_LOWER_UP
47#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
48#endif
49#ifndef IFF_DORMANT
50#define IFF_DORMANT 0x20000 /* driver signals dormant */
51#endif
52
53#ifndef IF_OPER_DORMANT
54#define IF_OPER_DORMANT 5
55#endif
56#ifndef IF_OPER_UP
57#define IF_OPER_UP 6
58#endif
59
c5121837
JM
60struct i802_bss {
61 struct i802_bss *next;
b4fd6fab 62 int ifindex;
c5121837
JM
63 unsigned int beacon_set:1;
64};
3f5285e8
JM
65
66struct wpa_driver_nl80211_data {
67 void *ctx;
08063178 68 struct netlink_data *netlink;
c5121837 69 int ioctl_sock; /* socket for ioctl() use */
3f5285e8
JM
70 char ifname[IFNAMSIZ + 1];
71 int ifindex;
7524cfb1 72 int if_removed;
c2a04078
JM
73 struct wpa_driver_capa capa;
74 int has_capability;
c2a04078 75
3f5285e8
JM
76 int operstate;
77
3f5285e8
JM
78 int scan_complete_events;
79
80 struct nl_handle *nl_handle;
335ce76b 81 struct nl_handle *nl_handle_event;
3f5285e8 82 struct nl_cache *nl_cache;
335ce76b 83 struct nl_cache *nl_cache_event;
3f5285e8
JM
84 struct nl_cb *nl_cb;
85 struct genl_family *nl80211;
1c873584 86
e6b8efeb 87 u8 auth_bssid[ETH_ALEN];
c2a04078
JM
88 u8 bssid[ETH_ALEN];
89 int associated;
fd05d64e
JM
90 u8 ssid[32];
91 size_t ssid_len;
ad1e68e6
JM
92 int nlmode;
93 int ap_scan_as_station;
4832ecd7 94 unsigned int assoc_freq;
d2440ba0 95
0915d02c
JM
96 int monitor_sock;
97 int monitor_ifidx;
7da3abe7 98
7da3abe7 99 unsigned int beacon_set:1;
c5121837
JM
100
101#ifdef HOSTAPD
c5121837 102 int eapol_sock; /* socket for EAPOL frames */
c5121837
JM
103
104 int default_if_indices[16];
105 int *if_indices;
106 int num_if_indices;
107
c5121837 108 struct i802_bss bss;
c5121837
JM
109
110 int last_freq;
111 int last_freq_ht;
c5121837 112#endif /* HOSTAPD */
3f5285e8
JM
113};
114
115
116static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
117 void *timeout_ctx);
ad1e68e6 118static int wpa_driver_nl80211_set_mode(void *priv, int mode);
362f781e 119static int
7524cfb1 120wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
d72aad94
JM
121static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
122 const u8 *addr, int cmd, u16 reason_code);
460456f8
JM
123static void nl80211_remove_monitor_interface(
124 struct wpa_driver_nl80211_data *drv);
0915d02c 125
072ad14c 126#ifdef HOSTAPD
2135f224
JM
127static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
128static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
b4fd6fab
JM
129static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
130 int ifindex);
bbaf0837 131static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
fbbfcbac
FF
132static int wpa_driver_nl80211_if_remove(void *priv,
133 enum wpa_driver_if_type type,
134 const char *ifname);
072ad14c
JM
135#endif /* HOSTAPD */
136
3f5285e8 137
6241fcb1
JM
138/* nl80211 code */
139static int ack_handler(struct nl_msg *msg, void *arg)
140{
141 int *err = arg;
142 *err = 0;
143 return NL_STOP;
144}
145
146static int finish_handler(struct nl_msg *msg, void *arg)
147{
8e8df255
JM
148 int *ret = arg;
149 *ret = 0;
6241fcb1
JM
150 return NL_SKIP;
151}
152
153static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
154 void *arg)
155{
156 int *ret = arg;
157 *ret = err->error;
158 return NL_SKIP;
159}
160
5b7b85f6
JM
161
162static int no_seq_check(struct nl_msg *msg, void *arg)
163{
164 return NL_OK;
165}
166
167
6241fcb1
JM
168static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
169 struct nl_msg *msg,
170 int (*valid_handler)(struct nl_msg *, void *),
171 void *valid_data)
172{
173 struct nl_cb *cb;
174 int err = -ENOMEM;
175
176 cb = nl_cb_clone(drv->nl_cb);
177 if (!cb)
178 goto out;
179
180 err = nl_send_auto_complete(drv->nl_handle, msg);
181 if (err < 0)
182 goto out;
183
184 err = 1;
185
186 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
8e8df255 187 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
6241fcb1
JM
188 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
189
190 if (valid_handler)
191 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
192 valid_handler, valid_data);
193
194 while (err > 0)
195 nl_recvmsgs(drv->nl_handle, cb);
196 out:
197 nl_cb_put(cb);
198 nlmsg_free(msg);
199 return err;
200}
201
202
97865538
JM
203struct family_data {
204 const char *group;
205 int id;
206};
207
208
209static int family_handler(struct nl_msg *msg, void *arg)
210{
211 struct family_data *res = arg;
212 struct nlattr *tb[CTRL_ATTR_MAX + 1];
213 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
214 struct nlattr *mcgrp;
215 int i;
216
217 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
218 genlmsg_attrlen(gnlh, 0), NULL);
219 if (!tb[CTRL_ATTR_MCAST_GROUPS])
220 return NL_SKIP;
221
222 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
223 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
224 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
225 nla_len(mcgrp), NULL);
226 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
227 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
228 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
229 res->group,
230 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
231 continue;
232 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
233 break;
234 };
235
236 return NL_SKIP;
237}
238
239
240static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
241 const char *family, const char *group)
242{
243 struct nl_msg *msg;
244 int ret = -1;
245 struct family_data res = { group, -ENOENT };
246
247 msg = nlmsg_alloc();
248 if (!msg)
249 return -ENOMEM;
250 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
251 0, 0, CTRL_CMD_GETFAMILY, 0);
252 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
253
254 ret = send_and_recv_msgs(drv, msg, family_handler, &res);
255 msg = NULL;
256 if (ret == 0)
257 ret = res.id;
258
259nla_put_failure:
260 nlmsg_free(msg);
261 return ret;
262}
263
264
41d931ee
JM
265#ifdef HOSTAPD
266static int get_ifhwaddr(struct wpa_driver_nl80211_data *drv,
267 const char *ifname, u8 *addr)
268{
269 struct ifreq ifr;
270
271 os_memset(&ifr, 0, sizeof(ifr));
272 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
273 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr)) {
274 wpa_printf(MSG_ERROR, "%s: ioctl(SIOCGIFHWADDR): %d (%s)",
275 ifname, errno, strerror(errno));
276 return -1;
277 }
278
279 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
280 wpa_printf(MSG_ERROR, "%s: Invalid HW-addr family 0x%04x",
281 ifname, ifr.ifr_hwaddr.sa_family);
282 return -1;
283 }
284 os_memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
285
286 return 0;
287}
288
289
290static int set_ifhwaddr(struct wpa_driver_nl80211_data *drv,
291 const char *ifname, const u8 *addr)
292{
293 struct ifreq ifr;
294
295 os_memset(&ifr, 0, sizeof(ifr));
296 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
297 os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
298 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
299
300 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
301 wpa_printf(MSG_DEBUG, "%s: ioctl(SIOCSIFHWADDR): %d (%s)",
302 ifname, errno, strerror(errno));
303 return -1;
304 }
305
306 return 0;
307}
308#endif /* HOSTAPD */
309
310
3f5285e8
JM
311static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
312{
313 struct wpa_driver_nl80211_data *drv = priv;
c2a04078
JM
314 if (!drv->associated)
315 return -1;
316 os_memcpy(bssid, drv->bssid, ETH_ALEN);
317 return 0;
3f5285e8
JM
318}
319
320
3f5285e8
JM
321static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
322{
323 struct wpa_driver_nl80211_data *drv = priv;
fd05d64e
JM
324 if (!drv->associated)
325 return -1;
326 os_memcpy(ssid, drv->ssid, drv->ssid_len);
327 return drv->ssid_len;
3f5285e8
JM
328}
329
330
7524cfb1 331static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
08063178 332 char *buf, size_t len, int del)
3f5285e8
JM
333{
334 union wpa_event_data event;
335
336 os_memset(&event, 0, sizeof(event));
337 if (len > sizeof(event.interface_status.ifname))
338 len = sizeof(event.interface_status.ifname) - 1;
339 os_memcpy(event.interface_status.ifname, buf, len);
340 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
341 EVENT_INTERFACE_ADDED;
342
343 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
344 del ? "DEL" : "NEW",
345 event.interface_status.ifname,
346 del ? "removed" : "added");
347
7524cfb1
JM
348 if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
349 if (del)
350 drv->if_removed = 1;
351 else
352 drv->if_removed = 0;
353 }
354
08063178 355 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
3f5285e8
JM
356}
357
358
7524cfb1 359static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
62d680c3 360 u8 *buf, size_t len)
7524cfb1 361{
62d680c3 362 int attrlen, rta_len;
7524cfb1
JM
363 struct rtattr *attr;
364
62d680c3
JM
365 attrlen = len;
366 attr = (struct rtattr *) buf;
7524cfb1
JM
367
368 rta_len = RTA_ALIGN(sizeof(struct rtattr));
369 while (RTA_OK(attr, attrlen)) {
370 if (attr->rta_type == IFLA_IFNAME) {
371 if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
372 == 0)
373 return 1;
374 else
375 break;
376 }
377 attr = RTA_NEXT(attr, attrlen);
378 }
379
380 return 0;
381}
382
383
384static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
62d680c3 385 int ifindex, u8 *buf, size_t len)
7524cfb1
JM
386{
387 if (drv->ifindex == ifindex)
388 return 1;
389
62d680c3 390 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
7524cfb1
JM
391 drv->ifindex = if_nametoindex(drv->ifname);
392 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
393 "interface");
394 wpa_driver_nl80211_finish_drv_init(drv);
395 return 1;
396 }
397
398 return 0;
399}
400
401
62d680c3
JM
402static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
403 struct ifinfomsg *ifi,
404 u8 *buf, size_t len)
3f5285e8 405{
08063178 406 struct wpa_driver_nl80211_data *drv = ctx;
62d680c3
JM
407 int attrlen, rta_len;
408 struct rtattr *attr;
3f5285e8 409
62d680c3 410 if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) {
3f5285e8
JM
411 wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
412 ifi->ifi_index);
413 return;
414 }
415
416 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
417 "(%s%s%s%s)",
418 drv->operstate, ifi->ifi_flags,
419 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
420 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
421 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
422 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
423 /*
424 * Some drivers send the association event before the operup event--in
425 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
426 * fails. This will hit us when wpa_supplicant does not need to do
427 * IEEE 802.1X authentication
428 */
429 if (drv->operstate == 1 &&
430 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
431 !(ifi->ifi_flags & IFF_RUNNING))
08063178 432 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
e2d02c29 433 -1, IF_OPER_UP);
3f5285e8 434
62d680c3
JM
435 attrlen = len;
436 attr = (struct rtattr *) buf;
3f5285e8
JM
437 rta_len = RTA_ALIGN(sizeof(struct rtattr));
438 while (RTA_OK(attr, attrlen)) {
d8816397 439 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 440 wpa_driver_nl80211_event_link(
08063178 441 drv,
7524cfb1
JM
442 ((char *) attr) + rta_len,
443 attr->rta_len - rta_len, 0);
3f5285e8
JM
444 }
445 attr = RTA_NEXT(attr, attrlen);
446 }
447}
448
449
62d680c3
JM
450static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
451 struct ifinfomsg *ifi,
452 u8 *buf, size_t len)
3f5285e8 453{
08063178 454 struct wpa_driver_nl80211_data *drv = ctx;
62d680c3
JM
455 int attrlen, rta_len;
456 struct rtattr *attr;
3f5285e8 457
62d680c3
JM
458 attrlen = len;
459 attr = (struct rtattr *) buf;
3f5285e8
JM
460
461 rta_len = RTA_ALIGN(sizeof(struct rtattr));
462 while (RTA_OK(attr, attrlen)) {
463 if (attr->rta_type == IFLA_IFNAME) {
7524cfb1 464 wpa_driver_nl80211_event_link(
08063178 465 drv,
7524cfb1
JM
466 ((char *) attr) + rta_len,
467 attr->rta_len - rta_len, 1);
3f5285e8
JM
468 }
469 attr = RTA_NEXT(attr, attrlen);
470 }
471}
472
473
c2a04078
JM
474static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
475 const u8 *frame, size_t len)
476{
477 const struct ieee80211_mgmt *mgmt;
478 union wpa_event_data event;
479
480 mgmt = (const struct ieee80211_mgmt *) frame;
481 if (len < 24 + sizeof(mgmt->u.auth)) {
482 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
483 "frame");
484 return;
485 }
486
e6b8efeb 487 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
c2a04078
JM
488 os_memset(&event, 0, sizeof(event));
489 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
490 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
491 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
492 if (len > 24 + sizeof(mgmt->u.auth)) {
493 event.auth.ies = mgmt->u.auth.variable;
494 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
495 }
496
497 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
498}
499
500
501static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
502 const u8 *frame, size_t len)
503{
504 const struct ieee80211_mgmt *mgmt;
505 union wpa_event_data event;
506 u16 status;
507
508 mgmt = (const struct ieee80211_mgmt *) frame;
509 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
510 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
511 "frame");
512 return;
513 }
514
515 status = le_to_host16(mgmt->u.assoc_resp.status_code);
516 if (status != WLAN_STATUS_SUCCESS) {
efa46078
JM
517 os_memset(&event, 0, sizeof(event));
518 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
519 event.assoc_reject.resp_ies =
520 (u8 *) mgmt->u.assoc_resp.variable;
521 event.assoc_reject.resp_ies_len =
522 len - 24 - sizeof(mgmt->u.assoc_resp);
523 }
524 event.assoc_reject.status_code = status;
525
526 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
c2a04078
JM
527 return;
528 }
529
530 drv->associated = 1;
531 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
532
533 os_memset(&event, 0, sizeof(event));
534 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
535 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
536 event.assoc_info.resp_ies_len =
efa46078 537 len - 24 - sizeof(mgmt->u.assoc_resp);
c2a04078
JM
538 }
539
4832ecd7
JM
540 event.assoc_info.freq = drv->assoc_freq;
541
c2a04078
JM
542 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
543}
544
c1bb3e0a 545
da72a1c1
ZY
546static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
547 enum nl80211_commands cmd, struct nlattr *status,
548 struct nlattr *addr, struct nlattr *req_ie,
549 struct nlattr *resp_ie)
550{
551 union wpa_event_data event;
552
7da2c527
JM
553 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
554 /*
555 * Avoid reporting two association events that would confuse
556 * the core code.
557 */
558 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
559 "when using userspace SME", cmd);
560 return;
561 }
562
da72a1c1
ZY
563 os_memset(&event, 0, sizeof(event));
564 if (cmd == NL80211_CMD_CONNECT &&
565 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
566 if (resp_ie) {
567 event.assoc_reject.resp_ies = nla_data(resp_ie);
568 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
569 }
570 event.assoc_reject.status_code = nla_get_u16(status);
571 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
572 return;
573 }
574
575 drv->associated = 1;
576 if (addr)
577 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
578
579 if (req_ie) {
580 event.assoc_info.req_ies = nla_data(req_ie);
581 event.assoc_info.req_ies_len = nla_len(req_ie);
582 }
583 if (resp_ie) {
584 event.assoc_info.resp_ies = nla_data(resp_ie);
585 event.assoc_info.resp_ies_len = nla_len(resp_ie);
586 }
587
588 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
589}
c2a04078 590
c1bb3e0a 591
da1fb17c
JM
592static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
593 enum nl80211_commands cmd, struct nlattr *addr)
594{
595 union wpa_event_data event;
596 enum wpa_event_type ev;
597
598 if (nla_len(addr) != ETH_ALEN)
599 return;
600
601 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
602 cmd, MAC2STR((u8 *) nla_data(addr)));
603
604 if (cmd == NL80211_CMD_AUTHENTICATE)
605 ev = EVENT_AUTH_TIMED_OUT;
606 else if (cmd == NL80211_CMD_ASSOCIATE)
607 ev = EVENT_ASSOC_TIMED_OUT;
608 else
609 return;
610
611 os_memset(&event, 0, sizeof(event));
612 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
613 wpa_supplicant_event(drv->ctx, ev, &event);
614}
615
616
c2a04078 617static void mlme_event(struct wpa_driver_nl80211_data *drv,
da1fb17c
JM
618 enum nl80211_commands cmd, struct nlattr *frame,
619 struct nlattr *addr, struct nlattr *timed_out)
c2a04078 620{
da1fb17c
JM
621 if (timed_out && addr) {
622 mlme_timeout_event(drv, cmd, addr);
623 return;
624 }
625
c2a04078
JM
626 if (frame == NULL) {
627 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
628 "data", cmd);
629 return;
630 }
631
632 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
633 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
634 nla_data(frame), nla_len(frame));
635
636 switch (cmd) {
637 case NL80211_CMD_AUTHENTICATE:
638 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
639 break;
640 case NL80211_CMD_ASSOCIATE:
641 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
642 break;
643 case NL80211_CMD_DEAUTHENTICATE:
644 drv->associated = 0;
645 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, NULL);
646 break;
647 case NL80211_CMD_DISASSOCIATE:
648 drv->associated = 0;
649 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
650 break;
651 default:
652 break;
653 }
654}
655
656
35583f3f
JM
657static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
658 struct nlattr *tb[])
659{
660 union wpa_event_data data;
661
662 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
663 os_memset(&data, 0, sizeof(data));
664 if (tb[NL80211_ATTR_MAC]) {
665 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
666 nla_data(tb[NL80211_ATTR_MAC]),
667 nla_len(tb[NL80211_ATTR_MAC]));
ad1e68e6 668 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
35583f3f
JM
669 }
670 if (tb[NL80211_ATTR_KEY_SEQ]) {
671 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
672 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
673 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
674 }
675 if (tb[NL80211_ATTR_KEY_TYPE]) {
676 enum nl80211_key_type key_type =
677 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
678 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
679 if (key_type == NL80211_KEYTYPE_PAIRWISE)
680 data.michael_mic_failure.unicast = 1;
681 } else
682 data.michael_mic_failure.unicast = 1;
683
684 if (tb[NL80211_ATTR_KEY_IDX]) {
685 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
686 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
687 }
688
689 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
690}
691
692
5cc4d64b
JM
693static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
694 struct nlattr *tb[])
695{
696 if (tb[NL80211_ATTR_MAC] == NULL) {
697 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
698 "event");
699 return;
700 }
701 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
702 drv->associated = 1;
703 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
704 MAC2STR(drv->bssid));
705
706 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
707}
708
709
97865538
JM
710static int process_event(struct nl_msg *msg, void *arg)
711{
712 struct wpa_driver_nl80211_data *drv = arg;
713 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
714 struct nlattr *tb[NL80211_ATTR_MAX + 1];
715
716 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
717 genlmsg_attrlen(gnlh, 0), NULL);
718
719 if (tb[NL80211_ATTR_IFINDEX]) {
720 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
721 if (ifindex != drv->ifindex) {
722 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
723 " for foreign interface (ifindex %d)",
724 gnlh->cmd, ifindex);
725 return NL_SKIP;
726 }
727 }
728
ad1e68e6
JM
729 if (drv->ap_scan_as_station &&
730 (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
731 gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
732 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
733 drv->ap_scan_as_station = 0;
734 }
735
97865538 736 switch (gnlh->cmd) {
d942a79e
JM
737 case NL80211_CMD_TRIGGER_SCAN:
738 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
739 break;
97865538
JM
740 case NL80211_CMD_NEW_SCAN_RESULTS:
741 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
742 drv->scan_complete_events = 1;
743 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
744 drv->ctx);
745 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
746 break;
747 case NL80211_CMD_SCAN_ABORTED:
748 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
749 /*
750 * Need to indicate that scan results are available in order
751 * not to make wpa_supplicant stop its scanning.
752 */
753 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
754 drv->ctx);
755 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
756 break;
c2a04078
JM
757 case NL80211_CMD_AUTHENTICATE:
758 case NL80211_CMD_ASSOCIATE:
759 case NL80211_CMD_DEAUTHENTICATE:
760 case NL80211_CMD_DISASSOCIATE:
da1fb17c
JM
761 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
762 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT]);
c2a04078 763 break;
da72a1c1
ZY
764 case NL80211_CMD_CONNECT:
765 case NL80211_CMD_ROAM:
766 mlme_event_connect(drv, gnlh->cmd,
767 tb[NL80211_ATTR_STATUS_CODE],
768 tb[NL80211_ATTR_MAC],
769 tb[NL80211_ATTR_REQ_IE],
770 tb[NL80211_ATTR_RESP_IE]);
771 break;
772 case NL80211_CMD_DISCONNECT:
7da2c527
JM
773 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
774 /*
775 * Avoid reporting two disassociation events that could
776 * confuse the core code.
777 */
778 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
779 "event when using userspace SME");
780 break;
781 }
da72a1c1
ZY
782 drv->associated = 0;
783 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
784 break;
35583f3f
JM
785 case NL80211_CMD_MICHAEL_MIC_FAILURE:
786 mlme_event_michael_mic_failure(drv, tb);
787 break;
5cc4d64b
JM
788 case NL80211_CMD_JOIN_IBSS:
789 mlme_event_join_ibss(drv, tb);
790 break;
97865538 791 default:
c2a04078
JM
792 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
793 "(cmd=%d)", gnlh->cmd);
97865538
JM
794 break;
795 }
796
797 return NL_SKIP;
798}
799
800
801static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
802 void *sock_ctx)
803{
804 struct nl_cb *cb;
805 struct wpa_driver_nl80211_data *drv = eloop_ctx;
806
807 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
808
809 cb = nl_cb_clone(drv->nl_cb);
810 if (!cb)
811 return;
812 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
813 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
335ce76b 814 nl_recvmsgs(drv->nl_handle_event, cb);
97865538
JM
815 nl_cb_put(cb);
816}
817
818
2135f224
JM
819static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data *drv,
820 const char *ifname, int dev_up)
3f5285e8
JM
821{
822 struct ifreq ifr;
823
2135f224
JM
824 if (drv->ioctl_sock < 0)
825 return -1;
826
3f5285e8
JM
827 os_memset(&ifr, 0, sizeof(ifr));
828 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
2135f224
JM
829
830 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
3f5285e8 831 perror("ioctl[SIOCGIFFLAGS]");
2135f224
JM
832 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
833 ifname);
3f5285e8
JM
834 return -1;
835 }
3f5285e8 836
2135f224
JM
837 if (dev_up) {
838 if (ifr.ifr_flags & IFF_UP)
839 return 0;
840 ifr.ifr_flags |= IFF_UP;
841 } else {
842 if (!(ifr.ifr_flags & IFF_UP))
843 return 0;
844 ifr.ifr_flags &= ~IFF_UP;
845 }
3f5285e8 846
2135f224
JM
847 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
848 perror("ioctl[SIOCSIFFLAGS]");
3f5285e8
JM
849 return -1;
850 }
3f5285e8 851
2135f224 852 return 0;
3f5285e8
JM
853}
854
855
6d158490
LR
856/**
857 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
858 * @priv: driver_nl80211 private data
859 * @alpha2_arg: country to which to switch to
860 * Returns: 0 on success, -1 on failure
861 *
862 * This asks nl80211 to set the regulatory domain for given
863 * country ISO / IEC alpha2.
864 */
865static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
866{
867 struct wpa_driver_nl80211_data *drv = priv;
868 char alpha2[3];
869 struct nl_msg *msg;
870
871 msg = nlmsg_alloc();
872 if (!msg)
e785c2ba 873 return -ENOMEM;
6d158490
LR
874
875 alpha2[0] = alpha2_arg[0];
876 alpha2[1] = alpha2_arg[1];
877 alpha2[2] = '\0';
878
879 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
880 0, NL80211_CMD_REQ_SET_REG, 0);
881
882 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
883 if (send_and_recv_msgs(drv, msg, NULL, NULL))
884 return -EINVAL;
885 return 0;
886nla_put_failure:
887 return -EINVAL;
888}
889
890
bbaf0837 891#ifndef HOSTAPD
80bc75f1
JM
892struct wiphy_info_data {
893 int max_scan_ssids;
1581b38b 894 int ap_supported;
93d11400
ZY
895 int auth_supported;
896 int connect_supported;
80bc75f1
JM
897};
898
899
900static int wiphy_info_handler(struct nl_msg *msg, void *arg)
901{
902 struct nlattr *tb[NL80211_ATTR_MAX + 1];
903 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
904 struct wiphy_info_data *info = arg;
905
906 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
907 genlmsg_attrlen(gnlh, 0), NULL);
908
909 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
910 info->max_scan_ssids =
911 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
912
1581b38b
JM
913 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
914 struct nlattr *nl_mode;
915 int i;
916 nla_for_each_nested(nl_mode,
917 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
918 if (nl_mode->nla_type == NL80211_IFTYPE_AP) {
919 info->ap_supported = 1;
920 break;
921 }
922 }
923 }
924
93d11400
ZY
925 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
926 struct nlattr *nl_cmd;
927 int i;
928
929 nla_for_each_nested(nl_cmd,
930 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
931 u32 cmd = nla_get_u32(nl_cmd);
932 if (cmd == NL80211_CMD_AUTHENTICATE)
933 info->auth_supported = 1;
934 else if (cmd == NL80211_CMD_CONNECT)
935 info->connect_supported = 1;
936 }
937 }
938
80bc75f1
JM
939 return NL_SKIP;
940}
941
942
943static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
944 struct wiphy_info_data *info)
945{
946 struct nl_msg *msg;
947
948 os_memset(info, 0, sizeof(*info));
949 msg = nlmsg_alloc();
950 if (!msg)
951 return -1;
952
953 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
954 0, NL80211_CMD_GET_WIPHY, 0);
955
956 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
957
958 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
959 return 0;
960 msg = NULL;
961nla_put_failure:
962 nlmsg_free(msg);
963 return -1;
964}
965
966
93d11400 967static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
80bc75f1
JM
968{
969 struct wiphy_info_data info;
970 if (wpa_driver_nl80211_get_info(drv, &info))
93d11400 971 return -1;
80bc75f1 972 drv->has_capability = 1;
1b2a72e8
JM
973 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
974 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
975 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
976 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
977 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
978 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
979 WPA_DRIVER_CAPA_ENC_WEP104 |
980 WPA_DRIVER_CAPA_ENC_TKIP |
981 WPA_DRIVER_CAPA_ENC_CCMP;
982
80bc75f1 983 drv->capa.max_scan_ssids = info.max_scan_ssids;
1581b38b
JM
984 if (info.ap_supported)
985 drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
93d11400
ZY
986
987 if (info.auth_supported)
988 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
989 else if (!info.connect_supported) {
990 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
991 "authentication/association or connect commands");
992 return -1;
993 }
994
0194fedb
JB
995 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
996
93d11400 997 return 0;
80bc75f1 998}
bbaf0837 999#endif /* HOSTAPD */
80bc75f1
JM
1000
1001
9fff9fdc
JM
1002static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
1003 void *ctx)
3f5285e8 1004{
9fff9fdc 1005 int ret;
3f5285e8 1006
9fff9fdc 1007 /* Initialize generic netlink and nl80211 */
3f5285e8
JM
1008
1009 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1010 if (drv->nl_cb == NULL) {
1011 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1012 "callbacks");
1013 goto err1;
1014 }
1015
1016 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1017 if (drv->nl_handle == NULL) {
1018 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1019 "callbacks");
1020 goto err2;
1021 }
1022
335ce76b
JM
1023 drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb);
1024 if (drv->nl_handle_event == NULL) {
1025 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1026 "callbacks (event)");
1027 goto err2b;
1028 }
1029
3f5285e8
JM
1030 if (genl_connect(drv->nl_handle)) {
1031 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1032 "netlink");
1033 goto err3;
1034 }
1035
335ce76b
JM
1036 if (genl_connect(drv->nl_handle_event)) {
1037 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1038 "netlink (event)");
1039 goto err3;
1040 }
1041
9fff9fdc
JM
1042#ifdef CONFIG_LIBNL20
1043 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
1044 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1045 "netlink cache");
1046 goto err3;
1047 }
335ce76b
JM
1048 if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
1049 0) {
1050 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1051 "netlink cache (event)");
1052 goto err3b;
1053 }
9fff9fdc 1054#else /* CONFIG_LIBNL20 */
3f5285e8
JM
1055 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1056 if (drv->nl_cache == NULL) {
1057 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1058 "netlink cache");
1059 goto err3;
1060 }
335ce76b
JM
1061 drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
1062 if (drv->nl_cache_event == NULL) {
1063 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1064 "netlink cache (event)");
1065 goto err3b;
1066 }
9fff9fdc
JM
1067#endif /* CONFIG_LIBNL20 */
1068
3f5285e8
JM
1069 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1070 if (drv->nl80211 == NULL) {
1071 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1072 "found");
1073 goto err4;
1074 }
1075
97865538
JM
1076 ret = nl_get_multicast_id(drv, "nl80211", "scan");
1077 if (ret >= 0)
335ce76b 1078 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
97865538
JM
1079 if (ret < 0) {
1080 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1081 "membership for scan events: %d (%s)",
1082 ret, strerror(-ret));
1083 goto err4;
1084 }
c2a04078
JM
1085
1086 ret = nl_get_multicast_id(drv, "nl80211", "mlme");
1087 if (ret >= 0)
335ce76b 1088 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
c2a04078
JM
1089 if (ret < 0) {
1090 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1091 "membership for mlme events: %d (%s)",
1092 ret, strerror(-ret));
1093 goto err4;
1094 }
c2a04078 1095
335ce76b 1096 eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
97865538
JM
1097 wpa_driver_nl80211_event_receive, drv, ctx);
1098
9fff9fdc
JM
1099 return 0;
1100
1101err4:
335ce76b
JM
1102 nl_cache_free(drv->nl_cache_event);
1103err3b:
9fff9fdc
JM
1104 nl_cache_free(drv->nl_cache);
1105err3:
335ce76b
JM
1106 nl_handle_destroy(drv->nl_handle_event);
1107err2b:
9fff9fdc
JM
1108 nl_handle_destroy(drv->nl_handle);
1109err2:
1110 nl_cb_put(drv->nl_cb);
1111err1:
1112 return -1;
1113}
1114
1115
1116/**
1117 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1118 * @ctx: context to be used when calling wpa_supplicant functions,
1119 * e.g., wpa_supplicant_event()
1120 * @ifname: interface name, e.g., wlan0
1121 * Returns: Pointer to private data, %NULL on failure
1122 */
1123static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
1124{
9fff9fdc 1125 struct wpa_driver_nl80211_data *drv;
08063178 1126 struct netlink_config *cfg;
9fff9fdc
JM
1127
1128 drv = os_zalloc(sizeof(*drv));
1129 if (drv == NULL)
1130 return NULL;
1131 drv->ctx = ctx;
1132 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
9fff9fdc
JM
1133 drv->monitor_ifidx = -1;
1134 drv->monitor_sock = -1;
bbaf0837 1135 drv->ioctl_sock = -1;
9fff9fdc 1136
bbaf0837
JM
1137 if (wpa_driver_nl80211_init_nl(drv, ctx)) {
1138 os_free(drv);
1139 return NULL;
1140 }
9fff9fdc 1141
3f5285e8
JM
1142 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1143 if (drv->ioctl_sock < 0) {
1144 perror("socket(PF_INET,SOCK_DGRAM)");
bbaf0837 1145 goto failed;
3f5285e8
JM
1146 }
1147
08063178
JM
1148 cfg = os_zalloc(sizeof(*cfg));
1149 if (cfg == NULL)
1150 goto failed;
1151 cfg->ctx = drv;
1152 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
1153 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
1154 drv->netlink = netlink_init(cfg);
1155 if (drv->netlink == NULL) {
1156 os_free(cfg);
1157 goto failed;
1158 }
1159 if (wpa_driver_nl80211_finish_drv_init(drv))
bbaf0837 1160 goto failed;
7524cfb1
JM
1161
1162 return drv;
1163
bbaf0837 1164failed:
08063178 1165 netlink_deinit(drv->netlink);
bbaf0837
JM
1166 if (drv->ioctl_sock >= 0)
1167 close(drv->ioctl_sock);
1168
7524cfb1 1169 genl_family_put(drv->nl80211);
7524cfb1 1170 nl_cache_free(drv->nl_cache);
7524cfb1 1171 nl_handle_destroy(drv->nl_handle);
7524cfb1 1172 nl_cb_put(drv->nl_cb);
bbaf0837 1173
7524cfb1
JM
1174 os_free(drv);
1175 return NULL;
1176}
1177
1178
362f781e 1179static int
7524cfb1
JM
1180wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
1181{
a87c9d96
JM
1182 drv->ifindex = if_nametoindex(drv->ifname);
1183
bbaf0837 1184#ifndef HOSTAPD
ad1e68e6 1185 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0) {
a87c9d96
JM
1186 wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
1187 "use managed mode");
1188 }
1189
2135f224
JM
1190 if (hostapd_set_iface_flags(drv, drv->ifname, 1)) {
1191 wpa_printf(MSG_ERROR, "Could not set interface '%s' "
1192 "UP", drv->ifname);
362f781e
JM
1193 return -1;
1194 }
3f5285e8 1195
93d11400
ZY
1196 if (wpa_driver_nl80211_capa(drv))
1197 return -1;
80bc75f1 1198
08063178 1199 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
e2d02c29 1200 1, IF_OPER_DORMANT);
bbaf0837 1201#endif /* HOSTAPD */
362f781e
JM
1202
1203 return 0;
3f5285e8
JM
1204}
1205
1206
bbaf0837
JM
1207#ifdef HOSTAPD
1208static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
1209{
1210 struct i802_bss *bss, *prev;
1211 bss = drv->bss.next;
1212 while (bss) {
1213 prev = bss;
1214 bss = bss->next;
1215 os_free(bss);
1216 }
1217}
1218#endif /* HOSTAPD */
1219
1220
8a27af5c
JM
1221static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
1222{
1223 struct nl_msg *msg;
1224
1225 msg = nlmsg_alloc();
1226 if (!msg)
1227 return -ENOMEM;
1228
1229 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1230 0, NL80211_CMD_DEL_BEACON, 0);
1231 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1232
1233 return send_and_recv_msgs(drv, msg, NULL, NULL);
1234 nla_put_failure:
1235 return -ENOBUFS;
1236}
8a27af5c
JM
1237
1238
3f5285e8 1239/**
7e5ba1b9
JM
1240 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1241 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
1242 *
1243 * Shut down driver interface and processing of driver events. Free
1244 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1245 */
7e5ba1b9 1246static void wpa_driver_nl80211_deinit(void *priv)
3f5285e8
JM
1247{
1248 struct wpa_driver_nl80211_data *drv = priv;
3f5285e8 1249
460456f8 1250 nl80211_remove_monitor_interface(drv);
0915d02c
JM
1251 if (drv->monitor_sock >= 0) {
1252 eloop_unregister_read_sock(drv->monitor_sock);
1253 close(drv->monitor_sock);
1254 }
8a27af5c
JM
1255
1256 if (drv->nlmode == NL80211_IFTYPE_AP)
1257 wpa_driver_nl80211_del_beacon(drv);
0915d02c 1258
bbaf0837
JM
1259#ifdef HOSTAPD
1260 if (drv->last_freq_ht) {
1261 /* Clear HT flags from the driver */
1262 struct hostapd_freq_params freq;
1263 os_memset(&freq, 0, sizeof(freq));
1264 freq.freq = drv->last_freq;
1265 i802_set_freq(priv, &freq);
1266 }
1267
bbaf0837
JM
1268 if (drv->eapol_sock >= 0) {
1269 eloop_unregister_read_sock(drv->eapol_sock);
1270 close(drv->eapol_sock);
1271 }
1272
1273 if (drv->if_indices != drv->default_if_indices)
1274 os_free(drv->if_indices);
1275
1276 wpa_driver_nl80211_free_bss(drv);
1b648c7e 1277#endif /* HOSTAPD */
3f5285e8 1278
08063178
JM
1279 netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
1280 netlink_deinit(drv->netlink);
3f5285e8 1281
bbaf0837
JM
1282 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
1283
1284 (void) hostapd_set_iface_flags(drv, drv->ifname, 0);
ad1e68e6 1285 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA);
3f5285e8 1286
bbaf0837
JM
1287 if (drv->ioctl_sock >= 0)
1288 close(drv->ioctl_sock);
3f5285e8 1289
335ce76b 1290 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
3f5285e8
JM
1291 genl_family_put(drv->nl80211);
1292 nl_cache_free(drv->nl_cache);
335ce76b 1293 nl_cache_free(drv->nl_cache_event);
3f5285e8 1294 nl_handle_destroy(drv->nl_handle);
335ce76b 1295 nl_handle_destroy(drv->nl_handle_event);
3f5285e8
JM
1296 nl_cb_put(drv->nl_cb);
1297
1298 os_free(drv);
1299}
1300
1301
1302/**
1303 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
ad1e68e6 1304 * @eloop_ctx: Driver private data
3f5285e8
JM
1305 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1306 *
1307 * This function can be used as registered timeout when starting a scan to
1308 * generate a scan completed event if the driver does not report this.
1309 */
1310static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1311{
ad1e68e6
JM
1312 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1313 if (drv->ap_scan_as_station) {
1314 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
1315 drv->ap_scan_as_station = 0;
1316 }
3f5285e8
JM
1317 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1318 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1319}
1320
1321
1322/**
1323 * wpa_driver_nl80211_scan - Request the driver to initiate scan
ad1e68e6 1324 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
6a1063e0 1325 * @params: Scan parameters
3f5285e8
JM
1326 * Returns: 0 on success, -1 on failure
1327 */
6a1063e0
JM
1328static int wpa_driver_nl80211_scan(void *priv,
1329 struct wpa_driver_scan_params *params)
3f5285e8
JM
1330{
1331 struct wpa_driver_nl80211_data *drv = priv;
3f5285e8 1332 int ret = 0, timeout;
d3a98225 1333 struct nl_msg *msg, *ssids, *freqs;
6a1063e0 1334 size_t i;
3f5285e8 1335
0e75527f
JM
1336 msg = nlmsg_alloc();
1337 ssids = nlmsg_alloc();
d3a98225
JM
1338 freqs = nlmsg_alloc();
1339 if (!msg || !ssids || !freqs) {
0e75527f
JM
1340 nlmsg_free(msg);
1341 nlmsg_free(ssids);
d3a98225 1342 nlmsg_free(freqs);
3f5285e8
JM
1343 return -1;
1344 }
1345
0e75527f
JM
1346 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1347 NL80211_CMD_TRIGGER_SCAN, 0);
1348
1349 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3f5285e8 1350
6a1063e0
JM
1351 for (i = 0; i < params->num_ssids; i++) {
1352 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
1353 params->ssids[i].ssid);
3f5285e8 1354 }
6a1063e0
JM
1355 if (params->num_ssids)
1356 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3f5285e8 1357
d173df52
JM
1358 if (params->extra_ies) {
1359 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
1360 params->extra_ies);
1361 }
1362
d3a98225
JM
1363 if (params->freqs) {
1364 for (i = 0; params->freqs[i]; i++)
1365 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
1366 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
1367 }
1368
0e75527f
JM
1369 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1370 msg = NULL;
1371 if (ret) {
1372 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
1373 "(%s)", ret, strerror(-ret));
ad1e68e6
JM
1374#ifdef HOSTAPD
1375 if (drv->nlmode == NL80211_IFTYPE_AP) {
1376 /*
1377 * mac80211 does not allow scan requests in AP mode, so
1378 * try to do this in station mode.
1379 */
1380 if (wpa_driver_nl80211_set_mode(drv,
1381 IEEE80211_MODE_INFRA))
1382 goto nla_put_failure;
1383
1384 if (wpa_driver_nl80211_scan(drv, params)) {
1385 wpa_driver_nl80211_set_mode(drv,
1386 IEEE80211_MODE_AP);
1387 goto nla_put_failure;
1388 }
1389
1390 /* Restore AP mode when processing scan results */
1391 drv->ap_scan_as_station = 1;
1392 ret = 0;
1393 } else
1394 goto nla_put_failure;
1395#else /* HOSTAPD */
0e75527f 1396 goto nla_put_failure;
ad1e68e6 1397#endif /* HOSTAPD */
3f5285e8
JM
1398 }
1399
1400 /* Not all drivers generate "scan completed" wireless event, so try to
1401 * read results after a timeout. */
0e75527f 1402 timeout = 10;
3f5285e8
JM
1403 if (drv->scan_complete_events) {
1404 /*
d173df52
JM
1405 * The driver seems to deliver events to notify when scan is
1406 * complete, so use longer timeout to avoid race conditions
1407 * with scanning and following association request.
3f5285e8
JM
1408 */
1409 timeout = 30;
1410 }
1411 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
1412 "seconds", ret, timeout);
1413 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
0e75527f
JM
1414 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
1415 drv, drv->ctx);
3f5285e8 1416
0e75527f
JM
1417nla_put_failure:
1418 nlmsg_free(ssids);
1419 nlmsg_free(msg);
d3a98225 1420 nlmsg_free(freqs);
3f5285e8
JM
1421 return ret;
1422}
1423
1424
b3db1e1c 1425static int bss_info_handler(struct nl_msg *msg, void *arg)
3f5285e8 1426{
b3db1e1c
JM
1427 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1428 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1429 struct nlattr *bss[NL80211_BSS_MAX + 1];
1430 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1431 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1432 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1433 [NL80211_BSS_TSF] = { .type = NLA_U64 },
1434 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
1435 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
1436 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1437 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
1438 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
e6b8efeb 1439 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
b3ad11bb 1440 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
b3db1e1c
JM
1441 };
1442 struct wpa_scan_results *res = arg;
3f5285e8
JM
1443 struct wpa_scan_res **tmp;
1444 struct wpa_scan_res *r;
b3db1e1c
JM
1445 const u8 *ie;
1446 size_t ie_len;
3f5285e8 1447
b3db1e1c
JM
1448 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1449 genlmsg_attrlen(gnlh, 0), NULL);
1450 if (!tb[NL80211_ATTR_BSS])
1451 return NL_SKIP;
1452 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1453 bss_policy))
1454 return NL_SKIP;
1455 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1456 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1457 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1458 } else {
1459 ie = NULL;
1460 ie_len = 0;
1461 }
3f5285e8 1462
b3db1e1c 1463 r = os_zalloc(sizeof(*r) + ie_len);
3f5285e8 1464 if (r == NULL)
b3db1e1c
JM
1465 return NL_SKIP;
1466 if (bss[NL80211_BSS_BSSID])
1467 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
1468 ETH_ALEN);
1469 if (bss[NL80211_BSS_FREQUENCY])
1470 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1471 if (bss[NL80211_BSS_BEACON_INTERVAL])
1472 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
1473 if (bss[NL80211_BSS_CAPABILITY])
1474 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
7c2849d2
JM
1475 r->flags |= WPA_SCAN_NOISE_INVALID;
1476 if (bss[NL80211_BSS_SIGNAL_MBM]) {
b3db1e1c 1477 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
7c2849d2
JM
1478 r->level /= 100; /* mBm to dBm */
1479 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
1480 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
1481 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
1482 r->flags |= WPA_SCAN_LEVEL_INVALID;
1483 } else
1484 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
b3db1e1c
JM
1485 if (bss[NL80211_BSS_TSF])
1486 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
b3ad11bb
JM
1487 if (bss[NL80211_BSS_SEEN_MS_AGO])
1488 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
b3db1e1c
JM
1489 r->ie_len = ie_len;
1490 if (ie)
1491 os_memcpy(r + 1, ie, ie_len);
3f5285e8 1492
e6b8efeb
JM
1493 if (bss[NL80211_BSS_STATUS]) {
1494 enum nl80211_bss_status status;
1495 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1496 switch (status) {
1497 case NL80211_BSS_STATUS_AUTHENTICATED:
1498 r->flags |= WPA_SCAN_AUTHENTICATED;
1499 break;
1500 case NL80211_BSS_STATUS_ASSOCIATED:
1501 r->flags |= WPA_SCAN_ASSOCIATED;
1502 break;
1503 default:
1504 break;
1505 }
1506 }
1507
3f5285e8
JM
1508 tmp = os_realloc(res->res,
1509 (res->num + 1) * sizeof(struct wpa_scan_res *));
1510 if (tmp == NULL) {
1511 os_free(r);
b3db1e1c 1512 return NL_SKIP;
3f5285e8
JM
1513 }
1514 tmp[res->num++] = r;
1515 res->res = tmp;
b3db1e1c
JM
1516
1517 return NL_SKIP;
3f5285e8 1518}
b3db1e1c 1519
3f5285e8 1520
d72aad94
JM
1521static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
1522 const u8 *addr)
1523{
1524 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1525 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
1526 "mismatch");
1527 wpa_driver_nl80211_mlme(drv, addr,
1528 NL80211_CMD_DEAUTHENTICATE,
1529 WLAN_REASON_PREV_AUTH_NOT_VALID);
1530 }
1531}
1532
1533
e6b8efeb
JM
1534static void wpa_driver_nl80211_check_bss_status(
1535 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
1536{
1537 size_t i;
1538
1539 for (i = 0; i < res->num; i++) {
1540 struct wpa_scan_res *r = res->res[i];
1541 if (r->flags & WPA_SCAN_AUTHENTICATED) {
1542 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
1543 "indicates BSS status with " MACSTR
1544 " as authenticated",
1545 MAC2STR(r->bssid));
1546 if (drv->nlmode == NL80211_IFTYPE_STATION &&
1547 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
1548 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
1549 0) {
1550 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
1551 " in local state (auth=" MACSTR
1552 " assoc=" MACSTR ")",
1553 MAC2STR(drv->auth_bssid),
1554 MAC2STR(drv->bssid));
1555 }
1556 }
1557
1558 if (r->flags & WPA_SCAN_ASSOCIATED) {
1559 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
1560 "indicate BSS status with " MACSTR
1561 " as associated",
1562 MAC2STR(r->bssid));
1563 if (drv->nlmode == NL80211_IFTYPE_STATION &&
1564 !drv->associated) {
1565 wpa_printf(MSG_DEBUG, "nl80211: Local state "
1566 "(not associated) does not match "
1567 "with BSS state");
d72aad94 1568 clear_state_mismatch(drv, r->bssid);
e6b8efeb
JM
1569 } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
1570 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
1571 0) {
1572 wpa_printf(MSG_DEBUG, "nl80211: Local state "
1573 "(associated with " MACSTR ") does "
1574 "not match with BSS state",
d72aad94
JM
1575 MAC2STR(drv->bssid));
1576 clear_state_mismatch(drv, r->bssid);
1577 clear_state_mismatch(drv, drv->bssid);
e6b8efeb
JM
1578 }
1579 }
1580 }
1581}
1582
1583
3f5285e8
JM
1584/**
1585 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1586 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1587 * Returns: Scan results on success, -1 on failure
1588 */
7e5ba1b9
JM
1589static struct wpa_scan_results *
1590wpa_driver_nl80211_get_scan_results(void *priv)
3f5285e8
JM
1591{
1592 struct wpa_driver_nl80211_data *drv = priv;
b3db1e1c 1593 struct nl_msg *msg;
3f5285e8 1594 struct wpa_scan_results *res;
b3db1e1c 1595 int ret;
3f5285e8
JM
1596
1597 res = os_zalloc(sizeof(*res));
b3db1e1c 1598 if (res == NULL)
8e2c104f 1599 return NULL;
b3db1e1c
JM
1600 msg = nlmsg_alloc();
1601 if (!msg)
1602 goto nla_put_failure;
3f5285e8 1603
b3db1e1c
JM
1604 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
1605 NL80211_CMD_GET_SCAN, 0);
1606 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3f5285e8 1607
b3db1e1c
JM
1608 ret = send_and_recv_msgs(drv, msg, bss_info_handler, res);
1609 msg = NULL;
1610 if (ret == 0) {
1611 wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
1612 (unsigned long) res->num);
e6b8efeb 1613 wpa_driver_nl80211_check_bss_status(drv, res);
b3db1e1c 1614 return res;
3f5285e8 1615 }
b3db1e1c
JM
1616 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1617 "(%s)", ret, strerror(-ret));
1618nla_put_failure:
1619 nlmsg_free(msg);
1620 wpa_scan_results_free(res);
1621 return NULL;
3f5285e8
JM
1622}
1623
1624
642187d6 1625static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
71934751
JM
1626 enum wpa_alg alg, const u8 *addr,
1627 int key_idx, int set_tx,
642187d6
JM
1628 const u8 *seq, size_t seq_len,
1629 const u8 *key, size_t key_len)
3f5285e8 1630{
642187d6
JM
1631 struct wpa_driver_nl80211_data *drv = priv;
1632 int ifindex = if_nametoindex(ifname);
3f5285e8 1633 struct nl_msg *msg;
1ad1cdc2 1634 int ret;
3f5285e8 1635
1ad1cdc2
JM
1636 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1637 "set_tx=%d seq_len=%lu key_len=%lu",
1638 __func__, ifindex, alg, addr, key_idx, set_tx,
3f5285e8
JM
1639 (unsigned long) seq_len, (unsigned long) key_len);
1640
1641 msg = nlmsg_alloc();
1ad1cdc2
JM
1642 if (!msg)
1643 return -ENOMEM;
3f5285e8
JM
1644
1645 if (alg == WPA_ALG_NONE) {
1ad1cdc2
JM
1646 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1647 0, NL80211_CMD_DEL_KEY, 0);
3f5285e8 1648 } else {
1ad1cdc2
JM
1649 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1650 0, NL80211_CMD_NEW_KEY, 0);
3f5285e8 1651 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
d723bab4
JM
1652 switch (alg) {
1653 case WPA_ALG_WEP:
1654 if (key_len == 5)
1655 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1656 WLAN_CIPHER_SUITE_WEP40);
1657 else
1658 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1659 WLAN_CIPHER_SUITE_WEP104);
1660 break;
1661 case WPA_ALG_TKIP:
1662 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1663 WLAN_CIPHER_SUITE_TKIP);
1664 break;
1665 case WPA_ALG_CCMP:
1666 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1667 WLAN_CIPHER_SUITE_CCMP);
1668 break;
1669 case WPA_ALG_IGTK:
1670 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1671 WLAN_CIPHER_SUITE_AES_CMAC);
1672 break;
1673 default:
1674 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
1675 "algorithm %d", __func__, alg);
3f5285e8
JM
1676 nlmsg_free(msg);
1677 return -1;
1678 }
1679 }
1680
849ef835 1681 if (seq && seq_len)
1ad1cdc2
JM
1682 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
1683
3f5285e8
JM
1684 if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1685 {
1686 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1687 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1688 }
1689 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1ad1cdc2 1690 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3f5285e8 1691
1ad1cdc2
JM
1692 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1693 if (ret == -ENOENT && alg == WPA_ALG_NONE)
1694 ret = 0;
1695 if (ret)
1696 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
1697 ret, strerror(-ret));
3f5285e8 1698
1ad1cdc2
JM
1699 /*
1700 * If we failed or don't need to set the default TX key (below),
1701 * we're done here.
1702 */
1703 if (ret || !set_tx || alg == WPA_ALG_NONE)
1704 return ret;
1705#ifdef HOSTAPD /* FIX: is this needed? */
1706 if (addr)
1707 return ret;
1708#endif /* HOSTAPD */
3f5285e8 1709
1ad1cdc2
JM
1710 msg = nlmsg_alloc();
1711 if (!msg)
1712 return -ENOMEM;
3f5285e8 1713
1ad1cdc2
JM
1714 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1715 0, NL80211_CMD_SET_KEY, 0);
1716 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1717 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
1718 if (alg == WPA_ALG_IGTK)
1719 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
1720 else
1721 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
3f5285e8 1722
1ad1cdc2
JM
1723 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1724 if (ret == -ENOENT)
1725 ret = 0;
1726 if (ret)
1727 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
1728 "err=%d %s)", ret, strerror(-ret));
1729 return ret;
3f5285e8
JM
1730
1731nla_put_failure:
6241fcb1 1732 return -ENOBUFS;
3f5285e8
JM
1733}
1734
1735
71934751 1736static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
1737 int key_idx, int defkey,
1738 const u8 *seq, size_t seq_len,
1739 const u8 *key, size_t key_len)
1740{
1741 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
1742 if (!key_attr)
1743 return -1;
1744
1745 if (defkey && alg == WPA_ALG_IGTK)
1746 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
1747 else if (defkey)
1748 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
1749
1750 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
1751
d723bab4
JM
1752 switch (alg) {
1753 case WPA_ALG_WEP:
1754 if (key_len == 5)
2aa5f847
JM
1755 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1756 WLAN_CIPHER_SUITE_WEP40);
d723bab4 1757 else
2aa5f847
JM
1758 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1759 WLAN_CIPHER_SUITE_WEP104);
d723bab4
JM
1760 break;
1761 case WPA_ALG_TKIP:
2aa5f847 1762 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
d723bab4
JM
1763 break;
1764 case WPA_ALG_CCMP:
2aa5f847 1765 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
d723bab4
JM
1766 break;
1767 case WPA_ALG_IGTK:
2aa5f847
JM
1768 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1769 WLAN_CIPHER_SUITE_AES_CMAC);
d723bab4
JM
1770 break;
1771 default:
1772 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
1773 "algorithm %d", __func__, alg);
0194fedb 1774 return -1;
d723bab4 1775 }
0194fedb
JB
1776
1777 if (seq && seq_len)
1778 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
1779
1780 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
1781
1782 nla_nest_end(msg, key_attr);
1783
1784 return 0;
1785 nla_put_failure:
1786 return -1;
1787}
1788
c811d5bc 1789
cfaab580
ZY
1790static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
1791 struct nl_msg *msg)
1792{
1793 int i, privacy = 0;
1794 struct nlattr *nl_keys, *nl_key;
1795
1796 for (i = 0; i < 4; i++) {
1797 if (!params->wep_key[i])
1798 continue;
1799 privacy = 1;
1800 break;
1801 }
1802 if (!privacy)
1803 return 0;
1804
1805 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
1806
1807 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
1808 if (!nl_keys)
1809 goto nla_put_failure;
1810
1811 for (i = 0; i < 4; i++) {
1812 if (!params->wep_key[i])
1813 continue;
1814
1815 nl_key = nla_nest_start(msg, i);
1816 if (!nl_key)
1817 goto nla_put_failure;
1818
1819 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
1820 params->wep_key[i]);
1821 if (params->wep_key_len[i] == 5)
1822 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1823 WLAN_CIPHER_SUITE_WEP40);
1824 else
1825 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1826 WLAN_CIPHER_SUITE_WEP104);
1827
1828 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
1829
1830 if (i == params->wep_tx_keyidx)
1831 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
1832
1833 nla_nest_end(msg, nl_key);
1834 }
1835 nla_nest_end(msg, nl_keys);
1836
1837 return 0;
1838
1839nla_put_failure:
1840 return -ENOBUFS;
1841}
1842
1843
c2a04078
JM
1844static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
1845 const u8 *addr, int cmd, u16 reason_code)
1846{
1847 int ret = -1;
1848 struct nl_msg *msg;
1849
1850 msg = nlmsg_alloc();
1851 if (!msg)
1852 return -1;
1853
1854 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
1855
1856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1857 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
1858 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1859
1860 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1861 msg = NULL;
1862 if (ret) {
1863 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
1864 "(%s)", ret, strerror(-ret));
1865 goto nla_put_failure;
1866 }
1867 ret = 0;
1868
1869nla_put_failure:
1870 nlmsg_free(msg);
1871 return ret;
1872}
3f5285e8
JM
1873
1874
cfaab580
ZY
1875static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
1876 const u8 *addr, int reason_code)
1877{
1878 wpa_printf(MSG_DEBUG, "%s", __func__);
1879 drv->associated = 0;
1880 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
1881 reason_code);
1882}
1883
1884
3f5285e8 1885static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
c2a04078 1886 int reason_code)
3f5285e8
JM
1887{
1888 struct wpa_driver_nl80211_data *drv = priv;
cfaab580
ZY
1889 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
1890 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
c2a04078 1891 wpa_printf(MSG_DEBUG, "%s", __func__);
13405f35 1892 drv->associated = 0;
c2a04078
JM
1893 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
1894 reason_code);
3f5285e8
JM
1895}
1896
1897
1898static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
c2a04078 1899 int reason_code)
3f5285e8
JM
1900{
1901 struct wpa_driver_nl80211_data *drv = priv;
cfaab580
ZY
1902 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
1903 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
c2a04078 1904 wpa_printf(MSG_DEBUG, "%s", __func__);
13405f35 1905 drv->associated = 0;
c2a04078
JM
1906 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
1907 reason_code);
3f5285e8
JM
1908}
1909
1910
c2a04078
JM
1911static int wpa_driver_nl80211_authenticate(
1912 void *priv, struct wpa_driver_auth_params *params)
1913{
1914 struct wpa_driver_nl80211_data *drv = priv;
a0b2f99b 1915 int ret = -1, i;
c2a04078
JM
1916 struct nl_msg *msg;
1917 enum nl80211_auth_type type;
6d6f4bb8 1918 int count = 0;
c2a04078
JM
1919
1920 drv->associated = 0;
e6b8efeb 1921 os_memset(drv->auth_bssid, 0, ETH_ALEN);
c2a04078 1922
4a867032
JM
1923 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0)
1924 return -1;
1925
6d6f4bb8 1926retry:
c2a04078
JM
1927 msg = nlmsg_alloc();
1928 if (!msg)
1929 return -1;
1930
1931 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
1932 drv->ifindex);
a0b2f99b 1933
0194fedb
JB
1934 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1935 NL80211_CMD_AUTHENTICATE, 0);
1936
a0b2f99b
JM
1937 for (i = 0; i < 4; i++) {
1938 if (!params->wep_key[i])
1939 continue;
642187d6
JM
1940 wpa_driver_nl80211_set_key(drv->ifname, drv, WPA_ALG_WEP, NULL,
1941 i,
a0b2f99b
JM
1942 i == params->wep_tx_keyidx, NULL, 0,
1943 params->wep_key[i],
1944 params->wep_key_len[i]);
0194fedb
JB
1945 if (params->wep_tx_keyidx != i)
1946 continue;
1947 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
1948 params->wep_key[i], params->wep_key_len[i])) {
1949 nlmsg_free(msg);
1950 return -1;
1951 }
a0b2f99b
JM
1952 }
1953
c2a04078
JM
1954 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1955 if (params->bssid) {
1956 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
1957 MAC2STR(params->bssid));
1958 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
1959 }
1960 if (params->freq) {
1961 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
1962 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
1963 }
1964 if (params->ssid) {
1965 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
1966 params->ssid, params->ssid_len);
1967 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
1968 params->ssid);
1969 }
1970 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
1971 if (params->ie)
1972 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
1973 /*
1974 * TODO: if multiple auth_alg options enabled, try them one by one if
1975 * the AP rejects authentication due to unknown auth alg
1976 */
1977 if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
1978 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1979 else if (params->auth_alg & AUTH_ALG_SHARED_KEY)
1980 type = NL80211_AUTHTYPE_SHARED_KEY;
1981 else if (params->auth_alg & AUTH_ALG_LEAP)
1982 type = NL80211_AUTHTYPE_NETWORK_EAP;
1983 else if (params->auth_alg & AUTH_ALG_FT)
1984 type = NL80211_AUTHTYPE_FT;
1985 else
1986 goto nla_put_failure;
1987 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
1988 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
1989
1990 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1991 msg = NULL;
1992 if (ret) {
1993 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
1994 "(%s)", ret, strerror(-ret));
6d6f4bb8
JM
1995 count++;
1996 if (ret == -EALREADY && count == 1 && params->bssid) {
1997 /*
1998 * mac80211 does not currently accept new
1999 * authentication if we are already authenticated. As a
2000 * workaround, force deauthentication and try again.
2001 */
2002 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2003 "after forced deauthentication");
2004 wpa_driver_nl80211_deauthenticate(
2005 drv, params->bssid,
2006 WLAN_REASON_PREV_AUTH_NOT_VALID);
2007 nlmsg_free(msg);
2008 goto retry;
2009 }
c2a04078
JM
2010 goto nla_put_failure;
2011 }
2012 ret = 0;
2013 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
2014 "successfully");
2015
2016nla_put_failure:
2017 nlmsg_free(msg);
2018 return ret;
2019}
2020
2021
282d5590
JM
2022struct phy_info_arg {
2023 u16 *num_modes;
2024 struct hostapd_hw_modes *modes;
2025};
2026
2027static int phy_info_handler(struct nl_msg *msg, void *arg)
2028{
2029 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2030 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2031 struct phy_info_arg *phy_info = arg;
2032
2033 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
2034
2035 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
2036 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2037 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2038 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2039 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
2040 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
2041 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2042 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2043 };
2044
2045 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
2046 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
2047 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
2048 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
2049 };
2050
2051 struct nlattr *nl_band;
2052 struct nlattr *nl_freq;
2053 struct nlattr *nl_rate;
2054 int rem_band, rem_freq, rem_rate;
2055 struct hostapd_hw_modes *mode;
2056 int idx, mode_is_set;
2057
2058 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2059 genlmsg_attrlen(gnlh, 0), NULL);
2060
2061 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
2062 return NL_SKIP;
2063
2064 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
e62fb0a0 2065 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
282d5590
JM
2066 if (!mode)
2067 return NL_SKIP;
2068 phy_info->modes = mode;
2069
2070 mode_is_set = 0;
2071
2072 mode = &phy_info->modes[*(phy_info->num_modes)];
2073 memset(mode, 0, sizeof(*mode));
2074 *(phy_info->num_modes) += 1;
2075
2076 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
2077 nla_len(nl_band), NULL);
2078
2079 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
2080 mode->ht_capab = nla_get_u16(
2081 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
2082 }
2083
be8eb8ab
JM
2084 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
2085 mode->a_mpdu_params |= nla_get_u8(
2086 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
2087 0x03;
2088 }
2089
2090 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
2091 mode->a_mpdu_params |= nla_get_u8(
2092 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
2093 2;
2094 }
2095
08eb154d
JM
2096 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
2097 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
2098 u8 *mcs;
2099 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
2100 os_memcpy(mode->mcs_set, mcs, 16);
2101 }
2102
282d5590
JM
2103 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
2104 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
2105 nla_len(nl_freq), freq_policy);
2106 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
2107 continue;
2108 mode->num_channels++;
2109 }
2110
e62fb0a0 2111 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
282d5590
JM
2112 if (!mode->channels)
2113 return NL_SKIP;
2114
2115 idx = 0;
2116
2117 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
2118 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
2119 nla_len(nl_freq), freq_policy);
2120 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
2121 continue;
2122
2123 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
2124 mode->channels[idx].flag = 0;
2125
2126 if (!mode_is_set) {
2127 /* crude heuristic */
2128 if (mode->channels[idx].freq < 4000)
2129 mode->mode = HOSTAPD_MODE_IEEE80211B;
2130 else
2131 mode->mode = HOSTAPD_MODE_IEEE80211A;
2132 mode_is_set = 1;
2133 }
2134
2135 /* crude heuristic */
2136 if (mode->channels[idx].freq < 4000)
5a0ffb5f 2137 if (mode->channels[idx].freq == 2484)
282d5590
JM
2138 mode->channels[idx].chan = 14;
2139 else
2140 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
2141 else
2142 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
2143
2144 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
2145 mode->channels[idx].flag |=
2146 HOSTAPD_CHAN_DISABLED;
2147 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
2148 mode->channels[idx].flag |=
2149 HOSTAPD_CHAN_PASSIVE_SCAN;
2150 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
2151 mode->channels[idx].flag |=
2152 HOSTAPD_CHAN_NO_IBSS;
2153 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
2154 mode->channels[idx].flag |=
2155 HOSTAPD_CHAN_RADAR;
2156
2157 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
2158 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
2159 mode->channels[idx].max_tx_power =
2160 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
2161
2162 idx++;
2163 }
2164
2165 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
2166 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
2167 nla_len(nl_rate), rate_policy);
2168 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
2169 continue;
2170 mode->num_rates++;
2171 }
2172
e62fb0a0 2173 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
282d5590
JM
2174 if (!mode->rates)
2175 return NL_SKIP;
2176
2177 idx = 0;
2178
2179 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
2180 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
2181 nla_len(nl_rate), rate_policy);
2182 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
2183 continue;
fb7842aa 2184 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
282d5590
JM
2185
2186 /* crude heuristic */
2187 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
fb7842aa 2188 mode->rates[idx] > 200)
282d5590
JM
2189 mode->mode = HOSTAPD_MODE_IEEE80211G;
2190
282d5590
JM
2191 idx++;
2192 }
2193 }
2194
2195 return NL_SKIP;
2196}
2197
2198static struct hostapd_hw_modes *
2199wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
2200{
2201 u16 m;
2202 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
2203 int i, mode11g_idx = -1;
2204
2205 /* If only 802.11g mode is included, use it to construct matching
2206 * 802.11b mode data. */
2207
2208 for (m = 0; m < *num_modes; m++) {
2209 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
2210 return modes; /* 802.11b already included */
2211 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
2212 mode11g_idx = m;
2213 }
2214
2215 if (mode11g_idx < 0)
2216 return modes; /* 2.4 GHz band not supported at all */
2217
2218 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
2219 if (nmodes == NULL)
2220 return modes; /* Could not add 802.11b mode */
2221
2222 mode = &nmodes[*num_modes];
2223 os_memset(mode, 0, sizeof(*mode));
2224 (*num_modes)++;
2225 modes = nmodes;
2226
2227 mode->mode = HOSTAPD_MODE_IEEE80211B;
2228
2229 mode11g = &modes[mode11g_idx];
2230 mode->num_channels = mode11g->num_channels;
2231 mode->channels = os_malloc(mode11g->num_channels *
2232 sizeof(struct hostapd_channel_data));
2233 if (mode->channels == NULL) {
2234 (*num_modes)--;
2235 return modes; /* Could not add 802.11b mode */
2236 }
2237 os_memcpy(mode->channels, mode11g->channels,
2238 mode11g->num_channels * sizeof(struct hostapd_channel_data));
2239
2240 mode->num_rates = 0;
fb7842aa 2241 mode->rates = os_malloc(4 * sizeof(int));
282d5590
JM
2242 if (mode->rates == NULL) {
2243 os_free(mode->channels);
2244 (*num_modes)--;
2245 return modes; /* Could not add 802.11b mode */
2246 }
2247
2248 for (i = 0; i < mode11g->num_rates; i++) {
fb7842aa
JM
2249 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
2250 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
282d5590
JM
2251 continue;
2252 mode->rates[mode->num_rates] = mode11g->rates[i];
2253 mode->num_rates++;
2254 if (mode->num_rates == 4)
2255 break;
2256 }
2257
2258 if (mode->num_rates == 0) {
2259 os_free(mode->channels);
2260 os_free(mode->rates);
2261 (*num_modes)--;
2262 return modes; /* No 802.11b rates */
2263 }
2264
2265 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
2266 "information");
2267
2268 return modes;
2269}
2270
2271
2272static struct hostapd_hw_modes *
2273wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
2274{
2275 struct wpa_driver_nl80211_data *drv = priv;
2276 struct nl_msg *msg;
2277 struct phy_info_arg result = {
2278 .num_modes = num_modes,
2279 .modes = NULL,
2280 };
2281
2282 *num_modes = 0;
2283 *flags = 0;
2284
2285 msg = nlmsg_alloc();
2286 if (!msg)
2287 return NULL;
2288
2289 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2290 0, NL80211_CMD_GET_WIPHY, 0);
2291
2292 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2293
2294 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
2295 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
2296 nla_put_failure:
2297 return NULL;
2298}
2299
2300
2c2010ac
JM
2301static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
2302 const void *data, size_t len,
2303 int encrypt)
2304{
2305 __u8 rtap_hdr[] = {
2306 0x00, 0x00, /* radiotap version */
2307 0x0e, 0x00, /* radiotap length */
2308 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2309 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
2310 0x00, /* padding */
2311 0x00, 0x00, /* RX and TX flags to indicate that */
2312 0x00, 0x00, /* this is the injected frame directly */
2313 };
2314 struct iovec iov[2] = {
2315 {
2316 .iov_base = &rtap_hdr,
2317 .iov_len = sizeof(rtap_hdr),
2318 },
2319 {
2320 .iov_base = (void *) data,
2321 .iov_len = len,
2322 }
2323 };
2324 struct msghdr msg = {
2325 .msg_name = NULL,
2326 .msg_namelen = 0,
2327 .msg_iov = iov,
2328 .msg_iovlen = 2,
2329 .msg_control = NULL,
2330 .msg_controllen = 0,
2331 .msg_flags = 0,
2332 };
2333
2334 if (encrypt)
2335 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
2336
2337 return sendmsg(drv->monitor_sock, &msg, 0);
2338}
2339
2340
2341static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
2342 size_t data_len)
2343{
2344 struct wpa_driver_nl80211_data *drv = priv;
2345 struct ieee80211_mgmt *mgmt;
7a47d567 2346 int encrypt = 1;
2c2010ac
JM
2347 u16 fc;
2348
2349 mgmt = (struct ieee80211_mgmt *) data;
2350 fc = le_to_host16(mgmt->frame_control);
2351
2352 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2353 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
2354 /*
2355 * Only one of the authentication frame types is encrypted.
2356 * In order for static WEP encryption to work properly (i.e.,
2357 * to not encrypt the frame), we need to tell mac80211 about
2358 * the frames that must not be encrypted.
2359 */
2360 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2361 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
2362 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
2363 encrypt = 0;
2c2010ac
JM
2364 }
2365
7a47d567 2366 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
2c2010ac
JM
2367}
2368
2369
5d674872
JM
2370static int wpa_driver_nl80211_set_beacon(const char *ifname, void *priv,
2371 const u8 *head, size_t head_len,
2372 const u8 *tail, size_t tail_len,
2373 int dtim_period, int beacon_int)
d2440ba0
JM
2374{
2375 struct wpa_driver_nl80211_data *drv = priv;
2376 struct nl_msg *msg;
2377 u8 cmd = NL80211_CMD_NEW_BEACON;
2378 int ret;
b4fd6fab 2379 int beacon_set;
5d674872 2380 int ifindex = if_nametoindex(ifname);
b4fd6fab
JM
2381#ifdef HOSTAPD
2382 struct i802_bss *bss;
2383
2384 bss = get_bss(drv, ifindex);
2385 if (bss == NULL)
2386 return -ENOENT;
2387 beacon_set = bss->beacon_set;
2388#else /* HOSTAPD */
2389 beacon_set = drv->beacon_set;
2390#endif /* HOSTAPD */
d2440ba0
JM
2391
2392 msg = nlmsg_alloc();
2393 if (!msg)
2394 return -ENOMEM;
2395
2396 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
2397 beacon_set);
2398 if (beacon_set)
d2440ba0
JM
2399 cmd = NL80211_CMD_SET_BEACON;
2400
2401 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2402 0, cmd, 0);
2403 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
2404 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
b4fd6fab 2405 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5d674872 2406 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
d2440ba0
JM
2407 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
2408
2409 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2410 if (ret) {
2411 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
2412 ret, strerror(-ret));
b4fd6fab
JM
2413 } else {
2414#ifdef HOSTAPD
2415 bss->beacon_set = 1;
2416#else /* HOSTAPD */
d2440ba0 2417 drv->beacon_set = 1;
b4fd6fab
JM
2418#endif /* HOSTAPD */
2419 }
d2440ba0
JM
2420 return ret;
2421 nla_put_failure:
2422 return -ENOBUFS;
2423}
2424
2425
f019981a
JM
2426static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
2427 int freq, int ht_enabled,
2428 int sec_channel_offset)
1581b38b
JM
2429{
2430 struct nl_msg *msg;
d2440ba0 2431 int ret;
1581b38b
JM
2432
2433 msg = nlmsg_alloc();
2434 if (!msg)
2435 return -1;
2436
2437 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
2438 NL80211_CMD_SET_WIPHY, 0);
2439
2440 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
f019981a
JM
2441 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
2442 if (ht_enabled) {
2443 switch (sec_channel_offset) {
2444 case -1:
2445 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2446 NL80211_CHAN_HT40MINUS);
2447 break;
2448 case 1:
2449 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2450 NL80211_CHAN_HT40PLUS);
2451 break;
2452 default:
2453 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2454 NL80211_CHAN_HT20);
2455 break;
2456 }
2457 }
1581b38b 2458
d2440ba0
JM
2459 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2460 if (ret == 0)
1581b38b 2461 return 0;
f019981a
JM
2462 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
2463 "%d (%s)", freq, ret, strerror(-ret));
1581b38b
JM
2464nla_put_failure:
2465 return -1;
2466}
2467
0f4e8b4f
JM
2468
2469static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
2470 struct hostapd_sta_add_params *params)
2471{
2472 struct wpa_driver_nl80211_data *drv = priv;
2473 struct nl_msg *msg;
2474 int ret = -ENOBUFS;
2475
2476 msg = nlmsg_alloc();
2477 if (!msg)
2478 return -ENOMEM;
2479
2480 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2481 0, NL80211_CMD_NEW_STATION, 0);
2482
2483 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(ifname));
2484 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
2485 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
2486 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
2487 params->supp_rates);
2488 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
2489 params->listen_interval);
0f4e8b4f
JM
2490 if (params->ht_capabilities) {
2491 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
fc4e2d95
JM
2492 sizeof(*params->ht_capabilities),
2493 params->ht_capabilities);
0f4e8b4f 2494 }
0f4e8b4f
JM
2495
2496 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2497 if (ret)
2498 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
2499 "result: %d (%s)", ret, strerror(-ret));
2500 if (ret == -EEXIST)
2501 ret = 0;
2502 nla_put_failure:
2503 return ret;
2504}
2505
2506
2507static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
2508{
2509 struct wpa_driver_nl80211_data *drv = priv;
2510 struct nl_msg *msg;
2511 int ret;
2512
2513 msg = nlmsg_alloc();
2514 if (!msg)
2515 return -ENOMEM;
2516
2517 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2518 0, NL80211_CMD_DEL_STATION, 0);
2519
2520 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2521 if_nametoindex(drv->ifname));
2522 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
2523
2524 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2525 if (ret == -ENOENT)
2526 return 0;
2527 return ret;
2528 nla_put_failure:
2529 return -ENOBUFS;
2530}
2531
1581b38b 2532
0915d02c
JM
2533static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
2534 int ifidx)
2535{
2536 struct nl_msg *msg;
2537
c6e8e8e4
JM
2538 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
2539
2135f224
JM
2540#ifdef HOSTAPD
2541 /* stop listening for EAPOL on this interface */
2542 del_ifidx(drv, ifidx);
2543#endif /* HOSTAPD */
2544
0915d02c
JM
2545 msg = nlmsg_alloc();
2546 if (!msg)
2547 goto nla_put_failure;
2548
2549 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2550 0, NL80211_CMD_DEL_INTERFACE, 0);
2551 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
2552
2553 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
2554 return;
2555 nla_put_failure:
2556 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d).\n",
2557 ifidx);
2558}
2559
2560
a35187e7
KH
2561static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
2562 const char *ifname,
2563 enum nl80211_iftype iftype,
fbbfcbac 2564 const u8 *addr, int wds)
0915d02c
JM
2565{
2566 struct nl_msg *msg, *flags = NULL;
2567 int ifidx;
2568 int ret = -ENOBUFS;
2569
2570 msg = nlmsg_alloc();
2571 if (!msg)
2572 return -1;
2573
2574 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2575 0, NL80211_CMD_NEW_INTERFACE, 0);
2576 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2577 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
2578 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
2579
2580 if (iftype == NL80211_IFTYPE_MONITOR) {
2581 int err;
2582
2583 flags = nlmsg_alloc();
2584 if (!flags)
2585 goto nla_put_failure;
2586
2587 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
2588
2589 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
2590
2591 nlmsg_free(flags);
2592
2593 if (err)
2594 goto nla_put_failure;
fbbfcbac
FF
2595 } else if (wds) {
2596 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
0915d02c
JM
2597 }
2598
2599 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2600 if (ret) {
2601 nla_put_failure:
a35187e7
KH
2602 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
2603 ifname, ret, strerror(-ret));
0915d02c
JM
2604 return ret;
2605 }
2606
2607 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
2608 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
2609 ifname, ifidx);
0915d02c
JM
2610
2611 if (ifidx <= 0)
2612 return -1;
2613
2135f224
JM
2614#ifdef HOSTAPD
2615 /* start listening for EAPOL on this interface */
2616 add_ifidx(drv, ifidx);
2617
41d931ee
JM
2618 if (addr && iftype == NL80211_IFTYPE_AP &&
2619 set_ifhwaddr(drv, ifname, addr)) {
2620 nl80211_remove_iface(drv, ifidx);
2621 return -1;
2135f224
JM
2622 }
2623#endif /* HOSTAPD */
2624
0915d02c
JM
2625 return ifidx;
2626}
22a7c9d7
JM
2627
2628
a35187e7
KH
2629static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
2630 const char *ifname, enum nl80211_iftype iftype,
fbbfcbac 2631 const u8 *addr, int wds)
a35187e7
KH
2632{
2633 int ret;
2634
fbbfcbac 2635 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
a35187e7
KH
2636
2637 /* if error occured and interface exists already */
2638 if (ret == -ENFILE && if_nametoindex(ifname)) {
2639 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
2640
2641 /* Try to remove the interface that was already there. */
2642 nl80211_remove_iface(drv, if_nametoindex(ifname));
2643
2644 /* Try to create the interface again */
fbbfcbac
FF
2645 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
2646 wds);
a35187e7
KH
2647 }
2648
2649 return ret;
2650}
0915d02c 2651
2135f224 2652
0915d02c
JM
2653static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
2654{
2655 struct ieee80211_hdr *hdr;
f8b1f695
JM
2656 u16 fc;
2657 union wpa_event_data event;
0915d02c
JM
2658
2659 hdr = (struct ieee80211_hdr *) buf;
2660 fc = le_to_host16(hdr->frame_control);
2661
f8b1f695
JM
2662 os_memset(&event, 0, sizeof(event));
2663 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
2664 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
2665 event.tx_status.dst = hdr->addr1;
2666 event.tx_status.data = buf;
2667 event.tx_status.data_len = len;
2668 event.tx_status.ack = ok;
2669 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
0915d02c
JM
2670}
2671
2672
4b9841d3
JM
2673static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
2674 struct ieee80211_hdr *hdr, size_t len)
0915d02c 2675{
f8b1f695
JM
2676 union wpa_event_data event;
2677 os_memset(&event, 0, sizeof(event));
2678 event.rx_from_unknown.hdr = hdr;
2679 event.rx_from_unknown.len = len;
2680 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4b9841d3 2681}
0915d02c 2682
4b9841d3
JM
2683
2684static void handle_frame(struct wpa_driver_nl80211_data *drv,
2685 u8 *buf, size_t len,
2686 struct hostapd_frame_info *hfi)
2687{
2688 struct ieee80211_hdr *hdr;
f8b1f695
JM
2689 u16 fc;
2690 union wpa_event_data event;
0915d02c
JM
2691
2692 hdr = (struct ieee80211_hdr *) buf;
2693 fc = le_to_host16(hdr->frame_control);
0915d02c 2694
4b9841d3 2695 switch (WLAN_FC_GET_TYPE(fc)) {
0915d02c 2696 case WLAN_FC_TYPE_MGMT:
f8b1f695
JM
2697 os_memset(&event, 0, sizeof(event));
2698 event.rx_mgmt.frame = buf;
2699 event.rx_mgmt.frame_len = len;
2700 event.rx_mgmt.fi = hfi;
2701 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
0915d02c
JM
2702 break;
2703 case WLAN_FC_TYPE_CTRL:
2704 /* can only get here with PS-Poll frames */
2705 wpa_printf(MSG_DEBUG, "CTRL");
4b9841d3 2706 from_unknown_sta(drv, hdr, len);
0915d02c
JM
2707 break;
2708 case WLAN_FC_TYPE_DATA:
4b9841d3 2709 from_unknown_sta(drv, hdr, len);
0915d02c
JM
2710 break;
2711 }
2712}
2713
2714
2715static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
2716{
2717 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2718 int len;
2719 unsigned char buf[3000];
2720 struct ieee80211_radiotap_iterator iter;
2721 int ret;
2722 struct hostapd_frame_info hfi;
4b9841d3 2723 int injected = 0, failed = 0, rxflags = 0;
0915d02c
JM
2724
2725 len = recv(sock, buf, sizeof(buf), 0);
2726 if (len < 0) {
2727 perror("recv");
2728 return;
2729 }
2730
2731 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
2732 printf("received invalid radiotap frame\n");
2733 return;
2734 }
2735
2736 memset(&hfi, 0, sizeof(hfi));
2737
2738 while (1) {
2739 ret = ieee80211_radiotap_iterator_next(&iter);
2740 if (ret == -ENOENT)
2741 break;
2742 if (ret) {
2743 printf("received invalid radiotap frame (%d)\n", ret);
2744 return;
2745 }
2746 switch (iter.this_arg_index) {
2747 case IEEE80211_RADIOTAP_FLAGS:
2748 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
2749 len -= 4;
2750 break;
2751 case IEEE80211_RADIOTAP_RX_FLAGS:
2752 rxflags = 1;
2753 break;
2754 case IEEE80211_RADIOTAP_TX_FLAGS:
2755 injected = 1;
2756 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
2757 IEEE80211_RADIOTAP_F_TX_FAIL;
2758 break;
2759 case IEEE80211_RADIOTAP_DATA_RETRIES:
2760 break;
2761 case IEEE80211_RADIOTAP_CHANNEL:
2762 /* TODO convert from freq/flags to channel number
2763 hfi.channel = XXX;
0915d02c
JM
2764 */
2765 break;
2766 case IEEE80211_RADIOTAP_RATE:
2767 hfi.datarate = *iter.this_arg * 5;
2768 break;
2769 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
2770 hfi.ssi_signal = *iter.this_arg;
2771 break;
2772 }
2773 }
2774
2775 if (rxflags && injected)
2776 return;
2777
2778 if (!injected)
4b9841d3
JM
2779 handle_frame(drv, buf + iter.max_length,
2780 len - iter.max_length, &hfi);
0915d02c 2781 else
4b9841d3
JM
2782 handle_tx_callback(drv->ctx, buf + iter.max_length,
2783 len - iter.max_length, !failed);
0915d02c
JM
2784}
2785
2786
2787/*
2788 * we post-process the filter code later and rewrite
2789 * this to the offset to the last instruction
2790 */
2791#define PASS 0xFF
2792#define FAIL 0xFE
2793
2794static struct sock_filter msock_filter_insns[] = {
2795 /*
2796 * do a little-endian load of the radiotap length field
2797 */
2798 /* load lower byte into A */
2799 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
2800 /* put it into X (== index register) */
2801 BPF_STMT(BPF_MISC| BPF_TAX, 0),
2802 /* load upper byte into A */
2803 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
2804 /* left-shift it by 8 */
2805 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
2806 /* or with X */
2807 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
2808 /* put result into X */
2809 BPF_STMT(BPF_MISC| BPF_TAX, 0),
2810
2811 /*
2812 * Allow management frames through, this also gives us those
2813 * management frames that we sent ourselves with status
2814 */
2815 /* load the lower byte of the IEEE 802.11 frame control field */
2816 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2817 /* mask off frame type and version */
2818 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
2819 /* accept frame if it's both 0, fall through otherwise */
2820 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
2821
2822 /*
2823 * TODO: add a bit to radiotap RX flags that indicates
2824 * that the sending station is not associated, then
2825 * add a filter here that filters on our DA and that flag
2826 * to allow us to deauth frames to that bad station.
2827 *
2828 * Not a regression -- we didn't do it before either.
2829 */
2830
2831#if 0
2832 /*
fbbfcbac 2833 * drop non-data frames
0915d02c
JM
2834 */
2835 /* load the lower byte of the frame control field */
2836 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2837 /* mask off QoS bit */
2838 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
2839 /* drop non-data frames */
2840 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
fbbfcbac 2841#endif
0915d02c 2842 /* load the upper byte of the frame control field */
fbbfcbac 2843 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
0915d02c
JM
2844 /* mask off toDS/fromDS */
2845 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
fbbfcbac
FF
2846 /* accept WDS frames */
2847 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
0915d02c
JM
2848
2849 /*
2850 * add header length to index
2851 */
2852 /* load the lower byte of the frame control field */
2853 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2854 /* mask off QoS bit */
2855 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
2856 /* right shift it by 6 to give 0 or 2 */
2857 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
2858 /* add data frame header length */
2859 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
2860 /* add index, was start of 802.11 header */
2861 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
2862 /* move to index, now start of LL header */
2863 BPF_STMT(BPF_MISC | BPF_TAX, 0),
2864
2865 /*
2866 * Accept empty data frames, we use those for
2867 * polling activity.
2868 */
2869 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
2870 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
2871
2872 /*
2873 * Accept EAPOL frames
2874 */
2875 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
2876 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
2877 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
2878 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
2879
2880 /* keep these last two statements or change the code below */
2881 /* return 0 == "DROP" */
2882 BPF_STMT(BPF_RET | BPF_K, 0),
2883 /* return ~0 == "keep all" */
2884 BPF_STMT(BPF_RET | BPF_K, ~0),
2885};
2886
2887static struct sock_fprog msock_filter = {
2888 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
2889 .filter = msock_filter_insns,
2890};
2891
2892
2893static int add_monitor_filter(int s)
2894{
2895 int idx;
2896
2897 /* rewrite all PASS/FAIL jump offsets */
2898 for (idx = 0; idx < msock_filter.len; idx++) {
2899 struct sock_filter *insn = &msock_filter_insns[idx];
2900
2901 if (BPF_CLASS(insn->code) == BPF_JMP) {
2902 if (insn->code == (BPF_JMP|BPF_JA)) {
2903 if (insn->k == PASS)
2904 insn->k = msock_filter.len - idx - 2;
2905 else if (insn->k == FAIL)
2906 insn->k = msock_filter.len - idx - 3;
2907 }
2908
2909 if (insn->jt == PASS)
2910 insn->jt = msock_filter.len - idx - 2;
2911 else if (insn->jt == FAIL)
2912 insn->jt = msock_filter.len - idx - 3;
2913
2914 if (insn->jf == PASS)
2915 insn->jf = msock_filter.len - idx - 2;
2916 else if (insn->jf == FAIL)
2917 insn->jf = msock_filter.len - idx - 3;
2918 }
2919 }
2920
2921 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2922 &msock_filter, sizeof(msock_filter))) {
2923 perror("SO_ATTACH_FILTER");
2924 return -1;
2925 }
2926
2927 return 0;
2928}
2929
2930
460456f8
JM
2931static void nl80211_remove_monitor_interface(
2932 struct wpa_driver_nl80211_data *drv)
2933{
2934 if (drv->monitor_ifidx >= 0) {
2935 nl80211_remove_iface(drv, drv->monitor_ifidx);
2936 drv->monitor_ifidx = -1;
2937 }
2938}
2939
2940
0915d02c
JM
2941static int
2942nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
2943{
2944 char buf[IFNAMSIZ];
2945 struct sockaddr_ll ll;
2946 int optval;
2947 socklen_t optlen;
0915d02c
JM
2948
2949 snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
2950 buf[IFNAMSIZ - 1] = '\0';
2951
2952 drv->monitor_ifidx =
fbbfcbac
FF
2953 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
2954 0);
0915d02c
JM
2955
2956 if (drv->monitor_ifidx < 0)
2957 return -1;
2958
2135f224 2959 if (hostapd_set_iface_flags(drv, buf, 1))
0915d02c 2960 goto error;
0915d02c
JM
2961
2962 memset(&ll, 0, sizeof(ll));
2963 ll.sll_family = AF_PACKET;
2964 ll.sll_ifindex = drv->monitor_ifidx;
2965 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2966 if (drv->monitor_sock < 0) {
2967 perror("socket[PF_PACKET,SOCK_RAW]");
2968 goto error;
2969 }
2970
2971 if (add_monitor_filter(drv->monitor_sock)) {
2972 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
2973 "interface; do filtering in user space");
2974 /* This works, but will cost in performance. */
2975 }
2976
2135f224 2977 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
0915d02c
JM
2978 perror("monitor socket bind");
2979 goto error;
2980 }
2981
2982 optlen = sizeof(optval);
2983 optval = 20;
2984 if (setsockopt
2985 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
2986 perror("Failed to set socket priority");
2987 goto error;
2988 }
2989
2990 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
2991 drv, NULL)) {
2992 printf("Could not register monitor read socket\n");
2993 goto error;
2994 }
2995
2996 return 0;
2997 error:
460456f8 2998 nl80211_remove_monitor_interface(drv);
0915d02c
JM
2999 return -1;
3000}
3001
db149ac9
JM
3002
3003static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3004
3005static int wpa_driver_nl80211_hapd_send_eapol(
3006 void *priv, const u8 *addr, const u8 *data,
3007 size_t data_len, int encrypt, const u8 *own_addr)
3008{
3009 struct wpa_driver_nl80211_data *drv = priv;
3010 struct ieee80211_hdr *hdr;
3011 size_t len;
3012 u8 *pos;
3013 int res;
3014#if 0 /* FIX */
0de39516 3015 int qos = sta->flags & WPA_STA_WMM;
db149ac9
JM
3016#else
3017 int qos = 0;
3018#endif
3019
3020 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
3021 data_len;
3022 hdr = os_zalloc(len);
3023 if (hdr == NULL) {
3024 printf("malloc() failed for i802_send_data(len=%lu)\n",
3025 (unsigned long) len);
3026 return -1;
3027 }
3028
3029 hdr->frame_control =
3030 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
3031 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
3032 if (encrypt)
3033 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
3034#if 0 /* To be enabled if qos determination is added above */
3035 if (qos) {
3036 hdr->frame_control |=
3037 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
3038 }
3039#endif
3040
3041 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
3042 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
3043 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
3044 pos = (u8 *) (hdr + 1);
3045
3046#if 0 /* To be enabled if qos determination is added above */
3047 if (qos) {
3048 /* add an empty QoS header if needed */
3049 pos[0] = 0;
3050 pos[1] = 0;
3051 pos += 2;
3052 }
3053#endif
3054
3055 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
3056 pos += sizeof(rfc1042_header);
3057 WPA_PUT_BE16(pos, ETH_P_PAE);
3058 pos += 2;
3059 memcpy(pos, data, data_len);
3060
3061 res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
3062 if (res < 0) {
3063 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
3064 "failed: %d (%s)",
3065 (unsigned long) len, errno, strerror(errno));
3066 }
7bf12757 3067 os_free(hdr);
db149ac9
JM
3068
3069 return res;
3070}
3071
a8d6ffa4 3072
7e76ee9c
JM
3073static u32 sta_flags_nl80211(int flags)
3074{
3075 u32 f = 0;
3076
0de39516 3077 if (flags & WPA_STA_AUTHORIZED)
7e76ee9c 3078 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
0de39516 3079 if (flags & WPA_STA_WMM)
7e76ee9c 3080 f |= BIT(NL80211_STA_FLAG_WME);
0de39516 3081 if (flags & WPA_STA_SHORT_PREAMBLE)
7e76ee9c 3082 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
0de39516 3083 if (flags & WPA_STA_MFP)
7e76ee9c
JM
3084 f |= BIT(NL80211_STA_FLAG_MFP);
3085
3086 return f;
3087}
3088
3089
a8d6ffa4
JM
3090static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
3091 int total_flags, int flags_or,
3092 int flags_and)
3093{
3094 struct wpa_driver_nl80211_data *drv = priv;
3095 struct nl_msg *msg, *flags = NULL;
7e76ee9c 3096 struct nl80211_sta_flag_update upd;
a8d6ffa4
JM
3097
3098 msg = nlmsg_alloc();
3099 if (!msg)
3100 return -ENOMEM;
3101
3102 flags = nlmsg_alloc();
3103 if (!flags) {
3104 nlmsg_free(msg);
3105 return -ENOMEM;
3106 }
3107
3108 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3109 0, NL80211_CMD_SET_STATION, 0);
3110
3111 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3112 if_nametoindex(drv->ifname));
3113 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3114
7e76ee9c
JM
3115 /*
3116 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
3117 * can be removed eventually.
3118 */
0de39516 3119 if (total_flags & WPA_STA_AUTHORIZED)
a8d6ffa4
JM
3120 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
3121
0de39516 3122 if (total_flags & WPA_STA_WMM)
a8d6ffa4
JM
3123 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
3124
0de39516 3125 if (total_flags & WPA_STA_SHORT_PREAMBLE)
a8d6ffa4
JM
3126 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
3127
0de39516 3128 if (total_flags & WPA_STA_MFP)
a8d6ffa4
JM
3129 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
3130
3131 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
3132 goto nla_put_failure;
3133
7e76ee9c
JM
3134 os_memset(&upd, 0, sizeof(upd));
3135 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
3136 upd.set = sta_flags_nl80211(flags_or);
3137 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
3138
a8d6ffa4
JM
3139 nlmsg_free(flags);
3140
3141 return send_and_recv_msgs(drv, msg, NULL, NULL);
3142 nla_put_failure:
3143 nlmsg_free(flags);
3144 return -ENOBUFS;
3145}
3146
0915d02c 3147
1581b38b
JM
3148static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
3149 struct wpa_driver_associate_params *params)
3150{
3151 if (wpa_driver_nl80211_set_mode(drv, params->mode) ||
f019981a 3152 wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
460456f8 3153 nl80211_remove_monitor_interface(drv);
1581b38b 3154 return -1;
0915d02c 3155 }
1581b38b
JM
3156
3157 /* TODO: setup monitor interface (and add code somewhere to remove this
3158 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
1581b38b
JM
3159
3160 return 0;
3161}
1581b38b
JM
3162
3163
5cc4d64b
JM
3164static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
3165{
3166 struct nl_msg *msg;
3167 int ret = -1;
3168
3169 msg = nlmsg_alloc();
3170 if (!msg)
3171 return -1;
3172
3173 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3174 NL80211_CMD_LEAVE_IBSS, 0);
3175 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3176 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3177 msg = NULL;
3178 if (ret) {
3179 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
3180 "(%s)", ret, strerror(-ret));
3181 goto nla_put_failure;
3182 }
3183
3184 ret = 0;
3185 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
3186
3187nla_put_failure:
3188 nlmsg_free(msg);
3189 return ret;
3190}
3191
3192
3193static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
3194 struct wpa_driver_associate_params *params)
3195{
3196 struct nl_msg *msg;
3197 int ret = -1;
3198 int count = 0;
3199
3200 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
3201
3202 if (wpa_driver_nl80211_set_mode(drv, params->mode)) {
3203 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
3204 "IBSS mode");
3205 return -1;
3206 }
3207
3208retry:
3209 msg = nlmsg_alloc();
3210 if (!msg)
3211 return -1;
3212
3213 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3214 NL80211_CMD_JOIN_IBSS, 0);
3215 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3216
3217 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
3218 goto nla_put_failure;
3219
3220 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3221 params->ssid, params->ssid_len);
3222 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3223 params->ssid);
3224 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3225 drv->ssid_len = params->ssid_len;
3226
3227 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3228 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3229
3230 ret = nl80211_set_conn_keys(params, msg);
3231 if (ret)
3232 goto nla_put_failure;
3233
a95795ad
JM
3234 if (params->wpa_ie) {
3235 wpa_hexdump(MSG_DEBUG,
3236 " * Extra IEs for Beacon/Probe Response frames",
3237 params->wpa_ie, params->wpa_ie_len);
3238 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3239 params->wpa_ie);
3240 }
3241
5cc4d64b
JM
3242 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3243 msg = NULL;
3244 if (ret) {
3245 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
3246 ret, strerror(-ret));
3247 count++;
3248 if (ret == -EALREADY && count == 1) {
3249 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
3250 "forced leave");
3251 nl80211_leave_ibss(drv);
3252 nlmsg_free(msg);
3253 goto retry;
3254 }
3255
3256 goto nla_put_failure;
3257 }
3258 ret = 0;
3259 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
3260
3261nla_put_failure:
3262 nlmsg_free(msg);
3263 return ret;
3264}
3265
3266
cfaab580
ZY
3267static int wpa_driver_nl80211_connect(
3268 struct wpa_driver_nl80211_data *drv,
3269 struct wpa_driver_associate_params *params)
3270{
3271 struct nl_msg *msg;
3272 enum nl80211_auth_type type;
3273 int ret = 0;
3274
3275 msg = nlmsg_alloc();
3276 if (!msg)
3277 return -1;
3278
3279 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
3280 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3281 NL80211_CMD_CONNECT, 0);
3282
3283 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3284 if (params->bssid) {
3285 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3286 MAC2STR(params->bssid));
3287 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3288 }
3289 if (params->freq) {
3290 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3291 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3292 }
3293 if (params->ssid) {
3294 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3295 params->ssid, params->ssid_len);
3296 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3297 params->ssid);
3298 if (params->ssid_len > sizeof(drv->ssid))
3299 goto nla_put_failure;
3300 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3301 drv->ssid_len = params->ssid_len;
3302 }
3303 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
3304 if (params->wpa_ie)
3305 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3306 params->wpa_ie);
3307
3308 if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
3309 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3310 else if (params->auth_alg & AUTH_ALG_SHARED_KEY)
3311 type = NL80211_AUTHTYPE_SHARED_KEY;
3312 else if (params->auth_alg & AUTH_ALG_LEAP)
3313 type = NL80211_AUTHTYPE_NETWORK_EAP;
3314 else if (params->auth_alg & AUTH_ALG_FT)
3315 type = NL80211_AUTHTYPE_FT;
3316 else
3317 goto nla_put_failure;
3318
3319 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3320 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
3321
3322 if (params->wpa_ie && params->wpa_ie_len) {
3323 enum nl80211_wpa_versions ver;
3324
3325 if (params->wpa_ie[0] == WLAN_EID_RSN)
3326 ver = NL80211_WPA_VERSION_2;
3327 else
3328 ver = NL80211_WPA_VERSION_1;
3329
3330 wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
3331 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
3332 }
3333
3334 if (params->pairwise_suite != CIPHER_NONE) {
c1bb3e0a 3335 int cipher;
cfaab580
ZY
3336
3337 switch (params->pairwise_suite) {
3338 case CIPHER_WEP40:
3339 cipher = WLAN_CIPHER_SUITE_WEP40;
3340 break;
3341 case CIPHER_WEP104:
3342 cipher = WLAN_CIPHER_SUITE_WEP104;
3343 break;
3344 case CIPHER_CCMP:
3345 cipher = WLAN_CIPHER_SUITE_CCMP;
3346 break;
3347 case CIPHER_TKIP:
3348 default:
3349 cipher = WLAN_CIPHER_SUITE_TKIP;
3350 break;
3351 }
3352 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
3353 }
3354
3355 if (params->group_suite != CIPHER_NONE) {
c1bb3e0a 3356 int cipher;
cfaab580
ZY
3357
3358 switch (params->group_suite) {
3359 case CIPHER_WEP40:
3360 cipher = WLAN_CIPHER_SUITE_WEP40;
3361 break;
3362 case CIPHER_WEP104:
3363 cipher = WLAN_CIPHER_SUITE_WEP104;
3364 break;
3365 case CIPHER_CCMP:
3366 cipher = WLAN_CIPHER_SUITE_CCMP;
3367 break;
3368 case CIPHER_TKIP:
3369 default:
3370 cipher = WLAN_CIPHER_SUITE_TKIP;
3371 break;
3372 }
3373 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
3374 }
3375
3376 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
3377 params->key_mgmt_suite == KEY_MGMT_PSK) {
3378 int mgmt = WLAN_AKM_SUITE_PSK;
3379
3380 switch (params->key_mgmt_suite) {
3381 case KEY_MGMT_802_1X:
3382 mgmt = WLAN_AKM_SUITE_8021X;
3383 break;
3384 case KEY_MGMT_PSK:
3385 default:
3386 mgmt = WLAN_AKM_SUITE_PSK;
3387 break;
3388 }
3389 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
3390 }
3391
3392 ret = nl80211_set_conn_keys(params, msg);
3393 if (ret)
3394 goto nla_put_failure;
3395
3396 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3397 msg = NULL;
3398 if (ret) {
3399 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
3400 "(%s)", ret, strerror(-ret));
3401 goto nla_put_failure;
3402 }
3403 ret = 0;
3404 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
3405
3406nla_put_failure:
3407 nlmsg_free(msg);
3408 return ret;
3409
3410}
3411
3412
c2a04078
JM
3413static int wpa_driver_nl80211_associate(
3414 void *priv, struct wpa_driver_associate_params *params)
3415{
3416 struct wpa_driver_nl80211_data *drv = priv;
3417 int ret = -1;
3418 struct nl_msg *msg;
3419
5cc4d64b 3420 if (params->mode == IEEE80211_MODE_AP)
1581b38b 3421 return wpa_driver_nl80211_ap(drv, params);
1581b38b 3422
5cc4d64b
JM
3423 if (params->mode == IEEE80211_MODE_IBSS)
3424 return wpa_driver_nl80211_ibss(drv, params);
3425
4a867032
JM
3426 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
3427 if (wpa_driver_nl80211_set_mode(drv, params->mode) < 0)
3428 return -1;
cfaab580 3429 return wpa_driver_nl80211_connect(drv, params);
4a867032 3430 }
cfaab580 3431
c2a04078
JM
3432 drv->associated = 0;
3433
3434 msg = nlmsg_alloc();
3435 if (!msg)
3436 return -1;
3437
3438 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
3439 drv->ifindex);
3440 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3441 NL80211_CMD_ASSOCIATE, 0);
3442
3443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3444 if (params->bssid) {
3445 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3446 MAC2STR(params->bssid));
3447 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3448 }
3449 if (params->freq) {
3450 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3451 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4832ecd7
JM
3452 drv->assoc_freq = params->freq;
3453 } else
3454 drv->assoc_freq = 0;
c2a04078
JM
3455 if (params->ssid) {
3456 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3457 params->ssid, params->ssid_len);
3458 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3459 params->ssid);
fd05d64e
JM
3460 if (params->ssid_len > sizeof(drv->ssid))
3461 goto nla_put_failure;
3462 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3463 drv->ssid_len = params->ssid_len;
c2a04078
JM
3464 }
3465 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
3466 if (params->wpa_ie)
3467 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3468 params->wpa_ie);
3469
e572fa33
JM
3470#ifdef CONFIG_IEEE80211W
3471 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
3472 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
3473#endif /* CONFIG_IEEE80211W */
3474
01652550
JM
3475 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
3476
62fa124c
JM
3477 if (params->prev_bssid) {
3478 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
3479 MAC2STR(params->prev_bssid));
3480 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
3481 params->prev_bssid);
3482 }
3483
c2a04078
JM
3484 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3485 msg = NULL;
3486 if (ret) {
3487 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3488 "(%s)", ret, strerror(-ret));
3489 goto nla_put_failure;
3490 }
3491 ret = 0;
3492 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
3493 "successfully");
3494
3495nla_put_failure:
3496 nlmsg_free(msg);
3497 return ret;
3498}
3f5285e8
JM
3499
3500
ad1e68e6
JM
3501static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
3502 int ifindex, int mode)
3f5285e8 3503{
3f5285e8 3504 struct nl_msg *msg;
ad1e68e6
JM
3505 int ret = -ENOBUFS;
3506
3507 msg = nlmsg_alloc();
3508 if (!msg)
3509 return -ENOMEM;
3510
3511 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3512 0, NL80211_CMD_SET_INTERFACE, 0);
3513 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3514 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
3515
3516 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3517 if (!ret)
3518 return 0;
3519nla_put_failure:
3520 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
3521 " %d (%s)", ifindex, mode, ret, strerror(-ret));
3522 return ret;
3523}
3524
3525
3526static int wpa_driver_nl80211_set_mode(void *priv, int mode)
3527{
3528 struct wpa_driver_nl80211_data *drv = priv;
3529 int ret = -1;
1581b38b
JM
3530 int nlmode;
3531
3532 switch (mode) {
3533 case 0:
3534 nlmode = NL80211_IFTYPE_STATION;
3535 break;
3536 case 1:
3537 nlmode = NL80211_IFTYPE_ADHOC;
3538 break;
3539 case 2:
3540 nlmode = NL80211_IFTYPE_AP;
3541 break;
3542 default:
3543 return -1;
3544 }
3f5285e8 3545
ad1e68e6
JM
3546 if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
3547 drv->nlmode = nlmode;
460456f8
JM
3548 ret = 0;
3549 goto done;
ad1e68e6 3550 }
3f5285e8 3551
460456f8 3552 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
3553 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
3554 "requested mode - ignore error");
460456f8
JM
3555 ret = 0;
3556 goto done; /* Already in the requested mode */
3557 }
3f5285e8 3558
3f5285e8
JM
3559 /* mac80211 doesn't allow mode changes while the device is up, so
3560 * take the device down, try to set the mode again, and bring the
3561 * device back up.
3562 */
2135f224 3563 if (hostapd_set_iface_flags(drv, drv->ifname, 0) == 0) {
3f5285e8 3564 /* Try to set the mode again while the interface is down */
ad1e68e6 3565 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
2135f224
JM
3566 if (hostapd_set_iface_flags(drv, drv->ifname, 1))
3567 ret = -1;
3f5285e8
JM
3568 }
3569
c6e8e8e4
JM
3570 if (!ret) {
3571 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
3572 "interface is down");
ad1e68e6 3573 drv->nlmode = nlmode;
c6e8e8e4 3574 }
ad1e68e6 3575
460456f8 3576done:
460456f8
JM
3577 if (!ret && nlmode == NL80211_IFTYPE_AP) {
3578 /* Setup additional AP mode functionality if needed */
3579 if (drv->monitor_ifidx < 0 &&
3580 nl80211_create_monitor_interface(drv))
3581 return -1;
3582 } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
3583 /* Remove additional AP mode functionality */
3584 nl80211_remove_monitor_interface(drv);
3585 }
460456f8 3586
c6e8e8e4
JM
3587 if (ret)
3588 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
3589 "from %d failed", nlmode, drv->nlmode);
3590
3f5285e8
JM
3591 return ret;
3592}
3593
3594
3f5285e8
JM
3595static int wpa_driver_nl80211_get_capa(void *priv,
3596 struct wpa_driver_capa *capa)
3597{
3598 struct wpa_driver_nl80211_data *drv = priv;
3599 if (!drv->has_capability)
3600 return -1;
3601 os_memcpy(capa, &drv->capa, sizeof(*capa));
3602 return 0;
3603}
3604
3605
3606static int wpa_driver_nl80211_set_operstate(void *priv, int state)
3607{
3608 struct wpa_driver_nl80211_data *drv = priv;
3609
3610 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
3611 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
3612 drv->operstate = state;
08063178 3613 return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
e2d02c29 3614 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
3615}
3616
01652550
JM
3617
3618static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
3619{
3620 struct wpa_driver_nl80211_data *drv = priv;
3621 struct nl_msg *msg;
3622 struct nl80211_sta_flag_update upd;
3623
3624 msg = nlmsg_alloc();
3625 if (!msg)
3626 return -ENOMEM;
3627
3628 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3629 0, NL80211_CMD_SET_STATION, 0);
3630
3631 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3632 if_nametoindex(drv->ifname));
3633 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
3634
3635 os_memset(&upd, 0, sizeof(upd));
3636 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
3637 if (authorized)
3638 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
3639 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
3640
3641 return send_and_recv_msgs(drv, msg, NULL, NULL);
3642 nla_put_failure:
3643 return -ENOBUFS;
3644}
3645
3f5285e8 3646
c5121837
JM
3647#ifdef HOSTAPD
3648
c5121837 3649static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
b4fd6fab 3650 int ifindex)
c5121837
JM
3651{
3652 struct i802_bss *bss = &drv->bss;
3653 while (bss) {
b4fd6fab 3654 if (ifindex == bss->ifindex)
c5121837
JM
3655 return bss;
3656 bss = bss->next;
3657 }
b4fd6fab 3658 wpa_printf(MSG_DEBUG, "nl80211: get_bss(%d) failed", ifindex);
c5121837
JM
3659 return NULL;
3660}
3661
3662
3663static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3664{
3665 int i;
3666 int *old;
3667
3668 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
3669 ifidx);
3670 for (i = 0; i < drv->num_if_indices; i++) {
3671 if (drv->if_indices[i] == 0) {
3672 drv->if_indices[i] = ifidx;
3673 return;
3674 }
3675 }
3676
3677 if (drv->if_indices != drv->default_if_indices)
3678 old = drv->if_indices;
3679 else
3680 old = NULL;
3681
7bf12757
JM
3682 drv->if_indices = os_realloc(old,
3683 sizeof(int) * (drv->num_if_indices + 1));
c5121837
JM
3684 if (!drv->if_indices) {
3685 if (!old)
3686 drv->if_indices = drv->default_if_indices;
3687 else
3688 drv->if_indices = old;
3689 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
3690 "interfaces");
3691 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
3692 return;
3693 }
3694 drv->if_indices[drv->num_if_indices] = ifidx;
3695 drv->num_if_indices++;
3696}
3697
3698
3699static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3700{
3701 int i;
3702
3703 for (i = 0; i < drv->num_if_indices; i++) {
3704 if (drv->if_indices[i] == ifidx) {
3705 drv->if_indices[i] = 0;
3706 break;
3707 }
3708 }
3709}
3710
3711
3712static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3713{
3714 int i;
3715
3716 for (i = 0; i < drv->num_if_indices; i++)
3717 if (drv->if_indices[i] == ifidx)
3718 return 1;
3719
3720 return 0;
3721}
3722
3723
c5121837
JM
3724static inline int min_int(int a, int b)
3725{
3726 if (a < b)
3727 return a;
3728 return b;
3729}
3730
3731
3732static int get_key_handler(struct nl_msg *msg, void *arg)
3733{
3734 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3735 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3736
3737 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3738 genlmsg_attrlen(gnlh, 0), NULL);
3739
3740 /*
3741 * TODO: validate the key index and mac address!
3742 * Otherwise, there's a race condition as soon as
3743 * the kernel starts sending key notifications.
3744 */
3745
3746 if (tb[NL80211_ATTR_KEY_SEQ])
3747 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
3748 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
3749 return NL_SKIP;
3750}
3751
3752
3753static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
3754 int idx, u8 *seq)
3755{
3756 struct wpa_driver_nl80211_data *drv = priv;
3757 struct nl_msg *msg;
3758
3759 msg = nlmsg_alloc();
3760 if (!msg)
3761 return -ENOMEM;
3762
3763 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3764 0, NL80211_CMD_GET_KEY, 0);
3765
3766 if (addr)
3767 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3768 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
3769 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
3770
3771 memset(seq, 0, 6);
3772
3773 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
3774 nla_put_failure:
3775 return -ENOBUFS;
3776}
3777
3778
3779static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
3780 int mode)
3781{
3782 struct wpa_driver_nl80211_data *drv = priv;
3783 struct nl_msg *msg;
3784 u8 rates[NL80211_MAX_SUPP_RATES];
3785 u8 rates_len = 0;
3786 int i;
3787
3788 msg = nlmsg_alloc();
3789 if (!msg)
3790 return -ENOMEM;
3791
3792 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3793 NL80211_CMD_SET_BSS, 0);
3794
3795 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3796 rates[rates_len++] = basic_rates[i] / 5;
3797
3798 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3799
3800 /* TODO: multi-BSS support */
3801 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
3802
3803 return send_and_recv_msgs(drv, msg, NULL, NULL);
3804 nla_put_failure:
3805 return -ENOBUFS;
3806}
3807
3808
c5121837
JM
3809/* Set kernel driver on given frequency (MHz) */
3810static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
3811{
3812 struct wpa_driver_nl80211_data *drv = priv;
f019981a
JM
3813 return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
3814 freq->sec_channel_offset);
c5121837
JM
3815}
3816
3817
3818static int i802_set_rts(void *priv, int rts)
3819{
3820 struct wpa_driver_nl80211_data *drv = priv;
ad649451
JM
3821 struct nl_msg *msg;
3822 int ret = -ENOBUFS;
3823 u32 val;
c5121837 3824
ad649451
JM
3825 msg = nlmsg_alloc();
3826 if (!msg)
3827 return -ENOMEM;
c5121837 3828
ad649451
JM
3829 if (rts >= 2347)
3830 val = (u32) -1;
3831 else
3832 val = rts;
c5121837 3833
ad649451
JM
3834 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3835 0, NL80211_CMD_SET_WIPHY, 0);
3836 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3837 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
3838
3839 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3840 if (!ret)
3841 return 0;
3842nla_put_failure:
3843 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
3844 "%d (%s)", rts, ret, strerror(-ret));
3845 return ret;
c5121837
JM
3846}
3847
3848
3849static int i802_set_frag(void *priv, int frag)
3850{
3851 struct wpa_driver_nl80211_data *drv = priv;
ad649451
JM
3852 struct nl_msg *msg;
3853 int ret = -ENOBUFS;
3854 u32 val;
c5121837 3855
ad649451
JM
3856 msg = nlmsg_alloc();
3857 if (!msg)
3858 return -ENOMEM;
c5121837 3859
ad649451
JM
3860 if (frag >= 2346)
3861 val = (u32) -1;
3862 else
3863 val = frag;
c5121837 3864
ad649451
JM
3865 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3866 0, NL80211_CMD_SET_WIPHY, 0);
3867 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3868 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
3869
3870 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3871 if (!ret)
3872 return 0;
3873nla_put_failure:
3874 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
3875 "%d: %d (%s)", frag, ret, strerror(-ret));
3876 return ret;
c5121837
JM
3877}
3878
3879
c5121837
JM
3880static int i802_flush(void *priv)
3881{
3882 struct wpa_driver_nl80211_data *drv = priv;
3883 struct nl_msg *msg;
3884
3885 msg = nlmsg_alloc();
3886 if (!msg)
3887 return -1;
3888
3889 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3890 0, NL80211_CMD_DEL_STATION, 0);
3891
3892 /*
3893 * XXX: FIX! this needs to flush all VLANs too
3894 */
3895 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3896 if_nametoindex(drv->ifname));
3897
3898 return send_and_recv_msgs(drv, msg, NULL, NULL);
3899 nla_put_failure:
3900 return -ENOBUFS;
3901}
3902
3903
3904static int get_sta_handler(struct nl_msg *msg, void *arg)
3905{
3906 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3907 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3908 struct hostap_sta_driver_data *data = arg;
3909 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
3910 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
3911 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
3912 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
3913 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
3914 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
3915 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
3916 };
3917
3918 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3919 genlmsg_attrlen(gnlh, 0), NULL);
3920
3921 /*
3922 * TODO: validate the interface and mac address!
3923 * Otherwise, there's a race condition as soon as
3924 * the kernel starts sending station notifications.
3925 */
3926
3927 if (!tb[NL80211_ATTR_STA_INFO]) {
3928 wpa_printf(MSG_DEBUG, "sta stats missing!");
3929 return NL_SKIP;
3930 }
3931 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
3932 tb[NL80211_ATTR_STA_INFO],
3933 stats_policy)) {
3934 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
3935 return NL_SKIP;
3936 }
3937
3938 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
3939 data->inactive_msec =
3940 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
3941 if (stats[NL80211_STA_INFO_RX_BYTES])
3942 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
3943 if (stats[NL80211_STA_INFO_TX_BYTES])
3944 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
3945 if (stats[NL80211_STA_INFO_RX_PACKETS])
3946 data->rx_packets =
3947 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
3948 if (stats[NL80211_STA_INFO_TX_PACKETS])
3949 data->tx_packets =
3950 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
3951
3952 return NL_SKIP;
3953}
3954
3955static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
3956 const u8 *addr)
3957{
3958 struct wpa_driver_nl80211_data *drv = priv;
3959 struct nl_msg *msg;
3960
3961 os_memset(data, 0, sizeof(*data));
3962 msg = nlmsg_alloc();
3963 if (!msg)
3964 return -ENOMEM;
3965
3966 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3967 0, NL80211_CMD_GET_STATION, 0);
3968
3969 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3970 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
3971
3972 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
3973 nla_put_failure:
3974 return -ENOBUFS;
3975}
3976
3977
c5121837
JM
3978static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
3979 int cw_min, int cw_max, int burst_time)
3980{
3981 struct wpa_driver_nl80211_data *drv = priv;
3982 struct nl_msg *msg;
3983 struct nlattr *txq, *params;
3984
3985 msg = nlmsg_alloc();
3986 if (!msg)
3987 return -1;
3988
3989 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3990 0, NL80211_CMD_SET_WIPHY, 0);
3991
3992 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
3993
3994 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
3995 if (!txq)
3996 goto nla_put_failure;
3997
3998 /* We are only sending parameters for a single TXQ at a time */
3999 params = nla_nest_start(msg, 1);
4000 if (!params)
4001 goto nla_put_failure;
4002
4003 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
4004 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
4005 * 32 usec, so need to convert the value here. */
4006 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
4007 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
4008 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
4009 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
4010
4011 nla_nest_end(msg, params);
4012
4013 nla_nest_end(msg, txq);
4014
4015 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4016 return 0;
4017 nla_put_failure:
4018 return -1;
4019}
4020
4021
c5121837
JM
4022static int i802_set_bss(void *priv, int cts, int preamble, int slot)
4023{
4024 struct wpa_driver_nl80211_data *drv = priv;
4025 struct nl_msg *msg;
4026
4027 msg = nlmsg_alloc();
4028 if (!msg)
4029 return -ENOMEM;
4030
4031 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4032 NL80211_CMD_SET_BSS, 0);
4033
4034 if (cts >= 0)
4035 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
4036 if (preamble >= 0)
4037 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
4038 if (slot >= 0)
4039 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
4040
4041 /* TODO: multi-BSS support */
4042 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4043
4044 return send_and_recv_msgs(drv, msg, NULL, NULL);
4045 nla_put_failure:
4046 return -ENOBUFS;
4047}
4048
4049
4050static int i802_set_cts_protect(void *priv, int value)
4051{
4052 return i802_set_bss(priv, value, -1, -1);
4053}
4054
4055
4056static int i802_set_preamble(void *priv, int value)
4057{
4058 return i802_set_bss(priv, -1, value, -1);
4059}
4060
4061
4062static int i802_set_short_slot_time(void *priv, int value)
4063{
4064 return i802_set_bss(priv, -1, -1, value);
4065}
4066
4067
c5121837
JM
4068static int i802_set_sta_vlan(void *priv, const u8 *addr,
4069 const char *ifname, int vlan_id)
4070{
4071 struct wpa_driver_nl80211_data *drv = priv;
4072 struct nl_msg *msg;
4073
4074 msg = nlmsg_alloc();
4075 if (!msg)
4076 return -ENOMEM;
4077
4078 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4079 0, NL80211_CMD_SET_STATION, 0);
4080
4081 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
4082 if_nametoindex(drv->ifname));
4083 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1c766b09 4084 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
c5121837
JM
4085 if_nametoindex(ifname));
4086
4087 return send_and_recv_msgs(drv, msg, NULL, NULL);
4088 nla_put_failure:
4089 return -ENOBUFS;
4090}
4091
4092
fbbfcbac
FF
4093static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val)
4094{
4095 struct wpa_driver_nl80211_data *drv = priv;
4096 char name[16];
4097
4098 os_snprintf(name, sizeof(name), "%s.sta%d", drv->ifname, aid);
4099 if (val) {
4100 if (nl80211_create_iface(priv, name, NL80211_IFTYPE_AP_VLAN,
4101 NULL, 1) < 0)
4102 return -1;
4103 hostapd_set_iface_flags(drv, name, 1);
4104 return i802_set_sta_vlan(priv, addr, name, 0);
4105 } else {
4106 i802_set_sta_vlan(priv, addr, drv->ifname, 0);
4107 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
4108 name);
4109 }
4110}
4111
4112
c5121837
JM
4113static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
4114{
4115 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4116 struct sockaddr_ll lladdr;
4117 unsigned char buf[3000];
4118 int len;
4119 socklen_t fromlen = sizeof(lladdr);
4120
4121 len = recvfrom(sock, buf, sizeof(buf), 0,
4122 (struct sockaddr *)&lladdr, &fromlen);
4123 if (len < 0) {
4124 perror("recv");
4125 return;
4126 }
4127
4128 if (have_ifidx(drv, lladdr.sll_ifindex)) {
4b9841d3
JM
4129 void *ctx;
4130 ctx = hostapd_sta_get_bss(drv->ctx, lladdr.sll_addr);
4131 if (!ctx)
c5121837 4132 return;
4b9841d3 4133 hostapd_eapol_receive(ctx, lladdr.sll_addr, buf, len);
c5121837
JM
4134 }
4135}
4136
4137
c5121837
JM
4138static int i802_get_inact_sec(void *priv, const u8 *addr)
4139{
4140 struct hostap_sta_driver_data data;
4141 int ret;
4142
4143 data.inactive_msec = (unsigned long) -1;
4144 ret = i802_read_sta_data(priv, &data, addr);
4145 if (ret || data.inactive_msec == (unsigned long) -1)
4146 return -1;
4147 return data.inactive_msec / 1000;
4148}
4149
4150
4151static int i802_sta_clear_stats(void *priv, const u8 *addr)
4152{
4153#if 0
4154 /* TODO */
4155#endif
4156 return 0;
4157}
4158
4159
731723a5
JM
4160static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
4161 int reason)
c5121837
JM
4162{
4163 struct wpa_driver_nl80211_data *drv = priv;
4164 struct ieee80211_mgmt mgmt;
4165
4166 memset(&mgmt, 0, sizeof(mgmt));
4167 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4168 WLAN_FC_STYPE_DEAUTH);
4169 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
4170 memcpy(mgmt.sa, own_addr, ETH_ALEN);
4171 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 4172 mgmt.u.deauth.reason_code = host_to_le16(reason);
9f324b61
JM
4173 return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
4174 IEEE80211_HDRLEN +
4175 sizeof(mgmt.u.deauth));
c5121837
JM
4176}
4177
4178
731723a5
JM
4179static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
4180 int reason)
c5121837
JM
4181{
4182 struct wpa_driver_nl80211_data *drv = priv;
4183 struct ieee80211_mgmt mgmt;
4184
4185 memset(&mgmt, 0, sizeof(mgmt));
4186 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4187 WLAN_FC_STYPE_DISASSOC);
4188 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
4189 memcpy(mgmt.sa, own_addr, ETH_ALEN);
4190 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 4191 mgmt.u.disassoc.reason_code = host_to_le16(reason);
9f324b61
JM
4192 return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
4193 IEEE80211_HDRLEN +
4194 sizeof(mgmt.u.disassoc));
c5121837
JM
4195}
4196
4197
92f475b4
JM
4198static void *i802_init(struct hostapd_data *hapd,
4199 struct wpa_init_params *params)
c5121837
JM
4200{
4201 struct wpa_driver_nl80211_data *drv;
4202 size_t i;
4203
bbaf0837
JM
4204 drv = wpa_driver_nl80211_init(hapd, params->ifname);
4205 if (drv == NULL)
c5121837 4206 return NULL;
c5121837 4207
b4fd6fab 4208 drv->bss.ifindex = drv->ifindex;
c5121837
JM
4209
4210 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4211 drv->if_indices = drv->default_if_indices;
92f475b4
JM
4212 for (i = 0; i < params->num_bridge; i++) {
4213 if (params->bridge[i])
4214 add_ifidx(drv, if_nametoindex(params->bridge[i]));
c5121837 4215 }
c5121837 4216
ad1e68e6
JM
4217 /* start listening for EAPOL on the default AP interface */
4218 add_ifidx(drv, drv->ifindex);
4219
6980c191
FF
4220 if (hostapd_set_iface_flags(drv, drv->ifname, 0))
4221 goto failed;
ad1e68e6 4222
6980c191 4223 if (params->bssid) {
460456f8
JM
4224 if (set_ifhwaddr(drv, drv->ifname, params->bssid))
4225 goto failed;
4226 }
ad1e68e6 4227
4a867032 4228 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP)) {
ad1e68e6
JM
4229 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
4230 "into AP mode", drv->ifname);
bbaf0837 4231 goto failed;
ad1e68e6
JM
4232 }
4233
4234 if (hostapd_set_iface_flags(drv, drv->ifname, 1))
bbaf0837 4235 goto failed;
ad1e68e6
JM
4236
4237 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
4238 if (drv->eapol_sock < 0) {
4239 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
bbaf0837 4240 goto failed;
ad1e68e6
JM
4241 }
4242
4243 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
4244 {
4245 printf("Could not register read socket for eapol\n");
c5121837 4246 goto failed;
ad1e68e6
JM
4247 }
4248
412036f5 4249 if (get_ifhwaddr(drv, drv->ifname, params->own_addr))
bbaf0837 4250 goto failed;
c5121837 4251
c5121837
JM
4252 return drv;
4253
4254failed:
460456f8 4255 nl80211_remove_monitor_interface(drv);
c5121837
JM
4256 if (drv->ioctl_sock >= 0)
4257 close(drv->ioctl_sock);
c5121837
JM
4258
4259 genl_family_put(drv->nl80211);
4260 nl_cache_free(drv->nl_cache);
4261 nl_handle_destroy(drv->nl_handle);
4262 nl_cb_put(drv->nl_cb);
4263
bbaf0837
JM
4264 os_free(drv);
4265 return NULL;
4266}
c5121837 4267
c5121837 4268
bbaf0837
JM
4269static void i802_deinit(void *priv)
4270{
4271 wpa_driver_nl80211_deinit(priv);
c5121837
JM
4272}
4273
4274#endif /* HOSTAPD */
4275
4276
22a7c9d7
JM
4277static enum nl80211_iftype wpa_driver_nl80211_if_type(
4278 enum wpa_driver_if_type type)
4279{
4280 switch (type) {
4281 case WPA_IF_STATION:
4282 return NL80211_IFTYPE_STATION;
4283 case WPA_IF_AP_VLAN:
4284 return NL80211_IFTYPE_AP_VLAN;
4285 case WPA_IF_AP_BSS:
4286 return NL80211_IFTYPE_AP;
4287 }
4288 return -1;
4289}
4290
4291
4292static int wpa_driver_nl80211_if_add(const char *iface, void *priv,
4293 enum wpa_driver_if_type type,
8043e725
JM
4294 const char *ifname, const u8 *addr,
4295 void *bss_ctx)
22a7c9d7
JM
4296{
4297 struct wpa_driver_nl80211_data *drv = priv;
4298 int ifidx;
4299#ifdef HOSTAPD
4300 struct i802_bss *bss = NULL;
4301
4302 if (type == WPA_IF_AP_BSS) {
4303 bss = os_zalloc(sizeof(*bss));
4304 if (bss == NULL)
4305 return -1;
4306 }
4307#endif /* HOSTAPD */
4308
4309 ifidx = nl80211_create_iface(drv, ifname,
fbbfcbac
FF
4310 wpa_driver_nl80211_if_type(type), addr,
4311 0);
22a7c9d7
JM
4312 if (ifidx < 0) {
4313#ifdef HOSTAPD
4314 os_free(bss);
4315#endif /* HOSTAPD */
4316 return -1;
4317 }
4318
4319#ifdef HOSTAPD
4320 if (type == WPA_IF_AP_BSS) {
4321 if (hostapd_set_iface_flags(priv, ifname, 1)) {
4322 nl80211_remove_iface(priv, ifidx);
4323 os_free(bss);
4324 return -1;
4325 }
4326 bss->ifindex = ifidx;
4327 bss->next = drv->bss.next;
4328 drv->bss.next = bss;
4329 }
4330#endif /* HOSTAPD */
4331
4332 return 0;
4333}
4334
4335
4336static int wpa_driver_nl80211_if_remove(void *priv,
4337 enum wpa_driver_if_type type,
4338 const char *ifname)
4339{
4340 struct wpa_driver_nl80211_data *drv = priv;
4341 int ifindex = if_nametoindex(ifname);
4342
4343 nl80211_remove_iface(drv, ifindex);
4344
4345#ifdef HOSTAPD
4346 if (type == WPA_IF_AP_BSS) {
4347 struct i802_bss *bss, *prev;
4348 prev = &drv->bss;
4349 bss = drv->bss.next;
4350 while (bss) {
4351 if (ifindex == bss->ifindex) {
4352 prev->next = bss->next;
4353 os_free(bss);
4354 break;
4355 }
4356 prev = bss;
4357 bss = bss->next;
4358 }
4359 }
4360#endif /* HOSTAPD */
4361
4362 return 0;
4363}
4364
4365
3f5285e8
JM
4366const struct wpa_driver_ops wpa_driver_nl80211_ops = {
4367 .name = "nl80211",
4368 .desc = "Linux nl80211/cfg80211",
4369 .get_bssid = wpa_driver_nl80211_get_bssid,
4370 .get_ssid = wpa_driver_nl80211_get_ssid,
3f5285e8 4371 .set_key = wpa_driver_nl80211_set_key,
6a1063e0 4372 .scan2 = wpa_driver_nl80211_scan,
3f5285e8
JM
4373 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
4374 .deauthenticate = wpa_driver_nl80211_deauthenticate,
4375 .disassociate = wpa_driver_nl80211_disassociate,
c2a04078 4376 .authenticate = wpa_driver_nl80211_authenticate,
3f5285e8 4377 .associate = wpa_driver_nl80211_associate,
3f5285e8
JM
4378 .init = wpa_driver_nl80211_init,
4379 .deinit = wpa_driver_nl80211_deinit,
3f5285e8
JM
4380 .get_capa = wpa_driver_nl80211_get_capa,
4381 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 4382 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 4383 .set_country = wpa_driver_nl80211_set_country,
d2440ba0 4384 .set_beacon = wpa_driver_nl80211_set_beacon,
22a7c9d7
JM
4385 .if_add = wpa_driver_nl80211_if_add,
4386 .if_remove = wpa_driver_nl80211_if_remove,
071f8ac4 4387 .send_mlme = wpa_driver_nl80211_send_mlme,
c3965310 4388 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
0f4e8b4f
JM
4389 .sta_add = wpa_driver_nl80211_sta_add,
4390 .sta_remove = wpa_driver_nl80211_sta_remove,
db149ac9 4391 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 4392 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837
JM
4393#ifdef HOSTAPD
4394 .hapd_init = i802_init,
c5121837 4395 .hapd_deinit = i802_deinit,
c5121837
JM
4396 .get_seqnum = i802_get_seqnum,
4397 .flush = i802_flush,
4398 .read_sta_data = i802_read_sta_data,
c5121837
JM
4399 .sta_deauth = i802_sta_deauth,
4400 .sta_disassoc = i802_sta_disassoc,
c5121837
JM
4401 .get_inact_sec = i802_get_inact_sec,
4402 .sta_clear_stats = i802_sta_clear_stats,
4403 .set_freq = i802_set_freq,
4404 .set_rts = i802_set_rts,
4405 .set_frag = i802_set_frag,
c5121837 4406 .set_rate_sets = i802_set_rate_sets,
c5121837
JM
4407 .set_cts_protect = i802_set_cts_protect,
4408 .set_preamble = i802_set_preamble,
4409 .set_short_slot_time = i802_set_short_slot_time,
4410 .set_tx_queue_params = i802_set_tx_queue_params,
c5121837 4411 .set_sta_vlan = i802_set_sta_vlan,
fbbfcbac 4412 .set_wds_sta = i802_set_wds_sta,
c5121837 4413#endif /* HOSTAPD */
3f5285e8 4414};