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