2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009, Atheros Communications
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Alternatively, this software may be distributed under the terms of BSD
16 * See README and COPYING for more details.
20 #include <sys/ioctl.h>
21 #include <net/if_arp.h>
23 #include <netlink/genl/genl.h>
24 #include <netlink/genl/family.h>
25 #include <netlink/genl/ctrl.h>
26 #include <netpacket/packet.h>
27 #include <linux/filter.h>
28 #include "nl80211_copy.h"
32 #include "common/ieee802_11_defs.h"
34 #include "linux_ioctl.h"
36 #include "radiotap_iter.h"
40 /* libnl 2.0 compatibility code */
41 #define nl_handle nl_sock
42 #define nl_handle_alloc_cb nl_socket_alloc_cb
43 #define nl_handle_destroy nl_socket_free
44 #endif /* CONFIG_LIBNL20 */
48 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
51 #define IFF_DORMANT 0x20000 /* driver signals dormant */
54 #ifndef IF_OPER_DORMANT
55 #define IF_OPER_DORMANT 5
62 struct i802_bss
*next
;
64 unsigned int beacon_set
:1;
67 struct wpa_driver_nl80211_data
{
69 struct netlink_data
*netlink
;
70 int ioctl_sock
; /* socket for ioctl() use */
71 char ifname
[IFNAMSIZ
+ 1];
74 struct wpa_driver_capa capa
;
79 int scan_complete_events
;
81 struct nl_handle
*nl_handle
;
82 struct nl_handle
*nl_handle_event
;
83 struct nl_cache
*nl_cache
;
84 struct nl_cache
*nl_cache_event
;
86 struct genl_family
*nl80211
;
88 u8 auth_bssid
[ETH_ALEN
];
94 int ap_scan_as_station
;
95 unsigned int assoc_freq
;
101 unsigned int beacon_set
:1;
102 unsigned int pending_remain_on_chan
:1;
104 u64 remain_on_chan_cookie
;
107 int eapol_sock
; /* socket for EAPOL frames */
109 int default_if_indices
[16];
121 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
,
123 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
);
125 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
);
126 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
127 const u8
*addr
, int cmd
, u16 reason_code
);
128 static void nl80211_remove_monitor_interface(
129 struct wpa_driver_nl80211_data
*drv
);
132 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
133 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
);
134 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
136 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
);
137 static int wpa_driver_nl80211_if_remove(void *priv
,
138 enum wpa_driver_if_type type
,
142 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx
,
147 static int ack_handler(struct nl_msg
*msg
, void *arg
)
154 static int finish_handler(struct nl_msg
*msg
, void *arg
)
161 static int error_handler(struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
170 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
176 static int send_and_recv_msgs(struct wpa_driver_nl80211_data
*drv
,
178 int (*valid_handler
)(struct nl_msg
*, void *),
184 cb
= nl_cb_clone(drv
->nl_cb
);
188 err
= nl_send_auto_complete(drv
->nl_handle
, msg
);
194 nl_cb_err(cb
, NL_CB_CUSTOM
, error_handler
, &err
);
195 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, finish_handler
, &err
);
196 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_handler
, &err
);
199 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
200 valid_handler
, valid_data
);
203 nl_recvmsgs(drv
->nl_handle
, cb
);
217 static int family_handler(struct nl_msg
*msg
, void *arg
)
219 struct family_data
*res
= arg
;
220 struct nlattr
*tb
[CTRL_ATTR_MAX
+ 1];
221 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
222 struct nlattr
*mcgrp
;
225 nla_parse(tb
, CTRL_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
226 genlmsg_attrlen(gnlh
, 0), NULL
);
227 if (!tb
[CTRL_ATTR_MCAST_GROUPS
])
230 nla_for_each_nested(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
231 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
232 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, nla_data(mcgrp
),
233 nla_len(mcgrp
), NULL
);
234 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] ||
235 !tb2
[CTRL_ATTR_MCAST_GRP_ID
] ||
236 os_strncmp(nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]),
238 nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
])) != 0)
240 res
->id
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
248 static int nl_get_multicast_id(struct wpa_driver_nl80211_data
*drv
,
249 const char *family
, const char *group
)
253 struct family_data res
= { group
, -ENOENT
};
258 genlmsg_put(msg
, 0, 0, genl_ctrl_resolve(drv
->nl_handle
, "nlctrl"),
259 0, 0, CTRL_CMD_GETFAMILY
, 0);
260 NLA_PUT_STRING(msg
, CTRL_ATTR_FAMILY_NAME
, family
);
262 ret
= send_and_recv_msgs(drv
, msg
, family_handler
, &res
);
273 static int get_ifhwaddr(struct wpa_driver_nl80211_data
*drv
,
274 const char *ifname
, u8
*addr
)
278 os_memset(&ifr
, 0, sizeof(ifr
));
279 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
280 if (ioctl(drv
->ioctl_sock
, SIOCGIFHWADDR
, &ifr
)) {
281 wpa_printf(MSG_ERROR
, "%s: ioctl(SIOCGIFHWADDR): %d (%s)",
282 ifname
, errno
, strerror(errno
));
286 if (ifr
.ifr_hwaddr
.sa_family
!= ARPHRD_ETHER
) {
287 wpa_printf(MSG_ERROR
, "%s: Invalid HW-addr family 0x%04x",
288 ifname
, ifr
.ifr_hwaddr
.sa_family
);
291 os_memcpy(addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
297 static int set_ifhwaddr(struct wpa_driver_nl80211_data
*drv
,
298 const char *ifname
, const u8
*addr
)
302 os_memset(&ifr
, 0, sizeof(ifr
));
303 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
304 os_memcpy(ifr
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
305 ifr
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
307 if (ioctl(drv
->ioctl_sock
, SIOCSIFHWADDR
, &ifr
)) {
308 wpa_printf(MSG_DEBUG
, "%s: ioctl(SIOCSIFHWADDR): %d (%s)",
309 ifname
, errno
, strerror(errno
));
317 static int wpa_driver_nl80211_get_bssid(void *priv
, u8
*bssid
)
319 struct wpa_driver_nl80211_data
*drv
= priv
;
320 if (!drv
->associated
)
322 os_memcpy(bssid
, drv
->bssid
, ETH_ALEN
);
327 static int wpa_driver_nl80211_get_ssid(void *priv
, u8
*ssid
)
329 struct wpa_driver_nl80211_data
*drv
= priv
;
330 if (!drv
->associated
)
332 os_memcpy(ssid
, drv
->ssid
, drv
->ssid_len
);
333 return drv
->ssid_len
;
337 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data
*drv
,
338 char *buf
, size_t len
, int del
)
340 union wpa_event_data event
;
342 os_memset(&event
, 0, sizeof(event
));
343 if (len
> sizeof(event
.interface_status
.ifname
))
344 len
= sizeof(event
.interface_status
.ifname
) - 1;
345 os_memcpy(event
.interface_status
.ifname
, buf
, len
);
346 event
.interface_status
.ievent
= del
? EVENT_INTERFACE_REMOVED
:
347 EVENT_INTERFACE_ADDED
;
349 wpa_printf(MSG_DEBUG
, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
351 event
.interface_status
.ifname
,
352 del
? "removed" : "added");
354 if (os_strcmp(drv
->ifname
, event
.interface_status
.ifname
) == 0) {
361 wpa_supplicant_event(drv
->ctx
, EVENT_INTERFACE_STATUS
, &event
);
365 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data
*drv
,
368 int attrlen
, rta_len
;
372 attr
= (struct rtattr
*) buf
;
374 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
375 while (RTA_OK(attr
, attrlen
)) {
376 if (attr
->rta_type
== IFLA_IFNAME
) {
377 if (os_strcmp(((char *) attr
) + rta_len
, drv
->ifname
)
383 attr
= RTA_NEXT(attr
, attrlen
);
390 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data
*drv
,
391 int ifindex
, u8
*buf
, size_t len
)
393 if (drv
->ifindex
== ifindex
)
396 if (drv
->if_removed
&& wpa_driver_nl80211_own_ifname(drv
, buf
, len
)) {
397 drv
->ifindex
= if_nametoindex(drv
->ifname
);
398 wpa_printf(MSG_DEBUG
, "nl80211: Update ifindex for a removed "
400 wpa_driver_nl80211_finish_drv_init(drv
);
408 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx
,
409 struct ifinfomsg
*ifi
,
412 struct wpa_driver_nl80211_data
*drv
= ctx
;
413 int attrlen
, rta_len
;
416 if (!wpa_driver_nl80211_own_ifindex(drv
, ifi
->ifi_index
, buf
, len
)) {
417 wpa_printf(MSG_DEBUG
, "Ignore event for foreign ifindex %d",
422 wpa_printf(MSG_DEBUG
, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
424 drv
->operstate
, ifi
->ifi_flags
,
425 (ifi
->ifi_flags
& IFF_UP
) ? "[UP]" : "",
426 (ifi
->ifi_flags
& IFF_RUNNING
) ? "[RUNNING]" : "",
427 (ifi
->ifi_flags
& IFF_LOWER_UP
) ? "[LOWER_UP]" : "",
428 (ifi
->ifi_flags
& IFF_DORMANT
) ? "[DORMANT]" : "");
430 * Some drivers send the association event before the operup event--in
431 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
432 * fails. This will hit us when wpa_supplicant does not need to do
433 * IEEE 802.1X authentication
435 if (drv
->operstate
== 1 &&
436 (ifi
->ifi_flags
& (IFF_LOWER_UP
| IFF_DORMANT
)) == IFF_LOWER_UP
&&
437 !(ifi
->ifi_flags
& IFF_RUNNING
))
438 netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
,
442 attr
= (struct rtattr
*) buf
;
443 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
444 while (RTA_OK(attr
, attrlen
)) {
445 if (attr
->rta_type
== IFLA_IFNAME
) {
446 wpa_driver_nl80211_event_link(
448 ((char *) attr
) + rta_len
,
449 attr
->rta_len
- rta_len
, 0);
451 attr
= RTA_NEXT(attr
, attrlen
);
456 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx
,
457 struct ifinfomsg
*ifi
,
460 struct wpa_driver_nl80211_data
*drv
= ctx
;
461 int attrlen
, rta_len
;
465 attr
= (struct rtattr
*) buf
;
467 rta_len
= RTA_ALIGN(sizeof(struct rtattr
));
468 while (RTA_OK(attr
, attrlen
)) {
469 if (attr
->rta_type
== IFLA_IFNAME
) {
470 wpa_driver_nl80211_event_link(
472 ((char *) attr
) + rta_len
,
473 attr
->rta_len
- rta_len
, 1);
475 attr
= RTA_NEXT(attr
, attrlen
);
480 static void mlme_event_auth(struct wpa_driver_nl80211_data
*drv
,
481 const u8
*frame
, size_t len
)
483 const struct ieee80211_mgmt
*mgmt
;
484 union wpa_event_data event
;
486 mgmt
= (const struct ieee80211_mgmt
*) frame
;
487 if (len
< 24 + sizeof(mgmt
->u
.auth
)) {
488 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
493 os_memcpy(drv
->auth_bssid
, mgmt
->sa
, ETH_ALEN
);
494 os_memset(&event
, 0, sizeof(event
));
495 os_memcpy(event
.auth
.peer
, mgmt
->sa
, ETH_ALEN
);
496 event
.auth
.auth_type
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
497 event
.auth
.status_code
= le_to_host16(mgmt
->u
.auth
.status_code
);
498 if (len
> 24 + sizeof(mgmt
->u
.auth
)) {
499 event
.auth
.ies
= mgmt
->u
.auth
.variable
;
500 event
.auth
.ies_len
= len
- 24 - sizeof(mgmt
->u
.auth
);
503 wpa_supplicant_event(drv
->ctx
, EVENT_AUTH
, &event
);
507 static void mlme_event_assoc(struct wpa_driver_nl80211_data
*drv
,
508 const u8
*frame
, size_t len
)
510 const struct ieee80211_mgmt
*mgmt
;
511 union wpa_event_data event
;
514 mgmt
= (const struct ieee80211_mgmt
*) frame
;
515 if (len
< 24 + sizeof(mgmt
->u
.assoc_resp
)) {
516 wpa_printf(MSG_DEBUG
, "nl80211: Too short association event "
521 status
= le_to_host16(mgmt
->u
.assoc_resp
.status_code
);
522 if (status
!= WLAN_STATUS_SUCCESS
) {
523 os_memset(&event
, 0, sizeof(event
));
524 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
525 event
.assoc_reject
.resp_ies
=
526 (u8
*) mgmt
->u
.assoc_resp
.variable
;
527 event
.assoc_reject
.resp_ies_len
=
528 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
530 event
.assoc_reject
.status_code
= status
;
532 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
537 os_memcpy(drv
->bssid
, mgmt
->sa
, ETH_ALEN
);
539 os_memset(&event
, 0, sizeof(event
));
540 if (len
> 24 + sizeof(mgmt
->u
.assoc_resp
)) {
541 event
.assoc_info
.resp_ies
= (u8
*) mgmt
->u
.assoc_resp
.variable
;
542 event
.assoc_info
.resp_ies_len
=
543 len
- 24 - sizeof(mgmt
->u
.assoc_resp
);
546 event
.assoc_info
.freq
= drv
->assoc_freq
;
548 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
552 static void mlme_event_connect(struct wpa_driver_nl80211_data
*drv
,
553 enum nl80211_commands cmd
, struct nlattr
*status
,
554 struct nlattr
*addr
, struct nlattr
*req_ie
,
555 struct nlattr
*resp_ie
)
557 union wpa_event_data event
;
559 if (drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
) {
561 * Avoid reporting two association events that would confuse
564 wpa_printf(MSG_DEBUG
, "nl80211: Ignore connect event (cmd=%d) "
565 "when using userspace SME", cmd
);
569 os_memset(&event
, 0, sizeof(event
));
570 if (cmd
== NL80211_CMD_CONNECT
&&
571 nla_get_u16(status
) != WLAN_STATUS_SUCCESS
) {
573 event
.assoc_reject
.resp_ies
= nla_data(resp_ie
);
574 event
.assoc_reject
.resp_ies_len
= nla_len(resp_ie
);
576 event
.assoc_reject
.status_code
= nla_get_u16(status
);
577 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC_REJECT
, &event
);
583 os_memcpy(drv
->bssid
, nla_data(addr
), ETH_ALEN
);
586 event
.assoc_info
.req_ies
= nla_data(req_ie
);
587 event
.assoc_info
.req_ies_len
= nla_len(req_ie
);
590 event
.assoc_info
.resp_ies
= nla_data(resp_ie
);
591 event
.assoc_info
.resp_ies_len
= nla_len(resp_ie
);
594 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, &event
);
598 static void mlme_timeout_event(struct wpa_driver_nl80211_data
*drv
,
599 enum nl80211_commands cmd
, struct nlattr
*addr
)
601 union wpa_event_data event
;
602 enum wpa_event_type ev
;
604 if (nla_len(addr
) != ETH_ALEN
)
607 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d; timeout with " MACSTR
,
608 cmd
, MAC2STR((u8
*) nla_data(addr
)));
610 if (cmd
== NL80211_CMD_AUTHENTICATE
)
611 ev
= EVENT_AUTH_TIMED_OUT
;
612 else if (cmd
== NL80211_CMD_ASSOCIATE
)
613 ev
= EVENT_ASSOC_TIMED_OUT
;
617 os_memset(&event
, 0, sizeof(event
));
618 os_memcpy(event
.timeout_event
.addr
, nla_data(addr
), ETH_ALEN
);
619 wpa_supplicant_event(drv
->ctx
, ev
, &event
);
623 static void mlme_event(struct wpa_driver_nl80211_data
*drv
,
624 enum nl80211_commands cmd
, struct nlattr
*frame
,
625 struct nlattr
*addr
, struct nlattr
*timed_out
)
627 if (timed_out
&& addr
) {
628 mlme_timeout_event(drv
, cmd
, addr
);
633 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d without frame "
638 wpa_printf(MSG_DEBUG
, "nl80211: MLME event %d", cmd
);
639 wpa_hexdump(MSG_MSGDUMP
, "nl80211: MLME event frame",
640 nla_data(frame
), nla_len(frame
));
643 case NL80211_CMD_AUTHENTICATE
:
644 mlme_event_auth(drv
, nla_data(frame
), nla_len(frame
));
646 case NL80211_CMD_ASSOCIATE
:
647 mlme_event_assoc(drv
, nla_data(frame
), nla_len(frame
));
649 case NL80211_CMD_DEAUTHENTICATE
:
651 wpa_supplicant_event(drv
->ctx
, EVENT_DEAUTH
, NULL
);
653 case NL80211_CMD_DISASSOCIATE
:
655 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
663 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data
*drv
,
666 union wpa_event_data data
;
668 wpa_printf(MSG_DEBUG
, "nl80211: MLME event Michael MIC failure");
669 os_memset(&data
, 0, sizeof(data
));
670 if (tb
[NL80211_ATTR_MAC
]) {
671 wpa_hexdump(MSG_DEBUG
, "nl80211: Source MAC address",
672 nla_data(tb
[NL80211_ATTR_MAC
]),
673 nla_len(tb
[NL80211_ATTR_MAC
]));
674 data
.michael_mic_failure
.src
= nla_data(tb
[NL80211_ATTR_MAC
]);
676 if (tb
[NL80211_ATTR_KEY_SEQ
]) {
677 wpa_hexdump(MSG_DEBUG
, "nl80211: TSC",
678 nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
679 nla_len(tb
[NL80211_ATTR_KEY_SEQ
]));
681 if (tb
[NL80211_ATTR_KEY_TYPE
]) {
682 enum nl80211_key_type key_type
=
683 nla_get_u32(tb
[NL80211_ATTR_KEY_TYPE
]);
684 wpa_printf(MSG_DEBUG
, "nl80211: Key Type %d", key_type
);
685 if (key_type
== NL80211_KEYTYPE_PAIRWISE
)
686 data
.michael_mic_failure
.unicast
= 1;
688 data
.michael_mic_failure
.unicast
= 1;
690 if (tb
[NL80211_ATTR_KEY_IDX
]) {
691 u8 key_id
= nla_get_u8(tb
[NL80211_ATTR_KEY_IDX
]);
692 wpa_printf(MSG_DEBUG
, "nl80211: Key Id %d", key_id
);
695 wpa_supplicant_event(drv
->ctx
, EVENT_MICHAEL_MIC_FAILURE
, &data
);
699 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data
*drv
,
702 if (tb
[NL80211_ATTR_MAC
] == NULL
) {
703 wpa_printf(MSG_DEBUG
, "nl80211: No address in IBSS joined "
707 os_memcpy(drv
->bssid
, nla_data(tb
[NL80211_ATTR_MAC
]), ETH_ALEN
);
709 wpa_printf(MSG_DEBUG
, "nl80211: IBSS " MACSTR
" joined",
710 MAC2STR(drv
->bssid
));
712 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, NULL
);
716 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data
*drv
,
717 int cancel_event
, struct nlattr
*tb
[])
719 unsigned int freq
, chan_type
, duration
;
720 union wpa_event_data data
;
723 if (tb
[NL80211_ATTR_WIPHY_FREQ
])
724 freq
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_FREQ
]);
728 if (tb
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
])
729 chan_type
= nla_get_u32(tb
[NL80211_ATTR_WIPHY_CHANNEL_TYPE
]);
733 if (tb
[NL80211_ATTR_DURATION
])
734 duration
= nla_get_u32(tb
[NL80211_ATTR_DURATION
]);
738 if (tb
[NL80211_ATTR_COOKIE
])
739 cookie
= nla_get_u64(tb
[NL80211_ATTR_COOKIE
]);
743 wpa_printf(MSG_DEBUG
, "nl80211: Remain-on-channel event (cancel=%d "
744 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
745 cancel_event
, freq
, chan_type
, duration
,
746 (long long unsigned int) cookie
,
747 cookie
== drv
->remain_on_chan_cookie
? "match" : "unknown");
749 if (cookie
!= drv
->remain_on_chan_cookie
)
750 return; /* not for us */
752 drv
->pending_remain_on_chan
= !cancel_event
;
754 os_memset(&data
, 0, sizeof(data
));
755 data
.remain_on_channel
.freq
= freq
;
756 data
.remain_on_channel
.duration
= duration
;
757 wpa_supplicant_event(drv
->ctx
, cancel_event
?
758 EVENT_CANCEL_REMAIN_ON_CHANNEL
:
759 EVENT_REMAIN_ON_CHANNEL
, &data
);
763 static void send_scan_event(struct wpa_driver_nl80211_data
*drv
, int aborted
,
766 union wpa_event_data event
;
769 struct scan_info
*info
;
770 #define MAX_REPORT_FREQS 50
771 int freqs
[MAX_REPORT_FREQS
];
774 os_memset(&event
, 0, sizeof(event
));
775 info
= &event
.scan_info
;
776 info
->aborted
= aborted
;
778 if (tb
[NL80211_ATTR_SCAN_SSIDS
]) {
779 nla_for_each_nested(nl
, tb
[NL80211_ATTR_SCAN_SSIDS
], rem
) {
780 struct wpa_driver_scan_ssid
*s
=
781 &info
->ssids
[info
->num_ssids
];
782 s
->ssid
= nla_data(nl
);
783 s
->ssid_len
= nla_len(nl
);
785 if (info
->num_ssids
== WPAS_MAX_SCAN_SSIDS
)
789 if (tb
[NL80211_ATTR_SCAN_FREQUENCIES
]) {
790 nla_for_each_nested(nl
, tb
[NL80211_ATTR_SCAN_FREQUENCIES
], rem
)
792 freqs
[num_freqs
] = nla_get_u32(nl
);
794 if (num_freqs
== MAX_REPORT_FREQS
- 1)
798 info
->num_freqs
= num_freqs
;
800 wpa_supplicant_event(drv
->ctx
, EVENT_SCAN_RESULTS
, &event
);
804 static int process_event(struct nl_msg
*msg
, void *arg
)
806 struct wpa_driver_nl80211_data
*drv
= arg
;
807 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
808 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
810 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
811 genlmsg_attrlen(gnlh
, 0), NULL
);
813 if (tb
[NL80211_ATTR_IFINDEX
]) {
814 int ifindex
= nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]);
815 if (ifindex
!= drv
->ifindex
) {
816 wpa_printf(MSG_DEBUG
, "nl80211: Ignored event (cmd=%d)"
817 " for foreign interface (ifindex %d)",
823 if (drv
->ap_scan_as_station
&&
824 (gnlh
->cmd
== NL80211_CMD_NEW_SCAN_RESULTS
||
825 gnlh
->cmd
== NL80211_CMD_SCAN_ABORTED
)) {
826 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
827 drv
->ap_scan_as_station
= 0;
831 case NL80211_CMD_TRIGGER_SCAN
:
832 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger");
834 case NL80211_CMD_NEW_SCAN_RESULTS
:
835 wpa_printf(MSG_DEBUG
, "nl80211: New scan results available");
836 drv
->scan_complete_events
= 1;
837 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
839 send_scan_event(drv
, 0, tb
);
841 case NL80211_CMD_SCAN_ABORTED
:
842 wpa_printf(MSG_DEBUG
, "nl80211: Scan aborted");
844 * Need to indicate that scan results are available in order
845 * not to make wpa_supplicant stop its scanning.
847 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
,
849 send_scan_event(drv
, 1, tb
);
851 case NL80211_CMD_AUTHENTICATE
:
852 case NL80211_CMD_ASSOCIATE
:
853 case NL80211_CMD_DEAUTHENTICATE
:
854 case NL80211_CMD_DISASSOCIATE
:
855 mlme_event(drv
, gnlh
->cmd
, tb
[NL80211_ATTR_FRAME
],
856 tb
[NL80211_ATTR_MAC
], tb
[NL80211_ATTR_TIMED_OUT
]);
858 case NL80211_CMD_CONNECT
:
859 case NL80211_CMD_ROAM
:
860 mlme_event_connect(drv
, gnlh
->cmd
,
861 tb
[NL80211_ATTR_STATUS_CODE
],
862 tb
[NL80211_ATTR_MAC
],
863 tb
[NL80211_ATTR_REQ_IE
],
864 tb
[NL80211_ATTR_RESP_IE
]);
866 case NL80211_CMD_DISCONNECT
:
867 if (drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
) {
869 * Avoid reporting two disassociation events that could
870 * confuse the core code.
872 wpa_printf(MSG_DEBUG
, "nl80211: Ignore disconnect "
873 "event when using userspace SME");
877 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
879 case NL80211_CMD_MICHAEL_MIC_FAILURE
:
880 mlme_event_michael_mic_failure(drv
, tb
);
882 case NL80211_CMD_JOIN_IBSS
:
883 mlme_event_join_ibss(drv
, tb
);
885 case NL80211_CMD_REMAIN_ON_CHANNEL
:
886 mlme_event_remain_on_channel(drv
, 0, tb
);
888 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
:
889 mlme_event_remain_on_channel(drv
, 1, tb
);
892 wpa_printf(MSG_DEBUG
, "nl80211: Ignored unknown event "
893 "(cmd=%d)", gnlh
->cmd
);
901 static void wpa_driver_nl80211_event_receive(int sock
, void *eloop_ctx
,
905 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
907 wpa_printf(MSG_DEBUG
, "nl80211: Event message available");
909 cb
= nl_cb_clone(drv
->nl_cb
);
912 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
913 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, process_event
, drv
);
914 nl_recvmsgs(drv
->nl_handle_event
, cb
);
920 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
921 * @priv: driver_nl80211 private data
922 * @alpha2_arg: country to which to switch to
923 * Returns: 0 on success, -1 on failure
925 * This asks nl80211 to set the regulatory domain for given
926 * country ISO / IEC alpha2.
928 static int wpa_driver_nl80211_set_country(void *priv
, const char *alpha2_arg
)
930 struct wpa_driver_nl80211_data
*drv
= priv
;
938 alpha2
[0] = alpha2_arg
[0];
939 alpha2
[1] = alpha2_arg
[1];
942 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
943 0, NL80211_CMD_REQ_SET_REG
, 0);
945 NLA_PUT_STRING(msg
, NL80211_ATTR_REG_ALPHA2
, alpha2
);
946 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
))
955 struct wiphy_info_data
{
959 int connect_supported
;
963 static int wiphy_info_handler(struct nl_msg
*msg
, void *arg
)
965 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
966 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
967 struct wiphy_info_data
*info
= arg
;
969 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
970 genlmsg_attrlen(gnlh
, 0), NULL
);
972 if (tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
])
973 info
->max_scan_ssids
=
974 nla_get_u8(tb
[NL80211_ATTR_MAX_NUM_SCAN_SSIDS
]);
976 if (tb
[NL80211_ATTR_SUPPORTED_IFTYPES
]) {
977 struct nlattr
*nl_mode
;
979 nla_for_each_nested(nl_mode
,
980 tb
[NL80211_ATTR_SUPPORTED_IFTYPES
], i
) {
981 if (nl_mode
->nla_type
== NL80211_IFTYPE_AP
) {
982 info
->ap_supported
= 1;
988 if (tb
[NL80211_ATTR_SUPPORTED_COMMANDS
]) {
989 struct nlattr
*nl_cmd
;
992 nla_for_each_nested(nl_cmd
,
993 tb
[NL80211_ATTR_SUPPORTED_COMMANDS
], i
) {
994 u32 cmd
= nla_get_u32(nl_cmd
);
995 if (cmd
== NL80211_CMD_AUTHENTICATE
)
996 info
->auth_supported
= 1;
997 else if (cmd
== NL80211_CMD_CONNECT
)
998 info
->connect_supported
= 1;
1006 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data
*drv
,
1007 struct wiphy_info_data
*info
)
1011 os_memset(info
, 0, sizeof(*info
));
1012 msg
= nlmsg_alloc();
1016 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1017 0, NL80211_CMD_GET_WIPHY
, 0);
1019 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1021 if (send_and_recv_msgs(drv
, msg
, wiphy_info_handler
, info
) == 0)
1030 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data
*drv
)
1032 struct wiphy_info_data info
;
1033 if (wpa_driver_nl80211_get_info(drv
, &info
))
1035 drv
->has_capability
= 1;
1036 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
1037 drv
->capa
.key_mgmt
= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
1038 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
1039 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
1040 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
;
1041 drv
->capa
.enc
= WPA_DRIVER_CAPA_ENC_WEP40
|
1042 WPA_DRIVER_CAPA_ENC_WEP104
|
1043 WPA_DRIVER_CAPA_ENC_TKIP
|
1044 WPA_DRIVER_CAPA_ENC_CCMP
;
1045 drv
->capa
.auth
= WPA_DRIVER_AUTH_OPEN
|
1046 WPA_DRIVER_AUTH_SHARED
|
1047 WPA_DRIVER_AUTH_LEAP
;
1049 drv
->capa
.max_scan_ssids
= info
.max_scan_ssids
;
1050 if (info
.ap_supported
)
1051 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_AP
;
1053 if (info
.auth_supported
)
1054 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SME
;
1055 else if (!info
.connect_supported
) {
1056 wpa_printf(MSG_INFO
, "nl80211: Driver does not support "
1057 "authentication/association or connect commands");
1061 drv
->capa
.flags
|= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE
;
1065 #endif /* HOSTAPD */
1068 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data
*drv
,
1073 /* Initialize generic netlink and nl80211 */
1075 drv
->nl_cb
= nl_cb_alloc(NL_CB_DEFAULT
);
1076 if (drv
->nl_cb
== NULL
) {
1077 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1082 drv
->nl_handle
= nl_handle_alloc_cb(drv
->nl_cb
);
1083 if (drv
->nl_handle
== NULL
) {
1084 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1089 drv
->nl_handle_event
= nl_handle_alloc_cb(drv
->nl_cb
);
1090 if (drv
->nl_handle_event
== NULL
) {
1091 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate netlink "
1092 "callbacks (event)");
1096 if (genl_connect(drv
->nl_handle
)) {
1097 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
1102 if (genl_connect(drv
->nl_handle_event
)) {
1103 wpa_printf(MSG_ERROR
, "nl80211: Failed to connect to generic "
1108 #ifdef CONFIG_LIBNL20
1109 if (genl_ctrl_alloc_cache(drv
->nl_handle
, &drv
->nl_cache
) < 0) {
1110 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1114 if (genl_ctrl_alloc_cache(drv
->nl_handle_event
, &drv
->nl_cache_event
) <
1116 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1117 "netlink cache (event)");
1120 #else /* CONFIG_LIBNL20 */
1121 drv
->nl_cache
= genl_ctrl_alloc_cache(drv
->nl_handle
);
1122 if (drv
->nl_cache
== NULL
) {
1123 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1127 drv
->nl_cache_event
= genl_ctrl_alloc_cache(drv
->nl_handle_event
);
1128 if (drv
->nl_cache_event
== NULL
) {
1129 wpa_printf(MSG_ERROR
, "nl80211: Failed to allocate generic "
1130 "netlink cache (event)");
1133 #endif /* CONFIG_LIBNL20 */
1135 drv
->nl80211
= genl_ctrl_search_by_name(drv
->nl_cache
, "nl80211");
1136 if (drv
->nl80211
== NULL
) {
1137 wpa_printf(MSG_ERROR
, "nl80211: 'nl80211' generic netlink not "
1142 ret
= nl_get_multicast_id(drv
, "nl80211", "scan");
1144 ret
= nl_socket_add_membership(drv
->nl_handle_event
, ret
);
1146 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1147 "membership for scan events: %d (%s)",
1148 ret
, strerror(-ret
));
1152 ret
= nl_get_multicast_id(drv
, "nl80211", "mlme");
1154 ret
= nl_socket_add_membership(drv
->nl_handle_event
, ret
);
1156 wpa_printf(MSG_ERROR
, "nl80211: Could not add multicast "
1157 "membership for mlme events: %d (%s)",
1158 ret
, strerror(-ret
));
1162 eloop_register_read_sock(nl_socket_get_fd(drv
->nl_handle_event
),
1163 wpa_driver_nl80211_event_receive
, drv
, ctx
);
1168 nl_cache_free(drv
->nl_cache_event
);
1170 nl_cache_free(drv
->nl_cache
);
1172 nl_handle_destroy(drv
->nl_handle_event
);
1174 nl_handle_destroy(drv
->nl_handle
);
1176 nl_cb_put(drv
->nl_cb
);
1183 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1184 * @ctx: context to be used when calling wpa_supplicant functions,
1185 * e.g., wpa_supplicant_event()
1186 * @ifname: interface name, e.g., wlan0
1187 * Returns: Pointer to private data, %NULL on failure
1189 static void * wpa_driver_nl80211_init(void *ctx
, const char *ifname
)
1191 struct wpa_driver_nl80211_data
*drv
;
1192 struct netlink_config
*cfg
;
1194 drv
= os_zalloc(sizeof(*drv
));
1198 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
1199 drv
->monitor_ifidx
= -1;
1200 drv
->monitor_sock
= -1;
1201 drv
->ioctl_sock
= -1;
1203 if (wpa_driver_nl80211_init_nl(drv
, ctx
)) {
1208 drv
->ioctl_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
1209 if (drv
->ioctl_sock
< 0) {
1210 perror("socket(PF_INET,SOCK_DGRAM)");
1214 cfg
= os_zalloc(sizeof(*cfg
));
1218 cfg
->newlink_cb
= wpa_driver_nl80211_event_rtm_newlink
;
1219 cfg
->dellink_cb
= wpa_driver_nl80211_event_rtm_dellink
;
1220 drv
->netlink
= netlink_init(cfg
);
1221 if (drv
->netlink
== NULL
) {
1225 if (wpa_driver_nl80211_finish_drv_init(drv
))
1231 netlink_deinit(drv
->netlink
);
1232 if (drv
->ioctl_sock
>= 0)
1233 close(drv
->ioctl_sock
);
1235 genl_family_put(drv
->nl80211
);
1236 nl_cache_free(drv
->nl_cache
);
1237 nl_handle_destroy(drv
->nl_handle
);
1238 nl_cb_put(drv
->nl_cb
);
1246 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data
*drv
)
1248 drv
->ifindex
= if_nametoindex(drv
->ifname
);
1251 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
) < 0) {
1252 wpa_printf(MSG_DEBUG
, "nl80211: Could not configure driver to "
1253 "use managed mode");
1256 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 1)) {
1257 wpa_printf(MSG_ERROR
, "Could not set interface '%s' UP",
1262 if (wpa_driver_nl80211_capa(drv
))
1265 netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
,
1266 1, IF_OPER_DORMANT
);
1267 #endif /* HOSTAPD */
1274 static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data
*drv
)
1276 struct i802_bss
*bss
, *prev
;
1277 bss
= drv
->bss
.next
;
1284 #endif /* HOSTAPD */
1287 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data
*drv
)
1291 msg
= nlmsg_alloc();
1295 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1296 0, NL80211_CMD_DEL_BEACON
, 0);
1297 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1299 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1306 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1307 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1309 * Shut down driver interface and processing of driver events. Free
1310 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1312 static void wpa_driver_nl80211_deinit(void *priv
)
1314 struct wpa_driver_nl80211_data
*drv
= priv
;
1316 nl80211_remove_monitor_interface(drv
);
1318 if (drv
->nlmode
== NL80211_IFTYPE_AP
)
1319 wpa_driver_nl80211_del_beacon(drv
);
1322 if (drv
->last_freq_ht
) {
1323 /* Clear HT flags from the driver */
1324 struct hostapd_freq_params freq
;
1325 os_memset(&freq
, 0, sizeof(freq
));
1326 freq
.freq
= drv
->last_freq
;
1327 i802_set_freq(priv
, &freq
);
1330 if (drv
->eapol_sock
>= 0) {
1331 eloop_unregister_read_sock(drv
->eapol_sock
);
1332 close(drv
->eapol_sock
);
1335 if (drv
->if_indices
!= drv
->default_if_indices
)
1336 os_free(drv
->if_indices
);
1338 wpa_driver_nl80211_free_bss(drv
);
1339 #endif /* HOSTAPD */
1341 netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
, 0, IF_OPER_UP
);
1342 netlink_deinit(drv
->netlink
);
1344 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1346 (void) linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 0);
1347 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
);
1349 if (drv
->ioctl_sock
>= 0)
1350 close(drv
->ioctl_sock
);
1352 eloop_unregister_read_sock(nl_socket_get_fd(drv
->nl_handle_event
));
1353 genl_family_put(drv
->nl80211
);
1354 nl_cache_free(drv
->nl_cache
);
1355 nl_cache_free(drv
->nl_cache_event
);
1356 nl_handle_destroy(drv
->nl_handle
);
1357 nl_handle_destroy(drv
->nl_handle_event
);
1358 nl_cb_put(drv
->nl_cb
);
1360 eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout
,
1368 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1369 * @eloop_ctx: Driver private data
1370 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1372 * This function can be used as registered timeout when starting a scan to
1373 * generate a scan completed event if the driver does not report this.
1375 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1377 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
1378 if (drv
->ap_scan_as_station
) {
1379 wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
);
1380 drv
->ap_scan_as_station
= 0;
1382 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1383 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1388 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1389 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
1390 * @params: Scan parameters
1391 * Returns: 0 on success, -1 on failure
1393 static int wpa_driver_nl80211_scan(void *priv
,
1394 struct wpa_driver_scan_params
*params
)
1396 struct wpa_driver_nl80211_data
*drv
= priv
;
1397 int ret
= 0, timeout
;
1398 struct nl_msg
*msg
, *ssids
, *freqs
;
1401 msg
= nlmsg_alloc();
1402 ssids
= nlmsg_alloc();
1403 freqs
= nlmsg_alloc();
1404 if (!msg
|| !ssids
|| !freqs
) {
1411 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
1412 NL80211_CMD_TRIGGER_SCAN
, 0);
1414 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1416 for (i
= 0; i
< params
->num_ssids
; i
++) {
1417 NLA_PUT(ssids
, i
+ 1, params
->ssids
[i
].ssid_len
,
1418 params
->ssids
[i
].ssid
);
1420 if (params
->num_ssids
)
1421 nla_put_nested(msg
, NL80211_ATTR_SCAN_SSIDS
, ssids
);
1423 if (params
->extra_ies
) {
1424 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->extra_ies_len
,
1428 if (params
->freqs
) {
1429 for (i
= 0; params
->freqs
[i
]; i
++)
1430 NLA_PUT_U32(freqs
, i
+ 1, params
->freqs
[i
]);
1431 nla_put_nested(msg
, NL80211_ATTR_SCAN_FREQUENCIES
, freqs
);
1434 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1437 wpa_printf(MSG_DEBUG
, "nl80211: Scan trigger failed: ret=%d "
1438 "(%s)", ret
, strerror(-ret
));
1440 if (drv
->nlmode
== NL80211_IFTYPE_AP
) {
1442 * mac80211 does not allow scan requests in AP mode, so
1443 * try to do this in station mode.
1445 if (wpa_driver_nl80211_set_mode(drv
,
1446 IEEE80211_MODE_INFRA
))
1447 goto nla_put_failure
;
1449 if (wpa_driver_nl80211_scan(drv
, params
)) {
1450 wpa_driver_nl80211_set_mode(drv
,
1452 goto nla_put_failure
;
1455 /* Restore AP mode when processing scan results */
1456 drv
->ap_scan_as_station
= 1;
1459 goto nla_put_failure
;
1461 goto nla_put_failure
;
1462 #endif /* HOSTAPD */
1465 /* Not all drivers generate "scan completed" wireless event, so try to
1466 * read results after a timeout. */
1468 if (drv
->scan_complete_events
) {
1470 * The driver seems to deliver events to notify when scan is
1471 * complete, so use longer timeout to avoid race conditions
1472 * with scanning and following association request.
1476 wpa_printf(MSG_DEBUG
, "Scan requested (ret=%d) - scan timeout %d "
1477 "seconds", ret
, timeout
);
1478 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout
, drv
, drv
->ctx
);
1479 eloop_register_timeout(timeout
, 0, wpa_driver_nl80211_scan_timeout
,
1490 static int bss_info_handler(struct nl_msg
*msg
, void *arg
)
1492 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
1493 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1494 struct nlattr
*bss
[NL80211_BSS_MAX
+ 1];
1495 static struct nla_policy bss_policy
[NL80211_BSS_MAX
+ 1] = {
1496 [NL80211_BSS_BSSID
] = { .type
= NLA_UNSPEC
},
1497 [NL80211_BSS_FREQUENCY
] = { .type
= NLA_U32
},
1498 [NL80211_BSS_TSF
] = { .type
= NLA_U64
},
1499 [NL80211_BSS_BEACON_INTERVAL
] = { .type
= NLA_U16
},
1500 [NL80211_BSS_CAPABILITY
] = { .type
= NLA_U16
},
1501 [NL80211_BSS_INFORMATION_ELEMENTS
] = { .type
= NLA_UNSPEC
},
1502 [NL80211_BSS_SIGNAL_MBM
] = { .type
= NLA_U32
},
1503 [NL80211_BSS_SIGNAL_UNSPEC
] = { .type
= NLA_U8
},
1504 [NL80211_BSS_STATUS
] = { .type
= NLA_U32
},
1505 [NL80211_BSS_SEEN_MS_AGO
] = { .type
= NLA_U32
},
1507 struct wpa_scan_results
*res
= arg
;
1508 struct wpa_scan_res
**tmp
;
1509 struct wpa_scan_res
*r
;
1513 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1514 genlmsg_attrlen(gnlh
, 0), NULL
);
1515 if (!tb
[NL80211_ATTR_BSS
])
1517 if (nla_parse_nested(bss
, NL80211_BSS_MAX
, tb
[NL80211_ATTR_BSS
],
1520 if (bss
[NL80211_BSS_INFORMATION_ELEMENTS
]) {
1521 ie
= nla_data(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1522 ie_len
= nla_len(bss
[NL80211_BSS_INFORMATION_ELEMENTS
]);
1528 r
= os_zalloc(sizeof(*r
) + ie_len
);
1531 if (bss
[NL80211_BSS_BSSID
])
1532 os_memcpy(r
->bssid
, nla_data(bss
[NL80211_BSS_BSSID
]),
1534 if (bss
[NL80211_BSS_FREQUENCY
])
1535 r
->freq
= nla_get_u32(bss
[NL80211_BSS_FREQUENCY
]);
1536 if (bss
[NL80211_BSS_BEACON_INTERVAL
])
1537 r
->beacon_int
= nla_get_u16(bss
[NL80211_BSS_BEACON_INTERVAL
]);
1538 if (bss
[NL80211_BSS_CAPABILITY
])
1539 r
->caps
= nla_get_u16(bss
[NL80211_BSS_CAPABILITY
]);
1540 r
->flags
|= WPA_SCAN_NOISE_INVALID
;
1541 if (bss
[NL80211_BSS_SIGNAL_MBM
]) {
1542 r
->level
= nla_get_u32(bss
[NL80211_BSS_SIGNAL_MBM
]);
1543 r
->level
/= 100; /* mBm to dBm */
1544 r
->flags
|= WPA_SCAN_LEVEL_DBM
| WPA_SCAN_QUAL_INVALID
;
1545 } else if (bss
[NL80211_BSS_SIGNAL_UNSPEC
]) {
1546 r
->level
= nla_get_u8(bss
[NL80211_BSS_SIGNAL_UNSPEC
]);
1547 r
->flags
|= WPA_SCAN_LEVEL_INVALID
;
1549 r
->flags
|= WPA_SCAN_LEVEL_INVALID
| WPA_SCAN_QUAL_INVALID
;
1550 if (bss
[NL80211_BSS_TSF
])
1551 r
->tsf
= nla_get_u64(bss
[NL80211_BSS_TSF
]);
1552 if (bss
[NL80211_BSS_SEEN_MS_AGO
])
1553 r
->age
= nla_get_u32(bss
[NL80211_BSS_SEEN_MS_AGO
]);
1556 os_memcpy(r
+ 1, ie
, ie_len
);
1558 if (bss
[NL80211_BSS_STATUS
]) {
1559 enum nl80211_bss_status status
;
1560 status
= nla_get_u32(bss
[NL80211_BSS_STATUS
]);
1562 case NL80211_BSS_STATUS_AUTHENTICATED
:
1563 r
->flags
|= WPA_SCAN_AUTHENTICATED
;
1565 case NL80211_BSS_STATUS_ASSOCIATED
:
1566 r
->flags
|= WPA_SCAN_ASSOCIATED
;
1573 tmp
= os_realloc(res
->res
,
1574 (res
->num
+ 1) * sizeof(struct wpa_scan_res
*));
1579 tmp
[res
->num
++] = r
;
1586 static void clear_state_mismatch(struct wpa_driver_nl80211_data
*drv
,
1589 if (drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
) {
1590 wpa_printf(MSG_DEBUG
, "nl80211: Clear possible state "
1592 wpa_driver_nl80211_mlme(drv
, addr
,
1593 NL80211_CMD_DEAUTHENTICATE
,
1594 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1599 static void wpa_driver_nl80211_check_bss_status(
1600 struct wpa_driver_nl80211_data
*drv
, struct wpa_scan_results
*res
)
1604 for (i
= 0; i
< res
->num
; i
++) {
1605 struct wpa_scan_res
*r
= res
->res
[i
];
1606 if (r
->flags
& WPA_SCAN_AUTHENTICATED
) {
1607 wpa_printf(MSG_DEBUG
, "nl80211: Scan results "
1608 "indicates BSS status with " MACSTR
1609 " as authenticated",
1611 if (drv
->nlmode
== NL80211_IFTYPE_STATION
&&
1612 os_memcmp(r
->bssid
, drv
->bssid
, ETH_ALEN
) != 0 &&
1613 os_memcmp(r
->bssid
, drv
->auth_bssid
, ETH_ALEN
) !=
1615 wpa_printf(MSG_DEBUG
, "nl80211: Unknown BSSID"
1616 " in local state (auth=" MACSTR
1617 " assoc=" MACSTR
")",
1618 MAC2STR(drv
->auth_bssid
),
1619 MAC2STR(drv
->bssid
));
1623 if (r
->flags
& WPA_SCAN_ASSOCIATED
) {
1624 wpa_printf(MSG_DEBUG
, "nl80211: Scan results "
1625 "indicate BSS status with " MACSTR
1628 if (drv
->nlmode
== NL80211_IFTYPE_STATION
&&
1630 wpa_printf(MSG_DEBUG
, "nl80211: Local state "
1631 "(not associated) does not match "
1633 clear_state_mismatch(drv
, r
->bssid
);
1634 } else if (drv
->nlmode
== NL80211_IFTYPE_STATION
&&
1635 os_memcmp(drv
->bssid
, r
->bssid
, ETH_ALEN
) !=
1637 wpa_printf(MSG_DEBUG
, "nl80211: Local state "
1638 "(associated with " MACSTR
") does "
1639 "not match with BSS state",
1640 MAC2STR(drv
->bssid
));
1641 clear_state_mismatch(drv
, r
->bssid
);
1642 clear_state_mismatch(drv
, drv
->bssid
);
1649 static void wpa_scan_results_free(struct wpa_scan_results
*res
)
1656 for (i
= 0; i
< res
->num
; i
++)
1657 os_free(res
->res
[i
]);
1664 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1665 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1666 * Returns: Scan results on success, -1 on failure
1668 static struct wpa_scan_results
*
1669 wpa_driver_nl80211_get_scan_results(void *priv
)
1671 struct wpa_driver_nl80211_data
*drv
= priv
;
1673 struct wpa_scan_results
*res
;
1676 res
= os_zalloc(sizeof(*res
));
1679 msg
= nlmsg_alloc();
1681 goto nla_put_failure
;
1683 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, NLM_F_DUMP
,
1684 NL80211_CMD_GET_SCAN
, 0);
1685 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1687 ret
= send_and_recv_msgs(drv
, msg
, bss_info_handler
, res
);
1690 wpa_printf(MSG_DEBUG
, "Received scan results (%lu BSSes)",
1691 (unsigned long) res
->num
);
1692 wpa_driver_nl80211_check_bss_status(drv
, res
);
1695 wpa_printf(MSG_DEBUG
, "nl80211: Scan result fetch failed: ret=%d "
1696 "(%s)", ret
, strerror(-ret
));
1699 wpa_scan_results_free(res
);
1704 static int wpa_driver_nl80211_set_key(const char *ifname
, void *priv
,
1705 enum wpa_alg alg
, const u8
*addr
,
1706 int key_idx
, int set_tx
,
1707 const u8
*seq
, size_t seq_len
,
1708 const u8
*key
, size_t key_len
)
1710 struct wpa_driver_nl80211_data
*drv
= priv
;
1711 int ifindex
= if_nametoindex(ifname
);
1715 wpa_printf(MSG_DEBUG
, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1716 "set_tx=%d seq_len=%lu key_len=%lu",
1717 __func__
, ifindex
, alg
, addr
, key_idx
, set_tx
,
1718 (unsigned long) seq_len
, (unsigned long) key_len
);
1720 msg
= nlmsg_alloc();
1724 if (alg
== WPA_ALG_NONE
) {
1725 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1726 0, NL80211_CMD_DEL_KEY
, 0);
1728 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1729 0, NL80211_CMD_NEW_KEY
, 0);
1730 NLA_PUT(msg
, NL80211_ATTR_KEY_DATA
, key_len
, key
);
1734 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1735 WLAN_CIPHER_SUITE_WEP40
);
1737 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1738 WLAN_CIPHER_SUITE_WEP104
);
1741 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1742 WLAN_CIPHER_SUITE_TKIP
);
1745 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1746 WLAN_CIPHER_SUITE_CCMP
);
1749 NLA_PUT_U32(msg
, NL80211_ATTR_KEY_CIPHER
,
1750 WLAN_CIPHER_SUITE_AES_CMAC
);
1753 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1754 "algorithm %d", __func__
, alg
);
1761 NLA_PUT(msg
, NL80211_ATTR_KEY_SEQ
, seq_len
, seq
);
1763 if (addr
&& os_memcmp(addr
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
1765 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1766 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1768 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1769 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1771 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1772 if (ret
== -ENOENT
&& alg
== WPA_ALG_NONE
)
1775 wpa_printf(MSG_DEBUG
, "nl80211: set_key failed; err=%d %s)",
1776 ret
, strerror(-ret
));
1779 * If we failed or don't need to set the default TX key (below),
1782 if (ret
|| !set_tx
|| alg
== WPA_ALG_NONE
)
1784 #ifdef HOSTAPD /* FIX: is this needed? */
1787 #endif /* HOSTAPD */
1789 msg
= nlmsg_alloc();
1793 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
1794 0, NL80211_CMD_SET_KEY
, 0);
1795 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, key_idx
);
1796 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
1797 if (alg
== WPA_ALG_IGTK
)
1798 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT_MGMT
);
1800 NLA_PUT_FLAG(msg
, NL80211_ATTR_KEY_DEFAULT
);
1802 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1806 wpa_printf(MSG_DEBUG
, "nl80211: set_key default failed; "
1807 "err=%d %s)", ret
, strerror(-ret
));
1815 static int nl_add_key(struct nl_msg
*msg
, enum wpa_alg alg
,
1816 int key_idx
, int defkey
,
1817 const u8
*seq
, size_t seq_len
,
1818 const u8
*key
, size_t key_len
)
1820 struct nlattr
*key_attr
= nla_nest_start(msg
, NL80211_ATTR_KEY
);
1824 if (defkey
&& alg
== WPA_ALG_IGTK
)
1825 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT_MGMT
);
1827 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
1829 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, key_idx
);
1834 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1835 WLAN_CIPHER_SUITE_WEP40
);
1837 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1838 WLAN_CIPHER_SUITE_WEP104
);
1841 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, WLAN_CIPHER_SUITE_TKIP
);
1844 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, WLAN_CIPHER_SUITE_CCMP
);
1847 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1848 WLAN_CIPHER_SUITE_AES_CMAC
);
1851 wpa_printf(MSG_ERROR
, "%s: Unsupported encryption "
1852 "algorithm %d", __func__
, alg
);
1857 NLA_PUT(msg
, NL80211_KEY_SEQ
, seq_len
, seq
);
1859 NLA_PUT(msg
, NL80211_KEY_DATA
, key_len
, key
);
1861 nla_nest_end(msg
, key_attr
);
1869 static int nl80211_set_conn_keys(struct wpa_driver_associate_params
*params
,
1873 struct nlattr
*nl_keys
, *nl_key
;
1875 for (i
= 0; i
< 4; i
++) {
1876 if (!params
->wep_key
[i
])
1884 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
1886 nl_keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
1888 goto nla_put_failure
;
1890 for (i
= 0; i
< 4; i
++) {
1891 if (!params
->wep_key
[i
])
1894 nl_key
= nla_nest_start(msg
, i
);
1896 goto nla_put_failure
;
1898 NLA_PUT(msg
, NL80211_KEY_DATA
, params
->wep_key_len
[i
],
1899 params
->wep_key
[i
]);
1900 if (params
->wep_key_len
[i
] == 5)
1901 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1902 WLAN_CIPHER_SUITE_WEP40
);
1904 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
,
1905 WLAN_CIPHER_SUITE_WEP104
);
1907 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, i
);
1909 if (i
== params
->wep_tx_keyidx
)
1910 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
1912 nla_nest_end(msg
, nl_key
);
1914 nla_nest_end(msg
, nl_keys
);
1923 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data
*drv
,
1924 const u8
*addr
, int cmd
, u16 reason_code
)
1929 msg
= nlmsg_alloc();
1933 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0, cmd
, 0);
1935 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
1936 NLA_PUT_U16(msg
, NL80211_ATTR_REASON_CODE
, reason_code
);
1937 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
1939 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
1942 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
1943 "(%s)", ret
, strerror(-ret
));
1944 goto nla_put_failure
;
1954 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data
*drv
,
1955 const u8
*addr
, int reason_code
)
1957 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1958 drv
->associated
= 0;
1959 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DISCONNECT
,
1964 static int wpa_driver_nl80211_deauthenticate(void *priv
, const u8
*addr
,
1967 struct wpa_driver_nl80211_data
*drv
= priv
;
1968 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
))
1969 return wpa_driver_nl80211_disconnect(drv
, addr
, reason_code
);
1970 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1971 drv
->associated
= 0;
1972 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DEAUTHENTICATE
,
1977 static int wpa_driver_nl80211_disassociate(void *priv
, const u8
*addr
,
1980 struct wpa_driver_nl80211_data
*drv
= priv
;
1981 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
))
1982 return wpa_driver_nl80211_disconnect(drv
, addr
, reason_code
);
1983 wpa_printf(MSG_DEBUG
, "%s", __func__
);
1984 drv
->associated
= 0;
1985 return wpa_driver_nl80211_mlme(drv
, addr
, NL80211_CMD_DISASSOCIATE
,
1990 static int wpa_driver_nl80211_authenticate(
1991 void *priv
, struct wpa_driver_auth_params
*params
)
1993 struct wpa_driver_nl80211_data
*drv
= priv
;
1996 enum nl80211_auth_type type
;
1999 drv
->associated
= 0;
2000 os_memset(drv
->auth_bssid
, 0, ETH_ALEN
);
2002 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_INFRA
) < 0)
2006 msg
= nlmsg_alloc();
2010 wpa_printf(MSG_DEBUG
, "nl80211: Authenticate (ifindex=%d)",
2013 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2014 NL80211_CMD_AUTHENTICATE
, 0);
2016 for (i
= 0; i
< 4; i
++) {
2017 if (!params
->wep_key
[i
])
2019 wpa_driver_nl80211_set_key(drv
->ifname
, drv
, WPA_ALG_WEP
, NULL
,
2021 i
== params
->wep_tx_keyidx
, NULL
, 0,
2023 params
->wep_key_len
[i
]);
2024 if (params
->wep_tx_keyidx
!= i
)
2026 if (nl_add_key(msg
, WPA_ALG_WEP
, i
, 1, NULL
, 0,
2027 params
->wep_key
[i
], params
->wep_key_len
[i
])) {
2033 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2034 if (params
->bssid
) {
2035 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
2036 MAC2STR(params
->bssid
));
2037 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
2040 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
2041 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
2044 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
2045 params
->ssid
, params
->ssid_len
);
2046 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
2049 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->ie
, params
->ie_len
);
2051 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->ie_len
, params
->ie
);
2053 * TODO: if multiple auth_alg options enabled, try them one by one if
2054 * the AP rejects authentication due to unknown auth alg
2056 if (params
->auth_alg
& WPA_AUTH_ALG_OPEN
)
2057 type
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
2058 else if (params
->auth_alg
& WPA_AUTH_ALG_SHARED
)
2059 type
= NL80211_AUTHTYPE_SHARED_KEY
;
2060 else if (params
->auth_alg
& WPA_AUTH_ALG_LEAP
)
2061 type
= NL80211_AUTHTYPE_NETWORK_EAP
;
2062 else if (params
->auth_alg
& WPA_AUTH_ALG_FT
)
2063 type
= NL80211_AUTHTYPE_FT
;
2065 goto nla_put_failure
;
2066 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
2067 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
2069 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2072 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
2073 "(%s)", ret
, strerror(-ret
));
2075 if (ret
== -EALREADY
&& count
== 1 && params
->bssid
) {
2077 * mac80211 does not currently accept new
2078 * authentication if we are already authenticated. As a
2079 * workaround, force deauthentication and try again.
2081 wpa_printf(MSG_DEBUG
, "nl80211: Retry authentication "
2082 "after forced deauthentication");
2083 wpa_driver_nl80211_deauthenticate(
2085 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2089 goto nla_put_failure
;
2092 wpa_printf(MSG_DEBUG
, "nl80211: Authentication request send "
2101 struct phy_info_arg
{
2103 struct hostapd_hw_modes
*modes
;
2106 static int phy_info_handler(struct nl_msg
*msg
, void *arg
)
2108 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
2109 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
2110 struct phy_info_arg
*phy_info
= arg
;
2112 struct nlattr
*tb_band
[NL80211_BAND_ATTR_MAX
+ 1];
2114 struct nlattr
*tb_freq
[NL80211_FREQUENCY_ATTR_MAX
+ 1];
2115 static struct nla_policy freq_policy
[NL80211_FREQUENCY_ATTR_MAX
+ 1] = {
2116 [NL80211_FREQUENCY_ATTR_FREQ
] = { .type
= NLA_U32
},
2117 [NL80211_FREQUENCY_ATTR_DISABLED
] = { .type
= NLA_FLAG
},
2118 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
] = { .type
= NLA_FLAG
},
2119 [NL80211_FREQUENCY_ATTR_NO_IBSS
] = { .type
= NLA_FLAG
},
2120 [NL80211_FREQUENCY_ATTR_RADAR
] = { .type
= NLA_FLAG
},
2121 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] = { .type
= NLA_U32
},
2124 struct nlattr
*tb_rate
[NL80211_BITRATE_ATTR_MAX
+ 1];
2125 static struct nla_policy rate_policy
[NL80211_BITRATE_ATTR_MAX
+ 1] = {
2126 [NL80211_BITRATE_ATTR_RATE
] = { .type
= NLA_U32
},
2127 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE
] = { .type
= NLA_FLAG
},
2130 struct nlattr
*nl_band
;
2131 struct nlattr
*nl_freq
;
2132 struct nlattr
*nl_rate
;
2133 int rem_band
, rem_freq
, rem_rate
;
2134 struct hostapd_hw_modes
*mode
;
2135 int idx
, mode_is_set
;
2137 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
2138 genlmsg_attrlen(gnlh
, 0), NULL
);
2140 if (!tb_msg
[NL80211_ATTR_WIPHY_BANDS
])
2143 nla_for_each_nested(nl_band
, tb_msg
[NL80211_ATTR_WIPHY_BANDS
], rem_band
) {
2144 mode
= os_realloc(phy_info
->modes
, (*phy_info
->num_modes
+ 1) * sizeof(*mode
));
2147 phy_info
->modes
= mode
;
2151 mode
= &phy_info
->modes
[*(phy_info
->num_modes
)];
2152 memset(mode
, 0, sizeof(*mode
));
2153 *(phy_info
->num_modes
) += 1;
2155 nla_parse(tb_band
, NL80211_BAND_ATTR_MAX
, nla_data(nl_band
),
2156 nla_len(nl_band
), NULL
);
2158 if (tb_band
[NL80211_BAND_ATTR_HT_CAPA
]) {
2159 mode
->ht_capab
= nla_get_u16(
2160 tb_band
[NL80211_BAND_ATTR_HT_CAPA
]);
2163 if (tb_band
[NL80211_BAND_ATTR_HT_AMPDU_FACTOR
]) {
2164 mode
->a_mpdu_params
|= nla_get_u8(
2165 tb_band
[NL80211_BAND_ATTR_HT_AMPDU_FACTOR
]) &
2169 if (tb_band
[NL80211_BAND_ATTR_HT_AMPDU_DENSITY
]) {
2170 mode
->a_mpdu_params
|= nla_get_u8(
2171 tb_band
[NL80211_BAND_ATTR_HT_AMPDU_DENSITY
]) <<
2175 if (tb_band
[NL80211_BAND_ATTR_HT_MCS_SET
] &&
2176 nla_len(tb_band
[NL80211_BAND_ATTR_HT_MCS_SET
])) {
2178 mcs
= nla_data(tb_band
[NL80211_BAND_ATTR_HT_MCS_SET
]);
2179 os_memcpy(mode
->mcs_set
, mcs
, 16);
2182 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
2183 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
2184 nla_len(nl_freq
), freq_policy
);
2185 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
2187 mode
->num_channels
++;
2190 mode
->channels
= os_zalloc(mode
->num_channels
* sizeof(struct hostapd_channel_data
));
2191 if (!mode
->channels
)
2196 nla_for_each_nested(nl_freq
, tb_band
[NL80211_BAND_ATTR_FREQS
], rem_freq
) {
2197 nla_parse(tb_freq
, NL80211_FREQUENCY_ATTR_MAX
, nla_data(nl_freq
),
2198 nla_len(nl_freq
), freq_policy
);
2199 if (!tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
])
2202 mode
->channels
[idx
].freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
2203 mode
->channels
[idx
].flag
= 0;
2206 /* crude heuristic */
2207 if (mode
->channels
[idx
].freq
< 4000)
2208 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
2210 mode
->mode
= HOSTAPD_MODE_IEEE80211A
;
2214 /* crude heuristic */
2215 if (mode
->channels
[idx
].freq
< 4000)
2216 if (mode
->channels
[idx
].freq
== 2484)
2217 mode
->channels
[idx
].chan
= 14;
2219 mode
->channels
[idx
].chan
= (mode
->channels
[idx
].freq
- 2407) / 5;
2221 mode
->channels
[idx
].chan
= mode
->channels
[idx
].freq
/5 - 1000;
2223 if (tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
2224 mode
->channels
[idx
].flag
|=
2225 HOSTAPD_CHAN_DISABLED
;
2226 if (tb_freq
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
])
2227 mode
->channels
[idx
].flag
|=
2228 HOSTAPD_CHAN_PASSIVE_SCAN
;
2229 if (tb_freq
[NL80211_FREQUENCY_ATTR_NO_IBSS
])
2230 mode
->channels
[idx
].flag
|=
2231 HOSTAPD_CHAN_NO_IBSS
;
2232 if (tb_freq
[NL80211_FREQUENCY_ATTR_RADAR
])
2233 mode
->channels
[idx
].flag
|=
2236 if (tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
] &&
2237 !tb_freq
[NL80211_FREQUENCY_ATTR_DISABLED
])
2238 mode
->channels
[idx
].max_tx_power
=
2239 nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER
]) / 100;
2244 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
2245 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
2246 nla_len(nl_rate
), rate_policy
);
2247 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
2252 mode
->rates
= os_zalloc(mode
->num_rates
* sizeof(int));
2258 nla_for_each_nested(nl_rate
, tb_band
[NL80211_BAND_ATTR_RATES
], rem_rate
) {
2259 nla_parse(tb_rate
, NL80211_BITRATE_ATTR_MAX
, nla_data(nl_rate
),
2260 nla_len(nl_rate
), rate_policy
);
2261 if (!tb_rate
[NL80211_BITRATE_ATTR_RATE
])
2263 mode
->rates
[idx
] = nla_get_u32(tb_rate
[NL80211_BITRATE_ATTR_RATE
]);
2265 /* crude heuristic */
2266 if (mode
->mode
== HOSTAPD_MODE_IEEE80211B
&&
2267 mode
->rates
[idx
] > 200)
2268 mode
->mode
= HOSTAPD_MODE_IEEE80211G
;
2277 static struct hostapd_hw_modes
*
2278 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes
*modes
, u16
*num_modes
)
2281 struct hostapd_hw_modes
*mode11g
= NULL
, *nmodes
, *mode
;
2282 int i
, mode11g_idx
= -1;
2284 /* If only 802.11g mode is included, use it to construct matching
2285 * 802.11b mode data. */
2287 for (m
= 0; m
< *num_modes
; m
++) {
2288 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211B
)
2289 return modes
; /* 802.11b already included */
2290 if (modes
[m
].mode
== HOSTAPD_MODE_IEEE80211G
)
2294 if (mode11g_idx
< 0)
2295 return modes
; /* 2.4 GHz band not supported at all */
2297 nmodes
= os_realloc(modes
, (*num_modes
+ 1) * sizeof(*nmodes
));
2299 return modes
; /* Could not add 802.11b mode */
2301 mode
= &nmodes
[*num_modes
];
2302 os_memset(mode
, 0, sizeof(*mode
));
2306 mode
->mode
= HOSTAPD_MODE_IEEE80211B
;
2308 mode11g
= &modes
[mode11g_idx
];
2309 mode
->num_channels
= mode11g
->num_channels
;
2310 mode
->channels
= os_malloc(mode11g
->num_channels
*
2311 sizeof(struct hostapd_channel_data
));
2312 if (mode
->channels
== NULL
) {
2314 return modes
; /* Could not add 802.11b mode */
2316 os_memcpy(mode
->channels
, mode11g
->channels
,
2317 mode11g
->num_channels
* sizeof(struct hostapd_channel_data
));
2319 mode
->num_rates
= 0;
2320 mode
->rates
= os_malloc(4 * sizeof(int));
2321 if (mode
->rates
== NULL
) {
2322 os_free(mode
->channels
);
2324 return modes
; /* Could not add 802.11b mode */
2327 for (i
= 0; i
< mode11g
->num_rates
; i
++) {
2328 if (mode11g
->rates
[i
] != 10 && mode11g
->rates
[i
] != 20 &&
2329 mode11g
->rates
[i
] != 55 && mode11g
->rates
[i
] != 110)
2331 mode
->rates
[mode
->num_rates
] = mode11g
->rates
[i
];
2333 if (mode
->num_rates
== 4)
2337 if (mode
->num_rates
== 0) {
2338 os_free(mode
->channels
);
2339 os_free(mode
->rates
);
2341 return modes
; /* No 802.11b rates */
2344 wpa_printf(MSG_DEBUG
, "nl80211: Added 802.11b mode based on 802.11g "
2351 static struct hostapd_hw_modes
*
2352 wpa_driver_nl80211_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
2354 struct wpa_driver_nl80211_data
*drv
= priv
;
2356 struct phy_info_arg result
= {
2357 .num_modes
= num_modes
,
2364 msg
= nlmsg_alloc();
2368 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2369 0, NL80211_CMD_GET_WIPHY
, 0);
2371 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2373 if (send_and_recv_msgs(drv
, msg
, phy_info_handler
, &result
) == 0)
2374 return wpa_driver_nl80211_add_11b(result
.modes
, num_modes
);
2380 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data
*drv
,
2381 const void *data
, size_t len
,
2385 0x00, 0x00, /* radiotap version */
2386 0x0e, 0x00, /* radiotap length */
2387 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2388 IEEE80211_RADIOTAP_F_FRAG
, /* F_FRAG (fragment if required) */
2390 0x00, 0x00, /* RX and TX flags to indicate that */
2391 0x00, 0x00, /* this is the injected frame directly */
2393 struct iovec iov
[2] = {
2395 .iov_base
= &rtap_hdr
,
2396 .iov_len
= sizeof(rtap_hdr
),
2399 .iov_base
= (void *) data
,
2403 struct msghdr msg
= {
2408 .msg_control
= NULL
,
2409 .msg_controllen
= 0,
2414 rtap_hdr
[8] |= IEEE80211_RADIOTAP_F_WEP
;
2416 return sendmsg(drv
->monitor_sock
, &msg
, 0);
2420 static int wpa_driver_nl80211_send_mlme(void *priv
, const u8
*data
,
2423 struct wpa_driver_nl80211_data
*drv
= priv
;
2424 struct ieee80211_mgmt
*mgmt
;
2428 mgmt
= (struct ieee80211_mgmt
*) data
;
2429 fc
= le_to_host16(mgmt
->frame_control
);
2431 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
2432 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_AUTH
) {
2434 * Only one of the authentication frame types is encrypted.
2435 * In order for static WEP encryption to work properly (i.e.,
2436 * to not encrypt the frame), we need to tell mac80211 about
2437 * the frames that must not be encrypted.
2439 u16 auth_alg
= le_to_host16(mgmt
->u
.auth
.auth_alg
);
2440 u16 auth_trans
= le_to_host16(mgmt
->u
.auth
.auth_transaction
);
2441 if (auth_alg
!= WLAN_AUTH_SHARED_KEY
|| auth_trans
!= 3)
2445 return wpa_driver_nl80211_send_frame(drv
, data
, data_len
, encrypt
);
2449 static int wpa_driver_nl80211_set_beacon(const char *ifname
, void *priv
,
2450 const u8
*head
, size_t head_len
,
2451 const u8
*tail
, size_t tail_len
,
2452 int dtim_period
, int beacon_int
)
2454 struct wpa_driver_nl80211_data
*drv
= priv
;
2456 u8 cmd
= NL80211_CMD_NEW_BEACON
;
2459 int ifindex
= if_nametoindex(ifname
);
2461 struct i802_bss
*bss
;
2463 bss
= get_bss(drv
, ifindex
);
2466 beacon_set
= bss
->beacon_set
;
2468 beacon_set
= drv
->beacon_set
;
2469 #endif /* HOSTAPD */
2471 msg
= nlmsg_alloc();
2475 wpa_printf(MSG_DEBUG
, "nl80211: Set beacon (beacon_set=%d)",
2478 cmd
= NL80211_CMD_SET_BEACON
;
2480 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2482 NLA_PUT(msg
, NL80211_ATTR_BEACON_HEAD
, head_len
, head
);
2483 NLA_PUT(msg
, NL80211_ATTR_BEACON_TAIL
, tail_len
, tail
);
2484 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
2485 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, beacon_int
);
2486 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, dtim_period
);
2488 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2490 wpa_printf(MSG_DEBUG
, "nl80211: Beacon set failed: %d (%s)",
2491 ret
, strerror(-ret
));
2494 bss
->beacon_set
= 1;
2496 drv
->beacon_set
= 1;
2497 #endif /* HOSTAPD */
2505 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data
*drv
,
2506 int freq
, int ht_enabled
,
2507 int sec_channel_offset
)
2512 msg
= nlmsg_alloc();
2516 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
2517 NL80211_CMD_SET_WIPHY
, 0);
2519 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2520 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
2522 switch (sec_channel_offset
) {
2524 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2525 NL80211_CHAN_HT40MINUS
);
2528 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2529 NL80211_CHAN_HT40PLUS
);
2532 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_CHANNEL_TYPE
,
2538 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2541 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set channel (freq=%d): "
2542 "%d (%s)", freq
, ret
, strerror(-ret
));
2548 static int wpa_driver_nl80211_sta_add(const char *ifname
, void *priv
,
2549 struct hostapd_sta_add_params
*params
)
2551 struct wpa_driver_nl80211_data
*drv
= priv
;
2555 msg
= nlmsg_alloc();
2559 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2560 0, NL80211_CMD_NEW_STATION
, 0);
2562 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(ifname
));
2563 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->addr
);
2564 NLA_PUT_U16(msg
, NL80211_ATTR_STA_AID
, params
->aid
);
2565 NLA_PUT(msg
, NL80211_ATTR_STA_SUPPORTED_RATES
, params
->supp_rates_len
,
2566 params
->supp_rates
);
2567 NLA_PUT_U16(msg
, NL80211_ATTR_STA_LISTEN_INTERVAL
,
2568 params
->listen_interval
);
2569 if (params
->ht_capabilities
) {
2570 NLA_PUT(msg
, NL80211_ATTR_HT_CAPABILITY
,
2571 sizeof(*params
->ht_capabilities
),
2572 params
->ht_capabilities
);
2575 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2577 wpa_printf(MSG_DEBUG
, "nl80211: NL80211_CMD_NEW_STATION "
2578 "result: %d (%s)", ret
, strerror(-ret
));
2586 static int wpa_driver_nl80211_sta_remove(void *priv
, const u8
*addr
)
2588 struct wpa_driver_nl80211_data
*drv
= priv
;
2592 msg
= nlmsg_alloc();
2596 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2597 0, NL80211_CMD_DEL_STATION
, 0);
2599 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
2600 if_nametoindex(drv
->ifname
));
2601 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
2603 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2612 static void nl80211_remove_iface(struct wpa_driver_nl80211_data
*drv
,
2617 wpa_printf(MSG_DEBUG
, "nl80211: Remove interface ifindex=%d", ifidx
);
2620 /* stop listening for EAPOL on this interface */
2621 del_ifidx(drv
, ifidx
);
2622 #endif /* HOSTAPD */
2624 msg
= nlmsg_alloc();
2626 goto nla_put_failure
;
2628 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2629 0, NL80211_CMD_DEL_INTERFACE
, 0);
2630 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifidx
);
2632 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
2635 wpa_printf(MSG_ERROR
, "Failed to remove interface (ifidx=%d).\n",
2640 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data
*drv
,
2642 enum nl80211_iftype iftype
,
2643 const u8
*addr
, int wds
)
2645 struct nl_msg
*msg
, *flags
= NULL
;
2649 msg
= nlmsg_alloc();
2653 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
2654 0, NL80211_CMD_NEW_INTERFACE
, 0);
2655 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
2656 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, ifname
);
2657 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, iftype
);
2659 if (iftype
== NL80211_IFTYPE_MONITOR
) {
2662 flags
= nlmsg_alloc();
2664 goto nla_put_failure
;
2666 NLA_PUT_FLAG(flags
, NL80211_MNTR_FLAG_COOK_FRAMES
);
2668 err
= nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
2673 goto nla_put_failure
;
2675 NLA_PUT_U8(msg
, NL80211_ATTR_4ADDR
, wds
);
2678 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
2681 wpa_printf(MSG_ERROR
, "Failed to create interface %s: %d (%s)",
2682 ifname
, ret
, strerror(-ret
));
2686 ifidx
= if_nametoindex(ifname
);
2687 wpa_printf(MSG_DEBUG
, "nl80211: New interface %s created: ifindex=%d",
2694 /* start listening for EAPOL on this interface */
2695 add_ifidx(drv
, ifidx
);
2696 #endif /* HOSTAPD */
2698 if (addr
&& iftype
!= NL80211_IFTYPE_MONITOR
&&
2699 set_ifhwaddr(drv
, ifname
, addr
)) {
2700 nl80211_remove_iface(drv
, ifidx
);
2708 static int nl80211_create_iface(struct wpa_driver_nl80211_data
*drv
,
2709 const char *ifname
, enum nl80211_iftype iftype
,
2710 const u8
*addr
, int wds
)
2714 ret
= nl80211_create_iface_once(drv
, ifname
, iftype
, addr
, wds
);
2716 /* if error occured and interface exists already */
2717 if (ret
== -ENFILE
&& if_nametoindex(ifname
)) {
2718 wpa_printf(MSG_INFO
, "Try to remove and re-create %s", ifname
);
2720 /* Try to remove the interface that was already there. */
2721 nl80211_remove_iface(drv
, if_nametoindex(ifname
));
2723 /* Try to create the interface again */
2724 ret
= nl80211_create_iface_once(drv
, ifname
, iftype
, addr
,
2732 static void handle_tx_callback(void *ctx
, u8
*buf
, size_t len
, int ok
)
2734 struct ieee80211_hdr
*hdr
;
2736 union wpa_event_data event
;
2738 hdr
= (struct ieee80211_hdr
*) buf
;
2739 fc
= le_to_host16(hdr
->frame_control
);
2741 os_memset(&event
, 0, sizeof(event
));
2742 event
.tx_status
.type
= WLAN_FC_GET_TYPE(fc
);
2743 event
.tx_status
.stype
= WLAN_FC_GET_STYPE(fc
);
2744 event
.tx_status
.dst
= hdr
->addr1
;
2745 event
.tx_status
.data
= buf
;
2746 event
.tx_status
.data_len
= len
;
2747 event
.tx_status
.ack
= ok
;
2748 wpa_supplicant_event(ctx
, EVENT_TX_STATUS
, &event
);
2752 static void from_unknown_sta(struct wpa_driver_nl80211_data
*drv
,
2753 u8
*buf
, size_t len
)
2755 union wpa_event_data event
;
2756 os_memset(&event
, 0, sizeof(event
));
2757 event
.rx_from_unknown
.frame
= buf
;
2758 event
.rx_from_unknown
.len
= len
;
2759 wpa_supplicant_event(drv
->ctx
, EVENT_RX_FROM_UNKNOWN
, &event
);
2763 static void handle_frame(struct wpa_driver_nl80211_data
*drv
,
2764 u8
*buf
, size_t len
, int datarate
, int ssi_signal
)
2766 struct ieee80211_hdr
*hdr
;
2768 union wpa_event_data event
;
2770 hdr
= (struct ieee80211_hdr
*) buf
;
2771 fc
= le_to_host16(hdr
->frame_control
);
2773 switch (WLAN_FC_GET_TYPE(fc
)) {
2774 case WLAN_FC_TYPE_MGMT
:
2775 os_memset(&event
, 0, sizeof(event
));
2776 event
.rx_mgmt
.frame
= buf
;
2777 event
.rx_mgmt
.frame_len
= len
;
2778 event
.rx_mgmt
.datarate
= datarate
;
2779 event
.rx_mgmt
.ssi_signal
= ssi_signal
;
2780 wpa_supplicant_event(drv
->ctx
, EVENT_RX_MGMT
, &event
);
2782 case WLAN_FC_TYPE_CTRL
:
2783 /* can only get here with PS-Poll frames */
2784 wpa_printf(MSG_DEBUG
, "CTRL");
2785 from_unknown_sta(drv
, buf
, len
);
2787 case WLAN_FC_TYPE_DATA
:
2788 from_unknown_sta(drv
, buf
, len
);
2794 static void handle_monitor_read(int sock
, void *eloop_ctx
, void *sock_ctx
)
2796 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
2798 unsigned char buf
[3000];
2799 struct ieee80211_radiotap_iterator iter
;
2801 int datarate
= 0, ssi_signal
= 0;
2802 int injected
= 0, failed
= 0, rxflags
= 0;
2804 len
= recv(sock
, buf
, sizeof(buf
), 0);
2810 if (drv
->nlmode
== NL80211_IFTYPE_STATION
&& !drv
->probe_req_report
) {
2811 wpa_printf(MSG_DEBUG
, "nl80211: Ignore monitor interface "
2812 "frame since Probe Request reporting is disabled");
2816 if (ieee80211_radiotap_iterator_init(&iter
, (void*)buf
, len
)) {
2817 printf("received invalid radiotap frame\n");
2822 ret
= ieee80211_radiotap_iterator_next(&iter
);
2826 printf("received invalid radiotap frame (%d)\n", ret
);
2829 switch (iter
.this_arg_index
) {
2830 case IEEE80211_RADIOTAP_FLAGS
:
2831 if (*iter
.this_arg
& IEEE80211_RADIOTAP_F_FCS
)
2834 case IEEE80211_RADIOTAP_RX_FLAGS
:
2837 case IEEE80211_RADIOTAP_TX_FLAGS
:
2839 failed
= le_to_host16((*(uint16_t *) iter
.this_arg
)) &
2840 IEEE80211_RADIOTAP_F_TX_FAIL
;
2842 case IEEE80211_RADIOTAP_DATA_RETRIES
:
2844 case IEEE80211_RADIOTAP_CHANNEL
:
2845 /* TODO: convert from freq/flags to channel number */
2847 case IEEE80211_RADIOTAP_RATE
:
2848 datarate
= *iter
.this_arg
* 5;
2850 case IEEE80211_RADIOTAP_DB_ANTSIGNAL
:
2851 ssi_signal
= *iter
.this_arg
;
2856 if (rxflags
&& injected
)
2860 handle_frame(drv
, buf
+ iter
.max_length
,
2861 len
- iter
.max_length
, datarate
, ssi_signal
);
2863 handle_tx_callback(drv
->ctx
, buf
+ iter
.max_length
,
2864 len
- iter
.max_length
, !failed
);
2869 * we post-process the filter code later and rewrite
2870 * this to the offset to the last instruction
2875 static struct sock_filter msock_filter_insns
[] = {
2877 * do a little-endian load of the radiotap length field
2879 /* load lower byte into A */
2880 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 2),
2881 /* put it into X (== index register) */
2882 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2883 /* load upper byte into A */
2884 BPF_STMT(BPF_LD
| BPF_B
| BPF_ABS
, 3),
2885 /* left-shift it by 8 */
2886 BPF_STMT(BPF_ALU
| BPF_LSH
| BPF_K
, 8),
2888 BPF_STMT(BPF_ALU
| BPF_OR
| BPF_X
, 0),
2889 /* put result into X */
2890 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2893 * Allow management frames through, this also gives us those
2894 * management frames that we sent ourselves with status
2896 /* load the lower byte of the IEEE 802.11 frame control field */
2897 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2898 /* mask off frame type and version */
2899 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0xF),
2900 /* accept frame if it's both 0, fall through otherwise */
2901 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0, PASS
, 0),
2904 * TODO: add a bit to radiotap RX flags that indicates
2905 * that the sending station is not associated, then
2906 * add a filter here that filters on our DA and that flag
2907 * to allow us to deauth frames to that bad station.
2909 * Not a regression -- we didn't do it before either.
2914 * drop non-data frames
2916 /* load the lower byte of the frame control field */
2917 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2918 /* mask off QoS bit */
2919 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x0c),
2920 /* drop non-data frames */
2921 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 8, 0, FAIL
),
2923 /* load the upper byte of the frame control field */
2924 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 1),
2925 /* mask off toDS/fromDS */
2926 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x03),
2927 /* accept WDS frames */
2928 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 3, PASS
, 0),
2931 * add header length to index
2933 /* load the lower byte of the frame control field */
2934 BPF_STMT(BPF_LD
| BPF_B
| BPF_IND
, 0),
2935 /* mask off QoS bit */
2936 BPF_STMT(BPF_ALU
| BPF_AND
| BPF_K
, 0x80),
2937 /* right shift it by 6 to give 0 or 2 */
2938 BPF_STMT(BPF_ALU
| BPF_RSH
| BPF_K
, 6),
2939 /* add data frame header length */
2940 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_K
, 24),
2941 /* add index, was start of 802.11 header */
2942 BPF_STMT(BPF_ALU
| BPF_ADD
| BPF_X
, 0),
2943 /* move to index, now start of LL header */
2944 BPF_STMT(BPF_MISC
| BPF_TAX
, 0),
2947 * Accept empty data frames, we use those for
2950 BPF_STMT(BPF_LD
| BPF_W
| BPF_LEN
, 0),
2951 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_X
, 0, PASS
, 0),
2954 * Accept EAPOL frames
2956 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 0),
2957 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0xAAAA0300, 0, FAIL
),
2958 BPF_STMT(BPF_LD
| BPF_W
| BPF_IND
, 4),
2959 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, 0x0000888E, PASS
, FAIL
),
2961 /* keep these last two statements or change the code below */
2962 /* return 0 == "DROP" */
2963 BPF_STMT(BPF_RET
| BPF_K
, 0),
2964 /* return ~0 == "keep all" */
2965 BPF_STMT(BPF_RET
| BPF_K
, ~0),
2968 static struct sock_fprog msock_filter
= {
2969 .len
= sizeof(msock_filter_insns
)/sizeof(msock_filter_insns
[0]),
2970 .filter
= msock_filter_insns
,
2974 static int add_monitor_filter(int s
)
2978 /* rewrite all PASS/FAIL jump offsets */
2979 for (idx
= 0; idx
< msock_filter
.len
; idx
++) {
2980 struct sock_filter
*insn
= &msock_filter_insns
[idx
];
2982 if (BPF_CLASS(insn
->code
) == BPF_JMP
) {
2983 if (insn
->code
== (BPF_JMP
|BPF_JA
)) {
2984 if (insn
->k
== PASS
)
2985 insn
->k
= msock_filter
.len
- idx
- 2;
2986 else if (insn
->k
== FAIL
)
2987 insn
->k
= msock_filter
.len
- idx
- 3;
2990 if (insn
->jt
== PASS
)
2991 insn
->jt
= msock_filter
.len
- idx
- 2;
2992 else if (insn
->jt
== FAIL
)
2993 insn
->jt
= msock_filter
.len
- idx
- 3;
2995 if (insn
->jf
== PASS
)
2996 insn
->jf
= msock_filter
.len
- idx
- 2;
2997 else if (insn
->jf
== FAIL
)
2998 insn
->jf
= msock_filter
.len
- idx
- 3;
3002 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
,
3003 &msock_filter
, sizeof(msock_filter
))) {
3004 perror("SO_ATTACH_FILTER");
3012 static void nl80211_remove_monitor_interface(
3013 struct wpa_driver_nl80211_data
*drv
)
3015 if (drv
->monitor_ifidx
>= 0) {
3016 nl80211_remove_iface(drv
, drv
->monitor_ifidx
);
3017 drv
->monitor_ifidx
= -1;
3019 if (drv
->monitor_sock
>= 0) {
3020 eloop_unregister_read_sock(drv
->monitor_sock
);
3021 close(drv
->monitor_sock
);
3022 drv
->monitor_sock
= -1;
3028 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data
*drv
)
3031 struct sockaddr_ll ll
;
3035 snprintf(buf
, IFNAMSIZ
, "mon.%s", drv
->ifname
);
3036 buf
[IFNAMSIZ
- 1] = '\0';
3038 drv
->monitor_ifidx
=
3039 nl80211_create_iface(drv
, buf
, NL80211_IFTYPE_MONITOR
, NULL
,
3042 if (drv
->monitor_ifidx
< 0)
3045 if (linux_set_iface_flags(drv
->ioctl_sock
, buf
, 1))
3048 memset(&ll
, 0, sizeof(ll
));
3049 ll
.sll_family
= AF_PACKET
;
3050 ll
.sll_ifindex
= drv
->monitor_ifidx
;
3051 drv
->monitor_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
3052 if (drv
->monitor_sock
< 0) {
3053 perror("socket[PF_PACKET,SOCK_RAW]");
3057 if (add_monitor_filter(drv
->monitor_sock
)) {
3058 wpa_printf(MSG_INFO
, "Failed to set socket filter for monitor "
3059 "interface; do filtering in user space");
3060 /* This works, but will cost in performance. */
3063 if (bind(drv
->monitor_sock
, (struct sockaddr
*) &ll
, sizeof(ll
)) < 0) {
3064 perror("monitor socket bind");
3068 optlen
= sizeof(optval
);
3071 (drv
->monitor_sock
, SOL_SOCKET
, SO_PRIORITY
, &optval
, optlen
)) {
3072 perror("Failed to set socket priority");
3076 if (eloop_register_read_sock(drv
->monitor_sock
, handle_monitor_read
,
3078 printf("Could not register monitor read socket\n");
3084 nl80211_remove_monitor_interface(drv
);
3089 static const u8 rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3091 static int wpa_driver_nl80211_hapd_send_eapol(
3092 void *priv
, const u8
*addr
, const u8
*data
,
3093 size_t data_len
, int encrypt
, const u8
*own_addr
)
3095 struct wpa_driver_nl80211_data
*drv
= priv
;
3096 struct ieee80211_hdr
*hdr
;
3101 int qos
= sta
->flags
& WPA_STA_WMM
;
3106 len
= sizeof(*hdr
) + (qos
? 2 : 0) + sizeof(rfc1042_header
) + 2 +
3108 hdr
= os_zalloc(len
);
3110 printf("malloc() failed for i802_send_data(len=%lu)\n",
3111 (unsigned long) len
);
3115 hdr
->frame_control
=
3116 IEEE80211_FC(WLAN_FC_TYPE_DATA
, WLAN_FC_STYPE_DATA
);
3117 hdr
->frame_control
|= host_to_le16(WLAN_FC_FROMDS
);
3119 hdr
->frame_control
|= host_to_le16(WLAN_FC_ISWEP
);
3120 #if 0 /* To be enabled if qos determination is added above */
3122 hdr
->frame_control
|=
3123 host_to_le16(WLAN_FC_STYPE_QOS_DATA
<< 4);
3127 memcpy(hdr
->IEEE80211_DA_FROMDS
, addr
, ETH_ALEN
);
3128 memcpy(hdr
->IEEE80211_BSSID_FROMDS
, own_addr
, ETH_ALEN
);
3129 memcpy(hdr
->IEEE80211_SA_FROMDS
, own_addr
, ETH_ALEN
);
3130 pos
= (u8
*) (hdr
+ 1);
3132 #if 0 /* To be enabled if qos determination is added above */
3134 /* add an empty QoS header if needed */
3141 memcpy(pos
, rfc1042_header
, sizeof(rfc1042_header
));
3142 pos
+= sizeof(rfc1042_header
);
3143 WPA_PUT_BE16(pos
, ETH_P_PAE
);
3145 memcpy(pos
, data
, data_len
);
3147 res
= wpa_driver_nl80211_send_frame(drv
, (u8
*) hdr
, len
, encrypt
);
3149 wpa_printf(MSG_ERROR
, "i802_send_eapol - packet len: %lu - "
3151 (unsigned long) len
, errno
, strerror(errno
));
3159 static u32
sta_flags_nl80211(int flags
)
3163 if (flags
& WPA_STA_AUTHORIZED
)
3164 f
|= BIT(NL80211_STA_FLAG_AUTHORIZED
);
3165 if (flags
& WPA_STA_WMM
)
3166 f
|= BIT(NL80211_STA_FLAG_WME
);
3167 if (flags
& WPA_STA_SHORT_PREAMBLE
)
3168 f
|= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE
);
3169 if (flags
& WPA_STA_MFP
)
3170 f
|= BIT(NL80211_STA_FLAG_MFP
);
3176 static int wpa_driver_nl80211_sta_set_flags(void *priv
, const u8
*addr
,
3177 int total_flags
, int flags_or
,
3180 struct wpa_driver_nl80211_data
*drv
= priv
;
3181 struct nl_msg
*msg
, *flags
= NULL
;
3182 struct nl80211_sta_flag_update upd
;
3184 msg
= nlmsg_alloc();
3188 flags
= nlmsg_alloc();
3194 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3195 0, NL80211_CMD_SET_STATION
, 0);
3197 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3198 if_nametoindex(drv
->ifname
));
3199 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3202 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
3203 * can be removed eventually.
3205 if (total_flags
& WPA_STA_AUTHORIZED
)
3206 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_AUTHORIZED
);
3208 if (total_flags
& WPA_STA_WMM
)
3209 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_WME
);
3211 if (total_flags
& WPA_STA_SHORT_PREAMBLE
)
3212 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_SHORT_PREAMBLE
);
3214 if (total_flags
& WPA_STA_MFP
)
3215 NLA_PUT_FLAG(flags
, NL80211_STA_FLAG_MFP
);
3217 if (nla_put_nested(msg
, NL80211_ATTR_STA_FLAGS
, flags
))
3218 goto nla_put_failure
;
3220 os_memset(&upd
, 0, sizeof(upd
));
3221 upd
.mask
= sta_flags_nl80211(flags_or
| ~flags_and
);
3222 upd
.set
= sta_flags_nl80211(flags_or
);
3223 NLA_PUT(msg
, NL80211_ATTR_STA_FLAGS2
, sizeof(upd
), &upd
);
3227 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3234 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data
*drv
,
3235 struct wpa_driver_associate_params
*params
)
3237 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) ||
3238 wpa_driver_nl80211_set_freq(drv
, params
->freq
, 0, 0)) {
3239 nl80211_remove_monitor_interface(drv
);
3243 /* TODO: setup monitor interface (and add code somewhere to remove this
3244 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
3250 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data
*drv
)
3255 msg
= nlmsg_alloc();
3259 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3260 NL80211_CMD_LEAVE_IBSS
, 0);
3261 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3262 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3265 wpa_printf(MSG_DEBUG
, "nl80211: Leave IBSS failed: ret=%d "
3266 "(%s)", ret
, strerror(-ret
));
3267 goto nla_put_failure
;
3271 wpa_printf(MSG_DEBUG
, "nl80211: Leave IBSS request sent successfully");
3279 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data
*drv
,
3280 struct wpa_driver_associate_params
*params
)
3286 wpa_printf(MSG_DEBUG
, "nl80211: Join IBSS (ifindex=%d)", drv
->ifindex
);
3288 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
)) {
3289 wpa_printf(MSG_INFO
, "nl80211: Failed to set interface into "
3295 msg
= nlmsg_alloc();
3299 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3300 NL80211_CMD_JOIN_IBSS
, 0);
3301 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3303 if (params
->ssid
== NULL
|| params
->ssid_len
> sizeof(drv
->ssid
))
3304 goto nla_put_failure
;
3306 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
3307 params
->ssid
, params
->ssid_len
);
3308 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
3310 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
3311 drv
->ssid_len
= params
->ssid_len
;
3313 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
3314 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
3316 ret
= nl80211_set_conn_keys(params
, msg
);
3318 goto nla_put_failure
;
3320 if (params
->wpa_ie
) {
3321 wpa_hexdump(MSG_DEBUG
,
3322 " * Extra IEs for Beacon/Probe Response frames",
3323 params
->wpa_ie
, params
->wpa_ie_len
);
3324 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
3328 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3331 wpa_printf(MSG_DEBUG
, "nl80211: Join IBSS failed: ret=%d (%s)",
3332 ret
, strerror(-ret
));
3334 if (ret
== -EALREADY
&& count
== 1) {
3335 wpa_printf(MSG_DEBUG
, "nl80211: Retry IBSS join after "
3337 nl80211_leave_ibss(drv
);
3342 goto nla_put_failure
;
3345 wpa_printf(MSG_DEBUG
, "nl80211: Join IBSS request sent successfully");
3353 static int wpa_driver_nl80211_connect(
3354 struct wpa_driver_nl80211_data
*drv
,
3355 struct wpa_driver_associate_params
*params
)
3358 enum nl80211_auth_type type
;
3361 msg
= nlmsg_alloc();
3365 wpa_printf(MSG_DEBUG
, "nl80211: Connect (ifindex=%d)", drv
->ifindex
);
3366 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3367 NL80211_CMD_CONNECT
, 0);
3369 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3370 if (params
->bssid
) {
3371 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
3372 MAC2STR(params
->bssid
));
3373 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
3376 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
3377 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
3380 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
3381 params
->ssid
, params
->ssid_len
);
3382 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
3384 if (params
->ssid_len
> sizeof(drv
->ssid
))
3385 goto nla_put_failure
;
3386 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
3387 drv
->ssid_len
= params
->ssid_len
;
3389 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
3391 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
3394 if (params
->auth_alg
& WPA_AUTH_ALG_OPEN
)
3395 type
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
3396 else if (params
->auth_alg
& WPA_AUTH_ALG_SHARED
)
3397 type
= NL80211_AUTHTYPE_SHARED_KEY
;
3398 else if (params
->auth_alg
& WPA_AUTH_ALG_LEAP
)
3399 type
= NL80211_AUTHTYPE_NETWORK_EAP
;
3400 else if (params
->auth_alg
& WPA_AUTH_ALG_FT
)
3401 type
= NL80211_AUTHTYPE_FT
;
3403 goto nla_put_failure
;
3405 wpa_printf(MSG_DEBUG
, " * Auth Type %d", type
);
3406 NLA_PUT_U32(msg
, NL80211_ATTR_AUTH_TYPE
, type
);
3408 if (params
->wpa_ie
&& params
->wpa_ie_len
) {
3409 enum nl80211_wpa_versions ver
;
3411 if (params
->wpa_ie
[0] == WLAN_EID_RSN
)
3412 ver
= NL80211_WPA_VERSION_2
;
3414 ver
= NL80211_WPA_VERSION_1
;
3416 wpa_printf(MSG_DEBUG
, " * WPA Version %d", ver
);
3417 NLA_PUT_U32(msg
, NL80211_ATTR_WPA_VERSIONS
, ver
);
3420 if (params
->pairwise_suite
!= CIPHER_NONE
) {
3423 switch (params
->pairwise_suite
) {
3425 cipher
= WLAN_CIPHER_SUITE_WEP40
;
3428 cipher
= WLAN_CIPHER_SUITE_WEP104
;
3431 cipher
= WLAN_CIPHER_SUITE_CCMP
;
3435 cipher
= WLAN_CIPHER_SUITE_TKIP
;
3438 NLA_PUT_U32(msg
, NL80211_ATTR_CIPHER_SUITES_PAIRWISE
, cipher
);
3441 if (params
->group_suite
!= CIPHER_NONE
) {
3444 switch (params
->group_suite
) {
3446 cipher
= WLAN_CIPHER_SUITE_WEP40
;
3449 cipher
= WLAN_CIPHER_SUITE_WEP104
;
3452 cipher
= WLAN_CIPHER_SUITE_CCMP
;
3456 cipher
= WLAN_CIPHER_SUITE_TKIP
;
3459 NLA_PUT_U32(msg
, NL80211_ATTR_CIPHER_SUITE_GROUP
, cipher
);
3462 if (params
->key_mgmt_suite
== KEY_MGMT_802_1X
||
3463 params
->key_mgmt_suite
== KEY_MGMT_PSK
) {
3464 int mgmt
= WLAN_AKM_SUITE_PSK
;
3466 switch (params
->key_mgmt_suite
) {
3467 case KEY_MGMT_802_1X
:
3468 mgmt
= WLAN_AKM_SUITE_8021X
;
3472 mgmt
= WLAN_AKM_SUITE_PSK
;
3475 NLA_PUT_U32(msg
, NL80211_ATTR_AKM_SUITES
, mgmt
);
3478 ret
= nl80211_set_conn_keys(params
, msg
);
3480 goto nla_put_failure
;
3482 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3485 wpa_printf(MSG_DEBUG
, "nl80211: MLME connect failed: ret=%d "
3486 "(%s)", ret
, strerror(-ret
));
3487 goto nla_put_failure
;
3490 wpa_printf(MSG_DEBUG
, "nl80211: Connect request send successfully");
3499 static int wpa_driver_nl80211_associate(
3500 void *priv
, struct wpa_driver_associate_params
*params
)
3502 struct wpa_driver_nl80211_data
*drv
= priv
;
3506 if (params
->mode
== IEEE80211_MODE_AP
)
3507 return wpa_driver_nl80211_ap(drv
, params
);
3509 if (params
->mode
== IEEE80211_MODE_IBSS
)
3510 return wpa_driver_nl80211_ibss(drv
, params
);
3512 if (!(drv
->capa
.flags
& WPA_DRIVER_FLAGS_SME
)) {
3513 if (wpa_driver_nl80211_set_mode(drv
, params
->mode
) < 0)
3515 return wpa_driver_nl80211_connect(drv
, params
);
3518 drv
->associated
= 0;
3520 msg
= nlmsg_alloc();
3524 wpa_printf(MSG_DEBUG
, "nl80211: Associate (ifindex=%d)",
3526 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3527 NL80211_CMD_ASSOCIATE
, 0);
3529 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3530 if (params
->bssid
) {
3531 wpa_printf(MSG_DEBUG
, " * bssid=" MACSTR
,
3532 MAC2STR(params
->bssid
));
3533 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, params
->bssid
);
3536 wpa_printf(MSG_DEBUG
, " * freq=%d", params
->freq
);
3537 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, params
->freq
);
3538 drv
->assoc_freq
= params
->freq
;
3540 drv
->assoc_freq
= 0;
3542 wpa_hexdump_ascii(MSG_DEBUG
, " * SSID",
3543 params
->ssid
, params
->ssid_len
);
3544 NLA_PUT(msg
, NL80211_ATTR_SSID
, params
->ssid_len
,
3546 if (params
->ssid_len
> sizeof(drv
->ssid
))
3547 goto nla_put_failure
;
3548 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
3549 drv
->ssid_len
= params
->ssid_len
;
3551 wpa_hexdump(MSG_DEBUG
, " * IEs", params
->wpa_ie
, params
->wpa_ie_len
);
3553 NLA_PUT(msg
, NL80211_ATTR_IE
, params
->wpa_ie_len
,
3556 #ifdef CONFIG_IEEE80211W
3557 if (params
->mgmt_frame_protection
== MGMT_FRAME_PROTECTION_REQUIRED
)
3558 NLA_PUT_U32(msg
, NL80211_ATTR_USE_MFP
, NL80211_MFP_REQUIRED
);
3559 #endif /* CONFIG_IEEE80211W */
3561 NLA_PUT_FLAG(msg
, NL80211_ATTR_CONTROL_PORT
);
3563 if (params
->prev_bssid
) {
3564 wpa_printf(MSG_DEBUG
, " * prev_bssid=" MACSTR
,
3565 MAC2STR(params
->prev_bssid
));
3566 NLA_PUT(msg
, NL80211_ATTR_PREV_BSSID
, ETH_ALEN
,
3567 params
->prev_bssid
);
3570 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3573 wpa_printf(MSG_DEBUG
, "nl80211: MLME command failed: ret=%d "
3574 "(%s)", ret
, strerror(-ret
));
3575 goto nla_put_failure
;
3578 wpa_printf(MSG_DEBUG
, "nl80211: Association request send "
3587 static int nl80211_set_mode(struct wpa_driver_nl80211_data
*drv
,
3588 int ifindex
, int mode
)
3593 msg
= nlmsg_alloc();
3597 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3598 0, NL80211_CMD_SET_INTERFACE
, 0);
3599 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
3600 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, mode
);
3602 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3606 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set interface %d to mode %d:"
3607 " %d (%s)", ifindex
, mode
, ret
, strerror(-ret
));
3612 static int wpa_driver_nl80211_set_mode(void *priv
, int mode
)
3614 struct wpa_driver_nl80211_data
*drv
= priv
;
3620 nlmode
= NL80211_IFTYPE_STATION
;
3623 nlmode
= NL80211_IFTYPE_ADHOC
;
3626 nlmode
= NL80211_IFTYPE_AP
;
3632 if (nl80211_set_mode(drv
, drv
->ifindex
, nlmode
) == 0) {
3633 drv
->nlmode
= nlmode
;
3638 if (nlmode
== drv
->nlmode
) {
3639 wpa_printf(MSG_DEBUG
, "nl80211: Interface already in "
3640 "requested mode - ignore error");
3642 goto done
; /* Already in the requested mode */
3645 /* mac80211 doesn't allow mode changes while the device is up, so
3646 * take the device down, try to set the mode again, and bring the
3649 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 0) == 0) {
3650 /* Try to set the mode again while the interface is down */
3651 ret
= nl80211_set_mode(drv
, drv
->ifindex
, nlmode
);
3652 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 1))
3657 wpa_printf(MSG_DEBUG
, "nl80211: Mode change succeeded while "
3658 "interface is down");
3659 drv
->nlmode
= nlmode
;
3663 if (!ret
&& nlmode
== NL80211_IFTYPE_AP
) {
3664 /* Setup additional AP mode functionality if needed */
3665 if (drv
->monitor_ifidx
< 0 &&
3666 nl80211_create_monitor_interface(drv
))
3668 } else if (!ret
&& nlmode
!= NL80211_IFTYPE_AP
) {
3669 /* Remove additional AP mode functionality */
3670 nl80211_remove_monitor_interface(drv
);
3674 wpa_printf(MSG_DEBUG
, "nl80211: Interface mode change to %d "
3675 "from %d failed", nlmode
, drv
->nlmode
);
3681 static int wpa_driver_nl80211_get_capa(void *priv
,
3682 struct wpa_driver_capa
*capa
)
3684 struct wpa_driver_nl80211_data
*drv
= priv
;
3685 if (!drv
->has_capability
)
3687 os_memcpy(capa
, &drv
->capa
, sizeof(*capa
));
3692 static int wpa_driver_nl80211_set_operstate(void *priv
, int state
)
3694 struct wpa_driver_nl80211_data
*drv
= priv
;
3696 wpa_printf(MSG_DEBUG
, "%s: operstate %d->%d (%s)",
3697 __func__
, drv
->operstate
, state
, state
? "UP" : "DORMANT");
3698 drv
->operstate
= state
;
3699 return netlink_send_oper_ifla(drv
->netlink
, drv
->ifindex
, -1,
3700 state
? IF_OPER_UP
: IF_OPER_DORMANT
);
3704 static int wpa_driver_nl80211_set_supp_port(void *priv
, int authorized
)
3706 struct wpa_driver_nl80211_data
*drv
= priv
;
3708 struct nl80211_sta_flag_update upd
;
3710 msg
= nlmsg_alloc();
3714 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3715 0, NL80211_CMD_SET_STATION
, 0);
3717 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3718 if_nametoindex(drv
->ifname
));
3719 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, drv
->bssid
);
3721 os_memset(&upd
, 0, sizeof(upd
));
3722 upd
.mask
= BIT(NL80211_STA_FLAG_AUTHORIZED
);
3724 upd
.set
= BIT(NL80211_STA_FLAG_AUTHORIZED
);
3725 NLA_PUT(msg
, NL80211_ATTR_STA_FLAGS2
, sizeof(upd
), &upd
);
3727 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3735 static struct i802_bss
* get_bss(struct wpa_driver_nl80211_data
*drv
,
3738 struct i802_bss
*bss
= &drv
->bss
;
3740 if (ifindex
== bss
->ifindex
)
3744 wpa_printf(MSG_DEBUG
, "nl80211: get_bss(%d) failed", ifindex
);
3749 static void add_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3754 wpa_printf(MSG_DEBUG
, "nl80211: Add own interface ifindex %d",
3756 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3757 if (drv
->if_indices
[i
] == 0) {
3758 drv
->if_indices
[i
] = ifidx
;
3763 if (drv
->if_indices
!= drv
->default_if_indices
)
3764 old
= drv
->if_indices
;
3768 drv
->if_indices
= os_realloc(old
,
3769 sizeof(int) * (drv
->num_if_indices
+ 1));
3770 if (!drv
->if_indices
) {
3772 drv
->if_indices
= drv
->default_if_indices
;
3774 drv
->if_indices
= old
;
3775 wpa_printf(MSG_ERROR
, "Failed to reallocate memory for "
3777 wpa_printf(MSG_ERROR
, "Ignoring EAPOL on interface %d", ifidx
);
3780 drv
->if_indices
[drv
->num_if_indices
] = ifidx
;
3781 drv
->num_if_indices
++;
3785 static void del_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3789 for (i
= 0; i
< drv
->num_if_indices
; i
++) {
3790 if (drv
->if_indices
[i
] == ifidx
) {
3791 drv
->if_indices
[i
] = 0;
3798 static int have_ifidx(struct wpa_driver_nl80211_data
*drv
, int ifidx
)
3802 for (i
= 0; i
< drv
->num_if_indices
; i
++)
3803 if (drv
->if_indices
[i
] == ifidx
)
3810 static inline int min_int(int a
, int b
)
3818 static int get_key_handler(struct nl_msg
*msg
, void *arg
)
3820 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3821 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3823 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
3824 genlmsg_attrlen(gnlh
, 0), NULL
);
3827 * TODO: validate the key index and mac address!
3828 * Otherwise, there's a race condition as soon as
3829 * the kernel starts sending key notifications.
3832 if (tb
[NL80211_ATTR_KEY_SEQ
])
3833 memcpy(arg
, nla_data(tb
[NL80211_ATTR_KEY_SEQ
]),
3834 min_int(nla_len(tb
[NL80211_ATTR_KEY_SEQ
]), 6));
3839 static int i802_get_seqnum(const char *iface
, void *priv
, const u8
*addr
,
3842 struct wpa_driver_nl80211_data
*drv
= priv
;
3845 msg
= nlmsg_alloc();
3849 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3850 0, NL80211_CMD_GET_KEY
, 0);
3853 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
3854 NLA_PUT_U8(msg
, NL80211_ATTR_KEY_IDX
, idx
);
3855 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(iface
));
3859 return send_and_recv_msgs(drv
, msg
, get_key_handler
, seq
);
3865 static int i802_set_rate_sets(void *priv
, int *supp_rates
, int *basic_rates
,
3868 struct wpa_driver_nl80211_data
*drv
= priv
;
3870 u8 rates
[NL80211_MAX_SUPP_RATES
];
3874 msg
= nlmsg_alloc();
3878 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
3879 NL80211_CMD_SET_BSS
, 0);
3881 for (i
= 0; i
< NL80211_MAX_SUPP_RATES
&& basic_rates
[i
] >= 0; i
++)
3882 rates
[rates_len
++] = basic_rates
[i
] / 5;
3884 NLA_PUT(msg
, NL80211_ATTR_BSS_BASIC_RATES
, rates_len
, rates
);
3886 /* TODO: multi-BSS support */
3887 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
3889 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3895 /* Set kernel driver on given frequency (MHz) */
3896 static int i802_set_freq(void *priv
, struct hostapd_freq_params
*freq
)
3898 struct wpa_driver_nl80211_data
*drv
= priv
;
3899 return wpa_driver_nl80211_set_freq(drv
, freq
->freq
, freq
->ht_enabled
,
3900 freq
->sec_channel_offset
);
3904 static int i802_set_rts(void *priv
, int rts
)
3906 struct wpa_driver_nl80211_data
*drv
= priv
;
3911 msg
= nlmsg_alloc();
3920 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3921 0, NL80211_CMD_SET_WIPHY
, 0);
3922 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3923 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_RTS_THRESHOLD
, val
);
3925 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3929 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set RTS threshold %d: "
3930 "%d (%s)", rts
, ret
, strerror(-ret
));
3935 static int i802_set_frag(void *priv
, int frag
)
3937 struct wpa_driver_nl80211_data
*drv
= priv
;
3942 msg
= nlmsg_alloc();
3951 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3952 0, NL80211_CMD_SET_WIPHY
, 0);
3953 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
3954 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FRAG_THRESHOLD
, val
);
3956 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3960 wpa_printf(MSG_DEBUG
, "nl80211: Failed to set fragmentation threshold "
3961 "%d: %d (%s)", frag
, ret
, strerror(-ret
));
3966 static int i802_flush(void *priv
)
3968 struct wpa_driver_nl80211_data
*drv
= priv
;
3971 msg
= nlmsg_alloc();
3975 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
3976 0, NL80211_CMD_DEL_STATION
, 0);
3979 * XXX: FIX! this needs to flush all VLANs too
3981 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
3982 if_nametoindex(drv
->ifname
));
3984 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
3990 static int get_sta_handler(struct nl_msg
*msg
, void *arg
)
3992 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
3993 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
3994 struct hostap_sta_driver_data
*data
= arg
;
3995 struct nlattr
*stats
[NL80211_STA_INFO_MAX
+ 1];
3996 static struct nla_policy stats_policy
[NL80211_STA_INFO_MAX
+ 1] = {
3997 [NL80211_STA_INFO_INACTIVE_TIME
] = { .type
= NLA_U32
},
3998 [NL80211_STA_INFO_RX_BYTES
] = { .type
= NLA_U32
},
3999 [NL80211_STA_INFO_TX_BYTES
] = { .type
= NLA_U32
},
4000 [NL80211_STA_INFO_RX_PACKETS
] = { .type
= NLA_U32
},
4001 [NL80211_STA_INFO_TX_PACKETS
] = { .type
= NLA_U32
},
4004 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
4005 genlmsg_attrlen(gnlh
, 0), NULL
);
4008 * TODO: validate the interface and mac address!
4009 * Otherwise, there's a race condition as soon as
4010 * the kernel starts sending station notifications.
4013 if (!tb
[NL80211_ATTR_STA_INFO
]) {
4014 wpa_printf(MSG_DEBUG
, "sta stats missing!");
4017 if (nla_parse_nested(stats
, NL80211_STA_INFO_MAX
,
4018 tb
[NL80211_ATTR_STA_INFO
],
4020 wpa_printf(MSG_DEBUG
, "failed to parse nested attributes!");
4024 if (stats
[NL80211_STA_INFO_INACTIVE_TIME
])
4025 data
->inactive_msec
=
4026 nla_get_u32(stats
[NL80211_STA_INFO_INACTIVE_TIME
]);
4027 if (stats
[NL80211_STA_INFO_RX_BYTES
])
4028 data
->rx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_RX_BYTES
]);
4029 if (stats
[NL80211_STA_INFO_TX_BYTES
])
4030 data
->tx_bytes
= nla_get_u32(stats
[NL80211_STA_INFO_TX_BYTES
]);
4031 if (stats
[NL80211_STA_INFO_RX_PACKETS
])
4033 nla_get_u32(stats
[NL80211_STA_INFO_RX_PACKETS
]);
4034 if (stats
[NL80211_STA_INFO_TX_PACKETS
])
4036 nla_get_u32(stats
[NL80211_STA_INFO_TX_PACKETS
]);
4041 static int i802_read_sta_data(void *priv
, struct hostap_sta_driver_data
*data
,
4044 struct wpa_driver_nl80211_data
*drv
= priv
;
4047 os_memset(data
, 0, sizeof(*data
));
4048 msg
= nlmsg_alloc();
4052 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
4053 0, NL80211_CMD_GET_STATION
, 0);
4055 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
4056 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
4058 return send_and_recv_msgs(drv
, msg
, get_sta_handler
, data
);
4064 static int i802_set_tx_queue_params(void *priv
, int queue
, int aifs
,
4065 int cw_min
, int cw_max
, int burst_time
)
4067 struct wpa_driver_nl80211_data
*drv
= priv
;
4069 struct nlattr
*txq
, *params
;
4071 msg
= nlmsg_alloc();
4075 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
4076 0, NL80211_CMD_SET_WIPHY
, 0);
4078 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
4080 txq
= nla_nest_start(msg
, NL80211_ATTR_WIPHY_TXQ_PARAMS
);
4082 goto nla_put_failure
;
4084 /* We are only sending parameters for a single TXQ at a time */
4085 params
= nla_nest_start(msg
, 1);
4087 goto nla_put_failure
;
4089 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_QUEUE
, queue
);
4090 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
4091 * 32 usec, so need to convert the value here. */
4092 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_TXOP
, (burst_time
* 100 + 16) / 32);
4093 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMIN
, cw_min
);
4094 NLA_PUT_U16(msg
, NL80211_TXQ_ATTR_CWMAX
, cw_max
);
4095 NLA_PUT_U8(msg
, NL80211_TXQ_ATTR_AIFS
, aifs
);
4097 nla_nest_end(msg
, params
);
4099 nla_nest_end(msg
, txq
);
4101 if (send_and_recv_msgs(drv
, msg
, NULL
, NULL
) == 0)
4108 static int i802_set_bss(void *priv
, int cts
, int preamble
, int slot
)
4110 struct wpa_driver_nl80211_data
*drv
= priv
;
4113 msg
= nlmsg_alloc();
4117 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
4118 NL80211_CMD_SET_BSS
, 0);
4121 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_CTS_PROT
, cts
);
4123 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_PREAMBLE
, preamble
);
4125 NLA_PUT_U8(msg
, NL80211_ATTR_BSS_SHORT_SLOT_TIME
, slot
);
4127 /* TODO: multi-BSS support */
4128 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(drv
->ifname
));
4130 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4136 static int i802_set_cts_protect(void *priv
, int value
)
4138 return i802_set_bss(priv
, value
, -1, -1);
4142 static int i802_set_preamble(void *priv
, int value
)
4144 return i802_set_bss(priv
, -1, value
, -1);
4148 static int i802_set_short_slot_time(void *priv
, int value
)
4150 return i802_set_bss(priv
, -1, -1, value
);
4154 static int i802_set_sta_vlan(void *priv
, const u8
*addr
,
4155 const char *ifname
, int vlan_id
)
4157 struct wpa_driver_nl80211_data
*drv
= priv
;
4160 msg
= nlmsg_alloc();
4164 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0,
4165 0, NL80211_CMD_SET_STATION
, 0);
4167 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
,
4168 if_nametoindex(drv
->ifname
));
4169 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, addr
);
4170 NLA_PUT_U32(msg
, NL80211_ATTR_STA_VLAN
,
4171 if_nametoindex(ifname
));
4173 return send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4179 static int i802_set_wds_sta(void *priv
, const u8
*addr
, int aid
, int val
)
4181 struct wpa_driver_nl80211_data
*drv
= priv
;
4184 os_snprintf(name
, sizeof(name
), "%s.sta%d", drv
->ifname
, aid
);
4186 if (nl80211_create_iface(priv
, name
, NL80211_IFTYPE_AP_VLAN
,
4189 linux_set_iface_flags(drv
->ioctl_sock
, name
, 1);
4190 return i802_set_sta_vlan(priv
, addr
, name
, 0);
4192 i802_set_sta_vlan(priv
, addr
, drv
->ifname
, 0);
4193 return wpa_driver_nl80211_if_remove(priv
, WPA_IF_AP_VLAN
,
4199 static void handle_eapol(int sock
, void *eloop_ctx
, void *sock_ctx
)
4201 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
4202 struct sockaddr_ll lladdr
;
4203 unsigned char buf
[3000];
4205 socklen_t fromlen
= sizeof(lladdr
);
4207 len
= recvfrom(sock
, buf
, sizeof(buf
), 0,
4208 (struct sockaddr
*)&lladdr
, &fromlen
);
4214 if (have_ifidx(drv
, lladdr
.sll_ifindex
))
4215 drv_event_eapol_rx(drv
->ctx
, lladdr
.sll_addr
, buf
, len
);
4219 static int i802_get_inact_sec(void *priv
, const u8
*addr
)
4221 struct hostap_sta_driver_data data
;
4224 data
.inactive_msec
= (unsigned long) -1;
4225 ret
= i802_read_sta_data(priv
, &data
, addr
);
4226 if (ret
|| data
.inactive_msec
== (unsigned long) -1)
4228 return data
.inactive_msec
/ 1000;
4232 static int i802_sta_clear_stats(void *priv
, const u8
*addr
)
4241 static int i802_sta_deauth(void *priv
, const u8
*own_addr
, const u8
*addr
,
4244 struct wpa_driver_nl80211_data
*drv
= priv
;
4245 struct ieee80211_mgmt mgmt
;
4247 memset(&mgmt
, 0, sizeof(mgmt
));
4248 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
4249 WLAN_FC_STYPE_DEAUTH
);
4250 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
4251 memcpy(mgmt
.sa
, own_addr
, ETH_ALEN
);
4252 memcpy(mgmt
.bssid
, own_addr
, ETH_ALEN
);
4253 mgmt
.u
.deauth
.reason_code
= host_to_le16(reason
);
4254 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
4256 sizeof(mgmt
.u
.deauth
));
4260 static int i802_sta_disassoc(void *priv
, const u8
*own_addr
, const u8
*addr
,
4263 struct wpa_driver_nl80211_data
*drv
= priv
;
4264 struct ieee80211_mgmt mgmt
;
4266 memset(&mgmt
, 0, sizeof(mgmt
));
4267 mgmt
.frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
4268 WLAN_FC_STYPE_DISASSOC
);
4269 memcpy(mgmt
.da
, addr
, ETH_ALEN
);
4270 memcpy(mgmt
.sa
, own_addr
, ETH_ALEN
);
4271 memcpy(mgmt
.bssid
, own_addr
, ETH_ALEN
);
4272 mgmt
.u
.disassoc
.reason_code
= host_to_le16(reason
);
4273 return wpa_driver_nl80211_send_mlme(drv
, (u8
*) &mgmt
,
4275 sizeof(mgmt
.u
.disassoc
));
4279 static void *i802_init(struct hostapd_data
*hapd
,
4280 struct wpa_init_params
*params
)
4282 struct wpa_driver_nl80211_data
*drv
;
4285 drv
= wpa_driver_nl80211_init(hapd
, params
->ifname
);
4289 drv
->bss
.ifindex
= drv
->ifindex
;
4291 drv
->num_if_indices
= sizeof(drv
->default_if_indices
) / sizeof(int);
4292 drv
->if_indices
= drv
->default_if_indices
;
4293 for (i
= 0; i
< params
->num_bridge
; i
++) {
4294 if (params
->bridge
[i
])
4295 add_ifidx(drv
, if_nametoindex(params
->bridge
[i
]));
4298 /* start listening for EAPOL on the default AP interface */
4299 add_ifidx(drv
, drv
->ifindex
);
4301 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 0))
4304 if (params
->bssid
) {
4305 if (set_ifhwaddr(drv
, drv
->ifname
, params
->bssid
))
4309 if (wpa_driver_nl80211_set_mode(drv
, IEEE80211_MODE_AP
)) {
4310 wpa_printf(MSG_ERROR
, "nl80211: Failed to set interface %s "
4311 "into AP mode", drv
->ifname
);
4315 if (linux_set_iface_flags(drv
->ioctl_sock
, drv
->ifname
, 1))
4318 drv
->eapol_sock
= socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_PAE
));
4319 if (drv
->eapol_sock
< 0) {
4320 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
4324 if (eloop_register_read_sock(drv
->eapol_sock
, handle_eapol
, drv
, NULL
))
4326 printf("Could not register read socket for eapol\n");
4330 if (get_ifhwaddr(drv
, drv
->ifname
, params
->own_addr
))
4336 nl80211_remove_monitor_interface(drv
);
4337 if (drv
->ioctl_sock
>= 0)
4338 close(drv
->ioctl_sock
);
4340 genl_family_put(drv
->nl80211
);
4341 nl_cache_free(drv
->nl_cache
);
4342 nl_handle_destroy(drv
->nl_handle
);
4343 nl_cb_put(drv
->nl_cb
);
4350 static void i802_deinit(void *priv
)
4352 wpa_driver_nl80211_deinit(priv
);
4355 #endif /* HOSTAPD */
4358 static enum nl80211_iftype
wpa_driver_nl80211_if_type(
4359 enum wpa_driver_if_type type
)
4362 case WPA_IF_STATION
:
4363 return NL80211_IFTYPE_STATION
;
4364 case WPA_IF_AP_VLAN
:
4365 return NL80211_IFTYPE_AP_VLAN
;
4367 return NL80211_IFTYPE_AP
;
4373 static int wpa_driver_nl80211_if_add(const char *iface
, void *priv
,
4374 enum wpa_driver_if_type type
,
4375 const char *ifname
, const u8
*addr
,
4378 struct wpa_driver_nl80211_data
*drv
= priv
;
4381 struct i802_bss
*bss
= NULL
;
4383 if (type
== WPA_IF_AP_BSS
) {
4384 bss
= os_zalloc(sizeof(*bss
));
4388 #endif /* HOSTAPD */
4390 ifidx
= nl80211_create_iface(drv
, ifname
,
4391 wpa_driver_nl80211_if_type(type
), addr
,
4396 #endif /* HOSTAPD */
4401 if (type
== WPA_IF_AP_BSS
) {
4402 if (linux_set_iface_flags(drv
->ioctl_sock
, ifname
, 1)) {
4403 nl80211_remove_iface(drv
, ifidx
);
4407 bss
->ifindex
= ifidx
;
4408 bss
->next
= drv
->bss
.next
;
4409 drv
->bss
.next
= bss
;
4411 #endif /* HOSTAPD */
4417 static int wpa_driver_nl80211_if_remove(void *priv
,
4418 enum wpa_driver_if_type type
,
4421 struct wpa_driver_nl80211_data
*drv
= priv
;
4422 int ifindex
= if_nametoindex(ifname
);
4424 nl80211_remove_iface(drv
, ifindex
);
4427 if (type
== WPA_IF_AP_BSS
) {
4428 struct i802_bss
*bss
, *prev
;
4430 bss
= drv
->bss
.next
;
4432 if (ifindex
== bss
->ifindex
) {
4433 prev
->next
= bss
->next
;
4441 #endif /* HOSTAPD */
4447 static int cookie_handler(struct nl_msg
*msg
, void *arg
)
4449 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
4450 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
4452 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
4453 genlmsg_attrlen(gnlh
, 0), NULL
);
4454 if (tb
[NL80211_ATTR_COOKIE
])
4455 *cookie
= nla_get_u64(tb
[NL80211_ATTR_COOKIE
]);
4460 static int wpa_driver_nl80211_remain_on_channel(void *priv
, unsigned int freq
,
4461 unsigned int duration
)
4463 struct wpa_driver_nl80211_data
*drv
= priv
;
4468 msg
= nlmsg_alloc();
4472 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
4473 NL80211_CMD_REMAIN_ON_CHANNEL
, 0);
4475 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
4476 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
4477 NLA_PUT_U32(msg
, NL80211_ATTR_DURATION
, duration
);
4480 ret
= send_and_recv_msgs(drv
, msg
, cookie_handler
, &cookie
);
4482 wpa_printf(MSG_DEBUG
, "nl80211: Remain-on-channel cookie "
4483 "0x%llx for freq=%u MHz duration=%u",
4484 (long long unsigned int) cookie
, freq
, duration
);
4485 drv
->remain_on_chan_cookie
= cookie
;
4488 wpa_printf(MSG_DEBUG
, "nl80211: Failed to request remain-on-channel "
4489 "(freq=%d): %d (%s)", freq
, ret
, strerror(-ret
));
4495 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv
)
4497 struct wpa_driver_nl80211_data
*drv
= priv
;
4501 if (!drv
->pending_remain_on_chan
) {
4502 wpa_printf(MSG_DEBUG
, "nl80211: No pending remain-on-channel "
4507 wpa_printf(MSG_DEBUG
, "nl80211: Cancel remain-on-channel with cookie "
4509 (long long unsigned int) drv
->remain_on_chan_cookie
);
4511 msg
= nlmsg_alloc();
4515 genlmsg_put(msg
, 0, 0, genl_family_get_id(drv
->nl80211
), 0, 0,
4516 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
, 0);
4518 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, drv
->ifindex
);
4519 NLA_PUT_U64(msg
, NL80211_ATTR_COOKIE
, drv
->remain_on_chan_cookie
);
4521 ret
= send_and_recv_msgs(drv
, msg
, NULL
, NULL
);
4524 wpa_printf(MSG_DEBUG
, "nl80211: Failed to cancel remain-on-channel: "
4525 "%d (%s)", ret
, strerror(-ret
));
4531 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx
,
4534 struct wpa_driver_nl80211_data
*drv
= eloop_ctx
;
4535 if (drv
->monitor_ifidx
< 0)
4536 return; /* monitor interface already removed */
4538 if (drv
->nlmode
!= NL80211_IFTYPE_STATION
)
4539 return; /* not in station mode anymore */
4541 if (drv
->probe_req_report
)
4542 return; /* reporting enabled */
4544 wpa_printf(MSG_DEBUG
, "nl80211: Remove monitor interface due to no "
4545 "Probe Request reporting needed anymore");
4546 nl80211_remove_monitor_interface(drv
);
4550 static int wpa_driver_nl80211_probe_req_report(void *priv
, int report
)
4552 struct wpa_driver_nl80211_data
*drv
= priv
;
4554 if (drv
->nlmode
!= NL80211_IFTYPE_STATION
) {
4555 wpa_printf(MSG_DEBUG
, "nl80211: probe_req_report control only "
4556 "allowed in station mode (iftype=%d)",
4560 drv
->probe_req_report
= report
;
4563 eloop_cancel_timeout(
4564 wpa_driver_nl80211_probe_req_report_timeout
,
4566 if (drv
->monitor_ifidx
< 0 &&
4567 nl80211_create_monitor_interface(drv
))
4571 * It takes a while to remove the monitor interface, so try to
4572 * avoid doing this if it is needed again shortly. Instead,
4573 * schedule the interface to be removed later if no need for it
4576 wpa_printf(MSG_DEBUG
, "nl80211: Scheduling monitor interface "
4577 "to be removed after 10 seconds of no use");
4578 eloop_register_timeout(
4579 10, 0, wpa_driver_nl80211_probe_req_report_timeout
,
4587 static int wpa_driver_nl80211_alloc_interface_addr(void *priv
, u8
*addr
)
4589 struct wpa_driver_nl80211_data
*drv
= priv
;
4591 if (get_ifhwaddr(drv
, drv
->ifname
, addr
) < 0)
4594 if (addr
[0] & 0x02) {
4595 /* TODO: add support for generating multiple addresses */
4598 addr
[0] = 0x02; /* locally administered */
4604 static void wpa_driver_nl80211_release_interface_addr(void *priv
,
4607 /* TODO: keep list of allocated address and release them here */
4611 const struct wpa_driver_ops wpa_driver_nl80211_ops
= {
4613 .desc
= "Linux nl80211/cfg80211",
4614 .get_bssid
= wpa_driver_nl80211_get_bssid
,
4615 .get_ssid
= wpa_driver_nl80211_get_ssid
,
4616 .set_key
= wpa_driver_nl80211_set_key
,
4617 .scan2
= wpa_driver_nl80211_scan
,
4618 .get_scan_results2
= wpa_driver_nl80211_get_scan_results
,
4619 .deauthenticate
= wpa_driver_nl80211_deauthenticate
,
4620 .disassociate
= wpa_driver_nl80211_disassociate
,
4621 .authenticate
= wpa_driver_nl80211_authenticate
,
4622 .associate
= wpa_driver_nl80211_associate
,
4623 .init
= wpa_driver_nl80211_init
,
4624 .deinit
= wpa_driver_nl80211_deinit
,
4625 .get_capa
= wpa_driver_nl80211_get_capa
,
4626 .set_operstate
= wpa_driver_nl80211_set_operstate
,
4627 .set_supp_port
= wpa_driver_nl80211_set_supp_port
,
4628 .set_country
= wpa_driver_nl80211_set_country
,
4629 .set_beacon
= wpa_driver_nl80211_set_beacon
,
4630 .if_add
= wpa_driver_nl80211_if_add
,
4631 .if_remove
= wpa_driver_nl80211_if_remove
,
4632 .send_mlme
= wpa_driver_nl80211_send_mlme
,
4633 .get_hw_feature_data
= wpa_driver_nl80211_get_hw_feature_data
,
4634 .sta_add
= wpa_driver_nl80211_sta_add
,
4635 .sta_remove
= wpa_driver_nl80211_sta_remove
,
4636 .hapd_send_eapol
= wpa_driver_nl80211_hapd_send_eapol
,
4637 .sta_set_flags
= wpa_driver_nl80211_sta_set_flags
,
4639 .hapd_init
= i802_init
,
4640 .hapd_deinit
= i802_deinit
,
4641 .get_seqnum
= i802_get_seqnum
,
4642 .flush
= i802_flush
,
4643 .read_sta_data
= i802_read_sta_data
,
4644 .sta_deauth
= i802_sta_deauth
,
4645 .sta_disassoc
= i802_sta_disassoc
,
4646 .get_inact_sec
= i802_get_inact_sec
,
4647 .sta_clear_stats
= i802_sta_clear_stats
,
4648 .set_freq
= i802_set_freq
,
4649 .set_rts
= i802_set_rts
,
4650 .set_frag
= i802_set_frag
,
4651 .set_rate_sets
= i802_set_rate_sets
,
4652 .set_cts_protect
= i802_set_cts_protect
,
4653 .set_preamble
= i802_set_preamble
,
4654 .set_short_slot_time
= i802_set_short_slot_time
,
4655 .set_tx_queue_params
= i802_set_tx_queue_params
,
4656 .set_sta_vlan
= i802_set_sta_vlan
,
4657 .set_wds_sta
= i802_set_wds_sta
,
4658 #endif /* HOSTAPD */
4659 .remain_on_channel
= wpa_driver_nl80211_remain_on_channel
,
4660 .cancel_remain_on_channel
=
4661 wpa_driver_nl80211_cancel_remain_on_channel
,
4662 .probe_req_report
= wpa_driver_nl80211_probe_req_report
,
4663 .alloc_interface_addr
= wpa_driver_nl80211_alloc_interface_addr
,
4664 .release_interface_addr
= wpa_driver_nl80211_release_interface_addr
,