2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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"
27 #include "ieee802_11_defs.h"
29 #if defined(CONFIG_AP) || defined(HOSTAPD)
30 #include <netpacket/packet.h>
31 #include <linux/filter.h>
33 #include "radiotap_iter.h"
34 #endif /* CONFIG_AP || HOSTAPD */
38 #include "../hostapd/hostapd_defs.h"
41 #define ETH_P_ALL 0x0003
44 #endif /* CONFIG_AP */
47 #include <netlink/msg.h>
48 #include <netlink/attr.h>
50 #include <net/if_arp.h>
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"
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 */
69 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
72 #define IFF_DORMANT 0x20000 /* driver signals dormant */
75 #ifndef IF_OPER_DORMANT
76 #define IF_OPER_DORMANT 5
83 struct i802_bss
*next
;
84 char ifname
[IFNAMSIZ
+ 1];
85 unsigned int beacon_set
:1;
88 struct wpa_driver_nl80211_data
{
91 int ioctl_sock
; /* socket for ioctl() use */
92 char ifname
[IFNAMSIZ
+ 1];
95 struct wpa_driver_capa capa
;
100 int scan_complete_events
;
102 struct nl_handle
*nl_handle
;
103 struct nl_cache
*nl_cache
;
105 struct genl_family
*nl80211
;
114 unsigned int beacon_set
:1;
117 #endif /* CONFIG_AP */
120 struct hostapd_data
*hapd
;
122 int wext_sock
; /* socket for wireless events */
123 int eapol_sock
; /* socket for EAPOL frames */
124 int monitor_sock
; /* socket for monitor */
127 int default_if_indices
[16];
134 unsigned int ht_40mhz_scan
:1;
138 struct hostapd_neighbor_bss
*neighbors
;
139 size_t num_neighbors
;
144 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
,
146 static int wpa_driver_nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
,
149 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
);
152 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
154 #endif /* CONFIG_AP */
158 #define wpa_supplicant_event(c, e, d) do { } while (0)
162 static int ack_handler(struct nl_msg
*msg
, void *arg
)
169 static int finish_handler(struct nl_msg
*msg
, void *arg
)
176 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
184 static int send_and_recv_msgs(struct wpa_driver_nl80211_data
*drv
,
186 int (*valid_handler
)(struct nl_msg
*, void *),
192 cb
= nl_cb_clone(drv
->nl_cb
);
196 err
= nl_send_auto_complete(drv
->nl_handle
, msg
);
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
);
207 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
208 valid_handler
, valid_data
);
211 nl_recvmsgs(drv
->nl_handle
, cb
);
225 static int family_handler(struct nl_msg
*msg
, void *arg
)
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
;
233 nla_parse(tb
, CTRL_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
234 genlmsg_attrlen(gnlh
, 0), NULL
);
235 if (!tb
[CTRL_ATTR_MCAST_GROUPS
])
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
]),
246 nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
])) != 0)
248 res
->id
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
256 static int nl_get_multicast_id(struct wpa_driver_nl80211_data
*drv
,
257 const char *family
, const char *group
)
261 struct family_data res
= { group
, -ENOENT
};
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
);
270 ret
= send_and_recv_msgs(drv
, msg
, family_handler
, &res
);
281 static int wpa_driver_nl80211_send_oper_ifla(
282 struct wpa_driver_nl80211_data
*drv
,
283 int linkmode
, int operstate
)
287 struct ifinfomsg ifinfo
;
294 os_memset(&req
, 0, sizeof(req
));
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;
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;
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));
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));
327 wpa_printf(MSG_DEBUG
, "WEXT: Operstate: linkmode=%d, operstate=%d",
328 linkmode
, operstate
);
330 ret
= send(drv
->link_event_sock
, &req
, req
.hdr
.nlmsg_len
, 0);
332 wpa_printf(MSG_DEBUG
, "WEXT: Sending operstate IFLA failed: "
333 "%s (assume operstate is not supported)",
337 return ret
< 0 ? -1 : 0;
341 static int wpa_driver_nl80211_set_auth_param(
342 struct wpa_driver_nl80211_data
*drv
, int idx
, u32 value
)
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
;
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
));
358 ret
= errno
== EOPNOTSUPP
? -2 : -1;
365 static int wpa_driver_nl80211_get_bssid(void *priv
, u8
*bssid
)
367 struct wpa_driver_nl80211_data
*drv
= priv
;
368 if (!drv
->associated
)
370 os_memcpy(bssid
, drv
->bssid
, ETH_ALEN
);
375 static int wpa_driver_nl80211_get_ssid(void *priv
, u8
*ssid
)
377 struct wpa_driver_nl80211_data
*drv
= priv
;
378 if (!drv
->associated
)
380 os_memcpy(ssid
, drv
->ssid
, drv
->ssid_len
);
381 return drv
->ssid_len
;
385 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data
*drv
,
386 void *ctx
, char *buf
, size_t len
,
389 union wpa_event_data event
;
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
;
398 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
400 event
.interface_status
.ifname
,
401 del
? "removed" : "added");
403 if (os_strcmp(drv
->ifname
, event
.interface_status
.ifname
) == 0) {
410 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
414 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data
*drv
,
417 struct ifinfomsg
*ifi
;
418 int attrlen
, _nlmsg_len
, rta_len
;
423 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
425 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
429 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
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
)
440 attr
= RTA_NEXT(attr
, attrlen
);
447 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data
*drv
,
448 int ifindex
, struct nlmsghdr
*h
)
450 if (drv
->ifindex
== ifindex
)
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 "
457 wpa_driver_nl80211_finish_drv_init(drv
);
465 static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data
*drv
,
466 void *ctx
, struct nlmsghdr
*h
,
469 struct ifinfomsg
*ifi
;
470 int attrlen
, _nlmsg_len
, rta_len
;
471 struct rtattr
* attr
;
473 if (len
< sizeof(*ifi
))
478 if (!wpa_driver_nl80211_own_ifindex(drv
, ifi
->ifi_index
, h
)) {
479 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
484 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
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]" : "");
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
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
);
502 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
504 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
508 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
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(
515 ((char *) attr
) + rta_len
,
516 attr
->rta_len
- rta_len
, 0);
518 attr
= RTA_NEXT(attr
, attrlen
);
523 static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data
*drv
,
524 void *ctx
, struct nlmsghdr
*h
,
527 struct ifinfomsg
*ifi
;
528 int attrlen
, _nlmsg_len
, rta_len
;
529 struct rtattr
* attr
;
531 if (len
< sizeof(*ifi
))
536 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
538 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
542 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
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(
549 ((char *) attr
) + rta_len
,
550 attr
->rta_len
- rta_len
, 1);
552 attr
= RTA_NEXT(attr
, attrlen
);
557 static void wpa_driver_nl80211_event_receive_link(int sock
, void *eloop_ctx
,
562 struct sockaddr_nl from
;
568 fromlen
= sizeof(from
);
569 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
570 (struct sockaddr
*) &from
, &fromlen
);
572 if (errno
!= EINTR
&& errno
!= EAGAIN
)
573 perror("recvfrom(netlink)");
577 h
= (struct nlmsghdr
*) buf
;
578 while (left
>= (int) sizeof(*h
)) {
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",
590 switch (h
->nlmsg_type
) {
592 wpa_driver_nl80211_event_rtm_newlink(eloop_ctx
, sock_ctx
,
596 wpa_driver_nl80211_event_rtm_dellink(eloop_ctx
, sock_ctx
,
601 len
= NLMSG_ALIGN(len
);
603 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
607 wpa_printf(MSG_DEBUG
, "%d extra bytes in the end of netlink "
611 if (--max_events
> 0) {
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.
624 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
630 static void mlme_event_auth(struct wpa_driver_nl80211_data
*drv
,
631 const u8
*frame
, size_t len
)
633 const struct ieee80211_mgmt
*mgmt
;
634 union wpa_event_data event
;
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 "
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
);
652 wpa_supplicant_event(drv
->ctx
, EVENT_AUTH
, &event
);
656 static void mlme_event_assoc(struct wpa_driver_nl80211_data
*drv
,
657 const u8
*frame
, size_t len
)
659 const struct ieee80211_mgmt
*mgmt
;
660 union wpa_event_data event
;
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 "
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
);
679 event
.assoc_reject
.status_code
= status
;
681 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
686 os_memcpy(drv
->bssid
, mgmt
->sa
, ETH_ALEN
);
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
);
695 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
699 static void mlme_event(struct wpa_driver_nl80211_data
*drv
,
700 enum nl80211_commands cmd
, struct nlattr
*frame
)
703 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d without frame "
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
));
713 case NL80211_CMD_AUTHENTICATE
:
714 mlme_event_auth(drv
, nla_data(frame
), nla_len(frame
));
716 case NL80211_CMD_ASSOCIATE
:
717 mlme_event_assoc(drv
, nla_data(frame
), nla_len(frame
));
719 case NL80211_CMD_DEAUTHENTICATE
:
721 wpa_supplicant_event(drv
->ctx
, EVENT_DEAUTH
, NULL
);
723 case NL80211_CMD_DISASSOCIATE
:
725 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
733 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data
*drv
,
736 union wpa_event_data data
;
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
]));
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
]));
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;
757 data
.michael_mic_failure
.unicast
= 1;
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
);
764 wpa_supplicant_event(drv
->ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
768 static int process_event(struct nl_msg
*msg
, void *arg
)
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];
774 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
775 genlmsg_attrlen(gnlh
, 0), NULL
);
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)",
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
,
793 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, NULL
);
795 case NL80211_CMD_SCAN_ABORTED
:
796 wpa_printf(MSG_DEBUG
, "nl80211: Scan aborted");
798 * Need to indicate that scan results are available in order
799 * not to make wpa_supplicant stop its scanning.
801 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
803 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, NULL
);
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
]);
811 case NL80211_CMD_MICHAEL_MIC_FAILURE
:
812 mlme_event_michael_mic_failure(drv
, tb
);
815 wpa_printf(MSG_DEBUG
, "nl80211: Ignored unknown event "
816 "(cmd=%d)", gnlh
->cmd
);
824 static void wpa_driver_nl80211_event_receive(int sock
, void *eloop_ctx
,
828 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
830 wpa_printf(MSG_DEBUG
, "nl80211: Event message available");
832 cb
= nl_cb_clone(drv
->nl_cb
);
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
);
842 static int wpa_driver_nl80211_get_ifflags_ifname(struct wpa_driver_nl80211_data
*drv
,
843 const char *ifname
, int *flags
)
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]");
853 *flags
= ifr
.ifr_flags
& 0xffff;
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
864 static int wpa_driver_nl80211_get_ifflags(struct wpa_driver_nl80211_data
*drv
,
867 return wpa_driver_nl80211_get_ifflags_ifname(drv
, drv
->ifname
, flags
);
871 static int wpa_driver_nl80211_set_ifflags_ifname(
872 struct wpa_driver_nl80211_data
*drv
,
873 const char *ifname
, int flags
)
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");
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
894 static int wpa_driver_nl80211_set_ifflags(struct wpa_driver_nl80211_data
*drv
,
897 return wpa_driver_nl80211_set_ifflags_ifname(drv
, drv
->ifname
, flags
);
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
907 * This asks nl80211 to set the regulatory domain for given
908 * country ISO / IEC alpha2.
910 static int wpa_driver_nl80211_set_country(void *priv
, const char *alpha2_arg
)
912 struct wpa_driver_nl80211_data
*drv
= priv
;
918 goto nla_put_failure
;
920 alpha2
[0] = alpha2_arg
[0];
921 alpha2
[1] = alpha2_arg
[1];
924 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
925 0, NL80211_CMD_REQ_SET_REG
, 0);
927 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
928 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
))
936 struct wiphy_info_data
{
942 static int wiphy_info_handler(struct nl_msg
*msg
, void *arg
)
944 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
945 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
946 struct wiphy_info_data
*info
= arg
;
948 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
949 genlmsg_attrlen(gnlh
, 0), NULL
);
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
]);
955 if (tb
[NL80211_ATTR_SUPPORTED_IFTYPES
]) {
956 struct nlattr
*nl_mode
;
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;
971 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data
*drv
,
972 struct wiphy_info_data
*info
)
976 os_memset(info
, 0, sizeof(*info
));
981 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
982 0, NL80211_CMD_GET_WIPHY
, 0);
984 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
986 if (send_and_recv_msgs(drv
, msg
, wiphy_info_handler
, info
) == 0)
995 static void wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data
*drv
)
997 struct wiphy_info_data info
;
998 if (wpa_driver_nl80211_get_info(drv
, &info
))
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
;
1011 drv
->capa
.max_scan_ssids
= info
.max_scan_ssids
;
1012 if (info
.ap_supported
)
1013 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_AP
;
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
1024 static void * wpa_driver_nl80211_init(void *ctx
, const char *ifname
)
1027 struct sockaddr_nl local
;
1028 struct wpa_driver_nl80211_data
*drv
;
1030 drv
= os_zalloc(sizeof(*drv
));
1034 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1036 drv
->monitor_ifidx
= -1;
1037 drv
->monitor_sock
= -1;
1038 #endif /* CONFIG_AP */
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 "
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 "
1054 if (genl_connect(drv
->nl_handle
)) {
1055 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
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 "
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 "
1074 ret
= nl_get_multicast_id(drv
, "nl80211", "scan");
1076 ret
= nl_socket_add_membership(drv
->nl_handle
, ret
);
1078 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1079 "membership for scan events: %d (%s)",
1080 ret
, strerror(-ret
));
1084 ret
= nl_get_multicast_id(drv
, "nl80211", "mlme");
1086 ret
= nl_socket_add_membership(drv
->nl_handle
, ret
);
1088 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1089 "membership for mlme events: %d (%s)",
1090 ret
, strerror(-ret
));
1093 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SME
;
1095 eloop_register_read_sock(nl_socket_get_fd(drv
->nl_handle
),
1096 wpa_driver_nl80211_event_receive
, drv
, ctx
);
1098 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1099 if (drv
->ioctl_sock
< 0) {
1100 perror("socket(PF_INET,SOCK_DGRAM)");
1104 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1106 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
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)");
1119 eloop_register_read_sock(s
, wpa_driver_nl80211_event_receive_link
, drv
,
1121 drv
->link_event_sock
= s
;
1123 if (wpa_driver_nl80211_finish_drv_init(drv
))
1129 eloop_unregister_read_sock(drv
->link_event_sock
);
1130 close(drv
->link_event_sock
);
1132 close(drv
->ioctl_sock
);
1134 genl_family_put(drv
->nl80211
);
1136 nl_cache_free(drv
->nl_cache
);
1138 nl_handle_destroy(drv
->nl_handle
);
1140 nl_cb_put(drv
->nl_cb
);
1148 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
)
1152 drv
->ifindex
= if_nametoindex(drv
->ifname
);
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");
1159 if (wpa_driver_nl80211_get_ifflags(drv
, &flags
) != 0) {
1160 wpa_printf(MSG_ERROR
, "Could not get interface '%s' flags",
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' "
1172 wpa_driver_nl80211_capa(drv
);
1174 wpa_driver_nl80211_send_oper_ifla(drv
, 1, IF_OPER_DORMANT
);
1181 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1182 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1184 * Shut down driver interface and processing of driver events. Free
1185 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1187 static void wpa_driver_nl80211_deinit(void *priv
)
1189 struct wpa_driver_nl80211_data
*drv
= priv
;
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
);
1199 #endif /* CONFIG_AP */
1201 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1203 wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
, 0);
1205 wpa_driver_nl80211_send_oper_ifla(priv
, 0, IF_OPER_UP
);
1207 eloop_unregister_read_sock(drv
->link_event_sock
);
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);
1213 close(drv
->link_event_sock
);
1214 close(drv
->ioctl_sock
);
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
);
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()
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.
1234 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1236 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1237 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
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
1247 static int wpa_driver_nl80211_scan(void *priv
,
1248 struct wpa_driver_scan_params
*params
)
1250 struct wpa_driver_nl80211_data
*drv
= priv
;
1251 int ret
= 0, timeout
;
1252 struct nl_msg
*msg
, *ssids
, *freqs
;
1255 msg
= nlmsg_alloc();
1256 ssids
= nlmsg_alloc();
1257 freqs
= nlmsg_alloc();
1258 if (!msg
|| !ssids
|| !freqs
) {
1265 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1266 NL80211_CMD_TRIGGER_SCAN
, 0);
1268 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
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
);
1274 if (params
->num_ssids
)
1275 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
1277 if (params
->extra_ies
) {
1278 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->extra_ies_len
,
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
);
1288 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1291 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger failed: ret=%d "
1292 "(%s)", ret
, strerror(-ret
));
1293 goto nla_put_failure
;
1296 /* Not all drivers generate "scan completed" wireless event, so try to
1297 * read results after a timeout. */
1299 if (drv
->scan_complete_events
) {
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.
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
,
1321 static int bss_info_handler(struct nl_msg
*msg
, void *arg
)
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
},
1336 struct wpa_scan_results
*res
= arg
;
1337 struct wpa_scan_res
**tmp
;
1338 struct wpa_scan_res
*r
;
1342 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1343 genlmsg_attrlen(gnlh
, 0), NULL
);
1344 if (!tb
[NL80211_ATTR_BSS
])
1346 if (nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
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
]);
1357 r
= os_zalloc(sizeof(*r
) + ie_len
);
1360 if (bss
[NL80211_BSS_BSSID
])
1361 os_memcpy(r
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]),
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
;
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
]);
1383 os_memcpy(r
+ 1, ie
, ie_len
);
1385 tmp
= os_realloc(res
->res
,
1386 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1391 tmp
[res
->num
++] = r
;
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
1403 static struct wpa_scan_results
*
1404 wpa_driver_nl80211_get_scan_results(void *priv
)
1406 struct wpa_driver_nl80211_data
*drv
= priv
;
1408 struct wpa_scan_results
*res
;
1411 res
= os_zalloc(sizeof(*res
));
1414 msg
= nlmsg_alloc();
1416 goto nla_put_failure
;
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
);
1422 ret
= send_and_recv_msgs(drv
, msg
, bss_info_handler
, res
);
1425 wpa_printf(MSG_DEBUG
, "Received scan results (%lu BSSes)",
1426 (unsigned long) res
->num
);
1429 wpa_printf(MSG_DEBUG
, "nl80211: Scan result fetch failed: ret=%d "
1430 "(%s)", ret
, strerror(-ret
));
1433 wpa_scan_results_free(res
);
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
)
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
);
1451 msg
= nlmsg_alloc();
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);
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
);
1465 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1468 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1472 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC02);
1475 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC04);
1478 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC06);
1481 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1482 "algorithm %d", __func__
, alg
);
1489 NLA_PUT(msg
, NL80211_ATTR_KEY_SEQ
, seq_len
, seq
);
1491 if (addr
&& os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
1493 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1494 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1496 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1497 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1499 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1500 if (ret
== -ENOENT
&& alg
== WPA_ALG_NONE
)
1503 wpa_printf(MSG_DEBUG
, "nl80211: set_key failed; err=%d %s)",
1504 ret
, strerror(-ret
));
1507 * If we failed or don't need to set the default TX key (below),
1510 if (ret
|| !set_tx
|| alg
== WPA_ALG_NONE
)
1512 #ifdef HOSTAPD /* FIX: is this needed? */
1515 #endif /* HOSTAPD */
1517 msg
= nlmsg_alloc();
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
);
1528 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
1530 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1534 wpa_printf(MSG_DEBUG
, "nl80211: set_key default failed; "
1535 "err=%d %s)", ret
, strerror(-ret
));
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
,
1547 const u8
*key
, size_t key_len
)
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
);
1555 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
1556 const u8
*addr
, int cmd
, u16 reason_code
)
1561 msg
= nlmsg_alloc();
1565 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0, cmd
, 0);
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
);
1571 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1574 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1575 "(%s)", ret
, strerror(-ret
));
1576 goto nla_put_failure
;
1586 static int wpa_driver_nl80211_deauthenticate(void *priv
, const u8
*addr
,
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
,
1596 static int wpa_driver_nl80211_disassociate(void *priv
, const u8
*addr
,
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
,
1606 static int wpa_driver_nl80211_authenticate(
1607 void *priv
, struct wpa_driver_auth_params
*params
)
1609 struct wpa_driver_nl80211_data
*drv
= priv
;
1612 enum nl80211_auth_type type
;
1614 drv
->associated
= 0;
1616 msg
= nlmsg_alloc();
1620 wpa_printf(MSG_DEBUG
, "nl80211: Authenticate (ifindex=%d)",
1623 for (i
= 0; i
< 4; i
++) {
1624 if (!params
->wep_key
[i
])
1626 wpa_driver_nl80211_set_key(drv
, WPA_ALG_WEP
, NULL
, i
,
1627 i
== params
->wep_tx_keyidx
, NULL
, 0,
1629 params
->wep_key_len
[i
]);
1632 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1633 NL80211_CMD_AUTHENTICATE
, 0);
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
);
1642 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
1643 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
1646 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
1647 params
->ssid
, params
->ssid_len
);
1648 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
1651 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->ie
, params
->ie_len
);
1653 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->ie_len
, params
->ie
);
1655 * TODO: if multiple auth_alg options enabled, try them one by one if
1656 * the AP rejects authentication due to unknown auth alg
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
;
1667 goto nla_put_failure
;
1668 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
1669 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
1671 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1674 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1675 "(%s)", ret
, strerror(-ret
));
1676 goto nla_put_failure
;
1679 wpa_printf(MSG_DEBUG
, "nl80211: Authentication request send "
1688 #if defined(CONFIG_AP) || defined(HOSTAPD)
1690 struct phy_info_arg
{
1692 struct hostapd_hw_modes
*modes
;
1695 static int phy_info_handler(struct nl_msg
*msg
, void *arg
)
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
;
1701 struct nlattr
*tb_band
[NL80211_BAND_ATTR_MAX
+ 1];
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
},
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
},
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
;
1726 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1727 genlmsg_attrlen(gnlh
, 0), NULL
);
1729 if (!tb_msg
[NL80211_ATTR_WIPHY_BANDS
])
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
));
1736 phy_info
->modes
= mode
;
1740 mode
= &phy_info
->modes
[*(phy_info
->num_modes
)];
1741 memset(mode
, 0, sizeof(*mode
));
1742 *(phy_info
->num_modes
) += 1;
1744 nla_parse(tb_band
, NL80211_BAND_ATTR_MAX
, nla_data(nl_band
),
1745 nla_len(nl_band
), NULL
);
1747 if (tb_band
[NL80211_BAND_ATTR_HT_CAPA
]) {
1748 mode
->ht_capab
= nla_get_u16(
1749 tb_band
[NL80211_BAND_ATTR_HT_CAPA
]);
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
])
1757 mode
->num_channels
++;
1760 mode
->channels
= calloc(mode
->num_channels
, sizeof(struct hostapd_channel_data
));
1761 if (!mode
->channels
)
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
])
1772 mode
->channels
[idx
].freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
1773 mode
->channels
[idx
].flag
= 0;
1776 /* crude heuristic */
1777 if (mode
->channels
[idx
].freq
< 4000)
1778 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
1780 mode
->mode
= HOSTAPD_MODE_IEEE80211A
;
1784 /* crude heuristic */
1785 if (mode
->channels
[idx
].freq
< 4000)
1786 if (mode
->channels
[idx
].freq
== 2484)
1787 mode
->channels
[idx
].chan
= 14;
1789 mode
->channels
[idx
].chan
= (mode
->channels
[idx
].freq
- 2407) / 5;
1791 mode
->channels
[idx
].chan
= mode
->channels
[idx
].freq
/5 - 1000;
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
|=
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;
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
])
1822 mode
->rates
= calloc(mode
->num_rates
, sizeof(struct hostapd_rate_data
));
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
])
1833 mode
->rates
[idx
].rate
= nla_get_u32(tb_rate
[NL80211_BITRATE_ATTR_RATE
]);
1835 /* crude heuristic */
1836 if (mode
->mode
== HOSTAPD_MODE_IEEE80211B
&&
1837 mode
->rates
[idx
].rate
> 200)
1838 mode
->mode
= HOSTAPD_MODE_IEEE80211G
;
1840 if (tb_rate
[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
])
1841 mode
->rates
[idx
].flags
|= HOSTAPD_RATE_PREAMBLE2
;
1850 static struct hostapd_hw_modes
*
1851 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes
*modes
, u16
*num_modes
)
1854 struct hostapd_hw_modes
*mode11g
= NULL
, *nmodes
, *mode
;
1855 int i
, mode11g_idx
= -1;
1857 /* If only 802.11g mode is included, use it to construct matching
1858 * 802.11b mode data. */
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
)
1867 if (mode11g_idx
< 0)
1868 return modes
; /* 2.4 GHz band not supported at all */
1870 nmodes
= os_realloc(modes
, (*num_modes
+ 1) * sizeof(*nmodes
));
1872 return modes
; /* Could not add 802.11b mode */
1874 mode
= &nmodes
[*num_modes
];
1875 os_memset(mode
, 0, sizeof(*mode
));
1879 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
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
) {
1887 return modes
; /* Could not add 802.11b mode */
1889 os_memcpy(mode
->channels
, mode11g
->channels
,
1890 mode11g
->num_channels
* sizeof(struct hostapd_channel_data
));
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
);
1897 return modes
; /* Could not add 802.11b mode */
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
))
1905 mode
->rates
[mode
->num_rates
] = mode11g
->rates
[i
];
1907 if (mode
->num_rates
== 4)
1911 if (mode
->num_rates
== 0) {
1912 os_free(mode
->channels
);
1913 os_free(mode
->rates
);
1915 return modes
; /* No 802.11b rates */
1918 wpa_printf(MSG_DEBUG
, "nl80211: Added 802.11b mode based on 802.11g "
1925 static struct hostapd_hw_modes
*
1926 wpa_driver_nl80211_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
1928 struct wpa_driver_nl80211_data
*drv
= priv
;
1930 struct phy_info_arg result
= {
1931 .num_modes
= num_modes
,
1938 msg
= nlmsg_alloc();
1942 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1943 0, NL80211_CMD_GET_WIPHY
, 0);
1945 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1947 if (send_and_recv_msgs(drv
, msg
, phy_info_handler
, &result
) == 0)
1948 return wpa_driver_nl80211_add_11b(result
.modes
, num_modes
);
1953 #endif /* CONFIG_AP || HOSTAPD */
1958 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data
*drv
,
1959 const void *data
, size_t len
,
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) */
1968 0x00, 0x00, /* RX and TX flags to indicate that */
1969 0x00, 0x00, /* this is the injected frame directly */
1971 struct iovec iov
[2] = {
1973 .iov_base
= &rtap_hdr
,
1974 .iov_len
= sizeof(rtap_hdr
),
1977 .iov_base
= (void *) data
,
1981 struct msghdr msg
= {
1986 .msg_control
= NULL
,
1987 .msg_controllen
= 0,
1992 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
1994 return sendmsg(drv
->monitor_sock
, &msg
, 0);
1998 static int wpa_driver_nl80211_send_mlme(void *priv
, const u8
*data
,
2001 struct wpa_driver_nl80211_data
*drv
= priv
;
2002 struct ieee80211_mgmt
*mgmt
;
2003 int do_not_encrypt
= 0;
2006 mgmt
= (struct ieee80211_mgmt
*) data
;
2007 fc
= le_to_host16(mgmt
->frame_control
);
2009 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
2010 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
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.
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))
2024 return wpa_driver_nl80211_send_frame(drv
, data
, data_len
,
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
,
2034 struct wpa_driver_nl80211_data
*drv
= priv
;
2036 u8 cmd
= NL80211_CMD_NEW_BEACON
;
2039 msg
= nlmsg_alloc();
2043 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon (beacon_set=%d)",
2045 if (drv
->beacon_set
)
2046 cmd
= NL80211_CMD_SET_BEACON
;
2048 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 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
);
2058 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2060 wpa_printf(MSG_DEBUG
, "nl80211: Beacon set failed: %d (%s)",
2061 ret
, strerror(-ret
));
2063 drv
->beacon_set
= 1;
2070 static int wpa_driver_nl80211_set_beacon_int(void *priv
, int value
)
2072 struct wpa_driver_nl80211_data
*drv
= priv
;
2075 drv
->beacon_int
= value
;
2077 if (!drv
->beacon_set
)
2080 msg
= nlmsg_alloc();
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
);
2090 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, value
);
2092 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2098 static int wpa_driver_nl80211_set_freq2(
2099 struct wpa_driver_nl80211_data
*drv
,
2100 struct wpa_driver_associate_params
*params
)
2105 msg
= nlmsg_alloc();
2109 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2110 NL80211_CMD_SET_WIPHY
, 0);
2112 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2114 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
2116 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2119 wpa_printf(MSG_DEBUG
, "nl80211: MLME Failed to set channel (freq=%d): "
2120 "%d (%s)", params
->freq
, ret
, strerror(-ret
));
2126 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
2131 msg
= nlmsg_alloc();
2133 goto nla_put_failure
;
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
);
2139 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
2142 wpa_printf(MSG_ERROR
, "Failed to remove interface (ifidx=%d).\n",
2147 static int nl80211_create_iface(struct wpa_driver_nl80211_data
*drv
,
2148 const char *ifname
, enum nl80211_iftype iftype
)
2150 struct nl_msg
*msg
, *flags
= NULL
;
2154 msg
= nlmsg_alloc();
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
);
2164 if (iftype
== NL80211_IFTYPE_MONITOR
) {
2167 flags
= nlmsg_alloc();
2169 goto nla_put_failure
;
2171 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
2173 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
2178 goto nla_put_failure
;
2181 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2184 wpa_printf(MSG_ERROR
, "Failed to create interface %s.",
2189 ifidx
= if_nametoindex(ifname
);
2198 enum ieee80211_msg_type
{
2199 ieee80211_msg_normal
= 0,
2200 ieee80211_msg_tx_callback_ack
= 1,
2201 ieee80211_msg_tx_callback_fail
= 2,
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
);
2213 static void handle_tx_callback(void *ctx
, u8
*buf
, size_t len
, int ok
)
2215 struct ieee80211_hdr
*hdr
;
2216 u16 fc
, type
, stype
;
2218 hdr
= (struct ieee80211_hdr
*) buf
;
2219 fc
= le_to_host16(hdr
->frame_control
);
2221 type
= WLAN_FC_GET_TYPE(fc
);
2222 stype
= WLAN_FC_GET_STYPE(fc
);
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
);
2230 case WLAN_FC_TYPE_CTRL
:
2231 wpa_printf(MSG_DEBUG
, "CTRL (TX callback) %s",
2232 ok
? "ACK" : "fail");
2234 case WLAN_FC_TYPE_DATA
:
2235 ap_tx_status(ctx
, hdr
->addr1
, buf
, len
, ok
);
2238 wpa_printf(MSG_DEBUG
, "unknown TX callback frame type %d",
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
)
2250 struct ieee80211_hdr
*hdr
;
2251 u16 fc
, type
, stype
;
2252 size_t data_len
= len
;
2255 * PS-Poll frames are 16 bytes. All other frames are
2256 * 24 bytes or longer.
2261 hdr
= (struct ieee80211_hdr
*) buf
;
2262 fc
= le_to_host16(hdr
->frame_control
);
2264 type
= WLAN_FC_GET_TYPE(fc
);
2265 stype
= WLAN_FC_GET_STYPE(fc
);
2268 case WLAN_FC_TYPE_DATA
:
2271 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
2274 case WLAN_FC_FROMDS
:
2281 case WLAN_FC_TYPE_CTRL
:
2282 /* discard non-ps-poll frames */
2283 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
2286 case WLAN_FC_TYPE_MGMT
:
2294 case ieee80211_msg_normal
:
2295 /* continue processing */
2297 case ieee80211_msg_tx_callback_ack
:
2298 handle_tx_callback(drv
->ctx
, buf
, data_len
, 1);
2300 case ieee80211_msg_tx_callback_fail
:
2301 handle_tx_callback(drv
->ctx
, buf
, data_len
, 0);
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
);
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
);
2317 case WLAN_FC_TYPE_DATA
:
2318 ap_rx_from_unknown_sta(drv
->ctx
, hdr
->addr2
);
2324 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
2326 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
2328 unsigned char buf
[3000];
2329 struct ieee80211_radiotap_iterator iter
;
2331 struct hostapd_frame_info hfi
;
2332 int injected
= 0, failed
= 0, msg_type
, rxflags
= 0;
2334 len
= recv(sock
, buf
, sizeof(buf
), 0);
2340 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
2341 printf("received invalid radiotap frame\n");
2345 memset(&hfi
, 0, sizeof(hfi
));
2348 ret
= ieee80211_radiotap_iterator_next(&iter
);
2352 printf("received invalid radiotap frame (%d)\n", ret
);
2355 switch (iter
.this_arg_index
) {
2356 case IEEE80211_RADIOTAP_FLAGS
:
2357 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
2360 case IEEE80211_RADIOTAP_RX_FLAGS
:
2363 case IEEE80211_RADIOTAP_TX_FLAGS
:
2365 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
2366 IEEE80211_RADIOTAP_F_TX_FAIL
;
2368 case IEEE80211_RADIOTAP_DATA_RETRIES
:
2370 case IEEE80211_RADIOTAP_CHANNEL
:
2371 /* TODO convert from freq/flags to channel number
2376 case IEEE80211_RADIOTAP_RATE
:
2377 hfi
.datarate
= *iter
.this_arg
* 5;
2379 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
2380 hfi
.ssi_signal
= *iter
.this_arg
;
2385 if (rxflags
&& injected
)
2389 msg_type
= ieee80211_msg_normal
;
2391 msg_type
= ieee80211_msg_tx_callback_fail
;
2393 msg_type
= ieee80211_msg_tx_callback_ack
;
2395 handle_frame(drv
, buf
+ iter
.max_length
,
2396 len
- iter
.max_length
, &hfi
, msg_type
);
2401 * we post-process the filter code later and rewrite
2402 * this to the offset to the last instruction
2407 static struct sock_filter msock_filter_insns
[] = {
2409 * do a little-endian load of the radiotap length field
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),
2420 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
2421 /* put result into X */
2422 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2425 * Allow management frames through, this also gives us those
2426 * management frames that we sent ourselves with status
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),
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.
2441 * Not a regression -- we didn't do it before either.
2446 * drop non-data frames, WDS frames
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),
2463 * add header length to index
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),
2479 * Accept empty data frames, we use those for
2482 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
2483 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
2486 * Accept EAPOL frames
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
),
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),
2500 static struct sock_fprog msock_filter
= {
2501 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
2502 .filter
= msock_filter_insns
,
2506 static int add_monitor_filter(int s
)
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
];
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;
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;
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;
2534 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
2535 &msock_filter
, sizeof(msock_filter
))) {
2536 perror("SO_ATTACH_FILTER");
2545 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data
*drv
)
2548 struct sockaddr_ll ll
;
2553 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->ifname
);
2554 buf
[IFNAMSIZ
- 1] = '\0';
2556 drv
->monitor_ifidx
=
2557 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
);
2559 if (drv
->monitor_ifidx
< 0)
2562 if (wpa_driver_nl80211_get_ifflags_ifname(drv
, buf
, &flags
) != 0) {
2563 wpa_printf(MSG_ERROR
, "Could not get interface '%s' flags",
2567 if (!(flags
& IFF_UP
)) {
2568 if (wpa_driver_nl80211_set_ifflags_ifname(drv
, buf
,
2569 flags
| IFF_UP
) != 0)
2571 wpa_printf(MSG_ERROR
, "Could not set interface '%s' "
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]");
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. */
2592 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
,
2594 perror("monitor socket bind");
2598 optlen
= sizeof(optval
);
2601 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
2602 perror("Failed to set socket priority");
2606 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
2608 printf("Could not register monitor read socket\n");
2614 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2619 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data
*drv
,
2620 struct wpa_driver_associate_params
*params
)
2622 if (drv
->monitor_ifidx
< 0 &&
2623 nl80211_create_monitor_interface(drv
))
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;
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) */
2638 #endif /* CONFIG_AP */
2641 static int wpa_driver_nl80211_associate(
2642 void *priv
, struct wpa_driver_associate_params
*params
)
2644 struct wpa_driver_nl80211_data
*drv
= priv
;
2649 if (params
->mode
== 2)
2650 return wpa_driver_nl80211_ap(drv
, params
);
2651 #endif /* CONFIG_AP */
2653 wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
,
2654 params
->drop_unencrypted
);
2656 drv
->associated
= 0;
2658 msg
= nlmsg_alloc();
2662 wpa_printf(MSG_DEBUG
, "nl80211: Associate (ifindex=%d)",
2664 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2665 NL80211_CMD_ASSOCIATE
, 0);
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
);
2674 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
2675 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
2678 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
2679 params
->ssid
, params
->ssid_len
);
2680 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
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
;
2687 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
2689 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
2692 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2695 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
2696 "(%s)", ret
, strerror(-ret
));
2697 goto nla_put_failure
;
2700 wpa_printf(MSG_DEBUG
, "nl80211: Association request send "
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
2715 static int wpa_driver_nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
,
2718 int ret
= -1, flags
;
2724 nlmode
= NL80211_IFTYPE_STATION
;
2727 nlmode
= NL80211_IFTYPE_ADHOC
;
2730 nlmode
= NL80211_IFTYPE_AP
;
2736 msg
= nlmsg_alloc();
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
);
2745 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2752 wpa_printf(MSG_ERROR
, "nl80211: Failed to set interface mode: %d (%s)",
2753 ret
, strerror(-ret
));
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
2761 if (wpa_driver_nl80211_get_ifflags(drv
, &flags
) == 0) {
2762 (void) wpa_driver_nl80211_set_ifflags(drv
, flags
& ~IFF_UP
);
2764 /* Try to set the mode again while the interface is down */
2765 msg
= nlmsg_alloc();
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
);
2775 wpa_printf(MSG_ERROR
, "Failed to set interface %s "
2776 "mode(try_again): %d (%s)",
2777 drv
->ifname
, ret
, strerror(-ret
));
2780 /* Ignore return value of get_ifflags to ensure that the device
2781 * is always up like it was before this function was called.
2783 (void) wpa_driver_nl80211_get_ifflags(drv
, &flags
);
2784 (void) wpa_driver_nl80211_set_ifflags(drv
, flags
| IFF_UP
);
2791 static int wpa_driver_nl80211_get_capa(void *priv
,
2792 struct wpa_driver_capa
*capa
)
2794 struct wpa_driver_nl80211_data
*drv
= priv
;
2795 if (!drv
->has_capability
)
2797 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
2802 static int wpa_driver_nl80211_set_operstate(void *priv
, int state
)
2804 struct wpa_driver_nl80211_data
*drv
= priv
;
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
);
2816 static const u8 rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2818 enum ieee80211_msg_type
{
2819 ieee80211_msg_normal
= 0,
2820 ieee80211_msg_tx_callback_ack
= 1,
2821 ieee80211_msg_tx_callback_fail
= 2,
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
);
2829 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
2832 struct i802_bss
*bss
= &drv
->bss
;
2834 if (os_strncmp(iface
, bss
->ifname
, IFNAMSIZ
) == 0)
2838 wpa_printf(MSG_DEBUG
, "nl80211: get_bss(%s) failed", iface
);
2843 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
2848 wpa_printf(MSG_DEBUG
, "nl80211: Add own interface ifindex %d",
2850 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
2851 if (drv
->if_indices
[i
] == 0) {
2852 drv
->if_indices
[i
] = ifidx
;
2857 if (drv
->if_indices
!= drv
->default_if_indices
)
2858 old
= drv
->if_indices
;
2862 drv
->if_indices
= realloc(old
,
2863 sizeof(int) * (drv
->num_if_indices
+ 1));
2864 if (!drv
->if_indices
) {
2866 drv
->if_indices
= drv
->default_if_indices
;
2868 drv
->if_indices
= old
;
2869 wpa_printf(MSG_ERROR
, "Failed to reallocate memory for "
2871 wpa_printf(MSG_ERROR
, "Ignoring EAPOL on interface %d", ifidx
);
2874 drv
->if_indices
[drv
->num_if_indices
] = ifidx
;
2875 drv
->num_if_indices
++;
2879 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
2883 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
2884 if (drv
->if_indices
[i
] == ifidx
) {
2885 drv
->if_indices
[i
] = 0;
2892 static int have_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
2896 for (i
= 0; i
< drv
->num_if_indices
; i
++)
2897 if (drv
->if_indices
[i
] == ifidx
)
2904 static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data
*drv
,
2905 const char *ifname
, int dev_up
)
2909 if (drv
->ioctl_sock
< 0)
2912 memset(&ifr
, 0, sizeof(ifr
));
2913 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2915 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, &ifr
) != 0) {
2916 perror("ioctl[SIOCGIFFLAGS]");
2917 wpa_printf(MSG_DEBUG
, "Could not read interface flags (%s)",
2923 ifr
.ifr_flags
|= IFF_UP
;
2925 ifr
.ifr_flags
&= ~IFF_UP
;
2927 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, &ifr
) != 0) {
2928 perror("ioctl[SIOCSIFFLAGS]");
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
)
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
);
2946 static inline int min_int(int a
, int b
)
2954 static int get_key_handler(struct nl_msg
*msg
, void *arg
)
2956 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
2957 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
2959 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
2960 genlmsg_attrlen(gnlh
, 0), NULL
);
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.
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));
2975 static int i802_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
2978 struct wpa_driver_nl80211_data
*drv
= priv
;
2981 msg
= nlmsg_alloc();
2985 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2986 0, NL80211_CMD_GET_KEY
, 0);
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
));
2995 return send_and_recv_msgs(drv
, msg
, get_key_handler
, seq
);
3001 static int i802_set_rate_sets(void *priv
, int *supp_rates
, int *basic_rates
,
3004 struct wpa_driver_nl80211_data
*drv
= priv
;
3006 u8 rates
[NL80211_MAX_SUPP_RATES
];
3010 msg
= nlmsg_alloc();
3014 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3015 NL80211_CMD_SET_BSS
, 0);
3017 for (i
= 0; i
< NL80211_MAX_SUPP_RATES
&& basic_rates
[i
] >= 0; i
++)
3018 rates
[rates_len
++] = basic_rates
[i
] / 5;
3020 NLA_PUT(msg
, NL80211_ATTR_BSS_BASIC_RATES
, rates_len
, rates
);
3022 /* TODO: multi-BSS support */
3023 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3025 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3031 static int i802_send_frame(void *priv
, const void *data
, size_t len
,
3032 int encrypt
, int flags
)
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) */
3040 0x00, 0x00, /* RX and TX flags to indicate that */
3041 0x00, 0x00, /* this is the injected frame directly */
3043 struct wpa_driver_nl80211_data
*drv
= priv
;
3044 struct iovec iov
[2] = {
3046 .iov_base
= &rtap_hdr
,
3047 .iov_len
= sizeof(rtap_hdr
),
3050 .iov_base
= (void*)data
,
3054 struct msghdr msg
= {
3059 .msg_control
= NULL
,
3060 .msg_controllen
= 0,
3065 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
3067 return sendmsg(drv
->monitor_sock
, &msg
, flags
);
3070 static int i802_send_mgmt_frame(void *priv
, const void *data
, size_t len
,
3073 struct ieee80211_mgmt
*mgmt
;
3074 int do_not_encrypt
= 0;
3077 mgmt
= (struct ieee80211_mgmt
*) data
;
3078 fc
= le_to_host16(mgmt
->frame_control
);
3080 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
3081 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
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.
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))
3095 return i802_send_frame(priv
, data
, len
, !do_not_encrypt
, flags
);
3098 /* Set kernel driver on given frequency (MHz) */
3099 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
)
3101 struct wpa_driver_nl80211_data
*drv
= priv
;
3104 msg
= nlmsg_alloc();
3108 drv
->last_freq
= freq
->freq
;
3109 drv
->last_freq_ht
= freq
->ht_enabled
;
3111 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3112 NL80211_CMD_SET_WIPHY
, 0);
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
) {
3119 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
3120 NL80211_CHAN_HT40MINUS
);
3123 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
3124 NL80211_CHAN_HT40PLUS
);
3127 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
3133 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
3140 static int i802_set_rts(void *priv
, int rts
)
3142 struct wpa_driver_nl80211_data
*drv
= priv
;
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;
3150 if (ioctl(drv
->ioctl_sock
, SIOCSIWRTS
, &iwr
) < 0) {
3151 perror("ioctl[SIOCSIWRTS]");
3159 static int i802_set_frag(void *priv
, int frag
)
3161 struct wpa_driver_nl80211_data
*drv
= priv
;
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;
3169 if (ioctl(drv
->ioctl_sock
, SIOCSIWFRAG
, &iwr
) < 0) {
3170 perror("ioctl[SIOCSIWFRAG]");
3178 static int i802_set_retry(void *priv
, int short_retry
, int long_retry
)
3180 struct wpa_driver_nl80211_data
*drv
= priv
;
3183 memset(&iwr
, 0, sizeof(iwr
));
3184 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
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)]");
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)]");
3204 static int i802_flush(void *priv
)
3206 struct wpa_driver_nl80211_data
*drv
= priv
;
3209 msg
= nlmsg_alloc();
3213 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3214 0, NL80211_CMD_DEL_STATION
, 0);
3217 * XXX: FIX! this needs to flush all VLANs too
3219 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3220 if_nametoindex(drv
->ifname
));
3222 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3228 static int get_sta_handler(struct nl_msg
*msg
, void *arg
)
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
},
3242 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3243 genlmsg_attrlen(gnlh
, 0), NULL
);
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.
3251 if (!tb
[NL80211_ATTR_STA_INFO
]) {
3252 wpa_printf(MSG_DEBUG
, "sta stats missing!");
3255 if (nla_parse_nested(stats
, NL80211_STA_INFO_MAX
,
3256 tb
[NL80211_ATTR_STA_INFO
],
3258 wpa_printf(MSG_DEBUG
, "failed to parse nested attributes!");
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
])
3271 nla_get_u32(stats
[NL80211_STA_INFO_RX_PACKETS
]);
3272 if (stats
[NL80211_STA_INFO_TX_PACKETS
])
3274 nla_get_u32(stats
[NL80211_STA_INFO_TX_PACKETS
]);
3279 static int i802_read_sta_data(void *priv
, struct hostap_sta_driver_data
*data
,
3282 struct wpa_driver_nl80211_data
*drv
= priv
;
3285 os_memset(data
, 0, sizeof(*data
));
3286 msg
= nlmsg_alloc();
3290 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3291 0, NL80211_CMD_GET_STATION
, 0);
3293 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3294 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3296 return send_and_recv_msgs(drv
, msg
, get_sta_handler
, data
);
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
)
3305 struct wpa_driver_nl80211_data
*drv
= priv
;
3306 struct ieee80211_hdr
*hdr
;
3311 int qos
= sta
->flags
& WLAN_STA_WME
;
3316 len
= sizeof(*hdr
) + (qos
? 2 : 0) + sizeof(rfc1042_header
) + 2 +
3318 hdr
= os_zalloc(len
);
3320 printf("malloc() failed for i802_send_data(len=%lu)\n",
3321 (unsigned long) len
);
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
);
3329 hdr
->frame_control
|= host_to_le16(WLAN_FC_ISWEP
);
3330 #if 0 /* To be enabled if qos determination is added above */
3332 hdr
->frame_control
|=
3333 host_to_le16(WLAN_FC_STYPE_QOS_DATA
<< 4);
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);
3342 #if 0 /* To be enabled if qos determination is added above */
3344 /* add an empty QoS header if needed */
3351 memcpy(pos
, rfc1042_header
, sizeof(rfc1042_header
));
3352 pos
+= sizeof(rfc1042_header
);
3353 WPA_PUT_BE16(pos
, ETH_P_PAE
);
3355 memcpy(pos
, data
, data_len
);
3357 res
= i802_send_frame(drv
, (u8
*) hdr
, len
, encrypt
, 0);
3361 perror("i802_send_eapol: send");
3362 printf("i802_send_eapol - packet len: %lu - failed\n",
3363 (unsigned long) len
);
3370 static int i802_sta_add(const char *ifname
, void *priv
,
3371 struct hostapd_sta_add_params
*params
)
3373 struct wpa_driver_nl80211_data
*drv
= priv
;
3377 msg
= nlmsg_alloc();
3381 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3382 0, NL80211_CMD_NEW_STATION
, 0);
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
);
3393 #ifdef CONFIG_IEEE80211N
3394 if (params
->ht_capabilities
) {
3395 NLA_PUT(msg
, NL80211_ATTR_HT_CAPABILITY
,
3396 params
->ht_capabilities
->length
,
3397 ¶ms
->ht_capabilities
->data
);
3399 #endif /* CONFIG_IEEE80211N */
3401 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3403 wpa_printf(MSG_DEBUG
, "nl80211: NL80211_CMD_NEW_STATION "
3404 "result: %d (%s)", ret
, strerror(-ret
));
3412 static int i802_sta_remove(void *priv
, const u8
*addr
)
3414 struct wpa_driver_nl80211_data
*drv
= priv
;
3418 msg
= nlmsg_alloc();
3422 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3423 0, NL80211_CMD_DEL_STATION
, 0);
3425 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3426 if_nametoindex(drv
->ifname
));
3427 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3429 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3438 static int i802_sta_set_flags(void *priv
, const u8
*addr
,
3439 int total_flags
, int flags_or
, int flags_and
)
3441 struct wpa_driver_nl80211_data
*drv
= priv
;
3442 struct nl_msg
*msg
, *flags
= NULL
;
3444 msg
= nlmsg_alloc();
3448 flags
= nlmsg_alloc();
3454 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3455 0, NL80211_CMD_SET_STATION
, 0);
3457 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3458 if_nametoindex(drv
->ifname
));
3459 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3461 if (total_flags
& WLAN_STA_AUTHORIZED
)
3462 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_AUTHORIZED
);
3464 if (total_flags
& WLAN_STA_WMM
)
3465 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_WME
);
3467 if (total_flags
& WLAN_STA_SHORT_PREAMBLE
)
3468 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_SHORT_PREAMBLE
);
3470 if (total_flags
& WLAN_STA_MFP
)
3471 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_MFP
);
3473 if (nla_put_nested(msg
, NL80211_ATTR_STA_FLAGS
, flags
))
3474 goto nla_put_failure
;
3478 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3485 static int i802_set_tx_queue_params(void *priv
, int queue
, int aifs
,
3486 int cw_min
, int cw_max
, int burst_time
)
3488 struct wpa_driver_nl80211_data
*drv
= priv
;
3490 struct nlattr
*txq
, *params
;
3492 msg
= nlmsg_alloc();
3496 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3497 0, NL80211_CMD_SET_WIPHY
, 0);
3499 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3501 txq
= nla_nest_start(msg
, NL80211_ATTR_WIPHY_TXQ_PARAMS
);
3503 goto nla_put_failure
;
3505 /* We are only sending parameters for a single TXQ at a time */
3506 params
= nla_nest_start(msg
, 1);
3508 goto nla_put_failure
;
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
);
3518 nla_nest_end(msg
, params
);
3520 nla_nest_end(msg
, txq
);
3522 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
3529 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3533 /* stop listening for EAPOL on this interface */
3534 del_ifidx(drv
, ifidx
);
3536 msg
= nlmsg_alloc();
3538 goto nla_put_failure
;
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
);
3544 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
3547 printf("Failed to remove interface.\n");
3551 static int nl80211_create_iface(struct wpa_driver_nl80211_data
*drv
,
3553 enum nl80211_iftype iftype
,
3556 struct nl_msg
*msg
, *flags
= NULL
;
3562 msg
= nlmsg_alloc();
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
);
3572 if (iftype
== NL80211_IFTYPE_MONITOR
) {
3575 flags
= nlmsg_alloc();
3577 goto nla_put_failure
;
3579 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
3581 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
3586 goto nla_put_failure
;
3589 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3592 printf("Failed to create interface %s.\n", ifname
);
3596 ifidx
= if_nametoindex(ifname
);
3601 /* start listening for EAPOL on this interface */
3602 add_ifidx(drv
, ifidx
);
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
;
3611 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifreq
)) {
3612 nl80211_remove_iface(drv
, ifidx
);
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
))
3634 static int i802_bss_add(void *priv
, const char *ifname
, const u8
*bssid
)
3636 struct wpa_driver_nl80211_data
*drv
= priv
;
3638 struct i802_bss
*bss
;
3640 bss
= os_zalloc(sizeof(*bss
));
3643 os_strlcpy(bss
->ifname
, ifname
, IFNAMSIZ
);
3645 ifidx
= nl80211_create_iface(priv
, ifname
, NL80211_IFTYPE_AP
, bssid
);
3650 if (hostapd_set_iface_flags(priv
, ifname
, 1)) {
3651 nl80211_remove_iface(priv
, ifidx
);
3655 bss
->next
= drv
->bss
.next
;
3656 drv
->bss
.next
= bss
;
3661 static int i802_bss_remove(void *priv
, const char *ifname
)
3663 struct wpa_driver_nl80211_data
*drv
= priv
;
3664 struct i802_bss
*bss
, *prev
;
3665 nl80211_remove_iface(priv
, if_nametoindex(ifname
));
3667 bss
= drv
->bss
.next
;
3669 if (os_strncmp(ifname
, bss
->ifname
, IFNAMSIZ
) == 0) {
3670 prev
->next
= bss
->next
;
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
)
3685 struct wpa_driver_nl80211_data
*drv
= priv
;
3687 u8 cmd
= NL80211_CMD_NEW_BEACON
;
3689 struct i802_bss
*bss
;
3691 bss
= get_bss(drv
, iface
);
3695 msg
= nlmsg_alloc();
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
;
3704 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 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
);
3712 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3714 bss
->beacon_set
= 1;
3721 static int i802_del_beacon(struct wpa_driver_nl80211_data
*drv
)
3725 msg
= nlmsg_alloc();
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
));
3733 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3739 static int i802_set_beacon_int(void *priv
, int value
)
3741 struct wpa_driver_nl80211_data
*drv
= priv
;
3744 drv
->beacon_int
= value
;
3746 if (!drv
->bss
.beacon_set
)
3749 msg
= nlmsg_alloc();
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
));
3759 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, value
);
3761 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3767 static int i802_set_bss(void *priv
, int cts
, int preamble
, int slot
)
3769 struct wpa_driver_nl80211_data
*drv
= priv
;
3772 msg
= nlmsg_alloc();
3776 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3777 NL80211_CMD_SET_BSS
, 0);
3780 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_CTS_PROT
, cts
);
3782 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_PREAMBLE
, preamble
);
3784 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_SLOT_TIME
, slot
);
3786 /* TODO: multi-BSS support */
3787 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3789 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3795 static int i802_set_cts_protect(void *priv
, int value
)
3797 return i802_set_bss(priv
, value
, -1, -1);
3801 static int i802_set_preamble(void *priv
, int value
)
3803 return i802_set_bss(priv
, -1, value
, -1);
3807 static int i802_set_short_slot_time(void *priv
, int value
)
3809 return i802_set_bss(priv
, -1, -1, value
);
3813 static enum nl80211_iftype
i802_if_type(enum hostapd_driver_if_type type
)
3816 case HOSTAPD_IF_VLAN
:
3817 return NL80211_IFTYPE_AP_VLAN
;
3818 case HOSTAPD_IF_WDS
:
3819 return NL80211_IFTYPE_WDS
;
3825 static int i802_if_add(const char *iface
, void *priv
,
3826 enum hostapd_driver_if_type type
, char *ifname
,
3829 if (nl80211_create_iface(priv
, ifname
, i802_if_type(type
), addr
) < 0)
3835 static int i802_if_update(void *priv
, enum hostapd_driver_if_type type
,
3836 char *ifname
, const u8
*addr
)
3838 /* unused at the moment */
3843 static int i802_if_remove(void *priv
, enum hostapd_driver_if_type type
,
3844 const char *ifname
, const u8
*addr
)
3846 nl80211_remove_iface(priv
, if_nametoindex(ifname
));
3851 static int i802_set_sta_vlan(void *priv
, const u8
*addr
,
3852 const char *ifname
, int vlan_id
)
3854 struct wpa_driver_nl80211_data
*drv
= priv
;
3857 msg
= nlmsg_alloc();
3861 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3862 0, NL80211_CMD_SET_STATION
, 0);
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
));
3870 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3876 static int i802_set_country(void *priv
, const char *country
)
3878 struct wpa_driver_nl80211_data
*drv
= priv
;
3882 msg
= nlmsg_alloc();
3886 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3887 0, NL80211_CMD_REQ_SET_REG
, 0);
3889 alpha2
[0] = country
[0];
3890 alpha2
[1] = country
[1];
3892 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
3894 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3900 static void handle_tx_callback(struct hostapd_data
*hapd
, u8
*buf
, size_t len
,
3903 struct ieee80211_hdr
*hdr
;
3904 u16 fc
, type
, stype
;
3906 hdr
= (struct ieee80211_hdr
*) buf
;
3907 fc
= le_to_host16(hdr
->frame_control
);
3909 type
= WLAN_FC_GET_TYPE(fc
);
3910 stype
= WLAN_FC_GET_STYPE(fc
);
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
);
3918 case WLAN_FC_TYPE_CTRL
:
3919 wpa_printf(MSG_DEBUG
, "CTRL (TX callback) %s",
3920 ok
? "ACK" : "fail");
3922 case WLAN_FC_TYPE_DATA
:
3923 hostapd_tx_status(hapd
, hdr
->addr1
, buf
, len
, ok
);
3926 printf("unknown TX callback frame type %d\n", type
);
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
)
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;
3946 * PS-Poll frames are 16 bytes. All other frames are
3947 * 24 bytes or longer.
3952 hdr
= (struct ieee80211_hdr
*) buf
;
3953 fc
= le_to_host16(hdr
->frame_control
);
3955 type
= WLAN_FC_GET_TYPE(fc
);
3956 stype
= WLAN_FC_GET_STYPE(fc
);
3959 case WLAN_FC_TYPE_DATA
:
3962 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
3966 case WLAN_FC_FROMDS
:
3974 case WLAN_FC_TYPE_CTRL
:
3975 /* discard non-ps-poll frames */
3976 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
3980 case WLAN_FC_TYPE_MGMT
:
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
];
3997 hapd
= iface
->bss
[0];
3999 if (bssid
[0] != 0xff || bssid
[1] != 0xff ||
4000 bssid
[2] != 0xff || bssid
[3] != 0xff ||
4001 bssid
[4] != 0xff || bssid
[5] != 0xff) {
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)
4008 if ((type
!= WLAN_FC_TYPE_MGMT
||
4009 stype
!= WLAN_FC_STYPE_BEACON
))
4012 broadcast_bssid
= 1;
4016 case ieee80211_msg_normal
:
4017 /* continue processing */
4019 case ieee80211_msg_tx_callback_ack
:
4020 handle_tx_callback(hapd
, buf
, data_len
, 1);
4022 case ieee80211_msg_tx_callback_fail
:
4023 handle_tx_callback(hapd
, buf
, data_len
, 0);
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
,
4037 hostapd_mgmt_rx(hapd
, buf
, data_len
, stype
, hfi
);
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
);
4044 case WLAN_FC_TYPE_DATA
:
4045 hostapd_rx_from_unknown_sta(drv
->hapd
, hdr
->addr2
);
4051 static void handle_eapol(int sock
, void *eloop_ctx
, void *sock_ctx
)
4053 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
4054 struct sockaddr_ll lladdr
;
4055 unsigned char buf
[3000];
4057 socklen_t fromlen
= sizeof(lladdr
);
4059 len
= recvfrom(sock
, buf
, sizeof(buf
), 0,
4060 (struct sockaddr
*)&lladdr
, &fromlen
);
4066 if (have_ifidx(drv
, lladdr
.sll_ifindex
)) {
4067 struct hostapd_data
*hapd
;
4068 hapd
= hostapd_sta_get_bss(drv
->hapd
, lladdr
.sll_addr
);
4071 hostapd_eapol_receive(hapd
, lladdr
.sll_addr
, buf
, len
);
4076 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
4078 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
4080 unsigned char buf
[3000];
4081 struct hostapd_data
*hapd
= drv
->hapd
;
4082 struct ieee80211_radiotap_iterator iter
;
4084 struct hostapd_frame_info hfi
;
4085 int injected
= 0, failed
= 0, msg_type
, rxflags
= 0;
4087 len
= recv(sock
, buf
, sizeof(buf
), 0);
4093 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
4094 printf("received invalid radiotap frame\n");
4098 memset(&hfi
, 0, sizeof(hfi
));
4101 ret
= ieee80211_radiotap_iterator_next(&iter
);
4105 printf("received invalid radiotap frame (%d)\n", ret
);
4108 switch (iter
.this_arg_index
) {
4109 case IEEE80211_RADIOTAP_FLAGS
:
4110 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
4113 case IEEE80211_RADIOTAP_RX_FLAGS
:
4116 case IEEE80211_RADIOTAP_TX_FLAGS
:
4118 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
4119 IEEE80211_RADIOTAP_F_TX_FAIL
;
4121 case IEEE80211_RADIOTAP_DATA_RETRIES
:
4123 case IEEE80211_RADIOTAP_CHANNEL
:
4124 /* TODO convert from freq/flags to channel number
4129 case IEEE80211_RADIOTAP_RATE
:
4130 hfi
.datarate
= *iter
.this_arg
* 5;
4132 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
4133 hfi
.ssi_signal
= *iter
.this_arg
;
4138 if (rxflags
&& injected
)
4142 msg_type
= ieee80211_msg_normal
;
4144 msg_type
= ieee80211_msg_tx_callback_fail
;
4146 msg_type
= ieee80211_msg_tx_callback_ack
;
4148 handle_frame(drv
, hapd
->iface
, buf
+ iter
.max_length
,
4149 len
- iter
.max_length
, &hfi
, msg_type
);
4154 * we post-process the filter code later and rewrite
4155 * this to the offset to the last instruction
4160 static struct sock_filter msock_filter_insns
[] = {
4162 * do a little-endian load of the radiotap length field
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),
4173 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
4174 /* put result into X */
4175 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
4178 * Allow management frames through, this also gives us those
4179 * management frames that we sent ourselves with status
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),
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.
4194 * Not a regression -- we didn't do it before either.
4199 * drop non-data frames, WDS frames
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),
4216 * add header length to index
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),
4232 * Accept empty data frames, we use those for
4235 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
4236 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
4239 * Accept EAPOL frames
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
),
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),
4253 static struct sock_fprog msock_filter
= {
4254 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
4255 .filter
= msock_filter_insns
,
4259 static int add_monitor_filter(int s
)
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
];
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;
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;
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;
4287 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
4288 &msock_filter
, sizeof(msock_filter
))) {
4289 perror("SO_ATTACH_FILTER");
4297 static int nl80211_create_monitor_interface(struct wpa_driver_nl80211_data
*drv
)
4300 struct sockaddr_ll ll
;
4304 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->ifname
);
4305 buf
[IFNAMSIZ
- 1] = '\0';
4307 drv
->monitor_ifidx
=
4308 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
, NULL
);
4310 if (drv
->monitor_ifidx
< 0)
4313 if (hostapd_set_iface_flags(drv
, buf
, 1))
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]");
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. */
4331 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
,
4333 perror("monitor socket bind");
4337 optlen
= sizeof(optval
);
4340 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
4341 perror("Failed to set socket priority");
4345 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
4347 printf("Could not register monitor read socket\n");
4353 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
4358 static int nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
, const char *ifname
,
4364 msg
= nlmsg_alloc();
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
);
4374 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4378 wpa_printf(MSG_ERROR
, "Failed to set interface %s to master "
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
)
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
;
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;
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
);
4412 nnei
= os_realloc(drv
->neighbors
, (drv
->num_neighbors
+ 1) *
4413 sizeof(struct hostapd_neighbor_bss
));
4416 drv
->neighbors
= nnei
;
4417 nnei
= &nnei
[drv
->num_neighbors
];
4418 os_memcpy(nnei
->bssid
, bssid
, ETH_ALEN
);
4421 nnei
->pri_chan
= pri_chan
;
4422 nnei
->sec_chan
= sec_chan
;
4423 drv
->num_neighbors
++;
4427 static int i802_get_scan_freq(struct iw_event
*iwe
, int *freq
)
4429 int divi
= 1000000, i
;
4431 if (iwe
->u
.freq
.e
== 0) {
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.
4444 if (iwe
->u
.freq
.m
>= 1 && iwe
->u
.freq
.m
<= 13) {
4445 *freq
= 2407 + 5 * iwe
->u
.freq
.m
;
4447 } else if (iwe
->u
.freq
.m
== 14) {
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
);
4459 for (i
= 0; i
< iwe
->u
.freq
.e
; i
++)
4461 *freq
= iwe
->u
.freq
.m
/ divi
;
4466 static int i802_parse_scan(struct wpa_driver_nl80211_data
*drv
, u8
*res_buf
,
4471 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
4472 char *pos
, *end
, *custom
;
4481 pos
= (char *) res_buf
;
4482 end
= (char *) res_buf
+ len
;
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
)
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
);
4499 os_memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
4500 custom
+= IW_EV_POINT_OFF
;
4506 i802_add_neighbor(drv
, bssid
, freq
, ie
,
4509 os_memcpy(bssid
, iwe
->u
.ap_addr
.sa_data
, ETH_ALEN
);
4515 i802_get_scan_freq(iwe
, &freq
);
4518 if (custom
+ iwe
->u
.data
.length
> end
) {
4519 wpa_printf(MSG_ERROR
, "IWEVGENIE overflow");
4523 ie_len
= iwe
->u
.data
.length
;
4531 i802_add_neighbor(drv
, bssid
, freq
, ie
, ie_len
);
4537 static int i802_get_ht_scan_res(struct wpa_driver_nl80211_data
*drv
)
4544 res_buf_len
= IW_SCAN_MAX_DATA
;
4546 res_buf
= os_malloc(res_buf_len
);
4547 if (res_buf
== NULL
)
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
;
4554 if (ioctl(drv
->ioctl_sock
, SIOCGIWSCAN
, &iwr
) == 0)
4557 if (errno
== E2BIG
&& res_buf_len
< 65535) {
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
);
4567 perror("ioctl[SIOCGIWSCAN]");
4573 if (iwr
.u
.data
.length
> res_buf_len
) {
4578 res
= i802_parse_scan(drv
, res_buf
, iwr
.u
.data
.length
);
4585 static int i802_is_event_wireless_scan_complete(char *data
, int len
)
4587 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
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
)
4607 static int i802_is_rtm_scan_complete(int ifindex
, struct nlmsghdr
*h
, int len
)
4609 struct ifinfomsg
*ifi
;
4610 int attrlen
, _nlmsg_len
, rta_len
;
4611 struct rtattr
*attr
;
4613 if (len
< (int) sizeof(*ifi
))
4616 ifi
= NLMSG_DATA(h
);
4618 if (ifindex
!= ifi
->ifi_index
)
4619 return 0; /* event for foreign ifindex */
4621 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
4623 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
4627 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
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
))
4636 attr
= RTA_NEXT(attr
, attrlen
);
4643 static int i802_is_scan_complete(int s
, int ifindex
)
4649 left
= recv(s
, buf
, sizeof(buf
), MSG_DONTWAIT
);
4651 perror("recv(netlink)");
4655 h
= (struct nlmsghdr
*) buf
;
4656 while (left
>= (int) sizeof(*h
)) {
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",
4668 switch (h
->nlmsg_type
) {
4670 if (i802_is_rtm_scan_complete(ifindex
, h
, plen
))
4675 len
= NLMSG_ALIGN(len
);
4677 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
4684 static int i802_ht_scan(struct wpa_driver_nl80211_data
*drv
)
4687 int s
, res
, ifindex
;
4688 struct sockaddr_nl local
;
4693 wpa_printf(MSG_DEBUG
, "nl80211: Scanning overlapping BSSes before "
4694 "starting HT 20/40 MHz BSS");
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]");
4705 ifindex
= if_nametoindex(drv
->ifname
);
4707 /* Wait for scan completion event or timeout */
4708 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
4710 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
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)");
4724 end
+= 30; /* Wait at most 30 seconds for scan results */
4727 tv
.tv_sec
= end
> now
? end
- now
: 0;
4731 res
= select(s
+ 1, &rfds
, NULL
, NULL
, &tv
);
4734 /* Assume results are ready after 10 seconds wait */
4738 if (i802_is_scan_complete(s
, ifindex
)) {
4739 wpa_printf(MSG_DEBUG
, "nl80211: Scan "
4744 wpa_printf(MSG_DEBUG
, "nl80211: Scan timeout");
4745 /* Assume results are ready to be read now */
4752 return i802_get_ht_scan_res(drv
);
4754 #endif /* CONFIG_IEEE80211N */
4757 static int i802_init_sockets(struct wpa_driver_nl80211_data
*drv
, const u8
*bssid
)
4761 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
4762 if (drv
->ioctl_sock
< 0) {
4763 perror("socket[PF_INET,SOCK_DGRAM]");
4767 /* start listening for EAPOL on the default AP interface */
4768 add_ifidx(drv
, if_nametoindex(drv
->ifname
));
4770 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 0))
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
;
4778 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifr
)) {
4779 perror("ioctl(SIOCSIFHWADDR)");
4785 * initialise generic netlink and nl80211
4787 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
4789 printf("Failed to allocate netlink callbacks.\n");
4793 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
4794 if (!drv
->nl_handle
) {
4795 printf("Failed to allocate netlink handle.\n");
4799 if (genl_connect(drv
->nl_handle
)) {
4800 printf("Failed to connect to generic netlink.\n");
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");
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");
4815 #endif /* CONFIG_LIBNL20 */
4817 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
4818 if (!drv
->nl80211
) {
4819 printf("nl80211 not found.\n");
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");
4834 #endif /* CONFIG_IEEE80211N */
4836 /* Initialise a monitor interface */
4837 if (nl80211_create_monitor_interface(drv
))
4840 if (nl80211_set_mode(drv
, drv
->ifname
, NL80211_IFTYPE_AP
))
4843 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1))
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)");
4852 if (eloop_register_read_sock(drv
->eapol_sock
, handle_eapol
, drv
, NULL
))
4854 printf("Could not register read socket for eapol\n");
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)");
4865 if (ifr
.ifr_hwaddr
.sa_family
!= ARPHRD_ETHER
) {
4866 printf("Invalid HW-addr family 0x%04x\n",
4867 ifr
.ifr_hwaddr
.sa_family
);
4870 memcpy(drv
->hapd
->own_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
4875 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
4880 static int i802_get_inact_sec(void *priv
, const u8
*addr
)
4882 struct hostap_sta_driver_data data
;
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)
4889 return data
.inactive_msec
/ 1000;
4893 static int i802_sta_clear_stats(void *priv
, const u8
*addr
)
4903 hostapd_wireless_event_wireless_custom(struct wpa_driver_nl80211_data
*drv
,
4906 wpa_printf(MSG_DEBUG
, "Custom wireless event: '%s'", custom
);
4908 if (strncmp(custom
, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
4911 pos
= strstr(custom
, "addr=");
4913 wpa_printf(MSG_DEBUG
,
4914 "MLME-MICHAELMICFAILURE.indication "
4915 "without sender address ignored");
4919 if (hwaddr_aton(pos
, addr
) == 0) {
4920 hostapd_michael_mic_failure(drv
->hapd
, addr
);
4922 wpa_printf(MSG_DEBUG
,
4923 "MLME-MICHAELMICFAILURE.indication "
4924 "with invalid MAC address");
4930 static void hostapd_wireless_event_wireless(struct wpa_driver_nl80211_data
*drv
,
4931 char *data
, int len
)
4933 struct iw_event iwe_buf
, *iwe
= &iwe_buf
;
4934 char *pos
, *end
, *custom
, *buf
;
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
)
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
);
4958 memcpy(&iwe_buf
, pos
, sizeof(struct iw_event
));
4959 custom
+= IW_EV_POINT_OFF
;
4964 if (custom
+ iwe
->u
.data
.length
> end
)
4966 buf
= malloc(iwe
->u
.data
.length
+ 1);
4969 memcpy(buf
, custom
, iwe
->u
.data
.length
);
4970 buf
[iwe
->u
.data
.length
] = '\0';
4971 hostapd_wireless_event_wireless_custom(drv
, buf
);
4981 static void hostapd_wireless_event_rtm_newlink(struct wpa_driver_nl80211_data
*drv
,
4982 struct nlmsghdr
*h
, int len
)
4984 struct ifinfomsg
*ifi
;
4985 int attrlen
, _nlmsg_len
, rta_len
;
4986 struct rtattr
*attr
;
4988 if (len
< (int) sizeof(*ifi
))
4991 ifi
= NLMSG_DATA(h
);
4993 /* TODO: use ifi->ifi_index to filter out wireless events from other
4996 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
4998 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
5002 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
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
);
5011 attr
= RTA_NEXT(attr
, attrlen
);
5016 static void hostapd_wireless_event_receive(int sock
, void *eloop_ctx
,
5021 struct sockaddr_nl from
;
5024 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
5026 fromlen
= sizeof(from
);
5027 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
5028 (struct sockaddr
*) &from
, &fromlen
);
5030 if (errno
!= EINTR
&& errno
!= EAGAIN
)
5031 perror("recvfrom(netlink)");
5035 h
= (struct nlmsghdr
*) buf
;
5036 while (left
>= (int) sizeof(*h
)) {
5040 plen
= len
- sizeof(*h
);
5041 if (len
> left
|| plen
< 0) {
5042 printf("Malformed netlink message: "
5043 "len=%d left=%d plen=%d\n",
5048 switch (h
->nlmsg_type
) {
5050 hostapd_wireless_event_rtm_newlink(drv
, h
, plen
);
5054 len
= NLMSG_ALIGN(len
);
5056 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
5060 printf("%d extra bytes in the end of netlink message\n", left
);
5065 static int hostap_get_we_version(struct wpa_driver_nl80211_data
*drv
)
5067 struct iw_range
*range
;
5072 drv
->we_version
= 0;
5075 * Use larger buffer than struct iw_range in order to allow the
5076 * structure to grow in the future.
5078 buflen
= sizeof(struct iw_range
) + 500;
5079 range
= os_zalloc(buflen
);
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
;
5088 minlen
= ((char *) &range
->enc_capa
) - (char *) range
+
5089 sizeof(range
->enc_capa
);
5091 if (ioctl(drv
->ioctl_sock
, SIOCGIWRANGE
, &iwr
) < 0) {
5092 perror("ioctl[SIOCGIWRANGE]");
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
,
5102 drv
->we_version
= range
->we_version_compiled
;
5110 static int i802_wireless_event_init(struct wpa_driver_nl80211_data
*drv
)
5113 struct sockaddr_nl local
;
5115 hostap_get_we_version(drv
);
5117 drv
->wext_sock
= -1;
5119 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
5121 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
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)");
5134 eloop_register_read_sock(s
, hostapd_wireless_event_receive
, drv
,
5142 static void i802_wireless_event_deinit(struct wpa_driver_nl80211_data
*drv
)
5144 if (drv
->wext_sock
< 0)
5146 eloop_unregister_read_sock(drv
->wext_sock
);
5147 close(drv
->wext_sock
);
5151 static int i802_sta_deauth(void *priv
, const u8
*addr
, int reason
)
5153 struct wpa_driver_nl80211_data
*drv
= priv
;
5154 struct ieee80211_mgmt mgmt
;
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);
5168 static int i802_sta_disassoc(void *priv
, const u8
*addr
, int reason
)
5170 struct wpa_driver_nl80211_data
*drv
= priv
;
5171 struct ieee80211_mgmt mgmt
;
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);
5185 static const struct hostapd_neighbor_bss
*
5186 i802_get_neighbor_bss(void *priv
, size_t *num
)
5188 struct wpa_driver_nl80211_data
*drv
= priv
;
5189 *num
= drv
->num_neighbors
;
5190 return drv
->neighbors
;
5194 static void *i802_init_bssid(struct hostapd_data
*hapd
, const u8
*bssid
)
5196 struct wpa_driver_nl80211_data
*drv
;
5199 drv
= os_zalloc(sizeof(struct wpa_driver_nl80211_data
));
5201 printf("Could not allocate memory for i802 driver data\n");
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
);
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
));
5217 drv
->ht_40mhz_scan
= hapd
->iconf
->secondary_channel
!= 0;
5219 if (i802_init_sockets(drv
, bssid
))
5222 if (i802_wireless_event_init(drv
))
5233 static void *i802_init(struct hostapd_data
*hapd
)
5235 return i802_init_bssid(hapd
, NULL
);
5239 static void i802_deinit(void *priv
)
5241 struct wpa_driver_nl80211_data
*drv
= priv
;
5242 struct i802_bss
*bss
, *prev
;
5244 i802_wireless_event_deinit(drv
);
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
);
5254 i802_del_beacon(drv
);
5256 /* remove monitor interface */
5257 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
5259 (void) hostapd_set_iface_flags(drv
, drv
->ifname
, 0);
5261 if (drv
->monitor_sock
>= 0) {
5262 eloop_unregister_read_sock(drv
->monitor_sock
);
5263 close(drv
->monitor_sock
);
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
);
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
);
5277 if (drv
->if_indices
!= drv
->default_if_indices
)
5278 free(drv
->if_indices
);
5280 os_free(drv
->neighbors
);
5282 bss
= drv
->bss
.next
;
5292 #endif /* HOSTAPD */
5295 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
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
,
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 */
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 */