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