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