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