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
null_key_alloc(struct ieee80211vap * vap,struct ieee80211_key * k,ieee80211_keyix * keyix,ieee80211_keyix * rxkeyix)62e6e547d5SSam Leffler null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
63c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
641a1e1d21SSam Leffler {
65*2589197aSAdrian Chadd
66*2589197aSAdrian Chadd if (!ieee80211_is_key_global(vap, k)) {
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
null_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)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
null_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)92bc813c40SAdrian Chadd null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
938a1b9b6aSSam Leffler {
948a1b9b6aSSam Leffler return 1;
958a1b9b6aSSam Leffler }
null_key_update(struct ieee80211vap * vap)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
cipher_detach(struct ieee80211_key * key)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 *
cipher_attach(struct ieee80211vap * vap,struct ieee80211_key * key)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
dev_key_alloc(struct ieee80211vap * vap,struct ieee80211_key * key,ieee80211_keyix * keyix,ieee80211_keyix * rxkeyix)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
dev_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * key)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
dev_key_set(struct ieee80211vap * vap,const struct ieee80211_key * key)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
ieee80211_crypto_attach(struct ieee80211com * ic)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;
157c7f5f140SAdrian Chadd
158c7f5f140SAdrian Chadd /*
159c7f5f140SAdrian Chadd * Default set of key management types supported by net80211.
160c7f5f140SAdrian Chadd *
161c7f5f140SAdrian Chadd * These are supported by software net80211 and announced/
162c7f5f140SAdrian Chadd * driven by hostapd + wpa_supplicant.
163c7f5f140SAdrian Chadd *
164c7f5f140SAdrian Chadd * Drivers doing full supplicant offload must not set
165c7f5f140SAdrian Chadd * anything here.
166c7f5f140SAdrian Chadd *
167c7f5f140SAdrian Chadd * Note that IEEE80211_C_WPA1 and IEEE80211_C_WPA2 are the
168c7f5f140SAdrian Chadd * "old" style way of drivers announcing key management
169c7f5f140SAdrian Chadd * capabilities. There are many, many more key management
170c7f5f140SAdrian Chadd * suites in 802.11-2016 (see 9.4.2.25.3 - AKM suites.)
171c7f5f140SAdrian Chadd * For now they still need to be set - these flags are checked
172c7f5f140SAdrian Chadd * when assembling a beacon to reserve space for the WPA
173c7f5f140SAdrian Chadd * vendor IE (WPA 1) and RSN IE (WPA 2).
174c7f5f140SAdrian Chadd */
175c7f5f140SAdrian Chadd ic->ic_sw_keymgmtcaps = 0;
1761a1e1d21SSam Leffler }
1771a1e1d21SSam Leffler
1788a1b9b6aSSam Leffler /*
1798a1b9b6aSSam Leffler * Teardown crypto support.
1808a1b9b6aSSam Leffler */
1818a1b9b6aSSam Leffler void
ieee80211_crypto_detach(struct ieee80211com * ic)1828a1b9b6aSSam Leffler ieee80211_crypto_detach(struct ieee80211com *ic)
1838a1b9b6aSSam Leffler {
184b032f27cSSam Leffler }
185b032f27cSSam Leffler
186b032f27cSSam Leffler /*
187e9961ea1SAdrian Chadd * Set the supported ciphers for software encryption.
188e9961ea1SAdrian Chadd */
189e9961ea1SAdrian Chadd void
ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com * ic,uint32_t cipher_set)190e9961ea1SAdrian Chadd ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *ic,
191e9961ea1SAdrian Chadd uint32_t cipher_set)
192e9961ea1SAdrian Chadd {
193e9961ea1SAdrian Chadd ic->ic_sw_cryptocaps = cipher_set;
194e9961ea1SAdrian Chadd }
195e9961ea1SAdrian Chadd
196e9961ea1SAdrian Chadd /*
197e9961ea1SAdrian Chadd * Set the supported ciphers for hardware encryption.
198e9961ea1SAdrian Chadd */
199e9961ea1SAdrian Chadd void
ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com * ic,uint32_t cipher_set)200e9961ea1SAdrian Chadd ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *ic,
201e9961ea1SAdrian Chadd uint32_t cipher_set)
202e9961ea1SAdrian Chadd {
203e9961ea1SAdrian Chadd ic->ic_cryptocaps = cipher_set;
204e9961ea1SAdrian Chadd }
205e9961ea1SAdrian Chadd
206c7f5f140SAdrian Chadd /*
207c7f5f140SAdrian Chadd * Set the supported software key management by the driver.
208c7f5f140SAdrian Chadd *
209c7f5f140SAdrian Chadd * These are the key management suites that are supported via
210c7f5f140SAdrian Chadd * the driver via hostapd/wpa_supplicant.
211c7f5f140SAdrian Chadd *
212c7f5f140SAdrian Chadd * Key management which is completely offloaded (ie, the supplicant
213c7f5f140SAdrian Chadd * runs in hardware/firmware) must not be set here.
214c7f5f140SAdrian Chadd */
215c7f5f140SAdrian Chadd void
ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com * ic,uint32_t keymgmt_set)216c7f5f140SAdrian Chadd ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *ic,
217c7f5f140SAdrian Chadd uint32_t keymgmt_set)
218c7f5f140SAdrian Chadd {
219c7f5f140SAdrian Chadd
220c7f5f140SAdrian Chadd ic->ic_sw_keymgmtcaps = keymgmt_set;
221c7f5f140SAdrian Chadd }
222e9961ea1SAdrian Chadd
223e9961ea1SAdrian Chadd /*
224b032f27cSSam Leffler * Setup crypto support for a vap.
225b032f27cSSam Leffler */
226b032f27cSSam Leffler void
ieee80211_crypto_vattach(struct ieee80211vap * vap)227b032f27cSSam Leffler ieee80211_crypto_vattach(struct ieee80211vap *vap)
228b032f27cSSam Leffler {
229b032f27cSSam Leffler int i;
230b032f27cSSam Leffler
231b032f27cSSam Leffler /* NB: we assume everything is pre-zero'd */
232b032f27cSSam Leffler vap->iv_max_keyix = IEEE80211_WEP_NKID;
233b032f27cSSam Leffler vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
234b032f27cSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++)
235b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
236b032f27cSSam Leffler IEEE80211_KEYIX_NONE);
237b032f27cSSam Leffler /*
238b032f27cSSam Leffler * Initialize the driver key support routines to noop entries.
239b032f27cSSam Leffler * This is useful especially for the cipher test modules.
240b032f27cSSam Leffler */
241b032f27cSSam Leffler vap->iv_key_alloc = null_key_alloc;
242b032f27cSSam Leffler vap->iv_key_set = null_key_set;
243b032f27cSSam Leffler vap->iv_key_delete = null_key_delete;
244b032f27cSSam Leffler vap->iv_key_update_begin = null_key_update;
245b032f27cSSam Leffler vap->iv_key_update_end = null_key_update;
246b032f27cSSam Leffler }
247b032f27cSSam Leffler
248b032f27cSSam Leffler /*
249b032f27cSSam Leffler * Teardown crypto support for a vap.
250b032f27cSSam Leffler */
251b032f27cSSam Leffler void
ieee80211_crypto_vdetach(struct ieee80211vap * vap)252b032f27cSSam Leffler ieee80211_crypto_vdetach(struct ieee80211vap *vap)
253b032f27cSSam Leffler {
254b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(vap);
2551a1e1d21SSam Leffler }
2561a1e1d21SSam Leffler
2578a1b9b6aSSam Leffler /*
2588a1b9b6aSSam Leffler * Register a crypto cipher module.
2598a1b9b6aSSam Leffler */
2608a1b9b6aSSam Leffler void
ieee80211_crypto_register(const struct ieee80211_cipher * cip)2618a1b9b6aSSam Leffler ieee80211_crypto_register(const struct ieee80211_cipher *cip)
2628a1b9b6aSSam Leffler {
2638a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
2648a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n",
2658a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher);
2668a1b9b6aSSam Leffler return;
2678a1b9b6aSSam Leffler }
2688a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
2698a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n",
2708a1b9b6aSSam Leffler __func__, cip->ic_name);
2718a1b9b6aSSam Leffler return;
2728a1b9b6aSSam Leffler }
2738a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = cip;
2748a1b9b6aSSam Leffler }
2758a1b9b6aSSam Leffler
2768a1b9b6aSSam Leffler /*
2778a1b9b6aSSam Leffler * Unregister a crypto cipher module.
2788a1b9b6aSSam Leffler */
2798a1b9b6aSSam Leffler void
ieee80211_crypto_unregister(const struct ieee80211_cipher * cip)2808a1b9b6aSSam Leffler ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
2818a1b9b6aSSam Leffler {
2828a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
2838a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n",
2848a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher);
2858a1b9b6aSSam Leffler return;
2868a1b9b6aSSam Leffler }
2878a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
2888a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n",
2898a1b9b6aSSam Leffler __func__, cip->ic_name);
2908a1b9b6aSSam Leffler return;
2918a1b9b6aSSam Leffler }
2928a1b9b6aSSam Leffler /* NB: don't complain about not being registered */
2938a1b9b6aSSam Leffler /* XXX disallow if references */
2948a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = NULL;
2958a1b9b6aSSam Leffler }
2968a1b9b6aSSam Leffler
2978a1b9b6aSSam Leffler int
ieee80211_crypto_available(u_int cipher)2988a1b9b6aSSam Leffler ieee80211_crypto_available(u_int cipher)
2998a1b9b6aSSam Leffler {
3008a1b9b6aSSam Leffler return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
3018a1b9b6aSSam Leffler }
3028a1b9b6aSSam Leffler
3038a1b9b6aSSam Leffler /* XXX well-known names! */
304b032f27cSSam Leffler static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
3059fcf5318SSam Leffler [IEEE80211_CIPHER_WEP] = "wlan_wep",
3069fcf5318SSam Leffler [IEEE80211_CIPHER_TKIP] = "wlan_tkip",
3079fcf5318SSam Leffler [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
3089fcf5318SSam Leffler [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
3099fcf5318SSam Leffler [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */
3109fcf5318SSam Leffler [IEEE80211_CIPHER_CKIP] = "wlan_ckip",
3119fcf5318SSam Leffler [IEEE80211_CIPHER_NONE] = "wlan_none",
31298e8df90SAdrian Chadd [IEEE80211_CIPHER_AES_CCM_256] = "wlan_ccmp",
31398e8df90SAdrian Chadd [IEEE80211_CIPHER_BIP_CMAC_128] = "wlan_bip_cmac",
31498e8df90SAdrian Chadd [IEEE80211_CIPHER_BIP_CMAC_256] = "wlan_bip_cmac",
31598e8df90SAdrian Chadd [IEEE80211_CIPHER_BIP_GMAC_128] = "wlan_bip_gmac",
31698e8df90SAdrian Chadd [IEEE80211_CIPHER_BIP_GMAC_256] = "wlan_bip_gmac",
31798e8df90SAdrian Chadd [IEEE80211_CIPHER_AES_GCM_128] = "wlan_gcmp",
31898e8df90SAdrian Chadd [IEEE80211_CIPHER_AES_GCM_256] = "wlan_gcmp",
3198a1b9b6aSSam Leffler };
3208a1b9b6aSSam Leffler
321411ccf5fSSam Leffler /* NB: there must be no overlap between user-supplied and device-owned flags */
322411ccf5fSSam Leffler CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
323411ccf5fSSam Leffler
3248a1b9b6aSSam Leffler /*
3258a1b9b6aSSam Leffler * Establish a relationship between the specified key and cipher
326dd70e17bSSam Leffler * and, if necessary, allocate a hardware index from the driver.
327411ccf5fSSam Leffler * Note that when a fixed key index is required it must be specified.
3288a1b9b6aSSam Leffler *
3298a1b9b6aSSam Leffler * This must be the first call applied to a key; all the other key
3308a1b9b6aSSam Leffler * routines assume wk_cipher is setup.
3318a1b9b6aSSam Leffler *
3328a1b9b6aSSam Leffler * Locking must be handled by the caller using:
333b032f27cSSam Leffler * ieee80211_key_update_begin(vap);
334b032f27cSSam Leffler * ieee80211_key_update_end(vap);
3358a1b9b6aSSam Leffler */
3368a1b9b6aSSam Leffler int
ieee80211_crypto_newkey(struct ieee80211vap * vap,int cipher,int flags,struct ieee80211_key * key)337b032f27cSSam Leffler ieee80211_crypto_newkey(struct ieee80211vap *vap,
338dd70e17bSSam Leffler int cipher, int flags, struct ieee80211_key *key)
3398a1b9b6aSSam Leffler {
340b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic;
3418a1b9b6aSSam Leffler const struct ieee80211_cipher *cip;
342c1225b52SSam Leffler ieee80211_keyix keyix, rxkeyix;
3438a1b9b6aSSam Leffler void *keyctx;
3448a1b9b6aSSam Leffler int oflags;
3458a1b9b6aSSam Leffler
34607760642SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
34707760642SSam Leffler "%s: cipher %u flags 0x%x keyix %u\n",
34807760642SSam Leffler __func__, cipher, flags, key->wk_keyix);
34907760642SSam Leffler
3508a1b9b6aSSam Leffler /*
3518a1b9b6aSSam Leffler * Validate cipher and set reference to cipher routines.
3528a1b9b6aSSam Leffler */
3538a1b9b6aSSam Leffler if (cipher >= IEEE80211_CIPHER_MAX) {
354b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3558a1b9b6aSSam Leffler "%s: invalid cipher %u\n", __func__, cipher);
356b032f27cSSam Leffler vap->iv_stats.is_crypto_badcipher++;
3578a1b9b6aSSam Leffler return 0;
3588a1b9b6aSSam Leffler }
3598a1b9b6aSSam Leffler cip = ciphers[cipher];
3608a1b9b6aSSam Leffler if (cip == NULL) {
3618a1b9b6aSSam Leffler /*
3628a1b9b6aSSam Leffler * Auto-load cipher module if we have a well-known name
3638a1b9b6aSSam Leffler * for it. It might be better to use string names rather
3648a1b9b6aSSam Leffler * than numbers and craft a module name based on the cipher
3658a1b9b6aSSam Leffler * name; e.g. wlan_cipher_<cipher-name>.
3668a1b9b6aSSam Leffler */
367b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3688a1b9b6aSSam Leffler "%s: unregistered cipher %u, load module %s\n",
3698a1b9b6aSSam Leffler __func__, cipher, cipher_modnames[cipher]);
3708a1b9b6aSSam Leffler ieee80211_load_module(cipher_modnames[cipher]);
3718a1b9b6aSSam Leffler /*
3728a1b9b6aSSam Leffler * If cipher module loaded it should immediately
3738a1b9b6aSSam Leffler * call ieee80211_crypto_register which will fill
3748a1b9b6aSSam Leffler * in the entry in the ciphers array.
3758a1b9b6aSSam Leffler */
3768a1b9b6aSSam Leffler cip = ciphers[cipher];
3778a1b9b6aSSam Leffler if (cip == NULL) {
378b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3798a1b9b6aSSam Leffler "%s: unable to load cipher %u, module %s\n",
380b032f27cSSam Leffler __func__, cipher, cipher_modnames[cipher]);
381b032f27cSSam Leffler vap->iv_stats.is_crypto_nocipher++;
3828a1b9b6aSSam Leffler return 0;
3838a1b9b6aSSam Leffler }
3848a1b9b6aSSam Leffler }
3858a1b9b6aSSam Leffler
3868a1b9b6aSSam Leffler oflags = key->wk_flags;
387dd70e17bSSam Leffler flags &= IEEE80211_KEY_COMMON;
388411ccf5fSSam Leffler /* NB: preserve device attributes */
389411ccf5fSSam Leffler flags |= (oflags & IEEE80211_KEY_DEVICE);
3908a1b9b6aSSam Leffler /*
3918a1b9b6aSSam Leffler * If the hardware does not support the cipher then
3928a1b9b6aSSam Leffler * fallback to a host-based implementation.
3938a1b9b6aSSam Leffler */
394b032f27cSSam Leffler if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
395b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
3968a1b9b6aSSam Leffler "%s: no h/w support for cipher %s, falling back to s/w\n",
3978a1b9b6aSSam Leffler __func__, cip->ic_name);
398dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWCRYPT;
3998a1b9b6aSSam Leffler }
4008a1b9b6aSSam Leffler /*
4018a1b9b6aSSam Leffler * Hardware TKIP with software MIC is an important
4028a1b9b6aSSam Leffler * combination; we handle it by flagging each key,
4038a1b9b6aSSam Leffler * the cipher modules honor it.
4048a1b9b6aSSam Leffler */
4058a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP &&
406b032f27cSSam Leffler (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
407b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
4088a1b9b6aSSam Leffler "%s: no h/w support for TKIP MIC, falling back to s/w\n",
4098a1b9b6aSSam Leffler __func__);
410dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWMIC;
4118a1b9b6aSSam Leffler }
4128a1b9b6aSSam Leffler
4138a1b9b6aSSam Leffler /*
4148a1b9b6aSSam Leffler * Bind cipher to key instance. Note we do this
4158a1b9b6aSSam Leffler * after checking the device capabilities so the
4168a1b9b6aSSam Leffler * cipher module can optimize space usage based on
4178a1b9b6aSSam Leffler * whether or not it needs to do the cipher work.
4188a1b9b6aSSam Leffler */
419dd70e17bSSam Leffler if (key->wk_cipher != cip || key->wk_flags != flags) {
420dd70e17bSSam Leffler /*
421dd70e17bSSam Leffler * Fillin the flags so cipher modules can see s/w
422dd70e17bSSam Leffler * crypto requirements and potentially allocate
423dd70e17bSSam Leffler * different state and/or attach different method
424dd70e17bSSam Leffler * pointers.
425dd70e17bSSam Leffler */
426dd70e17bSSam Leffler key->wk_flags = flags;
427b032f27cSSam Leffler keyctx = cip->ic_attach(vap, key);
4288a1b9b6aSSam Leffler if (keyctx == NULL) {
429b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
4308a1b9b6aSSam Leffler "%s: unable to attach cipher %s\n",
4318a1b9b6aSSam Leffler __func__, cip->ic_name);
4328a1b9b6aSSam Leffler key->wk_flags = oflags; /* restore old flags */
433b032f27cSSam Leffler vap->iv_stats.is_crypto_attachfail++;
4348a1b9b6aSSam Leffler return 0;
4358a1b9b6aSSam Leffler }
4368a1b9b6aSSam Leffler cipher_detach(key);
4378a1b9b6aSSam Leffler key->wk_cipher = cip; /* XXX refcnt? */
4388a1b9b6aSSam Leffler key->wk_private = keyctx;
4398a1b9b6aSSam Leffler }
4408a1b9b6aSSam Leffler
4418a1b9b6aSSam Leffler /*
4428a1b9b6aSSam Leffler * Ask the driver for a key index if we don't have one.
4438a1b9b6aSSam Leffler * Note that entries in the global key table always have
4448a1b9b6aSSam Leffler * an index; this means it's safe to call this routine
4458a1b9b6aSSam Leffler * for these entries just to setup the reference to the
4468a1b9b6aSSam Leffler * cipher template. Note also that when using software
4478a1b9b6aSSam Leffler * crypto we also call the driver to give us a key index.
4488a1b9b6aSSam Leffler */
449e6e547d5SSam Leffler if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
450b032f27cSSam Leffler if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
4518a1b9b6aSSam Leffler /*
452e6e547d5SSam Leffler * Unable to setup driver state.
4538a1b9b6aSSam Leffler */
454b032f27cSSam Leffler vap->iv_stats.is_crypto_keyfail++;
455b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
4568a1b9b6aSSam Leffler "%s: unable to setup cipher %s\n",
4578a1b9b6aSSam Leffler __func__, cip->ic_name);
4588a1b9b6aSSam Leffler return 0;
4598a1b9b6aSSam Leffler }
460e6e547d5SSam Leffler if (key->wk_flags != flags) {
461e6e547d5SSam Leffler /*
462e6e547d5SSam Leffler * Driver overrode flags we setup; typically because
463e6e547d5SSam Leffler * resources were unavailable to handle _this_ key.
464e6e547d5SSam Leffler * Re-attach the cipher context to allow cipher
465e6e547d5SSam Leffler * modules to handle differing requirements.
466e6e547d5SSam Leffler */
467e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
468e6e547d5SSam Leffler "%s: driver override for cipher %s, flags "
469e6e547d5SSam Leffler "0x%x -> 0x%x\n", __func__, cip->ic_name,
470e6e547d5SSam Leffler oflags, key->wk_flags);
471e6e547d5SSam Leffler keyctx = cip->ic_attach(vap, key);
472e6e547d5SSam Leffler if (keyctx == NULL) {
473e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
474e6e547d5SSam Leffler "%s: unable to attach cipher %s with "
475e6e547d5SSam Leffler "flags 0x%x\n", __func__, cip->ic_name,
476e6e547d5SSam Leffler key->wk_flags);
477e6e547d5SSam Leffler key->wk_flags = oflags; /* restore old flags */
478e6e547d5SSam Leffler vap->iv_stats.is_crypto_attachfail++;
479e6e547d5SSam Leffler return 0;
480e6e547d5SSam Leffler }
481e6e547d5SSam Leffler cipher_detach(key);
482e6e547d5SSam Leffler key->wk_cipher = cip; /* XXX refcnt? */
483e6e547d5SSam Leffler key->wk_private = keyctx;
484e6e547d5SSam Leffler }
485c1225b52SSam Leffler key->wk_keyix = keyix;
486c1225b52SSam Leffler key->wk_rxkeyix = rxkeyix;
487e6e547d5SSam Leffler key->wk_flags |= IEEE80211_KEY_DEVKEY;
4888a1b9b6aSSam Leffler }
4898a1b9b6aSSam Leffler return 1;
4908a1b9b6aSSam Leffler }
4918a1b9b6aSSam Leffler
4928a1b9b6aSSam Leffler /*
4938a1b9b6aSSam Leffler * Remove the key (no locking, for internal use).
4948a1b9b6aSSam Leffler */
4958a1b9b6aSSam Leffler static int
_ieee80211_crypto_delkey(struct ieee80211vap * vap,struct ieee80211_key * key)496b032f27cSSam Leffler _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
4978a1b9b6aSSam Leffler {
4988a1b9b6aSSam Leffler KASSERT(key->wk_cipher != NULL, ("No cipher!"));
4998a1b9b6aSSam Leffler
500b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
501fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
502fc508e8eSSam Leffler __func__, key->wk_cipher->ic_name,
503fc508e8eSSam Leffler key->wk_keyix, key->wk_flags,
504b032f27cSSam Leffler key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
505b032f27cSSam Leffler key->wk_keylen);
506fc508e8eSSam Leffler
507e6e547d5SSam Leffler if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
5088a1b9b6aSSam Leffler /*
5098a1b9b6aSSam Leffler * Remove hardware entry.
5108a1b9b6aSSam Leffler */
5118a1b9b6aSSam Leffler /* XXX key cache */
512b032f27cSSam Leffler if (!dev_key_delete(vap, key)) {
513b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
5148a1b9b6aSSam Leffler "%s: driver did not delete key index %u\n",
515e6e547d5SSam Leffler __func__, key->wk_keyix);
516b032f27cSSam Leffler vap->iv_stats.is_crypto_delkey++;
5178a1b9b6aSSam Leffler /* XXX recovery? */
5188a1b9b6aSSam Leffler }
5198a1b9b6aSSam Leffler }
5208a1b9b6aSSam Leffler cipher_detach(key);
5218a1b9b6aSSam Leffler memset(key, 0, sizeof(*key));
522b032f27cSSam Leffler ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
5238a1b9b6aSSam Leffler return 1;
5248a1b9b6aSSam Leffler }
5258a1b9b6aSSam Leffler
5268a1b9b6aSSam Leffler /*
5278a1b9b6aSSam Leffler * Remove the specified key.
5288a1b9b6aSSam Leffler */
5298a1b9b6aSSam Leffler int
ieee80211_crypto_delkey(struct ieee80211vap * vap,struct ieee80211_key * key)530b032f27cSSam Leffler ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
5318a1b9b6aSSam Leffler {
5328a1b9b6aSSam Leffler int status;
5338a1b9b6aSSam Leffler
534b032f27cSSam Leffler ieee80211_key_update_begin(vap);
535b032f27cSSam Leffler status = _ieee80211_crypto_delkey(vap, key);
536b032f27cSSam Leffler ieee80211_key_update_end(vap);
5378a1b9b6aSSam Leffler return status;
5388a1b9b6aSSam Leffler }
5398a1b9b6aSSam Leffler
5408a1b9b6aSSam Leffler /*
5418a1b9b6aSSam Leffler * Clear the global key table.
5428a1b9b6aSSam Leffler */
5438a1b9b6aSSam Leffler void
ieee80211_crypto_delglobalkeys(struct ieee80211vap * vap)544b032f27cSSam Leffler ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
5458a1b9b6aSSam Leffler {
5468a1b9b6aSSam Leffler int i;
5478a1b9b6aSSam Leffler
548b032f27cSSam Leffler ieee80211_key_update_begin(vap);
5498a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++)
550b032f27cSSam Leffler (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
551b032f27cSSam Leffler ieee80211_key_update_end(vap);
5528a1b9b6aSSam Leffler }
5538a1b9b6aSSam Leffler
5548a1b9b6aSSam Leffler /*
5558a1b9b6aSSam Leffler * Set the contents of the specified key.
5568a1b9b6aSSam Leffler *
5578a1b9b6aSSam Leffler * Locking must be handled by the caller using:
558b032f27cSSam Leffler * ieee80211_key_update_begin(vap);
559b032f27cSSam Leffler * ieee80211_key_update_end(vap);
5608a1b9b6aSSam Leffler */
5618a1b9b6aSSam Leffler int
ieee80211_crypto_setkey(struct ieee80211vap * vap,struct ieee80211_key * key)56271fe06caSSam Leffler ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
5638a1b9b6aSSam Leffler {
5648a1b9b6aSSam Leffler const struct ieee80211_cipher *cip = key->wk_cipher;
5658a1b9b6aSSam Leffler
5668a1b9b6aSSam Leffler KASSERT(cip != NULL, ("No cipher!"));
5678a1b9b6aSSam Leffler
568b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
569fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
570fc508e8eSSam Leffler __func__, cip->ic_name, key->wk_keyix,
57171fe06caSSam Leffler key->wk_flags, ether_sprintf(key->wk_macaddr),
572b032f27cSSam Leffler key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
573b032f27cSSam Leffler key->wk_keylen);
574fc508e8eSSam Leffler
575e6e547d5SSam Leffler if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
576e6e547d5SSam Leffler /* XXX nothing allocated, should not happen */
577e6e547d5SSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
578e6e547d5SSam Leffler "%s: no device key setup done; should not happen!\n",
579e6e547d5SSam Leffler __func__);
580e6e547d5SSam Leffler vap->iv_stats.is_crypto_setkey_nokey++;
581e6e547d5SSam Leffler return 0;
582e6e547d5SSam Leffler }
5838a1b9b6aSSam Leffler /*
5848a1b9b6aSSam Leffler * Give cipher a chance to validate key contents.
5858a1b9b6aSSam Leffler * XXX should happen before modifying state.
5868a1b9b6aSSam Leffler */
5878a1b9b6aSSam Leffler if (!cip->ic_setkey(key)) {
588b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
5898a1b9b6aSSam Leffler "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
5908a1b9b6aSSam Leffler __func__, cip->ic_name, key->wk_keyix,
5918a1b9b6aSSam Leffler key->wk_keylen, key->wk_flags);
592b032f27cSSam Leffler vap->iv_stats.is_crypto_setkey_cipher++;
5938a1b9b6aSSam Leffler return 0;
5948a1b9b6aSSam Leffler }
59571fe06caSSam Leffler return dev_key_set(vap, key);
5968a1b9b6aSSam Leffler }
5978a1b9b6aSSam Leffler
59854a95d0dSAdrian Chadd /*
59954a95d0dSAdrian Chadd * Return index if the key is a WEP key (0..3); -1 otherwise.
60054a95d0dSAdrian Chadd *
60154a95d0dSAdrian Chadd * This is different to "get_keyid" which defaults to returning
60254a95d0dSAdrian Chadd * 0 for unicast keys; it assumes that it won't be used for WEP.
60354a95d0dSAdrian Chadd */
60454a95d0dSAdrian Chadd int
ieee80211_crypto_get_key_wepidx(const struct ieee80211vap * vap,const struct ieee80211_key * k)60554a95d0dSAdrian Chadd ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap,
60654a95d0dSAdrian Chadd const struct ieee80211_key *k)
60754a95d0dSAdrian Chadd {
60854a95d0dSAdrian Chadd
609*2589197aSAdrian Chadd if (ieee80211_is_key_global(vap, k)) {
61054a95d0dSAdrian Chadd return (k - vap->iv_nw_keys);
611*2589197aSAdrian Chadd }
612d0155f67SXin LI return (-1);
61354a95d0dSAdrian Chadd }
61454a95d0dSAdrian Chadd
61554a95d0dSAdrian Chadd /*
61654a95d0dSAdrian Chadd * Note: only supports a single unicast key (0).
61754a95d0dSAdrian Chadd */
618ef0d8f63SAdrian Chadd uint8_t
ieee80211_crypto_get_keyid(struct ieee80211vap * vap,struct ieee80211_key * k)619ef0d8f63SAdrian Chadd ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k)
620ef0d8f63SAdrian Chadd {
621*2589197aSAdrian Chadd if (ieee80211_is_key_global(vap, k)) {
622ef0d8f63SAdrian Chadd return (k - vap->iv_nw_keys);
623*2589197aSAdrian Chadd }
624*2589197aSAdrian Chadd
625ef0d8f63SAdrian Chadd return (0);
626ef0d8f63SAdrian Chadd }
627ef0d8f63SAdrian Chadd
6288a1b9b6aSSam Leffler struct ieee80211_key *
ieee80211_crypto_get_txkey(struct ieee80211_node * ni,struct mbuf * m)62915395998SAdrian Chadd ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m)
6308a1b9b6aSSam Leffler {
631b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap;
6328a1b9b6aSSam Leffler struct ieee80211_frame *wh;
6338a1b9b6aSSam Leffler
6348a1b9b6aSSam Leffler /*
6358a1b9b6aSSam Leffler * Multicast traffic always uses the multicast key.
63661605e0aSdomienschepers *
63761605e0aSdomienschepers * Historically we would fall back to the default
63861605e0aSdomienschepers * transmit key if there was no unicast key. This
63961605e0aSdomienschepers * behaviour was documented up to IEEE Std 802.11-2016,
64061605e0aSdomienschepers * 12.9.2.2 Per-MSDU/Per-A-MSDU Tx pseudocode, in the
64161605e0aSdomienschepers * 'else' case but is no longer in later versions of
64261605e0aSdomienschepers * the standard. Additionally falling back to the
64361605e0aSdomienschepers * group key for unicast was a security risk.
6448a1b9b6aSSam Leffler */
6458a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *);
64661605e0aSdomienschepers if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
647b032f27cSSam Leffler if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
648b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
649b032f27cSSam Leffler wh->i_addr1,
650b032f27cSSam Leffler "no default transmit key (%s) deftxkey %u",
651b032f27cSSam Leffler __func__, vap->iv_def_txkey);
652b032f27cSSam Leffler vap->iv_stats.is_tx_nodefkey++;
6538a1b9b6aSSam Leffler return NULL;
6548a1b9b6aSSam Leffler }
65515395998SAdrian Chadd return &vap->iv_nw_keys[vap->iv_def_txkey];
65615395998SAdrian Chadd }
657ef0d8f63SAdrian Chadd
65861605e0aSdomienschepers if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
65961605e0aSdomienschepers return NULL;
66015395998SAdrian Chadd return &ni->ni_ucastkey;
66115395998SAdrian Chadd }
66215395998SAdrian Chadd
66315395998SAdrian Chadd /*
66415395998SAdrian Chadd * Add privacy headers appropriate for the specified key.
66515395998SAdrian Chadd */
66615395998SAdrian Chadd struct ieee80211_key *
ieee80211_crypto_encap(struct ieee80211_node * ni,struct mbuf * m)66715395998SAdrian Chadd ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
66815395998SAdrian Chadd {
66915395998SAdrian Chadd struct ieee80211_key *k;
67015395998SAdrian Chadd const struct ieee80211_cipher *cip;
67115395998SAdrian Chadd
67215395998SAdrian Chadd if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) {
6738a1b9b6aSSam Leffler cip = k->wk_cipher;
674ef0d8f63SAdrian Chadd return (cip->ic_encap(k, m) ? k : NULL);
6758a1b9b6aSSam Leffler }
6768a1b9b6aSSam Leffler
67715395998SAdrian Chadd return NULL;
67815395998SAdrian Chadd }
67915395998SAdrian Chadd
6808a1b9b6aSSam Leffler /*
6818a1b9b6aSSam Leffler * Validate and strip privacy headers (and trailer) for a
6828a1b9b6aSSam Leffler * received frame that has the WEP/Privacy bit set.
6838a1b9b6aSSam Leffler */
684fe75b452SAdrian Chadd int
ieee80211_crypto_decap(struct ieee80211_node * ni,struct mbuf * m,int hdrlen,struct ieee80211_key ** key)685fe75b452SAdrian Chadd ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen,
686fe75b452SAdrian Chadd struct ieee80211_key **key)
6878a1b9b6aSSam Leffler {
6888a1b9b6aSSam Leffler #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
6898a1b9b6aSSam Leffler #define IEEE80211_WEP_MINLEN \
690a92c6eb0SSam Leffler (sizeof(struct ieee80211_frame) + \
6918a1b9b6aSSam Leffler IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
692b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap;
6938a1b9b6aSSam Leffler struct ieee80211_key *k;
6948a1b9b6aSSam Leffler struct ieee80211_frame *wh;
695fe75b452SAdrian Chadd const struct ieee80211_rx_stats *rxs;
6968a1b9b6aSSam Leffler const struct ieee80211_cipher *cip;
69768e8e04eSSam Leffler uint8_t keyid;
6988a1b9b6aSSam Leffler
699fe75b452SAdrian Chadd /*
700fe75b452SAdrian Chadd * Check for hardware decryption and IV stripping.
701fe75b452SAdrian Chadd * If the IV is stripped then we definitely can't find a key.
702fe75b452SAdrian Chadd * Set the key to NULL but return true; upper layers
703fe75b452SAdrian Chadd * will need to handle a NULL key for a successful
704fe75b452SAdrian Chadd * decrypt.
705fe75b452SAdrian Chadd */
706fe75b452SAdrian Chadd rxs = ieee80211_get_rx_params_ptr(m);
707fe75b452SAdrian Chadd if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
708fe75b452SAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) {
709fe75b452SAdrian Chadd /*
710fe75b452SAdrian Chadd * Hardware decrypted, IV stripped.
711fe75b452SAdrian Chadd * We can't find a key with a stripped IV.
712fe75b452SAdrian Chadd * Return successful.
713fe75b452SAdrian Chadd */
714fe75b452SAdrian Chadd *key = NULL;
715fe75b452SAdrian Chadd return (1);
716fe75b452SAdrian Chadd }
717fe75b452SAdrian Chadd }
718fe75b452SAdrian Chadd
7198a1b9b6aSSam Leffler /* NB: this minimum size data frame could be bigger */
7208a1b9b6aSSam Leffler if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
721b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
7228a1b9b6aSSam Leffler "%s: WEP data frame too short, len %u\n",
7238a1b9b6aSSam Leffler __func__, m->m_pkthdr.len);
724b032f27cSSam Leffler vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */
725fe75b452SAdrian Chadd *key = NULL;
726fe75b452SAdrian Chadd return (0);
7271a1e1d21SSam Leffler }
7281a1e1d21SSam Leffler
7291a1e1d21SSam Leffler /*
7308a1b9b6aSSam Leffler * Locate the key. If unicast and there is no unicast
7318a1b9b6aSSam Leffler * key then we fall back to the key id in the header.
7328a1b9b6aSSam Leffler * This assumes unicast keys are only configured when
7338a1b9b6aSSam Leffler * the key id in the header is meaningless (typically 0).
7341a1e1d21SSam Leffler */
7358a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *);
736b032f27cSSam Leffler m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
7378a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
738cda15ce1SSam Leffler IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
739b032f27cSSam Leffler k = &vap->iv_nw_keys[keyid >> 6];
7401a1e1d21SSam Leffler else
7418a1b9b6aSSam Leffler k = &ni->ni_ucastkey;
7421a1e1d21SSam Leffler
7431a1e1d21SSam Leffler /*
74479e0962dSAndriy Voskoboinyk * Insure crypto header is contiguous and long enough for all
74579e0962dSAndriy Voskoboinyk * decap work.
7461a1e1d21SSam Leffler */
7478a1b9b6aSSam Leffler cip = k->wk_cipher;
74879e0962dSAndriy Voskoboinyk if (m->m_len < hdrlen + cip->ic_header) {
749b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
75079e0962dSAndriy Voskoboinyk "frame is too short (%d < %u) for crypto decap",
75179e0962dSAndriy Voskoboinyk cip->ic_name, m->m_len, hdrlen + cip->ic_header);
75279e0962dSAndriy Voskoboinyk vap->iv_stats.is_rx_tooshort++;
753fe75b452SAdrian Chadd *key = NULL;
754fe75b452SAdrian Chadd return (0);
7558a1b9b6aSSam Leffler }
7561a1e1d21SSam Leffler
757fe75b452SAdrian Chadd /*
758fe75b452SAdrian Chadd * Attempt decryption.
759fe75b452SAdrian Chadd *
760fe75b452SAdrian Chadd * If we fail then don't return the key - return NULL
761fe75b452SAdrian Chadd * and an error.
762fe75b452SAdrian Chadd */
763fe75b452SAdrian Chadd if (cip->ic_decap(k, m, hdrlen)) {
764fe75b452SAdrian Chadd /* success */
765fe75b452SAdrian Chadd *key = k;
766fe75b452SAdrian Chadd return (1);
767fe75b452SAdrian Chadd }
768fe75b452SAdrian Chadd
769fe75b452SAdrian Chadd /* Failure */
770fe75b452SAdrian Chadd *key = NULL;
771fe75b452SAdrian Chadd return (0);
7728a1b9b6aSSam Leffler #undef IEEE80211_WEP_MINLEN
7738a1b9b6aSSam Leffler #undef IEEE80211_WEP_HDRLEN
7741a1e1d21SSam Leffler }
77506b2d888SSam Leffler
776ee9d294bSAdrian Chadd /*
777ee9d294bSAdrian Chadd * Check and remove any MIC.
778ee9d294bSAdrian Chadd */
779ee9d294bSAdrian Chadd int
ieee80211_crypto_demic(struct ieee80211vap * vap,struct ieee80211_key * k,struct mbuf * m,int force)780ee9d294bSAdrian Chadd ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
781ee9d294bSAdrian Chadd struct mbuf *m, int force)
782ee9d294bSAdrian Chadd {
783ee9d294bSAdrian Chadd const struct ieee80211_cipher *cip;
784ee9d294bSAdrian Chadd const struct ieee80211_rx_stats *rxs;
785ee9d294bSAdrian Chadd struct ieee80211_frame *wh;
786ee9d294bSAdrian Chadd
787ee9d294bSAdrian Chadd rxs = ieee80211_get_rx_params_ptr(m);
788ee9d294bSAdrian Chadd wh = mtod(m, struct ieee80211_frame *);
789ee9d294bSAdrian Chadd
790ee9d294bSAdrian Chadd /*
791ee9d294bSAdrian Chadd * Handle demic / mic errors from hardware-decrypted offload devices.
792ee9d294bSAdrian Chadd */
793ee9d294bSAdrian Chadd if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
794ee9d294bSAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) {
795ee9d294bSAdrian Chadd /*
796ee9d294bSAdrian Chadd * Hardware has said MIC failed. We don't care about
797ee9d294bSAdrian Chadd * whether it was stripped or not.
798ee9d294bSAdrian Chadd *
799ee9d294bSAdrian Chadd * Eventually - teach the demic methods in crypto
800ee9d294bSAdrian Chadd * modules to handle a NULL key and not to dereference
801ee9d294bSAdrian Chadd * it.
802ee9d294bSAdrian Chadd */
803ee9d294bSAdrian Chadd ieee80211_notify_michael_failure(vap, wh, -1);
804ee9d294bSAdrian Chadd return (0);
805ee9d294bSAdrian Chadd }
806ee9d294bSAdrian Chadd
807ee9d294bSAdrian Chadd if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) {
808ee9d294bSAdrian Chadd /*
809ee9d294bSAdrian Chadd * Hardware has decrypted and not indicated a
810ee9d294bSAdrian Chadd * MIC failure and has stripped the MIC.
811ee9d294bSAdrian Chadd * We may not have a key, so for now just
812ee9d294bSAdrian Chadd * return OK.
813ee9d294bSAdrian Chadd */
814ee9d294bSAdrian Chadd return (1);
815ee9d294bSAdrian Chadd }
816ee9d294bSAdrian Chadd }
817ee9d294bSAdrian Chadd
818ee9d294bSAdrian Chadd /*
819ee9d294bSAdrian Chadd * If we don't have a key at this point then we don't
820ee9d294bSAdrian Chadd * have to demic anything.
821ee9d294bSAdrian Chadd */
822ee9d294bSAdrian Chadd if (k == NULL)
823ee9d294bSAdrian Chadd return (1);
824ee9d294bSAdrian Chadd
825ee9d294bSAdrian Chadd cip = k->wk_cipher;
826ee9d294bSAdrian Chadd return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
827ee9d294bSAdrian Chadd }
828ee9d294bSAdrian Chadd
82906b2d888SSam Leffler static void
load_ucastkey(void * arg,struct ieee80211_node * ni)83006b2d888SSam Leffler load_ucastkey(void *arg, struct ieee80211_node *ni)
83106b2d888SSam Leffler {
83206b2d888SSam Leffler struct ieee80211vap *vap = ni->ni_vap;
83306b2d888SSam Leffler struct ieee80211_key *k;
83406b2d888SSam Leffler
83506b2d888SSam Leffler if (vap->iv_state != IEEE80211_S_RUN)
83606b2d888SSam Leffler return;
83706b2d888SSam Leffler k = &ni->ni_ucastkey;
838e6e547d5SSam Leffler if (k->wk_flags & IEEE80211_KEY_DEVKEY)
83906b2d888SSam Leffler dev_key_set(vap, k);
84006b2d888SSam Leffler }
84106b2d888SSam Leffler
84206b2d888SSam Leffler /*
84306b2d888SSam Leffler * Re-load all keys known to the 802.11 layer that may
84406b2d888SSam Leffler * have hardware state backing them. This is used by
84506b2d888SSam Leffler * drivers on resume to push keys down into the device.
84606b2d888SSam Leffler */
84706b2d888SSam Leffler void
ieee80211_crypto_reload_keys(struct ieee80211com * ic)84806b2d888SSam Leffler ieee80211_crypto_reload_keys(struct ieee80211com *ic)
84906b2d888SSam Leffler {
85006b2d888SSam Leffler struct ieee80211vap *vap;
85106b2d888SSam Leffler int i;
85206b2d888SSam Leffler
85306b2d888SSam Leffler /*
85406b2d888SSam Leffler * Keys in the global key table of each vap.
85506b2d888SSam Leffler */
85606b2d888SSam Leffler /* NB: used only during resume so don't lock for now */
85706b2d888SSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
85806b2d888SSam Leffler if (vap->iv_state != IEEE80211_S_RUN)
85906b2d888SSam Leffler continue;
86006b2d888SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) {
86106b2d888SSam Leffler const struct ieee80211_key *k = &vap->iv_nw_keys[i];
862e6e547d5SSam Leffler if (k->wk_flags & IEEE80211_KEY_DEVKEY)
86306b2d888SSam Leffler dev_key_set(vap, k);
86406b2d888SSam Leffler }
86506b2d888SSam Leffler }
86606b2d888SSam Leffler /*
86706b2d888SSam Leffler * Unicast keys.
86806b2d888SSam Leffler */
86906b2d888SSam Leffler ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
87006b2d888SSam Leffler }
871781487cfSAdrian Chadd
872781487cfSAdrian Chadd /*
873781487cfSAdrian Chadd * Set the default key index for WEP, or KEYIX_NONE for no default TX key.
874781487cfSAdrian Chadd *
875781487cfSAdrian Chadd * This should be done as part of a key update block (iv_key_update_begin /
876781487cfSAdrian Chadd * iv_key_update_end.)
877781487cfSAdrian Chadd */
878781487cfSAdrian Chadd void
ieee80211_crypto_set_deftxkey(struct ieee80211vap * vap,ieee80211_keyix kid)879781487cfSAdrian Chadd ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
880781487cfSAdrian Chadd {
881781487cfSAdrian Chadd
882781487cfSAdrian Chadd /* XXX TODO: assert we're in a key update block */
883781487cfSAdrian Chadd
884781487cfSAdrian Chadd vap->iv_update_deftxkey(vap, kid);
885781487cfSAdrian Chadd }
886