Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto.c revision 1.23.2.4
      1  1.23.2.4  christos /*	$NetBSD: ieee80211_crypto.c,v 1.23.2.4 2019/06/10 22:09:46 christos Exp $ */
      2  1.23.2.2      phil 
      3  1.23.2.1      phil /*-
      4  1.23.2.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.23.2.1      phil  *
      6       1.1    dyoung  * Copyright (c) 2001 Atsushi Onoe
      7  1.23.2.1      phil  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
      8       1.1    dyoung  * All rights reserved.
      9       1.1    dyoung  *
     10       1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     11       1.1    dyoung  * modification, are permitted provided that the following conditions
     12       1.1    dyoung  * are met:
     13       1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     14       1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     15       1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     17       1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     18       1.1    dyoung  *
     19       1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24       1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25       1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26       1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27       1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28       1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29       1.1    dyoung  */
     30       1.1    dyoung 
     31       1.1    dyoung #include <sys/cdefs.h>
     32  1.23.2.4  christos #ifdef __NetBSD__
     33  1.23.2.4  christos __KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto.c,v 1.23.2.4 2019/06/10 22:09:46 christos Exp $");
     34  1.23.2.2      phil #endif
     35       1.1    dyoung 
     36       1.7    dyoung /*
     37       1.7    dyoung  * IEEE 802.11 generic crypto support.
     38       1.7    dyoung  */
     39  1.23.2.4  christos #ifdef _KERNEL_OPT
     40  1.23.2.1      phil #include "opt_wlan.h"
     41  1.23.2.4  christos #endif
     42  1.23.2.1      phil 
     43       1.1    dyoung #include <sys/param.h>
     44  1.23.2.1      phil #include <sys/kernel.h>
     45  1.23.2.1      phil #include <sys/malloc.h>
     46  1.23.2.1      phil #include <sys/mbuf.h>
     47       1.7    dyoung 
     48       1.1    dyoung #include <sys/socket.h>
     49       1.1    dyoung 
     50       1.1    dyoung #include <net/if.h>
     51       1.1    dyoung #include <net/if_media.h>
     52  1.23.2.3      phil #if __FreeBSD__
     53  1.23.2.1      phil #include <net/ethernet.h>		/* XXX ETHER_HDR_LEN */
     54  1.23.2.2      phil #endif
     55  1.23.2.3      phil #if __NetBSD__
     56  1.23.2.3      phil #include <net/if_ether.h>
     57  1.23.2.2      phil #include <net/route.h>
     58  1.23.2.2      phil #endif
     59       1.1    dyoung 
     60       1.1    dyoung #include <net80211/ieee80211_var.h>
     61       1.1    dyoung 
     62  1.23.2.2      phil #ifdef __NetBSD__
     63  1.23.2.2      phil #undef  KASSERT
     64  1.23.2.2      phil #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     65  1.23.2.2      phil #endif
     66  1.23.2.2      phil 
     67  1.23.2.1      phil MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state");
     68  1.23.2.1      phil 
     69  1.23.2.1      phil static	int _ieee80211_crypto_delkey(struct ieee80211vap *,
     70  1.23.2.1      phil 		struct ieee80211_key *);
     71  1.23.2.1      phil 
     72       1.7    dyoung /*
     73       1.7    dyoung  * Table of registered cipher modules.
     74       1.7    dyoung  */
     75  1.23.2.1      phil static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
     76       1.7    dyoung 
     77       1.7    dyoung /*
     78       1.7    dyoung  * Default "null" key management routines.
     79       1.7    dyoung  */
     80       1.7    dyoung static int
     81  1.23.2.1      phil null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
     82  1.23.2.1      phil 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
     83       1.7    dyoung {
     84  1.23.2.1      phil 	if (!(&vap->iv_nw_keys[0] <= k &&
     85  1.23.2.1      phil 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
     86       1.9    dyoung 		/*
     87       1.9    dyoung 		 * Not in the global key table, the driver should handle this
     88       1.9    dyoung 		 * by allocating a slot in the h/w key table/cache.  In
     89       1.9    dyoung 		 * lieu of that return key slot 0 for any unicast key
     90       1.9    dyoung 		 * request.  We disallow the request if this is a group key.
     91       1.9    dyoung 		 * This default policy does the right thing for legacy hardware
     92       1.9    dyoung 		 * with a 4 key table.  It also handles devices that pass
     93       1.9    dyoung 		 * packets through untouched when marked with the WEP bit
     94       1.9    dyoung 		 * and key index 0.
     95       1.9    dyoung 		 */
     96      1.10     skrll 		if (k->wk_flags & IEEE80211_KEY_GROUP)
     97      1.10     skrll 			return 0;
     98      1.10     skrll 		*keyix = 0;	/* NB: use key index 0 for ucast key */
     99      1.10     skrll 	} else {
    100  1.23.2.1      phil 		*keyix = ieee80211_crypto_get_key_wepidx(vap, k);
    101       1.9    dyoung 	}
    102      1.10     skrll 	*rxkeyix = IEEE80211_KEYIX_NONE;	/* XXX maybe *keyix? */
    103      1.10     skrll 	return 1;
    104       1.7    dyoung }
    105       1.7    dyoung static int
    106  1.23.2.1      phil null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
    107       1.7    dyoung {
    108       1.7    dyoung 	return 1;
    109       1.7    dyoung }
    110  1.23.2.1      phil static 	int
    111  1.23.2.1      phil null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
    112       1.7    dyoung {
    113       1.7    dyoung 	return 1;
    114       1.7    dyoung }
    115  1.23.2.1      phil static void null_key_update(struct ieee80211vap *vap) {}
    116       1.7    dyoung 
    117       1.7    dyoung /*
    118       1.7    dyoung  * Write-arounds for common operations.
    119       1.7    dyoung  */
    120       1.7    dyoung static __inline void
    121       1.7    dyoung cipher_detach(struct ieee80211_key *key)
    122       1.7    dyoung {
    123       1.7    dyoung 	key->wk_cipher->ic_detach(key);
    124       1.7    dyoung }
    125       1.7    dyoung 
    126  1.23.2.1      phil static __inline void *
    127  1.23.2.1      phil cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
    128  1.23.2.1      phil {
    129  1.23.2.1      phil 	return key->wk_cipher->ic_attach(vap, key);
    130  1.23.2.1      phil }
    131  1.23.2.1      phil 
    132  1.23.2.1      phil /*
    133       1.7    dyoung  * Wrappers for driver key management methods.
    134       1.7    dyoung  */
    135       1.7    dyoung static __inline int
    136  1.23.2.1      phil dev_key_alloc(struct ieee80211vap *vap,
    137  1.23.2.1      phil 	struct ieee80211_key *key,
    138  1.23.2.1      phil 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
    139       1.7    dyoung {
    140  1.23.2.1      phil 	return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
    141       1.7    dyoung }
    142       1.7    dyoung 
    143       1.7    dyoung static __inline int
    144  1.23.2.1      phil dev_key_delete(struct ieee80211vap *vap,
    145  1.23.2.1      phil 	const struct ieee80211_key *key)
    146       1.7    dyoung {
    147  1.23.2.1      phil 	return vap->iv_key_delete(vap, key);
    148       1.7    dyoung }
    149       1.1    dyoung 
    150       1.7    dyoung static __inline int
    151  1.23.2.1      phil dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
    152       1.7    dyoung {
    153  1.23.2.1      phil 	return vap->iv_key_set(vap, key);
    154       1.7    dyoung }
    155       1.1    dyoung 
    156       1.7    dyoung /*
    157  1.23.2.1      phil  * Setup crypto support for a device/shared instance.
    158       1.7    dyoung  */
    159       1.1    dyoung void
    160       1.7    dyoung ieee80211_crypto_attach(struct ieee80211com *ic)
    161       1.1    dyoung {
    162  1.23.2.1      phil 	/* NB: we assume everything is pre-zero'd */
    163  1.23.2.1      phil 	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
    164  1.23.2.1      phil }
    165  1.23.2.1      phil 
    166  1.23.2.1      phil /*
    167  1.23.2.1      phil  * Teardown crypto support.
    168  1.23.2.1      phil  */
    169  1.23.2.1      phil void
    170  1.23.2.1      phil ieee80211_crypto_detach(struct ieee80211com *ic)
    171  1.23.2.1      phil {
    172  1.23.2.1      phil }
    173  1.23.2.1      phil 
    174  1.23.2.1      phil /*
    175  1.23.2.1      phil  * Setup crypto support for a vap.
    176  1.23.2.1      phil  */
    177  1.23.2.1      phil void
    178  1.23.2.1      phil ieee80211_crypto_vattach(struct ieee80211vap *vap)
    179  1.23.2.1      phil {
    180       1.7    dyoung 	int i;
    181       1.1    dyoung 
    182       1.7    dyoung 	/* NB: we assume everything is pre-zero'd */
    183  1.23.2.1      phil 	vap->iv_max_keyix = IEEE80211_WEP_NKID;
    184  1.23.2.1      phil 	vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
    185       1.7    dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
    186  1.23.2.1      phil 		ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
    187       1.7    dyoung 			IEEE80211_KEYIX_NONE);
    188       1.1    dyoung 	/*
    189       1.7    dyoung 	 * Initialize the driver key support routines to noop entries.
    190       1.7    dyoung 	 * This is useful especially for the cipher test modules.
    191       1.1    dyoung 	 */
    192  1.23.2.1      phil 	vap->iv_key_alloc = null_key_alloc;
    193  1.23.2.1      phil 	vap->iv_key_set = null_key_set;
    194  1.23.2.1      phil 	vap->iv_key_delete = null_key_delete;
    195  1.23.2.1      phil 	vap->iv_key_update_begin = null_key_update;
    196  1.23.2.1      phil 	vap->iv_key_update_end = null_key_update;
    197       1.1    dyoung }
    198       1.1    dyoung 
    199       1.7    dyoung /*
    200  1.23.2.1      phil  * Teardown crypto support for a vap.
    201       1.7    dyoung  */
    202       1.1    dyoung void
    203  1.23.2.1      phil ieee80211_crypto_vdetach(struct ieee80211vap *vap)
    204       1.1    dyoung {
    205  1.23.2.1      phil 	ieee80211_crypto_delglobalkeys(vap);
    206       1.7    dyoung }
    207       1.1    dyoung 
    208       1.7    dyoung /*
    209       1.7    dyoung  * Register a crypto cipher module.
    210       1.7    dyoung  */
    211       1.7    dyoung void
    212       1.7    dyoung ieee80211_crypto_register(const struct ieee80211_cipher *cip)
    213       1.7    dyoung {
    214       1.7    dyoung 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
    215       1.7    dyoung 		printf("%s: cipher %s has an invalid cipher index %u\n",
    216       1.7    dyoung 			__func__, cip->ic_name, cip->ic_cipher);
    217       1.7    dyoung 		return;
    218       1.7    dyoung 	}
    219       1.7    dyoung 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
    220       1.7    dyoung 		printf("%s: cipher %s registered with a different template\n",
    221       1.7    dyoung 			__func__, cip->ic_name);
    222       1.7    dyoung 		return;
    223       1.1    dyoung 	}
    224       1.7    dyoung 	ciphers[cip->ic_cipher] = cip;
    225       1.1    dyoung }
    226       1.1    dyoung 
    227       1.7    dyoung /*
    228       1.7    dyoung  * Unregister a crypto cipher module.
    229       1.7    dyoung  */
    230       1.7    dyoung void
    231       1.7    dyoung ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
    232       1.1    dyoung {
    233       1.7    dyoung 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
    234       1.7    dyoung 		printf("%s: cipher %s has an invalid cipher index %u\n",
    235       1.7    dyoung 			__func__, cip->ic_name, cip->ic_cipher);
    236       1.7    dyoung 		return;
    237       1.1    dyoung 	}
    238       1.7    dyoung 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
    239       1.7    dyoung 		printf("%s: cipher %s registered with a different template\n",
    240       1.7    dyoung 			__func__, cip->ic_name);
    241       1.7    dyoung 		return;
    242       1.5    dyoung 	}
    243       1.7    dyoung 	/* NB: don't complain about not being registered */
    244       1.7    dyoung 	/* XXX disallow if references */
    245       1.7    dyoung 	ciphers[cip->ic_cipher] = NULL;
    246       1.7    dyoung }
    247       1.7    dyoung 
    248       1.7    dyoung int
    249       1.7    dyoung ieee80211_crypto_available(u_int cipher)
    250       1.7    dyoung {
    251       1.7    dyoung 	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
    252       1.7    dyoung }
    253       1.7    dyoung 
    254  1.23.2.3      phil #if __FreeBSD__
    255       1.7    dyoung /* XXX well-known names! */
    256  1.23.2.1      phil static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
    257  1.23.2.1      phil 	[IEEE80211_CIPHER_WEP]	   = "wlan_wep",
    258  1.23.2.1      phil 	[IEEE80211_CIPHER_TKIP]	   = "wlan_tkip",
    259  1.23.2.1      phil 	[IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
    260  1.23.2.1      phil 	[IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
    261  1.23.2.1      phil 	[IEEE80211_CIPHER_TKIPMIC] = "#4",	/* NB: reserved */
    262  1.23.2.1      phil 	[IEEE80211_CIPHER_CKIP]	   = "wlan_ckip",
    263  1.23.2.1      phil 	[IEEE80211_CIPHER_NONE]	   = "wlan_none",
    264       1.7    dyoung };
    265  1.23.2.2      phil #endif
    266       1.7    dyoung 
    267  1.23.2.1      phil /* NB: there must be no overlap between user-supplied and device-owned flags */
    268  1.23.2.1      phil CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
    269  1.23.2.1      phil 
    270       1.7    dyoung /*
    271       1.7    dyoung  * Establish a relationship between the specified key and cipher
    272       1.7    dyoung  * and, if necessary, allocate a hardware index from the driver.
    273  1.23.2.1      phil  * Note that when a fixed key index is required it must be specified.
    274       1.7    dyoung  *
    275       1.7    dyoung  * This must be the first call applied to a key; all the other key
    276       1.7    dyoung  * routines assume wk_cipher is setup.
    277       1.7    dyoung  *
    278       1.7    dyoung  * Locking must be handled by the caller using:
    279  1.23.2.1      phil  *	ieee80211_key_update_begin(vap);
    280  1.23.2.1      phil  *	ieee80211_key_update_end(vap);
    281       1.7    dyoung  */
    282       1.7    dyoung int
    283  1.23.2.1      phil ieee80211_crypto_newkey(struct ieee80211vap *vap,
    284  1.23.2.1      phil 	int cipher, int flags, struct ieee80211_key *key)
    285       1.7    dyoung {
    286  1.23.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    287       1.7    dyoung 	const struct ieee80211_cipher *cip;
    288      1.10     skrll 	ieee80211_keyix keyix, rxkeyix;
    289       1.7    dyoung 	void *keyctx;
    290       1.7    dyoung 	int oflags;
    291       1.7    dyoung 
    292  1.23.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    293  1.23.2.1      phil 	    "%s: cipher %u flags 0x%x keyix %u\n",
    294  1.23.2.1      phil 	    __func__, cipher, flags, key->wk_keyix);
    295  1.23.2.1      phil 
    296       1.7    dyoung 	/*
    297       1.7    dyoung 	 * Validate cipher and set reference to cipher routines.
    298       1.7    dyoung 	 */
    299       1.7    dyoung 	if (cipher >= IEEE80211_CIPHER_MAX) {
    300  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    301  1.23.2.1      phil 		    "%s: invalid cipher %u\n", __func__, cipher);
    302  1.23.2.1      phil 		vap->iv_stats.is_crypto_badcipher++;
    303       1.7    dyoung 		return 0;
    304       1.1    dyoung 	}
    305       1.7    dyoung 	cip = ciphers[cipher];
    306       1.7    dyoung 	if (cip == NULL) {
    307  1.23.2.2      phil #if __FreeBSD__
    308       1.1    dyoung 		/*
    309       1.7    dyoung 		 * Auto-load cipher module if we have a well-known name
    310       1.7    dyoung 		 * for it.  It might be better to use string names rather
    311       1.7    dyoung 		 * than numbers and craft a module name based on the cipher
    312       1.7    dyoung 		 * name; e.g. wlan_cipher_<cipher-name>.
    313       1.1    dyoung 		 */
    314  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    315  1.23.2.1      phil 		    "%s: unregistered cipher %u, load module %s\n",
    316  1.23.2.1      phil 		    __func__, cipher, cipher_modnames[cipher]);
    317  1.23.2.1      phil 		ieee80211_load_module(cipher_modnames[cipher]);
    318  1.23.2.1      phil 		/*
    319  1.23.2.1      phil 		 * If cipher module loaded it should immediately
    320  1.23.2.1      phil 		 * call ieee80211_crypto_register which will fill
    321  1.23.2.1      phil 		 * in the entry in the ciphers array.
    322  1.23.2.1      phil 		 */
    323  1.23.2.1      phil 		cip = ciphers[cipher];
    324       1.7    dyoung 		if (cip == NULL) {
    325  1.23.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    326  1.23.2.1      phil 			    "%s: unable to load cipher %u, module %s\n",
    327  1.23.2.1      phil 			    __func__, cipher, cipher_modnames[cipher]);
    328  1.23.2.1      phil 			vap->iv_stats.is_crypto_nocipher++;
    329       1.7    dyoung 			return 0;
    330       1.1    dyoung 		}
    331  1.23.2.2      phil #else
    332  1.23.2.2      phil 		panic("wlan_cipher not usable.");    /* NNN NetBSD modules? */
    333  1.23.2.2      phil #endif
    334       1.7    dyoung 	}
    335       1.7    dyoung 
    336       1.7    dyoung 	oflags = key->wk_flags;
    337       1.7    dyoung 	flags &= IEEE80211_KEY_COMMON;
    338  1.23.2.1      phil 	/* NB: preserve device attributes */
    339  1.23.2.1      phil 	flags |= (oflags & IEEE80211_KEY_DEVICE);
    340       1.7    dyoung 	/*
    341       1.7    dyoung 	 * If the hardware does not support the cipher then
    342  1.23.2.1      phil 	 * fallback to a host-based implementation.
    343       1.7    dyoung 	 */
    344  1.23.2.1      phil 	if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
    345  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    346       1.7    dyoung 		    "%s: no h/w support for cipher %s, falling back to s/w\n",
    347       1.7    dyoung 		    __func__, cip->ic_name);
    348       1.7    dyoung 		flags |= IEEE80211_KEY_SWCRYPT;
    349       1.7    dyoung 	}
    350       1.7    dyoung 	/*
    351       1.7    dyoung 	 * Hardware TKIP with software MIC is an important
    352       1.7    dyoung 	 * combination; we handle it by flagging each key,
    353       1.7    dyoung 	 * the cipher modules honor it.
    354       1.7    dyoung 	 */
    355       1.7    dyoung 	if (cipher == IEEE80211_CIPHER_TKIP &&
    356  1.23.2.1      phil 	    (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
    357  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    358       1.7    dyoung 		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
    359       1.7    dyoung 		    __func__);
    360       1.7    dyoung 		flags |= IEEE80211_KEY_SWMIC;
    361       1.7    dyoung 	}
    362       1.7    dyoung 
    363       1.7    dyoung 	/*
    364       1.7    dyoung 	 * Bind cipher to key instance.  Note we do this
    365       1.7    dyoung 	 * after checking the device capabilities so the
    366       1.7    dyoung 	 * cipher module can optimize space usage based on
    367       1.7    dyoung 	 * whether or not it needs to do the cipher work.
    368       1.7    dyoung 	 */
    369       1.7    dyoung 	if (key->wk_cipher != cip || key->wk_flags != flags) {
    370       1.7    dyoung 		/*
    371       1.7    dyoung 		 * Fillin the flags so cipher modules can see s/w
    372       1.7    dyoung 		 * crypto requirements and potentially allocate
    373       1.7    dyoung 		 * different state and/or attach different method
    374       1.7    dyoung 		 * pointers.
    375       1.7    dyoung 		 */
    376       1.7    dyoung 		key->wk_flags = flags;
    377  1.23.2.1      phil 		keyctx = cip->ic_attach(vap, key);
    378       1.7    dyoung 		if (keyctx == NULL) {
    379  1.23.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    380       1.7    dyoung 				"%s: unable to attach cipher %s\n",
    381       1.7    dyoung 				__func__, cip->ic_name);
    382       1.7    dyoung 			key->wk_flags = oflags;	/* restore old flags */
    383  1.23.2.1      phil 			vap->iv_stats.is_crypto_attachfail++;
    384       1.7    dyoung 			return 0;
    385       1.1    dyoung 		}
    386       1.7    dyoung 		cipher_detach(key);
    387       1.7    dyoung 		key->wk_cipher = cip;		/* XXX refcnt? */
    388       1.7    dyoung 		key->wk_private = keyctx;
    389       1.7    dyoung 	}
    390       1.7    dyoung 
    391       1.7    dyoung 	/*
    392       1.7    dyoung 	 * Ask the driver for a key index if we don't have one.
    393       1.7    dyoung 	 * Note that entries in the global key table always have
    394       1.7    dyoung 	 * an index; this means it's safe to call this routine
    395       1.7    dyoung 	 * for these entries just to setup the reference to the
    396       1.7    dyoung 	 * cipher template.  Note also that when using software
    397       1.7    dyoung 	 * crypto we also call the driver to give us a key index.
    398       1.7    dyoung 	 */
    399  1.23.2.1      phil 	if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
    400  1.23.2.1      phil 		if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
    401       1.7    dyoung 			/*
    402  1.23.2.1      phil 			 * Unable to setup driver state.
    403       1.7    dyoung 			 */
    404  1.23.2.1      phil 			vap->iv_stats.is_crypto_keyfail++;
    405  1.23.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    406       1.7    dyoung 			    "%s: unable to setup cipher %s\n",
    407       1.7    dyoung 			    __func__, cip->ic_name);
    408       1.7    dyoung 			return 0;
    409       1.1    dyoung 		}
    410  1.23.2.1      phil 		if (key->wk_flags != flags) {
    411  1.23.2.1      phil 			/*
    412  1.23.2.1      phil 			 * Driver overrode flags we setup; typically because
    413  1.23.2.1      phil 			 * resources were unavailable to handle _this_ key.
    414  1.23.2.1      phil 			 * Re-attach the cipher context to allow cipher
    415  1.23.2.1      phil 			 * modules to handle differing requirements.
    416  1.23.2.1      phil 			 */
    417  1.23.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    418  1.23.2.1      phil 			    "%s: driver override for cipher %s, flags "
    419  1.23.2.1      phil 			    "0x%x -> 0x%x\n", __func__, cip->ic_name,
    420  1.23.2.1      phil 			    oflags, key->wk_flags);
    421  1.23.2.1      phil 			keyctx = cip->ic_attach(vap, key);
    422  1.23.2.1      phil 			if (keyctx == NULL) {
    423  1.23.2.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    424  1.23.2.1      phil 				    "%s: unable to attach cipher %s with "
    425  1.23.2.1      phil 				    "flags 0x%x\n", __func__, cip->ic_name,
    426  1.23.2.1      phil 				    key->wk_flags);
    427  1.23.2.1      phil 				key->wk_flags = oflags;	/* restore old flags */
    428  1.23.2.1      phil 				vap->iv_stats.is_crypto_attachfail++;
    429  1.23.2.1      phil 				return 0;
    430  1.23.2.1      phil 			}
    431  1.23.2.1      phil 			cipher_detach(key);
    432  1.23.2.1      phil 			key->wk_cipher = cip;		/* XXX refcnt? */
    433  1.23.2.1      phil 			key->wk_private = keyctx;
    434  1.23.2.1      phil 		}
    435      1.10     skrll 		key->wk_keyix = keyix;
    436      1.10     skrll 		key->wk_rxkeyix = rxkeyix;
    437  1.23.2.1      phil 		key->wk_flags |= IEEE80211_KEY_DEVKEY;
    438       1.7    dyoung 	}
    439       1.7    dyoung 	return 1;
    440       1.7    dyoung }
    441       1.7    dyoung 
    442       1.7    dyoung /*
    443       1.7    dyoung  * Remove the key (no locking, for internal use).
    444       1.7    dyoung  */
    445       1.7    dyoung static int
    446  1.23.2.1      phil _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
    447       1.7    dyoung {
    448  1.23.2.1      phil 	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
    449       1.7    dyoung 
    450  1.23.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    451       1.7    dyoung 	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
    452       1.7    dyoung 	    __func__, key->wk_cipher->ic_name,
    453       1.7    dyoung 	    key->wk_keyix, key->wk_flags,
    454  1.23.2.1      phil 	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
    455  1.23.2.1      phil 	    key->wk_keylen);
    456       1.7    dyoung 
    457  1.23.2.1      phil 	if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
    458       1.7    dyoung 		/*
    459       1.7    dyoung 		 * Remove hardware entry.
    460       1.7    dyoung 		 */
    461       1.7    dyoung 		/* XXX key cache */
    462  1.23.2.1      phil 		if (!dev_key_delete(vap, key)) {
    463  1.23.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    464       1.7    dyoung 			    "%s: driver did not delete key index %u\n",
    465  1.23.2.1      phil 			    __func__, key->wk_keyix);
    466  1.23.2.1      phil 			vap->iv_stats.is_crypto_delkey++;
    467       1.7    dyoung 			/* XXX recovery? */
    468       1.1    dyoung 		}
    469       1.1    dyoung 	}
    470       1.7    dyoung 	cipher_detach(key);
    471       1.7    dyoung 	memset(key, 0, sizeof(*key));
    472  1.23.2.1      phil 	ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
    473       1.7    dyoung 	return 1;
    474       1.7    dyoung }
    475       1.7    dyoung 
    476       1.7    dyoung /*
    477       1.7    dyoung  * Remove the specified key.
    478       1.7    dyoung  */
    479       1.7    dyoung int
    480  1.23.2.1      phil ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
    481       1.7    dyoung {
    482       1.7    dyoung 	int status;
    483       1.1    dyoung 
    484  1.23.2.1      phil 	ieee80211_key_update_begin(vap);
    485  1.23.2.1      phil 	status = _ieee80211_crypto_delkey(vap, key);
    486  1.23.2.1      phil 	ieee80211_key_update_end(vap);
    487       1.7    dyoung 	return status;
    488       1.1    dyoung }
    489       1.1    dyoung 
    490       1.1    dyoung /*
    491       1.7    dyoung  * Clear the global key table.
    492       1.1    dyoung  */
    493       1.7    dyoung void
    494  1.23.2.1      phil ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
    495       1.7    dyoung {
    496       1.7    dyoung 	int i;
    497       1.7    dyoung 
    498  1.23.2.1      phil 	ieee80211_key_update_begin(vap);
    499       1.7    dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
    500  1.23.2.1      phil 		(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
    501  1.23.2.1      phil 	ieee80211_key_update_end(vap);
    502       1.7    dyoung }
    503       1.7    dyoung 
    504       1.7    dyoung /*
    505       1.7    dyoung  * Set the contents of the specified key.
    506       1.7    dyoung  *
    507       1.7    dyoung  * Locking must be handled by the caller using:
    508  1.23.2.1      phil  *	ieee80211_key_update_begin(vap);
    509  1.23.2.1      phil  *	ieee80211_key_update_end(vap);
    510       1.7    dyoung  */
    511       1.7    dyoung int
    512  1.23.2.1      phil ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
    513       1.7    dyoung {
    514       1.7    dyoung 	const struct ieee80211_cipher *cip = key->wk_cipher;
    515       1.7    dyoung 
    516  1.23.2.1      phil 	KASSERT(cip != NULL, ("No cipher!"));
    517       1.7    dyoung 
    518  1.23.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    519       1.7    dyoung 	    "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
    520       1.7    dyoung 	    __func__, cip->ic_name, key->wk_keyix,
    521  1.23.2.1      phil 	    key->wk_flags, ether_sprintf(key->wk_macaddr),
    522  1.23.2.1      phil 	    key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
    523  1.23.2.1      phil 	    key->wk_keylen);
    524       1.7    dyoung 
    525  1.23.2.1      phil 	if ((key->wk_flags & IEEE80211_KEY_DEVKEY)  == 0) {
    526  1.23.2.1      phil 		/* XXX nothing allocated, should not happen */
    527  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    528  1.23.2.1      phil 		    "%s: no device key setup done; should not happen!\n",
    529  1.23.2.1      phil 		    __func__);
    530  1.23.2.1      phil 		vap->iv_stats.is_crypto_setkey_nokey++;
    531  1.23.2.1      phil 		return 0;
    532  1.23.2.1      phil 	}
    533       1.7    dyoung 	/*
    534       1.7    dyoung 	 * Give cipher a chance to validate key contents.
    535       1.7    dyoung 	 * XXX should happen before modifying state.
    536       1.7    dyoung 	 */
    537       1.7    dyoung 	if (!cip->ic_setkey(key)) {
    538  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
    539       1.7    dyoung 		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
    540       1.7    dyoung 		    __func__, cip->ic_name, key->wk_keyix,
    541       1.7    dyoung 		    key->wk_keylen, key->wk_flags);
    542  1.23.2.1      phil 		vap->iv_stats.is_crypto_setkey_cipher++;
    543       1.7    dyoung 		return 0;
    544       1.7    dyoung 	}
    545  1.23.2.1      phil 	return dev_key_set(vap, key);
    546       1.7    dyoung }
    547       1.1    dyoung 
    548       1.7    dyoung /*
    549  1.23.2.1      phil  * Return index if the key is a WEP key (0..3); -1 otherwise.
    550      1.22      maxv  *
    551  1.23.2.1      phil  * This is different to "get_keyid" which defaults to returning
    552  1.23.2.1      phil  * 0 for unicast keys; it assumes that it won't be used for WEP.
    553       1.7    dyoung  */
    554  1.23.2.1      phil int
    555  1.23.2.1      phil ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap,
    556  1.23.2.1      phil     const struct ieee80211_key *k)
    557  1.23.2.1      phil {
    558  1.23.2.1      phil 
    559  1.23.2.1      phil 	if (k >= &vap->iv_nw_keys[0] &&
    560  1.23.2.1      phil 	    k <  &vap->iv_nw_keys[IEEE80211_WEP_NKID])
    561  1.23.2.1      phil 		return (k - vap->iv_nw_keys);
    562  1.23.2.1      phil 	return (-1);
    563  1.23.2.1      phil }
    564  1.23.2.1      phil 
    565  1.23.2.1      phil /*
    566  1.23.2.1      phil  * Note: only supports a single unicast key (0).
    567  1.23.2.1      phil  */
    568  1.23.2.1      phil uint8_t
    569  1.23.2.1      phil ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k)
    570  1.23.2.1      phil {
    571  1.23.2.1      phil 	if (k >= &vap->iv_nw_keys[0] &&
    572  1.23.2.1      phil 	    k <  &vap->iv_nw_keys[IEEE80211_WEP_NKID])
    573  1.23.2.1      phil 		return (k - vap->iv_nw_keys);
    574  1.23.2.1      phil 	else
    575  1.23.2.1      phil 		return (0);
    576  1.23.2.1      phil }
    577  1.23.2.1      phil 
    578       1.7    dyoung struct ieee80211_key *
    579  1.23.2.1      phil ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m)
    580       1.7    dyoung {
    581  1.23.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    582       1.7    dyoung 	struct ieee80211_frame *wh;
    583       1.1    dyoung 
    584       1.7    dyoung 	/*
    585       1.7    dyoung 	 * Multicast traffic always uses the multicast key.
    586       1.7    dyoung 	 * Otherwise if a unicast key is set we use that and
    587       1.7    dyoung 	 * it is always key index 0.  When no unicast key is
    588       1.7    dyoung 	 * set we fall back to the default transmit key.
    589       1.7    dyoung 	 */
    590       1.7    dyoung 	wh = mtod(m, struct ieee80211_frame *);
    591       1.7    dyoung 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    592  1.23.2.1      phil 	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
    593  1.23.2.1      phil 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
    594  1.23.2.1      phil 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
    595  1.23.2.1      phil 			    wh->i_addr1,
    596  1.23.2.1      phil 			    "no default transmit key (%s) deftxkey %u",
    597  1.23.2.1      phil 			    __func__, vap->iv_def_txkey);
    598  1.23.2.1      phil 			vap->iv_stats.is_tx_nodefkey++;
    599      1.10     skrll 			return NULL;
    600       1.1    dyoung 		}
    601  1.23.2.1      phil 		return &vap->iv_nw_keys[vap->iv_def_txkey];
    602       1.1    dyoung 	}
    603      1.20      maxv 
    604  1.23.2.1      phil 	return &ni->ni_ucastkey;
    605  1.23.2.1      phil }
    606  1.23.2.1      phil 
    607  1.23.2.1      phil /*
    608  1.23.2.1      phil  * Add privacy headers appropriate for the specified key.
    609  1.23.2.1      phil  */
    610  1.23.2.1      phil struct ieee80211_key *
    611  1.23.2.1      phil ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
    612  1.23.2.1      phil {
    613  1.23.2.1      phil 	struct ieee80211_key *k;
    614  1.23.2.1      phil 	const struct ieee80211_cipher *cip;
    615  1.23.2.1      phil 
    616  1.23.2.1      phil 	if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) {
    617  1.23.2.1      phil 		cip = k->wk_cipher;
    618  1.23.2.1      phil 		return (cip->ic_encap(k, m) ? k : NULL);
    619      1.20      maxv 	}
    620      1.20      maxv 
    621  1.23.2.1      phil 	return NULL;
    622       1.1    dyoung }
    623       1.1    dyoung 
    624       1.1    dyoung /*
    625       1.7    dyoung  * Validate and strip privacy headers (and trailer) for a
    626       1.7    dyoung  * received frame that has the WEP/Privacy bit set.
    627       1.1    dyoung  */
    628  1.23.2.1      phil int
    629  1.23.2.1      phil ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen,
    630  1.23.2.1      phil     struct ieee80211_key **key)
    631       1.7    dyoung {
    632  1.23.2.1      phil #define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
    633  1.23.2.1      phil #define	IEEE80211_WEP_MINLEN \
    634  1.23.2.1      phil 	(sizeof(struct ieee80211_frame) + \
    635  1.23.2.1      phil 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
    636  1.23.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    637       1.7    dyoung 	struct ieee80211_key *k;
    638       1.7    dyoung 	struct ieee80211_frame *wh;
    639  1.23.2.1      phil 	const struct ieee80211_rx_stats *rxs;
    640  1.23.2.1      phil 	const struct ieee80211_cipher *cip;
    641  1.23.2.1      phil 	uint8_t keyid;
    642      1.21      maxv 
    643      1.21      maxv 	/*
    644  1.23.2.1      phil 	 * Check for hardware decryption and IV stripping.
    645  1.23.2.1      phil 	 * If the IV is stripped then we definitely can't find a key.
    646  1.23.2.1      phil 	 * Set the key to NULL but return true; upper layers
    647  1.23.2.1      phil 	 * will need to handle a NULL key for a successful
    648  1.23.2.1      phil 	 * decrypt.
    649  1.23.2.1      phil 	 */
    650  1.23.2.1      phil 	rxs = ieee80211_get_rx_params_ptr(m);
    651  1.23.2.1      phil 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
    652  1.23.2.1      phil 		if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) {
    653  1.23.2.1      phil 			/*
    654  1.23.2.1      phil 			 * Hardware decrypted, IV stripped.
    655  1.23.2.1      phil 			 * We can't find a key with a stripped IV.
    656  1.23.2.1      phil 			 * Return successful.
    657  1.23.2.1      phil 			 */
    658  1.23.2.1      phil 			*key = NULL;
    659  1.23.2.1      phil 			return (1);
    660  1.23.2.1      phil 		}
    661  1.23.2.1      phil 	}
    662  1.23.2.1      phil 
    663  1.23.2.1      phil 	/* NB: this minimum size data frame could be bigger */
    664       1.7    dyoung 	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
    665  1.23.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
    666       1.7    dyoung 			"%s: WEP data frame too short, len %u\n",
    667       1.7    dyoung 			__func__, m->m_pkthdr.len);
    668  1.23.2.1      phil 		vap->iv_stats.is_rx_tooshort++;	/* XXX need unique stat? */
    669  1.23.2.1      phil 		*key = NULL;
    670  1.23.2.1      phil 		return (0);
    671       1.7    dyoung 	}
    672       1.7    dyoung 
    673       1.7    dyoung 	/*
    674       1.7    dyoung 	 * Locate the key. If unicast and there is no unicast
    675       1.7    dyoung 	 * key then we fall back to the key id in the header.
    676       1.7    dyoung 	 * This assumes unicast keys are only configured when
    677       1.7    dyoung 	 * the key id in the header is meaningless (typically 0).
    678       1.7    dyoung 	 */
    679       1.7    dyoung 	wh = mtod(m, struct ieee80211_frame *);
    680      1.11    dyoung 	m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
    681       1.7    dyoung 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    682  1.23.2.1      phil 	    IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
    683  1.23.2.1      phil 		k = &vap->iv_nw_keys[keyid >> 6];
    684  1.23.2.1      phil 	else
    685       1.7    dyoung 		k = &ni->ni_ucastkey;
    686       1.1    dyoung 
    687       1.7    dyoung 	/*
    688       1.7    dyoung 	 * Insure crypto header is contiguous for all decap work.
    689       1.7    dyoung 	 */
    690       1.7    dyoung 	cip = k->wk_cipher;
    691  1.23.2.1      phil 	if (m->m_len < hdrlen + cip->ic_header &&
    692  1.23.2.1      phil 	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
    693  1.23.2.1      phil 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
    694  1.23.2.1      phil 		    "unable to pullup %s header", cip->ic_name);
    695  1.23.2.1      phil 		vap->iv_stats.is_rx_wepfail++;	/* XXX */
    696  1.23.2.1      phil 		*key = NULL;
    697  1.23.2.1      phil 		return (0);
    698      1.19      maxv 	}
    699      1.19      maxv 
    700  1.23.2.1      phil 	/*
    701  1.23.2.1      phil 	 * Attempt decryption.
    702  1.23.2.1      phil 	 *
    703  1.23.2.1      phil 	 * If we fail then don't return the key - return NULL
    704  1.23.2.1      phil 	 * and an error.
    705  1.23.2.1      phil 	 */
    706  1.23.2.1      phil 	if (cip->ic_decap(k, m, hdrlen)) {
    707  1.23.2.1      phil 		/* success */
    708  1.23.2.1      phil 		*key = k;
    709  1.23.2.1      phil 		return (1);
    710       1.7    dyoung 	}
    711       1.1    dyoung 
    712  1.23.2.1      phil 	/* Failure */
    713  1.23.2.1      phil 	*key = NULL;
    714  1.23.2.1      phil 	return (0);
    715  1.23.2.1      phil #undef IEEE80211_WEP_MINLEN
    716  1.23.2.1      phil #undef IEEE80211_WEP_HDRLEN
    717  1.23.2.1      phil }
    718  1.23.2.1      phil 
    719  1.23.2.1      phil /*
    720  1.23.2.1      phil  * Check and remove any MIC.
    721  1.23.2.1      phil  */
    722  1.23.2.1      phil int
    723  1.23.2.1      phil ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
    724  1.23.2.1      phil     struct mbuf *m, int force)
    725  1.23.2.1      phil {
    726  1.23.2.1      phil 	const struct ieee80211_cipher *cip;
    727  1.23.2.1      phil 	const struct ieee80211_rx_stats *rxs;
    728  1.23.2.1      phil 	struct ieee80211_frame *wh;
    729  1.23.2.1      phil 
    730  1.23.2.1      phil 	rxs = ieee80211_get_rx_params_ptr(m);
    731  1.23.2.1      phil 	wh = mtod(m, struct ieee80211_frame *);
    732  1.23.2.1      phil 
    733      1.21      maxv 	/*
    734  1.23.2.1      phil 	 * Handle demic / mic errors from hardware-decrypted offload devices.
    735      1.21      maxv 	 */
    736  1.23.2.1      phil 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
    737  1.23.2.1      phil 		if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) {
    738  1.23.2.1      phil 			/*
    739  1.23.2.1      phil 			 * Hardware has said MIC failed.  We don't care about
    740  1.23.2.1      phil 			 * whether it was stripped or not.
    741  1.23.2.1      phil 			 *
    742  1.23.2.1      phil 			 * Eventually - teach the demic methods in crypto
    743  1.23.2.1      phil 			 * modules to handle a NULL key and not to dereference
    744  1.23.2.1      phil 			 * it.
    745  1.23.2.1      phil 			 */
    746  1.23.2.1      phil 			ieee80211_notify_michael_failure(vap, wh, -1);
    747  1.23.2.1      phil 			return (0);
    748  1.23.2.1      phil 		}
    749  1.23.2.1      phil 
    750  1.23.2.1      phil 		if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) {
    751  1.23.2.1      phil 			/*
    752  1.23.2.1      phil 			 * Hardware has decrypted and not indicated a
    753  1.23.2.1      phil 			 * MIC failure and has stripped the MIC.
    754  1.23.2.1      phil 			 * We may not have a key, so for now just
    755  1.23.2.1      phil 			 * return OK.
    756  1.23.2.1      phil 			 */
    757  1.23.2.1      phil 			return (1);
    758  1.23.2.1      phil 		}
    759  1.23.2.1      phil 	}
    760  1.23.2.1      phil 
    761  1.23.2.1      phil 	/*
    762  1.23.2.1      phil 	 * If we don't have a key at this point then we don't
    763  1.23.2.1      phil 	 * have to demic anything.
    764  1.23.2.1      phil 	 */
    765  1.23.2.1      phil 	if (k == NULL)
    766  1.23.2.1      phil 		return (1);
    767  1.23.2.1      phil 
    768  1.23.2.1      phil 	cip = k->wk_cipher;
    769  1.23.2.1      phil 	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
    770  1.23.2.1      phil }
    771  1.23.2.1      phil 
    772  1.23.2.1      phil 
    773  1.23.2.1      phil static void
    774  1.23.2.1      phil load_ucastkey(void *arg, struct ieee80211_node *ni)
    775  1.23.2.1      phil {
    776  1.23.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    777  1.23.2.1      phil 	struct ieee80211_key *k;
    778  1.23.2.1      phil 
    779  1.23.2.1      phil 	if (vap->iv_state != IEEE80211_S_RUN)
    780  1.23.2.1      phil 		return;
    781  1.23.2.1      phil 	k = &ni->ni_ucastkey;
    782  1.23.2.1      phil 	if (k->wk_flags & IEEE80211_KEY_DEVKEY)
    783  1.23.2.1      phil 		dev_key_set(vap, k);
    784  1.23.2.1      phil }
    785  1.23.2.1      phil 
    786  1.23.2.1      phil /*
    787  1.23.2.1      phil  * Re-load all keys known to the 802.11 layer that may
    788  1.23.2.1      phil  * have hardware state backing them.  This is used by
    789  1.23.2.1      phil  * drivers on resume to push keys down into the device.
    790  1.23.2.1      phil  */
    791  1.23.2.1      phil void
    792  1.23.2.1      phil ieee80211_crypto_reload_keys(struct ieee80211com *ic)
    793  1.23.2.1      phil {
    794  1.23.2.1      phil 	struct ieee80211vap *vap;
    795  1.23.2.1      phil 	int i;
    796  1.23.2.1      phil 
    797  1.23.2.1      phil 	/*
    798  1.23.2.1      phil 	 * Keys in the global key table of each vap.
    799  1.23.2.1      phil 	 */
    800  1.23.2.1      phil 	/* NB: used only during resume so don't lock for now */
    801  1.23.2.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    802  1.23.2.1      phil 		if (vap->iv_state != IEEE80211_S_RUN)
    803  1.23.2.1      phil 			continue;
    804  1.23.2.1      phil 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    805  1.23.2.1      phil 			const struct ieee80211_key *k = &vap->iv_nw_keys[i];
    806  1.23.2.1      phil 			if (k->wk_flags & IEEE80211_KEY_DEVKEY)
    807  1.23.2.1      phil 				dev_key_set(vap, k);
    808  1.23.2.1      phil 		}
    809      1.21      maxv 	}
    810  1.23.2.1      phil 	/*
    811  1.23.2.1      phil 	 * Unicast keys.
    812  1.23.2.1      phil 	 */
    813  1.23.2.1      phil 	ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
    814  1.23.2.1      phil }
    815  1.23.2.1      phil 
    816  1.23.2.1      phil /*
    817  1.23.2.1      phil  * Set the default key index for WEP, or KEYIX_NONE for no default TX key.
    818  1.23.2.1      phil  *
    819  1.23.2.1      phil  * This should be done as part of a key update block (iv_key_update_begin /
    820  1.23.2.1      phil  * iv_key_update_end.)
    821  1.23.2.1      phil  */
    822  1.23.2.1      phil void
    823  1.23.2.1      phil ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
    824  1.23.2.1      phil {
    825  1.23.2.1      phil 
    826  1.23.2.1      phil 	/* XXX TODO: assert we're in a key update block */
    827      1.21      maxv 
    828  1.23.2.1      phil 	vap->iv_update_deftxkey(vap, kid);
    829       1.1    dyoung }
    830