fragattacks/src/drivers/driver_bsd.c
Jouni Malinen aba7569ec8 driver_bsd: Fix build without SIOCS80211CHANNEL
At least FreeBSD 7 does not seem to define this and failed to build
after the previous changes.
2010-02-08 21:41:51 +02:00

1686 lines
42 KiB
C

/*
* WPA Supplicant - driver interaction with BSD net80211 layer
* Copyright (c) 2004, Sam Leffler <sam@errno.com>
* Copyright (c) 2004, 2Wire, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
#include "includes.h"
#include <sys/ioctl.h>
#include "common.h"
#include "driver.h"
#include "eloop.h"
#include "common/ieee802_11_defs.h"
#include <net/if.h>
#include <net/if_media.h>
#ifdef __NetBSD__
#include <net/if_ether.h>
#else
#include <net/ethernet.h>
#endif
#include <net/route.h>
#ifdef __DragonFly__
#include <netproto/802_11/ieee80211_ioctl.h>
#include <netproto/802_11/ieee80211_dragonfly.h>
#else /* __DragonFly__ */
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#include <net80211/ieee80211_crypto.h>
#endif /* __DragonFly__ */
#if __FreeBSD__
#include <net80211/ieee80211_freebsd.h>
#endif
#if __NetBSD__
#include <net80211/ieee80211_netbsd.h>
#endif
#include "l2_packet/l2_packet.h"
/* Generic functions for hostapd and wpa_supplicant */
static int
bsd_set80211var(int s, const char *ifname, int op, const void *arg, int arg_len)
{
struct ieee80211req ireq;
os_memset(&ireq, 0, sizeof(ireq));
os_strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
ireq.i_type = op;
ireq.i_len = arg_len;
ireq.i_data = (void *) arg;
if (ioctl(s, SIOCS80211, &ireq) < 0) {
fprintf(stderr, "ioctl[SIOCS80211, op %u, len %u]: %s\n",
op, arg_len, strerror(errno));
return -1;
}
return 0;
}
static int
bsd_get80211var(int s, const char *ifname, int op, void *arg, int arg_len)
{
struct ieee80211req ireq;
os_memset(&ireq, 0, sizeof(ireq));
os_strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
ireq.i_type = op;
ireq.i_len = arg_len;
ireq.i_data = arg;
if (ioctl(s, SIOCG80211, &ireq) < 0) {
fprintf(stderr, "ioctl[SIOCG80211, op %u, len %u]: %s\n",
op, arg_len, strerror(errno));
return -1;
}
return ireq.i_len;
}
static int
bsd_set80211param(int s, const char *ifname, int op, int arg)
{
struct ieee80211req ireq;
os_memset(&ireq, 0, sizeof(ireq));
os_strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
ireq.i_type = op;
ireq.i_val = arg;
if (ioctl(s, SIOCS80211, &ireq) < 0) {
fprintf(stderr, "ioctl[SIOCS80211, op %u, arg 0x%x]: %s\n",
op, arg, strerror(errno));
return -1;
}
return 0;
}
static int
bsd_get_ssid(int s, const char *ifname, u8 *ssid)
{
#ifdef SIOCG80211NWID
struct ieee80211_nwid nwid;
struct ifreq ifr;
os_memset(&ifr, 0, sizeof(ifr));
os_strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ifr.ifr_data = (void *)&nwid;
if (ioctl(s, SIOCG80211NWID, &ifr) < 0 ||
nwid.i_len > IEEE80211_NWID_LEN)
return -1;
os_memcpy(ssid, nwid.i_nwid, nwid.i_len);
return nwid.i_len;
#else
return bsd_get80211var(s, ifname, IEEE80211_IOC_SSID,
ssid, IEEE80211_NWID_LEN);
#endif
}
static int
bsd_set_ssid(int s, const char *ifname, const u8 *ssid, size_t ssid_len)
{
#ifdef SIOCS80211NWID
struct ieee80211_nwid nwid;
struct ifreq ifr;
os_memcpy(nwid.i_nwid, ssid, ssid_len);
nwid.i_len = ssid_len;
os_memset(&ifr, 0, sizeof(ifr));
os_strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ifr.ifr_data = (void *)&nwid;
return ioctl(s, SIOCS80211NWID, &ifr);
#else
return bsd_set80211var(s, ifname, IEEE80211_IOC_SSID, ssid, ssid_len);
#endif
}
static int
bsd_get_if_media(int s, const char *ifname)
{
struct ifmediareq ifmr;
os_memset(&ifmr, 0, sizeof(ifmr));
os_strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, &ifmr) < 0) {
wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__,
strerror(errno));
return -1;
}
return ifmr.ifm_current;
}
static int
bsd_set_if_media(int s, const char *ifname, int media)
{
struct ifreq ifr;
os_memset(&ifr, 0, sizeof(ifr));
os_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ifr.ifr_media = media;
if (ioctl(s, SIOCSIFMEDIA, &ifr) < 0) {
wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__,
strerror(errno));
return -1;
}
return 0;
}
static int
bsd_set_mediaopt(int s, const char *ifname, uint32_t mask, uint32_t mode)
{
int media = bsd_get_if_media(s, ifname);
if (media < 0)
return -1;
media &= ~mask;
media |= mode;
if (bsd_set_if_media(s, ifname, media) < 0)
return -1;
return 0;
}
static int
bsd_del_key(int s, const char *ifname, const u8 *addr, int key_idx)
{
struct ieee80211req_del_key wk;
os_memset(&wk, 0, sizeof(wk));
if (addr == NULL) {
wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx);
wk.idk_keyix = key_idx;
} else {
wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__,
MAC2STR(addr));
os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
}
return bsd_set80211var(s, ifname, IEEE80211_IOC_DELKEY, &wk,
sizeof(wk));
}
static int
bsd_send_mlme_param(int s, const char *ifname, const u8 op, const u16 reason,
const u8 *addr)
{
struct ieee80211req_mlme mlme;
os_memset(&mlme, 0, sizeof(mlme));
mlme.im_op = op;
mlme.im_reason = reason;
os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
return bsd_set80211var(s, ifname, IEEE80211_IOC_MLME, &mlme,
sizeof(mlme));
}
static int
bsd_ctrl_iface(int s, const char *ifname, int enable)
{
struct ifreq ifr;
os_memset(&ifr, 0, sizeof(ifr));
os_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
perror("ioctl[SIOCGIFFLAGS]");
return -1;
}
if (enable)
ifr.ifr_flags |= IFF_UP;
else
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
perror("ioctl[SIOCSIFFLAGS]");
return -1;
}
return 0;
}
static int
bsd_set_key(int s, const char *ifname, enum wpa_alg alg,
const unsigned char *addr, int key_idx, int set_tx, const u8 *seq,
size_t seq_len, const u8 *key, size_t key_len)
{
struct ieee80211req_key wk;
wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
"seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx,
set_tx, seq_len, key_len);
os_memset(&wk, 0, sizeof(wk));
switch (alg) {
case WPA_ALG_WEP:
wk.ik_type = IEEE80211_CIPHER_WEP;
break;
case WPA_ALG_TKIP:
wk.ik_type = IEEE80211_CIPHER_TKIP;
break;
case WPA_ALG_CCMP:
wk.ik_type = IEEE80211_CIPHER_AES_CCM;
break;
default:
wpa_printf(MSG_ERROR, "%s: unknown alg=%d", __func__, alg);
return -1;
}
wk.ik_flags = IEEE80211_KEY_RECV;
if (set_tx)
wk.ik_flags |= IEEE80211_KEY_XMIT;
if (addr == NULL) {
os_memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
wk.ik_keyix = key_idx;
} else {
os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
/*
* Deduce whether group/global or unicast key by checking
* the address (yech). Note also that we can only mark global
* keys default; doing this for a unicast key is an error.
*/
if (os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
IEEE80211_ADDR_LEN) == 0) {
wk.ik_flags |= IEEE80211_KEY_GROUP;
wk.ik_keyix = key_idx;
} else {
wk.ik_keyix = key_idx == 0 ? IEEE80211_KEYIX_NONE :
key_idx;
}
}
if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx)
wk.ik_flags |= IEEE80211_KEY_DEFAULT;
wk.ik_keylen = key_len;
os_memcpy(&wk.ik_keyrsc, seq, seq_len);
os_memcpy(wk.ik_keydata, key, key_len);
return bsd_set80211var(s, ifname, IEEE80211_IOC_WPAKEY, &wk,
sizeof(wk));
}
static int
bsd_configure_wpa(int s, const char *ifname, struct wpa_bss_params *params)
{
static const char *ciphernames[] =
{ "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" };
int v;
switch (params->wpa_group) {
case WPA_CIPHER_CCMP:
v = IEEE80211_CIPHER_AES_CCM;
break;
case WPA_CIPHER_TKIP:
v = IEEE80211_CIPHER_TKIP;
break;
case WPA_CIPHER_WEP104:
v = IEEE80211_CIPHER_WEP;
break;
case WPA_CIPHER_WEP40:
v = IEEE80211_CIPHER_WEP;
break;
case WPA_CIPHER_NONE:
v = IEEE80211_CIPHER_NONE;
break;
default:
printf("Unknown group key cipher %u\n",
params->wpa_group);
return -1;
}
wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
__func__, ciphernames[v], v);
if (bsd_set80211param(s, ifname, IEEE80211_IOC_MCASTCIPHER, v)) {
printf("Unable to set group key cipher to %u (%s)\n",
v, ciphernames[v]);
return -1;
}
if (v == IEEE80211_CIPHER_WEP) {
/* key length is done only for specific ciphers */
v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
if (bsd_set80211param(s, ifname, IEEE80211_IOC_MCASTKEYLEN,
v)) {
printf("Unable to set group key length to %u\n", v);
return -1;
}
}
v = 0;
if (params->wpa_pairwise & WPA_CIPHER_CCMP)
v |= 1<<IEEE80211_CIPHER_AES_CCM;
if (params->wpa_pairwise & WPA_CIPHER_TKIP)
v |= 1<<IEEE80211_CIPHER_TKIP;
if (params->wpa_pairwise & WPA_CIPHER_NONE)
v |= 1<<IEEE80211_CIPHER_NONE;
wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
if (bsd_set80211param(s, ifname, IEEE80211_IOC_UCASTCIPHERS, v)) {
printf("Unable to set pairwise key ciphers to 0x%x\n", v);
return -1;
}
wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x",
__func__, params->wpa_key_mgmt);
if (bsd_set80211param(s, ifname, IEEE80211_IOC_KEYMGTALGS, params->wpa_key_mgmt))
{
printf("Unable to set key management algorithms to 0x%x\n",
params->wpa_key_mgmt);
return -1;
}
v = 0;
if (params->rsn_preauth)
v |= BIT(0);
wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
__func__, params->rsn_preauth);
if (bsd_set80211param(s, ifname, IEEE80211_IOC_RSNCAPS, v)) {
printf("Unable to set RSN capabilities to 0x%x\n", v);
return -1;
}
wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa);
if (bsd_set80211param(s, ifname, IEEE80211_IOC_WPA, params->wpa)) {
printf("Unable to set WPA to %u\n", params->wpa);
return -1;
}
return 0;
}
static int
bsd_set_ieee8021x(int s, const char *ifname, struct wpa_bss_params *params)
{
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled);
if (!params->enabled) {
/* XXX restore state */
return bsd_set80211param(s, ifname, IEEE80211_IOC_AUTHMODE,
IEEE80211_AUTH_AUTO);
}
if (!params->wpa && !params->ieee802_1x) {
wpa_printf(MSG_ERROR, "%s: No 802.1X or WPA enabled",
__func__);
return -1;
}
if (params->wpa && bsd_configure_wpa(s, ifname, params) != 0) {
wpa_printf(MSG_ERROR, "%s: Failed to configure WPA state",
__func__);
return -1;
}
if (bsd_set80211param(s, ifname, IEEE80211_IOC_AUTHMODE,
(params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) {
wpa_printf(MSG_ERROR, "%s: Failed to enable WPA/802.1X",
__func__);
return -1;
}
return bsd_ctrl_iface(s, ifname, 1);
}
static int
bsd_set_sta_authorized(int s, const char *ifname, const u8 *addr,
int total_flags, int flags_or, int flags_and)
{
int authorized = -1;
/* For now, only support setting Authorized flag */
if (flags_or & WPA_STA_AUTHORIZED)
authorized = 1;
if (!(flags_and & WPA_STA_AUTHORIZED))
authorized = 0;
if (authorized < 0)
return 0;
return bsd_send_mlme_param(s, ifname, authorized ?
IEEE80211_MLME_AUTHORIZE :
IEEE80211_MLME_UNAUTHORIZE, 0, addr);
}
static void
bsd_new_sta(int s, const char *ifname, void *ctx, u8 addr[IEEE80211_ADDR_LEN])
{
struct ieee80211req_wpaie ie;
int ielen = 0;
u8 *iebuf = NULL;
/*
* Fetch and validate any negotiated WPA/RSN parameters.
*/
memset(&ie, 0, sizeof(ie));
memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
if (bsd_get80211var(s, ifname, IEEE80211_IOC_WPAIE,
&ie, sizeof(ie)) < 0) {
printf("Failed to get WPA/RSN information element.\n");
goto no_ie;
}
iebuf = ie.wpa_ie;
ielen = ie.wpa_ie[1];
if (ielen == 0)
iebuf = NULL;
else
ielen += 2;
no_ie:
drv_event_assoc(ctx, addr, iebuf, ielen);
}
static int
bsd_send_eapol(struct l2_packet_data *sock_xmit, const u8 *addr,
const u8 *data, size_t data_len)
{
wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", data, data_len);
return l2_packet_send(sock_xmit, addr, ETH_P_EAPOL, data, data_len);
}
static int
bsd_set_freq(int s, const char *ifname, u16 channel)
{
#ifdef SIOCS80211CHANNEL
struct ieee80211chanreq creq;
u32 mode;
if (channel < 14)
mode = IFM_IEEE80211_11G;
else if (channel == 14)
mode = IFM_IEEE80211_11B;
else
mode = IFM_IEEE80211_11A;
if (bsd_set_mediaopt(s, ifname, IFM_MMASK, mode) < 0) {
wpa_printf(MSG_ERROR, "%s: failed to set modulation mode",
__func__);
return -1;
}
os_memset(&creq, 0, sizeof(creq));
os_strlcpy(creq.i_name, ifname, sizeof(creq.i_name));
creq.i_channel = channel;
return ioctl(s, SIOCS80211CHANNEL, &creq);
#else
wpa_printf(MSG_ERROR, "bsd_set_freq: SIOCS80211CHANNEL not "
"defined");
return -1;
#endif
}
#ifdef HOSTAPD
/*
* Avoid conflicts with hostapd definitions by undefining couple of defines
* from net80211 header files.
*/
#undef RSN_VERSION
#undef WPA_VERSION
#undef WPA_OUI_TYPE
struct bsd_driver_data {
struct hostapd_data *hapd; /* back pointer */
char iface[IFNAMSIZ + 1];
struct l2_packet_data *sock_xmit; /* raw packet xmit socket */
int ioctl_sock; /* socket for ioctl() use */
int wext_sock; /* socket for wireless events */
};
static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
int reason_code);
static int
get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len)
{
return bsd_get80211var(drv->ioctl_sock, drv->iface, op, arg, arg_len);
}
static int
set80211param(struct bsd_driver_data *drv, int op, int arg)
{
return bsd_set80211param(drv->ioctl_sock, drv->iface, op, arg);
}
static const char *
ether_sprintf(const u8 *addr)
{
static char buf[sizeof(MACSTR)];
if (addr != NULL)
snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
else
snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0);
return buf;
}
static int
hostapd_bsd_ctrl_iface(struct bsd_driver_data *drv, int enable)
{
return bsd_ctrl_iface(drv->ioctl_sock, drv->iface, enable);
}
static int
bsd_set_privacy(const char *ifname, void *priv, int enabled)
{
struct bsd_driver_data *drv = priv;
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
return set80211param(drv, IEEE80211_IOC_PRIVACY, enabled);
}
static int
hostapd_bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags,
int flags_or, int flags_and)
{
struct bsd_driver_data *drv = priv;
return bsd_set_sta_authorized(drv->ioctl_sock, drv->iface, addr,
total_flags, flags_or, flags_and);
}
static int
hostapd_bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
const u8 *addr, int key_idx, int set_tx, const u8 *seq,
size_t seq_len, const u8 *key, size_t key_len)
{
struct bsd_driver_data *drv = priv;
if (alg == WPA_ALG_NONE)
return bsd_del_key(drv->ioctl_sock, drv->iface, addr, key_idx);
return bsd_set_key(drv->ioctl_sock, drv->iface, alg, addr, key_idx,
set_tx, seq, seq_len, key, key_len);
}
static int
bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
u8 *seq)
{
struct bsd_driver_data *drv = priv;
struct ieee80211req_key wk;
wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d",
__func__, ether_sprintf(addr), idx);
memset(&wk, 0, sizeof(wk));
if (addr == NULL)
memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
else
memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
wk.ik_keyix = idx;
if (get80211var(drv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
printf("Failed to get encryption.\n");
return -1;
}
#ifdef WORDS_BIGENDIAN
{
/*
* wk.ik_keytsc is in host byte order (big endian), need to
* swap it to match with the byte order used in WPA.
*/
int i;
u8 tmp[WPA_KEY_RSC_LEN];
memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
for (i = 0; i < WPA_KEY_RSC_LEN; i++) {
seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1];
}
}
#else /* WORDS_BIGENDIAN */
memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc));
#endif /* WORDS_BIGENDIAN */
return 0;
}
static int
bsd_flush(void *priv)
{
u8 allsta[IEEE80211_ADDR_LEN];
memset(allsta, 0xff, IEEE80211_ADDR_LEN);
return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE);
}
static int
bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data,
const u8 *addr)
{
struct bsd_driver_data *drv = priv;
struct ieee80211req_sta_stats stats;
memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN);
if (get80211var(drv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) > 0) {
/* XXX? do packets counts include non-data frames? */
data->rx_packets = stats.is_stats.ns_rx_data;
data->rx_bytes = stats.is_stats.ns_rx_bytes;
data->tx_packets = stats.is_stats.ns_tx_data;
data->tx_bytes = stats.is_stats.ns_tx_bytes;
}
return 0;
}
static int
bsd_set_opt_ie(const char *ifname, void *priv, const u8 *ie, size_t ie_len)
{
/*
* Do nothing; we setup parameters at startup that define the
* contents of the beacon information element.
*/
return 0;
}
static int
bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code)
{
struct bsd_driver_data *drv = priv;
return bsd_send_mlme_param(drv->ioctl_sock, drv->iface,
IEEE80211_MLME_DEAUTH, reason_code, addr);
}
static int
bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
int reason_code)
{
struct bsd_driver_data *drv = priv;
return bsd_send_mlme_param(drv->ioctl_sock, drv->iface,
IEEE80211_MLME_DISASSOC, reason_code, addr);
}
static void
bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
{
struct bsd_driver_data *drv = ctx;
char buf[2048];
struct if_announcemsghdr *ifan;
struct rt_msghdr *rtm;
struct ieee80211_michael_event *mic;
struct ieee80211_join_event *join;
struct ieee80211_leave_event *leave;
int n;
union wpa_event_data data;
n = read(sock, buf, sizeof(buf));
if (n < 0) {
if (errno != EINTR && errno != EAGAIN)
perror("read(PF_ROUTE)");
return;
}
rtm = (struct rt_msghdr *) buf;
if (rtm->rtm_version != RTM_VERSION) {
wpa_printf(MSG_DEBUG, "Routing message version %d not "
"understood\n", rtm->rtm_version);
return;
}
ifan = (struct if_announcemsghdr *) rtm;
switch (rtm->rtm_type) {
case RTM_IEEE80211:
switch (ifan->ifan_what) {
case RTM_IEEE80211_ASSOC:
case RTM_IEEE80211_REASSOC:
case RTM_IEEE80211_DISASSOC:
case RTM_IEEE80211_SCAN:
break;
case RTM_IEEE80211_LEAVE:
leave = (struct ieee80211_leave_event *) &ifan[1];
drv_event_disassoc(drv->hapd, leave->iev_addr);
break;
case RTM_IEEE80211_JOIN:
#ifdef RTM_IEEE80211_REJOIN
case RTM_IEEE80211_REJOIN:
#endif
join = (struct ieee80211_join_event *) &ifan[1];
bsd_new_sta(drv->ioctl_sock, drv->iface, drv->hapd,
join->iev_addr);
break;
case RTM_IEEE80211_REPLAY:
/* ignore */
break;
case RTM_IEEE80211_MICHAEL:
mic = (struct ieee80211_michael_event *) &ifan[1];
wpa_printf(MSG_DEBUG,
"Michael MIC failure wireless event: "
"keyix=%u src_addr=" MACSTR, mic->iev_keyix,
MAC2STR(mic->iev_src));
os_memset(&data, 0, sizeof(data));
data.michael_mic_failure.unicast = 1;
data.michael_mic_failure.src = mic->iev_src;
wpa_supplicant_event(drv->hapd,
EVENT_MICHAEL_MIC_FAILURE, &data);
break;
}
break;
}
}
static int
bsd_wireless_event_init(struct bsd_driver_data *drv)
{
int s;
drv->wext_sock = -1;
s = socket(PF_ROUTE, SOCK_RAW, 0);
if (s < 0) {
perror("socket(PF_ROUTE,SOCK_RAW)");
return -1;
}
eloop_register_read_sock(s, bsd_wireless_event_receive, drv, NULL);
drv->wext_sock = s;
return 0;
}
static void
bsd_wireless_event_deinit(struct bsd_driver_data *drv)
{
if (drv->wext_sock < 0)
return;
eloop_unregister_read_sock(drv->wext_sock);
close(drv->wext_sock);
}
static int
hostapd_bsd_send_eapol(void *priv, const u8 *addr, const u8 *data,
size_t data_len, int encrypt, const u8 *own_addr)
{
struct bsd_driver_data *drv = priv;
return bsd_send_eapol(drv->sock_xmit, addr, data, data_len);
}
static void
handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
{
struct bsd_driver_data *drv = ctx;
drv_event_eapol_rx(drv->hapd, src_addr, buf, len);
}
static int
hostapd_bsd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
{
struct bsd_driver_data *drv = priv;
int ssid_len;
ssid_len = bsd_get_ssid(drv->ioctl_sock, drv->iface, buf);
wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, ssid_len, buf);
return ssid_len;
}
static int
hostapd_bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
{
struct bsd_driver_data *drv = priv;
wpa_printf(MSG_DEBUG, "%s: ssid=\"%.*s\"", __func__, len, buf);
return bsd_set_ssid(drv->ioctl_sock, drv->iface, buf, len);
}
static int
hostapd_bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params)
{
struct bsd_driver_data *drv = priv;
return bsd_set_ieee8021x(drv->ioctl_sock, drv->iface, params);
}
static int
hostapd_bsd_set_freq(void *priv, struct hostapd_freq_params *freq)
{
struct bsd_driver_data *drv = priv;
return bsd_set_freq(drv->ioctl_sock, drv->iface, freq->channel);
}
static void *
bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
{
struct bsd_driver_data *drv;
drv = os_zalloc(sizeof(struct bsd_driver_data));
if (drv == NULL) {
printf("Could not allocate memory for bsd driver data\n");
goto bad;
}
drv->hapd = hapd;
drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
if (drv->ioctl_sock < 0) {
perror("socket[PF_INET,SOCK_DGRAM]");
goto bad;
}
memcpy(drv->iface, params->ifname, sizeof(drv->iface));
drv->sock_xmit = l2_packet_init(drv->iface, NULL, ETH_P_EAPOL,
handle_read, drv, 0);
if (drv->sock_xmit == NULL)
goto bad;
if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
goto bad;
/* mark down during setup */
if (hostapd_bsd_ctrl_iface(drv, 0) < 0)
goto bad;
if (bsd_wireless_event_init(drv))
goto bad;
if (bsd_set_mediaopt(drv->ioctl_sock, drv->iface, IFM_OMASK,
IFM_IEEE80211_HOSTAP) < 0) {
wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
__func__);
goto bad;
}
return drv;
bad:
if (drv->sock_xmit != NULL)
l2_packet_deinit(drv->sock_xmit);
if (drv->ioctl_sock >= 0)
close(drv->ioctl_sock);
if (drv != NULL)
os_free(drv);
return NULL;
}
static void
bsd_deinit(void *priv)
{
struct bsd_driver_data *drv = priv;
bsd_wireless_event_deinit(drv);
hostapd_bsd_ctrl_iface(drv, 0);
if (drv->ioctl_sock >= 0)
close(drv->ioctl_sock);
if (drv->sock_xmit != NULL)
l2_packet_deinit(drv->sock_xmit);
os_free(drv);
}
const struct wpa_driver_ops wpa_driver_bsd_ops = {
.name = "bsd",
.hapd_init = bsd_init,
.hapd_deinit = bsd_deinit,
.set_ieee8021x = hostapd_bsd_set_ieee8021x,
.set_privacy = bsd_set_privacy,
.set_key = hostapd_bsd_set_key,
.get_seqnum = bsd_get_seqnum,
.flush = bsd_flush,
.set_generic_elem = bsd_set_opt_ie,
.sta_set_flags = hostapd_bsd_sta_set_flags,
.read_sta_data = bsd_read_sta_driver_data,
.hapd_send_eapol = hostapd_bsd_send_eapol,
.sta_disassoc = bsd_sta_disassoc,
.sta_deauth = bsd_sta_deauth,
.hapd_set_ssid = hostapd_bsd_set_ssid,
.hapd_get_ssid = hostapd_bsd_get_ssid,
.set_freq = hostapd_bsd_set_freq,
};
#else /* HOSTAPD */
struct wpa_driver_bsd_data {
int sock; /* open socket for 802.11 ioctls */
struct l2_packet_data *sock_xmit;/* raw packet xmit socket */
int route; /* routing socket for events */
char ifname[IFNAMSIZ+1]; /* interface name */
unsigned int ifindex; /* interface index */
void *ctx;
struct wpa_driver_capa capa; /* driver capability */
int is_ap; /* Access point mode */
int prev_roaming; /* roaming state to restore on deinit */
int prev_privacy; /* privacy state to restore on deinit */
int prev_wpa; /* wpa state to restore on deinit */
};
static int
set80211var(struct wpa_driver_bsd_data *drv, int op, const void *arg, int arg_len)
{
return bsd_set80211var(drv->sock, drv->ifname, op, arg, arg_len);
}
static int
get80211var(struct wpa_driver_bsd_data *drv, int op, void *arg, int arg_len)
{
return bsd_get80211var(drv->sock, drv->ifname, op, arg, arg_len);
}
static int
set80211param(struct wpa_driver_bsd_data *drv, int op, int arg)
{
return bsd_set80211param(drv->sock, drv->ifname, op, arg);
}
static int
get80211param(struct wpa_driver_bsd_data *drv, int op)
{
struct ieee80211req ireq;
os_memset(&ireq, 0, sizeof(ireq));
os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name));
ireq.i_type = op;
if (ioctl(drv->sock, SIOCG80211, &ireq) < 0) {
fprintf(stderr, "ioctl[SIOCG80211, op %u]: %s\n",
op, strerror(errno));
return -1;
}
return ireq.i_val;
}
static int
wpa_driver_bsd_get_bssid(void *priv, u8 *bssid)
{
struct wpa_driver_bsd_data *drv = priv;
#ifdef SIOCG80211BSSID
struct ieee80211_bssid bs;
os_strncpy(bs.i_name, drv->ifname, sizeof(bs.i_name));
if (ioctl(drv->sock, SIOCG80211BSSID, &bs) < 0)
return -1;
os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid));
return 0;
#else
return get80211var(drv, IEEE80211_IOC_BSSID,
bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0;
#endif
}
#if 0
static int
wpa_driver_bsd_set_bssid(void *priv, const char *bssid)
{
struct wpa_driver_bsd_data *drv = priv;
return set80211var(drv, IEEE80211_IOC_BSSID,
bssid, IEEE80211_ADDR_LEN);
}
#endif
static int
wpa_driver_bsd_get_ssid(void *priv, u8 *ssid)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_get_ssid(drv->sock, drv->ifname, ssid);
}
static int
wpa_driver_bsd_set_ssid(void *priv, const u8 *ssid,
size_t ssid_len)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_set_ssid(drv->sock, drv->ifname, ssid, ssid_len);
}
static int
wpa_driver_bsd_set_wpa_ie(struct wpa_driver_bsd_data *drv,
const u8 *wpa_ie, size_t wpa_ie_len)
{
#ifdef IEEE80211_IOC_APPIE
return set80211var(drv, IEEE80211_IOC_APPIE, wpa_ie, wpa_ie_len);
#else /* IEEE80211_IOC_APPIE */
return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len);
#endif /* IEEE80211_IOC_APPIE */
}
static int
wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy)
{
struct wpa_driver_bsd_data *drv = priv;
int ret = 0;
wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d",
__FUNCTION__, wpa, privacy);
if (!wpa && wpa_driver_bsd_set_wpa_ie(drv, NULL, 0) < 0)
ret = -1;
if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0)
ret = -1;
if (set80211param(drv, IEEE80211_IOC_WPA, wpa) < 0)
ret = -1;
return ret;
}
static int
wpa_driver_bsd_set_wpa(void *priv, int enabled)
{
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled);
}
static int
wpa_driver_bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
const unsigned char *addr, int key_idx, int set_tx,
const u8 *seq, size_t seq_len,
const u8 *key, size_t key_len)
{
struct wpa_driver_bsd_data *drv = priv;
if (alg == WPA_ALG_NONE) {
if (addr == NULL ||
os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
IEEE80211_ADDR_LEN) == 0)
return bsd_del_key(drv->sock, drv->ifname, NULL,
key_idx);
else
return bsd_del_key(drv->sock, drv->ifname, addr,
key_idx);
}
return bsd_set_key(drv->sock, drv->ifname, alg, addr, key_idx, set_tx,
seq, seq_len, key, key_len);
}
static int
wpa_driver_bsd_set_countermeasures(void *priv, int enabled)
{
struct wpa_driver_bsd_data *drv = priv;
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
return set80211param(drv, IEEE80211_IOC_COUNTERMEASURES, enabled);
}
static int
wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled)
{
struct wpa_driver_bsd_data *drv = priv;
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
return set80211param(drv, IEEE80211_IOC_DROPUNENCRYPTED, enabled);
}
static int
wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_send_mlme_param(drv->sock, drv->ifname,
IEEE80211_MLME_DEAUTH, reason_code, addr);
}
static int
wpa_driver_bsd_disassociate(void *priv, const u8 *addr, int reason_code)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_send_mlme_param(drv->sock, drv->ifname,
IEEE80211_MLME_DISASSOC, reason_code, addr);
}
static int
wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg)
{
struct wpa_driver_bsd_data *drv = priv;
int authmode;
if ((auth_alg & WPA_AUTH_ALG_OPEN) &&
(auth_alg & WPA_AUTH_ALG_SHARED))
authmode = IEEE80211_AUTH_AUTO;
else if (auth_alg & WPA_AUTH_ALG_SHARED)
authmode = IEEE80211_AUTH_SHARED;
else
authmode = IEEE80211_AUTH_OPEN;
return set80211param(drv, IEEE80211_IOC_AUTHMODE, authmode);
}
static void
handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
{
struct wpa_driver_bsd_data *drv = ctx;
drv_event_eapol_rx(drv->ctx, src_addr, buf, len);
}
static int
wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
{
struct wpa_driver_bsd_data *drv = priv;
struct ieee80211req_mlme mlme;
u32 mode;
u16 channel;
int privacy;
int ret = 0;
wpa_printf(MSG_DEBUG,
"%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u"
, __func__
, (unsigned int) params->ssid_len, params->ssid
, (unsigned int) params->wpa_ie_len
, params->pairwise_suite
, params->group_suite
, params->key_mgmt_suite
);
switch (params->mode) {
case IEEE80211_MODE_INFRA:
mode = 0 /* STA */;
break;
case IEEE80211_MODE_IBSS:
mode = IFM_IEEE80211_IBSS;
break;
case IEEE80211_MODE_AP:
mode = IFM_IEEE80211_HOSTAP;
break;
default:
wpa_printf(MSG_ERROR, "%s: unknown operation mode", __func__);
return -1;
}
if (bsd_set_mediaopt(drv->sock, drv->ifname, IFM_OMASK, mode) < 0) {
wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
__func__);
return -1;
}
if (params->mode == IEEE80211_MODE_AP) {
if (params->freq >= 2412 && params->freq <= 2472)
channel = (params->freq - 2407) / 5;
else if (params->freq == 2484)
channel = 14;
else if ((params->freq >= 5180 && params->freq <= 5240) ||
(params->freq >= 5745 && params->freq <= 5825))
channel = (params->freq - 5000) / 5;
else
channel = 0;
if (bsd_set_freq(drv->sock, drv->ifname, channel) < 0)
return -1;
drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL,
handle_read, drv, 0);
if (drv->sock_xmit == NULL)
return -1;
drv->is_ap = 1;
return 0;
}
if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted)
< 0)
ret = -1;
if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0)
ret = -1;
/* XXX error handling is wrong but unclear what to do... */
if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0)
return -1;
privacy = !(params->pairwise_suite == CIPHER_NONE &&
params->group_suite == CIPHER_NONE &&
params->key_mgmt_suite == KEY_MGMT_NONE &&
params->wpa_ie_len == 0);
wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy);
if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0)
return -1;
if (params->wpa_ie_len &&
set80211param(drv, IEEE80211_IOC_WPA,
params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0)
return -1;
os_memset(&mlme, 0, sizeof(mlme));
mlme.im_op = IEEE80211_MLME_ASSOC;
if (params->ssid != NULL)
os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len);
mlme.im_ssid_len = params->ssid_len;
if (params->bssid != NULL)
os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN);
if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0)
return -1;
return ret;
}
static int
wpa_driver_bsd_ctrl_iface(struct wpa_driver_bsd_data *drv, int enable)
{
return bsd_ctrl_iface(drv->sock, drv->ifname, enable);
}
static int
wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params)
{
struct wpa_driver_bsd_data *drv = priv;
const u8 *ssid = params->ssids[0].ssid;
size_t ssid_len = params->ssids[0].ssid_len;
if (set80211param(drv, IEEE80211_IOC_ROAMING,
IEEE80211_ROAMING_MANUAL) < 0) {
wpa_printf(MSG_ERROR, "%s: failed to set "
"wpa_supplicant-based roaming: %s", __func__,
strerror(errno));
return -1;
}
if (wpa_driver_bsd_set_wpa(drv, 1) < 0) {
wpa_printf(MSG_ERROR, "%s: failed to set wpa: %s", __func__,
strerror(errno));
return -1;
}
/* NB: interface must be marked UP to do a scan */
if (wpa_driver_bsd_ctrl_iface(drv, 1) < 0)
return -1;
/* set desired ssid before scan */
if (wpa_driver_bsd_set_ssid(drv, ssid, ssid_len) < 0)
return -1;
/* NB: net80211 delivers a scan complete event so no need to poll */
return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0);
}
static void
wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
{
struct wpa_driver_bsd_data *drv = sock_ctx;
char buf[2048];
struct if_announcemsghdr *ifan;
struct if_msghdr *ifm;
struct rt_msghdr *rtm;
union wpa_event_data event;
struct ieee80211_michael_event *mic;
struct ieee80211_leave_event *leave;
struct ieee80211_join_event *join;
int n;
n = read(sock, buf, sizeof(buf));
if (n < 0) {
if (errno != EINTR && errno != EAGAIN)
perror("read(PF_ROUTE)");
return;
}
rtm = (struct rt_msghdr *) buf;
if (rtm->rtm_version != RTM_VERSION) {
wpa_printf(MSG_DEBUG, "Routing message version %d not "
"understood\n", rtm->rtm_version);
return;
}
os_memset(&event, 0, sizeof(event));
switch (rtm->rtm_type) {
case RTM_IFANNOUNCE:
ifan = (struct if_announcemsghdr *) rtm;
if (ifan->ifan_index != drv->ifindex)
break;
strlcpy(event.interface_status.ifname, drv->ifname,
sizeof(event.interface_status.ifname));
switch (ifan->ifan_what) {
case IFAN_DEPARTURE:
event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
default:
return;
}
wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s",
event.interface_status.ifname,
ifan->ifan_what == IFAN_DEPARTURE ?
"removed" : "added");
wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
break;
case RTM_IEEE80211:
ifan = (struct if_announcemsghdr *) rtm;
if (ifan->ifan_index != drv->ifindex)
break;
switch (ifan->ifan_what) {
case RTM_IEEE80211_ASSOC:
case RTM_IEEE80211_REASSOC:
if (drv->is_ap)
break;
wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
break;
case RTM_IEEE80211_DISASSOC:
if (drv->is_ap)
break;
wpa_supplicant_event(ctx, EVENT_DISASSOC, NULL);
break;
case RTM_IEEE80211_SCAN:
if (drv->is_ap)
break;
wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL);
break;
case RTM_IEEE80211_LEAVE:
leave = (struct ieee80211_leave_event *) &ifan[1];
drv_event_disassoc(ctx, leave->iev_addr);
break;
case RTM_IEEE80211_JOIN:
#ifdef RTM_IEEE80211_REJOIN
case RTM_IEEE80211_REJOIN:
#endif
join = (struct ieee80211_join_event *) &ifan[1];
bsd_new_sta(drv->sock, drv->ifname, ctx,
join->iev_addr);
break;
case RTM_IEEE80211_REPLAY:
/* ignore */
break;
case RTM_IEEE80211_MICHAEL:
mic = (struct ieee80211_michael_event *) &ifan[1];
wpa_printf(MSG_DEBUG,
"Michael MIC failure wireless event: "
"keyix=%u src_addr=" MACSTR, mic->iev_keyix,
MAC2STR(mic->iev_src));
os_memset(&event, 0, sizeof(event));
event.michael_mic_failure.unicast =
!IEEE80211_IS_MULTICAST(mic->iev_dst);
wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE,
&event);
break;
}
break;
case RTM_IFINFO:
ifm = (struct if_msghdr *) rtm;
if (ifm->ifm_index != drv->ifindex)
break;
if ((rtm->rtm_flags & RTF_UP) == 0) {
strlcpy(event.interface_status.ifname, drv->ifname,
sizeof(event.interface_status.ifname));
event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN",
event.interface_status.ifname);
wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
}
break;
}
}
static void
wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res,
struct ieee80211req_scan_result *sr)
{
struct wpa_scan_res *result, **tmp;
size_t extra_len;
u8 *pos;
extra_len = 2 + sr->isr_ssid_len;
extra_len += 2 + sr->isr_nrates;
extra_len += 3; /* ERP IE */
extra_len += sr->isr_ie_len;
result = os_zalloc(sizeof(*result) + extra_len);
if (result == NULL)
return;
os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN);
result->freq = sr->isr_freq;
result->beacon_int = sr->isr_intval;
result->caps = sr->isr_capinfo;
result->qual = sr->isr_rssi;
result->noise = sr->isr_noise;
pos = (u8 *)(result + 1);
*pos++ = WLAN_EID_SSID;
*pos++ = sr->isr_ssid_len;
os_memcpy(pos, sr + 1, sr->isr_ssid_len);
pos += sr->isr_ssid_len;
/*
* Deal all rates as supported rate.
* Because net80211 doesn't report extended supported rate or not.
*/
*pos++ = WLAN_EID_SUPP_RATES;
*pos++ = sr->isr_nrates;
os_memcpy(pos, sr->isr_rates, sr->isr_nrates);
pos += sr->isr_nrates;
*pos++ = WLAN_EID_ERP_INFO;
*pos++ = 1;
*pos++ = sr->isr_erp;
os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len);
pos += sr->isr_ie_len;
result->ie_len = pos - (u8 *)(result + 1);
tmp = os_realloc(res->res,
(res->num + 1) * sizeof(struct wpa_scan_res *));
if (tmp == NULL) {
os_free(result);
return;
}
tmp[res->num++] = result;
res->res = tmp;
}
struct wpa_scan_results *
wpa_driver_bsd_get_scan_results2(void *priv)
{
struct wpa_driver_bsd_data *drv = priv;
struct ieee80211req_scan_result *sr;
struct wpa_scan_results *res;
int len, rest;
uint8_t buf[24*1024], *pos;
len = get80211var(drv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024);
if (len < 0)
return NULL;
res = os_zalloc(sizeof(*res));
if (res == NULL)
return NULL;
pos = buf;
rest = len;
while (rest >= sizeof(struct ieee80211req_scan_result)) {
sr = (struct ieee80211req_scan_result *)pos;
wpa_driver_bsd_add_scan_entry(res, sr);
pos += sr->isr_len;
rest -= sr->isr_len;
}
wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)",
len, (unsigned long)res->num);
return res;
}
static int wpa_driver_bsd_capa(struct wpa_driver_bsd_data *drv)
{
/* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
WPA_DRIVER_CAPA_ENC_WEP104 |
WPA_DRIVER_CAPA_ENC_TKIP |
WPA_DRIVER_CAPA_ENC_CCMP;
drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
WPA_DRIVER_AUTH_SHARED |
WPA_DRIVER_AUTH_LEAP;
//drv->capa.max_scan_ssids = info.max_scan_ssids;
drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
return 0;
}
static void *
wpa_driver_bsd_init(void *ctx, const char *ifname)
{
#define GETPARAM(drv, param, v) \
(((v) = get80211param(drv, param)) != -1)
struct wpa_driver_bsd_data *drv;
drv = os_zalloc(sizeof(*drv));
if (drv == NULL)
return NULL;
/*
* NB: We require the interface name be mappable to an index.
* This implies we do not support having wpa_supplicant
* wait for an interface to appear. This seems ok; that
* doesn't belong here; it's really the job of devd.
*/
drv->ifindex = if_nametoindex(ifname);
if (drv->ifindex == 0) {
wpa_printf(MSG_DEBUG, "%s: interface %s does not exist",
__func__, ifname);
goto fail1;
}
drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
if (drv->sock < 0)
goto fail1;
drv->route = socket(PF_ROUTE, SOCK_RAW, 0);
if (drv->route < 0)
goto fail;
eloop_register_read_sock(drv->route,
wpa_driver_bsd_event_receive, ctx, drv);
drv->ctx = ctx;
os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
/* Down interface during setup. */
if (wpa_driver_bsd_ctrl_iface(drv, 0) < 0)
goto fail;
if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) {
wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s",
__func__, strerror(errno));
goto fail;
}
if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) {
wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s",
__func__, strerror(errno));
goto fail;
}
if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) {
wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s",
__func__, strerror(errno));
goto fail;
}
if (bsd_set_mediaopt(drv->sock, drv->ifname, IFM_OMASK,
0 /* STA */) < 0) {
wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
__func__);
goto fail;
}
if (wpa_driver_bsd_capa(drv))
goto fail;
return drv;
fail:
close(drv->sock);
fail1:
os_free(drv);
return NULL;
#undef GETPARAM
}
static void
wpa_driver_bsd_deinit(void *priv)
{
struct wpa_driver_bsd_data *drv = priv;
wpa_driver_bsd_set_wpa(drv, 0);
eloop_unregister_read_sock(drv->route);
/* NB: mark interface down */
wpa_driver_bsd_ctrl_iface(drv, 0);
wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy);
if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0)
wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state",
__func__);
if (drv->sock_xmit != NULL)
l2_packet_deinit(drv->sock_xmit);
(void) close(drv->route); /* ioctl socket */
(void) close(drv->sock); /* event socket */
os_free(drv);
}
static int
wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa)
{
struct wpa_driver_bsd_data *drv = priv;
os_memcpy(capa, &drv->capa, sizeof(*capa));
return 0;
}
static int
wpa_driver_bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_set_ieee8021x(drv->sock, drv->ifname, params);
}
static int
wpa_driver_bsd_hapd_set_ssid(const char *ifname, void *priv, const u8 *buf,
int len)
{
return wpa_driver_bsd_set_ssid(priv, buf, len);
}
static int
wpa_driver_bsd_hapd_get_ssid(const char *ifname, void *priv, u8 *buf, int len)
{
return wpa_driver_bsd_get_ssid(priv, buf);
}
static int
wpa_driver_bsd_send_eapol(void *priv, const u8 *addr, const u8 *data,
size_t data_len, int encrypt, const u8 *own_addr)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_send_eapol(drv->sock_xmit, addr, data, data_len);
}
static int
wpa_driver_bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags,
int flags_or, int flags_and)
{
struct wpa_driver_bsd_data *drv = priv;
return bsd_set_sta_authorized(drv->sock, drv->ifname, addr,
total_flags, flags_or, flags_and);
}
const struct wpa_driver_ops wpa_driver_bsd_ops = {
.name = "bsd",
.desc = "BSD 802.11 support",
.init = wpa_driver_bsd_init,
.deinit = wpa_driver_bsd_deinit,
.get_bssid = wpa_driver_bsd_get_bssid,
.get_ssid = wpa_driver_bsd_get_ssid,
.set_key = wpa_driver_bsd_set_key,
.set_countermeasures = wpa_driver_bsd_set_countermeasures,
.scan2 = wpa_driver_bsd_scan,
.get_scan_results2 = wpa_driver_bsd_get_scan_results2,
.deauthenticate = wpa_driver_bsd_deauthenticate,
.disassociate = wpa_driver_bsd_disassociate,
.associate = wpa_driver_bsd_associate,
.get_capa = wpa_driver_bsd_get_capa,
.set_ieee8021x = wpa_driver_bsd_set_ieee8021x,
.hapd_set_ssid = wpa_driver_bsd_hapd_set_ssid,
.hapd_get_ssid = wpa_driver_bsd_hapd_get_ssid,
.hapd_send_eapol = wpa_driver_bsd_send_eapol,
.sta_set_flags = wpa_driver_bsd_sta_set_flags,
};
#endif /* HOSTAPD */