11a1e1d21SSam Leffler /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 47535e66aSSam Leffler * Copyright (c) 2001 Atsushi Onoe 5b032f27cSSam Leffler * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 61a1e1d21SSam Leffler * All rights reserved. 71a1e1d21SSam Leffler * 81a1e1d21SSam Leffler * Redistribution and use in source and binary forms, with or without 91a1e1d21SSam Leffler * modification, are permitted provided that the following conditions 101a1e1d21SSam Leffler * are met: 111a1e1d21SSam Leffler * 1. Redistributions of source code must retain the above copyright 127535e66aSSam Leffler * notice, this list of conditions and the following disclaimer. 137535e66aSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 147535e66aSSam Leffler * notice, this list of conditions and the following disclaimer in the 157535e66aSSam Leffler * documentation and/or other materials provided with the distribution. 161a1e1d21SSam Leffler * 177535e66aSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 187535e66aSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 197535e66aSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 207535e66aSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 217535e66aSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 227535e66aSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237535e66aSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 247535e66aSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 257535e66aSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 267535e66aSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271a1e1d21SSam Leffler */ 281a1e1d21SSam Leffler 291a1e1d21SSam Leffler #include <sys/cdefs.h> 308a1b9b6aSSam Leffler /* 318a1b9b6aSSam Leffler * IEEE 802.11 generic crypto support. 328a1b9b6aSSam Leffler */ 33b032f27cSSam Leffler #include "opt_wlan.h" 34b032f27cSSam Leffler 351a1e1d21SSam Leffler #include <sys/param.h> 36b032f27cSSam Leffler #include <sys/kernel.h> 37b032f27cSSam Leffler #include <sys/malloc.h> 381a1e1d21SSam Leffler #include <sys/mbuf.h> 391a1e1d21SSam Leffler 408a1b9b6aSSam Leffler #include <sys/socket.h> 411a1e1d21SSam Leffler 421a1e1d21SSam Leffler #include <net/if.h> 431a1e1d21SSam Leffler #include <net/if_media.h> 448a1b9b6aSSam Leffler #include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 451a1e1d21SSam Leffler 461a1e1d21SSam Leffler #include <net80211/ieee80211_var.h> 471a1e1d21SSam Leffler 48b032f27cSSam Leffler MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state"); 49b032f27cSSam Leffler 50b032f27cSSam Leffler static int _ieee80211_crypto_delkey(struct ieee80211vap *, 51b032f27cSSam Leffler struct ieee80211_key *); 52b032f27cSSam Leffler 538a1b9b6aSSam Leffler /* 548a1b9b6aSSam Leffler * Table of registered cipher modules. 558a1b9b6aSSam Leffler */ 568a1b9b6aSSam Leffler static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 571a1e1d21SSam Leffler 588a1b9b6aSSam Leffler /* 598a1b9b6aSSam Leffler * Default "null" key management routines. 608a1b9b6aSSam Leffler */ 618a1b9b6aSSam Leffler static int 62e6e547d5SSam Leffler null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 63c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 641a1e1d21SSam Leffler { 65b032f27cSSam Leffler if (!(&vap->iv_nw_keys[0] <= k && 66b032f27cSSam Leffler k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 675c8a7b1bSSam Leffler /* 685c8a7b1bSSam Leffler * Not in the global key table, the driver should handle this 695c8a7b1bSSam Leffler * by allocating a slot in the h/w key table/cache. In 705c8a7b1bSSam Leffler * lieu of that return key slot 0 for any unicast key 715c8a7b1bSSam Leffler * request. We disallow the request if this is a group key. 725c8a7b1bSSam Leffler * This default policy does the right thing for legacy hardware 735c8a7b1bSSam Leffler * with a 4 key table. It also handles devices that pass 745c8a7b1bSSam Leffler * packets through untouched when marked with the WEP bit 755c8a7b1bSSam Leffler * and key index 0. 765c8a7b1bSSam Leffler */ 77c1225b52SSam Leffler if (k->wk_flags & IEEE80211_KEY_GROUP) 78c1225b52SSam Leffler return 0; 79c1225b52SSam Leffler *keyix = 0; /* NB: use key index 0 for ucast key */ 80c1225b52SSam Leffler } else { 814a19d712SAndriy Voskoboinyk *keyix = ieee80211_crypto_get_key_wepidx(vap, k); 828a1b9b6aSSam Leffler } 83c1225b52SSam Leffler *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 84c1225b52SSam Leffler return 1; 855c8a7b1bSSam Leffler } 868a1b9b6aSSam Leffler static int 87b032f27cSSam Leffler null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 888a1b9b6aSSam Leffler { 898a1b9b6aSSam Leffler return 1; 908a1b9b6aSSam Leffler } 918a1b9b6aSSam Leffler static int 92bc813c40SAdrian Chadd null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 938a1b9b6aSSam Leffler { 948a1b9b6aSSam Leffler return 1; 958a1b9b6aSSam Leffler } 96b032f27cSSam Leffler static void null_key_update(struct ieee80211vap *vap) {} 978a1b9b6aSSam Leffler 988a1b9b6aSSam Leffler /* 998a1b9b6aSSam Leffler * Write-arounds for common operations. 1008a1b9b6aSSam Leffler */ 1018a1b9b6aSSam Leffler static __inline void 1028a1b9b6aSSam Leffler cipher_detach(struct ieee80211_key *key) 1038a1b9b6aSSam Leffler { 1048a1b9b6aSSam Leffler key->wk_cipher->ic_detach(key); 1058a1b9b6aSSam Leffler } 1068a1b9b6aSSam Leffler 1078a1b9b6aSSam Leffler static __inline void * 108b032f27cSSam Leffler cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key) 1098a1b9b6aSSam Leffler { 110b032f27cSSam Leffler return key->wk_cipher->ic_attach(vap, key); 1118a1b9b6aSSam Leffler } 1128a1b9b6aSSam Leffler 1138a1b9b6aSSam Leffler /* 1148a1b9b6aSSam Leffler * Wrappers for driver key management methods. 1158a1b9b6aSSam Leffler */ 1168a1b9b6aSSam Leffler static __inline int 117b032f27cSSam Leffler dev_key_alloc(struct ieee80211vap *vap, 118e6e547d5SSam Leffler struct ieee80211_key *key, 119c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 1208a1b9b6aSSam Leffler { 121b032f27cSSam Leffler return vap->iv_key_alloc(vap, key, keyix, rxkeyix); 1228a1b9b6aSSam Leffler } 1238a1b9b6aSSam Leffler 1248a1b9b6aSSam Leffler static __inline int 125b032f27cSSam Leffler dev_key_delete(struct ieee80211vap *vap, 1268a1b9b6aSSam Leffler const struct ieee80211_key *key) 1278a1b9b6aSSam Leffler { 128b032f27cSSam Leffler return vap->iv_key_delete(vap, key); 1298a1b9b6aSSam Leffler } 1308a1b9b6aSSam Leffler 1318a1b9b6aSSam Leffler static __inline int 13271fe06caSSam Leffler dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key) 1338a1b9b6aSSam Leffler { 134bc813c40SAdrian Chadd return vap->iv_key_set(vap, key); 1358a1b9b6aSSam Leffler } 1361a1e1d21SSam Leffler 1371a1e1d21SSam Leffler /* 138b032f27cSSam Leffler * Setup crypto support for a device/shared instance. 1391a1e1d21SSam Leffler */ 1401a1e1d21SSam Leffler void 1418a1b9b6aSSam Leffler ieee80211_crypto_attach(struct ieee80211com *ic) 1421a1e1d21SSam Leffler { 1438a1b9b6aSSam Leffler /* NB: we assume everything is pre-zero'd */ 1448a1b9b6aSSam Leffler ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 1451116e8b9SAdrian Chadd 1461116e8b9SAdrian Chadd /* 1471116e8b9SAdrian Chadd * Default set of net80211 supported ciphers. 1481116e8b9SAdrian Chadd * 1491116e8b9SAdrian Chadd * These are the default set that all drivers are expected to 1501116e8b9SAdrian Chadd * support, either/or in hardware and software. 1511116e8b9SAdrian Chadd * 1521116e8b9SAdrian Chadd * Drivers can add their own support to this and the 1531116e8b9SAdrian Chadd * hardware cipher list (ic_cryptocaps.) 1541116e8b9SAdrian Chadd */ 1551116e8b9SAdrian Chadd ic->ic_sw_cryptocaps = IEEE80211_CRYPTO_WEP | 1561116e8b9SAdrian Chadd IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_AES_CCM; 1571a1e1d21SSam Leffler } 1581a1e1d21SSam Leffler 1598a1b9b6aSSam Leffler /* 1608a1b9b6aSSam Leffler * Teardown crypto support. 1618a1b9b6aSSam Leffler */ 1628a1b9b6aSSam Leffler void 1638a1b9b6aSSam Leffler ieee80211_crypto_detach(struct ieee80211com *ic) 1648a1b9b6aSSam Leffler { 165b032f27cSSam Leffler } 166b032f27cSSam Leffler 167b032f27cSSam Leffler /* 168*e9961ea1SAdrian Chadd * Set the supported ciphers for software encryption. 169*e9961ea1SAdrian Chadd */ 170*e9961ea1SAdrian Chadd void 171*e9961ea1SAdrian Chadd ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *ic, 172*e9961ea1SAdrian Chadd uint32_t cipher_set) 173*e9961ea1SAdrian Chadd { 174*e9961ea1SAdrian Chadd ic->ic_sw_cryptocaps = cipher_set; 175*e9961ea1SAdrian Chadd } 176*e9961ea1SAdrian Chadd 177*e9961ea1SAdrian Chadd /* 178*e9961ea1SAdrian Chadd * Set the supported ciphers for hardware encryption. 179*e9961ea1SAdrian Chadd */ 180*e9961ea1SAdrian Chadd void 181*e9961ea1SAdrian Chadd ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *ic, 182*e9961ea1SAdrian Chadd uint32_t cipher_set) 183*e9961ea1SAdrian Chadd { 184*e9961ea1SAdrian Chadd ic->ic_cryptocaps = cipher_set; 185*e9961ea1SAdrian Chadd } 186*e9961ea1SAdrian Chadd 187*e9961ea1SAdrian Chadd 188*e9961ea1SAdrian Chadd /* 189b032f27cSSam Leffler * Setup crypto support for a vap. 190b032f27cSSam Leffler */ 191b032f27cSSam Leffler void 192b032f27cSSam Leffler ieee80211_crypto_vattach(struct ieee80211vap *vap) 193b032f27cSSam Leffler { 194b032f27cSSam Leffler int i; 195b032f27cSSam Leffler 196b032f27cSSam Leffler /* NB: we assume everything is pre-zero'd */ 197b032f27cSSam Leffler vap->iv_max_keyix = IEEE80211_WEP_NKID; 198b032f27cSSam Leffler vap->iv_def_txkey = IEEE80211_KEYIX_NONE; 199b032f27cSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 200b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i], 201b032f27cSSam Leffler IEEE80211_KEYIX_NONE); 202b032f27cSSam Leffler /* 203b032f27cSSam Leffler * Initialize the driver key support routines to noop entries. 204b032f27cSSam Leffler * This is useful especially for the cipher test modules. 205b032f27cSSam Leffler */ 206b032f27cSSam Leffler vap->iv_key_alloc = null_key_alloc; 207b032f27cSSam Leffler vap->iv_key_set = null_key_set; 208b032f27cSSam Leffler vap->iv_key_delete = null_key_delete; 209b032f27cSSam Leffler vap->iv_key_update_begin = null_key_update; 210b032f27cSSam Leffler vap->iv_key_update_end = null_key_update; 211b032f27cSSam Leffler } 212b032f27cSSam Leffler 213b032f27cSSam Leffler /* 214b032f27cSSam Leffler * Teardown crypto support for a vap. 215b032f27cSSam Leffler */ 216b032f27cSSam Leffler void 217b032f27cSSam Leffler ieee80211_crypto_vdetach(struct ieee80211vap *vap) 218b032f27cSSam Leffler { 219b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(vap); 2201a1e1d21SSam Leffler } 2211a1e1d21SSam Leffler 2228a1b9b6aSSam Leffler /* 2238a1b9b6aSSam Leffler * Register a crypto cipher module. 2248a1b9b6aSSam Leffler */ 2258a1b9b6aSSam Leffler void 2268a1b9b6aSSam Leffler ieee80211_crypto_register(const struct ieee80211_cipher *cip) 2278a1b9b6aSSam Leffler { 2288a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 2298a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 2308a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 2318a1b9b6aSSam Leffler return; 2328a1b9b6aSSam Leffler } 2338a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 2348a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 2358a1b9b6aSSam Leffler __func__, cip->ic_name); 2368a1b9b6aSSam Leffler return; 2378a1b9b6aSSam Leffler } 2388a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = cip; 2398a1b9b6aSSam Leffler } 2408a1b9b6aSSam Leffler 2418a1b9b6aSSam Leffler /* 2428a1b9b6aSSam Leffler * Unregister a crypto cipher module. 2438a1b9b6aSSam Leffler */ 2448a1b9b6aSSam Leffler void 2458a1b9b6aSSam Leffler ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 2468a1b9b6aSSam Leffler { 2478a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 2488a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 2498a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 2508a1b9b6aSSam Leffler return; 2518a1b9b6aSSam Leffler } 2528a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 2538a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 2548a1b9b6aSSam Leffler __func__, cip->ic_name); 2558a1b9b6aSSam Leffler return; 2568a1b9b6aSSam Leffler } 2578a1b9b6aSSam Leffler /* NB: don't complain about not being registered */ 2588a1b9b6aSSam Leffler /* XXX disallow if references */ 2598a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = NULL; 2608a1b9b6aSSam Leffler } 2618a1b9b6aSSam Leffler 2628a1b9b6aSSam Leffler int 2638a1b9b6aSSam Leffler ieee80211_crypto_available(u_int cipher) 2648a1b9b6aSSam Leffler { 2658a1b9b6aSSam Leffler return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 2668a1b9b6aSSam Leffler } 2678a1b9b6aSSam Leffler 2688a1b9b6aSSam Leffler /* XXX well-known names! */ 269b032f27cSSam Leffler static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = { 2709fcf5318SSam Leffler [IEEE80211_CIPHER_WEP] = "wlan_wep", 2719fcf5318SSam Leffler [IEEE80211_CIPHER_TKIP] = "wlan_tkip", 2729fcf5318SSam Leffler [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb", 2739fcf5318SSam Leffler [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp", 2749fcf5318SSam Leffler [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */ 2759fcf5318SSam Leffler [IEEE80211_CIPHER_CKIP] = "wlan_ckip", 2769fcf5318SSam Leffler [IEEE80211_CIPHER_NONE] = "wlan_none", 2778a1b9b6aSSam Leffler }; 2788a1b9b6aSSam Leffler 279411ccf5fSSam Leffler /* NB: there must be no overlap between user-supplied and device-owned flags */ 280411ccf5fSSam Leffler CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0); 281411ccf5fSSam Leffler 2828a1b9b6aSSam Leffler /* 2838a1b9b6aSSam Leffler * Establish a relationship between the specified key and cipher 284dd70e17bSSam Leffler * and, if necessary, allocate a hardware index from the driver. 285411ccf5fSSam Leffler * Note that when a fixed key index is required it must be specified. 2868a1b9b6aSSam Leffler * 2878a1b9b6aSSam Leffler * This must be the first call applied to a key; all the other key 2888a1b9b6aSSam Leffler * routines assume wk_cipher is setup. 2898a1b9b6aSSam Leffler * 2908a1b9b6aSSam Leffler * Locking must be handled by the caller using: 291b032f27cSSam Leffler * ieee80211_key_update_begin(vap); 292b032f27cSSam Leffler * ieee80211_key_update_end(vap); 2938a1b9b6aSSam Leffler */ 2948a1b9b6aSSam Leffler int 295b032f27cSSam Leffler ieee80211_crypto_newkey(struct ieee80211vap *vap, 296dd70e17bSSam Leffler int cipher, int flags, struct ieee80211_key *key) 2978a1b9b6aSSam Leffler { 298b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 2998a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 300c1225b52SSam Leffler ieee80211_keyix keyix, rxkeyix; 3018a1b9b6aSSam Leffler void *keyctx; 3028a1b9b6aSSam Leffler int oflags; 3038a1b9b6aSSam Leffler 30407760642SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 30507760642SSam Leffler "%s: cipher %u flags 0x%x keyix %u\n", 30607760642SSam Leffler __func__, cipher, flags, key->wk_keyix); 30707760642SSam Leffler 3088a1b9b6aSSam Leffler /* 3098a1b9b6aSSam Leffler * Validate cipher and set reference to cipher routines. 3108a1b9b6aSSam Leffler */ 3118a1b9b6aSSam Leffler if (cipher >= IEEE80211_CIPHER_MAX) { 312b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3138a1b9b6aSSam Leffler "%s: invalid cipher %u\n", __func__, cipher); 314b032f27cSSam Leffler vap->iv_stats.is_crypto_badcipher++; 3158a1b9b6aSSam Leffler return 0; 3168a1b9b6aSSam Leffler } 3178a1b9b6aSSam Leffler cip = ciphers[cipher]; 3188a1b9b6aSSam Leffler if (cip == NULL) { 3198a1b9b6aSSam Leffler /* 3208a1b9b6aSSam Leffler * Auto-load cipher module if we have a well-known name 3218a1b9b6aSSam Leffler * for it. It might be better to use string names rather 3228a1b9b6aSSam Leffler * than numbers and craft a module name based on the cipher 3238a1b9b6aSSam Leffler * name; e.g. wlan_cipher_<cipher-name>. 3248a1b9b6aSSam Leffler */ 325b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3268a1b9b6aSSam Leffler "%s: unregistered cipher %u, load module %s\n", 3278a1b9b6aSSam Leffler __func__, cipher, cipher_modnames[cipher]); 3288a1b9b6aSSam Leffler ieee80211_load_module(cipher_modnames[cipher]); 3298a1b9b6aSSam Leffler /* 3308a1b9b6aSSam Leffler * If cipher module loaded it should immediately 3318a1b9b6aSSam Leffler * call ieee80211_crypto_register which will fill 3328a1b9b6aSSam Leffler * in the entry in the ciphers array. 3338a1b9b6aSSam Leffler */ 3348a1b9b6aSSam Leffler cip = ciphers[cipher]; 3358a1b9b6aSSam Leffler if (cip == NULL) { 336b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3378a1b9b6aSSam Leffler "%s: unable to load cipher %u, module %s\n", 338b032f27cSSam Leffler __func__, cipher, cipher_modnames[cipher]); 339b032f27cSSam Leffler vap->iv_stats.is_crypto_nocipher++; 3408a1b9b6aSSam Leffler return 0; 3418a1b9b6aSSam Leffler } 3428a1b9b6aSSam Leffler } 3438a1b9b6aSSam Leffler 3448a1b9b6aSSam Leffler oflags = key->wk_flags; 345dd70e17bSSam Leffler flags &= IEEE80211_KEY_COMMON; 346411ccf5fSSam Leffler /* NB: preserve device attributes */ 347411ccf5fSSam Leffler flags |= (oflags & IEEE80211_KEY_DEVICE); 3488a1b9b6aSSam Leffler /* 3498a1b9b6aSSam Leffler * If the hardware does not support the cipher then 3508a1b9b6aSSam Leffler * fallback to a host-based implementation. 3518a1b9b6aSSam Leffler */ 352b032f27cSSam Leffler if ((ic->ic_cryptocaps & (1<<cipher)) == 0) { 353b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3548a1b9b6aSSam Leffler "%s: no h/w support for cipher %s, falling back to s/w\n", 3558a1b9b6aSSam Leffler __func__, cip->ic_name); 356dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWCRYPT; 3578a1b9b6aSSam Leffler } 3588a1b9b6aSSam Leffler /* 3598a1b9b6aSSam Leffler * Hardware TKIP with software MIC is an important 3608a1b9b6aSSam Leffler * combination; we handle it by flagging each key, 3618a1b9b6aSSam Leffler * the cipher modules honor it. 3628a1b9b6aSSam Leffler */ 3638a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP && 364b032f27cSSam Leffler (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) { 365b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3668a1b9b6aSSam Leffler "%s: no h/w support for TKIP MIC, falling back to s/w\n", 3678a1b9b6aSSam Leffler __func__); 368dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWMIC; 3698a1b9b6aSSam Leffler } 3708a1b9b6aSSam Leffler 3718a1b9b6aSSam Leffler /* 3728a1b9b6aSSam Leffler * Bind cipher to key instance. Note we do this 3738a1b9b6aSSam Leffler * after checking the device capabilities so the 3748a1b9b6aSSam Leffler * cipher module can optimize space usage based on 3758a1b9b6aSSam Leffler * whether or not it needs to do the cipher work. 3768a1b9b6aSSam Leffler */ 377dd70e17bSSam Leffler if (key->wk_cipher != cip || key->wk_flags != flags) { 378dd70e17bSSam Leffler /* 379dd70e17bSSam Leffler * Fillin the flags so cipher modules can see s/w 380dd70e17bSSam Leffler * crypto requirements and potentially allocate 381dd70e17bSSam Leffler * different state and/or attach different method 382dd70e17bSSam Leffler * pointers. 383dd70e17bSSam Leffler */ 384dd70e17bSSam Leffler key->wk_flags = flags; 385b032f27cSSam Leffler keyctx = cip->ic_attach(vap, key); 3868a1b9b6aSSam Leffler if (keyctx == NULL) { 387b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3888a1b9b6aSSam Leffler "%s: unable to attach cipher %s\n", 3898a1b9b6aSSam Leffler __func__, cip->ic_name); 3908a1b9b6aSSam Leffler key->wk_flags = oflags; /* restore old flags */ 391b032f27cSSam Leffler vap->iv_stats.is_crypto_attachfail++; 3928a1b9b6aSSam Leffler return 0; 3938a1b9b6aSSam Leffler } 3948a1b9b6aSSam Leffler cipher_detach(key); 3958a1b9b6aSSam Leffler key->wk_cipher = cip; /* XXX refcnt? */ 3968a1b9b6aSSam Leffler key->wk_private = keyctx; 3978a1b9b6aSSam Leffler } 3988a1b9b6aSSam Leffler 3998a1b9b6aSSam Leffler /* 4008a1b9b6aSSam Leffler * Ask the driver for a key index if we don't have one. 4018a1b9b6aSSam Leffler * Note that entries in the global key table always have 4028a1b9b6aSSam Leffler * an index; this means it's safe to call this routine 4038a1b9b6aSSam Leffler * for these entries just to setup the reference to the 4048a1b9b6aSSam Leffler * cipher template. Note also that when using software 4058a1b9b6aSSam Leffler * crypto we also call the driver to give us a key index. 4068a1b9b6aSSam Leffler */ 407e6e547d5SSam Leffler if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 408b032f27cSSam Leffler if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) { 4098a1b9b6aSSam Leffler /* 410e6e547d5SSam Leffler * Unable to setup driver state. 4118a1b9b6aSSam Leffler */ 412b032f27cSSam Leffler vap->iv_stats.is_crypto_keyfail++; 413b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 4148a1b9b6aSSam Leffler "%s: unable to setup cipher %s\n", 4158a1b9b6aSSam Leffler __func__, cip->ic_name); 4168a1b9b6aSSam Leffler return 0; 4178a1b9b6aSSam Leffler } 418e6e547d5SSam Leffler if (key->wk_flags != flags) { 419e6e547d5SSam Leffler /* 420e6e547d5SSam Leffler * Driver overrode flags we setup; typically because 421e6e547d5SSam Leffler * resources were unavailable to handle _this_ key. 422e6e547d5SSam Leffler * Re-attach the cipher context to allow cipher 423e6e547d5SSam Leffler * modules to handle differing requirements. 424e6e547d5SSam Leffler */ 425e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 426e6e547d5SSam Leffler "%s: driver override for cipher %s, flags " 427e6e547d5SSam Leffler "0x%x -> 0x%x\n", __func__, cip->ic_name, 428e6e547d5SSam Leffler oflags, key->wk_flags); 429e6e547d5SSam Leffler keyctx = cip->ic_attach(vap, key); 430e6e547d5SSam Leffler if (keyctx == NULL) { 431e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 432e6e547d5SSam Leffler "%s: unable to attach cipher %s with " 433e6e547d5SSam Leffler "flags 0x%x\n", __func__, cip->ic_name, 434e6e547d5SSam Leffler key->wk_flags); 435e6e547d5SSam Leffler key->wk_flags = oflags; /* restore old flags */ 436e6e547d5SSam Leffler vap->iv_stats.is_crypto_attachfail++; 437e6e547d5SSam Leffler return 0; 438e6e547d5SSam Leffler } 439e6e547d5SSam Leffler cipher_detach(key); 440e6e547d5SSam Leffler key->wk_cipher = cip; /* XXX refcnt? */ 441e6e547d5SSam Leffler key->wk_private = keyctx; 442e6e547d5SSam Leffler } 443c1225b52SSam Leffler key->wk_keyix = keyix; 444c1225b52SSam Leffler key->wk_rxkeyix = rxkeyix; 445e6e547d5SSam Leffler key->wk_flags |= IEEE80211_KEY_DEVKEY; 4468a1b9b6aSSam Leffler } 4478a1b9b6aSSam Leffler return 1; 4488a1b9b6aSSam Leffler } 4498a1b9b6aSSam Leffler 4508a1b9b6aSSam Leffler /* 4518a1b9b6aSSam Leffler * Remove the key (no locking, for internal use). 4528a1b9b6aSSam Leffler */ 4538a1b9b6aSSam Leffler static int 454b032f27cSSam Leffler _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 4558a1b9b6aSSam Leffler { 4568a1b9b6aSSam Leffler KASSERT(key->wk_cipher != NULL, ("No cipher!")); 4578a1b9b6aSSam Leffler 458b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 459fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 460fc508e8eSSam Leffler __func__, key->wk_cipher->ic_name, 461fc508e8eSSam Leffler key->wk_keyix, key->wk_flags, 462b032f27cSSam Leffler key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 463b032f27cSSam Leffler key->wk_keylen); 464fc508e8eSSam Leffler 465e6e547d5SSam Leffler if (key->wk_flags & IEEE80211_KEY_DEVKEY) { 4668a1b9b6aSSam Leffler /* 4678a1b9b6aSSam Leffler * Remove hardware entry. 4688a1b9b6aSSam Leffler */ 4698a1b9b6aSSam Leffler /* XXX key cache */ 470b032f27cSSam Leffler if (!dev_key_delete(vap, key)) { 471b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 4728a1b9b6aSSam Leffler "%s: driver did not delete key index %u\n", 473e6e547d5SSam Leffler __func__, key->wk_keyix); 474b032f27cSSam Leffler vap->iv_stats.is_crypto_delkey++; 4758a1b9b6aSSam Leffler /* XXX recovery? */ 4768a1b9b6aSSam Leffler } 4778a1b9b6aSSam Leffler } 4788a1b9b6aSSam Leffler cipher_detach(key); 4798a1b9b6aSSam Leffler memset(key, 0, sizeof(*key)); 480b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE); 4818a1b9b6aSSam Leffler return 1; 4828a1b9b6aSSam Leffler } 4838a1b9b6aSSam Leffler 4848a1b9b6aSSam Leffler /* 4858a1b9b6aSSam Leffler * Remove the specified key. 4868a1b9b6aSSam Leffler */ 4878a1b9b6aSSam Leffler int 488b032f27cSSam Leffler ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 4898a1b9b6aSSam Leffler { 4908a1b9b6aSSam Leffler int status; 4918a1b9b6aSSam Leffler 492b032f27cSSam Leffler ieee80211_key_update_begin(vap); 493b032f27cSSam Leffler status = _ieee80211_crypto_delkey(vap, key); 494b032f27cSSam Leffler ieee80211_key_update_end(vap); 4958a1b9b6aSSam Leffler return status; 4968a1b9b6aSSam Leffler } 4978a1b9b6aSSam Leffler 4988a1b9b6aSSam Leffler /* 4998a1b9b6aSSam Leffler * Clear the global key table. 5008a1b9b6aSSam Leffler */ 5018a1b9b6aSSam Leffler void 502b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap) 5038a1b9b6aSSam Leffler { 5048a1b9b6aSSam Leffler int i; 5058a1b9b6aSSam Leffler 506b032f27cSSam Leffler ieee80211_key_update_begin(vap); 5078a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 508b032f27cSSam Leffler (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]); 509b032f27cSSam Leffler ieee80211_key_update_end(vap); 5108a1b9b6aSSam Leffler } 5118a1b9b6aSSam Leffler 5128a1b9b6aSSam Leffler /* 5138a1b9b6aSSam Leffler * Set the contents of the specified key. 5148a1b9b6aSSam Leffler * 5158a1b9b6aSSam Leffler * Locking must be handled by the caller using: 516b032f27cSSam Leffler * ieee80211_key_update_begin(vap); 517b032f27cSSam Leffler * ieee80211_key_update_end(vap); 5188a1b9b6aSSam Leffler */ 5198a1b9b6aSSam Leffler int 52071fe06caSSam Leffler ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key) 5218a1b9b6aSSam Leffler { 5228a1b9b6aSSam Leffler const struct ieee80211_cipher *cip = key->wk_cipher; 5238a1b9b6aSSam Leffler 5248a1b9b6aSSam Leffler KASSERT(cip != NULL, ("No cipher!")); 5258a1b9b6aSSam Leffler 526b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 527fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 528fc508e8eSSam Leffler __func__, cip->ic_name, key->wk_keyix, 52971fe06caSSam Leffler key->wk_flags, ether_sprintf(key->wk_macaddr), 530b032f27cSSam Leffler key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 531b032f27cSSam Leffler key->wk_keylen); 532fc508e8eSSam Leffler 533e6e547d5SSam Leffler if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 534e6e547d5SSam Leffler /* XXX nothing allocated, should not happen */ 535e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 536e6e547d5SSam Leffler "%s: no device key setup done; should not happen!\n", 537e6e547d5SSam Leffler __func__); 538e6e547d5SSam Leffler vap->iv_stats.is_crypto_setkey_nokey++; 539e6e547d5SSam Leffler return 0; 540e6e547d5SSam Leffler } 5418a1b9b6aSSam Leffler /* 5428a1b9b6aSSam Leffler * Give cipher a chance to validate key contents. 5438a1b9b6aSSam Leffler * XXX should happen before modifying state. 5448a1b9b6aSSam Leffler */ 5458a1b9b6aSSam Leffler if (!cip->ic_setkey(key)) { 546b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 5478a1b9b6aSSam Leffler "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 5488a1b9b6aSSam Leffler __func__, cip->ic_name, key->wk_keyix, 5498a1b9b6aSSam Leffler key->wk_keylen, key->wk_flags); 550b032f27cSSam Leffler vap->iv_stats.is_crypto_setkey_cipher++; 5518a1b9b6aSSam Leffler return 0; 5528a1b9b6aSSam Leffler } 55371fe06caSSam Leffler return dev_key_set(vap, key); 5548a1b9b6aSSam Leffler } 5558a1b9b6aSSam Leffler 55654a95d0dSAdrian Chadd /* 55754a95d0dSAdrian Chadd * Return index if the key is a WEP key (0..3); -1 otherwise. 55854a95d0dSAdrian Chadd * 55954a95d0dSAdrian Chadd * This is different to "get_keyid" which defaults to returning 56054a95d0dSAdrian Chadd * 0 for unicast keys; it assumes that it won't be used for WEP. 56154a95d0dSAdrian Chadd */ 56254a95d0dSAdrian Chadd int 56354a95d0dSAdrian Chadd ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap, 56454a95d0dSAdrian Chadd const struct ieee80211_key *k) 56554a95d0dSAdrian Chadd { 56654a95d0dSAdrian Chadd 56754a95d0dSAdrian Chadd if (k >= &vap->iv_nw_keys[0] && 56854a95d0dSAdrian Chadd k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 56954a95d0dSAdrian Chadd return (k - vap->iv_nw_keys); 570d0155f67SXin LI return (-1); 57154a95d0dSAdrian Chadd } 57254a95d0dSAdrian Chadd 57354a95d0dSAdrian Chadd /* 57454a95d0dSAdrian Chadd * Note: only supports a single unicast key (0). 57554a95d0dSAdrian Chadd */ 576ef0d8f63SAdrian Chadd uint8_t 577ef0d8f63SAdrian Chadd ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k) 578ef0d8f63SAdrian Chadd { 579ef0d8f63SAdrian Chadd if (k >= &vap->iv_nw_keys[0] && 580ef0d8f63SAdrian Chadd k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 581ef0d8f63SAdrian Chadd return (k - vap->iv_nw_keys); 582ef0d8f63SAdrian Chadd else 583ef0d8f63SAdrian Chadd return (0); 584ef0d8f63SAdrian Chadd } 585ef0d8f63SAdrian Chadd 5868a1b9b6aSSam Leffler struct ieee80211_key * 58715395998SAdrian Chadd ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m) 5888a1b9b6aSSam Leffler { 589b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 5908a1b9b6aSSam Leffler struct ieee80211_frame *wh; 5918a1b9b6aSSam Leffler 5928a1b9b6aSSam Leffler /* 5938a1b9b6aSSam Leffler * Multicast traffic always uses the multicast key. 59461605e0aSdomienschepers * 59561605e0aSdomienschepers * Historically we would fall back to the default 59661605e0aSdomienschepers * transmit key if there was no unicast key. This 59761605e0aSdomienschepers * behaviour was documented up to IEEE Std 802.11-2016, 59861605e0aSdomienschepers * 12.9.2.2 Per-MSDU/Per-A-MSDU Tx pseudocode, in the 59961605e0aSdomienschepers * 'else' case but is no longer in later versions of 60061605e0aSdomienschepers * the standard. Additionally falling back to the 60161605e0aSdomienschepers * group key for unicast was a security risk. 6028a1b9b6aSSam Leffler */ 6038a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 60461605e0aSdomienschepers if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 605b032f27cSSam Leffler if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) { 606b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 607b032f27cSSam Leffler wh->i_addr1, 608b032f27cSSam Leffler "no default transmit key (%s) deftxkey %u", 609b032f27cSSam Leffler __func__, vap->iv_def_txkey); 610b032f27cSSam Leffler vap->iv_stats.is_tx_nodefkey++; 6118a1b9b6aSSam Leffler return NULL; 6128a1b9b6aSSam Leffler } 61315395998SAdrian Chadd return &vap->iv_nw_keys[vap->iv_def_txkey]; 61415395998SAdrian Chadd } 615ef0d8f63SAdrian Chadd 61661605e0aSdomienschepers if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 61761605e0aSdomienschepers return NULL; 61815395998SAdrian Chadd return &ni->ni_ucastkey; 61915395998SAdrian Chadd } 62015395998SAdrian Chadd 62115395998SAdrian Chadd /* 62215395998SAdrian Chadd * Add privacy headers appropriate for the specified key. 62315395998SAdrian Chadd */ 62415395998SAdrian Chadd struct ieee80211_key * 62515395998SAdrian Chadd ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m) 62615395998SAdrian Chadd { 62715395998SAdrian Chadd struct ieee80211_key *k; 62815395998SAdrian Chadd const struct ieee80211_cipher *cip; 62915395998SAdrian Chadd 63015395998SAdrian Chadd if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) { 6318a1b9b6aSSam Leffler cip = k->wk_cipher; 632ef0d8f63SAdrian Chadd return (cip->ic_encap(k, m) ? k : NULL); 6338a1b9b6aSSam Leffler } 6348a1b9b6aSSam Leffler 63515395998SAdrian Chadd return NULL; 63615395998SAdrian Chadd } 63715395998SAdrian Chadd 6388a1b9b6aSSam Leffler /* 6398a1b9b6aSSam Leffler * Validate and strip privacy headers (and trailer) for a 6408a1b9b6aSSam Leffler * received frame that has the WEP/Privacy bit set. 6418a1b9b6aSSam Leffler */ 642fe75b452SAdrian Chadd int 643fe75b452SAdrian Chadd ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen, 644fe75b452SAdrian Chadd struct ieee80211_key **key) 6458a1b9b6aSSam Leffler { 6468a1b9b6aSSam Leffler #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 6478a1b9b6aSSam Leffler #define IEEE80211_WEP_MINLEN \ 648a92c6eb0SSam Leffler (sizeof(struct ieee80211_frame) + \ 6498a1b9b6aSSam Leffler IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 650b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 6518a1b9b6aSSam Leffler struct ieee80211_key *k; 6528a1b9b6aSSam Leffler struct ieee80211_frame *wh; 653fe75b452SAdrian Chadd const struct ieee80211_rx_stats *rxs; 6548a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 65568e8e04eSSam Leffler uint8_t keyid; 6568a1b9b6aSSam Leffler 657fe75b452SAdrian Chadd /* 658fe75b452SAdrian Chadd * Check for hardware decryption and IV stripping. 659fe75b452SAdrian Chadd * If the IV is stripped then we definitely can't find a key. 660fe75b452SAdrian Chadd * Set the key to NULL but return true; upper layers 661fe75b452SAdrian Chadd * will need to handle a NULL key for a successful 662fe75b452SAdrian Chadd * decrypt. 663fe75b452SAdrian Chadd */ 664fe75b452SAdrian Chadd rxs = ieee80211_get_rx_params_ptr(m); 665fe75b452SAdrian Chadd if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 666fe75b452SAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) { 667fe75b452SAdrian Chadd /* 668fe75b452SAdrian Chadd * Hardware decrypted, IV stripped. 669fe75b452SAdrian Chadd * We can't find a key with a stripped IV. 670fe75b452SAdrian Chadd * Return successful. 671fe75b452SAdrian Chadd */ 672fe75b452SAdrian Chadd *key = NULL; 673fe75b452SAdrian Chadd return (1); 674fe75b452SAdrian Chadd } 675fe75b452SAdrian Chadd } 676fe75b452SAdrian Chadd 6778a1b9b6aSSam Leffler /* NB: this minimum size data frame could be bigger */ 6788a1b9b6aSSam Leffler if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 679b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 6808a1b9b6aSSam Leffler "%s: WEP data frame too short, len %u\n", 6818a1b9b6aSSam Leffler __func__, m->m_pkthdr.len); 682b032f27cSSam Leffler vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */ 683fe75b452SAdrian Chadd *key = NULL; 684fe75b452SAdrian Chadd return (0); 6851a1e1d21SSam Leffler } 6861a1e1d21SSam Leffler 6871a1e1d21SSam Leffler /* 6888a1b9b6aSSam Leffler * Locate the key. If unicast and there is no unicast 6898a1b9b6aSSam Leffler * key then we fall back to the key id in the header. 6908a1b9b6aSSam Leffler * This assumes unicast keys are only configured when 6918a1b9b6aSSam Leffler * the key id in the header is meaningless (typically 0). 6921a1e1d21SSam Leffler */ 6938a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 694b032f27cSSam Leffler m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid); 6958a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 696cda15ce1SSam Leffler IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 697b032f27cSSam Leffler k = &vap->iv_nw_keys[keyid >> 6]; 6981a1e1d21SSam Leffler else 6998a1b9b6aSSam Leffler k = &ni->ni_ucastkey; 7001a1e1d21SSam Leffler 7011a1e1d21SSam Leffler /* 70279e0962dSAndriy Voskoboinyk * Insure crypto header is contiguous and long enough for all 70379e0962dSAndriy Voskoboinyk * decap work. 7041a1e1d21SSam Leffler */ 7058a1b9b6aSSam Leffler cip = k->wk_cipher; 70679e0962dSAndriy Voskoboinyk if (m->m_len < hdrlen + cip->ic_header) { 707b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 70879e0962dSAndriy Voskoboinyk "frame is too short (%d < %u) for crypto decap", 70979e0962dSAndriy Voskoboinyk cip->ic_name, m->m_len, hdrlen + cip->ic_header); 71079e0962dSAndriy Voskoboinyk vap->iv_stats.is_rx_tooshort++; 711fe75b452SAdrian Chadd *key = NULL; 712fe75b452SAdrian Chadd return (0); 7138a1b9b6aSSam Leffler } 7141a1e1d21SSam Leffler 715fe75b452SAdrian Chadd /* 716fe75b452SAdrian Chadd * Attempt decryption. 717fe75b452SAdrian Chadd * 718fe75b452SAdrian Chadd * If we fail then don't return the key - return NULL 719fe75b452SAdrian Chadd * and an error. 720fe75b452SAdrian Chadd */ 721fe75b452SAdrian Chadd if (cip->ic_decap(k, m, hdrlen)) { 722fe75b452SAdrian Chadd /* success */ 723fe75b452SAdrian Chadd *key = k; 724fe75b452SAdrian Chadd return (1); 725fe75b452SAdrian Chadd } 726fe75b452SAdrian Chadd 727fe75b452SAdrian Chadd /* Failure */ 728fe75b452SAdrian Chadd *key = NULL; 729fe75b452SAdrian Chadd return (0); 7308a1b9b6aSSam Leffler #undef IEEE80211_WEP_MINLEN 7318a1b9b6aSSam Leffler #undef IEEE80211_WEP_HDRLEN 7321a1e1d21SSam Leffler } 73306b2d888SSam Leffler 734ee9d294bSAdrian Chadd /* 735ee9d294bSAdrian Chadd * Check and remove any MIC. 736ee9d294bSAdrian Chadd */ 737ee9d294bSAdrian Chadd int 738ee9d294bSAdrian Chadd ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k, 739ee9d294bSAdrian Chadd struct mbuf *m, int force) 740ee9d294bSAdrian Chadd { 741ee9d294bSAdrian Chadd const struct ieee80211_cipher *cip; 742ee9d294bSAdrian Chadd const struct ieee80211_rx_stats *rxs; 743ee9d294bSAdrian Chadd struct ieee80211_frame *wh; 744ee9d294bSAdrian Chadd 745ee9d294bSAdrian Chadd rxs = ieee80211_get_rx_params_ptr(m); 746ee9d294bSAdrian Chadd wh = mtod(m, struct ieee80211_frame *); 747ee9d294bSAdrian Chadd 748ee9d294bSAdrian Chadd /* 749ee9d294bSAdrian Chadd * Handle demic / mic errors from hardware-decrypted offload devices. 750ee9d294bSAdrian Chadd */ 751ee9d294bSAdrian Chadd if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 752ee9d294bSAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) { 753ee9d294bSAdrian Chadd /* 754ee9d294bSAdrian Chadd * Hardware has said MIC failed. We don't care about 755ee9d294bSAdrian Chadd * whether it was stripped or not. 756ee9d294bSAdrian Chadd * 757ee9d294bSAdrian Chadd * Eventually - teach the demic methods in crypto 758ee9d294bSAdrian Chadd * modules to handle a NULL key and not to dereference 759ee9d294bSAdrian Chadd * it. 760ee9d294bSAdrian Chadd */ 761ee9d294bSAdrian Chadd ieee80211_notify_michael_failure(vap, wh, -1); 762ee9d294bSAdrian Chadd return (0); 763ee9d294bSAdrian Chadd } 764ee9d294bSAdrian Chadd 765ee9d294bSAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) { 766ee9d294bSAdrian Chadd /* 767ee9d294bSAdrian Chadd * Hardware has decrypted and not indicated a 768ee9d294bSAdrian Chadd * MIC failure and has stripped the MIC. 769ee9d294bSAdrian Chadd * We may not have a key, so for now just 770ee9d294bSAdrian Chadd * return OK. 771ee9d294bSAdrian Chadd */ 772ee9d294bSAdrian Chadd return (1); 773ee9d294bSAdrian Chadd } 774ee9d294bSAdrian Chadd } 775ee9d294bSAdrian Chadd 776ee9d294bSAdrian Chadd /* 777ee9d294bSAdrian Chadd * If we don't have a key at this point then we don't 778ee9d294bSAdrian Chadd * have to demic anything. 779ee9d294bSAdrian Chadd */ 780ee9d294bSAdrian Chadd if (k == NULL) 781ee9d294bSAdrian Chadd return (1); 782ee9d294bSAdrian Chadd 783ee9d294bSAdrian Chadd cip = k->wk_cipher; 784ee9d294bSAdrian Chadd return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1); 785ee9d294bSAdrian Chadd } 786ee9d294bSAdrian Chadd 78706b2d888SSam Leffler static void 78806b2d888SSam Leffler load_ucastkey(void *arg, struct ieee80211_node *ni) 78906b2d888SSam Leffler { 79006b2d888SSam Leffler struct ieee80211vap *vap = ni->ni_vap; 79106b2d888SSam Leffler struct ieee80211_key *k; 79206b2d888SSam Leffler 79306b2d888SSam Leffler if (vap->iv_state != IEEE80211_S_RUN) 79406b2d888SSam Leffler return; 79506b2d888SSam Leffler k = &ni->ni_ucastkey; 796e6e547d5SSam Leffler if (k->wk_flags & IEEE80211_KEY_DEVKEY) 79706b2d888SSam Leffler dev_key_set(vap, k); 79806b2d888SSam Leffler } 79906b2d888SSam Leffler 80006b2d888SSam Leffler /* 80106b2d888SSam Leffler * Re-load all keys known to the 802.11 layer that may 80206b2d888SSam Leffler * have hardware state backing them. This is used by 80306b2d888SSam Leffler * drivers on resume to push keys down into the device. 80406b2d888SSam Leffler */ 80506b2d888SSam Leffler void 80606b2d888SSam Leffler ieee80211_crypto_reload_keys(struct ieee80211com *ic) 80706b2d888SSam Leffler { 80806b2d888SSam Leffler struct ieee80211vap *vap; 80906b2d888SSam Leffler int i; 81006b2d888SSam Leffler 81106b2d888SSam Leffler /* 81206b2d888SSam Leffler * Keys in the global key table of each vap. 81306b2d888SSam Leffler */ 81406b2d888SSam Leffler /* NB: used only during resume so don't lock for now */ 81506b2d888SSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 81606b2d888SSam Leffler if (vap->iv_state != IEEE80211_S_RUN) 81706b2d888SSam Leffler continue; 81806b2d888SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) { 81906b2d888SSam Leffler const struct ieee80211_key *k = &vap->iv_nw_keys[i]; 820e6e547d5SSam Leffler if (k->wk_flags & IEEE80211_KEY_DEVKEY) 82106b2d888SSam Leffler dev_key_set(vap, k); 82206b2d888SSam Leffler } 82306b2d888SSam Leffler } 82406b2d888SSam Leffler /* 82506b2d888SSam Leffler * Unicast keys. 82606b2d888SSam Leffler */ 82706b2d888SSam Leffler ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL); 82806b2d888SSam Leffler } 829781487cfSAdrian Chadd 830781487cfSAdrian Chadd /* 831781487cfSAdrian Chadd * Set the default key index for WEP, or KEYIX_NONE for no default TX key. 832781487cfSAdrian Chadd * 833781487cfSAdrian Chadd * This should be done as part of a key update block (iv_key_update_begin / 834781487cfSAdrian Chadd * iv_key_update_end.) 835781487cfSAdrian Chadd */ 836781487cfSAdrian Chadd void 837781487cfSAdrian Chadd ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid) 838781487cfSAdrian Chadd { 839781487cfSAdrian Chadd 840781487cfSAdrian Chadd /* XXX TODO: assert we're in a key update block */ 841781487cfSAdrian Chadd 842781487cfSAdrian Chadd vap->iv_update_deftxkey(vap, kid); 843781487cfSAdrian Chadd } 844