Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto.c revision 1.1.1.4
      1      1.1  dyoung /*-
      2      1.1  dyoung  * Copyright (c) 2001 Atsushi Onoe
      3  1.1.1.3  dyoung  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      4      1.1  dyoung  * All rights reserved.
      5      1.1  dyoung  *
      6      1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      7      1.1  dyoung  * modification, are permitted provided that the following conditions
      8      1.1  dyoung  * are met:
      9      1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     10      1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     11      1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     14      1.1  dyoung  * 3. The name of the author may not be used to endorse or promote products
     15      1.1  dyoung  *    derived from this software without specific prior written permission.
     16      1.1  dyoung  *
     17      1.1  dyoung  * Alternatively, this software may be distributed under the terms of the
     18      1.1  dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     19      1.1  dyoung  * Software Foundation.
     20      1.1  dyoung  *
     21      1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1  dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1  dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1  dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1  dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1  dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1  dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1  dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1  dyoung  */
     32      1.1  dyoung 
     33      1.1  dyoung #include <sys/cdefs.h>
     34  1.1.1.4  dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto.c,v 1.10 2005/07/09 23:15:30 sam Exp $");
     35      1.1  dyoung 
     36  1.1.1.3  dyoung /*
     37  1.1.1.3  dyoung  * IEEE 802.11 generic crypto support.
     38  1.1.1.3  dyoung  */
     39      1.1  dyoung #include <sys/param.h>
     40      1.1  dyoung #include <sys/mbuf.h>
     41  1.1.1.3  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.1.1.3  dyoung #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.1.1.3  dyoung /*
     51  1.1.1.3  dyoung  * Table of registered cipher modules.
     52  1.1.1.3  dyoung  */
     53  1.1.1.3  dyoung static	const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX];
     54  1.1.1.3  dyoung 
     55  1.1.1.3  dyoung static	int _ieee80211_crypto_delkey(struct ieee80211com *,
     56  1.1.1.3  dyoung 		struct ieee80211_key *);
     57  1.1.1.3  dyoung 
     58  1.1.1.3  dyoung /*
     59  1.1.1.3  dyoung  * Default "null" key management routines.
     60  1.1.1.3  dyoung  */
     61  1.1.1.3  dyoung static int
     62  1.1.1.3  dyoung null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
     63  1.1.1.3  dyoung {
     64  1.1.1.4  dyoung 	if (!(&ic->ic_nw_keys[0] <= k &&
     65  1.1.1.4  dyoung 	     k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
     66  1.1.1.4  dyoung 		/*
     67  1.1.1.4  dyoung 		 * Not in the global key table, the driver should handle this
     68  1.1.1.4  dyoung 		 * by allocating a slot in the h/w key table/cache.  In
     69  1.1.1.4  dyoung 		 * lieu of that return key slot 0 for any unicast key
     70  1.1.1.4  dyoung 		 * request.  We disallow the request if this is a group key.
     71  1.1.1.4  dyoung 		 * This default policy does the right thing for legacy hardware
     72  1.1.1.4  dyoung 		 * with a 4 key table.  It also handles devices that pass
     73  1.1.1.4  dyoung 		 * packets through untouched when marked with the WEP bit
     74  1.1.1.4  dyoung 		 * and key index 0.
     75  1.1.1.4  dyoung 		 */
     76  1.1.1.4  dyoung 		if ((k->wk_flags & IEEE80211_KEY_GROUP) == 0)
     77  1.1.1.4  dyoung 			return 0;	/* NB: use key index 0 for ucast key */
     78  1.1.1.4  dyoung 		else
     79  1.1.1.4  dyoung 			return IEEE80211_KEYIX_NONE;
     80  1.1.1.4  dyoung 	}
     81  1.1.1.4  dyoung 	return k - ic->ic_nw_keys;
     82  1.1.1.3  dyoung }
     83  1.1.1.3  dyoung static int
     84  1.1.1.3  dyoung null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
     85  1.1.1.3  dyoung {
     86  1.1.1.3  dyoung 	return 1;
     87  1.1.1.3  dyoung }
     88  1.1.1.3  dyoung static 	int
     89  1.1.1.3  dyoung null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
     90  1.1.1.3  dyoung 	     const u_int8_t mac[IEEE80211_ADDR_LEN])
     91  1.1.1.3  dyoung {
     92  1.1.1.3  dyoung 	return 1;
     93  1.1.1.3  dyoung }
     94  1.1.1.3  dyoung static void null_key_update(struct ieee80211com *ic) {}
     95  1.1.1.3  dyoung 
     96  1.1.1.3  dyoung /*
     97  1.1.1.3  dyoung  * Write-arounds for common operations.
     98  1.1.1.3  dyoung  */
     99  1.1.1.3  dyoung static __inline void
    100  1.1.1.3  dyoung cipher_detach(struct ieee80211_key *key)
    101  1.1.1.3  dyoung {
    102  1.1.1.3  dyoung 	key->wk_cipher->ic_detach(key);
    103  1.1.1.3  dyoung }
    104      1.1  dyoung 
    105  1.1.1.3  dyoung static __inline void *
    106  1.1.1.3  dyoung cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key)
    107  1.1.1.3  dyoung {
    108  1.1.1.3  dyoung 	return key->wk_cipher->ic_attach(ic, key);
    109  1.1.1.3  dyoung }
    110      1.1  dyoung 
    111  1.1.1.3  dyoung /*
    112  1.1.1.3  dyoung  * Wrappers for driver key management methods.
    113  1.1.1.3  dyoung  */
    114  1.1.1.3  dyoung static __inline int
    115  1.1.1.3  dyoung dev_key_alloc(struct ieee80211com *ic,
    116  1.1.1.3  dyoung 	const struct ieee80211_key *key)
    117  1.1.1.3  dyoung {
    118  1.1.1.3  dyoung 	return ic->ic_crypto.cs_key_alloc(ic, key);
    119  1.1.1.3  dyoung }
    120      1.1  dyoung 
    121  1.1.1.3  dyoung static __inline int
    122  1.1.1.3  dyoung dev_key_delete(struct ieee80211com *ic,
    123  1.1.1.3  dyoung 	const struct ieee80211_key *key)
    124  1.1.1.3  dyoung {
    125  1.1.1.3  dyoung 	return ic->ic_crypto.cs_key_delete(ic, key);
    126  1.1.1.3  dyoung }
    127  1.1.1.3  dyoung 
    128  1.1.1.3  dyoung static __inline int
    129  1.1.1.3  dyoung dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key,
    130  1.1.1.3  dyoung 	const u_int8_t mac[IEEE80211_ADDR_LEN])
    131  1.1.1.3  dyoung {
    132  1.1.1.3  dyoung 	return ic->ic_crypto.cs_key_set(ic, key, mac);
    133  1.1.1.3  dyoung }
    134  1.1.1.3  dyoung 
    135  1.1.1.3  dyoung /*
    136  1.1.1.3  dyoung  * Setup crypto support.
    137  1.1.1.3  dyoung  */
    138      1.1  dyoung void
    139  1.1.1.3  dyoung ieee80211_crypto_attach(struct ieee80211com *ic)
    140      1.1  dyoung {
    141  1.1.1.3  dyoung 	struct ieee80211_crypto_state *cs = &ic->ic_crypto;
    142  1.1.1.3  dyoung 	int i;
    143      1.1  dyoung 
    144  1.1.1.3  dyoung 	/* NB: we assume everything is pre-zero'd */
    145  1.1.1.3  dyoung 	cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
    146  1.1.1.3  dyoung 	ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
    147  1.1.1.3  dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
    148  1.1.1.3  dyoung 		ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
    149  1.1.1.3  dyoung 			IEEE80211_KEYIX_NONE);
    150      1.1  dyoung 	/*
    151  1.1.1.3  dyoung 	 * Initialize the driver key support routines to noop entries.
    152  1.1.1.3  dyoung 	 * This is useful especially for the cipher test modules.
    153      1.1  dyoung 	 */
    154  1.1.1.3  dyoung 	cs->cs_key_alloc = null_key_alloc;
    155  1.1.1.3  dyoung 	cs->cs_key_set = null_key_set;
    156  1.1.1.3  dyoung 	cs->cs_key_delete = null_key_delete;
    157  1.1.1.3  dyoung 	cs->cs_key_update_begin = null_key_update;
    158  1.1.1.3  dyoung 	cs->cs_key_update_end = null_key_update;
    159      1.1  dyoung }
    160      1.1  dyoung 
    161  1.1.1.3  dyoung /*
    162  1.1.1.3  dyoung  * Teardown crypto support.
    163  1.1.1.3  dyoung  */
    164      1.1  dyoung void
    165  1.1.1.3  dyoung ieee80211_crypto_detach(struct ieee80211com *ic)
    166      1.1  dyoung {
    167  1.1.1.3  dyoung 	ieee80211_crypto_delglobalkeys(ic);
    168  1.1.1.3  dyoung }
    169      1.1  dyoung 
    170  1.1.1.3  dyoung /*
    171  1.1.1.3  dyoung  * Register a crypto cipher module.
    172  1.1.1.3  dyoung  */
    173  1.1.1.3  dyoung void
    174  1.1.1.3  dyoung ieee80211_crypto_register(const struct ieee80211_cipher *cip)
    175  1.1.1.3  dyoung {
    176  1.1.1.3  dyoung 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
    177  1.1.1.3  dyoung 		printf("%s: cipher %s has an invalid cipher index %u\n",
    178  1.1.1.3  dyoung 			__func__, cip->ic_name, cip->ic_cipher);
    179  1.1.1.3  dyoung 		return;
    180      1.1  dyoung 	}
    181  1.1.1.3  dyoung 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
    182  1.1.1.3  dyoung 		printf("%s: cipher %s registered with a different template\n",
    183  1.1.1.3  dyoung 			__func__, cip->ic_name);
    184  1.1.1.3  dyoung 		return;
    185  1.1.1.3  dyoung 	}
    186  1.1.1.3  dyoung 	ciphers[cip->ic_cipher] = cip;
    187      1.1  dyoung }
    188      1.1  dyoung 
    189  1.1.1.3  dyoung /*
    190  1.1.1.3  dyoung  * Unregister a crypto cipher module.
    191  1.1.1.3  dyoung  */
    192  1.1.1.3  dyoung void
    193  1.1.1.3  dyoung ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
    194      1.1  dyoung {
    195  1.1.1.3  dyoung 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
    196  1.1.1.3  dyoung 		printf("%s: cipher %s has an invalid cipher index %u\n",
    197  1.1.1.3  dyoung 			__func__, cip->ic_name, cip->ic_cipher);
    198  1.1.1.3  dyoung 		return;
    199      1.1  dyoung 	}
    200  1.1.1.3  dyoung 	if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
    201  1.1.1.3  dyoung 		printf("%s: cipher %s registered with a different template\n",
    202  1.1.1.3  dyoung 			__func__, cip->ic_name);
    203  1.1.1.3  dyoung 		return;
    204  1.1.1.3  dyoung 	}
    205  1.1.1.3  dyoung 	/* NB: don't complain about not being registered */
    206  1.1.1.3  dyoung 	/* XXX disallow if references */
    207  1.1.1.3  dyoung 	ciphers[cip->ic_cipher] = NULL;
    208  1.1.1.3  dyoung }
    209  1.1.1.3  dyoung 
    210  1.1.1.3  dyoung int
    211  1.1.1.3  dyoung ieee80211_crypto_available(u_int cipher)
    212  1.1.1.3  dyoung {
    213  1.1.1.3  dyoung 	return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
    214  1.1.1.3  dyoung }
    215  1.1.1.3  dyoung 
    216  1.1.1.3  dyoung /* XXX well-known names! */
    217  1.1.1.3  dyoung static const char *cipher_modnames[] = {
    218  1.1.1.3  dyoung 	"wlan_wep",	/* IEEE80211_CIPHER_WEP */
    219  1.1.1.3  dyoung 	"wlan_tkip",	/* IEEE80211_CIPHER_TKIP */
    220  1.1.1.3  dyoung 	"wlan_aes_ocb",	/* IEEE80211_CIPHER_AES_OCB */
    221  1.1.1.3  dyoung 	"wlan_ccmp",	/* IEEE80211_CIPHER_AES_CCM */
    222  1.1.1.3  dyoung 	"wlan_ckip",	/* IEEE80211_CIPHER_CKIP */
    223  1.1.1.3  dyoung };
    224  1.1.1.3  dyoung 
    225  1.1.1.3  dyoung /*
    226  1.1.1.3  dyoung  * Establish a relationship between the specified key and cipher
    227  1.1.1.3  dyoung  * and, if necessary, allocate a hardware index from the driver.
    228  1.1.1.3  dyoung  * Note that when a fixed key index is required it must be specified
    229  1.1.1.3  dyoung  * and we blindly assign it w/o consulting the driver (XXX).
    230  1.1.1.3  dyoung  *
    231  1.1.1.3  dyoung  * This must be the first call applied to a key; all the other key
    232  1.1.1.3  dyoung  * routines assume wk_cipher is setup.
    233  1.1.1.3  dyoung  *
    234  1.1.1.3  dyoung  * Locking must be handled by the caller using:
    235  1.1.1.3  dyoung  *	ieee80211_key_update_begin(ic);
    236  1.1.1.3  dyoung  *	ieee80211_key_update_end(ic);
    237  1.1.1.3  dyoung  */
    238  1.1.1.3  dyoung int
    239  1.1.1.3  dyoung ieee80211_crypto_newkey(struct ieee80211com *ic,
    240  1.1.1.3  dyoung 	int cipher, int flags, struct ieee80211_key *key)
    241  1.1.1.3  dyoung {
    242  1.1.1.3  dyoung #define	N(a)	(sizeof(a) / sizeof(a[0]))
    243  1.1.1.3  dyoung 	const struct ieee80211_cipher *cip;
    244  1.1.1.3  dyoung 	void *keyctx;
    245  1.1.1.3  dyoung 	int oflags;
    246  1.1.1.3  dyoung 
    247  1.1.1.3  dyoung 	/*
    248  1.1.1.3  dyoung 	 * Validate cipher and set reference to cipher routines.
    249  1.1.1.3  dyoung 	 */
    250  1.1.1.3  dyoung 	if (cipher >= IEEE80211_CIPHER_MAX) {
    251  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    252  1.1.1.3  dyoung 			"%s: invalid cipher %u\n", __func__, cipher);
    253  1.1.1.3  dyoung 		ic->ic_stats.is_crypto_badcipher++;
    254  1.1.1.3  dyoung 		return 0;
    255      1.1  dyoung 	}
    256  1.1.1.3  dyoung 	cip = ciphers[cipher];
    257  1.1.1.3  dyoung 	if (cip == NULL) {
    258      1.1  dyoung 		/*
    259  1.1.1.3  dyoung 		 * Auto-load cipher module if we have a well-known name
    260  1.1.1.3  dyoung 		 * for it.  It might be better to use string names rather
    261  1.1.1.3  dyoung 		 * than numbers and craft a module name based on the cipher
    262  1.1.1.3  dyoung 		 * name; e.g. wlan_cipher_<cipher-name>.
    263      1.1  dyoung 		 */
    264  1.1.1.3  dyoung 		if (cipher < N(cipher_modnames)) {
    265  1.1.1.3  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    266  1.1.1.3  dyoung 				"%s: unregistered cipher %u, load module %s\n",
    267  1.1.1.3  dyoung 				__func__, cipher, cipher_modnames[cipher]);
    268  1.1.1.3  dyoung 			ieee80211_load_module(cipher_modnames[cipher]);
    269  1.1.1.3  dyoung 			/*
    270  1.1.1.3  dyoung 			 * If cipher module loaded it should immediately
    271  1.1.1.3  dyoung 			 * call ieee80211_crypto_register which will fill
    272  1.1.1.3  dyoung 			 * in the entry in the ciphers array.
    273  1.1.1.3  dyoung 			 */
    274  1.1.1.3  dyoung 			cip = ciphers[cipher];
    275      1.1  dyoung 		}
    276  1.1.1.3  dyoung 		if (cip == NULL) {
    277  1.1.1.3  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    278  1.1.1.3  dyoung 				"%s: unable to load cipher %u, module %s\n",
    279  1.1.1.3  dyoung 				__func__, cipher,
    280  1.1.1.3  dyoung 				cipher < N(cipher_modnames) ?
    281  1.1.1.3  dyoung 					cipher_modnames[cipher] : "<unknown>");
    282  1.1.1.3  dyoung 			ic->ic_stats.is_crypto_nocipher++;
    283  1.1.1.3  dyoung 			return 0;
    284      1.1  dyoung 		}
    285  1.1.1.3  dyoung 	}
    286  1.1.1.3  dyoung 
    287  1.1.1.3  dyoung 	oflags = key->wk_flags;
    288  1.1.1.3  dyoung 	flags &= IEEE80211_KEY_COMMON;
    289  1.1.1.3  dyoung 	/*
    290  1.1.1.3  dyoung 	 * If the hardware does not support the cipher then
    291  1.1.1.3  dyoung 	 * fallback to a host-based implementation.
    292  1.1.1.3  dyoung 	 */
    293  1.1.1.3  dyoung 	if ((ic->ic_caps & (1<<cipher)) == 0) {
    294  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    295  1.1.1.3  dyoung 		    "%s: no h/w support for cipher %s, falling back to s/w\n",
    296  1.1.1.3  dyoung 		    __func__, cip->ic_name);
    297  1.1.1.3  dyoung 		flags |= IEEE80211_KEY_SWCRYPT;
    298  1.1.1.3  dyoung 	}
    299  1.1.1.3  dyoung 	/*
    300  1.1.1.3  dyoung 	 * Hardware TKIP with software MIC is an important
    301  1.1.1.3  dyoung 	 * combination; we handle it by flagging each key,
    302  1.1.1.3  dyoung 	 * the cipher modules honor it.
    303  1.1.1.3  dyoung 	 */
    304  1.1.1.3  dyoung 	if (cipher == IEEE80211_CIPHER_TKIP &&
    305  1.1.1.3  dyoung 	    (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
    306  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    307  1.1.1.3  dyoung 		    "%s: no h/w support for TKIP MIC, falling back to s/w\n",
    308  1.1.1.3  dyoung 		    __func__);
    309  1.1.1.3  dyoung 		flags |= IEEE80211_KEY_SWMIC;
    310  1.1.1.3  dyoung 	}
    311  1.1.1.3  dyoung 
    312  1.1.1.3  dyoung 	/*
    313  1.1.1.3  dyoung 	 * Bind cipher to key instance.  Note we do this
    314  1.1.1.3  dyoung 	 * after checking the device capabilities so the
    315  1.1.1.3  dyoung 	 * cipher module can optimize space usage based on
    316  1.1.1.3  dyoung 	 * whether or not it needs to do the cipher work.
    317  1.1.1.3  dyoung 	 */
    318  1.1.1.3  dyoung 	if (key->wk_cipher != cip || key->wk_flags != flags) {
    319  1.1.1.3  dyoung again:
    320  1.1.1.3  dyoung 		/*
    321  1.1.1.3  dyoung 		 * Fillin the flags so cipher modules can see s/w
    322  1.1.1.3  dyoung 		 * crypto requirements and potentially allocate
    323  1.1.1.3  dyoung 		 * different state and/or attach different method
    324  1.1.1.3  dyoung 		 * pointers.
    325  1.1.1.3  dyoung 		 *
    326  1.1.1.3  dyoung 		 * XXX this is not right when s/w crypto fallback
    327  1.1.1.3  dyoung 		 *     fails and we try to restore previous state.
    328  1.1.1.3  dyoung 		 */
    329  1.1.1.3  dyoung 		key->wk_flags = flags;
    330  1.1.1.3  dyoung 		keyctx = cip->ic_attach(ic, key);
    331  1.1.1.3  dyoung 		if (keyctx == NULL) {
    332  1.1.1.3  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    333  1.1.1.3  dyoung 				"%s: unable to attach cipher %s\n",
    334  1.1.1.3  dyoung 				__func__, cip->ic_name);
    335  1.1.1.3  dyoung 			key->wk_flags = oflags;	/* restore old flags */
    336  1.1.1.3  dyoung 			ic->ic_stats.is_crypto_attachfail++;
    337  1.1.1.3  dyoung 			return 0;
    338      1.1  dyoung 		}
    339  1.1.1.3  dyoung 		cipher_detach(key);
    340  1.1.1.3  dyoung 		key->wk_cipher = cip;		/* XXX refcnt? */
    341  1.1.1.3  dyoung 		key->wk_private = keyctx;
    342  1.1.1.3  dyoung 	}
    343  1.1.1.3  dyoung 	/*
    344  1.1.1.3  dyoung 	 * Commit to requested usage so driver can see the flags.
    345  1.1.1.3  dyoung 	 */
    346  1.1.1.3  dyoung 	key->wk_flags = flags;
    347  1.1.1.3  dyoung 
    348  1.1.1.3  dyoung 	/*
    349  1.1.1.3  dyoung 	 * Ask the driver for a key index if we don't have one.
    350  1.1.1.3  dyoung 	 * Note that entries in the global key table always have
    351  1.1.1.3  dyoung 	 * an index; this means it's safe to call this routine
    352  1.1.1.3  dyoung 	 * for these entries just to setup the reference to the
    353  1.1.1.3  dyoung 	 * cipher template.  Note also that when using software
    354  1.1.1.3  dyoung 	 * crypto we also call the driver to give us a key index.
    355  1.1.1.3  dyoung 	 */
    356  1.1.1.3  dyoung 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
    357  1.1.1.3  dyoung 		key->wk_keyix = dev_key_alloc(ic, key);
    358  1.1.1.3  dyoung 		if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
    359  1.1.1.3  dyoung 			/*
    360  1.1.1.3  dyoung 			 * Driver has no room; fallback to doing crypto
    361  1.1.1.3  dyoung 			 * in the host.  We change the flags and start the
    362  1.1.1.3  dyoung 			 * procedure over.  If we get back here then there's
    363  1.1.1.3  dyoung 			 * no hope and we bail.  Note that this can leave
    364  1.1.1.3  dyoung 			 * the key in a inconsistent state if the caller
    365  1.1.1.3  dyoung 			 * continues to use it.
    366  1.1.1.3  dyoung 			 */
    367  1.1.1.3  dyoung 			if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
    368  1.1.1.3  dyoung 				ic->ic_stats.is_crypto_swfallback++;
    369  1.1.1.3  dyoung 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    370  1.1.1.3  dyoung 				    "%s: no h/w resources for cipher %s, "
    371  1.1.1.3  dyoung 				    "falling back to s/w\n", __func__,
    372  1.1.1.3  dyoung 				    cip->ic_name);
    373  1.1.1.3  dyoung 				oflags = key->wk_flags;
    374  1.1.1.3  dyoung 				flags |= IEEE80211_KEY_SWCRYPT;
    375  1.1.1.3  dyoung 				if (cipher == IEEE80211_CIPHER_TKIP)
    376  1.1.1.3  dyoung 					flags |= IEEE80211_KEY_SWMIC;
    377  1.1.1.3  dyoung 				goto again;
    378  1.1.1.2  dyoung 			}
    379  1.1.1.3  dyoung 			ic->ic_stats.is_crypto_keyfail++;
    380  1.1.1.3  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    381  1.1.1.3  dyoung 			    "%s: unable to setup cipher %s\n",
    382  1.1.1.3  dyoung 			    __func__, cip->ic_name);
    383  1.1.1.3  dyoung 			return 0;
    384      1.1  dyoung 		}
    385  1.1.1.3  dyoung 	}
    386  1.1.1.3  dyoung 	return 1;
    387  1.1.1.3  dyoung #undef N
    388  1.1.1.3  dyoung }
    389  1.1.1.3  dyoung 
    390  1.1.1.3  dyoung /*
    391  1.1.1.3  dyoung  * Remove the key (no locking, for internal use).
    392  1.1.1.3  dyoung  */
    393  1.1.1.3  dyoung static int
    394  1.1.1.3  dyoung _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
    395  1.1.1.3  dyoung {
    396  1.1.1.3  dyoung 	u_int16_t keyix;
    397  1.1.1.3  dyoung 
    398  1.1.1.3  dyoung 	KASSERT(key->wk_cipher != NULL, ("No cipher!"));
    399  1.1.1.3  dyoung 
    400  1.1.1.3  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    401  1.1.1.3  dyoung 	    "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
    402  1.1.1.3  dyoung 	    __func__, key->wk_cipher->ic_name,
    403  1.1.1.3  dyoung 	    key->wk_keyix, key->wk_flags,
    404  1.1.1.3  dyoung 	    key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
    405  1.1.1.3  dyoung 
    406  1.1.1.3  dyoung 	keyix = key->wk_keyix;
    407  1.1.1.3  dyoung 	if (keyix != IEEE80211_KEYIX_NONE) {
    408  1.1.1.3  dyoung 		/*
    409  1.1.1.3  dyoung 		 * Remove hardware entry.
    410  1.1.1.3  dyoung 		 */
    411  1.1.1.3  dyoung 		/* XXX key cache */
    412  1.1.1.3  dyoung 		if (!dev_key_delete(ic, key)) {
    413  1.1.1.3  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    414  1.1.1.3  dyoung 			    "%s: driver did not delete key index %u\n",
    415  1.1.1.3  dyoung 			    __func__, keyix);
    416  1.1.1.3  dyoung 			ic->ic_stats.is_crypto_delkey++;
    417  1.1.1.3  dyoung 			/* XXX recovery? */
    418      1.1  dyoung 		}
    419      1.1  dyoung 	}
    420  1.1.1.3  dyoung 	cipher_detach(key);
    421  1.1.1.3  dyoung 	memset(key, 0, sizeof(*key));
    422  1.1.1.3  dyoung 	ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
    423  1.1.1.3  dyoung 	return 1;
    424  1.1.1.3  dyoung }
    425      1.1  dyoung 
    426  1.1.1.3  dyoung /*
    427  1.1.1.3  dyoung  * Remove the specified key.
    428  1.1.1.3  dyoung  */
    429  1.1.1.3  dyoung int
    430  1.1.1.3  dyoung ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key)
    431  1.1.1.3  dyoung {
    432  1.1.1.3  dyoung 	int status;
    433  1.1.1.3  dyoung 
    434  1.1.1.3  dyoung 	ieee80211_key_update_begin(ic);
    435  1.1.1.3  dyoung 	status = _ieee80211_crypto_delkey(ic, key);
    436  1.1.1.3  dyoung 	ieee80211_key_update_end(ic);
    437  1.1.1.3  dyoung 	return status;
    438      1.1  dyoung }
    439      1.1  dyoung 
    440      1.1  dyoung /*
    441  1.1.1.3  dyoung  * Clear the global key table.
    442      1.1  dyoung  */
    443  1.1.1.3  dyoung void
    444  1.1.1.3  dyoung ieee80211_crypto_delglobalkeys(struct ieee80211com *ic)
    445  1.1.1.3  dyoung {
    446  1.1.1.3  dyoung 	int i;
    447      1.1  dyoung 
    448  1.1.1.3  dyoung 	ieee80211_key_update_begin(ic);
    449  1.1.1.3  dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
    450  1.1.1.3  dyoung 		(void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]);
    451  1.1.1.3  dyoung 	ieee80211_key_update_end(ic);
    452  1.1.1.3  dyoung }
    453      1.1  dyoung 
    454  1.1.1.3  dyoung /*
    455  1.1.1.3  dyoung  * Set the contents of the specified key.
    456  1.1.1.3  dyoung  *
    457  1.1.1.3  dyoung  * Locking must be handled by the caller using:
    458  1.1.1.3  dyoung  *	ieee80211_key_update_begin(ic);
    459  1.1.1.3  dyoung  *	ieee80211_key_update_end(ic);
    460  1.1.1.3  dyoung  */
    461  1.1.1.3  dyoung int
    462  1.1.1.3  dyoung ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key,
    463  1.1.1.3  dyoung 		const u_int8_t macaddr[IEEE80211_ADDR_LEN])
    464      1.1  dyoung {
    465  1.1.1.3  dyoung 	const struct ieee80211_cipher *cip = key->wk_cipher;
    466      1.1  dyoung 
    467  1.1.1.3  dyoung 	KASSERT(cip != NULL, ("No cipher!"));
    468  1.1.1.3  dyoung 
    469  1.1.1.3  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    470  1.1.1.3  dyoung 	    "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
    471  1.1.1.3  dyoung 	    __func__, cip->ic_name, key->wk_keyix,
    472  1.1.1.3  dyoung 	    key->wk_flags, ether_sprintf(macaddr),
    473  1.1.1.3  dyoung 	    key->wk_keyrsc, key->wk_keytsc, key->wk_keylen);
    474  1.1.1.3  dyoung 
    475  1.1.1.3  dyoung 	/*
    476  1.1.1.3  dyoung 	 * Give cipher a chance to validate key contents.
    477  1.1.1.3  dyoung 	 * XXX should happen before modifying state.
    478  1.1.1.3  dyoung 	 */
    479  1.1.1.3  dyoung 	if (!cip->ic_setkey(key)) {
    480  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    481  1.1.1.3  dyoung 		    "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
    482  1.1.1.3  dyoung 		    __func__, cip->ic_name, key->wk_keyix,
    483  1.1.1.3  dyoung 		    key->wk_keylen, key->wk_flags);
    484  1.1.1.3  dyoung 		ic->ic_stats.is_crypto_setkey_cipher++;
    485  1.1.1.3  dyoung 		return 0;
    486  1.1.1.3  dyoung 	}
    487  1.1.1.3  dyoung 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
    488  1.1.1.3  dyoung 		/* XXX nothing allocated, should not happen */
    489  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    490  1.1.1.3  dyoung 		    "%s: no key index; should not happen!\n", __func__);
    491  1.1.1.3  dyoung 		ic->ic_stats.is_crypto_setkey_nokey++;
    492  1.1.1.3  dyoung 		return 0;
    493      1.1  dyoung 	}
    494  1.1.1.3  dyoung 	return dev_key_set(ic, key, macaddr);
    495      1.1  dyoung }
    496      1.1  dyoung 
    497      1.1  dyoung /*
    498  1.1.1.3  dyoung  * Add privacy headers appropriate for the specified key.
    499      1.1  dyoung  */
    500  1.1.1.3  dyoung struct ieee80211_key *
    501  1.1.1.3  dyoung ieee80211_crypto_encap(struct ieee80211com *ic,
    502  1.1.1.3  dyoung 	struct ieee80211_node *ni, struct mbuf *m)
    503  1.1.1.3  dyoung {
    504  1.1.1.3  dyoung 	struct ieee80211_key *k;
    505  1.1.1.3  dyoung 	struct ieee80211_frame *wh;
    506  1.1.1.3  dyoung 	const struct ieee80211_cipher *cip;
    507  1.1.1.3  dyoung 	u_int8_t keyid;
    508      1.1  dyoung 
    509  1.1.1.3  dyoung 	/*
    510  1.1.1.3  dyoung 	 * Multicast traffic always uses the multicast key.
    511  1.1.1.3  dyoung 	 * Otherwise if a unicast key is set we use that and
    512  1.1.1.3  dyoung 	 * it is always key index 0.  When no unicast key is
    513  1.1.1.3  dyoung 	 * set we fall back to the default transmit key.
    514  1.1.1.3  dyoung 	 */
    515  1.1.1.3  dyoung 	wh = mtod(m, struct ieee80211_frame *);
    516  1.1.1.3  dyoung 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    517  1.1.1.3  dyoung 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
    518  1.1.1.3  dyoung 		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
    519  1.1.1.3  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    520  1.1.1.3  dyoung 			    "[%s] no default transmit key (%s) deftxkey %u\n",
    521  1.1.1.3  dyoung 			    ether_sprintf(wh->i_addr1), __func__,
    522  1.1.1.3  dyoung 			    ic->ic_def_txkey);
    523  1.1.1.3  dyoung 			ic->ic_stats.is_tx_nodefkey++;
    524  1.1.1.3  dyoung 			return NULL;
    525  1.1.1.3  dyoung 		}
    526  1.1.1.3  dyoung 		keyid = ic->ic_def_txkey;
    527  1.1.1.3  dyoung 		k = &ic->ic_nw_keys[ic->ic_def_txkey];
    528  1.1.1.3  dyoung 	} else {
    529  1.1.1.3  dyoung 		keyid = 0;
    530  1.1.1.3  dyoung 		k = &ni->ni_ucastkey;
    531  1.1.1.3  dyoung 	}
    532  1.1.1.3  dyoung 	cip = k->wk_cipher;
    533  1.1.1.3  dyoung 	return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
    534  1.1.1.3  dyoung }
    535  1.1.1.3  dyoung 
    536  1.1.1.3  dyoung /*
    537  1.1.1.3  dyoung  * Validate and strip privacy headers (and trailer) for a
    538  1.1.1.3  dyoung  * received frame that has the WEP/Privacy bit set.
    539  1.1.1.3  dyoung  */
    540  1.1.1.3  dyoung struct ieee80211_key *
    541  1.1.1.3  dyoung ieee80211_crypto_decap(struct ieee80211com *ic,
    542  1.1.1.4  dyoung 	struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
    543      1.1  dyoung {
    544  1.1.1.3  dyoung #define	IEEE80211_WEP_HDRLEN	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
    545  1.1.1.3  dyoung #define	IEEE80211_WEP_MINLEN \
    546  1.1.1.3  dyoung 	(sizeof(struct ieee80211_frame) + ETHER_HDR_LEN + \
    547  1.1.1.3  dyoung 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
    548  1.1.1.3  dyoung 	struct ieee80211_key *k;
    549  1.1.1.3  dyoung 	struct ieee80211_frame *wh;
    550  1.1.1.3  dyoung 	const struct ieee80211_cipher *cip;
    551  1.1.1.3  dyoung 	const u_int8_t *ivp;
    552  1.1.1.3  dyoung 	u_int8_t keyid;
    553  1.1.1.3  dyoung 
    554  1.1.1.3  dyoung 	/* NB: this minimum size data frame could be bigger */
    555  1.1.1.3  dyoung 	if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
    556  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    557  1.1.1.3  dyoung 			"%s: WEP data frame too short, len %u\n",
    558  1.1.1.3  dyoung 			__func__, m->m_pkthdr.len);
    559  1.1.1.3  dyoung 		ic->ic_stats.is_rx_tooshort++;	/* XXX need unique stat? */
    560  1.1.1.3  dyoung 		return NULL;
    561  1.1.1.3  dyoung 	}
    562  1.1.1.3  dyoung 
    563  1.1.1.3  dyoung 	/*
    564  1.1.1.3  dyoung 	 * Locate the key. If unicast and there is no unicast
    565  1.1.1.3  dyoung 	 * key then we fall back to the key id in the header.
    566  1.1.1.3  dyoung 	 * This assumes unicast keys are only configured when
    567  1.1.1.3  dyoung 	 * the key id in the header is meaningless (typically 0).
    568  1.1.1.3  dyoung 	 */
    569  1.1.1.3  dyoung 	wh = mtod(m, struct ieee80211_frame *);
    570  1.1.1.3  dyoung 	ivp = mtod(m, const u_int8_t *) + hdrlen;	/* XXX contig */
    571  1.1.1.3  dyoung 	keyid = ivp[IEEE80211_WEP_IVLEN];
    572  1.1.1.3  dyoung 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    573  1.1.1.3  dyoung 	    ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none)
    574  1.1.1.3  dyoung 		k = &ic->ic_nw_keys[keyid >> 6];
    575  1.1.1.3  dyoung 	else
    576  1.1.1.3  dyoung 		k = &ni->ni_ucastkey;
    577  1.1.1.3  dyoung 
    578  1.1.1.3  dyoung 	/*
    579  1.1.1.3  dyoung 	 * Insure crypto header is contiguous for all decap work.
    580  1.1.1.3  dyoung 	 */
    581  1.1.1.3  dyoung 	cip = k->wk_cipher;
    582  1.1.1.3  dyoung 	if (m->m_len < hdrlen + cip->ic_header &&
    583  1.1.1.3  dyoung 	    (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
    584  1.1.1.3  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    585  1.1.1.3  dyoung 		    "[%s] unable to pullup %s header\n",
    586  1.1.1.3  dyoung 		    ether_sprintf(wh->i_addr2), cip->ic_name);
    587  1.1.1.3  dyoung 		ic->ic_stats.is_rx_wepfail++;	/* XXX */
    588  1.1.1.3  dyoung 		return 0;
    589  1.1.1.3  dyoung 	}
    590      1.1  dyoung 
    591  1.1.1.4  dyoung 	return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
    592  1.1.1.3  dyoung #undef IEEE80211_WEP_MINLEN
    593  1.1.1.3  dyoung #undef IEEE80211_WEP_HDRLEN
    594      1.1  dyoung }
    595