11a1e1d21SSam Leffler /*- 27535e66aSSam Leffler * Copyright (c) 2001 Atsushi Onoe 38a1b9b6aSSam Leffler * Copyright (c) 2002-2004 Sam Leffler, Errno Consulting 41a1e1d21SSam Leffler * All rights reserved. 51a1e1d21SSam Leffler * 61a1e1d21SSam Leffler * Redistribution and use in source and binary forms, with or without 71a1e1d21SSam Leffler * modification, are permitted provided that the following conditions 81a1e1d21SSam Leffler * are met: 91a1e1d21SSam Leffler * 1. Redistributions of source code must retain the above copyright 107535e66aSSam Leffler * notice, this list of conditions and the following disclaimer. 117535e66aSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 127535e66aSSam Leffler * notice, this list of conditions and the following disclaimer in the 137535e66aSSam Leffler * documentation and/or other materials provided with the distribution. 147535e66aSSam Leffler * 3. The name of the author may not be used to endorse or promote products 157535e66aSSam Leffler * derived from this software without specific prior written permission. 161a1e1d21SSam Leffler * 171a1e1d21SSam Leffler * Alternatively, this software may be distributed under the terms of the 181a1e1d21SSam Leffler * GNU General Public License ("GPL") version 2 as published by the Free 191a1e1d21SSam Leffler * Software Foundation. 201a1e1d21SSam Leffler * 217535e66aSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 227535e66aSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 237535e66aSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 247535e66aSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 257535e66aSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 267535e66aSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 277535e66aSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 287535e66aSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 297535e66aSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 307535e66aSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 311a1e1d21SSam Leffler */ 321a1e1d21SSam Leffler 331a1e1d21SSam Leffler #include <sys/cdefs.h> 341a1e1d21SSam Leffler __FBSDID("$FreeBSD$"); 351a1e1d21SSam Leffler 368a1b9b6aSSam Leffler /* 378a1b9b6aSSam Leffler * IEEE 802.11 generic crypto support. 388a1b9b6aSSam Leffler */ 391a1e1d21SSam Leffler #include <sys/param.h> 401a1e1d21SSam Leffler #include <sys/mbuf.h> 411a1e1d21SSam Leffler 428a1b9b6aSSam Leffler #include <sys/socket.h> 431a1e1d21SSam Leffler 441a1e1d21SSam Leffler #include <net/if.h> 451a1e1d21SSam Leffler #include <net/if_media.h> 468a1b9b6aSSam Leffler #include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 471a1e1d21SSam Leffler 481a1e1d21SSam Leffler #include <net80211/ieee80211_var.h> 491a1e1d21SSam Leffler 508a1b9b6aSSam Leffler /* 518a1b9b6aSSam Leffler * Table of registered cipher modules. 528a1b9b6aSSam Leffler */ 538a1b9b6aSSam Leffler static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 541a1e1d21SSam Leffler 558a1b9b6aSSam Leffler static int _ieee80211_crypto_delkey(struct ieee80211com *, 568a1b9b6aSSam Leffler struct ieee80211_key *); 571a1e1d21SSam Leffler 588a1b9b6aSSam Leffler /* 598a1b9b6aSSam Leffler * Default "null" key management routines. 608a1b9b6aSSam Leffler */ 618a1b9b6aSSam Leffler static int 628a1b9b6aSSam Leffler null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k) 631a1e1d21SSam Leffler { 648a1b9b6aSSam Leffler return IEEE80211_KEYIX_NONE; 658a1b9b6aSSam Leffler } 668a1b9b6aSSam Leffler static int 678a1b9b6aSSam Leffler null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k) 688a1b9b6aSSam Leffler { 698a1b9b6aSSam Leffler return 1; 708a1b9b6aSSam Leffler } 718a1b9b6aSSam Leffler static int 728a1b9b6aSSam Leffler null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k, 738a1b9b6aSSam Leffler const u_int8_t mac[IEEE80211_ADDR_LEN]) 748a1b9b6aSSam Leffler { 758a1b9b6aSSam Leffler return 1; 768a1b9b6aSSam Leffler } 778a1b9b6aSSam Leffler static void null_key_update(struct ieee80211com *ic) {} 788a1b9b6aSSam Leffler 798a1b9b6aSSam Leffler /* 808a1b9b6aSSam Leffler * Write-arounds for common operations. 818a1b9b6aSSam Leffler */ 828a1b9b6aSSam Leffler static __inline void 838a1b9b6aSSam Leffler cipher_detach(struct ieee80211_key *key) 848a1b9b6aSSam Leffler { 858a1b9b6aSSam Leffler key->wk_cipher->ic_detach(key); 868a1b9b6aSSam Leffler } 878a1b9b6aSSam Leffler 888a1b9b6aSSam Leffler static __inline void * 898a1b9b6aSSam Leffler cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key) 908a1b9b6aSSam Leffler { 918a1b9b6aSSam Leffler return key->wk_cipher->ic_attach(ic, key); 928a1b9b6aSSam Leffler } 938a1b9b6aSSam Leffler 948a1b9b6aSSam Leffler /* 958a1b9b6aSSam Leffler * Wrappers for driver key management methods. 968a1b9b6aSSam Leffler */ 978a1b9b6aSSam Leffler static __inline int 988a1b9b6aSSam Leffler dev_key_alloc(struct ieee80211com *ic, 998a1b9b6aSSam Leffler const struct ieee80211_key *key) 1008a1b9b6aSSam Leffler { 1018a1b9b6aSSam Leffler return ic->ic_crypto.cs_key_alloc(ic, key); 1028a1b9b6aSSam Leffler } 1038a1b9b6aSSam Leffler 1048a1b9b6aSSam Leffler static __inline int 1058a1b9b6aSSam Leffler dev_key_delete(struct ieee80211com *ic, 1068a1b9b6aSSam Leffler const struct ieee80211_key *key) 1078a1b9b6aSSam Leffler { 1088a1b9b6aSSam Leffler return ic->ic_crypto.cs_key_delete(ic, key); 1098a1b9b6aSSam Leffler } 1108a1b9b6aSSam Leffler 1118a1b9b6aSSam Leffler static __inline int 1128a1b9b6aSSam Leffler dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key, 1138a1b9b6aSSam Leffler const u_int8_t mac[IEEE80211_ADDR_LEN]) 1148a1b9b6aSSam Leffler { 1158a1b9b6aSSam Leffler return ic->ic_crypto.cs_key_set(ic, key, mac); 1168a1b9b6aSSam Leffler } 1171a1e1d21SSam Leffler 1181a1e1d21SSam Leffler /* 1191a1e1d21SSam Leffler * Setup crypto support. 1201a1e1d21SSam Leffler */ 1211a1e1d21SSam Leffler void 1228a1b9b6aSSam Leffler ieee80211_crypto_attach(struct ieee80211com *ic) 1231a1e1d21SSam Leffler { 1248a1b9b6aSSam Leffler struct ieee80211_crypto_state *cs = &ic->ic_crypto; 1258a1b9b6aSSam Leffler int i; 1261a1e1d21SSam Leffler 1278a1b9b6aSSam Leffler /* NB: we assume everything is pre-zero'd */ 1288a1b9b6aSSam Leffler cs->cs_def_txkey = IEEE80211_KEYIX_NONE; 1298a1b9b6aSSam Leffler ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 1308a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 1318a1b9b6aSSam Leffler ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i], i); 1321a1e1d21SSam Leffler /* 1338a1b9b6aSSam Leffler * Initialize the driver key support routines to noop entries. 1348a1b9b6aSSam Leffler * This is useful especially for the cipher test modules. 1351a1e1d21SSam Leffler */ 1368a1b9b6aSSam Leffler cs->cs_key_alloc = null_key_alloc; 1378a1b9b6aSSam Leffler cs->cs_key_set = null_key_set; 1388a1b9b6aSSam Leffler cs->cs_key_delete = null_key_delete; 1398a1b9b6aSSam Leffler cs->cs_key_update_begin = null_key_update; 1408a1b9b6aSSam Leffler cs->cs_key_update_end = null_key_update; 1411a1e1d21SSam Leffler } 1421a1e1d21SSam Leffler 1438a1b9b6aSSam Leffler /* 1448a1b9b6aSSam Leffler * Teardown crypto support. 1458a1b9b6aSSam Leffler */ 1468a1b9b6aSSam Leffler void 1478a1b9b6aSSam Leffler ieee80211_crypto_detach(struct ieee80211com *ic) 1488a1b9b6aSSam Leffler { 1498a1b9b6aSSam Leffler ieee80211_crypto_delglobalkeys(ic); 1501a1e1d21SSam Leffler } 1511a1e1d21SSam Leffler 1528a1b9b6aSSam Leffler /* 1538a1b9b6aSSam Leffler * Register a crypto cipher module. 1548a1b9b6aSSam Leffler */ 1558a1b9b6aSSam Leffler void 1568a1b9b6aSSam Leffler ieee80211_crypto_register(const struct ieee80211_cipher *cip) 1578a1b9b6aSSam Leffler { 1588a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 1598a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 1608a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 1618a1b9b6aSSam Leffler return; 1628a1b9b6aSSam Leffler } 1638a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 1648a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 1658a1b9b6aSSam Leffler __func__, cip->ic_name); 1668a1b9b6aSSam Leffler return; 1678a1b9b6aSSam Leffler } 1688a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = cip; 1698a1b9b6aSSam Leffler } 1708a1b9b6aSSam Leffler 1718a1b9b6aSSam Leffler /* 1728a1b9b6aSSam Leffler * Unregister a crypto cipher module. 1738a1b9b6aSSam Leffler */ 1748a1b9b6aSSam Leffler void 1758a1b9b6aSSam Leffler ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 1768a1b9b6aSSam Leffler { 1778a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 1788a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 1798a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 1808a1b9b6aSSam Leffler return; 1818a1b9b6aSSam Leffler } 1828a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 1838a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 1848a1b9b6aSSam Leffler __func__, cip->ic_name); 1858a1b9b6aSSam Leffler return; 1868a1b9b6aSSam Leffler } 1878a1b9b6aSSam Leffler /* NB: don't complain about not being registered */ 1888a1b9b6aSSam Leffler /* XXX disallow if references */ 1898a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = NULL; 1908a1b9b6aSSam Leffler } 1918a1b9b6aSSam Leffler 1928a1b9b6aSSam Leffler int 1938a1b9b6aSSam Leffler ieee80211_crypto_available(u_int cipher) 1948a1b9b6aSSam Leffler { 1958a1b9b6aSSam Leffler return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 1968a1b9b6aSSam Leffler } 1978a1b9b6aSSam Leffler 1988a1b9b6aSSam Leffler /* XXX well-known names! */ 1998a1b9b6aSSam Leffler static const char *cipher_modnames[] = { 2008a1b9b6aSSam Leffler "wlan_wep", /* IEEE80211_CIPHER_WEP */ 2018a1b9b6aSSam Leffler "wlan_tkip", /* IEEE80211_CIPHER_TKIP */ 2028a1b9b6aSSam Leffler "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */ 2038a1b9b6aSSam Leffler "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */ 2048a1b9b6aSSam Leffler "wlan_ckip", /* IEEE80211_CIPHER_CKIP */ 2058a1b9b6aSSam Leffler }; 2068a1b9b6aSSam Leffler 2078a1b9b6aSSam Leffler /* 2088a1b9b6aSSam Leffler * Establish a relationship between the specified key and cipher 2098a1b9b6aSSam Leffler * and, if not a global key, allocate a hardware index from the 2108a1b9b6aSSam Leffler * driver. Note that we may be called for global keys but they 2118a1b9b6aSSam Leffler * should have a key index already setup so the only work done 2128a1b9b6aSSam Leffler * is to setup the cipher reference. 2138a1b9b6aSSam Leffler * 2148a1b9b6aSSam Leffler * This must be the first call applied to a key; all the other key 2158a1b9b6aSSam Leffler * routines assume wk_cipher is setup. 2168a1b9b6aSSam Leffler * 2178a1b9b6aSSam Leffler * Locking must be handled by the caller using: 2188a1b9b6aSSam Leffler * ieee80211_key_update_begin(ic); 2198a1b9b6aSSam Leffler * ieee80211_key_update_end(ic); 2208a1b9b6aSSam Leffler */ 2218a1b9b6aSSam Leffler int 2228a1b9b6aSSam Leffler ieee80211_crypto_newkey(struct ieee80211com *ic, 2238a1b9b6aSSam Leffler int cipher, struct ieee80211_key *key) 2248a1b9b6aSSam Leffler { 2258a1b9b6aSSam Leffler #define N(a) (sizeof(a) / sizeof(a[0])) 2268a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 2278a1b9b6aSSam Leffler void *keyctx; 2288a1b9b6aSSam Leffler int oflags; 2298a1b9b6aSSam Leffler 2308a1b9b6aSSam Leffler /* 2318a1b9b6aSSam Leffler * Validate cipher and set reference to cipher routines. 2328a1b9b6aSSam Leffler */ 2338a1b9b6aSSam Leffler if (cipher >= IEEE80211_CIPHER_MAX) { 2348a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2358a1b9b6aSSam Leffler "%s: invalid cipher %u\n", __func__, cipher); 2368a1b9b6aSSam Leffler ic->ic_stats.is_crypto_badcipher++; 2378a1b9b6aSSam Leffler return 0; 2388a1b9b6aSSam Leffler } 2398a1b9b6aSSam Leffler cip = ciphers[cipher]; 2408a1b9b6aSSam Leffler if (cip == NULL) { 2418a1b9b6aSSam Leffler /* 2428a1b9b6aSSam Leffler * Auto-load cipher module if we have a well-known name 2438a1b9b6aSSam Leffler * for it. It might be better to use string names rather 2448a1b9b6aSSam Leffler * than numbers and craft a module name based on the cipher 2458a1b9b6aSSam Leffler * name; e.g. wlan_cipher_<cipher-name>. 2468a1b9b6aSSam Leffler */ 2478a1b9b6aSSam Leffler if (cipher < N(cipher_modnames)) { 2488a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2498a1b9b6aSSam Leffler "%s: unregistered cipher %u, load module %s\n", 2508a1b9b6aSSam Leffler __func__, cipher, cipher_modnames[cipher]); 2518a1b9b6aSSam Leffler ieee80211_load_module(cipher_modnames[cipher]); 2528a1b9b6aSSam Leffler /* 2538a1b9b6aSSam Leffler * If cipher module loaded it should immediately 2548a1b9b6aSSam Leffler * call ieee80211_crypto_register which will fill 2558a1b9b6aSSam Leffler * in the entry in the ciphers array. 2568a1b9b6aSSam Leffler */ 2578a1b9b6aSSam Leffler cip = ciphers[cipher]; 2588a1b9b6aSSam Leffler } 2598a1b9b6aSSam Leffler if (cip == NULL) { 2608a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2618a1b9b6aSSam Leffler "%s: unable to load cipher %u, module %s\n", 2628a1b9b6aSSam Leffler __func__, cipher, 2638a1b9b6aSSam Leffler cipher < N(cipher_modnames) ? 2648a1b9b6aSSam Leffler cipher_modnames[cipher] : "<unknown>"); 2658a1b9b6aSSam Leffler ic->ic_stats.is_crypto_nocipher++; 2668a1b9b6aSSam Leffler return 0; 2678a1b9b6aSSam Leffler } 2688a1b9b6aSSam Leffler } 2698a1b9b6aSSam Leffler 2708a1b9b6aSSam Leffler oflags = key->wk_flags; 2718a1b9b6aSSam Leffler /* 2728a1b9b6aSSam Leffler * If the hardware does not support the cipher then 2738a1b9b6aSSam Leffler * fallback to a host-based implementation. 2748a1b9b6aSSam Leffler */ 2758a1b9b6aSSam Leffler key->wk_flags &= ~(IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC); 2768a1b9b6aSSam Leffler if ((ic->ic_caps & (1<<cipher)) == 0) { 2778a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2788a1b9b6aSSam Leffler "%s: no h/w support for cipher %s, falling back to s/w\n", 2798a1b9b6aSSam Leffler __func__, cip->ic_name); 2808a1b9b6aSSam Leffler key->wk_flags |= IEEE80211_KEY_SWCRYPT; 2818a1b9b6aSSam Leffler } 2828a1b9b6aSSam Leffler /* 2838a1b9b6aSSam Leffler * Hardware TKIP with software MIC is an important 2848a1b9b6aSSam Leffler * combination; we handle it by flagging each key, 2858a1b9b6aSSam Leffler * the cipher modules honor it. 2868a1b9b6aSSam Leffler */ 2878a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP && 2888a1b9b6aSSam Leffler (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) { 2898a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2908a1b9b6aSSam Leffler "%s: no h/w support for TKIP MIC, falling back to s/w\n", 2918a1b9b6aSSam Leffler __func__); 2928a1b9b6aSSam Leffler key->wk_flags |= IEEE80211_KEY_SWMIC; 2938a1b9b6aSSam Leffler } 2948a1b9b6aSSam Leffler 2958a1b9b6aSSam Leffler /* 2968a1b9b6aSSam Leffler * Bind cipher to key instance. Note we do this 2978a1b9b6aSSam Leffler * after checking the device capabilities so the 2988a1b9b6aSSam Leffler * cipher module can optimize space usage based on 2998a1b9b6aSSam Leffler * whether or not it needs to do the cipher work. 3008a1b9b6aSSam Leffler */ 3018a1b9b6aSSam Leffler if (key->wk_cipher != cip || key->wk_flags != oflags) { 3028a1b9b6aSSam Leffler again: 3038a1b9b6aSSam Leffler keyctx = cip->ic_attach(ic, key); 3048a1b9b6aSSam Leffler if (keyctx == NULL) { 3058a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3068a1b9b6aSSam Leffler "%s: unable to attach cipher %s\n", 3078a1b9b6aSSam Leffler __func__, cip->ic_name); 3088a1b9b6aSSam Leffler key->wk_flags = oflags; /* restore old flags */ 3098a1b9b6aSSam Leffler ic->ic_stats.is_crypto_attachfail++; 3108a1b9b6aSSam Leffler return 0; 3118a1b9b6aSSam Leffler } 3128a1b9b6aSSam Leffler cipher_detach(key); 3138a1b9b6aSSam Leffler key->wk_cipher = cip; /* XXX refcnt? */ 3148a1b9b6aSSam Leffler key->wk_private = keyctx; 3158a1b9b6aSSam Leffler } 3168a1b9b6aSSam Leffler 3178a1b9b6aSSam Leffler /* 3188a1b9b6aSSam Leffler * Ask the driver for a key index if we don't have one. 3198a1b9b6aSSam Leffler * Note that entries in the global key table always have 3208a1b9b6aSSam Leffler * an index; this means it's safe to call this routine 3218a1b9b6aSSam Leffler * for these entries just to setup the reference to the 3228a1b9b6aSSam Leffler * cipher template. Note also that when using software 3238a1b9b6aSSam Leffler * crypto we also call the driver to give us a key index. 3248a1b9b6aSSam Leffler */ 3258a1b9b6aSSam Leffler if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 3268a1b9b6aSSam Leffler key->wk_keyix = dev_key_alloc(ic, key); 3278a1b9b6aSSam Leffler if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 3288a1b9b6aSSam Leffler /* 3298a1b9b6aSSam Leffler * Driver has no room; fallback to doing crypto 3308a1b9b6aSSam Leffler * in the host. We change the flags and start the 3318a1b9b6aSSam Leffler * procedure over. If we get back here then there's 3328a1b9b6aSSam Leffler * no hope and we bail. Note that this can leave 3338a1b9b6aSSam Leffler * the key in a inconsistent state if the caller 3348a1b9b6aSSam Leffler * continues to use it. 3358a1b9b6aSSam Leffler */ 3368a1b9b6aSSam Leffler if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 3378a1b9b6aSSam Leffler ic->ic_stats.is_crypto_swfallback++; 3388a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3398a1b9b6aSSam Leffler "%s: no h/w resources for cipher %s, " 3408a1b9b6aSSam Leffler "falling back to s/w\n", __func__, 3418a1b9b6aSSam Leffler cip->ic_name); 3428a1b9b6aSSam Leffler oflags = key->wk_flags; 3438a1b9b6aSSam Leffler key->wk_flags |= IEEE80211_KEY_SWCRYPT; 3448a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP) 3458a1b9b6aSSam Leffler key->wk_flags |= IEEE80211_KEY_SWMIC; 3468a1b9b6aSSam Leffler goto again; 3478a1b9b6aSSam Leffler } 3488a1b9b6aSSam Leffler ic->ic_stats.is_crypto_keyfail++; 3498a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3508a1b9b6aSSam Leffler "%s: unable to setup cipher %s\n", 3518a1b9b6aSSam Leffler __func__, cip->ic_name); 3528a1b9b6aSSam Leffler return 0; 3538a1b9b6aSSam Leffler } 3548a1b9b6aSSam Leffler } 3558a1b9b6aSSam Leffler return 1; 3568a1b9b6aSSam Leffler #undef N 3578a1b9b6aSSam Leffler } 3588a1b9b6aSSam Leffler 3598a1b9b6aSSam Leffler /* 3608a1b9b6aSSam Leffler * Remove the key (no locking, for internal use). 3618a1b9b6aSSam Leffler */ 3628a1b9b6aSSam Leffler static int 3638a1b9b6aSSam Leffler _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 3648a1b9b6aSSam Leffler { 3658a1b9b6aSSam Leffler u_int16_t keyix; 3668a1b9b6aSSam Leffler 3678a1b9b6aSSam Leffler KASSERT(key->wk_cipher != NULL, ("No cipher!")); 3688a1b9b6aSSam Leffler 3698a1b9b6aSSam Leffler keyix = key->wk_keyix; 3708a1b9b6aSSam Leffler if (keyix != IEEE80211_KEYIX_NONE) { 3718a1b9b6aSSam Leffler /* 3728a1b9b6aSSam Leffler * Remove hardware entry. 3738a1b9b6aSSam Leffler */ 3748a1b9b6aSSam Leffler /* XXX key cache */ 3758a1b9b6aSSam Leffler if (!dev_key_delete(ic, key)) { 3768a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3778a1b9b6aSSam Leffler "%s: driver did not delete key index %u\n", 3788a1b9b6aSSam Leffler __func__, keyix); 3798a1b9b6aSSam Leffler ic->ic_stats.is_crypto_delkey++; 3808a1b9b6aSSam Leffler /* XXX recovery? */ 3818a1b9b6aSSam Leffler } 3828a1b9b6aSSam Leffler } 3838a1b9b6aSSam Leffler cipher_detach(key); 3848a1b9b6aSSam Leffler memset(key, 0, sizeof(*key)); 3858a1b9b6aSSam Leffler key->wk_cipher = &ieee80211_cipher_none; 3868a1b9b6aSSam Leffler key->wk_private = cipher_attach(ic, key); 3878a1b9b6aSSam Leffler /* NB: cannot depend on key index to decide this */ 3888a1b9b6aSSam Leffler if (&ic->ic_nw_keys[0] <= key && 3898a1b9b6aSSam Leffler key < &ic->ic_nw_keys[IEEE80211_WEP_NKID]) 3908a1b9b6aSSam Leffler key->wk_keyix = keyix; /* preserve shared key state */ 3918a1b9b6aSSam Leffler else 3928a1b9b6aSSam Leffler key->wk_keyix = IEEE80211_KEYIX_NONE; 3938a1b9b6aSSam Leffler return 1; 3948a1b9b6aSSam Leffler } 3958a1b9b6aSSam Leffler 3968a1b9b6aSSam Leffler /* 3978a1b9b6aSSam Leffler * Remove the specified key. 3988a1b9b6aSSam Leffler */ 3998a1b9b6aSSam Leffler int 4008a1b9b6aSSam Leffler ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 4018a1b9b6aSSam Leffler { 4028a1b9b6aSSam Leffler int status; 4038a1b9b6aSSam Leffler 4048a1b9b6aSSam Leffler ieee80211_key_update_begin(ic); 4058a1b9b6aSSam Leffler status = _ieee80211_crypto_delkey(ic, key); 4068a1b9b6aSSam Leffler ieee80211_key_update_end(ic); 4078a1b9b6aSSam Leffler return status; 4088a1b9b6aSSam Leffler } 4098a1b9b6aSSam Leffler 4108a1b9b6aSSam Leffler /* 4118a1b9b6aSSam Leffler * Clear the global key table. 4128a1b9b6aSSam Leffler */ 4138a1b9b6aSSam Leffler void 4148a1b9b6aSSam Leffler ieee80211_crypto_delglobalkeys(struct ieee80211com *ic) 4158a1b9b6aSSam Leffler { 4168a1b9b6aSSam Leffler int i; 4178a1b9b6aSSam Leffler 4188a1b9b6aSSam Leffler ieee80211_key_update_begin(ic); 4198a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 4208a1b9b6aSSam Leffler (void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]); 4218a1b9b6aSSam Leffler ieee80211_key_update_end(ic); 4228a1b9b6aSSam Leffler } 4238a1b9b6aSSam Leffler 4248a1b9b6aSSam Leffler /* 4258a1b9b6aSSam Leffler * Set the contents of the specified key. 4268a1b9b6aSSam Leffler * 4278a1b9b6aSSam Leffler * Locking must be handled by the caller using: 4288a1b9b6aSSam Leffler * ieee80211_key_update_begin(ic); 4298a1b9b6aSSam Leffler * ieee80211_key_update_end(ic); 4308a1b9b6aSSam Leffler */ 4318a1b9b6aSSam Leffler int 4328a1b9b6aSSam Leffler ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key, 4338a1b9b6aSSam Leffler const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 4348a1b9b6aSSam Leffler { 4358a1b9b6aSSam Leffler const struct ieee80211_cipher *cip = key->wk_cipher; 4368a1b9b6aSSam Leffler 4378a1b9b6aSSam Leffler KASSERT(cip != NULL, ("No cipher!")); 4388a1b9b6aSSam Leffler 4398a1b9b6aSSam Leffler /* 4408a1b9b6aSSam Leffler * Give cipher a chance to validate key contents. 4418a1b9b6aSSam Leffler * XXX should happen before modifying state. 4428a1b9b6aSSam Leffler */ 4438a1b9b6aSSam Leffler if (!cip->ic_setkey(key)) { 4448a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 4458a1b9b6aSSam Leffler "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 4468a1b9b6aSSam Leffler __func__, cip->ic_name, key->wk_keyix, 4478a1b9b6aSSam Leffler key->wk_keylen, key->wk_flags); 4488a1b9b6aSSam Leffler ic->ic_stats.is_crypto_setkey_cipher++; 4498a1b9b6aSSam Leffler return 0; 4508a1b9b6aSSam Leffler } 4518a1b9b6aSSam Leffler if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 4528a1b9b6aSSam Leffler /* XXX nothing allocated, should not happen */ 4538a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 4548a1b9b6aSSam Leffler "%s: no key index; should not happen!\n", __func__); 4558a1b9b6aSSam Leffler ic->ic_stats.is_crypto_setkey_nokey++; 4568a1b9b6aSSam Leffler return 0; 4578a1b9b6aSSam Leffler } 4588a1b9b6aSSam Leffler return dev_key_set(ic, key, macaddr); 4598a1b9b6aSSam Leffler } 4608a1b9b6aSSam Leffler 4618a1b9b6aSSam Leffler /* 4628a1b9b6aSSam Leffler * Add privacy headers appropriate for the specified key. 4638a1b9b6aSSam Leffler */ 4648a1b9b6aSSam Leffler struct ieee80211_key * 4658a1b9b6aSSam Leffler ieee80211_crypto_encap(struct ieee80211com *ic, 4668a1b9b6aSSam Leffler struct ieee80211_node *ni, struct mbuf *m) 4678a1b9b6aSSam Leffler { 4688a1b9b6aSSam Leffler struct ieee80211_key *k; 4698a1b9b6aSSam Leffler struct ieee80211_frame *wh; 4708a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 4718a1b9b6aSSam Leffler u_int8_t keyix; 4728a1b9b6aSSam Leffler 4738a1b9b6aSSam Leffler /* 4748a1b9b6aSSam Leffler * Multicast traffic always uses the multicast key. 4758a1b9b6aSSam Leffler * Otherwise if a unicast key is set we use that and 4768a1b9b6aSSam Leffler * it is always key index 0. When no unicast key is 4778a1b9b6aSSam Leffler * set we fall back to the default transmit key. 4788a1b9b6aSSam Leffler */ 4798a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 4808a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 4818a1b9b6aSSam Leffler ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) { 4828a1b9b6aSSam Leffler if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) { 4838a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 4848a1b9b6aSSam Leffler "%s: No default xmit key for frame to %s\n", 4858a1b9b6aSSam Leffler __func__, ether_sprintf(wh->i_addr1)); 4868a1b9b6aSSam Leffler ic->ic_stats.is_tx_nodefkey++; 4878a1b9b6aSSam Leffler return NULL; 4888a1b9b6aSSam Leffler } 4898a1b9b6aSSam Leffler keyix = ic->ic_def_txkey; 4908a1b9b6aSSam Leffler k = &ic->ic_nw_keys[ic->ic_def_txkey]; 4918a1b9b6aSSam Leffler } else { 4928a1b9b6aSSam Leffler keyix = 0; 4938a1b9b6aSSam Leffler k = &ni->ni_ucastkey; 4948a1b9b6aSSam Leffler } 4958a1b9b6aSSam Leffler cip = k->wk_cipher; 4968a1b9b6aSSam Leffler return (cip->ic_encap(k, m, keyix<<6) ? k : NULL); 4978a1b9b6aSSam Leffler } 4988a1b9b6aSSam Leffler 4998a1b9b6aSSam Leffler /* 5008a1b9b6aSSam Leffler * Validate and strip privacy headers (and trailer) for a 5018a1b9b6aSSam Leffler * received frame that has the WEP/Privacy bit set. 5028a1b9b6aSSam Leffler */ 5038a1b9b6aSSam Leffler struct ieee80211_key * 5048a1b9b6aSSam Leffler ieee80211_crypto_decap(struct ieee80211com *ic, 5058a1b9b6aSSam Leffler struct ieee80211_node *ni, struct mbuf *m) 5068a1b9b6aSSam Leffler { 5078a1b9b6aSSam Leffler #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 5088a1b9b6aSSam Leffler #define IEEE80211_WEP_MINLEN \ 5098a1b9b6aSSam Leffler (sizeof(struct ieee80211_frame) + ETHER_HDR_LEN + \ 5108a1b9b6aSSam Leffler IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 5118a1b9b6aSSam Leffler struct ieee80211_key *k; 5128a1b9b6aSSam Leffler struct ieee80211_frame *wh; 5138a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 5148a1b9b6aSSam Leffler u_int8_t *ivp; 5158a1b9b6aSSam Leffler u_int8_t keyid; 5168a1b9b6aSSam Leffler int hdrlen; 5178a1b9b6aSSam Leffler 5188a1b9b6aSSam Leffler /* NB: this minimum size data frame could be bigger */ 5198a1b9b6aSSam Leffler if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 5208a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 5218a1b9b6aSSam Leffler "%s: WEP data frame too short, len %u\n", 5228a1b9b6aSSam Leffler __func__, m->m_pkthdr.len); 5238a1b9b6aSSam Leffler ic->ic_stats.is_rx_tooshort++; /* XXX need unique stat? */ 5241a1e1d21SSam Leffler return NULL; 5251a1e1d21SSam Leffler } 5261a1e1d21SSam Leffler 5271a1e1d21SSam Leffler /* 5288a1b9b6aSSam Leffler * Locate the key. If unicast and there is no unicast 5298a1b9b6aSSam Leffler * key then we fall back to the key id in the header. 5308a1b9b6aSSam Leffler * This assumes unicast keys are only configured when 5318a1b9b6aSSam Leffler * the key id in the header is meaningless (typically 0). 5321a1e1d21SSam Leffler */ 5338a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 5348a1b9b6aSSam Leffler hdrlen = ieee80211_hdrsize(wh); 5358a1b9b6aSSam Leffler ivp = mtod(m, u_int8_t *) + hdrlen; /* XXX contig */ 5368a1b9b6aSSam Leffler keyid = ivp[IEEE80211_WEP_IVLEN]; 5378a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 5388a1b9b6aSSam Leffler ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) 5398a1b9b6aSSam Leffler k = &ic->ic_nw_keys[keyid >> 6]; 5401a1e1d21SSam Leffler else 5418a1b9b6aSSam Leffler k = &ni->ni_ucastkey; 5421a1e1d21SSam Leffler 5431a1e1d21SSam Leffler /* 5448a1b9b6aSSam Leffler * Insure crypto header is contiguous for all decap work. 5451a1e1d21SSam Leffler */ 5468a1b9b6aSSam Leffler cip = k->wk_cipher; 5478a1b9b6aSSam Leffler if (m->m_len < hdrlen + cip->ic_header && 5488a1b9b6aSSam Leffler (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) { 5498a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 5508a1b9b6aSSam Leffler "[%s] unable to pullup %s header\n", 5518a1b9b6aSSam Leffler ether_sprintf(wh->i_addr2), cip->ic_name); 5528a1b9b6aSSam Leffler ic->ic_stats.is_rx_wepfail++; /* XXX */ 5538a1b9b6aSSam Leffler return 0; 5548a1b9b6aSSam Leffler } 5551a1e1d21SSam Leffler 5568a1b9b6aSSam Leffler return (cip->ic_decap(k, m) ? k : NULL); 5578a1b9b6aSSam Leffler #undef IEEE80211_WEP_MINLEN 5588a1b9b6aSSam Leffler #undef IEEE80211_WEP_HDRLEN 5591a1e1d21SSam Leffler } 560