#include <sys/cdefs.h>
#ifdef __FreeBSD__
__FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.12 2005/08/08 18:46:35 sam Exp $");
#endif
#ifdef __NetBSD__
__KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto.c,v 1.23 2018/05/08 07:02:07 maxv Exp $");
#endif
#ifdef _KERNEL_OPT
#include "opt_inet.h"
#endif
#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/if_arp.h>
#include <net/if_ether.h>
#include <net/if_llc.h>
#include <net80211/ieee80211_netbsd.h>
#include <net80211/ieee80211_var.h>
static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
#ifdef INET
#include <netinet/in.h>
#include <net/if_ether.h>
#endif
static int _ieee80211_crypto_delkey(struct ieee80211com *,
struct ieee80211_key *);
static int
null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
{
if (!(&ic->ic_nw_keys[0] <= k &&
k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
if (k->wk_flags & IEEE80211_KEY_GROUP)
return 0;
*keyix = 0;
} else {
*keyix = k - ic->ic_nw_keys;
}
*rxkeyix = IEEE80211_KEYIX_NONE;
return 1;
}
static int
null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
{
return 1;
}
static int
null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
const u_int8_t mac[IEEE80211_ADDR_LEN])
{
return 1;
}
static void
null_key_update(struct ieee80211com *ic)
{
;
}
static __inline void
cipher_detach(struct ieee80211_key *key)
{
key->wk_cipher->ic_detach(key);
}
static __inline int
dev_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *key,
ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
{
return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix);
}
static __inline int
dev_key_delete(struct ieee80211com *ic, const struct ieee80211_key *key)
{
return ic->ic_crypto.cs_key_delete(ic, key);
}
static __inline int
dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key,
const u_int8_t mac[IEEE80211_ADDR_LEN])
{
return ic->ic_crypto.cs_key_set(ic, key, mac);
}
void
ieee80211_crypto_attach(struct ieee80211com *ic)
{
struct ieee80211_crypto_state *cs = &ic->ic_crypto;
int i;
cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
cs->cs_max_keyix = IEEE80211_WEP_NKID;
ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
for (i = 0; i < IEEE80211_WEP_NKID; i++)
ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
IEEE80211_KEYIX_NONE);
cs->cs_key_alloc = null_key_alloc;
cs->cs_key_set = null_key_set;
cs->cs_key_delete = null_key_delete;
cs->cs_key_update_begin = null_key_update;
cs->cs_key_update_end = null_key_update;
}
void
ieee80211_crypto_detach(struct ieee80211com *ic)
{
ieee80211_crypto_delglobalkeys(ic);
}
void
ieee80211_crypto_register(const struct ieee80211_cipher *cip)
{
if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
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) {
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) {
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) {
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[] = {
"wlan_wep",
"wlan_tkip",
"wlan_aes_ocb",
"wlan_ccmp",
"wlan_ckip",
};
int
ieee80211_crypto_newkey(struct ieee80211com *ic, int cipher, int flags,
struct ieee80211_key *key)
{
#define N(a) (sizeof(a) / sizeof(a[0]))
const struct ieee80211_cipher *cip;
ieee80211_keyix keyix, rxkeyix;
void *keyctx;
int oflags;
if (cipher >= IEEE80211_CIPHER_MAX) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: invalid cipher %u\n", __func__, cipher);
ic->ic_stats.is_crypto_badcipher++;
return 0;
}
cip = ciphers[cipher];
if (cip == NULL) {
if (cipher < N(cipher_modnames)) {
IEEE80211_DPRINTF(ic, 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(ic, IEEE80211_MSG_CRYPTO,
"%s: unable to load cipher %u, module %s\n",
__func__, cipher,
cipher < N(cipher_modnames) ?
cipher_modnames[cipher] : "<unknown>");
ic->ic_stats.is_crypto_nocipher++;
return 0;
}
}
oflags = key->wk_flags;
flags &= IEEE80211_KEY_COMMON;
if ((ic->ic_caps & (1<<cipher)) == 0) {
IEEE80211_DPRINTF(ic, 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 (cipher == IEEE80211_CIPHER_TKIP &&
(ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
IEEE80211_DPRINTF(ic, 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) {
again:
key->wk_flags = flags;
keyctx = cip->ic_attach(ic, key);
if (keyctx == NULL) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: unable to attach cipher %s\n",
__func__, cip->ic_name);
key->wk_flags = oflags;
ic->ic_stats.is_crypto_attachfail++;
return 0;
}
cipher_detach(key);
key->wk_cipher = cip;
key->wk_private = keyctx;
}
key->wk_flags = flags;
if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) {
if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
ic->ic_stats.is_crypto_swfallback++;
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: no h/w resources for cipher %s, "
"falling back to s/w\n", __func__,
cip->ic_name);
oflags = key->wk_flags;
flags |= IEEE80211_KEY_SWCRYPT;
if (cipher == IEEE80211_CIPHER_TKIP)
flags |= IEEE80211_KEY_SWMIC;
goto again;
}
ic->ic_stats.is_crypto_keyfail++;
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: unable to setup cipher %s\n",
__func__, cip->ic_name);
return 0;
}
key->wk_keyix = keyix;
key->wk_rxkeyix = rxkeyix;
}
return 1;
#undef N
}
static int
_ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
{
ieee80211_keyix keyix;
IASSERT(key->wk_cipher != NULL, ("No cipher!"));
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
__func__, key->wk_cipher->ic_name,
key->wk_keyix, key->wk_flags,
key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
keyix = key->wk_keyix;
if (keyix != IEEE80211_KEYIX_NONE) {
if (!dev_key_delete(ic, key)) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: driver did not delete key index %u\n",
__func__, keyix);
ic->ic_stats.is_crypto_delkey++;
}
}
cipher_detach(key);
memset(key, 0, sizeof(*key));
ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
return 1;
}
int
ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
{
int status;
ieee80211_key_update_begin(ic);
status = _ieee80211_crypto_delkey(ic, key);
ieee80211_key_update_end(ic);
return status;
}
void
ieee80211_crypto_delglobalkeys(struct ieee80211com *ic)
{
int i;
ieee80211_key_update_begin(ic);
for (i = 0; i < IEEE80211_WEP_NKID; i++)
(void)_ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]);
ieee80211_key_update_end(ic);
}
int
ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key,
const u_int8_t macaddr[IEEE80211_ADDR_LEN])
{
const struct ieee80211_cipher *cip = key->wk_cipher;
IASSERT(cip != NULL, ("No cipher!"));
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
__func__, cip->ic_name, key->wk_keyix,
key->wk_flags, ether_sprintf(macaddr),
key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
if (!cip->ic_setkey(key)) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: cipher %s rejected key index %u len %u flags 0x%x\n",
__func__, cip->ic_name, key->wk_keyix,
key->wk_keylen, key->wk_flags);
ic->ic_stats.is_crypto_setkey_cipher++;
return 0;
}
if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"%s: no key index; should not happen!\n", __func__);
ic->ic_stats.is_crypto_setkey_nokey++;
return 0;
}
return dev_key_set(ic, key, macaddr);
}
struct ieee80211_key *
ieee80211_crypto_encap(struct ieee80211com *ic, struct ieee80211_node *ni,
struct mbuf *m)
{
struct ieee80211_key *k;
struct ieee80211_frame *wh;
const struct ieee80211_cipher *cip;
u_int8_t keyid, *hdr;
int hdrlen;
wh = mtod(m, struct ieee80211_frame *);
if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
"[%s] no default transmit key (%s) deftxkey %u\n",
ether_sprintf(wh->i_addr1), __func__,
ic->ic_def_txkey);
ic->ic_stats.is_tx_nodefkey++;
return NULL;
}
keyid = ic->ic_def_txkey;
k = &ic->ic_nw_keys[ic->ic_def_txkey];
} else {
keyid = 0;
k = &ni->ni_ucastkey;
}
cip = k->wk_cipher;
hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
M_PREPEND(m, cip->ic_header, M_NOWAIT);
if (m && m->m_len < hdrlen + cip->ic_header) {
m = m_pullup(m, hdrlen + cip->ic_header);
}
if (m == NULL) {
return NULL;
}
hdr = mtod(m, u_int8_t *);
memmove(hdr, hdr + cip->ic_header, hdrlen);
return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
}
#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 ieee80211_key *
ieee80211_crypto_decap(struct ieee80211com *ic,
struct ieee80211_node *ni, struct mbuf **mp, int hdrlen)
{
const struct ieee80211_cipher *cip;
struct ieee80211_key *k;
struct ieee80211_frame *wh;
struct mbuf *m = *mp;
u_int8_t keyid;
KASSERT((m->m_flags & M_PKTHDR) != 0);
if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
"%s: WEP data frame too short, len %u\n",
__func__, m->m_pkthdr.len);
ic->ic_stats.is_rx_tooshort++;
return NULL;
}
wh = mtod(m, struct ieee80211_frame *);
m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
k = &ic->ic_nw_keys[keyid >> 6];
} else {
k = &ni->ni_ucastkey;
}
cip = k->wk_cipher;
if (m->m_len < hdrlen + cip->ic_header) {
m = m_pullup(m, hdrlen + cip->ic_header);
*mp = m;
}
if (m == NULL) {
ic->ic_stats.is_rx_tooshort++;
return NULL;
}
if (m->m_pkthdr.len < hdrlen + cip->ic_header + cip->ic_trailer) {
IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
"%s: WEP data frame too short, len %u\n",
__func__, m->m_pkthdr.len);
ic->ic_stats.is_rx_tooshort++;
return NULL;
}
return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
}