2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009, Atheros Communications
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Alternatively, this software may be distributed under the terms of BSD
16 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
21 #include <net/if_arp.h>
23 #include <netlink/genl/genl.h>
24 #include <netlink/genl/family.h>
25 #include <netlink/genl/ctrl.h>
26 #include "nl80211_copy.h"
28 #include "wireless_copy.h"
34 #include "ieee802_11_defs.h"
36 #if defined(CONFIG_AP) || defined(HOSTAPD)
37 #include <netpacket/packet.h>
38 #include <linux/filter.h>
40 #include "radiotap_iter.h"
41 #endif /* CONFIG_AP || HOSTAPD */
45 #include "../hostapd/hostapd_defs.h"
48 #define ETH_P_ALL 0x0003
51 #endif /* CONFIG_AP */
54 #include "../../hostapd/hostapd.h"
55 #include "../../hostapd/sta_flags.h"
56 #include "ieee802_11_common.h"
59 /* libnl 2.0 compatibility code */
60 #define nl_handle_alloc_cb nl_socket_alloc_cb
61 #define nl_handle_destroy nl_socket_free
62 #endif /* CONFIG_LIBNL20 */
68 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
71 #define IFF_DORMANT 0x20000 /* driver signals dormant */
74 #ifndef IF_OPER_DORMANT
75 #define IF_OPER_DORMANT 5
81 enum ieee80211_msg_type
{
82 ieee80211_msg_normal
= 0,
83 ieee80211_msg_tx_callback_ack
= 1,
84 ieee80211_msg_tx_callback_fail
= 2,
88 struct i802_bss
*next
;
90 unsigned int beacon_set
:1;
93 struct wpa_driver_nl80211_data
{
96 int ioctl_sock
; /* socket for ioctl() use */
97 char ifname
[IFNAMSIZ
+ 1];
100 struct wpa_driver_capa capa
;
105 int scan_complete_events
;
107 struct nl_handle
*nl_handle
;
108 struct nl_cache
*nl_cache
;
110 struct genl_family
*nl80211
;
117 int ap_scan_as_station
;
124 unsigned int beacon_set
:1;
125 #endif /* CONFIG_AP */
128 struct hostapd_data
*hapd
;
130 int eapol_sock
; /* socket for EAPOL frames */
132 int default_if_indices
[16];
144 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
,
146 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
);
148 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
);
151 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
153 #endif /* CONFIG_AP */
156 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
157 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
158 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
160 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
162 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
);
163 static int i802_del_beacon(struct wpa_driver_nl80211_data
*drv
);
168 static int ack_handler(struct nl_msg
*msg
, void *arg
)
175 static int finish_handler(struct nl_msg
*msg
, void *arg
)
182 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
191 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
197 static int send_and_recv_msgs(struct wpa_driver_nl80211_data
*drv
,
199 int (*valid_handler
)(struct nl_msg
*, void *),
205 cb
= nl_cb_clone(drv
->nl_cb
);
209 err
= nl_send_auto_complete(drv
->nl_handle
, msg
);
215 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
216 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
217 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
220 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
221 valid_handler
, valid_data
);
224 nl_recvmsgs(drv
->nl_handle
, cb
);
238 static int family_handler(struct nl_msg
*msg
, void *arg
)
240 struct family_data
*res
= arg
;
241 struct nlattr
*tb
[CTRL_ATTR_MAX
+ 1];
242 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
243 struct nlattr
*mcgrp
;
246 nla_parse(tb
, CTRL_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
247 genlmsg_attrlen(gnlh
, 0), NULL
);
248 if (!tb
[CTRL_ATTR_MCAST_GROUPS
])
251 nla_for_each_nested(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
252 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
253 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, nla_data(mcgrp
),
254 nla_len(mcgrp
), NULL
);
255 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] ||
256 !tb2
[CTRL_ATTR_MCAST_GRP_ID
] ||
257 os_strncmp(nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]),
259 nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
])) != 0)
261 res
->id
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
269 static int nl_get_multicast_id(struct wpa_driver_nl80211_data
*drv
,
270 const char *family
, const char *group
)
274 struct family_data res
= { group
, -ENOENT
};
279 genlmsg_put(msg
, 0, 0, genl_ctrl_resolve(drv
->nl_handle
, "nlctrl"),
280 0, 0, CTRL_CMD_GETFAMILY
, 0);
281 NLA_PUT_STRING(msg
, CTRL_ATTR_FAMILY_NAME
, family
);
283 ret
= send_and_recv_msgs(drv
, msg
, family_handler
, &res
);
295 static int get_ifhwaddr(struct wpa_driver_nl80211_data
*drv
,
296 const char *ifname
, u8
*addr
)
300 os_memset(&ifr
, 0, sizeof(ifr
));
301 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
302 if (ioctl(drv
->ioctl_sock
, SIOCGIFHWADDR
, &ifr
)) {
303 wpa_printf(MSG_ERROR
, "%s: ioctl(SIOCGIFHWADDR): %d (%s)",
304 ifname
, errno
, strerror(errno
));
308 if (ifr
.ifr_hwaddr
.sa_family
!= ARPHRD_ETHER
) {
309 wpa_printf(MSG_ERROR
, "%s: Invalid HW-addr family 0x%04x",
310 ifname
, ifr
.ifr_hwaddr
.sa_family
);
313 os_memcpy(addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
319 static int set_ifhwaddr(struct wpa_driver_nl80211_data
*drv
,
320 const char *ifname
, const u8
*addr
)
324 os_memset(&ifr
, 0, sizeof(ifr
));
325 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
326 os_memcpy(ifr
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
327 ifr
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
329 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifr
)) {
330 wpa_printf(MSG_DEBUG
, "%s: ioctl(SIOCSIFHWADDR): %d (%s)",
331 ifname
, errno
, strerror(errno
));
340 static int wpa_driver_nl80211_send_oper_ifla(
341 struct wpa_driver_nl80211_data
*drv
,
342 int linkmode
, int operstate
)
346 struct ifinfomsg ifinfo
;
353 os_memset(&req
, 0, sizeof(req
));
355 req
.hdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
356 req
.hdr
.nlmsg_type
= RTM_SETLINK
;
357 req
.hdr
.nlmsg_flags
= NLM_F_REQUEST
;
358 req
.hdr
.nlmsg_seq
= ++nl_seq
;
359 req
.hdr
.nlmsg_pid
= 0;
361 req
.ifinfo
.ifi_family
= AF_UNSPEC
;
362 req
.ifinfo
.ifi_type
= 0;
363 req
.ifinfo
.ifi_index
= drv
->ifindex
;
364 req
.ifinfo
.ifi_flags
= 0;
365 req
.ifinfo
.ifi_change
= 0;
367 if (linkmode
!= -1) {
368 rta
= (struct rtattr
*)
369 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
370 rta
->rta_type
= IFLA_LINKMODE
;
371 rta
->rta_len
= RTA_LENGTH(sizeof(char));
372 *((char *) RTA_DATA(rta
)) = linkmode
;
373 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
374 RTA_LENGTH(sizeof(char));
376 if (operstate
!= -1) {
377 rta
= (struct rtattr
*)
378 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
379 rta
->rta_type
= IFLA_OPERSTATE
;
380 rta
->rta_len
= RTA_LENGTH(sizeof(char));
381 *((char *) RTA_DATA(rta
)) = operstate
;
382 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
383 RTA_LENGTH(sizeof(char));
386 wpa_printf(MSG_DEBUG
, "nl80211: Operstate: linkmode=%d, operstate=%d",
387 linkmode
, operstate
);
389 ret
= send(drv
->link_event_sock
, &req
, req
.hdr
.nlmsg_len
, 0);
391 wpa_printf(MSG_DEBUG
, "nl80211: Sending operstate IFLA failed:"
392 " %s (assume operstate is not supported)",
396 return ret
< 0 ? -1 : 0;
401 static int wpa_driver_nl80211_set_auth_param(
402 struct wpa_driver_nl80211_data
*drv
, int idx
, u32 value
)
407 os_memset(&iwr
, 0, sizeof(iwr
));
408 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
409 iwr
.u
.param
.flags
= idx
& IW_AUTH_INDEX
;
410 iwr
.u
.param
.value
= value
;
412 if (ioctl(drv
->ioctl_sock
, SIOCSIWAUTH
, &iwr
) < 0) {
413 if (errno
!= EOPNOTSUPP
) {
414 wpa_printf(MSG_DEBUG
, "WEXT: SIOCSIWAUTH(param %d "
415 "value 0x%x) failed: %s)",
416 idx
, value
, strerror(errno
));
418 ret
= errno
== EOPNOTSUPP
? -2 : -1;
426 static int wpa_driver_nl80211_get_bssid(void *priv
, u8
*bssid
)
428 struct wpa_driver_nl80211_data
*drv
= priv
;
429 if (!drv
->associated
)
431 os_memcpy(bssid
, drv
->bssid
, ETH_ALEN
);
436 static int wpa_driver_nl80211_get_ssid(void *priv
, u8
*ssid
)
438 struct wpa_driver_nl80211_data
*drv
= priv
;
439 if (!drv
->associated
)
441 os_memcpy(ssid
, drv
->ssid
, drv
->ssid_len
);
442 return drv
->ssid_len
;
447 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data
*drv
,
448 void *ctx
, char *buf
, size_t len
,
451 union wpa_event_data event
;
453 os_memset(&event
, 0, sizeof(event
));
454 if (len
> sizeof(event
.interface_status
.ifname
))
455 len
= sizeof(event
.interface_status
.ifname
) - 1;
456 os_memcpy(event
.interface_status
.ifname
, buf
, len
);
457 event
.interface_status
.ievent
= del
? EVENT_INTERFACE_REMOVED
:
458 EVENT_INTERFACE_ADDED
;
460 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
462 event
.interface_status
.ifname
,
463 del
? "removed" : "added");
465 if (os_strcmp(drv
->ifname
, event
.interface_status
.ifname
) == 0) {
472 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
476 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data
*drv
,
479 struct ifinfomsg
*ifi
;
480 int attrlen
, _nlmsg_len
, rta_len
;
485 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
487 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
491 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
493 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
494 while (RTA_OK(attr
, attrlen
)) {
495 if (attr
->rta_type
== IFLA_IFNAME
) {
496 if (os_strcmp(((char *) attr
) + rta_len
, drv
->ifname
)
502 attr
= RTA_NEXT(attr
, attrlen
);
509 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data
*drv
,
510 int ifindex
, struct nlmsghdr
*h
)
512 if (drv
->ifindex
== ifindex
)
515 if (drv
->if_removed
&& wpa_driver_nl80211_own_ifname(drv
, h
)) {
516 drv
->ifindex
= if_nametoindex(drv
->ifname
);
517 wpa_printf(MSG_DEBUG
, "nl80211: Update ifindex for a removed "
519 wpa_driver_nl80211_finish_drv_init(drv
);
527 static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data
*drv
,
528 void *ctx
, struct nlmsghdr
*h
,
531 struct ifinfomsg
*ifi
;
532 int attrlen
, _nlmsg_len
, rta_len
;
533 struct rtattr
* attr
;
535 if (len
< sizeof(*ifi
))
540 if (!wpa_driver_nl80211_own_ifindex(drv
, ifi
->ifi_index
, h
)) {
541 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
546 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
548 drv
->operstate
, ifi
->ifi_flags
,
549 (ifi
->ifi_flags
& IFF_UP
) ? "[UP]" : "",
550 (ifi
->ifi_flags
& IFF_RUNNING
) ? "[RUNNING]" : "",
551 (ifi
->ifi_flags
& IFF_LOWER_UP
) ? "[LOWER_UP]" : "",
552 (ifi
->ifi_flags
& IFF_DORMANT
) ? "[DORMANT]" : "");
554 * Some drivers send the association event before the operup event--in
555 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
556 * fails. This will hit us when wpa_supplicant does not need to do
557 * IEEE 802.1X authentication
559 if (drv
->operstate
== 1 &&
560 (ifi
->ifi_flags
& (IFF_LOWER_UP
| IFF_DORMANT
)) == IFF_LOWER_UP
&&
561 !(ifi
->ifi_flags
& IFF_RUNNING
))
562 wpa_driver_nl80211_send_oper_ifla(drv
, -1, IF_OPER_UP
);
564 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
566 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
570 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
572 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
573 while (RTA_OK(attr
, attrlen
)) {
574 if (attr
->rta_type
== IFLA_IFNAME
) {
575 wpa_driver_nl80211_event_link(
577 ((char *) attr
) + rta_len
,
578 attr
->rta_len
- rta_len
, 0);
580 attr
= RTA_NEXT(attr
, attrlen
);
585 static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data
*drv
,
586 void *ctx
, struct nlmsghdr
*h
,
589 struct ifinfomsg
*ifi
;
590 int attrlen
, _nlmsg_len
, rta_len
;
591 struct rtattr
* attr
;
593 if (len
< sizeof(*ifi
))
598 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
600 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
604 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
606 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
607 while (RTA_OK(attr
, attrlen
)) {
608 if (attr
->rta_type
== IFLA_IFNAME
) {
609 wpa_driver_nl80211_event_link(
611 ((char *) attr
) + rta_len
,
612 attr
->rta_len
- rta_len
, 1);
614 attr
= RTA_NEXT(attr
, attrlen
);
619 static void wpa_driver_nl80211_event_receive_link(int sock
, void *eloop_ctx
,
624 struct sockaddr_nl from
;
630 fromlen
= sizeof(from
);
631 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
632 (struct sockaddr
*) &from
, &fromlen
);
634 if (errno
!= EINTR
&& errno
!= EAGAIN
)
635 perror("recvfrom(netlink)");
639 h
= (struct nlmsghdr
*) buf
;
640 while (left
>= (int) sizeof(*h
)) {
644 plen
= len
- sizeof(*h
);
645 if (len
> left
|| plen
< 0) {
646 wpa_printf(MSG_DEBUG
, "Malformed netlink message: "
647 "len=%d left=%d plen=%d",
652 switch (h
->nlmsg_type
) {
654 wpa_driver_nl80211_event_rtm_newlink(eloop_ctx
, sock_ctx
,
658 wpa_driver_nl80211_event_rtm_dellink(eloop_ctx
, sock_ctx
,
663 len
= NLMSG_ALIGN(len
);
665 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
669 wpa_printf(MSG_DEBUG
, "%d extra bytes in the end of netlink "
673 if (--max_events
> 0) {
675 * Try to receive all events in one eloop call in order to
676 * limit race condition on cases where AssocInfo event, Assoc
677 * event, and EAPOL frames are received more or less at the
678 * same time. We want to process the event messages first
679 * before starting EAPOL processing.
686 static void mlme_event_auth(struct wpa_driver_nl80211_data
*drv
,
687 const u8
*frame
, size_t len
)
689 const struct ieee80211_mgmt
*mgmt
;
690 union wpa_event_data event
;
692 mgmt
= (const struct ieee80211_mgmt
*) frame
;
693 if (len
< 24 + sizeof(mgmt
->u
.auth
)) {
694 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
699 os_memset(&event
, 0, sizeof(event
));
700 os_memcpy(event
.auth
.peer
, mgmt
->sa
, ETH_ALEN
);
701 event
.auth
.auth_type
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
702 event
.auth
.status_code
= le_to_host16(mgmt
->u
.auth
.status_code
);
703 if (len
> 24 + sizeof(mgmt
->u
.auth
)) {
704 event
.auth
.ies
= mgmt
->u
.auth
.variable
;
705 event
.auth
.ies_len
= len
- 24 - sizeof(mgmt
->u
.auth
);
708 wpa_supplicant_event(drv
->ctx
, EVENT_AUTH
, &event
);
712 static void mlme_event_assoc(struct wpa_driver_nl80211_data
*drv
,
713 const u8
*frame
, size_t len
)
715 const struct ieee80211_mgmt
*mgmt
;
716 union wpa_event_data event
;
719 mgmt
= (const struct ieee80211_mgmt
*) frame
;
720 if (len
< 24 + sizeof(mgmt
->u
.assoc_resp
)) {
721 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
726 status
= le_to_host16(mgmt
->u
.assoc_resp
.status_code
);
727 if (status
!= WLAN_STATUS_SUCCESS
) {
728 os_memset(&event
, 0, sizeof(event
));
729 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
730 event
.assoc_reject
.resp_ies
=
731 (u8
*) mgmt
->u
.assoc_resp
.variable
;
732 event
.assoc_reject
.resp_ies_len
=
733 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
735 event
.assoc_reject
.status_code
= status
;
737 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
742 os_memcpy(drv
->bssid
, mgmt
->sa
, ETH_ALEN
);
744 os_memset(&event
, 0, sizeof(event
));
745 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
746 event
.assoc_info
.resp_ies
= (u8
*) mgmt
->u
.assoc_resp
.variable
;
747 event
.assoc_info
.resp_ies_len
=
748 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
751 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
755 static void mlme_event(struct wpa_driver_nl80211_data
*drv
,
756 enum nl80211_commands cmd
, struct nlattr
*frame
)
759 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d without frame "
764 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d", cmd
);
765 wpa_hexdump(MSG_MSGDUMP
, "nl80211: MLME event frame",
766 nla_data(frame
), nla_len(frame
));
769 case NL80211_CMD_AUTHENTICATE
:
770 mlme_event_auth(drv
, nla_data(frame
), nla_len(frame
));
772 case NL80211_CMD_ASSOCIATE
:
773 mlme_event_assoc(drv
, nla_data(frame
), nla_len(frame
));
775 case NL80211_CMD_DEAUTHENTICATE
:
777 wpa_supplicant_event(drv
->ctx
, EVENT_DEAUTH
, NULL
);
779 case NL80211_CMD_DISASSOCIATE
:
781 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
791 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data
*drv
,
794 union wpa_event_data data
;
796 wpa_printf(MSG_DEBUG
, "nl80211: MLME event Michael MIC failure");
797 os_memset(&data
, 0, sizeof(data
));
798 if (tb
[NL80211_ATTR_MAC
]) {
799 wpa_hexdump(MSG_DEBUG
, "nl80211: Source MAC address",
800 nla_data(tb
[NL80211_ATTR_MAC
]),
801 nla_len(tb
[NL80211_ATTR_MAC
]));
802 data
.michael_mic_failure
.src
= nla_data(tb
[NL80211_ATTR_MAC
]);
804 if (tb
[NL80211_ATTR_KEY_SEQ
]) {
805 wpa_hexdump(MSG_DEBUG
, "nl80211: TSC",
806 nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
807 nla_len(tb
[NL80211_ATTR_KEY_SEQ
]));
809 if (tb
[NL80211_ATTR_KEY_TYPE
]) {
810 enum nl80211_key_type key_type
=
811 nla_get_u32(tb
[NL80211_ATTR_KEY_TYPE
]);
812 wpa_printf(MSG_DEBUG
, "nl80211: Key Type %d", key_type
);
813 if (key_type
== NL80211_KEYTYPE_PAIRWISE
)
814 data
.michael_mic_failure
.unicast
= 1;
816 data
.michael_mic_failure
.unicast
= 1;
818 if (tb
[NL80211_ATTR_KEY_IDX
]) {
819 u8 key_id
= nla_get_u8(tb
[NL80211_ATTR_KEY_IDX
]);
820 wpa_printf(MSG_DEBUG
, "nl80211: Key Id %d", key_id
);
823 wpa_supplicant_event(drv
->ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
827 static int process_event(struct nl_msg
*msg
, void *arg
)
829 struct wpa_driver_nl80211_data
*drv
= arg
;
830 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
831 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
833 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
834 genlmsg_attrlen(gnlh
, 0), NULL
);
836 if (tb
[NL80211_ATTR_IFINDEX
]) {
837 int ifindex
= nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]);
838 if (ifindex
!= drv
->ifindex
) {
839 wpa_printf(MSG_DEBUG
, "nl80211: Ignored event (cmd=%d)"
840 " for foreign interface (ifindex %d)",
846 if (drv
->ap_scan_as_station
&&
847 (gnlh
->cmd
== NL80211_CMD_NEW_SCAN_RESULTS
||
848 gnlh
->cmd
== NL80211_CMD_SCAN_ABORTED
)) {
849 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
850 drv
->ap_scan_as_station
= 0;
854 case NL80211_CMD_NEW_SCAN_RESULTS
:
855 wpa_printf(MSG_DEBUG
, "nl80211: New scan results available");
856 drv
->scan_complete_events
= 1;
857 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
859 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, NULL
);
861 case NL80211_CMD_SCAN_ABORTED
:
862 wpa_printf(MSG_DEBUG
, "nl80211: Scan aborted");
864 * Need to indicate that scan results are available in order
865 * not to make wpa_supplicant stop its scanning.
867 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
869 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, NULL
);
872 case NL80211_CMD_AUTHENTICATE
:
873 case NL80211_CMD_ASSOCIATE
:
874 case NL80211_CMD_DEAUTHENTICATE
:
875 case NL80211_CMD_DISASSOCIATE
:
876 mlme_event(drv
, gnlh
->cmd
, tb
[NL80211_ATTR_FRAME
]);
879 case NL80211_CMD_MICHAEL_MIC_FAILURE
:
880 mlme_event_michael_mic_failure(drv
, tb
);
883 wpa_printf(MSG_DEBUG
, "nl80211: Ignored unknown event "
884 "(cmd=%d)", gnlh
->cmd
);
892 static void wpa_driver_nl80211_event_receive(int sock
, void *eloop_ctx
,
896 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
898 wpa_printf(MSG_DEBUG
, "nl80211: Event message available");
900 cb
= nl_cb_clone(drv
->nl_cb
);
903 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
904 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, process_event
, drv
);
905 nl_recvmsgs(drv
->nl_handle
, cb
);
910 static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data
*drv
,
911 const char *ifname
, int dev_up
)
915 if (drv
->ioctl_sock
< 0)
918 os_memset(&ifr
, 0, sizeof(ifr
));
919 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
921 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, &ifr
) != 0) {
922 perror("ioctl[SIOCGIFFLAGS]");
923 wpa_printf(MSG_DEBUG
, "Could not read interface flags (%s)",
929 if (ifr
.ifr_flags
& IFF_UP
)
931 ifr
.ifr_flags
|= IFF_UP
;
933 if (!(ifr
.ifr_flags
& IFF_UP
))
935 ifr
.ifr_flags
&= ~IFF_UP
;
938 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, &ifr
) != 0) {
939 perror("ioctl[SIOCSIFFLAGS]");
948 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
949 * @priv: driver_nl80211 private data
950 * @alpha2_arg: country to which to switch to
951 * Returns: 0 on success, -1 on failure
953 * This asks nl80211 to set the regulatory domain for given
954 * country ISO / IEC alpha2.
956 static int wpa_driver_nl80211_set_country(void *priv
, const char *alpha2_arg
)
958 struct wpa_driver_nl80211_data
*drv
= priv
;
966 alpha2
[0] = alpha2_arg
[0];
967 alpha2
[1] = alpha2_arg
[1];
970 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
971 0, NL80211_CMD_REQ_SET_REG
, 0);
973 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
974 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
))
983 struct wiphy_info_data
{
989 static int wiphy_info_handler(struct nl_msg
*msg
, void *arg
)
991 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
992 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
993 struct wiphy_info_data
*info
= arg
;
995 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
996 genlmsg_attrlen(gnlh
, 0), NULL
);
998 if (tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
])
999 info
->max_scan_ssids
=
1000 nla_get_u8(tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
]);
1002 if (tb
[NL80211_ATTR_SUPPORTED_IFTYPES
]) {
1003 struct nlattr
*nl_mode
;
1005 nla_for_each_nested(nl_mode
,
1006 tb
[NL80211_ATTR_SUPPORTED_IFTYPES
], i
) {
1007 if (nl_mode
->nla_type
== NL80211_IFTYPE_AP
) {
1008 info
->ap_supported
= 1;
1018 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data
*drv
,
1019 struct wiphy_info_data
*info
)
1023 os_memset(info
, 0, sizeof(*info
));
1024 msg
= nlmsg_alloc();
1028 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1029 0, NL80211_CMD_GET_WIPHY
, 0);
1031 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1033 if (send_and_recv_msgs(drv
, msg
, wiphy_info_handler
, info
) == 0)
1042 static void wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data
*drv
)
1044 struct wiphy_info_data info
;
1045 if (wpa_driver_nl80211_get_info(drv
, &info
))
1047 drv
->has_capability
= 1;
1048 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
1049 drv
->capa
.key_mgmt
= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
1050 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
1051 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
1052 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
;
1053 drv
->capa
.enc
= WPA_DRIVER_CAPA_ENC_WEP40
|
1054 WPA_DRIVER_CAPA_ENC_WEP104
|
1055 WPA_DRIVER_CAPA_ENC_TKIP
|
1056 WPA_DRIVER_CAPA_ENC_CCMP
;
1058 drv
->capa
.max_scan_ssids
= info
.max_scan_ssids
;
1059 if (info
.ap_supported
)
1060 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_AP
;
1062 #endif /* HOSTAPD */
1065 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data
*drv
,
1070 /* Initialize generic netlink and nl80211 */
1072 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
1073 if (drv
->nl_cb
== NULL
) {
1074 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1079 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
1080 if (drv
->nl_handle
== NULL
) {
1081 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1086 if (genl_connect(drv
->nl_handle
)) {
1087 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
1092 #ifdef CONFIG_LIBNL20
1093 if (genl_ctrl_alloc_cache(drv
->nl_handle
, &drv
->nl_cache
) < 0) {
1094 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1098 #else /* CONFIG_LIBNL20 */
1099 drv
->nl_cache
= genl_ctrl_alloc_cache(drv
->nl_handle
);
1100 if (drv
->nl_cache
== NULL
) {
1101 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1105 #endif /* CONFIG_LIBNL20 */
1108 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
1109 if (drv
->nl80211
== NULL
) {
1110 wpa_printf(MSG_ERROR
, "nl80211: 'nl80211' generic netlink not "
1115 ret
= nl_get_multicast_id(drv
, "nl80211", "scan");
1117 ret
= nl_socket_add_membership(drv
->nl_handle
, ret
);
1119 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1120 "membership for scan events: %d (%s)",
1121 ret
, strerror(-ret
));
1125 ret
= nl_get_multicast_id(drv
, "nl80211", "mlme");
1127 ret
= nl_socket_add_membership(drv
->nl_handle
, ret
);
1129 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1130 "membership for mlme events: %d (%s)",
1131 ret
, strerror(-ret
));
1135 eloop_register_read_sock(nl_socket_get_fd(drv
->nl_handle
),
1136 wpa_driver_nl80211_event_receive
, drv
, ctx
);
1141 nl_cache_free(drv
->nl_cache
);
1143 nl_handle_destroy(drv
->nl_handle
);
1145 nl_cb_put(drv
->nl_cb
);
1151 static int wpa_driver_nl80211_init_link_events(
1152 struct wpa_driver_nl80211_data
*drv
)
1158 struct sockaddr_nl local
;
1160 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1162 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1166 os_memset(&local
, 0, sizeof(local
));
1167 local
.nl_family
= AF_NETLINK
;
1168 local
.nl_groups
= RTMGRP_LINK
;
1169 if (bind(s
, (struct sockaddr
*) &local
, sizeof(local
)) < 0) {
1170 perror("bind(netlink)");
1175 eloop_register_read_sock(s
, wpa_driver_nl80211_event_receive_link
, drv
,
1177 drv
->link_event_sock
= s
;
1180 #endif /* HOSTAPD */
1185 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1186 * @ctx: context to be used when calling wpa_supplicant functions,
1187 * e.g., wpa_supplicant_event()
1188 * @ifname: interface name, e.g., wlan0
1189 * Returns: Pointer to private data, %NULL on failure
1191 static void * wpa_driver_nl80211_init(void *ctx
, const char *ifname
)
1193 struct wpa_driver_nl80211_data
*drv
;
1195 drv
= os_zalloc(sizeof(*drv
));
1199 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1200 drv
->monitor_ifidx
= -1;
1201 drv
->monitor_sock
= -1;
1202 drv
->link_event_sock
= -1;
1203 drv
->ioctl_sock
= -1;
1205 if (wpa_driver_nl80211_init_nl(drv
, ctx
)) {
1210 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SME
;
1212 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1213 if (drv
->ioctl_sock
< 0) {
1214 perror("socket(PF_INET,SOCK_DGRAM)");
1218 if (wpa_driver_nl80211_init_link_events(drv
) ||
1219 wpa_driver_nl80211_finish_drv_init(drv
))
1225 if (drv
->link_event_sock
>= 0) {
1226 eloop_unregister_read_sock(drv
->link_event_sock
);
1227 close(drv
->link_event_sock
);
1229 if (drv
->ioctl_sock
>= 0)
1230 close(drv
->ioctl_sock
);
1232 genl_family_put(drv
->nl80211
);
1233 nl_cache_free(drv
->nl_cache
);
1234 nl_handle_destroy(drv
->nl_handle
);
1235 nl_cb_put(drv
->nl_cb
);
1243 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
)
1245 drv
->ifindex
= if_nametoindex(drv
->ifname
);
1248 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
) < 0) {
1249 wpa_printf(MSG_DEBUG
, "nl80211: Could not configure driver to "
1250 "use managed mode");
1253 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1)) {
1254 wpa_printf(MSG_ERROR
, "Could not set interface '%s' "
1259 wpa_driver_nl80211_capa(drv
);
1261 wpa_driver_nl80211_send_oper_ifla(drv
, 1, IF_OPER_DORMANT
);
1262 #endif /* HOSTAPD */
1269 static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data
*drv
)
1271 struct i802_bss
*bss
, *prev
;
1272 bss
= drv
->bss
.next
;
1279 #endif /* HOSTAPD */
1283 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1284 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1286 * Shut down driver interface and processing of driver events. Free
1287 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1289 static void wpa_driver_nl80211_deinit(void *priv
)
1291 struct wpa_driver_nl80211_data
*drv
= priv
;
1293 #if defined(CONFIG_AP) || defined(HOSTAPD)
1294 if (drv
->monitor_ifidx
>= 0)
1295 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
1296 if (drv
->monitor_sock
>= 0) {
1297 eloop_unregister_read_sock(drv
->monitor_sock
);
1298 close(drv
->monitor_sock
);
1300 #endif /* CONFIG_AP || HOSTAPD */
1303 if (drv
->last_freq_ht
) {
1304 /* Clear HT flags from the driver */
1305 struct hostapd_freq_params freq
;
1306 os_memset(&freq
, 0, sizeof(freq
));
1307 freq
.freq
= drv
->last_freq
;
1308 i802_set_freq(priv
, &freq
);
1311 i802_del_beacon(drv
);
1313 if (drv
->eapol_sock
>= 0) {
1314 eloop_unregister_read_sock(drv
->eapol_sock
);
1315 close(drv
->eapol_sock
);
1318 if (drv
->if_indices
!= drv
->default_if_indices
)
1319 os_free(drv
->if_indices
);
1321 wpa_driver_nl80211_free_bss(drv
);
1324 wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
, 0);
1325 #endif /* NO_WEXT */
1327 wpa_driver_nl80211_send_oper_ifla(priv
, 0, IF_OPER_UP
);
1329 if (drv
->link_event_sock
>= 0) {
1330 eloop_unregister_read_sock(drv
->link_event_sock
);
1331 close(drv
->link_event_sock
);
1333 #endif /* HOSTAPD */
1335 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1337 (void) hostapd_set_iface_flags(drv
, drv
->ifname
, 0);
1338 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
);
1340 if (drv
->ioctl_sock
>= 0)
1341 close(drv
->ioctl_sock
);
1343 eloop_unregister_read_sock(nl_socket_get_fd(drv
->nl_handle
));
1344 genl_family_put(drv
->nl80211
);
1345 nl_cache_free(drv
->nl_cache
);
1346 nl_handle_destroy(drv
->nl_handle
);
1347 nl_cb_put(drv
->nl_cb
);
1354 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1355 * @eloop_ctx: Driver private data
1356 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1358 * This function can be used as registered timeout when starting a scan to
1359 * generate a scan completed event if the driver does not report this.
1361 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1363 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
1364 if (drv
->ap_scan_as_station
) {
1365 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
1366 drv
->ap_scan_as_station
= 0;
1368 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1369 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1374 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1375 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
1376 * @params: Scan parameters
1377 * Returns: 0 on success, -1 on failure
1379 static int wpa_driver_nl80211_scan(void *priv
,
1380 struct wpa_driver_scan_params
*params
)
1382 struct wpa_driver_nl80211_data
*drv
= priv
;
1383 int ret
= 0, timeout
;
1384 struct nl_msg
*msg
, *ssids
, *freqs
;
1387 msg
= nlmsg_alloc();
1388 ssids
= nlmsg_alloc();
1389 freqs
= nlmsg_alloc();
1390 if (!msg
|| !ssids
|| !freqs
) {
1397 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1398 NL80211_CMD_TRIGGER_SCAN
, 0);
1400 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1402 for (i
= 0; i
< params
->num_ssids
; i
++) {
1403 NLA_PUT(ssids
, i
+ 1, params
->ssids
[i
].ssid_len
,
1404 params
->ssids
[i
].ssid
);
1406 if (params
->num_ssids
)
1407 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
1409 if (params
->extra_ies
) {
1410 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->extra_ies_len
,
1414 if (params
->freqs
) {
1415 for (i
= 0; params
->freqs
[i
]; i
++)
1416 NLA_PUT_U32(freqs
, i
+ 1, params
->freqs
[i
]);
1417 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
1420 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1423 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger failed: ret=%d "
1424 "(%s)", ret
, strerror(-ret
));
1426 if (drv
->nlmode
== NL80211_IFTYPE_AP
) {
1428 * mac80211 does not allow scan requests in AP mode, so
1429 * try to do this in station mode.
1431 if (wpa_driver_nl80211_set_mode(drv
,
1432 IEEE80211_MODE_INFRA
))
1433 goto nla_put_failure
;
1435 if (wpa_driver_nl80211_scan(drv
, params
)) {
1436 wpa_driver_nl80211_set_mode(drv
,
1438 goto nla_put_failure
;
1441 /* Restore AP mode when processing scan results */
1442 drv
->ap_scan_as_station
= 1;
1445 goto nla_put_failure
;
1447 goto nla_put_failure
;
1448 #endif /* HOSTAPD */
1451 /* Not all drivers generate "scan completed" wireless event, so try to
1452 * read results after a timeout. */
1454 if (drv
->scan_complete_events
) {
1456 * The driver seems to deliver events to notify when scan is
1457 * complete, so use longer timeout to avoid race conditions
1458 * with scanning and following association request.
1462 wpa_printf(MSG_DEBUG
, "Scan requested (ret=%d) - scan timeout %d "
1463 "seconds", ret
, timeout
);
1464 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1465 eloop_register_timeout(timeout
, 0, wpa_driver_nl80211_scan_timeout
,
1476 static int bss_info_handler(struct nl_msg
*msg
, void *arg
)
1478 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1479 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1480 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1481 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1482 [NL80211_BSS_BSSID
] = { .type
= NLA_UNSPEC
},
1483 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1484 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1485 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1486 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1487 [NL80211_BSS_INFORMATION_ELEMENTS
] = { .type
= NLA_UNSPEC
},
1488 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1489 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1491 struct wpa_scan_results
*res
= arg
;
1492 struct wpa_scan_res
**tmp
;
1493 struct wpa_scan_res
*r
;
1497 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1498 genlmsg_attrlen(gnlh
, 0), NULL
);
1499 if (!tb
[NL80211_ATTR_BSS
])
1501 if (nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1504 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
]) {
1505 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1506 ie_len
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1512 r
= os_zalloc(sizeof(*r
) + ie_len
);
1515 if (bss
[NL80211_BSS_BSSID
])
1516 os_memcpy(r
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]),
1518 if (bss
[NL80211_BSS_FREQUENCY
])
1519 r
->freq
= nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]);
1520 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1521 r
->beacon_int
= nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]);
1522 if (bss
[NL80211_BSS_CAPABILITY
])
1523 r
->caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1524 r
->flags
|= WPA_SCAN_NOISE_INVALID
;
1525 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1526 r
->level
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1527 r
->level
/= 100; /* mBm to dBm */
1528 r
->flags
|= WPA_SCAN_LEVEL_DBM
| WPA_SCAN_QUAL_INVALID
;
1529 } else if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1530 r
->level
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1531 r
->flags
|= WPA_SCAN_LEVEL_INVALID
;
1533 r
->flags
|= WPA_SCAN_LEVEL_INVALID
| WPA_SCAN_QUAL_INVALID
;
1534 if (bss
[NL80211_BSS_TSF
])
1535 r
->tsf
= nla_get_u64(bss
[NL80211_BSS_TSF
]);
1538 os_memcpy(r
+ 1, ie
, ie_len
);
1540 tmp
= os_realloc(res
->res
,
1541 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1546 tmp
[res
->num
++] = r
;
1554 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1555 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1556 * Returns: Scan results on success, -1 on failure
1558 static struct wpa_scan_results
*
1559 wpa_driver_nl80211_get_scan_results(void *priv
)
1561 struct wpa_driver_nl80211_data
*drv
= priv
;
1563 struct wpa_scan_results
*res
;
1566 res
= os_zalloc(sizeof(*res
));
1569 msg
= nlmsg_alloc();
1571 goto nla_put_failure
;
1573 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, NLM_F_DUMP
,
1574 NL80211_CMD_GET_SCAN
, 0);
1575 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1577 ret
= send_and_recv_msgs(drv
, msg
, bss_info_handler
, res
);
1580 wpa_printf(MSG_DEBUG
, "Received scan results (%lu BSSes)",
1581 (unsigned long) res
->num
);
1584 wpa_printf(MSG_DEBUG
, "nl80211: Scan result fetch failed: ret=%d "
1585 "(%s)", ret
, strerror(-ret
));
1588 wpa_scan_results_free(res
);
1593 static int nl_set_encr(int ifindex
, struct wpa_driver_nl80211_data
*drv
,
1594 wpa_alg alg
, const u8
*addr
, int key_idx
, int set_tx
,
1595 const u8
*seq
, size_t seq_len
,
1596 const u8
*key
, size_t key_len
)
1601 wpa_printf(MSG_DEBUG
, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1602 "set_tx=%d seq_len=%lu key_len=%lu",
1603 __func__
, ifindex
, alg
, addr
, key_idx
, set_tx
,
1604 (unsigned long) seq_len
, (unsigned long) key_len
);
1606 msg
= nlmsg_alloc();
1610 if (alg
== WPA_ALG_NONE
) {
1611 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1612 0, NL80211_CMD_DEL_KEY
, 0);
1614 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1615 0, NL80211_CMD_NEW_KEY
, 0);
1616 NLA_PUT(msg
, NL80211_ATTR_KEY_DATA
, key_len
, key
);
1620 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1623 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1627 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC02);
1630 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC04);
1633 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC06);
1636 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1637 "algorithm %d", __func__
, alg
);
1644 NLA_PUT(msg
, NL80211_ATTR_KEY_SEQ
, seq_len
, seq
);
1646 if (addr
&& os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
1648 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1649 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1651 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1652 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1654 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1655 if (ret
== -ENOENT
&& alg
== WPA_ALG_NONE
)
1658 wpa_printf(MSG_DEBUG
, "nl80211: set_key failed; err=%d %s)",
1659 ret
, strerror(-ret
));
1662 * If we failed or don't need to set the default TX key (below),
1665 if (ret
|| !set_tx
|| alg
== WPA_ALG_NONE
)
1667 #ifdef HOSTAPD /* FIX: is this needed? */
1670 #endif /* HOSTAPD */
1672 msg
= nlmsg_alloc();
1676 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1677 0, NL80211_CMD_SET_KEY
, 0);
1678 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1679 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1680 if (alg
== WPA_ALG_IGTK
)
1681 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT_MGMT
);
1683 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
1685 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1689 wpa_printf(MSG_DEBUG
, "nl80211: set_key default failed; "
1690 "err=%d %s)", ret
, strerror(-ret
));
1698 static int wpa_driver_nl80211_set_key(void *priv
, wpa_alg alg
,
1699 const u8
*addr
, int key_idx
,
1700 int set_tx
, const u8
*seq
,
1702 const u8
*key
, size_t key_len
)
1704 struct wpa_driver_nl80211_data
*drv
= priv
;
1705 return nl_set_encr(drv
->ifindex
, drv
, alg
, addr
, key_idx
, set_tx
, seq
,
1706 seq_len
, key
, key_len
);
1710 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
1711 const u8
*addr
, int cmd
, u16 reason_code
)
1716 msg
= nlmsg_alloc();
1720 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0, cmd
, 0);
1722 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1723 NLA_PUT_U16(msg
, NL80211_ATTR_REASON_CODE
, reason_code
);
1724 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1726 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1729 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1730 "(%s)", ret
, strerror(-ret
));
1731 goto nla_put_failure
;
1741 static int wpa_driver_nl80211_deauthenticate(void *priv
, const u8
*addr
,
1744 struct wpa_driver_nl80211_data
*drv
= priv
;
1745 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1746 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DEAUTHENTICATE
,
1751 static int wpa_driver_nl80211_disassociate(void *priv
, const u8
*addr
,
1754 struct wpa_driver_nl80211_data
*drv
= priv
;
1755 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1756 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DISASSOCIATE
,
1761 static int wpa_driver_nl80211_authenticate(
1762 void *priv
, struct wpa_driver_auth_params
*params
)
1764 struct wpa_driver_nl80211_data
*drv
= priv
;
1767 enum nl80211_auth_type type
;
1769 drv
->associated
= 0;
1771 msg
= nlmsg_alloc();
1775 wpa_printf(MSG_DEBUG
, "nl80211: Authenticate (ifindex=%d)",
1778 for (i
= 0; i
< 4; i
++) {
1779 if (!params
->wep_key
[i
])
1781 wpa_driver_nl80211_set_key(drv
, WPA_ALG_WEP
, NULL
, i
,
1782 i
== params
->wep_tx_keyidx
, NULL
, 0,
1784 params
->wep_key_len
[i
]);
1787 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1788 NL80211_CMD_AUTHENTICATE
, 0);
1790 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1791 if (params
->bssid
) {
1792 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
1793 MAC2STR(params
->bssid
));
1794 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
1797 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
1798 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
1801 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
1802 params
->ssid
, params
->ssid_len
);
1803 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
1806 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->ie
, params
->ie_len
);
1808 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->ie_len
, params
->ie
);
1810 * TODO: if multiple auth_alg options enabled, try them one by one if
1811 * the AP rejects authentication due to unknown auth alg
1813 if (params
->auth_alg
& AUTH_ALG_OPEN_SYSTEM
)
1814 type
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
1815 else if (params
->auth_alg
& AUTH_ALG_SHARED_KEY
)
1816 type
= NL80211_AUTHTYPE_SHARED_KEY
;
1817 else if (params
->auth_alg
& AUTH_ALG_LEAP
)
1818 type
= NL80211_AUTHTYPE_NETWORK_EAP
;
1819 else if (params
->auth_alg
& AUTH_ALG_FT
)
1820 type
= NL80211_AUTHTYPE_FT
;
1822 goto nla_put_failure
;
1823 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
1824 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
1826 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1829 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1830 "(%s)", ret
, strerror(-ret
));
1831 goto nla_put_failure
;
1834 wpa_printf(MSG_DEBUG
, "nl80211: Authentication request send "
1843 #if defined(CONFIG_AP) || defined(HOSTAPD)
1845 struct phy_info_arg
{
1847 struct hostapd_hw_modes
*modes
;
1850 static int phy_info_handler(struct nl_msg
*msg
, void *arg
)
1852 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
1853 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1854 struct phy_info_arg
*phy_info
= arg
;
1856 struct nlattr
*tb_band
[NL80211_BAND_ATTR_MAX
+ 1];
1858 struct nlattr
*tb_freq
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1859 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1860 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1861 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1862 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1863 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1864 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1865 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1868 struct nlattr
*tb_rate
[NL80211_BITRATE_ATTR_MAX
+ 1];
1869 static struct nla_policy rate_policy
[NL80211_BITRATE_ATTR_MAX
+ 1] = {
1870 [NL80211_BITRATE_ATTR_RATE
] = { .type
= NLA_U32
},
1871 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
] = { .type
= NLA_FLAG
},
1874 struct nlattr
*nl_band
;
1875 struct nlattr
*nl_freq
;
1876 struct nlattr
*nl_rate
;
1877 int rem_band
, rem_freq
, rem_rate
;
1878 struct hostapd_hw_modes
*mode
;
1879 int idx
, mode_is_set
;
1881 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1882 genlmsg_attrlen(gnlh
, 0), NULL
);
1884 if (!tb_msg
[NL80211_ATTR_WIPHY_BANDS
])
1887 nla_for_each_nested(nl_band
, tb_msg
[NL80211_ATTR_WIPHY_BANDS
], rem_band
) {
1888 mode
= realloc(phy_info
->modes
, (*phy_info
->num_modes
+ 1) * sizeof(*mode
));
1891 phy_info
->modes
= mode
;
1895 mode
= &phy_info
->modes
[*(phy_info
->num_modes
)];
1896 memset(mode
, 0, sizeof(*mode
));
1897 *(phy_info
->num_modes
) += 1;
1899 nla_parse(tb_band
, NL80211_BAND_ATTR_MAX
, nla_data(nl_band
),
1900 nla_len(nl_band
), NULL
);
1902 if (tb_band
[NL80211_BAND_ATTR_HT_CAPA
]) {
1903 mode
->ht_capab
= nla_get_u16(
1904 tb_band
[NL80211_BAND_ATTR_HT_CAPA
]);
1907 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
1908 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
1909 nla_len(nl_freq
), freq_policy
);
1910 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
1912 mode
->num_channels
++;
1915 mode
->channels
= calloc(mode
->num_channels
, sizeof(struct hostapd_channel_data
));
1916 if (!mode
->channels
)
1921 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
1922 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
1923 nla_len(nl_freq
), freq_policy
);
1924 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
1927 mode
->channels
[idx
].freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
1928 mode
->channels
[idx
].flag
= 0;
1931 /* crude heuristic */
1932 if (mode
->channels
[idx
].freq
< 4000)
1933 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
1935 mode
->mode
= HOSTAPD_MODE_IEEE80211A
;
1939 /* crude heuristic */
1940 if (mode
->channels
[idx
].freq
< 4000)
1941 if (mode
->channels
[idx
].freq
== 2484)
1942 mode
->channels
[idx
].chan
= 14;
1944 mode
->channels
[idx
].chan
= (mode
->channels
[idx
].freq
- 2407) / 5;
1946 mode
->channels
[idx
].chan
= mode
->channels
[idx
].freq
/5 - 1000;
1948 if (tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
1949 mode
->channels
[idx
].flag
|=
1950 HOSTAPD_CHAN_DISABLED
;
1951 if (tb_freq
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
])
1952 mode
->channels
[idx
].flag
|=
1953 HOSTAPD_CHAN_PASSIVE_SCAN
;
1954 if (tb_freq
[NL80211_FREQUENCY_ATTR_NO_IBSS
])
1955 mode
->channels
[idx
].flag
|=
1956 HOSTAPD_CHAN_NO_IBSS
;
1957 if (tb_freq
[NL80211_FREQUENCY_ATTR_RADAR
])
1958 mode
->channels
[idx
].flag
|=
1961 if (tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] &&
1962 !tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
1963 mode
->channels
[idx
].max_tx_power
=
1964 nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]) / 100;
1969 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
1970 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
1971 nla_len(nl_rate
), rate_policy
);
1972 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
1977 mode
->rates
= calloc(mode
->num_rates
, sizeof(struct hostapd_rate_data
));
1983 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
1984 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
1985 nla_len(nl_rate
), rate_policy
);
1986 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
1988 mode
->rates
[idx
].rate
= nla_get_u32(tb_rate
[NL80211_BITRATE_ATTR_RATE
]);
1990 /* crude heuristic */
1991 if (mode
->mode
== HOSTAPD_MODE_IEEE80211B
&&
1992 mode
->rates
[idx
].rate
> 200)
1993 mode
->mode
= HOSTAPD_MODE_IEEE80211G
;
1995 if (tb_rate
[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
])
1996 mode
->rates
[idx
].flags
|= HOSTAPD_RATE_PREAMBLE2
;
2005 static struct hostapd_hw_modes
*
2006 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes
*modes
, u16
*num_modes
)
2009 struct hostapd_hw_modes
*mode11g
= NULL
, *nmodes
, *mode
;
2010 int i
, mode11g_idx
= -1;
2012 /* If only 802.11g mode is included, use it to construct matching
2013 * 802.11b mode data. */
2015 for (m
= 0; m
< *num_modes
; m
++) {
2016 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211B
)
2017 return modes
; /* 802.11b already included */
2018 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211G
)
2022 if (mode11g_idx
< 0)
2023 return modes
; /* 2.4 GHz band not supported at all */
2025 nmodes
= os_realloc(modes
, (*num_modes
+ 1) * sizeof(*nmodes
));
2027 return modes
; /* Could not add 802.11b mode */
2029 mode
= &nmodes
[*num_modes
];
2030 os_memset(mode
, 0, sizeof(*mode
));
2034 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
2036 mode11g
= &modes
[mode11g_idx
];
2037 mode
->num_channels
= mode11g
->num_channels
;
2038 mode
->channels
= os_malloc(mode11g
->num_channels
*
2039 sizeof(struct hostapd_channel_data
));
2040 if (mode
->channels
== NULL
) {
2042 return modes
; /* Could not add 802.11b mode */
2044 os_memcpy(mode
->channels
, mode11g
->channels
,
2045 mode11g
->num_channels
* sizeof(struct hostapd_channel_data
));
2047 mode
->num_rates
= 0;
2048 mode
->rates
= os_malloc(4 * sizeof(struct hostapd_rate_data
));
2049 if (mode
->rates
== NULL
) {
2050 os_free(mode
->channels
);
2052 return modes
; /* Could not add 802.11b mode */
2055 for (i
= 0; i
< mode11g
->num_rates
; i
++) {
2056 if (mode11g
->rates
[i
].rate
> 110 ||
2057 mode11g
->rates
[i
].flags
&
2058 (HOSTAPD_RATE_ERP
| HOSTAPD_RATE_OFDM
))
2060 mode
->rates
[mode
->num_rates
] = mode11g
->rates
[i
];
2062 if (mode
->num_rates
== 4)
2066 if (mode
->num_rates
== 0) {
2067 os_free(mode
->channels
);
2068 os_free(mode
->rates
);
2070 return modes
; /* No 802.11b rates */
2073 wpa_printf(MSG_DEBUG
, "nl80211: Added 802.11b mode based on 802.11g "
2080 static struct hostapd_hw_modes
*
2081 wpa_driver_nl80211_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
2083 struct wpa_driver_nl80211_data
*drv
= priv
;
2085 struct phy_info_arg result
= {
2086 .num_modes
= num_modes
,
2093 msg
= nlmsg_alloc();
2097 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2098 0, NL80211_CMD_GET_WIPHY
, 0);
2100 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2102 if (send_and_recv_msgs(drv
, msg
, phy_info_handler
, &result
) == 0)
2103 return wpa_driver_nl80211_add_11b(result
.modes
, num_modes
);
2109 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data
*drv
,
2110 const void *data
, size_t len
,
2114 0x00, 0x00, /* radiotap version */
2115 0x0e, 0x00, /* radiotap length */
2116 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2117 IEEE80211_RADIOTAP_F_FRAG
, /* F_FRAG (fragment if required) */
2119 0x00, 0x00, /* RX and TX flags to indicate that */
2120 0x00, 0x00, /* this is the injected frame directly */
2122 struct iovec iov
[2] = {
2124 .iov_base
= &rtap_hdr
,
2125 .iov_len
= sizeof(rtap_hdr
),
2128 .iov_base
= (void *) data
,
2132 struct msghdr msg
= {
2137 .msg_control
= NULL
,
2138 .msg_controllen
= 0,
2143 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
2145 return sendmsg(drv
->monitor_sock
, &msg
, 0);
2149 static int wpa_driver_nl80211_send_mlme(void *priv
, const u8
*data
,
2152 struct wpa_driver_nl80211_data
*drv
= priv
;
2153 struct ieee80211_mgmt
*mgmt
;
2154 int do_not_encrypt
= 0;
2157 mgmt
= (struct ieee80211_mgmt
*) data
;
2158 fc
= le_to_host16(mgmt
->frame_control
);
2160 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
2161 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
2163 * Only one of the authentication frame types is encrypted.
2164 * In order for static WEP encryption to work properly (i.e.,
2165 * to not encrypt the frame), we need to tell mac80211 about
2166 * the frames that must not be encrypted.
2168 u16 auth_alg
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
2169 u16 auth_trans
= le_to_host16(mgmt
->u
.auth
.auth_transaction
);
2170 if (auth_alg
== WLAN_AUTH_OPEN
||
2171 (auth_alg
== WLAN_AUTH_SHARED_KEY
&& auth_trans
!= 3))
2175 return wpa_driver_nl80211_send_frame(drv
, data
, data_len
,
2180 static int wpa_driver_nl80211_set_beacon_iface(int ifindex
, void *priv
,
2181 const u8
*head
, size_t head_len
,
2182 const u8
*tail
, size_t tail_len
,
2185 struct wpa_driver_nl80211_data
*drv
= priv
;
2187 u8 cmd
= NL80211_CMD_NEW_BEACON
;
2191 struct i802_bss
*bss
;
2193 bss
= get_bss(drv
, ifindex
);
2196 beacon_set
= bss
->beacon_set
;
2198 beacon_set
= drv
->beacon_set
;
2199 #endif /* HOSTAPD */
2201 msg
= nlmsg_alloc();
2205 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon (beacon_set=%d)",
2208 cmd
= NL80211_CMD_SET_BEACON
;
2210 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2212 NLA_PUT(msg
, NL80211_ATTR_BEACON_HEAD
, head_len
, head
);
2213 NLA_PUT(msg
, NL80211_ATTR_BEACON_TAIL
, tail_len
, tail
);
2214 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
2215 if (!drv
->beacon_int
)
2216 drv
->beacon_int
= 100;
2217 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, drv
->beacon_int
);
2218 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, dtim_period
);
2220 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2222 wpa_printf(MSG_DEBUG
, "nl80211: Beacon set failed: %d (%s)",
2223 ret
, strerror(-ret
));
2226 bss
->beacon_set
= 1;
2228 drv
->beacon_set
= 1;
2229 #endif /* HOSTAPD */
2237 static int wpa_driver_nl80211_set_beacon_int(void *priv
, int value
)
2239 struct wpa_driver_nl80211_data
*drv
= priv
;
2242 drv
->beacon_int
= value
;
2245 if (!drv
->bss
.beacon_set
)
2248 if (!drv
->beacon_set
)
2250 #endif /* HOSTAPD */
2252 msg
= nlmsg_alloc();
2256 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon interval %d", value
);
2257 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2258 0, NL80211_CMD_SET_BEACON
, 0);
2259 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2261 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, value
);
2263 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2269 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data
*drv
,
2270 int freq
, int ht_enabled
,
2271 int sec_channel_offset
)
2276 msg
= nlmsg_alloc();
2280 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2281 NL80211_CMD_SET_WIPHY
, 0);
2283 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2284 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
2286 switch (sec_channel_offset
) {
2288 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2289 NL80211_CHAN_HT40MINUS
);
2292 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2293 NL80211_CHAN_HT40PLUS
);
2296 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2302 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2305 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set channel (freq=%d): "
2306 "%d (%s)", freq
, ret
, strerror(-ret
));
2311 #endif /* CONFIG_AP || HOSTAPD */
2316 static int wpa_driver_nl80211_set_beacon(void *priv
,
2317 const u8
*head
, size_t head_len
,
2318 const u8
*tail
, size_t tail_len
,
2321 struct wpa_driver_nl80211_data
*drv
= priv
;
2322 return wpa_driver_nl80211_set_beacon_iface(drv
->ifindex
, priv
,
2328 #endif /* CONFIG_AP */
2330 #if defined(CONFIG_AP) || defined(HOSTAPD)
2332 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
2338 /* stop listening for EAPOL on this interface */
2339 del_ifidx(drv
, ifidx
);
2340 #endif /* HOSTAPD */
2342 msg
= nlmsg_alloc();
2344 goto nla_put_failure
;
2346 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2347 0, NL80211_CMD_DEL_INTERFACE
, 0);
2348 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifidx
);
2350 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
2353 wpa_printf(MSG_ERROR
, "Failed to remove interface (ifidx=%d).\n",
2358 static int nl80211_create_iface(struct wpa_driver_nl80211_data
*drv
,
2359 const char *ifname
, enum nl80211_iftype iftype
,
2362 struct nl_msg
*msg
, *flags
= NULL
;
2366 msg
= nlmsg_alloc();
2370 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2371 0, NL80211_CMD_NEW_INTERFACE
, 0);
2372 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2373 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, ifname
);
2374 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, iftype
);
2376 if (iftype
== NL80211_IFTYPE_MONITOR
) {
2379 flags
= nlmsg_alloc();
2381 goto nla_put_failure
;
2383 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
2385 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
2390 goto nla_put_failure
;
2393 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2396 wpa_printf(MSG_ERROR
, "Failed to create interface %s.",
2401 ifidx
= if_nametoindex(ifname
);
2407 /* start listening for EAPOL on this interface */
2408 add_ifidx(drv
, ifidx
);
2410 if (addr
&& iftype
== NL80211_IFTYPE_AP
&&
2411 set_ifhwaddr(drv
, ifname
, addr
)) {
2412 nl80211_remove_iface(drv
, ifidx
);
2415 #endif /* HOSTAPD */
2420 #endif /* CONFIG_AP || HOSTAPD */
2424 void ap_tx_status(void *ctx
, const u8
*addr
,
2425 const u8
*buf
, size_t len
, int ack
);
2426 void ap_rx_from_unknown_sta(void *ctx
, const u8
*addr
);
2427 void ap_mgmt_rx(void *ctx
, u8
*buf
, size_t len
, u16 stype
,
2428 struct hostapd_frame_info
*fi
);
2429 void ap_mgmt_tx_cb(void *ctx
, u8
*buf
, size_t len
, u16 stype
, int ok
);
2431 #endif /* CONFIG_AP */
2433 #if defined(CONFIG_AP) || defined(HOSTAPD)
2435 static void handle_tx_callback(void *ctx
, u8
*buf
, size_t len
, int ok
)
2437 struct ieee80211_hdr
*hdr
;
2438 u16 fc
, type
, stype
;
2440 hdr
= (struct ieee80211_hdr
*) buf
;
2441 fc
= le_to_host16(hdr
->frame_control
);
2443 type
= WLAN_FC_GET_TYPE(fc
);
2444 stype
= WLAN_FC_GET_STYPE(fc
);
2447 case WLAN_FC_TYPE_MGMT
:
2448 wpa_printf(MSG_DEBUG
, "MGMT (TX callback) %s",
2449 ok
? "ACK" : "fail");
2451 hostapd_mgmt_tx_cb(ctx
, buf
, len
, stype
, ok
);
2453 ap_mgmt_tx_cb(ctx
, buf
, len
, stype
, ok
);
2454 #endif /* HOSTAPD */
2456 case WLAN_FC_TYPE_CTRL
:
2457 wpa_printf(MSG_DEBUG
, "CTRL (TX callback) %s",
2458 ok
? "ACK" : "fail");
2460 case WLAN_FC_TYPE_DATA
:
2462 hostapd_tx_status(ctx
, hdr
->addr1
, buf
, len
, ok
);
2464 ap_tx_status(ctx
, hdr
->addr1
, buf
, len
, ok
);
2465 #endif /* HOSTAPD */
2468 wpa_printf(MSG_DEBUG
, "unknown TX callback frame type %d",
2475 static void handle_frame(struct wpa_driver_nl80211_data
*drv
,
2476 u8
*buf
, size_t len
,
2477 struct hostapd_frame_info
*hfi
,
2478 enum ieee80211_msg_type msg_type
)
2480 struct ieee80211_hdr
*hdr
;
2481 u16 fc
, type
, stype
;
2482 size_t data_len
= len
;
2484 void *ctx
= drv
->ctx
;
2486 struct hostapd_iface
*iface
= drv
->hapd
->iface
;
2487 struct hostapd_data
*hapd
= NULL
;
2488 int broadcast_bssid
= 0;
2490 #endif /* HOSTAPD */
2493 * PS-Poll frames are 16 bytes. All other frames are
2494 * 24 bytes or longer.
2499 hdr
= (struct ieee80211_hdr
*) buf
;
2500 fc
= le_to_host16(hdr
->frame_control
);
2502 type
= WLAN_FC_GET_TYPE(fc
);
2503 stype
= WLAN_FC_GET_STYPE(fc
);
2506 case WLAN_FC_TYPE_DATA
:
2509 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
2513 case WLAN_FC_FROMDS
:
2521 case WLAN_FC_TYPE_CTRL
:
2522 /* discard non-ps-poll frames */
2523 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
2527 case WLAN_FC_TYPE_MGMT
:
2536 /* find interface frame belongs to */
2537 for (i
= 0; i
< iface
->num_bss
; i
++) {
2538 if (memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0) {
2539 hapd
= iface
->bss
[i
];
2545 hapd
= iface
->bss
[0];
2547 if (bssid
[0] != 0xff || bssid
[1] != 0xff ||
2548 bssid
[2] != 0xff || bssid
[3] != 0xff ||
2549 bssid
[4] != 0xff || bssid
[5] != 0xff) {
2551 * Unknown BSSID - drop frame if this is not from
2552 * passive scanning or a beacon (at least ProbeReq
2553 * frames to other APs may be allowed through RX
2554 * filtering in the wlan hw/driver)
2556 if ((type
!= WLAN_FC_TYPE_MGMT
||
2557 stype
!= WLAN_FC_STYPE_BEACON
))
2560 broadcast_bssid
= 1;
2563 #endif /* HOSTAPD */
2566 case ieee80211_msg_normal
:
2567 /* continue processing */
2569 case ieee80211_msg_tx_callback_ack
:
2570 handle_tx_callback(ctx
, buf
, data_len
, 1);
2572 case ieee80211_msg_tx_callback_fail
:
2573 handle_tx_callback(ctx
, buf
, data_len
, 0);
2578 case WLAN_FC_TYPE_MGMT
:
2579 if (stype
!= WLAN_FC_STYPE_BEACON
&&
2580 stype
!= WLAN_FC_STYPE_PROBE_REQ
)
2581 wpa_printf(MSG_MSGDUMP
, "MGMT");
2583 if (broadcast_bssid
) {
2584 for (i
= 0; i
< iface
->num_bss
; i
++)
2585 hostapd_mgmt_rx(iface
->bss
[i
], buf
, data_len
,
2588 hostapd_mgmt_rx(hapd
, buf
, data_len
, stype
, hfi
);
2590 ap_mgmt_rx(drv
->ctx
, buf
, data_len
, stype
, hfi
);
2591 #endif /* HOSTAPD */
2593 case WLAN_FC_TYPE_CTRL
:
2594 /* can only get here with PS-Poll frames */
2595 wpa_printf(MSG_DEBUG
, "CTRL");
2597 hostapd_rx_from_unknown_sta(drv
->hapd
, hdr
->addr2
);
2599 ap_rx_from_unknown_sta(drv
->ctx
, hdr
->addr2
);
2600 #endif /* HOSTAPD */
2602 case WLAN_FC_TYPE_DATA
:
2604 hostapd_rx_from_unknown_sta(drv
->hapd
, hdr
->addr2
);
2606 ap_rx_from_unknown_sta(drv
->ctx
, hdr
->addr2
);
2607 #endif /* HOSTAPD */
2613 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
2615 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
2617 unsigned char buf
[3000];
2618 struct ieee80211_radiotap_iterator iter
;
2620 struct hostapd_frame_info hfi
;
2621 int injected
= 0, failed
= 0, msg_type
, rxflags
= 0;
2623 len
= recv(sock
, buf
, sizeof(buf
), 0);
2629 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
2630 printf("received invalid radiotap frame\n");
2634 memset(&hfi
, 0, sizeof(hfi
));
2637 ret
= ieee80211_radiotap_iterator_next(&iter
);
2641 printf("received invalid radiotap frame (%d)\n", ret
);
2644 switch (iter
.this_arg_index
) {
2645 case IEEE80211_RADIOTAP_FLAGS
:
2646 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
2649 case IEEE80211_RADIOTAP_RX_FLAGS
:
2652 case IEEE80211_RADIOTAP_TX_FLAGS
:
2654 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
2655 IEEE80211_RADIOTAP_F_TX_FAIL
;
2657 case IEEE80211_RADIOTAP_DATA_RETRIES
:
2659 case IEEE80211_RADIOTAP_CHANNEL
:
2660 /* TODO convert from freq/flags to channel number
2665 case IEEE80211_RADIOTAP_RATE
:
2666 hfi
.datarate
= *iter
.this_arg
* 5;
2668 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
2669 hfi
.ssi_signal
= *iter
.this_arg
;
2674 if (rxflags
&& injected
)
2678 msg_type
= ieee80211_msg_normal
;
2680 msg_type
= ieee80211_msg_tx_callback_fail
;
2682 msg_type
= ieee80211_msg_tx_callback_ack
;
2684 handle_frame(drv
, buf
+ iter
.max_length
,
2685 len
- iter
.max_length
, &hfi
, msg_type
);
2690 * we post-process the filter code later and rewrite
2691 * this to the offset to the last instruction
2696 static struct sock_filter msock_filter_insns
[] = {
2698 * do a little-endian load of the radiotap length field
2700 /* load lower byte into A */
2701 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 2),
2702 /* put it into X (== index register) */
2703 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2704 /* load upper byte into A */
2705 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 3),
2706 /* left-shift it by 8 */
2707 BPF_STMT(BPF_ALU
| BPF_LSH
| BPF_K
, 8),
2709 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
2710 /* put result into X */
2711 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2714 * Allow management frames through, this also gives us those
2715 * management frames that we sent ourselves with status
2717 /* load the lower byte of the IEEE 802.11 frame control field */
2718 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2719 /* mask off frame type and version */
2720 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0xF),
2721 /* accept frame if it's both 0, fall through otherwise */
2722 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0, PASS
, 0),
2725 * TODO: add a bit to radiotap RX flags that indicates
2726 * that the sending station is not associated, then
2727 * add a filter here that filters on our DA and that flag
2728 * to allow us to deauth frames to that bad station.
2730 * Not a regression -- we didn't do it before either.
2735 * drop non-data frames, WDS frames
2737 /* load the lower byte of the frame control field */
2738 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2739 /* mask off QoS bit */
2740 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x0c),
2741 /* drop non-data frames */
2742 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 8, 0, FAIL
),
2743 /* load the upper byte of the frame control field */
2744 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2745 /* mask off toDS/fromDS */
2746 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x03),
2747 /* drop WDS frames */
2748 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 3, FAIL
, 0),
2752 * add header length to index
2754 /* load the lower byte of the frame control field */
2755 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2756 /* mask off QoS bit */
2757 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x80),
2758 /* right shift it by 6 to give 0 or 2 */
2759 BPF_STMT(BPF_ALU
| BPF_RSH
| BPF_K
, 6),
2760 /* add data frame header length */
2761 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_K
, 24),
2762 /* add index, was start of 802.11 header */
2763 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_X
, 0),
2764 /* move to index, now start of LL header */
2765 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2768 * Accept empty data frames, we use those for
2771 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
2772 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
2775 * Accept EAPOL frames
2777 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 0),
2778 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0xAAAA0300, 0, FAIL
),
2779 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 4),
2780 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0x0000888E, PASS
, FAIL
),
2782 /* keep these last two statements or change the code below */
2783 /* return 0 == "DROP" */
2784 BPF_STMT(BPF_RET
| BPF_K
, 0),
2785 /* return ~0 == "keep all" */
2786 BPF_STMT(BPF_RET
| BPF_K
, ~0),
2789 static struct sock_fprog msock_filter
= {
2790 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
2791 .filter
= msock_filter_insns
,
2795 static int add_monitor_filter(int s
)
2799 /* rewrite all PASS/FAIL jump offsets */
2800 for (idx
= 0; idx
< msock_filter
.len
; idx
++) {
2801 struct sock_filter
*insn
= &msock_filter_insns
[idx
];
2803 if (BPF_CLASS(insn
->code
) == BPF_JMP
) {
2804 if (insn
->code
== (BPF_JMP
|BPF_JA
)) {
2805 if (insn
->k
== PASS
)
2806 insn
->k
= msock_filter
.len
- idx
- 2;
2807 else if (insn
->k
== FAIL
)
2808 insn
->k
= msock_filter
.len
- idx
- 3;
2811 if (insn
->jt
== PASS
)
2812 insn
->jt
= msock_filter
.len
- idx
- 2;
2813 else if (insn
->jt
== FAIL
)
2814 insn
->jt
= msock_filter
.len
- idx
- 3;
2816 if (insn
->jf
== PASS
)
2817 insn
->jf
= msock_filter
.len
- idx
- 2;
2818 else if (insn
->jf
== FAIL
)
2819 insn
->jf
= msock_filter
.len
- idx
- 3;
2823 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
2824 &msock_filter
, sizeof(msock_filter
))) {
2825 perror("SO_ATTACH_FILTER");
2834 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data
*drv
)
2837 struct sockaddr_ll ll
;
2841 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->ifname
);
2842 buf
[IFNAMSIZ
- 1] = '\0';
2844 drv
->monitor_ifidx
=
2845 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
, NULL
);
2847 if (drv
->monitor_ifidx
< 0)
2850 if (hostapd_set_iface_flags(drv
, buf
, 1))
2853 memset(&ll
, 0, sizeof(ll
));
2854 ll
.sll_family
= AF_PACKET
;
2855 ll
.sll_ifindex
= drv
->monitor_ifidx
;
2856 drv
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
2857 if (drv
->monitor_sock
< 0) {
2858 perror("socket[PF_PACKET,SOCK_RAW]");
2862 if (add_monitor_filter(drv
->monitor_sock
)) {
2863 wpa_printf(MSG_INFO
, "Failed to set socket filter for monitor "
2864 "interface; do filtering in user space");
2865 /* This works, but will cost in performance. */
2868 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
2869 perror("monitor socket bind");
2873 optlen
= sizeof(optval
);
2876 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
2877 perror("Failed to set socket priority");
2881 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
2883 printf("Could not register monitor read socket\n");
2889 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2893 #endif /* CONFIG_AP || HOSTAPD */
2897 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data
*drv
,
2898 struct wpa_driver_associate_params
*params
)
2900 if (drv
->monitor_ifidx
< 0 &&
2901 nl80211_create_monitor_interface(drv
))
2904 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) ||
2905 wpa_driver_nl80211_set_freq(drv
, params
->freq
, 0, 0)) {
2906 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2907 drv
->monitor_ifidx
= -1;
2911 /* TODO: setup monitor interface (and add code somewhere to remove this
2912 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
2916 #endif /* CONFIG_AP */
2919 static int wpa_driver_nl80211_associate(
2920 void *priv
, struct wpa_driver_associate_params
*params
)
2922 struct wpa_driver_nl80211_data
*drv
= priv
;
2927 if (params
->mode
== 2)
2928 return wpa_driver_nl80211_ap(drv
, params
);
2929 #endif /* CONFIG_AP */
2932 wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
,
2933 params
->drop_unencrypted
);
2934 #endif /* NO_WEXT */
2936 drv
->associated
= 0;
2938 msg
= nlmsg_alloc();
2942 wpa_printf(MSG_DEBUG
, "nl80211: Associate (ifindex=%d)",
2944 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2945 NL80211_CMD_ASSOCIATE
, 0);
2947 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2948 if (params
->bssid
) {
2949 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
2950 MAC2STR(params
->bssid
));
2951 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
2954 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
2955 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
2958 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
2959 params
->ssid
, params
->ssid_len
);
2960 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
2962 if (params
->ssid_len
> sizeof(drv
->ssid
))
2963 goto nla_put_failure
;
2964 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
2965 drv
->ssid_len
= params
->ssid_len
;
2967 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
2969 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
2972 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2975 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
2976 "(%s)", ret
, strerror(-ret
));
2977 goto nla_put_failure
;
2980 wpa_printf(MSG_DEBUG
, "nl80211: Association request send "
2989 static int nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
,
2990 int ifindex
, int mode
)
2995 msg
= nlmsg_alloc();
2999 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3000 0, NL80211_CMD_SET_INTERFACE
, 0);
3001 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
3002 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, mode
);
3004 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3008 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set interface %d to mode %d:"
3009 " %d (%s)", ifindex
, mode
, ret
, strerror(-ret
));
3014 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
)
3016 struct wpa_driver_nl80211_data
*drv
= priv
;
3022 nlmode
= NL80211_IFTYPE_STATION
;
3025 nlmode
= NL80211_IFTYPE_ADHOC
;
3028 nlmode
= NL80211_IFTYPE_AP
;
3034 if (nl80211_set_mode(drv
, drv
->ifindex
, nlmode
) == 0) {
3035 drv
->nlmode
= nlmode
;
3039 if (nlmode
== drv
->nlmode
)
3040 return 0; /* Already in the requested mode */
3042 /* mac80211 doesn't allow mode changes while the device is up, so
3043 * take the device down, try to set the mode again, and bring the
3046 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 0) == 0) {
3047 /* Try to set the mode again while the interface is down */
3048 ret
= nl80211_set_mode(drv
, drv
->ifindex
, nlmode
);
3049 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1))
3054 drv
->nlmode
= nlmode
;
3060 static int wpa_driver_nl80211_get_capa(void *priv
,
3061 struct wpa_driver_capa
*capa
)
3063 struct wpa_driver_nl80211_data
*drv
= priv
;
3064 if (!drv
->has_capability
)
3066 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
3071 static int wpa_driver_nl80211_set_operstate(void *priv
, int state
)
3073 struct wpa_driver_nl80211_data
*drv
= priv
;
3075 wpa_printf(MSG_DEBUG
, "%s: operstate %d->%d (%s)",
3076 __func__
, drv
->operstate
, state
, state
? "UP" : "DORMANT");
3077 drv
->operstate
= state
;
3078 return wpa_driver_nl80211_send_oper_ifla(
3079 drv
, -1, state
? IF_OPER_UP
: IF_OPER_DORMANT
);
3085 static const u8 rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3087 static int i802_sta_deauth(void *priv
, const u8
*addr
, int reason
);
3088 static int i802_sta_disassoc(void *priv
, const u8
*addr
, int reason
);
3091 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
3094 struct i802_bss
*bss
= &drv
->bss
;
3096 if (ifindex
== bss
->ifindex
)
3100 wpa_printf(MSG_DEBUG
, "nl80211: get_bss(%d) failed", ifindex
);
3105 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3110 wpa_printf(MSG_DEBUG
, "nl80211: Add own interface ifindex %d",
3112 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3113 if (drv
->if_indices
[i
] == 0) {
3114 drv
->if_indices
[i
] = ifidx
;
3119 if (drv
->if_indices
!= drv
->default_if_indices
)
3120 old
= drv
->if_indices
;
3124 drv
->if_indices
= realloc(old
,
3125 sizeof(int) * (drv
->num_if_indices
+ 1));
3126 if (!drv
->if_indices
) {
3128 drv
->if_indices
= drv
->default_if_indices
;
3130 drv
->if_indices
= old
;
3131 wpa_printf(MSG_ERROR
, "Failed to reallocate memory for "
3133 wpa_printf(MSG_ERROR
, "Ignoring EAPOL on interface %d", ifidx
);
3136 drv
->if_indices
[drv
->num_if_indices
] = ifidx
;
3137 drv
->num_if_indices
++;
3141 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3145 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3146 if (drv
->if_indices
[i
] == ifidx
) {
3147 drv
->if_indices
[i
] = 0;
3154 static int have_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3158 for (i
= 0; i
< drv
->num_if_indices
; i
++)
3159 if (drv
->if_indices
[i
] == ifidx
)
3166 static int i802_set_key(const char *iface
, void *priv
, wpa_alg alg
,
3167 const u8
*addr
, int key_idx
, int set_tx
, const u8
*seq
,
3168 size_t seq_len
, const u8
*key
, size_t key_len
)
3170 struct wpa_driver_nl80211_data
*drv
= priv
;
3171 return nl_set_encr(if_nametoindex(iface
), drv
, alg
, addr
, key_idx
,
3172 set_tx
, seq
, seq_len
, key
, key_len
);
3176 static inline int min_int(int a
, int b
)
3184 static int get_key_handler(struct nl_msg
*msg
, void *arg
)
3186 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3187 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3189 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3190 genlmsg_attrlen(gnlh
, 0), NULL
);
3193 * TODO: validate the key index and mac address!
3194 * Otherwise, there's a race condition as soon as
3195 * the kernel starts sending key notifications.
3198 if (tb
[NL80211_ATTR_KEY_SEQ
])
3199 memcpy(arg
, nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
3200 min_int(nla_len(tb
[NL80211_ATTR_KEY_SEQ
]), 6));
3205 static int i802_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
3208 struct wpa_driver_nl80211_data
*drv
= priv
;
3211 msg
= nlmsg_alloc();
3215 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3216 0, NL80211_CMD_GET_KEY
, 0);
3219 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3220 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
3221 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
3225 return send_and_recv_msgs(drv
, msg
, get_key_handler
, seq
);
3231 static int i802_set_rate_sets(void *priv
, int *supp_rates
, int *basic_rates
,
3234 struct wpa_driver_nl80211_data
*drv
= priv
;
3236 u8 rates
[NL80211_MAX_SUPP_RATES
];
3240 msg
= nlmsg_alloc();
3244 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3245 NL80211_CMD_SET_BSS
, 0);
3247 for (i
= 0; i
< NL80211_MAX_SUPP_RATES
&& basic_rates
[i
] >= 0; i
++)
3248 rates
[rates_len
++] = basic_rates
[i
] / 5;
3250 NLA_PUT(msg
, NL80211_ATTR_BSS_BASIC_RATES
, rates_len
, rates
);
3252 /* TODO: multi-BSS support */
3253 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3255 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3261 /* Set kernel driver on given frequency (MHz) */
3262 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
)
3264 struct wpa_driver_nl80211_data
*drv
= priv
;
3265 return wpa_driver_nl80211_set_freq(drv
, freq
->freq
, freq
->ht_enabled
,
3266 freq
->sec_channel_offset
);
3270 static int i802_set_rts(void *priv
, int rts
)
3275 struct wpa_driver_nl80211_data
*drv
= priv
;
3278 memset(&iwr
, 0, sizeof(iwr
));
3279 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3280 iwr
.u
.rts
.value
= rts
;
3281 iwr
.u
.rts
.fixed
= 1;
3283 if (ioctl(drv
->ioctl_sock
, SIOCSIWRTS
, &iwr
) < 0) {
3284 perror("ioctl[SIOCSIWRTS]");
3289 #endif /* NO_WEXT */
3293 static int i802_set_frag(void *priv
, int frag
)
3298 struct wpa_driver_nl80211_data
*drv
= priv
;
3301 memset(&iwr
, 0, sizeof(iwr
));
3302 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3303 iwr
.u
.frag
.value
= frag
;
3304 iwr
.u
.frag
.fixed
= 1;
3306 if (ioctl(drv
->ioctl_sock
, SIOCSIWFRAG
, &iwr
) < 0) {
3307 perror("ioctl[SIOCSIWFRAG]");
3312 #endif /* NO_WEXT */
3316 static int i802_set_retry(void *priv
, int short_retry
, int long_retry
)
3321 struct wpa_driver_nl80211_data
*drv
= priv
;
3324 memset(&iwr
, 0, sizeof(iwr
));
3325 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3327 iwr
.u
.retry
.value
= short_retry
;
3328 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MIN
;
3329 if (ioctl(drv
->ioctl_sock
, SIOCSIWRETRY
, &iwr
) < 0) {
3330 perror("ioctl[SIOCSIWRETRY(short)]");
3334 iwr
.u
.retry
.value
= long_retry
;
3335 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MAX
;
3336 if (ioctl(drv
->ioctl_sock
, SIOCSIWRETRY
, &iwr
) < 0) {
3337 perror("ioctl[SIOCSIWRETRY(long)]");
3342 #endif /* NO_WEXT */
3346 static int i802_flush(void *priv
)
3348 struct wpa_driver_nl80211_data
*drv
= priv
;
3351 msg
= nlmsg_alloc();
3355 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3356 0, NL80211_CMD_DEL_STATION
, 0);
3359 * XXX: FIX! this needs to flush all VLANs too
3361 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3362 if_nametoindex(drv
->ifname
));
3364 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3370 static int get_sta_handler(struct nl_msg
*msg
, void *arg
)
3372 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3373 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3374 struct hostap_sta_driver_data
*data
= arg
;
3375 struct nlattr
*stats
[NL80211_STA_INFO_MAX
+ 1];
3376 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
3377 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
3378 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
3379 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
3380 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
3381 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
3384 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3385 genlmsg_attrlen(gnlh
, 0), NULL
);
3388 * TODO: validate the interface and mac address!
3389 * Otherwise, there's a race condition as soon as
3390 * the kernel starts sending station notifications.
3393 if (!tb
[NL80211_ATTR_STA_INFO
]) {
3394 wpa_printf(MSG_DEBUG
, "sta stats missing!");
3397 if (nla_parse_nested(stats
, NL80211_STA_INFO_MAX
,
3398 tb
[NL80211_ATTR_STA_INFO
],
3400 wpa_printf(MSG_DEBUG
, "failed to parse nested attributes!");
3404 if (stats
[NL80211_STA_INFO_INACTIVE_TIME
])
3405 data
->inactive_msec
=
3406 nla_get_u32(stats
[NL80211_STA_INFO_INACTIVE_TIME
]);
3407 if (stats
[NL80211_STA_INFO_RX_BYTES
])
3408 data
->rx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_RX_BYTES
]);
3409 if (stats
[NL80211_STA_INFO_TX_BYTES
])
3410 data
->tx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_TX_BYTES
]);
3411 if (stats
[NL80211_STA_INFO_RX_PACKETS
])
3413 nla_get_u32(stats
[NL80211_STA_INFO_RX_PACKETS
]);
3414 if (stats
[NL80211_STA_INFO_TX_PACKETS
])
3416 nla_get_u32(stats
[NL80211_STA_INFO_TX_PACKETS
]);
3421 static int i802_read_sta_data(void *priv
, struct hostap_sta_driver_data
*data
,
3424 struct wpa_driver_nl80211_data
*drv
= priv
;
3427 os_memset(data
, 0, sizeof(*data
));
3428 msg
= nlmsg_alloc();
3432 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3433 0, NL80211_CMD_GET_STATION
, 0);
3435 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3436 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3438 return send_and_recv_msgs(drv
, msg
, get_sta_handler
, data
);
3444 static int i802_send_eapol(void *priv
, const u8
*addr
, const u8
*data
,
3445 size_t data_len
, int encrypt
, const u8
*own_addr
)
3447 struct wpa_driver_nl80211_data
*drv
= priv
;
3448 struct ieee80211_hdr
*hdr
;
3453 int qos
= sta
->flags
& WLAN_STA_WME
;
3458 len
= sizeof(*hdr
) + (qos
? 2 : 0) + sizeof(rfc1042_header
) + 2 +
3460 hdr
= os_zalloc(len
);
3462 printf("malloc() failed for i802_send_data(len=%lu)\n",
3463 (unsigned long) len
);
3467 hdr
->frame_control
=
3468 IEEE80211_FC(WLAN_FC_TYPE_DATA
, WLAN_FC_STYPE_DATA
);
3469 hdr
->frame_control
|= host_to_le16(WLAN_FC_FROMDS
);
3471 hdr
->frame_control
|= host_to_le16(WLAN_FC_ISWEP
);
3472 #if 0 /* To be enabled if qos determination is added above */
3474 hdr
->frame_control
|=
3475 host_to_le16(WLAN_FC_STYPE_QOS_DATA
<< 4);
3479 memcpy(hdr
->IEEE80211_DA_FROMDS
, addr
, ETH_ALEN
);
3480 memcpy(hdr
->IEEE80211_BSSID_FROMDS
, own_addr
, ETH_ALEN
);
3481 memcpy(hdr
->IEEE80211_SA_FROMDS
, own_addr
, ETH_ALEN
);
3482 pos
= (u8
*) (hdr
+ 1);
3484 #if 0 /* To be enabled if qos determination is added above */
3486 /* add an empty QoS header if needed */
3493 memcpy(pos
, rfc1042_header
, sizeof(rfc1042_header
));
3494 pos
+= sizeof(rfc1042_header
);
3495 WPA_PUT_BE16(pos
, ETH_P_PAE
);
3497 memcpy(pos
, data
, data_len
);
3499 res
= wpa_driver_nl80211_send_frame(drv
, (u8
*) hdr
, len
, encrypt
);
3501 wpa_printf(MSG_ERROR
, "i802_send_eapol - packet len: %lu - "
3503 (unsigned long) len
, errno
, strerror(errno
));
3511 static int i802_sta_add(const char *ifname
, void *priv
,
3512 struct hostapd_sta_add_params
*params
)
3514 struct wpa_driver_nl80211_data
*drv
= priv
;
3518 msg
= nlmsg_alloc();
3522 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3523 0, NL80211_CMD_NEW_STATION
, 0);
3525 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3526 if_nametoindex(drv
->ifname
));
3527 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->addr
);
3528 NLA_PUT_U16(msg
, NL80211_ATTR_STA_AID
, params
->aid
);
3529 NLA_PUT(msg
, NL80211_ATTR_STA_SUPPORTED_RATES
, params
->supp_rates_len
,
3530 params
->supp_rates
);
3531 NLA_PUT_U16(msg
, NL80211_ATTR_STA_LISTEN_INTERVAL
,
3532 params
->listen_interval
);
3534 #ifdef CONFIG_IEEE80211N
3535 if (params
->ht_capabilities
) {
3536 NLA_PUT(msg
, NL80211_ATTR_HT_CAPABILITY
,
3537 params
->ht_capabilities
->length
,
3538 ¶ms
->ht_capabilities
->data
);
3540 #endif /* CONFIG_IEEE80211N */
3542 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3544 wpa_printf(MSG_DEBUG
, "nl80211: NL80211_CMD_NEW_STATION "
3545 "result: %d (%s)", ret
, strerror(-ret
));
3553 static int i802_sta_remove(void *priv
, const u8
*addr
)
3555 struct wpa_driver_nl80211_data
*drv
= priv
;
3559 msg
= nlmsg_alloc();
3563 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3564 0, NL80211_CMD_DEL_STATION
, 0);
3566 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3567 if_nametoindex(drv
->ifname
));
3568 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3570 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3579 static int i802_sta_set_flags(void *priv
, const u8
*addr
,
3580 int total_flags
, int flags_or
, int flags_and
)
3582 struct wpa_driver_nl80211_data
*drv
= priv
;
3583 struct nl_msg
*msg
, *flags
= NULL
;
3585 msg
= nlmsg_alloc();
3589 flags
= nlmsg_alloc();
3595 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3596 0, NL80211_CMD_SET_STATION
, 0);
3598 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3599 if_nametoindex(drv
->ifname
));
3600 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3602 if (total_flags
& WLAN_STA_AUTHORIZED
)
3603 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_AUTHORIZED
);
3605 if (total_flags
& WLAN_STA_WMM
)
3606 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_WME
);
3608 if (total_flags
& WLAN_STA_SHORT_PREAMBLE
)
3609 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_SHORT_PREAMBLE
);
3611 if (total_flags
& WLAN_STA_MFP
)
3612 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_MFP
);
3614 if (nla_put_nested(msg
, NL80211_ATTR_STA_FLAGS
, flags
))
3615 goto nla_put_failure
;
3619 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3626 static int i802_set_tx_queue_params(void *priv
, int queue
, int aifs
,
3627 int cw_min
, int cw_max
, int burst_time
)
3629 struct wpa_driver_nl80211_data
*drv
= priv
;
3631 struct nlattr
*txq
, *params
;
3633 msg
= nlmsg_alloc();
3637 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3638 0, NL80211_CMD_SET_WIPHY
, 0);
3640 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3642 txq
= nla_nest_start(msg
, NL80211_ATTR_WIPHY_TXQ_PARAMS
);
3644 goto nla_put_failure
;
3646 /* We are only sending parameters for a single TXQ at a time */
3647 params
= nla_nest_start(msg
, 1);
3649 goto nla_put_failure
;
3651 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_QUEUE
, queue
);
3652 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
3653 * 32 usec, so need to convert the value here. */
3654 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_TXOP
, (burst_time
* 100 + 16) / 32);
3655 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMIN
, cw_min
);
3656 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMAX
, cw_max
);
3657 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_AIFS
, aifs
);
3659 nla_nest_end(msg
, params
);
3661 nla_nest_end(msg
, txq
);
3663 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
3670 static int i802_bss_add(void *priv
, const char *ifname
, const u8
*bssid
)
3672 struct wpa_driver_nl80211_data
*drv
= priv
;
3674 struct i802_bss
*bss
;
3676 bss
= os_zalloc(sizeof(*bss
));
3679 bss
->ifindex
= if_nametoindex(ifname
);
3681 ifidx
= nl80211_create_iface(priv
, ifname
, NL80211_IFTYPE_AP
, bssid
);
3686 if (hostapd_set_iface_flags(priv
, ifname
, 1)) {
3687 nl80211_remove_iface(priv
, ifidx
);
3691 bss
->next
= drv
->bss
.next
;
3692 drv
->bss
.next
= bss
;
3697 static int i802_bss_remove(void *priv
, const char *ifname
)
3699 struct wpa_driver_nl80211_data
*drv
= priv
;
3700 struct i802_bss
*bss
, *prev
;
3701 int ifindex
= if_nametoindex(ifname
);
3702 nl80211_remove_iface(priv
, ifindex
);
3704 bss
= drv
->bss
.next
;
3706 if (ifindex
== bss
->ifindex
) {
3707 prev
->next
= bss
->next
;
3718 static int i802_set_beacon(const char *iface
, void *priv
,
3719 const u8
*head
, size_t head_len
,
3720 const u8
*tail
, size_t tail_len
, int dtim_period
)
3722 return wpa_driver_nl80211_set_beacon_iface(if_nametoindex(iface
), priv
,
3729 static int i802_del_beacon(struct wpa_driver_nl80211_data
*drv
)
3733 msg
= nlmsg_alloc();
3737 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3738 0, NL80211_CMD_DEL_BEACON
, 0);
3739 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3741 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3747 static int i802_set_bss(void *priv
, int cts
, int preamble
, int slot
)
3749 struct wpa_driver_nl80211_data
*drv
= priv
;
3752 msg
= nlmsg_alloc();
3756 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3757 NL80211_CMD_SET_BSS
, 0);
3760 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_CTS_PROT
, cts
);
3762 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_PREAMBLE
, preamble
);
3764 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_SLOT_TIME
, slot
);
3766 /* TODO: multi-BSS support */
3767 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3769 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3775 static int i802_set_cts_protect(void *priv
, int value
)
3777 return i802_set_bss(priv
, value
, -1, -1);
3781 static int i802_set_preamble(void *priv
, int value
)
3783 return i802_set_bss(priv
, -1, value
, -1);
3787 static int i802_set_short_slot_time(void *priv
, int value
)
3789 return i802_set_bss(priv
, -1, -1, value
);
3793 static enum nl80211_iftype
i802_if_type(enum hostapd_driver_if_type type
)
3796 case HOSTAPD_IF_VLAN
:
3797 return NL80211_IFTYPE_AP_VLAN
;
3803 static int i802_if_add(const char *iface
, void *priv
,
3804 enum hostapd_driver_if_type type
, char *ifname
,
3807 if (nl80211_create_iface(priv
, ifname
, i802_if_type(type
), addr
) < 0)
3813 static int i802_if_update(void *priv
, enum hostapd_driver_if_type type
,
3814 char *ifname
, const u8
*addr
)
3816 /* unused at the moment */
3821 static int i802_if_remove(void *priv
, enum hostapd_driver_if_type type
,
3822 const char *ifname
, const u8
*addr
)
3824 nl80211_remove_iface(priv
, if_nametoindex(ifname
));
3829 static int i802_set_sta_vlan(void *priv
, const u8
*addr
,
3830 const char *ifname
, int vlan_id
)
3832 struct wpa_driver_nl80211_data
*drv
= priv
;
3835 msg
= nlmsg_alloc();
3839 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3840 0, NL80211_CMD_SET_STATION
, 0);
3842 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3843 if_nametoindex(drv
->ifname
));
3844 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3845 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3846 if_nametoindex(ifname
));
3848 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3854 static void handle_eapol(int sock
, void *eloop_ctx
, void *sock_ctx
)
3856 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
3857 struct sockaddr_ll lladdr
;
3858 unsigned char buf
[3000];
3860 socklen_t fromlen
= sizeof(lladdr
);
3862 len
= recvfrom(sock
, buf
, sizeof(buf
), 0,
3863 (struct sockaddr
*)&lladdr
, &fromlen
);
3869 if (have_ifidx(drv
, lladdr
.sll_ifindex
)) {
3870 struct hostapd_data
*hapd
;
3871 hapd
= hostapd_sta_get_bss(drv
->hapd
, lladdr
.sll_addr
);
3874 hostapd_eapol_receive(hapd
, lladdr
.sll_addr
, buf
, len
);
3879 static int i802_get_inact_sec(void *priv
, const u8
*addr
)
3881 struct hostap_sta_driver_data data
;
3884 data
.inactive_msec
= (unsigned long) -1;
3885 ret
= i802_read_sta_data(priv
, &data
, addr
);
3886 if (ret
|| data
.inactive_msec
== (unsigned long) -1)
3888 return data
.inactive_msec
/ 1000;
3892 static int i802_sta_clear_stats(void *priv
, const u8
*addr
)
3901 static int i802_sta_deauth(void *priv
, const u8
*addr
, int reason
)
3903 struct wpa_driver_nl80211_data
*drv
= priv
;
3904 struct ieee80211_mgmt mgmt
;
3906 memset(&mgmt
, 0, sizeof(mgmt
));
3907 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
3908 WLAN_FC_STYPE_DEAUTH
);
3909 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
3910 memcpy(mgmt
.sa
, drv
->hapd
->own_addr
, ETH_ALEN
);
3911 memcpy(mgmt
.bssid
, drv
->hapd
->own_addr
, ETH_ALEN
);
3912 mgmt
.u
.deauth
.reason_code
= host_to_le16(reason
);
3913 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
3915 sizeof(mgmt
.u
.deauth
));
3919 static int i802_sta_disassoc(void *priv
, const u8
*addr
, int reason
)
3921 struct wpa_driver_nl80211_data
*drv
= priv
;
3922 struct ieee80211_mgmt mgmt
;
3924 memset(&mgmt
, 0, sizeof(mgmt
));
3925 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
3926 WLAN_FC_STYPE_DISASSOC
);
3927 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
3928 memcpy(mgmt
.sa
, drv
->hapd
->own_addr
, ETH_ALEN
);
3929 memcpy(mgmt
.bssid
, drv
->hapd
->own_addr
, ETH_ALEN
);
3930 mgmt
.u
.disassoc
.reason_code
= host_to_le16(reason
);
3931 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
3933 sizeof(mgmt
.u
.disassoc
));
3937 static void *i802_init(struct hostapd_data
*hapd
,
3938 struct wpa_init_params
*params
)
3940 struct wpa_driver_nl80211_data
*drv
;
3943 drv
= wpa_driver_nl80211_init(hapd
, params
->ifname
);
3948 drv
->bss
.ifindex
= drv
->ifindex
;
3950 drv
->num_if_indices
= sizeof(drv
->default_if_indices
) / sizeof(int);
3951 drv
->if_indices
= drv
->default_if_indices
;
3952 for (i
= 0; i
< params
->num_bridge
; i
++) {
3953 if (params
->bridge
[i
])
3954 add_ifidx(drv
, if_nametoindex(params
->bridge
[i
]));
3957 /* start listening for EAPOL on the default AP interface */
3958 add_ifidx(drv
, drv
->ifindex
);
3960 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 0))
3963 if (params
->bssid
&& set_ifhwaddr(drv
, drv
->ifname
, params
->bssid
))
3966 /* Initialise a monitor interface */
3967 if (nl80211_create_monitor_interface(drv
))
3970 if (nl80211_set_mode(drv
, drv
->ifindex
, NL80211_IFTYPE_AP
)) {
3971 wpa_printf(MSG_ERROR
, "nl80211: Failed to set interface %s "
3972 "into AP mode", drv
->ifname
);
3976 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1))
3979 drv
->eapol_sock
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_PAE
));
3980 if (drv
->eapol_sock
< 0) {
3981 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
3985 if (eloop_register_read_sock(drv
->eapol_sock
, handle_eapol
, drv
, NULL
))
3987 printf("Could not register read socket for eapol\n");
3991 if (get_ifhwaddr(drv
, drv
->ifname
, drv
->hapd
->own_addr
))
3997 if (drv
->monitor_ifidx
>= 0)
3998 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
3999 if (drv
->ioctl_sock
>= 0)
4000 close(drv
->ioctl_sock
);
4002 genl_family_put(drv
->nl80211
);
4003 nl_cache_free(drv
->nl_cache
);
4004 nl_handle_destroy(drv
->nl_handle
);
4005 nl_cb_put(drv
->nl_cb
);
4012 static void i802_deinit(void *priv
)
4014 wpa_driver_nl80211_deinit(priv
);
4017 #endif /* HOSTAPD */
4020 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
4022 .desc
= "Linux nl80211/cfg80211",
4023 .get_bssid
= wpa_driver_nl80211_get_bssid
,
4024 .get_ssid
= wpa_driver_nl80211_get_ssid
,
4025 .set_key
= wpa_driver_nl80211_set_key
,
4026 .scan2
= wpa_driver_nl80211_scan
,
4027 .get_scan_results2
= wpa_driver_nl80211_get_scan_results
,
4028 .deauthenticate
= wpa_driver_nl80211_deauthenticate
,
4029 .disassociate
= wpa_driver_nl80211_disassociate
,
4030 .authenticate
= wpa_driver_nl80211_authenticate
,
4031 .associate
= wpa_driver_nl80211_associate
,
4032 .init
= wpa_driver_nl80211_init
,
4033 .deinit
= wpa_driver_nl80211_deinit
,
4034 .get_capa
= wpa_driver_nl80211_get_capa
,
4035 .set_operstate
= wpa_driver_nl80211_set_operstate
,
4036 .set_country
= wpa_driver_nl80211_set_country
,
4037 .set_mode
= wpa_driver_nl80211_set_mode
,
4039 .set_beacon
= wpa_driver_nl80211_set_beacon
,
4040 #endif /* CONFIG_AP */
4041 #if defined(CONFIG_AP) || defined(HOSTAPD)
4042 .send_mlme
= wpa_driver_nl80211_send_mlme
,
4043 .set_beacon_int
= wpa_driver_nl80211_set_beacon_int
,
4044 .get_hw_feature_data
= wpa_driver_nl80211_get_hw_feature_data
,
4045 #endif /* CONFIG_AP || HOSTAPD */
4047 .hapd_init
= i802_init
,
4048 .hapd_deinit
= i802_deinit
,
4049 .hapd_set_key
= i802_set_key
,
4050 .get_seqnum
= i802_get_seqnum
,
4051 .flush
= i802_flush
,
4052 .read_sta_data
= i802_read_sta_data
,
4053 .hapd_send_eapol
= i802_send_eapol
,
4054 .sta_set_flags
= i802_sta_set_flags
,
4055 .sta_deauth
= i802_sta_deauth
,
4056 .sta_disassoc
= i802_sta_disassoc
,
4057 .sta_remove
= i802_sta_remove
,
4058 .sta_add
= i802_sta_add
,
4059 .get_inact_sec
= i802_get_inact_sec
,
4060 .sta_clear_stats
= i802_sta_clear_stats
,
4061 .set_freq
= i802_set_freq
,
4062 .set_rts
= i802_set_rts
,
4063 .set_frag
= i802_set_frag
,
4064 .set_retry
= i802_set_retry
,
4065 .set_rate_sets
= i802_set_rate_sets
,
4066 .hapd_set_beacon
= i802_set_beacon
,
4067 .set_cts_protect
= i802_set_cts_protect
,
4068 .set_preamble
= i802_set_preamble
,
4069 .set_short_slot_time
= i802_set_short_slot_time
,
4070 .set_tx_queue_params
= i802_set_tx_queue_params
,
4071 .bss_add
= i802_bss_add
,
4072 .bss_remove
= i802_bss_remove
,
4073 .if_add
= i802_if_add
,
4074 .if_update
= i802_if_update
,
4075 .if_remove
= i802_if_remove
,
4076 .set_sta_vlan
= i802_set_sta_vlan
,
4077 #endif /* HOSTAPD */