#include <sys/cdefs.h>
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/ethernet.h>
#include <net80211/ieee80211_var.h>
MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
static int _ieee80211_crypto_delkey(struct ieee80211vap *,
struct ieee80211_key *);
static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
static int
null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
{
if (!ieee80211_is_key_global(vap, k)) {
if (k->wk_flags & IEEE80211_KEY_GROUP)
return 0;
*keyix = 0;
} else {
*keyix = ieee80211_crypto_get_key_wepidx(vap, k);
}
*rxkeyix = IEEE80211_KEYIX_NONE;
return 1;
}
static int
null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
{
return 1;
}
static int
null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
{
return 1;
}
static void null_key_update(struct ieee80211vap *vap) {}
static __inline void
cipher_detach(struct ieee80211_key *key)
{
key->wk_cipher->ic_detach(key);
}
static __inline void *
cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
{
return key->wk_cipher->ic_attach(vap, key);
}
static __inline int
dev_key_alloc(struct ieee80211vap *vap,
struct ieee80211_key *key,
ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
{
return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
}
static __inline int
dev_key_delete(struct ieee80211vap *vap,
const struct ieee80211_key *key)
{
return vap->iv_key_delete(vap, key);
}
static __inline int
dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
{
return vap->iv_key_set(vap, key);
}
void
ieee80211_crypto_attach(struct ieee80211com *ic)
{
ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
ic->ic_sw_cryptocaps = IEEE80211_CRYPTO_WEP |
IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_AES_CCM;
ic->ic_sw_keymgmtcaps = 0;
}
void
ieee80211_crypto_detach(struct ieee80211com *ic)
{
}
void
ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *ic,
uint32_t cipher_set)
{
ic->ic_sw_cryptocaps = cipher_set;
}
void
ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *ic,
uint32_t cipher_set)
{
ic->ic_cryptocaps = cipher_set;
}
void
ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *ic,
uint32_t keymgmt_set)
{
ic->ic_sw_keymgmtcaps = keymgmt_set;
}
void
ieee80211_crypto_vattach(struct ieee80211vap *vap)
{
int i;
vap->iv_max_keyix = IEEE80211_WEP_NKID;
vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
for (i = 0; i < IEEE80211_WEP_NKID; i++)
ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
IEEE80211_KEYIX_NONE);
vap->iv_key_alloc = null_key_alloc;
vap->iv_key_set = null_key_set;
vap->iv_key_delete = null_key_delete;
vap->iv_key_update_begin = null_key_update;
vap->iv_key_update_end = null_key_update;
}
void
ieee80211_crypto_vdetach(struct ieee80211vap *vap)
{
ieee80211_crypto_delglobalkeys(vap);
}
void
ieee80211_crypto_register(const struct ieee80211_cipher *cip)
{
if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
net80211_printf("%s: cipher %s has an invalid cipher index %u\n",
__func__, cip->ic_name, cip->ic_cipher);
return;
}
if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
net80211_printf("%s: cipher %s registered with a different template\n",
__func__, cip->ic_name);
return;
}
ciphers[cip->ic_cipher] = cip;
}
void
ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
{
if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
net80211_printf("%s: cipher %s has an invalid cipher index %u\n",
__func__, cip->ic_name, cip->ic_cipher);
return;
}
if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
net80211_printf("%s: cipher %s registered with a different template\n",
__func__, cip->ic_name);
return;
}
ciphers[cip->ic_cipher] = NULL;
}
int
ieee80211_crypto_available(u_int cipher)
{
return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
}
static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
[IEEE80211_CIPHER_WEP] = "wlan_wep",
[IEEE80211_CIPHER_TKIP] = "wlan_tkip",
[IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
[IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
[IEEE80211_CIPHER_TKIPMIC] = "#4",
[IEEE80211_CIPHER_CKIP] = "wlan_ckip",
[IEEE80211_CIPHER_NONE] = "wlan_none",
[IEEE80211_CIPHER_AES_CCM_256] = "wlan_ccmp",
[IEEE80211_CIPHER_BIP_CMAC_128] = "wlan_bip_cmac",
[IEEE80211_CIPHER_BIP_CMAC_256] = "wlan_bip_cmac",
[IEEE80211_CIPHER_BIP_GMAC_128] = "wlan_bip_gmac",
[IEEE80211_CIPHER_BIP_GMAC_256] = "wlan_bip_gmac",
[IEEE80211_CIPHER_AES_GCM_128] = "wlan_gcmp",
[IEEE80211_CIPHER_AES_GCM_256] = "wlan_gcmp",
};
CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
int
ieee80211_crypto_newkey(struct ieee80211vap *vap,
int cipher, int flags, struct ieee80211_key *key)
{
struct ieee80211com *ic = vap->iv_ic;
const struct ieee80211_cipher *cip;
ieee80211_keyix keyix, rxkeyix;
void *keyctx;
int oflags;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: cipher %u flags 0x%x keyix %u\n",
__func__, cipher, flags, key->wk_keyix);
if (cipher >= IEEE80211_CIPHER_MAX) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: invalid cipher %u\n", __func__, cipher);
vap->iv_stats.is_crypto_badcipher++;
return 0;
}
cip = ciphers[cipher];
if (cip == NULL) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: unregistered cipher %u, load module %s\n",
__func__, cipher, cipher_modnames[cipher]);
ieee80211_load_module(cipher_modnames[cipher]);
cip = ciphers[cipher];
if (cip == NULL) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: unable to load cipher %u, module %s\n",
__func__, cipher, cipher_modnames[cipher]);
vap->iv_stats.is_crypto_nocipher++;
return 0;
}
}
oflags = key->wk_flags;
flags &= IEEE80211_KEY_COMMON;
flags |= (oflags & IEEE80211_KEY_DEVICE);
if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: no h/w support for cipher %s, falling back to s/w\n",
__func__, cip->ic_name);
flags |= IEEE80211_KEY_SWCRYPT;
}
if ((flags & IEEE80211_KEY_SWCRYPT) &&
(ic->ic_sw_cryptocaps & (1<<cipher)) == 0) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: no s/w support for cipher %s, rejecting\n",
__func__, cip->ic_name);
vap->iv_stats.is_crypto_swcipherfail++;
return (0);
}
if (cipher == IEEE80211_CIPHER_TKIP &&
(ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: no h/w support for TKIP MIC, falling back to s/w\n",
__func__);
flags |= IEEE80211_KEY_SWMIC;
}
if (key->wk_cipher != cip || key->wk_flags != flags) {
key->wk_flags = flags;
keyctx = cip->ic_attach(vap, key);
if (keyctx == NULL) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: unable to attach cipher %s\n",
__func__, cip->ic_name);
key->wk_flags = oflags;
vap->iv_stats.is_crypto_attachfail++;
return 0;
}
cipher_detach(key);
key->wk_cipher = cip;
key->wk_private = keyctx;
}
if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
vap->iv_stats.is_crypto_keyfail++;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: unable to setup cipher %s\n",
__func__, cip->ic_name);
return 0;
}
if (key->wk_flags != flags) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: driver override for cipher %s, flags "
"%b -> %b\n", __func__, cip->ic_name,
oflags, IEEE80211_KEY_BITS,
key->wk_flags, IEEE80211_KEY_BITS);
keyctx = cip->ic_attach(vap, key);
if (keyctx == NULL) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: unable to attach cipher %s with "
"flags %b\n", __func__, cip->ic_name,
key->wk_flags, IEEE80211_KEY_BITS);
key->wk_flags = oflags;
vap->iv_stats.is_crypto_attachfail++;
return 0;
}
cipher_detach(key);
key->wk_cipher = cip;
key->wk_private = keyctx;
}
key->wk_keyix = keyix;
key->wk_rxkeyix = rxkeyix;
key->wk_flags |= IEEE80211_KEY_DEVKEY;
}
return 1;
}
static int
_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
{
KASSERT(key->wk_cipher != NULL, ("No cipher!"));
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: %s keyix %u flags %b rsc %ju tsc %ju len %u\n",
__func__, key->wk_cipher->ic_name,
key->wk_keyix, key->wk_flags, IEEE80211_KEY_BITS,
key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
key->wk_keylen);
if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
if (!dev_key_delete(vap, key)) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: driver did not delete key index %u\n",
__func__, key->wk_keyix);
vap->iv_stats.is_crypto_delkey++;
}
}
cipher_detach(key);
memset(key, 0, sizeof(*key));
ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
return 1;
}
int
ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
{
int status;
ieee80211_key_update_begin(vap);
status = _ieee80211_crypto_delkey(vap, key);
ieee80211_key_update_end(vap);
return status;
}
void
ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
{
int i;
ieee80211_key_update_begin(vap);
for (i = 0; i < IEEE80211_WEP_NKID; i++)
(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
ieee80211_key_update_end(vap);
}
int
ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
{
const struct ieee80211_cipher *cip = key->wk_cipher;
KASSERT(cip != NULL, ("No cipher!"));
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: %s keyix %u flags %b mac %s rsc %ju tsc %ju len %u\n",
__func__, cip->ic_name, key->wk_keyix,
key->wk_flags, IEEE80211_KEY_BITS, ether_sprintf(key->wk_macaddr),
key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
key->wk_keylen);
if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: no device key setup done; should not happen!\n",
__func__);
vap->iv_stats.is_crypto_setkey_nokey++;
return 0;
}
if (!cip->ic_setkey(key)) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
"%s: cipher %s rejected key index %u len %u flags %b\n",
__func__, cip->ic_name, key->wk_keyix,
key->wk_keylen, key->wk_flags, IEEE80211_KEY_BITS);
vap->iv_stats.is_crypto_setkey_cipher++;
return 0;
}
return dev_key_set(vap, key);
}
int
ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap,
const struct ieee80211_key *k)
{
if (ieee80211_is_key_global(vap, k)) {
return (k - vap->iv_nw_keys);
}
return (-1);
}
uint8_t
ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k)
{
if (ieee80211_is_key_global(vap, k)) {
return (k - vap->iv_nw_keys);
}
return (0);
}
struct ieee80211_key *
ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m)
{
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211_frame *wh;
wh = mtod(m, struct ieee80211_frame *);
if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
wh->i_addr1,
"no default transmit key (%s) deftxkey %u",
__func__, vap->iv_def_txkey);
vap->iv_stats.is_tx_nodefkey++;
return NULL;
}
return &vap->iv_nw_keys[vap->iv_def_txkey];
}
if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
return NULL;
return &ni->ni_ucastkey;
}
struct ieee80211_key *
ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
{
struct ieee80211_key *k;
const struct ieee80211_cipher *cip;
if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) {
cip = k->wk_cipher;
return (cip->ic_encap(k, m) ? k : NULL);
}
return NULL;
}
int
ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen,
struct ieee80211_key **key)
{
#define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
#define IEEE80211_WEP_MINLEN \
(sizeof(struct ieee80211_frame) + \
IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211_key *k;
struct ieee80211_frame *wh;
const struct ieee80211_rx_stats *rxs;
const struct ieee80211_cipher *cip;
uint8_t keyid;
rxs = ieee80211_get_rx_params_ptr(m);
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) {
*key = NULL;
return (1);
}
}
if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
"%s: WEP data frame too short, len %u\n",
__func__, m->m_pkthdr.len);
vap->iv_stats.is_rx_tooshort++;
*key = NULL;
return (0);
}
wh = mtod(m, struct ieee80211_frame *);
m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
k = &vap->iv_nw_keys[keyid >> 6];
else
k = &ni->ni_ucastkey;
cip = k->wk_cipher;
if (m->m_len < hdrlen + cip->ic_header) {
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
"frame is too short (%d < %u) for crypto decap",
cip->ic_name, m->m_len, hdrlen + cip->ic_header);
vap->iv_stats.is_rx_tooshort++;
*key = NULL;
return (0);
}
if (cip->ic_decap(k, m, hdrlen)) {
*key = k;
return (1);
}
*key = NULL;
return (0);
#undef IEEE80211_WEP_MINLEN
#undef IEEE80211_WEP_HDRLEN
}
int
ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
struct mbuf *m, int force)
{
const struct ieee80211_cipher *cip;
const struct ieee80211_rx_stats *rxs;
struct ieee80211_frame *wh;
rxs = ieee80211_get_rx_params_ptr(m);
wh = mtod(m, struct ieee80211_frame *);
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
if ((rxs->c_pktflags & IEEE80211_RX_F_FAIL_MMIC) != 0) {
ieee80211_notify_michael_failure(vap, wh,
IEEE80211_KEYIX_NONE);
return (0);
}
if ((rxs->c_pktflags &
(IEEE80211_RX_F_MIC_STRIP|IEEE80211_RX_F_MMIC_STRIP)) != 0) {
return (1);
}
}
if (k == NULL)
return (1);
cip = k->wk_cipher;
return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
}
static void
load_ucastkey(void *arg, struct ieee80211_node *ni)
{
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211_key *k;
if (vap->iv_state != IEEE80211_S_RUN)
return;
k = &ni->ni_ucastkey;
if (k->wk_flags & IEEE80211_KEY_DEVKEY)
dev_key_set(vap, k);
}
void
ieee80211_crypto_reload_keys(struct ieee80211com *ic)
{
struct ieee80211vap *vap;
int i;
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
if (vap->iv_state != IEEE80211_S_RUN)
continue;
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
const struct ieee80211_key *k = &vap->iv_nw_keys[i];
if (k->wk_flags & IEEE80211_KEY_DEVKEY)
dev_key_set(vap, k);
}
}
ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
}
void
ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
{
vap->iv_update_deftxkey(vap, kid);
}
uint16_t
ieee80211_crypto_init_aad(const struct ieee80211_frame *wh, uint8_t *aad,
int len)
{
uint16_t aad_len;
memset(aad, 0, len);
aad[0] = 0;
aad[2] = wh->i_fc[0] & 0x8f;
aad[3] = wh->i_fc[1] & 0xc7;
if (IEEE80211_IS_QOS_ANY(wh))
aad[3] &= 0x7f;
memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
aad[23] = 0;
if (IEEE80211_IS_DSTODS(wh)) {
IEEE80211_ADDR_COPY(aad + 24,
((const struct ieee80211_frame_addr4 *)wh)->i_addr4);
if (IEEE80211_IS_QOS_ANY(wh)) {
const struct ieee80211_qosframe_addr4 *qwh4 =
(const struct ieee80211_qosframe_addr4 *) wh;
aad[30] = qwh4->i_qos[0] & 0x0f;
aad[31] = 0;
aad_len = aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
} else {
*(uint16_t *)&aad[30] = 0;
aad_len = aad[1] = 22 + IEEE80211_ADDR_LEN;
}
} else {
if (IEEE80211_IS_QOS_ANY(wh)) {
const struct ieee80211_qosframe *qwh =
(const struct ieee80211_qosframe*) wh;
aad[24] = qwh->i_qos[0] & 0x0f;
aad[25] = 0;
aad_len = aad[1] = 22 + 2;
} else {
*(uint16_t *)&aad[24] = 0;
aad_len = aad[1] = 22;
}
*(uint16_t *)&aad[26] = 0;
*(uint32_t *)&aad[28] = 0;
}
return (aad_len);
}