11a1e1d21SSam Leffler /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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> 301a1e1d21SSam Leffler __FBSDID("$FreeBSD$"); 311a1e1d21SSam Leffler 328a1b9b6aSSam Leffler /* 338a1b9b6aSSam Leffler * IEEE 802.11 generic crypto support. 348a1b9b6aSSam Leffler */ 35b032f27cSSam Leffler #include "opt_wlan.h" 36b032f27cSSam Leffler 371a1e1d21SSam Leffler #include <sys/param.h> 38b032f27cSSam Leffler #include <sys/kernel.h> 39b032f27cSSam Leffler #include <sys/malloc.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 50b032f27cSSam Leffler MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state"); 51b032f27cSSam Leffler 52b032f27cSSam Leffler static int _ieee80211_crypto_delkey(struct ieee80211vap *, 53b032f27cSSam Leffler struct ieee80211_key *); 54b032f27cSSam Leffler 558a1b9b6aSSam Leffler /* 568a1b9b6aSSam Leffler * Table of registered cipher modules. 578a1b9b6aSSam Leffler */ 588a1b9b6aSSam Leffler static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 591a1e1d21SSam Leffler 608a1b9b6aSSam Leffler /* 618a1b9b6aSSam Leffler * Default "null" key management routines. 628a1b9b6aSSam Leffler */ 638a1b9b6aSSam Leffler static int 64e6e547d5SSam Leffler null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 65c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 661a1e1d21SSam Leffler { 67b032f27cSSam Leffler if (!(&vap->iv_nw_keys[0] <= k && 68b032f27cSSam Leffler k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 695c8a7b1bSSam Leffler /* 705c8a7b1bSSam Leffler * Not in the global key table, the driver should handle this 715c8a7b1bSSam Leffler * by allocating a slot in the h/w key table/cache. In 725c8a7b1bSSam Leffler * lieu of that return key slot 0 for any unicast key 735c8a7b1bSSam Leffler * request. We disallow the request if this is a group key. 745c8a7b1bSSam Leffler * This default policy does the right thing for legacy hardware 755c8a7b1bSSam Leffler * with a 4 key table. It also handles devices that pass 765c8a7b1bSSam Leffler * packets through untouched when marked with the WEP bit 775c8a7b1bSSam Leffler * and key index 0. 785c8a7b1bSSam Leffler */ 79c1225b52SSam Leffler if (k->wk_flags & IEEE80211_KEY_GROUP) 80c1225b52SSam Leffler return 0; 81c1225b52SSam Leffler *keyix = 0; /* NB: use key index 0 for ucast key */ 82c1225b52SSam Leffler } else { 834a19d712SAndriy Voskoboinyk *keyix = ieee80211_crypto_get_key_wepidx(vap, k); 848a1b9b6aSSam Leffler } 85c1225b52SSam Leffler *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 86c1225b52SSam Leffler return 1; 875c8a7b1bSSam Leffler } 888a1b9b6aSSam Leffler static int 89b032f27cSSam Leffler null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 908a1b9b6aSSam Leffler { 918a1b9b6aSSam Leffler return 1; 928a1b9b6aSSam Leffler } 938a1b9b6aSSam Leffler static int 94bc813c40SAdrian Chadd null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 958a1b9b6aSSam Leffler { 968a1b9b6aSSam Leffler return 1; 978a1b9b6aSSam Leffler } 98b032f27cSSam Leffler static void null_key_update(struct ieee80211vap *vap) {} 998a1b9b6aSSam Leffler 1008a1b9b6aSSam Leffler /* 1018a1b9b6aSSam Leffler * Write-arounds for common operations. 1028a1b9b6aSSam Leffler */ 1038a1b9b6aSSam Leffler static __inline void 1048a1b9b6aSSam Leffler cipher_detach(struct ieee80211_key *key) 1058a1b9b6aSSam Leffler { 1068a1b9b6aSSam Leffler key->wk_cipher->ic_detach(key); 1078a1b9b6aSSam Leffler } 1088a1b9b6aSSam Leffler 1098a1b9b6aSSam Leffler static __inline void * 110b032f27cSSam Leffler cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key) 1118a1b9b6aSSam Leffler { 112b032f27cSSam Leffler return key->wk_cipher->ic_attach(vap, key); 1138a1b9b6aSSam Leffler } 1148a1b9b6aSSam Leffler 1158a1b9b6aSSam Leffler /* 1168a1b9b6aSSam Leffler * Wrappers for driver key management methods. 1178a1b9b6aSSam Leffler */ 1188a1b9b6aSSam Leffler static __inline int 119b032f27cSSam Leffler dev_key_alloc(struct ieee80211vap *vap, 120e6e547d5SSam Leffler struct ieee80211_key *key, 121c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 1228a1b9b6aSSam Leffler { 123b032f27cSSam Leffler return vap->iv_key_alloc(vap, key, keyix, rxkeyix); 1248a1b9b6aSSam Leffler } 1258a1b9b6aSSam Leffler 1268a1b9b6aSSam Leffler static __inline int 127b032f27cSSam Leffler dev_key_delete(struct ieee80211vap *vap, 1288a1b9b6aSSam Leffler const struct ieee80211_key *key) 1298a1b9b6aSSam Leffler { 130b032f27cSSam Leffler return vap->iv_key_delete(vap, key); 1318a1b9b6aSSam Leffler } 1328a1b9b6aSSam Leffler 1338a1b9b6aSSam Leffler static __inline int 13471fe06caSSam Leffler dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key) 1358a1b9b6aSSam Leffler { 136bc813c40SAdrian Chadd return vap->iv_key_set(vap, key); 1378a1b9b6aSSam Leffler } 1381a1e1d21SSam Leffler 1391a1e1d21SSam Leffler /* 140b032f27cSSam Leffler * Setup crypto support for a device/shared instance. 1411a1e1d21SSam Leffler */ 1421a1e1d21SSam Leffler void 1438a1b9b6aSSam Leffler ieee80211_crypto_attach(struct ieee80211com *ic) 1441a1e1d21SSam Leffler { 1458a1b9b6aSSam Leffler /* NB: we assume everything is pre-zero'd */ 1468a1b9b6aSSam Leffler ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 1471a1e1d21SSam Leffler } 1481a1e1d21SSam Leffler 1498a1b9b6aSSam Leffler /* 1508a1b9b6aSSam Leffler * Teardown crypto support. 1518a1b9b6aSSam Leffler */ 1528a1b9b6aSSam Leffler void 1538a1b9b6aSSam Leffler ieee80211_crypto_detach(struct ieee80211com *ic) 1548a1b9b6aSSam Leffler { 155b032f27cSSam Leffler } 156b032f27cSSam Leffler 157b032f27cSSam Leffler /* 158b032f27cSSam Leffler * Setup crypto support for a vap. 159b032f27cSSam Leffler */ 160b032f27cSSam Leffler void 161b032f27cSSam Leffler ieee80211_crypto_vattach(struct ieee80211vap *vap) 162b032f27cSSam Leffler { 163b032f27cSSam Leffler int i; 164b032f27cSSam Leffler 165b032f27cSSam Leffler /* NB: we assume everything is pre-zero'd */ 166b032f27cSSam Leffler vap->iv_max_keyix = IEEE80211_WEP_NKID; 167b032f27cSSam Leffler vap->iv_def_txkey = IEEE80211_KEYIX_NONE; 168b032f27cSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 169b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i], 170b032f27cSSam Leffler IEEE80211_KEYIX_NONE); 171b032f27cSSam Leffler /* 172b032f27cSSam Leffler * Initialize the driver key support routines to noop entries. 173b032f27cSSam Leffler * This is useful especially for the cipher test modules. 174b032f27cSSam Leffler */ 175b032f27cSSam Leffler vap->iv_key_alloc = null_key_alloc; 176b032f27cSSam Leffler vap->iv_key_set = null_key_set; 177b032f27cSSam Leffler vap->iv_key_delete = null_key_delete; 178b032f27cSSam Leffler vap->iv_key_update_begin = null_key_update; 179b032f27cSSam Leffler vap->iv_key_update_end = null_key_update; 180b032f27cSSam Leffler } 181b032f27cSSam Leffler 182b032f27cSSam Leffler /* 183b032f27cSSam Leffler * Teardown crypto support for a vap. 184b032f27cSSam Leffler */ 185b032f27cSSam Leffler void 186b032f27cSSam Leffler ieee80211_crypto_vdetach(struct ieee80211vap *vap) 187b032f27cSSam Leffler { 188b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(vap); 1891a1e1d21SSam Leffler } 1901a1e1d21SSam Leffler 1918a1b9b6aSSam Leffler /* 1928a1b9b6aSSam Leffler * Register a crypto cipher module. 1938a1b9b6aSSam Leffler */ 1948a1b9b6aSSam Leffler void 1958a1b9b6aSSam Leffler ieee80211_crypto_register(const struct ieee80211_cipher *cip) 1968a1b9b6aSSam Leffler { 1978a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 1988a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 1998a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 2008a1b9b6aSSam Leffler return; 2018a1b9b6aSSam Leffler } 2028a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 2038a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 2048a1b9b6aSSam Leffler __func__, cip->ic_name); 2058a1b9b6aSSam Leffler return; 2068a1b9b6aSSam Leffler } 2078a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = cip; 2088a1b9b6aSSam Leffler } 2098a1b9b6aSSam Leffler 2108a1b9b6aSSam Leffler /* 2118a1b9b6aSSam Leffler * Unregister a crypto cipher module. 2128a1b9b6aSSam Leffler */ 2138a1b9b6aSSam Leffler void 2148a1b9b6aSSam Leffler ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 2158a1b9b6aSSam Leffler { 2168a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 2178a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 2188a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 2198a1b9b6aSSam Leffler return; 2208a1b9b6aSSam Leffler } 2218a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 2228a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 2238a1b9b6aSSam Leffler __func__, cip->ic_name); 2248a1b9b6aSSam Leffler return; 2258a1b9b6aSSam Leffler } 2268a1b9b6aSSam Leffler /* NB: don't complain about not being registered */ 2278a1b9b6aSSam Leffler /* XXX disallow if references */ 2288a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = NULL; 2298a1b9b6aSSam Leffler } 2308a1b9b6aSSam Leffler 2318a1b9b6aSSam Leffler int 2328a1b9b6aSSam Leffler ieee80211_crypto_available(u_int cipher) 2338a1b9b6aSSam Leffler { 2348a1b9b6aSSam Leffler return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 2358a1b9b6aSSam Leffler } 2368a1b9b6aSSam Leffler 2378a1b9b6aSSam Leffler /* XXX well-known names! */ 238b032f27cSSam Leffler static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = { 2399fcf5318SSam Leffler [IEEE80211_CIPHER_WEP] = "wlan_wep", 2409fcf5318SSam Leffler [IEEE80211_CIPHER_TKIP] = "wlan_tkip", 2419fcf5318SSam Leffler [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb", 2429fcf5318SSam Leffler [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp", 2439fcf5318SSam Leffler [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */ 2449fcf5318SSam Leffler [IEEE80211_CIPHER_CKIP] = "wlan_ckip", 2459fcf5318SSam Leffler [IEEE80211_CIPHER_NONE] = "wlan_none", 2468a1b9b6aSSam Leffler }; 2478a1b9b6aSSam Leffler 248411ccf5fSSam Leffler /* NB: there must be no overlap between user-supplied and device-owned flags */ 249411ccf5fSSam Leffler CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0); 250411ccf5fSSam Leffler 2518a1b9b6aSSam Leffler /* 2528a1b9b6aSSam Leffler * Establish a relationship between the specified key and cipher 253dd70e17bSSam Leffler * and, if necessary, allocate a hardware index from the driver. 254411ccf5fSSam Leffler * Note that when a fixed key index is required it must be specified. 2558a1b9b6aSSam Leffler * 2568a1b9b6aSSam Leffler * This must be the first call applied to a key; all the other key 2578a1b9b6aSSam Leffler * routines assume wk_cipher is setup. 2588a1b9b6aSSam Leffler * 2598a1b9b6aSSam Leffler * Locking must be handled by the caller using: 260b032f27cSSam Leffler * ieee80211_key_update_begin(vap); 261b032f27cSSam Leffler * ieee80211_key_update_end(vap); 2628a1b9b6aSSam Leffler */ 2638a1b9b6aSSam Leffler int 264b032f27cSSam Leffler ieee80211_crypto_newkey(struct ieee80211vap *vap, 265dd70e17bSSam Leffler int cipher, int flags, struct ieee80211_key *key) 2668a1b9b6aSSam Leffler { 267b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 2688a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 269c1225b52SSam Leffler ieee80211_keyix keyix, rxkeyix; 2708a1b9b6aSSam Leffler void *keyctx; 2718a1b9b6aSSam Leffler int oflags; 2728a1b9b6aSSam Leffler 27307760642SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 27407760642SSam Leffler "%s: cipher %u flags 0x%x keyix %u\n", 27507760642SSam Leffler __func__, cipher, flags, key->wk_keyix); 27607760642SSam Leffler 2778a1b9b6aSSam Leffler /* 2788a1b9b6aSSam Leffler * Validate cipher and set reference to cipher routines. 2798a1b9b6aSSam Leffler */ 2808a1b9b6aSSam Leffler if (cipher >= IEEE80211_CIPHER_MAX) { 281b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 2828a1b9b6aSSam Leffler "%s: invalid cipher %u\n", __func__, cipher); 283b032f27cSSam Leffler vap->iv_stats.is_crypto_badcipher++; 2848a1b9b6aSSam Leffler return 0; 2858a1b9b6aSSam Leffler } 2868a1b9b6aSSam Leffler cip = ciphers[cipher]; 2878a1b9b6aSSam Leffler if (cip == NULL) { 2888a1b9b6aSSam Leffler /* 2898a1b9b6aSSam Leffler * Auto-load cipher module if we have a well-known name 2908a1b9b6aSSam Leffler * for it. It might be better to use string names rather 2918a1b9b6aSSam Leffler * than numbers and craft a module name based on the cipher 2928a1b9b6aSSam Leffler * name; e.g. wlan_cipher_<cipher-name>. 2938a1b9b6aSSam Leffler */ 294b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 2958a1b9b6aSSam Leffler "%s: unregistered cipher %u, load module %s\n", 2968a1b9b6aSSam Leffler __func__, cipher, cipher_modnames[cipher]); 2978a1b9b6aSSam Leffler ieee80211_load_module(cipher_modnames[cipher]); 2988a1b9b6aSSam Leffler /* 2998a1b9b6aSSam Leffler * If cipher module loaded it should immediately 3008a1b9b6aSSam Leffler * call ieee80211_crypto_register which will fill 3018a1b9b6aSSam Leffler * in the entry in the ciphers array. 3028a1b9b6aSSam Leffler */ 3038a1b9b6aSSam Leffler cip = ciphers[cipher]; 3048a1b9b6aSSam Leffler if (cip == NULL) { 305b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3068a1b9b6aSSam Leffler "%s: unable to load cipher %u, module %s\n", 307b032f27cSSam Leffler __func__, cipher, cipher_modnames[cipher]); 308b032f27cSSam Leffler vap->iv_stats.is_crypto_nocipher++; 3098a1b9b6aSSam Leffler return 0; 3108a1b9b6aSSam Leffler } 3118a1b9b6aSSam Leffler } 3128a1b9b6aSSam Leffler 3138a1b9b6aSSam Leffler oflags = key->wk_flags; 314dd70e17bSSam Leffler flags &= IEEE80211_KEY_COMMON; 315411ccf5fSSam Leffler /* NB: preserve device attributes */ 316411ccf5fSSam Leffler flags |= (oflags & IEEE80211_KEY_DEVICE); 3178a1b9b6aSSam Leffler /* 3188a1b9b6aSSam Leffler * If the hardware does not support the cipher then 3198a1b9b6aSSam Leffler * fallback to a host-based implementation. 3208a1b9b6aSSam Leffler */ 321b032f27cSSam Leffler if ((ic->ic_cryptocaps & (1<<cipher)) == 0) { 322b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3238a1b9b6aSSam Leffler "%s: no h/w support for cipher %s, falling back to s/w\n", 3248a1b9b6aSSam Leffler __func__, cip->ic_name); 325dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWCRYPT; 3268a1b9b6aSSam Leffler } 3278a1b9b6aSSam Leffler /* 3288a1b9b6aSSam Leffler * Hardware TKIP with software MIC is an important 3298a1b9b6aSSam Leffler * combination; we handle it by flagging each key, 3308a1b9b6aSSam Leffler * the cipher modules honor it. 3318a1b9b6aSSam Leffler */ 3328a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP && 333b032f27cSSam Leffler (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) { 334b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3358a1b9b6aSSam Leffler "%s: no h/w support for TKIP MIC, falling back to s/w\n", 3368a1b9b6aSSam Leffler __func__); 337dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWMIC; 3388a1b9b6aSSam Leffler } 3398a1b9b6aSSam Leffler 3408a1b9b6aSSam Leffler /* 3418a1b9b6aSSam Leffler * Bind cipher to key instance. Note we do this 3428a1b9b6aSSam Leffler * after checking the device capabilities so the 3438a1b9b6aSSam Leffler * cipher module can optimize space usage based on 3448a1b9b6aSSam Leffler * whether or not it needs to do the cipher work. 3458a1b9b6aSSam Leffler */ 346dd70e17bSSam Leffler if (key->wk_cipher != cip || key->wk_flags != flags) { 347dd70e17bSSam Leffler /* 348dd70e17bSSam Leffler * Fillin the flags so cipher modules can see s/w 349dd70e17bSSam Leffler * crypto requirements and potentially allocate 350dd70e17bSSam Leffler * different state and/or attach different method 351dd70e17bSSam Leffler * pointers. 352dd70e17bSSam Leffler */ 353dd70e17bSSam Leffler key->wk_flags = flags; 354b032f27cSSam Leffler keyctx = cip->ic_attach(vap, key); 3558a1b9b6aSSam Leffler if (keyctx == NULL) { 356b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3578a1b9b6aSSam Leffler "%s: unable to attach cipher %s\n", 3588a1b9b6aSSam Leffler __func__, cip->ic_name); 3598a1b9b6aSSam Leffler key->wk_flags = oflags; /* restore old flags */ 360b032f27cSSam Leffler vap->iv_stats.is_crypto_attachfail++; 3618a1b9b6aSSam Leffler return 0; 3628a1b9b6aSSam Leffler } 3638a1b9b6aSSam Leffler cipher_detach(key); 3648a1b9b6aSSam Leffler key->wk_cipher = cip; /* XXX refcnt? */ 3658a1b9b6aSSam Leffler key->wk_private = keyctx; 3668a1b9b6aSSam Leffler } 3678a1b9b6aSSam Leffler 3688a1b9b6aSSam Leffler /* 3698a1b9b6aSSam Leffler * Ask the driver for a key index if we don't have one. 3708a1b9b6aSSam Leffler * Note that entries in the global key table always have 3718a1b9b6aSSam Leffler * an index; this means it's safe to call this routine 3728a1b9b6aSSam Leffler * for these entries just to setup the reference to the 3738a1b9b6aSSam Leffler * cipher template. Note also that when using software 3748a1b9b6aSSam Leffler * crypto we also call the driver to give us a key index. 3758a1b9b6aSSam Leffler */ 376e6e547d5SSam Leffler if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 377b032f27cSSam Leffler if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) { 3788a1b9b6aSSam Leffler /* 379e6e547d5SSam Leffler * Unable to setup driver state. 3808a1b9b6aSSam Leffler */ 381b032f27cSSam Leffler vap->iv_stats.is_crypto_keyfail++; 382b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 3838a1b9b6aSSam Leffler "%s: unable to setup cipher %s\n", 3848a1b9b6aSSam Leffler __func__, cip->ic_name); 3858a1b9b6aSSam Leffler return 0; 3868a1b9b6aSSam Leffler } 387e6e547d5SSam Leffler if (key->wk_flags != flags) { 388e6e547d5SSam Leffler /* 389e6e547d5SSam Leffler * Driver overrode flags we setup; typically because 390e6e547d5SSam Leffler * resources were unavailable to handle _this_ key. 391e6e547d5SSam Leffler * Re-attach the cipher context to allow cipher 392e6e547d5SSam Leffler * modules to handle differing requirements. 393e6e547d5SSam Leffler */ 394e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 395e6e547d5SSam Leffler "%s: driver override for cipher %s, flags " 396e6e547d5SSam Leffler "0x%x -> 0x%x\n", __func__, cip->ic_name, 397e6e547d5SSam Leffler oflags, key->wk_flags); 398e6e547d5SSam Leffler keyctx = cip->ic_attach(vap, key); 399e6e547d5SSam Leffler if (keyctx == NULL) { 400e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 401e6e547d5SSam Leffler "%s: unable to attach cipher %s with " 402e6e547d5SSam Leffler "flags 0x%x\n", __func__, cip->ic_name, 403e6e547d5SSam Leffler key->wk_flags); 404e6e547d5SSam Leffler key->wk_flags = oflags; /* restore old flags */ 405e6e547d5SSam Leffler vap->iv_stats.is_crypto_attachfail++; 406e6e547d5SSam Leffler return 0; 407e6e547d5SSam Leffler } 408e6e547d5SSam Leffler cipher_detach(key); 409e6e547d5SSam Leffler key->wk_cipher = cip; /* XXX refcnt? */ 410e6e547d5SSam Leffler key->wk_private = keyctx; 411e6e547d5SSam Leffler } 412c1225b52SSam Leffler key->wk_keyix = keyix; 413c1225b52SSam Leffler key->wk_rxkeyix = rxkeyix; 414e6e547d5SSam Leffler key->wk_flags |= IEEE80211_KEY_DEVKEY; 4158a1b9b6aSSam Leffler } 4168a1b9b6aSSam Leffler return 1; 4178a1b9b6aSSam Leffler } 4188a1b9b6aSSam Leffler 4198a1b9b6aSSam Leffler /* 4208a1b9b6aSSam Leffler * Remove the key (no locking, for internal use). 4218a1b9b6aSSam Leffler */ 4228a1b9b6aSSam Leffler static int 423b032f27cSSam Leffler _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 4248a1b9b6aSSam Leffler { 4258a1b9b6aSSam Leffler KASSERT(key->wk_cipher != NULL, ("No cipher!")); 4268a1b9b6aSSam Leffler 427b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 428fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 429fc508e8eSSam Leffler __func__, key->wk_cipher->ic_name, 430fc508e8eSSam Leffler key->wk_keyix, key->wk_flags, 431b032f27cSSam Leffler key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 432b032f27cSSam Leffler key->wk_keylen); 433fc508e8eSSam Leffler 434e6e547d5SSam Leffler if (key->wk_flags & IEEE80211_KEY_DEVKEY) { 4358a1b9b6aSSam Leffler /* 4368a1b9b6aSSam Leffler * Remove hardware entry. 4378a1b9b6aSSam Leffler */ 4388a1b9b6aSSam Leffler /* XXX key cache */ 439b032f27cSSam Leffler if (!dev_key_delete(vap, key)) { 440b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 4418a1b9b6aSSam Leffler "%s: driver did not delete key index %u\n", 442e6e547d5SSam Leffler __func__, key->wk_keyix); 443b032f27cSSam Leffler vap->iv_stats.is_crypto_delkey++; 4448a1b9b6aSSam Leffler /* XXX recovery? */ 4458a1b9b6aSSam Leffler } 4468a1b9b6aSSam Leffler } 4478a1b9b6aSSam Leffler cipher_detach(key); 4488a1b9b6aSSam Leffler memset(key, 0, sizeof(*key)); 449b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE); 4508a1b9b6aSSam Leffler return 1; 4518a1b9b6aSSam Leffler } 4528a1b9b6aSSam Leffler 4538a1b9b6aSSam Leffler /* 4548a1b9b6aSSam Leffler * Remove the specified key. 4558a1b9b6aSSam Leffler */ 4568a1b9b6aSSam Leffler int 457b032f27cSSam Leffler ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 4588a1b9b6aSSam Leffler { 4598a1b9b6aSSam Leffler int status; 4608a1b9b6aSSam Leffler 461b032f27cSSam Leffler ieee80211_key_update_begin(vap); 462b032f27cSSam Leffler status = _ieee80211_crypto_delkey(vap, key); 463b032f27cSSam Leffler ieee80211_key_update_end(vap); 4648a1b9b6aSSam Leffler return status; 4658a1b9b6aSSam Leffler } 4668a1b9b6aSSam Leffler 4678a1b9b6aSSam Leffler /* 4688a1b9b6aSSam Leffler * Clear the global key table. 4698a1b9b6aSSam Leffler */ 4708a1b9b6aSSam Leffler void 471b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap) 4728a1b9b6aSSam Leffler { 4738a1b9b6aSSam Leffler int i; 4748a1b9b6aSSam Leffler 475b032f27cSSam Leffler ieee80211_key_update_begin(vap); 4768a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 477b032f27cSSam Leffler (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]); 478b032f27cSSam Leffler ieee80211_key_update_end(vap); 4798a1b9b6aSSam Leffler } 4808a1b9b6aSSam Leffler 4818a1b9b6aSSam Leffler /* 4828a1b9b6aSSam Leffler * Set the contents of the specified key. 4838a1b9b6aSSam Leffler * 4848a1b9b6aSSam Leffler * Locking must be handled by the caller using: 485b032f27cSSam Leffler * ieee80211_key_update_begin(vap); 486b032f27cSSam Leffler * ieee80211_key_update_end(vap); 4878a1b9b6aSSam Leffler */ 4888a1b9b6aSSam Leffler int 48971fe06caSSam Leffler ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key) 4908a1b9b6aSSam Leffler { 4918a1b9b6aSSam Leffler const struct ieee80211_cipher *cip = key->wk_cipher; 4928a1b9b6aSSam Leffler 4938a1b9b6aSSam Leffler KASSERT(cip != NULL, ("No cipher!")); 4948a1b9b6aSSam Leffler 495b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 496fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 497fc508e8eSSam Leffler __func__, cip->ic_name, key->wk_keyix, 49871fe06caSSam Leffler key->wk_flags, ether_sprintf(key->wk_macaddr), 499b032f27cSSam Leffler key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 500b032f27cSSam Leffler key->wk_keylen); 501fc508e8eSSam Leffler 502e6e547d5SSam Leffler if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 503e6e547d5SSam Leffler /* XXX nothing allocated, should not happen */ 504e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 505e6e547d5SSam Leffler "%s: no device key setup done; should not happen!\n", 506e6e547d5SSam Leffler __func__); 507e6e547d5SSam Leffler vap->iv_stats.is_crypto_setkey_nokey++; 508e6e547d5SSam Leffler return 0; 509e6e547d5SSam Leffler } 5108a1b9b6aSSam Leffler /* 5118a1b9b6aSSam Leffler * Give cipher a chance to validate key contents. 5128a1b9b6aSSam Leffler * XXX should happen before modifying state. 5138a1b9b6aSSam Leffler */ 5148a1b9b6aSSam Leffler if (!cip->ic_setkey(key)) { 515b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 5168a1b9b6aSSam Leffler "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 5178a1b9b6aSSam Leffler __func__, cip->ic_name, key->wk_keyix, 5188a1b9b6aSSam Leffler key->wk_keylen, key->wk_flags); 519b032f27cSSam Leffler vap->iv_stats.is_crypto_setkey_cipher++; 5208a1b9b6aSSam Leffler return 0; 5218a1b9b6aSSam Leffler } 52271fe06caSSam Leffler return dev_key_set(vap, key); 5238a1b9b6aSSam Leffler } 5248a1b9b6aSSam Leffler 52554a95d0dSAdrian Chadd /* 52654a95d0dSAdrian Chadd * Return index if the key is a WEP key (0..3); -1 otherwise. 52754a95d0dSAdrian Chadd * 52854a95d0dSAdrian Chadd * This is different to "get_keyid" which defaults to returning 52954a95d0dSAdrian Chadd * 0 for unicast keys; it assumes that it won't be used for WEP. 53054a95d0dSAdrian Chadd */ 53154a95d0dSAdrian Chadd int 53254a95d0dSAdrian Chadd ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap, 53354a95d0dSAdrian Chadd const struct ieee80211_key *k) 53454a95d0dSAdrian Chadd { 53554a95d0dSAdrian Chadd 53654a95d0dSAdrian Chadd if (k >= &vap->iv_nw_keys[0] && 53754a95d0dSAdrian Chadd k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 53854a95d0dSAdrian Chadd return (k - vap->iv_nw_keys); 539d0155f67SXin LI return (-1); 54054a95d0dSAdrian Chadd } 54154a95d0dSAdrian Chadd 54254a95d0dSAdrian Chadd /* 54354a95d0dSAdrian Chadd * Note: only supports a single unicast key (0). 54454a95d0dSAdrian Chadd */ 545ef0d8f63SAdrian Chadd uint8_t 546ef0d8f63SAdrian Chadd ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k) 547ef0d8f63SAdrian Chadd { 548ef0d8f63SAdrian Chadd if (k >= &vap->iv_nw_keys[0] && 549ef0d8f63SAdrian Chadd k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 550ef0d8f63SAdrian Chadd return (k - vap->iv_nw_keys); 551ef0d8f63SAdrian Chadd else 552ef0d8f63SAdrian Chadd return (0); 553ef0d8f63SAdrian Chadd } 554ef0d8f63SAdrian Chadd 5558a1b9b6aSSam Leffler struct ieee80211_key * 55615395998SAdrian Chadd ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m) 5578a1b9b6aSSam Leffler { 558b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 5598a1b9b6aSSam Leffler struct ieee80211_frame *wh; 5608a1b9b6aSSam Leffler 5618a1b9b6aSSam Leffler /* 5628a1b9b6aSSam Leffler * Multicast traffic always uses the multicast key. 563*61605e0aSdomienschepers * 564*61605e0aSdomienschepers * Historically we would fall back to the default 565*61605e0aSdomienschepers * transmit key if there was no unicast key. This 566*61605e0aSdomienschepers * behaviour was documented up to IEEE Std 802.11-2016, 567*61605e0aSdomienschepers * 12.9.2.2 Per-MSDU/Per-A-MSDU Tx pseudocode, in the 568*61605e0aSdomienschepers * 'else' case but is no longer in later versions of 569*61605e0aSdomienschepers * the standard. Additionally falling back to the 570*61605e0aSdomienschepers * group key for unicast was a security risk. 5718a1b9b6aSSam Leffler */ 5728a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 573*61605e0aSdomienschepers if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 574b032f27cSSam Leffler if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) { 575b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 576b032f27cSSam Leffler wh->i_addr1, 577b032f27cSSam Leffler "no default transmit key (%s) deftxkey %u", 578b032f27cSSam Leffler __func__, vap->iv_def_txkey); 579b032f27cSSam Leffler vap->iv_stats.is_tx_nodefkey++; 5808a1b9b6aSSam Leffler return NULL; 5818a1b9b6aSSam Leffler } 58215395998SAdrian Chadd return &vap->iv_nw_keys[vap->iv_def_txkey]; 58315395998SAdrian Chadd } 584ef0d8f63SAdrian Chadd 585*61605e0aSdomienschepers if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 586*61605e0aSdomienschepers return NULL; 58715395998SAdrian Chadd return &ni->ni_ucastkey; 58815395998SAdrian Chadd } 58915395998SAdrian Chadd 59015395998SAdrian Chadd /* 59115395998SAdrian Chadd * Add privacy headers appropriate for the specified key. 59215395998SAdrian Chadd */ 59315395998SAdrian Chadd struct ieee80211_key * 59415395998SAdrian Chadd ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m) 59515395998SAdrian Chadd { 59615395998SAdrian Chadd struct ieee80211_key *k; 59715395998SAdrian Chadd const struct ieee80211_cipher *cip; 59815395998SAdrian Chadd 59915395998SAdrian Chadd if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) { 6008a1b9b6aSSam Leffler cip = k->wk_cipher; 601ef0d8f63SAdrian Chadd return (cip->ic_encap(k, m) ? k : NULL); 6028a1b9b6aSSam Leffler } 6038a1b9b6aSSam Leffler 60415395998SAdrian Chadd return NULL; 60515395998SAdrian Chadd } 60615395998SAdrian Chadd 6078a1b9b6aSSam Leffler /* 6088a1b9b6aSSam Leffler * Validate and strip privacy headers (and trailer) for a 6098a1b9b6aSSam Leffler * received frame that has the WEP/Privacy bit set. 6108a1b9b6aSSam Leffler */ 611fe75b452SAdrian Chadd int 612fe75b452SAdrian Chadd ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen, 613fe75b452SAdrian Chadd struct ieee80211_key **key) 6148a1b9b6aSSam Leffler { 6158a1b9b6aSSam Leffler #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 6168a1b9b6aSSam Leffler #define IEEE80211_WEP_MINLEN \ 617a92c6eb0SSam Leffler (sizeof(struct ieee80211_frame) + \ 6188a1b9b6aSSam Leffler IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 619b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap; 6208a1b9b6aSSam Leffler struct ieee80211_key *k; 6218a1b9b6aSSam Leffler struct ieee80211_frame *wh; 622fe75b452SAdrian Chadd const struct ieee80211_rx_stats *rxs; 6238a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 62468e8e04eSSam Leffler uint8_t keyid; 6258a1b9b6aSSam Leffler 626fe75b452SAdrian Chadd /* 627fe75b452SAdrian Chadd * Check for hardware decryption and IV stripping. 628fe75b452SAdrian Chadd * If the IV is stripped then we definitely can't find a key. 629fe75b452SAdrian Chadd * Set the key to NULL but return true; upper layers 630fe75b452SAdrian Chadd * will need to handle a NULL key for a successful 631fe75b452SAdrian Chadd * decrypt. 632fe75b452SAdrian Chadd */ 633fe75b452SAdrian Chadd rxs = ieee80211_get_rx_params_ptr(m); 634fe75b452SAdrian Chadd if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 635fe75b452SAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) { 636fe75b452SAdrian Chadd /* 637fe75b452SAdrian Chadd * Hardware decrypted, IV stripped. 638fe75b452SAdrian Chadd * We can't find a key with a stripped IV. 639fe75b452SAdrian Chadd * Return successful. 640fe75b452SAdrian Chadd */ 641fe75b452SAdrian Chadd *key = NULL; 642fe75b452SAdrian Chadd return (1); 643fe75b452SAdrian Chadd } 644fe75b452SAdrian Chadd } 645fe75b452SAdrian Chadd 6468a1b9b6aSSam Leffler /* NB: this minimum size data frame could be bigger */ 6478a1b9b6aSSam Leffler if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 648b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 6498a1b9b6aSSam Leffler "%s: WEP data frame too short, len %u\n", 6508a1b9b6aSSam Leffler __func__, m->m_pkthdr.len); 651b032f27cSSam Leffler vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */ 652fe75b452SAdrian Chadd *key = NULL; 653fe75b452SAdrian Chadd return (0); 6541a1e1d21SSam Leffler } 6551a1e1d21SSam Leffler 6561a1e1d21SSam Leffler /* 6578a1b9b6aSSam Leffler * Locate the key. If unicast and there is no unicast 6588a1b9b6aSSam Leffler * key then we fall back to the key id in the header. 6598a1b9b6aSSam Leffler * This assumes unicast keys are only configured when 6608a1b9b6aSSam Leffler * the key id in the header is meaningless (typically 0). 6611a1e1d21SSam Leffler */ 6628a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 663b032f27cSSam Leffler m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid); 6648a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 665cda15ce1SSam Leffler IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 666b032f27cSSam Leffler k = &vap->iv_nw_keys[keyid >> 6]; 6671a1e1d21SSam Leffler else 6688a1b9b6aSSam Leffler k = &ni->ni_ucastkey; 6691a1e1d21SSam Leffler 6701a1e1d21SSam Leffler /* 67179e0962dSAndriy Voskoboinyk * Insure crypto header is contiguous and long enough for all 67279e0962dSAndriy Voskoboinyk * decap work. 6731a1e1d21SSam Leffler */ 6748a1b9b6aSSam Leffler cip = k->wk_cipher; 67579e0962dSAndriy Voskoboinyk if (m->m_len < hdrlen + cip->ic_header) { 676b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 67779e0962dSAndriy Voskoboinyk "frame is too short (%d < %u) for crypto decap", 67879e0962dSAndriy Voskoboinyk cip->ic_name, m->m_len, hdrlen + cip->ic_header); 67979e0962dSAndriy Voskoboinyk vap->iv_stats.is_rx_tooshort++; 680fe75b452SAdrian Chadd *key = NULL; 681fe75b452SAdrian Chadd return (0); 6828a1b9b6aSSam Leffler } 6831a1e1d21SSam Leffler 684fe75b452SAdrian Chadd /* 685fe75b452SAdrian Chadd * Attempt decryption. 686fe75b452SAdrian Chadd * 687fe75b452SAdrian Chadd * If we fail then don't return the key - return NULL 688fe75b452SAdrian Chadd * and an error. 689fe75b452SAdrian Chadd */ 690fe75b452SAdrian Chadd if (cip->ic_decap(k, m, hdrlen)) { 691fe75b452SAdrian Chadd /* success */ 692fe75b452SAdrian Chadd *key = k; 693fe75b452SAdrian Chadd return (1); 694fe75b452SAdrian Chadd } 695fe75b452SAdrian Chadd 696fe75b452SAdrian Chadd /* Failure */ 697fe75b452SAdrian Chadd *key = NULL; 698fe75b452SAdrian Chadd return (0); 6998a1b9b6aSSam Leffler #undef IEEE80211_WEP_MINLEN 7008a1b9b6aSSam Leffler #undef IEEE80211_WEP_HDRLEN 7011a1e1d21SSam Leffler } 70206b2d888SSam Leffler 703ee9d294bSAdrian Chadd /* 704ee9d294bSAdrian Chadd * Check and remove any MIC. 705ee9d294bSAdrian Chadd */ 706ee9d294bSAdrian Chadd int 707ee9d294bSAdrian Chadd ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k, 708ee9d294bSAdrian Chadd struct mbuf *m, int force) 709ee9d294bSAdrian Chadd { 710ee9d294bSAdrian Chadd const struct ieee80211_cipher *cip; 711ee9d294bSAdrian Chadd const struct ieee80211_rx_stats *rxs; 712ee9d294bSAdrian Chadd struct ieee80211_frame *wh; 713ee9d294bSAdrian Chadd 714ee9d294bSAdrian Chadd rxs = ieee80211_get_rx_params_ptr(m); 715ee9d294bSAdrian Chadd wh = mtod(m, struct ieee80211_frame *); 716ee9d294bSAdrian Chadd 717ee9d294bSAdrian Chadd /* 718ee9d294bSAdrian Chadd * Handle demic / mic errors from hardware-decrypted offload devices. 719ee9d294bSAdrian Chadd */ 720ee9d294bSAdrian Chadd if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 721ee9d294bSAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) { 722ee9d294bSAdrian Chadd /* 723ee9d294bSAdrian Chadd * Hardware has said MIC failed. We don't care about 724ee9d294bSAdrian Chadd * whether it was stripped or not. 725ee9d294bSAdrian Chadd * 726ee9d294bSAdrian Chadd * Eventually - teach the demic methods in crypto 727ee9d294bSAdrian Chadd * modules to handle a NULL key and not to dereference 728ee9d294bSAdrian Chadd * it. 729ee9d294bSAdrian Chadd */ 730ee9d294bSAdrian Chadd ieee80211_notify_michael_failure(vap, wh, -1); 731ee9d294bSAdrian Chadd return (0); 732ee9d294bSAdrian Chadd } 733ee9d294bSAdrian Chadd 734ee9d294bSAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) { 735ee9d294bSAdrian Chadd /* 736ee9d294bSAdrian Chadd * Hardware has decrypted and not indicated a 737ee9d294bSAdrian Chadd * MIC failure and has stripped the MIC. 738ee9d294bSAdrian Chadd * We may not have a key, so for now just 739ee9d294bSAdrian Chadd * return OK. 740ee9d294bSAdrian Chadd */ 741ee9d294bSAdrian Chadd return (1); 742ee9d294bSAdrian Chadd } 743ee9d294bSAdrian Chadd } 744ee9d294bSAdrian Chadd 745ee9d294bSAdrian Chadd /* 746ee9d294bSAdrian Chadd * If we don't have a key at this point then we don't 747ee9d294bSAdrian Chadd * have to demic anything. 748ee9d294bSAdrian Chadd */ 749ee9d294bSAdrian Chadd if (k == NULL) 750ee9d294bSAdrian Chadd return (1); 751ee9d294bSAdrian Chadd 752ee9d294bSAdrian Chadd cip = k->wk_cipher; 753ee9d294bSAdrian Chadd return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1); 754ee9d294bSAdrian Chadd } 755ee9d294bSAdrian Chadd 75606b2d888SSam Leffler static void 75706b2d888SSam Leffler load_ucastkey(void *arg, struct ieee80211_node *ni) 75806b2d888SSam Leffler { 75906b2d888SSam Leffler struct ieee80211vap *vap = ni->ni_vap; 76006b2d888SSam Leffler struct ieee80211_key *k; 76106b2d888SSam Leffler 76206b2d888SSam Leffler if (vap->iv_state != IEEE80211_S_RUN) 76306b2d888SSam Leffler return; 76406b2d888SSam Leffler k = &ni->ni_ucastkey; 765e6e547d5SSam Leffler if (k->wk_flags & IEEE80211_KEY_DEVKEY) 76606b2d888SSam Leffler dev_key_set(vap, k); 76706b2d888SSam Leffler } 76806b2d888SSam Leffler 76906b2d888SSam Leffler /* 77006b2d888SSam Leffler * Re-load all keys known to the 802.11 layer that may 77106b2d888SSam Leffler * have hardware state backing them. This is used by 77206b2d888SSam Leffler * drivers on resume to push keys down into the device. 77306b2d888SSam Leffler */ 77406b2d888SSam Leffler void 77506b2d888SSam Leffler ieee80211_crypto_reload_keys(struct ieee80211com *ic) 77606b2d888SSam Leffler { 77706b2d888SSam Leffler struct ieee80211vap *vap; 77806b2d888SSam Leffler int i; 77906b2d888SSam Leffler 78006b2d888SSam Leffler /* 78106b2d888SSam Leffler * Keys in the global key table of each vap. 78206b2d888SSam Leffler */ 78306b2d888SSam Leffler /* NB: used only during resume so don't lock for now */ 78406b2d888SSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 78506b2d888SSam Leffler if (vap->iv_state != IEEE80211_S_RUN) 78606b2d888SSam Leffler continue; 78706b2d888SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) { 78806b2d888SSam Leffler const struct ieee80211_key *k = &vap->iv_nw_keys[i]; 789e6e547d5SSam Leffler if (k->wk_flags & IEEE80211_KEY_DEVKEY) 79006b2d888SSam Leffler dev_key_set(vap, k); 79106b2d888SSam Leffler } 79206b2d888SSam Leffler } 79306b2d888SSam Leffler /* 79406b2d888SSam Leffler * Unicast keys. 79506b2d888SSam Leffler */ 79606b2d888SSam Leffler ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL); 79706b2d888SSam Leffler } 798781487cfSAdrian Chadd 799781487cfSAdrian Chadd /* 800781487cfSAdrian Chadd * Set the default key index for WEP, or KEYIX_NONE for no default TX key. 801781487cfSAdrian Chadd * 802781487cfSAdrian Chadd * This should be done as part of a key update block (iv_key_update_begin / 803781487cfSAdrian Chadd * iv_key_update_end.) 804781487cfSAdrian Chadd */ 805781487cfSAdrian Chadd void 806781487cfSAdrian Chadd ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid) 807781487cfSAdrian Chadd { 808781487cfSAdrian Chadd 809781487cfSAdrian Chadd /* XXX TODO: assert we're in a key update block */ 810781487cfSAdrian Chadd 811781487cfSAdrian Chadd vap->iv_update_deftxkey(vap, kid); 812781487cfSAdrian Chadd } 813