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