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>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include "nl80211_copy.h"
26 #include "wireless_copy.h"
31 #include "ieee802_11_defs.h"
33 #if defined(CONFIG_AP) || defined(HOSTAPD)
34 #include <netpacket/packet.h>
35 #include <linux/filter.h>
37 #include "radiotap_iter.h"
38 #endif /* CONFIG_AP || HOSTAPD */
42 #include "../hostapd/hostapd_defs.h"
45 #define ETH_P_ALL 0x0003
48 #endif /* CONFIG_AP */
51 #include "../../hostapd/hostapd.h"
52 #include "../../hostapd/sta_flags.h"
53 #include "ieee802_11_common.h"
56 /* libnl 2.0 compatibility code */
57 #define nl_handle_alloc_cb nl_socket_alloc_cb
58 #define nl_handle_destroy nl_socket_free
59 #endif /* CONFIG_LIBNL20 */
65 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
68 #define IFF_DORMANT 0x20000 /* driver signals dormant */
71 #ifndef IF_OPER_DORMANT
72 #define IF_OPER_DORMANT 5
78 enum ieee80211_msg_type
{
79 ieee80211_msg_normal
= 0,
80 ieee80211_msg_tx_callback_ack
= 1,
81 ieee80211_msg_tx_callback_fail
= 2,
85 struct i802_bss
*next
;
87 unsigned int beacon_set
:1;
90 struct wpa_driver_nl80211_data
{
93 int ioctl_sock
; /* socket for ioctl() use */
94 char ifname
[IFNAMSIZ
+ 1];
97 struct wpa_driver_capa capa
;
102 int scan_complete_events
;
104 struct nl_handle
*nl_handle
;
105 struct nl_cache
*nl_cache
;
107 struct genl_family
*nl80211
;
114 int ap_scan_as_station
;
116 #if defined(CONFIG_AP) || defined(HOSTAPD)
120 #endif /* CONFIG_AP || HOSTAPD */
123 unsigned int beacon_set
:1;
124 #endif /* CONFIG_AP */
127 struct hostapd_data
*hapd
;
129 int eapol_sock
; /* socket for EAPOL frames */
131 int default_if_indices
[16];
143 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
,
145 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
);
147 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
);
150 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
152 #endif /* CONFIG_AP */
155 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
156 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
157 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
163 static int ack_handler(struct nl_msg
*msg
, void *arg
)
170 static int finish_handler(struct nl_msg
*msg
, void *arg
)
177 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
186 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
192 static int send_and_recv_msgs(struct wpa_driver_nl80211_data
*drv
,
194 int (*valid_handler
)(struct nl_msg
*, void *),
200 cb
= nl_cb_clone(drv
->nl_cb
);
204 err
= nl_send_auto_complete(drv
->nl_handle
, msg
);
210 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
211 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
212 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
215 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
216 valid_handler
, valid_data
);
219 nl_recvmsgs(drv
->nl_handle
, cb
);
233 static int family_handler(struct nl_msg
*msg
, void *arg
)
235 struct family_data
*res
= arg
;
236 struct nlattr
*tb
[CTRL_ATTR_MAX
+ 1];
237 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
238 struct nlattr
*mcgrp
;
241 nla_parse(tb
, CTRL_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
242 genlmsg_attrlen(gnlh
, 0), NULL
);
243 if (!tb
[CTRL_ATTR_MCAST_GROUPS
])
246 nla_for_each_nested(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
247 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
248 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, nla_data(mcgrp
),
249 nla_len(mcgrp
), NULL
);
250 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] ||
251 !tb2
[CTRL_ATTR_MCAST_GRP_ID
] ||
252 os_strncmp(nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]),
254 nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
])) != 0)
256 res
->id
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
264 static int nl_get_multicast_id(struct wpa_driver_nl80211_data
*drv
,
265 const char *family
, const char *group
)
269 struct family_data res
= { group
, -ENOENT
};
274 genlmsg_put(msg
, 0, 0, genl_ctrl_resolve(drv
->nl_handle
, "nlctrl"),
275 0, 0, CTRL_CMD_GETFAMILY
, 0);
276 NLA_PUT_STRING(msg
, CTRL_ATTR_FAMILY_NAME
, family
);
278 ret
= send_and_recv_msgs(drv
, msg
, family_handler
, &res
);
289 static int wpa_driver_nl80211_send_oper_ifla(
290 struct wpa_driver_nl80211_data
*drv
,
291 int linkmode
, int operstate
)
295 struct ifinfomsg ifinfo
;
302 os_memset(&req
, 0, sizeof(req
));
304 req
.hdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
305 req
.hdr
.nlmsg_type
= RTM_SETLINK
;
306 req
.hdr
.nlmsg_flags
= NLM_F_REQUEST
;
307 req
.hdr
.nlmsg_seq
= ++nl_seq
;
308 req
.hdr
.nlmsg_pid
= 0;
310 req
.ifinfo
.ifi_family
= AF_UNSPEC
;
311 req
.ifinfo
.ifi_type
= 0;
312 req
.ifinfo
.ifi_index
= drv
->ifindex
;
313 req
.ifinfo
.ifi_flags
= 0;
314 req
.ifinfo
.ifi_change
= 0;
316 if (linkmode
!= -1) {
317 rta
= (struct rtattr
*)
318 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
319 rta
->rta_type
= IFLA_LINKMODE
;
320 rta
->rta_len
= RTA_LENGTH(sizeof(char));
321 *((char *) RTA_DATA(rta
)) = linkmode
;
322 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
323 RTA_LENGTH(sizeof(char));
325 if (operstate
!= -1) {
326 rta
= (struct rtattr
*)
327 ((char *) &req
+ NLMSG_ALIGN(req
.hdr
.nlmsg_len
));
328 rta
->rta_type
= IFLA_OPERSTATE
;
329 rta
->rta_len
= RTA_LENGTH(sizeof(char));
330 *((char *) RTA_DATA(rta
)) = operstate
;
331 req
.hdr
.nlmsg_len
= NLMSG_ALIGN(req
.hdr
.nlmsg_len
) +
332 RTA_LENGTH(sizeof(char));
335 wpa_printf(MSG_DEBUG
, "nl80211: Operstate: linkmode=%d, operstate=%d",
336 linkmode
, operstate
);
338 ret
= send(drv
->link_event_sock
, &req
, req
.hdr
.nlmsg_len
, 0);
340 wpa_printf(MSG_DEBUG
, "nl80211: Sending operstate IFLA failed:"
341 " %s (assume operstate is not supported)",
345 return ret
< 0 ? -1 : 0;
349 static int wpa_driver_nl80211_set_auth_param(
350 struct wpa_driver_nl80211_data
*drv
, int idx
, u32 value
)
355 os_memset(&iwr
, 0, sizeof(iwr
));
356 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
357 iwr
.u
.param
.flags
= idx
& IW_AUTH_INDEX
;
358 iwr
.u
.param
.value
= value
;
360 if (ioctl(drv
->ioctl_sock
, SIOCSIWAUTH
, &iwr
) < 0) {
361 if (errno
!= EOPNOTSUPP
) {
362 wpa_printf(MSG_DEBUG
, "WEXT: SIOCSIWAUTH(param %d "
363 "value 0x%x) failed: %s)",
364 idx
, value
, strerror(errno
));
366 ret
= errno
== EOPNOTSUPP
? -2 : -1;
373 static int wpa_driver_nl80211_get_bssid(void *priv
, u8
*bssid
)
375 struct wpa_driver_nl80211_data
*drv
= priv
;
376 if (!drv
->associated
)
378 os_memcpy(bssid
, drv
->bssid
, ETH_ALEN
);
383 static int wpa_driver_nl80211_get_ssid(void *priv
, u8
*ssid
)
385 struct wpa_driver_nl80211_data
*drv
= priv
;
386 if (!drv
->associated
)
388 os_memcpy(ssid
, drv
->ssid
, drv
->ssid_len
);
389 return drv
->ssid_len
;
394 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data
*drv
,
395 void *ctx
, char *buf
, size_t len
,
398 union wpa_event_data event
;
400 os_memset(&event
, 0, sizeof(event
));
401 if (len
> sizeof(event
.interface_status
.ifname
))
402 len
= sizeof(event
.interface_status
.ifname
) - 1;
403 os_memcpy(event
.interface_status
.ifname
, buf
, len
);
404 event
.interface_status
.ievent
= del
? EVENT_INTERFACE_REMOVED
:
405 EVENT_INTERFACE_ADDED
;
407 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
409 event
.interface_status
.ifname
,
410 del
? "removed" : "added");
412 if (os_strcmp(drv
->ifname
, event
.interface_status
.ifname
) == 0) {
419 wpa_supplicant_event(ctx
, EVENT_INTERFACE_STATUS
, &event
);
423 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data
*drv
,
426 struct ifinfomsg
*ifi
;
427 int attrlen
, _nlmsg_len
, rta_len
;
432 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
434 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
438 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
440 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
441 while (RTA_OK(attr
, attrlen
)) {
442 if (attr
->rta_type
== IFLA_IFNAME
) {
443 if (os_strcmp(((char *) attr
) + rta_len
, drv
->ifname
)
449 attr
= RTA_NEXT(attr
, attrlen
);
456 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data
*drv
,
457 int ifindex
, struct nlmsghdr
*h
)
459 if (drv
->ifindex
== ifindex
)
462 if (drv
->if_removed
&& wpa_driver_nl80211_own_ifname(drv
, h
)) {
463 drv
->ifindex
= if_nametoindex(drv
->ifname
);
464 wpa_printf(MSG_DEBUG
, "nl80211: Update ifindex for a removed "
466 wpa_driver_nl80211_finish_drv_init(drv
);
474 static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data
*drv
,
475 void *ctx
, struct nlmsghdr
*h
,
478 struct ifinfomsg
*ifi
;
479 int attrlen
, _nlmsg_len
, rta_len
;
480 struct rtattr
* attr
;
482 if (len
< sizeof(*ifi
))
487 if (!wpa_driver_nl80211_own_ifindex(drv
, ifi
->ifi_index
, h
)) {
488 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
493 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
495 drv
->operstate
, ifi
->ifi_flags
,
496 (ifi
->ifi_flags
& IFF_UP
) ? "[UP]" : "",
497 (ifi
->ifi_flags
& IFF_RUNNING
) ? "[RUNNING]" : "",
498 (ifi
->ifi_flags
& IFF_LOWER_UP
) ? "[LOWER_UP]" : "",
499 (ifi
->ifi_flags
& IFF_DORMANT
) ? "[DORMANT]" : "");
501 * Some drivers send the association event before the operup event--in
502 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
503 * fails. This will hit us when wpa_supplicant does not need to do
504 * IEEE 802.1X authentication
506 if (drv
->operstate
== 1 &&
507 (ifi
->ifi_flags
& (IFF_LOWER_UP
| IFF_DORMANT
)) == IFF_LOWER_UP
&&
508 !(ifi
->ifi_flags
& IFF_RUNNING
))
509 wpa_driver_nl80211_send_oper_ifla(drv
, -1, IF_OPER_UP
);
511 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
513 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
517 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
519 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
520 while (RTA_OK(attr
, attrlen
)) {
521 if (attr
->rta_type
== IFLA_IFNAME
) {
522 wpa_driver_nl80211_event_link(
524 ((char *) attr
) + rta_len
,
525 attr
->rta_len
- rta_len
, 0);
527 attr
= RTA_NEXT(attr
, attrlen
);
532 static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data
*drv
,
533 void *ctx
, struct nlmsghdr
*h
,
536 struct ifinfomsg
*ifi
;
537 int attrlen
, _nlmsg_len
, rta_len
;
538 struct rtattr
* attr
;
540 if (len
< sizeof(*ifi
))
545 _nlmsg_len
= NLMSG_ALIGN(sizeof(struct ifinfomsg
));
547 attrlen
= h
->nlmsg_len
- _nlmsg_len
;
551 attr
= (struct rtattr
*) (((char *) ifi
) + _nlmsg_len
);
553 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
554 while (RTA_OK(attr
, attrlen
)) {
555 if (attr
->rta_type
== IFLA_IFNAME
) {
556 wpa_driver_nl80211_event_link(
558 ((char *) attr
) + rta_len
,
559 attr
->rta_len
- rta_len
, 1);
561 attr
= RTA_NEXT(attr
, attrlen
);
566 static void wpa_driver_nl80211_event_receive_link(int sock
, void *eloop_ctx
,
571 struct sockaddr_nl from
;
577 fromlen
= sizeof(from
);
578 left
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
,
579 (struct sockaddr
*) &from
, &fromlen
);
581 if (errno
!= EINTR
&& errno
!= EAGAIN
)
582 perror("recvfrom(netlink)");
586 h
= (struct nlmsghdr
*) buf
;
587 while (left
>= (int) sizeof(*h
)) {
591 plen
= len
- sizeof(*h
);
592 if (len
> left
|| plen
< 0) {
593 wpa_printf(MSG_DEBUG
, "Malformed netlink message: "
594 "len=%d left=%d plen=%d",
599 switch (h
->nlmsg_type
) {
601 wpa_driver_nl80211_event_rtm_newlink(eloop_ctx
, sock_ctx
,
605 wpa_driver_nl80211_event_rtm_dellink(eloop_ctx
, sock_ctx
,
610 len
= NLMSG_ALIGN(len
);
612 h
= (struct nlmsghdr
*) ((char *) h
+ len
);
616 wpa_printf(MSG_DEBUG
, "%d extra bytes in the end of netlink "
620 if (--max_events
> 0) {
622 * Try to receive all events in one eloop call in order to
623 * limit race condition on cases where AssocInfo event, Assoc
624 * event, and EAPOL frames are received more or less at the
625 * same time. We want to process the event messages first
626 * before starting EAPOL processing.
633 static void mlme_event_auth(struct wpa_driver_nl80211_data
*drv
,
634 const u8
*frame
, size_t len
)
636 const struct ieee80211_mgmt
*mgmt
;
637 union wpa_event_data event
;
639 mgmt
= (const struct ieee80211_mgmt
*) frame
;
640 if (len
< 24 + sizeof(mgmt
->u
.auth
)) {
641 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
646 os_memset(&event
, 0, sizeof(event
));
647 os_memcpy(event
.auth
.peer
, mgmt
->sa
, ETH_ALEN
);
648 event
.auth
.auth_type
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
649 event
.auth
.status_code
= le_to_host16(mgmt
->u
.auth
.status_code
);
650 if (len
> 24 + sizeof(mgmt
->u
.auth
)) {
651 event
.auth
.ies
= mgmt
->u
.auth
.variable
;
652 event
.auth
.ies_len
= len
- 24 - sizeof(mgmt
->u
.auth
);
655 wpa_supplicant_event(drv
->ctx
, EVENT_AUTH
, &event
);
659 static void mlme_event_assoc(struct wpa_driver_nl80211_data
*drv
,
660 const u8
*frame
, size_t len
)
662 const struct ieee80211_mgmt
*mgmt
;
663 union wpa_event_data event
;
666 mgmt
= (const struct ieee80211_mgmt
*) frame
;
667 if (len
< 24 + sizeof(mgmt
->u
.assoc_resp
)) {
668 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
673 status
= le_to_host16(mgmt
->u
.assoc_resp
.status_code
);
674 if (status
!= WLAN_STATUS_SUCCESS
) {
675 os_memset(&event
, 0, sizeof(event
));
676 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
677 event
.assoc_reject
.resp_ies
=
678 (u8
*) mgmt
->u
.assoc_resp
.variable
;
679 event
.assoc_reject
.resp_ies_len
=
680 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
682 event
.assoc_reject
.status_code
= status
;
684 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
689 os_memcpy(drv
->bssid
, mgmt
->sa
, ETH_ALEN
);
691 os_memset(&event
, 0, sizeof(event
));
692 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
693 event
.assoc_info
.resp_ies
= (u8
*) mgmt
->u
.assoc_resp
.variable
;
694 event
.assoc_info
.resp_ies_len
=
695 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
698 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
702 static void mlme_event(struct wpa_driver_nl80211_data
*drv
,
703 enum nl80211_commands cmd
, struct nlattr
*frame
)
706 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d without frame "
711 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d", cmd
);
712 wpa_hexdump(MSG_MSGDUMP
, "nl80211: MLME event frame",
713 nla_data(frame
), nla_len(frame
));
716 case NL80211_CMD_AUTHENTICATE
:
717 mlme_event_auth(drv
, nla_data(frame
), nla_len(frame
));
719 case NL80211_CMD_ASSOCIATE
:
720 mlme_event_assoc(drv
, nla_data(frame
), nla_len(frame
));
722 case NL80211_CMD_DEAUTHENTICATE
:
724 wpa_supplicant_event(drv
->ctx
, EVENT_DEAUTH
, NULL
);
726 case NL80211_CMD_DISASSOCIATE
:
728 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
738 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data
*drv
,
741 union wpa_event_data data
;
743 wpa_printf(MSG_DEBUG
, "nl80211: MLME event Michael MIC failure");
744 os_memset(&data
, 0, sizeof(data
));
745 if (tb
[NL80211_ATTR_MAC
]) {
746 wpa_hexdump(MSG_DEBUG
, "nl80211: Source MAC address",
747 nla_data(tb
[NL80211_ATTR_MAC
]),
748 nla_len(tb
[NL80211_ATTR_MAC
]));
749 data
.michael_mic_failure
.src
= nla_data(tb
[NL80211_ATTR_MAC
]);
751 if (tb
[NL80211_ATTR_KEY_SEQ
]) {
752 wpa_hexdump(MSG_DEBUG
, "nl80211: TSC",
753 nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
754 nla_len(tb
[NL80211_ATTR_KEY_SEQ
]));
756 if (tb
[NL80211_ATTR_KEY_TYPE
]) {
757 enum nl80211_key_type key_type
=
758 nla_get_u32(tb
[NL80211_ATTR_KEY_TYPE
]);
759 wpa_printf(MSG_DEBUG
, "nl80211: Key Type %d", key_type
);
760 if (key_type
== NL80211_KEYTYPE_PAIRWISE
)
761 data
.michael_mic_failure
.unicast
= 1;
763 data
.michael_mic_failure
.unicast
= 1;
765 if (tb
[NL80211_ATTR_KEY_IDX
]) {
766 u8 key_id
= nla_get_u8(tb
[NL80211_ATTR_KEY_IDX
]);
767 wpa_printf(MSG_DEBUG
, "nl80211: Key Id %d", key_id
);
770 wpa_supplicant_event(drv
->ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
774 static int process_event(struct nl_msg
*msg
, void *arg
)
776 struct wpa_driver_nl80211_data
*drv
= arg
;
777 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
778 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
780 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
781 genlmsg_attrlen(gnlh
, 0), NULL
);
783 if (tb
[NL80211_ATTR_IFINDEX
]) {
784 int ifindex
= nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]);
785 if (ifindex
!= drv
->ifindex
) {
786 wpa_printf(MSG_DEBUG
, "nl80211: Ignored event (cmd=%d)"
787 " for foreign interface (ifindex %d)",
793 if (drv
->ap_scan_as_station
&&
794 (gnlh
->cmd
== NL80211_CMD_NEW_SCAN_RESULTS
||
795 gnlh
->cmd
== NL80211_CMD_SCAN_ABORTED
)) {
796 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
797 drv
->ap_scan_as_station
= 0;
801 case NL80211_CMD_NEW_SCAN_RESULTS
:
802 wpa_printf(MSG_DEBUG
, "nl80211: New scan results available");
803 drv
->scan_complete_events
= 1;
804 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
806 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, NULL
);
808 case NL80211_CMD_SCAN_ABORTED
:
809 wpa_printf(MSG_DEBUG
, "nl80211: Scan aborted");
811 * Need to indicate that scan results are available in order
812 * not to make wpa_supplicant stop its scanning.
814 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
816 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, NULL
);
819 case NL80211_CMD_AUTHENTICATE
:
820 case NL80211_CMD_ASSOCIATE
:
821 case NL80211_CMD_DEAUTHENTICATE
:
822 case NL80211_CMD_DISASSOCIATE
:
823 mlme_event(drv
, gnlh
->cmd
, tb
[NL80211_ATTR_FRAME
]);
826 case NL80211_CMD_MICHAEL_MIC_FAILURE
:
827 mlme_event_michael_mic_failure(drv
, tb
);
830 wpa_printf(MSG_DEBUG
, "nl80211: Ignored unknown event "
831 "(cmd=%d)", gnlh
->cmd
);
839 static void wpa_driver_nl80211_event_receive(int sock
, void *eloop_ctx
,
843 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
845 wpa_printf(MSG_DEBUG
, "nl80211: Event message available");
847 cb
= nl_cb_clone(drv
->nl_cb
);
850 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
851 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, process_event
, drv
);
852 nl_recvmsgs(drv
->nl_handle
, cb
);
857 static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data
*drv
,
858 const char *ifname
, int dev_up
)
862 if (drv
->ioctl_sock
< 0)
865 os_memset(&ifr
, 0, sizeof(ifr
));
866 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
868 if (ioctl(drv
->ioctl_sock
, SIOCGIFFLAGS
, &ifr
) != 0) {
869 perror("ioctl[SIOCGIFFLAGS]");
870 wpa_printf(MSG_DEBUG
, "Could not read interface flags (%s)",
876 if (ifr
.ifr_flags
& IFF_UP
)
878 ifr
.ifr_flags
|= IFF_UP
;
880 if (!(ifr
.ifr_flags
& IFF_UP
))
882 ifr
.ifr_flags
&= ~IFF_UP
;
885 if (ioctl(drv
->ioctl_sock
, SIOCSIFFLAGS
, &ifr
) != 0) {
886 perror("ioctl[SIOCSIFFLAGS]");
895 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
896 * @priv: driver_nl80211 private data
897 * @alpha2_arg: country to which to switch to
898 * Returns: 0 on success, -1 on failure
900 * This asks nl80211 to set the regulatory domain for given
901 * country ISO / IEC alpha2.
903 static int wpa_driver_nl80211_set_country(void *priv
, const char *alpha2_arg
)
905 struct wpa_driver_nl80211_data
*drv
= priv
;
913 alpha2
[0] = alpha2_arg
[0];
914 alpha2
[1] = alpha2_arg
[1];
917 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
918 0, NL80211_CMD_REQ_SET_REG
, 0);
920 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
921 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
))
929 struct wiphy_info_data
{
935 static int wiphy_info_handler(struct nl_msg
*msg
, void *arg
)
937 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
938 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
939 struct wiphy_info_data
*info
= arg
;
941 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
942 genlmsg_attrlen(gnlh
, 0), NULL
);
944 if (tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
])
945 info
->max_scan_ssids
=
946 nla_get_u8(tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
]);
948 if (tb
[NL80211_ATTR_SUPPORTED_IFTYPES
]) {
949 struct nlattr
*nl_mode
;
951 nla_for_each_nested(nl_mode
,
952 tb
[NL80211_ATTR_SUPPORTED_IFTYPES
], i
) {
953 if (nl_mode
->nla_type
== NL80211_IFTYPE_AP
) {
954 info
->ap_supported
= 1;
964 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data
*drv
,
965 struct wiphy_info_data
*info
)
969 os_memset(info
, 0, sizeof(*info
));
974 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
975 0, NL80211_CMD_GET_WIPHY
, 0);
977 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
979 if (send_and_recv_msgs(drv
, msg
, wiphy_info_handler
, info
) == 0)
988 static void wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data
*drv
)
990 struct wiphy_info_data info
;
991 if (wpa_driver_nl80211_get_info(drv
, &info
))
993 drv
->has_capability
= 1;
994 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
995 drv
->capa
.key_mgmt
= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
996 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
997 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
998 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
;
999 drv
->capa
.enc
= WPA_DRIVER_CAPA_ENC_WEP40
|
1000 WPA_DRIVER_CAPA_ENC_WEP104
|
1001 WPA_DRIVER_CAPA_ENC_TKIP
|
1002 WPA_DRIVER_CAPA_ENC_CCMP
;
1004 drv
->capa
.max_scan_ssids
= info
.max_scan_ssids
;
1005 if (info
.ap_supported
)
1006 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_AP
;
1010 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data
*drv
,
1015 /* Initialize generic netlink and nl80211 */
1017 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
1018 if (drv
->nl_cb
== NULL
) {
1019 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1024 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
1025 if (drv
->nl_handle
== NULL
) {
1026 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1031 if (genl_connect(drv
->nl_handle
)) {
1032 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
1037 #ifdef CONFIG_LIBNL20
1038 if (genl_ctrl_alloc_cache(drv
->nl_handle
, &drv
->nl_cache
) < 0) {
1039 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1043 #else /* CONFIG_LIBNL20 */
1044 drv
->nl_cache
= genl_ctrl_alloc_cache(drv
->nl_handle
);
1045 if (drv
->nl_cache
== NULL
) {
1046 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1050 #endif /* CONFIG_LIBNL20 */
1053 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
1054 if (drv
->nl80211
== NULL
) {
1055 wpa_printf(MSG_ERROR
, "nl80211: 'nl80211' generic netlink not "
1060 ret
= nl_get_multicast_id(drv
, "nl80211", "scan");
1062 ret
= nl_socket_add_membership(drv
->nl_handle
, ret
);
1064 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1065 "membership for scan events: %d (%s)",
1066 ret
, strerror(-ret
));
1070 ret
= nl_get_multicast_id(drv
, "nl80211", "mlme");
1072 ret
= nl_socket_add_membership(drv
->nl_handle
, ret
);
1074 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1075 "membership for mlme events: %d (%s)",
1076 ret
, strerror(-ret
));
1080 eloop_register_read_sock(nl_socket_get_fd(drv
->nl_handle
),
1081 wpa_driver_nl80211_event_receive
, drv
, ctx
);
1086 nl_cache_free(drv
->nl_cache
);
1088 nl_handle_destroy(drv
->nl_handle
);
1090 nl_cb_put(drv
->nl_cb
);
1097 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1098 * @ctx: context to be used when calling wpa_supplicant functions,
1099 * e.g., wpa_supplicant_event()
1100 * @ifname: interface name, e.g., wlan0
1101 * Returns: Pointer to private data, %NULL on failure
1103 static void * wpa_driver_nl80211_init(void *ctx
, const char *ifname
)
1106 struct sockaddr_nl local
;
1107 struct wpa_driver_nl80211_data
*drv
;
1109 drv
= os_zalloc(sizeof(*drv
));
1113 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1115 drv
->monitor_ifidx
= -1;
1116 drv
->monitor_sock
= -1;
1117 #endif /* CONFIG_AP */
1119 if (wpa_driver_nl80211_init_nl(drv
, ctx
))
1122 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SME
;
1124 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1125 if (drv
->ioctl_sock
< 0) {
1126 perror("socket(PF_INET,SOCK_DGRAM)");
1130 s
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1132 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1136 os_memset(&local
, 0, sizeof(local
));
1137 local
.nl_family
= AF_NETLINK
;
1138 local
.nl_groups
= RTMGRP_LINK
;
1139 if (bind(s
, (struct sockaddr
*) &local
, sizeof(local
)) < 0) {
1140 perror("bind(netlink)");
1146 eloop_register_read_sock(s
, wpa_driver_nl80211_event_receive_link
, drv
,
1148 #endif /* HOSTAPD */
1149 drv
->link_event_sock
= s
;
1151 if (wpa_driver_nl80211_finish_drv_init(drv
))
1157 eloop_unregister_read_sock(drv
->link_event_sock
);
1158 close(drv
->link_event_sock
);
1160 close(drv
->ioctl_sock
);
1162 genl_family_put(drv
->nl80211
);
1163 nl_cache_free(drv
->nl_cache
);
1164 nl_handle_destroy(drv
->nl_handle
);
1165 nl_cb_put(drv
->nl_cb
);
1173 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
)
1175 drv
->ifindex
= if_nametoindex(drv
->ifname
);
1177 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
) < 0) {
1178 wpa_printf(MSG_DEBUG
, "nl80211: Could not configure driver to "
1179 "use managed mode");
1182 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1)) {
1183 wpa_printf(MSG_ERROR
, "Could not set interface '%s' "
1188 wpa_driver_nl80211_capa(drv
);
1190 wpa_driver_nl80211_send_oper_ifla(drv
, 1, IF_OPER_DORMANT
);
1197 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1198 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1200 * Shut down driver interface and processing of driver events. Free
1201 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1203 static void wpa_driver_nl80211_deinit(void *priv
)
1205 struct wpa_driver_nl80211_data
*drv
= priv
;
1208 if (drv
->monitor_ifidx
>= 0)
1209 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
1210 if (drv
->monitor_sock
>= 0) {
1211 eloop_unregister_read_sock(drv
->monitor_sock
);
1212 close(drv
->monitor_sock
);
1214 #endif /* CONFIG_AP */
1216 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1218 wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
, 0);
1220 wpa_driver_nl80211_send_oper_ifla(priv
, 0, IF_OPER_UP
);
1222 eloop_unregister_read_sock(drv
->link_event_sock
);
1224 hostapd_set_iface_flags(drv
, drv
->ifname
, 0);
1225 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
);
1227 close(drv
->link_event_sock
);
1228 close(drv
->ioctl_sock
);
1230 eloop_unregister_read_sock(nl_socket_get_fd(drv
->nl_handle
));
1231 genl_family_put(drv
->nl80211
);
1232 nl_cache_free(drv
->nl_cache
);
1233 nl_handle_destroy(drv
->nl_handle
);
1234 nl_cb_put(drv
->nl_cb
);
1241 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1242 * @eloop_ctx: Driver private data
1243 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1245 * This function can be used as registered timeout when starting a scan to
1246 * generate a scan completed event if the driver does not report this.
1248 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1250 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
1251 if (drv
->ap_scan_as_station
) {
1252 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
1253 drv
->ap_scan_as_station
= 0;
1255 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1256 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1261 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1262 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
1263 * @params: Scan parameters
1264 * Returns: 0 on success, -1 on failure
1266 static int wpa_driver_nl80211_scan(void *priv
,
1267 struct wpa_driver_scan_params
*params
)
1269 struct wpa_driver_nl80211_data
*drv
= priv
;
1270 int ret
= 0, timeout
;
1271 struct nl_msg
*msg
, *ssids
, *freqs
;
1274 msg
= nlmsg_alloc();
1275 ssids
= nlmsg_alloc();
1276 freqs
= nlmsg_alloc();
1277 if (!msg
|| !ssids
|| !freqs
) {
1284 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1285 NL80211_CMD_TRIGGER_SCAN
, 0);
1287 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1289 for (i
= 0; i
< params
->num_ssids
; i
++) {
1290 NLA_PUT(ssids
, i
+ 1, params
->ssids
[i
].ssid_len
,
1291 params
->ssids
[i
].ssid
);
1293 if (params
->num_ssids
)
1294 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
1296 if (params
->extra_ies
) {
1297 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->extra_ies_len
,
1301 if (params
->freqs
) {
1302 for (i
= 0; params
->freqs
[i
]; i
++)
1303 NLA_PUT_U32(freqs
, i
+ 1, params
->freqs
[i
]);
1304 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
1307 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1310 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger failed: ret=%d "
1311 "(%s)", ret
, strerror(-ret
));
1313 if (drv
->nlmode
== NL80211_IFTYPE_AP
) {
1315 * mac80211 does not allow scan requests in AP mode, so
1316 * try to do this in station mode.
1318 if (wpa_driver_nl80211_set_mode(drv
,
1319 IEEE80211_MODE_INFRA
))
1320 goto nla_put_failure
;
1322 if (wpa_driver_nl80211_scan(drv
, params
)) {
1323 wpa_driver_nl80211_set_mode(drv
,
1325 goto nla_put_failure
;
1328 /* Restore AP mode when processing scan results */
1329 drv
->ap_scan_as_station
= 1;
1332 goto nla_put_failure
;
1334 goto nla_put_failure
;
1335 #endif /* HOSTAPD */
1338 /* Not all drivers generate "scan completed" wireless event, so try to
1339 * read results after a timeout. */
1341 if (drv
->scan_complete_events
) {
1343 * The driver seems to deliver events to notify when scan is
1344 * complete, so use longer timeout to avoid race conditions
1345 * with scanning and following association request.
1349 wpa_printf(MSG_DEBUG
, "Scan requested (ret=%d) - scan timeout %d "
1350 "seconds", ret
, timeout
);
1351 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1352 eloop_register_timeout(timeout
, 0, wpa_driver_nl80211_scan_timeout
,
1363 static int bss_info_handler(struct nl_msg
*msg
, void *arg
)
1365 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1366 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1367 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1368 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1369 [NL80211_BSS_BSSID
] = { .type
= NLA_UNSPEC
},
1370 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1371 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1372 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1373 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1374 [NL80211_BSS_INFORMATION_ELEMENTS
] = { .type
= NLA_UNSPEC
},
1375 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1376 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1378 struct wpa_scan_results
*res
= arg
;
1379 struct wpa_scan_res
**tmp
;
1380 struct wpa_scan_res
*r
;
1384 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1385 genlmsg_attrlen(gnlh
, 0), NULL
);
1386 if (!tb
[NL80211_ATTR_BSS
])
1388 if (nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1391 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
]) {
1392 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1393 ie_len
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1399 r
= os_zalloc(sizeof(*r
) + ie_len
);
1402 if (bss
[NL80211_BSS_BSSID
])
1403 os_memcpy(r
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]),
1405 if (bss
[NL80211_BSS_FREQUENCY
])
1406 r
->freq
= nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]);
1407 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1408 r
->beacon_int
= nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]);
1409 if (bss
[NL80211_BSS_CAPABILITY
])
1410 r
->caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1411 r
->flags
|= WPA_SCAN_NOISE_INVALID
;
1412 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1413 r
->level
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1414 r
->level
/= 100; /* mBm to dBm */
1415 r
->flags
|= WPA_SCAN_LEVEL_DBM
| WPA_SCAN_QUAL_INVALID
;
1416 } else if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1417 r
->level
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1418 r
->flags
|= WPA_SCAN_LEVEL_INVALID
;
1420 r
->flags
|= WPA_SCAN_LEVEL_INVALID
| WPA_SCAN_QUAL_INVALID
;
1421 if (bss
[NL80211_BSS_TSF
])
1422 r
->tsf
= nla_get_u64(bss
[NL80211_BSS_TSF
]);
1425 os_memcpy(r
+ 1, ie
, ie_len
);
1427 tmp
= os_realloc(res
->res
,
1428 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1433 tmp
[res
->num
++] = r
;
1441 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1442 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1443 * Returns: Scan results on success, -1 on failure
1445 static struct wpa_scan_results
*
1446 wpa_driver_nl80211_get_scan_results(void *priv
)
1448 struct wpa_driver_nl80211_data
*drv
= priv
;
1450 struct wpa_scan_results
*res
;
1453 res
= os_zalloc(sizeof(*res
));
1456 msg
= nlmsg_alloc();
1458 goto nla_put_failure
;
1460 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, NLM_F_DUMP
,
1461 NL80211_CMD_GET_SCAN
, 0);
1462 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1464 ret
= send_and_recv_msgs(drv
, msg
, bss_info_handler
, res
);
1467 wpa_printf(MSG_DEBUG
, "Received scan results (%lu BSSes)",
1468 (unsigned long) res
->num
);
1471 wpa_printf(MSG_DEBUG
, "nl80211: Scan result fetch failed: ret=%d "
1472 "(%s)", ret
, strerror(-ret
));
1475 wpa_scan_results_free(res
);
1480 static int nl_set_encr(int ifindex
, struct wpa_driver_nl80211_data
*drv
,
1481 wpa_alg alg
, const u8
*addr
, int key_idx
, int set_tx
,
1482 const u8
*seq
, size_t seq_len
,
1483 const u8
*key
, size_t key_len
)
1488 wpa_printf(MSG_DEBUG
, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1489 "set_tx=%d seq_len=%lu key_len=%lu",
1490 __func__
, ifindex
, alg
, addr
, key_idx
, set_tx
,
1491 (unsigned long) seq_len
, (unsigned long) key_len
);
1493 msg
= nlmsg_alloc();
1497 if (alg
== WPA_ALG_NONE
) {
1498 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1499 0, NL80211_CMD_DEL_KEY
, 0);
1501 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1502 0, NL80211_CMD_NEW_KEY
, 0);
1503 NLA_PUT(msg
, NL80211_ATTR_KEY_DATA
, key_len
, key
);
1507 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1510 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1514 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC02);
1517 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC04);
1520 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
, 0x000FAC06);
1523 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1524 "algorithm %d", __func__
, alg
);
1531 NLA_PUT(msg
, NL80211_ATTR_KEY_SEQ
, seq_len
, seq
);
1533 if (addr
&& os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
1535 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1536 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1538 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1539 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1541 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1542 if (ret
== -ENOENT
&& alg
== WPA_ALG_NONE
)
1545 wpa_printf(MSG_DEBUG
, "nl80211: set_key failed; err=%d %s)",
1546 ret
, strerror(-ret
));
1549 * If we failed or don't need to set the default TX key (below),
1552 if (ret
|| !set_tx
|| alg
== WPA_ALG_NONE
)
1554 #ifdef HOSTAPD /* FIX: is this needed? */
1557 #endif /* HOSTAPD */
1559 msg
= nlmsg_alloc();
1563 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1564 0, NL80211_CMD_SET_KEY
, 0);
1565 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1566 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1567 if (alg
== WPA_ALG_IGTK
)
1568 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT_MGMT
);
1570 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
1572 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1576 wpa_printf(MSG_DEBUG
, "nl80211: set_key default failed; "
1577 "err=%d %s)", ret
, strerror(-ret
));
1585 static int wpa_driver_nl80211_set_key(void *priv
, wpa_alg alg
,
1586 const u8
*addr
, int key_idx
,
1587 int set_tx
, const u8
*seq
,
1589 const u8
*key
, size_t key_len
)
1591 struct wpa_driver_nl80211_data
*drv
= priv
;
1592 return nl_set_encr(drv
->ifindex
, drv
, alg
, addr
, key_idx
, set_tx
, seq
,
1593 seq_len
, key
, key_len
);
1597 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
1598 const u8
*addr
, int cmd
, u16 reason_code
)
1603 msg
= nlmsg_alloc();
1607 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0, cmd
, 0);
1609 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1610 NLA_PUT_U16(msg
, NL80211_ATTR_REASON_CODE
, reason_code
);
1611 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1613 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1616 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1617 "(%s)", ret
, strerror(-ret
));
1618 goto nla_put_failure
;
1628 static int wpa_driver_nl80211_deauthenticate(void *priv
, const u8
*addr
,
1631 struct wpa_driver_nl80211_data
*drv
= priv
;
1632 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1633 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DEAUTHENTICATE
,
1638 static int wpa_driver_nl80211_disassociate(void *priv
, const u8
*addr
,
1641 struct wpa_driver_nl80211_data
*drv
= priv
;
1642 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1643 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DISASSOCIATE
,
1648 static int wpa_driver_nl80211_authenticate(
1649 void *priv
, struct wpa_driver_auth_params
*params
)
1651 struct wpa_driver_nl80211_data
*drv
= priv
;
1654 enum nl80211_auth_type type
;
1656 drv
->associated
= 0;
1658 msg
= nlmsg_alloc();
1662 wpa_printf(MSG_DEBUG
, "nl80211: Authenticate (ifindex=%d)",
1665 for (i
= 0; i
< 4; i
++) {
1666 if (!params
->wep_key
[i
])
1668 wpa_driver_nl80211_set_key(drv
, WPA_ALG_WEP
, NULL
, i
,
1669 i
== params
->wep_tx_keyidx
, NULL
, 0,
1671 params
->wep_key_len
[i
]);
1674 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1675 NL80211_CMD_AUTHENTICATE
, 0);
1677 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1678 if (params
->bssid
) {
1679 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
1680 MAC2STR(params
->bssid
));
1681 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
1684 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
1685 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
1688 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
1689 params
->ssid
, params
->ssid_len
);
1690 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
1693 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->ie
, params
->ie_len
);
1695 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->ie_len
, params
->ie
);
1697 * TODO: if multiple auth_alg options enabled, try them one by one if
1698 * the AP rejects authentication due to unknown auth alg
1700 if (params
->auth_alg
& AUTH_ALG_OPEN_SYSTEM
)
1701 type
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
1702 else if (params
->auth_alg
& AUTH_ALG_SHARED_KEY
)
1703 type
= NL80211_AUTHTYPE_SHARED_KEY
;
1704 else if (params
->auth_alg
& AUTH_ALG_LEAP
)
1705 type
= NL80211_AUTHTYPE_NETWORK_EAP
;
1706 else if (params
->auth_alg
& AUTH_ALG_FT
)
1707 type
= NL80211_AUTHTYPE_FT
;
1709 goto nla_put_failure
;
1710 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
1711 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
1713 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1716 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1717 "(%s)", ret
, strerror(-ret
));
1718 goto nla_put_failure
;
1721 wpa_printf(MSG_DEBUG
, "nl80211: Authentication request send "
1730 #if defined(CONFIG_AP) || defined(HOSTAPD)
1732 struct phy_info_arg
{
1734 struct hostapd_hw_modes
*modes
;
1737 static int phy_info_handler(struct nl_msg
*msg
, void *arg
)
1739 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
1740 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1741 struct phy_info_arg
*phy_info
= arg
;
1743 struct nlattr
*tb_band
[NL80211_BAND_ATTR_MAX
+ 1];
1745 struct nlattr
*tb_freq
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
1746 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
1747 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
1748 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
1749 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
1750 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
1751 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
1752 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
1755 struct nlattr
*tb_rate
[NL80211_BITRATE_ATTR_MAX
+ 1];
1756 static struct nla_policy rate_policy
[NL80211_BITRATE_ATTR_MAX
+ 1] = {
1757 [NL80211_BITRATE_ATTR_RATE
] = { .type
= NLA_U32
},
1758 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
] = { .type
= NLA_FLAG
},
1761 struct nlattr
*nl_band
;
1762 struct nlattr
*nl_freq
;
1763 struct nlattr
*nl_rate
;
1764 int rem_band
, rem_freq
, rem_rate
;
1765 struct hostapd_hw_modes
*mode
;
1766 int idx
, mode_is_set
;
1768 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1769 genlmsg_attrlen(gnlh
, 0), NULL
);
1771 if (!tb_msg
[NL80211_ATTR_WIPHY_BANDS
])
1774 nla_for_each_nested(nl_band
, tb_msg
[NL80211_ATTR_WIPHY_BANDS
], rem_band
) {
1775 mode
= realloc(phy_info
->modes
, (*phy_info
->num_modes
+ 1) * sizeof(*mode
));
1778 phy_info
->modes
= mode
;
1782 mode
= &phy_info
->modes
[*(phy_info
->num_modes
)];
1783 memset(mode
, 0, sizeof(*mode
));
1784 *(phy_info
->num_modes
) += 1;
1786 nla_parse(tb_band
, NL80211_BAND_ATTR_MAX
, nla_data(nl_band
),
1787 nla_len(nl_band
), NULL
);
1789 if (tb_band
[NL80211_BAND_ATTR_HT_CAPA
]) {
1790 mode
->ht_capab
= nla_get_u16(
1791 tb_band
[NL80211_BAND_ATTR_HT_CAPA
]);
1794 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
1795 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
1796 nla_len(nl_freq
), freq_policy
);
1797 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
1799 mode
->num_channels
++;
1802 mode
->channels
= calloc(mode
->num_channels
, sizeof(struct hostapd_channel_data
));
1803 if (!mode
->channels
)
1808 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
1809 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
1810 nla_len(nl_freq
), freq_policy
);
1811 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
1814 mode
->channels
[idx
].freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
1815 mode
->channels
[idx
].flag
= 0;
1818 /* crude heuristic */
1819 if (mode
->channels
[idx
].freq
< 4000)
1820 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
1822 mode
->mode
= HOSTAPD_MODE_IEEE80211A
;
1826 /* crude heuristic */
1827 if (mode
->channels
[idx
].freq
< 4000)
1828 if (mode
->channels
[idx
].freq
== 2484)
1829 mode
->channels
[idx
].chan
= 14;
1831 mode
->channels
[idx
].chan
= (mode
->channels
[idx
].freq
- 2407) / 5;
1833 mode
->channels
[idx
].chan
= mode
->channels
[idx
].freq
/5 - 1000;
1835 if (tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
1836 mode
->channels
[idx
].flag
|=
1837 HOSTAPD_CHAN_DISABLED
;
1838 if (tb_freq
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
])
1839 mode
->channels
[idx
].flag
|=
1840 HOSTAPD_CHAN_PASSIVE_SCAN
;
1841 if (tb_freq
[NL80211_FREQUENCY_ATTR_NO_IBSS
])
1842 mode
->channels
[idx
].flag
|=
1843 HOSTAPD_CHAN_NO_IBSS
;
1844 if (tb_freq
[NL80211_FREQUENCY_ATTR_RADAR
])
1845 mode
->channels
[idx
].flag
|=
1848 if (tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] &&
1849 !tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
1850 mode
->channels
[idx
].max_tx_power
=
1851 nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]) / 100;
1856 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
1857 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
1858 nla_len(nl_rate
), rate_policy
);
1859 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
1864 mode
->rates
= calloc(mode
->num_rates
, sizeof(struct hostapd_rate_data
));
1870 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
1871 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
1872 nla_len(nl_rate
), rate_policy
);
1873 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
1875 mode
->rates
[idx
].rate
= nla_get_u32(tb_rate
[NL80211_BITRATE_ATTR_RATE
]);
1877 /* crude heuristic */
1878 if (mode
->mode
== HOSTAPD_MODE_IEEE80211B
&&
1879 mode
->rates
[idx
].rate
> 200)
1880 mode
->mode
= HOSTAPD_MODE_IEEE80211G
;
1882 if (tb_rate
[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
])
1883 mode
->rates
[idx
].flags
|= HOSTAPD_RATE_PREAMBLE2
;
1892 static struct hostapd_hw_modes
*
1893 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes
*modes
, u16
*num_modes
)
1896 struct hostapd_hw_modes
*mode11g
= NULL
, *nmodes
, *mode
;
1897 int i
, mode11g_idx
= -1;
1899 /* If only 802.11g mode is included, use it to construct matching
1900 * 802.11b mode data. */
1902 for (m
= 0; m
< *num_modes
; m
++) {
1903 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211B
)
1904 return modes
; /* 802.11b already included */
1905 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211G
)
1909 if (mode11g_idx
< 0)
1910 return modes
; /* 2.4 GHz band not supported at all */
1912 nmodes
= os_realloc(modes
, (*num_modes
+ 1) * sizeof(*nmodes
));
1914 return modes
; /* Could not add 802.11b mode */
1916 mode
= &nmodes
[*num_modes
];
1917 os_memset(mode
, 0, sizeof(*mode
));
1921 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
1923 mode11g
= &modes
[mode11g_idx
];
1924 mode
->num_channels
= mode11g
->num_channels
;
1925 mode
->channels
= os_malloc(mode11g
->num_channels
*
1926 sizeof(struct hostapd_channel_data
));
1927 if (mode
->channels
== NULL
) {
1929 return modes
; /* Could not add 802.11b mode */
1931 os_memcpy(mode
->channels
, mode11g
->channels
,
1932 mode11g
->num_channels
* sizeof(struct hostapd_channel_data
));
1934 mode
->num_rates
= 0;
1935 mode
->rates
= os_malloc(4 * sizeof(struct hostapd_rate_data
));
1936 if (mode
->rates
== NULL
) {
1937 os_free(mode
->channels
);
1939 return modes
; /* Could not add 802.11b mode */
1942 for (i
= 0; i
< mode11g
->num_rates
; i
++) {
1943 if (mode11g
->rates
[i
].rate
> 110 ||
1944 mode11g
->rates
[i
].flags
&
1945 (HOSTAPD_RATE_ERP
| HOSTAPD_RATE_OFDM
))
1947 mode
->rates
[mode
->num_rates
] = mode11g
->rates
[i
];
1949 if (mode
->num_rates
== 4)
1953 if (mode
->num_rates
== 0) {
1954 os_free(mode
->channels
);
1955 os_free(mode
->rates
);
1957 return modes
; /* No 802.11b rates */
1960 wpa_printf(MSG_DEBUG
, "nl80211: Added 802.11b mode based on 802.11g "
1967 static struct hostapd_hw_modes
*
1968 wpa_driver_nl80211_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
1970 struct wpa_driver_nl80211_data
*drv
= priv
;
1972 struct phy_info_arg result
= {
1973 .num_modes
= num_modes
,
1980 msg
= nlmsg_alloc();
1984 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1985 0, NL80211_CMD_GET_WIPHY
, 0);
1987 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1989 if (send_and_recv_msgs(drv
, msg
, phy_info_handler
, &result
) == 0)
1990 return wpa_driver_nl80211_add_11b(result
.modes
, num_modes
);
1996 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data
*drv
,
1997 const void *data
, size_t len
,
2001 0x00, 0x00, /* radiotap version */
2002 0x0e, 0x00, /* radiotap length */
2003 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2004 IEEE80211_RADIOTAP_F_FRAG
, /* F_FRAG (fragment if required) */
2006 0x00, 0x00, /* RX and TX flags to indicate that */
2007 0x00, 0x00, /* this is the injected frame directly */
2009 struct iovec iov
[2] = {
2011 .iov_base
= &rtap_hdr
,
2012 .iov_len
= sizeof(rtap_hdr
),
2015 .iov_base
= (void *) data
,
2019 struct msghdr msg
= {
2024 .msg_control
= NULL
,
2025 .msg_controllen
= 0,
2030 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
2032 return sendmsg(drv
->monitor_sock
, &msg
, 0);
2036 static int wpa_driver_nl80211_send_mlme(void *priv
, const u8
*data
,
2039 struct wpa_driver_nl80211_data
*drv
= priv
;
2040 struct ieee80211_mgmt
*mgmt
;
2041 int do_not_encrypt
= 0;
2044 mgmt
= (struct ieee80211_mgmt
*) data
;
2045 fc
= le_to_host16(mgmt
->frame_control
);
2047 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
2048 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
2050 * Only one of the authentication frame types is encrypted.
2051 * In order for static WEP encryption to work properly (i.e.,
2052 * to not encrypt the frame), we need to tell mac80211 about
2053 * the frames that must not be encrypted.
2055 u16 auth_alg
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
2056 u16 auth_trans
= le_to_host16(mgmt
->u
.auth
.auth_transaction
);
2057 if (auth_alg
== WLAN_AUTH_OPEN
||
2058 (auth_alg
== WLAN_AUTH_SHARED_KEY
&& auth_trans
!= 3))
2062 return wpa_driver_nl80211_send_frame(drv
, data
, data_len
,
2067 static int wpa_driver_nl80211_set_beacon_iface(int ifindex
, void *priv
,
2068 const u8
*head
, size_t head_len
,
2069 const u8
*tail
, size_t tail_len
,
2072 struct wpa_driver_nl80211_data
*drv
= priv
;
2074 u8 cmd
= NL80211_CMD_NEW_BEACON
;
2078 struct i802_bss
*bss
;
2080 bss
= get_bss(drv
, ifindex
);
2083 beacon_set
= bss
->beacon_set
;
2085 beacon_set
= drv
->beacon_set
;
2086 #endif /* HOSTAPD */
2088 msg
= nlmsg_alloc();
2092 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon (beacon_set=%d)",
2095 cmd
= NL80211_CMD_SET_BEACON
;
2097 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2099 NLA_PUT(msg
, NL80211_ATTR_BEACON_HEAD
, head_len
, head
);
2100 NLA_PUT(msg
, NL80211_ATTR_BEACON_TAIL
, tail_len
, tail
);
2101 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
2102 if (!drv
->beacon_int
)
2103 drv
->beacon_int
= 100;
2104 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, drv
->beacon_int
);
2105 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, dtim_period
);
2107 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2109 wpa_printf(MSG_DEBUG
, "nl80211: Beacon set failed: %d (%s)",
2110 ret
, strerror(-ret
));
2113 bss
->beacon_set
= 1;
2115 drv
->beacon_set
= 1;
2116 #endif /* HOSTAPD */
2124 static int wpa_driver_nl80211_set_beacon_int(void *priv
, int value
)
2126 struct wpa_driver_nl80211_data
*drv
= priv
;
2129 drv
->beacon_int
= value
;
2132 if (!drv
->bss
.beacon_set
)
2135 if (!drv
->beacon_set
)
2137 #endif /* HOSTAPD */
2139 msg
= nlmsg_alloc();
2143 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon interval %d", value
);
2144 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2145 0, NL80211_CMD_SET_BEACON
, 0);
2146 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2148 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, value
);
2150 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2156 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data
*drv
,
2157 int freq
, int ht_enabled
,
2158 int sec_channel_offset
)
2163 msg
= nlmsg_alloc();
2167 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2168 NL80211_CMD_SET_WIPHY
, 0);
2170 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2171 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
2173 switch (sec_channel_offset
) {
2175 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2176 NL80211_CHAN_HT40MINUS
);
2179 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2180 NL80211_CHAN_HT40PLUS
);
2183 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2189 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2192 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set channel (freq=%d): "
2193 "%d (%s)", freq
, ret
, strerror(-ret
));
2198 #endif /* CONFIG_AP || HOSTAPD */
2203 static int wpa_driver_nl80211_set_beacon(void *priv
,
2204 const u8
*head
, size_t head_len
,
2205 const u8
*tail
, size_t tail_len
,
2208 struct wpa_driver_nl80211_data
*drv
= priv
;
2209 return wpa_driver_nl80211_set_beacon_iface(drv
->ifindex
, priv
,
2215 #endif /* CONFIG_AP */
2217 #if defined(CONFIG_AP) || defined(HOSTAPD)
2219 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
2225 /* stop listening for EAPOL on this interface */
2226 del_ifidx(drv
, ifidx
);
2227 #endif /* HOSTAPD */
2229 msg
= nlmsg_alloc();
2231 goto nla_put_failure
;
2233 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2234 0, NL80211_CMD_DEL_INTERFACE
, 0);
2235 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifidx
);
2237 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
2240 wpa_printf(MSG_ERROR
, "Failed to remove interface (ifidx=%d).\n",
2245 static int nl80211_create_iface(struct wpa_driver_nl80211_data
*drv
,
2246 const char *ifname
, enum nl80211_iftype iftype
,
2249 struct nl_msg
*msg
, *flags
= NULL
;
2255 #endif /* HOSTAPD */
2257 msg
= nlmsg_alloc();
2261 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2262 0, NL80211_CMD_NEW_INTERFACE
, 0);
2263 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2264 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, ifname
);
2265 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, iftype
);
2267 if (iftype
== NL80211_IFTYPE_MONITOR
) {
2270 flags
= nlmsg_alloc();
2272 goto nla_put_failure
;
2274 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
2276 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
2281 goto nla_put_failure
;
2284 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2287 wpa_printf(MSG_ERROR
, "Failed to create interface %s.",
2292 ifidx
= if_nametoindex(ifname
);
2298 /* start listening for EAPOL on this interface */
2299 add_ifidx(drv
, ifidx
);
2303 case NL80211_IFTYPE_AP
:
2304 os_strlcpy(ifreq
.ifr_name
, ifname
, IFNAMSIZ
);
2305 memcpy(ifreq
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
2306 ifreq
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
2308 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifreq
)) {
2309 nl80211_remove_iface(drv
, ifidx
);
2313 case NL80211_IFTYPE_WDS
:
2314 memset(&iwr
, 0, sizeof(iwr
));
2315 os_strlcpy(iwr
.ifr_name
, ifname
, IFNAMSIZ
);
2316 iwr
.u
.addr
.sa_family
= ARPHRD_ETHER
;
2317 memcpy(iwr
.u
.addr
.sa_data
, addr
, ETH_ALEN
);
2318 if (ioctl(drv
->ioctl_sock
, SIOCSIWAP
, &iwr
))
2326 #endif /* HOSTAPD */
2331 #endif /* CONFIG_AP || HOSTAPD */
2335 void ap_tx_status(void *ctx
, const u8
*addr
,
2336 const u8
*buf
, size_t len
, int ack
);
2337 void ap_rx_from_unknown_sta(void *ctx
, const u8
*addr
);
2338 void ap_mgmt_rx(void *ctx
, u8
*buf
, size_t len
, u16 stype
,
2339 struct hostapd_frame_info
*fi
);
2340 void ap_mgmt_tx_cb(void *ctx
, u8
*buf
, size_t len
, u16 stype
, int ok
);
2342 #endif /* CONFIG_AP */
2344 #if defined(CONFIG_AP) || defined(HOSTAPD)
2346 static void handle_tx_callback(void *ctx
, u8
*buf
, size_t len
, int ok
)
2348 struct ieee80211_hdr
*hdr
;
2349 u16 fc
, type
, stype
;
2351 hdr
= (struct ieee80211_hdr
*) buf
;
2352 fc
= le_to_host16(hdr
->frame_control
);
2354 type
= WLAN_FC_GET_TYPE(fc
);
2355 stype
= WLAN_FC_GET_STYPE(fc
);
2358 case WLAN_FC_TYPE_MGMT
:
2359 wpa_printf(MSG_DEBUG
, "MGMT (TX callback) %s",
2360 ok
? "ACK" : "fail");
2362 hostapd_mgmt_tx_cb(ctx
, buf
, len
, stype
, ok
);
2364 ap_mgmt_tx_cb(ctx
, buf
, len
, stype
, ok
);
2365 #endif /* HOSTAPD */
2367 case WLAN_FC_TYPE_CTRL
:
2368 wpa_printf(MSG_DEBUG
, "CTRL (TX callback) %s",
2369 ok
? "ACK" : "fail");
2371 case WLAN_FC_TYPE_DATA
:
2373 hostapd_tx_status(ctx
, hdr
->addr1
, buf
, len
, ok
);
2375 ap_tx_status(ctx
, hdr
->addr1
, buf
, len
, ok
);
2376 #endif /* HOSTAPD */
2379 wpa_printf(MSG_DEBUG
, "unknown TX callback frame type %d",
2386 static void handle_frame(struct wpa_driver_nl80211_data
*drv
,
2387 u8
*buf
, size_t len
,
2388 struct hostapd_frame_info
*hfi
,
2389 enum ieee80211_msg_type msg_type
)
2391 struct ieee80211_hdr
*hdr
;
2392 u16 fc
, type
, stype
;
2393 size_t data_len
= len
;
2395 void *ctx
= drv
->ctx
;
2397 struct hostapd_iface
*iface
= drv
->hapd
->iface
;
2398 struct hostapd_data
*hapd
= NULL
;
2399 int broadcast_bssid
= 0;
2401 #endif /* HOSTAPD */
2404 * PS-Poll frames are 16 bytes. All other frames are
2405 * 24 bytes or longer.
2410 hdr
= (struct ieee80211_hdr
*) buf
;
2411 fc
= le_to_host16(hdr
->frame_control
);
2413 type
= WLAN_FC_GET_TYPE(fc
);
2414 stype
= WLAN_FC_GET_STYPE(fc
);
2417 case WLAN_FC_TYPE_DATA
:
2420 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
2424 case WLAN_FC_FROMDS
:
2432 case WLAN_FC_TYPE_CTRL
:
2433 /* discard non-ps-poll frames */
2434 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
2438 case WLAN_FC_TYPE_MGMT
:
2447 /* find interface frame belongs to */
2448 for (i
= 0; i
< iface
->num_bss
; i
++) {
2449 if (memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0) {
2450 hapd
= iface
->bss
[i
];
2456 hapd
= iface
->bss
[0];
2458 if (bssid
[0] != 0xff || bssid
[1] != 0xff ||
2459 bssid
[2] != 0xff || bssid
[3] != 0xff ||
2460 bssid
[4] != 0xff || bssid
[5] != 0xff) {
2462 * Unknown BSSID - drop frame if this is not from
2463 * passive scanning or a beacon (at least ProbeReq
2464 * frames to other APs may be allowed through RX
2465 * filtering in the wlan hw/driver)
2467 if ((type
!= WLAN_FC_TYPE_MGMT
||
2468 stype
!= WLAN_FC_STYPE_BEACON
))
2471 broadcast_bssid
= 1;
2474 #endif /* HOSTAPD */
2477 case ieee80211_msg_normal
:
2478 /* continue processing */
2480 case ieee80211_msg_tx_callback_ack
:
2481 handle_tx_callback(ctx
, buf
, data_len
, 1);
2483 case ieee80211_msg_tx_callback_fail
:
2484 handle_tx_callback(ctx
, buf
, data_len
, 0);
2489 case WLAN_FC_TYPE_MGMT
:
2490 if (stype
!= WLAN_FC_STYPE_BEACON
&&
2491 stype
!= WLAN_FC_STYPE_PROBE_REQ
)
2492 wpa_printf(MSG_MSGDUMP
, "MGMT");
2494 if (broadcast_bssid
) {
2495 for (i
= 0; i
< iface
->num_bss
; i
++)
2496 hostapd_mgmt_rx(iface
->bss
[i
], buf
, data_len
,
2499 hostapd_mgmt_rx(hapd
, buf
, data_len
, stype
, hfi
);
2501 ap_mgmt_rx(drv
->ctx
, buf
, data_len
, stype
, hfi
);
2502 #endif /* HOSTAPD */
2504 case WLAN_FC_TYPE_CTRL
:
2505 /* can only get here with PS-Poll frames */
2506 wpa_printf(MSG_DEBUG
, "CTRL");
2508 hostapd_rx_from_unknown_sta(drv
->hapd
, hdr
->addr2
);
2510 ap_rx_from_unknown_sta(drv
->ctx
, hdr
->addr2
);
2511 #endif /* HOSTAPD */
2513 case WLAN_FC_TYPE_DATA
:
2515 hostapd_rx_from_unknown_sta(drv
->hapd
, hdr
->addr2
);
2517 ap_rx_from_unknown_sta(drv
->ctx
, hdr
->addr2
);
2518 #endif /* HOSTAPD */
2524 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
2526 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
2528 unsigned char buf
[3000];
2529 struct ieee80211_radiotap_iterator iter
;
2531 struct hostapd_frame_info hfi
;
2532 int injected
= 0, failed
= 0, msg_type
, rxflags
= 0;
2534 len
= recv(sock
, buf
, sizeof(buf
), 0);
2540 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
2541 printf("received invalid radiotap frame\n");
2545 memset(&hfi
, 0, sizeof(hfi
));
2548 ret
= ieee80211_radiotap_iterator_next(&iter
);
2552 printf("received invalid radiotap frame (%d)\n", ret
);
2555 switch (iter
.this_arg_index
) {
2556 case IEEE80211_RADIOTAP_FLAGS
:
2557 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
2560 case IEEE80211_RADIOTAP_RX_FLAGS
:
2563 case IEEE80211_RADIOTAP_TX_FLAGS
:
2565 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
2566 IEEE80211_RADIOTAP_F_TX_FAIL
;
2568 case IEEE80211_RADIOTAP_DATA_RETRIES
:
2570 case IEEE80211_RADIOTAP_CHANNEL
:
2571 /* TODO convert from freq/flags to channel number
2576 case IEEE80211_RADIOTAP_RATE
:
2577 hfi
.datarate
= *iter
.this_arg
* 5;
2579 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
2580 hfi
.ssi_signal
= *iter
.this_arg
;
2585 if (rxflags
&& injected
)
2589 msg_type
= ieee80211_msg_normal
;
2591 msg_type
= ieee80211_msg_tx_callback_fail
;
2593 msg_type
= ieee80211_msg_tx_callback_ack
;
2595 handle_frame(drv
, buf
+ iter
.max_length
,
2596 len
- iter
.max_length
, &hfi
, msg_type
);
2601 * we post-process the filter code later and rewrite
2602 * this to the offset to the last instruction
2607 static struct sock_filter msock_filter_insns
[] = {
2609 * do a little-endian load of the radiotap length field
2611 /* load lower byte into A */
2612 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 2),
2613 /* put it into X (== index register) */
2614 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2615 /* load upper byte into A */
2616 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 3),
2617 /* left-shift it by 8 */
2618 BPF_STMT(BPF_ALU
| BPF_LSH
| BPF_K
, 8),
2620 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
2621 /* put result into X */
2622 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2625 * Allow management frames through, this also gives us those
2626 * management frames that we sent ourselves with status
2628 /* load the lower byte of the IEEE 802.11 frame control field */
2629 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2630 /* mask off frame type and version */
2631 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0xF),
2632 /* accept frame if it's both 0, fall through otherwise */
2633 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0, PASS
, 0),
2636 * TODO: add a bit to radiotap RX flags that indicates
2637 * that the sending station is not associated, then
2638 * add a filter here that filters on our DA and that flag
2639 * to allow us to deauth frames to that bad station.
2641 * Not a regression -- we didn't do it before either.
2646 * drop non-data frames, WDS frames
2648 /* load the lower byte of the frame control field */
2649 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2650 /* mask off QoS bit */
2651 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x0c),
2652 /* drop non-data frames */
2653 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 8, 0, FAIL
),
2654 /* load the upper byte of the frame control field */
2655 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2656 /* mask off toDS/fromDS */
2657 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x03),
2658 /* drop WDS frames */
2659 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 3, FAIL
, 0),
2663 * add header length to index
2665 /* load the lower byte of the frame control field */
2666 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2667 /* mask off QoS bit */
2668 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x80),
2669 /* right shift it by 6 to give 0 or 2 */
2670 BPF_STMT(BPF_ALU
| BPF_RSH
| BPF_K
, 6),
2671 /* add data frame header length */
2672 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_K
, 24),
2673 /* add index, was start of 802.11 header */
2674 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_X
, 0),
2675 /* move to index, now start of LL header */
2676 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2679 * Accept empty data frames, we use those for
2682 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
2683 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
2686 * Accept EAPOL frames
2688 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 0),
2689 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0xAAAA0300, 0, FAIL
),
2690 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 4),
2691 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0x0000888E, PASS
, FAIL
),
2693 /* keep these last two statements or change the code below */
2694 /* return 0 == "DROP" */
2695 BPF_STMT(BPF_RET
| BPF_K
, 0),
2696 /* return ~0 == "keep all" */
2697 BPF_STMT(BPF_RET
| BPF_K
, ~0),
2700 static struct sock_fprog msock_filter
= {
2701 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
2702 .filter
= msock_filter_insns
,
2706 static int add_monitor_filter(int s
)
2710 /* rewrite all PASS/FAIL jump offsets */
2711 for (idx
= 0; idx
< msock_filter
.len
; idx
++) {
2712 struct sock_filter
*insn
= &msock_filter_insns
[idx
];
2714 if (BPF_CLASS(insn
->code
) == BPF_JMP
) {
2715 if (insn
->code
== (BPF_JMP
|BPF_JA
)) {
2716 if (insn
->k
== PASS
)
2717 insn
->k
= msock_filter
.len
- idx
- 2;
2718 else if (insn
->k
== FAIL
)
2719 insn
->k
= msock_filter
.len
- idx
- 3;
2722 if (insn
->jt
== PASS
)
2723 insn
->jt
= msock_filter
.len
- idx
- 2;
2724 else if (insn
->jt
== FAIL
)
2725 insn
->jt
= msock_filter
.len
- idx
- 3;
2727 if (insn
->jf
== PASS
)
2728 insn
->jf
= msock_filter
.len
- idx
- 2;
2729 else if (insn
->jf
== FAIL
)
2730 insn
->jf
= msock_filter
.len
- idx
- 3;
2734 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
2735 &msock_filter
, sizeof(msock_filter
))) {
2736 perror("SO_ATTACH_FILTER");
2745 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data
*drv
)
2748 struct sockaddr_ll ll
;
2752 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->ifname
);
2753 buf
[IFNAMSIZ
- 1] = '\0';
2755 drv
->monitor_ifidx
=
2756 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
, NULL
);
2758 if (drv
->monitor_ifidx
< 0)
2761 if (hostapd_set_iface_flags(drv
, buf
, 1))
2764 memset(&ll
, 0, sizeof(ll
));
2765 ll
.sll_family
= AF_PACKET
;
2766 ll
.sll_ifindex
= drv
->monitor_ifidx
;
2767 drv
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
2768 if (drv
->monitor_sock
< 0) {
2769 perror("socket[PF_PACKET,SOCK_RAW]");
2773 if (add_monitor_filter(drv
->monitor_sock
)) {
2774 wpa_printf(MSG_INFO
, "Failed to set socket filter for monitor "
2775 "interface; do filtering in user space");
2776 /* This works, but will cost in performance. */
2779 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
2780 perror("monitor socket bind");
2784 optlen
= sizeof(optval
);
2787 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
2788 perror("Failed to set socket priority");
2792 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
2794 printf("Could not register monitor read socket\n");
2800 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2804 #endif /* CONFIG_AP || HOSTAPD */
2808 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data
*drv
,
2809 struct wpa_driver_associate_params
*params
)
2811 if (drv
->monitor_ifidx
< 0 &&
2812 nl80211_create_monitor_interface(drv
))
2815 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) ||
2816 wpa_driver_nl80211_set_freq(drv
, params
->freq
, 0, 0)) {
2817 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
2818 drv
->monitor_ifidx
= -1;
2822 /* TODO: setup monitor interface (and add code somewhere to remove this
2823 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
2827 #endif /* CONFIG_AP */
2830 static int wpa_driver_nl80211_associate(
2831 void *priv
, struct wpa_driver_associate_params
*params
)
2833 struct wpa_driver_nl80211_data
*drv
= priv
;
2838 if (params
->mode
== 2)
2839 return wpa_driver_nl80211_ap(drv
, params
);
2840 #endif /* CONFIG_AP */
2842 wpa_driver_nl80211_set_auth_param(drv
, IW_AUTH_DROP_UNENCRYPTED
,
2843 params
->drop_unencrypted
);
2845 drv
->associated
= 0;
2847 msg
= nlmsg_alloc();
2851 wpa_printf(MSG_DEBUG
, "nl80211: Associate (ifindex=%d)",
2853 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2854 NL80211_CMD_ASSOCIATE
, 0);
2856 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2857 if (params
->bssid
) {
2858 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
2859 MAC2STR(params
->bssid
));
2860 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
2863 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
2864 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
2867 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
2868 params
->ssid
, params
->ssid_len
);
2869 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
2871 if (params
->ssid_len
> sizeof(drv
->ssid
))
2872 goto nla_put_failure
;
2873 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
2874 drv
->ssid_len
= params
->ssid_len
;
2876 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
2878 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
2881 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2884 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
2885 "(%s)", ret
, strerror(-ret
));
2886 goto nla_put_failure
;
2889 wpa_printf(MSG_DEBUG
, "nl80211: Association request send "
2898 static int nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
,
2899 int ifindex
, int mode
)
2904 msg
= nlmsg_alloc();
2908 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2909 0, NL80211_CMD_SET_INTERFACE
, 0);
2910 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
2911 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, mode
);
2913 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2917 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set interface %d to mode %d:"
2918 " %d (%s)", ifindex
, mode
, ret
, strerror(-ret
));
2923 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
)
2925 struct wpa_driver_nl80211_data
*drv
= priv
;
2931 nlmode
= NL80211_IFTYPE_STATION
;
2934 nlmode
= NL80211_IFTYPE_ADHOC
;
2937 nlmode
= NL80211_IFTYPE_AP
;
2943 if (nl80211_set_mode(drv
, drv
->ifindex
, nlmode
) == 0) {
2944 drv
->nlmode
= nlmode
;
2948 if (nlmode
== drv
->nlmode
)
2949 return 0; /* Already in the requested mode */
2951 /* mac80211 doesn't allow mode changes while the device is up, so
2952 * take the device down, try to set the mode again, and bring the
2955 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 0) == 0) {
2956 /* Try to set the mode again while the interface is down */
2957 ret
= nl80211_set_mode(drv
, drv
->ifindex
, nlmode
);
2958 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1))
2963 drv
->nlmode
= nlmode
;
2969 static int wpa_driver_nl80211_get_capa(void *priv
,
2970 struct wpa_driver_capa
*capa
)
2972 struct wpa_driver_nl80211_data
*drv
= priv
;
2973 if (!drv
->has_capability
)
2975 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
2980 static int wpa_driver_nl80211_set_operstate(void *priv
, int state
)
2982 struct wpa_driver_nl80211_data
*drv
= priv
;
2984 wpa_printf(MSG_DEBUG
, "%s: operstate %d->%d (%s)",
2985 __func__
, drv
->operstate
, state
, state
? "UP" : "DORMANT");
2986 drv
->operstate
= state
;
2987 return wpa_driver_nl80211_send_oper_ifla(
2988 drv
, -1, state
? IF_OPER_UP
: IF_OPER_DORMANT
);
2994 static const u8 rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2996 static int i802_sta_deauth(void *priv
, const u8
*addr
, int reason
);
2997 static int i802_sta_disassoc(void *priv
, const u8
*addr
, int reason
);
3000 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
3003 struct i802_bss
*bss
= &drv
->bss
;
3005 if (ifindex
== bss
->ifindex
)
3009 wpa_printf(MSG_DEBUG
, "nl80211: get_bss(%d) failed", ifindex
);
3014 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3019 wpa_printf(MSG_DEBUG
, "nl80211: Add own interface ifindex %d",
3021 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3022 if (drv
->if_indices
[i
] == 0) {
3023 drv
->if_indices
[i
] = ifidx
;
3028 if (drv
->if_indices
!= drv
->default_if_indices
)
3029 old
= drv
->if_indices
;
3033 drv
->if_indices
= realloc(old
,
3034 sizeof(int) * (drv
->num_if_indices
+ 1));
3035 if (!drv
->if_indices
) {
3037 drv
->if_indices
= drv
->default_if_indices
;
3039 drv
->if_indices
= old
;
3040 wpa_printf(MSG_ERROR
, "Failed to reallocate memory for "
3042 wpa_printf(MSG_ERROR
, "Ignoring EAPOL on interface %d", ifidx
);
3045 drv
->if_indices
[drv
->num_if_indices
] = ifidx
;
3046 drv
->num_if_indices
++;
3050 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3054 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3055 if (drv
->if_indices
[i
] == ifidx
) {
3056 drv
->if_indices
[i
] = 0;
3063 static int have_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3067 for (i
= 0; i
< drv
->num_if_indices
; i
++)
3068 if (drv
->if_indices
[i
] == ifidx
)
3075 static int i802_set_key(const char *iface
, void *priv
, wpa_alg alg
,
3076 const u8
*addr
, int key_idx
, int set_tx
, const u8
*seq
,
3077 size_t seq_len
, const u8
*key
, size_t key_len
)
3079 struct wpa_driver_nl80211_data
*drv
= priv
;
3080 return nl_set_encr(if_nametoindex(iface
), drv
, alg
, addr
, key_idx
,
3081 set_tx
, seq
, seq_len
, key
, key_len
);
3085 static inline int min_int(int a
, int b
)
3093 static int get_key_handler(struct nl_msg
*msg
, void *arg
)
3095 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3096 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3098 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3099 genlmsg_attrlen(gnlh
, 0), NULL
);
3102 * TODO: validate the key index and mac address!
3103 * Otherwise, there's a race condition as soon as
3104 * the kernel starts sending key notifications.
3107 if (tb
[NL80211_ATTR_KEY_SEQ
])
3108 memcpy(arg
, nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
3109 min_int(nla_len(tb
[NL80211_ATTR_KEY_SEQ
]), 6));
3114 static int i802_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
3117 struct wpa_driver_nl80211_data
*drv
= priv
;
3120 msg
= nlmsg_alloc();
3124 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3125 0, NL80211_CMD_GET_KEY
, 0);
3128 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3129 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
3130 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
3134 return send_and_recv_msgs(drv
, msg
, get_key_handler
, seq
);
3140 static int i802_set_rate_sets(void *priv
, int *supp_rates
, int *basic_rates
,
3143 struct wpa_driver_nl80211_data
*drv
= priv
;
3145 u8 rates
[NL80211_MAX_SUPP_RATES
];
3149 msg
= nlmsg_alloc();
3153 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3154 NL80211_CMD_SET_BSS
, 0);
3156 for (i
= 0; i
< NL80211_MAX_SUPP_RATES
&& basic_rates
[i
] >= 0; i
++)
3157 rates
[rates_len
++] = basic_rates
[i
] / 5;
3159 NLA_PUT(msg
, NL80211_ATTR_BSS_BASIC_RATES
, rates_len
, rates
);
3161 /* TODO: multi-BSS support */
3162 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3164 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3170 /* Set kernel driver on given frequency (MHz) */
3171 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
)
3173 struct wpa_driver_nl80211_data
*drv
= priv
;
3174 return wpa_driver_nl80211_set_freq(drv
, freq
->freq
, freq
->ht_enabled
,
3175 freq
->sec_channel_offset
);
3179 static int i802_set_rts(void *priv
, int rts
)
3181 struct wpa_driver_nl80211_data
*drv
= priv
;
3184 memset(&iwr
, 0, sizeof(iwr
));
3185 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3186 iwr
.u
.rts
.value
= rts
;
3187 iwr
.u
.rts
.fixed
= 1;
3189 if (ioctl(drv
->ioctl_sock
, SIOCSIWRTS
, &iwr
) < 0) {
3190 perror("ioctl[SIOCSIWRTS]");
3198 static int i802_set_frag(void *priv
, int frag
)
3200 struct wpa_driver_nl80211_data
*drv
= priv
;
3203 memset(&iwr
, 0, sizeof(iwr
));
3204 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3205 iwr
.u
.frag
.value
= frag
;
3206 iwr
.u
.frag
.fixed
= 1;
3208 if (ioctl(drv
->ioctl_sock
, SIOCSIWFRAG
, &iwr
) < 0) {
3209 perror("ioctl[SIOCSIWFRAG]");
3217 static int i802_set_retry(void *priv
, int short_retry
, int long_retry
)
3219 struct wpa_driver_nl80211_data
*drv
= priv
;
3222 memset(&iwr
, 0, sizeof(iwr
));
3223 os_strlcpy(iwr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3225 iwr
.u
.retry
.value
= short_retry
;
3226 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MIN
;
3227 if (ioctl(drv
->ioctl_sock
, SIOCSIWRETRY
, &iwr
) < 0) {
3228 perror("ioctl[SIOCSIWRETRY(short)]");
3232 iwr
.u
.retry
.value
= long_retry
;
3233 iwr
.u
.retry
.flags
= IW_RETRY_LIMIT
| IW_RETRY_MAX
;
3234 if (ioctl(drv
->ioctl_sock
, SIOCSIWRETRY
, &iwr
) < 0) {
3235 perror("ioctl[SIOCSIWRETRY(long)]");
3243 static int i802_flush(void *priv
)
3245 struct wpa_driver_nl80211_data
*drv
= priv
;
3248 msg
= nlmsg_alloc();
3252 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3253 0, NL80211_CMD_DEL_STATION
, 0);
3256 * XXX: FIX! this needs to flush all VLANs too
3258 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3259 if_nametoindex(drv
->ifname
));
3261 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3267 static int get_sta_handler(struct nl_msg
*msg
, void *arg
)
3269 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3270 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3271 struct hostap_sta_driver_data
*data
= arg
;
3272 struct nlattr
*stats
[NL80211_STA_INFO_MAX
+ 1];
3273 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
3274 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
3275 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
3276 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
3277 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
3278 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
3281 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3282 genlmsg_attrlen(gnlh
, 0), NULL
);
3285 * TODO: validate the interface and mac address!
3286 * Otherwise, there's a race condition as soon as
3287 * the kernel starts sending station notifications.
3290 if (!tb
[NL80211_ATTR_STA_INFO
]) {
3291 wpa_printf(MSG_DEBUG
, "sta stats missing!");
3294 if (nla_parse_nested(stats
, NL80211_STA_INFO_MAX
,
3295 tb
[NL80211_ATTR_STA_INFO
],
3297 wpa_printf(MSG_DEBUG
, "failed to parse nested attributes!");
3301 if (stats
[NL80211_STA_INFO_INACTIVE_TIME
])
3302 data
->inactive_msec
=
3303 nla_get_u32(stats
[NL80211_STA_INFO_INACTIVE_TIME
]);
3304 if (stats
[NL80211_STA_INFO_RX_BYTES
])
3305 data
->rx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_RX_BYTES
]);
3306 if (stats
[NL80211_STA_INFO_TX_BYTES
])
3307 data
->tx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_TX_BYTES
]);
3308 if (stats
[NL80211_STA_INFO_RX_PACKETS
])
3310 nla_get_u32(stats
[NL80211_STA_INFO_RX_PACKETS
]);
3311 if (stats
[NL80211_STA_INFO_TX_PACKETS
])
3313 nla_get_u32(stats
[NL80211_STA_INFO_TX_PACKETS
]);
3318 static int i802_read_sta_data(void *priv
, struct hostap_sta_driver_data
*data
,
3321 struct wpa_driver_nl80211_data
*drv
= priv
;
3324 os_memset(data
, 0, sizeof(*data
));
3325 msg
= nlmsg_alloc();
3329 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3330 0, NL80211_CMD_GET_STATION
, 0);
3332 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3333 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3335 return send_and_recv_msgs(drv
, msg
, get_sta_handler
, data
);
3341 static int i802_send_eapol(void *priv
, const u8
*addr
, const u8
*data
,
3342 size_t data_len
, int encrypt
, const u8
*own_addr
)
3344 struct wpa_driver_nl80211_data
*drv
= priv
;
3345 struct ieee80211_hdr
*hdr
;
3350 int qos
= sta
->flags
& WLAN_STA_WME
;
3355 len
= sizeof(*hdr
) + (qos
? 2 : 0) + sizeof(rfc1042_header
) + 2 +
3357 hdr
= os_zalloc(len
);
3359 printf("malloc() failed for i802_send_data(len=%lu)\n",
3360 (unsigned long) len
);
3364 hdr
->frame_control
=
3365 IEEE80211_FC(WLAN_FC_TYPE_DATA
, WLAN_FC_STYPE_DATA
);
3366 hdr
->frame_control
|= host_to_le16(WLAN_FC_FROMDS
);
3368 hdr
->frame_control
|= host_to_le16(WLAN_FC_ISWEP
);
3369 #if 0 /* To be enabled if qos determination is added above */
3371 hdr
->frame_control
|=
3372 host_to_le16(WLAN_FC_STYPE_QOS_DATA
<< 4);
3376 memcpy(hdr
->IEEE80211_DA_FROMDS
, addr
, ETH_ALEN
);
3377 memcpy(hdr
->IEEE80211_BSSID_FROMDS
, own_addr
, ETH_ALEN
);
3378 memcpy(hdr
->IEEE80211_SA_FROMDS
, own_addr
, ETH_ALEN
);
3379 pos
= (u8
*) (hdr
+ 1);
3381 #if 0 /* To be enabled if qos determination is added above */
3383 /* add an empty QoS header if needed */
3390 memcpy(pos
, rfc1042_header
, sizeof(rfc1042_header
));
3391 pos
+= sizeof(rfc1042_header
);
3392 WPA_PUT_BE16(pos
, ETH_P_PAE
);
3394 memcpy(pos
, data
, data_len
);
3396 res
= wpa_driver_nl80211_send_frame(drv
, (u8
*) hdr
, len
, encrypt
);
3398 wpa_printf(MSG_ERROR
, "i802_send_eapol - packet len: %lu - "
3400 (unsigned long) len
, errno
, strerror(errno
));
3408 static int i802_sta_add(const char *ifname
, void *priv
,
3409 struct hostapd_sta_add_params
*params
)
3411 struct wpa_driver_nl80211_data
*drv
= priv
;
3415 msg
= nlmsg_alloc();
3419 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3420 0, NL80211_CMD_NEW_STATION
, 0);
3422 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3423 if_nametoindex(drv
->ifname
));
3424 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->addr
);
3425 NLA_PUT_U16(msg
, NL80211_ATTR_STA_AID
, params
->aid
);
3426 NLA_PUT(msg
, NL80211_ATTR_STA_SUPPORTED_RATES
, params
->supp_rates_len
,
3427 params
->supp_rates
);
3428 NLA_PUT_U16(msg
, NL80211_ATTR_STA_LISTEN_INTERVAL
,
3429 params
->listen_interval
);
3431 #ifdef CONFIG_IEEE80211N
3432 if (params
->ht_capabilities
) {
3433 NLA_PUT(msg
, NL80211_ATTR_HT_CAPABILITY
,
3434 params
->ht_capabilities
->length
,
3435 ¶ms
->ht_capabilities
->data
);
3437 #endif /* CONFIG_IEEE80211N */
3439 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3441 wpa_printf(MSG_DEBUG
, "nl80211: NL80211_CMD_NEW_STATION "
3442 "result: %d (%s)", ret
, strerror(-ret
));
3450 static int i802_sta_remove(void *priv
, const u8
*addr
)
3452 struct wpa_driver_nl80211_data
*drv
= priv
;
3456 msg
= nlmsg_alloc();
3460 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3461 0, NL80211_CMD_DEL_STATION
, 0);
3463 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3464 if_nametoindex(drv
->ifname
));
3465 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3467 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3476 static int i802_sta_set_flags(void *priv
, const u8
*addr
,
3477 int total_flags
, int flags_or
, int flags_and
)
3479 struct wpa_driver_nl80211_data
*drv
= priv
;
3480 struct nl_msg
*msg
, *flags
= NULL
;
3482 msg
= nlmsg_alloc();
3486 flags
= nlmsg_alloc();
3492 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3493 0, NL80211_CMD_SET_STATION
, 0);
3495 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3496 if_nametoindex(drv
->ifname
));
3497 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3499 if (total_flags
& WLAN_STA_AUTHORIZED
)
3500 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_AUTHORIZED
);
3502 if (total_flags
& WLAN_STA_WMM
)
3503 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_WME
);
3505 if (total_flags
& WLAN_STA_SHORT_PREAMBLE
)
3506 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_SHORT_PREAMBLE
);
3508 if (total_flags
& WLAN_STA_MFP
)
3509 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_MFP
);
3511 if (nla_put_nested(msg
, NL80211_ATTR_STA_FLAGS
, flags
))
3512 goto nla_put_failure
;
3516 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3523 static int i802_set_tx_queue_params(void *priv
, int queue
, int aifs
,
3524 int cw_min
, int cw_max
, int burst_time
)
3526 struct wpa_driver_nl80211_data
*drv
= priv
;
3528 struct nlattr
*txq
, *params
;
3530 msg
= nlmsg_alloc();
3534 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3535 0, NL80211_CMD_SET_WIPHY
, 0);
3537 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3539 txq
= nla_nest_start(msg
, NL80211_ATTR_WIPHY_TXQ_PARAMS
);
3541 goto nla_put_failure
;
3543 /* We are only sending parameters for a single TXQ at a time */
3544 params
= nla_nest_start(msg
, 1);
3546 goto nla_put_failure
;
3548 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_QUEUE
, queue
);
3549 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
3550 * 32 usec, so need to convert the value here. */
3551 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_TXOP
, (burst_time
* 100 + 16) / 32);
3552 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMIN
, cw_min
);
3553 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMAX
, cw_max
);
3554 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_AIFS
, aifs
);
3556 nla_nest_end(msg
, params
);
3558 nla_nest_end(msg
, txq
);
3560 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
3567 static int i802_bss_add(void *priv
, const char *ifname
, const u8
*bssid
)
3569 struct wpa_driver_nl80211_data
*drv
= priv
;
3571 struct i802_bss
*bss
;
3573 bss
= os_zalloc(sizeof(*bss
));
3576 bss
->ifindex
= if_nametoindex(ifname
);
3578 ifidx
= nl80211_create_iface(priv
, ifname
, NL80211_IFTYPE_AP
, bssid
);
3583 if (hostapd_set_iface_flags(priv
, ifname
, 1)) {
3584 nl80211_remove_iface(priv
, ifidx
);
3588 bss
->next
= drv
->bss
.next
;
3589 drv
->bss
.next
= bss
;
3594 static int i802_bss_remove(void *priv
, const char *ifname
)
3596 struct wpa_driver_nl80211_data
*drv
= priv
;
3597 struct i802_bss
*bss
, *prev
;
3598 int ifindex
= if_nametoindex(ifname
);
3599 nl80211_remove_iface(priv
, ifindex
);
3601 bss
= drv
->bss
.next
;
3603 if (ifindex
== bss
->ifindex
) {
3604 prev
->next
= bss
->next
;
3615 static int i802_set_beacon(const char *iface
, void *priv
,
3616 const u8
*head
, size_t head_len
,
3617 const u8
*tail
, size_t tail_len
, int dtim_period
)
3619 return wpa_driver_nl80211_set_beacon_iface(if_nametoindex(iface
), priv
,
3626 static int i802_del_beacon(struct wpa_driver_nl80211_data
*drv
)
3630 msg
= nlmsg_alloc();
3634 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3635 0, NL80211_CMD_DEL_BEACON
, 0);
3636 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3638 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3644 static int i802_set_bss(void *priv
, int cts
, int preamble
, int slot
)
3646 struct wpa_driver_nl80211_data
*drv
= priv
;
3649 msg
= nlmsg_alloc();
3653 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3654 NL80211_CMD_SET_BSS
, 0);
3657 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_CTS_PROT
, cts
);
3659 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_PREAMBLE
, preamble
);
3661 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_SLOT_TIME
, slot
);
3663 /* TODO: multi-BSS support */
3664 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3666 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3672 static int i802_set_cts_protect(void *priv
, int value
)
3674 return i802_set_bss(priv
, value
, -1, -1);
3678 static int i802_set_preamble(void *priv
, int value
)
3680 return i802_set_bss(priv
, -1, value
, -1);
3684 static int i802_set_short_slot_time(void *priv
, int value
)
3686 return i802_set_bss(priv
, -1, -1, value
);
3690 static enum nl80211_iftype
i802_if_type(enum hostapd_driver_if_type type
)
3693 case HOSTAPD_IF_VLAN
:
3694 return NL80211_IFTYPE_AP_VLAN
;
3695 case HOSTAPD_IF_WDS
:
3696 return NL80211_IFTYPE_WDS
;
3702 static int i802_if_add(const char *iface
, void *priv
,
3703 enum hostapd_driver_if_type type
, char *ifname
,
3706 if (nl80211_create_iface(priv
, ifname
, i802_if_type(type
), addr
) < 0)
3712 static int i802_if_update(void *priv
, enum hostapd_driver_if_type type
,
3713 char *ifname
, const u8
*addr
)
3715 /* unused at the moment */
3720 static int i802_if_remove(void *priv
, enum hostapd_driver_if_type type
,
3721 const char *ifname
, const u8
*addr
)
3723 nl80211_remove_iface(priv
, if_nametoindex(ifname
));
3728 static int i802_set_sta_vlan(void *priv
, const u8
*addr
,
3729 const char *ifname
, int vlan_id
)
3731 struct wpa_driver_nl80211_data
*drv
= priv
;
3734 msg
= nlmsg_alloc();
3738 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3739 0, NL80211_CMD_SET_STATION
, 0);
3741 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3742 if_nametoindex(drv
->ifname
));
3743 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3744 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3745 if_nametoindex(ifname
));
3747 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3753 static void handle_eapol(int sock
, void *eloop_ctx
, void *sock_ctx
)
3755 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
3756 struct sockaddr_ll lladdr
;
3757 unsigned char buf
[3000];
3759 socklen_t fromlen
= sizeof(lladdr
);
3761 len
= recvfrom(sock
, buf
, sizeof(buf
), 0,
3762 (struct sockaddr
*)&lladdr
, &fromlen
);
3768 if (have_ifidx(drv
, lladdr
.sll_ifindex
)) {
3769 struct hostapd_data
*hapd
;
3770 hapd
= hostapd_sta_get_bss(drv
->hapd
, lladdr
.sll_addr
);
3773 hostapd_eapol_receive(hapd
, lladdr
.sll_addr
, buf
, len
);
3778 static int i802_get_inact_sec(void *priv
, const u8
*addr
)
3780 struct hostap_sta_driver_data data
;
3783 data
.inactive_msec
= (unsigned long) -1;
3784 ret
= i802_read_sta_data(priv
, &data
, addr
);
3785 if (ret
|| data
.inactive_msec
== (unsigned long) -1)
3787 return data
.inactive_msec
/ 1000;
3791 static int i802_sta_clear_stats(void *priv
, const u8
*addr
)
3800 static int i802_sta_deauth(void *priv
, const u8
*addr
, int reason
)
3802 struct wpa_driver_nl80211_data
*drv
= priv
;
3803 struct ieee80211_mgmt mgmt
;
3805 memset(&mgmt
, 0, sizeof(mgmt
));
3806 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
3807 WLAN_FC_STYPE_DEAUTH
);
3808 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
3809 memcpy(mgmt
.sa
, drv
->hapd
->own_addr
, ETH_ALEN
);
3810 memcpy(mgmt
.bssid
, drv
->hapd
->own_addr
, ETH_ALEN
);
3811 mgmt
.u
.deauth
.reason_code
= host_to_le16(reason
);
3812 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
3814 sizeof(mgmt
.u
.deauth
));
3818 static int i802_sta_disassoc(void *priv
, const u8
*addr
, int reason
)
3820 struct wpa_driver_nl80211_data
*drv
= priv
;
3821 struct ieee80211_mgmt mgmt
;
3823 memset(&mgmt
, 0, sizeof(mgmt
));
3824 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
3825 WLAN_FC_STYPE_DISASSOC
);
3826 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
3827 memcpy(mgmt
.sa
, drv
->hapd
->own_addr
, ETH_ALEN
);
3828 memcpy(mgmt
.bssid
, drv
->hapd
->own_addr
, ETH_ALEN
);
3829 mgmt
.u
.disassoc
.reason_code
= host_to_le16(reason
);
3830 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
3832 sizeof(mgmt
.u
.disassoc
));
3836 static void *i802_init(struct hostapd_data
*hapd
,
3837 struct wpa_init_params
*params
)
3839 struct wpa_driver_nl80211_data
*drv
;
3843 drv
= os_zalloc(sizeof(struct wpa_driver_nl80211_data
));
3845 printf("Could not allocate memory for i802 driver data\n");
3851 memcpy(drv
->ifname
, params
->ifname
, sizeof(drv
->ifname
));
3852 drv
->ifindex
= if_nametoindex(drv
->ifname
);
3853 drv
->bss
.ifindex
= drv
->ifindex
;
3855 drv
->num_if_indices
= sizeof(drv
->default_if_indices
) / sizeof(int);
3856 drv
->if_indices
= drv
->default_if_indices
;
3857 for (i
= 0; i
< params
->num_bridge
; i
++) {
3858 if (params
->bridge
[i
])
3859 add_ifidx(drv
, if_nametoindex(params
->bridge
[i
]));
3862 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
3863 if (drv
->ioctl_sock
< 0) {
3864 perror("socket[PF_INET,SOCK_DGRAM]");
3868 /* start listening for EAPOL on the default AP interface */
3869 add_ifidx(drv
, drv
->ifindex
);
3871 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 0))
3874 if (params
->bssid
) {
3875 os_strlcpy(ifr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
3876 os_memcpy(ifr
.ifr_hwaddr
.sa_data
, params
->bssid
, ETH_ALEN
);
3877 ifr
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
3879 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifr
)) {
3880 perror("ioctl(SIOCSIFHWADDR)");
3885 if (wpa_driver_nl80211_init_nl(drv
, drv
->hapd
))
3888 /* Initialise a monitor interface */
3889 if (nl80211_create_monitor_interface(drv
))
3892 if (nl80211_set_mode(drv
, drv
->ifindex
, NL80211_IFTYPE_AP
)) {
3893 wpa_printf(MSG_ERROR
, "nl80211: Failed to set interface %s "
3894 "into AP mode", drv
->ifname
);
3898 if (hostapd_set_iface_flags(drv
, drv
->ifname
, 1))
3901 drv
->eapol_sock
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_PAE
));
3902 if (drv
->eapol_sock
< 0) {
3903 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
3907 if (eloop_register_read_sock(drv
->eapol_sock
, handle_eapol
, drv
, NULL
))
3909 printf("Could not register read socket for eapol\n");
3913 os_memset(&ifr
, 0, sizeof(ifr
));
3914 os_strlcpy(ifr
.ifr_name
, drv
->ifname
, sizeof(ifr
.ifr_name
));
3915 if (ioctl(drv
->ioctl_sock
, SIOCGIFHWADDR
, &ifr
) != 0) {
3916 perror("ioctl(SIOCGIFHWADDR)");
3920 if (ifr
.ifr_hwaddr
.sa_family
!= ARPHRD_ETHER
) {
3921 printf("Invalid HW-addr family 0x%04x\n",
3922 ifr
.ifr_hwaddr
.sa_family
);
3925 os_memcpy(drv
->hapd
->own_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
3930 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
3937 static void i802_deinit(void *priv
)
3939 struct wpa_driver_nl80211_data
*drv
= priv
;
3940 struct i802_bss
*bss
, *prev
;
3942 if (drv
->last_freq_ht
) {
3943 /* Clear HT flags from the driver */
3944 struct hostapd_freq_params freq
;
3945 os_memset(&freq
, 0, sizeof(freq
));
3946 freq
.freq
= drv
->last_freq
;
3947 i802_set_freq(priv
, &freq
);
3950 i802_del_beacon(drv
);
3952 /* remove monitor interface */
3953 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
3955 (void) hostapd_set_iface_flags(drv
, drv
->ifname
, 0);
3957 if (drv
->monitor_sock
>= 0) {
3958 eloop_unregister_read_sock(drv
->monitor_sock
);
3959 close(drv
->monitor_sock
);
3961 if (drv
->ioctl_sock
>= 0)
3962 close(drv
->ioctl_sock
);
3963 if (drv
->eapol_sock
>= 0) {
3964 eloop_unregister_read_sock(drv
->eapol_sock
);
3965 close(drv
->eapol_sock
);
3968 eloop_unregister_read_sock(nl_socket_get_fd(drv
->nl_handle
));
3969 genl_family_put(drv
->nl80211
);
3970 nl_cache_free(drv
->nl_cache
);
3971 nl_handle_destroy(drv
->nl_handle
);
3972 nl_cb_put(drv
->nl_cb
);
3974 if (drv
->if_indices
!= drv
->default_if_indices
)
3975 free(drv
->if_indices
);
3977 bss
= drv
->bss
.next
;
3987 #endif /* HOSTAPD */
3990 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
3992 .desc
= "Linux nl80211/cfg80211",
3993 .get_bssid
= wpa_driver_nl80211_get_bssid
,
3994 .get_ssid
= wpa_driver_nl80211_get_ssid
,
3995 .set_key
= wpa_driver_nl80211_set_key
,
3996 .scan2
= wpa_driver_nl80211_scan
,
3997 .get_scan_results2
= wpa_driver_nl80211_get_scan_results
,
3998 .deauthenticate
= wpa_driver_nl80211_deauthenticate
,
3999 .disassociate
= wpa_driver_nl80211_disassociate
,
4000 .authenticate
= wpa_driver_nl80211_authenticate
,
4001 .associate
= wpa_driver_nl80211_associate
,
4002 .init
= wpa_driver_nl80211_init
,
4003 .deinit
= wpa_driver_nl80211_deinit
,
4004 .get_capa
= wpa_driver_nl80211_get_capa
,
4005 .set_operstate
= wpa_driver_nl80211_set_operstate
,
4006 .set_country
= wpa_driver_nl80211_set_country
,
4007 .set_mode
= wpa_driver_nl80211_set_mode
,
4009 .set_beacon
= wpa_driver_nl80211_set_beacon
,
4010 #endif /* CONFIG_AP */
4011 #if defined(CONFIG_AP) || defined(HOSTAPD)
4012 .send_mlme
= wpa_driver_nl80211_send_mlme
,
4013 .set_beacon_int
= wpa_driver_nl80211_set_beacon_int
,
4014 .get_hw_feature_data
= wpa_driver_nl80211_get_hw_feature_data
,
4015 #endif /* CONFIG_AP || HOSTAPD */
4017 .hapd_init
= i802_init
,
4018 .hapd_deinit
= i802_deinit
,
4019 .hapd_set_key
= i802_set_key
,
4020 .get_seqnum
= i802_get_seqnum
,
4021 .flush
= i802_flush
,
4022 .read_sta_data
= i802_read_sta_data
,
4023 .hapd_send_eapol
= i802_send_eapol
,
4024 .sta_set_flags
= i802_sta_set_flags
,
4025 .sta_deauth
= i802_sta_deauth
,
4026 .sta_disassoc
= i802_sta_disassoc
,
4027 .sta_remove
= i802_sta_remove
,
4028 .sta_add
= i802_sta_add
,
4029 .get_inact_sec
= i802_get_inact_sec
,
4030 .sta_clear_stats
= i802_sta_clear_stats
,
4031 .set_freq
= i802_set_freq
,
4032 .set_rts
= i802_set_rts
,
4033 .set_frag
= i802_set_frag
,
4034 .set_retry
= i802_set_retry
,
4035 .set_rate_sets
= i802_set_rate_sets
,
4036 .hapd_set_beacon
= i802_set_beacon
,
4037 .set_cts_protect
= i802_set_cts_protect
,
4038 .set_preamble
= i802_set_preamble
,
4039 .set_short_slot_time
= i802_set_short_slot_time
,
4040 .set_tx_queue_params
= i802_set_tx_queue_params
,
4041 .bss_add
= i802_bss_add
,
4042 .bss_remove
= i802_bss_remove
,
4043 .if_add
= i802_if_add
,
4044 .if_update
= i802_if_update
,
4045 .if_remove
= i802_if_remove
,
4046 .set_sta_vlan
= i802_set_sta_vlan
,
4047 #endif /* HOSTAPD */